2 * $Id: sendserver.c,v 1.1 2004/11/14 07:26:26 paulus Exp $
4 * Copyright (C) 1995,1996,1997 Lars Fenneberg
6 * Copyright 1992 Livingston Enterprises, Inc.
8 * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
9 * and Merit Network, Inc. All Rights Reserved
11 * See the file COPYRIGHT for the respective terms and conditions.
12 * If the file is missing contact me at lf@elemental.net
13 * and I'll send you a copy.
18 #include <radiusclient.h>
19 #include <pathnames.h>
21 static void rc_random_vector (unsigned char *);
22 static int rc_check_reply (AUTH_HDR *, int, char *, unsigned char *, unsigned char);
25 * Function: rc_pack_list
27 * Purpose: Packs an attribute value pair list into a buffer.
29 * Returns: Number of octets packed.
33 static int rc_pack_list (VALUE_PAIR *vp, char *secret, AUTH_HDR *auth)
35 int length, i, pc, secretlen, padded_length;
38 unsigned char passbuf[MAX(AUTH_PASS_LEN, CHAP_VALUE_LENGTH)];
39 unsigned char md5buf[256];
40 unsigned char *buf, *vector, *lenptr;
44 while (vp != (VALUE_PAIR *) NULL)
47 if (vp->vendorcode != VENDOR_NONE) {
48 *buf++ = PW_VENDOR_SPECIFIC;
50 /* Place-holder for where to put length */
53 /* Insert vendor code */
55 *buf++ = (((unsigned int) vp->vendorcode) >> 16) & 255;
56 *buf++ = (((unsigned int) vp->vendorcode) >> 8) & 255;
57 *buf++ = ((unsigned int) vp->vendorcode) & 255;
59 /* Insert vendor-type */
60 *buf++ = vp->attribute;
68 memcpy(buf, vp->strvalue, (size_t) length);
70 total_length += length+8;
74 length = sizeof(UINT4);
77 lvalue = htonl(vp->lvalue);
78 memcpy(buf, (char *) &lvalue, sizeof(UINT4));
80 total_length += length+8;
86 *buf++ = vp->attribute;
87 switch (vp->attribute) {
88 case PW_USER_PASSWORD:
90 /* Encrypt the password */
92 /* Chop off password at AUTH_PASS_LEN */
94 if (length > AUTH_PASS_LEN) length = AUTH_PASS_LEN;
96 /* Calculate the padded length */
97 padded_length = (length+(AUTH_VECTOR_LEN-1)) & ~(AUTH_VECTOR_LEN-1);
99 /* Record the attribute length */
100 *buf++ = padded_length + 2;
102 /* Pad the password with zeros */
103 memset ((char *) passbuf, '\0', AUTH_PASS_LEN);
104 memcpy ((char *) passbuf, vp->strvalue, (size_t) length);
106 secretlen = strlen (secret);
107 vector = (char *)auth->vector;
108 for(i = 0; i < padded_length; i += AUTH_VECTOR_LEN) {
109 /* Calculate the MD5 digest*/
110 strcpy ((char *) md5buf, secret);
111 memcpy ((char *) md5buf + secretlen, vector,
113 rc_md5_calc (buf, md5buf, secretlen + AUTH_VECTOR_LEN);
115 /* Remeber the start of the digest */
118 /* Xor the password into the MD5 digest */
119 for (pc = i; pc < (i + AUTH_VECTOR_LEN); pc++) {
120 *buf++ ^= passbuf[pc];
124 total_length += padded_length + 2;
128 case PW_CHAP_PASSWORD:
130 *buf++ = CHAP_VALUE_LENGTH + 2;
132 /* Encrypt the Password */
134 if (length > CHAP_VALUE_LENGTH) {
135 length = CHAP_VALUE_LENGTH;
137 memset ((char *) passbuf, '\0', CHAP_VALUE_LENGTH);
138 memcpy ((char *) passbuf, vp->strvalue, (size_t) length);
140 /* Calculate the MD5 Digest */
141 secretlen = strlen (secret);
142 strcpy ((char *) md5buf, secret);
143 memcpy ((char *) md5buf + secretlen, (char *) auth->vector,
145 rc_md5_calc (buf, md5buf, secretlen + AUTH_VECTOR_LEN);
147 /* Xor the password into the MD5 digest */
148 for (i = 0; i < CHAP_VALUE_LENGTH; i++) {
149 *buf++ ^= passbuf[i];
151 total_length += CHAP_VALUE_LENGTH + 2;
160 memcpy (buf, vp->strvalue, (size_t) length);
162 total_length += length + 2;
165 case PW_TYPE_INTEGER:
167 *buf++ = sizeof (UINT4) + 2;
168 lvalue = htonl (vp->lvalue);
169 memcpy (buf, (char *) &lvalue, sizeof (UINT4));
170 buf += sizeof (UINT4);
171 total_length += sizeof (UINT4) + 2;
186 * Function: rc_send_server
188 * Purpose: send a request to a RADIUS server and wait for the reply
192 int rc_send_server (SEND_DATA *data, char *msg, REQUEST_INFO *info)
195 struct sockaddr salocal;
196 struct sockaddr saremote;
197 struct sockaddr_in *sin;
198 struct timeval authtime;
200 AUTH_HDR *auth, *recv_auth;
202 char *server_name; /* Name of server to query */
209 char secret[MAX_SECRET_LENGTH + 1];
210 unsigned char vector[AUTH_VECTOR_LEN];
211 char recv_buffer[BUFFER_LEN];
212 char send_buffer[BUFFER_LEN];
216 server_name = data->server;
217 if (server_name == (char *) NULL || server_name[0] == '\0')
220 if ((vp = rc_avpair_get(data->send_pairs, PW_SERVICE_TYPE)) && \
221 (vp->lvalue == PW_ADMINISTRATIVE))
223 strcpy(secret, MGMT_POLL_SECRET);
224 if ((auth_ipaddr = rc_get_ipaddr(server_name)) == 0)
229 if (rc_find_server (server_name, &auth_ipaddr, secret) != 0)
235 sockfd = socket (AF_INET, SOCK_DGRAM, 0);
238 memset (secret, '\0', sizeof (secret));
239 error("rc_send_server: socket: %s", strerror(errno));
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)
253 memset (secret, '\0', sizeof (secret));
254 error("rc_send_server: bind: %s: %m", server_name);
258 retry_max = data->retries; /* Max. numbers to try for reply */
259 retries = 0; /* Init retry cnt for blocking call */
261 /* Build a request */
262 auth = (AUTH_HDR *) send_buffer;
263 auth->code = data->code;
264 auth->id = data->seq_nbr;
266 if (data->code == PW_ACCOUNTING_REQUEST)
268 total_length = rc_pack_list(data->send_pairs, secret, auth) + AUTH_HDR_LEN;
270 auth->length = htons ((unsigned short) total_length);
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, (char *) auth, total_length + secretlen);
276 memcpy ((char *) auth->vector, (char *) vector, AUTH_VECTOR_LEN);
280 rc_random_vector (vector);
281 memcpy (auth->vector, vector, AUTH_VECTOR_LEN);
283 total_length = rc_pack_list(data->send_pairs, secret, auth) + AUTH_HDR_LEN;
285 auth->length = htons ((unsigned short) total_length);
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);
296 sendto (sockfd, (char *) auth, (unsigned int) total_length, (int) 0,
297 (struct sockaddr *) sin, sizeof (struct sockaddr_in));
299 authtime.tv_usec = 0L;
300 authtime.tv_sec = (long) data->timeout;
302 FD_SET (sockfd, &readfds);
303 if (select (sockfd + 1, &readfds, NULL, NULL, &authtime) < 0)
307 error("rc_send_server: select: %m");
308 memset (secret, '\0', sizeof (secret));
312 if (FD_ISSET (sockfd, &readfds))
316 * Timed out waiting for response. Retry "retry_max" times
317 * before giving up. If retry_max = 0, don't retry at all.
319 if (++retries >= retry_max)
321 error("rc_send_server: no reply from RADIUS server %s:%u",
322 rc_ip_hostname (auth_ipaddr), data->svc_port);
324 memset (secret, '\0', sizeof (secret));
328 salen = sizeof (saremote);
329 length = recvfrom (sockfd, (char *) recv_buffer,
330 (int) sizeof (recv_buffer),
331 (int) 0, &saremote, &salen);
335 error("rc_send_server: recvfrom: %s:%d: %m", server_name,\
338 memset (secret, '\0', sizeof (secret));
342 recv_auth = (AUTH_HDR *)recv_buffer;
344 result = rc_check_reply (recv_auth, BUFFER_LEN, secret, vector, data->seq_nbr);
346 data->receive_pairs = rc_avpair_gen(recv_auth);
351 memcpy(info->secret, secret, sizeof(info->secret));
352 memcpy(info->request_vector, vector,
353 sizeof(info->request_vector));
355 memset (secret, '\0', sizeof (secret));
357 if (result != OK_RC) return (result);
360 vp = data->receive_pairs;
363 if ((vp = rc_avpair_get(vp, PW_REPLY_MESSAGE)))
365 strcat(msg, vp->strvalue);
371 if ((recv_auth->code == PW_ACCESS_ACCEPT) ||
372 (recv_auth->code == PW_PASSWORD_ACK) ||
373 (recv_auth->code == PW_ACCOUNTING_RESPONSE))
386 * Function: rc_check_reply
388 * Purpose: verify items in returned packet.
390 * Returns: OK_RC -- upon success,
391 * BADRESP_RC -- if anything looks funny.
395 static int rc_check_reply (AUTH_HDR *auth, int bufferlen, char *secret,
396 unsigned char *vector, unsigned char seq_nbr)
400 unsigned char calc_digest[AUTH_VECTOR_LEN];
401 unsigned char reply_digest[AUTH_VECTOR_LEN];
403 totallen = ntohs (auth->length);
405 secretlen = strlen (secret);
407 /* Do sanity checks on packet length */
408 if ((totallen < 20) || (totallen > 4096))
410 error("rc_check_reply: received RADIUS server response with invalid length");
414 /* Verify buffer space, should never trigger with current buffer size and check above */
415 if ((totallen + secretlen) > bufferlen)
417 error("rc_check_reply: not enough buffer space to verify RADIUS server response");
420 /* Verify that id (seq. number) matches what we sent */
421 if (auth->id != seq_nbr)
423 error("rc_check_reply: received non-matching id in RADIUS server response");
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, (char *) auth, totallen + secretlen);
437 fputs("reply_digest: ", stderr);
438 for (i = 0; i < AUTH_VECTOR_LEN; i++)
440 fprintf(stderr,"%.2x ", (int) reply_digest[i]);
442 fputs("\ncalc_digest: ", stderr);
443 for (i = 0; i < AUTH_VECTOR_LEN; i++)
445 fprintf(stderr,"%.2x ", (int) calc_digest[i]);
451 if (memcmp ((char *) reply_digest, (char *) calc_digest,
452 AUTH_VECTOR_LEN) != 0)
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
463 if (auth->code == PW_ACCOUNTING_RESPONSE)
466 error("rc_check_reply: received invalid reply digest from RADIUS server");
475 * Function: rc_random_vector
477 * Purpose: generates a random vector of AUTH_VECTOR_LEN octets.
479 * Returns: the vector (call by reference)
483 static void rc_random_vector (unsigned char *vector)
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 */
493 if ((fd = open(_PATH_DEV_URANDOM, O_RDONLY)) >= 0)
502 readcount = read(fd, (char *)pos, i);
509 } /* else fall through */
511 for (i = 0; i < AUTH_VECTOR_LEN;)
514 memcpy ((char *) vector, (char *) &randno, sizeof (int));
515 vector += sizeof (int);