2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
41 #include <gnutls/gnutls.h>
46 gnutls_certificate_credentials_t creds;
52 struct sockaddr_storage name;
53 gnutls_session_t sess;
57 static gnutls_dh_params_t dhparams;
59 static int tlsblock(int fd, gnutls_session_t sess, time_t to)
61 if(gnutls_record_get_direction(sess))
62 return(block(fd, EV_WRITE, to));
64 return(block(fd, EV_READ, to));
67 static ssize_t sslread(void *cookie, char *buf, size_t len)
69 struct sslconn *ssl = cookie;
73 while(ssl->in.d == 0) {
74 sizebuf(ssl->in, ssl->in.d + 1024);
75 ret = gnutls_record_recv(ssl->sess, ssl->in.b + ssl->in.d, ssl->in.s - ssl->in.d);
76 if((ret == GNUTLS_E_INTERRUPTED) || (ret == GNUTLS_E_AGAIN)) {
77 if(tlsblock(ssl->fd, ssl->sess, 60) == 0) {
90 xf = min(ssl->in.d, len);
91 memcpy(buf, ssl->in.b, xf);
92 memmove(ssl->in.b, ssl->in.b + xf, ssl->in.d -= xf);
96 static ssize_t sslwrite(void *cookie, const char *buf, size_t len)
98 struct sslconn *ssl = cookie;
104 ret = gnutls_record_send(ssl->sess, buf + off, len - off);
105 if((ret == GNUTLS_E_INTERRUPTED) || (ret == GNUTLS_E_AGAIN)) {
106 if(tlsblock(ssl->fd, ssl->sess, 60) == 0) {
126 static int sslclose(void *cookie)
131 static cookie_io_functions_t iofuns = {
137 static int initreq(struct conn *conn, struct hthead *req)
139 struct sslconn *ssl = conn->pdata;
140 struct sockaddr_storage sa;
144 headappheader(req, "X-Ash-Address", formathaddress((struct sockaddr *)&ssl->name, sizeof(sa)));
145 if(ssl->name.ss_family == AF_INET) {
146 headappheader(req, "X-Ash-Address", inet_ntop(AF_INET, &((struct sockaddr_in *)&ssl->name)->sin_addr, nmbuf, sizeof(nmbuf)));
147 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in *)&ssl->name)->sin_port)));
148 } else if(ssl->name.ss_family == AF_INET6) {
149 headappheader(req, "X-Ash-Address", inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&ssl->name)->sin6_addr, nmbuf, sizeof(nmbuf)));
150 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&ssl->name)->sin6_port)));
153 if(!getsockname(ssl->fd, (struct sockaddr *)&sa, &salen))
154 headappheader(req, "X-Ash-Server-Address", formathaddress((struct sockaddr *)&sa, sizeof(sa)));
155 headappheader(req, "X-Ash-Server-Port", sprintf3("%i", ssl->port->sport));
156 headappheader(req, "X-Ash-Protocol", "https");
160 static void servessl(struct muth *muth, va_list args)
163 vavar(struct sockaddr_storage, name);
164 vavar(struct sslport *, pd);
167 gnutls_session_t sess;
171 int setcreds(gnutls_session_t sess)
179 namlen = sizeof(nambuf);
180 if(gnutls_server_name_get(sess, nambuf, &namlen, &ntype, i) != 0)
182 if(ntype != GNUTLS_NAME_DNS)
185 gnutls_credentials_set(sess, GNUTLS_CRD_CERTIFICATE, pd->creds);
186 gnutls_certificate_server_set_request(sess, GNUTLS_CERT_REQUEST);
190 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
191 gnutls_init(&sess, GNUTLS_SERVER);
192 gnutls_set_default_priority(sess);
193 gnutls_handshake_set_post_client_hello_function(sess, setcreds);
194 gnutls_transport_set_ptr(sess, (gnutls_transport_ptr_t)(intptr_t)fd);
195 while((ret = gnutls_handshake(sess)) != 0) {
196 if((ret != GNUTLS_E_INTERRUPTED) && (ret != GNUTLS_E_AGAIN))
198 if(tlsblock(fd, sess, 60) <= 0)
201 memset(&conn, 0, sizeof(conn));
202 memset(&ssl, 0, sizeof(ssl));
204 conn.initreq = initreq;
210 in = fopencookie(&ssl, "r+", iofuns);
218 static void listenloop(struct muth *muth, va_list args)
220 vavar(struct sslport *, pd);
222 struct sockaddr_storage name;
226 namelen = sizeof(name);
227 block(pd->fd, EV_READ, 0);
228 ns = accept(pd->fd, (struct sockaddr *)&name, &namelen);
230 flog(LOG_ERR, "accept: %s", strerror(errno));
233 mustart(servessl, ns, name, pd);
241 static void init(void)
243 static int inited = 0;
249 if((ret = gnutls_global_init()) != 0) {
250 flog(LOG_ERR, "could not initialize GnuTLS: %s", gnutls_strerror(ret));
253 if(((ret = gnutls_dh_params_init(&dhparams)) != 0) ||
254 ((ret = gnutls_dh_params_generate2(dhparams, 2048)) != 0)) {
255 flog(LOG_ERR, "GnuTLS could not generate Diffie-Hellman parameters: %s", gnutls_strerror(ret));
260 void handlegnussl(int argc, char **argp, char **argv)
262 int i, ret, port, fd;
263 gnutls_certificate_credentials_t creds;
265 char *crtfile, *keyfile;
269 gnutls_certificate_allocate_credentials(&creds);
270 keyfile = crtfile = NULL;
271 for(i = 0; i < argc; i++) {
272 if(!strcmp(argp[i], "help")) {
273 printf("ssl handler parameters:\n");
274 printf("\tcert=CERT-FILE [mandatory]\n");
275 printf("\t\tThe name of the file to read the certificate from.\n");
276 printf("\tkey=KEY-FILE [same as CERT-FILE]\n");
277 printf("\t\tThe name of the file to read the private key from.\n");
278 printf("\ttrust=CA-FILE [no default]\n");
279 printf("\t\tThe name of a file to read trusted certificates from.\n");
280 printf("\t\tMay be given multiple times.\n");
281 printf("\tcrl=CRL-FILE [no default]\n");
282 printf("\t\tThe name of a file to read revocation lists from.\n");
283 printf("\t\tMay be given multiple times.\n");
284 printf("\tport=PORT [443]\n");
285 printf("\t\tThe TCP port to listen on.\n");
287 printf("\tAll X.509 data files must be PEM-encoded.\n");
289 } else if(!strcmp(argp[i], "cert")) {
291 } else if(!strcmp(argp[i], "key")) {
293 } else if(!strcmp(argp[i], "trust")) {
294 if((ret = gnutls_certificate_set_x509_trust_file(creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) {
295 flog(LOG_ERR, "ssl: could not load trust file `%s': %s", argv[i], gnutls_strerror(ret));
298 } else if(!strcmp(argp[i], "crl")) {
299 if((ret = gnutls_certificate_set_x509_crl_file(creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) {
300 flog(LOG_ERR, "ssl: could not load CRL file `%s': %s", argv[i], gnutls_strerror(ret));
303 } else if(!strcmp(argp[i], "port")) {
304 port = atoi(argv[i]);
306 flog(LOG_ERR, "unknown parameter `%s' to ssl handler", argp[i]);
310 if(crtfile == NULL) {
311 flog(LOG_ERR, "ssl: needs certificate file at the very least");
316 if((ret = gnutls_certificate_set_x509_key_file(creds, crtfile, keyfile, GNUTLS_X509_FMT_PEM)) != 0) {
317 flog(LOG_ERR, "ssl: could not load certificate or key: %s", gnutls_strerror(ret));
320 gnutls_certificate_set_dh_params(creds, dhparams);
321 if((fd = listensock6(port)) < 0) {
322 flog(LOG_ERR, "could not listen on IPv6 port (port %i): %s", port, strerror(errno));
329 mustart(listenloop, pd);
330 if((fd = listensock6(port)) < 0) {
331 if(errno != EADDRINUSE) {
332 flog(LOG_ERR, "could not listen on IPv6 port (port %i): %s", port, strerror(errno));
340 mustart(listenloop, pd);