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);
293 debug(DBG_INFO, "tlsclientrd: exiting for %s", server->conf->name);
295 SSL_shutdown(server->ssl);
296 shutdown(server->sock, SHUT_RDWR);
299 /* Wake up clientwr(). */
300 server->clientrdgone = 1;
301 pthread_mutex_lock(&server->newrq_mutex);
302 pthread_cond_signal(&server->newrq_cond);
303 pthread_mutex_unlock(&server->newrq_mutex);
307 void *tlsserverwr(void *arg) {
310 struct client *client = (struct client *)arg;
311 struct gqueue *replyq;
312 struct request *reply;
314 debug(DBG_DBG, "tlsserverwr: starting for %s", addr2string(client->addr));
315 replyq = client->replyq;
317 pthread_mutex_lock(&replyq->mutex);
318 while (!list_first(replyq->entries)) {
320 debug(DBG_DBG, "tlsserverwr: waiting for signal");
321 pthread_cond_wait(&replyq->cond, &replyq->mutex);
322 debug(DBG_DBG, "tlsserverwr: got signal");
325 /* ssl might have changed while waiting */
326 pthread_mutex_unlock(&replyq->mutex);
327 debug(DBG_DBG, "tlsserverwr: exiting as requested");
332 reply = (struct request *)list_shift(replyq->entries);
333 pthread_mutex_unlock(&replyq->mutex);
334 cnt = SSL_write(client->ssl, reply->replybuf, RADLEN(reply->replybuf));
336 debug(DBG_DBG, "tlsserverwr: sent %d bytes, Radius packet of length %d to %s",
337 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
339 while ((error = ERR_get_error()))
340 debug(DBG_ERR, "tlsserverwr: SSL: %s", ERR_error_string(error, NULL));
345 void tlsserverrd(struct client *client) {
348 pthread_t tlsserverwrth;
350 debug(DBG_DBG, "tlsserverrd: starting for %s", addr2string(client->addr));
352 if (pthread_create(&tlsserverwrth, &pthread_attr, tlsserverwr, (void *)client)) {
353 debug(DBG_ERR, "tlsserverrd: pthread_create failed");
358 buf = radtlsget(client->ssl, 0);
360 debug(DBG_ERR, "tlsserverrd: connection from %s lost", addr2string(client->addr));
363 debug(DBG_DBG, "tlsserverrd: got Radius message from %s", addr2string(client->addr));
372 debug(DBG_ERR, "tlsserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
377 /* stop writer by setting ssl to NULL and give signal in case waiting for data */
379 pthread_mutex_lock(&client->replyq->mutex);
380 pthread_cond_signal(&client->replyq->cond);
381 pthread_mutex_unlock(&client->replyq->mutex);
382 debug(DBG_DBG, "tlsserverrd: waiting for writer to end");
383 pthread_join(tlsserverwrth, NULL);
384 debug(DBG_DBG, "tlsserverrd: reader for %s exiting", addr2string(client->addr));
387 void *tlsservernew(void *arg) {
389 struct sockaddr_storage from;
390 socklen_t fromlen = sizeof(from);
391 struct clsrvconf *conf;
392 struct list_node *cur = NULL;
397 struct client *client;
398 struct tls *accepted_tls = NULL;
401 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
402 debug(DBG_DBG, "tlsservernew: getpeername failed, exiting");
405 debug(DBG_WARN, "tlsservernew: incoming TLS connection from %s", addr2string((struct sockaddr *)&from));
407 conf = find_clconf(handle, (struct sockaddr *)&from, &cur);
409 ctx = tlsgetctx(handle, conf->tlsconf);
417 if (SSL_accept(ssl) <= 0) {
418 while ((error = ERR_get_error()))
419 debug(DBG_ERR, "tlsservernew: SSL: %s", ERR_error_string(error, NULL));
420 debug(DBG_ERR, "tlsservernew: SSL_accept failed");
423 cert = verifytlscert(ssl);
426 accepted_tls = conf->tlsconf;
430 if (accepted_tls == conf->tlsconf && verifyconfcert(cert, conf)) {
432 client = addclient(conf, 1);
435 client->addr = addr_copy((struct sockaddr *)&from);
437 removeclient(client);
439 debug(DBG_WARN, "tlsservernew: failed to create new client instance");
442 conf = find_clconf(handle, (struct sockaddr *)&from, &cur);
444 debug(DBG_WARN, "tlsservernew: ignoring request, no matching TLS client");
454 shutdown(s, SHUT_RDWR);
459 void *tlslistener(void *arg) {
460 pthread_t tlsserverth;
461 int s, *sp = (int *)arg;
462 struct sockaddr_storage from;
463 socklen_t fromlen = sizeof(from);
468 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
470 debug(DBG_WARN, "accept failed");
473 if (pthread_create(&tlsserverth, &pthread_attr, tlsservernew, (void *)&s)) {
474 debug(DBG_ERR, "tlslistener: pthread_create failed");
475 shutdown(s, SHUT_RDWR);
479 pthread_detach(tlsserverth);
485 const struct protodefs *tlsinit(uint8_t h) {
490 /* Local Variables: */
491 /* c-file-style: "stroustrup" */