15#include <radcli/radcli.h>
22#if defined(HAVE_GNUTLS)
23# include <gnutls/gnutls.h>
24# include <gnutls/crypto.h>
32#define SCLOSE(fd) if (sfuncs->close_fd) sfuncs->close_fd(fd)
34static void rc_random_vector(
unsigned char[AUTH_VECTOR_LEN]);
35static int rc_check_reply(AUTH_HDR *,
int,
char const *,
unsigned char const *,
56int rc_pack_list(VALUE_PAIR * vp,
char *secret, AUTH_HDR * auth,
int max_len)
58 int length, i, pc, padded_length;
60 uint32_t lvalue, vendor;
61 unsigned char passbuf[RC_MAX(AUTH_PASS_LEN, CHAP_VALUE_LENGTH)];
62 unsigned char md5buf[MAX_SECRET_LENGTH + AUTH_VECTOR_LEN];
63 unsigned char *vector;
65 uint8_t *attr_start, *attr_len_ptr, *vsa_len_ptr;
69 pb.head = (uint8_t *)auth;
70 pb.data = (uint8_t *)auth;
72 pb.end = (uint8_t *)auth + max_len;
76 unsigned max_vlen = AUTH_STRING_LEN;
79 max_vlen = AUTH_STRING_LEN - VSA_HDR_LEN;
81 vsa_len_ptr = pb.tail;
82 if (pb_put_byte(&pb, 6) < 0)
goto too_large;
84 if (pb_put_bytes(&pb, &vendor,
sizeof(uint32_t)) < 0)
goto too_large;
88 if (pb_put_byte(&pb, vp->
attribute & 0xff) < 0)
goto too_large;
89 attr_len_ptr = pb.tail;
90 if (pb_put_byte(&pb, 2) < 0)
goto too_large;
95 if (length > AUTH_PASS_LEN)
96 length = AUTH_PASS_LEN;
98 (length + (AUTH_VECTOR_LEN - 1)) & ~(AUTH_VECTOR_LEN - 1);
100 if (pb.tail + padded_length > pb.end)
goto too_large;
103 memset((
char *)passbuf,
'\0', AUTH_PASS_LEN);
104 memcpy((
char *)passbuf, vp->
strvalue, (
size_t) length);
106 secretlen = strlen(secret);
107 if (secretlen > MAX_SECRET_LENGTH)
108 secretlen = MAX_SECRET_LENGTH;
109 vector = (
unsigned char *)auth->vector;
110 for (i = 0; i < padded_length; i += AUTH_VECTOR_LEN) {
112 memcpy(md5buf, secret, secretlen);
113 memcpy(md5buf + secretlen, vector, AUTH_VECTOR_LEN);
114 rc_md5_calc(pb.tail, md5buf, secretlen + AUTH_VECTOR_LEN);
120 for (pc = i; pc < (i + AUTH_VECTOR_LEN); pc++)
121 *pb.tail++ ^= passbuf[pc];
129 if (vp->
lvalue > max_vlen)
goto too_large;
135 if (pb_put_bytes(&pb, vp->
strvalue, 16) < 0)
142 lvalue = htonl(vp->
lvalue);
143 if (pb_put_bytes(&pb, &lvalue,
sizeof(uint32_t)) < 0)
154 *attr_len_ptr = (uint8_t)(pb.tail - attr_start);
155 if (vsa_len_ptr != NULL)
156 *vsa_len_ptr += *attr_len_ptr;
160 return (
int)pb_written(&pb);
163 rc_log(LOG_ERR,
"rc_pack_list: attribute value too large or packet would exceed %d bytes", max_len);
174static void strappend(
char *dest,
unsigned max_size,
int *pos,
const char *src)
176 unsigned len = strlen(src) + 1;
181 if (len + *pos > max_size) {
186 memcpy(&dest[*pos], src, len);
192static int populate_ctx(RC_AAA_CTX ** ctx,
char secret[MAX_SECRET_LENGTH + 1],
193 uint8_t vector[AUTH_VECTOR_LEN])
199 *ctx = malloc(
sizeof(RC_AAA_CTX));
201 memcpy((*ctx)->secret, secret,
sizeof((*ctx)->secret));
202 memcpy((*ctx)->request_vector, vector,
203 sizeof((*ctx)->request_vector));
223 return rc_send_server_ctx(rh, NULL, data, msg, type);
235static int rc_check_reply(AUTH_HDR * auth,
int bufferlen,
char const *secret,
236 unsigned char const *vector, uint8_t seq_nbr)
240 unsigned char calc_digest[AUTH_VECTOR_LEN];
241 unsigned char reply_digest[AUTH_VECTOR_LEN];
243 totallen = ntohs(auth->length);
244 secretlen = (int)strlen(secret);
247 if ((totallen < 20) || (totallen > 4096)) {
249 "rc_check_reply: received RADIUS server response with invalid length");
254 if ((totallen + secretlen) > bufferlen) {
256 "rc_check_reply: not enough buffer space to verify RADIUS server response");
261 if (auth->id != seq_nbr) {
263 "rc_check_reply: received non-matching id in RADIUS server response");
267 memcpy((
char *)reply_digest, (
char *)auth->vector, AUTH_VECTOR_LEN);
268 memcpy((
char *)auth->vector, (
char *)vector, AUTH_VECTOR_LEN);
269 memcpy((
char *)auth + totallen, secret, secretlen);
270 rc_md5_calc(calc_digest, (
unsigned char *)auth, totallen + secretlen);
272 if (rc_memcmp((
char *)reply_digest, (
char *)calc_digest,
273 AUTH_VECTOR_LEN) != 0) {
275 "rc_check_reply: received invalid reply digest from RADIUS server");
287static void rc_random_vector(
unsigned char vector[AUTH_VECTOR_LEN])
289#if defined(HAVE_GNUTLS)
290 int ret = gnutls_rnd(GNUTLS_RND_NONCE, vector, AUTH_VECTOR_LEN);
293 int ret = getentropy(vector, AUTH_VECTOR_LEN);
313static int add_msg_auth_attr(rc_handle * rh,
char * secret,
314 AUTH_HDR *auth,
int total_length)
316 size_t secretlen = strlen(secret);
317 uint8_t *msg_auth = (uint8_t *)auth + total_length;
320 memset(&msg_auth[2], 0, MD5_DIGEST_SIZE);
322 auth->length = htons((
unsigned short)total_length);
325 uint8_t digest[MD5_DIGEST_SIZE];
326 rc_hmac_md5((uint8_t *)auth, (
size_t)total_length, (uint8_t *)secret, secretlen, digest);
327 memcpy(&msg_auth[2], digest, MD5_DIGEST_SIZE);
343static int validate_message_authenticator(
const uint8_t *recv_buffer,
344 size_t length,
const char *secret,
345 const unsigned char *req_auth)
347 uint8_t verify_buffer[RC_BUFFER_LEN];
349 uint8_t ma_copy[MD5_DIGEST_SIZE];
350 uint8_t digest[MD5_DIGEST_SIZE];
351 uint8_t attr_type, attr_len;
354 if (AUTH_HDR_LEN + length >
sizeof(verify_buffer)) {
355 rc_log(LOG_ERR,
"%s: packet too large for verification buffer", __func__);
361 memcpy(verify_buffer, recv_buffer, AUTH_HDR_LEN + length);
362 memcpy(verify_buffer + 4, req_auth, AUTH_VECTOR_LEN);
363 pb_init_read(&vb, verify_buffer + AUTH_HDR_LEN, length, length);
365 while (pb_len(&vb) >= 2) {
366 attr_type = vb.data[0];
367 attr_len = vb.data[1];
368 if (attr_len < 2 || (
size_t)attr_len > pb_len(&vb))
372 if (attr_len != 2 + MD5_DIGEST_SIZE) {
373 rc_log(LOG_ERR,
"%s: Message-Authenticator has wrong length %u",
374 __func__, (
unsigned)(attr_len - 2));
378 memcpy(ma_copy, vb.data + 2, MD5_DIGEST_SIZE);
379 memset(vb.data + 2,
'\0', MD5_DIGEST_SIZE);
383 assert(pb_pull(&vb, attr_len) == 0);
389 rc_hmac_md5(verify_buffer, AUTH_HDR_LEN + length, (uint8_t *)secret, strlen(secret), digest);
390 return rc_memcmp(ma_copy, digest, MD5_DIGEST_SIZE);
405int rc_send_server_ctx(rc_handle * rh, RC_AAA_CTX ** ctx, SEND_DATA * data,
409 AUTH_HDR *auth, *recv_auth;
410 char *server_name, *p;
411 struct sockaddr_storage our_sockaddr;
412 struct addrinfo *auth_addr = NULL;
418 const rc_sockets_override *sfuncs;
419 unsigned discover_local_ip;
421 char secret[MAX_SECRET_LENGTH + 1];
422 unsigned char vector[AUTH_VECTOR_LEN];
423 uint8_t recv_buffer[RC_BUFFER_LEN];
424 uint8_t send_buffer[RC_BUFFER_LEN];
427 uint8_t attr_type, attr_len;
431 double start_time, timeout;
432 struct sockaddr_storage *ss_set = NULL;
433 char *server_type =
"auth";
437 server_name = data->
server;
438 if (server_name == NULL || server_name[0] ==
'\0')
443 if(-1 == rc_set_netns(ns, &ns_def_hdl)) {
444 rc_log(LOG_ERR,
"rc_send_server: namespace %s set failed", ns);
449 (vp->
lvalue == PW_ADMINISTRATIVE)) {
452 rc_getaddrinfo(server_name,
453 type ==
AUTH ? PW_AI_AUTH : PW_AI_ACCT);
454 if (auth_addr == NULL) {
459 if (data->
secret != NULL) {
460 strlcpy(secret, data->
secret,
sizeof(secret));
467 (rh, server_name, &auth_addr, secret, type) != 0) {
469 "rc_send_server: unable to find server: %s",
479 if (sfuncs->static_secret) {
481 strlcpy(secret, sfuncs->static_secret,
sizeof(secret));
485 if (sfuncs->lock(sfuncs->ptr) != 0) {
486 rc_log(LOG_ERR,
"%s: lock error", __func__);
493 discover_local_ip = 0;
494 if (our_sockaddr.ss_family == AF_INET) {
495 if (((
struct sockaddr_in *)(&our_sockaddr))->sin_addr.s_addr ==
497 discover_local_ip = 1;
501 DEBUG(LOG_ERR,
"DEBUG: rc_send_server: creating socket to: %s",
503 if (discover_local_ip) {
505 if (result != OK_RC) {
506 memset(secret,
'\0',
sizeof(secret));
508 "rc_send_server: cannot figure our own address");
513 if (sfuncs->get_fd) {
514 sockfd = sfuncs->get_fd(sfuncs->ptr, SA(&our_sockaddr));
516 memset(secret,
'\0',
sizeof(secret));
517 rc_log(LOG_ERR,
"rc_send_server: socket: %s",
524 if(our_sockaddr.ss_family == AF_INET6) {
526 char *non_temp_addr =
rc_conf_str(rh,
"use-public-addr");
527 if (non_temp_addr && (strcasecmp(non_temp_addr,
"true") == 0)) {
528#if defined(__linux__)
529 int sock_opt = IPV6_PREFER_SRC_PUBLIC;
530 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADDR_PREFERENCES,
531 &sock_opt,
sizeof(sock_opt)) != 0) {
532 rc_log(LOG_ERR,
"rc_send_server: setsockopt: %s",
537#elif defined(BSD) || defined(__APPLE__)
539 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_PREFER_TEMPADDR,
540 &sock_opt,
sizeof(sock_opt)) != 0) {
541 rc_log(LOG_ERR,
"rc_send_server: setsockopt: %s",
547 rc_log(LOG_INFO,
"rc_send_server: Usage of non-temporary IPv6"
548 " address is not supported in this system");
553 retry_max = data->retries;
557 if (our_sockaddr.ss_family == AF_INET)
558 ((
struct sockaddr_in *)auth_addr->ai_addr)->sin_port =
559 htons((
unsigned short)data->
svc_port);
561 ((
struct sockaddr_in6 *)auth_addr->ai_addr)->sin6_port =
562 htons((
unsigned short)data->
svc_port);
568 if (rh->nas_addr_set) {
572 ss_set = &rh->nas_addr;
576 ss_set = &our_sockaddr;
580 if (ss_set->ss_family == AF_INET) {
583 *) (&((
struct sockaddr_in *)ss_set)->
591 p = &((
struct sockaddr_in6 *)ss_set)->sin6_addr;
609 auth = (AUTH_HDR *) send_buffer;
610 auth->code = data->
code;
613 if (data->
code == PW_ACCOUNTING_REQUEST) {
614 server_type =
"acct";
616 if (total_length < 0) {
621 tlen = htons((
unsigned short)total_length);
622 memcpy(&auth->length, &tlen,
sizeof(uint16_t));
624 memset((
char *)auth->vector, 0, AUTH_VECTOR_LEN);
625 secretlen = strlen(secret);
626 memcpy((
char *)auth + total_length, secret, secretlen);
627 rc_md5_calc(vector, (
unsigned char *)auth,
628 total_length + secretlen);
629 memcpy((
char *)auth->vector, (
char *)vector, AUTH_VECTOR_LEN);
631 rc_random_vector(vector);
632 memcpy((
char *)auth->vector, (
char *)vector, AUTH_VECTOR_LEN);
636 RC_MAX_PACKET_LEN - (2 + MD5_DIGEST_SIZE));
637 if (total_length < 0) {
642 total_length = add_msg_auth_attr(rh, secret, auth, total_length);
644 auth->length = htons((
unsigned short)total_length);
648 char our_addr_txt[50] =
"";
649 char auth_addr_txt[50] =
"";
651 getnameinfo(SA(&our_sockaddr), SS_LEN(&our_sockaddr), NULL, 0,
652 our_addr_txt,
sizeof(our_addr_txt), NI_NUMERICHOST);
653 getnameinfo(auth_addr->ai_addr, auth_addr->ai_addrlen, NULL, 0,
654 auth_addr_txt,
sizeof(auth_addr_txt),
658 "DEBUG: timeout=%d retries=%d local %s : 0, remote %s : %u\n",
659 data->
timeout, retry_max, our_addr_txt, auth_addr_txt,
666 sfuncs->sendto(sfuncs->ptr, sockfd, (
char *)auth,
667 (
unsigned int)total_length, (
int)0,
668 SA(auth_addr->ai_addr),
669 auth_addr->ai_addrlen);
670 }
while (result == -1 && errno == EINTR);
672 result = errno == ENETUNREACH ? NETUNREACH_RC : ERROR_RC;
673 rc_log(LOG_ERR,
"%s: socket: %s", __FUNCTION__,
680 if (sfuncs->get_active_fd) {
681 int new_fd = sfuncs->get_active_fd(sfuncs->ptr);
688 start_time = rc_getmtime();
689 for (timeout = data->
timeout; timeout > 0;
690 timeout -= rc_getmtime() - start_time) {
691 result = poll(&pfd, 1, timeout * 1000);
692 if (result != -1 || errno != EINTR)
697 rc_log(LOG_ERR,
"rc_send_server: poll: %s",
699 memset(secret,
'\0',
sizeof(secret));
705 if (result == 1 && (pfd.revents & POLLIN) != 0) {
706 salen = auth_addr->ai_addrlen;
708 length = sfuncs->recvfrom(sfuncs->ptr, sockfd,
715 }
while (length == -1 && errno == EINTR);
720 "rc_send_server: recvfrom: %s:%d: %s",
723 if (length == -1 && (e == EAGAIN || e == EINTR))
726 memset(secret,
'\0',
sizeof(secret));
731 recv_auth = (AUTH_HDR *) recv_buffer;
733 if (length < AUTH_HDR_LEN
734 || length < ntohs(recv_auth->length)) {
736 "rc_send_server: recvfrom: %s:%d: reply is too short",
739 memset(secret,
'\0',
sizeof(secret));
745 rc_check_reply(recv_auth, RC_BUFFER_LEN, secret,
747 if (result != BADRESPID_RC) {
760 if (retries++ >= retry_max) {
761 char radius_server_ip[128];
762 struct sockaddr_in *si =
763 (
struct sockaddr_in *)auth_addr->ai_addr;
764 inet_ntop(auth_addr->ai_family, &si->sin_addr,
765 radius_server_ip,
sizeof(radius_server_ip));
767 "rc_send_server: no reply from RADIUS %s server %s:%u",
768 server_type, radius_server_ip, data->
svc_port);
770 memset(secret,
'\0',
sizeof(secret));
779 if (length > ntohs(recv_auth->length))
780 length = ntohs(recv_auth->length);
785 pb_init_read(&rb, recv_buffer, length, RC_BUFFER_LEN);
786 assert(pb_pull(&rb, AUTH_HDR_LEN) == 0);
787 while (pb_len(&rb) > 0) {
788 if (pb_peek_byte(&rb, 0, &attr_type) < 0 ||
789 pb_peek_byte(&rb, 1, &attr_len) < 0) {
791 "rc_send_server: recvfrom: %s:%d: truncated attribute",
794 memset(secret,
'\0',
sizeof(secret));
798 if (attr_type == 0) {
800 "rc_send_server: recvfrom: %s:%d: attribute zero is invalid",
803 memset(secret,
'\0',
sizeof(secret));
809 "rc_send_server: recvfrom: %s:%d: attribute length is too small",
812 memset(secret,
'\0',
sizeof(secret));
816 if (attr_len > pb_len(&rb)) {
818 "rc_send_server: recvfrom: %s:%d: attribute overflows the packet",
821 memset(secret,
'\0',
sizeof(secret));
825 assert(pb_pull(&rb, attr_len) == 0);
828 length = ntohs(recv_auth->length) - AUTH_HDR_LEN;
830 data->
receive_pairs = rc_avpair_gen(rh, NULL, recv_auth->data,
837 result = populate_ctx(ctx, secret, vector);
838 if (result != OK_RC) {
839 memset(secret,
'\0',
sizeof(secret));
850 if (validate_message_authenticator(recv_buffer, length, secret, vector)) {
852 "rc_send_server: recvfrom: %s:%d: received attribute Message-Authenticator is incorrect",
854 memset(secret,
'\0',
sizeof(secret));
863 p =
rc_conf_str(rh,
"require-message-authenticator");
864 if (p == NULL || (strcasecmp(p,
"false") != 0 && strcasecmp(p,
"no") != 0)) {
866 "rc_send_server: recvfrom: %s:%d: required attribute Message-Authenticator is missing or not first",
868 memset(secret,
'\0',
sizeof(secret));
875 memset(secret,
'\0',
sizeof(secret));
883 strappend(msg, PW_MAX_MSG_SIZE, &pos,
885 strappend(msg, PW_MAX_MSG_SIZE, &pos,
"\n");
891 switch (recv_auth->code) {
892 case PW_ACCESS_ACCEPT:
893 case PW_PASSWORD_ACK:
894 case PW_ACCOUNTING_RESPONSE:
898 case PW_ACCESS_REJECT:
899 case PW_PASSWORD_REJECT:
903 case PW_ACCESS_CHALLENGE:
904 result = CHALLENGE_RC;
908 rc_log(LOG_ERR,
"rc_send_server: received RADIUS server response neither ACCEPT nor REJECT, code=%d is invalid",
915 freeaddrinfo(auth_addr);
917 if (sfuncs->unlock) {
918 if (sfuncs->unlock(sfuncs->ptr) != 0) {
919 rc_log(LOG_ERR,
"%s: unlock error", __func__);
924 if(-1 == rc_reset_netns(&ns_def_hdl)) {
925 rc_log(LOG_ERR,
"rc_send_server: namespace %s reset failed", ns);
int rc_get_srcaddr(struct sockaddr *lia, const struct sockaddr *ria)
void rc_own_bind_addr(rc_handle *rh, struct sockaddr_storage *lia)
void rc_avpair_remove(VALUE_PAIR **list, uint32_t attrid, uint32_t vendorspec)
int rc_pack_list(VALUE_PAIR *vp, char *secret, AUTH_HDR *auth, int max_len)
#define MGMT_POLL_SECRET
Default for Merit radiusd.
int rc_send_server(rc_handle *rh, SEND_DATA *data, char *msg, rc_type type)
char * rc_conf_str(rc_handle const *rh, char const *optname)
int rc_find_server_addr(rc_handle const *rh, char const *server_name, struct addrinfo **info, char *secret, rc_type type)
VALUE_PAIR * rc_avpair_add(rc_handle const *rh, VALUE_PAIR **list, uint32_t attrid, void const *pval, int len, uint32_t vendorspec)
VALUE_PAIR * rc_avpair_get(VALUE_PAIR *vp, uint32_t attrid, uint32_t vendorspec)
@ AUTH
Request for authentication server.
@ PW_NAS_IDENTIFIER
Its type is string.
@ PW_NAS_IP_ADDRESS
Its type is ipaddr.
@ PW_SERVICE_TYPE
Its type is integer.
@ PW_NAS_IPV6_ADDRESS
Its type is string.
@ PW_MESSAGE_AUTHENTICATOR
Its type is string.
@ PW_REPLY_MESSAGE
Its type is string.
@ PW_VENDOR_SPECIFIC
Its type is string.
@ PW_USER_PASSWORD
Its type is string.
@ PW_TYPE_IPADDR
The attribute is an IPv4 address in host-byte order.
@ PW_TYPE_IPV6ADDR
The attribute is an 128-bit IPv6 address.
@ PW_TYPE_IPV6PREFIX
The attribute is an IPv6 prefix; the lvalue will indicate its size.
@ PW_TYPE_INTEGER
The attribute is a 32-bit integer.
@ PW_TYPE_DATE
The attribute contains a 32-bit number indicating the seconds since epoch.
@ PW_TYPE_STRING
The attribute is a printable string.
rc_attr_type type
attribute type.
uint64_t attribute
attribute numeric value of type rc_attr_id including vendor; use VENDOR() and ATTRID() to separate.
uint32_t lvalue
attribute value if type is PW_TYPE_INTEGER, PW_TYPE_DATE or PW_TYPE_IPADDR.
char strvalue[AUTH_STRING_LEN+1]
contains attribute value in other cases.
int timeout
Session timeout in seconds.
char * secret
Shared secret of RADIUS server.
uint8_t seq_nbr
Packet sequence number.
int svc_port
RADIUS protocol destination port.
char * server
Name/address of RADIUS server.
VALUE_PAIR * send_pairs
More a/v pairs to send.
VALUE_PAIR * receive_pairs
Where to place received a/v pairs.
uint8_t code
RADIUS packet code.