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 25594 2008-06-25 14:58:47Z androsyn $
33 #define READBUF_SIZE 16384
36 static void setup_signals(void);
38 static inline rb_int32_t
buf_to_int32(char *buf
)
41 memcpy(&x
, buf
, sizeof(x
));
45 static inline void int32_to_buf(char *buf
, rb_int32_t x
)
47 memcpy(buf
, &x
, sizeof(x
));
51 static inline rb_uint16_t
buf_to_uint16(char *buf
)
54 memcpy(&x
, buf
, sizeof(x
));
58 static inline void uint16_to_buf(char *buf
, rb_uint16_t x
)
60 memcpy(buf
, &x
, sizeof(x
));
65 static char inbuf
[READBUF_SIZE
];
67 static char outbuf
[READBUF_SIZE
];
70 typedef struct _mod_ctl_buf
75 rb_fde_t
*F
[MAXPASSFD
];
79 typedef struct _mod_ctl
89 static mod_ctl_t
*mod_ctl
;
93 typedef struct _zlib_stream
104 rawbuf_head_t
*modbuf_out
;
105 rawbuf_head_t
*plainbuf_out
;
111 unsigned long long mod_out
;
112 unsigned long long mod_in
;
113 unsigned long long plain_in
;
114 unsigned long long plain_out
;
119 #define FLAG_SSL 0x01
120 #define FLAG_ZIP 0x02
121 #define FLAG_CORK 0x04
122 #define FLAG_DEAD 0x08
123 #define FLAG_SSL_W_WANTS_R 0x10 /* output needs to wait until input possible */
124 #define FLAG_SSL_R_WANTS_W 0x20 /* input needs to wait until output possible */
126 #define IsSSL(x) ((x)->flags & FLAG_SSL)
127 #define IsZip(x) ((x)->flags & FLAG_ZIP)
128 #define IsCork(x) ((x)->flags & FLAG_CORK)
129 #define IsDead(x) ((x)->flags & FLAG_DEAD)
130 #define IsSSLWWantsR(x) ((x)->flags & FLAG_SSL_W_WANTS_R)
131 #define IsSSLRWantsW(x) ((x)->flags & FLAG_SSL_R_WANTS_W)
133 #define SetSSL(x) ((x)->flags |= FLAG_SSL)
134 #define SetZip(x) ((x)->flags |= FLAG_ZIP)
135 #define SetCork(x) ((x)->flags |= FLAG_CORK)
136 #define SetDead(x) ((x)->flags |= FLAG_DEAD)
137 #define SetSSLWWantsR(x) ((x)->flags |= FLAG_SSL_W_WANTS_R)
138 #define SetSSLRWantsW(x) ((x)->flags |= FLAG_SSL_R_WANTS_W)
140 #define ClearSSL(x) ((x)->flags &= ~FLAG_SSL)
141 #define ClearZip(x) ((x)->flags &= ~FLAG_ZIP)
142 #define ClearCork(x) ((x)->flags &= ~FLAG_CORK)
143 #define ClearDead(x) ((x)->flags &= ~FLAG_DEAD)
144 #define ClearSSLWWantsR(x) ((x)->flags &= ~FLAG_SSL_W_WANTS_R)
145 #define ClearSSLRWantsW(x) ((x)->flags &= ~FLAG_SSL_R_WANTS_W)
148 #define WAIT_PLAIN 0x1
150 #define HASH_WALK_SAFE(i, max, ptr, next, table) for(i = 0; i < max; i++) { RB_DLINK_FOREACH_SAFE(ptr, next, table[i].head)
151 #define HASH_WALK_END }
152 #define CONN_HASH_SIZE 2000
153 #define connid_hash(x) (&connid_hash_table[(x % CONN_HASH_SIZE)])
157 static rb_dlink_list connid_hash_table
[CONN_HASH_SIZE
];
158 static rb_dlink_list dead_list
;
160 static void conn_mod_read_cb(rb_fde_t
* fd
, void *data
);
161 static void conn_mod_write_sendq(rb_fde_t
*, void *data
);
162 static void conn_plain_write_sendq(rb_fde_t
*, void *data
);
163 static void mod_write_ctl(rb_fde_t
*, void *data
);
164 static void conn_plain_read_cb(rb_fde_t
* fd
, void *data
);
165 static void mod_cmd_write_queue(mod_ctl_t
*ctl
, const void *data
, size_t len
);
166 static const char *remote_closed
= "Remote host closed the connection";
169 static int zlib_ok
= 1;
171 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
)
190 conn_find_by_id(rb_int32_t id
)
195 RB_DLINK_FOREACH(ptr
, (connid_hash(id
))->head
)
198 if(conn
->id
== id
&& !IsDead(conn
))
205 conn_add_id_hash(conn_t
* conn
, rb_int32_t id
)
208 rb_dlinkAdd(conn
, &conn
->node
, connid_hash(id
));
212 free_conn(conn_t
* conn
)
214 rb_free_rawbuffer(conn
->modbuf_out
);
215 rb_free_rawbuffer(conn
->plainbuf_out
);
219 zlib_stream_t
*stream
= conn
->stream
;
220 inflateEnd(&stream
->instream
);
221 deflateEnd(&stream
->outstream
);
228 clean_dead_conns(void *unused
)
231 rb_dlink_node
*ptr
, *next
;
232 RB_DLINK_FOREACH_SAFE(ptr
, next
, dead_list
.head
)
237 dead_list
.tail
= dead_list
.head
= NULL
;
242 close_conn(conn_t
* conn
, int wait_plain
, const char *fmt
, ...)
245 char reason
[128]; /* must always be under 250 bytes */
251 rb_rawbuf_flush(conn
->modbuf_out
, conn
->mod_fd
);
252 rb_rawbuf_flush(conn
->plainbuf_out
, conn
->plain_fd
);
253 rb_close(conn
->mod_fd
);
256 if(!wait_plain
|| fmt
== NULL
)
258 rb_close(conn
->plain_fd
);
261 rb_dlinkDelete(&conn
->node
, connid_hash(conn
->id
));
262 rb_dlinkAdd(conn
, &conn
->node
, &dead_list
);
265 rb_setselect(conn
->plain_fd
, RB_SELECT_WRITE
|RB_SELECT_READ
, NULL
, NULL
);
267 rb_vsnprintf(reason
, sizeof(reason
), fmt
, ap
);
271 int32_to_buf(&buf
[1], conn
->id
);
272 strcpy(&buf
[5], reason
);
273 len
= (strlen(reason
) + 1) + 5;
274 mod_cmd_write_queue(conn
->ctl
, buf
, len
);
278 make_conn(mod_ctl_t
*ctl
, rb_fde_t
* mod_fd
, rb_fde_t
* plain_fd
)
280 conn_t
*conn
= rb_malloc(sizeof(conn_t
));
282 conn
->modbuf_out
= rb_new_rawbuffer();
283 conn
->plainbuf_out
= rb_new_rawbuffer();
284 conn
->mod_fd
= mod_fd
;
285 conn
->plain_fd
= plain_fd
;
294 check_handshake_flood(void *unused
)
297 rb_dlink_node
*ptr
, *next
;
300 HASH_WALK_SAFE(i
, CONN_HASH_SIZE
, ptr
, next
, connid_hash_table
)
306 count
= rb_ssl_handshake_count(conn
->mod_fd
);
307 /* nothing needs to do this more than twice in ten seconds i don't think */
309 close_conn(conn
, WAIT_PLAIN
, "Handshake flooding");
311 rb_ssl_clear_handshake_count(conn
->mod_fd
);
318 conn_mod_write_sendq(rb_fde_t
* fd
, void *data
)
326 if(IsSSLWWantsR(conn
))
328 ClearSSLWWantsR(conn
);
329 conn_mod_read_cb(conn
->mod_fd
, conn
);
334 while ((retlen
= rb_rawbuf_flush(conn
->modbuf_out
, fd
)) > 0)
335 conn
->mod_out
+= retlen
;
337 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
340 close_conn(conn
, WAIT_PLAIN
, "%s", remote_closed
);
341 if(IsSSL(conn
) && retlen
== RB_RW_SSL_ERROR
)
342 err
= rb_get_ssl_strerror(conn
->mod_fd
);
344 err
= strerror(errno
);
345 close_conn(conn
, WAIT_PLAIN
, "Write error: %s", err
);
348 if(rb_rawbuf_length(conn
->modbuf_out
) > 0)
350 if(retlen
!= RB_RW_SSL_NEED_READ
)
351 rb_setselect(conn
->mod_fd
, RB_SELECT_WRITE
, conn_mod_write_sendq
, conn
);
354 rb_setselect(conn
->mod_fd
, RB_SELECT_READ
, conn_mod_write_sendq
, conn
);
355 rb_setselect(conn
->mod_fd
, RB_SELECT_WRITE
, NULL
, NULL
);
360 rb_setselect(conn
->mod_fd
, RB_SELECT_WRITE
, NULL
, NULL
);
362 if(IsCork(conn
) && rb_rawbuf_length(conn
->modbuf_out
) == 0)
365 conn_plain_read_cb(conn
->plain_fd
, conn
);
371 conn_mod_write(conn_t
* conn
, void *data
, size_t len
)
373 if(IsDead(conn
)) /* no point in queueing to a dead man */
375 rb_rawbuf_append(conn
->modbuf_out
, data
, len
);
379 conn_plain_write(conn_t
* conn
, void *data
, size_t len
)
381 if(IsDead(conn
)) /* again no point in queueing to dead men */
383 rb_rawbuf_append(conn
->plainbuf_out
, data
, len
);
387 mod_cmd_write_queue(mod_ctl_t
* ctl
, const void *data
, size_t len
)
389 mod_ctl_buf_t
*ctl_buf
;
390 ctl_buf
= rb_malloc(sizeof(mod_ctl_buf_t
));
391 ctl_buf
->buf
= rb_malloc(len
);
392 ctl_buf
->buflen
= len
;
393 memcpy(ctl_buf
->buf
, data
, len
);
395 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->writeq
);
396 mod_write_ctl(ctl
->F
, ctl
);
401 common_zlib_deflate(conn_t
* conn
, void *buf
, size_t len
)
404 z_stream
*outstream
= &((zlib_stream_t
*)conn
->stream
)->outstream
;
405 outstream
->next_in
= buf
;
406 outstream
->avail_in
= len
;
407 outstream
->next_out
= (Bytef
*) outbuf
;
408 outstream
->avail_out
= sizeof(outbuf
);
410 ret
= deflate(outstream
, Z_SYNC_FLUSH
);
414 close_conn(conn
, WAIT_PLAIN
, "Deflate failed: %s", zError(ret
));
417 if(outstream
->avail_out
== 0)
419 /* avail_out empty */
420 close_conn(conn
, WAIT_PLAIN
, "error compressing data, avail_out == 0");
423 if(outstream
->avail_in
!= 0)
425 /* avail_in isn't empty...*/
426 close_conn(conn
, WAIT_PLAIN
, "error compressing data, avail_in != 0");
429 have
= sizeof(outbuf
) - outstream
->avail_out
;
430 conn_mod_write(conn
, outbuf
, have
);
434 common_zlib_inflate(conn_t
* conn
, void *buf
, size_t len
)
437 ((zlib_stream_t
*)conn
->stream
)->instream
.next_in
= buf
;
438 ((zlib_stream_t
*)conn
->stream
)->instream
.avail_in
= len
;
439 ((zlib_stream_t
*)conn
->stream
)->instream
.next_out
= (Bytef
*) outbuf
;
440 ((zlib_stream_t
*)conn
->stream
)->instream
.avail_out
= sizeof(outbuf
);
442 while (((zlib_stream_t
*)conn
->stream
)->instream
.avail_in
)
444 ret
= inflate(&((zlib_stream_t
*)conn
->stream
)->instream
, Z_NO_FLUSH
);
447 if(!strncmp("ERROR ", buf
, 6))
449 close_conn(conn
, WAIT_PLAIN
, "Received uncompressed ERROR");
452 close_conn(conn
, WAIT_PLAIN
, "Inflate failed: %s", zError(ret
));
455 have
= sizeof(outbuf
) - ((zlib_stream_t
*)conn
->stream
)->instream
.avail_out
;
457 if(((zlib_stream_t
*)conn
->stream
)->instream
.avail_in
)
459 conn_plain_write(conn
, outbuf
, have
);
461 ((zlib_stream_t
*)conn
->stream
)->instream
.next_out
= (Bytef
*) outbuf
;
462 ((zlib_stream_t
*)conn
->stream
)->instream
.avail_out
= sizeof(outbuf
);
468 conn_plain_write(conn
, outbuf
, have
);
473 plain_check_cork(conn_t
* conn
)
475 if(rb_rawbuf_length(conn
->modbuf_out
) >= 4096)
477 /* if we have over 4k pending outbound, don't read until
478 * we've cleared the queue */
480 rb_setselect(conn
->plain_fd
, RB_SELECT_READ
, NULL
, NULL
);
482 conn_mod_write_sendq(conn
->mod_fd
, conn
);
490 conn_plain_read_cb(rb_fde_t
* fd
, void *data
)
500 if(plain_check_cork(conn
))
508 length
= rb_read(conn
->plain_fd
, inbuf
, sizeof(inbuf
));
510 if(length
== 0 || (length
< 0 && !rb_ignore_errno(errno
)))
512 close_conn(conn
, NO_WAIT
, NULL
);
518 rb_setselect(conn
->plain_fd
, RB_SELECT_READ
, conn_plain_read_cb
, conn
);
519 conn_mod_write_sendq(conn
->mod_fd
, conn
);
522 conn
->plain_in
+= length
;
526 common_zlib_deflate(conn
, inbuf
, length
);
529 conn_mod_write(conn
, inbuf
, length
);
532 if(plain_check_cork(conn
))
538 conn_mod_read_cb(rb_fde_t
* fd
, void *data
)
541 const char *err
= remote_closed
;
548 if(IsSSLRWantsW(conn
))
550 ClearSSLRWantsW(conn
);
551 conn_mod_write_sendq(conn
->mod_fd
, conn
);
561 length
= rb_read(conn
->mod_fd
, inbuf
, sizeof(inbuf
));
563 if(length
== 0 || (length
< 0 && !rb_ignore_errno(errno
)))
566 close_conn(conn
, WAIT_PLAIN
, "%s", remote_closed
);
570 if(IsSSL(conn
) && length
== RB_RW_SSL_ERROR
)
571 err
= rb_get_ssl_strerror(conn
->mod_fd
);
573 err
= strerror(errno
);
574 close_conn(conn
, WAIT_PLAIN
, "Read error: %s", err
);
579 if(length
!= RB_RW_SSL_NEED_WRITE
)
580 rb_setselect(conn
->mod_fd
, RB_SELECT_READ
, conn_mod_read_cb
, conn
);
583 rb_setselect(conn
->mod_fd
, RB_SELECT_READ
, NULL
, NULL
);
584 rb_setselect(conn
->mod_fd
, RB_SELECT_WRITE
, conn_mod_read_cb
, conn
);
587 conn_plain_write_sendq(conn
->plain_fd
, conn
);
590 conn
->mod_in
+= length
;
593 common_zlib_inflate(conn
, inbuf
, length
);
596 conn_plain_write(conn
, inbuf
, length
);
601 conn_plain_write_sendq(rb_fde_t
* fd
, void *data
)
609 while ((retlen
= rb_rawbuf_flush(conn
->plainbuf_out
, fd
)) > 0)
611 conn
->plain_out
+= retlen
;
613 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
615 close_conn(data
, NO_WAIT
, NULL
);
620 if(rb_rawbuf_length(conn
->plainbuf_out
) > 0)
621 rb_setselect(conn
->plain_fd
, RB_SELECT_WRITE
, conn_plain_write_sendq
, conn
);
623 rb_setselect(conn
->plain_fd
, RB_SELECT_WRITE
, NULL
, NULL
);
629 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
632 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
634 return limit
.rlim_cur
;
636 #endif /* RLIMIT_FD_MAX */
637 return MAXCONNECTIONS
;
641 ssl_process_accept_cb(rb_fde_t
* F
, int status
, struct sockaddr
*addr
, rb_socklen_t len
, void *data
)
646 conn_mod_read_cb(conn
->mod_fd
, conn
);
647 conn_plain_read_cb(conn
->plain_fd
, conn
);
650 /* ircd doesn't care about the reason for this */
651 close_conn(conn
, NO_WAIT
, 0);
656 ssl_process_connect_cb(rb_fde_t
* F
, int status
, void *data
)
661 conn_mod_read_cb(conn
->mod_fd
, conn
);
662 conn_plain_read_cb(conn
->plain_fd
, conn
);
664 else if(status
== RB_ERR_TIMEOUT
)
665 close_conn(conn
, WAIT_PLAIN
, "SSL handshake timed out");
666 else if(status
== RB_ERROR_SSL
)
667 close_conn(conn
, WAIT_PLAIN
, "%s", rb_get_ssl_strerror(conn
->mod_fd
));
669 close_conn(conn
, WAIT_PLAIN
, "SSL handshake failed");
674 ssl_process_accept(mod_ctl_t
* ctl
, mod_ctl_buf_t
* ctlb
)
679 conn
= make_conn(ctl
, ctlb
->F
[0], ctlb
->F
[1]);
681 id
= buf_to_int32(&ctlb
->buf
[1]);
684 conn_add_id_hash(conn
, id
);
687 if(rb_get_type(conn
->mod_fd
) & RB_FD_UNKNOWN
)
690 rb_set_type(conn
->mod_fd
, RB_FD_SOCKET
);
692 if(rb_get_type(conn
->mod_fd
) == RB_FD_UNKNOWN
)
693 rb_set_type(conn
->plain_fd
, RB_FD_SOCKET
);
695 rb_ssl_start_accepted(ctlb
->F
[0], ssl_process_accept_cb
, conn
, 10);
699 ssl_process_connect(mod_ctl_t
* ctl
, mod_ctl_buf_t
* ctlb
)
703 conn
= make_conn(ctl
, ctlb
->F
[0], ctlb
->F
[1]);
705 id
= buf_to_int32(&ctlb
->buf
[1]);
708 conn_add_id_hash(conn
, id
);
711 if(rb_get_type(conn
->mod_fd
) == RB_FD_UNKNOWN
)
712 rb_set_type(conn
->mod_fd
, RB_FD_SOCKET
);
714 if(rb_get_type(conn
->mod_fd
) == RB_FD_UNKNOWN
)
715 rb_set_type(conn
->plain_fd
, RB_FD_SOCKET
);
718 rb_ssl_start_connected(ctlb
->F
[0], ssl_process_connect_cb
, conn
, 10);
722 process_stats(mod_ctl_t
* ctl
, mod_ctl_buf_t
* ctlb
)
729 id
= buf_to_int32(&ctlb
->buf
[1]);
734 odata
= &ctlb
->buf
[5];
735 conn
= conn_find_by_id(id
);
740 rb_snprintf(outstat
, sizeof(outstat
), "S %s %llu %llu %llu %llu", odata
,
741 conn
->plain_out
, conn
->mod_in
, conn
->plain_in
, conn
->mod_out
);
746 mod_cmd_write_queue(ctl
, outstat
, strlen(outstat
) + 1); /* +1 is so we send the \0 as well */
751 zlib_send_zip_ready(mod_ctl_t
*ctl
, conn_t
*conn
)
756 int32_to_buf(&buf
[1], conn
->id
);
757 mod_cmd_write_queue(conn
->ctl
, buf
, sizeof(buf
));
761 zlib_process(mod_ctl_t
* ctl
, mod_ctl_buf_t
* ctlb
)
765 size_t hdr
= (sizeof(rb_uint8_t
) * 2) + sizeof(rb_int32_t
);
767 z_stream
*instream
, *outstream
;
771 conn
= make_conn(ctl
, ctlb
->F
[0], ctlb
->F
[1]);
772 if(rb_get_type(conn
->mod_fd
) == RB_FD_UNKNOWN
)
773 rb_set_type(conn
->mod_fd
, RB_FD_SOCKET
);
775 if(rb_get_type(conn
->plain_fd
) == RB_FD_UNKNOWN
)
776 rb_set_type(conn
->plain_fd
, RB_FD_SOCKET
);
778 id
= buf_to_int32(&ctlb
->buf
[1]);
779 conn_add_id_hash(conn
, id
);
781 level
= (rb_uint8_t
) ctlb
->buf
[5];
783 recvqlen
= ctlb
->buflen
- hdr
;
784 recvq_start
= &ctlb
->buf
[6];
787 conn
->stream
= rb_malloc(sizeof(zlib_stream_t
));
788 instream
= &((zlib_stream_t
*)conn
->stream
)->instream
;
789 outstream
= &((zlib_stream_t
*)conn
->stream
)->outstream
;
791 instream
->total_in
= 0;
792 instream
->total_out
= 0;
793 instream
->zalloc
= (alloc_func
) ssld_alloc
;
794 instream
->zfree
= (free_func
) ssld_free
;
795 instream
->data_type
= Z_ASCII
;
796 inflateInit(&((zlib_stream_t
*)conn
->stream
)->instream
);
798 outstream
->total_in
= 0;
799 outstream
->total_out
= 0;
800 outstream
->zalloc
= (alloc_func
) ssld_alloc
;
801 outstream
->zfree
= (free_func
) ssld_free
;
802 outstream
->data_type
= Z_ASCII
;
805 level
= Z_DEFAULT_COMPRESSION
;
807 deflateInit(&((zlib_stream_t
*)conn
->stream
)->outstream
, level
);
809 common_zlib_inflate(conn
, recvq_start
, recvqlen
);
810 zlib_send_zip_ready(ctl
, conn
);
811 conn_mod_read_cb(conn
->mod_fd
, conn
);
812 conn_plain_read_cb(conn
->plain_fd
, conn
);
819 init_prng(mod_ctl_t
* ctl
, mod_ctl_buf_t
* ctl_buf
)
822 prng_seed_t seed_type
;
824 seed_type
= (prng_seed_t
)ctl_buf
->buf
[1];
825 path
= &ctl_buf
->buf
[2];
826 rb_init_prng(path
, seed_type
);
831 ssl_new_keys(mod_ctl_t
* ctl
, mod_ctl_buf_t
* ctl_buf
)
834 char *cert
, *key
, *dhparam
;
836 buf
= &ctl_buf
->buf
[2];
838 buf
+= strlen(cert
) + 1;
840 buf
+= strlen(key
) + 1;
842 if(strlen(dhparam
) == 0)
845 if(!rb_setup_ssl_server(cert
, key
, dhparam
))
847 const char *invalid
= "I";
848 mod_cmd_write_queue(ctl
, invalid
, strlen(invalid
));
854 send_nossl_support(mod_ctl_t
*ctl
, mod_ctl_buf_t
*ctlb
)
856 static const char *nossl_cmd
= "N";
862 conn
= make_conn(ctl
, ctlb
->F
[0], ctlb
->F
[1]);
863 id
= buf_to_int32(&ctlb
->buf
[1]);
866 conn_add_id_hash(conn
, id
);
867 close_conn(conn
, WAIT_PLAIN
, "libratbox reports no SSL/TLS support");
869 mod_cmd_write_queue(ctl
, nossl_cmd
, strlen(nossl_cmd
));
873 send_i_am_useless(mod_ctl_t
*ctl
)
875 static const char *useless
= "U";
876 mod_cmd_write_queue(ctl
, useless
, strlen(useless
));
880 send_nozlib_support(mod_ctl_t
*ctl
, mod_ctl_buf_t
*ctlb
)
882 static const char *nozlib_cmd
= "z";
887 conn
= make_conn(ctl
, ctlb
->F
[0], ctlb
->F
[1]);
888 id
= buf_to_int32(&ctlb
->buf
[1]);
891 conn_add_id_hash(conn
, id
);
892 close_conn(conn
, WAIT_PLAIN
, "libratbox reports no zlib support");
894 mod_cmd_write_queue(ctl
, nozlib_cmd
, strlen(nozlib_cmd
));
898 mod_process_cmd_recv(mod_ctl_t
* ctl
)
900 rb_dlink_node
*ptr
, *next
;
901 mod_ctl_buf_t
*ctl_buf
;
903 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->readq
.head
)
907 switch (*ctl_buf
->buf
)
913 send_nossl_support(ctl
, ctl_buf
);
916 ssl_process_accept(ctl
, ctl_buf
);
923 send_nossl_support(ctl
, ctl_buf
);
926 ssl_process_connect(ctl
, ctl_buf
);
934 send_nossl_support(ctl
, ctl_buf
);
937 ssl_new_keys(ctl
, ctl_buf
);
941 init_prng(ctl
, ctl_buf
);
945 process_stats(ctl
, ctl_buf
);
952 zlib_process(ctl
, ctl_buf
);
958 send_nozlib_support(ctl
, ctl_buf
);
964 /* Log unknown commands */
966 rb_dlinkDelete(ptr
, &ctl
->readq
);
967 rb_free(ctl_buf
->buf
);
976 mod_read_ctl(rb_fde_t
* F
, void *data
)
978 mod_ctl_buf_t
*ctl_buf
;
979 mod_ctl_t
*ctl
= data
;
984 ctl_buf
= rb_malloc(sizeof(mod_ctl_buf_t
));
985 ctl_buf
->buf
= rb_malloc(READBUF_SIZE
);
986 ctl_buf
->buflen
= READBUF_SIZE
;
987 retlen
= rb_recv_fd_buf(ctl
->F
, ctl_buf
->buf
, ctl_buf
->buflen
, ctl_buf
->F
,
991 rb_free(ctl_buf
->buf
);
996 ctl_buf
->buflen
= retlen
;
997 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->readq
);
1002 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
1005 mod_process_cmd_recv(ctl
);
1006 rb_setselect(ctl
->F
, RB_SELECT_READ
, mod_read_ctl
, ctl
);
1010 mod_write_ctl(rb_fde_t
* F
, void *data
)
1012 mod_ctl_t
*ctl
= data
;
1013 mod_ctl_buf_t
*ctl_buf
;
1014 rb_dlink_node
*ptr
, *next
;
1017 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->writeq
.head
)
1019 ctl_buf
= ptr
->data
;
1020 retlen
= rb_send_fd_buf(ctl
->F
, ctl_buf
->F
, ctl_buf
->nfds
, ctl_buf
->buf
,
1024 rb_dlinkDelete(ptr
, &ctl
->writeq
);
1025 for (x
= 0; x
< ctl_buf
->nfds
; x
++)
1026 rb_close(ctl_buf
->F
[x
]);
1027 rb_free(ctl_buf
->buf
);
1031 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
1034 rb_setselect(ctl
->F
, RB_SELECT_WRITE
, mod_write_ctl
, ctl
);
1040 read_pipe_ctl(rb_fde_t
* F
, void *data
)
1043 while ((retlen
= rb_read(F
, inbuf
, sizeof(inbuf
))) > 0)
1045 ;; /* we don't do anything with the pipe really, just care if the other process dies.. */
1047 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
1049 rb_setselect(F
, RB_SELECT_READ
, read_pipe_ctl
, NULL
);
1054 main(int argc
, char **argv
)
1056 const char *s_ctlfd
, *s_pipe
;
1057 int ctlfd
, pipefd
, x
, maxfd
;
1059 s_ctlfd
= getenv("CTL_FD");
1060 s_pipe
= getenv("CTL_PIPE");
1062 if(s_ctlfd
== NULL
|| s_pipe
== NULL
)
1064 fprintf(stderr
, "This is ircd-ratbox ssld. You know you aren't supposed to run me directly?\n");
1065 fprintf(stderr
, "You get an Id tag for this: $Id: ssld.c 25594 2008-06-25 14:58:47Z androsyn $\n");
1066 fprintf(stderr
, "Have a nice life\n");
1070 ctlfd
= atoi(s_ctlfd
);
1071 pipefd
= atoi(s_pipe
);
1073 for (x
= 0; x
< maxfd
; x
++)
1075 if(x
!= ctlfd
&& x
!= pipefd
&& x
> 2)
1080 x
= open("/dev/null", O_RDWR
);
1083 if(ctlfd
!= 0 && pipefd
!= 0)
1085 if(ctlfd
!= 1 && pipefd
!= 1)
1087 if(ctlfd
!= 2 && pipefd
!= 2)
1094 rb_lib_init(NULL
, NULL
, NULL
, 0, maxfd
, 1024, 4096);
1095 rb_init_rawbuffers(1024);
1096 ssl_ok
= rb_supports_ssl();
1098 mod_ctl
= rb_malloc(sizeof(mod_ctl_t
));
1099 mod_ctl
->F
= rb_open(ctlfd
, RB_FD_SOCKET
, "ircd control socket");
1100 mod_ctl
->F_pipe
= rb_open(pipefd
, RB_FD_PIPE
, "ircd pipe");
1101 rb_set_nb(mod_ctl
->F
);
1102 rb_set_nb(mod_ctl
->F_pipe
);
1103 rb_event_addish("clean_dead_conns", clean_dead_conns
, NULL
, 10);
1104 rb_event_add("check_handshake_flood", check_handshake_flood
, NULL
, 10);
1105 read_pipe_ctl(mod_ctl
->F_pipe
, NULL
);
1106 mod_read_ctl(mod_ctl
->F
, mod_ctl
);
1107 if(!zlib_ok
&& !ssl_ok
)
1109 /* this is really useless... */
1110 send_i_am_useless(mod_ctl
);
1111 /* sleep until the ircd kills us */
1117 send_nozlib_support(mod_ctl
, NULL
);
1119 send_nossl_support(mod_ctl
, NULL
);
1127 dummy_handler(int sig
)
1135 struct sigaction act
;
1138 act
.sa_handler
= SIG_IGN
;
1139 sigemptyset(&act
.sa_mask
);
1140 sigaddset(&act
.sa_mask
, SIGPIPE
);
1141 sigaddset(&act
.sa_mask
, SIGALRM
);
1143 sigaddset(&act
.sa_mask
, SIGTRAP
);
1147 sigaddset(&act
.sa_mask
, SIGWINCH
);
1148 sigaction(SIGWINCH
, &act
, 0);
1150 sigaction(SIGPIPE
, &act
, 0);
1152 sigaction(SIGTRAP
, &act
, 0);
1155 act
.sa_handler
= dummy_handler
;
1156 sigaction(SIGALRM
, &act
, 0);