2 * Copyright (C) 2006-2009 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 #include <sys/socket.h>
11 #include <netinet/in.h>
20 #include <sys/types.h>
21 #include <sys/select.h>
24 #include <arpa/inet.h>
27 #include <openssl/ssl.h>
28 #include <openssl/err.h>
31 #include "radsecproxy.h"
37 static void setprotoopts(struct commonprotoopts *opts);
38 static char **getlistenerargs();
39 void *tlslistener(void *arg);
40 int tlsconnect(struct server *server, struct timeval *when, int timeout, char *text);
41 void *tlsclientrd(void *arg);
42 int clientradputtls(struct server *server, unsigned char *rad);
45 static const struct protodefs protodefs = {
47 "mysecret", /* secretdefault */
48 SOCK_STREAM, /* socktype */
49 "2083", /* portdefault */
50 0, /* retrycountdefault */
51 0, /* retrycountmax */
52 REQUEST_RETRY_INTERVAL * REQUEST_RETRY_COUNT, /* retryintervaldefault */
53 60, /* retryintervalmax */
54 DUPLICATE_INTERVAL, /* duplicateintervaldefault */
55 setprotoopts, /* setprotoopts */
56 getlistenerargs, /* getlistenerargs */
57 tlslistener, /* listener */
58 tlsconnect, /* connecter */
59 tlsclientrd, /* clientconnreader */
60 clientradputtls, /* clientradput */
62 NULL, /* addserverextra */
63 tlssetsrcres, /* setsrcres */
67 static struct addrinfo *srcres = NULL;
68 static uint8_t handle;
69 static struct commonprotoopts *protoopts = NULL;
71 const struct protodefs *tlsinit(uint8_t h) {
76 static void setprotoopts(struct commonprotoopts *opts) {
80 static char **getlistenerargs() {
81 return protoopts ? protoopts->listenargs : NULL;
86 srcres = resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL, NULL, protodefs.socktype);
89 int tlsconnect(struct server *server, struct timeval *when, int timeout, char *text) {
96 debug(DBG_DBG, "tlsconnect: called from %s", text);
97 pthread_mutex_lock(&server->lock);
98 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
99 /* already reconnected, nothing to do */
100 debug(DBG_DBG, "tlsconnect(%s): seems already reconnected", text);
101 pthread_mutex_unlock(&server->lock);
106 gettimeofday(&now, NULL);
107 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
108 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
109 debug(DBG_DBG, "tlsconnect: timeout");
110 if (server->sock >= 0)
112 SSL_free(server->ssl);
114 pthread_mutex_unlock(&server->lock);
117 if (server->connectionok) {
118 server->connectionok = 0;
120 } else if (elapsed < 1)
122 else if (elapsed < 60) {
123 debug(DBG_INFO, "tlsconnect: sleeping %lds", elapsed);
125 } else if (elapsed < 100000) {
126 debug(DBG_INFO, "tlsconnect: sleeping %ds", 60);
129 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
131 if (server->sock >= 0)
133 if ((server->sock = connecttcphostlist(server->conf->hostports, srcres)) < 0)
136 SSL_free(server->ssl);
138 ctx = tlsgetctx(handle, server->conf->tlsconf);
141 server->ssl = SSL_new(ctx);
145 SSL_set_fd(server->ssl, server->sock);
146 if (SSL_connect(server->ssl) <= 0) {
147 while ((error = ERR_get_error()))
148 debug(DBG_ERR, "tlsconnect: TLS: %s", ERR_error_string(error, NULL));
151 cert = verifytlscert(server->ssl);
154 if (verifyconfcert(cert, server->conf)) {
160 debug(DBG_WARN, "tlsconnect: TLS connection to %s up", server->conf->name);
161 server->connectionok = 1;
162 gettimeofday(&server->lastconnecttry, NULL);
163 pthread_mutex_unlock(&server->lock);
167 /* timeout in seconds, 0 means no timeout (blocking), returns when num bytes have been read, or timeout */
168 /* returns 0 on timeout, -1 on error and num if ok */
169 int sslreadtimeout(SSL *ssl, unsigned char *buf, int num, int timeout) {
170 int s, ndesc, cnt, len;
171 fd_set readfds, writefds;
172 struct timeval timer;
177 /* make socket non-blocking? */
178 for (len = 0; len < num; len += cnt) {
183 timer.tv_sec = timeout;
186 ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
190 cnt = SSL_read(ssl, buf + len, num - len);
192 switch (SSL_get_error(ssl, cnt)) {
193 case SSL_ERROR_WANT_READ:
194 case SSL_ERROR_WANT_WRITE:
197 case SSL_ERROR_ZERO_RETURN:
198 /* remote end sent close_notify, send one back */
208 /* timeout in seconds, 0 means no timeout (blocking) */
209 unsigned char *radtlsget(SSL *ssl, int timeout) {
211 unsigned char buf[4], *rad;
214 cnt = sslreadtimeout(ssl, buf, 4, timeout);
216 debug(DBG_DBG, cnt ? "radtlsget: connection lost" : "radtlsget: timeout");
223 debug(DBG_ERR, "radtlsget: malloc failed");
228 cnt = sslreadtimeout(ssl, rad + 4, len - 4, timeout);
230 debug(DBG_DBG, cnt ? "radtlsget: connection lost" : "radtlsget: timeout");
239 debug(DBG_WARN, "radtlsget: packet smaller than minimum radius size");
242 debug(DBG_DBG, "radtlsget: got %d bytes", len);
246 int clientradputtls(struct server *server, unsigned char *rad) {
250 struct clsrvconf *conf = server->conf;
252 if (!server->connectionok)
255 if ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
256 while ((error = ERR_get_error()))
257 debug(DBG_ERR, "clientradputtls: TLS: %s", ERR_error_string(error, NULL));
261 debug(DBG_DBG, "clientradputtls: Sent %d bytes, Radius packet of length %d to TLS peer %s", cnt, len, conf->name);
265 void *tlsclientrd(void *arg) {
266 struct server *server = (struct server *)arg;
268 struct timeval now, lastconnecttry;
271 /* yes, lastconnecttry is really necessary */
272 lastconnecttry = server->lastconnecttry;
273 buf = radtlsget(server->ssl, server->dynamiclookuparg ? IDLE_TIMEOUT : 0);
275 if (server->dynamiclookuparg)
277 tlsconnect(server, &lastconnecttry, 0, "tlsclientrd");
283 if (server->dynamiclookuparg) {
284 gettimeofday(&now, NULL);
285 if (now.tv_sec - server->lastreply.tv_sec > IDLE_TIMEOUT) {
286 debug(DBG_INFO, "tlsclientrd: idle timeout for %s", server->conf->name);
292 server->clientrdgone = 1;
296 void *tlsserverwr(void *arg) {
299 struct client *client = (struct client *)arg;
300 struct gqueue *replyq;
301 struct request *reply;
303 debug(DBG_DBG, "tlsserverwr: starting for %s", addr2string(client->addr));
304 replyq = client->replyq;
306 pthread_mutex_lock(&replyq->mutex);
307 while (!list_first(replyq->entries)) {
309 debug(DBG_DBG, "tlsserverwr: waiting for signal");
310 pthread_cond_wait(&replyq->cond, &replyq->mutex);
311 debug(DBG_DBG, "tlsserverwr: got signal");
314 /* ssl might have changed while waiting */
315 pthread_mutex_unlock(&replyq->mutex);
316 debug(DBG_DBG, "tlsserverwr: exiting as requested");
321 reply = (struct request *)list_shift(replyq->entries);
322 pthread_mutex_unlock(&replyq->mutex);
323 cnt = SSL_write(client->ssl, reply->replybuf, RADLEN(reply->replybuf));
325 debug(DBG_DBG, "tlsserverwr: sent %d bytes, Radius packet of length %d to %s",
326 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
328 while ((error = ERR_get_error()))
329 debug(DBG_ERR, "tlsserverwr: SSL: %s", ERR_error_string(error, NULL));
334 void tlsserverrd(struct client *client) {
337 pthread_t tlsserverwrth;
339 debug(DBG_DBG, "tlsserverrd: starting for %s", addr2string(client->addr));
341 if (pthread_create(&tlsserverwrth, NULL, tlsserverwr, (void *)client)) {
342 debug(DBG_ERR, "tlsserverrd: pthread_create failed");
347 buf = radtlsget(client->ssl, 0);
349 debug(DBG_ERR, "tlsserverrd: connection from %s lost", addr2string(client->addr));
352 debug(DBG_DBG, "tlsserverrd: got Radius message from %s", addr2string(client->addr));
361 debug(DBG_ERR, "tlsserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
366 /* stop writer by setting ssl to NULL and give signal in case waiting for data */
368 pthread_mutex_lock(&client->replyq->mutex);
369 pthread_cond_signal(&client->replyq->cond);
370 pthread_mutex_unlock(&client->replyq->mutex);
371 debug(DBG_DBG, "tlsserverrd: waiting for writer to end");
372 pthread_join(tlsserverwrth, NULL);
373 debug(DBG_DBG, "tlsserverrd: reader for %s exiting", addr2string(client->addr));
376 void *tlsservernew(void *arg) {
378 struct sockaddr_storage from;
379 socklen_t fromlen = sizeof(from);
380 struct clsrvconf *conf;
381 struct list_node *cur = NULL;
386 struct client *client;
389 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
390 debug(DBG_DBG, "tlsservernew: getpeername failed, exiting");
393 debug(DBG_WARN, "tlsservernew: incoming TLS connection from %s", addr2string((struct sockaddr *)&from));
395 conf = find_clconf(handle, (struct sockaddr *)&from, &cur);
397 ctx = tlsgetctx(handle, conf->tlsconf);
405 if (SSL_accept(ssl) <= 0) {
406 while ((error = ERR_get_error()))
407 debug(DBG_ERR, "tlsservernew: SSL: %s", ERR_error_string(error, NULL));
408 debug(DBG_ERR, "tlsservernew: SSL_accept failed");
411 cert = verifytlscert(ssl);
417 if (verifyconfcert(cert, conf)) {
419 client = addclient(conf, 1);
422 client->addr = addr_copy((struct sockaddr *)&from);
424 removeclient(client);
426 debug(DBG_WARN, "tlsservernew: failed to create new client instance");
429 conf = find_clconf(handle, (struct sockaddr *)&from, &cur);
431 debug(DBG_WARN, "tlsservernew: ignoring request, no matching TLS client");
441 shutdown(s, SHUT_RDWR);
446 void *tlslistener(void *arg) {
447 pthread_t tlsserverth;
448 int s, *sp = (int *)arg;
449 struct sockaddr_storage from;
450 socklen_t fromlen = sizeof(from);
455 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
457 debug(DBG_WARN, "accept failed");
460 if (pthread_create(&tlsserverth, NULL, tlsservernew, (void *)&s)) {
461 debug(DBG_ERR, "tlslistener: pthread_create failed");
462 shutdown(s, SHUT_RDWR);
466 pthread_detach(tlsserverth);
472 const struct protodefs *tlsinit(uint8_t h) {
477 /* Local Variables: */
478 /* c-file-style: "stroustrup" */