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 TRI-D Systems, Inc.
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
32 #include "otp_pw_valid.h"
40 /* transform otpd return codes into rlm return codes */
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;
55 static otp_fd_t *otp_fd_head;
56 static pthread_mutex_t otp_fd_head_mutex = PTHREAD_MUTEX_INITIALIZER;
59 * Test for passcode validity by asking otpd.
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
68 * Returns one of the RLM_MODULE_* codes. passcode is filled in.
69 * NB: The returned passcode will contain the PIN! DO NOT LOG!
72 otp_pw_valid(REQUEST *request, int pwe, const char *challenge,
73 const otp_option_t *opt, char passcode[OTP_MAX_PASSCODE_LEN + 1])
75 otp_request_t otp_request;
76 otp_reply_t otp_reply;
77 VALUE_PAIR *cvp, *rvp;
78 char *username = request->username->vp_strvalue;
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;
85 /* we already know challenge is short enough */
87 otp_request.version = 1;
88 (void) strcpy(otp_request.username, username);
89 (void) strcpy(otp_request.challenge, challenge);
90 otp_request.pwe.pwe = pwe;
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]);
97 * Validate available vps based on pwe type.
98 * Unfortunately (?) otpd must do this also.
100 switch (otp_request.pwe.pwe) {
102 if (rvp->length > OTP_MAX_PASSCODE_LEN) {
103 (void) radlog(L_AUTH, "rlm_otp: passcode for [%s] too long", username);
104 return RLM_MODULE_REJECT;
106 (void) strcpy(otp_request.pwe.passcode, rvp->vp_strvalue);
110 if (cvp->length > 16) {
111 (void) radlog(L_AUTH, "rlm_otp: CHAP challenge for [%s] too long",
113 return RLM_MODULE_INVALID;
115 if (rvp->length != 17) {
116 (void) radlog(L_AUTH, "rlm_otp: CHAP response for [%s] wrong size",
118 return RLM_MODULE_INVALID;
120 (void) memcpy(otp_request.pwe.challenge, cvp->vp_strvalue, cvp->length);
121 otp_request.pwe.clen = cvp->length;
122 (void) memcpy(otp_request.pwe.response, rvp->vp_strvalue, rvp->length);
123 otp_request.pwe.rlen = rvp->length;
127 if (cvp->length != 8) {
128 (void) radlog(L_AUTH, "rlm_otp: MS-CHAP challenge for [%s] wrong size",
130 return RLM_MODULE_INVALID;
132 if (rvp->length != 50) {
133 (void) radlog(L_AUTH, "rlm_otp: MS-CHAP response for [%s] wrong size",
135 return RLM_MODULE_INVALID;
137 (void) memcpy(otp_request.pwe.challenge, cvp->vp_strvalue, cvp->length);
138 otp_request.pwe.clen = cvp->length;
139 (void) memcpy(otp_request.pwe.response, rvp->vp_strvalue, rvp->length);
140 otp_request.pwe.rlen = rvp->length;
144 if (cvp->length != 16) {
145 (void) radlog(L_AUTH, "rlm_otp: MS-CHAP2 challenge for [%s] wrong size",
147 return RLM_MODULE_INVALID;
149 if (rvp->length != 50) {
150 (void) radlog(L_AUTH, "rlm_otp: MS-CHAP2 response for [%s] wrong size",
152 return RLM_MODULE_INVALID;
154 (void) memcpy(otp_request.pwe.challenge, cvp->vp_strvalue, cvp->length);
155 otp_request.pwe.clen = cvp->length;
156 (void) memcpy(otp_request.pwe.response, rvp->vp_strvalue, rvp->length);
157 otp_request.pwe.rlen = rvp->length;
159 } /* switch (otp_request.pwe.pwe) */
161 /* last byte must also be a terminator so otpd can verify length easily */
162 otp_request.username[OTP_MAX_USERNAME_LEN] = '\0';
163 otp_request.challenge[OTP_MAX_CHALLENGE_LEN] = '\0';
164 otp_request.pwe.passcode[OTP_MAX_PASSCODE_LEN] = '\0';
166 otp_request.allow_sync = opt->allow_sync;
167 otp_request.allow_async = opt->allow_async;
168 otp_request.challenge_delay = opt->challenge_delay;
169 otp_request.resync = 1;
171 rc = otp_verify(opt, &otp_request, &otp_reply);
173 (void) strcpy(passcode, otp_reply.passcode);
174 return otprc2rlmrc(rc);
178 * Verify an otp by asking otpd.
179 * Returns an OTP_* code, or -1 on system failure.
183 otp_verify(const otp_option_t *opt,
184 const otp_request_t *request, otp_reply_t *reply)
193 fdp = otp_getfd(opt);
194 if (!fdp || fdp->fd == -1)
197 if ((rc = otp_write(fdp, (const char *) request, sizeof(*request))) != 0) {
199 goto retry; /* otpd disconnect */ /*TODO: pause */
204 if ((rc = otp_read(fdp, (char *) reply, sizeof(*reply))) != sizeof(*reply)) {
206 goto retry; /* otpd disconnect */ /*TODO: pause */
211 /* validate the reply */
212 if (reply->version != 1) {
213 (void) radlog(L_AUTH, "rlm_otp: otpd reply for [%s] invalid "
215 request->username, reply->version);
220 if (reply->passcode[OTP_MAX_PASSCODE_LEN] != '\0') {
221 (void) radlog(L_AUTH, "rlm_otp: otpd reply for [%s] invalid (passcode)",
232 * Full read with logging, and close on failure.
233 * Returns nread on success, 0 on EOF, -1 on other failures.
236 otp_read(otp_fd_t *fdp, char *buf, size_t len)
239 size_t nread = 0; /* bytes read into buf */
241 while (nread < len) {
242 if ((n = read(fdp->fd, &buf[nread], len - nread)) == -1) {
243 if (errno == EINTR) {
246 (void) radlog(L_ERR, "rlm_otp: %s: read from otpd: %s",
247 __func__, strerror(errno));
253 (void) radlog(L_ERR, "rlm_otp: %s: otpd disconnect", __func__);
258 } /* while (more to read) */
264 * Full write with logging, and close on failure.
265 * Returns 0 on success, errno on failure.
268 otp_write(otp_fd_t *fdp, const char *buf, size_t len)
274 if ((nwrote = write(fdp->fd, &buf[len - nleft], nleft)) == -1) {
275 if (errno == EINTR || errno == EPIPE) {
278 (void) radlog(L_ERR, "rlm_otp: %s: write to otpd: %s",
279 __func__, strerror(errno));
290 /* connect to otpd and return fd */
292 otp_connect(const char *path)
295 struct sockaddr_un sa;
296 size_t sp_len; /* sun_path length (strlen) */
298 /* setup for unix domain socket */
299 sp_len = strlen(path);
300 if (sp_len > sizeof(sa.sun_path) - 1) {
301 (void) radlog(L_ERR, "rlm_otp: %s: rendezvous point name too long",
305 sa.sun_family = AF_UNIX;
306 (void) strcpy(sa.sun_path, path);
308 /* connect to otpd */
309 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
310 (void) radlog(L_ERR, "rlm_otp: %s: socket: %s", __func__, strerror(errno));
313 if (connect(fd, (struct sockaddr *) &sa,
314 sizeof(sa.sun_family) + sp_len) == -1) {
315 (void) radlog(L_ERR, "rlm_otp: %s: connect(%s): %s",
316 __func__, path, strerror(errno));
324 * Retrieve an fd (from pool) to use for otpd connection.
325 * It'd be simpler to use TLS but FR can have lots of threads
326 * and we don't want to waste fd's that way.
327 * We can't have a global fd because we'd then be pipelining
328 * requests to otpd and we have no way to demultiplex
332 otp_getfd(const otp_option_t *opt)
337 /* walk the connection pool looking for an available fd */
338 for (fdp = otp_fd_head; fdp; fdp = fdp->next) {
339 rc = otp_pthread_mutex_trylock(&fdp->mutex);
341 if (!strcmp(fdp->path, opt->otpd_rp)) /* could just use == */
346 /* no fd was available, add a new one */
347 fdp = rad_malloc(sizeof(*fdp));
348 otp_pthread_mutex_init(&fdp->mutex, NULL);
349 otp_pthread_mutex_lock(&fdp->mutex);
350 /* insert new fd at head */
351 otp_pthread_mutex_lock(&otp_fd_head_mutex);
352 fdp->next = otp_fd_head;
354 otp_pthread_mutex_unlock(&otp_fd_head_mutex);
356 fdp->path = opt->otpd_rp;
360 /* establish connection */
362 fdp->fd = otp_connect(fdp->path);
367 /* release fd, and optionally disconnect from otpd */
369 otp_putfd(otp_fd_t *fdp, int disconnect)
372 (void) close(fdp->fd);
376 /* make connection available to another thread */
377 otp_pthread_mutex_unlock(&fdp->mutex);