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>
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_SERVICE_ERR: return RLM_MODULE_FAIL;
56 default: return RLM_MODULE_FAIL;
60 static otp_fd_t *otp_fd_head;
61 static pthread_mutex_t otp_fd_head_mutex = PTHREAD_MUTEX_INITIALIZER;
64 * Test for passcode validity by asking otpd.
66 * If challenge is supplied, it is used to generate the card response
67 * against which the passcode will be compared. If challenge is not
68 * supplied, or if the comparison fails, synchronous responses are
69 * generated and tested. NOTE: for async authentications, sync mode
70 * responses are still considered valid! (Assuming module configuration
73 * Returns one of the RLM_MODULE_* codes. passcode is filled in.
74 * NB: The returned passcode will contain the PIN! DO NOT LOG!
77 otp_pw_valid(REQUEST *request, int pwe, const char *challenge,
78 const otp_option_t *opt, char passcode[OTP_MAX_PASSCODE_LEN + 1])
80 otp_request_t otp_request;
81 otp_reply_t otp_reply;
82 VALUE_PAIR *cvp, *rvp;
83 char *username = request->username->strvalue;
86 if (request->username->length > OTP_MAX_USERNAME_LEN) {
87 (void) radlog(L_AUTH, "rlm_otp: username [%s] too long", username);
88 return RLM_MODULE_REJECT;
90 /* we already know challenge is short enough */
92 otp_request.version = 2;
93 (void) strcpy(otp_request.username, username);
94 (void) strcpy(otp_request.challenge, challenge);
95 otp_request.pwe.pwe = pwe;
97 /* otp_pwe_present() (done by caller) guarantees that both of these exist */
98 cvp = pairfind(request->packet->vps, pwattr[pwe - 1]);
99 rvp = pairfind(request->packet->vps, pwattr[pwe]);
100 /* this is just to quiet Coverity */
102 return RLM_MODULE_REJECT;
105 * Validate available vps based on pwe type.
106 * Unfortunately (?) otpd must do this also.
108 switch (otp_request.pwe.pwe) {
110 if (rvp->length > OTP_MAX_PASSCODE_LEN) {
111 (void) radlog(L_AUTH, "rlm_otp: passcode for [%s] too long", username);
112 return RLM_MODULE_REJECT;
114 (void) strcpy(otp_request.pwe.u.pap.passcode, rvp->strvalue);
118 if (cvp->length > 16) {
119 (void) radlog(L_AUTH, "rlm_otp: CHAP challenge for [%s] too long",
121 return RLM_MODULE_INVALID;
123 if (rvp->length != 17) {
124 (void) radlog(L_AUTH, "rlm_otp: CHAP response for [%s] wrong size",
126 return RLM_MODULE_INVALID;
128 (void) memcpy(otp_request.pwe.u.chap.challenge, cvp->strvalue,
130 otp_request.pwe.u.chap.clen = cvp->length;
131 (void) memcpy(otp_request.pwe.u.chap.response, rvp->strvalue,
133 otp_request.pwe.u.chap.rlen = rvp->length;
137 if (cvp->length != 8) {
138 (void) radlog(L_AUTH, "rlm_otp: MS-CHAP challenge for [%s] wrong size",
140 return RLM_MODULE_INVALID;
142 if (rvp->length != 50) {
143 (void) radlog(L_AUTH, "rlm_otp: MS-CHAP response for [%s] wrong size",
145 return RLM_MODULE_INVALID;
147 (void) memcpy(otp_request.pwe.u.chap.challenge, cvp->strvalue,
149 otp_request.pwe.u.chap.clen = cvp->length;
150 (void) memcpy(otp_request.pwe.u.chap.response, rvp->strvalue,
152 otp_request.pwe.u.chap.rlen = rvp->length;
156 if (cvp->length != 16) {
157 (void) radlog(L_AUTH, "rlm_otp: MS-CHAP2 challenge for [%s] wrong size",
159 return RLM_MODULE_INVALID;
161 if (rvp->length != 50) {
162 (void) radlog(L_AUTH, "rlm_otp: MS-CHAP2 response for [%s] wrong size",
164 return RLM_MODULE_INVALID;
166 (void) memcpy(otp_request.pwe.u.chap.challenge, cvp->strvalue,
168 otp_request.pwe.u.chap.clen = cvp->length;
169 (void) memcpy(otp_request.pwe.u.chap.response, rvp->strvalue,
171 otp_request.pwe.u.chap.rlen = rvp->length;
173 } /* switch (otp_request.pwe.pwe) */
175 /* last byte must also be a terminator so otpd can verify length easily */
176 otp_request.username[OTP_MAX_USERNAME_LEN] = '\0';
177 otp_request.challenge[OTP_MAX_CHALLENGE_LEN] = '\0';
178 if (otp_request.pwe.pwe == PWE_PAP)
179 otp_request.pwe.u.pap.passcode[OTP_MAX_PASSCODE_LEN] = '\0';
181 otp_request.allow_sync = opt->allow_sync;
182 otp_request.allow_async = opt->allow_async;
183 otp_request.challenge_delay = opt->challenge_delay;
184 otp_request.resync = 1;
186 rc = otp_verify(opt, &otp_request, &otp_reply);
188 (void) strcpy(passcode, otp_reply.passcode);
189 return otprc2rlmrc(rc);
193 * Verify an otp by asking otpd.
194 * Returns an OTP_* code, or -1 on system failure.
198 otp_verify(const otp_option_t *opt,
199 const otp_request_t *request, otp_reply_t *reply)
208 fdp = otp_getfd(opt);
209 if (!fdp || fdp->fd == -1)
212 if ((rc = otp_write(fdp, (const char *) request, sizeof(*request))) != 0) {
214 goto retry; /* otpd disconnect */ /*TODO: pause */
219 if ((rc = otp_read(fdp, (char *) reply, sizeof(*reply))) != sizeof(*reply)) {
221 goto retry; /* otpd disconnect */ /*TODO: pause */
226 /* validate the reply */
227 if (reply->version != 1) {
228 (void) radlog(L_AUTH, "rlm_otp: otpd reply for [%s] invalid "
230 request->username, reply->version);
235 if (reply->passcode[OTP_MAX_PASSCODE_LEN] != '\0') {
236 (void) radlog(L_AUTH, "rlm_otp: otpd reply for [%s] invalid (passcode)",
247 * Full read with logging, and close on failure.
248 * Returns nread on success, 0 on EOF, -1 on other failures.
251 otp_read(otp_fd_t *fdp, char *buf, size_t len)
254 size_t nread = 0; /* bytes read into buf */
256 while (nread < len) {
257 if ((n = read(fdp->fd, &buf[nread], len - nread)) == -1) {
258 if (errno == EINTR) {
261 (void) radlog(L_ERR, "rlm_otp: %s: read from otpd: %s",
262 __func__, strerror(errno));
268 (void) radlog(L_ERR, "rlm_otp: %s: otpd disconnect", __func__);
273 } /* while (more to read) */
279 * Full write with logging, and close on failure.
280 * Returns 0 on success, errno on failure.
283 otp_write(otp_fd_t *fdp, const char *buf, size_t len)
289 if ((nwrote = write(fdp->fd, &buf[len - nleft], nleft)) == -1) {
290 if (errno == EINTR || errno == EPIPE) {
293 (void) radlog(L_ERR, "rlm_otp: %s: write to otpd: %s",
294 __func__, strerror(errno));
305 /* connect to otpd and return fd */
307 otp_connect(const char *path)
310 struct sockaddr_un sa;
311 size_t sp_len; /* sun_path length (strlen) */
313 /* setup for unix domain socket */
314 sp_len = strlen(path);
315 if (sp_len > sizeof(sa.sun_path) - 1) {
316 (void) radlog(L_ERR, "rlm_otp: %s: rendezvous point name too long",
320 sa.sun_family = AF_UNIX;
321 (void) strcpy(sa.sun_path, path);
323 /* connect to otpd */
324 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
325 (void) radlog(L_ERR, "rlm_otp: %s: socket: %s", __func__, strerror(errno));
328 if (connect(fd, (struct sockaddr *) &sa,
329 sizeof(sa.sun_family) + sp_len) == -1) {
330 (void) radlog(L_ERR, "rlm_otp: %s: connect(%s): %s",
331 __func__, path, strerror(errno));
339 * Retrieve an fd (from pool) to use for otpd connection.
340 * It'd be simpler to use TLS but FR can have lots of threads
341 * and we don't want to waste fd's that way.
342 * We can't have a global fd because we'd then be pipelining
343 * requests to otpd and we have no way to demultiplex
347 otp_getfd(const otp_option_t *opt)
352 /* walk the connection pool looking for an available fd */
353 for (fdp = otp_fd_head; fdp; fdp = fdp->next) {
354 rc = otp_pthread_mutex_trylock(&fdp->mutex);
356 if (!strcmp(fdp->path, opt->otpd_rp)) /* could just use == */
361 /* no fd was available, add a new one */
362 fdp = rad_malloc(sizeof(*fdp));
363 otp_pthread_mutex_init(&fdp->mutex, NULL);
364 otp_pthread_mutex_lock(&fdp->mutex);
365 /* insert new fd at head */
366 otp_pthread_mutex_lock(&otp_fd_head_mutex);
367 fdp->next = otp_fd_head;
369 otp_pthread_mutex_unlock(&otp_fd_head_mutex);
371 fdp->path = opt->otpd_rp;
375 /* establish connection */
377 fdp->fd = otp_connect(fdp->path);
382 /* release fd, and optionally disconnect from otpd */
384 otp_putfd(otp_fd_t *fdp, int disconnect)
387 (void) close(fdp->fd);
391 /* make connection available to another thread */
392 otp_pthread_mutex_unlock(&fdp->mutex);