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