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;
759 h->bindto = INADDR_ANY;
764 rad_bind_to(struct rad_handle *h, in_addr_t addr)
770 rad_create_response(struct rad_handle *h, int code)
773 if (h->type != RADIUS_SERVER) {
774 generr(h, "denied function call");
777 h->out[POS_CODE] = code;
778 h->out[POS_IDENT] = h->in[POS_IDENT];
779 memset(&h->out[POS_AUTH], 0, LEN_AUTH);
780 h->out_len = POS_ATTRS;
782 h->authentic_pos = 0;
788 rad_cvt_addr(const void *data)
790 struct in_addr value;
792 memcpy(&value.s_addr, data, sizeof value.s_addr);
797 rad_cvt_int(const void *data)
801 memcpy(&value, data, sizeof value);
806 rad_cvt_string(const void *data, size_t len)
812 memcpy(s, data, len);
819 * Returns the attribute type. If none are left, returns 0. On failure,
823 rad_get_attr(struct rad_handle *h, const void **value, size_t *len)
827 if (h->in_pos >= h->in_len)
829 if (h->in_pos + 2 > h->in_len) {
830 generr(h, "Malformed attribute in response");
833 type = h->in[h->in_pos++];
834 *len = h->in[h->in_pos++] - 2;
835 if (h->in_pos + (int)*len > h->in_len) {
836 generr(h, "Malformed attribute in response");
839 *value = &h->in[h->in_pos];
845 * Returns -1 on error, 0 to indicate no event and >0 for success
848 rad_init_send_request(struct rad_handle *h, int *fd, struct timeval *tv)
852 if (h->type == RADIUS_SERVER) {
853 generr(h, "denied function call");
856 /* Make sure we have a socket to use */
858 struct sockaddr_in sin;
860 if ((h->fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
861 generr(h, "Cannot create socket: %s", strerror(errno));
864 memset(&sin, 0, sizeof sin);
865 sin.sin_len = sizeof sin;
866 sin.sin_family = AF_INET;
867 sin.sin_addr.s_addr = h->bindto;
868 sin.sin_port = htons(0);
869 if (bind(h->fd, (const struct sockaddr *)&sin,
871 generr(h, "bind: %s", strerror(errno));
878 if (h->out[POS_CODE] != RAD_ACCESS_REQUEST) {
879 /* Make sure no password given */
880 if (h->pass_pos || h->chap_pass) {
881 generr(h, "User or Chap Password"
882 " in accounting request");
886 if (h->eap_msg == 0) {
887 /* Make sure the user gave us a password */
888 if (h->pass_pos == 0 && !h->chap_pass) {
889 generr(h, "No User or Chap Password"
890 " attributes given");
893 if (h->pass_pos != 0 && h->chap_pass) {
894 generr(h, "Both User and Chap Password"
895 " attributes given");
901 /* Fill in the length field in the message */
902 h->out[POS_LENGTH] = h->out_len >> 8;
903 h->out[POS_LENGTH+1] = h->out_len;
906 * Count the total number of tries we will make, and zero the
907 * counter for each server.
910 for (srv = 0; srv < h->num_servers; srv++) {
911 h->total_tries += h->servers[srv].max_tries;
912 h->servers[srv].num_tries = 0;
914 if (h->total_tries == 0) {
915 generr(h, "No RADIUS servers specified");
921 return rad_continue_send_request(h, 0, fd, tv);
925 * Create and initialize a rad_handle structure, and return it to the
926 * caller. Can fail only if the necessary memory cannot be allocated.
927 * In that case, it returns NULL.
932 struct rad_handle *h;
934 h = (struct rad_handle *)malloc(sizeof(struct rad_handle));
941 memset(h->pass, 0, sizeof h->pass);
945 h->authentic_pos = 0;
946 h->type = RADIUS_AUTH;
956 struct rad_handle *h;
960 h->type = RADIUS_ACCT;
965 rad_server_open(int fd)
967 struct rad_handle *h;
971 h->type = RADIUS_SERVER;
980 return rad_auth_open();
984 rad_put_addr(struct rad_handle *h, int type, struct in_addr addr)
986 return rad_put_attr(h, type, &addr.s_addr, sizeof addr.s_addr);
990 rad_put_attr(struct rad_handle *h, int type, const void *value, size_t len)
994 if (!h->out_created) {
995 generr(h, "Please call rad_create_request()"
996 " before putting attributes");
1000 if (h->out[POS_CODE] == RAD_ACCOUNTING_REQUEST) {
1001 if (type == RAD_EAP_MESSAGE) {
1002 generr(h, "EAP-Message attribute is not valid"
1003 " in accounting requests");
1009 * When proxying EAP Messages, the Message Authenticator
1010 * MUST be present; see RFC 3579.
1012 if (type == RAD_EAP_MESSAGE) {
1013 if (rad_put_message_authentic(h) == -1)
1017 if (type == RAD_USER_PASSWORD) {
1018 result = put_password_attr(h, type, value, len);
1019 } else if (type == RAD_MESSAGE_AUTHENTIC) {
1020 result = rad_put_message_authentic(h);
1022 result = put_raw_attr(h, type, value, len);
1024 if (type == RAD_CHAP_PASSWORD)
1026 else if (type == RAD_EAP_MESSAGE)
1035 rad_put_int(struct rad_handle *h, int type, u_int32_t value)
1039 nvalue = htonl(value);
1040 return rad_put_attr(h, type, &nvalue, sizeof nvalue);
1044 rad_put_string(struct rad_handle *h, int type, const char *str)
1046 return rad_put_attr(h, type, str, strlen(str));
1050 rad_put_message_authentic(struct rad_handle *h)
1053 u_char md_zero[MD5_DIGEST_LENGTH];
1055 if (h->out[POS_CODE] == RAD_ACCOUNTING_REQUEST) {
1056 generr(h, "Message-Authenticator is not valid"
1057 " in accounting requests");
1061 if (h->authentic_pos == 0) {
1062 h->authentic_pos = h->out_len;
1063 memset(md_zero, 0, sizeof(md_zero));
1064 return (put_raw_attr(h, RAD_MESSAGE_AUTHENTIC, md_zero,
1069 generr(h, "Message Authenticator not supported,"
1070 " please recompile libradius with SSL support");
1076 * Returns the response type code on success, or -1 on failure.
1079 rad_send_request(struct rad_handle *h)
1081 struct timeval timelimit;
1086 n = rad_init_send_request(h, &fd, &tv);
1091 gettimeofday(&timelimit, NULL);
1092 timeradd(&tv, &timelimit, &timelimit);
1098 FD_SET(fd, &readfds);
1100 n = select(fd + 1, &readfds, NULL, NULL, &tv);
1103 generr(h, "select: %s", strerror(errno));
1107 if (!FD_ISSET(fd, &readfds)) {
1108 /* Compute a new timeout */
1109 gettimeofday(&tv, NULL);
1110 timersub(&timelimit, &tv, &tv);
1111 if (tv.tv_sec > 0 || (tv.tv_sec == 0 && tv.tv_usec > 0))
1112 /* Continue the select */
1116 n = rad_continue_send_request(h, n, &fd, &tv);
1121 gettimeofday(&timelimit, NULL);
1122 timeradd(&tv, &timelimit, &timelimit);
1127 rad_strerror(struct rad_handle *h)
1133 * Destructively split a string into fields separated by white space.
1134 * `#' at the beginning of a field begins a comment that extends to the
1135 * end of the string. Fields may be quoted with `"'. Inside quoted
1136 * strings, the backslash escapes `\"' and `\\' are honored.
1138 * Pointers to up to the first maxfields fields are stored in the fields
1139 * array. Missing fields get NULL pointers.
1141 * The return value is the actual number of fields parsed, and is always
1144 * On a syntax error, places a message in the msg string, and returns -1.
1147 split(char *str, char *fields[], int maxfields, char *msg, size_t msglen)
1151 static const char ws[] = " \t";
1153 for (i = 0; i < maxfields; i++)
1157 while (*p != '\0') {
1159 if (*p == '#' || *p == '\0')
1161 if (i >= maxfields) {
1162 snprintf(msg, msglen, "line has too many fields");
1173 if (*p != '"' && *p != '\\' &&
1175 snprintf(msg, msglen,
1176 "invalid `\\' escape");
1181 snprintf(msg, msglen,
1182 "unterminated quoted string");
1189 if (*fields[i] == '\0') {
1190 snprintf(msg, msglen,
1191 "empty quoted string not permitted");
1194 if (*p != '\0' && strspn(p, ws) == 0) {
1195 snprintf(msg, msglen, "quoted string not"
1196 " followed by white space");
1201 p += strcspn(p, ws);
1211 rad_get_vendor_attr(u_int32_t *vendor, const void **data, size_t *len)
1213 struct vendor_attribute *attr;
1215 attr = (struct vendor_attribute *)*data;
1216 *vendor = ntohl(attr->vendor_value);
1217 *data = attr->attrib_data;
1218 *len = attr->attrib_len - 2;
1220 return (attr->attrib_type);
1224 rad_put_vendor_addr(struct rad_handle *h, int vendor, int type,
1225 struct in_addr addr)
1227 return (rad_put_vendor_attr(h, vendor, type, &addr.s_addr,
1228 sizeof addr.s_addr));
1232 rad_put_vendor_attr(struct rad_handle *h, int vendor, int type,
1233 const void *value, size_t len)
1235 struct vendor_attribute *attr;
1238 if (!h->out_created) {
1239 generr(h, "Please call rad_create_request()"
1240 " before putting attributes");
1244 if ((attr = malloc(len + 6)) == NULL) {
1245 generr(h, "malloc failure (%zu bytes)", len + 6);
1249 attr->vendor_value = htonl(vendor);
1250 attr->attrib_type = type;
1251 attr->attrib_len = len + 2;
1252 memcpy(attr->attrib_data, value, len);
1254 res = put_raw_attr(h, RAD_VENDOR_SPECIFIC, attr, len + 6);
1256 if (res == 0 && vendor == RAD_VENDOR_MICROSOFT
1257 && (type == RAD_MICROSOFT_MS_CHAP_RESPONSE
1258 || type == RAD_MICROSOFT_MS_CHAP2_RESPONSE)) {
1265 rad_put_vendor_int(struct rad_handle *h, int vendor, int type, u_int32_t i)
1270 return (rad_put_vendor_attr(h, vendor, type, &value, sizeof value));
1274 rad_put_vendor_string(struct rad_handle *h, int vendor, int type,
1277 return (rad_put_vendor_attr(h, vendor, type, str, strlen(str)));
1281 rad_request_authenticator(struct rad_handle *h, char *buf, size_t len)
1285 memcpy(buf, h->out + POS_AUTH, LEN_AUTH);
1287 buf[LEN_AUTH] = '\0';
1292 rad_demangle(struct rad_handle *h, const void *mangled, size_t mlen)
1298 u_char b[MD5_DIGEST_LENGTH], *C, *demangled;
1300 if ((mlen % 16 != 0) || mlen > 128) {
1301 generr(h, "Cannot interpret mangled data of length %lu",
1306 C = (u_char *)mangled;
1308 /* We need the shared secret as Salt */
1309 S = rad_server_secret(h);
1311 /* We need the request authenticator */
1312 if (rad_request_authenticator(h, R, sizeof R) != LEN_AUTH) {
1313 generr(h, "Cannot obtain the RADIUS request authenticator");
1317 demangled = malloc(mlen);
1322 MD5Update(&Context, S, strlen(S));
1323 MD5Update(&Context, R, LEN_AUTH);
1324 MD5Final(b, &Context);
1329 for (i = 0; i < 16; i++)
1330 demangled[Ppos++] = C[i] ^ b[i];
1334 MD5Update(&Context, S, strlen(S));
1335 MD5Update(&Context, C, 16);
1336 MD5Final(b, &Context);
1346 rad_demangle_mppe_key(struct rad_handle *h, const void *mangled,
1347 size_t mlen, size_t *len)
1349 char R[LEN_AUTH]; /* variable names as per rfc2548 */
1351 u_char b[MD5_DIGEST_LENGTH], *demangled;
1352 const u_char *A, *C;
1354 int Slen, i, Clen, Ppos;
1357 if (mlen % 16 != SALT_LEN) {
1358 generr(h, "Cannot interpret mangled data of length %lu",
1363 /* We need the RADIUS Request-Authenticator */
1364 if (rad_request_authenticator(h, R, sizeof R) != LEN_AUTH) {
1365 generr(h, "Cannot obtain the RADIUS request authenticator");
1369 A = (const u_char *)mangled; /* Salt comes first */
1370 C = (const u_char *)mangled + SALT_LEN; /* Then the ciphertext */
1371 Clen = mlen - SALT_LEN;
1372 S = rad_server_secret(h); /* We need the RADIUS secret */
1374 P = alloca(Clen); /* We derive our plaintext */
1377 MD5Update(&Context, S, Slen);
1378 MD5Update(&Context, R, LEN_AUTH);
1379 MD5Update(&Context, A, SALT_LEN);
1380 MD5Final(b, &Context);
1386 for (i = 0; i < 16; i++)
1387 P[Ppos++] = C[i] ^ b[i];
1391 MD5Update(&Context, S, Slen);
1392 MD5Update(&Context, C, 16);
1393 MD5Final(b, &Context);
1400 * The resulting plain text consists of a one-byte length, the text and
1401 * maybe some padding.
1404 if (*len > mlen - 1) {
1405 generr(h, "Mangled data seems to be garbage %zu %zu",
1410 if (*len > MPPE_KEY_LEN * 2) {
1411 generr(h, "Key to long (%zu) for me max. %d",
1412 *len, MPPE_KEY_LEN * 2);
1415 demangled = malloc(*len);
1419 memcpy(demangled, P + 1, *len);
1424 rad_server_secret(struct rad_handle *h)
1426 return (h->servers[h->srv].secret);