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;
172 fd_set readfds, writefds;
173 struct timeval timer;
178 /* make socket non-blocking? */
179 for (len = 0; len < num; len += cnt) {
184 timer.tv_sec = timeout;
187 ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
191 cnt = SSL_read(ssl, buf + len, num - len);
193 switch (SSL_get_error(ssl, cnt)) {
194 case SSL_ERROR_WANT_READ:
195 case SSL_ERROR_WANT_WRITE:
198 case SSL_ERROR_ZERO_RETURN:
199 /* remote end sent close_notify, send one back */
209 /* timeout in seconds, 0 means no timeout (blocking) */
210 unsigned char *radtlsget(SSL *ssl, int timeout) {
212 unsigned char buf[4], *rad;
215 cnt = sslreadtimeout(ssl, buf, 4, timeout);
217 debug(DBG_DBG, cnt ? "radtlsget: connection lost" : "radtlsget: timeout");
224 debug(DBG_ERR, "radtlsget: malloc failed");
229 cnt = sslreadtimeout(ssl, rad + 4, len - 4, timeout);
231 debug(DBG_DBG, cnt ? "radtlsget: connection lost" : "radtlsget: timeout");
240 debug(DBG_WARN, "radtlsget: packet smaller than minimum radius size");
243 debug(DBG_DBG, "radtlsget: got %d bytes", len);
247 int clientradputtls(struct server *server, unsigned char *rad) {
251 struct clsrvconf *conf = server->conf;
253 if (!server->connectionok)
256 if ((cnt = SSL_write(server->ssl, rad, len)) <= 0) {
257 while ((error = ERR_get_error()))
258 debug(DBG_ERR, "clientradputtls: TLS: %s", ERR_error_string(error, NULL));
262 debug(DBG_DBG, "clientradputtls: Sent %d bytes, Radius packet of length %d to TLS peer %s", cnt, len, conf->name);
266 void *tlsclientrd(void *arg) {
267 struct server *server = (struct server *)arg;
269 struct timeval now, lastconnecttry;
272 /* yes, lastconnecttry is really necessary */
273 lastconnecttry = server->lastconnecttry;
274 buf = radtlsget(server->ssl, server->dynamiclookuparg ? IDLE_TIMEOUT : 0);
276 if (server->dynamiclookuparg)
278 tlsconnect(server, &lastconnecttry, 0, "tlsclientrd");
284 if (server->dynamiclookuparg) {
285 gettimeofday(&now, NULL);
286 if (now.tv_sec - server->lastreply.tv_sec > IDLE_TIMEOUT) {
287 debug(DBG_INFO, "tlsclientrd: idle timeout for %s", server->conf->name);
293 server->clientrdgone = 1;
297 void *tlsserverwr(void *arg) {
300 struct client *client = (struct client *)arg;
301 struct gqueue *replyq;
302 struct request *reply;
304 debug(DBG_DBG, "tlsserverwr: starting for %s", addr2string(client->addr));
305 replyq = client->replyq;
307 pthread_mutex_lock(&replyq->mutex);
308 while (!list_first(replyq->entries)) {
310 debug(DBG_DBG, "tlsserverwr: waiting for signal");
311 pthread_cond_wait(&replyq->cond, &replyq->mutex);
312 debug(DBG_DBG, "tlsserverwr: got signal");
315 /* ssl might have changed while waiting */
316 pthread_mutex_unlock(&replyq->mutex);
317 debug(DBG_DBG, "tlsserverwr: exiting as requested");
322 reply = (struct request *)list_shift(replyq->entries);
323 pthread_mutex_unlock(&replyq->mutex);
324 cnt = SSL_write(client->ssl, reply->replybuf, RADLEN(reply->replybuf));
326 debug(DBG_DBG, "tlsserverwr: sent %d bytes, Radius packet of length %d to %s",
327 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
329 while ((error = ERR_get_error()))
330 debug(DBG_ERR, "tlsserverwr: SSL: %s", ERR_error_string(error, NULL));
335 void tlsserverrd(struct client *client) {
338 pthread_t tlsserverwrth;
340 debug(DBG_DBG, "tlsserverrd: starting for %s", addr2string(client->addr));
342 if (pthread_create(&tlsserverwrth, NULL, tlsserverwr, (void *)client)) {
343 debug(DBG_ERR, "tlsserverrd: pthread_create failed");
348 buf = radtlsget(client->ssl, 0);
350 debug(DBG_ERR, "tlsserverrd: connection from %s lost", addr2string(client->addr));
353 debug(DBG_DBG, "tlsserverrd: got Radius message from %s", addr2string(client->addr));
362 debug(DBG_ERR, "tlsserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
367 /* stop writer by setting ssl to NULL and give signal in case waiting for data */
369 pthread_mutex_lock(&client->replyq->mutex);
370 pthread_cond_signal(&client->replyq->cond);
371 pthread_mutex_unlock(&client->replyq->mutex);
372 debug(DBG_DBG, "tlsserverrd: waiting for writer to end");
373 pthread_join(tlsserverwrth, NULL);
374 debug(DBG_DBG, "tlsserverrd: reader for %s exiting", addr2string(client->addr));
377 void *tlsservernew(void *arg) {
379 struct sockaddr_storage from;
380 socklen_t fromlen = sizeof(from);
381 struct clsrvconf *conf;
382 struct list_node *cur = NULL;
387 struct client *client;
390 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
391 debug(DBG_DBG, "tlsservernew: getpeername failed, exiting");
394 debug(DBG_WARN, "tlsservernew: incoming TLS connection from %s", addr2string((struct sockaddr *)&from));
396 conf = find_clconf(handle, (struct sockaddr *)&from, &cur);
398 ctx = tlsgetctx(handle, conf->tlsconf);
406 if (SSL_accept(ssl) <= 0) {
407 while ((error = ERR_get_error()))
408 debug(DBG_ERR, "tlsservernew: SSL: %s", ERR_error_string(error, NULL));
409 debug(DBG_ERR, "tlsservernew: SSL_accept failed");
412 cert = verifytlscert(ssl);
418 if (verifyconfcert(cert, conf)) {
420 client = addclient(conf, 1);
423 client->addr = addr_copy((struct sockaddr *)&from);
425 removeclient(client);
427 debug(DBG_WARN, "tlsservernew: failed to create new client instance");
430 conf = find_clconf(handle, (struct sockaddr *)&from, &cur);
432 debug(DBG_WARN, "tlsservernew: ignoring request, no matching TLS client");
442 shutdown(s, SHUT_RDWR);
447 void *tlslistener(void *arg) {
448 pthread_t tlsserverth;
449 int s, *sp = (int *)arg;
450 struct sockaddr_storage from;
451 socklen_t fromlen = sizeof(from);
456 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
458 debug(DBG_WARN, "accept failed");
461 if (pthread_create(&tlsserverth, NULL, tlsservernew, (void *)&s)) {
462 debug(DBG_ERR, "tlslistener: pthread_create failed");
463 shutdown(s, SHUT_RDWR);
467 pthread_detach(tlsserverth);
473 const struct protodefs *tlsinit(uint8_t h) {
478 /* Local Variables: */
479 /* c-file-style: "stroustrup" */