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