radlib.c (1ee774f614e70c52baafdf8b7e9013b545d36f2b) | radlib.c (082bfe6741360907d0bfad91e4e836e02c361395) |
---|---|
1/*- 2 * Copyright 1998 Juniper Networks, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 8 unchanged lines hidden (view full) --- 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 24 * SUCH DAMAGE. | 1/*- 2 * Copyright 1998 Juniper Networks, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 8 unchanged lines hidden (view full) --- 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 24 * SUCH DAMAGE. |
25 * 26 * $FreeBSD$ |
|
25 */ 26 | 27 */ 28 |
27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD$"); 29 | |
30#include <sys/types.h> 31#include <sys/socket.h> 32#include <sys/time.h> 33#include <netinet/in.h> 34#include <arpa/inet.h> | 29#include <sys/types.h> 30#include <sys/socket.h> 31#include <sys/time.h> 32#include <netinet/in.h> 33#include <arpa/inet.h> |
35#ifdef WITH_SSL 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 41#else 42#define MD5_DIGEST_LENGTH 16 43#include <md5.h> 44#endif | |
45 | 34 |
46/* We need the MPPE_KEY_LEN define */ 47#include <netgraph/ng_mppc.h> 48 | |
49#include <errno.h> | 35#include <errno.h> |
36#include <md5.h> |
|
50#include <netdb.h> 51#include <stdarg.h> 52#include <stddef.h> 53#include <stdio.h> 54#include <stdlib.h> 55#include <string.h> 56#include <unistd.h> 57 58#include "radlib_private.h" 59 60static void clear_password(struct rad_handle *); 61static void generr(struct rad_handle *, const char *, ...) 62 __printflike(2, 3); 63static void insert_scrambled_password(struct rad_handle *, int); | 37#include <netdb.h> 38#include <stdarg.h> 39#include <stddef.h> 40#include <stdio.h> 41#include <stdlib.h> 42#include <string.h> 43#include <unistd.h> 44 45#include "radlib_private.h" 46 47static void clear_password(struct rad_handle *); 48static void generr(struct rad_handle *, const char *, ...) 49 __printflike(2, 3); 50static void insert_scrambled_password(struct rad_handle *, int); |
64static void insert_request_authenticator(struct rad_handle *, int); 65static void insert_message_authenticator(struct rad_handle *, int); | |
66static int is_valid_response(struct rad_handle *, int, 67 const struct sockaddr_in *); 68static int put_password_attr(struct rad_handle *, int, 69 const void *, size_t); 70static int put_raw_attr(struct rad_handle *, int, 71 const void *, size_t); 72static int split(char *, char *[], int, char *, size_t); 73 74static void 75clear_password(struct rad_handle *h) 76{ 77 if (h->pass_len != 0) { 78 memset(h->pass, 0, h->pass_len); 79 h->pass_len = 0; | 51static int is_valid_response(struct rad_handle *, int, 52 const struct sockaddr_in *); 53static int put_password_attr(struct rad_handle *, int, 54 const void *, size_t); 55static int put_raw_attr(struct rad_handle *, int, 56 const void *, size_t); 57static int split(char *, char *[], int, char *, size_t); 58 59static void 60clear_password(struct rad_handle *h) 61{ 62 if (h->pass_len != 0) { 63 memset(h->pass, 0, h->pass_len); 64 h->pass_len = 0; |
65 h->pass_pos = 0; |
|
80 } | 66 } |
81 h->pass_pos = 0; | |
82} 83 84static void 85generr(struct rad_handle *h, const char *format, ...) 86{ 87 va_list ap; 88 89 va_start(ap, format); 90 vsnprintf(h->errmsg, ERRSIZE, format, ap); 91 va_end(ap); 92} 93 94static void 95insert_scrambled_password(struct rad_handle *h, int srv) 96{ 97 MD5_CTX ctx; | 67} 68 69static void 70generr(struct rad_handle *h, const char *format, ...) 71{ 72 va_list ap; 73 74 va_start(ap, format); 75 vsnprintf(h->errmsg, ERRSIZE, format, ap); 76 va_end(ap); 77} 78 79static void 80insert_scrambled_password(struct rad_handle *h, int srv) 81{ 82 MD5_CTX ctx; |
98 unsigned char md5[MD5_DIGEST_LENGTH]; | 83 unsigned char md5[16]; |
99 const struct rad_server *srvp; 100 int padded_len; 101 int pos; 102 103 srvp = &h->servers[srv]; 104 padded_len = h->pass_len == 0 ? 16 : (h->pass_len+15) & ~0xf; 105 | 84 const struct rad_server *srvp; 85 int padded_len; 86 int pos; 87 88 srvp = &h->servers[srv]; 89 padded_len = h->pass_len == 0 ? 16 : (h->pass_len+15) & ~0xf; 90 |
106 memcpy(md5, &h->out[POS_AUTH], LEN_AUTH); | 91 memcpy(md5, &h->request[POS_AUTH], LEN_AUTH); |
107 for (pos = 0; pos < padded_len; pos += 16) { 108 int i; 109 110 /* Calculate the new scrambler */ 111 MD5Init(&ctx); 112 MD5Update(&ctx, srvp->secret, strlen(srvp->secret)); 113 MD5Update(&ctx, md5, 16); 114 MD5Final(md5, &ctx); 115 116 /* 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. 121 */ 122 for (i = 0; i < 16; i++) | 92 for (pos = 0; pos < padded_len; pos += 16) { 93 int i; 94 95 /* Calculate the new scrambler */ 96 MD5Init(&ctx); 97 MD5Update(&ctx, srvp->secret, strlen(srvp->secret)); 98 MD5Update(&ctx, md5, 16); 99 MD5Final(md5, &ctx); 100 101 /* 102 * Mix in the current chunk of the password, and copy 103 * the result into the right place in the request. Also 104 * modify the scrambler in place, since we will use this 105 * in calculating the scrambler for next time. 106 */ 107 for (i = 0; i < 16; i++) |
123 h->out[h->pass_pos + pos + i] = | 108 h->request[h->pass_pos + pos + i] = |
124 md5[i] ^= h->pass[pos + i]; 125 } 126} 127 | 109 md5[i] ^= h->pass[pos + i]; 110 } 111} 112 |
128static void 129insert_request_authenticator(struct rad_handle *h, int resp) 130{ 131 MD5_CTX ctx; 132 const struct rad_server *srvp; 133 134 srvp = &h->servers[h->srv]; 135 136 /* Create the request authenticator */ 137 MD5Init(&ctx); 138 MD5Update(&ctx, &h->out[POS_CODE], POS_AUTH - POS_CODE); 139 if (resp) 140 MD5Update(&ctx, &h->in[POS_AUTH], LEN_AUTH); 141 else 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); 146} 147 148static void 149insert_message_authenticator(struct rad_handle *h, int resp) 150{ 151#ifdef WITH_SSL 152 u_char md[EVP_MAX_MD_SIZE]; 153 u_int md_len; 154 const struct rad_server *srvp; 155 HMAC_CTX ctx; 156 srvp = &h->servers[h->srv]; 157 158 if (h->authentic_pos != 0) { 159 HMAC_CTX_init(&ctx); 160 HMAC_Init(&ctx, srvp->secret, strlen(srvp->secret), EVP_md5()); 161 HMAC_Update(&ctx, &h->out[POS_CODE], POS_AUTH - POS_CODE); 162 if (resp) 163 HMAC_Update(&ctx, &h->in[POS_AUTH], LEN_AUTH); 164 else 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); 170 HMAC_cleanup(&ctx); 171 memcpy(&h->out[h->authentic_pos + 2], md, md_len); 172 } 173#endif 174} 175 | |
176/* 177 * Return true if the current response is valid for a request to the 178 * specified server. 179 */ 180static int 181is_valid_response(struct rad_handle *h, int srv, 182 const struct sockaddr_in *from) 183{ 184 MD5_CTX ctx; | 113/* 114 * Return true if the current response is valid for a request to the 115 * specified server. 116 */ 117static int 118is_valid_response(struct rad_handle *h, int srv, 119 const struct sockaddr_in *from) 120{ 121 MD5_CTX ctx; |
185 unsigned char md5[MD5_DIGEST_LENGTH]; | 122 unsigned char md5[16]; |
186 const struct rad_server *srvp; 187 int len; | 123 const struct rad_server *srvp; 124 int len; |
188#ifdef WITH_SSL 189 HMAC_CTX hctx; 190 u_char resp[MSGSIZE], md[EVP_MAX_MD_SIZE]; 191 u_int md_len; 192 int pos; 193#endif | |
194 195 srvp = &h->servers[srv]; 196 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) 201 return 0; 202 203 /* Check the message length */ | 125 126 srvp = &h->servers[srv]; 127 128 /* Check the source address */ 129 if (from->sin_family != srvp->addr.sin_family || 130 from->sin_addr.s_addr != srvp->addr.sin_addr.s_addr || 131 from->sin_port != srvp->addr.sin_port) 132 return 0; 133 134 /* Check the message length */ |
204 if (h->in_len < POS_ATTRS) | 135 if (h->resp_len < POS_ATTRS) |
205 return 0; | 136 return 0; |
206 len = h->in[POS_LENGTH] << 8 | h->in[POS_LENGTH+1]; 207 if (len > h->in_len) | 137 len = h->response[POS_LENGTH] << 8 | h->response[POS_LENGTH+1]; 138 if (len > h->resp_len) |
208 return 0; 209 210 /* Check the response authenticator */ 211 MD5Init(&ctx); | 139 return 0; 140 141 /* Check the response authenticator */ 142 MD5Init(&ctx); |
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); | 143 MD5Update(&ctx, &h->response[POS_CODE], POS_AUTH - POS_CODE); 144 MD5Update(&ctx, &h->request[POS_AUTH], LEN_AUTH); 145 MD5Update(&ctx, &h->response[POS_ATTRS], len - POS_ATTRS); |
215 MD5Update(&ctx, srvp->secret, strlen(srvp->secret)); 216 MD5Final(md5, &ctx); | 146 MD5Update(&ctx, srvp->secret, strlen(srvp->secret)); 147 MD5Final(md5, &ctx); |
217 if (memcmp(&h->in[POS_AUTH], md5, sizeof md5) != 0) | 148 if (memcmp(&h->response[POS_AUTH], md5, sizeof md5) != 0) |
218 return 0; 219 | 149 return 0; 150 |
220#ifdef WITH_SSL 221 /* 222 * For non accounting responses check the message authenticator, 223 * if any. 224 */ 225 if (h->in[POS_CODE] != RAD_ACCOUNTING_RESPONSE) { 226 227 memcpy(resp, h->in, MSGSIZE); 228 pos = POS_ATTRS; 229 230 /* Search and verify the Message-Authenticator */ 231 while (pos < len - 2) { 232 233 if (h->in[pos] == RAD_MESSAGE_AUTHENTIC) { 234 /* zero fill the Message-Authenticator */ 235 memset(&resp[pos + 2], 0, MD5_DIGEST_LENGTH); 236 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], 243 LEN_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); 248 HMAC_cleanup(&hctx); 249 if (memcmp(md, &h->in[pos + 2], 250 MD5_DIGEST_LENGTH) != 0) 251 return 0; 252 break; 253 } 254 pos += h->in[pos + 1]; 255 } 256 } 257#endif | |
258 return 1; 259} 260 | 151 return 1; 152} 153 |
261/* 262 * Return true if the current request is valid for the specified server. 263 */ | |
264static int | 154static int |
265is_valid_request(struct rad_handle *h) 266{ 267 MD5_CTX ctx; 268 unsigned char md5[MD5_DIGEST_LENGTH]; 269 const struct rad_server *srvp; 270 int len; 271#ifdef WITH_SSL 272 HMAC_CTX hctx; 273 u_char resp[MSGSIZE], md[EVP_MAX_MD_SIZE]; 274 u_int md_len; 275 int pos; 276#endif 277 278 srvp = &h->servers[h->srv]; 279 280 /* Check the message length */ 281 if (h->in_len < POS_ATTRS) 282 return (0); 283 len = h->in[POS_LENGTH] << 8 | h->in[POS_LENGTH+1]; 284 if (len > h->in_len) 285 return (0); 286 287 if (h->in[POS_CODE] != RAD_ACCESS_REQUEST) { 288 uint32_t zeroes[4] = { 0, 0, 0, 0 }; 289 /* Check the request authenticator */ 290 MD5Init(&ctx); 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)); 295 MD5Final(md5, &ctx); 296 if (memcmp(&h->in[POS_AUTH], md5, sizeof md5) != 0) 297 return (0); 298 } 299 300#ifdef WITH_SSL 301 /* Search and verify the Message-Authenticator */ 302 pos = POS_ATTRS; 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); 311 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); 318 HMAC_cleanup(&hctx); 319 if (memcmp(md, &h->in[pos + 2], 320 MD5_DIGEST_LENGTH) != 0) 321 return (0); 322 break; 323 } 324 pos += h->in[pos + 1]; 325 } 326#endif 327 return (1); 328} 329 330static int | |
331put_password_attr(struct rad_handle *h, int type, const void *value, size_t len) 332{ 333 int padded_len; 334 int pad_len; 335 336 if (h->pass_pos != 0) { 337 generr(h, "Multiple User-Password attributes specified"); 338 return -1; --- 4 unchanged lines hidden (view full) --- 343 pad_len = padded_len - len; 344 345 /* 346 * Put in a place-holder attribute containing all zeros, and 347 * remember where it is so we can fill it in later. 348 */ 349 clear_password(h); 350 put_raw_attr(h, type, h->pass, padded_len); | 155put_password_attr(struct rad_handle *h, int type, const void *value, size_t len) 156{ 157 int padded_len; 158 int pad_len; 159 160 if (h->pass_pos != 0) { 161 generr(h, "Multiple User-Password attributes specified"); 162 return -1; --- 4 unchanged lines hidden (view full) --- 167 pad_len = padded_len - len; 168 169 /* 170 * Put in a place-holder attribute containing all zeros, and 171 * remember where it is so we can fill it in later. 172 */ 173 clear_password(h); 174 put_raw_attr(h, type, h->pass, padded_len); |
351 h->pass_pos = h->out_len - padded_len; | 175 h->pass_pos = h->req_len - padded_len; |
352 353 /* Save the cleartext password, padded as necessary */ 354 memcpy(h->pass, value, len); 355 h->pass_len = len; 356 memset(h->pass + len, 0, pad_len); 357 return 0; 358} 359 360static int 361put_raw_attr(struct rad_handle *h, int type, const void *value, size_t len) 362{ 363 if (len > 253) { 364 generr(h, "Attribute too long"); 365 return -1; 366 } | 176 177 /* Save the cleartext password, padded as necessary */ 178 memcpy(h->pass, value, len); 179 h->pass_len = len; 180 memset(h->pass + len, 0, pad_len); 181 return 0; 182} 183 184static int 185put_raw_attr(struct rad_handle *h, int type, const void *value, size_t len) 186{ 187 if (len > 253) { 188 generr(h, "Attribute too long"); 189 return -1; 190 } |
367 if (h->out_len + 2 + len > MSGSIZE) { | 191 if (h->req_len + 2 + len > MSGSIZE) { |
368 generr(h, "Maximum message length exceeded"); 369 return -1; 370 } | 192 generr(h, "Maximum message length exceeded"); 193 return -1; 194 } |
371 h->out[h->out_len++] = type; 372 h->out[h->out_len++] = len + 2; 373 memcpy(&h->out[h->out_len], value, len); 374 h->out_len += len; | 195 h->request[h->req_len++] = type; 196 h->request[h->req_len++] = len + 2; 197 memcpy(&h->request[h->req_len], value, len); 198 h->req_len += len; |
375 return 0; 376} 377 378int 379rad_add_server(struct rad_handle *h, const char *host, int port, 380 const char *secret, int timeout, int tries) 381{ 382 struct rad_server *srvp; --- 13 unchanged lines hidden (view full) --- 396 if ((hent = gethostbyname(host)) == NULL) { 397 generr(h, "%s: host not found", host); 398 return -1; 399 } 400 memcpy(&srvp->addr.sin_addr, hent->h_addr, 401 sizeof srvp->addr.sin_addr); 402 } 403 if (port != 0) | 199 return 0; 200} 201 202int 203rad_add_server(struct rad_handle *h, const char *host, int port, 204 const char *secret, int timeout, int tries) 205{ 206 struct rad_server *srvp; --- 13 unchanged lines hidden (view full) --- 220 if ((hent = gethostbyname(host)) == NULL) { 221 generr(h, "%s: host not found", host); 222 return -1; 223 } 224 memcpy(&srvp->addr.sin_addr, hent->h_addr, 225 sizeof srvp->addr.sin_addr); 226 } 227 if (port != 0) |
404 srvp->addr.sin_port = htons((u_short)port); | 228 srvp->addr.sin_port = htons(port); |
405 else { 406 struct servent *sent; 407 | 229 else { 230 struct servent *sent; 231 |
408 if (h->type == RADIUS_AUTH) 409 srvp->addr.sin_port = 410 (sent = getservbyname("radius", "udp")) != NULL ? 411 sent->s_port : htons(RADIUS_PORT); 412 else 413 srvp->addr.sin_port = 414 (sent = getservbyname("radacct", "udp")) != NULL ? 415 sent->s_port : htons(RADACCT_PORT); | 232 srvp->addr.sin_port = 233 (sent = getservbyname("radius", "udp")) != NULL ? 234 sent->s_port : htons(RADIUS_PORT); |
416 } 417 if ((srvp->secret = strdup(secret)) == NULL) { 418 generr(h, "Out of memory"); 419 return -1; 420 } 421 srvp->timeout = timeout; 422 srvp->max_tries = tries; 423 srvp->num_tries = 0; --- 30 unchanged lines hidden (view full) --- 454 if ((fp = fopen(path, "r")) == NULL) { 455 generr(h, "Cannot open \"%s\": %s", path, strerror(errno)); 456 return -1; 457 } 458 retval = 0; 459 linenum = 0; 460 while (fgets(buf, sizeof buf, fp) != NULL) { 461 int len; | 235 } 236 if ((srvp->secret = strdup(secret)) == NULL) { 237 generr(h, "Out of memory"); 238 return -1; 239 } 240 srvp->timeout = timeout; 241 srvp->max_tries = tries; 242 srvp->num_tries = 0; --- 30 unchanged lines hidden (view full) --- 273 if ((fp = fopen(path, "r")) == NULL) { 274 generr(h, "Cannot open \"%s\": %s", path, strerror(errno)); 275 return -1; 276 } 277 retval = 0; 278 linenum = 0; 279 while (fgets(buf, sizeof buf, fp) != NULL) { 280 int len; |
462 char *fields[5]; | 281 char *fields[4]; |
463 int nfields; 464 char msg[ERRSIZE]; | 282 int nfields; 283 char msg[ERRSIZE]; |
465 char *type; 466 char *host, *res; | 284 char *host; |
467 char *port_str; 468 char *secret; 469 char *timeout_str; 470 char *maxtries_str; 471 char *end; | 285 char *port_str; 286 char *secret; 287 char *timeout_str; 288 char *maxtries_str; 289 char *end; |
472 char *wanttype; | |
473 unsigned long timeout; 474 unsigned long maxtries; 475 int port; | 290 unsigned long timeout; 291 unsigned long maxtries; 292 int port; |
476 int i; | |
477 478 linenum++; 479 len = strlen(buf); 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, 484 linenum); 485 else 486 generr(h, "%s:%d: missing newline", path, 487 linenum); 488 retval = -1; 489 break; 490 } 491 buf[len - 1] = '\0'; 492 493 /* Extract the fields from the line. */ | 293 294 linenum++; 295 len = strlen(buf); 296 /* We know len > 0, else fgets would have returned NULL. */ 297 if (buf[len - 1] != '\n') { 298 if (len == sizeof buf - 1) 299 generr(h, "%s:%d: line too long", path, 300 linenum); 301 else 302 generr(h, "%s:%d: missing newline", path, 303 linenum); 304 retval = -1; 305 break; 306 } 307 buf[len - 1] = '\0'; 308 309 /* Extract the fields from the line. */ |
494 nfields = split(buf, fields, 5, msg, sizeof msg); | 310 nfields = split(buf, fields, 4, msg, sizeof msg); |
495 if (nfields == -1) { 496 generr(h, "%s:%d: %s", path, linenum, msg); 497 retval = -1; 498 break; 499 } 500 if (nfields == 0) 501 continue; | 311 if (nfields == -1) { 312 generr(h, "%s:%d: %s", path, linenum, msg); 313 retval = -1; 314 break; 315 } 316 if (nfields == 0) 317 continue; |
502 /* 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. 507 */ 508 if (strcmp(fields[0], "auth") != 0 && 509 strcmp(fields[0], "acct") != 0) { 510 if (nfields >= 5) { 511 generr(h, "%s:%d: invalid service type", path, 512 linenum); 513 retval = -1; 514 break; 515 } 516 nfields++; 517 for (i = nfields; --i > 0; ) 518 fields[i] = fields[i - 1]; 519 fields[0] = "auth"; 520 } 521 if (nfields < 3) { | 318 if (nfields < 2) { |
522 generr(h, "%s:%d: missing shared secret", path, 523 linenum); 524 retval = -1; 525 break; 526 } | 319 generr(h, "%s:%d: missing shared secret", path, 320 linenum); 321 retval = -1; 322 break; 323 } |
527 type = fields[0]; 528 host = fields[1]; 529 secret = fields[2]; 530 timeout_str = fields[3]; 531 maxtries_str = fields[4]; | 324 host = fields[0]; 325 secret = fields[1]; 326 timeout_str = fields[2]; 327 maxtries_str = fields[3]; |
532 | 328 |
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) 536 continue; 537 | |
538 /* Parse and validate the fields. */ | 329 /* Parse and validate the fields. */ |
539 res = host; 540 host = strsep(&res, ":"); 541 port_str = strsep(&res, ":"); | 330 host = strtok(host, ":"); 331 port_str = strtok(NULL, ":"); |
542 if (port_str != NULL) { 543 port = strtoul(port_str, &end, 10); 544 if (*end != '\0') { 545 generr(h, "%s:%d: invalid port", path, 546 linenum); 547 retval = -1; 548 break; 549 } --- 17 unchanged lines hidden (view full) --- 567 retval = -1; 568 break; 569 } 570 } else 571 maxtries = MAXTRIES; 572 573 if (rad_add_server(h, host, port, secret, timeout, maxtries) == 574 -1) { | 332 if (port_str != NULL) { 333 port = strtoul(port_str, &end, 10); 334 if (*end != '\0') { 335 generr(h, "%s:%d: invalid port", path, 336 linenum); 337 retval = -1; 338 break; 339 } --- 17 unchanged lines hidden (view full) --- 357 retval = -1; 358 break; 359 } 360 } else 361 maxtries = MAXTRIES; 362 363 if (rad_add_server(h, host, port, secret, timeout, maxtries) == 364 -1) { |
365 char msg[ERRSIZE]; 366 |
|
575 strcpy(msg, h->errmsg); 576 generr(h, "%s:%d: %s", path, linenum, msg); 577 retval = -1; 578 break; 579 } 580 } 581 /* Clear out the buffer to wipe a possible copy of a shared secret */ 582 memset(buf, 0, sizeof buf); 583 fclose(fp); 584 return retval; 585} 586 | 367 strcpy(msg, h->errmsg); 368 generr(h, "%s:%d: %s", path, linenum, msg); 369 retval = -1; 370 break; 371 } 372 } 373 /* Clear out the buffer to wipe a possible copy of a shared secret */ 374 memset(buf, 0, sizeof buf); 375 fclose(fp); 376 return retval; 377} 378 |
587/* 588 * rad_init_send_request() must have previously been called. 589 * Returns: 590 * 0 The application should select on *fd with a timeout of tv before 591 * calling rad_continue_send_request again. 592 * < 0 Failure 593 * > 0 Success 594 */ | |
595int | 379int |
596rad_continue_send_request(struct rad_handle *h, int selected, int *fd, 597 struct timeval *tv) 598{ 599 int n; 600 601 if (h->type == RADIUS_SERVER) { 602 generr(h, "denied function call"); 603 return (-1); 604 } 605 if (selected) { 606 struct sockaddr_in from; 607 socklen_t fromlen; 608 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)); 614 return -1; 615 } 616 if (is_valid_response(h, h->srv, &from)) { 617 h->in_len = h->in[POS_LENGTH] << 8 | 618 h->in[POS_LENGTH+1]; 619 h->in_pos = POS_ATTRS; 620 return h->in[POS_CODE]; 621 } 622 } 623 624 if (h->try == h->total_tries) { 625 generr(h, "No valid RADIUS responses received"); 626 return -1; 627 } 628 629 /* 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. 633 */ 634 while (h->servers[h->srv].num_tries >= h->servers[h->srv].max_tries) 635 if (++h->srv >= h->num_servers) 636 h->srv = 0; 637 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); 642 } 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); 647 } 648 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); 653 if (n != h->out_len) 654 tv->tv_sec = 1; /* Do not wait full timeout if send failed. */ 655 else 656 tv->tv_sec = h->servers[h->srv].timeout; 657 h->try++; 658 h->servers[h->srv].num_tries++; 659 tv->tv_usec = 0; 660 *fd = h->fd; 661 662 return 0; 663} 664 665int 666rad_receive_request(struct rad_handle *h) 667{ 668 struct sockaddr_in from; 669 socklen_t fromlen; 670 int n; 671 672 if (h->type != RADIUS_SERVER) { 673 generr(h, "denied function call"); 674 return (-1); 675 } 676 h->srv = -1; 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)); 682 return (-1); 683 } 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; 687 h->srv = n; 688 break; 689 } 690 } 691 if (h->srv == -1) 692 return (-2); 693 if (is_valid_request(h)) { 694 h->in_len = h->in[POS_LENGTH] << 8 | 695 h->in[POS_LENGTH+1]; 696 h->in_pos = POS_ATTRS; 697 return (h->in[POS_CODE]); 698 } 699 return (-3); 700} 701 702int 703rad_send_response(struct rad_handle *h) 704{ 705 int n; 706 707 if (h->type != RADIUS_SERVER) { 708 generr(h, "denied function call"); 709 return (-1); 710 } 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; 714 715 insert_message_authenticator(h, 716 (h->in[POS_CODE] == RAD_ACCESS_REQUEST) ? 1 : 0); 717 insert_request_authenticator(h, 1); 718 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) { 724 if (n == -1) 725 generr(h, "sendto: %s", strerror(errno)); 726 else 727 generr(h, "sendto: short write"); 728 return -1; 729 } 730 731 return 0; 732} 733 734int | |
735rad_create_request(struct rad_handle *h, int code) 736{ 737 int i; 738 | 380rad_create_request(struct rad_handle *h, int code) 381{ 382 int i; 383 |
739 if (h->type == RADIUS_SERVER) { 740 generr(h, "denied function call"); 741 return (-1); | 384 h->request[POS_CODE] = code; 385 h->request[POS_IDENT] = ++h->ident; 386 /* Create a random authenticator */ 387 for (i = 0; i < LEN_AUTH; i += 2) { 388 long r; 389 r = random(); 390 h->request[POS_AUTH+i] = r; 391 h->request[POS_AUTH+i+1] = r >> 8; |
742 } | 392 } |
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) { 748 long r; 749 r = random(); 750 h->out[POS_AUTH+i] = (u_char)r; 751 h->out[POS_AUTH+i+1] = (u_char)(r >> 8); 752 } 753 } else 754 memset(&h->out[POS_AUTH], 0, LEN_AUTH); 755 h->out_len = POS_ATTRS; | 393 h->req_len = POS_ATTRS; |
756 clear_password(h); | 394 clear_password(h); |
757 h->authentic_pos = 0; 758 h->out_created = 1; | |
759 return 0; 760} 761 | 395 return 0; 396} 397 |
762int 763rad_create_response(struct rad_handle *h, int code) 764{ 765 766 if (h->type != RADIUS_SERVER) { 767 generr(h, "denied function call"); 768 return (-1); 769 } 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; 774 clear_password(h); 775 h->authentic_pos = 0; 776 h->out_created = 1; 777 return 0; 778} 779 | |
780struct in_addr 781rad_cvt_addr(const void *data) 782{ 783 struct in_addr value; 784 785 memcpy(&value.s_addr, data, sizeof value.s_addr); 786 return value; 787} --- 24 unchanged lines hidden (view full) --- 812 * Returns the attribute type. If none are left, returns 0. On failure, 813 * returns -1. 814 */ 815int 816rad_get_attr(struct rad_handle *h, const void **value, size_t *len) 817{ 818 int type; 819 | 398struct in_addr 399rad_cvt_addr(const void *data) 400{ 401 struct in_addr value; 402 403 memcpy(&value.s_addr, data, sizeof value.s_addr); 404 return value; 405} --- 24 unchanged lines hidden (view full) --- 430 * Returns the attribute type. If none are left, returns 0. On failure, 431 * returns -1. 432 */ 433int 434rad_get_attr(struct rad_handle *h, const void **value, size_t *len) 435{ 436 int type; 437 |
820 if (h->in_pos >= h->in_len) | 438 if (h->resp_pos >= h->resp_len) |
821 return 0; | 439 return 0; |
822 if (h->in_pos + 2 > h->in_len) { | 440 if (h->resp_pos + 2 > h->resp_len) { |
823 generr(h, "Malformed attribute in response"); 824 return -1; 825 } | 441 generr(h, "Malformed attribute in response"); 442 return -1; 443 } |
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) { | 444 type = h->response[h->resp_pos++]; 445 *len = h->response[h->resp_pos++] - 2; 446 if (h->resp_pos + *len > h->resp_len) { |
829 generr(h, "Malformed attribute in response"); 830 return -1; 831 } | 447 generr(h, "Malformed attribute in response"); 448 return -1; 449 } |
832 *value = &h->in[h->in_pos]; 833 h->in_pos += *len; | 450 *value = &h->response[h->resp_pos]; 451 h->resp_pos += *len; |
834 return type; 835} 836 837/* | 452 return type; 453} 454 455/* |
838 * Returns -1 on error, 0 to indicate no event and >0 for success 839 */ 840int 841rad_init_send_request(struct rad_handle *h, int *fd, struct timeval *tv) 842{ 843 int srv; 844 845 if (h->type == RADIUS_SERVER) { 846 generr(h, "denied function call"); 847 return (-1); 848 } 849 /* Make sure we have a socket to use */ 850 if (h->fd == -1) { 851 struct sockaddr_in sin; 852 853 if ((h->fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { 854 generr(h, "Cannot create socket: %s", strerror(errno)); 855 return -1; 856 } 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, 863 sizeof sin) == -1) { 864 generr(h, "bind: %s", strerror(errno)); 865 close(h->fd); 866 h->fd = -1; 867 return -1; 868 } 869 } 870 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"); 876 return -1; 877 } 878 } else { 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"); 884 return -1; 885 } 886 if (h->pass_pos != 0 && h->chap_pass) { 887 generr(h, "Both User and Chap Password" 888 " attributes given"); 889 return -1; 890 } 891 } 892 } 893 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; 897 898 /* 899 * Count the total number of tries we will make, and zero the 900 * counter for each server. 901 */ 902 h->total_tries = 0; 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; 906 } 907 if (h->total_tries == 0) { 908 generr(h, "No RADIUS servers specified"); 909 return -1; 910 } 911 912 h->try = h->srv = 0; 913 914 return rad_continue_send_request(h, 0, fd, tv); 915} 916 917/* | |
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. 921 */ 922struct rad_handle * | 456 * Create and initialize a rad_handle structure, and return it to the 457 * caller. Can fail only if the necessary memory cannot be allocated. 458 * In that case, it returns NULL. 459 */ 460struct rad_handle * |
923rad_auth_open(void) | 461rad_open(void) |
924{ 925 struct rad_handle *h; 926 927 h = (struct rad_handle *)malloc(sizeof(struct rad_handle)); 928 if (h != NULL) { 929 srandomdev(); 930 h->fd = -1; 931 h->num_servers = 0; 932 h->ident = random(); 933 h->errmsg[0] = '\0'; 934 memset(h->pass, 0, sizeof h->pass); 935 h->pass_len = 0; 936 h->pass_pos = 0; | 462{ 463 struct rad_handle *h; 464 465 h = (struct rad_handle *)malloc(sizeof(struct rad_handle)); 466 if (h != NULL) { 467 srandomdev(); 468 h->fd = -1; 469 h->num_servers = 0; 470 h->ident = random(); 471 h->errmsg[0] = '\0'; 472 memset(h->pass, 0, sizeof h->pass); 473 h->pass_len = 0; 474 h->pass_pos = 0; |
937 h->chap_pass = 0; 938 h->authentic_pos = 0; 939 h->type = RADIUS_AUTH; 940 h->out_created = 0; 941 h->eap_msg = 0; | |
942 } 943 return h; 944} 945 | 475 } 476 return h; 477} 478 |
946struct rad_handle * 947rad_acct_open(void) 948{ 949 struct rad_handle *h; 950 951 h = rad_open(); 952 if (h != NULL) 953 h->type = RADIUS_ACCT; 954 return h; 955} 956 957struct rad_handle * 958rad_server_open(int fd) 959{ 960 struct rad_handle *h; 961 962 h = rad_open(); 963 if (h != NULL) { 964 h->type = RADIUS_SERVER; 965 h->fd = fd; 966 } 967 return h; 968} 969 970struct rad_handle * 971rad_open(void) 972{ 973 return rad_auth_open(); 974} 975 | |
976int 977rad_put_addr(struct rad_handle *h, int type, struct in_addr addr) 978{ 979 return rad_put_attr(h, type, &addr.s_addr, sizeof addr.s_addr); 980} 981 982int 983rad_put_attr(struct rad_handle *h, int type, const void *value, size_t len) 984{ | 479int 480rad_put_addr(struct rad_handle *h, int type, struct in_addr addr) 481{ 482 return rad_put_attr(h, type, &addr.s_addr, sizeof addr.s_addr); 483} 484 485int 486rad_put_attr(struct rad_handle *h, int type, const void *value, size_t len) 487{ |
985 int result; 986 987 if (!h->out_created) { 988 generr(h, "Please call rad_create_request()" 989 " before putting attributes"); 990 return -1; 991 } 992 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"); 997 return -1; 998 } 999 } 1000 1001 /* 1002 * When proxying EAP Messages, the Message Authenticator 1003 * MUST be present; see RFC 3579. 1004 */ 1005 if (type == RAD_EAP_MESSAGE) { 1006 if (rad_put_message_authentic(h) == -1) 1007 return -1; 1008 } 1009 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); 1014 } else { 1015 result = put_raw_attr(h, type, value, len); 1016 if (result == 0) { 1017 if (type == RAD_CHAP_PASSWORD) 1018 h->chap_pass = 1; 1019 else if (type == RAD_EAP_MESSAGE) 1020 h->eap_msg = 1; 1021 } 1022 } 1023 1024 return result; | 488 return type == RAD_USER_PASSWORD ? 489 put_password_attr(h, type, value, len) : 490 put_raw_attr(h, type, value, len); |
1025} 1026 1027int 1028rad_put_int(struct rad_handle *h, int type, u_int32_t value) 1029{ 1030 u_int32_t nvalue; 1031 1032 nvalue = htonl(value); 1033 return rad_put_attr(h, type, &nvalue, sizeof nvalue); 1034} 1035 1036int 1037rad_put_string(struct rad_handle *h, int type, const char *str) 1038{ 1039 return rad_put_attr(h, type, str, strlen(str)); 1040} 1041 | 491} 492 493int 494rad_put_int(struct rad_handle *h, int type, u_int32_t value) 495{ 496 u_int32_t nvalue; 497 498 nvalue = htonl(value); 499 return rad_put_attr(h, type, &nvalue, sizeof nvalue); 500} 501 502int 503rad_put_string(struct rad_handle *h, int type, const char *str) 504{ 505 return rad_put_attr(h, type, str, strlen(str)); 506} 507 |
1042int 1043rad_put_message_authentic(struct rad_handle *h) 1044{ 1045#ifdef WITH_SSL 1046 u_char md_zero[MD5_DIGEST_LENGTH]; 1047 1048 if (h->out[POS_CODE] == RAD_ACCOUNTING_REQUEST) { 1049 generr(h, "Message-Authenticator is not valid" 1050 " in accounting requests"); 1051 return -1; 1052 } 1053 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, 1058 sizeof(md_zero))); 1059 } 1060 return 0; 1061#else 1062 generr(h, "Message Authenticator not supported," 1063 " please recompile libradius with SSL support"); 1064 return -1; 1065#endif 1066} 1067 | |
1068/* 1069 * Returns the response type code on success, or -1 on failure. 1070 */ 1071int 1072rad_send_request(struct rad_handle *h) 1073{ | 508/* 509 * Returns the response type code on success, or -1 on failure. 510 */ 511int 512rad_send_request(struct rad_handle *h) 513{ |
1074 struct timeval timelimit; 1075 struct timeval tv; 1076 int fd; | 514 int total_tries; 515 int try; 516 int srv; |
1077 int n; | 517 int n; |
518 int got_valid_response; |
|
1078 | 519 |
1079 n = rad_init_send_request(h, &fd, &tv); | 520 /* Make sure we have a socket to use */ 521 if (h->fd == -1) { 522 struct sockaddr_in sin; |
1080 | 523 |
1081 if (n != 0) 1082 return n; | 524 if ((h->fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { 525 generr(h, "Cannot create socket: %s", strerror(errno)); 526 return -1; 527 } 528 memset(&sin, 0, sizeof sin); 529 sin.sin_len = sizeof sin; 530 sin.sin_family = AF_INET; 531 sin.sin_addr.s_addr = INADDR_ANY; 532 sin.sin_port = htons(0); 533 if (bind(h->fd, (const struct sockaddr *)&sin, 534 sizeof sin) == -1) { 535 generr(h, "bind: %s", strerror(errno)); 536 close(h->fd); 537 h->fd = -1; 538 return -1; 539 } 540 } |
1083 | 541 |
1084 gettimeofday(&timelimit, NULL); 1085 timeradd(&tv, &timelimit, &timelimit); | 542 /* Make sure the user gave us a password */ 543 if (h->pass_pos == 0) { 544 generr(h, "No User-Password attribute given"); 545 return -1; 546 } |
1086 | 547 |
1087 for ( ; ; ) { 1088 fd_set readfds; | 548 /* Fill in the length field in the message */ 549 h->request[POS_LENGTH] = h->req_len >> 8; 550 h->request[POS_LENGTH+1] = h->req_len; |
1089 | 551 |
1090 FD_ZERO(&readfds); 1091 FD_SET(fd, &readfds); | 552 /* 553 * Count the total number of tries we will make, and zero the 554 * counter for each server. 555 */ 556 total_tries = 0; 557 for (srv = 0; srv < h->num_servers; srv++) { 558 total_tries += h->servers[srv].max_tries; 559 h->servers[srv].num_tries = 0; 560 } 561 if (total_tries == 0) { 562 generr(h, "No RADIUS servers specified"); 563 return -1; 564 } |
1092 | 565 |
1093 n = select(fd + 1, &readfds, NULL, NULL, &tv); | 566 srv = 0; 567 got_valid_response = 0; 568 for (try = 0; try < total_tries; try++) { 569 struct timeval timelimit; 570 struct timeval tv; |
1094 | 571 |
1095 if (n == -1) { 1096 generr(h, "select: %s", strerror(errno)); | 572 /* 573 * Scan round-robin to the next server that has some 574 * tries left. There is guaranteed to be one, or we 575 * would have exited this loop by now. 576 */ 577 while (h->servers[srv].num_tries >= 578 h->servers[srv].max_tries) 579 if (++srv >= h->num_servers) 580 srv = 0; 581 582 /* Insert the scrambled password into the request */ 583 insert_scrambled_password(h, srv); 584 585 /* Send the request */ 586 n = sendto(h->fd, h->request, h->req_len, 0, 587 (const struct sockaddr *)&h->servers[srv].addr, 588 sizeof h->servers[srv].addr); 589 if (n != h->req_len) { 590 if (n == -1) 591 generr(h, "sendto: %s", strerror(errno)); 592 else 593 generr(h, "sendto: short write"); |
1097 return -1; 1098 } | 594 return -1; 595 } |
596 h->servers[srv].num_tries++; |
|
1099 | 597 |
1100 if (!FD_ISSET(fd, &readfds)) { | 598 /* Wait for a valid response */ 599 gettimeofday(&timelimit, NULL); 600 timelimit.tv_sec += h->servers[srv].timeout; 601 602 tv.tv_sec = h->servers[srv].timeout; 603 tv.tv_usec = 0; 604 for ( ; ; ) { 605 fd_set readfds; 606 607 FD_ZERO(&readfds); 608 FD_SET(h->fd, &readfds); 609 n = select(h->fd + 1, &readfds, NULL, NULL, &tv); 610 if (n == -1) { 611 generr(h, "select: %s", strerror(errno)); 612 return -1; 613 } 614 if (n == 0) /* Timed out */ 615 break; 616 if (FD_ISSET(h->fd, &readfds)) { 617 struct sockaddr_in from; 618 int fromlen; 619 620 fromlen = sizeof from; 621 h->resp_len = recvfrom(h->fd, h->response, 622 MSGSIZE, MSG_WAITALL, 623 (struct sockaddr *)&from, &fromlen); 624 if (h->resp_len == -1) { 625 generr(h, "recvfrom: %s", 626 strerror(errno)); 627 return -1; 628 } 629 if (is_valid_response(h, srv, &from)) { 630 got_valid_response = 1; 631 break; 632 } 633 } |
1101 /* Compute a new timeout */ 1102 gettimeofday(&tv, NULL); 1103 timersub(&timelimit, &tv, &tv); | 634 /* Compute a new timeout */ 635 gettimeofday(&tv, NULL); 636 timersub(&timelimit, &tv, &tv); |
1104 if (tv.tv_sec > 0 || (tv.tv_sec == 0 && tv.tv_usec > 0)) 1105 /* Continue the select */ 1106 continue; | 637 if (tv.tv_sec < 0) /* Still poll once more */ 638 timerclear(&tv); |
1107 } | 639 } |
1108 1109 n = rad_continue_send_request(h, n, &fd, &tv); 1110 1111 if (n != 0) 1112 return n; 1113 1114 gettimeofday(&timelimit, NULL); 1115 timeradd(&tv, &timelimit, &timelimit); | 640 if (got_valid_response) 641 break; 642 /* Advance to the next server */ 643 if (++srv >= h->num_servers) 644 srv = 0; |
1116 } | 645 } |
646 if (!got_valid_response) { 647 generr(h, "No valid RADIUS responses received"); 648 return -1; 649 } 650 h->resp_len = h->response[POS_LENGTH] << 8 | h->response[POS_LENGTH+1]; 651 h->resp_pos = POS_ATTRS; 652 return h->response[POS_CODE]; |
|
1117} 1118 1119const char * 1120rad_strerror(struct rad_handle *h) 1121{ 1122 return h->errmsg; 1123} 1124 --- 69 unchanged lines hidden (view full) --- 1194 p += strcspn(p, ws); 1195 if (*p != '\0') 1196 *p++ = '\0'; 1197 } 1198 i++; 1199 } 1200 return i; 1201} | 653} 654 655const char * 656rad_strerror(struct rad_handle *h) 657{ 658 return h->errmsg; 659} 660 --- 69 unchanged lines hidden (view full) --- 730 p += strcspn(p, ws); 731 if (*p != '\0') 732 *p++ = '\0'; 733 } 734 i++; 735 } 736 return i; 737} |
1202 1203int 1204rad_get_vendor_attr(u_int32_t *vendor, const void **data, size_t *len) 1205{ 1206 struct vendor_attribute *attr; 1207 1208 attr = (struct vendor_attribute *)*data; 1209 *vendor = ntohl(attr->vendor_value); 1210 *data = attr->attrib_data; 1211 *len = attr->attrib_len - 2; 1212 1213 return (attr->attrib_type); 1214} 1215 1216int 1217rad_put_vendor_addr(struct rad_handle *h, int vendor, int type, 1218 struct in_addr addr) 1219{ 1220 return (rad_put_vendor_attr(h, vendor, type, &addr.s_addr, 1221 sizeof addr.s_addr)); 1222} 1223 1224int 1225rad_put_vendor_attr(struct rad_handle *h, int vendor, int type, 1226 const void *value, size_t len) 1227{ 1228 struct vendor_attribute *attr; 1229 int res; 1230 1231 if (!h->out_created) { 1232 generr(h, "Please call rad_create_request()" 1233 " before putting attributes"); 1234 return -1; 1235 } 1236 1237 if ((attr = malloc(len + 6)) == NULL) { 1238 generr(h, "malloc failure (%zu bytes)", len + 6); 1239 return -1; 1240 } 1241 1242 attr->vendor_value = htonl(vendor); 1243 attr->attrib_type = type; 1244 attr->attrib_len = len + 2; 1245 memcpy(attr->attrib_data, value, len); 1246 1247 res = put_raw_attr(h, RAD_VENDOR_SPECIFIC, attr, len + 6); 1248 free(attr); 1249 if (res == 0 && vendor == RAD_VENDOR_MICROSOFT 1250 && (type == RAD_MICROSOFT_MS_CHAP_RESPONSE 1251 || type == RAD_MICROSOFT_MS_CHAP2_RESPONSE)) { 1252 h->chap_pass = 1; 1253 } 1254 return (res); 1255} 1256 1257int 1258rad_put_vendor_int(struct rad_handle *h, int vendor, int type, u_int32_t i) 1259{ 1260 u_int32_t value; 1261 1262 value = htonl(i); 1263 return (rad_put_vendor_attr(h, vendor, type, &value, sizeof value)); 1264} 1265 1266int 1267rad_put_vendor_string(struct rad_handle *h, int vendor, int type, 1268 const char *str) 1269{ 1270 return (rad_put_vendor_attr(h, vendor, type, str, strlen(str))); 1271} 1272 1273ssize_t 1274rad_request_authenticator(struct rad_handle *h, char *buf, size_t len) 1275{ 1276 if (len < LEN_AUTH) 1277 return (-1); 1278 memcpy(buf, h->out + POS_AUTH, LEN_AUTH); 1279 if (len > LEN_AUTH) 1280 buf[LEN_AUTH] = '\0'; 1281 return (LEN_AUTH); 1282} 1283 1284u_char * 1285rad_demangle(struct rad_handle *h, const void *mangled, size_t mlen) 1286{ 1287 char R[LEN_AUTH]; 1288 const char *S; 1289 int i, Ppos; 1290 MD5_CTX Context; 1291 u_char b[MD5_DIGEST_LENGTH], *C, *demangled; 1292 1293 if ((mlen % 16 != 0) || mlen > 128) { 1294 generr(h, "Cannot interpret mangled data of length %lu", 1295 (u_long)mlen); 1296 return NULL; 1297 } 1298 1299 C = (u_char *)mangled; 1300 1301 /* We need the shared secret as Salt */ 1302 S = rad_server_secret(h); 1303 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"); 1307 return NULL; 1308 } 1309 1310 demangled = malloc(mlen); 1311 if (!demangled) 1312 return NULL; 1313 1314 MD5Init(&Context); 1315 MD5Update(&Context, S, strlen(S)); 1316 MD5Update(&Context, R, LEN_AUTH); 1317 MD5Final(b, &Context); 1318 Ppos = 0; 1319 while (mlen) { 1320 1321 mlen -= 16; 1322 for (i = 0; i < 16; i++) 1323 demangled[Ppos++] = C[i] ^ b[i]; 1324 1325 if (mlen) { 1326 MD5Init(&Context); 1327 MD5Update(&Context, S, strlen(S)); 1328 MD5Update(&Context, C, 16); 1329 MD5Final(b, &Context); 1330 } 1331 1332 C += 16; 1333 } 1334 1335 return demangled; 1336} 1337 1338u_char * 1339rad_demangle_mppe_key(struct rad_handle *h, const void *mangled, 1340 size_t mlen, size_t *len) 1341{ 1342 char R[LEN_AUTH]; /* variable names as per rfc2548 */ 1343 const char *S; 1344 u_char b[MD5_DIGEST_LENGTH], *demangled; 1345 const u_char *A, *C; 1346 MD5_CTX Context; 1347 int Slen, i, Clen, Ppos; 1348 u_char *P; 1349 1350 if (mlen % 16 != SALT_LEN) { 1351 generr(h, "Cannot interpret mangled data of length %lu", 1352 (u_long)mlen); 1353 return NULL; 1354 } 1355 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"); 1359 return NULL; 1360 } 1361 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 */ 1366 Slen = strlen(S); 1367 P = alloca(Clen); /* We derive our plaintext */ 1368 1369 MD5Init(&Context); 1370 MD5Update(&Context, S, Slen); 1371 MD5Update(&Context, R, LEN_AUTH); 1372 MD5Update(&Context, A, SALT_LEN); 1373 MD5Final(b, &Context); 1374 Ppos = 0; 1375 1376 while (Clen) { 1377 Clen -= 16; 1378 1379 for (i = 0; i < 16; i++) 1380 P[Ppos++] = C[i] ^ b[i]; 1381 1382 if (Clen) { 1383 MD5Init(&Context); 1384 MD5Update(&Context, S, Slen); 1385 MD5Update(&Context, C, 16); 1386 MD5Final(b, &Context); 1387 } 1388 1389 C += 16; 1390 } 1391 1392 /* 1393 * The resulting plain text consists of a one-byte length, the text and 1394 * maybe some padding. 1395 */ 1396 *len = *P; 1397 if (*len > mlen - 1) { 1398 generr(h, "Mangled data seems to be garbage %zu %zu", 1399 *len, mlen-1); 1400 return NULL; 1401 } 1402 1403 if (*len > MPPE_KEY_LEN * 2) { 1404 generr(h, "Key to long (%zu) for me max. %d", 1405 *len, MPPE_KEY_LEN * 2); 1406 return NULL; 1407 } 1408 demangled = malloc(*len); 1409 if (!demangled) 1410 return NULL; 1411 1412 memcpy(demangled, P + 1, *len); 1413 return demangled; 1414} 1415 1416const char * 1417rad_server_secret(struct rad_handle *h) 1418{ 1419 return (h->servers[h->srv].secret); 1420} | |