4 * (c) 2007 Martin Mares <mj@ucw.cz>
9 * - competition timeout & per-contestant exceptions
10 * - open-data problems
17 #include "lib/getopt.h"
24 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <arpa/inet.h>
28 #include <netinet/in.h>
32 /*** CONFIGURATION ***/
34 static uns port = 8888;
35 static uns dh_bits = 1024;
36 static uns max_conn = 10;
37 static uns session_timeout;
38 static byte *ca_cert_name = "?";
39 static byte *server_cert_name = "?";
40 static byte *server_key_name = "?";
41 static clist access_rules;
44 uns max_attachment_size;
47 static struct cf_section access_conf = {
48 CF_TYPE(struct access_rule),
50 CF_USER("IP", PTR_TO(struct access_rule, addrmask), &ip_addrmask_type),
51 CF_UNS("Admin", PTR_TO(struct access_rule, allow_admin)),
52 CF_UNS("PlainText", PTR_TO(struct access_rule, plain_text)),
53 CF_UNS("MaxConn", PTR_TO(struct access_rule, max_conn)),
58 static struct cf_section submitd_conf = {
60 CF_UNS("Port", &port),
61 CF_UNS("DHBits", &dh_bits),
62 CF_UNS("MaxConn", &max_conn),
63 CF_UNS("SessionTimeout", &session_timeout),
64 CF_UNS("MaxRequestSize", &max_request_size),
65 CF_UNS("MaxAttachSize", &max_attachment_size),
66 CF_STRING("CACert", &ca_cert_name),
67 CF_STRING("ServerCert", &server_cert_name),
68 CF_STRING("ServerKey", &server_key_name),
69 CF_LIST("Access", &access_rules, &access_conf),
70 CF_UNS("TraceTLS", &trace_tls),
71 CF_UNS("TraceCommands", &trace_commands),
78 static clist connections;
79 static uns last_conn_id;
85 clist_init(&connections);
91 struct conn *c = xmalloc_zero(sizeof(*c));
92 c->id = ++last_conn_id;
93 clist_add_tail(&connections, &c->n);
99 conn_free(struct conn *c)
108 static struct access_rule *
111 CLIST_FOR_EACH(struct access_rule *, r, access_rules)
112 if (ip_addrmask_match(&r->addrmask, ip))
121 CLIST_FOR_EACH(struct conn *, c, connections)
129 static gnutls_certificate_credentials_t cert_cred;
130 static gnutls_dh_params_t dh_params;
132 #define TLS_CHECK(name) if (err < 0) die(#name " failed: %s", gnutls_strerror(err))
139 gnutls_global_init();
140 err = gnutls_certificate_allocate_credentials(&cert_cred);
141 TLS_CHECK(gnutls_certificate_allocate_credentials);
142 err = gnutls_certificate_set_x509_trust_file(cert_cred, ca_cert_name, GNUTLS_X509_FMT_PEM);
144 die("No CA certificate found");
146 die("Unable to load X509 trust file: %s", gnutls_strerror(err));
147 err = gnutls_certificate_set_x509_key_file(cert_cred, server_cert_name, server_key_name, GNUTLS_X509_FMT_PEM);
149 die("Unable to load X509 key file: %s", gnutls_strerror(err));
151 err = gnutls_dh_params_init(&dh_params); TLS_CHECK(gnutls_dh_params_init);
152 err = gnutls_dh_params_generate2(dh_params, dh_bits); TLS_CHECK(gnutls_dh_params_generate2);
153 gnutls_certificate_set_dh_params(cert_cred, dh_params);
156 static gnutls_session_t
157 tls_new_session(int sk)
162 err = gnutls_init(&s, GNUTLS_SERVER); TLS_CHECK(gnutls_init);
163 err = gnutls_set_default_priority(s); TLS_CHECK(gnutls_set_default_priority); // FIXME
164 gnutls_credentials_set(s, GNUTLS_CRD_CERTIFICATE, cert_cred);
165 gnutls_certificate_server_set_request(s, GNUTLS_CERT_REQUEST);
166 gnutls_dh_set_prime_bits(s, dh_bits);
167 gnutls_transport_set_ptr(s, (gnutls_transport_ptr_t) sk);
172 tls_verify_cert(struct conn *c)
174 gnutls_session_t s = c->tls;
175 uns status, num_certs;
177 gnutls_x509_crt_t cert;
178 const gnutls_datum_t *certs;
180 DBG("Verifying peer certificates");
181 err = gnutls_certificate_verify_peers2(s, &status);
183 return gnutls_strerror(err);
184 DBG("Verify status: %04x", status);
185 if (status & GNUTLS_CERT_INVALID)
186 return "Certificate is invalid";
187 /* XXX: We do not handle revokation. */
188 if (gnutls_certificate_type_get(s) != GNUTLS_CRT_X509)
189 return "Certificate is not X509";
191 err = gnutls_x509_crt_init(&cert);
193 return "gnutls_x509_crt_init() failed";
194 certs = gnutls_certificate_get_peers(s, &num_certs);
196 return "No peer certificate found";
197 DBG("Got certificate list with %d peers", num_certs);
199 err = gnutls_x509_crt_import(cert, &certs[0], GNUTLS_X509_FMT_DER);
201 return "Cannot import certificate";
202 /* XXX: We do not check expiration and activation since the keys are generated for a single contest only anyway. */
205 size_t dn_len = sizeof(dn);
206 err = gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME, 0, 0, dn, &dn_len);
208 return "Cannot retrieve common name";
210 log(L_INFO, "Cert CN: %s", dn);
211 c->cert_name = xstrdup(dn);
213 /* Check certificate purpose */
218 size_t purp_len = sizeof(purp);
220 err = gnutls_x509_crt_get_key_purpose_oid(cert, purpi++, purp, &purp_len, &crit);
221 if (err == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
222 return "Not a client certificate";
223 TLS_CHECK(gnutls_x509_crt_get_key_purpose_oid);
225 while (strcmp(purp, GNUTLS_KP_TLS_WWW_CLIENT));
232 tls_log_params(struct conn *c)
236 gnutls_session_t s = c->tls;
237 const char *proto = gnutls_protocol_get_name(gnutls_protocol_get_version(s));
238 const char *kx = gnutls_kx_get_name(gnutls_kx_get(s));
239 const char *cert = gnutls_certificate_type_get_name(gnutls_certificate_type_get(s));
240 const char *comp = gnutls_compression_get_name(gnutls_compression_get(s));
241 const char *cipher = gnutls_cipher_get_name(gnutls_cipher_get(s));
242 const char *mac = gnutls_mac_get_name(gnutls_mac_get(s));
243 log(L_DEBUG, "TLS params: proto=%s kx=%s cert=%s comp=%s cipher=%s mac=%s",
244 proto, kx, cert, comp, cipher, mac);
247 /*** FASTBUFS OVER SOCKETS AND TLS ***/
249 void NONRET // Fatal protocol violation
250 client_error(char *msg, ...)
254 vlog_msg(L_ERROR_R, msg, args);
259 sk_fb_refill(struct fastbuf *f)
261 struct conn *c = SKIP_BACK(struct conn, rx_fb, f);
262 int cnt = read(c->sk, f->buffer, f->bufend - f->buffer);
264 client_error("Read error: %m");
266 f->bstop = f->buffer + cnt;
271 sk_fb_spout(struct fastbuf *f)
273 struct conn *c = SKIP_BACK(struct conn, tx_fb, f);
274 int len = f->bptr - f->buffer;
277 int cnt = careful_write(c->sk, f->buffer, len);
279 client_error("Write error");
284 init_sk_fastbufs(struct conn *c)
286 struct fastbuf *rf = &c->rx_fb, *tf = &c->tx_fb;
288 rf->buffer = xmalloc(1024);
289 rf->bufend = rf->buffer + 1024;
290 rf->bptr = rf->bstop = rf->buffer;
292 rf->refill = sk_fb_refill;
294 tf->buffer = xmalloc(1024);
295 tf->bufend = tf->buffer + 1024;
296 tf->bptr = tf->bstop = tf->buffer;
298 tf->spout = sk_fb_spout;
302 tls_fb_refill(struct fastbuf *f)
304 struct conn *c = SKIP_BACK(struct conn, rx_fb, f);
306 int cnt = gnutls_record_recv(c->tls, f->buffer, f->bufend - f->buffer);
307 DBG("TLS: Received %d bytes", cnt);
309 client_error("TLS read error: %s", gnutls_strerror(cnt));
311 f->bstop = f->buffer + cnt;
316 tls_fb_spout(struct fastbuf *f)
318 struct conn *c = SKIP_BACK(struct conn, tx_fb, f);
319 int len = f->bptr - f->buffer;
322 int cnt = gnutls_record_send(c->tls, f->buffer, len);
323 DBG("TLS: Sent %d bytes", cnt);
325 client_error("TLS write error: %s", gnutls_strerror(cnt));
330 init_tls_fastbufs(struct conn *c)
332 struct fastbuf *rf = &c->rx_fb, *tf = &c->tx_fb;
334 ASSERT(rf->buffer && tf->buffer); // Already set up for the plaintext connection
335 rf->refill = tls_fb_refill;
336 tf->spout = tls_fb_spout;
339 /*** CLIENT LOOP (runs in a child process) ***/
342 sigalrm_handler(int sig UNUSED)
344 // We do not try to do any gracious shutdown to avoid races
345 client_error("Timed out");
349 client_loop(struct conn *c)
351 setproctitle("submitd: client %s", c->ip_string);
355 signal(SIGPIPE, SIG_IGN);
356 struct sigaction sa = {
357 .sa_handler = sigalrm_handler
359 if (sigaction(SIGALRM, &sa, NULL) < 0)
360 die("Cannot setup SIGALRM handler: %m");
362 if (c->rule->plain_text)
364 bputsn(&c->tx_fb, "+OK");
369 bputsn(&c->tx_fb, "+TLS");
371 c->tls = tls_new_session(c->sk);
372 int err = gnutls_handshake(c->tls);
374 client_error("TLS handshake failed: %s", gnutls_strerror(err));
376 const char *cert_err = tls_verify_cert(c);
378 client_error("TLS certificate failure: %s", cert_err);
379 init_tls_fastbufs(c);
382 alarm(session_timeout);
383 if (!process_init(c))
384 log(L_ERROR, "Protocol handshake failed");
387 setproctitle("submitd: client %s (%s)", c->ip_string, c->user);
390 alarm(session_timeout);
391 if (!process_command(c))
397 gnutls_bye(c->tls, GNUTLS_SHUT_WR);
400 gnutls_deinit(c->tls);
406 sigchld_handler(int sig UNUSED)
408 /* We do not need to do anything, just interrupt the accept syscall */
412 reap_child(pid_t pid, int status)
414 byte msg[EXIT_STATUS_MSG_SIZE];
415 if (format_exit_status(msg, status))
416 log(L_ERROR, "Child %d %s", (int)pid, msg);
418 CLIST_FOR_EACH(struct conn *, c, connections)
421 log(L_INFO, "Connection %d closed", c->id);
425 log(L_ERROR, "Cannot find connection for child process %d", (int)pid);
428 static int listen_sk;
433 listen_sk = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
437 if (setsockopt(listen_sk, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
438 die("setsockopt(SO_REUSEADDR): %m");
440 struct sockaddr_in sa;
441 bzero(&sa, sizeof(sa));
442 sa.sin_family = AF_INET;
443 sa.sin_addr.s_addr = INADDR_ANY;
444 sa.sin_port = htons(port);
445 if (bind(listen_sk, (struct sockaddr *) &sa, sizeof(sa)) < 0)
446 die("Cannot bind to port %d: %m", port);
447 if (listen(listen_sk, 1024) < 0)
448 die("Cannot listen on port %d: %m", port);
454 struct sockaddr_in sa;
455 int salen = sizeof(sa);
456 int sk = accept(listen_sk, (struct sockaddr *) &sa, &salen);
464 byte ipbuf[INET_ADDRSTRLEN];
465 inet_ntop(AF_INET, &sa.sin_addr, ipbuf, sizeof(ipbuf));
466 u32 addr = ntohl(sa.sin_addr.s_addr);
467 uns port = ntohs(sa.sin_port);
470 struct access_rule *rule = lookup_rule(addr);
473 err = "Unauthorized";
477 if (num_conn >= max_conn)
479 err = "Too many connections";
483 if (conn_count(addr) >= rule->max_conn)
485 err = "Too many connections from this address";
489 struct conn *c = conn_new();
490 log(L_INFO, "Connection from %s:%d (id %d, %s, %s)",
492 (rule->plain_text ? "plain-text" : "TLS"),
493 (rule->allow_admin ? "admin" : "user"));
495 c->ip_string = xstrdup(ipbuf);
503 err = "Server overloaded";
504 log(L_ERROR, "Fork failed: %m");
517 log(L_ERROR_R, "Connection from %s:%d rejected (%s)", ipbuf, port, err);
519 // Write an error message to the socket, but do not allow it to slow us down
520 struct linger ling = { .l_onoff=0 };
521 if (setsockopt(sk, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling)) < 0)
522 log(L_ERROR, "Cannot set SO_LINGER: %m");
524 write(sk, err, strlen(err));
529 int main(int argc, char **argv)
531 setproctitle_init(argc, argv);
532 cf_def_file = "config";
533 cf_declare_section("SubmitD", &submitd_conf, 0);
534 cf_declare_section("Tasks", &tasks_conf, 0);
537 if ((opt = cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL)) >= 0)
538 die("This program has no options");
540 log(L_INFO, "Initializing TLS");
545 log(L_INFO, "Listening on port %d", port);
547 struct sigaction sa = {
548 .sa_handler = sigchld_handler
550 if (sigaction(SIGCHLD, &sa, NULL) < 0)
551 die("Cannot setup SIGCHLD handler: %m");
555 setproctitle("submitd: %d connections", num_conn);
557 pid_t pid = waitpid(-1, &status, WNOHANG);
559 reap_child(pid, status);