2008-04-06 12:46:52 +02:00
|
|
|
/*
|
|
|
|
* ssld.c: The ircd-ratbox ssl/zlib helper daemon thingy
|
|
|
|
* 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-07-01 05:04:00 +02:00
|
|
|
#include <ircd/stdinc.h>
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
#include <zlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAXPASSFD 4
|
|
|
|
#ifndef READBUF_SIZE
|
|
|
|
#define READBUF_SIZE 16384
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void setup_signals(void);
|
2008-12-03 00:49:39 +01:00
|
|
|
static pid_t ppid;
|
2008-04-06 12:46:52 +02:00
|
|
|
|
2015-12-11 06:40:24 +01:00
|
|
|
static inline uint32_t
|
|
|
|
buf_to_uint32(uint8_t *buf)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
2015-12-11 06:40:24 +01:00
|
|
|
uint32_t x;
|
2008-06-26 19:21:46 +02:00
|
|
|
memcpy(&x, buf, sizeof(x));
|
2008-04-06 12:46:52 +02:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
static inline void
|
2015-12-11 06:40:24 +01:00
|
|
|
uint32_to_buf(uint8_t *buf, uint32_t x)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
2008-06-26 19:21:46 +02:00
|
|
|
memcpy(buf, &x, sizeof(x));
|
2008-04-06 12:46:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct _mod_ctl_buf
|
|
|
|
{
|
|
|
|
rb_dlink_node node;
|
2014-02-08 19:40:35 +01:00
|
|
|
uint8_t *buf;
|
2008-04-06 12:46:52 +02:00
|
|
|
size_t buflen;
|
|
|
|
rb_fde_t *F[MAXPASSFD];
|
|
|
|
int nfds;
|
|
|
|
} mod_ctl_buf_t;
|
|
|
|
|
|
|
|
typedef struct _mod_ctl
|
|
|
|
{
|
|
|
|
rb_dlink_node node;
|
|
|
|
int cli_count;
|
|
|
|
rb_fde_t *F;
|
|
|
|
rb_fde_t *F_pipe;
|
|
|
|
rb_dlink_list readq;
|
|
|
|
rb_dlink_list writeq;
|
|
|
|
} mod_ctl_t;
|
|
|
|
|
|
|
|
static mod_ctl_t *mod_ctl;
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
typedef struct _zlib_stream
|
|
|
|
{
|
|
|
|
z_stream instream;
|
|
|
|
z_stream outstream;
|
|
|
|
} zlib_stream_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct _conn
|
|
|
|
{
|
|
|
|
rb_dlink_node node;
|
|
|
|
mod_ctl_t *ctl;
|
|
|
|
rawbuf_head_t *modbuf_out;
|
|
|
|
rawbuf_head_t *plainbuf_out;
|
|
|
|
|
2015-12-11 06:40:24 +01:00
|
|
|
uint32_t id;
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
rb_fde_t *mod_fd;
|
|
|
|
rb_fde_t *plain_fd;
|
2015-12-12 11:51:43 +01:00
|
|
|
uint64_t mod_out;
|
|
|
|
uint64_t mod_in;
|
|
|
|
uint64_t plain_in;
|
|
|
|
uint64_t plain_out;
|
2008-07-30 00:57:01 +02:00
|
|
|
uint8_t flags;
|
2008-04-06 12:46:52 +02:00
|
|
|
void *stream;
|
|
|
|
} conn_t;
|
|
|
|
|
|
|
|
#define FLAG_SSL 0x01
|
|
|
|
#define FLAG_ZIP 0x02
|
|
|
|
#define FLAG_CORK 0x04
|
|
|
|
#define FLAG_DEAD 0x08
|
2008-12-03 00:49:39 +01:00
|
|
|
#define FLAG_SSL_W_WANTS_R 0x10 /* output needs to wait until input possible */
|
|
|
|
#define FLAG_SSL_R_WANTS_W 0x20 /* input needs to wait until output possible */
|
2009-03-07 03:23:17 +01:00
|
|
|
#define FLAG_ZIPSSL 0x40
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
#define IsSSL(x) ((x)->flags & FLAG_SSL)
|
|
|
|
#define IsZip(x) ((x)->flags & FLAG_ZIP)
|
|
|
|
#define IsCork(x) ((x)->flags & FLAG_CORK)
|
|
|
|
#define IsDead(x) ((x)->flags & FLAG_DEAD)
|
2008-04-10 18:37:42 +02:00
|
|
|
#define IsSSLWWantsR(x) ((x)->flags & FLAG_SSL_W_WANTS_R)
|
|
|
|
#define IsSSLRWantsW(x) ((x)->flags & FLAG_SSL_R_WANTS_W)
|
2009-03-07 03:23:17 +01:00
|
|
|
#define IsZipSSL(x) ((x)->flags & FLAG_ZIPSSL)
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
#define SetSSL(x) ((x)->flags |= FLAG_SSL)
|
|
|
|
#define SetZip(x) ((x)->flags |= FLAG_ZIP)
|
|
|
|
#define SetCork(x) ((x)->flags |= FLAG_CORK)
|
|
|
|
#define SetDead(x) ((x)->flags |= FLAG_DEAD)
|
2008-04-10 18:37:42 +02:00
|
|
|
#define SetSSLWWantsR(x) ((x)->flags |= FLAG_SSL_W_WANTS_R)
|
|
|
|
#define SetSSLRWantsW(x) ((x)->flags |= FLAG_SSL_R_WANTS_W)
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
#define ClearCork(x) ((x)->flags &= ~FLAG_CORK)
|
2008-04-10 18:37:42 +02:00
|
|
|
#define ClearSSLWWantsR(x) ((x)->flags &= ~FLAG_SSL_W_WANTS_R)
|
|
|
|
#define ClearSSLRWantsW(x) ((x)->flags &= ~FLAG_SSL_R_WANTS_W)
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
#define NO_WAIT 0x0
|
|
|
|
#define WAIT_PLAIN 0x1
|
|
|
|
|
2008-06-26 19:21:46 +02:00
|
|
|
#define HASH_WALK_SAFE(i, max, ptr, next, table) for(i = 0; i < max; i++) { RB_DLINK_FOREACH_SAFE(ptr, next, table[i].head)
|
|
|
|
#define HASH_WALK_END }
|
2008-04-06 12:46:52 +02:00
|
|
|
#define CONN_HASH_SIZE 2000
|
|
|
|
#define connid_hash(x) (&connid_hash_table[(x % CONN_HASH_SIZE)])
|
|
|
|
|
2008-06-26 19:21:46 +02:00
|
|
|
|
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
static rb_dlink_list connid_hash_table[CONN_HASH_SIZE];
|
|
|
|
static rb_dlink_list dead_list;
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
static void conn_mod_read_cb(rb_fde_t *fd, void *data);
|
2008-04-06 12:46:52 +02:00
|
|
|
static void conn_mod_write_sendq(rb_fde_t *, void *data);
|
|
|
|
static void conn_plain_write_sendq(rb_fde_t *, void *data);
|
|
|
|
static void mod_write_ctl(rb_fde_t *, void *data);
|
2008-12-03 00:49:39 +01:00
|
|
|
static void conn_plain_read_cb(rb_fde_t *fd, void *data);
|
2009-11-15 23:11:18 +01:00
|
|
|
static void conn_plain_read_shutdown_cb(rb_fde_t *fd, void *data);
|
2008-12-03 00:49:39 +01:00
|
|
|
static void mod_cmd_write_queue(mod_ctl_t * ctl, const void *data, size_t len);
|
2008-04-06 12:46:52 +02:00
|
|
|
static const char *remote_closed = "Remote host closed the connection";
|
2016-03-19 06:58:15 +01:00
|
|
|
static bool ssld_ssl_ok;
|
2016-04-23 23:51:05 +02:00
|
|
|
static int certfp_method = RB_SSL_CERTFP_METH_CERT_SHA1;
|
2008-04-06 12:46:52 +02:00
|
|
|
#ifdef HAVE_LIBZ
|
2016-03-09 09:19:31 +01:00
|
|
|
static bool zlib_ok = true;
|
2008-04-06 12:46:52 +02:00
|
|
|
#else
|
2016-03-09 09:19:31 +01:00
|
|
|
static bool zlib_ok = false;
|
2008-04-06 12:46:52 +02:00
|
|
|
#endif
|
2008-06-26 19:21:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
2008-04-06 12:46:52 +02:00
|
|
|
static void *
|
|
|
|
ssld_alloc(void *unused, size_t count, size_t size)
|
|
|
|
{
|
|
|
|
return rb_malloc(count * size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ssld_free(void *unused, void *ptr)
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
rb_free(ptr);
|
2008-04-06 12:46:52 +02:00
|
|
|
}
|
2008-06-26 19:21:46 +02:00
|
|
|
#endif
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
static conn_t *
|
2015-12-11 06:40:24 +01:00
|
|
|
conn_find_by_id(uint32_t id)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
rb_dlink_node *ptr;
|
|
|
|
conn_t *conn;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH(ptr, (connid_hash(id))->head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
conn = (conn_t *)ptr->data;
|
2008-04-06 12:46:52 +02:00
|
|
|
if(conn->id == id && !IsDead(conn))
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-12-11 06:40:24 +01:00
|
|
|
conn_add_id_hash(conn_t * conn, uint32_t id)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
conn->id = id;
|
|
|
|
rb_dlinkAdd(conn, &conn->node, connid_hash(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_conn(conn_t * conn)
|
|
|
|
{
|
|
|
|
rb_free_rawbuffer(conn->modbuf_out);
|
|
|
|
rb_free_rawbuffer(conn->plainbuf_out);
|
2008-06-26 19:21:46 +02:00
|
|
|
#ifdef HAVE_LIBZ
|
2008-04-06 12:46:52 +02:00
|
|
|
if(IsZip(conn))
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
zlib_stream_t *stream = (zlib_stream_t *)conn->stream;
|
2008-04-06 12:46:52 +02:00
|
|
|
inflateEnd(&stream->instream);
|
2008-12-03 00:49:39 +01:00
|
|
|
deflateEnd(&stream->outstream);
|
2016-02-05 15:02:47 +01:00
|
|
|
rb_free(stream);
|
2008-04-06 12:46:52 +02:00
|
|
|
}
|
2008-06-26 19:21:46 +02:00
|
|
|
#endif
|
2008-04-06 12:46:52 +02:00
|
|
|
rb_free(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clean_dead_conns(void *unused)
|
|
|
|
{
|
|
|
|
conn_t *conn;
|
|
|
|
rb_dlink_node *ptr, *next;
|
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
conn = (conn_t *)ptr->data;
|
2008-04-06 12:46:52 +02:00
|
|
|
free_conn(conn);
|
|
|
|
}
|
|
|
|
dead_list.tail = dead_list.head = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
close_conn(conn_t * conn, int wait_plain, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2008-12-03 00:49:39 +01:00
|
|
|
char reason[128]; /* must always be under 250 bytes */
|
2014-02-08 19:40:35 +01:00
|
|
|
uint8_t buf[256];
|
2008-04-06 12:46:52 +02:00
|
|
|
int len;
|
|
|
|
if(IsDead(conn))
|
|
|
|
return;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
rb_rawbuf_flush(conn->modbuf_out, conn->mod_fd);
|
|
|
|
rb_rawbuf_flush(conn->plainbuf_out, conn->plain_fd);
|
|
|
|
rb_close(conn->mod_fd);
|
|
|
|
SetDead(conn);
|
|
|
|
|
2015-12-11 06:40:24 +01:00
|
|
|
if(!IsZipSSL(conn))
|
2009-11-15 23:11:18 +01:00
|
|
|
rb_dlinkDelete(&conn->node, connid_hash(conn->id));
|
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
if(!wait_plain || fmt == NULL)
|
|
|
|
{
|
|
|
|
rb_close(conn->plain_fd);
|
|
|
|
rb_dlinkAdd(conn, &conn->node, &dead_list);
|
|
|
|
return;
|
|
|
|
}
|
2009-11-15 23:11:18 +01:00
|
|
|
rb_setselect(conn->plain_fd, RB_SELECT_READ, conn_plain_read_shutdown_cb, conn);
|
|
|
|
rb_setselect(conn->plain_fd, RB_SELECT_WRITE, NULL, NULL);
|
2008-04-06 12:46:52 +02:00
|
|
|
va_start(ap, fmt);
|
2016-02-10 02:25:32 +01:00
|
|
|
vsnprintf(reason, sizeof(reason), fmt, ap);
|
2008-04-06 12:46:52 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
buf[0] = 'D';
|
2015-12-11 06:40:24 +01:00
|
|
|
uint32_to_buf(&buf[1], conn->id);
|
2014-02-08 19:40:35 +01:00
|
|
|
rb_strlcpy((char *) &buf[5], reason, sizeof(buf) - 5);
|
2008-04-06 12:46:52 +02:00
|
|
|
len = (strlen(reason) + 1) + 5;
|
|
|
|
mod_cmd_write_queue(conn->ctl, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static conn_t *
|
2008-12-03 00:49:39 +01:00
|
|
|
make_conn(mod_ctl_t * ctl, rb_fde_t *mod_fd, rb_fde_t *plain_fd)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
conn_t *conn = (conn_t *)rb_malloc(sizeof(conn_t));
|
2008-04-06 12:46:52 +02:00
|
|
|
conn->ctl = ctl;
|
|
|
|
conn->modbuf_out = rb_new_rawbuffer();
|
|
|
|
conn->plainbuf_out = rb_new_rawbuffer();
|
|
|
|
conn->mod_fd = mod_fd;
|
|
|
|
conn->plain_fd = plain_fd;
|
|
|
|
conn->id = -1;
|
|
|
|
conn->stream = NULL;
|
|
|
|
rb_set_nb(mod_fd);
|
|
|
|
rb_set_nb(plain_fd);
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
2008-06-26 19:21:46 +02:00
|
|
|
static void
|
|
|
|
check_handshake_flood(void *unused)
|
|
|
|
{
|
|
|
|
conn_t *conn;
|
|
|
|
rb_dlink_node *ptr, *next;
|
|
|
|
unsigned int count;
|
|
|
|
int i;
|
|
|
|
HASH_WALK_SAFE(i, CONN_HASH_SIZE, ptr, next, connid_hash_table)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
conn = (conn_t *)ptr->data;
|
2008-06-26 19:21:46 +02:00
|
|
|
if(!IsSSL(conn))
|
|
|
|
continue;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-06-26 19:21:46 +02:00
|
|
|
count = rb_ssl_handshake_count(conn->mod_fd);
|
|
|
|
/* nothing needs to do this more than twice in ten seconds i don't think */
|
|
|
|
if(count > 2)
|
|
|
|
close_conn(conn, WAIT_PLAIN, "Handshake flooding");
|
|
|
|
else
|
|
|
|
rb_ssl_clear_handshake_count(conn->mod_fd);
|
2008-12-03 00:49:39 +01:00
|
|
|
}
|
|
|
|
HASH_WALK_END}
|
2008-06-26 19:21:46 +02:00
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
conn_mod_write_sendq(rb_fde_t *fd, void *data)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
conn_t *conn = (conn_t *)data;
|
2008-04-06 12:46:52 +02:00
|
|
|
const char *err;
|
|
|
|
int retlen;
|
|
|
|
if(IsDead(conn))
|
|
|
|
return;
|
|
|
|
|
2008-04-10 18:37:42 +02:00
|
|
|
if(IsSSLWWantsR(conn))
|
|
|
|
{
|
|
|
|
ClearSSLWWantsR(conn);
|
|
|
|
conn_mod_read_cb(conn->mod_fd, conn);
|
|
|
|
if(IsDead(conn))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
while((retlen = rb_rawbuf_flush(conn->modbuf_out, fd)) > 0)
|
2008-04-06 12:46:52 +02:00
|
|
|
conn->mod_out += retlen;
|
|
|
|
|
|
|
|
if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
|
|
|
|
{
|
|
|
|
if(retlen == 0)
|
|
|
|
close_conn(conn, WAIT_PLAIN, "%s", remote_closed);
|
|
|
|
if(IsSSL(conn) && retlen == RB_RW_SSL_ERROR)
|
|
|
|
err = rb_get_ssl_strerror(conn->mod_fd);
|
|
|
|
else
|
|
|
|
err = strerror(errno);
|
|
|
|
close_conn(conn, WAIT_PLAIN, "Write error: %s", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(rb_rawbuf_length(conn->modbuf_out) > 0)
|
|
|
|
{
|
2008-04-10 18:37:42 +02:00
|
|
|
if(retlen != RB_RW_SSL_NEED_READ)
|
|
|
|
rb_setselect(conn->mod_fd, RB_SELECT_WRITE, conn_mod_write_sendq, conn);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb_setselect(conn->mod_fd, RB_SELECT_READ, conn_mod_write_sendq, conn);
|
|
|
|
rb_setselect(conn->mod_fd, RB_SELECT_WRITE, NULL, NULL);
|
|
|
|
SetSSLWWantsR(conn);
|
|
|
|
}
|
2008-04-06 12:46:52 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
rb_setselect(conn->mod_fd, RB_SELECT_WRITE, NULL, NULL);
|
|
|
|
|
|
|
|
if(IsCork(conn) && rb_rawbuf_length(conn->modbuf_out) == 0)
|
|
|
|
{
|
|
|
|
ClearCork(conn);
|
|
|
|
conn_plain_read_cb(conn->plain_fd, conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
conn_mod_write(conn_t * conn, void *data, size_t len)
|
|
|
|
{
|
|
|
|
if(IsDead(conn)) /* no point in queueing to a dead man */
|
|
|
|
return;
|
|
|
|
rb_rawbuf_append(conn->modbuf_out, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
conn_plain_write(conn_t * conn, void *data, size_t len)
|
|
|
|
{
|
|
|
|
if(IsDead(conn)) /* again no point in queueing to dead men */
|
|
|
|
return;
|
|
|
|
rb_rawbuf_append(conn->plainbuf_out, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mod_cmd_write_queue(mod_ctl_t * ctl, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
mod_ctl_buf_t *ctl_buf;
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl_buf = (mod_ctl_buf_t *)rb_malloc(sizeof(mod_ctl_buf_t));
|
|
|
|
ctl_buf->buf = (uint8_t *)rb_malloc(len);
|
2008-04-06 12:46:52 +02:00
|
|
|
ctl_buf->buflen = len;
|
|
|
|
memcpy(ctl_buf->buf, data, len);
|
|
|
|
ctl_buf->nfds = 0;
|
|
|
|
rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->writeq);
|
|
|
|
mod_write_ctl(ctl->F, ctl);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
static void
|
|
|
|
common_zlib_deflate(conn_t * conn, void *buf, size_t len)
|
|
|
|
{
|
2015-12-12 11:50:15 +01:00
|
|
|
char outbuf[READBUF_SIZE];
|
2008-04-06 12:46:52 +02:00
|
|
|
int ret, have;
|
2008-12-03 00:49:39 +01:00
|
|
|
z_stream *outstream = &((zlib_stream_t *) conn->stream)->outstream;
|
2016-07-13 07:17:21 +02:00
|
|
|
outstream->next_in = (Bytef *)buf;
|
2008-04-06 12:46:52 +02:00
|
|
|
outstream->avail_in = len;
|
|
|
|
outstream->next_out = (Bytef *) outbuf;
|
|
|
|
outstream->avail_out = sizeof(outbuf);
|
|
|
|
|
|
|
|
ret = deflate(outstream, Z_SYNC_FLUSH);
|
|
|
|
if(ret != Z_OK)
|
|
|
|
{
|
|
|
|
/* deflate error */
|
|
|
|
close_conn(conn, WAIT_PLAIN, "Deflate failed: %s", zError(ret));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(outstream->avail_out == 0)
|
|
|
|
{
|
|
|
|
/* avail_out empty */
|
|
|
|
close_conn(conn, WAIT_PLAIN, "error compressing data, avail_out == 0");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(outstream->avail_in != 0)
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
/* avail_in isn't empty... */
|
2008-04-06 12:46:52 +02:00
|
|
|
close_conn(conn, WAIT_PLAIN, "error compressing data, avail_in != 0");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
have = sizeof(outbuf) - outstream->avail_out;
|
|
|
|
conn_mod_write(conn, outbuf, have);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
common_zlib_inflate(conn_t * conn, void *buf, size_t len)
|
|
|
|
{
|
2015-12-12 11:50:15 +01:00
|
|
|
char outbuf[READBUF_SIZE];
|
2008-12-03 00:49:39 +01:00
|
|
|
int ret, have = 0;
|
2016-07-13 07:17:21 +02:00
|
|
|
((zlib_stream_t *) conn->stream)->instream.next_in = (Bytef *)buf;
|
2008-12-03 00:49:39 +01:00
|
|
|
((zlib_stream_t *) conn->stream)->instream.avail_in = len;
|
|
|
|
((zlib_stream_t *) conn->stream)->instream.next_out = (Bytef *) outbuf;
|
|
|
|
((zlib_stream_t *) conn->stream)->instream.avail_out = sizeof(outbuf);
|
2008-04-06 12:46:52 +02:00
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
while(((zlib_stream_t *) conn->stream)->instream.avail_in)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
ret = inflate(&((zlib_stream_t *) conn->stream)->instream, Z_NO_FLUSH);
|
2008-04-06 12:46:52 +02:00
|
|
|
if(ret != Z_OK)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
if(!strncmp("ERROR ", (const char *)buf, 6))
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
close_conn(conn, WAIT_PLAIN, "Received uncompressed ERROR");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
close_conn(conn, WAIT_PLAIN, "Inflate failed: %s", zError(ret));
|
|
|
|
return;
|
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
have = sizeof(outbuf) - ((zlib_stream_t *) conn->stream)->instream.avail_out;
|
2008-04-06 12:46:52 +02:00
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
if(((zlib_stream_t *) conn->stream)->instream.avail_in)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
conn_plain_write(conn, outbuf, have);
|
|
|
|
have = 0;
|
2008-12-03 00:49:39 +01:00
|
|
|
((zlib_stream_t *) conn->stream)->instream.next_out = (Bytef *) outbuf;
|
|
|
|
((zlib_stream_t *) conn->stream)->instream.avail_out = sizeof(outbuf);
|
2008-04-06 12:46:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(have == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
conn_plain_write(conn, outbuf, have);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-03-24 18:52:16 +01:00
|
|
|
static bool
|
2008-04-06 12:46:52 +02:00
|
|
|
plain_check_cork(conn_t * conn)
|
|
|
|
{
|
|
|
|
if(rb_rawbuf_length(conn->modbuf_out) >= 4096)
|
|
|
|
{
|
2014-03-03 05:25:47 +01:00
|
|
|
/* if we have over 4k pending outbound, don't read until
|
2008-04-06 12:46:52 +02:00
|
|
|
* we've cleared the queue */
|
|
|
|
SetCork(conn);
|
|
|
|
rb_setselect(conn->plain_fd, RB_SELECT_READ, NULL, NULL);
|
|
|
|
/* try to write */
|
|
|
|
conn_mod_write_sendq(conn->mod_fd, conn);
|
2016-03-24 18:52:16 +01:00
|
|
|
return true;
|
2008-04-06 12:46:52 +02:00
|
|
|
}
|
2016-03-24 18:52:16 +01:00
|
|
|
return false;
|
2008-04-06 12:46:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
conn_plain_read_cb(rb_fde_t *fd, void *data)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
2015-12-12 11:50:15 +01:00
|
|
|
char inbuf[READBUF_SIZE];
|
2016-07-13 07:17:21 +02:00
|
|
|
conn_t *conn = (conn_t *)data;
|
2008-04-06 12:46:52 +02:00
|
|
|
int length = 0;
|
|
|
|
if(conn == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(IsDead(conn))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(plain_check_cork(conn))
|
|
|
|
return;
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
while(1)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
if(IsDead(conn))
|
|
|
|
return;
|
|
|
|
|
|
|
|
length = rb_read(conn->plain_fd, inbuf, sizeof(inbuf));
|
|
|
|
|
|
|
|
if(length == 0 || (length < 0 && !rb_ignore_errno(errno)))
|
|
|
|
{
|
|
|
|
close_conn(conn, NO_WAIT, NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(length < 0)
|
|
|
|
{
|
|
|
|
rb_setselect(conn->plain_fd, RB_SELECT_READ, conn_plain_read_cb, conn);
|
|
|
|
conn_mod_write_sendq(conn->mod_fd, conn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
conn->plain_in += length;
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
if(IsZip(conn))
|
|
|
|
common_zlib_deflate(conn, inbuf, length);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
conn_mod_write(conn, inbuf, length);
|
|
|
|
if(IsDead(conn))
|
|
|
|
return;
|
|
|
|
if(plain_check_cork(conn))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-15 23:11:18 +01:00
|
|
|
static void
|
|
|
|
conn_plain_read_shutdown_cb(rb_fde_t *fd, void *data)
|
|
|
|
{
|
2015-12-12 11:50:15 +01:00
|
|
|
char inbuf[READBUF_SIZE];
|
2016-07-13 07:17:21 +02:00
|
|
|
conn_t *conn = (conn_t *)data;
|
2009-11-15 23:11:18 +01:00
|
|
|
int length = 0;
|
|
|
|
|
|
|
|
if(conn == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
length = rb_read(conn->plain_fd, inbuf, sizeof(inbuf));
|
|
|
|
|
|
|
|
if(length == 0 || (length < 0 && !rb_ignore_errno(errno)))
|
|
|
|
{
|
|
|
|
rb_close(conn->plain_fd);
|
|
|
|
rb_dlinkAdd(conn, &conn->node, &dead_list);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(length < 0)
|
|
|
|
{
|
|
|
|
rb_setselect(conn->plain_fd, RB_SELECT_READ, conn_plain_read_shutdown_cb, conn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
conn_mod_read_cb(rb_fde_t *fd, void *data)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
2015-12-12 11:50:15 +01:00
|
|
|
char inbuf[READBUF_SIZE];
|
2016-07-13 07:17:21 +02:00
|
|
|
conn_t *conn = (conn_t *)data;
|
2008-04-06 12:46:52 +02:00
|
|
|
const char *err = remote_closed;
|
|
|
|
int length;
|
|
|
|
if(conn == NULL)
|
|
|
|
return;
|
|
|
|
if(IsDead(conn))
|
|
|
|
return;
|
|
|
|
|
2008-04-10 18:37:42 +02:00
|
|
|
if(IsSSLRWantsW(conn))
|
|
|
|
{
|
|
|
|
ClearSSLRWantsW(conn);
|
|
|
|
conn_mod_write_sendq(conn->mod_fd, conn);
|
|
|
|
if(IsDead(conn))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
while(1)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
if(IsDead(conn))
|
|
|
|
return;
|
|
|
|
|
|
|
|
length = rb_read(conn->mod_fd, inbuf, sizeof(inbuf));
|
|
|
|
|
|
|
|
if(length == 0 || (length < 0 && !rb_ignore_errno(errno)))
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
if(length == 0)
|
|
|
|
{
|
2008-04-06 12:46:52 +02:00
|
|
|
close_conn(conn, WAIT_PLAIN, "%s", remote_closed);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(IsSSL(conn) && length == RB_RW_SSL_ERROR)
|
|
|
|
err = rb_get_ssl_strerror(conn->mod_fd);
|
|
|
|
else
|
|
|
|
err = strerror(errno);
|
|
|
|
close_conn(conn, WAIT_PLAIN, "Read error: %s", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(length < 0)
|
|
|
|
{
|
2008-04-10 18:37:42 +02:00
|
|
|
if(length != RB_RW_SSL_NEED_WRITE)
|
|
|
|
rb_setselect(conn->mod_fd, RB_SELECT_READ, conn_mod_read_cb, conn);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb_setselect(conn->mod_fd, RB_SELECT_READ, NULL, NULL);
|
|
|
|
rb_setselect(conn->mod_fd, RB_SELECT_WRITE, conn_mod_read_cb, conn);
|
|
|
|
SetSSLRWantsW(conn);
|
|
|
|
}
|
2008-04-06 12:46:52 +02:00
|
|
|
conn_plain_write_sendq(conn->plain_fd, conn);
|
|
|
|
return;
|
2008-12-03 00:49:39 +01:00
|
|
|
}
|
2008-04-06 12:46:52 +02:00
|
|
|
conn->mod_in += length;
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
if(IsZip(conn))
|
|
|
|
common_zlib_inflate(conn, inbuf, length);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
conn_plain_write(conn, inbuf, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
conn_plain_write_sendq(rb_fde_t *fd, void *data)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
conn_t *conn = (conn_t *)data;
|
2008-04-06 12:46:52 +02:00
|
|
|
int retlen;
|
|
|
|
|
|
|
|
if(IsDead(conn))
|
|
|
|
return;
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
while((retlen = rb_rawbuf_flush(conn->plainbuf_out, fd)) > 0)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
conn->plain_out += retlen;
|
|
|
|
}
|
|
|
|
if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
close_conn(conn, NO_WAIT, NULL);
|
2008-04-06 12:46:52 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
if(rb_rawbuf_length(conn->plainbuf_out) > 0)
|
|
|
|
rb_setselect(conn->plain_fd, RB_SELECT_WRITE, conn_plain_write_sendq, conn);
|
|
|
|
else
|
|
|
|
rb_setselect(conn->plain_fd, RB_SELECT_WRITE, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
maxconn(void)
|
|
|
|
{
|
|
|
|
#if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
|
|
|
|
struct rlimit limit;
|
|
|
|
|
|
|
|
if(!getrlimit(RLIMIT_NOFILE, &limit))
|
|
|
|
{
|
|
|
|
return limit.rlim_cur;
|
|
|
|
}
|
|
|
|
#endif /* RLIMIT_FD_MAX */
|
|
|
|
return MAXCONNECTIONS;
|
|
|
|
}
|
|
|
|
|
2015-12-11 06:40:24 +01:00
|
|
|
static void
|
|
|
|
ssl_send_cipher(conn_t *conn)
|
|
|
|
{
|
|
|
|
size_t len;
|
2015-12-12 11:50:35 +01:00
|
|
|
uint8_t buf[512];
|
2015-12-11 06:40:24 +01:00
|
|
|
char cstring[256];
|
|
|
|
const char *p;
|
|
|
|
if(!IsSSL(conn))
|
|
|
|
return;
|
|
|
|
|
|
|
|
p = rb_ssl_get_cipher(conn->mod_fd);
|
|
|
|
|
|
|
|
if(p == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
rb_strlcpy(cstring, p, sizeof(cstring));
|
|
|
|
|
|
|
|
buf[0] = 'C';
|
|
|
|
uint32_to_buf(&buf[1], conn->id);
|
2015-12-12 11:50:35 +01:00
|
|
|
strcpy((char *) &buf[5], cstring);
|
2015-12-11 06:40:24 +01:00
|
|
|
len = (strlen(cstring) + 1) + 5;
|
|
|
|
mod_cmd_write_queue(conn->ctl, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ssl_send_certfp(conn_t *conn)
|
|
|
|
{
|
2016-04-25 21:12:27 +02:00
|
|
|
uint8_t buf[13 + RB_SSL_CERTFP_LEN];
|
2015-12-11 06:40:24 +01:00
|
|
|
|
2016-04-25 21:12:27 +02:00
|
|
|
int len = rb_get_ssl_certfp(conn->mod_fd, &buf[13], certfp_method);
|
2015-12-11 06:40:24 +01:00
|
|
|
if (!len)
|
|
|
|
return;
|
|
|
|
|
|
|
|
lrb_assert(len <= RB_SSL_CERTFP_LEN);
|
|
|
|
buf[0] = 'F';
|
|
|
|
uint32_to_buf(&buf[1], conn->id);
|
2016-04-25 21:12:27 +02:00
|
|
|
uint32_to_buf(&buf[5], certfp_method);
|
|
|
|
uint32_to_buf(&buf[9], len);
|
|
|
|
mod_cmd_write_queue(conn->ctl, buf, 13 + len);
|
2015-12-11 06:40:24 +01:00
|
|
|
}
|
|
|
|
|
2016-04-24 12:48:35 +02:00
|
|
|
static void
|
|
|
|
ssl_send_open(conn_t *conn)
|
|
|
|
{
|
|
|
|
uint8_t buf[5];
|
|
|
|
|
|
|
|
buf[0] = 'O';
|
|
|
|
uint32_to_buf(&buf[1], conn->id);
|
|
|
|
mod_cmd_write_queue(conn->ctl, buf, 5);
|
|
|
|
}
|
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
ssl_process_accept_cb(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t len, void *data)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
conn_t *conn = (conn_t *)data;
|
2010-01-31 19:04:20 +01:00
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
if(status == RB_OK)
|
|
|
|
{
|
2015-12-11 06:40:24 +01:00
|
|
|
ssl_send_cipher(conn);
|
|
|
|
ssl_send_certfp(conn);
|
2016-04-24 12:48:35 +02:00
|
|
|
ssl_send_open(conn);
|
2016-04-24 11:39:16 +02:00
|
|
|
conn_mod_read_cb(conn->mod_fd, conn);
|
|
|
|
conn_plain_read_cb(conn->plain_fd, conn);
|
2008-04-06 12:46:52 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-06-29 21:56:31 +02:00
|
|
|
/* ircd doesn't care about the reason for this */
|
2008-04-06 12:46:52 +02:00
|
|
|
close_conn(conn, NO_WAIT, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
ssl_process_connect_cb(rb_fde_t *F, int status, void *data)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
conn_t *conn = (conn_t *)data;
|
2010-12-15 04:28:35 +01:00
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
if(status == RB_OK)
|
|
|
|
{
|
2015-12-11 06:40:24 +01:00
|
|
|
ssl_send_cipher(conn);
|
|
|
|
ssl_send_certfp(conn);
|
2016-04-24 12:48:35 +02:00
|
|
|
ssl_send_open(conn);
|
2016-04-24 11:39:16 +02:00
|
|
|
conn_mod_read_cb(conn->mod_fd, conn);
|
|
|
|
conn_plain_read_cb(conn->plain_fd, conn);
|
2008-04-06 12:46:52 +02:00
|
|
|
}
|
2008-06-29 21:56:31 +02:00
|
|
|
else if(status == RB_ERR_TIMEOUT)
|
|
|
|
close_conn(conn, WAIT_PLAIN, "SSL handshake timed out");
|
|
|
|
else if(status == RB_ERROR_SSL)
|
|
|
|
close_conn(conn, WAIT_PLAIN, "%s", rb_get_ssl_strerror(conn->mod_fd));
|
|
|
|
else
|
|
|
|
close_conn(conn, WAIT_PLAIN, "SSL handshake failed");
|
2008-04-06 12:46:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-15 22:41:02 +01:00
|
|
|
static void
|
|
|
|
cleanup_bad_message(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* XXX should log this somehow */
|
|
|
|
for (i = 0; i < ctlb->nfds; i++)
|
|
|
|
rb_close(ctlb->F[i]);
|
|
|
|
}
|
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
static void
|
|
|
|
ssl_process_accept(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
|
|
|
|
{
|
|
|
|
conn_t *conn;
|
2015-12-11 06:40:24 +01:00
|
|
|
uint32_t id;
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]);
|
|
|
|
|
2015-12-11 06:40:24 +01:00
|
|
|
id = buf_to_uint32(&ctlb->buf[1]);
|
|
|
|
conn_add_id_hash(conn, id);
|
2008-04-06 12:46:52 +02:00
|
|
|
SetSSL(conn);
|
|
|
|
|
|
|
|
if(rb_get_type(conn->mod_fd) & RB_FD_UNKNOWN)
|
|
|
|
rb_set_type(conn->mod_fd, RB_FD_SOCKET);
|
2015-12-16 14:32:12 +01:00
|
|
|
|
|
|
|
if(rb_get_type(conn->plain_fd) == RB_FD_UNKNOWN)
|
2008-04-06 12:46:52 +02:00
|
|
|
rb_set_type(conn->plain_fd, RB_FD_SOCKET);
|
|
|
|
|
|
|
|
rb_ssl_start_accepted(ctlb->F[0], ssl_process_accept_cb, conn, 10);
|
|
|
|
}
|
|
|
|
|
2015-05-27 23:46:46 +02:00
|
|
|
static void
|
|
|
|
ssl_change_certfp_method(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
|
|
|
|
{
|
2015-12-11 06:40:24 +01:00
|
|
|
certfp_method = buf_to_uint32(&ctlb->buf[1]);
|
2015-05-27 23:46:46 +02:00
|
|
|
}
|
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
static void
|
|
|
|
ssl_process_connect(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
|
|
|
|
{
|
|
|
|
conn_t *conn;
|
2015-12-11 06:40:24 +01:00
|
|
|
uint32_t id;
|
2008-04-06 12:46:52 +02:00
|
|
|
conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]);
|
|
|
|
|
2015-12-11 06:40:24 +01:00
|
|
|
id = buf_to_uint32(&ctlb->buf[1]);
|
|
|
|
conn_add_id_hash(conn, id);
|
2008-04-06 12:46:52 +02:00
|
|
|
SetSSL(conn);
|
|
|
|
|
|
|
|
if(rb_get_type(conn->mod_fd) == RB_FD_UNKNOWN)
|
|
|
|
rb_set_type(conn->mod_fd, RB_FD_SOCKET);
|
|
|
|
|
2015-12-16 14:32:12 +01:00
|
|
|
if(rb_get_type(conn->plain_fd) == RB_FD_UNKNOWN)
|
2008-04-06 12:46:52 +02:00
|
|
|
rb_set_type(conn->plain_fd, RB_FD_SOCKET);
|
|
|
|
|
|
|
|
|
|
|
|
rb_ssl_start_connected(ctlb->F[0], ssl_process_connect_cb, conn, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_stats(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
|
|
|
|
{
|
|
|
|
char outstat[512];
|
|
|
|
conn_t *conn;
|
2014-02-08 19:40:35 +01:00
|
|
|
uint8_t *odata;
|
2015-12-11 06:40:24 +01:00
|
|
|
uint32_t id;
|
2008-04-06 12:46:52 +02:00
|
|
|
|
2015-12-11 06:40:24 +01:00
|
|
|
id = buf_to_uint32(&ctlb->buf[1]);
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
odata = &ctlb->buf[5];
|
|
|
|
conn = conn_find_by_id(id);
|
|
|
|
|
|
|
|
if(conn == NULL)
|
|
|
|
return;
|
|
|
|
|
2016-02-10 02:25:32 +01:00
|
|
|
snprintf(outstat, sizeof(outstat), "S %s %llu %llu %llu %llu", odata,
|
2016-02-11 22:58:44 +01:00
|
|
|
(unsigned long long)conn->plain_out,
|
|
|
|
(unsigned long long)conn->mod_in,
|
|
|
|
(unsigned long long)conn->plain_in,
|
|
|
|
(unsigned long long)conn->mod_out);
|
2008-04-06 12:46:52 +02:00
|
|
|
conn->plain_out = 0;
|
|
|
|
conn->plain_in = 0;
|
|
|
|
conn->mod_in = 0;
|
|
|
|
conn->mod_out = 0;
|
|
|
|
mod_cmd_write_queue(ctl, outstat, strlen(outstat) + 1); /* +1 is so we send the \0 as well */
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
static void
|
|
|
|
zlib_process(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
|
|
|
|
{
|
2008-07-30 00:57:01 +02:00
|
|
|
uint8_t level;
|
2008-04-06 12:46:52 +02:00
|
|
|
size_t recvqlen;
|
2015-12-11 06:40:24 +01:00
|
|
|
size_t hdr = (sizeof(uint8_t) * 2) + sizeof(uint32_t);
|
2008-04-06 12:46:52 +02:00
|
|
|
void *recvq_start;
|
|
|
|
z_stream *instream, *outstream;
|
|
|
|
conn_t *conn;
|
2015-12-11 06:40:24 +01:00
|
|
|
uint32_t id;
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]);
|
|
|
|
if(rb_get_type(conn->mod_fd) == RB_FD_UNKNOWN)
|
|
|
|
rb_set_type(conn->mod_fd, RB_FD_SOCKET);
|
|
|
|
|
|
|
|
if(rb_get_type(conn->plain_fd) == RB_FD_UNKNOWN)
|
|
|
|
rb_set_type(conn->plain_fd, RB_FD_SOCKET);
|
|
|
|
|
2015-12-11 06:40:24 +01:00
|
|
|
id = buf_to_uint32(&ctlb->buf[1]);
|
2008-04-06 12:46:52 +02:00
|
|
|
conn_add_id_hash(conn, id);
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
level = (uint8_t)ctlb->buf[5];
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
recvqlen = ctlb->buflen - hdr;
|
|
|
|
recvq_start = &ctlb->buf[6];
|
|
|
|
|
|
|
|
SetZip(conn);
|
|
|
|
conn->stream = rb_malloc(sizeof(zlib_stream_t));
|
2008-12-03 00:49:39 +01:00
|
|
|
instream = &((zlib_stream_t *) conn->stream)->instream;
|
|
|
|
outstream = &((zlib_stream_t *) conn->stream)->outstream;
|
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
instream->total_in = 0;
|
|
|
|
instream->total_out = 0;
|
|
|
|
instream->zalloc = (alloc_func) ssld_alloc;
|
|
|
|
instream->zfree = (free_func) ssld_free;
|
|
|
|
instream->data_type = Z_ASCII;
|
2008-12-03 00:49:39 +01:00
|
|
|
inflateInit(&((zlib_stream_t *) conn->stream)->instream);
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
outstream->total_in = 0;
|
|
|
|
outstream->total_out = 0;
|
|
|
|
outstream->zalloc = (alloc_func) ssld_alloc;
|
|
|
|
outstream->zfree = (free_func) ssld_free;
|
|
|
|
outstream->data_type = Z_ASCII;
|
|
|
|
|
|
|
|
if(level > 9)
|
2015-12-11 06:40:24 +01:00
|
|
|
level = (uint8_t) Z_DEFAULT_COMPRESSION;
|
2008-04-06 12:46:52 +02:00
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
deflateInit(&((zlib_stream_t *) conn->stream)->outstream, level);
|
2008-04-06 12:46:52 +02:00
|
|
|
if(recvqlen > 0)
|
|
|
|
common_zlib_inflate(conn, recvq_start, recvqlen);
|
2009-01-01 12:50:21 +01:00
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
conn_mod_read_cb(conn->mod_fd, conn);
|
|
|
|
conn_plain_read_cb(conn->plain_fd, conn);
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
ssl_new_keys(mod_ctl_t * ctl, mod_ctl_buf_t * ctl_buf)
|
|
|
|
{
|
|
|
|
char *buf;
|
2015-12-12 14:50:48 +01:00
|
|
|
char *cert, *key, *dhparam, *cipher_list;
|
2008-04-06 12:46:52 +02:00
|
|
|
|
2014-02-08 19:40:35 +01:00
|
|
|
buf = (char *) &ctl_buf->buf[2];
|
2008-04-06 12:46:52 +02:00
|
|
|
cert = buf;
|
|
|
|
buf += strlen(cert) + 1;
|
|
|
|
key = buf;
|
|
|
|
buf += strlen(key) + 1;
|
|
|
|
dhparam = buf;
|
2015-12-12 14:50:48 +01:00
|
|
|
buf += strlen(dhparam) + 1;
|
|
|
|
cipher_list = buf;
|
2016-05-05 06:10:57 +02:00
|
|
|
if(strlen(key) == 0)
|
|
|
|
key = cert;
|
2016-05-03 19:47:29 +02:00
|
|
|
if(strlen(dhparam) == 0)
|
|
|
|
dhparam = NULL;
|
2015-12-12 14:50:48 +01:00
|
|
|
if(strlen(cipher_list) == 0)
|
|
|
|
cipher_list = NULL;
|
2008-04-06 12:46:52 +02:00
|
|
|
|
2015-12-12 14:50:48 +01:00
|
|
|
if(!rb_setup_ssl_server(cert, key, dhparam, cipher_list))
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
const char *invalid = "I";
|
|
|
|
mod_cmd_write_queue(ctl, invalid, strlen(invalid));
|
|
|
|
return;
|
2008-12-03 00:49:39 +01:00
|
|
|
}
|
2008-04-06 12:46:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
send_nossl_support(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
static const char *nossl_cmd = "N";
|
|
|
|
conn_t *conn;
|
2015-12-11 06:40:24 +01:00
|
|
|
uint32_t id;
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
if(ctlb != NULL)
|
2008-12-03 00:49:39 +01:00
|
|
|
{
|
2008-04-06 12:46:52 +02:00
|
|
|
conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]);
|
2015-12-11 06:40:24 +01:00
|
|
|
id = buf_to_uint32(&ctlb->buf[1]);
|
|
|
|
conn_add_id_hash(conn, id);
|
2008-04-06 12:46:52 +02:00
|
|
|
close_conn(conn, WAIT_PLAIN, "libratbox reports no SSL/TLS support");
|
2008-12-03 00:49:39 +01:00
|
|
|
}
|
|
|
|
mod_cmd_write_queue(ctl, nossl_cmd, strlen(nossl_cmd));
|
2008-04-06 12:46:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
send_i_am_useless(mod_ctl_t * ctl)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
static const char *useless = "U";
|
|
|
|
mod_cmd_write_queue(ctl, useless, strlen(useless));
|
|
|
|
}
|
|
|
|
|
2016-03-03 23:08:13 +01:00
|
|
|
static void
|
|
|
|
send_version(mod_ctl_t * ctl)
|
|
|
|
{
|
|
|
|
char version[256] = { 'V', 0 };
|
|
|
|
strncpy(&version[1], rb_lib_version(), sizeof(version) - 2);
|
|
|
|
mod_cmd_write_queue(ctl, version, strlen(version));
|
|
|
|
}
|
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
send_nozlib_support(mod_ctl_t * ctl, mod_ctl_buf_t * ctlb)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
static const char *nozlib_cmd = "z";
|
|
|
|
conn_t *conn;
|
2015-12-11 06:40:24 +01:00
|
|
|
uint32_t id;
|
2008-04-06 12:46:52 +02:00
|
|
|
if(ctlb != NULL)
|
|
|
|
{
|
|
|
|
conn = make_conn(ctl, ctlb->F[0], ctlb->F[1]);
|
2015-12-11 06:40:24 +01:00
|
|
|
id = buf_to_uint32(&ctlb->buf[1]);
|
|
|
|
conn_add_id_hash(conn, id);
|
2008-04-06 12:46:52 +02:00
|
|
|
close_conn(conn, WAIT_PLAIN, "libratbox reports no zlib support");
|
2008-12-03 00:49:39 +01:00
|
|
|
}
|
2008-04-06 12:46:52 +02:00
|
|
|
mod_cmd_write_queue(ctl, nozlib_cmd, strlen(nozlib_cmd));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mod_process_cmd_recv(mod_ctl_t * ctl)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *next;
|
|
|
|
mod_ctl_buf_t *ctl_buf;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, ctl->readq.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl_buf = (mod_ctl_buf_t *)ptr->data;
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
switch (*ctl_buf->buf)
|
|
|
|
{
|
|
|
|
case 'A':
|
|
|
|
{
|
2009-11-15 22:41:02 +01:00
|
|
|
if (ctl_buf->nfds != 2 || ctl_buf->buflen != 5)
|
|
|
|
{
|
|
|
|
cleanup_bad_message(ctl, ctl_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-03-19 06:58:15 +01:00
|
|
|
if(!ssld_ssl_ok)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
send_nossl_support(ctl, ctl_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ssl_process_accept(ctl, ctl_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'C':
|
|
|
|
{
|
2016-04-23 23:46:25 +02:00
|
|
|
if (ctl_buf->buflen != 5)
|
2009-11-15 22:41:02 +01:00
|
|
|
{
|
|
|
|
cleanup_bad_message(ctl, ctl_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-03-19 06:58:15 +01:00
|
|
|
if(!ssld_ssl_ok)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
send_nossl_support(ctl, ctl_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ssl_process_connect(ctl, ctl_buf);
|
|
|
|
break;
|
|
|
|
}
|
2015-05-27 23:46:46 +02:00
|
|
|
case 'F':
|
|
|
|
{
|
2016-04-23 21:46:26 +02:00
|
|
|
if (ctl_buf->buflen != 5)
|
2015-05-27 23:46:46 +02:00
|
|
|
{
|
|
|
|
cleanup_bad_message(ctl, ctl_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ssl_change_certfp_method(ctl, ctl_buf);
|
|
|
|
break;
|
|
|
|
}
|
2008-04-06 12:46:52 +02:00
|
|
|
case 'K':
|
|
|
|
{
|
2016-03-19 06:58:15 +01:00
|
|
|
if(!ssld_ssl_ok)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
send_nossl_support(ctl, ctl_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ssl_new_keys(ctl, ctl_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'S':
|
|
|
|
{
|
|
|
|
process_stats(ctl, ctl_buf);
|
|
|
|
break;
|
|
|
|
}
|
2009-03-07 03:23:17 +01:00
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
case 'Z':
|
|
|
|
{
|
2009-11-15 22:41:02 +01:00
|
|
|
if (ctl_buf->nfds != 2 || ctl_buf->buflen < 6)
|
|
|
|
{
|
|
|
|
cleanup_bad_message(ctl, ctl_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
/* just zlib only */
|
|
|
|
zlib_process(ctl, ctl_buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
2014-03-03 05:25:47 +01:00
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
case 'Z':
|
2008-06-26 19:19:19 +02:00
|
|
|
send_nozlib_support(ctl, ctl_buf);
|
2008-04-06 12:46:52 +02:00
|
|
|
break;
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
/* Log unknown commands */
|
|
|
|
}
|
|
|
|
rb_dlinkDelete(ptr, &ctl->readq);
|
|
|
|
rb_free(ctl_buf->buf);
|
|
|
|
rb_free(ctl_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
mod_read_ctl(rb_fde_t *F, void *data)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
mod_ctl_buf_t *ctl_buf;
|
2016-07-13 07:17:21 +02:00
|
|
|
mod_ctl_t *ctl = (mod_ctl_t *)data;
|
2008-04-06 12:46:52 +02:00
|
|
|
int retlen;
|
2009-11-15 22:41:02 +01:00
|
|
|
int i;
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl_buf = (mod_ctl_buf_t *)rb_malloc(sizeof(mod_ctl_buf_t));
|
|
|
|
ctl_buf->buf = (uint8_t *)rb_malloc(READBUF_SIZE);
|
2008-04-06 12:46:52 +02:00
|
|
|
ctl_buf->buflen = READBUF_SIZE;
|
|
|
|
retlen = rb_recv_fd_buf(ctl->F, ctl_buf->buf, ctl_buf->buflen, ctl_buf->F,
|
|
|
|
MAXPASSFD);
|
|
|
|
if(retlen <= 0)
|
|
|
|
{
|
|
|
|
rb_free(ctl_buf->buf);
|
|
|
|
rb_free(ctl_buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctl_buf->buflen = retlen;
|
|
|
|
rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq);
|
2009-11-15 22:41:02 +01:00
|
|
|
for (i = 0; i < MAXPASSFD && ctl_buf->F[i] != NULL; i++)
|
|
|
|
;
|
|
|
|
ctl_buf->nfds = i;
|
2008-04-06 12:46:52 +02:00
|
|
|
}
|
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
while(retlen > 0);
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
mod_process_cmd_recv(ctl);
|
|
|
|
rb_setselect(ctl->F, RB_SELECT_READ, mod_read_ctl, ctl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
mod_write_ctl(rb_fde_t *F, void *data)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
mod_ctl_t *ctl = (mod_ctl_t *)data;
|
2008-04-06 12:46:52 +02:00
|
|
|
mod_ctl_buf_t *ctl_buf;
|
|
|
|
rb_dlink_node *ptr, *next;
|
|
|
|
int retlen, x;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, next, ctl->writeq.head)
|
|
|
|
{
|
2016-07-13 07:17:21 +02:00
|
|
|
ctl_buf = (mod_ctl_buf_t *)ptr->data;
|
2008-04-06 12:46:52 +02:00
|
|
|
retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf,
|
2008-12-03 00:49:39 +01:00
|
|
|
ctl_buf->buflen, ppid);
|
2008-04-06 12:46:52 +02:00
|
|
|
if(retlen > 0)
|
|
|
|
{
|
|
|
|
rb_dlinkDelete(ptr, &ctl->writeq);
|
2008-12-03 00:49:39 +01:00
|
|
|
for(x = 0; x < ctl_buf->nfds; x++)
|
2008-04-06 12:46:52 +02:00
|
|
|
rb_close(ctl_buf->F[x]);
|
|
|
|
rb_free(ctl_buf->buf);
|
|
|
|
rb_free(ctl_buf);
|
|
|
|
|
|
|
|
}
|
|
|
|
if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
|
2008-12-03 00:49:39 +01:00
|
|
|
exit(0);
|
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
}
|
2009-03-03 22:32:18 +01:00
|
|
|
if(rb_dlink_list_length(&ctl->writeq) > 0)
|
|
|
|
rb_setselect(ctl->F, RB_SELECT_WRITE, mod_write_ctl, ctl);
|
2008-04-06 12:46:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2008-12-03 00:49:39 +01:00
|
|
|
read_pipe_ctl(rb_fde_t *F, void *data)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
2015-12-12 11:50:15 +01:00
|
|
|
char inbuf[READBUF_SIZE];
|
2008-04-06 12:46:52 +02:00
|
|
|
int retlen;
|
2008-12-03 00:49:39 +01:00
|
|
|
while((retlen = rb_read(F, inbuf, sizeof(inbuf))) > 0)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
;; /* we don't do anything with the pipe really, just care if the other process dies.. */
|
|
|
|
}
|
|
|
|
if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
|
|
|
|
exit(0);
|
|
|
|
rb_setselect(F, RB_SELECT_READ, read_pipe_ctl, NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
const char *s_ctlfd, *s_pipe, *s_pid;
|
2008-04-06 12:46:52 +02:00
|
|
|
int ctlfd, pipefd, x, maxfd;
|
|
|
|
maxfd = maxconn();
|
2008-12-03 00:49:39 +01:00
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
s_ctlfd = getenv("CTL_FD");
|
|
|
|
s_pipe = getenv("CTL_PIPE");
|
2008-12-03 00:49:39 +01:00
|
|
|
s_pid = getenv("CTL_PPID");
|
2008-04-06 12:46:52 +02:00
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
if(s_ctlfd == NULL || s_pipe == NULL || s_pid == NULL)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
fprintf(stderr,
|
2016-03-06 04:39:51 +01:00
|
|
|
"This is the charybdis ssld for internal ircd use.\n");
|
2008-12-03 00:49:39 +01:00
|
|
|
fprintf(stderr,
|
2016-03-06 04:39:51 +01:00
|
|
|
"You aren't supposed to run me directly. Exiting.\n");
|
2008-04-06 12:46:52 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctlfd = atoi(s_ctlfd);
|
|
|
|
pipefd = atoi(s_pipe);
|
2008-12-03 00:49:39 +01:00
|
|
|
ppid = atoi(s_pid);
|
|
|
|
x = 0;
|
|
|
|
#ifndef _WIN32
|
|
|
|
for(x = 0; x < maxfd; x++)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
if(x != ctlfd && x != pipefd && x > 2)
|
|
|
|
close(x);
|
|
|
|
}
|
|
|
|
x = open("/dev/null", O_RDWR);
|
2009-03-03 22:32:18 +01:00
|
|
|
|
2008-04-06 12:46:52 +02:00
|
|
|
if(x >= 0)
|
|
|
|
{
|
|
|
|
if(ctlfd != 0 && pipefd != 0)
|
|
|
|
dup2(x, 0);
|
|
|
|
if(ctlfd != 1 && pipefd != 1)
|
|
|
|
dup2(x, 1);
|
|
|
|
if(ctlfd != 2 && pipefd != 2)
|
|
|
|
dup2(x, 2);
|
|
|
|
if(x > 2)
|
|
|
|
close(x);
|
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
#endif
|
2008-04-06 12:46:52 +02:00
|
|
|
setup_signals();
|
|
|
|
rb_lib_init(NULL, NULL, NULL, 0, maxfd, 1024, 4096);
|
|
|
|
rb_init_rawbuffers(1024);
|
2016-04-25 20:02:03 +02:00
|
|
|
rb_init_prng(NULL, RB_PRNG_DEFAULT);
|
2016-03-19 06:58:15 +01:00
|
|
|
ssld_ssl_ok = rb_supports_ssl();
|
2016-07-13 07:17:21 +02:00
|
|
|
mod_ctl = (mod_ctl_t *)rb_malloc(sizeof(mod_ctl_t));
|
2008-04-06 12:46:52 +02:00
|
|
|
mod_ctl->F = rb_open(ctlfd, RB_FD_SOCKET, "ircd control socket");
|
|
|
|
mod_ctl->F_pipe = rb_open(pipefd, RB_FD_PIPE, "ircd pipe");
|
|
|
|
rb_set_nb(mod_ctl->F);
|
|
|
|
rb_set_nb(mod_ctl->F_pipe);
|
|
|
|
rb_event_addish("clean_dead_conns", clean_dead_conns, NULL, 10);
|
2008-06-26 19:21:46 +02:00
|
|
|
rb_event_add("check_handshake_flood", check_handshake_flood, NULL, 10);
|
2008-04-06 12:46:52 +02:00
|
|
|
read_pipe_ctl(mod_ctl->F_pipe, NULL);
|
|
|
|
mod_read_ctl(mod_ctl->F, mod_ctl);
|
2016-03-03 23:08:13 +01:00
|
|
|
send_version(mod_ctl);
|
2016-03-19 06:58:15 +01:00
|
|
|
if(!zlib_ok && !ssld_ssl_ok)
|
2008-04-06 12:46:52 +02:00
|
|
|
{
|
|
|
|
/* this is really useless... */
|
|
|
|
send_i_am_useless(mod_ctl);
|
|
|
|
/* sleep until the ircd kills us */
|
2016-06-01 22:17:09 +02:00
|
|
|
rb_sleep(1 << 30, 0);
|
2008-04-06 12:46:52 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!zlib_ok)
|
|
|
|
send_nozlib_support(mod_ctl, NULL);
|
2016-03-19 06:58:15 +01:00
|
|
|
if(!ssld_ssl_ok)
|
2008-04-06 12:46:52 +02:00
|
|
|
send_nossl_support(mod_ctl, NULL);
|
|
|
|
rb_lib_loop(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-03 00:49:39 +01:00
|
|
|
#ifndef _WIN32
|
2008-04-06 12:46:52 +02:00
|
|
|
static void
|
|
|
|
dummy_handler(int sig)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-12-03 00:49:39 +01:00
|
|
|
#endif
|
2008-04-06 12:46:52 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
setup_signals()
|
|
|
|
{
|
2008-12-03 00:49:39 +01:00
|
|
|
#ifndef _WIN32
|
2008-04-06 12:46:52 +02:00
|
|
|
struct sigaction act;
|
|
|
|
|
|
|
|
act.sa_flags = 0;
|
|
|
|
act.sa_handler = SIG_IGN;
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
sigaddset(&act.sa_mask, SIGPIPE);
|
|
|
|
sigaddset(&act.sa_mask, SIGALRM);
|
|
|
|
#ifdef SIGTRAP
|
|
|
|
sigaddset(&act.sa_mask, SIGTRAP);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGWINCH
|
|
|
|
sigaddset(&act.sa_mask, SIGWINCH);
|
|
|
|
sigaction(SIGWINCH, &act, 0);
|
|
|
|
#endif
|
|
|
|
sigaction(SIGPIPE, &act, 0);
|
|
|
|
#ifdef SIGTRAP
|
|
|
|
sigaction(SIGTRAP, &act, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
act.sa_handler = dummy_handler;
|
|
|
|
sigaction(SIGALRM, &act, 0);
|
2008-12-03 00:49:39 +01:00
|
|
|
#endif
|
2008-04-06 12:46:52 +02:00
|
|
|
}
|