60#include <radcli/radcli.h>
61#include <radcli/radcli2.h>
71#define RADCLI_DAE_DEFAULT_PORT 3799
75#define RADCLI_DAE_MAX_DACS 64
102#define RADCLI_DAE_SLOTS 256
104struct radcli_dae_slot {
107 uint8_t dedup_key[RC_SHA256_DIGEST_SIZE];
115 uint32_t error_cause;
118struct radcli_dae_dac {
119 struct sockaddr_storage addr;
132#define RADCLI_DAE_RADSEC_QUEUE_SIZE 8
134struct radcli_dae_st {
137 struct radcli_dae_dac *dacs;
139 struct radcli_dae_slot *slots;
143 int require_message_authenticator;
144 unsigned no_nas_check;
164 struct radcli_dae_request_st *radsec_queue[RADCLI_DAE_RADSEC_QUEUE_SIZE];
165 unsigned radsec_queue_len;
168struct radcli_dae_request_st {
169 struct radcli_dae_st *dae;
170 struct radcli_dae_dac *dac;
173 uint8_t request_authenticator[AUTH_VECTOR_LEN];
174 uint8_t dedup_key[RC_SHA256_DIGEST_SIZE];
181 struct sockaddr_storage from;
183 char secret[MAX_SECRET_LENGTH + 1];
184 radcli_avp_list *attrs;
197 unsigned is_cached_duplicate;
198 uint8_t cached_reply_code;
199 uint32_t cached_error_cause;
207static void free_dacs(
struct radcli_dae_dac *dacs,
unsigned n)
213 for (i = 0; i < n; i++)
214 free(dacs[i].secret);
226static void radsec_queue_push(
struct radcli_dae_st *dae,
struct radcli_dae_request_st *req)
230 if (dae->radsec_queue_len == RADCLI_DAE_RADSEC_QUEUE_SIZE) {
231 rc_log(LOG_WARNING,
"radcli_ctx_dispatch: RadSec dynamic-authorization "
232 "queue full (%u), dropping the oldest undelivered request",
233 (
unsigned)RADCLI_DAE_RADSEC_QUEUE_SIZE);
235 for (i = 1; i < dae->radsec_queue_len; i++)
236 dae->radsec_queue[i - 1] = dae->radsec_queue[i];
237 dae->radsec_queue_len--;
239 dae->radsec_queue[dae->radsec_queue_len++] = req;
247static struct radcli_dae_request_st *radsec_queue_pop(
struct radcli_dae_st *dae)
249 struct radcli_dae_request_st *req;
252 if (dae->radsec_queue_len == 0)
254 req = dae->radsec_queue[0];
255 for (i = 1; i < dae->radsec_queue_len; i++)
256 dae->radsec_queue[i - 1] = dae->radsec_queue[i];
257 dae->radsec_queue_len--;
282static int parse_dae_server_token(
const char *token,
char **name,
char **secret)
284 const char *close, *colon;
290 if (token[0] ==
'\0' || strchr(token,
'/') != NULL)
293 if (token[0] ==
'[') {
294 close = strchr(token,
']');
295 if (close == NULL || close == token + 1)
297 namelen = (size_t)(close - (token + 1));
298 *name = malloc(namelen + 1);
301 memcpy(*name, token + 1, namelen);
302 (*name)[namelen] =
'\0';
304 if (close[1] ==
':') {
305 if (close[2] ==
'\0')
307 if (strlen(close + 2) > MAX_SECRET_LENGTH) {
308 rc_log(LOG_ERR,
"radcli_dae_new: dae-server: secret override for "
309 "\"%s\" is longer than %d bytes", *name, MAX_SECRET_LENGTH);
312 *secret = strdup(close + 2);
315 }
else if (close[1] !=
'\0') {
321 colon = strchr(token,
':');
322 if (colon != NULL && strchr(colon + 1,
':') == NULL) {
323 if (colon == token || colon[1] ==
'\0')
325 namelen = (size_t)(colon - token);
326 *name = malloc(namelen + 1);
329 memcpy(*name, token, namelen);
330 (*name)[namelen] =
'\0';
331 if (strlen(colon + 1) > MAX_SECRET_LENGTH) {
332 rc_log(LOG_ERR,
"radcli_dae_new: dae-server: secret override for "
333 "\"%s\" is longer than %d bytes", *name, MAX_SECRET_LENGTH);
336 *secret = strdup(colon + 1);
342 *name = strdup(token);
369static int add_dac_addrs(
struct radcli_dae_st *dae,
const char *name,
370 const char *secret_override)
372 struct addrinfo hints, *res, *rp;
374 struct radcli_dae_dac *tmp;
377 memset(&hints, 0,
sizeof(hints));
378 hints.ai_family = AF_UNSPEC;
379 hints.ai_socktype = SOCK_DGRAM;
381 err = getaddrinfo(name, NULL, &hints, &res);
383 rc_log(LOG_ERR,
"radcli_dae_new: dae-server: cannot resolve %s: %s",
384 name, gai_strerror(err));
388 for (rp = res; rp != NULL; rp = rp->ai_next) {
389 if (dae->n_dacs >= RADCLI_DAE_MAX_DACS) {
390 rc_log(LOG_ERR,
"radcli_dae_new: dae-server: too many resolved "
391 "addresses (max %d)", RADCLI_DAE_MAX_DACS);
395 tmp = realloc(dae->dacs, (dae->n_dacs + 1) *
sizeof(*tmp));
401 memset(&dae->dacs[dae->n_dacs], 0,
sizeof(*tmp));
402 memcpy(&dae->dacs[dae->n_dacs].addr, rp->ai_addr, rp->ai_addrlen);
403 dae->dacs[dae->n_dacs].addrlen = rp->ai_addrlen;
404 if (secret_override != NULL) {
405 dae->dacs[dae->n_dacs].secret = strdup(secret_override);
406 if (dae->dacs[dae->n_dacs].secret == NULL) {
417 rc_log(LOG_ERR,
"radcli_dae_new: dae-server: %s resolved to no addresses", name);
432static int parse_dae_listen(
const char *spec,
char **host,
int *port)
434 const char *close, *colon;
439 *port = RADCLI_DAE_DEFAULT_PORT;
441 if (spec == NULL || spec[0] ==
'\0')
444 if (spec[0] ==
'[') {
445 close = strchr(spec,
']');
446 if (close == NULL || close == spec + 1)
448 *host = malloc((
size_t)(close - (spec + 1)) + 1);
451 memcpy(*host, spec + 1, (
size_t)(close - (spec + 1)));
452 (*host)[close - (spec + 1)] =
'\0';
454 if (close[1] ==
':') {
455 p = strtol(close + 2, &end, 10);
456 if (*end !=
'\0' || p < 0 || p > 65535)
459 }
else if (close[1] !=
'\0') {
465 colon = strrchr(spec,
':');
467 *host = strdup(spec);
474 *host = malloc((
size_t)(colon - spec) + 1);
477 memcpy(*host, spec, (
size_t)(colon - spec));
478 (*host)[colon - spec] =
'\0';
480 if (colon[1] !=
'\0') {
481 p = strtol(colon + 1, &end, 10);
482 if (*end !=
'\0' || p < 0 || p > 65535)
515 rc_handle *rh = (rc_handle *)ctx;
516 struct radcli_dae_st *dae = NULL;
517 const char *accept_str, *secret, *server_str, *require_ma_str, *listen_str;
518 char *server_dup = NULL, *saveptr, *tok;
519 int force_udp, radsec_mode;
527 accept_str = rc_conf_str_id(rh, OPT_DAE_ACCEPT);
528 if (accept_str == NULL || strcasecmp(accept_str,
"no") == 0) {
534 if (strcasecmp(accept_str,
"yes") == 0) {
536 }
else if (strcasecmp(accept_str,
"udp") == 0) {
539 rc_log(LOG_ERR,
"radcli_dae_new: dae-accept: invalid value \"%s\" "
540 "(must be no, yes, or udp)", accept_str);
553 radsec_mode = (!force_udp && (rh->so_type == RC_SOCKET_TLS || rh->so_type == RC_SOCKET_DTLS));
555 if (rh->active_dae != NULL) {
556 rc_log(LOG_ERR,
"radcli_dae_new: a radcli_dae is already active on this context");
565 const char *verify_str = rc_conf_str_id(rh, OPT_TLS_VERIFY_HOSTNAME);
567 if (verify_str != NULL &&
568 (strcasecmp(verify_str,
"false") == 0 || strcasecmp(verify_str,
"no") == 0)) {
569 rc_log(LOG_ERR,
"radcli_dae_new: dae-accept=yes under serv-type "
570 "tls/dtls needs tls-verify-hostname enabled: RadSec's "
571 "verified peer identity is what authorizes a "
572 "CoA/Disconnect-Request sender, replacing dae-server");
582 static const struct {
586 { OPT_DAE_LISTEN,
"dae-listen" },
587 { OPT_DAE_SERVER,
"dae-server" },
588 { OPT_DAE_SECRET,
"dae-secret" },
589 { OPT_DAE_REQUIRE_MESSAGE_AUTHENTICATOR,
"dae-require-message-authenticator" }
593 for (i = 0; i <
sizeof(inapplicable) /
sizeof(inapplicable[0]); i++) {
594 const char *v = rc_conf_str_id(rh, inapplicable[i].
id);
596 if (v != NULL && v[0] !=
'\0')
597 rc_log(LOG_WARNING,
"radcli_dae_new: %s is set but has no "
598 "effect under serv-type tls/dtls (RadSec replaces "
599 "it)", inapplicable[i].name);
603 dae = calloc(1,
sizeof(*dae));
614 dae->slots = calloc(RADCLI_DAE_SLOTS,
sizeof(*dae->slots));
615 if (dae->slots == NULL)
618 dae->max_clock_skew = rc_conf_int_id(rh, OPT_DAE_MAX_CLOCK_SKEW);
619 if (dae->max_clock_skew < 0) {
620 rc_log(LOG_ERR,
"radcli_dae_new: dae-max-clock-skew must not be negative");
624 rh->active_dae = dae;
628 secret = rc_conf_str_id(rh, OPT_DAE_SECRET);
629 server_str = rc_conf_str_id(rh, OPT_DAE_SERVER);
630 if (secret == NULL || secret[0] ==
'\0' || server_str == NULL || server_str[0] ==
'\0') {
631 rc_log(LOG_ERR,
"radcli_dae_new: dae-accept is enabled but dae-server "
632 "and/or dae-secret is not set");
635 if (strlen(secret) > MAX_SECRET_LENGTH) {
636 rc_log(LOG_ERR,
"radcli_dae_new: dae-secret is longer than %d bytes",
641 dae = calloc(1,
sizeof(*dae));
648 dae->secret = strdup(secret);
649 if (dae->secret == NULL)
652 server_dup = strdup(server_str);
653 if (server_dup == NULL)
656 for (tok = strtok_r(server_dup,
",", &saveptr); tok != NULL;
657 tok = strtok_r(NULL,
",", &saveptr)) {
658 char *name = NULL, *secret_override = NULL;
661 while (*tok ==
' ' || *tok ==
'\t')
664 if (parse_dae_server_token(tok, &name, &secret_override) != 0) {
665 rc_log(LOG_ERR,
"radcli_dae_new: dae-server: invalid entry \"%s\"", tok);
669 ret = add_dac_addrs(dae, name, secret_override);
671 free(secret_override);
678 if (dae->n_dacs == 0) {
679 rc_log(LOG_ERR,
"radcli_dae_new: dae-server produced no authorized senders");
687 dae->slots = calloc(RADCLI_DAE_SLOTS,
sizeof(*dae->slots));
688 if (dae->slots == NULL)
691 dae->max_clock_skew = rc_conf_int_id(rh, OPT_DAE_MAX_CLOCK_SKEW);
692 if (dae->max_clock_skew < 0) {
693 rc_log(LOG_ERR,
"radcli_dae_new: dae-max-clock-skew must not be negative");
697 require_ma_str = rc_conf_str_id(rh, OPT_DAE_REQUIRE_MESSAGE_AUTHENTICATOR);
698 dae->require_message_authenticator =
699 (require_ma_str != NULL && strcasecmp(require_ma_str,
"yes") == 0);
701 listen_str = rc_conf_str_id(rh, OPT_DAE_LISTEN);
702 if (parse_dae_listen(listen_str, &dae->listen_host, &dae->listen_port) != 0) {
703 rc_log(LOG_ERR,
"radcli_dae_new: dae-listen: invalid value \"%s\"",
704 listen_str ? listen_str :
"");
708 rh->active_dae = dae;
729 dae->handler_user = user;
750int radcli2_priv_set_nonblock_cloexec(
int fd)
754 flags = fcntl(fd, F_GETFL, 0);
755 if (flags == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
757 flags = fcntl(fd, F_GETFD, 0);
758 if (flags == -1 || fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
769 struct addrinfo hints, *res, *rp;
772 char addr_txt[NI_MAXHOST] =
"?";
777 rc_log(LOG_ERR,
"radcli_dae_start: already started");
787 if (radcli2_priv_sess_ensure_connected(dae->rh) != 0) {
788 rc_log(LOG_ERR,
"radcli_dae_start: could not establish the "
789 "RadSec (TLS/DTLS) session");
796 snprintf(portstr,
sizeof(portstr),
"%d", dae->listen_port);
798 memset(&hints, 0,
sizeof(hints));
799 hints.ai_family = AF_UNSPEC;
800 hints.ai_socktype = SOCK_DGRAM;
801 hints.ai_flags = AI_PASSIVE;
803 err = getaddrinfo(dae->listen_host, portstr, &hints, &res);
805 rc_log(LOG_ERR,
"radcli_dae_start: dae-listen: %s", gai_strerror(err));
809 for (rp = res; rp != NULL; rp = rp->ai_next) {
810 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
813 if (radcli2_priv_set_nonblock_cloexec(fd) != 0) {
818 if (bind(fd, rp->ai_addr, rp->ai_addrlen) == 0) {
819 getnameinfo(rp->ai_addr, rp->ai_addrlen, addr_txt,
sizeof(addr_txt),
820 NULL, 0, NI_NUMERICHOST);
829 rc_log(LOG_ERR,
"radcli_dae_start: bind: %s", strerror(errno));
838 rc_log(LOG_INFO,
"radcli_dae_start: listening on %s port %d", addr_txt, dae->listen_port);
855 if (dae->rh != NULL && dae->rh->active_dae == dae)
856 dae->rh->active_dae = NULL;
862 for (i = 0; i < dae->radsec_queue_len; i++)
864 free_dacs(dae->dacs, dae->n_dacs);
867 free(dae->listen_host);
876static int watchdog_deadline_ms(rc_handle *rh,
int fd)
886 interval = rc_conf_int_id(rh, OPT_WATCHDOG_INTERVAL);
888 time_t last = radcli2_priv_sess_last_msg(rh);
889 long elapsed_ms, remaining_ms;
893 if (rh->watchdog_skipped > last)
894 last = rh->watchdog_skipped;
895 elapsed_ms = (long)(time(0) - last) * 1000L;
896 remaining_ms = (long)interval * 1000L - elapsed_ms;
898 return (remaining_ms > 0) ? (int)remaining_ms : 0;
905static void fold_timeout(
int *timeout_ms,
int candidate)
909 if (*timeout_ms < 0 || candidate < *timeout_ms)
910 *timeout_ms = candidate;
961 size_t *nfds,
int *timeout_ms)
963 rc_handle *rh = (rc_handle *)ctx;
966 if (rh == NULL || pfds == NULL || nfds == NULL || timeout_ms == NULL)
973 if (rh->so_type == RC_SOCKET_TLS || rh->so_type == RC_SOCKET_DTLS) {
982 int dae_radsec = (rh->active_dae != NULL && rh->active_dae->radsec);
985 int fd = radcli2_priv_sess_poll(rh, &events, &sess_timeout_ms);
989 pfds[n].events = events;
999 if (dae_radsec && rh->active_dae->radsec_queue_len > 0)
1002 fold_timeout(timeout_ms, watchdog_deadline_ms(rh, radcli2_priv_sess_fd(rh)));
1004 fold_timeout(timeout_ms, radcli2_priv_reqreg_earliest_deadline_ms(rh));
1007 fold_timeout(timeout_ms, sess_timeout_ms);
1017 if (rh->active_dae != NULL && rh->active_dae->fd != -1) {
1018 pfds[n].fd = rh->active_dae->fd;
1019 pfds[n].events = POLLIN;
1020 pfds[n].revents = 0;
1023 if (rh->so_type == RC_SOCKET_TCP) {
1025 int sess_timeout_ms;
1026 int fd = radcli2_priv_sess_poll(rh, &events, &sess_timeout_ms);
1030 pfds[n].events = events;
1031 pfds[n].revents = 0;
1034 fold_timeout(timeout_ms, sess_timeout_ms);
1035 }
else if (rh->req_fd != -1) {
1036 pfds[n].fd = rh->req_fd;
1037 pfds[n].events = POLLIN;
1038 pfds[n].revents = 0;
1046 fold_timeout(timeout_ms, radcli2_priv_reqreg_earliest_deadline_ms(rh));
1112int radcli2_priv_dae_send_watchdog(radcli_ctx *ctx)
1114 rc_handle *rh = (rc_handle *)ctx;
1115 uint8_t send_buffer[RC_BUFFER_LEN];
1116 unsigned char vector[AUTH_VECTOR_LEN];
1117 char secret[MAX_SECRET_LENGTH + 1];
1118 radcli_avp_list *empty;
1127 if (rh->so_type != RC_SOCKET_TLS && rh->so_type != RC_SOCKET_DTLS)
1130 if (radcli2_priv_sess_fd(rh) == -1)
1134 int interval = rc_conf_int_id(rh, OPT_WATCHDOG_INTERVAL);
1137 (
double)(time(0) - radcli2_priv_sess_last_recv(rh)) >= interval * 2.5) {
1138 rc_log(LOG_WARNING,
"radcli2_priv_dae_send_watchdog: no record received in "
1139 "%.1fx watchdog-interval -- presuming the peer dead "
1140 "and reconnecting", 2.5);
1143 if (radcli2_priv_sess_force_reconnect(rh) < 0)
1148 if (rh->so.static_secret == NULL)
1150 strlcpy(secret, rh->so.static_secret,
sizeof(secret));
1162 radcli2_priv_reqreg_release(rh, rh->watchdog_slot);
1163 rh->watchdog_slot = -1;
1164 if (radcli2_priv_reqreg_pick_id(rh, &
id, &slot) != 0) {
1166 rh->watchdog_skipped = time(0);
1170 ret = radcli_encode_request(rh, PW_STATUS_SERVER, empty, secret,
1171 send_buffer,
id, vector, &total_length);
1174 radcli2_priv_reqreg_release(rh, slot);
1178 ret = radcli2_priv_sess_send_queued(rh, send_buffer, (
size_t)total_length,
1179 RADSEC_SEND_WATCHDOG, -1);
1181 rh->watchdog_slot = slot;
1183 radcli2_priv_reqreg_release(rh, slot);
1185 if (ret == 0 || ret == -2)
1186 rh->watchdog_skipped = time(0);
1202static int get_v4_mapped(
const struct sockaddr *sa,
struct in_addr *out)
1204 const struct sockaddr_in6 *sin6;
1206 if (sa->sa_family != AF_INET6)
1208 sin6 = (
const struct sockaddr_in6 *)sa;
1209 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
1211 memcpy(out, &sin6->sin6_addr.s6_addr[12],
sizeof(*out));
1225static struct radcli_dae_dac *find_dac(
struct radcli_dae_st *dae,
1226 const struct sockaddr *from)
1229 struct in_addr mapped;
1230 int have_mapped = get_v4_mapped(from, &mapped);
1232 for (i = 0; i < dae->n_dacs; i++) {
1233 const struct sockaddr *caddr = (
const struct sockaddr *)&dae->dacs[i].addr;
1235 if (caddr->sa_family == from->sa_family) {
1236 if (memcmp(SA_GET_INADDR(caddr), SA_GET_INADDR(from), SA_GET_INLEN(from)) == 0)
1237 return &dae->dacs[i];
1244 if (have_mapped && caddr->sa_family == AF_INET &&
1245 memcmp(&((
const struct sockaddr_in *)caddr)->sin_addr, &mapped,
1246 sizeof(mapped)) == 0)
1247 return &dae->dacs[i];
1267static char *dup_avp_str(rc_handle *rh,
const radcli_avp_list *attrs, uint32_t attrid)
1270 const radcli_avp *a;
1280 s = malloc(len + 1);
1283 memcpy(s, val, len);
1303static int verify_request_authenticator(uint8_t *buf,
size_t length,
const char *secret)
1305 uint8_t received[AUTH_VECTOR_LEN];
1306 uint8_t calc[AUTH_VECTOR_LEN];
1307 size_t secretlen = rc_secret_len(secret);
1309 if (length < AUTH_HDR_LEN)
1312 memcpy(received, buf + 4, AUTH_VECTOR_LEN);
1313 memset(buf + 4, 0, AUTH_VECTOR_LEN);
1314 memcpy(buf + length, secret, secretlen);
1315 rc_md5_calc(calc, buf, length + secretlen);
1316 memcpy(buf + 4, received, AUTH_VECTOR_LEN);
1318 return rc_memcmp(received, calc, AUTH_VECTOR_LEN) == 0 ? 0 : -1;
1339static int build_reply(
struct radcli_dae_request_st *req, uint8_t reply_code, uint32_t error_cause,
1340 uint8_t *out_buf,
size_t out_cap,
int *out_len)
1342 rc_handle *rh = req->dae->rh;
1343 AUTH_HDR *auth = (AUTH_HDR *)out_buf;
1344 radcli_avp_list *reply_attrs;
1345 const radcli_avp *a;
1346 const radcli_attr_def *d_proxy_state;
1347 int encoded_len, total_length;
1349 uint8_t digest[AUTH_VECTOR_LEN];
1358 if (out_cap < (
size_t)(AUTH_HDR_LEN + 2 + MD5_DIGEST_SIZE))
1362 if (reply_attrs == NULL)
1365 if (error_cause != 0) {
1373 if (d_proxy_state != NULL) {
1387 auth->code = reply_code;
1389 memcpy(auth->vector, req->request_authenticator, AUTH_VECTOR_LEN);
1391 encoded_len = radcli_avp_encode(rh, reply_attrs, req->secret, req->request_authenticator,
1392 auth->data, out_cap - AUTH_HDR_LEN - (2 + MD5_DIGEST_SIZE), NULL);
1394 if (encoded_len < 0)
1397 total_length = AUTH_HDR_LEN + encoded_len;
1398 auth->length = htons((uint16_t)total_length);
1400 if (out_cap < total_length + (2 + MD5_DIGEST_SIZE))
1402 total_length = add_msg_auth_attr(rh, req->secret, auth, total_length);
1404 secretlen = rc_secret_len(req->secret);
1405 if ((
size_t)(out_cap - total_length) < secretlen)
1407 memcpy(out_buf + total_length, req->secret, secretlen);
1408 rc_md5_calc(digest, out_buf, (
size_t)total_length + secretlen);
1409 memcpy(auth->vector, digest, AUTH_VECTOR_LEN);
1411 *out_len = total_length;
1423static int send_reply(
struct radcli_dae_request_st *req, uint8_t reply_code, uint32_t error_cause)
1425 uint8_t send_buffer[RC_BUFFER_LEN];
1428 if (build_reply(req, reply_code, error_cause, send_buffer,
sizeof(send_buffer),
1429 &total_length) != 0)
1432 if (req->dae->radsec) {
1436 if (radcli2_priv_sess_send_queued(req->dae->rh, send_buffer, (
size_t)total_length,
1437 RADSEC_SEND_DAE_REPLY, -1) == -1)
1442 if (sendto(req->dae->fd, send_buffer, (
size_t)total_length, 0,
1443 (
struct sockaddr *)&req->from, req->fromlen) != total_length)
1488 return req->session_id;
1501 return req->user_name;
1514 const radcli_attr_def *d;
1515 const radcli_avp *a;
1517 if (req == NULL || out == NULL)
1527 struct sockaddr_in sin;
1529 memset(&sin, 0,
sizeof(sin));
1530 sin.sin_family = AF_INET;
1531 sin.sin_addr.s_addr = htonl(val);
1532 memset(out, 0,
sizeof(*out));
1533 memcpy(out, &sin,
sizeof(sin));
1541 struct in6_addr addr;
1545 struct sockaddr_in6 sin6;
1547 memset(&sin6, 0,
sizeof(sin6));
1548 sin6.sin6_family = AF_INET6;
1549 sin6.sin6_addr = addr;
1550 memset(out, 0,
sizeof(*out));
1551 memcpy(out, &sin6,
sizeof(sin6));
1569 const radcli_attr_def *d;
1570 const radcli_avp *a;
1602static void record_reply_decision(
struct radcli_dae_request_st *req, uint8_t reply_code,
1603 uint32_t error_cause)
1605 struct radcli_dae_slot *slot;
1607 if (req->dae == NULL)
1609 slot = &req->dae->slots[req->id];
1610 if (slot->valid && slot->pending &&
1611 rc_memcmp(slot->dedup_key, req->dedup_key,
1612 RC_SHA256_DIGEST_SIZE) == 0) {
1614 slot->reply_code = reply_code;
1615 slot->error_cause = error_cause;
1628static int reply_and_record(
struct radcli_dae_request_st *req, uint8_t reply_code, uint32_t error_cause)
1632 if (req == NULL || req->replied)
1636 ret = send_reply(req, reply_code, error_cause);
1637 record_reply_decision(req, reply_code, error_cause);
1650static uint8_t select_reply_code(
const struct radcli_dae_request_st *req,
int ack)
1653 ? (req->code == RADCLI_DISCONNECT_REQUEST ? RADCLI_DISCONNECT_ACK : RADCLI_COA_ACK)
1654 : (req->code == RADCLI_DISCONNECT_REQUEST ? RADCLI_DISCONNECT_NAK : RADCLI_COA_NAK);
1673 return reply_and_record(req, select_reply_code(req, ack), 0);
1689 reply_code = (req->code == RADCLI_DISCONNECT_REQUEST) ? RADCLI_DISCONNECT_NAK : RADCLI_COA_NAK;
1690 return reply_and_record(req, reply_code, error_cause);
1715 void *buf,
size_t *len)
1720 if (req == NULL || buf == NULL || len == NULL)
1723 if (req->is_cached_duplicate) {
1727 reply_code = req->cached_reply_code;
1728 error_cause = req->cached_error_cause;
1732 reply_code = select_reply_code(req, ack);
1735 if (build_reply(req, reply_code, error_cause, (uint8_t *)buf, *len, &out_len) != 0)
1737 *len = (size_t)out_len;
1739 if (!req->is_cached_duplicate) {
1741 record_reply_decision(req, reply_code, error_cause);
1758 free(req->session_id);
1759 free(req->user_name);
1770enum process_result {
1773 PROCESS_DUP_ANSWERED,
1798static enum process_result process_packet(
struct radcli_dae_st *dae, uint8_t *buf,
size_t len,
1799 const struct sockaddr *from, socklen_t fromlen,
1800 struct radcli_dae_request_st **out_req)
1802 rc_handle *rh = dae->rh;
1803 size_t length = len;
1804 struct radcli_dae_dac *dac;
1805 struct radcli_dae_slot *slot;
1808 struct radcli_dae_request_st *req;
1809 radcli_avp_list *attrs = NULL;
1810 const radcli_attr_def *d;
1811 const radcli_avp *a;
1824 secret = rh->so.static_secret;
1829 dac = find_dac(dae, from);
1831 return PROCESS_DROP;
1832 secret = (dac->secret != NULL) ? dac->secret : dae->secret;
1841 if (length < AUTH_HDR_LEN)
1842 return PROCESS_DROP;
1844 uint16_t wire_length;
1846 memcpy(&wire_length, buf + 2,
sizeof(wire_length));
1847 wire_length = ntohs(wire_length);
1848 if (wire_length < AUTH_HDR_LEN || wire_length > length ||
1849 wire_length > RC_MAX_PACKET_LEN)
1850 return PROCESS_DROP;
1851 length = wire_length;
1853 if (buf[0] != RADCLI_DISCONNECT_REQUEST && buf[0] != RADCLI_COA_REQUEST)
1854 return PROCESS_DROP;
1857 if (verify_request_authenticator(buf, length, secret) != 0)
1858 return PROCESS_DROP;
1860 if (radcli_avp_decode(rh, secret, buf + 4, buf + AUTH_HDR_LEN,
1861 length - AUTH_HDR_LEN, 0, &attrs) != 0)
1862 return PROCESS_DROP;
1880 uint8_t zero_vector[AUTH_VECTOR_LEN];
1882 memset(zero_vector, 0,
sizeof(zero_vector));
1883 if (validate_message_authenticator(buf, length - AUTH_HDR_LEN, secret,
1884 zero_vector) != 0) {
1886 return PROCESS_DROP;
1888 }
else if (dae->require_message_authenticator) {
1890 return PROCESS_DROP;
1896 if (dae->max_clock_skew > 0) {
1905 return PROCESS_DROP;
1908 diff = (long)now - (
long)ts;
1911 if (diff > dae->max_clock_skew) {
1913 return PROCESS_DROP;
1937 retention = (dae->max_clock_skew > 0) ? dae->max_clock_skew : 30;
1938 slot = &dae->slots[buf[1]];
1940 req = calloc(1,
sizeof(*req));
1943 return PROCESS_DROP;
1949 memcpy(req->request_authenticator, buf + 4, AUTH_VECTOR_LEN);
1950 rc_sha256_calc(req->dedup_key, buf, length);
1957 if (!dae->radsec && from != NULL) {
1958 memcpy(&req->from, from, fromlen);
1959 req->fromlen = fromlen;
1961 strlcpy(req->secret, secret,
sizeof(req->secret));
1964 req->session_id = dup_avp_str(rh, req->attrs, PW_ACCT_SESSION_ID);
1965 req->user_name = dup_avp_str(rh, req->attrs, PW_USER_NAME);
1967 if (slot->valid && (now - slot->timestamp) <= retention &&
1968 rc_memcmp(slot->dedup_key, req->dedup_key,
1969 RC_SHA256_DIGEST_SIZE) == 0) {
1970 if (!slot->pending) {
1971 req->is_cached_duplicate = 1;
1972 req->cached_reply_code = slot->reply_code;
1973 req->cached_error_cause = slot->error_cause;
1975 return PROCESS_DUP_ANSWERED;
1980 return PROCESS_DROP;
1992 if (!dae->no_nas_check) {
1993 const char *cfg_id = rc_conf_str_id(rh, OPT_NAS_IDENTIFIER);
1995 if (cfg_id != NULL) {
2003 (vlen != strlen(cfg_id) || memcmp(val, cfg_id, vlen) != 0)) {
2004 req->cached_reply_code = (req->code == RADCLI_DISCONNECT_REQUEST)
2005 ? RADCLI_DISCONNECT_NAK : RADCLI_COA_NAK;
2006 req->cached_error_cause = RADCLI_ERROR_NAS_IDENTIFICATION_MISMATCH;
2008 slot->timestamp = now;
2009 memcpy(slot->dedup_key, req->dedup_key, RC_SHA256_DIGEST_SIZE);
2011 slot->reply_code = req->cached_reply_code;
2012 slot->error_cause = req->cached_error_cause;
2013 req->is_cached_duplicate = 1;
2015 return PROCESS_DUP_ANSWERED;
2022 slot->timestamp = now;
2023 memcpy(slot->dedup_key, req->dedup_key, RC_SHA256_DIGEST_SIZE);
2049static void send_radsec_unsupported_nak(rc_handle *rh,
const uint8_t *reqbuf,
size_t reqlen)
2051 uint8_t out[AUTH_HDR_LEN + 6 + 2 + MD5_DIGEST_SIZE];
2052 AUTH_HDR *auth = (AUTH_HDR *)out;
2053 radcli_avp_list *reply_attrs;
2054 const radcli_attr_def *d_ec;
2055 int encoded_len, total_length;
2057 uint8_t digest[AUTH_VECTOR_LEN];
2058 const char *secret = rh->so.static_secret;
2060 if (reqlen < AUTH_HDR_LEN || secret == NULL)
2064 if (reply_attrs == NULL)
2071 auth->code = (reqbuf[0] == RADCLI_DISCONNECT_REQUEST) ? RADCLI_DISCONNECT_NAK : RADCLI_COA_NAK;
2072 auth->id = reqbuf[1];
2073 memcpy(auth->vector, reqbuf + 4, AUTH_VECTOR_LEN);
2075 encoded_len = radcli_avp_encode(rh, reply_attrs, secret, reqbuf + 4,
2076 auth->data,
sizeof(out) - AUTH_HDR_LEN - (2 + MD5_DIGEST_SIZE), NULL);
2078 if (encoded_len < 0)
2081 total_length = AUTH_HDR_LEN + encoded_len;
2082 auth->length = htons((uint16_t)total_length);
2083 total_length = add_msg_auth_attr(rh, (
char *)secret, auth, total_length);
2085 secretlen = rc_secret_len(secret);
2086 if ((
size_t)(
sizeof(out) - total_length) < secretlen)
2088 memcpy(out + total_length, secret, secretlen);
2089 rc_md5_calc(digest, out, (
size_t)total_length + secretlen);
2090 memcpy(auth->vector, digest, AUTH_VECTOR_LEN);
2095 radcli2_priv_sess_send_queued(rh, out, (
size_t)total_length, RADSEC_SEND_DAE_REPLY, -1);
2114void radcli2_priv_dae_on_radsec_packet(rc_handle *rh,
const uint8_t *buf,
size_t len)
2116 struct radcli_dae_st *dae = rh->active_dae;
2117 uint8_t local_buf[RC_BUFFER_LEN];
2118 struct radcli_dae_request_st *req = NULL;
2120 if (dae == NULL || !dae->radsec) {
2121 send_radsec_unsupported_nak(rh, buf, len);
2125 if (len == 0 || len >
sizeof(local_buf) - 1)
2131 memcpy(local_buf, buf, len);
2133 switch (process_packet(dae, local_buf, len, NULL, 0, &req)) {
2135 radsec_queue_push(dae, req);
2137 case PROCESS_DUP_ANSWERED:
2139 send_reply(req, req->cached_reply_code, req->cached_error_cause);
2179 rc_handle *rh = (rc_handle *)ctx;
2180 struct radcli_dae_st *dae;
2181 uint8_t buf[RC_BUFFER_LEN];
2182 struct sockaddr_storage from;
2185 struct radcli_dae_request_st *req = NULL;
2190 if (rh->in_dispatch) {
2191 rc_log(LOG_ERR,
"radcli_ctx_dispatch: reentrant call");
2195 rh->in_dispatch = 1;
2200 if (RC_USES_SESSION(rh)) {
2201 radcli2_priv_sess_progress(rh);
2202 radcli2_priv_sess_flush(rh);
2203 radcli2_priv_sess_drain(rh);
2205 radcli2_priv_reqreg_drain(rh);
2207 radcli2_priv_reqreg_service_timeouts(rh);
2209 if (rh->so_type == RC_SOCKET_TLS || rh->so_type == RC_SOCKET_DTLS) {
2217 if (watchdog_deadline_ms(rh, radcli2_priv_sess_fd(rh)) == 0)
2218 radcli2_priv_dae_send_watchdog(ctx);
2221 dae = rh->active_dae;
2223 rh->in_dispatch = 0;
2231 req = radsec_queue_pop(dae);
2234 if (dae->handler != NULL)
2240 rh->in_dispatch = 0;
2244 if (dae->fd == -1) {
2248 rh->in_dispatch = 0;
2252 fromlen =
sizeof(from);
2253 n = recvfrom(dae->fd, buf,
sizeof(buf) - 1, 0, (
struct sockaddr *)&from, &fromlen);
2259 rh->in_dispatch = 0;
2263 switch (process_packet(dae, buf, (
size_t)n, (
struct sockaddr *)&from, fromlen, &req)) {
2265 if (dae->handler != NULL)
2270 case PROCESS_DUP_ANSWERED:
2272 send_reply(req, req->cached_reply_code, req->cached_error_cause);
2279 rh->in_dispatch = 0;
2314 const struct sockaddr *from, socklen_t fromlen,
2317 struct radcli_dae_request_st *built_req = NULL;
2318 uint8_t local_buf[RC_BUFFER_LEN];
2319 enum process_result result;
2321 if (dae == NULL || buf == NULL || from == NULL || req == NULL ||
2322 len == 0 || len >
sizeof(local_buf) - 1)
2346 if (fromlen <
sizeof(
struct sockaddr_in) || fromlen >
sizeof(
struct sockaddr_storage))
2348 if (from->sa_family == AF_INET6 && fromlen <
sizeof(
struct sockaddr_in6))
2350 if (from->sa_family != AF_INET && from->sa_family != AF_INET6)
2356 memcpy(local_buf, buf, len);
2358 result = process_packet(dae, local_buf, len, from, fromlen, &built_req);
2359 if (result == PROCESS_DROP)
int radcli_dae_req_nas_port(const radcli_dae_request *req, uint32_t *out)
Return the request's NAS-Port.
void radcli_dae_free(radcli_dae *dae)
Release a listener, closing its socket if radcli_dae_start() opened one.
const char * radcli_dae_req_session_id(const radcli_dae_request *req)
Return the request's Acct-Session-Id, if it carried one.
void(* radcli_dae_handler)(radcli_dae_request *req, void *user)
Application callback invoked by radcli_ctx_dispatch() for a validated request.
int radcli_ctx_get_poll(radcli_ctx *ctx, struct pollfd *pfds, size_t max_pfds, size_t *nfds, int *timeout_ms)
Report what to wait for on ctx's behalf, for the caller's own event loop – radcli never calls poll()/...
void radcli_dae_set_handler(radcli_dae *dae, radcli_dae_handler cb, void *user)
Register the callback radcli_ctx_dispatch() invokes for each validated request. May be called before ...
radcli_code radcli_dae_req_code(const radcli_dae_request *req)
Return the received packet's RADIUS code.
int radcli_ctx_dispatch(radcli_ctx *ctx)
Read what is ready on ctx's descriptor(s), validate it, and invoke the registered handler for anythin...
int radcli_dae_reply(radcli_dae_request *req, int ack)
Answer a request with an ACK or NAK, selecting 41/42 or 44/45 from the request's own code,...
struct radcli_dae_st radcli_dae
const char * radcli_dae_req_user_name(const radcli_dae_request *req)
Return the request's User-Name, if it carried one.
int radcli_dae_start(radcli_dae *dae)
Start receiving: binds the socket described by dae-listen.
const radcli_avp_list * radcli_dae_req_attrs(const radcli_dae_request *req)
Return the request's decoded attributes.
int radcli_dae_reply_to_buffer(radcli_dae_request *req, int ack, uint32_t error_cause, void *buf, size_t *len)
Produce a reply as bytes instead of sending it – the L0 counterpart of radcli_dae_reply()/radcli_dae_...
#define RADCLI_CTX_MAX_POLLFDS
int radcli_dae_process(radcli_dae *dae, const void *buf, size_t len, const struct sockaddr *from, socklen_t fromlen, radcli_dae_request **req)
Validate a caller-supplied packet, without a radcli-owned socket – the L0 counterpart of radcli_ctx_d...
#define RADCLI_DAE_DUPLICATE
int radcli_dae_reply_error(radcli_dae_request *req, uint32_t error_cause)
Answer a request with a NAK carrying the given Error-Cause.
radcli_dae * radcli_dae_new(radcli_ctx *ctx, unsigned flags)
Validate dae-* configuration and build a dynamic-authorization listener. Opens no socket – see radcli...
int radcli_dae_req_framed_ip(const radcli_dae_request *req, struct sockaddr_storage *out)
Return the request's Framed-IP-Address or Framed-IPv6-Address.
struct radcli_dae_request_st radcli_dae_request
void radcli_dae_request_free(radcli_dae_request *req)
Release a request.
@ RADCLI_DAE_NO_NAS_CHECK
void radcli_avp_list_free(radcli_avp_list *list)
Free a list and every attribute it holds.
radcli_avp_iter radcli_avp_list_iter(const radcli_avp_list *list)
Begin iterating list.
const radcli_avp * radcli_avp_get(const radcli_avp_list *list, const radcli_attr_def *def, unsigned idx)
Find the idx-th occurrence of an attribute in a list.
int radcli_avp_get_ip6(const radcli_avp *a, struct in6_addr *out, unsigned *prefix)
Read an attribute's value as an IPv6 address or prefix.
int radcli_avp_add_bytes(radcli_avp_list *list, const radcli_attr_def *def, const void *value, size_t len)
Append an attribute holding an arbitrary byte string.
int radcli_avp_get_uint32(const radcli_avp *a, uint32_t *out)
Read an attribute's value as an integer/IPv4-address/date.
radcli_avp_list * radcli_avp_list_new(void)
Create an empty attribute-value pair list.
const radcli_attr_def * radcli_avp_def(const radcli_avp *a)
Return the attribute definition of a.
int radcli_avp_add_uint32(radcli_avp_list *list, const radcli_attr_def *def, uint32_t value)
Append an integer/IPv4-address/date-typed attribute.
const radcli_avp * radcli_avp_iter_next(radcli_avp_iter *it)
Return the current attribute and advance.
int radcli_avp_get_bytes(const radcli_avp *a, const void **out, size_t *len)
Read an attribute's value as raw bytes.
const radcli_attr_def * radcli_dict_lookup_num(const radcli_ctx *ctx, uint32_t attrid, uint32_t vendor)
Look up a dictionary attribute by its legacy numeric ID and vendor.