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