4 * Passcode verification function (otpd client) for rlm_otp.
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.
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.
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
21 * Copyright 2006,2007 TRI-D Systems, Inc.
33 #include "otp_pw_valid.h"
39 #include <sys/types.h>
40 #include <sys/socket.h>
45 /* transform otpd return codes into rlm return codes */
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_NEXTPASSCODE: return RLM_MODULE_USERLOCK;
56 case OTP_RC_IPIN: return RLM_MODULE_REJECT;
57 case OTP_RC_SERVICE_ERR: return RLM_MODULE_FAIL;
58 default: return RLM_MODULE_FAIL;
62 static otp_fd_t *otp_fd_head;
63 static pthread_mutex_t otp_fd_head_mutex = PTHREAD_MUTEX_INITIALIZER;
66 * Test for passcode validity by asking otpd.
68 * If challenge is supplied, it is used to generate the card response
69 * against which the passcode will be compared. If challenge is not
70 * supplied, or if the comparison fails, synchronous responses are
71 * generated and tested. NOTE: for async authentications, sync mode
72 * responses are still considered valid! (Assuming module configuration
75 * Returns one of the RLM_MODULE_* codes. passcode is filled in.
76 * NB: The returned passcode will contain the PIN! DO NOT LOG!
79 otp_pw_valid(REQUEST *request, int pwe, const char *challenge,
80 const otp_option_t *opt, char passcode[OTP_MAX_PASSCODE_LEN + 1])
82 otp_request_t otp_request;
83 otp_reply_t otp_reply;
84 VALUE_PAIR *cvp, *rvp;
85 char *username = request->username->strvalue;
88 if (request->username->length > OTP_MAX_USERNAME_LEN) {
89 (void) radlog(L_AUTH, "rlm_otp: username [%s] too long", username);
90 return RLM_MODULE_REJECT;
92 /* we already know challenge is short enough */
94 otp_request.version = 2;
95 (void) strcpy(otp_request.username, username);
96 (void) strcpy(otp_request.challenge, challenge);
97 otp_request.pwe.pwe = pwe;
99 /* otp_pwe_present() (done by caller) guarantees that both of these exist */
100 cvp = pairfind(request->packet->vps, pwattr[pwe - 1]);
101 rvp = pairfind(request->packet->vps, pwattr[pwe]);
102 /* this is just to quiet Coverity */
104 return RLM_MODULE_REJECT;
107 * Validate available vps based on pwe type.
108 * Unfortunately (?) otpd must do this also.
110 switch (otp_request.pwe.pwe) {
112 if (rvp->length > OTP_MAX_PASSCODE_LEN) {
113 (void) radlog(L_AUTH, "rlm_otp: passcode for [%s] too long", username);
114 return RLM_MODULE_REJECT;
116 (void) strcpy(otp_request.pwe.u.pap.passcode, rvp->strvalue);
120 if (cvp->length > 16) {
121 (void) radlog(L_AUTH, "rlm_otp: CHAP challenge for [%s] too long",
123 return RLM_MODULE_INVALID;
125 if (rvp->length != 17) {
126 (void) radlog(L_AUTH, "rlm_otp: CHAP response for [%s] wrong size",
128 return RLM_MODULE_INVALID;
130 (void) memcpy(otp_request.pwe.u.chap.challenge, cvp->strvalue,
132 otp_request.pwe.u.chap.clen = cvp->length;
133 (void) memcpy(otp_request.pwe.u.chap.response, rvp->strvalue,
135 otp_request.pwe.u.chap.rlen = rvp->length;
139 if (cvp->length != 8) {
140 (void) radlog(L_AUTH, "rlm_otp: MS-CHAP challenge for [%s] wrong size",
142 return RLM_MODULE_INVALID;
144 if (rvp->length != 50) {
145 (void) radlog(L_AUTH, "rlm_otp: MS-CHAP response for [%s] wrong size",
147 return RLM_MODULE_INVALID;
149 (void) memcpy(otp_request.pwe.u.chap.challenge, cvp->strvalue,
151 otp_request.pwe.u.chap.clen = cvp->length;
152 (void) memcpy(otp_request.pwe.u.chap.response, rvp->strvalue,
154 otp_request.pwe.u.chap.rlen = rvp->length;
158 if (cvp->length != 16) {
159 (void) radlog(L_AUTH, "rlm_otp: MS-CHAP2 challenge for [%s] wrong size",
161 return RLM_MODULE_INVALID;
163 if (rvp->length != 50) {
164 (void) radlog(L_AUTH, "rlm_otp: MS-CHAP2 response for [%s] wrong size",
166 return RLM_MODULE_INVALID;
168 (void) memcpy(otp_request.pwe.u.chap.challenge, cvp->strvalue,
170 otp_request.pwe.u.chap.clen = cvp->length;
171 (void) memcpy(otp_request.pwe.u.chap.response, rvp->strvalue,
173 otp_request.pwe.u.chap.rlen = rvp->length;
175 } /* switch (otp_request.pwe.pwe) */
177 /* last byte must also be a terminator so otpd can verify length easily */
178 otp_request.username[OTP_MAX_USERNAME_LEN] = '\0';
179 otp_request.challenge[OTP_MAX_CHALLENGE_LEN] = '\0';
180 if (otp_request.pwe.pwe == PWE_PAP)
181 otp_request.pwe.u.pap.passcode[OTP_MAX_PASSCODE_LEN] = '\0';
183 otp_request.allow_sync = opt->allow_sync;
184 otp_request.allow_async = opt->allow_async;
185 otp_request.challenge_delay = opt->challenge_delay;
186 otp_request.resync = 1;
188 rc = otp_verify(opt, &otp_request, &otp_reply);
190 (void) strcpy(passcode, otp_reply.passcode);
191 return otprc2rlmrc(rc);
195 * Verify an otp by asking otpd.
196 * Returns an OTP_* code, or -1 on system failure.
200 otp_verify(const otp_option_t *opt,
201 const otp_request_t *request, otp_reply_t *reply)
210 fdp = otp_getfd(opt);
211 if (!fdp || fdp->fd == -1)
214 if ((rc = otp_write(fdp, (const char *) request, sizeof(*request))) != 0) {
216 goto retry; /* otpd disconnect */ /*TODO: pause */
221 if ((rc = otp_read(fdp, (char *) reply, sizeof(*reply))) != sizeof(*reply)) {
223 goto retry; /* otpd disconnect */ /*TODO: pause */
228 /* validate the reply */
229 if (reply->version != 1) {
230 (void) radlog(L_AUTH, "rlm_otp: otpd reply for [%s] invalid "
232 request->username, reply->version);
237 if (reply->passcode[OTP_MAX_PASSCODE_LEN] != '\0') {
238 (void) radlog(L_AUTH, "rlm_otp: otpd reply for [%s] invalid (passcode)",
249 * Full read with logging, and close on failure.
250 * Returns nread on success, 0 on EOF, -1 on other failures.
253 otp_read(otp_fd_t *fdp, char *buf, size_t len)
256 size_t nread = 0; /* bytes read into buf */
258 while (nread < len) {
259 if ((n = read(fdp->fd, &buf[nread], len - nread)) == -1) {
260 if (errno == EINTR) {
263 (void) radlog(L_ERR, "rlm_otp: %s: read from otpd: %s",
264 __func__, strerror(errno));
270 (void) radlog(L_ERR, "rlm_otp: %s: otpd disconnect", __func__);
275 } /* while (more to read) */
281 * Full write with logging, and close on failure.
282 * Returns 0 on success, errno on failure.
285 otp_write(otp_fd_t *fdp, const char *buf, size_t len)
291 if ((nwrote = write(fdp->fd, &buf[len - nleft], nleft)) == -1) {
292 if (errno == EINTR || errno == EPIPE) {
295 (void) radlog(L_ERR, "rlm_otp: %s: write to otpd: %s",
296 __func__, strerror(errno));
307 /* connect to otpd and return fd */
309 otp_connect(const char *path)
312 struct sockaddr_un sa;
313 size_t sp_len; /* sun_path length (strlen) */
315 /* setup for unix domain socket */
316 sp_len = strlen(path);
317 if (sp_len > sizeof(sa.sun_path) - 1) {
318 (void) radlog(L_ERR, "rlm_otp: %s: rendezvous point name too long",
322 sa.sun_family = AF_UNIX;
323 (void) strcpy(sa.sun_path, path);
325 /* connect to otpd */
326 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
327 (void) radlog(L_ERR, "rlm_otp: %s: socket: %s", __func__, strerror(errno));
330 if (connect(fd, (struct sockaddr *) &sa,
331 sizeof(sa.sun_family) + sp_len) == -1) {
332 (void) radlog(L_ERR, "rlm_otp: %s: connect(%s): %s",
333 __func__, path, strerror(errno));
341 * Retrieve an fd (from pool) to use for otpd connection.
342 * It'd be simpler to use TLS but FR can have lots of threads
343 * and we don't want to waste fd's that way.
344 * We can't have a global fd because we'd then be pipelining
345 * requests to otpd and we have no way to demultiplex
349 otp_getfd(const otp_option_t *opt)
354 /* walk the connection pool looking for an available fd */
355 for (fdp = otp_fd_head; fdp; fdp = fdp->next) {
356 rc = otp_pthread_mutex_trylock(&fdp->mutex);
358 if (!strcmp(fdp->path, opt->otpd_rp)) /* could just use == */
363 /* no fd was available, add a new one */
364 fdp = rad_malloc(sizeof(*fdp));
365 otp_pthread_mutex_init(&fdp->mutex, NULL);
366 otp_pthread_mutex_lock(&fdp->mutex);
367 /* insert new fd at head */
368 otp_pthread_mutex_lock(&otp_fd_head_mutex);
369 fdp->next = otp_fd_head;
371 otp_pthread_mutex_unlock(&otp_fd_head_mutex);
373 fdp->path = opt->otpd_rp;
377 /* establish connection */
379 fdp->fd = otp_connect(fdp->path);
384 /* release fd, and optionally disconnect from otpd */
386 otp_putfd(otp_fd_t *fdp, int disconnect)
389 (void) close(fdp->fd);
393 /* make connection available to another thread */
394 otp_pthread_mutex_unlock(&fdp->mutex);