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