Retry on write failure. Closes bug #58
[freeradius.git] / src / modules / rlm_otp / otp_pw_valid.c
1 /*
2  * $Id$
3  *
4  * Passcode verification function (otpd client) for rlm_otp.
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  *
21  * Copyright 2006,2007 TRI-D Systems, Inc.
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29
30 #include "extern.h"
31 #include "otp.h"
32 #include "otp_pw_valid.h"
33
34 #ifdef HAVE_PTHREAD_H
35 #include <pthread.h>
36 #endif
37 #include <sys/un.h>
38
39
40 /* transform otpd return codes into rlm return codes */
41 static int
42 otprc2rlmrc(int rc)
43 {
44   switch (rc) {
45     case OTP_RC_OK:                     return RLM_MODULE_OK;
46     case OTP_RC_USER_UNKNOWN:           return RLM_MODULE_REJECT;
47     case OTP_RC_AUTHINFO_UNAVAIL:       return RLM_MODULE_REJECT;
48     case OTP_RC_AUTH_ERR:               return RLM_MODULE_REJECT;
49     case OTP_RC_MAXTRIES:               return RLM_MODULE_USERLOCK;
50     case OTP_RC_NEXTPASSCODE:           return RLM_MODULE_USERLOCK;
51     case OTP_RC_IPIN:                   return RLM_MODULE_REJECT;
52     case OTP_RC_SERVICE_ERR:            return RLM_MODULE_FAIL;
53     default:                            return RLM_MODULE_FAIL;
54   }
55 }
56
57 static otp_fd_t *otp_fd_head;
58 static pthread_mutex_t otp_fd_head_mutex = PTHREAD_MUTEX_INITIALIZER;
59
60 /*
61  * Test for passcode validity by asking otpd.
62  *
63  * If challenge is supplied, it is used to generate the card response
64  * against which the passcode will be compared.  If challenge is not
65  * supplied, or if the comparison fails, synchronous responses are
66  * generated and tested.  NOTE: for async authentications, sync mode
67  * responses are still considered valid!  (Assuming module configuration
68  * allows sync mode.)
69  *
70  * Returns one of the RLM_MODULE_* codes.  passcode is filled in.
71  * NB: The returned passcode will contain the PIN!  DO NOT LOG!
72  */
73 int
74 otp_pw_valid(REQUEST *request, int pwe, const char *challenge,
75              const otp_option_t *opt, char passcode[OTP_MAX_PASSCODE_LEN + 1])
76 {
77   otp_request_t otp_request;
78   otp_reply_t   otp_reply;
79   VALUE_PAIR    *cvp, *rvp;
80   char          *username = request->username->vp_strvalue;
81   int           rc;
82
83   if (request->username->length > OTP_MAX_USERNAME_LEN) {
84     (void) radlog(L_AUTH, "rlm_otp: username [%s] too long", username);
85     return RLM_MODULE_REJECT;
86   }
87   /* we already know challenge is short enough */
88
89   otp_request.version = 2;
90   (void) strcpy(otp_request.username, username);
91   (void) strcpy(otp_request.challenge, challenge);
92   otp_request.pwe.pwe = pwe;
93
94   /* otp_pwe_present() (done by caller) guarantees that both of these exist */
95   cvp = pairfind(request->packet->vps, pwattr[pwe - 1]->attr,  pwattr[pwe - 1]->vendor);
96   rvp = pairfind(request->packet->vps, pwattr[pwe]->attr, pwattr[pwe]->vendor);
97   /* this is just to quiet Coverity */
98   if (!rvp || !cvp)
99     return RLM_MODULE_REJECT;
100
101   /*
102    * Validate available vps based on pwe type.
103    * Unfortunately (?) otpd must do this also.
104    */
105   switch (otp_request.pwe.pwe) {
106   case PWE_PAP:
107     if (rvp->length > OTP_MAX_PASSCODE_LEN) {
108       (void) radlog(L_AUTH, "rlm_otp: passcode for [%s] too long", username);
109       return RLM_MODULE_REJECT;
110     }
111     (void) strcpy(otp_request.pwe.u.pap.passcode, rvp->vp_strvalue);
112     break;
113
114   case PWE_CHAP:
115     if (cvp->length > 16) {
116       (void) radlog(L_AUTH, "rlm_otp: CHAP challenge for [%s] too long",
117                     username);
118       return RLM_MODULE_INVALID;
119     }
120     if (rvp->length != 17) {
121       (void) radlog(L_AUTH, "rlm_otp: CHAP response for [%s] wrong size",
122                     username);
123       return RLM_MODULE_INVALID;
124     }
125     (void) memcpy(otp_request.pwe.u.chap.challenge, cvp->vp_strvalue,
126                   cvp->length);
127     otp_request.pwe.u.chap.clen = cvp->length;
128     (void) memcpy(otp_request.pwe.u.chap.response, rvp->vp_strvalue,
129                   rvp->length);
130     otp_request.pwe.u.chap.rlen = rvp->length;
131     break;
132
133   case PWE_MSCHAP:
134     if (cvp->length != 8) {
135       (void) radlog(L_AUTH, "rlm_otp: MS-CHAP challenge for [%s] wrong size",
136                     username);
137       return RLM_MODULE_INVALID;
138     }
139     if (rvp->length != 50) {
140       (void) radlog(L_AUTH, "rlm_otp: MS-CHAP response for [%s] wrong size",
141                     username);
142       return RLM_MODULE_INVALID;
143     }
144     (void) memcpy(otp_request.pwe.u.chap.challenge, cvp->vp_strvalue,
145                   cvp->length);
146     otp_request.pwe.u.chap.clen = cvp->length;
147     (void) memcpy(otp_request.pwe.u.chap.response, rvp->vp_strvalue,
148                   rvp->length);
149     otp_request.pwe.u.chap.rlen = rvp->length;
150     break;
151
152   case PWE_MSCHAP2:
153     if (cvp->length != 16) {
154       (void) radlog(L_AUTH, "rlm_otp: MS-CHAP2 challenge for [%s] wrong size",
155                     username);
156       return RLM_MODULE_INVALID;
157     }
158     if (rvp->length != 50) {
159       (void) radlog(L_AUTH, "rlm_otp: MS-CHAP2 response for [%s] wrong size",
160                     username);
161       return RLM_MODULE_INVALID;
162     }
163     (void) memcpy(otp_request.pwe.u.chap.challenge, cvp->vp_strvalue,
164                   cvp->length);
165     otp_request.pwe.u.chap.clen = cvp->length;
166     (void) memcpy(otp_request.pwe.u.chap.response, rvp->vp_strvalue,
167                   rvp->length);
168     otp_request.pwe.u.chap.rlen = rvp->length;
169     break;
170   } /* switch (otp_request.pwe.pwe) */
171
172   /* last byte must also be a terminator so otpd can verify length easily */
173   otp_request.username[OTP_MAX_USERNAME_LEN] = '\0';
174   otp_request.challenge[OTP_MAX_CHALLENGE_LEN] = '\0';
175   if (otp_request.pwe.pwe == PWE_PAP)
176     otp_request.pwe.u.pap.passcode[OTP_MAX_PASSCODE_LEN] = '\0';
177
178   otp_request.allow_sync = opt->allow_sync;
179   otp_request.allow_async = opt->allow_async;
180   otp_request.challenge_delay = opt->challenge_delay;
181   otp_request.resync = 1;
182
183   rc = otp_verify(opt, &otp_request, &otp_reply);
184   if (rc == OTP_RC_OK)
185     (void) strcpy(passcode, otp_reply.passcode);
186   return otprc2rlmrc(rc);
187 }
188
189 /*
190  * Verify an otp by asking otpd.
191  * Returns an OTP_* code, or -1 on system failure.
192  * Fills in reply.
193  */
194 static int
195 otp_verify(const otp_option_t *opt,
196            const otp_request_t *request, otp_reply_t *reply)
197 {
198   otp_fd_t *fdp;
199   int rc;
200   int tryagain = 2;
201
202 retry:
203   if (!tryagain--)
204     return -1;
205   fdp = otp_getfd(opt);
206   if (!fdp || fdp->fd == -1)
207     return -1;
208
209   if (otp_write(fdp, (const char *) request, sizeof(*request)) != 0) {
210     if (rc == 0)
211       goto retry;       /* otpd disconnect */   /*TODO: pause */
212     else
213       return -1;
214   }
215
216   if ((rc = otp_read(fdp, (char *) reply, sizeof(*reply))) != sizeof(*reply)) {
217     if (rc == 0)
218       goto retry;       /* otpd disconnect */   /*TODO: pause */
219     else
220       return -1;
221   }
222
223   /* validate the reply */
224   if (reply->version != 1) {
225     (void) radlog(L_AUTH, "rlm_otp: otpd reply for [%s] invalid "
226                           "(version %d != 1)",
227                   request->username, reply->version);
228     otp_putfd(fdp, 1);
229     return -1;
230   }
231
232   if (reply->passcode[OTP_MAX_PASSCODE_LEN] != '\0') {
233     (void) radlog(L_AUTH, "rlm_otp: otpd reply for [%s] invalid (passcode)",
234                   request->username);
235     otp_putfd(fdp, 1);
236     return -1;
237   }
238
239   otp_putfd(fdp, 0);
240   return reply->rc;
241 }
242
243 /*
244  * Full read with logging, and close on failure.
245  * Returns nread on success, 0 on EOF, -1 on other failures.
246  */
247 static int
248 otp_read(otp_fd_t *fdp, char *buf, size_t len)
249 {
250   ssize_t n;
251   size_t nread = 0;     /* bytes read into buf */
252
253   while (nread < len) {
254     if ((n = read(fdp->fd, &buf[nread], len - nread)) == -1) {
255       if (errno == EINTR) {
256         continue;
257       } else {
258         (void) radlog(L_ERR, "rlm_otp: %s: read from otpd: %s",
259                       __func__, strerror(errno));
260         otp_putfd(fdp, 1);
261         return -1;
262       }
263     }
264     if (!n) {
265       (void) radlog(L_ERR, "rlm_otp: %s: otpd disconnect", __func__);
266       otp_putfd(fdp, 1);
267       return 0;
268     }
269     nread += n;
270   } /* while (more to read) */
271
272   return nread;
273 }
274
275 /*
276  * Full write with logging, and close on failure.
277  * Returns 0 on success, errno on failure.
278  */
279 static int
280 otp_write(otp_fd_t *fdp, const char *buf, size_t len)
281 {
282   size_t nleft = len;
283   ssize_t nwrote;
284
285   while (nleft) {
286     if ((nwrote = write(fdp->fd, &buf[len - nleft], nleft)) == -1) {
287       if (errno == EINTR) {
288         continue;
289       } else {
290         (void) radlog(L_ERR, "rlm_otp: %s: write to otpd: %s",
291                       __func__, strerror(errno));
292         otp_putfd(fdp, 1);
293         return errno;
294       }
295     }
296     nleft -= nwrote;
297   }
298
299   return 0;
300 }
301
302 /* connect to otpd and return fd */
303 static int
304 otp_connect(const char *path)
305 {
306   int fd;
307   struct sockaddr_un sa;
308   size_t sp_len;                /* sun_path length (strlen) */
309
310   /* setup for unix domain socket */
311   sp_len = strlen(path);
312   if (sp_len > sizeof(sa.sun_path) - 1) {
313     (void) radlog(L_ERR, "rlm_otp: %s: rendezvous point name too long",
314                   __func__);
315     return -1;
316   }
317   sa.sun_family = AF_UNIX;
318   (void) strcpy(sa.sun_path, path);
319
320   /* connect to otpd */
321   if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
322     (void) radlog(L_ERR, "rlm_otp: %s: socket: %s", __func__, strerror(errno));
323     return -1;
324   }
325   if (connect(fd, (struct sockaddr *) &sa,
326               sizeof(sa.sun_family) + sp_len) == -1) {
327     (void) radlog(L_ERR, "rlm_otp: %s: connect(%s): %s",
328                   __func__, path, strerror(errno));
329     (void) close(fd);
330     return -1;
331   }
332   return fd;
333 }
334
335 /*
336  * Retrieve an fd (from pool) to use for otpd connection.
337  * It'd be simpler to use TLS but FR can have lots of threads
338  * and we don't want to waste fd's that way.
339  * We can't have a global fd because we'd then be pipelining
340  * requests to otpd and we have no way to demultiplex
341  * the responses.
342  */
343 static otp_fd_t *
344 otp_getfd(const otp_option_t *opt)
345 {
346   int rc;
347   otp_fd_t *fdp;
348
349   /* walk the connection pool looking for an available fd */
350   for (fdp = otp_fd_head; fdp; fdp = fdp->next) {
351     rc = otp_pthread_mutex_trylock(&fdp->mutex);
352     if (!rc)
353       if (!strcmp(fdp->path, opt->otpd_rp))     /* could just use == */
354         break;
355   }
356
357   if (!fdp) {
358     /* no fd was available, add a new one */
359     fdp = rad_malloc(sizeof(*fdp));
360     otp_pthread_mutex_init(&fdp->mutex, NULL);
361     otp_pthread_mutex_lock(&fdp->mutex);
362     /* insert new fd at head */
363     otp_pthread_mutex_lock(&otp_fd_head_mutex);
364     fdp->next = otp_fd_head;
365     otp_fd_head = fdp;
366     otp_pthread_mutex_unlock(&otp_fd_head_mutex);
367     /* initialize */
368     fdp->path = opt->otpd_rp;
369     fdp->fd = -1;
370   }
371
372   /* establish connection */
373   if (fdp->fd == -1)
374     fdp->fd = otp_connect(fdp->path);
375
376   return fdp;
377 }
378
379 /* release fd, and optionally disconnect from otpd */
380 static void
381 otp_putfd(otp_fd_t *fdp, int disconnect)
382 {
383   if (disconnect) {
384     (void) close(fdp->fd);
385     fdp->fd = -1;
386   }
387
388   /* make connection available to another thread */
389   otp_pthread_mutex_unlock(&fdp->mutex);
390 }