25#include <radcli/radcli.h>
30#include "dict_rfc_gen.h"
37static int rc_conf_int_2(rc_handle
const *rh,
char const *optname,
int complain);
42static const OPTION config_options_default[] = {
43#define RADCLI_OPT_ENTRY(id, name, type) {name, type, ST_UNDEF, NULL},
45#undef RADCLI_OPT_ENTRY
48#define NUM_OPTIONS ((sizeof(config_options_default))/(sizeof(config_options_default[0])))
66static OPTION *find_option(rc_handle
const *rh,
char const *optname,
unsigned int type)
71 for (i = 0; i < NUM_OPTIONS; i++) {
72 if (!strcmp(rh->config_options[i].name, optname) &&
73 (rh->config_options[i].type & type))
75 return &rh->config_options[i];
90static int rc_ignored_option(
char const *name)
92#define X(n) if (!strcmp(name, n)) return 1;
93 RC_IGNORED_OPTION_TABLE
109static inline OPTION *rc_option_by_id(rc_handle
const *rh, rc_option_id
id)
111 assert(rh != NULL && rh->config_options != NULL);
112 assert((
unsigned)
id < NUM_OPTIONS);
113 return &rh->config_options[id];
122int rc_conf_int_id(rc_handle
const *rh, rc_option_id
id)
124 OPTION *option = rc_option_by_id(rh,
id);
126 assert(option->type & RADCLI_OPT_TYPE_INT);
129 return *((
int *)option->val);
131 rc_log(LOG_INFO,
"radcli2_priv_conf_int: config option %s was not set", option->name);
141char *rc_conf_str_id(rc_handle
const *rh, rc_option_id
id)
143 OPTION *option = rc_option_by_id(rh,
id);
145 assert(option->type & RADCLI_OPT_TYPE_STR);
147 return (
char *)option->val;
156SERVER *rc_conf_srv_id(rc_handle
const *rh, rc_option_id
id)
158 OPTION *option = rc_option_by_id(rh,
id);
160 assert(option->type & RADCLI_OPT_TYPE_SRV);
162 return (SERVER *)option->val;
173static int set_option_str(
char const *filename,
int line, OPTION *option,
char const *p)
176 option->val = (
void *) strdup(p);
177 if (option->val == NULL) {
178 rc_log(LOG_CRIT,
"read_config: out of memory");
207static int set_option_int(
char const *filename,
int line, OPTION *option,
char const *p)
213 rc_log(LOG_ERR,
"%s: line %d: bogus option value", filename, line);
219 if (strcmp(option->name,
"watchdog-interval") == 0 && val >= 1 && val <= 5) {
220 rc_log(LOG_ERR,
"%s: line %d: watchdog-interval must be 0 (disabled) "
221 "or at least 6 seconds, not %d", filename, line, val);
225 if ((iptr = malloc(
sizeof(*iptr))) == NULL) {
226 rc_log(LOG_CRIT,
"read_config: out of memory");
231 option->val = (
void *) iptr;
246static void server_free_entries(SERVER *serv,
unsigned from,
unsigned to)
250 for (i = from; i < to; i++) {
252 free(serv->secret[i]);
253 serv->name[i] = NULL;
254 serv->secret[i] = NULL;
268static int set_option_srv(rc_handle *rh,
char const *filename,
int line, OPTION *option,
char const *p)
281 if (p_dupe == NULL) {
282 rc_log(LOG_ERR,
"%s: line %d: Invalid option or memory failure", filename, line);
286 serv = (SERVER *) option->val;
288 serv = calloc(1,
sizeof(*serv));
290 rc_log(LOG_CRIT,
"read_config: out of memory");
296 start_max = serv->max;
298 p_pointer = strtok_r(p_dupe,
", \t", &p_save);
300 while(p_pointer != NULL) {
301 if (serv->max >= RC_SERVER_MAX) {
302 DEBUG(rh, LOG_ERR,
"cannot set more than %d servers", RC_SERVER_MAX);
306 DEBUG(rh, LOG_ERR,
"processing server: %s", p_pointer);
308 if ((q = strchr(p_pointer,
'[')) != NULL) {
313 q = strchr(p_pointer,
']');
315 rc_log(LOG_CRIT,
"read_config: IPv6 parse error");
326 if((s=strchr(q,
':')) != NULL) {
329 serv->secret[serv->max] = strdup(s);
330 if (serv->secret[serv->max] == NULL) {
331 rc_log(LOG_CRIT,
"read_config: out of memory");
337 if ((q = strchr(p_pointer,
':')) != NULL) {
342 if((s = strchr(q,
':')) != NULL) {
345 serv->secret[serv->max] = strdup(s);
346 if (serv->secret[serv->max] == NULL) {
347 rc_log(LOG_CRIT,
"read_config: out of memory");
353 if(q && strlen(q) > 0) {
354 serv->port[serv->max] = atoi(q);
356 if (!strcmp(option->name,
"authserver"))
357 if ((svp = getservbyname (
"radius",
"udp")) == NULL)
358 serv->port[serv->max] = PW_AUTH_UDP_PORT;
360 serv->port[serv->max] = ntohs ((
unsigned int) svp->s_port);
361 else if (!strcmp(option->name,
"acctserver"))
362 if ((svp = getservbyname (
"radacct",
"udp")) == NULL)
363 serv->port[serv->max] = PW_ACCT_UDP_PORT;
365 serv->port[serv->max] = ntohs ((
unsigned int) svp->s_port);
367 rc_log(LOG_ERR,
"%s: line %d: no default port for %s", filename, line, option->name);
372 serv->name[serv->max] = strdup(p_pointer);
373 if (serv->name[serv->max] == NULL) {
374 rc_log(LOG_CRIT,
"read_config: out of memory");
379 p_pointer = strtok_r(NULL,
", \t", &p_save);
383 if (option->val == NULL)
384 option->val = (
void *)serv;
395 server_free_entries(serv, start_max, serv->max);
396 serv->max = start_max;
397 if (serv->max < RC_SERVER_MAX) {
398 free(serv->secret[serv->max]);
399 serv->secret[serv->max] = NULL;
401 if (option->val == NULL)
417int radcli2_priv_add_config(rc_handle *rh,
char const *option_name,
char const *option_val,
char const *source,
int line)
421 if ((option = find_option(rh, option_name, OT_ANY)) == NULL)
423 if (rc_ignored_option(option_name))
425 rc_log(LOG_ERR,
"ERROR: unrecognized option: %s", option_name);
429 if (option->status != ST_UNDEF)
431 rc_log(LOG_ERR,
"ERROR: duplicate option: %s", option_name);
435 switch (option->type) {
436 case RADCLI_OPT_TYPE_STR:
437 if (set_option_str(source, line, option, option_val) < 0) {
441 case RADCLI_OPT_TYPE_INT:
442 if (set_option_int(source, line, option, option_val) < 0) {
446 case RADCLI_OPT_TYPE_SRV:
447 if (set_option_srv(rh, source, line, option, option_val) < 0) {
452 rc_log(LOG_CRIT,
"radcli2_priv_add_config: impossible case branch!");
469rc_handle *radcli2_priv_config_init(rc_handle *rh)
471 SERVER *authservers = NULL;
476 rh->config_options = malloc(
sizeof(config_options_default));
477 if (rh->config_options == NULL)
479 rc_log(LOG_CRIT,
"radcli2_priv_config_init: out of memory");
480 radcli2_priv_destroy(rh);
483 memcpy(rh->config_options, &config_options_default,
sizeof(config_options_default));
485 auth = find_option(rh,
"authserver", OT_ANY);
487 authservers = calloc(1,
sizeof(SERVER));
488 if(authservers == NULL) {
489 rc_log(LOG_CRIT,
"radcli2_priv_config_init: error initializing server structs");
490 radcli2_priv_destroy(rh);
493 auth->val = authservers;
496 acct = find_option(rh,
"acctserver", OT_ANY);
498 acctservers = calloc(1,
sizeof(SERVER));
499 if(acctservers == NULL) {
500 rc_log(LOG_CRIT,
"radcli2_priv_config_init: error initializing server structs");
501 radcli2_priv_destroy(rh);
502 if(authservers) free(authservers);
505 acct->val = acctservers;
514static ssize_t plain_sendto(
void *ptr,
int sockfd,
515 const void *buf,
size_t len,
int flags,
516 const struct sockaddr *dest_addr, socklen_t addrlen)
518 return sendto(sockfd, buf, len, flags, dest_addr, addrlen);
524static ssize_t plain_recvfrom(
void *ptr,
int sockfd,
525 void *buf,
size_t len,
int flags,
526 struct sockaddr *src_addr, socklen_t * addrlen)
528 return recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
534static void plain_close_fd(
int fd)
546static int plain_get_fd(
void *ptr,
struct sockaddr *our_sockaddr)
550 sockfd = socket(our_sockaddr->sa_family, SOCK_DGRAM, 0);
555 if (our_sockaddr->sa_family == AF_INET)
556 ((
struct sockaddr_in *)our_sockaddr)->sin_port = 0;
558 ((
struct sockaddr_in6 *)our_sockaddr)->sin6_port = 0;
560 if (bind(sockfd, SA(our_sockaddr), SA_LEN(our_sockaddr)) < 0) {
567static const rc_sockets_override default_socket_funcs = {
568 .get_fd = plain_get_fd,
569 .close_fd = plain_close_fd,
570 .sendto = plain_sendto,
571 .recvfrom = plain_recvfrom
580static int set_addr(
struct sockaddr_storage *ss,
const char *ip)
582 memset(ss, 0,
sizeof(*ss));
583 if (inet_pton(AF_INET, ip, &((
struct sockaddr_in *)ss)->sin_addr) == 1) {
584 ss->ss_family = AF_INET;
585 }
else if (inet_pton(AF_INET6, ip, &((
struct sockaddr_in6 *)ss)->sin6_addr) == 1) {
586 ss->ss_family = AF_INET6;
588 rc_log(LOG_CRIT,
"invalid IP address for nas-ip: %s", ip);
613static void apply_secret_fallback_one(rc_handle *rh, rc_option_id
id,
const char *secret)
615 SERVER *serv = rc_conf_srv_id(rh,
id);
618 if (serv == NULL || serv->max == 0)
620 if (serv->secret[0] != NULL && serv->secret[0][0] !=
'\0')
623 dup = strdup(secret);
627 free(serv->secret[0]);
628 serv->secret[0] = dup;
636static void apply_secret_fallback(rc_handle *rh)
638 const char *secret = rc_conf_str_id(rh, OPT_SECRET);
640 if (secret == NULL || secret[0] ==
'\0')
643 apply_secret_fallback_one(rh, OPT_AUTHSERVER, secret);
644 apply_secret_fallback_one(rh, OPT_ACCTSERVER, secret);
656static void apply_int_default(rc_handle *rh,
char const *optname,
int def)
658 OPTION *option = find_option(rh, optname, RADCLI_OPT_TYPE_INT);
661 if (option == NULL || option->val != NULL)
664 val = malloc(
sizeof(*val));
687static int radcli2_priv_check_config(rc_handle *rh,
char const *source)
699 srv = rc_conf_srv_id(rh, OPT_AUTHSERVER);
700 if (!srv || !srv->max)
702 if (rc_conf_str_id(rh, OPT_DAE_ACCEPT) == NULL)
704 rc_log(LOG_ERR,
"%s: no authserver or DAE listener specified", source);
715 srv = rc_conf_srv_id(rh, OPT_ACCTSERVER);
716 if (!srv || !srv->max)
722 const char *stype = rc_conf_str_id(rh, OPT_SERV_TYPE);
724 stype = rc_conf_str_id(rh, OPT_SERV_AUTH_TYPE);
726 (strcasecmp(stype,
"tcp") != 0 && strcasecmp(stype,
"tls") != 0 &&
727 strcasecmp(stype,
"dtls") != 0))
728 rc_log(LOG_DEBUG,
"%s: no acctserver specified", source);
732 if (rc_conf_int_id(rh, OPT_RADIUS_TIMEOUT) <= 0)
734 rc_log(LOG_ERR,
"%s: radius_timeout <= 0 is illegal", source);
737 if (rc_conf_int_id(rh, OPT_RADIUS_RETRIES) < 0)
739 rc_log(LOG_ERR,
"%s: radius_retries < 0 is illegal", source);
758int radcli2_priv_apply_config(rc_handle *rh)
763 if (radcli2_priv_check_config(rh,
"radcli2_priv_apply_config") == -1)
766 apply_secret_fallback(rh);
767 apply_int_default(rh,
"watchdog-interval", 15);
768 apply_int_default(rh,
"dae-max-clock-skew", 300);
770 memset(&rh->own_bind_addr, 0,
sizeof(rh->own_bind_addr));
771 rh->own_bind_addr_set = 0;
772 rc_own_bind_addr(rh, &rh->own_bind_addr);
773 rh->own_bind_addr_set = 1;
775 txt = rc_conf_str_id(rh, OPT_NAS_IP);
777 if (set_addr(&rh->nas_addr, txt) < 0)
779 rh->nas_addr_set = 1;
782 txt = rc_conf_str_id(rh, OPT_SERV_TYPE);
784 txt = rc_conf_str_id(rh, OPT_SERV_AUTH_TYPE);
789 if (strcasecmp(txt,
"udp") == 0) {
790 memset(&rh->so, 0,
sizeof(rh->so));
792 memcpy(&rh->so, &default_socket_funcs,
sizeof(rh->so));
794 }
else if (strcasecmp(txt,
"tcp") == 0) {
795 ret = radcli2_priv_sess_init_tcp(rh);
797 }
else if (strcasecmp(txt,
"dtls") == 0) {
798 ret = rc_init_tls(rh, SEC_FLAG_DTLS);
799 }
else if (strcasecmp(txt,
"tls") == 0) {
800 ret = rc_init_tls(rh, 0);
803 rc_log(LOG_CRIT,
"unknown server type: %s", txt);
808 rc_log(LOG_CRIT,
"error initializing %s", txt);
824int radcli2_priv_load_builtin_dict(rc_handle *rh)
826 if (radcli2_priv_read_dictionary_from_buffer(rh, rc_rfc_dictionary,
827 sizeof(rc_rfc_dictionary) - 1) != 0) {
828 rc_log(LOG_CRIT,
"radcli2_priv_load_builtin_dict: failed to load built-in RFC dictionary");
845rc_handle *radcli2_priv_read_config(
char const *filename,
int skip_builtin_dict)
848 char *buffer = NULL, *p;
857 rh = radcli2_priv_new();
861 rh->config_options = malloc(
sizeof(config_options_default));
862 if (rh->config_options == NULL) {
863 rc_log(LOG_CRIT,
"radcli2_priv_read_config: out of memory");
864 radcli2_priv_destroy(rh);
867 memcpy(rh->config_options, &config_options_default,
sizeof(config_options_default));
869 if ((configfd = fopen(filename,
"r")) == NULL)
871 rc_log(LOG_ERR,
"radcli2_priv_read_config: can't open %s: %s", filename, strerror(errno));
872 radcli2_priv_destroy(rh);
877 while ((nread = getline(&buffer, &bufsize, configfd)) != -1)
881 if (nread > 0 && buffer[nread-1] ==
'\n')
882 buffer[--nread] =
'\0';
886 if ((*p ==
'#') || (*p ==
'\0'))
889 if ((pos = strcspn(p,
"\t ")) == 0) {
890 rc_log(LOG_ERR,
"%s: line %d: bogus format: %s", filename, line, p);
896 if ((option = find_option(rh, p, OT_ANY)) == NULL) {
897 if (rc_ignored_option(p)) {
898 rc_log(LOG_INFO,
"%s: line %d: option '%s' is no longer used, ignoring", filename, line, p);
901 rc_log(LOG_ERR,
"%s: line %d: unrecognized keyword: %s", filename, line, p);
905 if (option->status != ST_UNDEF) {
906 rc_log(LOG_ERR,
"%s: line %d: duplicate option line: %s", filename, line, p);
915 while (pos != 0 && isspace(p[pos]))
920 switch (option->type) {
921 case RADCLI_OPT_TYPE_STR:
922 if (set_option_str(filename, line, option, p) < 0)
925 case RADCLI_OPT_TYPE_INT:
926 if (set_option_int(filename, line, option, p) < 0)
929 case RADCLI_OPT_TYPE_SRV:
930 if (set_option_srv(rh, filename, line, option, p) < 0)
934 rc_log(LOG_CRIT,
"radcli2_priv_read_config: impossible case branch!");
941 if (radcli2_priv_test_config(rh, filename) == -1) {
942 radcli2_priv_destroy(rh);
947 int clientdebug = rc_conf_int_2(rh,
"clientdebug", FALSE);
948 if(clientdebug > 0) {
949 rh->debug = clientdebug;
956 if (!skip_builtin_dict && radcli2_priv_load_builtin_dict(rh) != 0) {
957 radcli2_priv_destroy(rh);
961 p = rc_conf_str_id(rh, OPT_DICTIONARY);
963 if (radcli2_priv_read_dictionary(rh, p) != 0) {
964 rc_log(LOG_CRIT,
"could not load dictionary");
965 radcli2_priv_destroy(rh);
975 radcli2_priv_destroy(rh);
985char *radcli2_priv_conf_str(rc_handle
const *rh,
char const *optname)
989 option = find_option(rh, optname, RADCLI_OPT_TYPE_STR);
991 if (option != NULL) {
992 return (
char *)option->val;
994 rc_log(LOG_CRIT,
"radcli2_priv_conf_str: unknown config option requested: %s", optname);
1007static int rc_conf_int_2(rc_handle
const *rh,
char const *optname,
int complain)
1011 option = find_option(rh, optname, RADCLI_OPT_TYPE_INT);
1013 if (option != NULL) {
1015 return *((
int *)option->val);
1016 }
else if(complain) {
1017 rc_log(LOG_ERR,
"radcli2_priv_conf_int: config option %s was not set", optname);
1021 rc_log(LOG_CRIT,
"radcli2_priv_conf_int: unknown config option requested: %s", optname);
1032int radcli2_priv_conf_int(rc_handle
const *rh,
char const *optname)
1034 return rc_conf_int_2(rh, optname, TRUE);
1043SERVER *radcli2_priv_conf_srv(rc_handle
const *rh,
char const *optname)
1047 option = find_option(rh, optname, RADCLI_OPT_TYPE_SRV);
1049 if (option != NULL) {
1050 return (SERVER *)option->val;
1052 rc_log(LOG_CRIT,
"radcli2_priv_conf_srv: unknown config option requested: %s", optname);
1063int radcli2_priv_test_config(rc_handle *rh,
char const *filename)
1065 if (radcli2_priv_check_config(rh, filename) == -1)
1068 if (radcli2_priv_apply_config(rh) == -1) {
1081static int find_match (
const struct addrinfo* addr,
const struct addrinfo *hostname)
1083 const struct addrinfo *ptr, *ptr2;
1084 unsigned len1, len2;
1090 len1 = SA_GET_INLEN(ptr->ai_addr);
1091 len2 = SA_GET_INLEN(ptr2->ai_addr);
1095 memcmp(SA_GET_INADDR(ptr->ai_addr), SA_GET_INADDR(ptr2->ai_addr), len1) == 0) {
1098 ptr2 = ptr2->ai_next;
1110static int rc_ipaddr_local(
const struct sockaddr *addr)
1112 int temp_sock, res, serrno;
1113 struct sockaddr_storage tmpaddr;
1115 memcpy(&tmpaddr, addr, SA_LEN(addr));
1117 temp_sock = socket(addr->sa_family, SOCK_DGRAM, 0);
1118 if (temp_sock == -1)
1121 if (addr->sa_family == AF_INET) {
1122 ((
struct sockaddr_in*)&tmpaddr)->sin_port = 0;
1124 ((
struct sockaddr_in6*)&tmpaddr)->sin6_port = 0;
1126 res = bind(temp_sock, SA(&tmpaddr), SS_LEN(&tmpaddr));
1131 if (serrno == EADDRNOTAVAIL)
1141static int rc_is_myname(
const struct addrinfo *info)
1143 const struct addrinfo *p;
1148 res = rc_ipaddr_local(p->ai_addr);
1149 if (res == 0 || res == -1) {
1167int radcli2_priv_find_server_addr (rc_handle
const *rh,
char const *server_name,
1168 struct addrinfo** info,
char *secret,
rc_type type)
1174 char *buffer = NULL;
1176 char hostnm[AUTH_ID_LEN + 1];
1180 struct addrinfo *tmpinfo = NULL;
1181 const char *fservers;
1184 if ((*info = rc_getaddrinfo (server_name, type==
AUTH?PW_AI_AUTH:PW_AI_ACCT)) == NULL)
1189 case AUTH: servers = rc_conf_srv_id(rh, OPT_AUTHSERVER);
break;
1190 case ACCT: servers = rc_conf_srv_id(rh, OPT_ACCTSERVER);
break;
1191 default: servers = NULL;
1194 if (servers != NULL)
1198 for (servernum = 0; servernum < servers->max; servernum++)
1200 if( (strcmp(server_name, servers->name[servernum]) == 0) &&
1201 (servers->secret[servernum] != NULL) )
1203 memset(secret,
'\0', MAX_SECRET_LENGTH);
1204 strlcpy(secret, servers->secret[servernum], MAX_SECRET_LENGTH);
1214 fservers = rc_conf_str_id(rh, OPT_SERVERS);
1215 if (fservers != NULL) {
1216 if ((clientfd = fopen (fservers,
"r")) == NULL)
1218 rc_log(LOG_ERR,
"rc_find_server: couldn't open file: %s: %s", strerror(errno), fservers);
1222 while (getline (&buffer, &bufsize, clientfd) != -1)
1227 if ((h = strtok_r(buffer,
" \t\n", &buffer_save)) == NULL)
1230 strlcpy (hostnm, h, AUTH_ID_LEN);
1232 if ((s = strtok_r (NULL,
" \t\n", &buffer_save)) == NULL)
1235 strlcpy (secret, s, MAX_SECRET_LENGTH);
1237 if (!strchr (hostnm,
'/'))
1239 tmpinfo = rc_getaddrinfo(hostnm, 0);
1242 result = find_match (*info, tmpinfo);
1249 freeaddrinfo(tmpinfo);
1255 strtok_r(hostnm,
"/", &hostnm_save);
1256 tmpinfo = rc_getaddrinfo(hostnm, 0);
1259 if (rc_is_myname(tmpinfo) == 0)
1261 if (find_match (*info, tmpinfo) == 0)
1269 if (find_match (*info, tmpinfo) == 0)
1275 freeaddrinfo(tmpinfo);
1299 memset(secret,
'\0', MAX_SECRET_LENGTH);
1303 memset (secret,
'\0', MAX_SECRET_LENGTH);
1304 rc_log(LOG_ERR,
"rc_find_server: couldn't find RADIUS server %s in %s",
1305 server_name, rc_conf_str_id(rh, OPT_SERVERS));
1313 freeaddrinfo(*info);
1318 freeaddrinfo(tmpinfo);
1330void radcli2_priv_config_free(rc_handle *rh)
1335 if (rh->config_options == NULL)
1338 for (i = 0; i < NUM_OPTIONS; i++) {
1339 if (rh->config_options[i].val == NULL)
1341 if (rh->config_options[i].type == RADCLI_OPT_TYPE_SRV) {
1342 serv = (SERVER *)rh->config_options[i].val;
1343 server_free_entries(serv, 0, serv->max);
1346 free(rh->config_options[i].val);
1349 free(rh->config_options);
1350 free(rh->first_dict_read);
1351 rh->config_options = NULL;
1352 rh->first_dict_read = NULL;
1360rc_handle *radcli2_priv_new(
void)
1364 rh = calloc(1,
sizeof(*rh));
1366 rc_log(LOG_CRIT,
"radcli2_priv_new: out of memory");
1371 rh->watchdog_slot = -1;
1379void radcli2_priv_destroy(rc_handle *rh)
1381 radcli2_priv_dict_free(rh);
1382 radcli2_priv_sess_deinit(rh);
1383 radcli2_priv_config_free(rh);
1387 if (rh->req_fd != -1 && rh->so.close_fd)
1388 rh->so.close_fd(rh->req_fd);
1390 free(rh->tls_psk_identity);
1391 free(rh->tls_psk_key);
@ ACCT
Request for accounting server.
@ AUTH
Request for authentication server.
@ RC_SOCKET_UDP
Plain UDP socket.
@ RC_SOCKET_DTLS
DTLS socket.
@ RC_SOCKET_TLS
TLS socket.