4 * (c) 2007 Martin Mares <mj@ucw.cz>
11 #include "lib/getopt.h"
18 #include <sys/types.h>
19 #include <sys/socket.h>
21 #include <arpa/inet.h>
22 #include <netinet/in.h>
26 /*** CONFIGURATION ***/
28 static byte *log_name;
29 static uns port = 8888;
30 static uns dh_bits = 1024;
31 static uns max_conn = 10;
32 static uns session_timeout;
33 static byte *ca_cert_name = "?";
34 static byte *server_cert_name = "?";
35 static byte *server_key_name = "?";
37 static clist access_rules;
40 uns max_attachment_size;
43 static struct cf_section ip_node_conf = {
44 CF_TYPE(struct ip_node),
46 CF_USER("IP", PTR_TO(struct ip_node, addrmask), &ip_addrmask_type),
51 static struct cf_section access_conf = {
52 CF_TYPE(struct access_rule),
54 CF_LIST("IP", PTR_TO(struct access_rule, ip_list), &ip_node_conf),
55 CF_UNS("Admin", PTR_TO(struct access_rule, allow_admin)),
56 CF_UNS("PlainText", PTR_TO(struct access_rule, plain_text)),
57 CF_UNS("MaxConn", PTR_TO(struct access_rule, max_conn)),
62 static struct cf_section submitd_conf = {
64 CF_STRING("LogFile", &log_name),
65 CF_UNS("Port", &port),
66 CF_UNS("DHBits", &dh_bits),
67 CF_UNS("MaxConn", &max_conn),
68 CF_UNS("SessionTimeout", &session_timeout),
69 CF_UNS("MaxRequestSize", &max_request_size),
70 CF_UNS("MaxAttachSize", &max_attachment_size),
71 CF_STRING("CACert", &ca_cert_name),
72 CF_STRING("ServerCert", &server_cert_name),
73 CF_STRING("ServerKey", &server_key_name),
74 CF_STRING("History", &history_format),
75 CF_LIST("Access", &access_rules, &access_conf),
76 CF_UNS("TraceTLS", &trace_tls),
77 CF_UNS("TraceCommands", &trace_commands),
84 static clist connections;
85 static uns last_conn_id;
91 clist_init(&connections);
97 struct conn *c = xmalloc_zero(sizeof(*c));
98 c->id = ++last_conn_id;
99 clist_add_tail(&connections, &c->n);
105 conn_free(struct conn *c)
114 static struct access_rule *
117 CLIST_FOR_EACH(struct access_rule *, r, access_rules)
118 CLIST_FOR_EACH(struct ip_node *, n, r->ip_list)
119 if (ip_addrmask_match(&n->addrmask, ip))
128 CLIST_FOR_EACH(struct conn *, c, connections)
136 static gnutls_certificate_credentials_t cert_cred;
137 static gnutls_dh_params_t dh_params;
139 #define TLS_CHECK(name) if (err < 0) die(#name " failed: %s", gnutls_strerror(err))
146 gnutls_global_init();
147 err = gnutls_certificate_allocate_credentials(&cert_cred);
148 TLS_CHECK(gnutls_certificate_allocate_credentials);
149 err = gnutls_certificate_set_x509_trust_file(cert_cred, ca_cert_name, GNUTLS_X509_FMT_PEM);
151 die("No CA certificate found");
153 die("Unable to load X509 trust file: %s", gnutls_strerror(err));
154 err = gnutls_certificate_set_x509_key_file(cert_cred, server_cert_name, server_key_name, GNUTLS_X509_FMT_PEM);
156 die("Unable to load X509 key file: %s", gnutls_strerror(err));
158 err = gnutls_dh_params_init(&dh_params); TLS_CHECK(gnutls_dh_params_init);
159 err = gnutls_dh_params_generate2(dh_params, dh_bits); TLS_CHECK(gnutls_dh_params_generate2);
160 gnutls_certificate_set_dh_params(cert_cred, dh_params);
163 static gnutls_session_t
164 tls_new_session(int sk)
169 err = gnutls_init(&s, GNUTLS_SERVER); TLS_CHECK(gnutls_init);
170 err = gnutls_set_default_priority(s); TLS_CHECK(gnutls_set_default_priority); // FIXME
171 gnutls_credentials_set(s, GNUTLS_CRD_CERTIFICATE, cert_cred);
172 gnutls_certificate_server_set_request(s, GNUTLS_CERT_REQUEST);
173 gnutls_dh_set_prime_bits(s, dh_bits);
174 gnutls_transport_set_ptr(s, (gnutls_transport_ptr_t) sk);
179 tls_verify_cert(struct conn *c)
181 gnutls_session_t s = c->tls;
182 uns status, num_certs;
184 gnutls_x509_crt_t cert;
185 const gnutls_datum_t *certs;
187 DBG("Verifying peer certificates");
188 err = gnutls_certificate_verify_peers2(s, &status);
190 return gnutls_strerror(err);
191 DBG("Verify status: %04x", status);
192 if (status & GNUTLS_CERT_INVALID)
193 return "Certificate is invalid";
194 /* XXX: We do not handle revokation. */
195 if (gnutls_certificate_type_get(s) != GNUTLS_CRT_X509)
196 return "Certificate is not X509";
198 err = gnutls_x509_crt_init(&cert);
200 return "gnutls_x509_crt_init() failed";
201 certs = gnutls_certificate_get_peers(s, &num_certs);
203 return "No peer certificate found";
204 DBG("Got certificate list with %d peers", num_certs);
206 err = gnutls_x509_crt_import(cert, &certs[0], GNUTLS_X509_FMT_DER);
208 return "Cannot import certificate";
209 /* XXX: We do not check expiration and activation since the keys are generated for a single contest only anyway. */
212 size_t dn_len = sizeof(dn);
213 err = gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME, 0, 0, dn, &dn_len);
215 return "Cannot retrieve common name";
217 msg(L_INFO, "Cert CN: %s", dn);
218 c->cert_name = xstrdup(dn);
220 /* Check certificate purpose */
225 size_t purp_len = sizeof(purp);
227 err = gnutls_x509_crt_get_key_purpose_oid(cert, purpi++, purp, &purp_len, &crit);
228 if (err == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
229 return "Not a client certificate";
230 TLS_CHECK(gnutls_x509_crt_get_key_purpose_oid);
232 while (strcmp(purp, GNUTLS_KP_TLS_WWW_CLIENT));
239 tls_log_params(struct conn *c)
243 gnutls_session_t s = c->tls;
244 const char *proto = gnutls_protocol_get_name(gnutls_protocol_get_version(s));
245 const char *kx = gnutls_kx_get_name(gnutls_kx_get(s));
246 const char *cert = gnutls_certificate_type_get_name(gnutls_certificate_type_get(s));
247 const char *comp = gnutls_compression_get_name(gnutls_compression_get(s));
248 const char *cipher = gnutls_cipher_get_name(gnutls_cipher_get(s));
249 const char *mac = gnutls_mac_get_name(gnutls_mac_get(s));
250 msg(L_DEBUG, "TLS params: proto=%s kx=%s cert=%s comp=%s cipher=%s mac=%s",
251 proto, kx, cert, comp, cipher, mac);
254 /*** FASTBUFS OVER SOCKETS AND TLS ***/
256 void NONRET // Fatal protocol violation
257 client_error(char *msg, ...)
261 vmsg(L_ERROR_R, msg, args);
266 sk_fb_refill(struct fastbuf *f)
268 struct conn *c = SKIP_BACK(struct conn, rx_fb, f);
269 int cnt = read(c->sk, f->buffer, f->bufend - f->buffer);
271 client_error("Read error: %m");
273 f->bstop = f->buffer + cnt;
278 sk_fb_spout(struct fastbuf *f)
280 struct conn *c = SKIP_BACK(struct conn, tx_fb, f);
281 int len = f->bptr - f->buffer;
284 int cnt = careful_write(c->sk, f->buffer, len);
286 client_error("Write error");
291 init_sk_fastbufs(struct conn *c)
293 struct fastbuf *rf = &c->rx_fb, *tf = &c->tx_fb;
295 rf->buffer = xmalloc(1024);
296 rf->bufend = rf->buffer + 1024;
297 rf->bptr = rf->bstop = rf->buffer;
299 rf->refill = sk_fb_refill;
301 tf->buffer = xmalloc(1024);
302 tf->bufend = tf->buffer + 1024;
303 tf->bptr = tf->bstop = tf->buffer;
305 tf->spout = sk_fb_spout;
309 tls_fb_refill(struct fastbuf *f)
311 struct conn *c = SKIP_BACK(struct conn, rx_fb, f);
313 int cnt = gnutls_record_recv(c->tls, f->buffer, f->bufend - f->buffer);
314 DBG("TLS: Received %d bytes", cnt);
316 client_error("TLS read error: %s", gnutls_strerror(cnt));
318 f->bstop = f->buffer + cnt;
323 tls_fb_spout(struct fastbuf *f)
325 struct conn *c = SKIP_BACK(struct conn, tx_fb, f);
326 int len = f->bptr - f->buffer;
329 int cnt = gnutls_record_send(c->tls, f->buffer, len);
330 DBG("TLS: Sent %d bytes", cnt);
332 client_error("TLS write error: %s", gnutls_strerror(cnt));
337 init_tls_fastbufs(struct conn *c)
339 struct fastbuf *rf = &c->rx_fb, *tf = &c->tx_fb;
341 ASSERT(rf->buffer && tf->buffer); // Already set up for the plaintext connection
342 rf->refill = tls_fb_refill;
343 tf->spout = tls_fb_spout;
346 /*** CLIENT LOOP (runs in a child process) ***/
349 sigalrm_handler(int sig UNUSED)
351 // We do not try to do any gracious shutdown to avoid races
352 client_error("Timed out");
356 client_loop(struct conn *c)
358 setproctitle("submitd: client %s", c->ip_string);
362 signal(SIGPIPE, SIG_IGN);
363 struct sigaction sa = {
364 .sa_handler = sigalrm_handler
366 if (sigaction(SIGALRM, &sa, NULL) < 0)
367 die("Cannot setup SIGALRM handler: %m");
369 if (c->rule->plain_text)
371 bputsn(&c->tx_fb, "+OK");
376 bputsn(&c->tx_fb, "+TLS");
378 c->tls = tls_new_session(c->sk);
379 int err = gnutls_handshake(c->tls);
381 client_error("TLS handshake failed: %s", gnutls_strerror(err));
383 const char *cert_err = tls_verify_cert(c);
385 client_error("TLS certificate failure: %s", cert_err);
386 init_tls_fastbufs(c);
389 alarm(session_timeout);
390 if (!process_init(c))
391 msg(L_ERROR, "Protocol handshake failed");
394 setproctitle("submitd: client %s (%s)", c->ip_string, c->user);
397 alarm(session_timeout);
398 if (!process_command(c))
404 gnutls_bye(c->tls, GNUTLS_SHUT_WR);
407 gnutls_deinit(c->tls);
413 sigchld_handler(int sig UNUSED)
415 /* We do not need to do anything, just interrupt the accept syscall */
419 reap_child(pid_t pid, int status)
421 byte buf[EXIT_STATUS_MSG_SIZE];
422 if (format_exit_status(buf, status))
423 msg(L_ERROR, "Child %d %s", (int)pid, buf);
425 CLIST_FOR_EACH(struct conn *, c, connections)
428 msg(L_INFO, "Connection %d closed", c->id);
432 msg(L_ERROR, "Cannot find connection for child process %d", (int)pid);
435 static int listen_sk;
440 listen_sk = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
444 if (setsockopt(listen_sk, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
445 die("setsockopt(SO_REUSEADDR): %m");
447 struct sockaddr_in sa;
448 bzero(&sa, sizeof(sa));
449 sa.sin_family = AF_INET;
450 sa.sin_addr.s_addr = INADDR_ANY;
451 sa.sin_port = htons(port);
452 if (bind(listen_sk, (struct sockaddr *) &sa, sizeof(sa)) < 0)
453 die("Cannot bind to port %d: %m", port);
454 if (listen(listen_sk, 1024) < 0)
455 die("Cannot listen on port %d: %m", port);
461 struct sockaddr_in sa;
462 int salen = sizeof(sa);
463 int sk = accept(listen_sk, (struct sockaddr *) &sa, &salen);
471 byte ipbuf[INET_ADDRSTRLEN];
472 inet_ntop(AF_INET, &sa.sin_addr, ipbuf, sizeof(ipbuf));
473 u32 addr = ntohl(sa.sin_addr.s_addr);
474 uns port = ntohs(sa.sin_port);
477 struct access_rule *rule = lookup_rule(addr);
480 err = "Unauthorized";
484 if (num_conn >= max_conn)
486 err = "Too many connections";
490 if (conn_count(addr) >= rule->max_conn)
492 err = "Too many connections from this address";
496 struct conn *c = conn_new();
497 msg(L_INFO, "Connection from %s:%d (id %d, %s, %s)",
499 (rule->plain_text ? "plain-text" : "TLS"),
500 (rule->allow_admin ? "admin" : "user"));
502 c->ip_string = xstrdup(ipbuf);
510 err = "Server overloaded";
511 msg(L_ERROR, "Fork failed: %m");
524 msg(L_ERROR_R, "Connection from %s:%d rejected (%s)", ipbuf, port, err);
526 // Write an error message to the socket, but do not allow it to slow us down
527 struct linger ling = { .l_onoff=0 };
528 if (setsockopt(sk, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling)) < 0)
529 msg(L_ERROR, "Cannot set SO_LINGER: %m");
531 write(sk, err, strlen(err));
536 int main(int argc, char **argv)
538 setproctitle_init(argc, argv);
539 cf_def_file = "submit/config";
540 cf_declare_section("SubmitD", &submitd_conf, 0);
541 cf_declare_section("Tasks", &tasks_conf, 0);
544 if ((opt = cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL)) >= 0)
545 die("This program has no options");
549 msg(L_INFO, "Initializing TLS");
554 msg(L_INFO, "Listening on port %d", port);
556 struct sigaction sa = {
557 .sa_handler = sigchld_handler
559 if (sigaction(SIGCHLD, &sa, NULL) < 0)
560 die("Cannot setup SIGCHLD handler: %m");
564 setproctitle("submitd: %d connections", num_conn);
566 pid_t pid = waitpid(-1, &status, WNOHANG);
568 reap_child(pid, status);