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
38 static void ssl_read_ctl(rb_fde_t
* F
, void *data
);
39 static int ssld_count
;
41 static char tmpbuf
[READBUF_SIZE
];
42 static char nul
= '\0';
46 typedef struct _ssl_ctl_buf
51 rb_fde_t
*F
[MAXPASSFD
];
70 static void ssld_update_config_one(ssl_ctl_t
*ctl
);
71 static void send_new_ssl_certs_one(ssl_ctl_t
* ctl
);
72 static void send_certfp_method(ssl_ctl_t
*ctl
);
75 static rb_dlink_list ssl_daemons
;
77 static inline uint32_t
78 buf_to_uint32(char *buf
)
81 memcpy(&x
, buf
, sizeof(x
));
86 uint32_to_buf(char *buf
, uint32_t x
)
88 memcpy(buf
, &x
, sizeof(x
));
93 allocate_ssl_daemon(rb_fde_t
* F
, rb_fde_t
* P
, int pid
)
97 if(F
== NULL
|| pid
< 0)
99 ctl
= rb_malloc(sizeof(ssl_ctl_t
));
104 rb_dlinkAdd(ctl
, &ctl
->node
, &ssl_daemons
);
109 free_ssl_daemon(ssl_ctl_t
* ctl
)
112 ssl_ctl_buf_t
*ctl_buf
;
117 RB_DLINK_FOREACH(ptr
, ctl
->readq
.head
)
120 for(x
= 0; x
< ctl_buf
->nfds
; x
++)
121 rb_close(ctl_buf
->F
[x
]);
123 rb_free(ctl_buf
->buf
);
127 RB_DLINK_FOREACH(ptr
, ctl
->writeq
.head
)
130 for(x
= 0; x
< ctl_buf
->nfds
; x
++)
131 rb_close(ctl_buf
->F
[x
]);
133 rb_free(ctl_buf
->buf
);
138 rb_dlinkDelete(&ctl
->node
, &ssl_daemons
);
142 static char *ssld_path
;
144 static int ssld_spin_count
= 0;
145 static time_t last_spin
;
146 static int ssld_wait
= 0;
152 rb_dlink_node
*ptr
, *next
;
155 RB_DLINK_FOREACH_SAFE(ptr
, next
, ssl_daemons
.head
)
166 rb_kill(ctl
->pid
, SIGKILL
);
167 free_ssl_daemon(ctl
);
174 start_ssldaemon(ServerInfo
.ssld_count
);
180 rb_dlink_node
*ptr
, *next
;
182 RB_DLINK_FOREACH_SAFE(ptr
, next
, ssl_daemons
.head
)
190 rb_kill(ctl
->pid
, SIGKILL
);
192 free_ssl_daemon(ctl
);
197 ssl_dead(ssl_ctl_t
* ctl
)
203 rb_kill(ctl
->pid
, SIGKILL
); /* make sure the process is really gone */
208 ilog(L_MAIN
, "ssld helper died - attempting to restart");
209 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "ssld helper died - attempting to restart");
215 ssl_do_pipe(rb_fde_t
* F
, void *data
)
218 ssl_ctl_t
*ctl
= data
;
219 retlen
= rb_write(F
, "0", 1);
220 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
225 rb_setselect(F
, RB_SELECT_READ
, ssl_do_pipe
, data
);
229 restart_ssld_event(void *unused
)
234 if(ServerInfo
.ssld_count
> get_ssld_count())
236 int start
= ServerInfo
.ssld_count
- get_ssld_count();
237 ilog(L_MAIN
, "Attempting to restart ssld processes");
238 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "Attempt to restart ssld processes");
239 start_ssldaemon(start
);
244 start_ssldaemon(int count
)
249 const char *suffix
= ".exe";
251 const char *suffix
= "";
254 char fullpath
[PATH_MAX
+ 1];
265 if(ssld_spin_count
> 20 && (rb_current_time() - last_spin
< 5))
267 ilog(L_MAIN
, "ssld helper is spinning - will attempt to restart in 1 minute");
268 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
269 "ssld helper is spinning - will attempt to restart in 1 minute");
270 rb_event_add("restart_ssld_event", restart_ssld_event
, NULL
, 60);
276 last_spin
= rb_current_time();
278 if(ssld_path
== NULL
)
280 snprintf(fullpath
, sizeof(fullpath
), "%s%cssld%s", ircd_paths
[IRCD_PATH_LIBEXEC
], RB_PATH_SEPARATOR
, suffix
);
282 if(access(fullpath
, X_OK
) == -1)
284 snprintf(fullpath
, sizeof(fullpath
), "%s%cbin%cssld%s",
285 ConfigFileEntry
.dpath
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
, suffix
);
286 if(access(fullpath
, X_OK
) == -1)
289 "Unable to execute ssld%s in %s or %s/bin",
290 suffix
, ircd_paths
[IRCD_PATH_LIBEXEC
], ConfigFileEntry
.dpath
);
294 ssld_path
= rb_strdup(fullpath
);
296 rb_strlcpy(buf
, "-ircd ssld daemon", sizeof(buf
));
300 for(i
= 0; i
< count
; i
++)
303 if(rb_socketpair(AF_UNIX
, SOCK_DGRAM
, 0, &F1
, &F2
, "SSL/TLS handle passing socket") == -1)
305 ilog(L_MAIN
, "Unable to create ssld - rb_socketpair failed: %s", strerror(errno
));
309 rb_set_buffers(F1
, READBUF_SIZE
);
310 rb_set_buffers(F2
, READBUF_SIZE
);
311 snprintf(fdarg
, sizeof(fdarg
), "%d", rb_get_fd(F2
));
312 rb_setenv("CTL_FD", fdarg
, 1);
313 if(rb_pipe(&P1
, &P2
, "SSL/TLS pipe") == -1)
315 ilog(L_MAIN
, "Unable to create ssld - rb_pipe failed: %s", strerror(errno
));
318 snprintf(fdarg
, sizeof(fdarg
), "%d", rb_get_fd(P1
));
319 rb_setenv("CTL_PIPE", fdarg
, 1);
320 snprintf(s_pid
, sizeof(s_pid
), "%d", (int)getpid());
321 rb_setenv("CTL_PPID", s_pid
, 1);
323 rb_clear_cloexec(F2
);
324 rb_clear_cloexec(P1
);
326 pid
= rb_spawn_process(ssld_path
, (const char **) parv
);
329 ilog(L_MAIN
, "Unable to create ssld: %s\n", strerror(errno
));
339 ctl
= allocate_ssl_daemon(F1
, P2
, pid
);
341 ssld_update_config_one(ctl
);
342 ssl_read_ctl(ctl
->F
, ctl
);
343 ssl_do_pipe(P2
, ctl
);
350 ssl_process_open_fd(ssl_ctl_t
* ctl
, ssl_ctl_buf_t
* ctl_buf
)
352 struct Client
*client_p
;
355 if(ctl_buf
->buflen
< 5)
356 return; /* bogus message..drop it.. XXX should warn here */
358 fd
= buf_to_uint32(&ctl_buf
->buf
[1]);
359 client_p
= find_cli_connid_hash(fd
);
360 if(client_p
== NULL
|| client_p
->localClient
== NULL
)
363 if(client_p
->localClient
->ssl_callback
)
365 SSL_OPEN_CB
*hdl
= client_p
->localClient
->ssl_callback
;
367 client_p
->localClient
->ssl_callback
= NULL
;
369 hdl(client_p
, RB_OK
);
374 ssl_process_dead_fd(ssl_ctl_t
* ctl
, ssl_ctl_buf_t
* ctl_buf
)
376 struct Client
*client_p
;
380 if(ctl_buf
->buflen
< 6)
381 return; /* bogus message..drop it.. XXX should warn here */
383 fd
= buf_to_uint32(&ctl_buf
->buf
[1]);
384 rb_strlcpy(reason
, &ctl_buf
->buf
[5], sizeof(reason
));
385 client_p
= find_cli_connid_hash(fd
);
386 if(client_p
== NULL
|| client_p
->localClient
== NULL
)
389 if(IsAnyServer(client_p
))
391 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "ssld error for %s: %s", client_p
->name
, reason
);
392 ilog(L_SERVER
, "ssld error for %s: %s", log_client_name(client_p
, SHOW_IP
), reason
);
395 /* if there is still a pending callback, call it now */
396 if(client_p
->localClient
->ssl_callback
)
398 SSL_OPEN_CB
*hdl
= client_p
->localClient
->ssl_callback
;
400 client_p
->localClient
->ssl_callback
= NULL
;
402 if (hdl(client_p
, RB_ERROR_SSL
))
404 /* the callback has exited the client */
409 if(IsAnyServer(client_p
) || IsRegistered(client_p
))
411 /* read any last moment ERROR, QUIT or the like -- jilles */
412 if (!strcmp(reason
, "Remote host closed the connection"))
413 read_packet(client_p
->localClient
->F
, client_p
);
414 if (IsAnyDead(client_p
))
417 exit_client(client_p
, client_p
, &me
, reason
);
422 ssl_process_cipher_string(ssl_ctl_t
*ctl
, ssl_ctl_buf_t
*ctl_buf
)
424 struct Client
*client_p
;
428 if(ctl_buf
->buflen
< 6)
429 return; /* bogus message..drop it.. XXX should warn here */
431 fd
= buf_to_uint32(&ctl_buf
->buf
[1]);
432 cstring
= (const char *)&ctl_buf
->buf
[5];
434 if(EmptyString(cstring
))
437 client_p
= find_cli_connid_hash(fd
);
438 if(client_p
!= NULL
&& client_p
->localClient
!= NULL
)
440 rb_free(client_p
->localClient
->cipher_string
);
441 client_p
->localClient
->cipher_string
= rb_strdup(cstring
);
447 ssl_process_certfp(ssl_ctl_t
* ctl
, ssl_ctl_buf_t
* ctl_buf
)
449 struct Client
*client_p
;
451 uint32_t certfp_method
;
455 const char *method_string
;
458 if(ctl_buf
->buflen
> 13 + RB_SSL_CERTFP_LEN
)
459 return; /* bogus message..drop it.. XXX should warn here */
461 fd
= buf_to_uint32(&ctl_buf
->buf
[1]);
462 certfp_method
= buf_to_uint32(&ctl_buf
->buf
[5]);
463 len
= buf_to_uint32(&ctl_buf
->buf
[9]);
464 certfp
= (uint8_t *)&ctl_buf
->buf
[13];
465 client_p
= find_cli_connid_hash(fd
);
469 switch (certfp_method
) {
470 case RB_SSL_CERTFP_METH_CERT_SHA1
:
471 method_string
= CERTFP_PREFIX_CERT_SHA1
;
473 case RB_SSL_CERTFP_METH_CERT_SHA256
:
474 method_string
= CERTFP_PREFIX_CERT_SHA256
;
476 case RB_SSL_CERTFP_METH_CERT_SHA512
:
477 method_string
= CERTFP_PREFIX_CERT_SHA512
;
479 case RB_SSL_CERTFP_METH_SPKI_SHA256
:
480 method_string
= CERTFP_PREFIX_SPKI_SHA256
;
482 case RB_SSL_CERTFP_METH_SPKI_SHA512
:
483 method_string
= CERTFP_PREFIX_SPKI_SHA512
;
488 method_len
= strlen(method_string
);
490 rb_free(client_p
->certfp
);
491 certfp_string
= rb_malloc(method_len
+ len
* 2 + 1);
492 rb_strlcpy(certfp_string
, method_string
, method_len
+ len
* 2 + 1);
493 for(uint32_t i
= 0; i
< len
; i
++)
494 snprintf(certfp_string
+ method_len
+ 2 * i
, 3, "%02x",
496 client_p
->certfp
= certfp_string
;
500 ssl_process_cmd_recv(ssl_ctl_t
* ctl
)
502 static const char *cannot_setup_ssl
= "ssld cannot setup ssl, check your certificates and private key";
503 static const char *no_ssl_or_zlib
= "ssld has neither SSL/TLS or zlib support killing all sslds";
504 rb_dlink_node
*ptr
, *next
;
505 ssl_ctl_buf_t
*ctl_buf
;
511 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->readq
.head
)
514 switch (*ctl_buf
->buf
)
517 ircd_ssl_ok
= false; /* ssld says it can't do ssl/tls */
520 ssl_process_open_fd(ctl
, ctl_buf
);
523 ssl_process_dead_fd(ctl
, ctl_buf
);
526 ssl_process_cipher_string(ctl
, ctl_buf
);
529 ssl_process_certfp(ctl
, ctl_buf
);
533 ilog(L_MAIN
, "%s", cannot_setup_ssl
);
534 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "%s", cannot_setup_ssl
);
539 ilog(L_MAIN
, "%s", no_ssl_or_zlib
);
540 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "%s", no_ssl_or_zlib
);
544 len
= ctl_buf
->buflen
- 1;
545 if (len
> sizeof(ctl
->version
) - 1)
546 len
= sizeof(ctl
->version
) - 1;
547 strncpy(ctl
->version
, &ctl_buf
->buf
[1], len
);
552 ilog(L_MAIN
, "Received invalid command from ssld: %s", ctl_buf
->buf
);
553 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "Received invalid command from ssld");
556 rb_dlinkDelete(ptr
, &ctl
->readq
);
557 rb_free(ctl_buf
->buf
);
565 ssl_read_ctl(rb_fde_t
* F
, void *data
)
567 ssl_ctl_buf_t
*ctl_buf
;
568 ssl_ctl_t
*ctl
= data
;
575 ctl_buf
= rb_malloc(sizeof(ssl_ctl_buf_t
));
576 ctl_buf
->buf
= rb_malloc(READSIZE
);
577 retlen
= rb_recv_fd_buf(ctl
->F
, ctl_buf
->buf
, READSIZE
, ctl_buf
->F
, 4);
578 ctl_buf
->buflen
= retlen
;
581 rb_free(ctl_buf
->buf
);
585 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->readq
);
589 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
594 ssl_process_cmd_recv(ctl
);
595 rb_setselect(ctl
->F
, RB_SELECT_READ
, ssl_read_ctl
, ctl
);
601 ssl_ctl_t
*ctl
, *lowest
= NULL
;
604 RB_DLINK_FOREACH(ptr
, ssl_daemons
.head
)
616 if(ctl
->cli_count
< lowest
->cli_count
)
623 ssl_write_ctl(rb_fde_t
* F
, void *data
)
625 ssl_ctl_t
*ctl
= data
;
626 ssl_ctl_buf_t
*ctl_buf
;
627 rb_dlink_node
*ptr
, *next
;
633 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->writeq
.head
)
636 /* in theory unix sock_dgram shouldn't ever short write this.. */
637 retlen
= rb_send_fd_buf(ctl
->F
, ctl_buf
->F
, ctl_buf
->nfds
, ctl_buf
->buf
, ctl_buf
->buflen
, ctl
->pid
);
640 rb_dlinkDelete(ptr
, &ctl
->writeq
);
641 for(x
= 0; x
< ctl_buf
->nfds
; x
++)
642 rb_close(ctl_buf
->F
[x
]);
643 rb_free(ctl_buf
->buf
);
647 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
654 rb_setselect(ctl
->F
, RB_SELECT_WRITE
, ssl_write_ctl
, ctl
);
660 ssl_cmd_write_queue(ssl_ctl_t
* ctl
, rb_fde_t
** F
, int count
, const void *buf
, size_t buflen
)
662 ssl_ctl_buf_t
*ctl_buf
;
669 ctl_buf
= rb_malloc(sizeof(ssl_ctl_buf_t
));
670 ctl_buf
->buf
= rb_malloc(buflen
);
671 memcpy(ctl_buf
->buf
, buf
, buflen
);
672 ctl_buf
->buflen
= buflen
;
674 for(x
= 0; x
< count
&& x
< MAXPASSFD
; x
++)
676 ctl_buf
->F
[x
] = F
[x
];
678 ctl_buf
->nfds
= count
;
679 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->writeq
);
680 ssl_write_ctl(ctl
->F
, ctl
);
685 send_new_ssl_certs_one(ssl_ctl_t
* ctl
)
689 if(ServerInfo
.ssl_cert
)
690 len
+= strlen(ServerInfo
.ssl_cert
);
694 if(ServerInfo
.ssl_private_key
)
695 len
+= strlen(ServerInfo
.ssl_private_key
);
697 if(ServerInfo
.ssl_dh_params
)
698 len
+= strlen(ServerInfo
.ssl_dh_params
);
700 if(ServerInfo
.ssl_cipher_list
)
701 len
+= strlen(ServerInfo
.ssl_cipher_list
);
703 if(len
> sizeof(tmpbuf
))
705 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
706 "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
707 len
, sizeof(tmpbuf
));
709 "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
710 len
, sizeof(tmpbuf
));
714 int ret
= snprintf(tmpbuf
, sizeof(tmpbuf
), "K%c%s%c%s%c%s%c%s%c", nul
,
715 ServerInfo
.ssl_cert
, nul
,
716 ServerInfo
.ssl_private_key
!= NULL
? ServerInfo
.ssl_private_key
: "", nul
,
717 ServerInfo
.ssl_dh_params
!= NULL
? ServerInfo
.ssl_dh_params
: "", nul
,
718 ServerInfo
.ssl_cipher_list
!= NULL
? ServerInfo
.ssl_cipher_list
: "", nul
);
721 ssl_cmd_write_queue(ctl
, NULL
, 0, tmpbuf
, (size_t) ret
);
725 send_certfp_method(ssl_ctl_t
*ctl
)
730 uint32_to_buf(&buf
[1], ConfigFileEntry
.certfp_method
);
731 ssl_cmd_write_queue(ctl
, NULL
, 0, buf
, sizeof(buf
));
735 ssld_update_config_one(ssl_ctl_t
*ctl
)
737 send_certfp_method(ctl
);
738 send_new_ssl_certs_one(ctl
);
742 ssld_update_config(void)
746 RB_DLINK_FOREACH(ptr
, ssl_daemons
.head
)
748 ssl_ctl_t
*ctl
= ptr
->data
;
750 if (ctl
->dead
|| ctl
->shutdown
)
753 ssld_update_config_one(ctl
);
758 start_ssld_accept(rb_fde_t
* sslF
, rb_fde_t
* plainF
, uint32_t id
)
767 uint32_to_buf(&buf
[1], id
);
772 ssl_cmd_write_queue(ctl
, F
, 2, buf
, sizeof(buf
));
777 start_ssld_connect(rb_fde_t
* sslF
, rb_fde_t
* plainF
, uint32_t id
)
786 uint32_to_buf(&buf
[1], id
);
792 ssl_cmd_write_queue(ctl
, F
, 2, buf
, sizeof(buf
));
797 ssld_decrement_clicount(ssl_ctl_t
* ctl
)
803 if(ctl
->shutdown
&& !ctl
->cli_count
)
806 rb_kill(ctl
->pid
, SIGKILL
);
808 if(ctl
->dead
&& !ctl
->cli_count
)
810 free_ssl_daemon(ctl
);
815 cleanup_dead_ssl(void *unused
)
817 rb_dlink_node
*ptr
, *next
;
819 RB_DLINK_FOREACH_SAFE(ptr
, next
, ssl_daemons
.head
)
822 if(ctl
->dead
&& !ctl
->cli_count
)
824 free_ssl_daemon(ctl
);
836 ssld_foreach_info(void (*func
)(void *data
, pid_t pid
, int cli_count
, enum ssld_status status
, const char *version
), void *data
)
838 rb_dlink_node
*ptr
, *next
;
840 RB_DLINK_FOREACH_SAFE(ptr
, next
, ssl_daemons
.head
)
843 func(data
, ctl
->pid
, ctl
->cli_count
,
844 ctl
->dead
? SSLD_DEAD
:
845 (ctl
->shutdown
? SSLD_SHUTDOWN
: SSLD_ACTIVE
),
853 rb_event_addish("cleanup_dead_ssld", cleanup_dead_ssl
, NULL
, 60);