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>
29 #include "radsecproxy.h"
36 static void setprotoopts(struct commonprotoopts *opts);
37 static char **getlistenerargs();
38 void *tlslistener(void *arg);
39 int tlsconnect(struct server *server, struct timeval *when, int timeout, char *text);
40 void *tlsclientrd(void *arg);
41 int clientradputtls(struct server *server, unsigned char *rad);
44 static const struct protodefs protodefs = {
46 "mysecret", /* secretdefault */
47 SOCK_STREAM, /* socktype */
48 "2083", /* portdefault */
49 0, /* retrycountdefault */
50 0, /* retrycountmax */
51 REQUEST_RETRY_INTERVAL * REQUEST_RETRY_COUNT, /* retryintervaldefault */
52 60, /* retryintervalmax */
53 DUPLICATE_INTERVAL, /* duplicateintervaldefault */
54 setprotoopts, /* setprotoopts */
55 getlistenerargs, /* getlistenerargs */
56 tlslistener, /* listener */
57 tlsconnect, /* connecter */
58 tlsclientrd, /* clientconnreader */
59 clientradputtls, /* clientradput */
61 NULL, /* addserverextra */
62 tlssetsrcres, /* setsrcres */
66 static struct addrinfo *srcres = NULL;
67 static uint8_t handle;
68 static struct commonprotoopts *protoopts = NULL;
70 const struct protodefs *tlsinit(uint8_t h) {
75 static void setprotoopts(struct commonprotoopts *opts) {
79 static char **getlistenerargs() {
80 return protoopts ? protoopts->listenargs : NULL;
85 srcres = resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL, NULL, protodefs.socktype);
88 int tlsconnect(struct server *server, struct timeval *when, int timeout, char *text) {
95 debug(DBG_DBG, "tlsconnect: called from %s", text);
96 pthread_mutex_lock(&server->lock);
97 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
98 /* already reconnected, nothing to do */
99 debug(DBG_DBG, "tlsconnect(%s): seems already reconnected", text);
100 pthread_mutex_unlock(&server->lock);
105 gettimeofday(&now, NULL);
106 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
107 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
108 debug(DBG_DBG, "tlsconnect: timeout");
109 if (server->sock >= 0)
111 SSL_free(server->ssl);
113 pthread_mutex_unlock(&server->lock);
116 if (server->connectionok) {
117 server->connectionok = 0;
119 } else if (elapsed < 1)
121 else if (elapsed < 60) {
122 debug(DBG_INFO, "tlsconnect: sleeping %lds", elapsed);
124 } else if (elapsed < 100000) {
125 debug(DBG_INFO, "tlsconnect: sleeping %ds", 60);
128 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
130 if (server->sock >= 0)
132 if ((server->sock = connecttcphostlist(server->conf->hostports, srcres)) < 0)
135 SSL_free(server->ssl);
137 ctx = tlsgetctx(handle, server->conf->tlsconf);
140 server->ssl = SSL_new(ctx);
144 SSL_set_fd(server->ssl, server->sock);
145 if (SSL_connect(server->ssl) <= 0) {
146 while ((error = ERR_get_error()))
147 debug(DBG_ERR, "tlsconnect: TLS: %s", ERR_error_string(error, NULL));
150 cert = verifytlscert(server->ssl);
153 if (verifyconfcert(cert, server->conf)) {
159 debug(DBG_WARN, "tlsconnect: TLS connection to %s up", server->conf->name);
160 server->connectionok = 1;
161 gettimeofday(&server->lastconnecttry, NULL);
162 pthread_mutex_unlock(&server->lock);
166 /* timeout in seconds, 0 means no timeout (blocking), returns when num bytes have been read, or timeout */
167 /* returns 0 on timeout, -1 on error and num if ok */
168 int sslreadtimeout(SSL *ssl, unsigned char *buf, int num, int timeout) {
169 int s, ndesc, cnt, len;
170 fd_set readfds, writefds;
171 struct timeval timer;
176 /* make socket non-blocking? */
177 for (len = 0; len < num; len += cnt) {
182 timer.tv_sec = timeout;
185 ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
189 cnt = SSL_read(ssl, buf + len, num - len);
191 switch (SSL_get_error(ssl, cnt)) {
192 case SSL_ERROR_WANT_READ:
193 case SSL_ERROR_WANT_WRITE:
196 case SSL_ERROR_ZERO_RETURN:
197 /* remote end sent close_notify, send one back */
207 /* timeout in seconds, 0 means no timeout (blocking) */
208 unsigned char *radtlsget(SSL *ssl, int timeout) {
210 unsigned char buf[4], *rad;
213 cnt = sslreadtimeout(ssl, buf, 4, timeout);
215 debug(DBG_DBG, cnt ? "radtlsget: connection lost" : "radtlsget: timeout");
222 debug(DBG_ERR, "radtlsget: malloc failed");
227 cnt = sslreadtimeout(ssl, rad + 4, len - 4, timeout);
229 debug(DBG_DBG, cnt ? "radtlsget: connection lost" : "radtlsget: timeout");
238 debug(DBG_WARN, "radtlsget: packet smaller than minimum radius size");
241 debug(DBG_DBG, "radtlsget: got %d bytes", len);
245 int clientradputtls(struct server *server, unsigned char *rad) {
249 struct clsrvconf *conf = server->conf;
251 if (!server->connectionok)
254 if ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
255 while ((error = ERR_get_error()))
256 debug(DBG_ERR, "clientradputtls: TLS: %s", ERR_error_string(error, NULL));
260 debug(DBG_DBG, "clientradputtls: Sent %d bytes, Radius packet of length %d to TLS peer %s", cnt, len, conf->name);
264 void *tlsclientrd(void *arg) {
265 struct server *server = (struct server *)arg;
267 struct timeval now, lastconnecttry;
270 /* yes, lastconnecttry is really necessary */
271 lastconnecttry = server->lastconnecttry;
272 buf = radtlsget(server->ssl, server->dynamiclookuparg ? IDLE_TIMEOUT : 0);
274 if (server->dynamiclookuparg)
276 tlsconnect(server, &lastconnecttry, 0, "tlsclientrd");
282 if (server->dynamiclookuparg) {
283 gettimeofday(&now, NULL);
284 if (now.tv_sec - server->lastreply.tv_sec > IDLE_TIMEOUT) {
285 debug(DBG_INFO, "tlsclientrd: idle timeout for %s", server->conf->name);
291 server->clientrdgone = 1;
295 void *tlsserverwr(void *arg) {
298 struct client *client = (struct client *)arg;
299 struct gqueue *replyq;
300 struct request *reply;
302 debug(DBG_DBG, "tlsserverwr: starting for %s", addr2string(client->addr));
303 replyq = client->replyq;
305 pthread_mutex_lock(&replyq->mutex);
306 while (!list_first(replyq->entries)) {
308 debug(DBG_DBG, "tlsserverwr: waiting for signal");
309 pthread_cond_wait(&replyq->cond, &replyq->mutex);
310 debug(DBG_DBG, "tlsserverwr: got signal");
313 /* ssl might have changed while waiting */
314 pthread_mutex_unlock(&replyq->mutex);
315 debug(DBG_DBG, "tlsserverwr: exiting as requested");
320 reply = (struct request *)list_shift(replyq->entries);
321 pthread_mutex_unlock(&replyq->mutex);
322 cnt = SSL_write(client->ssl, reply->replybuf, RADLEN(reply->replybuf));
324 debug(DBG_DBG, "tlsserverwr: sent %d bytes, Radius packet of length %d to %s",
325 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
327 while ((error = ERR_get_error()))
328 debug(DBG_ERR, "tlsserverwr: SSL: %s", ERR_error_string(error, NULL));
333 void tlsserverrd(struct client *client) {
336 pthread_t tlsserverwrth;
338 debug(DBG_DBG, "tlsserverrd: starting for %s", addr2string(client->addr));
340 if (pthread_create(&tlsserverwrth, NULL, tlsserverwr, (void *)client)) {
341 debug(DBG_ERR, "tlsserverrd: pthread_create failed");
346 buf = radtlsget(client->ssl, 0);
348 debug(DBG_ERR, "tlsserverrd: connection from %s lost", addr2string(client->addr));
351 debug(DBG_DBG, "tlsserverrd: got Radius message from %s", addr2string(client->addr));
360 debug(DBG_ERR, "tlsserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
365 /* stop writer by setting ssl to NULL and give signal in case waiting for data */
367 pthread_mutex_lock(&client->replyq->mutex);
368 pthread_cond_signal(&client->replyq->cond);
369 pthread_mutex_unlock(&client->replyq->mutex);
370 debug(DBG_DBG, "tlsserverrd: waiting for writer to end");
371 pthread_join(tlsserverwrth, NULL);
372 debug(DBG_DBG, "tlsserverrd: reader for %s exiting", addr2string(client->addr));
375 void *tlsservernew(void *arg) {
377 struct sockaddr_storage from;
378 socklen_t fromlen = sizeof(from);
379 struct clsrvconf *conf;
380 struct list_node *cur = NULL;
385 struct client *client;
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);
416 if (verifyconfcert(cert, conf)) {
418 client = addclient(conf, 1);
421 client->addr = addr_copy((struct sockaddr *)&from);
423 removeclient(client);
425 debug(DBG_WARN, "tlsservernew: failed to create new client instance");
428 conf = find_clconf(handle, (struct sockaddr *)&from, &cur);
430 debug(DBG_WARN, "tlsservernew: ignoring request, no matching TLS client");
440 shutdown(s, SHUT_RDWR);
445 void *tlslistener(void *arg) {
446 pthread_t tlsserverth;
447 int s, *sp = (int *)arg;
448 struct sockaddr_storage from;
449 socklen_t fromlen = sizeof(from);
454 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
456 debug(DBG_WARN, "accept failed");
459 if (pthread_create(&tlsserverth, NULL, tlsservernew, (void *)&s)) {
460 debug(DBG_ERR, "tlslistener: pthread_create failed");
461 shutdown(s, SHUT_RDWR);
465 pthread_detach(tlsserverth);
471 const struct protodefs *tlsinit(uint8_t h) {
476 /* Local Variables: */
477 /* c-file-style: "stroustrup" */