2 * sslproc.c: An interface to ssld
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: sslproc.c 25677 2008-07-06 04:21:42Z androsyn $
24 #include <ratbox_lib.h>
37 #define ZIPSTATS_TIME 60
39 static void collect_zipstats(void *unused
);
40 static void ssl_read_ctl(rb_fde_t
*F
, void *data
);
41 static int ssld_count
;
43 static char tmpbuf
[READBUF_SIZE
];
44 static char nul
= '\0';
48 typedef struct _ssl_ctl_buf
53 rb_fde_t
*F
[MAXPASSFD
];
70 static void send_new_ssl_certs_one(ssl_ctl_t
*ctl
, const char *ssl_cert
, const char *ssl_private_key
, const char *ssl_dh_params
);
71 static void send_init_prng(ssl_ctl_t
*ctl
, prng_seed_t seedtype
, const char *path
);
74 static rb_dlink_list ssl_daemons
;
76 static inline int32_t buf_to_int32(char *buf
)
79 memcpy(&x
, buf
, sizeof(x
));
83 static inline void int32_to_buf(char *buf
, int32_t x
)
85 memcpy(buf
, &x
, sizeof(x
));
90 static inline uint16_t buf_to_uint16(char *buf
)
93 memcpy(&x
, buf
, sizeof(x
));
97 static inline void uint16_to_buf(char *buf
, uint16_t x
)
99 memcpy(buf
, &x
, sizeof(x
));
105 allocate_ssl_daemon(rb_fde_t
*F
, rb_fde_t
*P
, int pid
)
109 if(F
== NULL
|| pid
< 0)
111 ctl
= rb_malloc(sizeof(ssl_ctl_t
));
116 rb_dlinkAdd(ctl
, &ctl
->node
, &ssl_daemons
);
121 free_ssl_daemon(ssl_ctl_t
*ctl
)
124 ssl_ctl_buf_t
*ctl_buf
;
129 RB_DLINK_FOREACH(ptr
, ctl
->readq
.head
)
132 for(x
= 0; x
< ctl_buf
->nfds
; x
++)
133 rb_close(ctl_buf
->F
[x
]);
135 rb_free(ctl_buf
->buf
);
139 RB_DLINK_FOREACH(ptr
, ctl
->writeq
.head
)
142 for(x
= 0; x
< ctl_buf
->nfds
; x
++)
143 rb_close(ctl_buf
->F
[x
]);
145 rb_free(ctl_buf
->buf
);
150 rb_dlinkDelete(&ctl
->node
, &ssl_daemons
);
154 static char *ssld_path
;
156 static int ssld_spin_count
= 0;
157 static time_t last_spin
;
158 static int ssld_wait
= 0;
164 rb_dlink_node
*ptr
, *next
;
166 RB_DLINK_FOREACH_SAFE(ptr
, next
, ssl_daemons
.head
)
173 kill(ctl
->pid
, SIGKILL
);
178 ssl_dead(ssl_ctl_t
*ctl
)
185 kill(ctl
->pid
, SIGKILL
); /* make sure the process is really gone */
186 ilog(L_MAIN
, "ssld helper died - attempting to restart");
187 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "ssld helper died - attempting to restart");
188 start_ssldaemon(1, ServerInfo
.ssl_cert
, ServerInfo
.ssl_private_key
, ServerInfo
.ssl_dh_params
);
192 ssl_do_pipe(rb_fde_t
*F
, void *data
)
195 ssl_ctl_t
*ctl
= data
;
196 retlen
= rb_write(F
, "0", 1);
197 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
202 rb_setselect(F
, RB_SELECT_READ
, ssl_do_pipe
, data
);
206 restart_ssld_event(void *unused
)
211 if(ServerInfo
.ssld_count
> get_ssld_count())
213 int start
= ServerInfo
.ssld_count
- get_ssld_count();
214 ilog(L_MAIN
, "Attempting to restart ssld processes");
215 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Attempt to restart ssld processes");
216 start_ssldaemon(start
, ServerInfo
.ssl_cert
, ServerInfo
.ssl_private_key
, ServerInfo
.ssl_dh_params
);
221 start_ssldaemon(int count
, const char *ssl_cert
, const char *ssl_private_key
, const char *ssl_dh_params
)
225 char fullpath
[PATH_MAX
+ 1];
235 if(ssld_spin_count
> 20 && (rb_current_time() - last_spin
< 5))
237 ilog(L_MAIN
, "ssld helper is spinning - will attempt to restart in 5 minutes");
238 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "ssld helper is spinning - will attempt to restart in 1 minute");
239 rb_event_add("restart_ssld_event", restart_ssld_event
, NULL
, 60);
245 last_spin
= rb_current_time();
246 if(ssld_path
== NULL
)
248 rb_snprintf(fullpath
, sizeof(fullpath
), "%s/ssld", BINPATH
);
250 if(access(fullpath
, X_OK
) == -1)
252 rb_snprintf(fullpath
, sizeof(fullpath
), "%s/bin/ssld", ConfigFileEntry
.dpath
);
253 if(access(fullpath
, X_OK
) == -1)
255 ilog(L_MAIN
, "Unable to execute ssld in %s/bin or %s", ConfigFileEntry
.dpath
, BINPATH
);
259 ssld_path
= rb_strdup(fullpath
);
262 rb_strlcpy(buf
, "-ircd ssld daemon helper", sizeof(buf
));
266 for(i
= 0; i
< count
; i
++)
269 rb_socketpair(AF_UNIX
, SOCK_DGRAM
, 0, &F1
, &F2
, "SSL/TLS handle passing socket");
270 rb_set_buffers(F1
, READBUF_SIZE
);
271 rb_set_buffers(F2
, READBUF_SIZE
);
272 rb_snprintf(fdarg
, sizeof(fdarg
), "%d", rb_get_fd(F2
));
273 setenv("CTL_FD", fdarg
, 1);
274 rb_pipe(&P1
, &P2
, "SSL/TLS pipe");
275 rb_snprintf(fdarg
, sizeof(fdarg
), "%d", rb_get_fd(P1
));
276 setenv("CTL_PIPE", fdarg
, 1);
278 pid
= rb_spawn_process(ssld_path
, (const char **)parv
);
281 ilog(L_MAIN
, "Unable to create ssld: %s\n", strerror(errno
));
291 ctl
= allocate_ssl_daemon(F1
, P2
, pid
);
294 if(ConfigFileEntry
.use_egd
&& (ConfigFileEntry
.egdpool_path
!= NULL
))
295 send_init_prng(ctl
, RB_PRNG_EGD
, ConfigFileEntry
.egdpool_path
);
297 send_init_prng(ctl
, RB_PRNG_DEFAULT
, NULL
);
299 if(ssl_ok
&& ssl_cert
!= NULL
&& ssl_private_key
!= NULL
)
300 send_new_ssl_certs_one(ctl
, ssl_cert
, ssl_private_key
, ssl_dh_params
!= NULL
? ssl_dh_params
: "");
301 ssl_read_ctl(ctl
->F
, ctl
);
302 ssl_do_pipe(P2
, ctl
);
308 ssl_process_zipstats(ssl_ctl_t
*ctl
, ssl_ctl_buf_t
*ctl_buf
)
310 struct Client
*server
;
311 struct ZipStats
*zips
;
314 parc
= rb_string_to_array(ctl_buf
->buf
, parv
, 6);
315 server
= find_server(NULL
, parv
[1]);
316 if(server
== NULL
|| server
->localClient
== NULL
|| !IsCapable(server
, CAP_ZIP
))
318 if(server
->localClient
->zipstats
== NULL
)
319 server
->localClient
->zipstats
= rb_malloc(sizeof(struct ZipStats
));
321 zips
= server
->localClient
->zipstats
;
323 zips
->in
+= strtoull(parv
[2], NULL
, 10);
324 zips
->in_wire
+= strtoull(parv
[3], NULL
, 10);
325 zips
->out
+= strtoull(parv
[4], NULL
, 10);
326 zips
->out_wire
+= strtoull(parv
[5], NULL
, 10);
329 zips
->in_ratio
= ((double)(zips
->in
- zips
->in_wire
) / (double) zips
->in
) * 100.00;
334 zips
->out_ratio
= ((double)(zips
->out
- zips
->out_wire
) / (double) zips
->out
) * 100.00;
340 ssl_process_dead_fd(ssl_ctl_t
*ctl
, ssl_ctl_buf_t
*ctl_buf
)
342 struct Client
*client_p
;
346 if(ctl_buf
->buflen
< 6)
347 return; /* bogus message..drop it.. XXX should warn here */
349 fd
= buf_to_int32(&ctl_buf
->buf
[1]);
350 rb_strlcpy(reason
, &ctl_buf
->buf
[5], sizeof(reason
));
351 client_p
= find_cli_fd_hash(fd
);
354 if(IsAnyServer(client_p
))
356 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "ssld error for %s: %s", client_p
->name
, reason
);
357 ilog(L_SERVER
, "ssld error for %s: %s", log_client_name(client_p
, SHOW_IP
), reason
);
359 exit_client(client_p
, client_p
, &me
, reason
);
364 ssl_process_zip_ready(ssl_ctl_t
*ctl
, ssl_ctl_buf_t
*ctl_buf
)
366 struct Client
*client_p
;
369 if(ctl_buf
->buflen
< 5)
370 return; /* bogus message..drop it.. XXX should warn here */
372 fd
= buf_to_int32(&ctl_buf
->buf
[1]);
373 client_p
= find_cli_fd_hash(fd
);
377 /* Now start sending the data that should be compressed. */
378 // ClearCork(client_p);
379 send_pop_queue(client_p
);
380 /* Start reading uncompressed data. */
381 read_packet(client_p
->localClient
->F
, client_p
);
386 ssl_process_cmd_recv(ssl_ctl_t
*ctl
)
388 static const char *cannot_setup_ssl
= "ssld cannot setup ssl, check your certificates and private key";
389 static const char *no_ssl_or_zlib
= "ssld has neither SSL/TLS or zlib support killing all sslds";
390 rb_dlink_node
*ptr
, *next
;
391 ssl_ctl_buf_t
*ctl_buf
;
394 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->readq
.head
)
397 switch(*ctl_buf
->buf
)
400 ssl_ok
= 0; /* ssld says it can't do ssl/tls */
403 ssl_process_dead_fd(ctl
, ctl_buf
);
406 ssl_process_zipstats(ctl
, ctl_buf
);
410 ilog(L_MAIN
, cannot_setup_ssl
);
411 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, cannot_setup_ssl
);
415 ilog(L_MAIN
, no_ssl_or_zlib
);
416 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, no_ssl_or_zlib
);
420 ssl_process_zip_ready(ctl
, ctl_buf
);
426 ilog(L_MAIN
, "Received invalid command from ssld: %s", ctl_buf
->buf
);
427 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Received invalid command from ssld");
430 rb_dlinkDelete(ptr
, &ctl
->readq
);
431 rb_free(ctl_buf
->buf
);
439 ssl_read_ctl(rb_fde_t
*F
, void *data
)
441 ssl_ctl_buf_t
*ctl_buf
;
442 ssl_ctl_t
*ctl
= data
;
449 ctl_buf
= rb_malloc(sizeof(ssl_ctl_buf_t
));
450 ctl_buf
->buf
= rb_malloc(READSIZE
);
451 retlen
= rb_recv_fd_buf(ctl
->F
, ctl_buf
->buf
, READSIZE
, ctl_buf
->F
, 4);
452 ctl_buf
->buflen
= retlen
;
454 rb_free(ctl_buf
->buf
);
458 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->readq
);
461 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
466 ssl_process_cmd_recv(ctl
);
467 rb_setselect(ctl
->F
, RB_SELECT_READ
, ssl_read_ctl
, ctl
);
473 ssl_ctl_t
*ctl
, *lowest
= NULL
;
476 RB_DLINK_FOREACH(ptr
, ssl_daemons
.head
)
485 if(ctl
->cli_count
< lowest
->cli_count
)
492 ssl_write_ctl(rb_fde_t
*F
, void *data
)
494 ssl_ctl_t
*ctl
= data
;
495 ssl_ctl_buf_t
*ctl_buf
;
496 rb_dlink_node
*ptr
, *next
;
502 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->writeq
.head
)
505 /* in theory unix sock_dgram shouldn't ever short write this.. */
506 retlen
= rb_send_fd_buf(ctl
->F
, ctl_buf
->F
, ctl_buf
->nfds
, ctl_buf
->buf
, ctl_buf
->buflen
);
509 rb_dlinkDelete(ptr
, &ctl
->writeq
);
510 for(x
= 0; x
< ctl_buf
->nfds
; x
++)
511 rb_close(ctl_buf
->F
[x
]);
512 rb_free(ctl_buf
->buf
);
516 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
521 rb_setselect(ctl
->F
, RB_SELECT_WRITE
, ssl_write_ctl
, ctl
);
527 ssl_cmd_write_queue(ssl_ctl_t
*ctl
, rb_fde_t
**F
, int count
, const void *buf
, size_t buflen
)
529 ssl_ctl_buf_t
*ctl_buf
;
536 ctl_buf
= rb_malloc(sizeof(ssl_ctl_buf_t
));
537 ctl_buf
->buf
= rb_malloc(buflen
);
538 memcpy(ctl_buf
->buf
, buf
, buflen
);
539 ctl_buf
->buflen
= buflen
;
541 for(x
= 0; x
< count
&& x
< MAXPASSFD
; x
++)
543 ctl_buf
->F
[x
] = F
[x
];
545 ctl_buf
->nfds
= count
;
546 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->writeq
);
547 ssl_write_ctl(ctl
->F
, ctl
);
552 send_new_ssl_certs_one(ssl_ctl_t
*ctl
, const char *ssl_cert
, const char *ssl_private_key
, const char *ssl_dh_params
)
556 len
= strlen(ssl_cert
) + strlen(ssl_private_key
) + strlen(ssl_dh_params
) + 5;
557 if(len
> sizeof(tmpbuf
))
559 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
560 "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
561 len
, sizeof(tmpbuf
));
562 ilog(L_MAIN
, "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
563 len
, sizeof(tmpbuf
));
566 len
= rb_snprintf(tmpbuf
, sizeof(tmpbuf
), "K%c%s%c%s%c%s%c", nul
, ssl_cert
, nul
, ssl_private_key
, nul
, ssl_dh_params
, nul
);
567 ssl_cmd_write_queue(ctl
, NULL
, 0, tmpbuf
, len
);
571 send_init_prng(ssl_ctl_t
*ctl
, prng_seed_t seedtype
, const char *path
)
575 uint8_t seed
= (uint8_t) seedtype
;
583 if(len
> sizeof(tmpbuf
))
585 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
586 "Parameters for send_init_prng too long (%zd > %zd) to pass to ssld, not sending...",
587 len
, sizeof(tmpbuf
));
588 ilog(L_MAIN
, "Parameters for send_init_prng too long (%zd > %zd) to pass to ssld, not sending...",
589 len
, sizeof(tmpbuf
));
593 len
= rb_snprintf(tmpbuf
, sizeof(tmpbuf
), "I%c%s%c", seed
, s
, nul
);
594 ssl_cmd_write_queue(ctl
, NULL
, 0, tmpbuf
, len
);
598 send_new_ssl_certs(const char *ssl_cert
, const char *ssl_private_key
, const char *ssl_dh_params
)
601 if(ssl_cert
== NULL
|| ssl_private_key
== NULL
|| ssl_dh_params
== NULL
)
606 RB_DLINK_FOREACH(ptr
, ssl_daemons
.head
)
608 ssl_ctl_t
*ctl
= ptr
->data
;
609 send_new_ssl_certs_one(ctl
, ssl_cert
, ssl_private_key
, ssl_dh_params
);
615 start_ssld_accept(rb_fde_t
*sslF
, rb_fde_t
*plainF
, int32_t id
)
624 int32_to_buf(&buf
[1], id
);
627 ssl_cmd_write_queue(ctl
, F
, 2, buf
, sizeof(buf
));
632 start_ssld_connect(rb_fde_t
*sslF
, rb_fde_t
*plainF
, int32_t id
)
641 int32_to_buf(&buf
[1], id
);
645 ssl_cmd_write_queue(ctl
, F
, 2, buf
, sizeof(buf
));
650 ssld_decrement_clicount(ssl_ctl_t
*ctl
)
656 if(ctl
->dead
&& !ctl
->cli_count
)
658 free_ssl_daemon(ctl
);
663 * what we end up sending to the ssld process for ziplinks is the following
664 * Z[ourfd][level][RECVQ]
665 * Z = ziplinks command = buf[0]
666 * ourfd = Our end of the socketpair = buf[1..4]
667 * level = zip level buf[5]
668 * recvqlen = our recvq len = buf[6-7]
669 * recvq = any data we read prior to starting ziplinks
672 start_zlib_session(void *data
)
674 struct Client
*server
= (struct Client
*)data
;
684 size_t hdr
= (sizeof(uint8_t) * 2) + sizeof(int32_t);
688 server
->localClient
->event
= NULL
;
690 recvqlen
= rb_linebuf_len(&server
->localClient
->buf_recvq
);
692 len
= recvqlen
+ hdr
;
694 if(len
> READBUF_SIZE
)
697 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "ssld - attempted to pass message of %zd len, max len %d, giving up", len
, READBUF_SIZE
);
698 ilog(L_MAIN
, "ssld - attempted to pass message of %zd len, max len %d, giving up", len
, READBUF_SIZE
);
699 exit_client(server
, server
, server
, "ssld readbuf exceeded");
703 buf
= rb_malloc(len
);
704 level
= ConfigFileEntry
.compression_level
;
706 int32_to_buf(&buf
[1], rb_get_fd(server
->localClient
->F
));
707 buf
[5] = (char)level
;
709 recvq_start
= &buf
[6];
710 server
->localClient
->zipstats
= rb_malloc(sizeof(struct ZipStats
));
717 cpylen
= rb_linebuf_get(&server
->localClient
->buf_recvq
, xbuf
, left
, LINEBUF_PARTIAL
, LINEBUF_RAW
);
722 /* Pass the socket to ssld. */
724 rb_socketpair(AF_UNIX
, SOCK_STREAM
, 0, &xF1
, &xF2
, "Initial zlib socketpairs");
726 F
[0] = server
->localClient
->F
;
728 del_from_cli_fd_hash(server
);
729 server
->localClient
->F
= xF2
;
730 /* need to redo as what we did before isn't valid now */
731 int32_to_buf(&buf
[1], rb_get_fd(server
->localClient
->F
));
732 add_to_cli_fd_hash(server
);
733 server
->localClient
->ssl_ctl
= which_ssld();
734 server
->localClient
->ssl_ctl
->cli_count
++;
735 ssl_cmd_write_queue(server
->localClient
->ssl_ctl
, F
, 2, buf
, len
);
740 collect_zipstats(void *unused
)
743 struct Client
*target_p
;
744 char buf
[sizeof(uint8_t) + sizeof(int32_t) + HOSTLEN
];
750 odata
= buf
+ sizeof(uint8_t) + sizeof(int32_t);
752 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
754 target_p
= ptr
->data
;
755 if(IsCapable(target_p
, CAP_ZIP
))
757 len
= sizeof(uint8_t) + sizeof(uint32_t);
759 id
= rb_get_fd(target_p
->localClient
->F
);
760 int32_to_buf(&buf
[1], rb_get_fd(target_p
->localClient
->F
));
761 rb_strlcpy(odata
, target_p
->name
, (sizeof(buf
)-len
));
762 len
+= strlen(odata
) + 1; /* Get the \0 as well */
763 ssl_cmd_write_queue(target_p
->localClient
->ssl_ctl
, NULL
, 0, buf
, len
);
769 cleanup_dead_ssl(void *unused
)
771 rb_dlink_node
*ptr
, *next
;
773 RB_DLINK_FOREACH_SAFE(ptr
, next
, ssl_daemons
.head
)
776 if(ctl
->dead
&& !ctl
->cli_count
)
778 free_ssl_daemon(ctl
);
791 rb_event_addish("collect_zipstats", collect_zipstats
, NULL
, ZIPSTATS_TIME
);
792 rb_event_addish("cleanup_dead_ssld", cleanup_dead_ssl
, NULL
, 1200);