| 1 |
/* |
| 2 |
* Copyright (C) 1995,1996,1997 Lars Fenneberg |
| 3 |
* |
| 4 |
* Copyright 1992 Livingston Enterprises, Inc. |
| 5 |
* |
| 6 |
* Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan |
| 7 |
* and Merit Network, Inc. All Rights Reserved |
| 8 |
* |
| 9 |
* See the file COPYRIGHT for the respective terms and conditions. |
| 10 |
* If the file is missing contact me at [email protected] |
| 11 |
* and I'll send you a copy. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <includes.h> |
| 16 |
#include <radiusclient.h> |
| 17 |
#include <pathnames.h> |
| 18 |
#include <signal.h> |
| 19 |
|
| 20 |
static void rc_random_vector (unsigned char *); |
| 21 |
static int rc_check_reply (AUTH_HDR *, int, char *, unsigned char *, unsigned char); |
| 22 |
|
| 23 |
/* |
| 24 |
* Function: rc_pack_list |
| 25 |
* |
| 26 |
* Purpose: Packs an attribute value pair list into a buffer. |
| 27 |
* |
| 28 |
* Returns: Number of octets packed. |
| 29 |
* |
| 30 |
*/ |
| 31 |
|
| 32 |
static int rc_pack_list (VALUE_PAIR *vp, char *secret, AUTH_HDR *auth) |
| 33 |
{ |
| 34 |
int length, i, pc, secretlen, padded_length; |
| 35 |
int total_length = 0; |
| 36 |
UINT4 lvalue; |
| 37 |
unsigned char passbuf[MAX(AUTH_PASS_LEN, CHAP_VALUE_LENGTH)]; |
| 38 |
unsigned char md5buf[256]; |
| 39 |
unsigned char *buf, *vector, *lenptr; |
| 40 |
|
| 41 |
buf = auth->data; |
| 42 |
|
| 43 |
while (vp != (VALUE_PAIR *) NULL) |
| 44 |
{ |
| 45 |
|
| 46 |
if (vp->vendorcode != VENDOR_NONE) { |
| 47 |
*buf++ = PW_VENDOR_SPECIFIC; |
| 48 |
|
| 49 |
/* Place-holder for where to put length */ |
| 50 |
lenptr = buf++; |
| 51 |
|
| 52 |
/* Insert vendor code */ |
| 53 |
*buf++ = 0; |
| 54 |
*buf++ = (((unsigned int) vp->vendorcode) >> 16) & 255; |
| 55 |
*buf++ = (((unsigned int) vp->vendorcode) >> 8) & 255; |
| 56 |
*buf++ = ((unsigned int) vp->vendorcode) & 255; |
| 57 |
|
| 58 |
/* Insert vendor-type */ |
| 59 |
*buf++ = vp->attribute; |
| 60 |
|
| 61 |
/* Insert value */ |
| 62 |
switch(vp->type) { |
| 63 |
case PW_TYPE_STRING: |
| 64 |
length = vp->lvalue; |
| 65 |
*lenptr = length + 8; |
| 66 |
*buf++ = length+2; |
| 67 |
memcpy(buf, vp->strvalue, (size_t) length); |
| 68 |
buf += length; |
| 69 |
total_length += length+8; |
| 70 |
break; |
| 71 |
case PW_TYPE_INTEGER: |
| 72 |
case PW_TYPE_IPADDR: |
| 73 |
length = sizeof(UINT4); |
| 74 |
*lenptr = length + 8; |
| 75 |
*buf++ = length+2; |
| 76 |
lvalue = htonl(vp->lvalue); |
| 77 |
memcpy(buf, (char *) &lvalue, sizeof(UINT4)); |
| 78 |
buf += length; |
| 79 |
total_length += length+8; |
| 80 |
break; |
| 81 |
default: |
| 82 |
break; |
| 83 |
} |
| 84 |
} else { |
| 85 |
*buf++ = vp->attribute; |
| 86 |
switch (vp->attribute) { |
| 87 |
case PW_USER_PASSWORD: |
| 88 |
|
| 89 |
/* Encrypt the password */ |
| 90 |
|
| 91 |
/* Chop off password at AUTH_PASS_LEN */ |
| 92 |
length = vp->lvalue; |
| 93 |
if (length > AUTH_PASS_LEN) length = AUTH_PASS_LEN; |
| 94 |
|
| 95 |
/* Calculate the padded length */ |
| 96 |
padded_length = (length+(AUTH_VECTOR_LEN-1)) & ~(AUTH_VECTOR_LEN-1); |
| 97 |
|
| 98 |
/* Record the attribute length */ |
| 99 |
*buf++ = padded_length + 2; |
| 100 |
|
| 101 |
/* Pad the password with zeros */ |
| 102 |
memset ((char *) passbuf, '\0', AUTH_PASS_LEN); |
| 103 |
memcpy ((char *) passbuf, vp->strvalue, (size_t) length); |
| 104 |
|
| 105 |
secretlen = strlen (secret); |
| 106 |
vector = auth->vector; |
| 107 |
for(i = 0; i < padded_length; i += AUTH_VECTOR_LEN) { |
| 108 |
/* Calculate the MD5 digest*/ |
| 109 |
strcpy ((char *) md5buf, secret); |
| 110 |
memcpy ((char *) md5buf + secretlen, vector, |
| 111 |
AUTH_VECTOR_LEN); |
| 112 |
rc_md5_calc (buf, md5buf, secretlen + AUTH_VECTOR_LEN); |
| 113 |
|
| 114 |
/* Remeber the start of the digest */ |
| 115 |
vector = buf; |
| 116 |
|
| 117 |
/* Xor the password into the MD5 digest */ |
| 118 |
for (pc = i; pc < (i + AUTH_VECTOR_LEN); pc++) { |
| 119 |
*buf++ ^= passbuf[pc]; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
total_length += padded_length + 2; |
| 124 |
|
| 125 |
break; |
| 126 |
#if 0 |
| 127 |
case PW_CHAP_PASSWORD: |
| 128 |
|
| 129 |
*buf++ = CHAP_VALUE_LENGTH + 2; |
| 130 |
|
| 131 |
/* Encrypt the Password */ |
| 132 |
length = vp->lvalue; |
| 133 |
if (length > CHAP_VALUE_LENGTH) { |
| 134 |
length = CHAP_VALUE_LENGTH; |
| 135 |
} |
| 136 |
memset ((char *) passbuf, '\0', CHAP_VALUE_LENGTH); |
| 137 |
memcpy ((char *) passbuf, vp->strvalue, (size_t) length); |
| 138 |
|
| 139 |
/* Calculate the MD5 Digest */ |
| 140 |
secretlen = strlen (secret); |
| 141 |
strcpy ((char *) md5buf, secret); |
| 142 |
memcpy ((char *) md5buf + secretlen, (char *) auth->vector, |
| 143 |
AUTH_VECTOR_LEN); |
| 144 |
rc_md5_calc (buf, md5buf, secretlen + AUTH_VECTOR_LEN); |
| 145 |
|
| 146 |
/* Xor the password into the MD5 digest */ |
| 147 |
for (i = 0; i < CHAP_VALUE_LENGTH; i++) { |
| 148 |
*buf++ ^= passbuf[i]; |
| 149 |
} |
| 150 |
total_length += CHAP_VALUE_LENGTH + 2; |
| 151 |
|
| 152 |
break; |
| 153 |
#endif |
| 154 |
default: |
| 155 |
switch (vp->type) { |
| 156 |
case PW_TYPE_STRING: |
| 157 |
length = vp->lvalue; |
| 158 |
*buf++ = length + 2; |
| 159 |
memcpy (buf, vp->strvalue, (size_t) length); |
| 160 |
buf += length; |
| 161 |
total_length += length + 2; |
| 162 |
break; |
| 163 |
|
| 164 |
case PW_TYPE_INTEGER: |
| 165 |
case PW_TYPE_IPADDR: |
| 166 |
*buf++ = sizeof (UINT4) + 2; |
| 167 |
lvalue = htonl (vp->lvalue); |
| 168 |
memcpy (buf, (char *) &lvalue, sizeof (UINT4)); |
| 169 |
buf += sizeof (UINT4); |
| 170 |
total_length += sizeof (UINT4) + 2; |
| 171 |
break; |
| 172 |
|
| 173 |
default: |
| 174 |
break; |
| 175 |
} |
| 176 |
break; |
| 177 |
} |
| 178 |
} |
| 179 |
vp = vp->next; |
| 180 |
} |
| 181 |
return total_length; |
| 182 |
} |
| 183 |
|
| 184 |
/* |
| 185 |
* Function: rc_send_server |
| 186 |
* |
| 187 |
* Purpose: send a request to a RADIUS server and wait for the reply |
| 188 |
* |
| 189 |
*/ |
| 190 |
|
| 191 |
int rc_send_server (SEND_DATA *data, char *msg, REQUEST_INFO *info) |
| 192 |
{ |
| 193 |
int sockfd; |
| 194 |
struct sockaddr salocal; |
| 195 |
struct sockaddr saremote; |
| 196 |
struct sockaddr_in *sin; |
| 197 |
struct timeval authtime; |
| 198 |
fd_set readfds; |
| 199 |
AUTH_HDR *auth, *recv_auth; |
| 200 |
UINT4 auth_ipaddr; |
| 201 |
char *server_name; /* Name of server to query */ |
| 202 |
socklen_t salen; |
| 203 |
int result; |
| 204 |
int total_length; |
| 205 |
socklen_t length; |
| 206 |
int retry_max; |
| 207 |
int secretlen; |
| 208 |
char secret[MAX_SECRET_LENGTH + 1]; |
| 209 |
unsigned char vector[AUTH_VECTOR_LEN]; |
| 210 |
char recv_buffer[BUFFER_LEN]; |
| 211 |
char send_buffer[BUFFER_LEN]; |
| 212 |
int retries; |
| 213 |
VALUE_PAIR *vp; |
| 214 |
|
| 215 |
server_name = data->server; |
| 216 |
if (server_name == (char *) NULL || server_name[0] == '\0') |
| 217 |
return (ERROR_RC); |
| 218 |
|
| 219 |
if ((vp = rc_avpair_get(data->send_pairs, PW_SERVICE_TYPE)) && \ |
| 220 |
(vp->lvalue == PW_ADMINISTRATIVE)) |
| 221 |
{ |
| 222 |
strcpy(secret, MGMT_POLL_SECRET); |
| 223 |
if ((auth_ipaddr = rc_get_ipaddr(server_name)) == 0) |
| 224 |
return (ERROR_RC); |
| 225 |
} |
| 226 |
else |
| 227 |
{ |
| 228 |
if (rc_find_server (server_name, &auth_ipaddr, secret) != 0) |
| 229 |
{ |
| 230 |
memset (secret, '\0', sizeof (secret)); |
| 231 |
return (ERROR_RC); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
sockfd = socket (AF_INET, SOCK_DGRAM, 0); |
| 236 |
if (sockfd < 0) |
| 237 |
{ |
| 238 |
memset (secret, '\0', sizeof (secret)); |
| 239 |
error("rc_send_server: socket: %s", strerror(errno)); |
| 240 |
return (ERROR_RC); |
| 241 |
} |
| 242 |
|
| 243 |
length = sizeof (salocal); |
| 244 |
sin = (struct sockaddr_in *) & salocal; |
| 245 |
memset ((char *) sin, '\0', (size_t) length); |
| 246 |
sin->sin_family = AF_INET; |
| 247 |
sin->sin_addr.s_addr = htonl(rc_own_bind_ipaddress()); |
| 248 |
sin->sin_port = htons ((unsigned short) 0); |
| 249 |
if (bind (sockfd, (struct sockaddr *) sin, length) < 0 || |
| 250 |
getsockname (sockfd, (struct sockaddr *) sin, &length) < 0) |
| 251 |
{ |
| 252 |
close (sockfd); |
| 253 |
memset (secret, '\0', sizeof (secret)); |
| 254 |
error("rc_send_server: bind: %s: %m", server_name); |
| 255 |
return (ERROR_RC); |
| 256 |
} |
| 257 |
|
| 258 |
retry_max = data->retries; /* Max. numbers to try for reply */ |
| 259 |
retries = 0; /* Init retry cnt for blocking call */ |
| 260 |
|
| 261 |
/* Build a request */ |
| 262 |
auth = (AUTH_HDR *) send_buffer; |
| 263 |
auth->code = data->code; |
| 264 |
auth->id = data->seq_nbr; |
| 265 |
|
| 266 |
if (data->code == PW_ACCOUNTING_REQUEST) |
| 267 |
{ |
| 268 |
total_length = rc_pack_list(data->send_pairs, secret, auth) + AUTH_HDR_LEN; |
| 269 |
|
| 270 |
auth->length = htons ((unsigned short) total_length); |
| 271 |
|
| 272 |
memset((char *) auth->vector, 0, AUTH_VECTOR_LEN); |
| 273 |
secretlen = strlen (secret); |
| 274 |
memcpy ((char *) auth + total_length, secret, secretlen); |
| 275 |
rc_md5_calc (vector, (unsigned char *) auth, total_length + secretlen); |
| 276 |
memcpy ((char *) auth->vector, (char *) vector, AUTH_VECTOR_LEN); |
| 277 |
} |
| 278 |
else |
| 279 |
{ |
| 280 |
rc_random_vector (vector); |
| 281 |
memcpy (auth->vector, vector, AUTH_VECTOR_LEN); |
| 282 |
|
| 283 |
total_length = rc_pack_list(data->send_pairs, secret, auth) + AUTH_HDR_LEN; |
| 284 |
|
| 285 |
auth->length = htons ((unsigned short) total_length); |
| 286 |
} |
| 287 |
|
| 288 |
sin = (struct sockaddr_in *) & saremote; |
| 289 |
memset ((char *) sin, '\0', sizeof (saremote)); |
| 290 |
sin->sin_family = AF_INET; |
| 291 |
sin->sin_addr.s_addr = htonl (auth_ipaddr); |
| 292 |
sin->sin_port = htons ((unsigned short) data->svc_port); |
| 293 |
|
| 294 |
for (;;) |
| 295 |
{ |
| 296 |
sendto (sockfd, (char *) auth, (unsigned int) total_length, (int) 0, |
| 297 |
(struct sockaddr *) sin, sizeof (struct sockaddr_in)); |
| 298 |
|
| 299 |
authtime.tv_usec = 0L; |
| 300 |
authtime.tv_sec = (long) data->timeout; |
| 301 |
FD_ZERO (&readfds); |
| 302 |
FD_SET (sockfd, &readfds); |
| 303 |
if (select (sockfd + 1, &readfds, NULL, NULL, &authtime) < 0) |
| 304 |
{ |
| 305 |
if (errno == EINTR && !ppp_signaled(SIGTERM)) |
| 306 |
continue; |
| 307 |
error("rc_send_server: select: %m"); |
| 308 |
memset (secret, '\0', sizeof (secret)); |
| 309 |
close (sockfd); |
| 310 |
return (ERROR_RC); |
| 311 |
} |
| 312 |
if (FD_ISSET (sockfd, &readfds)) |
| 313 |
break; |
| 314 |
|
| 315 |
/* |
| 316 |
* Timed out waiting for response. Retry "retry_max" times |
| 317 |
* before giving up. If retry_max = 0, don't retry at all. |
| 318 |
*/ |
| 319 |
if (++retries >= retry_max) |
| 320 |
{ |
| 321 |
error("rc_send_server: no reply from RADIUS server %s:%u", |
| 322 |
rc_ip_hostname (auth_ipaddr), data->svc_port); |
| 323 |
close (sockfd); |
| 324 |
memset (secret, '\0', sizeof (secret)); |
| 325 |
return (TIMEOUT_RC); |
| 326 |
} |
| 327 |
} |
| 328 |
salen = sizeof (saremote); |
| 329 |
length = recvfrom (sockfd, (char *) recv_buffer, |
| 330 |
(int) sizeof (recv_buffer), |
| 331 |
(int) 0, &saremote, &salen); |
| 332 |
|
| 333 |
if (length <= 0) |
| 334 |
{ |
| 335 |
error("rc_send_server: recvfrom: %s:%d: %m", server_name,\ |
| 336 |
data->svc_port); |
| 337 |
close (sockfd); |
| 338 |
memset (secret, '\0', sizeof (secret)); |
| 339 |
return (ERROR_RC); |
| 340 |
} |
| 341 |
|
| 342 |
recv_auth = (AUTH_HDR *)recv_buffer; |
| 343 |
|
| 344 |
result = rc_check_reply (recv_auth, BUFFER_LEN, secret, vector, data->seq_nbr); |
| 345 |
|
| 346 |
data->receive_pairs = rc_avpair_gen(recv_auth); |
| 347 |
|
| 348 |
close (sockfd); |
| 349 |
if (info) |
| 350 |
{ |
| 351 |
memcpy(info->secret, secret, sizeof(info->secret)); |
| 352 |
memcpy(info->request_vector, vector, |
| 353 |
sizeof(info->request_vector)); |
| 354 |
} |
| 355 |
memset (secret, '\0', sizeof (secret)); |
| 356 |
|
| 357 |
if (result != OK_RC) return (result); |
| 358 |
|
| 359 |
*msg = '\0'; |
| 360 |
vp = data->receive_pairs; |
| 361 |
while (vp) |
| 362 |
{ |
| 363 |
if ((vp = rc_avpair_get(vp, PW_REPLY_MESSAGE))) |
| 364 |
{ |
| 365 |
strcat(msg, (char*) vp->strvalue); |
| 366 |
strcat(msg, "\n"); |
| 367 |
vp = vp->next; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
if ((recv_auth->code == PW_ACCESS_ACCEPT) || |
| 372 |
(recv_auth->code == PW_PASSWORD_ACK) || |
| 373 |
(recv_auth->code == PW_ACCOUNTING_RESPONSE)) |
| 374 |
{ |
| 375 |
result = OK_RC; |
| 376 |
} |
| 377 |
else |
| 378 |
{ |
| 379 |
result = BADRESP_RC; |
| 380 |
} |
| 381 |
|
| 382 |
return (result); |
| 383 |
} |
| 384 |
|
| 385 |
/* |
| 386 |
* Function: rc_check_reply |
| 387 |
* |
| 388 |
* Purpose: verify items in returned packet. |
| 389 |
* |
| 390 |
* Returns: OK_RC -- upon success, |
| 391 |
* BADRESP_RC -- if anything looks funny. |
| 392 |
* |
| 393 |
*/ |
| 394 |
|
| 395 |
static int rc_check_reply (AUTH_HDR *auth, int bufferlen, char *secret, |
| 396 |
unsigned char *vector, unsigned char seq_nbr) |
| 397 |
{ |
| 398 |
int secretlen; |
| 399 |
int totallen; |
| 400 |
unsigned char calc_digest[AUTH_VECTOR_LEN]; |
| 401 |
unsigned char reply_digest[AUTH_VECTOR_LEN]; |
| 402 |
|
| 403 |
totallen = ntohs (auth->length); |
| 404 |
|
| 405 |
secretlen = strlen (secret); |
| 406 |
|
| 407 |
/* Do sanity checks on packet length */ |
| 408 |
if ((totallen < 20) || (totallen > 4096)) |
| 409 |
{ |
| 410 |
error("rc_check_reply: received RADIUS server response with invalid length"); |
| 411 |
return (BADRESP_RC); |
| 412 |
} |
| 413 |
|
| 414 |
/* Verify buffer space, should never trigger with current buffer size and check above */ |
| 415 |
if ((totallen + secretlen) > bufferlen) |
| 416 |
{ |
| 417 |
error("rc_check_reply: not enough buffer space to verify RADIUS server response"); |
| 418 |
return (BADRESP_RC); |
| 419 |
} |
| 420 |
/* Verify that id (seq. number) matches what we sent */ |
| 421 |
if (auth->id != seq_nbr) |
| 422 |
{ |
| 423 |
error("rc_check_reply: received non-matching id in RADIUS server response"); |
| 424 |
return (BADRESP_RC); |
| 425 |
} |
| 426 |
|
| 427 |
/* Verify the reply digest */ |
| 428 |
memcpy ((char *) reply_digest, (char *) auth->vector, AUTH_VECTOR_LEN); |
| 429 |
memcpy ((char *) auth->vector, (char *) vector, AUTH_VECTOR_LEN); |
| 430 |
memcpy ((char *) auth + totallen, secret, secretlen); |
| 431 |
rc_md5_calc (calc_digest, (unsigned char *) auth, totallen + secretlen); |
| 432 |
|
| 433 |
#ifdef DIGEST_DEBUG |
| 434 |
{ |
| 435 |
int i; |
| 436 |
|
| 437 |
fputs("reply_digest: ", stderr); |
| 438 |
for (i = 0; i < AUTH_VECTOR_LEN; i++) |
| 439 |
{ |
| 440 |
fprintf(stderr,"%.2x ", (int) reply_digest[i]); |
| 441 |
} |
| 442 |
fputs("\ncalc_digest: ", stderr); |
| 443 |
for (i = 0; i < AUTH_VECTOR_LEN; i++) |
| 444 |
{ |
| 445 |
fprintf(stderr,"%.2x ", (int) calc_digest[i]); |
| 446 |
} |
| 447 |
fputs("\n", stderr); |
| 448 |
} |
| 449 |
#endif |
| 450 |
|
| 451 |
if (memcmp ((char *) reply_digest, (char *) calc_digest, |
| 452 |
AUTH_VECTOR_LEN) != 0) |
| 453 |
{ |
| 454 |
#ifdef RADIUS_116 |
| 455 |
/* the original Livingston radiusd v1.16 seems to have |
| 456 |
a bug in digest calculation with accounting requests, |
| 457 |
authentication request are ok. i looked at the code |
| 458 |
but couldn't find any bugs. any help to get this |
| 459 |
kludge out are welcome. preferably i want to |
| 460 |
reproduce the calculation bug here to be compatible |
| 461 |
to stock Livingston radiusd v1.16. -lf, 03/14/96 |
| 462 |
*/ |
| 463 |
if (auth->code == PW_ACCOUNTING_RESPONSE) |
| 464 |
return (OK_RC); |
| 465 |
#endif |
| 466 |
error("rc_check_reply: received invalid reply digest from RADIUS server"); |
| 467 |
return (BADRESP_RC); |
| 468 |
} |
| 469 |
|
| 470 |
return (OK_RC); |
| 471 |
|
| 472 |
} |
| 473 |
|
| 474 |
/* |
| 475 |
* Function: rc_random_vector |
| 476 |
* |
| 477 |
* Purpose: generates a random vector of AUTH_VECTOR_LEN octets. |
| 478 |
* |
| 479 |
* Returns: the vector (call by reference) |
| 480 |
* |
| 481 |
*/ |
| 482 |
|
| 483 |
static void rc_random_vector (unsigned char *vector) |
| 484 |
{ |
| 485 |
int randno; |
| 486 |
int i; |
| 487 |
int fd; |
| 488 |
|
| 489 |
/* well, I added this to increase the security for user passwords. |
| 490 |
we use /dev/urandom here, as /dev/random might block and we don't |
| 491 |
need that much randomness. BTW, great idea, Ted! -lf, 03/18/95 */ |
| 492 |
|
| 493 |
if ((fd = open(PPP_PATH_DEV_URANDOM, O_RDONLY)) >= 0) |
| 494 |
{ |
| 495 |
unsigned char *pos; |
| 496 |
int readcount; |
| 497 |
|
| 498 |
i = AUTH_VECTOR_LEN; |
| 499 |
pos = vector; |
| 500 |
while (i > 0) |
| 501 |
{ |
| 502 |
readcount = read(fd, (char *)pos, i); |
| 503 |
pos += readcount; |
| 504 |
i -= readcount; |
| 505 |
} |
| 506 |
|
| 507 |
close(fd); |
| 508 |
return; |
| 509 |
} /* else fall through */ |
| 510 |
|
| 511 |
for (i = 0; i < AUTH_VECTOR_LEN;) |
| 512 |
{ |
| 513 |
randno = magic(); |
| 514 |
memcpy ((char *) vector, (char *) &randno, sizeof (int)); |
| 515 |
vector += sizeof (int); |
| 516 |
i += sizeof (int); |
| 517 |
} |
| 518 |
|
| 519 |
return; |
| 520 |
} |