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.
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>
32 #include "radsecproxy.h"
34 void *tlslistener(void *arg);
35 int tlsconnect(struct server *server, struct timeval *when, int timeout, char *text);
36 void *tlsclientrd(void *arg);
37 int clientradputtls(struct server *server, unsigned char *rad);
38 void tlssetsrcres(char *source);
40 static const struct protodefs protodefs = {
42 "mysecret", /* secretdefault */
43 SOCK_STREAM, /* socktype */
44 "2083", /* portdefault */
45 0, /* retrycountdefault */
46 0, /* retrycountmax */
47 REQUEST_RETRY_INTERVAL * REQUEST_RETRY_COUNT, /* retryintervaldefault */
48 60, /* retryintervalmax */
49 DUPLICATE_INTERVAL, /* duplicateintervaldefault */
50 tlslistener, /* listener */
51 tlsconnect, /* connecter */
52 tlsclientrd, /* clientconnreader */
53 clientradputtls, /* clientradput */
55 NULL, /* addserverextra */
56 tlssetsrcres, /* setsrcres */
60 static struct addrinfo *srcres = NULL;
61 static uint8_t handle;
63 const struct protodefs *tlsinit(uint8_t h) {
68 void tlssetsrcres(char *source) {
70 srcres = resolve_hostport_addrinfo(handle, source);
73 int tlsconnect(struct server *server, struct timeval *when, int timeout, char *text) {
80 debug(DBG_DBG, "tlsconnect: called from %s", text);
81 pthread_mutex_lock(&server->lock);
82 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
83 /* already reconnected, nothing to do */
84 debug(DBG_DBG, "tlsconnect(%s): seems already reconnected", text);
85 pthread_mutex_unlock(&server->lock);
90 gettimeofday(&now, NULL);
91 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
92 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
93 debug(DBG_DBG, "tlsconnect: timeout");
94 if (server->sock >= 0)
96 SSL_free(server->ssl);
98 pthread_mutex_unlock(&server->lock);
101 if (server->connectionok) {
102 server->connectionok = 0;
104 } else if (elapsed < 1)
106 else if (elapsed < 60) {
107 debug(DBG_INFO, "tlsconnect: sleeping %lds", elapsed);
109 } else if (elapsed < 100000) {
110 debug(DBG_INFO, "tlsconnect: sleeping %ds", 60);
113 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
114 debug(DBG_WARN, "tlsconnect: trying to open TLS connection to %s port %s", server->conf->host, server->conf->port);
115 if (server->sock >= 0)
117 if ((server->sock = connecttcp(server->conf->addrinfo, srcres)) < 0) {
118 debug(DBG_ERR, "tlsconnect: connecttcp failed");
122 SSL_free(server->ssl);
124 ctx = tlsgetctx(handle, server->conf->tlsconf);
127 server->ssl = SSL_new(ctx);
131 SSL_set_fd(server->ssl, server->sock);
132 if (SSL_connect(server->ssl) <= 0) {
133 while ((error = ERR_get_error()))
134 debug(DBG_ERR, "tlsconnect: TLS: %s", ERR_error_string(error, NULL));
137 cert = verifytlscert(server->ssl);
140 if (verifyconfcert(cert, server->conf)) {
146 debug(DBG_WARN, "tlsconnect: TLS connection to %s port %s up", server->conf->host, server->conf->port);
147 server->connectionok = 1;
148 gettimeofday(&server->lastconnecttry, NULL);
149 pthread_mutex_unlock(&server->lock);
153 /* timeout in seconds, 0 means no timeout (blocking), returns when num bytes have been read, or timeout */
154 /* returns 0 on timeout, -1 on error and num if ok */
155 int sslreadtimeout(SSL *ssl, unsigned char *buf, int num, int timeout) {
156 int s, ndesc, cnt, len;
157 fd_set readfds, writefds;
158 struct timeval timer;
163 /* make socket non-blocking? */
164 for (len = 0; len < num; len += cnt) {
169 timer.tv_sec = timeout;
172 ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
176 cnt = SSL_read(ssl, buf + len, num - len);
178 switch (SSL_get_error(ssl, cnt)) {
179 case SSL_ERROR_WANT_READ:
180 case SSL_ERROR_WANT_WRITE:
183 case SSL_ERROR_ZERO_RETURN:
184 /* remote end sent close_notify, send one back */
194 /* timeout in seconds, 0 means no timeout (blocking) */
195 unsigned char *radtlsget(SSL *ssl, int timeout) {
197 unsigned char buf[4], *rad;
200 cnt = sslreadtimeout(ssl, buf, 4, timeout);
202 debug(DBG_DBG, cnt ? "radtlsget: connection lost" : "radtlsget: timeout");
209 debug(DBG_ERR, "radtlsget: malloc failed");
214 cnt = sslreadtimeout(ssl, rad + 4, len - 4, timeout);
216 debug(DBG_DBG, cnt ? "radtlsget: connection lost" : "radtlsget: timeout");
225 debug(DBG_WARN, "radtlsget: packet smaller than minimum radius size");
228 debug(DBG_DBG, "radtlsget: got %d bytes", len);
232 int clientradputtls(struct server *server, unsigned char *rad) {
236 struct clsrvconf *conf = server->conf;
238 if (!server->connectionok)
241 if ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
242 while ((error = ERR_get_error()))
243 debug(DBG_ERR, "clientradputtls: TLS: %s", ERR_error_string(error, NULL));
247 debug(DBG_DBG, "clientradputtls: Sent %d bytes, Radius packet of length %d to TLS peer %s", cnt, len, conf->host);
251 void *tlsclientrd(void *arg) {
252 struct server *server = (struct server *)arg;
254 struct timeval now, lastconnecttry;
257 /* yes, lastconnecttry is really necessary */
258 lastconnecttry = server->lastconnecttry;
259 buf = radtlsget(server->ssl, server->dynamiclookuparg ? IDLE_TIMEOUT : 0);
261 if (server->dynamiclookuparg)
263 tlsconnect(server, &lastconnecttry, 0, "tlsclientrd");
269 if (server->dynamiclookuparg) {
270 gettimeofday(&now, NULL);
271 if (now.tv_sec - server->lastreply.tv_sec > IDLE_TIMEOUT) {
272 debug(DBG_INFO, "tlsclientrd: idle timeout for %s", server->conf->name);
278 server->clientrdgone = 1;
282 void *tlsserverwr(void *arg) {
285 struct client *client = (struct client *)arg;
286 struct queue *replyq;
287 struct request *reply;
289 debug(DBG_DBG, "tlsserverwr: starting for %s", addr2string(client->addr));
290 replyq = client->replyq;
292 pthread_mutex_lock(&replyq->mutex);
293 while (!list_first(replyq->entries)) {
295 debug(DBG_DBG, "tlsserverwr: waiting for signal");
296 pthread_cond_wait(&replyq->cond, &replyq->mutex);
297 debug(DBG_DBG, "tlsserverwr: got signal");
300 /* ssl might have changed while waiting */
301 pthread_mutex_unlock(&replyq->mutex);
302 debug(DBG_DBG, "tlsserverwr: exiting as requested");
307 reply = (struct request *)list_shift(replyq->entries);
308 pthread_mutex_unlock(&replyq->mutex);
309 cnt = SSL_write(client->ssl, reply->replybuf, RADLEN(reply->replybuf));
311 debug(DBG_DBG, "tlsserverwr: sent %d bytes, Radius packet of length %d to %s",
312 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
314 while ((error = ERR_get_error()))
315 debug(DBG_ERR, "tlsserverwr: SSL: %s", ERR_error_string(error, NULL));
320 void tlsserverrd(struct client *client) {
323 pthread_t tlsserverwrth;
325 debug(DBG_DBG, "tlsserverrd: starting for %s", addr2string(client->addr));
327 if (pthread_create(&tlsserverwrth, NULL, tlsserverwr, (void *)client)) {
328 debug(DBG_ERR, "tlsserverrd: pthread_create failed");
333 buf = radtlsget(client->ssl, 0);
335 debug(DBG_ERR, "tlsserverrd: connection from %s lost", addr2string(client->addr));
338 debug(DBG_DBG, "tlsserverrd: got Radius message from %s", addr2string(client->addr));
347 debug(DBG_ERR, "tlsserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
352 /* stop writer by setting ssl to NULL and give signal in case waiting for data */
354 pthread_mutex_lock(&client->replyq->mutex);
355 pthread_cond_signal(&client->replyq->cond);
356 pthread_mutex_unlock(&client->replyq->mutex);
357 debug(DBG_DBG, "tlsserverrd: waiting for writer to end");
358 pthread_join(tlsserverwrth, NULL);
359 debug(DBG_DBG, "tlsserverrd: reader for %s exiting", addr2string(client->addr));
362 void *tlsservernew(void *arg) {
364 struct sockaddr_storage from;
365 socklen_t fromlen = sizeof(from);
366 struct clsrvconf *conf;
367 struct list_node *cur = NULL;
372 struct client *client;
375 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
376 debug(DBG_DBG, "tlsservernew: getpeername failed, exiting");
379 debug(DBG_WARN, "tlsservernew: incoming TLS connection from %s", addr2string((struct sockaddr *)&from));
381 conf = find_clconf(handle, (struct sockaddr *)&from, &cur);
383 ctx = tlsgetctx(handle, conf->tlsconf);
391 if (SSL_accept(ssl) <= 0) {
392 while ((error = ERR_get_error()))
393 debug(DBG_ERR, "tlsservernew: SSL: %s", ERR_error_string(error, NULL));
394 debug(DBG_ERR, "tlsservernew: SSL_accept failed");
397 cert = verifytlscert(ssl);
403 if (verifyconfcert(cert, conf)) {
405 client = addclient(conf, 1);
408 client->addr = addr_copy((struct sockaddr *)&from);
410 removeclient(client);
412 debug(DBG_WARN, "tlsservernew: failed to create new client instance");
415 conf = find_clconf(handle, (struct sockaddr *)&from, &cur);
417 debug(DBG_WARN, "tlsservernew: ignoring request, no matching TLS client");
427 shutdown(s, SHUT_RDWR);
432 void *tlslistener(void *arg) {
433 pthread_t tlsserverth;
434 int s, *sp = (int *)arg;
435 struct sockaddr_storage from;
436 socklen_t fromlen = sizeof(from);
441 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
443 debug(DBG_WARN, "accept failed");
446 if (pthread_create(&tlsserverth, NULL, tlsservernew, (void *)&s)) {
447 debug(DBG_ERR, "tlslistener: pthread_create failed");
448 shutdown(s, SHUT_RDWR);
452 pthread_detach(tlsserverth);