2 * Copyright (C) 2006 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.
10 * peers can not yet be specified with literal IPv6 addresses due to port syntax
14 * make our server ignore client retrans and do its own instead?
16 * radius keep alives (server status)
17 * tls certificate validation, see below urls
18 * clean tls shutdown, see http://www.linuxjournal.com/article/4822
19 * and http://www.linuxjournal.com/article/5487
20 * SSL_shutdown() and shutdown()
21 * If shutdown() we may not need REUSEADDR
22 * when tls client goes away, ensure that all related threads and state
24 * setsockopt(keepalive...), check if openssl has some keepalive feature
27 /* For UDP there is one server instance consisting of udpserverrd and udpserverth
28 * rd is responsible for init and launching wr
29 * For TLS there is a server instance that launches tlsserverrd for each TLS peer
30 * each tlsserverrd launches tlsserverwr
31 * For each UDP/TLS peer there is clientrd and clientwr, clientwr is responsible
32 * for init and launching rd
34 * serverrd will receive a request, processes it and puts it in the requestq of
35 * the appropriate clientwr
36 * clientwr monitors its requestq and sends requests
37 * clientrd looks for responses, processes them and puts them in the replyq of
38 * the peer the request came from
39 * serverwr monitors its reply and sends replies
41 * In addition to the main thread, we have:
42 * If UDP peers are configured, there will be 2 + 2 * #peers UDP threads
43 * If TLS peers are configured, there will initially be 2 * #peers TLS threads
44 * For each TLS peer connecting to us there will be 2 more TLS threads
45 * This is only for connected peers
46 * Example: With 3 UDP peer and 30 TLS peers, there will be a max of
47 * 1 + (2 + 2 * 3) + (2 * 30) + (2 * 30) = 129 threads
54 #include <openssl/ssl.h>
55 #include <openssl/rand.h>
56 #include <openssl/err.h>
57 #include <openssl/md5.h>
58 #include <openssl/hmac.h>
59 #include "radsecproxy.h"
61 static struct client clients[MAX_PEERS];
62 static struct server servers[MAX_PEERS];
64 static int client_count = 0;
65 static int server_count = 0;
67 static struct replyq udp_server_replyq;
68 static int udp_server_sock = -1;
69 static char *udp_server_port = DEFAULT_UDP_PORT;
70 static pthread_mutex_t *ssl_locks;
71 static long *ssl_lock_count;
72 static SSL_CTX *ssl_ctx_cl;
76 /* callbacks for making OpenSSL thread safe */
77 unsigned long ssl_thread_id() {
78 return (unsigned long)pthread_self();
81 void ssl_locking_callback(int mode, int type, const char *file, int line) {
82 if (mode & CRYPTO_LOCK) {
83 pthread_mutex_lock(&ssl_locks[type]);
84 ssl_lock_count[type]++;
86 pthread_mutex_unlock(&ssl_locks[type]);
89 void ssl_locks_setup() {
92 ssl_locks = malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
93 ssl_lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
94 for (i = 0; i < CRYPTO_num_locks(); i++) {
95 ssl_lock_count[i] = 0;
96 pthread_mutex_init(&ssl_locks[i], NULL);
99 CRYPTO_set_id_callback(ssl_thread_id);
100 CRYPTO_set_locking_callback(ssl_locking_callback);
103 void printauth(char *s, unsigned char *t) {
106 for (i = 0; i < 16; i++)
107 printf("%02x ", t[i]);
111 int resolvepeer(struct peer *peer) {
112 struct addrinfo hints, *addrinfo;
114 memset(&hints, 0, sizeof(hints));
115 hints.ai_socktype = (peer->type == 'T' ? SOCK_STREAM : SOCK_DGRAM);
116 hints.ai_family = AF_UNSPEC;
117 if (getaddrinfo(peer->host, peer->port, &hints, &addrinfo)) {
118 err("resolvepeer: can't resolve %s port %s", peer->host, peer->port);
123 freeaddrinfo(peer->addrinfo);
124 peer->addrinfo = addrinfo;
128 int connecttoserver(struct addrinfo *addrinfo) {
130 struct addrinfo *res;
132 for (res = addrinfo; res; res = res->ai_next) {
133 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
135 err("connecttoserver: socket failed");
138 if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
140 err("connecttoserver: connect failed");
147 /* returns the client with matching address, or NULL */
148 /* if client argument is not NULL, we only check that one client */
149 struct client *find_client(char type, struct sockaddr *addr, struct client *client) {
150 struct sockaddr_in6 *sa6;
151 struct in_addr *a4 = NULL;
154 struct addrinfo *res;
156 if (addr->sa_family == AF_INET6) {
157 sa6 = (struct sockaddr_in6 *)addr;
158 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr))
159 a4 = (struct in_addr *)&sa6->sin6_addr.s6_addr[12];
161 a4 = &((struct sockaddr_in *)addr)->sin_addr;
163 c = (client ? client : clients);
164 for (i = 0; i < client_count; i++) {
165 if (c->peer.type == type)
166 for (res = c->peer.addrinfo; res; res = res->ai_next)
167 if ((a4 && res->ai_family == AF_INET &&
168 !memcmp(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, 4)) ||
169 (res->ai_family == AF_INET6 &&
170 !memcmp(&sa6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 16)))
179 /* returns the server with matching address, or NULL */
180 /* if server argument is not NULL, we only check that one server */
181 struct server *find_server(char type, struct sockaddr *addr, struct server *server) {
182 struct sockaddr_in6 *sa6;
183 struct in_addr *a4 = NULL;
186 struct addrinfo *res;
188 if (addr->sa_family == AF_INET6) {
189 sa6 = (struct sockaddr_in6 *)addr;
190 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr))
191 a4 = (struct in_addr *)&sa6->sin6_addr.s6_addr[12];
193 a4 = &((struct sockaddr_in *)addr)->sin_addr;
195 s = (server ? server : servers);
196 for (i = 0; i < server_count; i++) {
197 if (s->peer.type == type)
198 for (res = s->peer.addrinfo; res; res = res->ai_next)
199 if ((a4 && res->ai_family == AF_INET &&
200 !memcmp(a4, &((struct sockaddr_in *)res->ai_addr)->sin_addr, 4)) ||
201 (res->ai_family == AF_INET6 &&
202 !memcmp(&sa6->sin6_addr, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 16)))
211 /* exactly one of client and server must be non-NULL */
212 /* if *peer == NULL we return who we received from, else require it to be from peer */
213 /* return from in sa if not NULL */
214 unsigned char *radudpget(int s, struct client **client, struct server **server, struct sockaddr_storage *sa) {
217 unsigned char buf[65536], *rad;
218 struct sockaddr_storage from;
219 socklen_t fromlen = sizeof(from);
222 cnt = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&from, &fromlen);
224 err("radudpget: recv failed");
227 printf("radudpget: got %d bytes from %s\n", cnt, addr2string((struct sockaddr *)&from, fromlen));
230 printf("radudpget: packet too small\n");
237 printf("radudpget: packet smaller than length field in radius header\n");
241 printf("radudpget: packet was padded with %d bytes\n", cnt - len);
244 ? (void *)find_client('U', (struct sockaddr *)&from, *client)
245 : (void *)find_server('U', (struct sockaddr *)&from, *server));
247 printf("radudpget: got packet from wrong or unknown UDP peer, ignoring\n");
254 err("radudpget: malloc failed");
256 memcpy(rad, buf, len);
258 *client = (struct client *)f; /* only need this if *client == NULL, but if not NULL *client == f here */
260 *server = (struct server *)f; /* only need this if *server == NULL, but if not NULL *server == f here */
266 void tlsconnect(struct server *server, struct timeval *when, char *text) {
271 printf("tlsconnect called from %s\n", text);
272 pthread_mutex_lock(&server->lock);
273 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
274 /* already reconnected, nothing to do */
275 printf("tlsconnect(%s): seems already reconnected\n", text);
276 pthread_mutex_unlock(&server->lock);
280 printf("tlsconnect %s\n", text);
283 gettimeofday(&now, NULL);
284 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
285 if (server->connectionok) {
286 server->connectionok = 0;
288 } else if (elapsed < 5)
290 else if (elapsed < 600)
292 else if (elapsed < 10000)
295 server->lastconnecttry.tv_sec = now.tv_sec; // no sleep at startup
296 printf("tlsconnect: trying to open TLS connection to %s port %s\n", server->peer.host, server->peer.port);
297 if (server->sock >= 0)
299 if ((server->sock = connecttoserver(server->peer.addrinfo)) < 0)
301 SSL_free(server->peer.ssl);
302 server->peer.ssl = SSL_new(ssl_ctx_cl);
303 SSL_set_fd(server->peer.ssl, server->sock);
304 if (SSL_connect(server->peer.ssl) > 0)
306 while ((error = ERR_get_error()))
307 err("tlsconnect: TLS: %s", ERR_error_string(error, NULL));
309 printf("tlsconnect: TLS connection to %s port %s up\n", server->peer.host, server->peer.port);
310 gettimeofday(&server->lastconnecttry, NULL);
311 pthread_mutex_unlock(&server->lock);
314 unsigned char *radtlsget(SSL *ssl) {
316 unsigned char buf[4], *rad;
319 for (total = 0; total < 4; total += cnt) {
320 cnt = SSL_read(ssl, buf + total, 4 - total);
322 printf("radtlsget: connection lost\n");
323 if (SSL_get_error(ssl, cnt) == SSL_ERROR_ZERO_RETURN) {
324 //remote end sent close_notify, send one back
334 err("radtlsget: malloc failed");
339 for (; total < len; total += cnt) {
340 cnt = SSL_read(ssl, rad + total, len - total);
342 printf("radtlsget: connection lost\n");
343 if (SSL_get_error(ssl, cnt) == SSL_ERROR_ZERO_RETURN) {
344 //remote end sent close_notify, send one back
356 printf("radtlsget: packet smaller than minimum radius size\n");
359 printf("radtlsget: got %d bytes\n", total);
363 int clientradput(struct server *server, unsigned char *rad) {
367 struct timeval lastconnecttry;
370 if (server->peer.type == 'U') {
371 if (send(server->sock, rad, len, 0) >= 0) {
372 printf("clienradput: sent UDP of length %d to %s port %s\n", len, server->peer.host, server->peer.port);
375 err("clientradput: send failed");
379 lastconnecttry = server->lastconnecttry;
380 while ((cnt = SSL_write(server->peer.ssl, rad, len)) <= 0) {
381 while ((error = ERR_get_error()))
382 err("clientwr: TLS: %s", ERR_error_string(error, NULL));
383 tlsconnect(server, &lastconnecttry, "clientradput");
384 lastconnecttry = server->lastconnecttry;
387 server->connectionok = 1;
388 printf("clientradput: Sent %d bytes, Radius packet of length %d to TLS peer %s\n",
389 cnt, len, server->peer.host);
393 int radsign(unsigned char *rad, unsigned char *sec) {
394 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
395 static unsigned char first = 1;
396 static EVP_MD_CTX mdctx;
400 pthread_mutex_lock(&lock);
402 EVP_MD_CTX_init(&mdctx);
406 result = (EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) &&
407 EVP_DigestUpdate(&mdctx, rad, RADLEN(rad)) &&
408 EVP_DigestUpdate(&mdctx, sec, strlen(sec)) &&
409 EVP_DigestFinal_ex(&mdctx, rad + 4, &md_len) &&
411 pthread_mutex_unlock(&lock);
415 int validauth(unsigned char *rad, unsigned char *reqauth, unsigned char *sec) {
416 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
417 static unsigned char first = 1;
418 static EVP_MD_CTX mdctx;
419 unsigned char hash[EVP_MAX_MD_SIZE];
423 pthread_mutex_lock(&lock);
425 EVP_MD_CTX_init(&mdctx);
431 result = (EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) &&
432 EVP_DigestUpdate(&mdctx, rad, 4) &&
433 EVP_DigestUpdate(&mdctx, reqauth, 16) &&
434 (len <= 20 || EVP_DigestUpdate(&mdctx, rad + 20, len - 20)) &&
435 EVP_DigestUpdate(&mdctx, sec, strlen(sec)) &&
436 EVP_DigestFinal_ex(&mdctx, hash, &len) &&
438 !memcmp(hash, rad + 4, 16));
439 pthread_mutex_unlock(&lock);
443 int checkmessageauth(char *rad, uint8_t *authattr, char *secret) {
444 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
445 static unsigned char first = 1;
446 static HMAC_CTX hmacctx;
448 uint8_t auth[16], hash[EVP_MAX_MD_SIZE];
450 pthread_mutex_lock(&lock);
452 HMAC_CTX_init(&hmacctx);
456 memcpy(auth, authattr, 16);
457 memset(authattr, 0, 16);
459 HMAC_Init_ex(&hmacctx, secret, strlen(secret), EVP_md5(), NULL);
460 HMAC_Update(&hmacctx, rad, RADLEN(rad));
461 HMAC_Final(&hmacctx, hash, &md_len);
462 memcpy(authattr, auth, 16);
464 printf("message auth computation failed\n");
465 pthread_mutex_unlock(&lock);
469 if (memcmp(auth, hash, 16)) {
470 printf("message authenticator, wrong value\n");
471 pthread_mutex_unlock(&lock);
475 pthread_mutex_unlock(&lock);
479 int createmessageauth(char *rad, char *authattrval, char *secret) {
480 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
481 static unsigned char first = 1;
482 static HMAC_CTX hmacctx;
488 pthread_mutex_lock(&lock);
490 HMAC_CTX_init(&hmacctx);
494 memset(authattrval, 0, 16);
496 HMAC_Init_ex(&hmacctx, secret, strlen(secret), EVP_md5(), NULL);
497 HMAC_Update(&hmacctx, rad, RADLEN(rad));
498 HMAC_Final(&hmacctx, authattrval, &md_len);
500 printf("message auth computation failed\n");
501 pthread_mutex_unlock(&lock);
505 pthread_mutex_unlock(&lock);
509 void sendrq(struct server *to, struct client *from, struct request *rq) {
512 pthread_mutex_lock(&to->newrq_mutex);
513 /* might simplify if only try nextid, might be ok */
514 for (i = to->nextid; i < MAX_REQUESTS; i++)
515 if (!to->requests[i].buf)
517 if (i == MAX_REQUESTS) {
518 for (i = 0; i < to->nextid; i++)
519 if (!to->requests[i].buf)
521 if (i == to->nextid) {
522 printf("No room in queue, dropping request\n");
523 pthread_mutex_unlock(&to->newrq_mutex);
529 rq->buf[1] = (char)i;
530 printf("sendrq: inserting packet with id %d in queue for %s\n", i, to->peer.host);
532 if (!createmessageauth(rq->buf, rq->messageauthattrval, to->peer.secret))
535 gettimeofday(&rq->expiry, NULL);
536 rq->expiry.tv_sec += 30;
537 to->requests[i] = *rq;
541 printf("signalling client writer\n");
542 pthread_cond_signal(&to->newrq_cond);
544 pthread_mutex_unlock(&to->newrq_mutex);
547 void sendreply(struct client *to, struct server *from, char *buf, struct sockaddr_storage *tosa) {
548 struct replyq *replyq = to->replyq;
550 pthread_mutex_lock(&replyq->count_mutex);
551 if (replyq->count == replyq->size) {
552 printf("No room in queue, dropping request\n");
553 pthread_mutex_unlock(&replyq->count_mutex);
557 replyq->replies[replyq->count].buf = buf;
559 replyq->replies[replyq->count].tosa = *tosa;
562 if (replyq->count == 1) {
563 printf("signalling client writer\n");
564 pthread_cond_signal(&replyq->count_cond);
566 pthread_mutex_unlock(&replyq->count_mutex);
569 int pwdencrypt(uint8_t *in, uint8_t len, uint8_t *shared, uint8_t sharedlen, uint8_t *auth) {
570 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
571 static unsigned char first = 1;
572 static EVP_MD_CTX mdctx;
573 unsigned char hash[EVP_MAX_MD_SIZE], *input;
575 uint8_t i, offset = 0, out[128];
577 pthread_mutex_lock(&lock);
579 EVP_MD_CTX_init(&mdctx);
585 if (!EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) ||
586 !EVP_DigestUpdate(&mdctx, shared, sharedlen) ||
587 !EVP_DigestUpdate(&mdctx, input, 16) ||
588 !EVP_DigestFinal_ex(&mdctx, hash, &md_len) ||
590 pthread_mutex_unlock(&lock);
593 for (i = 0; i < 16; i++)
594 out[offset + i] = hash[i] ^ in[offset + i];
595 input = out + offset - 16;
600 memcpy(in, out, len);
601 pthread_mutex_unlock(&lock);
605 int pwddecrypt(uint8_t *in, uint8_t len, uint8_t *shared, uint8_t sharedlen, uint8_t *auth) {
606 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
607 static unsigned char first = 1;
608 static EVP_MD_CTX mdctx;
609 unsigned char hash[EVP_MAX_MD_SIZE], *input;
611 uint8_t i, offset = 0, out[128];
613 pthread_mutex_lock(&lock);
615 EVP_MD_CTX_init(&mdctx);
621 if (!EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) ||
622 !EVP_DigestUpdate(&mdctx, shared, sharedlen) ||
623 !EVP_DigestUpdate(&mdctx, input, 16) ||
624 !EVP_DigestFinal_ex(&mdctx, hash, &md_len) ||
626 pthread_mutex_unlock(&lock);
629 for (i = 0; i < 16; i++)
630 out[offset + i] = hash[i] ^ in[offset + i];
636 memcpy(in, out, len);
637 pthread_mutex_unlock(&lock);
641 int msmppencrypt(uint8_t *text, uint8_t len, uint8_t *shared, uint8_t sharedlen, uint8_t *auth, uint8_t *salt) {
642 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
643 static unsigned char first = 1;
644 static EVP_MD_CTX mdctx;
645 unsigned char hash[EVP_MAX_MD_SIZE];
649 pthread_mutex_lock(&lock);
651 EVP_MD_CTX_init(&mdctx);
656 printf("msppencrypt auth in: ");
657 for (i = 0; i < 16; i++)
658 printf("%02x ", auth[i]);
661 printf("msppencrypt salt in: ");
662 for (i = 0; i < 2; i++)
663 printf("%02x ", salt[i]);
666 printf("msppencrypt in: ");
667 for (i = 0; i < len; i++)
668 printf("%02x ", text[i]);
672 if (!EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) ||
673 !EVP_DigestUpdate(&mdctx, shared, sharedlen) ||
674 !EVP_DigestUpdate(&mdctx, auth, 16) ||
675 !EVP_DigestUpdate(&mdctx, salt, 2) ||
676 !EVP_DigestFinal_ex(&mdctx, hash, &md_len)) {
677 pthread_mutex_unlock(&lock);
682 printf("msppencrypt hash: ");
683 for (i = 0; i < 16; i++)
684 printf("%02x ", hash[i]);
688 for (i = 0; i < 16; i++)
691 for (offset = 16; offset < len; offset += 16) {
693 printf("text + offset - 16 c(%d): ", offset / 16);
694 for (i = 0; i < 16; i++)
695 printf("%02x ", (text + offset - 16)[i]);
698 if (!EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) ||
699 !EVP_DigestUpdate(&mdctx, shared, sharedlen) ||
700 !EVP_DigestUpdate(&mdctx, text + offset - 16, 16) ||
701 !EVP_DigestFinal_ex(&mdctx, hash, &md_len) ||
703 pthread_mutex_unlock(&lock);
707 printf("msppencrypt hash: ");
708 for (i = 0; i < 16; i++)
709 printf("%02x ", hash[i]);
713 for (i = 0; i < 16; i++)
714 text[offset + i] ^= hash[i];
718 printf("msppencrypt out: ");
719 for (i = 0; i < len; i++)
720 printf("%02x ", text[i]);
724 pthread_mutex_unlock(&lock);
728 int msmppdecrypt(uint8_t *text, uint8_t len, uint8_t *shared, uint8_t sharedlen, uint8_t *auth, uint8_t *salt) {
729 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
730 static unsigned char first = 1;
731 static EVP_MD_CTX mdctx;
732 unsigned char hash[EVP_MAX_MD_SIZE];
737 pthread_mutex_lock(&lock);
739 EVP_MD_CTX_init(&mdctx);
744 printf("msppdecrypt auth in: ");
745 for (i = 0; i < 16; i++)
746 printf("%02x ", auth[i]);
749 printf("msppedecrypt salt in: ");
750 for (i = 0; i < 2; i++)
751 printf("%02x ", salt[i]);
754 printf("msppedecrypt in: ");
755 for (i = 0; i < len; i++)
756 printf("%02x ", text[i]);
760 if (!EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) ||
761 !EVP_DigestUpdate(&mdctx, shared, sharedlen) ||
762 !EVP_DigestUpdate(&mdctx, auth, 16) ||
763 !EVP_DigestUpdate(&mdctx, salt, 2) ||
764 !EVP_DigestFinal_ex(&mdctx, hash, &md_len)) {
765 pthread_mutex_unlock(&lock);
770 printf("msppedecrypt hash: ");
771 for (i = 0; i < 16; i++)
772 printf("%02x ", hash[i]);
776 for (i = 0; i < 16; i++)
777 plain[i] = text[i] ^ hash[i];
779 for (offset = 16; offset < len; offset += 16) {
781 printf("text + offset - 16 c(%d): ", offset / 16);
782 for (i = 0; i < 16; i++)
783 printf("%02x ", (text + offset - 16)[i]);
786 if (!EVP_DigestInit_ex(&mdctx, EVP_md5(), NULL) ||
787 !EVP_DigestUpdate(&mdctx, shared, sharedlen) ||
788 !EVP_DigestUpdate(&mdctx, text + offset - 16, 16) ||
789 !EVP_DigestFinal_ex(&mdctx, hash, &md_len) ||
791 pthread_mutex_unlock(&lock);
795 printf("msppedecrypt hash: ");
796 for (i = 0; i < 16; i++)
797 printf("%02x ", hash[i]);
801 for (i = 0; i < 16; i++)
802 plain[offset + i] = text[offset + i] ^ hash[i];
805 memcpy(text, plain, len);
807 printf("msppedecrypt out: ");
808 for (i = 0; i < len; i++)
809 printf("%02x ", text[i]);
813 pthread_mutex_unlock(&lock);
817 struct server *id2server(char *id, uint8_t len) {
819 char **realm, *idrealm;
821 idrealm = strchr(id, '@');
829 for (i = 0; i < server_count; i++) {
830 for (realm = servers[i].realms; *realm; realm++) {
831 if ((strlen(*realm) == 1 && **realm == '*') ||
832 (strlen(*realm) == len && !memcmp(idrealm, *realm, len))) {
833 printf("found matching realm: %s, host %s\n", *realm, servers[i].peer.host);
841 struct server *radsrv(struct request *rq, char *buf, struct client *from) {
842 uint8_t code, id, *auth, *attr, attrvallen;
843 uint8_t *usernameattr = NULL, *userpwdattr = NULL, *tunnelpwdattr = NULL, *messageauthattr = NULL;
848 unsigned char newauth[16];
850 code = *(uint8_t *)buf;
851 id = *(uint8_t *)(buf + 1);
853 auth = (uint8_t *)(buf + 4);
855 printf("radsrv: code %d, id %d, length %d\n", code, id, len);
857 if (code != RAD_Access_Request) {
858 printf("radsrv: server currently accepts only access-requests, ignoring\n");
866 left -= attr[RAD_Attr_Length];
868 printf("radsrv: attribute length exceeds packet length, ignoring packet\n");
871 switch (attr[RAD_Attr_Type]) {
872 case RAD_Attr_User_Name:
875 case RAD_Attr_User_Password:
878 case RAD_Attr_Tunnel_Password:
879 tunnelpwdattr = attr;
881 case RAD_Attr_Message_Authenticator:
882 messageauthattr = attr;
885 attr += attr[RAD_Attr_Length];
888 printf("radsrv: malformed packet? remaining byte after last attribute\n");
891 printf("radsrv: Username: ");
892 for (i = 0; i < usernameattr[RAD_Attr_Length] - 2; i++)
893 printf("%c", usernameattr[RAD_Attr_Value + i]);
897 to = id2server(&usernameattr[RAD_Attr_Value], usernameattr[RAD_Attr_Length] - 2);
899 printf("radsrv: ignoring request, don't know where to send it\n");
903 if (messageauthattr && (messageauthattr[RAD_Attr_Length] != 18 ||
904 !checkmessageauth(buf, &messageauthattr[RAD_Attr_Value], from->peer.secret))) {
905 printf("radsrv: message authentication failed\n");
909 if (!RAND_bytes(newauth, 16)) {
910 printf("radsrv: failed to generate random auth\n");
914 printauth("auth", auth);
915 printauth("newauth", newauth);
918 printf("radsrv: found userpwdattr of length %d\n", userpwdattr[RAD_Attr_Length]);
919 attrvallen = userpwdattr[RAD_Attr_Length] - 2;
920 if (attrvallen < 16 || attrvallen > 128 || attrvallen % 16) {
921 printf("radsrv: invalid user password length\n");
925 if (!pwddecrypt(&userpwdattr[RAD_Attr_Value], attrvallen, from->peer.secret, strlen(from->peer.secret), auth)) {
926 printf("radsrv: cannot decrypt password\n");
929 printf("radsrv: password: ");
930 for (i = 0; i < attrvallen; i++)
931 printf("%02x ", userpwdattr[RAD_Attr_Value + i]);
933 if (!pwdencrypt(&userpwdattr[RAD_Attr_Value], attrvallen, to->peer.secret, strlen(to->peer.secret), newauth)) {
934 printf("radsrv: cannot encrypt password\n");
940 printf("radsrv: found tunnelpwdattr of length %d\n", tunnelpwdattr[RAD_Attr_Length]);
941 attrvallen = tunnelpwdattr[RAD_Attr_Length] - 2;
942 if (attrvallen < 16 || attrvallen > 128 || attrvallen % 16) {
943 printf("radsrv: invalid user password length\n");
947 if (!pwddecrypt(&tunnelpwdattr[RAD_Attr_Value], attrvallen, from->peer.secret, strlen(from->peer.secret), auth)) {
948 printf("radsrv: cannot decrypt password\n");
951 printf("radsrv: password: ");
952 for (i = 0; i < attrvallen; i++)
953 printf("%02x ", tunnelpwdattr[RAD_Attr_Value + i]);
955 if (!pwdencrypt(&tunnelpwdattr[RAD_Attr_Value], attrvallen, to->peer.secret, strlen(to->peer.secret), newauth)) {
956 printf("radsrv: cannot encrypt password\n");
964 rq->messageauthattrval = (messageauthattr ? &messageauthattr[RAD_Attr_Value] : NULL);
965 memcpy(rq->origauth, auth, 16);
966 memcpy(auth, newauth, 16);
967 printauth("rq->origauth", rq->origauth);
968 printauth("auth", auth);
972 void *clientrd(void *arg) {
973 struct server *server = (struct server *)arg;
975 int i, left, subleft;
976 unsigned char *buf, *messageauthattr, *subattr, *attr;
977 struct sockaddr_storage fromsa;
978 struct timeval lastconnecttry;
983 lastconnecttry = server->lastconnecttry;
984 buf = (server->peer.type == 'U' ? radudpget(server->sock, NULL, &server, NULL) : radtlsget(server->peer.ssl));
985 if (!buf && server->peer.type == 'T') {
986 tlsconnect(server, &lastconnecttry, "clientrd");
990 server->connectionok = 1;
992 if (*buf != RAD_Access_Accept && *buf != RAD_Access_Reject && *buf != RAD_Access_Challenge) {
993 printf("clientrd: discarding, only accept access accept, access reject and access challenge messages\n");
997 i = buf[1]; /* i is the id */
999 pthread_mutex_lock(&server->newrq_mutex);
1000 if (!server->requests[i].buf || !server->requests[i].tries) {
1001 pthread_mutex_unlock(&server->newrq_mutex);
1002 printf("clientrd: no matching request sent with this id, ignoring\n");
1006 if (server->requests[i].received) {
1007 pthread_mutex_unlock(&server->newrq_mutex);
1008 printf("clientrd: already received, ignoring\n");
1012 if (!validauth(buf, server->requests[i].buf + 4, server->peer.secret)) {
1013 pthread_mutex_unlock(&server->newrq_mutex);
1014 printf("clientrd: invalid auth, ignoring\n");
1018 from = server->requests[i].from;
1021 /* messageauthattr present? */
1022 messageauthattr = NULL;
1023 left = RADLEN(buf) - 20;
1026 left -= attr[RAD_Attr_Length];
1028 printf("clientrd: attribute length exceeds packet length, ignoring packet\n");
1031 if (attr[RAD_Attr_Type] == RAD_Attr_Message_Authenticator) {
1032 if (attr[RAD_Attr_Length] != 18) {
1033 printf("clientrd: illegal message auth attribute length, ignoring packet\n");
1036 memcpy(tmp, buf + 4, 16);
1037 memcpy(buf + 4, server->requests[i].buf + 4, 16);
1038 if (!checkmessageauth(buf, &attr[RAD_Attr_Value], server->peer.secret)) {
1039 printf("clientrd: message authentication failed\n");
1042 memcpy(buf + 4, tmp, 16);
1043 printf("clientrd: message auth ok\n");
1044 messageauthattr = attr;
1047 attr += attr[RAD_Attr_Length];
1050 /* handle MS MPPE */
1051 left = RADLEN(buf) - 20;
1054 left -= attr[RAD_Attr_Length];
1056 printf("clientrd: attribute length exceeds packet length, ignoring packet\n");
1059 if (attr[RAD_Attr_Type] == RAD_Attr_Vendor_Specific &&
1060 ((uint16_t *)attr)[1] == 0 && ntohs(((uint16_t *)attr)[2]) == 311) { // 311 == MS
1061 subleft = attr[RAD_Attr_Length] - 6;
1063 while (subleft > 1) {
1064 subleft -= subattr[RAD_Attr_Length];
1067 if (subattr[RAD_Attr_Type] != RAD_VS_ATTR_MS_MPPE_Send_Key &&
1068 subattr[RAD_Attr_Type] != RAD_VS_ATTR_MS_MPPE_Recv_Key)
1070 printf("clientrd: Got MS MPPE\n");
1071 if (subattr[RAD_Attr_Length] < 20)
1074 if (!msmppdecrypt(subattr + 4, subattr[RAD_Attr_Length] - 4,
1075 server->peer.secret, strlen(server->peer.secret), server->requests[i].buf + 4, subattr + 2)) {
1076 printf("clientrd: failed to decrypt msppe key\n");
1080 if (!msmppencrypt(subattr + 4, subattr[RAD_Attr_Length] - 4,
1081 from->peer.secret, strlen(from->peer.secret), server->requests[i].origauth, subattr + 2)) {
1082 printf("clientrd: failed to encrypt msppe key\n");
1087 printf("clientrd: bad vendor specific attr or subattr length, ignoring packet\n");
1091 attr += attr[RAD_Attr_Length];
1094 /* once we set received = 1, requests[i] may be reused */
1095 buf[1] = (char)server->requests[i].origid;
1096 memcpy(buf + 4, server->requests[i].origauth, 16);
1097 printauth("origauth/buf+4", buf + 4);
1098 if (messageauthattr) {
1099 if (!createmessageauth(buf, &messageauthattr[RAD_Attr_Value], from->peer.secret))
1101 printf("clientrd: computed messageauthattr\n");
1104 if (from->peer.type == 'U')
1105 fromsa = server->requests[i].fromsa;
1106 server->requests[i].received = 1;
1107 pthread_mutex_unlock(&server->newrq_mutex);
1109 if (!radsign(buf, from->peer.secret)) {
1110 printf("clientrd: failed to sign message\n");
1113 printauth("signedorigauth/buf+4", buf + 4);
1114 printf("clientrd: giving packet back to where it came from\n");
1115 sendreply(from, server, buf, from->peer.type == 'U' ? &fromsa : NULL);
1119 void *clientwr(void *arg) {
1120 struct server *server = (struct server *)arg;
1122 pthread_t clientrdth;
1126 if (server->peer.type == 'U') {
1127 if ((server->sock = connecttoserver(server->peer.addrinfo)) < 0) {
1128 printf("clientwr: connecttoserver failed\n");
1132 tlsconnect(server, NULL, "new client");
1134 if (pthread_create(&clientrdth, NULL, clientrd, (void *)server))
1135 errx("clientwr: pthread_create failed");
1138 pthread_mutex_lock(&server->newrq_mutex);
1139 while (!server->newrq) {
1140 printf("clientwr: waiting for signal\n");
1141 pthread_cond_wait(&server->newrq_cond, &server->newrq_mutex);
1142 printf("clientwr: got signal\n");
1145 pthread_mutex_unlock(&server->newrq_mutex);
1147 for (i = 0; i < MAX_REQUESTS; i++) {
1148 pthread_mutex_lock(&server->newrq_mutex);
1149 while (!server->requests[i].buf && i < MAX_REQUESTS)
1151 if (i == MAX_REQUESTS) {
1152 pthread_mutex_unlock(&server->newrq_mutex);
1156 gettimeofday(&now, NULL);
1157 rq = server->requests + i;
1160 printf("clientwr: removing received packet from queue\n");
1162 /* setting this to NULL means that it can be reused */
1164 pthread_mutex_unlock(&server->newrq_mutex);
1167 if (now.tv_sec > rq->expiry.tv_sec) {
1168 printf("clientwr: removing expired packet from queue\n");
1170 /* setting this to NULL means that it can be reused */
1172 pthread_mutex_unlock(&server->newrq_mutex);
1177 continue; // not re-sending (yet)
1180 pthread_mutex_unlock(&server->newrq_mutex);
1182 clientradput(server, server->requests[i].buf);
1185 /* should do more work to maintain TLS connections, keepalives etc */
1188 void *udpserverwr(void *arg) {
1189 struct replyq *replyq = &udp_server_replyq;
1190 struct reply *reply = replyq->replies;
1192 pthread_mutex_lock(&replyq->count_mutex);
1194 while (!replyq->count) {
1195 printf("udp server writer, waiting for signal\n");
1196 pthread_cond_wait(&replyq->count_cond, &replyq->count_mutex);
1197 printf("udp server writer, got signal\n");
1199 pthread_mutex_unlock(&replyq->count_mutex);
1201 if (sendto(udp_server_sock, reply->buf, RADLEN(reply->buf), 0,
1202 (struct sockaddr *)&reply->tosa, SOCKADDR_SIZE(reply->tosa)) < 0)
1203 err("sendudp: send failed");
1206 pthread_mutex_lock(&replyq->count_mutex);
1208 memmove(replyq->replies, replyq->replies + 1,
1209 replyq->count * sizeof(struct reply));
1213 void *udpserverrd(void *arg) {
1218 pthread_t udpserverwrth;
1220 if ((udp_server_sock = bindport(SOCK_DGRAM, udp_server_port)) < 0) {
1221 printf("udpserverrd: socket/bind failed\n");
1224 printf("udpserverrd: listening on UDP port %s\n", udp_server_port);
1226 if (pthread_create(&udpserverwrth, NULL, udpserverwr, NULL))
1227 errx("pthread_create failed");
1231 memset(&rq, 0, sizeof(struct request));
1232 buf = radudpget(udp_server_sock, &fr, NULL, &rq.fromsa);
1233 to = radsrv(&rq, buf, fr);
1235 printf("udpserverrd: ignoring request, no place to send it\n");
1238 sendrq(to, fr, &rq);
1242 void *tlsserverwr(void *arg) {
1244 unsigned long error;
1245 struct client *client = (struct client *)arg;
1246 struct replyq *replyq;
1248 printf("tlsserverwr starting for %s\n", client->peer.host);
1249 replyq = client->replyq;
1250 pthread_mutex_lock(&replyq->count_mutex);
1252 while (!replyq->count) {
1253 if (client->peer.ssl) {
1254 printf("tls server writer, waiting for signal\n");
1255 pthread_cond_wait(&replyq->count_cond, &replyq->count_mutex);
1256 printf("tls server writer, got signal\n");
1258 if (!client->peer.ssl) {
1259 //ssl might have changed while waiting
1260 pthread_mutex_unlock(&replyq->count_mutex);
1261 printf("tlsserverwr: exiting as requested\n");
1265 pthread_mutex_unlock(&replyq->count_mutex);
1266 cnt = SSL_write(client->peer.ssl, replyq->replies->buf, RADLEN(replyq->replies->buf));
1268 printf("tlsserverwr: Sent %d bytes, Radius packet of length %d\n",
1269 cnt, RADLEN(replyq->replies->buf));
1271 while ((error = ERR_get_error()))
1272 err("tlsserverwr: SSL: %s", ERR_error_string(error, NULL));
1273 free(replyq->replies->buf);
1275 pthread_mutex_lock(&replyq->count_mutex);
1277 memmove(replyq->replies, replyq->replies + 1, replyq->count * sizeof(struct reply));
1281 void *tlsserverrd(void *arg) {
1284 unsigned long error;
1287 struct client *client = (struct client *)arg;
1288 pthread_t tlsserverwrth;
1291 printf("tlsserverrd starting for %s\n", client->peer.host);
1292 ssl = client->peer.ssl;
1294 if (SSL_accept(ssl) <= 0) {
1295 while ((error = ERR_get_error()))
1296 err("tlsserverrd: SSL: %s", ERR_error_string(error, NULL));
1297 errx("accept failed, child exiting");
1300 if (pthread_create(&tlsserverwrth, NULL, tlsserverwr, (void *)client))
1301 errx("pthread_create failed");
1304 buf = radtlsget(client->peer.ssl);
1307 printf("tlsserverrd: got Radius message from %s\n", client->peer.host);
1308 memset(&rq, 0, sizeof(struct request));
1309 to = radsrv(&rq, buf, client);
1311 printf("ignoring request, no place to send it\n");
1314 sendrq(to, client, &rq);
1316 printf("tlsserverrd: connection lost\n");
1317 // stop writer by setting peer.ssl to NULL and give signal in case waiting for data
1318 client->peer.ssl = NULL;
1319 pthread_mutex_lock(&client->replyq->count_mutex);
1320 pthread_cond_signal(&client->replyq->count_cond);
1321 pthread_mutex_unlock(&client->replyq->count_mutex);
1322 printf("tlsserverrd: waiting for writer to end\n");
1323 pthread_join(tlsserverwrth, NULL);
1324 s = SSL_get_fd(ssl);
1327 printf("tlsserverrd thread for %s exiting\n", client->peer.host);
1331 int tlslistener(SSL_CTX *ssl_ctx) {
1332 pthread_t tlsserverth;
1334 struct sockaddr_storage from;
1335 size_t fromlen = sizeof(from);
1336 struct client *client;
1338 if ((s = bindport(SOCK_STREAM, DEFAULT_TLS_PORT)) < 0) {
1339 printf("tlslistener: socket/bind failed\n");
1344 printf("listening for incoming TLS on port %s\n", DEFAULT_TLS_PORT);
1347 snew = accept(s, (struct sockaddr *)&from, &fromlen);
1349 errx("accept failed");
1350 printf("incoming TLS connection from %s\n", addr2string((struct sockaddr *)&from, fromlen));
1352 client = find_client('T', (struct sockaddr *)&from, NULL);
1354 printf("ignoring request, not a known TLS client\n");
1355 shutdown(snew, SHUT_RDWR);
1360 if (client->peer.ssl) {
1361 printf("Ignoring incoming connection, already have one from this client\n");
1362 shutdown(snew, SHUT_RDWR);
1366 client->peer.ssl = SSL_new(ssl_ctx);
1367 SSL_set_fd(client->peer.ssl, snew);
1368 if (pthread_create(&tlsserverth, NULL, tlsserverrd, (void *)client))
1369 errx("pthread_create failed");
1370 pthread_detach(tlsserverth);
1375 char *parsehostport(char *s, struct peer *peer) {
1380 // allow literal addresses and port, e.g. [2001:db8::1]:1812
1384 for (; *p && *p != ']' && *p != ' ' && *p != '\t' && *p != '\n'; p++);
1386 printf("no ] matching initial [\n");
1392 for (; *p && *p != ':' && *p != ' ' && *p != '\t' && *p != '\n'; p++);
1395 printf("missing host/address\n");
1398 peer->host = malloc(p - field + 1);
1400 errx("malloc failed");
1401 memcpy(peer->host, field, p - field);
1402 peer->host[p - field] = '\0';
1405 if (*p && *p != ':' && *p != ' ' && *p != '\t' && *p != '\n') {
1406 printf("unexpected character after ]\n");
1411 /* port number or service name is specified */;
1413 for (; *p && *p != ' ' && *p != '\t' && *p != '\n'; p++);
1415 printf("syntax error, : but no following port\n");
1418 peer->port = malloc(p - field + 1);
1420 errx("malloc failed");
1421 memcpy(peer->port, field, p - field);
1422 peer->port[p - field] = '\0';
1428 // * is default, else longest match ... ";" used for separator
1429 char *parserealmlist(char *s, struct server *server) {
1433 for (p = s, n = 1; *p && *p != ' ' && *p != '\t' && *p != '\n'; p++)
1438 server->realms = NULL;
1441 server->realmdata = malloc(l + 1);
1442 if (!server->realmdata)
1443 errx("malloc failed");
1444 memcpy(server->realmdata, s, l);
1445 server->realmdata[l] = '\0';
1446 server->realms = malloc((1+n) * sizeof(char *));
1447 if (!server->realms)
1448 errx("malloc failed");
1449 server->realms[0] = server->realmdata;
1450 for (n = 1, i = 0; i < l; i++)
1451 if (server->realmdata[i] == ';') {
1452 server->realmdata[i] = '\0';
1453 server->realms[n++] = server->realmdata + i + 1;
1455 server->realms[n] = NULL;
1459 /* exactly one argument must be non-NULL */
1460 void getconfig(const char *serverfile, const char *clientfile) {
1463 char *p, *field, **r;
1464 struct client *client;
1465 struct server *server;
1470 printf("opening file %s for reading\n", serverfile);
1471 f = fopen(serverfile, "r");
1473 errx("getconfig failed to open %s for reading", serverfile);
1474 count = &server_count;
1476 printf("opening file %s for reading\n", clientfile);
1477 f = fopen(clientfile, "r");
1479 errx("getconfig failed to open %s for reading", clientfile);
1480 udp_server_replyq.replies = malloc(4 * MAX_REQUESTS * sizeof(struct reply));
1481 if (!udp_server_replyq.replies)
1482 errx("malloc failed");
1483 udp_server_replyq.size = 4 * MAX_REQUESTS;
1484 udp_server_replyq.count = 0;
1485 pthread_mutex_init(&udp_server_replyq.count_mutex, NULL);
1486 pthread_cond_init(&udp_server_replyq.count_cond, NULL);
1487 count = &client_count;
1491 while (fgets(line, 1024, f) && *count < MAX_PEERS) {
1493 server = &servers[*count];
1494 memset(server, 0, sizeof(struct server));
1495 peer = &server->peer;
1497 client = &clients[*count];
1498 memset(client, 0, sizeof(struct client));
1499 peer = &client->peer;
1501 for (p = line; *p == ' ' || *p == '\t'; p++);
1502 if (*p == '#' || *p == '\n')
1504 if (*p != 'U' && *p != 'T') {
1505 printf("server type must be U or T, got %c\n", *p);
1509 for (p++; *p == ' ' || *p == '\t'; p++);
1510 p = parsehostport(p, peer);
1512 peer->port = (peer->type == 'U' ? DEFAULT_UDP_PORT : DEFAULT_TLS_PORT);
1513 for (; *p == ' ' || *p == '\t'; p++);
1515 p = parserealmlist(p, server);
1516 if (!server->realms) {
1517 printf("realm list must be specified\n");
1520 for (; *p == ' ' || *p == '\t'; p++);
1523 for (; *p && *p != ' ' && *p != '\t' && *p != '\n'; p++);
1525 /* no secret set and end of line, line is complete if TLS */
1526 if (peer->type == 'U') {
1527 printf("secret must be specified for UDP\n");
1530 peer->secret = DEFAULT_TLS_SECRET;
1532 peer->secret = malloc(p - field + 1);
1534 errx("malloc failed");
1535 memcpy(peer->secret, field, p - field);
1536 peer->secret[p - field] = '\0';
1537 /* check that rest of line only white space */
1538 for (; *p == ' ' || *p == '\t'; p++);
1539 if (*p && *p != '\n') {
1540 printf("max 4 fields per line, found a 5th\n");
1545 if ((serverfile && !resolvepeer(&server->peer)) ||
1546 (clientfile && !resolvepeer(&client->peer))) {
1547 printf("failed to resolve host %s port %s, exiting\n", peer->host, peer->port);
1552 pthread_mutex_init(&server->lock, NULL);
1554 server->requests = malloc(MAX_REQUESTS * sizeof(struct request));
1555 if (!server->requests)
1556 errx("malloc failed");
1557 memset(server->requests, 0, MAX_REQUESTS * sizeof(struct request));
1559 pthread_mutex_init(&server->newrq_mutex, NULL);
1560 pthread_cond_init(&server->newrq_cond, NULL);
1562 if (peer->type == 'U')
1563 client->replyq = &udp_server_replyq;
1565 client->replyq = malloc(sizeof(struct replyq));
1566 if (!client->replyq)
1567 errx("malloc failed");
1568 client->replyq->replies = malloc(MAX_REQUESTS * sizeof(struct reply));
1569 if (!client->replyq->replies)
1570 errx("malloc failed");
1571 client->replyq->size = MAX_REQUESTS;
1572 client->replyq->count = 0;
1573 pthread_mutex_init(&client->replyq->count_mutex, NULL);
1574 pthread_cond_init(&client->replyq->count_cond, NULL);
1577 printf("got type %c, host %s, port %s, secret %s\n", peer->type, peer->host, peer->port, peer->secret);
1579 printf(" with realms:");
1580 for (r = server->realms; *r; r++)
1589 void parseargs(int argc, char **argv) {
1592 while ((c = getopt(argc, argv, "p:")) != -1) {
1595 udp_server_port = optarg;
1605 printf("radsecproxy [ -p UDP-port ]\n");
1609 int main(int argc, char **argv) {
1610 SSL_CTX *ssl_ctx_srv;
1611 unsigned long error;
1612 pthread_t udpserverth;
1613 // pthread_attr_t joinable;
1616 parseargs(argc, argv);
1617 getconfig("servers.conf", NULL);
1618 getconfig(NULL, "clients.conf");
1622 // pthread_attr_init(&joinable);
1623 // pthread_attr_setdetachstate(&joinable, PTHREAD_CREATE_JOINABLE);
1625 /* listen on UDP if at least one UDP client */
1627 for (i = 0; i < client_count; i++)
1628 if (clients[i].peer.type == 'U') {
1629 if (pthread_create(&udpserverth, NULL /*&joinable*/, udpserverrd, NULL))
1630 errx("pthread_create failed");
1635 SSL_load_error_strings();
1638 while (!RAND_status()) {
1639 time_t t = time(NULL);
1640 pid_t pid = getpid();
1641 RAND_seed((unsigned char *)&t, sizeof(time_t));
1642 RAND_seed((unsigned char *)&pid, sizeof(pid));
1645 /* initialise client part and start clients */
1646 ssl_ctx_cl = SSL_CTX_new(TLSv1_client_method());
1650 for (i = 0; i < server_count; i++) {
1651 if (pthread_create(&servers[i].clientth, NULL, clientwr, (void *)&servers[i]))
1652 errx("pthread_create failed");
1655 for (i = 0; i < client_count; i++)
1656 if (clients[i].peer.type == 'T')
1659 if (i == client_count) {
1660 printf("No TLS clients defined, not starting TLS listener\n");
1661 /* just hang around doing nothing, anything to do here? */
1666 /* setting up server/daemon part */
1667 ssl_ctx_srv = SSL_CTX_new(TLSv1_server_method());
1670 if (!SSL_CTX_use_certificate_file(ssl_ctx_srv, "/tmp/server.pem", SSL_FILETYPE_PEM)) {
1671 while ((error = ERR_get_error()))
1672 err("SSL: %s", ERR_error_string(error, NULL));
1673 errx("Failed to load certificate");
1675 if (!SSL_CTX_use_PrivateKey_file(ssl_ctx_srv, "/tmp/server.key", SSL_FILETYPE_PEM)) {
1676 while ((error = ERR_get_error()))
1677 err("SSL: %s", ERR_error_string(error, NULL));
1678 errx("Failed to load private key");
1681 return tlslistener(ssl_ctx_srv);