2 * ssld.c: The ircd-ratbox ssl/zlib helper daemon thingy
3 * Copyright (C) 2007 Aaron Sethman <androsyn@ratbox.org>
4 * Copyright (C) 2007 ircd-ratbox development team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21 * $Id: ssld.c 25221 2008-04-09 22:12:16Z jilles $
33 #define READBUF_SIZE 16384
36 static void setup_signals(void);
38 static inline rb_int32_t
buf_to_int32(char *buf
)
48 static inline void int32_to_buf(char *buf
, rb_int32_t x
)
50 *(buf
) = x
>> 24 & 0xFF;
51 *(++buf
) = x
>> 16 & 0xFF;
52 *(++buf
) = x
>> 8 & 0xFF;
57 static inline rb_uint16_t
buf_to_uint16(char *buf
)
65 static inline void uint16_to_buf(char *buf
, rb_uint16_t x
)
67 *(buf
) = x
>> 8 & 0xFF;
74 static char inbuf
[READBUF_SIZE
];
75 static char outbuf
[READBUF_SIZE
];
77 typedef struct _mod_ctl_buf
82 rb_fde_t
*F
[MAXPASSFD
];
86 typedef struct _mod_ctl
96 static mod_ctl_t
*mod_ctl
;
100 typedef struct _zlib_stream
111 rawbuf_head_t
*modbuf_out
;
112 rawbuf_head_t
*plainbuf_out
;
118 unsigned long long mod_out
;
119 unsigned long long mod_in
;
120 unsigned long long plain_in
;
121 unsigned long long plain_out
;
126 #define FLAG_SSL 0x01
127 #define FLAG_ZIP 0x02
128 #define FLAG_CORK 0x04
129 #define FLAG_DEAD 0x08
130 #define FLAG_SSL_W_WANTS_R 0x10 /* output needs to wait until input possible */
131 #define FLAG_SSL_R_WANTS_W 0x20 /* input needs to wait until output possible */
133 #define IsSSL(x) ((x)->flags & FLAG_SSL)
134 #define IsZip(x) ((x)->flags & FLAG_ZIP)
135 #define IsCork(x) ((x)->flags & FLAG_CORK)
136 #define IsDead(x) ((x)->flags & FLAG_DEAD)
137 #define IsSSLWWantsR(x) ((x)->flags & FLAG_SSL_W_WANTS_R)
138 #define IsSSLRWantsW(x) ((x)->flags & FLAG_SSL_R_WANTS_W)
140 #define SetSSL(x) ((x)->flags |= FLAG_SSL)
141 #define SetZip(x) ((x)->flags |= FLAG_ZIP)
142 #define SetCork(x) ((x)->flags |= FLAG_CORK)
143 #define SetDead(x) ((x)->flags |= FLAG_DEAD)
144 #define SetSSLWWantsR(x) ((x)->flags |= FLAG_SSL_W_WANTS_R)
145 #define SetSSLRWantsW(x) ((x)->flags |= FLAG_SSL_R_WANTS_W)
147 #define ClearSSL(x) ((x)->flags &= ~FLAG_SSL)
148 #define ClearZip(x) ((x)->flags &= ~FLAG_ZIP)
149 #define ClearCork(x) ((x)->flags &= ~FLAG_CORK)
150 #define ClearDead(x) ((x)->flags &= ~FLAG_DEAD)
151 #define ClearSSLWWantsR(x) ((x)->flags &= ~FLAG_SSL_W_WANTS_R)
152 #define ClearSSLRWantsW(x) ((x)->flags &= ~FLAG_SSL_R_WANTS_W)
155 #define WAIT_PLAIN 0x1
157 #define CONN_HASH_SIZE 2000
158 #define connid_hash(x) (&connid_hash_table[(x % CONN_HASH_SIZE)])
160 static rb_dlink_list connid_hash_table
[CONN_HASH_SIZE
];
161 static rb_dlink_list dead_list
;
163 static void conn_mod_read_cb(rb_fde_t
* fd
, void *data
);
164 static void conn_mod_write_sendq(rb_fde_t
*, void *data
);
165 static void conn_plain_write_sendq(rb_fde_t
*, void *data
);
166 static void mod_write_ctl(rb_fde_t
*, void *data
);
167 static void conn_plain_read_cb(rb_fde_t
* fd
, void *data
);
168 static void mod_cmd_write_queue(mod_ctl_t
*ctl
, const void *data
, size_t len
);
169 static const char *remote_closed
= "Remote host closed the connection";
172 static int zlib_ok
= 1;
174 static int zlib_ok
= 0;
177 ssld_alloc(void *unused
, size_t count
, size_t size
)
179 return rb_malloc(count
* size
);
183 ssld_free(void *unused
, void *ptr
)
189 conn_find_by_id(rb_int32_t id
)
194 RB_DLINK_FOREACH(ptr
, (connid_hash(id
))->head
)
197 if(conn
->id
== id
&& !IsDead(conn
))
204 conn_add_id_hash(conn_t
* conn
, rb_int32_t id
)
207 rb_dlinkAdd(conn
, &conn
->node
, connid_hash(id
));
211 free_conn(conn_t
* conn
)
213 rb_free_rawbuffer(conn
->modbuf_out
);
214 rb_free_rawbuffer(conn
->plainbuf_out
);
217 zlib_stream_t
*stream
= conn
->stream
;
218 inflateEnd(&stream
->instream
);
219 deflateEnd(&stream
->outstream
);
225 clean_dead_conns(void *unused
)
228 rb_dlink_node
*ptr
, *next
;
229 RB_DLINK_FOREACH_SAFE(ptr
, next
, dead_list
.head
)
234 dead_list
.tail
= dead_list
.head
= NULL
;
239 close_conn(conn_t
* conn
, int wait_plain
, const char *fmt
, ...)
242 char reason
[128]; /* must always be under 250 bytes */
248 rb_rawbuf_flush(conn
->modbuf_out
, conn
->mod_fd
);
249 rb_rawbuf_flush(conn
->plainbuf_out
, conn
->plain_fd
);
250 rb_close(conn
->mod_fd
);
253 if(!wait_plain
|| fmt
== NULL
)
255 rb_close(conn
->plain_fd
);
258 rb_dlinkDelete(&conn
->node
, connid_hash(conn
->id
));
259 rb_dlinkAdd(conn
, &conn
->node
, &dead_list
);
262 rb_setselect(conn
->plain_fd
, RB_SELECT_WRITE
|RB_SELECT_READ
, NULL
, NULL
);
264 rb_vsnprintf(reason
, sizeof(reason
), fmt
, ap
);
268 int32_to_buf(&buf
[1], conn
->id
);
269 strcpy(&buf
[5], reason
);
270 len
= (strlen(reason
) + 1) + 5;
271 mod_cmd_write_queue(conn
->ctl
, buf
, len
);
275 make_conn(mod_ctl_t
*ctl
, rb_fde_t
* mod_fd
, rb_fde_t
* plain_fd
)
277 conn_t
*conn
= rb_malloc(sizeof(conn_t
));
279 conn
->modbuf_out
= rb_new_rawbuffer();
280 conn
->plainbuf_out
= rb_new_rawbuffer();
281 conn
->mod_fd
= mod_fd
;
282 conn
->plain_fd
= plain_fd
;
291 conn_mod_write_sendq(rb_fde_t
* fd
, void *data
)
299 if(IsSSLWWantsR(conn
))
301 ClearSSLWWantsR(conn
);
302 conn_mod_read_cb(conn
->mod_fd
, conn
);
307 while ((retlen
= rb_rawbuf_flush(conn
->modbuf_out
, fd
)) > 0)
308 conn
->mod_out
+= retlen
;
310 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
313 close_conn(conn
, WAIT_PLAIN
, "%s", remote_closed
);
314 if(IsSSL(conn
) && retlen
== RB_RW_SSL_ERROR
)
315 err
= rb_get_ssl_strerror(conn
->mod_fd
);
317 err
= strerror(errno
);
318 close_conn(conn
, WAIT_PLAIN
, "Write error: %s", err
);
321 if(rb_rawbuf_length(conn
->modbuf_out
) > 0)
323 if(retlen
!= RB_RW_SSL_NEED_READ
)
324 rb_setselect(conn
->mod_fd
, RB_SELECT_WRITE
, conn_mod_write_sendq
, conn
);
327 rb_setselect(conn
->mod_fd
, RB_SELECT_READ
, conn_mod_write_sendq
, conn
);
328 rb_setselect(conn
->mod_fd
, RB_SELECT_WRITE
, NULL
, NULL
);
333 rb_setselect(conn
->mod_fd
, RB_SELECT_WRITE
, NULL
, NULL
);
335 if(IsCork(conn
) && rb_rawbuf_length(conn
->modbuf_out
) == 0)
338 conn_plain_read_cb(conn
->plain_fd
, conn
);
344 conn_mod_write(conn_t
* conn
, void *data
, size_t len
)
346 if(IsDead(conn
)) /* no point in queueing to a dead man */
348 rb_rawbuf_append(conn
->modbuf_out
, data
, len
);
352 conn_plain_write(conn_t
* conn
, void *data
, size_t len
)
354 if(IsDead(conn
)) /* again no point in queueing to dead men */
356 rb_rawbuf_append(conn
->plainbuf_out
, data
, len
);
360 mod_cmd_write_queue(mod_ctl_t
* ctl
, const void *data
, size_t len
)
362 mod_ctl_buf_t
*ctl_buf
;
363 ctl_buf
= rb_malloc(sizeof(mod_ctl_buf_t
));
364 ctl_buf
->buf
= rb_malloc(len
);
365 ctl_buf
->buflen
= len
;
366 memcpy(ctl_buf
->buf
, data
, len
);
368 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->writeq
);
369 mod_write_ctl(ctl
->F
, ctl
);
374 common_zlib_deflate(conn_t
* conn
, void *buf
, size_t len
)
377 z_stream
*outstream
= &((zlib_stream_t
*)conn
->stream
)->outstream
;
378 outstream
->next_in
= buf
;
379 outstream
->avail_in
= len
;
380 outstream
->next_out
= (Bytef
*) outbuf
;
381 outstream
->avail_out
= sizeof(outbuf
);
383 ret
= deflate(outstream
, Z_SYNC_FLUSH
);
387 close_conn(conn
, WAIT_PLAIN
, "Deflate failed: %s", zError(ret
));
390 if(outstream
->avail_out
== 0)
392 /* avail_out empty */
393 close_conn(conn
, WAIT_PLAIN
, "error compressing data, avail_out == 0");
396 if(outstream
->avail_in
!= 0)
398 /* avail_in isn't empty...*/
399 close_conn(conn
, WAIT_PLAIN
, "error compressing data, avail_in != 0");
402 have
= sizeof(outbuf
) - outstream
->avail_out
;
403 conn_mod_write(conn
, outbuf
, have
);
407 common_zlib_inflate(conn_t
* conn
, void *buf
, size_t len
)
410 ((zlib_stream_t
*)conn
->stream
)->instream
.next_in
= buf
;
411 ((zlib_stream_t
*)conn
->stream
)->instream
.avail_in
= len
;
412 ((zlib_stream_t
*)conn
->stream
)->instream
.next_out
= (Bytef
*) outbuf
;
413 ((zlib_stream_t
*)conn
->stream
)->instream
.avail_out
= sizeof(outbuf
);
415 while (((zlib_stream_t
*)conn
->stream
)->instream
.avail_in
)
417 ret
= inflate(&((zlib_stream_t
*)conn
->stream
)->instream
, Z_NO_FLUSH
);
420 if(!strncmp("ERROR ", buf
, 6))
422 close_conn(conn
, WAIT_PLAIN
, "Received uncompressed ERROR");
425 close_conn(conn
, WAIT_PLAIN
, "Inflate failed: %s", zError(ret
));
428 have
= sizeof(outbuf
) - ((zlib_stream_t
*)conn
->stream
)->instream
.avail_out
;
430 if(((zlib_stream_t
*)conn
->stream
)->instream
.avail_in
)
432 conn_plain_write(conn
, outbuf
, have
);
434 ((zlib_stream_t
*)conn
->stream
)->instream
.next_out
= (Bytef
*) outbuf
;
435 ((zlib_stream_t
*)conn
->stream
)->instream
.avail_out
= sizeof(outbuf
);
441 conn_plain_write(conn
, outbuf
, have
);
446 plain_check_cork(conn_t
* conn
)
448 if(rb_rawbuf_length(conn
->modbuf_out
) >= 4096)
450 /* if we have over 4k pending outbound, don't read until
451 * we've cleared the queue */
453 rb_setselect(conn
->plain_fd
, RB_SELECT_READ
, NULL
, NULL
);
455 conn_mod_write_sendq(conn
->mod_fd
, conn
);
463 conn_plain_read_cb(rb_fde_t
* fd
, void *data
)
473 if(plain_check_cork(conn
))
481 length
= rb_read(conn
->plain_fd
, inbuf
, sizeof(inbuf
));
483 if(length
== 0 || (length
< 0 && !rb_ignore_errno(errno
)))
485 close_conn(conn
, NO_WAIT
, NULL
);
491 rb_setselect(conn
->plain_fd
, RB_SELECT_READ
, conn_plain_read_cb
, conn
);
492 conn_mod_write_sendq(conn
->mod_fd
, conn
);
495 conn
->plain_in
+= length
;
499 common_zlib_deflate(conn
, inbuf
, length
);
502 conn_mod_write(conn
, inbuf
, length
);
505 if(plain_check_cork(conn
))
511 conn_mod_read_cb(rb_fde_t
* fd
, void *data
)
514 const char *err
= remote_closed
;
521 if(IsSSLRWantsW(conn
))
523 ClearSSLRWantsW(conn
);
524 conn_mod_write_sendq(conn
->mod_fd
, conn
);
534 length
= rb_read(conn
->mod_fd
, inbuf
, sizeof(inbuf
));
536 if(length
== 0 || (length
< 0 && !rb_ignore_errno(errno
)))
539 close_conn(conn
, WAIT_PLAIN
, "%s", remote_closed
);
543 if(IsSSL(conn
) && length
== RB_RW_SSL_ERROR
)
544 err
= rb_get_ssl_strerror(conn
->mod_fd
);
546 err
= strerror(errno
);
547 close_conn(conn
, WAIT_PLAIN
, "Read error: %s", err
);
552 if(length
!= RB_RW_SSL_NEED_WRITE
)
553 rb_setselect(conn
->mod_fd
, RB_SELECT_READ
, conn_mod_read_cb
, conn
);
556 rb_setselect(conn
->mod_fd
, RB_SELECT_READ
, NULL
, NULL
);
557 rb_setselect(conn
->mod_fd
, RB_SELECT_WRITE
, conn_mod_read_cb
, conn
);
560 conn_plain_write_sendq(conn
->plain_fd
, conn
);
563 conn
->mod_in
+= length
;
566 common_zlib_inflate(conn
, inbuf
, length
);
569 conn_plain_write(conn
, inbuf
, length
);
574 conn_plain_write_sendq(rb_fde_t
* fd
, void *data
)
582 while ((retlen
= rb_rawbuf_flush(conn
->plainbuf_out
, fd
)) > 0)
584 conn
->plain_out
+= retlen
;
586 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
588 close_conn(data
, NO_WAIT
, NULL
);
593 if(rb_rawbuf_length(conn
->plainbuf_out
) > 0)
594 rb_setselect(conn
->plain_fd
, RB_SELECT_WRITE
, conn_plain_write_sendq
, conn
);
596 rb_setselect(conn
->plain_fd
, RB_SELECT_WRITE
, NULL
, NULL
);
602 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
605 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
607 return limit
.rlim_cur
;
609 #endif /* RLIMIT_FD_MAX */
610 return MAXCONNECTIONS
;
614 ssl_process_accept_cb(rb_fde_t
* F
, int status
, struct sockaddr
*addr
, rb_socklen_t len
, void *data
)
619 conn_mod_read_cb(conn
->mod_fd
, conn
);
620 conn_plain_read_cb(conn
->plain_fd
, conn
);
623 close_conn(conn
, NO_WAIT
, 0);
628 ssl_process_connect_cb(rb_fde_t
* F
, int status
, void *data
)
633 conn_mod_read_cb(conn
->mod_fd
, conn
);
634 conn_plain_read_cb(conn
->plain_fd
, conn
);
637 close_conn(conn
, NO_WAIT
, 0);
643 ssl_process_accept(mod_ctl_t
* ctl
, mod_ctl_buf_t
* ctlb
)
648 conn
= make_conn(ctl
, ctlb
->F
[0], ctlb
->F
[1]);
650 id
= buf_to_int32(&ctlb
->buf
[1]);
653 conn_add_id_hash(conn
, id
);
656 if(rb_get_type(conn
->mod_fd
) & RB_FD_UNKNOWN
)
659 rb_set_type(conn
->mod_fd
, RB_FD_SOCKET
);
661 if(rb_get_type(conn
->mod_fd
) == RB_FD_UNKNOWN
)
662 rb_set_type(conn
->plain_fd
, RB_FD_SOCKET
);
664 rb_ssl_start_accepted(ctlb
->F
[0], ssl_process_accept_cb
, conn
, 10);
668 ssl_process_connect(mod_ctl_t
* ctl
, mod_ctl_buf_t
* ctlb
)
672 conn
= make_conn(ctl
, ctlb
->F
[0], ctlb
->F
[1]);
674 id
= buf_to_int32(&ctlb
->buf
[1]);
677 conn_add_id_hash(conn
, id
);
680 if(rb_get_type(conn
->mod_fd
) == RB_FD_UNKNOWN
)
681 rb_set_type(conn
->mod_fd
, RB_FD_SOCKET
);
683 if(rb_get_type(conn
->mod_fd
) == RB_FD_UNKNOWN
)
684 rb_set_type(conn
->plain_fd
, RB_FD_SOCKET
);
687 rb_ssl_start_connected(ctlb
->F
[0], ssl_process_connect_cb
, conn
, 10);
691 process_stats(mod_ctl_t
* ctl
, mod_ctl_buf_t
* ctlb
)
698 id
= buf_to_int32(&ctlb
->buf
[1]);
703 odata
= &ctlb
->buf
[5];
704 conn
= conn_find_by_id(id
);
709 rb_snprintf(outstat
, sizeof(outstat
), "S %s %llu %llu %llu %llu", odata
,
710 conn
->plain_out
, conn
->mod_in
, conn
->plain_in
, conn
->mod_out
);
715 mod_cmd_write_queue(ctl
, outstat
, strlen(outstat
) + 1); /* +1 is so we send the \0 as well */
720 zlib_send_zip_ready(mod_ctl_t
*ctl
, conn_t
*conn
)
725 int32_to_buf(&buf
[1], conn
->id
);
726 mod_cmd_write_queue(conn
->ctl
, buf
, sizeof(buf
));
730 zlib_process(mod_ctl_t
* ctl
, mod_ctl_buf_t
* ctlb
)
734 size_t hdr
= (sizeof(rb_uint8_t
) * 2) + sizeof(rb_int32_t
);
736 z_stream
*instream
, *outstream
;
740 conn
= make_conn(ctl
, ctlb
->F
[0], ctlb
->F
[1]);
741 if(rb_get_type(conn
->mod_fd
) == RB_FD_UNKNOWN
)
742 rb_set_type(conn
->mod_fd
, RB_FD_SOCKET
);
744 if(rb_get_type(conn
->plain_fd
) == RB_FD_UNKNOWN
)
745 rb_set_type(conn
->plain_fd
, RB_FD_SOCKET
);
747 id
= buf_to_int32(&ctlb
->buf
[1]);
748 conn_add_id_hash(conn
, id
);
750 level
= (rb_uint8_t
) ctlb
->buf
[5];
752 recvqlen
= ctlb
->buflen
- hdr
;
753 recvq_start
= &ctlb
->buf
[6];
756 conn
->stream
= rb_malloc(sizeof(zlib_stream_t
));
757 instream
= &((zlib_stream_t
*)conn
->stream
)->instream
;
758 outstream
= &((zlib_stream_t
*)conn
->stream
)->outstream
;
760 instream
->total_in
= 0;
761 instream
->total_out
= 0;
762 instream
->zalloc
= (alloc_func
) ssld_alloc
;
763 instream
->zfree
= (free_func
) ssld_free
;
764 instream
->data_type
= Z_ASCII
;
765 inflateInit(&((zlib_stream_t
*)conn
->stream
)->instream
);
767 outstream
->total_in
= 0;
768 outstream
->total_out
= 0;
769 outstream
->zalloc
= (alloc_func
) ssld_alloc
;
770 outstream
->zfree
= (free_func
) ssld_free
;
771 outstream
->data_type
= Z_ASCII
;
774 level
= Z_DEFAULT_COMPRESSION
;
776 deflateInit(&((zlib_stream_t
*)conn
->stream
)->outstream
, level
);
778 common_zlib_inflate(conn
, recvq_start
, recvqlen
);
779 zlib_send_zip_ready(ctl
, conn
);
780 conn_mod_read_cb(conn
->mod_fd
, conn
);
781 conn_plain_read_cb(conn
->plain_fd
, conn
);
788 init_prng(mod_ctl_t
* ctl
, mod_ctl_buf_t
* ctl_buf
)
791 prng_seed_t seed_type
;
793 seed_type
= (prng_seed_t
)ctl_buf
->buf
[1];
794 path
= &ctl_buf
->buf
[2];
795 rb_init_prng(path
, seed_type
);
800 ssl_new_keys(mod_ctl_t
* ctl
, mod_ctl_buf_t
* ctl_buf
)
803 char *cert
, *key
, *dhparam
;
805 buf
= &ctl_buf
->buf
[2];
807 buf
+= strlen(cert
) + 1;
809 buf
+= strlen(key
) + 1;
811 if(strlen(dhparam
) == 0)
814 if(!rb_setup_ssl_server(cert
, key
, dhparam
))
816 const char *invalid
= "I";
817 mod_cmd_write_queue(ctl
, invalid
, strlen(invalid
));
823 send_nossl_support(mod_ctl_t
*ctl
, mod_ctl_buf_t
*ctlb
)
825 static const char *nossl_cmd
= "N";
831 conn
= make_conn(ctl
, ctlb
->F
[0], ctlb
->F
[1]);
832 id
= buf_to_int32(&ctlb
->buf
[1]);
835 conn_add_id_hash(conn
, id
);
836 close_conn(conn
, WAIT_PLAIN
, "libratbox reports no SSL/TLS support");
838 mod_cmd_write_queue(ctl
, nossl_cmd
, strlen(nossl_cmd
));
842 send_i_am_useless(mod_ctl_t
*ctl
)
844 static const char *useless
= "U";
845 mod_cmd_write_queue(ctl
, useless
, strlen(useless
));
849 send_nozlib_support(mod_ctl_t
*ctl
, mod_ctl_buf_t
*ctlb
)
851 static const char *nozlib_cmd
= "z";
856 conn
= make_conn(ctl
, ctlb
->F
[0], ctlb
->F
[1]);
857 id
= buf_to_int32(&ctlb
->buf
[1]);
860 conn_add_id_hash(conn
, id
);
861 close_conn(conn
, WAIT_PLAIN
, "libratbox reports no zlib support");
863 mod_cmd_write_queue(ctl
, nozlib_cmd
, strlen(nozlib_cmd
));
867 mod_process_cmd_recv(mod_ctl_t
* ctl
)
869 rb_dlink_node
*ptr
, *next
;
870 mod_ctl_buf_t
*ctl_buf
;
872 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->readq
.head
)
876 switch (*ctl_buf
->buf
)
882 send_nossl_support(ctl
, ctl_buf
);
885 ssl_process_accept(ctl
, ctl_buf
);
892 send_nossl_support(ctl
, ctl_buf
);
895 ssl_process_connect(ctl
, ctl_buf
);
903 send_nossl_support(ctl
, ctl_buf
);
906 ssl_new_keys(ctl
, ctl_buf
);
910 init_prng(ctl
, ctl_buf
);
914 process_stats(ctl
, ctl_buf
);
921 zlib_process(ctl
, ctl_buf
);
927 send_nozlib_support(ctl
);
933 /* Log unknown commands */
935 rb_dlinkDelete(ptr
, &ctl
->readq
);
936 rb_free(ctl_buf
->buf
);
945 mod_read_ctl(rb_fde_t
* F
, void *data
)
947 mod_ctl_buf_t
*ctl_buf
;
948 mod_ctl_t
*ctl
= data
;
953 ctl_buf
= rb_malloc(sizeof(mod_ctl_buf_t
));
954 ctl_buf
->buf
= rb_malloc(READBUF_SIZE
);
955 ctl_buf
->buflen
= READBUF_SIZE
;
956 retlen
= rb_recv_fd_buf(ctl
->F
, ctl_buf
->buf
, ctl_buf
->buflen
, ctl_buf
->F
,
960 rb_free(ctl_buf
->buf
);
965 ctl_buf
->buflen
= retlen
;
966 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->readq
);
971 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
974 mod_process_cmd_recv(ctl
);
975 rb_setselect(ctl
->F
, RB_SELECT_READ
, mod_read_ctl
, ctl
);
979 mod_write_ctl(rb_fde_t
* F
, void *data
)
981 mod_ctl_t
*ctl
= data
;
982 mod_ctl_buf_t
*ctl_buf
;
983 rb_dlink_node
*ptr
, *next
;
986 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->writeq
.head
)
989 retlen
= rb_send_fd_buf(ctl
->F
, ctl_buf
->F
, ctl_buf
->nfds
, ctl_buf
->buf
,
993 rb_dlinkDelete(ptr
, &ctl
->writeq
);
994 for (x
= 0; x
< ctl_buf
->nfds
; x
++)
995 rb_close(ctl_buf
->F
[x
]);
996 rb_free(ctl_buf
->buf
);
1000 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
1003 rb_setselect(ctl
->F
, RB_SELECT_WRITE
, mod_write_ctl
, ctl
);
1009 read_pipe_ctl(rb_fde_t
* F
, void *data
)
1012 while ((retlen
= rb_read(F
, inbuf
, sizeof(inbuf
))) > 0)
1014 ;; /* we don't do anything with the pipe really, just care if the other process dies.. */
1016 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
1018 rb_setselect(F
, RB_SELECT_READ
, read_pipe_ctl
, NULL
);
1023 main(int argc
, char **argv
)
1025 const char *s_ctlfd
, *s_pipe
;
1026 int ctlfd
, pipefd
, x
, maxfd
;
1028 s_ctlfd
= getenv("CTL_FD");
1029 s_pipe
= getenv("CTL_PIPE");
1031 if(s_ctlfd
== NULL
|| s_pipe
== NULL
)
1033 fprintf(stderr
, "This is ircd-ratbox ssld. You know you aren't supposed to run me directly?\n");
1034 fprintf(stderr
, "You get an Id tag for this: $Id: ssld.c 25221 2008-04-09 22:12:16Z jilles $\n");
1035 fprintf(stderr
, "Have a nice life\n");
1039 ctlfd
= atoi(s_ctlfd
);
1040 pipefd
= atoi(s_pipe
);
1042 for (x
= 0; x
< maxfd
; x
++)
1044 if(x
!= ctlfd
&& x
!= pipefd
&& x
> 2)
1049 x
= open("/dev/null", O_RDWR
);
1052 if(ctlfd
!= 0 && pipefd
!= 0)
1054 if(ctlfd
!= 1 && pipefd
!= 1)
1056 if(ctlfd
!= 2 && pipefd
!= 2)
1063 rb_lib_init(NULL
, NULL
, NULL
, 0, maxfd
, 1024, 4096);
1064 rb_init_rawbuffers(1024);
1065 ssl_ok
= rb_supports_ssl();
1067 mod_ctl
= rb_malloc(sizeof(mod_ctl_t
));
1068 mod_ctl
->F
= rb_open(ctlfd
, RB_FD_SOCKET
, "ircd control socket");
1069 mod_ctl
->F_pipe
= rb_open(pipefd
, RB_FD_PIPE
, "ircd pipe");
1070 rb_set_nb(mod_ctl
->F
);
1071 rb_set_nb(mod_ctl
->F_pipe
);
1072 rb_event_addish("clean_dead_conns", clean_dead_conns
, NULL
, 10);
1073 read_pipe_ctl(mod_ctl
->F_pipe
, NULL
);
1074 mod_read_ctl(mod_ctl
->F
, mod_ctl
);
1075 if(!zlib_ok
&& !ssl_ok
)
1077 /* this is really useless... */
1078 send_i_am_useless(mod_ctl
);
1079 /* sleep until the ircd kills us */
1085 send_nozlib_support(mod_ctl
, NULL
);
1087 send_nossl_support(mod_ctl
, NULL
);
1095 dummy_handler(int sig
)
1103 struct sigaction act
;
1106 act
.sa_handler
= SIG_IGN
;
1107 sigemptyset(&act
.sa_mask
);
1108 sigaddset(&act
.sa_mask
, SIGPIPE
);
1109 sigaddset(&act
.sa_mask
, SIGALRM
);
1111 sigaddset(&act
.sa_mask
, SIGTRAP
);
1115 sigaddset(&act
.sa_mask
, SIGWINCH
);
1116 sigaction(SIGWINCH
, &act
, 0);
1118 sigaction(SIGPIPE
, &act
, 0);
1120 sigaction(SIGTRAP
, &act
, 0);
1123 act
.sa_handler
= dummy_handler
;
1124 sigaction(SIGALRM
, &act
, 0);