1 /* Copyright (c) 2006-2010, UNINETT AS.
2 * Copyright (c) 2010, UNINETT AS, NORDUnet A/S.
3 * Copyright (c) 2010-2012, NORDUnet A/S. */
4 /* See LICENSE for licensing information. */
7 #include <sys/socket.h>
8 #include <netinet/in.h>
17 #include <sys/types.h>
18 #include <sys/select.h>
21 #include <arpa/inet.h>
24 #include <openssl/ssl.h>
25 #include <openssl/err.h>
26 #include "radsecproxy.h"
33 static void setprotoopts(struct commonprotoopts *opts);
34 static char **getlistenerargs();
35 void *tlslistener(void *arg);
36 int tlsconnect(struct server *server, struct timeval *when, int timeout, char *text);
37 void *tlsclientrd(void *arg);
38 int clientradputtls(struct server *server, unsigned char *rad);
41 static const struct protodefs protodefs = {
43 "radsec", /* secretdefault */
44 SOCK_STREAM, /* socktype */
45 "2083", /* portdefault */
46 0, /* retrycountdefault */
47 0, /* retrycountmax */
48 REQUEST_RETRY_INTERVAL * REQUEST_RETRY_COUNT, /* retryintervaldefault */
49 60, /* retryintervalmax */
50 DUPLICATE_INTERVAL, /* duplicateintervaldefault */
51 setprotoopts, /* setprotoopts */
52 getlistenerargs, /* getlistenerargs */
53 tlslistener, /* listener */
54 tlsconnect, /* connecter */
55 tlsclientrd, /* clientconnreader */
56 clientradputtls, /* clientradput */
58 NULL, /* addserverextra */
59 tlssetsrcres, /* setsrcres */
63 static struct addrinfo *srcres = NULL;
64 static uint8_t handle;
65 static struct commonprotoopts *protoopts = NULL;
67 const struct protodefs *tlsinit(uint8_t h) {
72 static void setprotoopts(struct commonprotoopts *opts) {
76 static char **getlistenerargs() {
77 return protoopts ? protoopts->listenargs : NULL;
83 resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL,
84 AF_UNSPEC, NULL, protodefs.socktype);
87 int tlsconnect(struct server *server, struct timeval *when, int timeout, char *text) {
94 debug(DBG_DBG, "tlsconnect: called from %s", text);
95 pthread_mutex_lock(&server->lock);
96 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
97 /* already reconnected, nothing to do */
98 debug(DBG_DBG, "tlsconnect(%s): seems already reconnected", text);
99 pthread_mutex_unlock(&server->lock);
104 gettimeofday(&now, NULL);
105 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
106 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
107 debug(DBG_DBG, "tlsconnect: timeout");
108 if (server->sock >= 0)
110 SSL_free(server->ssl);
112 pthread_mutex_unlock(&server->lock);
115 if (server->connectionok) {
116 server->connectionok = 0;
118 } else if (elapsed < 1)
120 else if (elapsed < 60) {
121 debug(DBG_INFO, "tlsconnect: sleeping %lds", elapsed);
123 } else if (elapsed < 100000) {
124 debug(DBG_INFO, "tlsconnect: sleeping %ds", 60);
127 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
129 if (server->sock >= 0)
131 if ((server->sock = connecttcphostlist(server->conf->hostports, srcres)) < 0)
134 SSL_free(server->ssl);
136 ctx = tlsgetctx(handle, server->conf->tlsconf);
139 server->ssl = SSL_new(ctx);
143 SSL_set_fd(server->ssl, server->sock);
144 if (SSL_connect(server->ssl) <= 0) {
145 while ((error = ERR_get_error()))
146 debug(DBG_ERR, "tlsconnect: TLS: %s", ERR_error_string(error, NULL));
149 cert = verifytlscert(server->ssl);
152 if (verifyconfcert(cert, server->conf)) {
158 debug(DBG_WARN, "tlsconnect: TLS connection to %s up", server->conf->name);
159 server->connectionok = 1;
160 gettimeofday(&server->lastconnecttry, NULL);
161 pthread_mutex_unlock(&server->lock);
165 /* timeout in seconds, 0 means no timeout (blocking), returns when num bytes have been read, or timeout */
166 /* returns 0 on timeout, -1 on error and num if ok */
167 int sslreadtimeout(SSL *ssl, unsigned char *buf, int num, int timeout) {
168 int s, ndesc, cnt, len;
169 fd_set readfds, writefds;
170 struct timeval timer;
175 /* make socket non-blocking? */
176 for (len = 0; len < num; len += cnt) {
181 timer.tv_sec = timeout;
184 ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
188 cnt = SSL_read(ssl, buf + len, num - len);
190 switch (SSL_get_error(ssl, cnt)) {
191 case SSL_ERROR_WANT_READ:
192 case SSL_ERROR_WANT_WRITE:
195 case SSL_ERROR_ZERO_RETURN:
196 /* remote end sent close_notify, send one back */
206 /* timeout in seconds, 0 means no timeout (blocking) */
207 unsigned char *radtlsget(SSL *ssl, int timeout) {
209 unsigned char buf[4], *rad;
212 cnt = sslreadtimeout(ssl, buf, 4, timeout);
214 debug(DBG_DBG, cnt ? "radtlsget: connection lost" : "radtlsget: timeout");
221 debug(DBG_ERR, "radtlsget: malloc failed");
226 cnt = sslreadtimeout(ssl, rad + 4, len - 4, timeout);
228 debug(DBG_DBG, cnt ? "radtlsget: connection lost" : "radtlsget: timeout");
237 debug(DBG_WARN, "radtlsget: packet smaller than minimum radius size");
240 debug(DBG_DBG, "radtlsget: got %d bytes", len);
244 int clientradputtls(struct server *server, unsigned char *rad) {
248 struct clsrvconf *conf = server->conf;
250 if (!server->connectionok)
253 if ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
254 while ((error = ERR_get_error()))
255 debug(DBG_ERR, "clientradputtls: TLS: %s", ERR_error_string(error, NULL));
259 debug(DBG_DBG, "clientradputtls: Sent %d bytes, Radius packet of length %d to TLS peer %s", cnt, len, conf->name);
263 void *tlsclientrd(void *arg) {
264 struct server *server = (struct server *)arg;
266 struct timeval now, lastconnecttry;
269 /* yes, lastconnecttry is really necessary */
270 lastconnecttry = server->lastconnecttry;
271 buf = radtlsget(server->ssl, server->dynamiclookuparg ? IDLE_TIMEOUT : 0);
273 if (server->dynamiclookuparg)
275 tlsconnect(server, &lastconnecttry, 0, "tlsclientrd");
281 if (server->dynamiclookuparg) {
282 gettimeofday(&now, NULL);
283 if (now.tv_sec - server->lastreply.tv_sec > IDLE_TIMEOUT) {
284 debug(DBG_INFO, "tlsclientrd: idle timeout for %s", server->conf->name);
290 server->clientrdgone = 1;
294 void *tlsserverwr(void *arg) {
297 struct client *client = (struct client *)arg;
298 struct gqueue *replyq;
299 struct request *reply;
301 debug(DBG_DBG, "tlsserverwr: starting for %s", addr2string(client->addr));
302 replyq = client->replyq;
304 pthread_mutex_lock(&replyq->mutex);
305 while (!list_first(replyq->entries)) {
307 debug(DBG_DBG, "tlsserverwr: waiting for signal");
308 pthread_cond_wait(&replyq->cond, &replyq->mutex);
309 debug(DBG_DBG, "tlsserverwr: got signal");
312 /* ssl might have changed while waiting */
313 pthread_mutex_unlock(&replyq->mutex);
314 debug(DBG_DBG, "tlsserverwr: exiting as requested");
319 reply = (struct request *)list_shift(replyq->entries);
320 pthread_mutex_unlock(&replyq->mutex);
321 cnt = SSL_write(client->ssl, reply->replybuf, RADLEN(reply->replybuf));
323 debug(DBG_DBG, "tlsserverwr: sent %d bytes, Radius packet of length %d to %s",
324 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
326 while ((error = ERR_get_error()))
327 debug(DBG_ERR, "tlsserverwr: SSL: %s", ERR_error_string(error, NULL));
332 void tlsserverrd(struct client *client) {
335 pthread_t tlsserverwrth;
337 debug(DBG_DBG, "tlsserverrd: starting for %s", addr2string(client->addr));
339 if (pthread_create(&tlsserverwrth, NULL, tlsserverwr, (void *)client)) {
340 debug(DBG_ERR, "tlsserverrd: pthread_create failed");
345 buf = radtlsget(client->ssl, 0);
347 debug(DBG_ERR, "tlsserverrd: connection from %s lost", addr2string(client->addr));
350 debug(DBG_DBG, "tlsserverrd: got Radius message from %s", addr2string(client->addr));
359 debug(DBG_ERR, "tlsserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
364 /* stop writer by setting ssl to NULL and give signal in case waiting for data */
366 pthread_mutex_lock(&client->replyq->mutex);
367 pthread_cond_signal(&client->replyq->cond);
368 pthread_mutex_unlock(&client->replyq->mutex);
369 debug(DBG_DBG, "tlsserverrd: waiting for writer to end");
370 pthread_join(tlsserverwrth, NULL);
371 debug(DBG_DBG, "tlsserverrd: reader for %s exiting", addr2string(client->addr));
374 void *tlsservernew(void *arg) {
376 struct sockaddr_storage from;
377 socklen_t fromlen = sizeof(from);
378 struct clsrvconf *conf;
379 struct list_node *cur = NULL;
384 struct client *client;
385 struct tls *accepted_tls = NULL;
388 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
389 debug(DBG_DBG, "tlsservernew: getpeername failed, exiting");
392 debug(DBG_WARN, "tlsservernew: incoming TLS connection from %s", addr2string((struct sockaddr *)&from));
394 conf = find_clconf(handle, (struct sockaddr *)&from, &cur);
396 ctx = tlsgetctx(handle, conf->tlsconf);
404 if (SSL_accept(ssl) <= 0) {
405 while ((error = ERR_get_error()))
406 debug(DBG_ERR, "tlsservernew: SSL: %s", ERR_error_string(error, NULL));
407 debug(DBG_ERR, "tlsservernew: SSL_accept failed");
410 cert = verifytlscert(ssl);
413 accepted_tls = conf->tlsconf;
417 if (accepted_tls == conf->tlsconf && 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" */