2008-04-06 18:33:34 +02:00
|
|
|
/*
|
|
|
|
* sslproc.c: An interface to ssld
|
|
|
|
* Copyright (C) 2007 Aaron Sethman <androsyn@ratbox.org>
|
|
|
|
* Copyright (C) 2007 ircd-ratbox development team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
|
|
|
* USA
|
|
|
|
*/
|
|
|
|
|
2016-08-13 05:05:54 +02:00
|
|
|
namespace ircd {
|
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
#define ZIPSTATS_TIME 60
|
|
|
|
|
|
|
|
static void collect_zipstats(void *unused);
|
2008-12-03 00:49:39 +01:00
|
|
|
static void ssl_read_ctl(rb_fde_t * F, void *data);
|
2008-04-06 18:33:34 +02:00
|
|
|
static int ssld_count;
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
static char tmpbuf[READBUF_SIZE];
|
2008-04-06 18:33:34 +02:00
|
|
|
static char nul = '\0';
|
|
|
|
|
|
|
|
#define MAXPASSFD 4
|
|
|
|
#define READSIZE 1024
|
|
|
|
typedef struct _ssl_ctl_buf
|
|
|
|
{
|
|
|
|
rb_dlink_node node;
|
|
|
|
char *buf;
|
|
|
|
size_t buflen;
|
|
|
|
rb_fde_t *F[MAXPASSFD];
|
|
|
|
int nfds;
|
|
|
|
} ssl_ctl_buf_t;
|
|
|
|
|
|
|
|
|
|
|
|
struct _ssl_ctl
|
|
|
|
{
|
|
|
|
rb_dlink_node node;
|
|
|
|
int cli_count;
|
|
|
|
rb_fde_t *F;
|
|
|
|
rb_fde_t *P;
|
|
|
|
pid_t pid;
|
|
|
|
rb_dlink_list readq;
|
|
|
|
rb_dlink_list writeq;
|
2016-02-10 23:57:16 +01:00
|
|
|
uint8_t shutdown;
|
2008-07-30 00:55:08 +02:00
|
|
|
uint8_t dead;
|
2016-03-03 23:08:13 +01:00
|
|
|
char version[256];
|
2008-04-06 18:33:34 +02:00
|
|
|
};
|
|
|
|
|
2016-04-25 20:22:10 +02:00
|
|
|
static void ssld_update_config_one(ssl_ctl_t *ctl);
|
2016-04-25 20:20:45 +02:00
|
|
|
static void send_new_ssl_certs_one(ssl_ctl_t * ctl);
|
2016-04-25 20:22:10 +02:00
|
|
|
static void send_certfp_method(ssl_ctl_t *ctl);
|
2008-04-06 18:33:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
static rb_dlink_list ssl_daemons;
|
|
|
|
|
2015-12-11 06:44:31 +01:00
|
|
|
static inline uint32_t
|
|
|
|
buf_to_uint32(char *buf)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
2015-12-11 06:44:31 +01:00
|
|
|
uint32_t x;
|
2008-07-30 00:55:08 +02:00
|
|
|
memcpy(&x, buf, sizeof(x));
|
2008-04-06 18:33:34 +02:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
static inline void
|
2015-12-11 06:44:31 +01:00
|
|
|
uint32_to_buf(char *buf, uint32_t x)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
2008-07-30 00:55:08 +02:00
|
|
|
memcpy(buf, &x, sizeof(x));
|
2008-04-06 18:33:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssl_ctl_t *
|
2008-12-03 00:49:39 +01:00
|
|
|
allocate_ssl_daemon(rb_fde_t * F, rb_fde_t * P, int pid)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
|
|
|
ssl_ctl_t *ctl;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
if(F == NULL || pid < 0)
|
|
|
|
return NULL;
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl = (ssl_ctl_t *)rb_malloc(sizeof(ssl_ctl_t));
|
2008-04-06 18:33:34 +02:00
|
|
|
ctl->F = F;
|
|
|
|
ctl->P = P;
|
|
|
|
ctl->pid = pid;
|
|
|
|
ssld_count++;
|
|
|
|
rb_dlinkAdd(ctl, &ctl->node, &ssl_daemons);
|
|
|
|
return ctl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
free_ssl_daemon(ssl_ctl_t * ctl)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
|
|
|
rb_dlink_node *ptr;
|
|
|
|
ssl_ctl_buf_t *ctl_buf;
|
|
|
|
int x;
|
|
|
|
if(ctl->cli_count)
|
|
|
|
return;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
RB_DLINK_FOREACH(ptr, ctl->readq.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl_buf = (ssl_ctl_buf_t *)ptr->data;
|
2008-04-06 18:33:34 +02:00
|
|
|
for(x = 0; x < ctl_buf->nfds; x++)
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_close(ctl_buf->F[x]);
|
2008-04-06 18:33:34 +02:00
|
|
|
|
|
|
|
rb_free(ctl_buf->buf);
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_free(ctl_buf);
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH(ptr, ctl->writeq.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl_buf = (ssl_ctl_buf_t *)ptr->data;
|
2008-04-06 18:33:34 +02:00
|
|
|
for(x = 0; x < ctl_buf->nfds; x++)
|
|
|
|
rb_close(ctl_buf->F[x]);
|
|
|
|
|
|
|
|
rb_free(ctl_buf->buf);
|
|
|
|
rb_free(ctl_buf);
|
|
|
|
}
|
|
|
|
rb_close(ctl->F);
|
|
|
|
rb_close(ctl->P);
|
|
|
|
rb_dlinkDelete(&ctl->node, &ssl_daemons);
|
|
|
|
rb_free(ctl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *ssld_path;
|
|
|
|
|
|
|
|
static int ssld_spin_count = 0;
|
|
|
|
static time_t last_spin;
|
|
|
|
static int ssld_wait = 0;
|
|
|
|
|
|
|
|
|
2016-02-10 23:57:16 +01:00
|
|
|
void
|
|
|
|
restart_ssld(void)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *next;
|
|
|
|
ssl_ctl_t *ctl;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, ssl_daemons.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl = (ssl_ctl_t *)ptr->data;
|
2016-02-10 23:57:16 +01:00
|
|
|
if(ctl->dead)
|
|
|
|
continue;
|
|
|
|
if(ctl->shutdown)
|
|
|
|
continue;
|
|
|
|
ctl->shutdown = 1;
|
|
|
|
ssld_count--;
|
|
|
|
if(!ctl->cli_count)
|
|
|
|
{
|
|
|
|
rb_kill(ctl->pid, SIGKILL);
|
|
|
|
free_ssl_daemon(ctl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 23:22:37 +02:00
|
|
|
ssld_spin_count = 0;
|
|
|
|
last_spin = 0;
|
|
|
|
ssld_wait = 0;
|
2016-04-25 20:20:45 +02:00
|
|
|
start_ssldaemon(ServerInfo.ssld_count);
|
2016-02-10 23:57:16 +01:00
|
|
|
}
|
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
static void
|
|
|
|
ssl_killall(void)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *next;
|
|
|
|
ssl_ctl_t *ctl;
|
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, ssl_daemons.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl = (ssl_ctl_t *)ptr->data;
|
2008-04-06 18:33:34 +02:00
|
|
|
if(ctl->dead)
|
|
|
|
continue;
|
|
|
|
ctl->dead = 1;
|
2016-02-10 23:57:16 +01:00
|
|
|
if(!ctl->shutdown)
|
|
|
|
ssld_count--;
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_kill(ctl->pid, SIGKILL);
|
2016-02-10 23:57:16 +01:00
|
|
|
if(!ctl->cli_count)
|
|
|
|
free_ssl_daemon(ctl);
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
ssl_dead(ssl_ctl_t * ctl)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
|
|
|
if(ctl->dead)
|
|
|
|
return;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
ctl->dead = 1;
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_kill(ctl->pid, SIGKILL); /* make sure the process is really gone */
|
2016-02-10 23:57:16 +01:00
|
|
|
|
|
|
|
if(!ctl->shutdown)
|
|
|
|
{
|
|
|
|
ssld_count--;
|
|
|
|
ilog(L_MAIN, "ssld helper died - attempting to restart");
|
|
|
|
sendto_realops_snomask(SNO_GENERAL, L_ALL, "ssld helper died - attempting to restart");
|
2016-04-25 20:20:45 +02:00
|
|
|
start_ssldaemon(1);
|
2016-02-10 23:57:16 +01:00
|
|
|
}
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
ssl_do_pipe(rb_fde_t * F, void *data)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
|
|
|
int retlen;
|
2016-07-13 07:17:21 +02:00
|
|
|
ssl_ctl_t *ctl = (ssl_ctl_t *)data;
|
2008-04-06 18:33:34 +02:00
|
|
|
retlen = rb_write(F, "0", 1);
|
|
|
|
if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
|
|
|
|
{
|
|
|
|
ssl_dead(ctl);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rb_setselect(F, RB_SELECT_READ, ssl_do_pipe, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
restart_ssld_event(void *unused)
|
|
|
|
{
|
|
|
|
ssld_spin_count = 0;
|
|
|
|
last_spin = 0;
|
|
|
|
ssld_wait = 0;
|
|
|
|
if(ServerInfo.ssld_count > get_ssld_count())
|
|
|
|
{
|
|
|
|
int start = ServerInfo.ssld_count - get_ssld_count();
|
|
|
|
ilog(L_MAIN, "Attempting to restart ssld processes");
|
2008-04-08 18:16:19 +02:00
|
|
|
sendto_realops_snomask(SNO_GENERAL, L_ALL, "Attempt to restart ssld processes");
|
2016-04-25 20:20:45 +02:00
|
|
|
start_ssldaemon(start);
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-04-25 20:20:45 +02:00
|
|
|
start_ssldaemon(int count)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
|
|
|
rb_fde_t *F1, *F2;
|
|
|
|
rb_fde_t *P1, *P2;
|
2008-12-03 00:49:39 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
const char *suffix = ".exe";
|
|
|
|
#else
|
|
|
|
const char *suffix = "";
|
|
|
|
#endif
|
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
char fullpath[PATH_MAX + 1];
|
|
|
|
char fdarg[6];
|
|
|
|
const char *parv[2];
|
|
|
|
char buf[128];
|
2008-12-03 00:49:39 +01:00
|
|
|
char s_pid[10];
|
2008-04-06 18:33:34 +02:00
|
|
|
pid_t pid;
|
|
|
|
int started = 0, i;
|
|
|
|
|
|
|
|
if(ssld_wait)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(ssld_spin_count > 20 && (rb_current_time() - last_spin < 5))
|
|
|
|
{
|
2008-08-08 18:01:52 +02:00
|
|
|
ilog(L_MAIN, "ssld helper is spinning - will attempt to restart in 1 minute");
|
2008-12-03 00:49:39 +01:00
|
|
|
sendto_realops_snomask(SNO_GENERAL, L_ALL,
|
|
|
|
"ssld helper is spinning - will attempt to restart in 1 minute");
|
2008-04-06 18:33:34 +02:00
|
|
|
rb_event_add("restart_ssld_event", restart_ssld_event, NULL, 60);
|
|
|
|
ssld_wait = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssld_spin_count++;
|
|
|
|
last_spin = rb_current_time();
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
if(ssld_path == NULL)
|
|
|
|
{
|
2016-08-14 05:35:06 +02:00
|
|
|
snprintf(fullpath, sizeof(fullpath), "%s%cssld%s", fs::path::get(fs::path::LIBEXEC), RB_PATH_SEPARATOR, suffix);
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
if(access(fullpath, X_OK) == -1)
|
|
|
|
{
|
2016-03-25 00:45:28 +01:00
|
|
|
snprintf(fullpath, sizeof(fullpath), "%s%cbin%cssld%s",
|
|
|
|
ConfigFileEntry.dpath, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR, suffix);
|
2008-04-06 18:33:34 +02:00
|
|
|
if(access(fullpath, X_OK) == -1)
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
ilog(L_MAIN,
|
2012-03-01 03:41:09 +01:00
|
|
|
"Unable to execute ssld%s in %s or %s/bin",
|
2016-08-14 05:35:06 +02:00
|
|
|
suffix, fs::path::get(fs::path::LIBEXEC), ConfigFileEntry.dpath);
|
2008-12-03 00:49:39 +01:00
|
|
|
return 0;
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ssld_path = rb_strdup(fullpath);
|
|
|
|
}
|
2016-01-06 09:15:23 +01:00
|
|
|
rb_strlcpy(buf, "-ircd ssld daemon", sizeof(buf));
|
2008-04-06 18:33:34 +02:00
|
|
|
parv[0] = buf;
|
|
|
|
parv[1] = NULL;
|
|
|
|
|
|
|
|
for(i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
ssl_ctl_t *ctl;
|
2009-05-12 23:30:46 +02:00
|
|
|
if(rb_socketpair(AF_UNIX, SOCK_DGRAM, 0, &F1, &F2, "SSL/TLS handle passing socket") == -1)
|
|
|
|
{
|
|
|
|
ilog(L_MAIN, "Unable to create ssld - rb_socketpair failed: %s", strerror(errno));
|
|
|
|
return started;
|
|
|
|
}
|
2014-03-03 05:25:47 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
rb_set_buffers(F1, READBUF_SIZE);
|
|
|
|
rb_set_buffers(F2, READBUF_SIZE);
|
2016-02-10 02:25:32 +01:00
|
|
|
snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(F2));
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_setenv("CTL_FD", fdarg, 1);
|
2009-05-13 22:07:57 +02:00
|
|
|
if(rb_pipe(&P1, &P2, "SSL/TLS pipe") == -1)
|
|
|
|
{
|
|
|
|
ilog(L_MAIN, "Unable to create ssld - rb_pipe failed: %s", strerror(errno));
|
|
|
|
return started;
|
|
|
|
}
|
2016-02-10 02:25:32 +01:00
|
|
|
snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(P1));
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_setenv("CTL_PIPE", fdarg, 1);
|
2016-02-10 02:25:32 +01:00
|
|
|
snprintf(s_pid, sizeof(s_pid), "%d", (int)getpid());
|
2008-12-03 00:49:39 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
pid = rb_spawn_process(ssld_path, (const char **) parv);
|
2008-04-06 18:33:34 +02:00
|
|
|
if(pid == -1)
|
|
|
|
{
|
|
|
|
ilog(L_MAIN, "Unable to create ssld: %s\n", strerror(errno));
|
|
|
|
rb_close(F1);
|
|
|
|
rb_close(F2);
|
|
|
|
rb_close(P1);
|
|
|
|
rb_close(P2);
|
|
|
|
return started;
|
|
|
|
}
|
|
|
|
started++;
|
|
|
|
rb_close(F2);
|
|
|
|
rb_close(P1);
|
|
|
|
ctl = allocate_ssl_daemon(F1, P2, pid);
|
2016-03-19 06:57:32 +01:00
|
|
|
if(ircd_ssl_ok)
|
2016-04-25 20:22:10 +02:00
|
|
|
ssld_update_config_one(ctl);
|
2008-04-06 18:33:34 +02:00
|
|
|
ssl_read_ctl(ctl->F, ctl);
|
|
|
|
ssl_do_pipe(P2, ctl);
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
return started;
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
ssl_process_zipstats(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *server;
|
2008-04-06 18:33:34 +02:00
|
|
|
struct ZipStats *zips;
|
2009-02-07 23:26:05 +01:00
|
|
|
char *parv[7];
|
2015-03-28 00:04:39 +01:00
|
|
|
(void) rb_string_to_array(ctl_buf->buf, parv, 6);
|
2008-04-06 18:33:34 +02:00
|
|
|
server = find_server(NULL, parv[1]);
|
|
|
|
if(server == NULL || server->localClient == NULL || !IsCapable(server, CAP_ZIP))
|
|
|
|
return;
|
|
|
|
if(server->localClient->zipstats == NULL)
|
2016-07-13 07:17:21 +02:00
|
|
|
server->localClient->zipstats = (ZipStats *)rb_malloc(sizeof(struct ZipStats));
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
zips = server->localClient->zipstats;
|
|
|
|
|
|
|
|
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);
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
if(zips->in > 0)
|
2008-12-03 00:49:39 +01:00
|
|
|
zips->in_ratio = ((double) (zips->in - zips->in_wire) / (double) zips->in) * 100.00;
|
2008-04-06 18:33:34 +02:00
|
|
|
else
|
|
|
|
zips->in_ratio = 0;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
if(zips->out > 0)
|
2008-12-03 00:49:39 +01:00
|
|
|
zips->out_ratio = ((double) (zips->out - zips->out_wire) / (double) zips->out) * 100.00;
|
2008-04-06 18:33:34 +02:00
|
|
|
else
|
|
|
|
zips->out_ratio = 0;
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:48:35 +02:00
|
|
|
static void
|
|
|
|
ssl_process_open_fd(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf)
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *client_p;
|
2016-04-24 12:48:35 +02:00
|
|
|
uint32_t fd;
|
|
|
|
|
|
|
|
if(ctl_buf->buflen < 5)
|
|
|
|
return; /* bogus message..drop it.. XXX should warn here */
|
|
|
|
|
|
|
|
fd = buf_to_uint32(&ctl_buf->buf[1]);
|
|
|
|
client_p = find_cli_connid_hash(fd);
|
|
|
|
if(client_p == NULL || client_p->localClient == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(client_p->localClient->ssl_callback)
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::SSL_OPEN_CB *hdl = client_p->localClient->ssl_callback;
|
2016-04-24 12:48:35 +02:00
|
|
|
|
|
|
|
client_p->localClient->ssl_callback = NULL;
|
|
|
|
|
2016-04-25 22:12:44 +02:00
|
|
|
hdl(client_p, RB_OK);
|
2016-04-24 12:48:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
ssl_process_dead_fd(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *client_p;
|
2008-04-06 18:33:34 +02:00
|
|
|
char reason[256];
|
2015-12-11 06:44:31 +01:00
|
|
|
uint32_t fd;
|
2008-04-06 18:33:34 +02:00
|
|
|
|
|
|
|
if(ctl_buf->buflen < 6)
|
2008-12-03 00:49:39 +01:00
|
|
|
return; /* bogus message..drop it.. XXX should warn here */
|
|
|
|
|
2015-12-11 06:44:31 +01:00
|
|
|
fd = buf_to_uint32(&ctl_buf->buf[1]);
|
2008-04-06 18:33:34 +02:00
|
|
|
rb_strlcpy(reason, &ctl_buf->buf[5], sizeof(reason));
|
2015-12-12 12:20:51 +01:00
|
|
|
client_p = find_cli_connid_hash(fd);
|
2016-04-24 12:48:35 +02:00
|
|
|
if(client_p == NULL || client_p->localClient == NULL)
|
2008-04-06 18:33:34 +02:00
|
|
|
return;
|
2016-04-24 12:48:35 +02:00
|
|
|
|
2016-08-23 04:33:36 +02:00
|
|
|
if(is_any_server(*client_p))
|
2016-04-24 12:48:35 +02:00
|
|
|
{
|
2016-08-23 04:33:36 +02:00
|
|
|
sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !is_server(*client_p) ? L_NETWIDE : L_ALL, "ssld error for %s: %s", client_p->name, reason);
|
2016-04-24 12:48:35 +02:00
|
|
|
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)
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::SSL_OPEN_CB *hdl = client_p->localClient->ssl_callback;
|
2016-04-24 12:48:35 +02:00
|
|
|
|
|
|
|
client_p->localClient->ssl_callback = NULL;
|
|
|
|
|
2016-04-25 22:12:44 +02:00
|
|
|
if (hdl(client_p, RB_ERROR_SSL))
|
|
|
|
{
|
|
|
|
/* the callback has exited the client */
|
|
|
|
return;
|
|
|
|
}
|
2016-04-24 12:48:35 +02:00
|
|
|
}
|
|
|
|
|
2016-08-23 04:33:36 +02:00
|
|
|
if(is_any_server(*client_p) || is_registered(*client_p))
|
2009-01-15 18:08:46 +01:00
|
|
|
{
|
|
|
|
/* read any last moment ERROR, QUIT or the like -- jilles */
|
|
|
|
if (!strcmp(reason, "Remote host closed the connection"))
|
|
|
|
read_packet(client_p->localClient->F, client_p);
|
2016-08-23 04:33:36 +02:00
|
|
|
if (is_any_dead(*client_p))
|
2009-01-15 18:08:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-04-06 18:33:34 +02:00
|
|
|
exit_client(client_p, client_p, &me, reason);
|
|
|
|
}
|
|
|
|
|
2015-12-11 15:19:50 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
ssl_process_cipher_string(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf)
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *client_p;
|
2015-12-11 15:19:50 +01:00
|
|
|
const char *cstring;
|
|
|
|
uint32_t fd;
|
|
|
|
|
|
|
|
if(ctl_buf->buflen < 6)
|
|
|
|
return; /* bogus message..drop it.. XXX should warn here */
|
|
|
|
|
|
|
|
fd = buf_to_uint32(&ctl_buf->buf[1]);
|
|
|
|
cstring = (const char *)&ctl_buf->buf[5];
|
|
|
|
|
|
|
|
if(EmptyString(cstring))
|
|
|
|
return;
|
|
|
|
|
2015-12-12 12:20:51 +01:00
|
|
|
client_p = find_cli_connid_hash(fd);
|
2015-12-11 15:19:50 +01:00
|
|
|
if(client_p != NULL && client_p->localClient != NULL)
|
|
|
|
{
|
|
|
|
rb_free(client_p->localClient->cipher_string);
|
|
|
|
client_p->localClient->cipher_string = rb_strdup(cstring);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-31 19:04:20 +01:00
|
|
|
static void
|
|
|
|
ssl_process_certfp(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf)
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *client_p;
|
2015-12-11 06:44:31 +01:00
|
|
|
uint32_t fd;
|
2016-04-25 21:12:27 +02:00
|
|
|
uint32_t certfp_method;
|
2015-12-11 06:44:31 +01:00
|
|
|
uint32_t len;
|
2010-01-31 19:04:20 +01:00
|
|
|
uint8_t *certfp;
|
2010-02-06 00:18:27 +01:00
|
|
|
char *certfp_string;
|
2016-04-25 21:12:27 +02:00
|
|
|
const char *method_string;
|
|
|
|
int method_len;
|
2010-01-31 19:04:20 +01:00
|
|
|
|
2016-04-25 21:12:27 +02:00
|
|
|
if(ctl_buf->buflen > 13 + RB_SSL_CERTFP_LEN)
|
2010-01-31 19:04:20 +01:00
|
|
|
return; /* bogus message..drop it.. XXX should warn here */
|
|
|
|
|
2015-12-11 06:44:31 +01:00
|
|
|
fd = buf_to_uint32(&ctl_buf->buf[1]);
|
2016-04-25 21:12:27 +02:00
|
|
|
certfp_method = buf_to_uint32(&ctl_buf->buf[5]);
|
|
|
|
len = buf_to_uint32(&ctl_buf->buf[9]);
|
|
|
|
certfp = (uint8_t *)&ctl_buf->buf[13];
|
2015-12-12 12:20:51 +01:00
|
|
|
client_p = find_cli_connid_hash(fd);
|
2010-01-31 19:04:20 +01:00
|
|
|
if(client_p == NULL)
|
|
|
|
return;
|
2016-04-25 21:12:27 +02:00
|
|
|
|
|
|
|
switch (certfp_method) {
|
|
|
|
case RB_SSL_CERTFP_METH_CERT_SHA1:
|
2016-04-26 21:19:59 +02:00
|
|
|
method_string = CERTFP_PREFIX_CERT_SHA1;
|
|
|
|
break;
|
2016-04-25 21:12:27 +02:00
|
|
|
case RB_SSL_CERTFP_METH_CERT_SHA256:
|
2016-04-26 21:19:59 +02:00
|
|
|
method_string = CERTFP_PREFIX_CERT_SHA256;
|
|
|
|
break;
|
2016-04-25 21:12:27 +02:00
|
|
|
case RB_SSL_CERTFP_METH_CERT_SHA512:
|
2016-04-26 21:19:59 +02:00
|
|
|
method_string = CERTFP_PREFIX_CERT_SHA512;
|
2016-04-25 21:12:27 +02:00
|
|
|
break;
|
|
|
|
case RB_SSL_CERTFP_METH_SPKI_SHA256:
|
2016-04-26 21:19:59 +02:00
|
|
|
method_string = CERTFP_PREFIX_SPKI_SHA256;
|
2016-04-25 21:12:27 +02:00
|
|
|
break;
|
|
|
|
case RB_SSL_CERTFP_METH_SPKI_SHA512:
|
2016-04-26 21:19:59 +02:00
|
|
|
method_string = CERTFP_PREFIX_SPKI_SHA512;
|
2016-04-25 21:12:27 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
method_len = strlen(method_string);
|
|
|
|
|
2010-02-06 00:18:27 +01:00
|
|
|
rb_free(client_p->certfp);
|
2016-07-13 07:17:21 +02:00
|
|
|
certfp_string = (char *)rb_malloc(method_len + len * 2 + 1);
|
2016-05-15 05:57:16 +02:00
|
|
|
rb_strlcpy(certfp_string, method_string, method_len + len * 2 + 1);
|
2016-03-23 03:53:56 +01:00
|
|
|
for(uint32_t i = 0; i < len; i++)
|
2016-04-25 21:12:27 +02:00
|
|
|
snprintf(certfp_string + method_len + 2 * i, 3, "%02x",
|
2010-01-31 19:04:20 +01:00
|
|
|
certfp[i]);
|
2010-02-06 00:18:27 +01:00
|
|
|
client_p->certfp = certfp_string;
|
2010-01-31 19:04:20 +01:00
|
|
|
}
|
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
ssl_process_cmd_recv(ssl_ctl_t * ctl)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
|
|
|
static const char *cannot_setup_ssl = "ssld cannot setup ssl, check your certificates and private key";
|
|
|
|
static const char *no_ssl_or_zlib = "ssld has neither SSL/TLS or zlib support killing all sslds";
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_dlink_node *ptr, *next;
|
2008-04-06 18:33:34 +02:00
|
|
|
ssl_ctl_buf_t *ctl_buf;
|
2016-03-23 03:53:56 +01:00
|
|
|
unsigned long len;
|
2016-03-03 23:08:13 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
if(ctl->dead)
|
|
|
|
return;
|
2016-03-03 23:08:13 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, ctl->readq.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl_buf = (ssl_ctl_buf_t *)ptr->data;
|
2008-12-03 00:49:39 +01:00
|
|
|
switch (*ctl_buf->buf)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
case 'N':
|
2016-03-19 06:57:32 +01:00
|
|
|
ircd_ssl_ok = false; /* ssld says it can't do ssl/tls */
|
2008-12-03 00:49:39 +01:00
|
|
|
break;
|
2016-04-24 12:48:35 +02:00
|
|
|
case 'O':
|
|
|
|
ssl_process_open_fd(ctl, ctl_buf);
|
|
|
|
break;
|
2008-12-03 00:49:39 +01:00
|
|
|
case 'D':
|
|
|
|
ssl_process_dead_fd(ctl, ctl_buf);
|
|
|
|
break;
|
2015-12-11 15:19:50 +01:00
|
|
|
case 'C':
|
|
|
|
ssl_process_cipher_string(ctl, ctl_buf);
|
|
|
|
break;
|
2010-01-31 19:04:20 +01:00
|
|
|
case 'F':
|
|
|
|
ssl_process_certfp(ctl, ctl_buf);
|
|
|
|
break;
|
2008-12-03 00:49:39 +01:00
|
|
|
case 'S':
|
|
|
|
ssl_process_zipstats(ctl, ctl_buf);
|
|
|
|
break;
|
|
|
|
case 'I':
|
2016-03-19 06:57:32 +01:00
|
|
|
ircd_ssl_ok = false;
|
2012-01-25 20:22:35 +01:00
|
|
|
ilog(L_MAIN, "%s", cannot_setup_ssl);
|
2012-01-25 20:24:04 +01:00
|
|
|
sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", cannot_setup_ssl);
|
2014-03-02 21:58:38 +01:00
|
|
|
break;
|
2008-12-03 00:49:39 +01:00
|
|
|
case 'U':
|
2016-03-19 06:58:57 +01:00
|
|
|
ircd_zlib_ok = 0;
|
2016-03-19 06:57:32 +01:00
|
|
|
ircd_ssl_ok = false;
|
2012-01-25 20:22:35 +01:00
|
|
|
ilog(L_MAIN, "%s", no_ssl_or_zlib);
|
2012-01-25 20:24:04 +01:00
|
|
|
sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", no_ssl_or_zlib);
|
2008-12-03 00:49:39 +01:00
|
|
|
ssl_killall();
|
2016-03-19 06:04:38 +01:00
|
|
|
return;
|
2016-03-03 23:08:13 +01:00
|
|
|
case 'V':
|
|
|
|
len = ctl_buf->buflen - 1;
|
|
|
|
if (len > sizeof(ctl->version) - 1)
|
|
|
|
len = sizeof(ctl->version) - 1;
|
|
|
|
strncpy(ctl->version, &ctl_buf->buf[1], len);
|
2008-12-03 00:49:39 +01:00
|
|
|
case 'z':
|
2016-03-19 06:58:57 +01:00
|
|
|
ircd_zlib_ok = 0;
|
2008-12-03 00:49:39 +01:00
|
|
|
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");
|
|
|
|
break;
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
rb_dlinkDelete(ptr, &ctl->readq);
|
|
|
|
rb_free(ctl_buf->buf);
|
|
|
|
rb_free(ctl_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
ssl_read_ctl(rb_fde_t * F, void *data)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
|
|
|
ssl_ctl_buf_t *ctl_buf;
|
2016-07-13 07:17:21 +02:00
|
|
|
ssl_ctl_t *ctl = (ssl_ctl_t *)data;
|
2008-04-06 18:33:34 +02:00
|
|
|
int retlen;
|
|
|
|
|
|
|
|
if(ctl->dead)
|
|
|
|
return;
|
|
|
|
do
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl_buf = (ssl_ctl_buf_t *)rb_malloc(sizeof(ssl_ctl_buf_t));
|
|
|
|
ctl_buf->buf = (char *)rb_malloc(READSIZE);
|
2008-04-06 18:33:34 +02:00
|
|
|
retlen = rb_recv_fd_buf(ctl->F, ctl_buf->buf, READSIZE, ctl_buf->F, 4);
|
|
|
|
ctl_buf->buflen = retlen;
|
2008-12-03 00:49:39 +01:00
|
|
|
if(retlen <= 0)
|
|
|
|
{
|
2008-04-06 18:33:34 +02:00
|
|
|
rb_free(ctl_buf->buf);
|
|
|
|
rb_free(ctl_buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq);
|
2008-12-03 00:49:39 +01:00
|
|
|
}
|
|
|
|
while(retlen > 0);
|
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
|
|
|
|
{
|
|
|
|
ssl_dead(ctl);
|
|
|
|
return;
|
2008-12-03 00:49:39 +01:00
|
|
|
}
|
2008-04-06 18:33:34 +02:00
|
|
|
ssl_process_cmd_recv(ctl);
|
|
|
|
rb_setselect(ctl->F, RB_SELECT_READ, ssl_read_ctl, ctl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssl_ctl_t *
|
|
|
|
which_ssld(void)
|
|
|
|
{
|
|
|
|
ssl_ctl_t *ctl, *lowest = NULL;
|
|
|
|
rb_dlink_node *ptr;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
RB_DLINK_FOREACH(ptr, ssl_daemons.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl = (ssl_ctl_t *)ptr->data;
|
2008-04-06 18:33:34 +02:00
|
|
|
if(ctl->dead)
|
|
|
|
continue;
|
2016-02-10 23:57:16 +01:00
|
|
|
if(ctl->shutdown)
|
|
|
|
continue;
|
2008-12-03 00:49:39 +01:00
|
|
|
if(lowest == NULL)
|
|
|
|
{
|
2008-04-06 18:33:34 +02:00
|
|
|
lowest = ctl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(ctl->cli_count < lowest->cli_count)
|
|
|
|
lowest = ctl;
|
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
return (lowest);
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
ssl_write_ctl(rb_fde_t * F, void *data)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ssl_ctl_t *ctl = (ssl_ctl_t *)data;
|
2008-04-06 18:33:34 +02:00
|
|
|
ssl_ctl_buf_t *ctl_buf;
|
|
|
|
rb_dlink_node *ptr, *next;
|
|
|
|
int retlen, x;
|
|
|
|
|
|
|
|
if(ctl->dead)
|
|
|
|
return;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, ctl->writeq.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl_buf = (ssl_ctl_buf_t *)ptr->data;
|
2008-04-06 18:33:34 +02:00
|
|
|
/* in theory unix sock_dgram shouldn't ever short write this.. */
|
2008-12-03 00:49:39 +01:00
|
|
|
retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf, ctl_buf->buflen, ctl->pid);
|
2008-04-06 18:33:34 +02:00
|
|
|
if(retlen > 0)
|
|
|
|
{
|
|
|
|
rb_dlinkDelete(ptr, &ctl->writeq);
|
|
|
|
for(x = 0; x < ctl_buf->nfds; x++)
|
|
|
|
rb_close(ctl_buf->F[x]);
|
|
|
|
rb_free(ctl_buf->buf);
|
|
|
|
rb_free(ctl_buf);
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
|
|
|
|
{
|
|
|
|
ssl_dead(ctl);
|
|
|
|
return;
|
2008-12-03 00:49:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-06 18:33:34 +02:00
|
|
|
rb_setselect(ctl->F, RB_SELECT_WRITE, ssl_write_ctl, ctl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
ssl_cmd_write_queue(ssl_ctl_t * ctl, rb_fde_t ** F, int count, const void *buf, size_t buflen)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
|
|
|
ssl_ctl_buf_t *ctl_buf;
|
2008-12-03 00:49:39 +01:00
|
|
|
int x;
|
2008-04-06 18:33:34 +02:00
|
|
|
|
|
|
|
/* don't bother */
|
|
|
|
if(ctl->dead)
|
|
|
|
return;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl_buf = (ssl_ctl_buf_t *)rb_malloc(sizeof(ssl_ctl_buf_t));
|
|
|
|
ctl_buf->buf = (char *)rb_malloc(buflen);
|
2008-04-06 18:33:34 +02:00
|
|
|
memcpy(ctl_buf->buf, buf, buflen);
|
|
|
|
ctl_buf->buflen = buflen;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
for(x = 0; x < count && x < MAXPASSFD; x++)
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
ctl_buf->F[x] = F[x];
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
ctl_buf->nfds = count;
|
|
|
|
rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->writeq);
|
|
|
|
ssl_write_ctl(ctl->F, ctl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2016-04-25 20:20:45 +02:00
|
|
|
send_new_ssl_certs_one(ssl_ctl_t * ctl)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
2016-05-05 05:25:59 +02:00
|
|
|
size_t len = 5;
|
|
|
|
|
|
|
|
if(ServerInfo.ssl_cert)
|
|
|
|
len += strlen(ServerInfo.ssl_cert);
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(ServerInfo.ssl_private_key)
|
|
|
|
len += strlen(ServerInfo.ssl_private_key);
|
2008-04-06 18:33:34 +02:00
|
|
|
|
2016-04-25 20:20:45 +02:00
|
|
|
if(ServerInfo.ssl_dh_params)
|
|
|
|
len += strlen(ServerInfo.ssl_dh_params);
|
2016-05-05 05:25:59 +02:00
|
|
|
|
2016-04-25 20:20:45 +02:00
|
|
|
if(ServerInfo.ssl_cipher_list)
|
|
|
|
len += strlen(ServerInfo.ssl_cipher_list);
|
2016-05-05 05:25:59 +02:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
if(len > sizeof(tmpbuf))
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
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));
|
2008-04-06 18:33:34 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-05-05 05:25:59 +02:00
|
|
|
|
|
|
|
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);
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
|
2015-12-07 08:49:30 +01:00
|
|
|
static void
|
2016-04-25 20:22:10 +02:00
|
|
|
send_certfp_method(ssl_ctl_t *ctl)
|
2015-12-07 08:49:30 +01:00
|
|
|
{
|
|
|
|
char buf[5];
|
|
|
|
|
|
|
|
buf[0] = 'F';
|
2016-04-25 20:22:10 +02:00
|
|
|
uint32_to_buf(&buf[1], ConfigFileEntry.certfp_method);
|
2015-12-07 08:49:30 +01:00
|
|
|
ssl_cmd_write_queue(ctl, NULL, 0, buf, sizeof(buf));
|
|
|
|
}
|
|
|
|
|
2016-04-25 20:22:10 +02:00
|
|
|
static void
|
|
|
|
ssld_update_config_one(ssl_ctl_t *ctl)
|
|
|
|
{
|
|
|
|
send_certfp_method(ctl);
|
|
|
|
send_new_ssl_certs_one(ctl);
|
|
|
|
}
|
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
void
|
2016-04-25 20:20:45 +02:00
|
|
|
ssld_update_config(void)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
|
|
|
rb_dlink_node *ptr;
|
2016-04-25 20:20:45 +02:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
RB_DLINK_FOREACH(ptr, ssl_daemons.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ssl_ctl_t *ctl = (ssl_ctl_t *)ptr->data;
|
2016-04-29 08:35:43 +02:00
|
|
|
|
|
|
|
if (ctl->dead || ctl->shutdown)
|
|
|
|
continue;
|
|
|
|
|
2016-04-25 20:22:10 +02:00
|
|
|
ssld_update_config_one(ctl);
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
ssl_ctl_t *
|
2015-12-11 06:44:31 +01:00
|
|
|
start_ssld_accept(rb_fde_t * sslF, rb_fde_t * plainF, uint32_t id)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
|
|
|
rb_fde_t *F[2];
|
|
|
|
ssl_ctl_t *ctl;
|
|
|
|
char buf[5];
|
|
|
|
F[0] = sslF;
|
|
|
|
F[1] = plainF;
|
|
|
|
|
|
|
|
buf[0] = 'A';
|
2015-12-11 06:44:31 +01:00
|
|
|
uint32_to_buf(&buf[1], id);
|
2008-04-06 18:33:34 +02:00
|
|
|
ctl = which_ssld();
|
2016-02-10 23:25:23 +01:00
|
|
|
if(!ctl)
|
|
|
|
return NULL;
|
2008-04-06 18:33:34 +02:00
|
|
|
ctl->cli_count++;
|
|
|
|
ssl_cmd_write_queue(ctl, F, 2, buf, sizeof(buf));
|
|
|
|
return ctl;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl_ctl_t *
|
2015-12-11 06:44:31 +01:00
|
|
|
start_ssld_connect(rb_fde_t * sslF, rb_fde_t * plainF, uint32_t id)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
|
|
|
rb_fde_t *F[2];
|
|
|
|
ssl_ctl_t *ctl;
|
|
|
|
char buf[5];
|
|
|
|
F[0] = sslF;
|
|
|
|
F[1] = plainF;
|
|
|
|
|
|
|
|
buf[0] = 'C';
|
2015-12-11 06:44:31 +01:00
|
|
|
uint32_to_buf(&buf[1], id);
|
2008-04-06 18:33:34 +02:00
|
|
|
|
|
|
|
ctl = which_ssld();
|
2016-02-10 23:25:23 +01:00
|
|
|
if(!ctl)
|
|
|
|
return NULL;
|
2008-04-06 18:33:34 +02:00
|
|
|
ctl->cli_count++;
|
|
|
|
ssl_cmd_write_queue(ctl, F, 2, buf, sizeof(buf));
|
2008-12-03 00:49:39 +01:00
|
|
|
return ctl;
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
void
|
|
|
|
ssld_decrement_clicount(ssl_ctl_t * ctl)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
|
|
|
if(ctl == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ctl->cli_count--;
|
2016-02-10 23:57:16 +01:00
|
|
|
if(ctl->shutdown && !ctl->cli_count)
|
|
|
|
{
|
|
|
|
ctl->dead = 1;
|
|
|
|
rb_kill(ctl->pid, SIGKILL);
|
|
|
|
}
|
2008-04-06 18:33:34 +02:00
|
|
|
if(ctl->dead && !ctl->cli_count)
|
|
|
|
{
|
|
|
|
free_ssl_daemon(ctl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-03 05:25:47 +01:00
|
|
|
/*
|
2008-04-06 18:33:34 +02:00
|
|
|
* what we end up sending to the ssld process for ziplinks is the following
|
2014-03-03 05:25:47 +01:00
|
|
|
* Z[ourfd][level][RECVQ]
|
|
|
|
* Z = ziplinks command = buf[0]
|
2008-04-06 18:33:34 +02:00
|
|
|
* 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)
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *server = (client::client *) data;
|
2008-07-30 00:55:08 +02:00
|
|
|
uint16_t recvqlen;
|
|
|
|
uint8_t level;
|
2008-04-06 18:33:34 +02:00
|
|
|
void *xbuf;
|
|
|
|
|
|
|
|
rb_fde_t *F[2];
|
|
|
|
rb_fde_t *xF1, *xF2;
|
|
|
|
char *buf;
|
|
|
|
void *recvq_start;
|
|
|
|
|
2015-12-11 06:44:31 +01:00
|
|
|
size_t hdr = (sizeof(uint8_t) * 2) + sizeof(uint32_t);
|
2008-04-06 18:33:34 +02:00
|
|
|
size_t len;
|
|
|
|
int cpylen, left;
|
|
|
|
|
|
|
|
server->localClient->event = NULL;
|
|
|
|
|
|
|
|
recvqlen = rb_linebuf_len(&server->localClient->buf_recvq);
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 18:33:34 +02:00
|
|
|
len = recvqlen + hdr;
|
|
|
|
|
|
|
|
if(len > READBUF_SIZE)
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
sendto_realops_snomask(SNO_GENERAL, L_ALL,
|
|
|
|
"ssld - attempted to pass message of %zd len, max len %d, giving up",
|
|
|
|
len, READBUF_SIZE);
|
2008-04-06 18:33:34 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-07-13 07:17:21 +02:00
|
|
|
buf = (char *)rb_malloc(len);
|
2008-04-06 18:33:34 +02:00
|
|
|
level = ConfigFileEntry.compression_level;
|
|
|
|
|
2015-12-11 06:44:31 +01:00
|
|
|
uint32_to_buf(&buf[1], rb_get_fd(server->localClient->F));
|
2008-12-03 00:49:39 +01:00
|
|
|
buf[5] = (char) level;
|
2008-04-06 18:33:34 +02:00
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
recvq_start = &buf[6];
|
2016-07-13 07:17:21 +02:00
|
|
|
server->localClient->zipstats = (ZipStats *)rb_malloc(sizeof(struct ZipStats));
|
2008-04-06 18:33:34 +02:00
|
|
|
|
|
|
|
xbuf = recvq_start;
|
|
|
|
left = recvqlen;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
cpylen = rb_linebuf_get(&server->localClient->buf_recvq, (char *)xbuf, left, LINEBUF_PARTIAL, LINEBUF_RAW);
|
2008-04-06 18:33:34 +02:00
|
|
|
left -= cpylen;
|
2008-12-03 00:49:39 +01:00
|
|
|
xbuf = (void *) (((uintptr_t) xbuf) + cpylen);
|
|
|
|
}
|
|
|
|
while(cpylen > 0);
|
2008-04-06 18:33:34 +02:00
|
|
|
|
|
|
|
/* Pass the socket to ssld. */
|
|
|
|
*buf = 'Z';
|
2009-05-12 23:30:46 +02:00
|
|
|
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");
|
2016-02-10 23:45:14 +01:00
|
|
|
rb_free(buf);
|
2009-05-12 23:30:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-03-03 05:25:47 +01:00
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
F[0] = server->localClient->F;
|
2008-04-06 18:33:34 +02:00
|
|
|
F[1] = xF1;
|
|
|
|
server->localClient->F = xF2;
|
|
|
|
/* need to redo as what we did before isn't valid now */
|
2016-03-26 01:49:01 +01:00
|
|
|
uint32_to_buf(&buf[1], connid_get(server));
|
2008-12-13 21:20:59 +01:00
|
|
|
|
|
|
|
server->localClient->z_ctl = which_ssld();
|
2016-02-10 23:25:23 +01:00
|
|
|
if(!server->localClient->z_ctl)
|
|
|
|
{
|
|
|
|
exit_client(server, server, server, "Error finding available ssld");
|
|
|
|
rb_free(buf);
|
|
|
|
return;
|
|
|
|
}
|
2008-12-13 21:20:59 +01:00
|
|
|
server->localClient->z_ctl->cli_count++;
|
|
|
|
ssl_cmd_write_queue(server->localClient->z_ctl, F, 2, buf, len);
|
2008-04-06 18:33:34 +02:00
|
|
|
rb_free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
collect_zipstats(void *unused)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr;
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *target_p;
|
2015-12-11 06:44:31 +01:00
|
|
|
char buf[sizeof(uint8_t) + sizeof(uint32_t) + HOSTLEN];
|
2008-04-06 18:33:34 +02:00
|
|
|
void *odata;
|
|
|
|
size_t len;
|
2015-12-11 06:44:31 +01:00
|
|
|
uint32_t id;
|
2008-04-06 18:33:34 +02:00
|
|
|
|
|
|
|
buf[0] = 'S';
|
2015-12-11 06:44:31 +01:00
|
|
|
odata = buf + sizeof(uint8_t) + sizeof(uint32_t);
|
2008-04-06 18:33:34 +02:00
|
|
|
|
|
|
|
RB_DLINK_FOREACH(ptr, serv_list.head)
|
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
target_p = (client::client *)ptr->data;
|
2008-04-06 18:33:34 +02:00
|
|
|
if(IsCapable(target_p, CAP_ZIP))
|
|
|
|
{
|
2008-07-30 00:55:08 +02:00
|
|
|
len = sizeof(uint8_t) + sizeof(uint32_t);
|
2008-04-06 18:33:34 +02:00
|
|
|
|
|
|
|
id = rb_get_fd(target_p->localClient->F);
|
2015-12-11 06:44:31 +01:00
|
|
|
uint32_to_buf(&buf[1], id);
|
2016-07-13 07:17:21 +02:00
|
|
|
rb_strlcpy((char *)odata, target_p->name, (sizeof(buf) - len));
|
|
|
|
len += strlen((const char *)odata) + 1; /* Get the \0 as well */
|
2008-12-13 21:20:59 +01:00
|
|
|
ssl_cmd_write_queue(target_p->localClient->z_ctl, NULL, 0, buf, len);
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup_dead_ssl(void *unused)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *next;
|
|
|
|
ssl_ctl_t *ctl;
|
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, ssl_daemons.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl = (ssl_ctl_t *)ptr->data;
|
2008-04-06 18:33:34 +02:00
|
|
|
if(ctl->dead && !ctl->cli_count)
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
free_ssl_daemon(ctl);
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_ssld_count(void)
|
|
|
|
{
|
|
|
|
return ssld_count;
|
|
|
|
}
|
|
|
|
|
2016-03-01 22:30:51 +01:00
|
|
|
void
|
2016-03-03 23:08:13 +01:00
|
|
|
ssld_foreach_info(void (*func)(void *data, pid_t pid, int cli_count, enum ssld_status status, const char *version), void *data)
|
2016-03-01 22:30:51 +01:00
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *next;
|
|
|
|
ssl_ctl_t *ctl;
|
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, ssl_daemons.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl = (ssl_ctl_t *)ptr->data;
|
2016-03-01 22:30:51 +01:00
|
|
|
func(data, ctl->pid, ctl->cli_count,
|
|
|
|
ctl->dead ? SSLD_DEAD :
|
2016-03-03 23:08:13 +01:00
|
|
|
(ctl->shutdown ? SSLD_SHUTDOWN : SSLD_ACTIVE),
|
|
|
|
ctl->version);
|
2016-03-01 22:30:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
void
|
|
|
|
init_ssld(void)
|
2008-04-06 18:33:34 +02:00
|
|
|
{
|
|
|
|
rb_event_addish("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
|
2015-12-13 14:58:50 +01:00
|
|
|
rb_event_addish("cleanup_dead_ssld", cleanup_dead_ssl, NULL, 60);
|
2008-04-06 18:33:34 +02:00
|
|
|
}
|
2016-08-13 05:05:54 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|