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
)
248 char fullpath
[PATH_MAX
+ 1];
259 if(ssld_spin_count
> 20 && (rb_current_time() - last_spin
< 5))
261 ilog(L_MAIN
, "ssld helper is spinning - will attempt to restart in 1 minute");
262 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
263 "ssld helper is spinning - will attempt to restart in 1 minute");
264 rb_event_add("restart_ssld_event", restart_ssld_event
, NULL
, 60);
270 last_spin
= rb_current_time();
272 if(ssld_path
== NULL
)
274 snprintf(fullpath
, sizeof(fullpath
), "%s/ssld", ircd_paths
[IRCD_PATH_LIBEXEC
]);
276 if(access(fullpath
, X_OK
) == -1)
278 snprintf(fullpath
, sizeof(fullpath
), "%s/bin/ssld", ConfigFileEntry
.dpath
);
279 if(access(fullpath
, X_OK
) == -1)
282 "Unable to execute ssld in %s or %s/bin",
283 ircd_paths
[IRCD_PATH_LIBEXEC
], ConfigFileEntry
.dpath
);
287 ssld_path
= rb_strdup(fullpath
);
289 rb_strlcpy(buf
, "-ircd ssld daemon", sizeof(buf
));
293 for(i
= 0; i
< count
; i
++)
296 if(rb_socketpair(AF_UNIX
, SOCK_DGRAM
, 0, &F1
, &F2
, "SSL/TLS handle passing socket") == -1)
298 ilog(L_MAIN
, "Unable to create ssld - rb_socketpair failed: %s", strerror(errno
));
302 rb_set_buffers(F1
, READBUF_SIZE
);
303 rb_set_buffers(F2
, READBUF_SIZE
);
304 snprintf(fdarg
, sizeof(fdarg
), "%d", rb_get_fd(F2
));
305 rb_setenv("CTL_FD", fdarg
, 1);
306 if(rb_pipe(&P1
, &P2
, "SSL/TLS pipe") == -1)
308 ilog(L_MAIN
, "Unable to create ssld - rb_pipe failed: %s", strerror(errno
));
311 snprintf(fdarg
, sizeof(fdarg
), "%d", rb_get_fd(P1
));
312 rb_setenv("CTL_PIPE", fdarg
, 1);
313 snprintf(s_pid
, sizeof(s_pid
), "%d", (int)getpid());
314 rb_setenv("CTL_PPID", s_pid
, 1);
316 rb_clear_cloexec(F2
);
317 rb_clear_cloexec(P1
);
319 pid
= rb_spawn_process(ssld_path
, (const char **) parv
);
322 ilog(L_MAIN
, "Unable to create ssld: %s\n", strerror(errno
));
332 ctl
= allocate_ssl_daemon(F1
, P2
, pid
);
334 ssld_update_config_one(ctl
);
335 ssl_read_ctl(ctl
->F
, ctl
);
336 ssl_do_pipe(P2
, ctl
);
343 ssl_process_open_fd(ssl_ctl_t
* ctl
, ssl_ctl_buf_t
* ctl_buf
)
345 struct Client
*client_p
;
348 if(ctl_buf
->buflen
< 5)
349 return; /* bogus message..drop it.. XXX should warn here */
351 fd
= buf_to_uint32(&ctl_buf
->buf
[1]);
352 client_p
= find_cli_connid_hash(fd
);
353 if(client_p
== NULL
|| client_p
->localClient
== NULL
)
356 if(client_p
->localClient
->ssl_callback
)
358 SSL_OPEN_CB
*hdl
= client_p
->localClient
->ssl_callback
;
360 client_p
->localClient
->ssl_callback
= NULL
;
362 hdl(client_p
, RB_OK
);
367 ssl_process_dead_fd(ssl_ctl_t
* ctl
, ssl_ctl_buf_t
* ctl_buf
)
369 struct Client
*client_p
;
373 if(ctl_buf
->buflen
< 6)
374 return; /* bogus message..drop it.. XXX should warn here */
376 fd
= buf_to_uint32(&ctl_buf
->buf
[1]);
377 rb_strlcpy(reason
, &ctl_buf
->buf
[5], sizeof(reason
));
378 client_p
= find_cli_connid_hash(fd
);
379 if(client_p
== NULL
|| client_p
->localClient
== NULL
)
382 if(IsAnyServer(client_p
))
384 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "ssld error for %s: %s", client_p
->name
, reason
);
385 ilog(L_SERVER
, "ssld error for %s: %s", log_client_name(client_p
, SHOW_IP
), reason
);
388 /* if there is still a pending callback, call it now */
389 if(client_p
->localClient
->ssl_callback
)
391 SSL_OPEN_CB
*hdl
= client_p
->localClient
->ssl_callback
;
393 client_p
->localClient
->ssl_callback
= NULL
;
395 if (hdl(client_p
, RB_ERROR_SSL
))
397 /* the callback has exited the client */
402 if(IsAnyServer(client_p
) || IsRegistered(client_p
))
404 /* read any last moment ERROR, QUIT or the like -- jilles */
405 if (!strcmp(reason
, "Remote host closed the connection"))
406 read_packet(client_p
->localClient
->F
, client_p
);
407 if (IsAnyDead(client_p
))
410 exit_client(client_p
, client_p
, &me
, reason
);
415 ssl_process_cipher_string(ssl_ctl_t
*ctl
, ssl_ctl_buf_t
*ctl_buf
)
417 struct Client
*client_p
;
421 if(ctl_buf
->buflen
< 6)
422 return; /* bogus message..drop it.. XXX should warn here */
424 fd
= buf_to_uint32(&ctl_buf
->buf
[1]);
425 cstring
= (const char *)&ctl_buf
->buf
[5];
427 if(EmptyString(cstring
))
430 client_p
= find_cli_connid_hash(fd
);
431 if(client_p
!= NULL
&& client_p
->localClient
!= NULL
)
433 rb_free(client_p
->localClient
->cipher_string
);
434 client_p
->localClient
->cipher_string
= rb_strdup(cstring
);
440 ssl_process_certfp(ssl_ctl_t
* ctl
, ssl_ctl_buf_t
* ctl_buf
)
442 struct Client
*client_p
;
444 uint32_t certfp_method
;
448 const char *method_string
;
451 if(ctl_buf
->buflen
> 13 + RB_SSL_CERTFP_LEN
)
452 return; /* bogus message..drop it.. XXX should warn here */
454 fd
= buf_to_uint32(&ctl_buf
->buf
[1]);
455 certfp_method
= buf_to_uint32(&ctl_buf
->buf
[5]);
456 len
= buf_to_uint32(&ctl_buf
->buf
[9]);
457 certfp
= (uint8_t *)&ctl_buf
->buf
[13];
458 client_p
= find_cli_connid_hash(fd
);
462 switch (certfp_method
) {
463 case RB_SSL_CERTFP_METH_CERT_SHA1
:
464 method_string
= CERTFP_PREFIX_CERT_SHA1
;
466 case RB_SSL_CERTFP_METH_CERT_SHA256
:
467 method_string
= CERTFP_PREFIX_CERT_SHA256
;
469 case RB_SSL_CERTFP_METH_CERT_SHA512
:
470 method_string
= CERTFP_PREFIX_CERT_SHA512
;
472 case RB_SSL_CERTFP_METH_SPKI_SHA256
:
473 method_string
= CERTFP_PREFIX_SPKI_SHA256
;
475 case RB_SSL_CERTFP_METH_SPKI_SHA512
:
476 method_string
= CERTFP_PREFIX_SPKI_SHA512
;
481 method_len
= strlen(method_string
);
483 rb_free(client_p
->certfp
);
484 certfp_string
= rb_malloc(method_len
+ len
* 2 + 1);
485 rb_strlcpy(certfp_string
, method_string
, method_len
+ len
* 2 + 1);
486 for(uint32_t i
= 0; i
< len
; i
++)
487 snprintf(certfp_string
+ method_len
+ 2 * i
, 3, "%02x",
489 client_p
->certfp
= certfp_string
;
493 ssl_process_cmd_recv(ssl_ctl_t
* ctl
)
495 static const char *cannot_setup_ssl
= "ssld cannot setup ssl, check your certificates and private key";
496 static const char *no_ssl_or_zlib
= "ssld has neither SSL/TLS or zlib support killing all sslds";
497 rb_dlink_node
*ptr
, *next
;
498 ssl_ctl_buf_t
*ctl_buf
;
504 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->readq
.head
)
507 switch (*ctl_buf
->buf
)
510 ircd_ssl_ok
= false; /* ssld says it can't do ssl/tls */
513 ssl_process_open_fd(ctl
, ctl_buf
);
516 ssl_process_dead_fd(ctl
, ctl_buf
);
519 ssl_process_cipher_string(ctl
, ctl_buf
);
522 ssl_process_certfp(ctl
, ctl_buf
);
526 ilog(L_MAIN
, "%s", cannot_setup_ssl
);
527 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "%s", cannot_setup_ssl
);
532 ilog(L_MAIN
, "%s", no_ssl_or_zlib
);
533 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "%s", no_ssl_or_zlib
);
537 len
= ctl_buf
->buflen
- 1;
538 if (len
> sizeof(ctl
->version
) - 1)
539 len
= sizeof(ctl
->version
) - 1;
540 strncpy(ctl
->version
, &ctl_buf
->buf
[1], len
);
545 ilog(L_MAIN
, "Received invalid command from ssld: %s", ctl_buf
->buf
);
546 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "Received invalid command from ssld");
549 rb_dlinkDelete(ptr
, &ctl
->readq
);
550 rb_free(ctl_buf
->buf
);
558 ssl_read_ctl(rb_fde_t
* F
, void *data
)
560 ssl_ctl_buf_t
*ctl_buf
;
561 ssl_ctl_t
*ctl
= data
;
568 ctl_buf
= rb_malloc(sizeof(ssl_ctl_buf_t
));
569 ctl_buf
->buf
= rb_malloc(READSIZE
);
570 retlen
= rb_recv_fd_buf(ctl
->F
, ctl_buf
->buf
, READSIZE
, ctl_buf
->F
, 4);
571 ctl_buf
->buflen
= retlen
;
574 rb_free(ctl_buf
->buf
);
578 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->readq
);
582 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
587 ssl_process_cmd_recv(ctl
);
588 rb_setselect(ctl
->F
, RB_SELECT_READ
, ssl_read_ctl
, ctl
);
594 ssl_ctl_t
*ctl
, *lowest
= NULL
;
597 RB_DLINK_FOREACH(ptr
, ssl_daemons
.head
)
609 if(ctl
->cli_count
< lowest
->cli_count
)
616 ssl_write_ctl(rb_fde_t
* F
, void *data
)
618 ssl_ctl_t
*ctl
= data
;
619 ssl_ctl_buf_t
*ctl_buf
;
620 rb_dlink_node
*ptr
, *next
;
626 RB_DLINK_FOREACH_SAFE(ptr
, next
, ctl
->writeq
.head
)
629 /* in theory unix sock_dgram shouldn't ever short write this.. */
630 retlen
= rb_send_fd_buf(ctl
->F
, ctl_buf
->F
, ctl_buf
->nfds
, ctl_buf
->buf
, ctl_buf
->buflen
, ctl
->pid
);
633 rb_dlinkDelete(ptr
, &ctl
->writeq
);
634 for(x
= 0; x
< ctl_buf
->nfds
; x
++)
635 rb_close(ctl_buf
->F
[x
]);
636 rb_free(ctl_buf
->buf
);
640 if(retlen
== 0 || (retlen
< 0 && !rb_ignore_errno(errno
)))
647 rb_setselect(ctl
->F
, RB_SELECT_WRITE
, ssl_write_ctl
, ctl
);
653 ssl_cmd_write_queue(ssl_ctl_t
* ctl
, rb_fde_t
** F
, int count
, const void *buf
, size_t buflen
)
655 ssl_ctl_buf_t
*ctl_buf
;
662 ctl_buf
= rb_malloc(sizeof(ssl_ctl_buf_t
));
663 ctl_buf
->buf
= rb_malloc(buflen
);
664 memcpy(ctl_buf
->buf
, buf
, buflen
);
665 ctl_buf
->buflen
= buflen
;
667 for(x
= 0; x
< count
&& x
< MAXPASSFD
; x
++)
669 ctl_buf
->F
[x
] = F
[x
];
671 ctl_buf
->nfds
= count
;
672 rb_dlinkAddTail(ctl_buf
, &ctl_buf
->node
, &ctl
->writeq
);
673 ssl_write_ctl(ctl
->F
, ctl
);
678 send_new_ssl_certs_one(ssl_ctl_t
* ctl
)
682 if(ServerInfo
.ssl_cert
)
683 len
+= strlen(ServerInfo
.ssl_cert
);
687 if(ServerInfo
.ssl_private_key
)
688 len
+= strlen(ServerInfo
.ssl_private_key
);
690 if(ServerInfo
.ssl_dh_params
)
691 len
+= strlen(ServerInfo
.ssl_dh_params
);
693 if(ServerInfo
.ssl_cipher_list
)
694 len
+= strlen(ServerInfo
.ssl_cipher_list
);
696 if(len
> sizeof(tmpbuf
))
698 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
699 "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
700 len
, sizeof(tmpbuf
));
702 "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...",
703 len
, sizeof(tmpbuf
));
707 int ret
= snprintf(tmpbuf
, sizeof(tmpbuf
), "K%c%s%c%s%c%s%c%s%c", nul
,
708 ServerInfo
.ssl_cert
, nul
,
709 ServerInfo
.ssl_private_key
!= NULL
? ServerInfo
.ssl_private_key
: "", nul
,
710 ServerInfo
.ssl_dh_params
!= NULL
? ServerInfo
.ssl_dh_params
: "", nul
,
711 ServerInfo
.ssl_cipher_list
!= NULL
? ServerInfo
.ssl_cipher_list
: "", nul
);
714 ssl_cmd_write_queue(ctl
, NULL
, 0, tmpbuf
, (size_t) ret
);
718 send_certfp_method(ssl_ctl_t
*ctl
)
723 uint32_to_buf(&buf
[1], ConfigFileEntry
.certfp_method
);
724 ssl_cmd_write_queue(ctl
, NULL
, 0, buf
, sizeof(buf
));
728 ssld_update_config_one(ssl_ctl_t
*ctl
)
730 send_certfp_method(ctl
);
731 send_new_ssl_certs_one(ctl
);
735 ssld_update_config(void)
739 RB_DLINK_FOREACH(ptr
, ssl_daemons
.head
)
741 ssl_ctl_t
*ctl
= ptr
->data
;
743 if (ctl
->dead
|| ctl
->shutdown
)
746 ssld_update_config_one(ctl
);
751 start_ssld_accept(rb_fde_t
* sslF
, rb_fde_t
* plainF
, uint32_t id
)
760 uint32_to_buf(&buf
[1], id
);
765 ssl_cmd_write_queue(ctl
, F
, 2, buf
, sizeof(buf
));
770 start_ssld_connect(rb_fde_t
* sslF
, rb_fde_t
* plainF
, uint32_t id
)
779 uint32_to_buf(&buf
[1], id
);
785 ssl_cmd_write_queue(ctl
, F
, 2, buf
, sizeof(buf
));
790 ssld_decrement_clicount(ssl_ctl_t
* ctl
)
796 if(ctl
->shutdown
&& !ctl
->cli_count
)
799 rb_kill(ctl
->pid
, SIGKILL
);
801 if(ctl
->dead
&& !ctl
->cli_count
)
803 free_ssl_daemon(ctl
);
808 cleanup_dead_ssl(void *unused
)
810 rb_dlink_node
*ptr
, *next
;
812 RB_DLINK_FOREACH_SAFE(ptr
, next
, ssl_daemons
.head
)
815 if(ctl
->dead
&& !ctl
->cli_count
)
817 free_ssl_daemon(ctl
);
829 ssld_foreach_info(void (*func
)(void *data
, pid_t pid
, int cli_count
, enum ssld_status status
, const char *version
), void *data
)
831 rb_dlink_node
*ptr
, *next
;
833 RB_DLINK_FOREACH_SAFE(ptr
, next
, ssl_daemons
.head
)
836 func(data
, ctl
->pid
, ctl
->cli_count
,
837 ctl
->dead
? SSLD_DEAD
:
838 (ctl
->shutdown
? SSLD_SHUTDOWN
: SSLD_ACTIVE
),
846 rb_event_addish("cleanup_dead_ssld", cleanup_dead_ssl
, NULL
, 60);