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