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