2 * Copyright (C) 2006-2008 Stig Venaas <venaas@uninett.no>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
9 /* For UDP there is one server instance consisting of udpserverrd and udpserverth
10 * rd is responsible for init and launching wr
11 * For TLS there is a server instance that launches tlsserverrd for each TLS peer
12 * each tlsserverrd launches tlsserverwr
13 * For each UDP/TLS peer there is clientrd and clientwr, clientwr is responsible
14 * for init and launching rd
16 * serverrd will receive a request, processes it and puts it in the requestq of
17 * the appropriate clientwr
18 * clientwr monitors its requestq and sends requests
19 * clientrd looks for responses, processes them and puts them in the replyq of
20 * the peer the request came from
21 * serverwr monitors its reply and sends replies
23 * In addition to the main thread, we have:
24 * If UDP peers are configured, there will be 2 + 2 * #peers UDP threads
25 * If TLS peers are configured, there will initially be 2 * #peers TLS threads
26 * For each TLS peer connecting to us there will be 2 more TLS threads
27 * This is only for connected peers
28 * Example: With 3 UDP peer and 30 TLS peers, there will be a max of
29 * 1 + (2 + 2 * 3) + (2 * 30) + (2 * 30) = 129 threads
33 #include <sys/socket.h>
34 #include <netinet/in.h>
39 #include <sys/types.h>
40 #include <arpa/inet.h>
44 #include <openssl/ssl.h>
45 #include <openssl/rand.h>
46 #include <openssl/err.h>
47 #include <openssl/md5.h>
48 #include <openssl/hmac.h>
49 #include <openssl/x509v3.h>
51 #include "radsecproxy.h"
53 static struct options options;
54 static struct client *clients = NULL;
55 static struct server *servers = NULL;
56 static struct realm *realms = NULL;
57 static struct tls *tlsconfs = NULL;
59 static int client_udp_count = 0;
60 static int client_tls_count = 0;
61 static int client_count = 0;
62 static int server_udp_count = 0;
63 static int server_tls_count = 0;
64 static int server_count = 0;
65 static int realm_count = 0;
66 static int tls_count = 0;
68 static struct peer *tcp_server_listen;
69 static struct peer *udp_server_listen;
70 static struct replyq udp_server_replyq;
71 static int udp_server_sock = -1;
72 static pthread_mutex_t *ssl_locks;
73 static long *ssl_lock_count;
77 /* callbacks for making OpenSSL thread safe */
78 unsigned long ssl_thread_id() {
79 return (unsigned long)pthread_self();
82 void ssl_locking_callback(int mode, int type, const char *file, int line) {
83 if (mode & CRYPTO_LOCK) {
84 pthread_mutex_lock(&ssl_locks[type]);
85 ssl_lock_count[type]++;
87 pthread_mutex_unlock(&ssl_locks[type]);
90 static int pem_passwd_cb(char *buf, int size, int rwflag, void *userdata) {
91 int pwdlen = strlen(userdata);
92 if (rwflag != 0 || pwdlen > size) /* not for decryption or too large */
94 memcpy(buf, userdata, pwdlen);
98 static int verify_cb(int ok, X509_STORE_CTX *ctx) {
103 err_cert = X509_STORE_CTX_get_current_cert(ctx);
104 err = X509_STORE_CTX_get_error(ctx);
105 depth = X509_STORE_CTX_get_error_depth(ctx);
107 if (depth > MAX_CERT_DEPTH) {
109 err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
110 X509_STORE_CTX_set_error(ctx, err);
114 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, 256);
115 debug(DBG_WARN, "verify error: num=%d:%s:depth=%d:%s", err, X509_verify_cert_error_string(err), depth, buf);
118 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
119 X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert), buf, 256);
120 debug(DBG_WARN, "\tIssuer=%s", buf);
122 case X509_V_ERR_CERT_NOT_YET_VALID:
123 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
124 debug(DBG_WARN, "\tCertificate not yet valid");
126 case X509_V_ERR_CERT_HAS_EXPIRED:
127 debug(DBG_WARN, "Certificate has expired");
129 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
130 debug(DBG_WARN, "Certificate no longer valid (after notAfter)");
135 printf("certificate verify returns %d\n", ok);
140 int resolvepeer(struct peer *peer, int ai_flags) {
141 struct addrinfo hints, *addrinfo;
143 memset(&hints, 0, sizeof(hints));
144 hints.ai_socktype = (peer->type == 'T' ? SOCK_STREAM : SOCK_DGRAM);
145 hints.ai_family = AF_UNSPEC;
146 hints.ai_flags = ai_flags;
147 if (getaddrinfo(peer->host, peer->port, &hints, &addrinfo)) {
148 debug(DBG_WARN, "resolvepeer: can't resolve %s port %s", peer->host, peer->port);
153 freeaddrinfo(peer->addrinfo);
154 peer->addrinfo = addrinfo;
158 int connecttoserver(struct addrinfo *addrinfo) {
160 struct addrinfo *res;
163 for (res = addrinfo; res; res = res->ai_next) {
164 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
166 debug(DBG_WARN, "connecttoserver: socket failed");
169 if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
171 debug(DBG_WARN, "connecttoserver: connect failed");
178 int bindtoaddr(struct addrinfo *addrinfo) {
180 struct addrinfo *res;
182 for (res = addrinfo; res; res = res->ai_next) {
183 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
185 debug(DBG_WARN, "bindtoaddr: socket failed");
188 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
189 if (!bind(s, res->ai_addr, res->ai_addrlen))
191 debug(DBG_WARN, "bindtoaddr: bind failed");
197 /* returns the client with matching address, or NULL */
198 /* if client argument is not NULL, we only check that one client */
199 struct client *find_client(char type, struct sockaddr *addr, struct client *client) {
200 struct sockaddr_in6 *sa6 = NULL;
201 struct in_addr *a4 = NULL;
204 struct addrinfo *res;
206 if (addr->sa_family == AF_INET6) {
207 sa6 = (struct sockaddr_in6 *)addr;
208 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr))
209 a4 = (struct in_addr *)&sa6->sin6_addr.s6_addr[12];
211 a4 = &((struct sockaddr_in *)addr)->sin_addr;
213 c = (client ? client : clients);
214 for (i = 0; i < client_count; i++) {
215 if (c->peer.type == type)
216 for (res = c->peer.addrinfo; res; res = res->ai_next)
217 if ((a4 && res->ai_family == AF_INET &&
218 !memcmp(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, 4)) ||
219 (sa6 && res->ai_family == AF_INET6 &&
220 !memcmp(&sa6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 16)))
229 /* returns the server with matching address, or NULL */
230 /* if server argument is not NULL, we only check that one server */
231 struct server *find_server(char type, struct sockaddr *addr, struct server *server) {
232 struct sockaddr_in6 *sa6 = NULL;
233 struct in_addr *a4 = NULL;
236 struct addrinfo *res;
238 if (addr->sa_family == AF_INET6) {
239 sa6 = (struct sockaddr_in6 *)addr;
240 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr))
241 a4 = (struct in_addr *)&sa6->sin6_addr.s6_addr[12];
243 a4 = &((struct sockaddr_in *)addr)->sin_addr;
245 s = (server ? server : servers);
246 for (i = 0; i < server_count; i++) {
247 if (s->peer.type == type)
248 for (res = s->peer.addrinfo; res; res = res->ai_next)
249 if ((a4 && res->ai_family == AF_INET &&
250 !memcmp(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, 4)) ||
251 (sa6 && res->ai_family == AF_INET6 &&
252 !memcmp(&sa6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 16)))
261 /* exactly one of client and server must be non-NULL */
262 /* if *peer == NULL we return who we received from, else require it to be from peer */
263 /* return from in sa if not NULL */
264 unsigned char *radudpget(int s, struct client **client, struct server **server, struct sockaddr_storage *sa) {
267 unsigned char buf[65536], *rad;
268 struct sockaddr_storage from;
269 socklen_t fromlen = sizeof(from);
272 cnt = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&from, &fromlen);
274 debug(DBG_WARN, "radudpget: recv failed");
277 debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from, fromlen));
280 debug(DBG_WARN, "radudpget: packet too small");
286 debug(DBG_WARN, "radudpget: length too small");
291 debug(DBG_WARN, "radudpget: packet smaller than length field in radius header");
295 debug(DBG_DBG, "radudpget: packet was padded with %d bytes", cnt - len);
298 ? (void *)find_client('U', (struct sockaddr *)&from, *client)
299 : (void *)find_server('U', (struct sockaddr *)&from, *server));
301 debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer, ignoring");
308 debug(DBG_ERR, "radudpget: malloc failed");
310 memcpy(rad, buf, len);
312 *client = (struct client *)f; /* only need this if *client == NULL, but if not NULL *client == f here */
314 *server = (struct server *)f; /* only need this if *server == NULL, but if not NULL *server == f here */
320 int subjectaltnameaddr(X509 *cert, int family, struct in6_addr *addr) {
321 int loc, i, l, n, r = 0;
324 STACK_OF(GENERAL_NAME) *alt;
327 debug(DBG_DBG, "subjectaltnameaddr");
329 loc = X509_get_ext_by_NID(cert, NID_subject_alt_name, -1);
333 ex = X509_get_ext(cert, loc);
334 alt = X509V3_EXT_d2i(ex);
338 n = sk_GENERAL_NAME_num(alt);
339 for (i = 0; i < n; i++) {
340 gn = sk_GENERAL_NAME_value(alt, i);
341 if (gn->type != GEN_IPADD)
344 v = (char *)ASN1_STRING_data(gn->d.ia5);
345 l = ASN1_STRING_length(gn->d.ia5);
346 if (((family == AF_INET && l == sizeof(struct in_addr)) || (family == AF_INET6 && l == sizeof(struct in6_addr)))
347 && !memcmp(v, &addr, l)) {
352 GENERAL_NAMES_free(alt);
356 int subjectaltnameregexp(X509 *cert, int type, char *exact, regex_t *regex) {
357 int loc, i, l, n, r = 0;
360 STACK_OF(GENERAL_NAME) *alt;
363 debug(DBG_DBG, "subjectaltnameregexp");
365 loc = X509_get_ext_by_NID(cert, NID_subject_alt_name, -1);
369 ex = X509_get_ext(cert, loc);
370 alt = X509V3_EXT_d2i(ex);
374 n = sk_GENERAL_NAME_num(alt);
375 for (i = 0; i < n; i++) {
376 gn = sk_GENERAL_NAME_value(alt, i);
377 if (gn->type != type)
380 v = (char *)ASN1_STRING_data(gn->d.ia5);
381 l = ASN1_STRING_length(gn->d.ia5);
385 printfchars(NULL, gn->type == GEN_DNS ? "dns" : "uri", NULL, v, l);
388 if (memcmp(v, exact, l))
391 s = stringcopy((char *)v, l);
393 debug(DBG_ERR, "malloc failed");
396 if (regexec(regex, s, 0, NULL, 0)) {
405 GENERAL_NAMES_free(alt);
409 int tlsverifycert(struct peer *peer) {
416 uint8_t type = 0; /* 0 for DNS, AF_INET for IPv4, AF_INET6 for IPv6 */
418 struct in6_addr addr;
420 if (SSL_get_verify_result(peer->ssl) != X509_V_OK) {
421 debug(DBG_ERR, "tlsverifycert: basic validation failed");
422 while ((error = ERR_get_error()))
423 debug(DBG_ERR, "tlsverifycert: TLS: %s", ERR_error_string(error, NULL));
427 cert = SSL_get_peer_certificate(peer->ssl);
429 debug(DBG_ERR, "tlsverifycert: failed to obtain certificate");
433 if (inet_pton(AF_INET, peer->host, &addr))
435 else if (inet_pton(AF_INET6, peer->host, &addr))
438 r = type ? subjectaltnameaddr(cert, type, &addr) : subjectaltnameregexp(cert, GEN_DNS, peer->host, NULL);
442 debug(DBG_DBG, "tlsverifycert: No subjectaltname matching %s %s", type ? "address" : "host", peer->host);
445 debug(DBG_DBG, "tlsverifycert: Found subjectaltname matching %s %s", type ? "address" : "host", peer->host);
447 nm = X509_get_subject_name(cert);
450 loc = X509_NAME_get_index_by_NID(nm, NID_commonName, loc);
453 e = X509_NAME_get_entry(nm, loc);
454 s = X509_NAME_ENTRY_get_data(e);
455 v = (char *) ASN1_STRING_data(s);
456 l = ASN1_STRING_length(s);
460 printfchars(NULL, "cn", NULL, v, l);
462 if (l == strlen(peer->host) && !strncasecmp(peer->host, v, l)) {
464 debug(DBG_DBG, "tlsverifycert: Found cn matching host %s, All OK", peer->host);
470 debug(DBG_ERR, "tlsverifycert: cn not matching host %s", peer->host);
474 if (peer->certuriregex) {
475 r = subjectaltnameregexp(cert, GEN_URI, NULL, peer->certuriregex);
477 debug(DBG_DBG, "tlsverifycert: subjectaltname URI not matching regex");
481 debug(DBG_DBG, "tlsverifycert: subjectaltname URI matching regex");
487 void tlsconnect(struct server *server, struct timeval *when, char *text) {
491 debug(DBG_DBG, "tlsconnect called from %s", text);
492 pthread_mutex_lock(&server->lock);
493 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
494 /* already reconnected, nothing to do */
495 debug(DBG_DBG, "tlsconnect(%s): seems already reconnected", text);
496 pthread_mutex_unlock(&server->lock);
500 debug(DBG_DBG, "tlsconnect %s", text);
503 gettimeofday(&now, NULL);
504 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
505 if (server->connectionok) {
506 server->connectionok = 0;
508 } else if (elapsed < 5)
510 else if (elapsed < 300) {
511 debug(DBG_INFO, "tlsconnect: sleeping %lds", elapsed);
513 } else if (elapsed < 100000) {
514 debug(DBG_INFO, "tlsconnect: sleeping %ds", 600);
517 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
518 debug(DBG_WARN, "tlsconnect: trying to open TLS connection to %s port %s", server->peer.host, server->peer.port);
519 if (server->sock >= 0)
521 if ((server->sock = connecttoserver(server->peer.addrinfo)) < 0) {
522 debug(DBG_ERR, "tlsconnect: connecttoserver failed");
526 SSL_free(server->peer.ssl);
527 server->peer.ssl = SSL_new(server->peer.ssl_ctx);
528 SSL_set_fd(server->peer.ssl, server->sock);
529 if (SSL_connect(server->peer.ssl) > 0 && tlsverifycert(&server->peer))
532 debug(DBG_WARN, "tlsconnect: TLS connection to %s port %s up", server->peer.host, server->peer.port);
533 gettimeofday(&server->lastconnecttry, NULL);
534 pthread_mutex_unlock(&server->lock);
537 unsigned char *radtlsget(SSL *ssl) {
539 unsigned char buf[4], *rad;
542 for (total = 0; total < 4; total += cnt) {
543 cnt = SSL_read(ssl, buf + total, 4 - total);
545 debug(DBG_ERR, "radtlsget: connection lost");
546 if (SSL_get_error(ssl, cnt) == SSL_ERROR_ZERO_RETURN) {
547 /* remote end sent close_notify, send one back */
557 debug(DBG_ERR, "radtlsget: malloc failed");
562 for (; total < len; total += cnt) {
563 cnt = SSL_read(ssl, rad + total, len - total);
565 debug(DBG_ERR, "radtlsget: connection lost");
566 if (SSL_get_error(ssl, cnt) == SSL_ERROR_ZERO_RETURN) {
567 /* remote end sent close_notify, send one back */
579 debug(DBG_WARN, "radtlsget: packet smaller than minimum radius size");
582 debug(DBG_DBG, "radtlsget: got %d bytes", total);
586 int clientradput(struct server *server, unsigned char *rad) {
590 struct timeval lastconnecttry;
593 if (server->peer.type == 'U') {
594 if (send(server->sock, rad, len, 0) >= 0) {
595 debug(DBG_DBG, "clienradput: sent UDP of length %d to %s port %s", len, server->peer.host, server->peer.port);
598 debug(DBG_WARN, "clientradput: send failed");
602 lastconnecttry = server->lastconnecttry;
603 while ((cnt = SSL_write(server->peer.ssl, rad, len)) <= 0) {
604 while ((error = ERR_get_error()))
605 debug(DBG_ERR, "clientradput: TLS: %s", ERR_error_string(error, NULL));
606 tlsconnect(server, &lastconnecttry, "clientradput");
607 lastconnecttry = server->lastconnecttry;
610 server->connectionok = 1;
611 debug(DBG_DBG, "clientradput: Sent %d bytes, Radius packet of length %d to TLS peer %s",
612 cnt, len, server->peer.host);
616 int radsign(unsigned char *rad, unsigned char *sec) {
617 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
618 static unsigned char first = 1;
619 static EVP_MD_CTX mdctx;
623 pthread_mutex_lock(&lock);
625 EVP_MD_CTX_init(&mdctx);
629 result = (EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) &&
630 EVP_DigestUpdate(&mdctx, rad, RADLEN(rad)) &&
631 EVP_DigestUpdate(&mdctx, sec, strlen((char *)sec)) &&
632 EVP_DigestFinal_ex(&mdctx, rad + 4, &md_len) &&
634 pthread_mutex_unlock(&lock);
638 int validauth(unsigned char *rad, unsigned char *reqauth, unsigned char *sec) {
639 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
640 static unsigned char first = 1;
641 static EVP_MD_CTX mdctx;
642 unsigned char hash[EVP_MAX_MD_SIZE];
646 pthread_mutex_lock(&lock);
648 EVP_MD_CTX_init(&mdctx);
654 result = (EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) &&
655 EVP_DigestUpdate(&mdctx, rad, 4) &&
656 EVP_DigestUpdate(&mdctx, reqauth, 16) &&
657 (len <= 20 || EVP_DigestUpdate(&mdctx, rad + 20, len - 20)) &&
658 EVP_DigestUpdate(&mdctx, sec, strlen((char *)sec)) &&
659 EVP_DigestFinal_ex(&mdctx, hash, &len) &&
661 !memcmp(hash, rad + 4, 16));
662 pthread_mutex_unlock(&lock);
666 int checkmessageauth(unsigned char *rad, uint8_t *authattr, char *secret) {
667 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
668 static unsigned char first = 1;
669 static HMAC_CTX hmacctx;
671 uint8_t auth[16], hash[EVP_MAX_MD_SIZE];
673 pthread_mutex_lock(&lock);
675 HMAC_CTX_init(&hmacctx);
679 memcpy(auth, authattr, 16);
680 memset(authattr, 0, 16);
682 HMAC_Init_ex(&hmacctx, secret, strlen(secret), EVP_md5(), NULL);
683 HMAC_Update(&hmacctx, rad, RADLEN(rad));
684 HMAC_Final(&hmacctx, hash, &md_len);
685 memcpy(authattr, auth, 16);
687 debug(DBG_WARN, "message auth computation failed");
688 pthread_mutex_unlock(&lock);
692 if (memcmp(auth, hash, 16)) {
693 debug(DBG_WARN, "message authenticator, wrong value");
694 pthread_mutex_unlock(&lock);
698 pthread_mutex_unlock(&lock);
702 int createmessageauth(unsigned char *rad, unsigned char *authattrval, char *secret) {
703 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
704 static unsigned char first = 1;
705 static HMAC_CTX hmacctx;
711 pthread_mutex_lock(&lock);
713 HMAC_CTX_init(&hmacctx);
717 memset(authattrval, 0, 16);
719 HMAC_Init_ex(&hmacctx, secret, strlen(secret), EVP_md5(), NULL);
720 HMAC_Update(&hmacctx, rad, RADLEN(rad));
721 HMAC_Final(&hmacctx, authattrval, &md_len);
723 debug(DBG_WARN, "message auth computation failed");
724 pthread_mutex_unlock(&lock);
728 pthread_mutex_unlock(&lock);
732 unsigned char *attrget(unsigned char *attrs, int length, uint8_t type) {
734 if (ATTRTYPE(attrs) == type)
736 length -= ATTRLEN(attrs);
737 attrs += ATTRLEN(attrs);
742 void sendrq(struct server *to, struct request *rq) {
746 pthread_mutex_lock(&to->newrq_mutex);
747 /* might simplify if only try nextid, might be ok */
748 for (i = to->nextid; i < MAX_REQUESTS; i++)
749 if (!to->requests[i].buf)
751 if (i == MAX_REQUESTS) {
752 for (i = 0; i < to->nextid; i++)
753 if (!to->requests[i].buf)
755 if (i == to->nextid) {
756 debug(DBG_WARN, "No room in queue, dropping request");
758 pthread_mutex_unlock(&to->newrq_mutex);
763 rq->buf[1] = (char)i;
765 attr = attrget(rq->buf + 20, RADLEN(rq->buf) - 20, RAD_Attr_Message_Authenticator);
766 if (attr && !createmessageauth(rq->buf, ATTRVAL(attr), to->peer.secret)) {
768 pthread_mutex_unlock(&to->newrq_mutex);
772 debug(DBG_DBG, "sendrq: inserting packet with id %d in queue for %s", i, to->peer.host);
773 to->requests[i] = *rq;
778 debug(DBG_DBG, "signalling client writer");
779 pthread_cond_signal(&to->newrq_cond);
781 pthread_mutex_unlock(&to->newrq_mutex);
784 void sendreply(struct client *to, unsigned char *buf, struct sockaddr_storage *tosa) {
785 struct replyq *replyq = to->replyq;
787 if (!radsign(buf, (unsigned char *)to->peer.secret)) {
789 debug(DBG_WARN, "sendreply: failed to sign message");
793 pthread_mutex_lock(&replyq->count_mutex);
794 if (replyq->count == replyq->size) {
795 debug(DBG_WARN, "No room in queue, dropping request");
796 pthread_mutex_unlock(&replyq->count_mutex);
801 replyq->replies[replyq->count].buf = buf;
803 replyq->replies[replyq->count].tosa = *tosa;
806 if (replyq->count == 1) {
807 debug(DBG_DBG, "signalling server writer");
808 pthread_cond_signal(&replyq->count_cond);
810 pthread_mutex_unlock(&replyq->count_mutex);
813 int pwdencrypt(uint8_t *in, uint8_t len, char *shared, uint8_t sharedlen, uint8_t *auth) {
814 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
815 static unsigned char first = 1;
816 static EVP_MD_CTX mdctx;
817 unsigned char hash[EVP_MAX_MD_SIZE], *input;
819 uint8_t i, offset = 0, out[128];
821 pthread_mutex_lock(&lock);
823 EVP_MD_CTX_init(&mdctx);
829 if (!EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) ||
830 !EVP_DigestUpdate(&mdctx, (uint8_t *)shared, sharedlen) ||
831 !EVP_DigestUpdate(&mdctx, input, 16) ||
832 !EVP_DigestFinal_ex(&mdctx, hash, &md_len) ||
834 pthread_mutex_unlock(&lock);
837 for (i = 0; i < 16; i++)
838 out[offset + i] = hash[i] ^ in[offset + i];
839 input = out + offset - 16;
844 memcpy(in, out, len);
845 pthread_mutex_unlock(&lock);
849 int pwddecrypt(uint8_t *in, uint8_t len, char *shared, uint8_t sharedlen, uint8_t *auth) {
850 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
851 static unsigned char first = 1;
852 static EVP_MD_CTX mdctx;
853 unsigned char hash[EVP_MAX_MD_SIZE], *input;
855 uint8_t i, offset = 0, out[128];
857 pthread_mutex_lock(&lock);
859 EVP_MD_CTX_init(&mdctx);
865 if (!EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) ||
866 !EVP_DigestUpdate(&mdctx, (uint8_t *)shared, sharedlen) ||
867 !EVP_DigestUpdate(&mdctx, input, 16) ||
868 !EVP_DigestFinal_ex(&mdctx, hash, &md_len) ||
870 pthread_mutex_unlock(&lock);
873 for (i = 0; i < 16; i++)
874 out[offset + i] = hash[i] ^ in[offset + i];
880 memcpy(in, out, len);
881 pthread_mutex_unlock(&lock);
885 int msmppencrypt(uint8_t *text, uint8_t len, uint8_t *shared, uint8_t sharedlen, uint8_t *auth, uint8_t *salt) {
886 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
887 static unsigned char first = 1;
888 static EVP_MD_CTX mdctx;
889 unsigned char hash[EVP_MAX_MD_SIZE];
893 pthread_mutex_lock(&lock);
895 EVP_MD_CTX_init(&mdctx);
900 printfchars(NULL, "msppencrypt auth in", "%02x ", auth, 16);
901 printfchars(NULL, "msppencrypt salt in", "%02x ", salt, 2);
902 printfchars(NULL, "msppencrypt in", "%02x ", text, len);
905 if (!EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) ||
906 !EVP_DigestUpdate(&mdctx, shared, sharedlen) ||
907 !EVP_DigestUpdate(&mdctx, auth, 16) ||
908 !EVP_DigestUpdate(&mdctx, salt, 2) ||
909 !EVP_DigestFinal_ex(&mdctx, hash, &md_len)) {
910 pthread_mutex_unlock(&lock);
915 printfchars(NULL, "msppencrypt hash", "%02x ", hash, 16);
918 for (i = 0; i < 16; i++)
921 for (offset = 16; offset < len; offset += 16) {
923 printf("text + offset - 16 c(%d): ", offset / 16);
924 printfchars(NULL, NULL, "%02x ", text + offset - 16, 16);
926 if (!EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) ||
927 !EVP_DigestUpdate(&mdctx, shared, sharedlen) ||
928 !EVP_DigestUpdate(&mdctx, text + offset - 16, 16) ||
929 !EVP_DigestFinal_ex(&mdctx, hash, &md_len) ||
931 pthread_mutex_unlock(&lock);
935 printfchars(NULL, "msppencrypt hash", "%02x ", hash, 16);
938 for (i = 0; i < 16; i++)
939 text[offset + i] ^= hash[i];
943 printfchars(NULL, "msppencrypt out", "%02x ", text, len);
946 pthread_mutex_unlock(&lock);
950 int msmppdecrypt(uint8_t *text, uint8_t len, uint8_t *shared, uint8_t sharedlen, uint8_t *auth, uint8_t *salt) {
951 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
952 static unsigned char first = 1;
953 static EVP_MD_CTX mdctx;
954 unsigned char hash[EVP_MAX_MD_SIZE];
959 pthread_mutex_lock(&lock);
961 EVP_MD_CTX_init(&mdctx);
966 printfchars(NULL, "msppdecrypt auth in", "%02x ", auth, 16);
967 printfchars(NULL, "msppdecrypt salt in", "%02x ", salt, 2);
968 printfchars(NULL, "msppdecrypt in", "%02x ", text, len);
971 if (!EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) ||
972 !EVP_DigestUpdate(&mdctx, shared, sharedlen) ||
973 !EVP_DigestUpdate(&mdctx, auth, 16) ||
974 !EVP_DigestUpdate(&mdctx, salt, 2) ||
975 !EVP_DigestFinal_ex(&mdctx, hash, &md_len)) {
976 pthread_mutex_unlock(&lock);
981 printfchars(NULL, "msppdecrypt hash", "%02x ", hash, 16);
984 for (i = 0; i < 16; i++)
985 plain[i] = text[i] ^ hash[i];
987 for (offset = 16; offset < len; offset += 16) {
989 printf("text + offset - 16 c(%d): ", offset / 16);
990 printfchars(NULL, "msppdecrypt hash", "%02x ", hash, 16);
992 if (!EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) ||
993 !EVP_DigestUpdate(&mdctx, shared, sharedlen) ||
994 !EVP_DigestUpdate(&mdctx, text + offset - 16, 16) ||
995 !EVP_DigestFinal_ex(&mdctx, hash, &md_len) ||
997 pthread_mutex_unlock(&lock);
1001 printfchars(NULL, "msppdecrypt hash", "%02x ", hash, 16);
1004 for (i = 0; i < 16; i++)
1005 plain[offset + i] = text[offset + i] ^ hash[i];
1008 memcpy(text, plain, len);
1010 printfchars(NULL, "msppdecrypt out", "%02x ", text, len);
1013 pthread_mutex_unlock(&lock);
1017 struct realm *id2realm(char *id, uint8_t len) {
1019 for (i = 0; i < realm_count; i++)
1020 if (!regexec(&realms[i].regex, id, 0, NULL, 0)) {
1021 debug(DBG_DBG, "found matching realm: %s", realms[i].name);
1027 int rqinqueue(struct server *to, struct client *from, uint8_t id) {
1030 pthread_mutex_lock(&to->newrq_mutex);
1031 for (i = 0; i < MAX_REQUESTS; i++)
1032 if (to->requests[i].buf && !to->requests[i].received && to->requests[i].origid == id && to->requests[i].from == from)
1034 pthread_mutex_unlock(&to->newrq_mutex);
1036 return i < MAX_REQUESTS;
1039 int attrvalidate(unsigned char *attrs, int length) {
1040 while (length > 1) {
1041 if (ATTRLEN(attrs) < 2) {
1042 debug(DBG_WARN, "attrvalidate: invalid attribute length %d", ATTRLEN(attrs));
1045 length -= ATTRLEN(attrs);
1047 debug(DBG_WARN, "attrvalidate: attribute length %d exceeds packet length", ATTRLEN(attrs));
1050 attrs += ATTRLEN(attrs);
1053 debug(DBG_WARN, "attrvalidate: malformed packet? remaining byte after last attribute");
1057 int pwdrecrypt(uint8_t *pwd, uint8_t len, char *oldsecret, char *newsecret, uint8_t *oldauth, uint8_t *newauth) {
1058 if (len < 16 || len > 128 || len % 16) {
1059 debug(DBG_WARN, "pwdrecrypt: invalid password length");
1063 if (!pwddecrypt(pwd, len, oldsecret, strlen(oldsecret), oldauth)) {
1064 debug(DBG_WARN, "pwdrecrypt: cannot decrypt password");
1068 printfchars(NULL, "pwdrecrypt: password", "%02x ", pwd, len);
1070 if (!pwdencrypt(pwd, len, newsecret, strlen(newsecret), newauth)) {
1071 debug(DBG_WARN, "pwdrecrypt: cannot encrypt password");
1077 int msmpprecrypt(uint8_t *msmpp, uint8_t len, char *oldsecret, char *newsecret, unsigned char *oldauth, char *newauth) {
1080 if (!msmppdecrypt(msmpp + 2, len - 2, (unsigned char *)oldsecret, strlen(oldsecret), oldauth, msmpp)) {
1081 debug(DBG_WARN, "msmpprecrypt: failed to decrypt msppe key");
1084 if (!msmppencrypt(msmpp + 2, len - 2, (unsigned char *)newsecret, strlen(newsecret), (unsigned char *)newauth, msmpp)) {
1085 debug(DBG_WARN, "msmpprecrypt: failed to encrypt msppe key");
1091 int msmppe(unsigned char *attrs, int length, uint8_t type, char *attrtxt, struct request *rq,
1092 char *oldsecret, char *newsecret) {
1093 unsigned char *attr;
1095 for (attr = attrs; (attr = attrget(attr, length - (attr - attrs), type)); attr += ATTRLEN(attr)) {
1096 debug(DBG_DBG, "msmppe: Got %s", attrtxt);
1097 if (!msmpprecrypt(ATTRVAL(attr), ATTRVALLEN(attr), oldsecret, newsecret, rq->buf + 4, rq->origauth))
1103 void respondstatusserver(struct request *rq) {
1104 unsigned char *resp;
1108 debug(DBG_ERR, "respondstatusserver: malloc failed");
1111 memcpy(resp, rq->buf, 20);
1112 resp[0] = RAD_Access_Accept;
1115 debug(DBG_DBG, "respondstatusserver: responding to %s", rq->from->peer.host);
1116 sendreply(rq->from, resp, rq->from->peer.type == 'U' ? &rq->fromsa : NULL);
1119 void respondreject(struct request *rq, char *message) {
1120 unsigned char *resp;
1124 len += 2 + strlen(message);
1128 debug(DBG_ERR, "respondreject: malloc failed");
1131 memcpy(resp, rq->buf, 20);
1132 resp[0] = RAD_Access_Reject;
1133 *(uint16_t *)(resp + 2) = htons(len);
1135 resp[20] = RAD_Attr_Reply_Message;
1136 resp[21] = len - 20;
1137 memcpy(resp + 22, message, len - 22);
1139 sendreply(rq->from, resp, rq->from->peer.type == 'U' ? &rq->fromsa : NULL);
1142 void radsrv(struct request *rq) {
1143 uint8_t code, id, *auth, *attrs, *attr;
1145 struct server *to = NULL;
1147 unsigned char *buf, newauth[16];
1148 struct realm *realm = NULL;
1151 code = *(uint8_t *)buf;
1152 id = *(uint8_t *)(buf + 1);
1154 auth = (uint8_t *)(buf + 4);
1156 debug(DBG_DBG, "radsrv: code %d, id %d, length %d", code, id, len);
1158 if (code != RAD_Access_Request && code != RAD_Status_Server) {
1159 debug(DBG_INFO, "radsrv: server currently accepts only access-requests and status-server, ignoring");
1167 if (!attrvalidate(attrs, len)) {
1168 debug(DBG_WARN, "radsrv: attribute validation failed, ignoring packet");
1173 if (code == RAD_Access_Request) {
1174 attr = attrget(attrs, len, RAD_Attr_User_Name);
1176 debug(DBG_WARN, "radsrv: ignoring request, no username attribute");
1180 memcpy(username, ATTRVAL(attr), ATTRVALLEN(attr));
1181 username[ATTRVALLEN(attr)] = '\0';
1182 debug(DBG_DBG, "Access Request with username: %s", username);
1184 realm = id2realm(username, strlen(username));
1186 debug(DBG_INFO, "radsrv: ignoring request, don't know where to send it");
1190 to = servers + realm->serverno;
1192 if (to && rqinqueue(to, rq->from, id)) {
1193 debug(DBG_INFO, "radsrv: already got request from host %s with id %d, ignoring", rq->from->peer.host, id);
1199 attr = attrget(attrs, len, RAD_Attr_Message_Authenticator);
1200 if (attr && (ATTRVALLEN(attr) != 16 || !checkmessageauth(buf, ATTRVAL(attr), rq->from->peer.secret))) {
1201 debug(DBG_WARN, "radsrv: message authentication failed");
1206 if (code == RAD_Status_Server) {
1207 respondstatusserver(rq);
1213 debug(DBG_INFO, "radsrv: sending reject to %s for %s", rq->from->peer.host, username);
1214 respondreject(rq, realm->message);
1219 if (!RAND_bytes(newauth, 16)) {
1220 debug(DBG_WARN, "radsrv: failed to generate random auth");
1226 printfchars(NULL, "auth", "%02x ", auth, 16);
1229 attr = attrget(attrs, len, RAD_Attr_User_Password);
1231 debug(DBG_DBG, "radsrv: found userpwdattr with value length %d", ATTRVALLEN(attr));
1232 if (!pwdrecrypt(ATTRVAL(attr), ATTRVALLEN(attr), rq->from->peer.secret, to->peer.secret, auth, newauth)) {
1238 attr = attrget(attrs, len, RAD_Attr_Tunnel_Password);
1240 debug(DBG_DBG, "radsrv: found tunnelpwdattr with value length %d", ATTRVALLEN(attr));
1241 if (!pwdrecrypt(ATTRVAL(attr), ATTRVALLEN(attr), rq->from->peer.secret, to->peer.secret, auth, newauth)) {
1248 memcpy(rq->origauth, auth, 16);
1249 memcpy(auth, newauth, 16);
1253 void *clientrd(void *arg) {
1254 struct server *server = (struct server *)arg;
1255 struct client *from;
1257 unsigned char *buf, *messageauth, *subattrs, *attrs, *attr;
1258 struct sockaddr_storage fromsa;
1259 struct timeval lastconnecttry;
1263 lastconnecttry = server->lastconnecttry;
1264 buf = (server->peer.type == 'U' ? radudpget(server->sock, NULL, &server, NULL) : radtlsget(server->peer.ssl));
1265 if (!buf && server->peer.type == 'T') {
1266 tlsconnect(server, &lastconnecttry, "clientrd");
1270 server->connectionok = 1;
1272 i = buf[1]; /* i is the id */
1275 case RAD_Access_Accept:
1276 debug(DBG_DBG, "got Access Accept with id %d", i);
1278 case RAD_Access_Reject:
1279 debug(DBG_DBG, "got Access Reject with id %d", i);
1281 case RAD_Access_Challenge:
1282 debug(DBG_DBG, "got Access Challenge with id %d", i);
1286 debug(DBG_INFO, "clientrd: discarding, only accept access accept, access reject and access challenge messages");
1290 pthread_mutex_lock(&server->newrq_mutex);
1291 if (!server->requests[i].buf || !server->requests[i].tries) {
1292 pthread_mutex_unlock(&server->newrq_mutex);
1294 debug(DBG_INFO, "clientrd: no matching request sent with this id, ignoring");
1298 if (server->requests[i].received) {
1299 pthread_mutex_unlock(&server->newrq_mutex);
1301 debug(DBG_INFO, "clientrd: already received, ignoring");
1305 if (!validauth(buf, server->requests[i].buf + 4, (unsigned char *)server->peer.secret)) {
1306 pthread_mutex_unlock(&server->newrq_mutex);
1308 debug(DBG_WARN, "clientrd: invalid auth, ignoring");
1312 from = server->requests[i].from;
1313 len = RADLEN(buf) - 20;
1316 if (!attrvalidate(attrs, len)) {
1317 pthread_mutex_unlock(&server->newrq_mutex);
1319 debug(DBG_WARN, "clientrd: attribute validation failed, ignoring packet");
1323 /* Message Authenticator */
1324 messageauth = attrget(attrs, len, RAD_Attr_Message_Authenticator);
1326 if (ATTRVALLEN(messageauth) != 16) {
1327 pthread_mutex_unlock(&server->newrq_mutex);
1329 debug(DBG_WARN, "clientrd: illegal message auth attribute length, ignoring packet");
1332 memcpy(tmp, buf + 4, 16);
1333 memcpy(buf + 4, server->requests[i].buf + 4, 16);
1334 if (!checkmessageauth(buf, ATTRVAL(messageauth), server->peer.secret)) {
1335 pthread_mutex_unlock(&server->newrq_mutex);
1337 debug(DBG_WARN, "clientrd: message authentication failed");
1340 memcpy(buf + 4, tmp, 16);
1341 debug(DBG_DBG, "clientrd: message auth ok");
1344 if (*server->requests[i].buf == RAD_Status_Server) {
1345 server->requests[i].received = 1;
1346 pthread_mutex_unlock(&server->newrq_mutex);
1348 debug(DBG_INFO, "clientrd: got status server response from %s", server->peer.host);
1353 for (attr = attrs; (attr = attrget(attr, len - (attr - attrs), RAD_Attr_Vendor_Specific)); attr += ATTRLEN(attr)) {
1354 if (ATTRVALLEN(attr) <= 4)
1357 if (attr[2] != 0 || attr[3] != 0 || attr[4] != 1 || attr[5] != 55) /* 311 == MS */
1360 sublen = ATTRVALLEN(attr) - 4;
1361 subattrs = ATTRVAL(attr) + 4;
1362 if (!attrvalidate(subattrs, sublen) ||
1363 !msmppe(subattrs, sublen, RAD_VS_ATTR_MS_MPPE_Send_Key, "MS MPPE Send Key",
1364 server->requests + i, server->peer.secret, from->peer.secret) ||
1365 !msmppe(subattrs, sublen, RAD_VS_ATTR_MS_MPPE_Recv_Key, "MS MPPE Recv Key",
1366 server->requests + i, server->peer.secret, from->peer.secret))
1370 pthread_mutex_unlock(&server->newrq_mutex);
1372 debug(DBG_WARN, "clientrd: MS attribute handling failed, ignoring packet");
1376 if (*buf == RAD_Access_Accept || *buf == RAD_Access_Reject) {
1377 attr = attrget(server->requests[i].buf + 20, RADLEN(server->requests[i].buf) - 20, RAD_Attr_User_Name);
1378 /* we know the attribute exists */
1379 memcpy(tmp, ATTRVAL(attr), ATTRVALLEN(attr));
1380 tmp[ATTRVALLEN(attr)] = '\0';
1382 case RAD_Access_Accept:
1383 debug(DBG_INFO, "Access Accept for %s from %s", tmp, server->peer.host);
1385 case RAD_Access_Reject:
1386 debug(DBG_INFO, "Access Reject for %s from %s", tmp, server->peer.host);
1391 /* once we set received = 1, requests[i] may be reused */
1392 buf[1] = (char)server->requests[i].origid;
1393 memcpy(buf + 4, server->requests[i].origauth, 16);
1395 printfchars(NULL, "origauth/buf+4", "%02x ", buf + 4, 16);
1399 if (!createmessageauth(buf, ATTRVAL(messageauth), from->peer.secret)) {
1400 pthread_mutex_unlock(&server->newrq_mutex);
1404 debug(DBG_DBG, "clientrd: computed messageauthattr");
1407 if (from->peer.type == 'U')
1408 fromsa = server->requests[i].fromsa;
1409 server->requests[i].received = 1;
1410 pthread_mutex_unlock(&server->newrq_mutex);
1412 debug(DBG_DBG, "clientrd: giving packet back to where it came from");
1413 sendreply(from, buf, from->peer.type == 'U' ? &fromsa : NULL);
1417 void *clientwr(void *arg) {
1418 struct server *server = (struct server *)arg;
1420 pthread_t clientrdth;
1423 struct timeval now, lastsend;
1424 struct timespec timeout;
1425 struct request statsrvrq;
1426 unsigned char statsrvbuf[38];
1428 memset(&timeout, 0, sizeof(struct timespec));
1430 if (server->statusserver) {
1431 memset(&statsrvrq, 0, sizeof(struct request));
1432 memset(statsrvbuf, 0, sizeof(statsrvbuf));
1433 statsrvbuf[0] = RAD_Status_Server;
1435 statsrvbuf[20] = RAD_Attr_Message_Authenticator;
1436 statsrvbuf[21] = 18;
1437 gettimeofday(&lastsend, NULL);
1440 if (server->peer.type == 'U') {
1441 if ((server->sock = connecttoserver(server->peer.addrinfo)) < 0)
1442 debugx(1, DBG_ERR, "clientwr: connecttoserver failed");
1444 tlsconnect(server, NULL, "new client");
1446 if (pthread_create(&clientrdth, NULL, clientrd, (void *)server))
1447 debugx(1, DBG_ERR, "clientwr: pthread_create failed");
1450 pthread_mutex_lock(&server->newrq_mutex);
1451 if (!server->newrq) {
1452 gettimeofday(&now, NULL);
1453 if (server->statusserver) {
1454 /* random 0-7 seconds */
1455 RAND_bytes(&rnd, 1);
1457 if (!timeout.tv_sec || timeout.tv_sec - now.tv_sec > lastsend.tv_sec + STATUS_SERVER_PERIOD + rnd)
1458 timeout.tv_sec = lastsend.tv_sec + STATUS_SERVER_PERIOD + rnd;
1460 if (timeout.tv_sec) {
1461 debug(DBG_DBG, "clientwr: waiting up to %ld secs for new request", timeout.tv_sec - now.tv_sec);
1462 pthread_cond_timedwait(&server->newrq_cond, &server->newrq_mutex, &timeout);
1465 debug(DBG_DBG, "clientwr: waiting for new request");
1466 pthread_cond_wait(&server->newrq_cond, &server->newrq_mutex);
1469 if (server->newrq) {
1470 debug(DBG_DBG, "clientwr: got new request");
1473 debug(DBG_DBG, "clientwr: request timer expired, processing request queue");
1474 pthread_mutex_unlock(&server->newrq_mutex);
1476 for (i = 0; i < MAX_REQUESTS; i++) {
1477 pthread_mutex_lock(&server->newrq_mutex);
1478 while (i < MAX_REQUESTS && !server->requests[i].buf)
1480 if (i == MAX_REQUESTS) {
1481 pthread_mutex_unlock(&server->newrq_mutex);
1484 rq = server->requests + i;
1487 debug(DBG_DBG, "clientwr: packet %d in queue is marked as received", i);
1489 debug(DBG_DBG, "clientwr: freeing received packet %d from queue", i);
1491 /* setting this to NULL means that it can be reused */
1494 pthread_mutex_unlock(&server->newrq_mutex);
1498 gettimeofday(&now, NULL);
1499 if (now.tv_sec < rq->expiry.tv_sec) {
1500 if (!timeout.tv_sec || rq->expiry.tv_sec < timeout.tv_sec)
1501 timeout.tv_sec = rq->expiry.tv_sec;
1502 pthread_mutex_unlock(&server->newrq_mutex);
1506 if (rq->tries == (*rq->buf == RAD_Status_Server || server->peer.type == 'T'
1507 ? 1 : REQUEST_RETRIES)) {
1508 debug(DBG_DBG, "clientwr: removing expired packet from queue");
1509 if (*rq->buf == RAD_Status_Server)
1510 debug(DBG_WARN, "clientwr: no status server response, %s dead?", server->peer.host);
1512 /* setting this to NULL means that it can be reused */
1514 pthread_mutex_unlock(&server->newrq_mutex);
1517 pthread_mutex_unlock(&server->newrq_mutex);
1519 rq->expiry.tv_sec = now.tv_sec +
1520 (*rq->buf == RAD_Status_Server || server->peer.type == 'T'
1521 ? REQUEST_EXPIRY : REQUEST_EXPIRY / REQUEST_RETRIES);
1522 if (!timeout.tv_sec || rq->expiry.tv_sec < timeout.tv_sec)
1523 timeout.tv_sec = rq->expiry.tv_sec;
1525 clientradput(server, server->requests[i].buf);
1526 gettimeofday(&lastsend, NULL);
1528 if (server->statusserver) {
1529 gettimeofday(&now, NULL);
1530 if (now.tv_sec - lastsend.tv_sec >= STATUS_SERVER_PERIOD) {
1531 if (!RAND_bytes(statsrvbuf + 4, 16)) {
1532 debug(DBG_WARN, "clientwr: failed to generate random auth");
1535 statsrvrq.buf = malloc(sizeof(statsrvbuf));
1536 if (!statsrvrq.buf) {
1537 debug(DBG_ERR, "clientwr: malloc failed");
1540 memcpy(statsrvrq.buf, statsrvbuf, sizeof(statsrvbuf));
1541 debug(DBG_DBG, "clientwr: sending status server to %s", server->peer.host);
1542 lastsend.tv_sec = now.tv_sec;
1543 sendrq(server, &statsrvrq);
1549 void *udpserverwr(void *arg) {
1550 struct replyq *replyq = &udp_server_replyq;
1551 struct reply *reply = replyq->replies;
1553 pthread_mutex_lock(&replyq->count_mutex);
1555 while (!replyq->count) {
1556 debug(DBG_DBG, "udp server writer, waiting for signal");
1557 pthread_cond_wait(&replyq->count_cond, &replyq->count_mutex);
1558 debug(DBG_DBG, "udp server writer, got signal");
1560 pthread_mutex_unlock(&replyq->count_mutex);
1562 if (sendto(udp_server_sock, reply->buf, RADLEN(reply->buf), 0,
1563 (struct sockaddr *)&reply->tosa, SOCKADDR_SIZE(reply->tosa)) < 0)
1564 debug(DBG_WARN, "sendudp: send failed");
1567 pthread_mutex_lock(&replyq->count_mutex);
1569 memmove(replyq->replies, replyq->replies + 1,
1570 replyq->count * sizeof(struct reply));
1574 void *udpserverrd(void *arg) {
1576 pthread_t udpserverwrth;
1578 if ((udp_server_sock = bindtoaddr(udp_server_listen->addrinfo)) < 0)
1579 debugx(1, DBG_ERR, "udpserverrd: socket/bind failed");
1581 debug(DBG_WARN, "udpserverrd: listening for UDP on %s:%s",
1582 udp_server_listen->host ? udp_server_listen->host : "*", udp_server_listen->port);
1584 if (pthread_create(&udpserverwrth, NULL, udpserverwr, NULL))
1585 debugx(1, DBG_ERR, "pthread_create failed");
1588 memset(&rq, 0, sizeof(struct request));
1589 rq.buf = radudpget(udp_server_sock, &rq.from, NULL, &rq.fromsa);
1594 void *tlsserverwr(void *arg) {
1596 unsigned long error;
1597 struct client *client = (struct client *)arg;
1598 struct replyq *replyq;
1600 debug(DBG_DBG, "tlsserverwr starting for %s", client->peer.host);
1601 replyq = client->replyq;
1602 pthread_mutex_lock(&replyq->count_mutex);
1604 while (!replyq->count) {
1605 if (client->peer.ssl) {
1606 debug(DBG_DBG, "tls server writer, waiting for signal");
1607 pthread_cond_wait(&replyq->count_cond, &replyq->count_mutex);
1608 debug(DBG_DBG, "tls server writer, got signal");
1610 if (!client->peer.ssl) {
1611 /* ssl might have changed while waiting */
1612 pthread_mutex_unlock(&replyq->count_mutex);
1613 debug(DBG_DBG, "tlsserverwr: exiting as requested");
1617 pthread_mutex_unlock(&replyq->count_mutex);
1618 cnt = SSL_write(client->peer.ssl, replyq->replies->buf, RADLEN(replyq->replies->buf));
1620 debug(DBG_DBG, "tlsserverwr: Sent %d bytes, Radius packet of length %d",
1621 cnt, RADLEN(replyq->replies->buf));
1623 while ((error = ERR_get_error()))
1624 debug(DBG_ERR, "tlsserverwr: SSL: %s", ERR_error_string(error, NULL));
1625 free(replyq->replies->buf);
1627 pthread_mutex_lock(&replyq->count_mutex);
1629 memmove(replyq->replies, replyq->replies + 1, replyq->count * sizeof(struct reply));
1633 void *tlsserverrd(void *arg) {
1635 unsigned long error;
1637 struct client *client = (struct client *)arg;
1638 pthread_t tlsserverwrth;
1641 debug(DBG_DBG, "tlsserverrd starting for %s", client->peer.host);
1642 ssl = client->peer.ssl;
1644 if (SSL_accept(ssl) <= 0) {
1645 while ((error = ERR_get_error()))
1646 debug(DBG_ERR, "tlsserverrd: SSL: %s", ERR_error_string(error, NULL));
1647 debug(DBG_ERR, "SSL_accept failed");
1650 if (tlsverifycert(&client->peer)) {
1651 if (pthread_create(&tlsserverwrth, NULL, tlsserverwr, (void *)client)) {
1652 debug(DBG_ERR, "tlsserverrd: pthread_create failed");
1656 memset(&rq, 0, sizeof(struct request));
1657 rq.buf = radtlsget(client->peer.ssl);
1660 debug(DBG_DBG, "tlsserverrd: got Radius message from %s", client->peer.host);
1664 debug(DBG_ERR, "tlsserverrd: connection lost");
1665 /* stop writer by setting peer.ssl to NULL and give signal in case waiting for data */
1666 client->peer.ssl = NULL;
1667 pthread_mutex_lock(&client->replyq->count_mutex);
1668 pthread_cond_signal(&client->replyq->count_cond);
1669 pthread_mutex_unlock(&client->replyq->count_mutex);
1670 debug(DBG_DBG, "tlsserverrd: waiting for writer to end");
1671 pthread_join(tlsserverwrth, NULL);
1675 s = SSL_get_fd(ssl);
1677 shutdown(s, SHUT_RDWR);
1679 debug(DBG_DBG, "tlsserverrd thread for %s exiting", client->peer.host);
1680 client->peer.ssl = NULL;
1685 pthread_t tlsserverth;
1687 struct sockaddr_storage from;
1688 size_t fromlen = sizeof(from);
1689 struct client *client;
1691 if ((s = bindtoaddr(tcp_server_listen->addrinfo)) < 0)
1692 debugx(1, DBG_ERR, "tlslistener: socket/bind failed");
1695 debug(DBG_WARN, "listening for incoming TCP on %s:%s",
1696 tcp_server_listen->host ? tcp_server_listen->host : "*", tcp_server_listen->port);
1699 snew = accept(s, (struct sockaddr *)&from, &fromlen);
1701 debug(DBG_WARN, "accept failed");
1704 debug(DBG_WARN, "incoming TLS connection from %s", addr2string((struct sockaddr *)&from, fromlen));
1706 client = find_client('T', (struct sockaddr *)&from, NULL);
1708 debug(DBG_WARN, "ignoring request, not a known TLS client");
1709 shutdown(snew, SHUT_RDWR);
1714 if (client->peer.ssl) {
1715 debug(DBG_WARN, "Ignoring incoming TLS connection, already have one from this client");
1716 shutdown(snew, SHUT_RDWR);
1720 client->peer.ssl = SSL_new(client->peer.ssl_ctx);
1721 SSL_set_fd(client->peer.ssl, snew);
1722 if (pthread_create(&tlsserverth, NULL, tlsserverrd, (void *)client)) {
1723 debug(DBG_ERR, "tlslistener: pthread_create failed");
1724 SSL_free(client->peer.ssl);
1725 shutdown(snew, SHUT_RDWR);
1727 client->peer.ssl = NULL;
1730 pthread_detach(tlsserverth);
1735 void tlsadd(char *value, char *cacertfile, char *cacertpath, char *certfile, char *certkeyfile, char *certkeypwd) {
1739 unsigned long error;
1741 if (!certfile || !certkeyfile)
1742 debugx(1, DBG_ERR, "TLSCertificateFile and TLSCertificateKeyFile must be specified in TLS context %s", value);
1744 if (!cacertfile && !cacertpath)
1745 debugx(1, DBG_ERR, "CA Certificate file or path need to be specified in TLS context %s", value);
1748 ssl_locks = malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
1749 ssl_lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
1750 for (i = 0; i < CRYPTO_num_locks(); i++) {
1751 ssl_lock_count[i] = 0;
1752 pthread_mutex_init(&ssl_locks[i], NULL);
1754 CRYPTO_set_id_callback(ssl_thread_id);
1755 CRYPTO_set_locking_callback(ssl_locking_callback);
1757 SSL_load_error_strings();
1760 while (!RAND_status()) {
1761 time_t t = time(NULL);
1762 pid_t pid = getpid();
1763 RAND_seed((unsigned char *)&t, sizeof(time_t));
1764 RAND_seed((unsigned char *)&pid, sizeof(pid));
1767 ctx = SSL_CTX_new(TLSv1_method());
1769 SSL_CTX_set_default_passwd_cb_userdata(ctx, certkeypwd);
1770 SSL_CTX_set_default_passwd_cb(ctx, pem_passwd_cb);
1772 if (!SSL_CTX_use_certificate_chain_file(ctx, certfile) ||
1773 !SSL_CTX_use_PrivateKey_file(ctx, certkeyfile, SSL_FILETYPE_PEM) ||
1774 !SSL_CTX_check_private_key(ctx) ||
1775 !SSL_CTX_load_verify_locations(ctx, cacertfile, cacertpath)) {
1776 while ((error = ERR_get_error()))
1777 debug(DBG_ERR, "SSL: %s", ERR_error_string(error, NULL));
1778 debugx(1, DBG_ERR, "Error initialising SSL/TLS in TLS context %s", value);
1781 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, verify_cb);
1782 SSL_CTX_set_verify_depth(ctx, MAX_CERT_DEPTH + 1);
1785 tlsconfs = realloc(tlsconfs, tls_count * sizeof(struct tls));
1787 debugx(1, DBG_ERR, "malloc failed");
1788 new = tlsconfs + tls_count - 1;
1789 memset(new, 0, sizeof(struct tls));
1790 new->name = stringcopy(value, 0);
1792 debugx(1, DBG_ERR, "malloc failed");
1795 debug(DBG_DBG, "tlsadd: added TLS context %s", value);
1800 for (i = 0; i < tls_count; i++) {
1801 free(tlsconfs[i].name);
1802 if (!tlsconfs[i].count)
1803 SSL_CTX_free(tlsconfs[i].ctx);
1810 SSL_CTX *tlsgetctx(char *alt1, char *alt2) {
1811 int i, c1 = -1, c2 = -1;
1812 for (i = 0; i < tls_count; i++) {
1813 if (!strcasecmp(tlsconfs[i].name, alt1)) {
1817 if (c2 == -1 && alt2 && !strcasecmp(tlsconfs[i].name, alt2))
1821 i = (c1 == -1 ? c2 : c1);
1824 tlsconfs[i].count++;
1825 return tlsconfs[i].ctx;
1828 void addrealm(char *value, char *server, char *message) {
1830 struct realm *realm;
1831 char *s, *regex = NULL;
1834 for (i = 0; i < server_count; i++)
1835 if (!strcasecmp(server, servers[i].peer.host))
1837 if (i == server_count)
1838 debugx(1, DBG_ERR, "addrealm failed, no server %s", server);
1841 if (*value == '/') {
1842 /* regexp, remove optional trailing / if present */
1843 if (value[strlen(value) - 1] == '/')
1844 value[strlen(value) - 1] = '\0';
1846 /* not a regexp, let us make it one */
1847 if (*value == '*' && !value[1])
1848 regex = stringcopy(".*", 0);
1850 for (n = 0, s = value; *s;)
1853 regex = malloc(strlen(value) + n + 3);
1856 for (n = 1, s = value; *s; s++) {
1866 debugx(1, DBG_ERR, "malloc failed");
1867 debug(DBG_DBG, "addrealm: constructed regexp %s from %s", regex, value);
1871 realms = realloc(realms, realm_count * sizeof(struct realm));
1873 debugx(1, DBG_ERR, "malloc failed");
1874 realm = realms + realm_count - 1;
1875 memset(realm, 0, sizeof(struct realm));
1876 realm->name = stringcopy(value, 0);
1878 debugx(1, DBG_ERR, "malloc failed");
1879 if (message && strlen(message) > 253)
1880 debugx(1, DBG_ERR, "ReplyMessage can be at most 253 bytes");
1881 realm->message = message;
1883 realm->serverno = i;
1884 if (regcomp(&realm->regex, regex ? regex : value + 1, REG_ICASE | REG_NOSUB))
1885 debugx(1, DBG_ERR, "addrealm: failed to compile regular expression %s", regex ? regex : value + 1);
1888 debug(DBG_DBG, "addrealm: added realm %s for server %s", value, server);
1891 char *parsehostport(char *s, struct peer *peer) {
1896 /* allow literal addresses and port, e.g. [2001:db8::1]:1812 */
1900 for (; *p && *p != ']' && *p != ' ' && *p != '\t' && *p != '\n'; p++);
1902 debugx(1, DBG_ERR, "no ] matching initial [");
1906 for (; *p && *p != ':' && *p != ' ' && *p != '\t' && *p != '\n'; p++);
1909 debugx(1, DBG_ERR, "missing host/address");
1911 peer->host = stringcopy(field, p - field);
1914 if (*p && *p != ':' && *p != ' ' && *p != '\t' && *p != '\n')
1915 debugx(1, DBG_ERR, "unexpected character after ]");
1918 /* port number or service name is specified */;
1920 for (; *p && *p != ' ' && *p != '\t' && *p != '\n'; p++);
1922 debugx(1, DBG_ERR, "syntax error, : but no following port");
1923 peer->port = stringcopy(field, p - field);
1925 peer->port = stringcopy(peer->type == 'U' ? DEFAULT_UDP_PORT : DEFAULT_TLS_PORT, 0);
1929 FILE *openconfigfile(const char *filename) {
1931 char pathname[100], *base = NULL;
1933 f = fopen(filename, "r");
1935 debug(DBG_DBG, "reading config file %s", filename);
1939 if (strlen(filename) + 1 <= sizeof(pathname)) {
1940 /* basename() might modify the string */
1941 strcpy(pathname, filename);
1942 base = basename(pathname);
1943 f = fopen(base, "r");
1947 debugx(1, DBG_ERR, "could not read config file %s nor %s\n%s", filename, base, strerror(errno));
1949 debug(DBG_DBG, "reading config file %s", base);
1953 struct peer *server_create(char type) {
1954 struct peer *server;
1957 server = malloc(sizeof(struct peer));
1959 debugx(1, DBG_ERR, "malloc failed");
1960 memset(server, 0, sizeof(struct peer));
1961 server->type = type;
1962 conf = (type == 'T' ? options.listentcp : options.listenudp);
1964 parsehostport(conf, server);
1965 if (!strcmp(server->host, "*")) {
1967 server->host = NULL;
1970 server->port = stringcopy(type == 'T' ? DEFAULT_TLS_PORT : DEFAULT_UDP_PORT, 0);
1971 if (!resolvepeer(server, AI_PASSIVE))
1972 debugx(1, DBG_ERR, "failed to resolve host %s port %s, exiting", server->host, server->port);
1976 /* returns NULL on error, where to continue parsing if token and ok. E.g. "" will return token with empty string */
1977 char *strtokenquote(char *s, char **token, char *del, char *quote, char *comment) {
1978 char *t = s, *q, *r;
1980 if (!t || !token || !del)
1982 while (*t && strchr(del, *t))
1984 if (!*t || (comment && strchr(comment, *t))) {
1986 return t + 1; /* needs to be non-NULL, but value doesn't matter */
1988 if (quote && (q = strchr(quote, *t))) {
1991 while (*t && *t != *q)
1993 if (!*t || (t[1] && !strchr(del, t[1])))
2001 while (*t && !strchr(del, *t))
2007 /* Parses config with following syntax:
2010 * option-name = value
2012 * option-name value {
2013 * option-name [=] value
2017 void getgeneralconfig(FILE *f, char *block, ...) {
2020 /* initialise lots of stuff to avoid stupid compiler warnings */
2021 char *tokens[3], *s, *opt = NULL, *val = NULL, *word, *optval, **str = NULL;
2022 int type = 0, tcount, conftype = 0;
2023 void (*cbk)(FILE *, char *, char *, char *) = NULL;
2025 while (fgets(line, 1024, f)) {
2027 for (tcount = 0; tcount < 3; tcount++) {
2028 s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
2030 debugx(1, DBG_ERR, "Syntax error in line starting with: %s", line);
2031 if (!tokens[tcount])
2034 if (!tcount || **tokens == '#')
2037 if (**tokens == '}') {
2040 debugx(1, DBG_ERR, "configuration error, found } with no matching {");
2047 conftype = CONF_STR;
2050 if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
2053 conftype = CONF_STR;
2056 if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
2059 conftype = CONF_CBK;
2065 debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
2066 debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
2070 debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", opt);
2072 va_start(ap, block);
2073 while ((word = va_arg(ap, char *))) {
2074 type = va_arg(ap, int);
2077 str = va_arg(ap, char **);
2079 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
2082 cbk = va_arg(ap, void (*)(FILE *, char *, char *, char *));
2085 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
2087 if (!strcasecmp(opt, word))
2094 debugx(1, DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
2095 debugx(1, DBG_ERR, "configuration error, unknown option %s", opt);
2098 if (type != conftype) {
2100 debugx(1, DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
2101 debugx(1, DBG_ERR, "configuration error, wrong syntax for option %s", opt);
2107 debug(DBG_DBG, "getgeneralconfig: block %s: %s = %s", block, opt, val);
2109 debug(DBG_DBG, "getgeneralconfig: %s = %s", opt, val);
2111 debugx(1, DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
2112 *str = stringcopy(val, 0);
2115 optval = malloc(strlen(opt) + strlen(val) + 2);
2117 debugx(1, DBG_ERR, "malloc failed");
2118 sprintf(optval, "%s %s", opt, val);
2119 cbk(f, optval, opt, val);
2123 debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
2128 int addmatchcertattr(struct peer *conf, char *matchcertattr) {
2131 v = matchcertattr + 20;
2132 if (strncasecmp(matchcertattr, "SubjectAltName:URI:/", 20) || !*v)
2134 /* regexp, remove optional trailing / if present */
2135 if (v[strlen(v) - 1] == '/')
2136 v[strlen(v) - 1] = '\0';
2140 conf->certuriregex = malloc(sizeof(regex_t));
2141 if (!conf->certuriregex) {
2142 debug(DBG_ERR, "malloc failed");
2145 if (regcomp(conf->certuriregex, v, REG_ICASE | REG_NOSUB)) {
2146 free(conf->certuriregex);
2147 conf->certuriregex = NULL;
2148 debug(DBG_ERR, "failed to compile regular expression %s", v);
2154 void confclsrv_cb(FILE *f, char *block, char *opt, char *val) {
2155 char *type = NULL, *secret = NULL, *port = NULL, *tls = NULL, *matchcertattr = NULL, *statusserver = NULL;
2156 struct client *client = NULL;
2157 struct server *server = NULL;
2160 debug(DBG_DBG, "confclsrv_cb called for %s", block);
2162 if (!strcasecmp(opt, "client")) {
2163 getgeneralconfig(f, block,
2164 "type", CONF_STR, &type,
2165 "secret", CONF_STR, &secret,
2166 "tls", CONF_STR, &tls,
2167 "matchcertificateattribute", CONF_STR, &matchcertattr,
2171 clients = realloc(clients, client_count * sizeof(struct client));
2173 debugx(1, DBG_ERR, "malloc failed");
2174 client = clients + client_count - 1;
2175 memset(client, 0, sizeof(struct client));
2176 peer = &client->peer;
2178 getgeneralconfig(f, block,
2179 "type", CONF_STR, &type,
2180 "secret", CONF_STR, &secret,
2181 "port", CONF_STR, &port,
2182 "tls", CONF_STR, &tls,
2183 "matchcertificateattribute", CONF_STR, &matchcertattr,
2184 "StatusServer", CONF_STR, &statusserver,
2188 servers = realloc(servers, server_count * sizeof(struct server));
2190 debugx(1, DBG_ERR, "malloc failed");
2191 server = servers + server_count - 1;
2192 memset(server, 0, sizeof(struct server));
2193 peer = &server->peer;
2196 if (!strcasecmp(statusserver, "on"))
2197 server->statusserver = 1;
2198 else if (strcasecmp(statusserver, "off"))
2199 debugx(1, DBG_ERR, "error in block %s, StatusServer is %s, must be on or off", block, statusserver);
2204 peer->host = stringcopy(val, 0);
2206 if (type && !strcasecmp(type, "udp")) {
2213 peer->port = stringcopy(DEFAULT_UDP_PORT, 0);
2215 } else if (type && !strcasecmp(type, "tls")) {
2217 peer->ssl_ctx = tls ? tlsgetctx(tls, NULL) : tlsgetctx("defaultclient", "default");
2220 peer->ssl_ctx = tls ? tlsgetctx(tls, NULL) : tlsgetctx("defaultserver", "default");
2223 peer->port = stringcopy(DEFAULT_TLS_PORT, 0);
2226 debugx(1, DBG_ERR, "error in block %s, no tls context defined", block);
2227 if (matchcertattr && !addmatchcertattr(peer, matchcertattr))
2228 debugx(1, DBG_ERR, "error in block %s, invalid MatchCertificateAttributeValue", block);
2231 debugx(1, DBG_ERR, "error in block %s, type must be set to UDP or TLS", block);
2236 free(matchcertattr);
2238 if (!resolvepeer(peer, 0))
2239 debugx(1, DBG_ERR, "failed to resolve host %s port %s, exiting", peer->host, peer->port);
2242 if (peer->type == 'U')
2243 debugx(1, DBG_ERR, "error in block %s, secret must be specified for UDP", block);
2244 peer->secret = stringcopy(DEFAULT_TLS_SECRET, 0);
2246 peer->secret = secret;
2250 if (peer->type == 'U')
2251 client->replyq = &udp_server_replyq;
2253 client->replyq = malloc(sizeof(struct replyq));
2254 if (!client->replyq)
2255 debugx(1, DBG_ERR, "malloc failed");
2256 client->replyq->replies = calloc(MAX_REQUESTS, sizeof(struct reply));
2257 if (!client->replyq->replies)
2258 debugx(1, DBG_ERR, "malloc failed");
2259 client->replyq->size = MAX_REQUESTS;
2260 client->replyq->count = 0;
2261 pthread_mutex_init(&client->replyq->count_mutex, NULL);
2262 pthread_cond_init(&client->replyq->count_cond, NULL);
2265 pthread_mutex_init(&server->lock, NULL);
2267 server->requests = calloc(MAX_REQUESTS, sizeof(struct request));
2268 if (!server->requests)
2269 debugx(1, DBG_ERR, "malloc failed");
2271 pthread_mutex_init(&server->newrq_mutex, NULL);
2272 pthread_cond_init(&server->newrq_cond, NULL);
2276 void confrealm_cb(FILE *f, char *block, char *opt, char *val) {
2277 char *server = NULL, *msg = NULL;
2279 debug(DBG_DBG, "confrealm_cb called for %s", block);
2281 getgeneralconfig(f, block,
2282 "server", CONF_STR, &server,
2283 "ReplyMessage", CONF_STR, &msg,
2287 addrealm(val, server, msg);
2291 void conftls_cb(FILE *f, char *block, char *opt, char *val) {
2292 char *cacertfile = NULL, *cacertpath = NULL, *certfile = NULL, *certkeyfile = NULL, *certkeypwd = NULL;
2294 debug(DBG_DBG, "conftls_cb called for %s", block);
2296 getgeneralconfig(f, block,
2297 "CACertificateFile", CONF_STR, &cacertfile,
2298 "CACertificatePath", CONF_STR, &cacertpath,
2299 "CertificateFile", CONF_STR, &certfile,
2300 "CertificateKeyFile", CONF_STR, &certkeyfile,
2301 "CertificateKeyPassword", CONF_STR, &certkeypwd,
2305 tlsadd(val, cacertfile, cacertpath, certfile, certkeyfile, certkeypwd);
2313 void getmainconfig(const char *configfile) {
2315 char *loglevel = NULL;
2317 f = openconfigfile(configfile);
2318 memset(&options, 0, sizeof(options));
2320 getgeneralconfig(f, NULL,
2321 "ListenUDP", CONF_STR, &options.listenudp,
2322 "ListenTCP", CONF_STR, &options.listentcp,
2323 "LogLevel", CONF_STR, &loglevel,
2324 "LogDestination", CONF_STR, &options.logdestination,
2325 "Client", CONF_CBK, confclsrv_cb,
2326 "Server", CONF_CBK, confclsrv_cb,
2327 "Realm", CONF_CBK, confrealm_cb,
2328 "TLS", CONF_CBK, conftls_cb,
2335 if (strlen(loglevel) != 1 || *loglevel < '1' || *loglevel > '4')
2336 debugx(1, DBG_ERR, "error in %s, value of option LogLevel is %s, must be 1, 2, 3 or 4", configfile, loglevel);
2337 options.loglevel = *loglevel - '0';
2341 if (client_udp_count) {
2342 udp_server_replyq.replies = malloc(client_udp_count * MAX_REQUESTS * sizeof(struct reply));
2343 if (!udp_server_replyq.replies)
2344 debugx(1, DBG_ERR, "malloc failed");
2345 udp_server_replyq.size = client_udp_count * MAX_REQUESTS;
2346 udp_server_replyq.count = 0;
2347 pthread_mutex_init(&udp_server_replyq.count_mutex, NULL);
2348 pthread_cond_init(&udp_server_replyq.count_cond, NULL);
2352 void getargs(int argc, char **argv, uint8_t *foreground, uint8_t *loglevel, char **configfile) {
2355 while ((c = getopt(argc, argv, "c:d:fv")) != -1) {
2358 *configfile = optarg;
2361 if (strlen(optarg) != 1 || *optarg < '1' || *optarg > '4')
2362 debugx(1, DBG_ERR, "Debug level must be 1, 2, 3 or 4, not %s", optarg);
2363 *loglevel = *optarg - '0';
2369 debugx(0, DBG_ERR, "radsecproxy 1.0p2");
2374 if (!(argc - optind))
2378 debug(DBG_ERR, "Usage:\n%s [ -c configfile ] [ -d debuglevel ] [ -f ] [ -v ]", argv[0]);
2382 void *sighandler(void *arg) {
2387 sigemptyset(&sigset);
2388 sigaddset(&sigset, SIGPIPE);
2389 sigwait(&sigset, &sig);
2390 /* only get SIGPIPE right now, so could simplify below code */
2393 /* completely ignoring this */
2396 debug(DBG_WARN, "sighandler: got SIGPIPE, TLS write error?");
2399 debug(DBG_WARN, "sighandler: ignoring signal %d", sig);
2404 int main(int argc, char **argv) {
2405 pthread_t sigth, udpserverth;
2408 uint8_t foreground = 0, loglevel = 0;
2409 char *configfile = NULL;
2411 debug_init("radsecproxy");
2412 debug_set_level(DEBUG_LEVEL);
2413 getargs(argc, argv, &foreground, &loglevel, &configfile);
2415 debug_set_level(loglevel);
2416 getmainconfig(configfile ? configfile : CONFIG_MAIN);
2418 options.loglevel = loglevel;
2419 else if (options.loglevel)
2420 debug_set_level(options.loglevel);
2422 options.logdestination = NULL;
2424 if (!options.logdestination)
2425 options.logdestination = "x-syslog:///";
2426 debug_set_destination(options.logdestination);
2430 debugx(1, DBG_ERR, "No servers configured, nothing to do, exiting");
2432 debugx(1, DBG_ERR, "No clients configured, nothing to do, exiting");
2434 debugx(1, DBG_ERR, "No realms configured, nothing to do, exiting");
2436 if (!foreground && (daemon(0, 0) < 0))
2437 debugx(1, DBG_ERR, "daemon() failed: %s", strerror(errno));
2439 debug(DBG_INFO, "radsecproxy 1.0p2 starting");
2441 sigemptyset(&sigset);
2442 /* exit on all but SIGPIPE, ignore more? */
2443 sigaddset(&sigset, SIGPIPE);
2444 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2445 pthread_create(&sigth, NULL, sighandler, NULL);
2447 if (client_udp_count) {
2448 udp_server_listen = server_create('U');
2449 if (pthread_create(&udpserverth, NULL, udpserverrd, NULL))
2450 debugx(1, DBG_ERR, "pthread_create failed");
2453 for (i = 0; i < server_count; i++)
2454 if (pthread_create(&servers[i].clientth, NULL, clientwr, (void *)&servers[i]))
2455 debugx(1, DBG_ERR, "pthread_create failed");
2457 if (client_tls_count) {
2458 tcp_server_listen = server_create('T');
2459 return tlslistener();
2462 /* just hang around doing nothing, anything to do here? */