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