Fixes from clang / scan-build
[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 ((rc = otp_write(fdp, (const char *) request, sizeof(*request))) != sizeof(*request)) {    if (rc == 0)
210       goto retry;       /* otpd disconnect */   /*TODO: pause */
211     else
212       return -1;
213   }
214
215   if ((rc = otp_read(fdp, (char *) reply, sizeof(*reply))) != sizeof(*reply)) {
216     if (rc == 0)
217       goto retry;       /* otpd disconnect */   /*TODO: pause */
218     else
219       return -1;
220   }
221
222   /* validate the reply */
223   if (reply->version != 1) {
224     (void) radlog(L_AUTH, "rlm_otp: otpd reply for [%s] invalid "
225                           "(version %d != 1)",
226                   request->username, reply->version);
227     otp_putfd(fdp, 1);
228     return -1;
229   }
230
231   if (reply->passcode[OTP_MAX_PASSCODE_LEN] != '\0') {
232     (void) radlog(L_AUTH, "rlm_otp: otpd reply for [%s] invalid (passcode)",
233                   request->username);
234     otp_putfd(fdp, 1);
235     return -1;
236   }
237
238   otp_putfd(fdp, 0);
239   return reply->rc;
240 }
241
242 /*
243  * Full read with logging, and close on failure.
244  * Returns nread on success, 0 on EOF, -1 on other failures.
245  */
246 static int
247 otp_read(otp_fd_t *fdp, char *buf, size_t len)
248 {
249   ssize_t n;
250   size_t nread = 0;     /* bytes read into buf */
251
252   while (nread < len) {
253     if ((n = read(fdp->fd, &buf[nread], len - nread)) == -1) {
254       if (errno == EINTR) {
255         continue;
256       } else {
257         (void) radlog(L_ERR, "rlm_otp: %s: read from otpd: %s",
258                       __func__, strerror(errno));
259         otp_putfd(fdp, 1);
260         return -1;
261       }
262     }
263     if (!n) {
264       (void) radlog(L_ERR, "rlm_otp: %s: otpd disconnect", __func__);
265       otp_putfd(fdp, 1);
266       return 0;
267     }
268     nread += n;
269   } /* while (more to read) */
270
271   return nread;
272 }
273
274 /*
275  * Full write with logging, and close on failure.
276  * Returns 0 on success, errno on failure.
277  */
278 static int
279 otp_write(otp_fd_t *fdp, const char *buf, size_t len)
280 {
281   size_t nleft = len;
282   ssize_t nwrote;
283
284   while (nleft) {
285     if ((nwrote = write(fdp->fd, &buf[len - nleft], nleft)) == -1) {
286       if (errno == EINTR) {
287         continue;
288       } else {
289         (void) radlog(L_ERR, "rlm_otp: %s: write to otpd: %s",
290                       __func__, strerror(errno));
291         otp_putfd(fdp, 1);
292         return errno;
293       }
294     }
295     nleft -= nwrote;
296   }
297
298   return 0;
299 }
300
301 /* connect to otpd and return fd */
302 static int
303 otp_connect(const char *path)
304 {
305   int fd;
306   struct sockaddr_un sa;
307   size_t sp_len;                /* sun_path length (strlen) */
308
309   /* setup for unix domain socket */
310   sp_len = strlen(path);
311   if (sp_len > sizeof(sa.sun_path) - 1) {
312     (void) radlog(L_ERR, "rlm_otp: %s: rendezvous point name too long",
313                   __func__);
314     return -1;
315   }
316   sa.sun_family = AF_UNIX;
317   (void) strcpy(sa.sun_path, path);
318
319   /* connect to otpd */
320   if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
321     (void) radlog(L_ERR, "rlm_otp: %s: socket: %s", __func__, strerror(errno));
322     return -1;
323   }
324   if (connect(fd, (struct sockaddr *) &sa,
325               sizeof(sa.sun_family) + sp_len) == -1) {
326     (void) radlog(L_ERR, "rlm_otp: %s: connect(%s): %s",
327                   __func__, path, strerror(errno));
328     (void) close(fd);
329     return -1;
330   }
331   return fd;
332 }
333
334 /*
335  * Retrieve an fd (from pool) to use for otpd connection.
336  * It'd be simpler to use TLS but FR can have lots of threads
337  * and we don't want to waste fd's that way.
338  * We can't have a global fd because we'd then be pipelining
339  * requests to otpd and we have no way to demultiplex
340  * the responses.
341  */
342 static otp_fd_t *
343 otp_getfd(const otp_option_t *opt)
344 {
345   int rc;
346   otp_fd_t *fdp;
347
348   /* walk the connection pool looking for an available fd */
349   for (fdp = otp_fd_head; fdp; fdp = fdp->next) {
350     rc = otp_pthread_mutex_trylock(&fdp->mutex);
351     if (!rc)
352       if (!strcmp(fdp->path, opt->otpd_rp))     /* could just use == */
353         break;
354   }
355
356   if (!fdp) {
357     /* no fd was available, add a new one */
358     fdp = rad_malloc(sizeof(*fdp));
359     otp_pthread_mutex_init(&fdp->mutex, NULL);
360     otp_pthread_mutex_lock(&fdp->mutex);
361     /* insert new fd at head */
362     otp_pthread_mutex_lock(&otp_fd_head_mutex);
363     fdp->next = otp_fd_head;
364     otp_fd_head = fdp;
365     otp_pthread_mutex_unlock(&otp_fd_head_mutex);
366     /* initialize */
367     fdp->path = opt->otpd_rp;
368     fdp->fd = -1;
369   }
370
371   /* establish connection */
372   if (fdp->fd == -1)
373     fdp->fd = otp_connect(fdp->path);
374
375   return fdp;
376 }
377
378 /* release fd, and optionally disconnect from otpd */
379 static void
380 otp_putfd(otp_fd_t *fdp, int disconnect)
381 {
382   if (disconnect) {
383     (void) close(fdp->fd);
384     fdp->fd = -1;
385   }
386
387   /* make connection available to another thread */
388   otp_pthread_mutex_unlock(&fdp->mutex);
389 }