#include "client.h"
#include "send.h"
#include "packet.h"
+#include "certfp.h"
-#define ZIPSTATS_TIME 60
-
-static void collect_zipstats(void *unused);
static void ssl_read_ctl(rb_fde_t * F, void *data);
static int ssld_count;
char version[256];
};
-static void send_new_ssl_certs_one(ssl_ctl_t * ctl, const char *ssl_cert,
- const char *ssl_private_key, const char *ssl_dh_params,
- const char *ssl_cipher_list);
-static void send_init_prng(ssl_ctl_t * ctl, prng_seed_t seedtype, const char *path);
-static void send_certfp_method(ssl_ctl_t *ctl, int method);
+static void ssld_update_config_one(ssl_ctl_t *ctl);
+static void send_new_ssl_certs_one(ssl_ctl_t * ctl);
+static void send_certfp_method(ssl_ctl_t *ctl);
static rb_dlink_list ssl_daemons;
}
}
- start_ssldaemon(ServerInfo.ssld_count, ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params, ServerInfo.ssl_cipher_list);
+ ssld_spin_count = 0;
+ last_spin = 0;
+ ssld_wait = 0;
+ start_ssldaemon(ServerInfo.ssld_count);
}
static void
{
ssld_count--;
ilog(L_MAIN, "ssld helper died - attempting to restart");
- sendto_realops_snomask(SNO_GENERAL, L_ALL, "ssld helper died - attempting to restart");
- start_ssldaemon(1, ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params, ServerInfo.ssl_cipher_list);
+ sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "ssld helper died - attempting to restart");
+ start_ssldaemon(1);
}
}
{
int start = ServerInfo.ssld_count - get_ssld_count();
ilog(L_MAIN, "Attempting to restart ssld processes");
- sendto_realops_snomask(SNO_GENERAL, L_ALL, "Attempt to restart ssld processes");
- start_ssldaemon(start, ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params, ServerInfo.ssl_cipher_list);
+ sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Attempt to restart ssld processes");
+ start_ssldaemon(start);
}
}
int
-start_ssldaemon(int count, const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params, const char *ssl_cipher_list)
+start_ssldaemon(int count)
{
rb_fde_t *F1, *F2;
rb_fde_t *P1, *P2;
-#ifdef _WIN32
- const char *suffix = ".exe";
-#else
- const char *suffix = "";
-#endif
-
char fullpath[PATH_MAX + 1];
char fdarg[6];
const char *parv[2];
if(ssld_spin_count > 20 && (rb_current_time() - last_spin < 5))
{
ilog(L_MAIN, "ssld helper is spinning - will attempt to restart in 1 minute");
- sendto_realops_snomask(SNO_GENERAL, L_ALL,
+ sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
"ssld helper is spinning - will attempt to restart in 1 minute");
rb_event_add("restart_ssld_event", restart_ssld_event, NULL, 60);
ssld_wait = 1;
if(ssld_path == NULL)
{
- snprintf(fullpath, sizeof(fullpath), "%s%cssld%s", ircd_paths[IRCD_PATH_LIBEXEC], RB_PATH_SEPARATOR, suffix);
+ snprintf(fullpath, sizeof(fullpath), "%s/ssld", ircd_paths[IRCD_PATH_LIBEXEC]);
if(access(fullpath, X_OK) == -1)
{
- snprintf(fullpath, sizeof(fullpath), "%s%cbin%cssld%s",
- ConfigFileEntry.dpath, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR, suffix);
+ snprintf(fullpath, sizeof(fullpath), "%s/bin/ssld", ConfigFileEntry.dpath);
if(access(fullpath, X_OK) == -1)
{
ilog(L_MAIN,
- "Unable to execute ssld%s in %s or %s/bin",
- suffix, ircd_paths[IRCD_PATH_LIBEXEC], ConfigFileEntry.dpath);
+ "Unable to execute ssld in %s or %s/bin",
+ ircd_paths[IRCD_PATH_LIBEXEC], ConfigFileEntry.dpath);
return 0;
}
}
rb_setenv("CTL_PIPE", fdarg, 1);
snprintf(s_pid, sizeof(s_pid), "%d", (int)getpid());
rb_setenv("CTL_PPID", s_pid, 1);
-#ifdef _WIN32
- SetHandleInformation((HANDLE) rb_get_fd(F2), HANDLE_FLAG_INHERIT, 1);
- SetHandleInformation((HANDLE) rb_get_fd(P1), HANDLE_FLAG_INHERIT, 1);
-#endif
+
+ rb_clear_cloexec(F2);
+ rb_clear_cloexec(P1);
pid = rb_spawn_process(ssld_path, (const char **) parv);
if(pid == -1)
rb_close(P1);
ctl = allocate_ssl_daemon(F1, P2, pid);
if(ircd_ssl_ok)
- {
- send_init_prng(ctl, RB_PRNG_DEFAULT, NULL);
- send_certfp_method(ctl, ConfigFileEntry.certfp_method);
-
- if(ssl_cert != NULL && ssl_private_key != NULL)
- send_new_ssl_certs_one(ctl, ssl_cert, ssl_private_key,
- ssl_dh_params != NULL ? ssl_dh_params : "",
- ssl_cipher_list != NULL ? ssl_cipher_list : "");
- }
+ ssld_update_config_one(ctl);
ssl_read_ctl(ctl->F, ctl);
ssl_do_pipe(P2, ctl);
}
static void
-ssl_process_zipstats(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf)
-{
- struct Client *server;
- struct ZipStats *zips;
- char *parv[7];
- (void) rb_string_to_array(ctl_buf->buf, parv, 6);
- server = find_server(NULL, parv[1]);
- if(server == NULL || server->localClient == NULL || !IsCapable(server, CAP_ZIP))
- return;
- if(server->localClient->zipstats == NULL)
- server->localClient->zipstats = rb_malloc(sizeof(struct ZipStats));
+ssl_process_open_fd(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf)
+{
+ struct Client *client_p;
+ uint32_t fd;
+
+ if(ctl_buf->buflen < 5)
+ return; /* bogus message..drop it.. XXX should warn here */
- zips = server->localClient->zipstats;
+ fd = buf_to_uint32(&ctl_buf->buf[1]);
+ client_p = find_cli_connid_hash(fd);
+ if(client_p == NULL || client_p->localClient == NULL)
+ return;
- zips->in += strtoull(parv[2], NULL, 10);
- zips->in_wire += strtoull(parv[3], NULL, 10);
- zips->out += strtoull(parv[4], NULL, 10);
- zips->out_wire += strtoull(parv[5], NULL, 10);
+ if(client_p->localClient->ssl_callback)
+ {
+ SSL_OPEN_CB *hdl = client_p->localClient->ssl_callback;
- if(zips->in > 0)
- zips->in_ratio = ((double) (zips->in - zips->in_wire) / (double) zips->in) * 100.00;
- else
- zips->in_ratio = 0;
+ client_p->localClient->ssl_callback = NULL;
- if(zips->out > 0)
- zips->out_ratio = ((double) (zips->out - zips->out_wire) / (double) zips->out) * 100.00;
- else
- zips->out_ratio = 0;
+ hdl(client_p, RB_OK);
+ }
}
static void
fd = buf_to_uint32(&ctl_buf->buf[1]);
rb_strlcpy(reason, &ctl_buf->buf[5], sizeof(reason));
client_p = find_cli_connid_hash(fd);
- if(client_p == NULL)
+ if(client_p == NULL || client_p->localClient == NULL)
return;
+
+ if(IsAnyServer(client_p))
+ {
+ sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "ssld error for %s: %s", client_p->name, reason);
+ ilog(L_SERVER, "ssld error for %s: %s", log_client_name(client_p, SHOW_IP), reason);
+ }
+
+ /* if there is still a pending callback, call it now */
+ if(client_p->localClient->ssl_callback)
+ {
+ SSL_OPEN_CB *hdl = client_p->localClient->ssl_callback;
+
+ client_p->localClient->ssl_callback = NULL;
+
+ if (hdl(client_p, RB_ERROR_SSL))
+ {
+ /* the callback has exited the client */
+ return;
+ }
+ }
+
if(IsAnyServer(client_p) || IsRegistered(client_p))
{
/* read any last moment ERROR, QUIT or the like -- jilles */
if (IsAnyDead(client_p))
return;
}
- if(IsAnyServer(client_p))
- {
- sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL, "ssld error for %s: %s", client_p->name, reason);
- ilog(L_SERVER, "ssld error for %s: %s", log_client_name(client_p, SHOW_IP), reason);
- }
exit_client(client_p, client_p, &me, reason);
}
{
struct Client *client_p;
uint32_t fd;
+ uint32_t certfp_method;
uint32_t len;
uint8_t *certfp;
char *certfp_string;
+ const char *method_string;
+ int method_len;
- if(ctl_buf->buflen > 5 + RB_SSL_CERTFP_LEN)
+ if(ctl_buf->buflen > 13 + RB_SSL_CERTFP_LEN)
return; /* bogus message..drop it.. XXX should warn here */
fd = buf_to_uint32(&ctl_buf->buf[1]);
- len = buf_to_uint32(&ctl_buf->buf[5]);
- certfp = (uint8_t *)&ctl_buf->buf[9];
+ certfp_method = buf_to_uint32(&ctl_buf->buf[5]);
+ len = buf_to_uint32(&ctl_buf->buf[9]);
+ certfp = (uint8_t *)&ctl_buf->buf[13];
client_p = find_cli_connid_hash(fd);
if(client_p == NULL)
return;
+
+ switch (certfp_method) {
+ case RB_SSL_CERTFP_METH_CERT_SHA1:
+ method_string = CERTFP_PREFIX_CERT_SHA1;
+ break;
+ case RB_SSL_CERTFP_METH_CERT_SHA256:
+ method_string = CERTFP_PREFIX_CERT_SHA256;
+ break;
+ case RB_SSL_CERTFP_METH_CERT_SHA512:
+ method_string = CERTFP_PREFIX_CERT_SHA512;
+ break;
+ case RB_SSL_CERTFP_METH_SPKI_SHA256:
+ method_string = CERTFP_PREFIX_SPKI_SHA256;
+ break;
+ case RB_SSL_CERTFP_METH_SPKI_SHA512:
+ method_string = CERTFP_PREFIX_SPKI_SHA512;
+ break;
+ default:
+ return;
+ }
+ method_len = strlen(method_string);
+
rb_free(client_p->certfp);
- certfp_string = rb_malloc(len * 2 + 1);
+ certfp_string = rb_malloc(method_len + len * 2 + 1);
+ rb_strlcpy(certfp_string, method_string, method_len + len * 2 + 1);
for(uint32_t i = 0; i < len; i++)
- snprintf(certfp_string + 2 * i, 3, "%02x",
+ snprintf(certfp_string + method_len + 2 * i, 3, "%02x",
certfp[i]);
client_p->certfp = certfp_string;
}
case 'N':
ircd_ssl_ok = false; /* ssld says it can't do ssl/tls */
break;
+ case 'O':
+ ssl_process_open_fd(ctl, ctl_buf);
+ break;
case 'D':
ssl_process_dead_fd(ctl, ctl_buf);
break;
case 'F':
ssl_process_certfp(ctl, ctl_buf);
break;
- case 'S':
- ssl_process_zipstats(ctl, ctl_buf);
- break;
case 'I':
ircd_ssl_ok = false;
ilog(L_MAIN, "%s", cannot_setup_ssl);
- sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", cannot_setup_ssl);
+ sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s", cannot_setup_ssl);
break;
case 'U':
ircd_zlib_ok = 0;
ircd_ssl_ok = false;
ilog(L_MAIN, "%s", no_ssl_or_zlib);
- sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", no_ssl_or_zlib);
+ sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s", no_ssl_or_zlib);
ssl_killall();
return;
case 'V':
break;
default:
ilog(L_MAIN, "Received invalid command from ssld: %s", ctl_buf->buf);
- sendto_realops_snomask(SNO_GENERAL, L_ALL, "Received invalid command from ssld");
+ sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Received invalid command from ssld");
break;
}
rb_dlinkDelete(ptr, &ctl->readq);
static void
-send_new_ssl_certs_one(ssl_ctl_t * ctl, const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params, const char *ssl_cipher_list)
+send_new_ssl_certs_one(ssl_ctl_t * ctl)
{
- size_t len;
+ size_t len = 5;
- len = strlen(ssl_cert) + strlen(ssl_private_key) + strlen(ssl_dh_params) + 5;
- if(len > sizeof(tmpbuf))
- {
- sendto_realops_snomask(SNO_GENERAL, L_ALL,
- "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
- len, sizeof(tmpbuf));
- ilog(L_MAIN,
- "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
- len, sizeof(tmpbuf));
+ if(ServerInfo.ssl_cert)
+ len += strlen(ServerInfo.ssl_cert);
+ else
return;
- }
- len = snprintf(tmpbuf, sizeof(tmpbuf), "K%c%s%c%s%c%s%c%s%c", nul, ssl_cert, nul,
- ssl_private_key, nul, ssl_dh_params, nul,
- ssl_cipher_list != NULL ? ssl_cipher_list : "", nul);
- ssl_cmd_write_queue(ctl, NULL, 0, tmpbuf, len);
-}
-static void
-send_init_prng(ssl_ctl_t * ctl, prng_seed_t seedtype, const char *path)
-{
- size_t len;
- const char *s;
- uint8_t seed = (uint8_t) seedtype;
+ if(ServerInfo.ssl_private_key)
+ len += strlen(ServerInfo.ssl_private_key);
- if(path == NULL)
- s = "";
- else
- s = path;
+ if(ServerInfo.ssl_dh_params)
+ len += strlen(ServerInfo.ssl_dh_params);
+
+ if(ServerInfo.ssl_cipher_list)
+ len += strlen(ServerInfo.ssl_cipher_list);
- len = strlen(s) + 3;
if(len > sizeof(tmpbuf))
{
- sendto_realops_snomask(SNO_GENERAL, L_ALL,
- "Parameters for send_init_prng too long (%zd > %zd) to pass to ssld, not sending...",
+ sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
+ "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
len, sizeof(tmpbuf));
ilog(L_MAIN,
- "Parameters for send_init_prng too long (%zd > %zd) to pass to ssld, not sending...",
+ "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
len, sizeof(tmpbuf));
return;
-
}
- len = snprintf(tmpbuf, sizeof(tmpbuf), "I%c%s%c", seed, s, nul);
- ssl_cmd_write_queue(ctl, NULL, 0, tmpbuf, len);
+
+ int ret = snprintf(tmpbuf, sizeof(tmpbuf), "K%c%s%c%s%c%s%c%s%c", nul,
+ ServerInfo.ssl_cert, nul,
+ ServerInfo.ssl_private_key != NULL ? ServerInfo.ssl_private_key : "", nul,
+ ServerInfo.ssl_dh_params != NULL ? ServerInfo.ssl_dh_params : "", nul,
+ ServerInfo.ssl_cipher_list != NULL ? ServerInfo.ssl_cipher_list : "", nul);
+
+ if(ret > 5)
+ ssl_cmd_write_queue(ctl, NULL, 0, tmpbuf, (size_t) ret);
}
static void
-send_certfp_method(ssl_ctl_t *ctl, int method)
+send_certfp_method(ssl_ctl_t *ctl)
{
char buf[5];
buf[0] = 'F';
- uint32_to_buf(&buf[1], method);
+ uint32_to_buf(&buf[1], ConfigFileEntry.certfp_method);
ssl_cmd_write_queue(ctl, NULL, 0, buf, sizeof(buf));
}
+static void
+ssld_update_config_one(ssl_ctl_t *ctl)
+{
+ send_certfp_method(ctl);
+ send_new_ssl_certs_one(ctl);
+}
+
void
-send_new_ssl_certs(const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params, const char *ssl_cipher_list)
+ssld_update_config(void)
{
rb_dlink_node *ptr;
- if(ssl_cert == NULL || ssl_private_key == NULL || ssl_dh_params == NULL)
- {
- ircd_ssl_ok = false;
- return;
- }
+
RB_DLINK_FOREACH(ptr, ssl_daemons.head)
{
ssl_ctl_t *ctl = ptr->data;
- send_new_ssl_certs_one(ctl, ssl_cert, ssl_private_key, ssl_dh_params, ssl_cipher_list);
+
+ if (ctl->dead || ctl->shutdown)
+ continue;
+
+ ssld_update_config_one(ctl);
}
}
-
ssl_ctl_t *
start_ssld_accept(rb_fde_t * sslF, rb_fde_t * plainF, uint32_t id)
{
}
}
-/*
- * what we end up sending to the ssld process for ziplinks is the following
- * Z[ourfd][level][RECVQ]
- * Z = ziplinks command = buf[0]
- * ourfd = Our end of the socketpair = buf[1..4]
- * level = zip level buf[5]
- * recvqlen = our recvq len = buf[6-7]
- * recvq = any data we read prior to starting ziplinks
- */
-void
-start_zlib_session(void *data)
-{
- struct Client *server = (struct Client *) data;
- uint16_t recvqlen;
- uint8_t level;
- void *xbuf;
-
- rb_fde_t *F[2];
- rb_fde_t *xF1, *xF2;
- char *buf;
- char buf2[9];
- void *recvq_start;
-
- size_t hdr = (sizeof(uint8_t) * 2) + sizeof(uint32_t);
- size_t len;
- int cpylen, left;
-
- server->localClient->event = NULL;
-
- recvqlen = rb_linebuf_len(&server->localClient->buf_recvq);
-
- len = recvqlen + hdr;
-
- if(len > READBUF_SIZE)
- {
- sendto_realops_snomask(SNO_GENERAL, L_ALL,
- "ssld - attempted to pass message of %zd len, max len %d, giving up",
- len, READBUF_SIZE);
- ilog(L_MAIN, "ssld - attempted to pass message of %zd len, max len %d, giving up", len, READBUF_SIZE);
- exit_client(server, server, server, "ssld readbuf exceeded");
- return;
- }
-
- buf = rb_malloc(len);
- level = ConfigFileEntry.compression_level;
-
- uint32_to_buf(&buf[1], rb_get_fd(server->localClient->F));
- buf[5] = (char) level;
-
- recvq_start = &buf[6];
- server->localClient->zipstats = rb_malloc(sizeof(struct ZipStats));
-
- xbuf = recvq_start;
- left = recvqlen;
-
- do
- {
- cpylen = rb_linebuf_get(&server->localClient->buf_recvq, xbuf, left, LINEBUF_PARTIAL, LINEBUF_RAW);
- left -= cpylen;
- xbuf = (void *) (((uintptr_t) xbuf) + cpylen);
- }
- while(cpylen > 0);
-
- /* Pass the socket to ssld. */
- *buf = 'Z';
- if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF1, &xF2, "Initial zlib socketpairs") == -1)
- {
- sendto_realops_snomask(SNO_GENERAL, L_ALL, "Error creating zlib socketpair - %s", strerror(errno));
- ilog(L_MAIN, "Error creating zlib socketpairs - %s", strerror(errno));
- exit_client(server, server, server, "Error creating zlib socketpair");
- rb_free(buf);
- return;
- }
-
- if(IsSSL(server))
- {
- /* tell ssld the new connid for the ssl part*/
- buf2[0] = 'Y';
- uint32_to_buf(&buf2[1], rb_get_fd(server->localClient->F));
- uint32_to_buf(&buf2[5], rb_get_fd(xF2));
- ssl_cmd_write_queue(server->localClient->ssl_ctl, NULL, 0, buf2, sizeof(buf2));
- }
-
-
- F[0] = server->localClient->F;
- F[1] = xF1;
- del_from_zconnid_hash(server);
- server->localClient->F = xF2;
- /* need to redo as what we did before isn't valid now */
- uint32_to_buf(&buf[1], server->localClient->zconnid);
- add_to_zconnid_hash(server);
-
- server->localClient->z_ctl = which_ssld();
- if(!server->localClient->z_ctl)
- {
- exit_client(server, server, server, "Error finding available ssld");
- rb_free(buf);
- return;
- }
- server->localClient->z_ctl->cli_count++;
- ssl_cmd_write_queue(server->localClient->z_ctl, F, 2, buf, len);
- rb_free(buf);
-}
-
-static void
-collect_zipstats(void *unused)
-{
- rb_dlink_node *ptr;
- struct Client *target_p;
- char buf[sizeof(uint8_t) + sizeof(uint32_t) + HOSTLEN];
- void *odata;
- size_t len;
- uint32_t id;
-
- buf[0] = 'S';
- odata = buf + sizeof(uint8_t) + sizeof(uint32_t);
-
- RB_DLINK_FOREACH(ptr, serv_list.head)
- {
- target_p = ptr->data;
- if(IsCapable(target_p, CAP_ZIP))
- {
- len = sizeof(uint8_t) + sizeof(uint32_t);
-
- id = rb_get_fd(target_p->localClient->F);
- uint32_to_buf(&buf[1], id);
- rb_strlcpy(odata, target_p->name, (sizeof(buf) - len));
- len += strlen(odata) + 1; /* Get the \0 as well */
- ssl_cmd_write_queue(target_p->localClient->z_ctl, NULL, 0, buf, len);
- }
- }
-}
-
static void
cleanup_dead_ssl(void *unused)
{
void
init_ssld(void)
{
- rb_event_addish("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
rb_event_addish("cleanup_dead_ssld", cleanup_dead_ssl, NULL, 60);
}