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 "radsec", /* 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;
86 resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL,
87 AF_UNSPEC, NULL, protodefs.socktype);
90 int tlsconnect(struct server *server, struct timeval *when, int timeout, char *text) {
97 debug(DBG_DBG, "tlsconnect: called from %s", text);
98 pthread_mutex_lock(&server->lock);
99 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
100 /* already reconnected, nothing to do */
101 debug(DBG_DBG, "tlsconnect(%s): seems already reconnected", text);
102 pthread_mutex_unlock(&server->lock);
107 gettimeofday(&now, NULL);
108 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
109 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
110 debug(DBG_DBG, "tlsconnect: timeout");
111 if (server->sock >= 0)
113 SSL_free(server->ssl);
115 pthread_mutex_unlock(&server->lock);
118 if (server->connectionok) {
119 server->connectionok = 0;
121 } else if (elapsed < 1)
123 else if (elapsed < 60) {
124 debug(DBG_INFO, "tlsconnect: sleeping %lds", elapsed);
126 } else if (elapsed < 100000) {
127 debug(DBG_INFO, "tlsconnect: sleeping %ds", 60);
130 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
132 if (server->sock >= 0)
134 if ((server->sock = connecttcphostlist(server->conf->hostports, srcres)) < 0)
137 SSL_free(server->ssl);
139 ctx = tlsgetctx(handle, server->conf->tlsconf);
142 server->ssl = SSL_new(ctx);
146 SSL_set_fd(server->ssl, server->sock);
147 if (SSL_connect(server->ssl) <= 0) {
148 while ((error = ERR_get_error()))
149 debug(DBG_ERR, "tlsconnect: TLS: %s", ERR_error_string(error, NULL));
152 cert = verifytlscert(server->ssl);
155 if (verifyconfcert(cert, server->conf)) {
161 debug(DBG_WARN, "tlsconnect: TLS connection to %s up", server->conf->name);
162 server->connectionok = 1;
163 gettimeofday(&server->lastconnecttry, NULL);
164 pthread_mutex_unlock(&server->lock);
168 /* timeout in seconds, 0 means no timeout (blocking), returns when num bytes have been read, or timeout */
169 /* returns 0 on timeout, -1 on error and num if ok */
170 int sslreadtimeout(SSL *ssl, unsigned char *buf, int num, int timeout) {
171 int s, ndesc, cnt, len;
173 struct timeval timer;
178 /* make socket non-blocking? */
179 for (len = 0; len < num; len += cnt) {
180 if (SSL_pending(ssl) == 0) {
184 timer.tv_sec = timeout;
187 ndesc = select(s + 1, &readfds, NULL, NULL, timeout ? &timer : NULL);
192 cnt = SSL_read(ssl, buf + len, num - len);
194 switch (SSL_get_error(ssl, cnt)) {
195 case SSL_ERROR_WANT_READ:
196 case SSL_ERROR_WANT_WRITE:
199 case SSL_ERROR_ZERO_RETURN:
200 /* remote end sent close_notify, send one back */
210 /* timeout in seconds, 0 means no timeout (blocking) */
211 unsigned char *radtlsget(SSL *ssl, int timeout) {
213 unsigned char buf[4], *rad;
216 cnt = sslreadtimeout(ssl, buf, 4, timeout);
218 debug(DBG_DBG, cnt ? "radtlsget: connection lost" : "radtlsget: timeout");
225 debug(DBG_ERR, "radtlsget: malloc failed");
230 cnt = sslreadtimeout(ssl, rad + 4, len - 4, timeout);
232 debug(DBG_DBG, cnt ? "radtlsget: connection lost" : "radtlsget: timeout");
241 debug(DBG_WARN, "radtlsget: packet smaller than minimum radius size");
244 debug(DBG_DBG, "radtlsget: got %d bytes", len);
248 int clientradputtls(struct server *server, unsigned char *rad) {
252 struct clsrvconf *conf = server->conf;
254 if (!server->connectionok)
257 if ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
258 while ((error = ERR_get_error()))
259 debug(DBG_ERR, "clientradputtls: TLS: %s", ERR_error_string(error, NULL));
263 debug(DBG_DBG, "clientradputtls: Sent %d bytes, Radius packet of length %d to TLS peer %s", cnt, len, conf->name);
267 void *tlsclientrd(void *arg) {
268 struct server *server = (struct server *)arg;
270 struct timeval now, lastconnecttry;
273 /* yes, lastconnecttry is really necessary */
274 lastconnecttry = server->lastconnecttry;
275 buf = radtlsget(server->ssl, server->dynamiclookuparg ? IDLE_TIMEOUT : 0);
277 if (server->dynamiclookuparg)
279 tlsconnect(server, &lastconnecttry, 0, "tlsclientrd");
285 if (server->dynamiclookuparg) {
286 gettimeofday(&now, NULL);
287 if (now.tv_sec - server->lastreply.tv_sec > IDLE_TIMEOUT) {
288 debug(DBG_INFO, "tlsclientrd: idle timeout for %s", server->conf->name);
294 server->clientrdgone = 1;
298 void *tlsserverwr(void *arg) {
301 struct client *client = (struct client *)arg;
302 struct gqueue *replyq;
303 struct request *reply;
305 debug(DBG_DBG, "tlsserverwr: starting for %s", addr2string(client->addr));
306 replyq = client->replyq;
308 pthread_mutex_lock(&replyq->mutex);
309 while (!list_first(replyq->entries)) {
311 debug(DBG_DBG, "tlsserverwr: waiting for signal");
312 pthread_cond_wait(&replyq->cond, &replyq->mutex);
313 debug(DBG_DBG, "tlsserverwr: got signal");
316 /* ssl might have changed while waiting */
317 pthread_mutex_unlock(&replyq->mutex);
318 debug(DBG_DBG, "tlsserverwr: exiting as requested");
323 reply = (struct request *)list_shift(replyq->entries);
324 pthread_mutex_unlock(&replyq->mutex);
325 cnt = SSL_write(client->ssl, reply->replybuf, RADLEN(reply->replybuf));
327 debug(DBG_DBG, "tlsserverwr: sent %d bytes, Radius packet of length %d to %s",
328 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
330 while ((error = ERR_get_error()))
331 debug(DBG_ERR, "tlsserverwr: SSL: %s", ERR_error_string(error, NULL));
336 void tlsserverrd(struct client *client) {
339 pthread_t tlsserverwrth;
341 debug(DBG_DBG, "tlsserverrd: starting for %s", addr2string(client->addr));
343 if (pthread_create(&tlsserverwrth, &pthread_attr, tlsserverwr, (void *)client)) {
344 debug(DBG_ERR, "tlsserverrd: pthread_create failed");
349 buf = radtlsget(client->ssl, 0);
351 debug(DBG_ERR, "tlsserverrd: connection from %s lost", addr2string(client->addr));
354 debug(DBG_DBG, "tlsserverrd: got Radius message from %s", addr2string(client->addr));
363 debug(DBG_ERR, "tlsserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
368 /* stop writer by setting ssl to NULL and give signal in case waiting for data */
370 pthread_mutex_lock(&client->replyq->mutex);
371 pthread_cond_signal(&client->replyq->cond);
372 pthread_mutex_unlock(&client->replyq->mutex);
373 debug(DBG_DBG, "tlsserverrd: waiting for writer to end");
374 pthread_join(tlsserverwrth, NULL);
375 debug(DBG_DBG, "tlsserverrd: reader for %s exiting", addr2string(client->addr));
378 void *tlsservernew(void *arg) {
380 struct sockaddr_storage from;
381 socklen_t fromlen = sizeof(from);
382 struct clsrvconf *conf;
383 struct list_node *cur = NULL;
388 struct client *client;
389 struct tls *accepted_tls = NULL;
392 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
393 debug(DBG_DBG, "tlsservernew: getpeername failed, exiting");
396 debug(DBG_WARN, "tlsservernew: incoming TLS connection from %s", addr2string((struct sockaddr *)&from));
398 conf = find_clconf(handle, (struct sockaddr *)&from, &cur);
400 ctx = tlsgetctx(handle, conf->tlsconf);
408 if (SSL_accept(ssl) <= 0) {
409 while ((error = ERR_get_error()))
410 debug(DBG_ERR, "tlsservernew: SSL: %s", ERR_error_string(error, NULL));
411 debug(DBG_ERR, "tlsservernew: SSL_accept failed");
414 cert = verifytlscert(ssl);
417 accepted_tls = conf->tlsconf;
421 if (accepted_tls == conf->tlsconf && verifyconfcert(cert, conf)) {
423 client = addclient(conf, 1);
426 client->addr = addr_copy((struct sockaddr *)&from);
428 removeclient(client);
430 debug(DBG_WARN, "tlsservernew: failed to create new client instance");
433 conf = find_clconf(handle, (struct sockaddr *)&from, &cur);
435 debug(DBG_WARN, "tlsservernew: ignoring request, no matching TLS client");
445 shutdown(s, SHUT_RDWR);
450 void *tlslistener(void *arg) {
451 pthread_t tlsserverth;
452 int s, *sp = (int *)arg;
453 struct sockaddr_storage from;
454 socklen_t fromlen = sizeof(from);
459 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
461 debug(DBG_WARN, "accept failed");
464 if (pthread_create(&tlsserverth, &pthread_attr, tlsservernew, (void *)&s)) {
465 debug(DBG_ERR, "tlslistener: pthread_create failed");
466 shutdown(s, SHUT_RDWR);
470 pthread_detach(tlsserverth);
476 const struct protodefs *tlsinit(uint8_t h) {
481 /* Local Variables: */
482 /* c-file-style: "stroustrup" */