2 * Copyright 1998 Juniper Networks, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
36 #include <openssl/hmac.h>
37 #include <openssl/md5.h>
38 #define MD5Init MD5_Init
39 #define MD5Update MD5_Update
40 #define MD5Final MD5_Final
42 #define MD5_DIGEST_LENGTH 16
46 /* We need the MPPE_KEY_LEN define */
47 #include <netgraph/ng_mppc.h>
58 #include "radlib_private.h"
60 static void clear_password(struct rad_handle *);
61 static void generr(struct rad_handle *, const char *, ...)
63 static void insert_scrambled_password(struct rad_handle *, int);
64 static void insert_request_authenticator(struct rad_handle *, int);
65 static void insert_message_authenticator(struct rad_handle *, int);
66 static int is_valid_response(struct rad_handle *, int,
67 const struct sockaddr_in *);
68 static int put_password_attr(struct rad_handle *, int,
69 const void *, size_t);
70 static int put_raw_attr(struct rad_handle *, int,
71 const void *, size_t);
72 static int split(char *, char *[], int, char *, size_t);
75 clear_password(struct rad_handle *h)
77 if (h->pass_len != 0) {
78 memset(h->pass, 0, h->pass_len);
85 generr(struct rad_handle *h, const char *format, ...)
90 vsnprintf(h->errmsg, ERRSIZE, format, ap);
95 insert_scrambled_password(struct rad_handle *h, int srv)
98 unsigned char md5[MD5_DIGEST_LENGTH];
99 const struct rad_server *srvp;
103 srvp = &h->servers[srv];
104 padded_len = h->pass_len == 0 ? 16 : (h->pass_len+15) & ~0xf;
106 memcpy(md5, &h->out[POS_AUTH], LEN_AUTH);
107 for (pos = 0; pos < padded_len; pos += 16) {
110 /* Calculate the new scrambler */
112 MD5Update(&ctx, srvp->secret, strlen(srvp->secret));
113 MD5Update(&ctx, md5, 16);
117 * Mix in the current chunk of the password, and copy
118 * the result into the right place in the request. Also
119 * modify the scrambler in place, since we will use this
120 * in calculating the scrambler for next time.
122 for (i = 0; i < 16; i++)
123 h->out[h->pass_pos + pos + i] =
124 md5[i] ^= h->pass[pos + i];
129 insert_request_authenticator(struct rad_handle *h, int resp)
132 const struct rad_server *srvp;
134 srvp = &h->servers[h->srv];
136 /* Create the request authenticator */
138 MD5Update(&ctx, &h->out[POS_CODE], POS_AUTH - POS_CODE);
140 MD5Update(&ctx, &h->in[POS_AUTH], LEN_AUTH);
142 MD5Update(&ctx, &h->out[POS_AUTH], LEN_AUTH);
143 MD5Update(&ctx, &h->out[POS_ATTRS], h->out_len - POS_ATTRS);
144 MD5Update(&ctx, srvp->secret, strlen(srvp->secret));
145 MD5Final(&h->out[POS_AUTH], &ctx);
149 insert_message_authenticator(struct rad_handle *h, int resp)
152 u_char md[EVP_MAX_MD_SIZE];
154 const struct rad_server *srvp;
156 srvp = &h->servers[h->srv];
158 if (h->authentic_pos != 0) {
160 HMAC_Init(&ctx, srvp->secret, strlen(srvp->secret), EVP_md5());
161 HMAC_Update(&ctx, &h->out[POS_CODE], POS_AUTH - POS_CODE);
163 HMAC_Update(&ctx, &h->in[POS_AUTH], LEN_AUTH);
165 HMAC_Update(&ctx, &h->out[POS_AUTH], LEN_AUTH);
166 HMAC_Update(&ctx, &h->out[POS_ATTRS],
167 h->out_len - POS_ATTRS);
168 HMAC_Final(&ctx, md, &md_len);
169 HMAC_CTX_cleanup(&ctx);
171 memcpy(&h->out[h->authentic_pos + 2], md, md_len);
177 * Return true if the current response is valid for a request to the
181 is_valid_response(struct rad_handle *h, int srv,
182 const struct sockaddr_in *from)
185 unsigned char md5[MD5_DIGEST_LENGTH];
186 const struct rad_server *srvp;
190 u_char resp[MSGSIZE], md[EVP_MAX_MD_SIZE];
195 srvp = &h->servers[srv];
197 /* Check the source address */
198 if (from->sin_family != srvp->addr.sin_family ||
199 from->sin_addr.s_addr != srvp->addr.sin_addr.s_addr ||
200 from->sin_port != srvp->addr.sin_port)
203 /* Check the message length */
204 if (h->in_len < POS_ATTRS)
206 len = h->in[POS_LENGTH] << 8 | h->in[POS_LENGTH+1];
210 /* Check the response authenticator */
212 MD5Update(&ctx, &h->in[POS_CODE], POS_AUTH - POS_CODE);
213 MD5Update(&ctx, &h->out[POS_AUTH], LEN_AUTH);
214 MD5Update(&ctx, &h->in[POS_ATTRS], len - POS_ATTRS);
215 MD5Update(&ctx, srvp->secret, strlen(srvp->secret));
217 if (memcmp(&h->in[POS_AUTH], md5, sizeof md5) != 0)
222 * For non accounting responses check the message authenticator,
225 if (h->in[POS_CODE] != RAD_ACCOUNTING_RESPONSE) {
227 memcpy(resp, h->in, MSGSIZE);
230 /* Search and verify the Message-Authenticator */
231 while (pos < len - 2) {
233 if (h->in[pos] == RAD_MESSAGE_AUTHENTIC) {
234 /* zero fill the Message-Authenticator */
235 memset(&resp[pos + 2], 0, MD5_DIGEST_LENGTH);
237 HMAC_CTX_init(&hctx);
238 HMAC_Init(&hctx, srvp->secret,
239 strlen(srvp->secret), EVP_md5());
240 HMAC_Update(&hctx, &h->in[POS_CODE],
241 POS_AUTH - POS_CODE);
242 HMAC_Update(&hctx, &h->out[POS_AUTH],
244 HMAC_Update(&hctx, &resp[POS_ATTRS],
245 h->in_len - POS_ATTRS);
246 HMAC_Final(&hctx, md, &md_len);
247 HMAC_CTX_cleanup(&hctx);
249 if (memcmp(md, &h->in[pos + 2],
250 MD5_DIGEST_LENGTH) != 0)
254 pos += h->in[pos + 1];
262 * Return true if the current request is valid for the specified server.
265 is_valid_request(struct rad_handle *h)
268 unsigned char md5[MD5_DIGEST_LENGTH];
269 const struct rad_server *srvp;
273 u_char resp[MSGSIZE], md[EVP_MAX_MD_SIZE];
278 srvp = &h->servers[h->srv];
280 /* Check the message length */
281 if (h->in_len < POS_ATTRS)
283 len = h->in[POS_LENGTH] << 8 | h->in[POS_LENGTH+1];
287 if (h->in[POS_CODE] != RAD_ACCESS_REQUEST) {
288 uint32_t zeroes[4] = { 0, 0, 0, 0 };
289 /* Check the request authenticator */
291 MD5Update(&ctx, &h->in[POS_CODE], POS_AUTH - POS_CODE);
292 MD5Update(&ctx, zeroes, LEN_AUTH);
293 MD5Update(&ctx, &h->in[POS_ATTRS], len - POS_ATTRS);
294 MD5Update(&ctx, srvp->secret, strlen(srvp->secret));
296 if (memcmp(&h->in[POS_AUTH], md5, sizeof md5) != 0)
301 /* Search and verify the Message-Authenticator */
303 while (pos < len - 2) {
304 if (h->in[pos] == RAD_MESSAGE_AUTHENTIC) {
305 memcpy(resp, h->in, MSGSIZE);
306 /* zero fill the Request-Authenticator */
307 if (h->in[POS_CODE] != RAD_ACCESS_REQUEST)
308 memset(&resp[POS_AUTH], 0, LEN_AUTH);
309 /* zero fill the Message-Authenticator */
310 memset(&resp[pos + 2], 0, MD5_DIGEST_LENGTH);
312 HMAC_CTX_init(&hctx);
313 HMAC_Init(&hctx, srvp->secret,
314 strlen(srvp->secret), EVP_md5());
315 HMAC_Update(&hctx, resp, h->in_len);
316 HMAC_Final(&hctx, md, &md_len);
317 HMAC_CTX_cleanup(&hctx);
319 if (memcmp(md, &h->in[pos + 2],
320 MD5_DIGEST_LENGTH) != 0)
324 pos += h->in[pos + 1];
331 put_password_attr(struct rad_handle *h, int type, const void *value, size_t len)
336 if (h->pass_pos != 0) {
337 generr(h, "Multiple User-Password attributes specified");
342 padded_len = len == 0 ? 16 : (len+15) & ~0xf;
343 pad_len = padded_len - len;
346 * Put in a place-holder attribute containing all zeros, and
347 * remember where it is so we can fill it in later.
350 put_raw_attr(h, type, h->pass, padded_len);
351 h->pass_pos = h->out_len - padded_len;
353 /* Save the cleartext password, padded as necessary */
354 memcpy(h->pass, value, len);
356 memset(h->pass + len, 0, pad_len);
361 put_raw_attr(struct rad_handle *h, int type, const void *value, size_t len)
364 generr(h, "Attribute too long");
367 if (h->out_len + 2 + len > MSGSIZE) {
368 generr(h, "Maximum message length exceeded");
371 h->out[h->out_len++] = type;
372 h->out[h->out_len++] = len + 2;
373 memcpy(&h->out[h->out_len], value, len);
379 rad_add_server(struct rad_handle *h, const char *host, int port,
380 const char *secret, int timeout, int tries)
382 struct rad_server *srvp;
384 if (h->num_servers >= MAXSERVERS) {
385 generr(h, "Too many RADIUS servers specified");
388 srvp = &h->servers[h->num_servers];
390 memset(&srvp->addr, 0, sizeof srvp->addr);
391 srvp->addr.sin_len = sizeof srvp->addr;
392 srvp->addr.sin_family = AF_INET;
393 if (!inet_aton(host, &srvp->addr.sin_addr)) {
394 struct hostent *hent;
396 if ((hent = gethostbyname(host)) == NULL) {
397 generr(h, "%s: host not found", host);
400 memcpy(&srvp->addr.sin_addr, hent->h_addr,
401 sizeof srvp->addr.sin_addr);
404 srvp->addr.sin_port = htons((u_short)port);
406 struct servent *sent;
408 if (h->type == RADIUS_AUTH)
409 srvp->addr.sin_port =
410 (sent = getservbyname("radius", "udp")) != NULL ?
411 sent->s_port : htons(RADIUS_PORT);
413 srvp->addr.sin_port =
414 (sent = getservbyname("radacct", "udp")) != NULL ?
415 sent->s_port : htons(RADACCT_PORT);
417 if ((srvp->secret = strdup(secret)) == NULL) {
418 generr(h, "Out of memory");
421 srvp->timeout = timeout;
422 srvp->max_tries = tries;
429 rad_close(struct rad_handle *h)
435 for (srv = 0; srv < h->num_servers; srv++) {
436 memset(h->servers[srv].secret, 0,
437 strlen(h->servers[srv].secret));
438 free(h->servers[srv].secret);
445 rad_config(struct rad_handle *h, const char *path)
448 char buf[MAXCONFLINE];
453 path = PATH_RADIUS_CONF;
454 if ((fp = fopen(path, "r")) == NULL) {
455 generr(h, "Cannot open \"%s\": %s", path, strerror(errno));
460 while (fgets(buf, sizeof buf, fp) != NULL) {
473 unsigned long timeout;
474 unsigned long maxtries;
480 /* We know len > 0, else fgets would have returned NULL. */
481 if (buf[len - 1] != '\n') {
482 if (len == sizeof buf - 1)
483 generr(h, "%s:%d: line too long", path,
486 generr(h, "%s:%d: missing newline", path,
493 /* Extract the fields from the line. */
494 nfields = split(buf, fields, 5, msg, sizeof msg);
496 generr(h, "%s:%d: %s", path, linenum, msg);
503 * The first field should contain "auth" or "acct" for
504 * authentication or accounting, respectively. But older
505 * versions of the file didn't have that field. Default
506 * it to "auth" for backward compatibility.
508 if (strcmp(fields[0], "auth") != 0 &&
509 strcmp(fields[0], "acct") != 0) {
511 generr(h, "%s:%d: invalid service type", path,
517 for (i = nfields; --i > 0; )
518 fields[i] = fields[i - 1];
522 generr(h, "%s:%d: missing shared secret", path,
530 timeout_str = fields[3];
531 maxtries_str = fields[4];
533 /* Ignore the line if it is for the wrong service type. */
534 wanttype = h->type == RADIUS_AUTH ? "auth" : "acct";
535 if (strcmp(type, wanttype) != 0)
538 /* Parse and validate the fields. */
540 host = strsep(&res, ":");
541 port_str = strsep(&res, ":");
542 if (port_str != NULL) {
543 port = strtoul(port_str, &end, 10);
545 generr(h, "%s:%d: invalid port", path,
552 if (timeout_str != NULL) {
553 timeout = strtoul(timeout_str, &end, 10);
555 generr(h, "%s:%d: invalid timeout", path,
562 if (maxtries_str != NULL) {
563 maxtries = strtoul(maxtries_str, &end, 10);
565 generr(h, "%s:%d: invalid maxtries", path,
573 if (rad_add_server(h, host, port, secret, timeout, maxtries) ==
575 strcpy(msg, h->errmsg);
576 generr(h, "%s:%d: %s", path, linenum, msg);
581 /* Clear out the buffer to wipe a possible copy of a shared secret */
582 memset(buf, 0, sizeof buf);
588 * rad_init_send_request() must have previously been called.
590 * 0 The application should select on *fd with a timeout of tv before
591 * calling rad_continue_send_request again.
596 rad_continue_send_request(struct rad_handle *h, int selected, int *fd,
601 if (h->type == RADIUS_SERVER) {
602 generr(h, "denied function call");
606 struct sockaddr_in from;
609 fromlen = sizeof from;
610 h->in_len = recvfrom(h->fd, h->in,
611 MSGSIZE, MSG_WAITALL, (struct sockaddr *)&from, &fromlen);
612 if (h->in_len == -1) {
613 generr(h, "recvfrom: %s", strerror(errno));
616 if (is_valid_response(h, h->srv, &from)) {
617 h->in_len = h->in[POS_LENGTH] << 8 |
619 h->in_pos = POS_ATTRS;
620 return h->in[POS_CODE];
624 if (h->try == h->total_tries) {
625 generr(h, "No valid RADIUS responses received");
630 * Scan round-robin to the next server that has some
631 * tries left. There is guaranteed to be one, or we
632 * would have exited this loop by now.
634 while (h->servers[h->srv].num_tries >= h->servers[h->srv].max_tries)
635 if (++h->srv >= h->num_servers)
638 if (h->out[POS_CODE] == RAD_ACCESS_REQUEST) {
639 /* Insert the scrambled password into the request */
640 if (h->pass_pos != 0)
641 insert_scrambled_password(h, h->srv);
643 insert_message_authenticator(h, 0);
644 if (h->out[POS_CODE] != RAD_ACCESS_REQUEST) {
645 /* Insert the request authenticator into the request */
646 insert_request_authenticator(h, h->srv);
649 /* Send the request */
650 n = sendto(h->fd, h->out, h->out_len, 0,
651 (const struct sockaddr *)&h->servers[h->srv].addr,
652 sizeof h->servers[h->srv].addr);
654 tv->tv_sec = 1; /* Do not wait full timeout if send failed. */
656 tv->tv_sec = h->servers[h->srv].timeout;
658 h->servers[h->srv].num_tries++;
666 rad_receive_request(struct rad_handle *h)
668 struct sockaddr_in from;
672 if (h->type != RADIUS_SERVER) {
673 generr(h, "denied function call");
677 fromlen = sizeof(from);
678 h->in_len = recvfrom(h->fd, h->in,
679 MSGSIZE, MSG_WAITALL, (struct sockaddr *)&from, &fromlen);
680 if (h->in_len == -1) {
681 generr(h, "recvfrom: %s", strerror(errno));
684 for (n = 0; n < h->num_servers; n++) {
685 if (h->servers[n].addr.sin_addr.s_addr == from.sin_addr.s_addr) {
686 h->servers[n].addr.sin_port = from.sin_port;
693 if (is_valid_request(h)) {
694 h->in_len = h->in[POS_LENGTH] << 8 |
696 h->in_pos = POS_ATTRS;
697 return (h->in[POS_CODE]);
703 rad_send_response(struct rad_handle *h)
707 if (h->type != RADIUS_SERVER) {
708 generr(h, "denied function call");
711 /* Fill in the length field in the message */
712 h->out[POS_LENGTH] = h->out_len >> 8;
713 h->out[POS_LENGTH+1] = h->out_len;
715 insert_message_authenticator(h,
716 (h->in[POS_CODE] == RAD_ACCESS_REQUEST) ? 1 : 0);
717 insert_request_authenticator(h, 1);
719 /* Send the request */
720 n = sendto(h->fd, h->out, h->out_len, 0,
721 (const struct sockaddr *)&h->servers[h->srv].addr,
722 sizeof h->servers[h->srv].addr);
723 if (n != h->out_len) {
725 generr(h, "sendto: %s", strerror(errno));
727 generr(h, "sendto: short write");
735 rad_create_request(struct rad_handle *h, int code)
739 if (h->type == RADIUS_SERVER) {
740 generr(h, "denied function call");
743 h->out[POS_CODE] = code;
744 h->out[POS_IDENT] = ++h->ident;
745 if (code == RAD_ACCESS_REQUEST) {
746 /* Create a random authenticator */
747 for (i = 0; i < LEN_AUTH; i += 2) {
750 h->out[POS_AUTH+i] = (u_char)r;
751 h->out[POS_AUTH+i+1] = (u_char)(r >> 8);
754 memset(&h->out[POS_AUTH], 0, LEN_AUTH);
755 h->out_len = POS_ATTRS;
757 h->authentic_pos = 0;
763 rad_create_response(struct rad_handle *h, int code)
766 if (h->type != RADIUS_SERVER) {
767 generr(h, "denied function call");
770 h->out[POS_CODE] = code;
771 h->out[POS_IDENT] = h->in[POS_IDENT];
772 memset(&h->out[POS_AUTH], 0, LEN_AUTH);
773 h->out_len = POS_ATTRS;
775 h->authentic_pos = 0;
781 rad_cvt_addr(const void *data)
783 struct in_addr value;
785 memcpy(&value.s_addr, data, sizeof value.s_addr);
790 rad_cvt_int(const void *data)
794 memcpy(&value, data, sizeof value);
799 rad_cvt_string(const void *data, size_t len)
805 memcpy(s, data, len);
812 * Returns the attribute type. If none are left, returns 0. On failure,
816 rad_get_attr(struct rad_handle *h, const void **value, size_t *len)
820 if (h->in_pos >= h->in_len)
822 if (h->in_pos + 2 > h->in_len) {
823 generr(h, "Malformed attribute in response");
826 type = h->in[h->in_pos++];
827 *len = h->in[h->in_pos++] - 2;
828 if (h->in_pos + (int)*len > h->in_len) {
829 generr(h, "Malformed attribute in response");
832 *value = &h->in[h->in_pos];
838 * Returns -1 on error, 0 to indicate no event and >0 for success
841 rad_init_send_request(struct rad_handle *h, int *fd, struct timeval *tv)
845 if (h->type == RADIUS_SERVER) {
846 generr(h, "denied function call");
849 /* Make sure we have a socket to use */
851 struct sockaddr_in sin;
853 if ((h->fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
854 generr(h, "Cannot create socket: %s", strerror(errno));
857 memset(&sin, 0, sizeof sin);
858 sin.sin_len = sizeof sin;
859 sin.sin_family = AF_INET;
860 sin.sin_addr.s_addr = INADDR_ANY;
861 sin.sin_port = htons(0);
862 if (bind(h->fd, (const struct sockaddr *)&sin,
864 generr(h, "bind: %s", strerror(errno));
871 if (h->out[POS_CODE] != RAD_ACCESS_REQUEST) {
872 /* Make sure no password given */
873 if (h->pass_pos || h->chap_pass) {
874 generr(h, "User or Chap Password"
875 " in accounting request");
879 if (h->eap_msg == 0) {
880 /* Make sure the user gave us a password */
881 if (h->pass_pos == 0 && !h->chap_pass) {
882 generr(h, "No User or Chap Password"
883 " attributes given");
886 if (h->pass_pos != 0 && h->chap_pass) {
887 generr(h, "Both User and Chap Password"
888 " attributes given");
894 /* Fill in the length field in the message */
895 h->out[POS_LENGTH] = h->out_len >> 8;
896 h->out[POS_LENGTH+1] = h->out_len;
899 * Count the total number of tries we will make, and zero the
900 * counter for each server.
903 for (srv = 0; srv < h->num_servers; srv++) {
904 h->total_tries += h->servers[srv].max_tries;
905 h->servers[srv].num_tries = 0;
907 if (h->total_tries == 0) {
908 generr(h, "No RADIUS servers specified");
914 return rad_continue_send_request(h, 0, fd, tv);
918 * Create and initialize a rad_handle structure, and return it to the
919 * caller. Can fail only if the necessary memory cannot be allocated.
920 * In that case, it returns NULL.
925 struct rad_handle *h;
927 h = (struct rad_handle *)malloc(sizeof(struct rad_handle));
934 memset(h->pass, 0, sizeof h->pass);
938 h->authentic_pos = 0;
939 h->type = RADIUS_AUTH;
949 struct rad_handle *h;
953 h->type = RADIUS_ACCT;
958 rad_server_open(int fd)
960 struct rad_handle *h;
964 h->type = RADIUS_SERVER;
973 return rad_auth_open();
977 rad_put_addr(struct rad_handle *h, int type, struct in_addr addr)
979 return rad_put_attr(h, type, &addr.s_addr, sizeof addr.s_addr);
983 rad_put_attr(struct rad_handle *h, int type, const void *value, size_t len)
987 if (!h->out_created) {
988 generr(h, "Please call rad_create_request()"
989 " before putting attributes");
993 if (h->out[POS_CODE] == RAD_ACCOUNTING_REQUEST) {
994 if (type == RAD_EAP_MESSAGE) {
995 generr(h, "EAP-Message attribute is not valid"
996 " in accounting requests");
1002 * When proxying EAP Messages, the Message Authenticator
1003 * MUST be present; see RFC 3579.
1005 if (type == RAD_EAP_MESSAGE) {
1006 if (rad_put_message_authentic(h) == -1)
1010 if (type == RAD_USER_PASSWORD) {
1011 result = put_password_attr(h, type, value, len);
1012 } else if (type == RAD_MESSAGE_AUTHENTIC) {
1013 result = rad_put_message_authentic(h);
1015 result = put_raw_attr(h, type, value, len);
1017 if (type == RAD_CHAP_PASSWORD)
1019 else if (type == RAD_EAP_MESSAGE)
1028 rad_put_int(struct rad_handle *h, int type, u_int32_t value)
1032 nvalue = htonl(value);
1033 return rad_put_attr(h, type, &nvalue, sizeof nvalue);
1037 rad_put_string(struct rad_handle *h, int type, const char *str)
1039 return rad_put_attr(h, type, str, strlen(str));
1043 rad_put_message_authentic(struct rad_handle *h)
1046 u_char md_zero[MD5_DIGEST_LENGTH];
1048 if (h->out[POS_CODE] == RAD_ACCOUNTING_REQUEST) {
1049 generr(h, "Message-Authenticator is not valid"
1050 " in accounting requests");
1054 if (h->authentic_pos == 0) {
1055 h->authentic_pos = h->out_len;
1056 memset(md_zero, 0, sizeof(md_zero));
1057 return (put_raw_attr(h, RAD_MESSAGE_AUTHENTIC, md_zero,
1062 generr(h, "Message Authenticator not supported,"
1063 " please recompile libradius with SSL support");
1069 * Returns the response type code on success, or -1 on failure.
1072 rad_send_request(struct rad_handle *h)
1074 struct timeval timelimit;
1079 n = rad_init_send_request(h, &fd, &tv);
1084 gettimeofday(&timelimit, NULL);
1085 timeradd(&tv, &timelimit, &timelimit);
1091 FD_SET(fd, &readfds);
1093 n = select(fd + 1, &readfds, NULL, NULL, &tv);
1096 generr(h, "select: %s", strerror(errno));
1100 if (!FD_ISSET(fd, &readfds)) {
1101 /* Compute a new timeout */
1102 gettimeofday(&tv, NULL);
1103 timersub(&timelimit, &tv, &tv);
1104 if (tv.tv_sec > 0 || (tv.tv_sec == 0 && tv.tv_usec > 0))
1105 /* Continue the select */
1109 n = rad_continue_send_request(h, n, &fd, &tv);
1114 gettimeofday(&timelimit, NULL);
1115 timeradd(&tv, &timelimit, &timelimit);
1120 rad_strerror(struct rad_handle *h)
1126 * Destructively split a string into fields separated by white space.
1127 * `#' at the beginning of a field begins a comment that extends to the
1128 * end of the string. Fields may be quoted with `"'. Inside quoted
1129 * strings, the backslash escapes `\"' and `\\' are honored.
1131 * Pointers to up to the first maxfields fields are stored in the fields
1132 * array. Missing fields get NULL pointers.
1134 * The return value is the actual number of fields parsed, and is always
1137 * On a syntax error, places a message in the msg string, and returns -1.
1140 split(char *str, char *fields[], int maxfields, char *msg, size_t msglen)
1144 static const char ws[] = " \t";
1146 for (i = 0; i < maxfields; i++)
1150 while (*p != '\0') {
1152 if (*p == '#' || *p == '\0')
1154 if (i >= maxfields) {
1155 snprintf(msg, msglen, "line has too many fields");
1166 if (*p != '"' && *p != '\\' &&
1168 snprintf(msg, msglen,
1169 "invalid `\\' escape");
1174 snprintf(msg, msglen,
1175 "unterminated quoted string");
1182 if (*fields[i] == '\0') {
1183 snprintf(msg, msglen,
1184 "empty quoted string not permitted");
1187 if (*p != '\0' && strspn(p, ws) == 0) {
1188 snprintf(msg, msglen, "quoted string not"
1189 " followed by white space");
1194 p += strcspn(p, ws);
1204 rad_get_vendor_attr(u_int32_t *vendor, const void **data, size_t *len)
1206 struct vendor_attribute *attr;
1208 attr = (struct vendor_attribute *)*data;
1209 *vendor = ntohl(attr->vendor_value);
1210 *data = attr->attrib_data;
1211 *len = attr->attrib_len - 2;
1213 return (attr->attrib_type);
1217 rad_put_vendor_addr(struct rad_handle *h, int vendor, int type,
1218 struct in_addr addr)
1220 return (rad_put_vendor_attr(h, vendor, type, &addr.s_addr,
1221 sizeof addr.s_addr));
1225 rad_put_vendor_attr(struct rad_handle *h, int vendor, int type,
1226 const void *value, size_t len)
1228 struct vendor_attribute *attr;
1231 if (!h->out_created) {
1232 generr(h, "Please call rad_create_request()"
1233 " before putting attributes");
1237 if ((attr = malloc(len + 6)) == NULL) {
1238 generr(h, "malloc failure (%zu bytes)", len + 6);
1242 attr->vendor_value = htonl(vendor);
1243 attr->attrib_type = type;
1244 attr->attrib_len = len + 2;
1245 memcpy(attr->attrib_data, value, len);
1247 res = put_raw_attr(h, RAD_VENDOR_SPECIFIC, attr, len + 6);
1249 if (res == 0 && vendor == RAD_VENDOR_MICROSOFT
1250 && (type == RAD_MICROSOFT_MS_CHAP_RESPONSE
1251 || type == RAD_MICROSOFT_MS_CHAP2_RESPONSE)) {
1258 rad_put_vendor_int(struct rad_handle *h, int vendor, int type, u_int32_t i)
1263 return (rad_put_vendor_attr(h, vendor, type, &value, sizeof value));
1267 rad_put_vendor_string(struct rad_handle *h, int vendor, int type,
1270 return (rad_put_vendor_attr(h, vendor, type, str, strlen(str)));
1274 rad_request_authenticator(struct rad_handle *h, char *buf, size_t len)
1278 memcpy(buf, h->out + POS_AUTH, LEN_AUTH);
1280 buf[LEN_AUTH] = '\0';
1285 rad_demangle(struct rad_handle *h, const void *mangled, size_t mlen)
1291 u_char b[MD5_DIGEST_LENGTH], *C, *demangled;
1293 if ((mlen % 16 != 0) || mlen > 128) {
1294 generr(h, "Cannot interpret mangled data of length %lu",
1299 C = (u_char *)mangled;
1301 /* We need the shared secret as Salt */
1302 S = rad_server_secret(h);
1304 /* We need the request authenticator */
1305 if (rad_request_authenticator(h, R, sizeof R) != LEN_AUTH) {
1306 generr(h, "Cannot obtain the RADIUS request authenticator");
1310 demangled = malloc(mlen);
1315 MD5Update(&Context, S, strlen(S));
1316 MD5Update(&Context, R, LEN_AUTH);
1317 MD5Final(b, &Context);
1322 for (i = 0; i < 16; i++)
1323 demangled[Ppos++] = C[i] ^ b[i];
1327 MD5Update(&Context, S, strlen(S));
1328 MD5Update(&Context, C, 16);
1329 MD5Final(b, &Context);
1339 rad_demangle_mppe_key(struct rad_handle *h, const void *mangled,
1340 size_t mlen, size_t *len)
1342 char R[LEN_AUTH]; /* variable names as per rfc2548 */
1344 u_char b[MD5_DIGEST_LENGTH], *demangled;
1345 const u_char *A, *C;
1347 int Slen, i, Clen, Ppos;
1350 if (mlen % 16 != SALT_LEN) {
1351 generr(h, "Cannot interpret mangled data of length %lu",
1356 /* We need the RADIUS Request-Authenticator */
1357 if (rad_request_authenticator(h, R, sizeof R) != LEN_AUTH) {
1358 generr(h, "Cannot obtain the RADIUS request authenticator");
1362 A = (const u_char *)mangled; /* Salt comes first */
1363 C = (const u_char *)mangled + SALT_LEN; /* Then the ciphertext */
1364 Clen = mlen - SALT_LEN;
1365 S = rad_server_secret(h); /* We need the RADIUS secret */
1367 P = alloca(Clen); /* We derive our plaintext */
1370 MD5Update(&Context, S, Slen);
1371 MD5Update(&Context, R, LEN_AUTH);
1372 MD5Update(&Context, A, SALT_LEN);
1373 MD5Final(b, &Context);
1379 for (i = 0; i < 16; i++)
1380 P[Ppos++] = C[i] ^ b[i];
1384 MD5Update(&Context, S, Slen);
1385 MD5Update(&Context, C, 16);
1386 MD5Final(b, &Context);
1393 * The resulting plain text consists of a one-byte length, the text and
1394 * maybe some padding.
1397 if (*len > mlen - 1) {
1398 generr(h, "Mangled data seems to be garbage %zu %zu",
1403 if (*len > MPPE_KEY_LEN * 2) {
1404 generr(h, "Key to long (%zu) for me max. %d",
1405 *len, MPPE_KEY_LEN * 2);
1408 demangled = malloc(*len);
1412 memcpy(demangled, P + 1, *len);
1417 rad_server_secret(struct rad_handle *h)
1419 return (h->servers[h->srv].secret);