]>
Commit | Line | Data |
---|---|---|
f8451915 AC |
1 | /* |
2 | * sslproc.c: An interface to ssld | |
3 | * Copyright (C) 2007 Aaron Sethman <androsyn@ratbox.org> | |
4 | * Copyright (C) 2007 ircd-ratbox development team | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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 | |
19 | * USA | |
20 | * | |
3202e249 | 21 | * $Id$ |
f8451915 AC |
22 | */ |
23 | ||
24 | #include <ratbox_lib.h> | |
25 | #include "stdinc.h" | |
3202e249 VY |
26 | |
27 | ||
f8451915 AC |
28 | #include "s_conf.h" |
29 | #include "logger.h" | |
30 | #include "listener.h" | |
31 | #include "sslproc.h" | |
32 | #include "s_serv.h" | |
33 | #include "ircd.h" | |
34 | #include "hash.h" | |
35 | #include "client.h" | |
36 | #include "send.h" | |
37 | #include "packet.h" | |
38 | ||
39 | #define ZIPSTATS_TIME 60 | |
40 | ||
41 | static void collect_zipstats(void *unused); | |
3202e249 | 42 | static void ssl_read_ctl(rb_fde_t * F, void *data); |
f8451915 AC |
43 | static int ssld_count; |
44 | ||
3202e249 | 45 | static char tmpbuf[READBUF_SIZE]; |
f8451915 AC |
46 | static char nul = '\0'; |
47 | ||
48 | #define MAXPASSFD 4 | |
49 | #define READSIZE 1024 | |
50 | typedef struct _ssl_ctl_buf | |
51 | { | |
52 | rb_dlink_node node; | |
53 | char *buf; | |
54 | size_t buflen; | |
55 | rb_fde_t *F[MAXPASSFD]; | |
56 | int nfds; | |
57 | } ssl_ctl_buf_t; | |
58 | ||
59 | ||
60 | struct _ssl_ctl | |
61 | { | |
62 | rb_dlink_node node; | |
63 | int cli_count; | |
64 | rb_fde_t *F; | |
65 | rb_fde_t *P; | |
66 | pid_t pid; | |
67 | rb_dlink_list readq; | |
68 | rb_dlink_list writeq; | |
0862e335 | 69 | uint8_t dead; |
f8451915 AC |
70 | }; |
71 | ||
3202e249 VY |
72 | static void send_new_ssl_certs_one(ssl_ctl_t * ctl, const char *ssl_cert, |
73 | const char *ssl_private_key, const char *ssl_dh_params); | |
74 | static void send_init_prng(ssl_ctl_t * ctl, prng_seed_t seedtype, const char *path); | |
13d8f0ed | 75 | static void send_certfp_method(ssl_ctl_t *ctl, int method); |
f8451915 AC |
76 | |
77 | ||
78 | static rb_dlink_list ssl_daemons; | |
79 | ||
196740c4 AC |
80 | static inline uint32_t |
81 | buf_to_uint32(char *buf) | |
f8451915 | 82 | { |
196740c4 | 83 | uint32_t x; |
0862e335 | 84 | memcpy(&x, buf, sizeof(x)); |
f8451915 AC |
85 | return x; |
86 | } | |
87 | ||
3202e249 | 88 | static inline void |
196740c4 | 89 | uint32_to_buf(char *buf, uint32_t x) |
f8451915 | 90 | { |
0862e335 | 91 | memcpy(buf, &x, sizeof(x)); |
f8451915 AC |
92 | return; |
93 | } | |
94 | ||
f8451915 | 95 | static ssl_ctl_t * |
3202e249 | 96 | allocate_ssl_daemon(rb_fde_t * F, rb_fde_t * P, int pid) |
f8451915 AC |
97 | { |
98 | ssl_ctl_t *ctl; | |
3202e249 | 99 | |
f8451915 AC |
100 | if(F == NULL || pid < 0) |
101 | return NULL; | |
3202e249 | 102 | ctl = rb_malloc(sizeof(ssl_ctl_t)); |
f8451915 AC |
103 | ctl->F = F; |
104 | ctl->P = P; | |
105 | ctl->pid = pid; | |
106 | ssld_count++; | |
107 | rb_dlinkAdd(ctl, &ctl->node, &ssl_daemons); | |
108 | return ctl; | |
109 | } | |
110 | ||
111 | static void | |
3202e249 | 112 | free_ssl_daemon(ssl_ctl_t * ctl) |
f8451915 AC |
113 | { |
114 | rb_dlink_node *ptr; | |
115 | ssl_ctl_buf_t *ctl_buf; | |
116 | int x; | |
117 | if(ctl->cli_count) | |
118 | return; | |
3202e249 | 119 | |
f8451915 AC |
120 | RB_DLINK_FOREACH(ptr, ctl->readq.head) |
121 | { | |
122 | ctl_buf = ptr->data; | |
123 | for(x = 0; x < ctl_buf->nfds; x++) | |
3202e249 | 124 | rb_close(ctl_buf->F[x]); |
f8451915 AC |
125 | |
126 | rb_free(ctl_buf->buf); | |
3202e249 | 127 | rb_free(ctl_buf); |
f8451915 AC |
128 | } |
129 | ||
130 | RB_DLINK_FOREACH(ptr, ctl->writeq.head) | |
131 | { | |
132 | ctl_buf = ptr->data; | |
133 | for(x = 0; x < ctl_buf->nfds; x++) | |
134 | rb_close(ctl_buf->F[x]); | |
135 | ||
136 | rb_free(ctl_buf->buf); | |
137 | rb_free(ctl_buf); | |
138 | } | |
139 | rb_close(ctl->F); | |
140 | rb_close(ctl->P); | |
141 | rb_dlinkDelete(&ctl->node, &ssl_daemons); | |
142 | rb_free(ctl); | |
143 | } | |
144 | ||
145 | static char *ssld_path; | |
146 | ||
147 | static int ssld_spin_count = 0; | |
148 | static time_t last_spin; | |
149 | static int ssld_wait = 0; | |
150 | ||
151 | ||
152 | static void | |
153 | ssl_killall(void) | |
154 | { | |
155 | rb_dlink_node *ptr, *next; | |
156 | ssl_ctl_t *ctl; | |
157 | RB_DLINK_FOREACH_SAFE(ptr, next, ssl_daemons.head) | |
158 | { | |
159 | ctl = ptr->data; | |
160 | if(ctl->dead) | |
161 | continue; | |
162 | ctl->dead = 1; | |
163 | ssld_count--; | |
3202e249 | 164 | rb_kill(ctl->pid, SIGKILL); |
f8451915 AC |
165 | } |
166 | } | |
167 | ||
168 | static void | |
3202e249 | 169 | ssl_dead(ssl_ctl_t * ctl) |
f8451915 AC |
170 | { |
171 | if(ctl->dead) | |
172 | return; | |
3202e249 | 173 | |
f8451915 AC |
174 | ctl->dead = 1; |
175 | ssld_count--; | |
3202e249 | 176 | rb_kill(ctl->pid, SIGKILL); /* make sure the process is really gone */ |
f8451915 | 177 | ilog(L_MAIN, "ssld helper died - attempting to restart"); |
536845c5 | 178 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "ssld helper died - attempting to restart"); |
f8451915 AC |
179 | start_ssldaemon(1, ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params); |
180 | } | |
181 | ||
182 | static void | |
3202e249 | 183 | ssl_do_pipe(rb_fde_t * F, void *data) |
f8451915 AC |
184 | { |
185 | int retlen; | |
186 | ssl_ctl_t *ctl = data; | |
187 | retlen = rb_write(F, "0", 1); | |
188 | if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) | |
189 | { | |
190 | ssl_dead(ctl); | |
191 | return; | |
192 | } | |
193 | rb_setselect(F, RB_SELECT_READ, ssl_do_pipe, data); | |
194 | } | |
195 | ||
196 | static void | |
197 | restart_ssld_event(void *unused) | |
198 | { | |
199 | ssld_spin_count = 0; | |
200 | last_spin = 0; | |
201 | ssld_wait = 0; | |
202 | if(ServerInfo.ssld_count > get_ssld_count()) | |
203 | { | |
204 | int start = ServerInfo.ssld_count - get_ssld_count(); | |
205 | ilog(L_MAIN, "Attempting to restart ssld processes"); | |
536845c5 | 206 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Attempt to restart ssld processes"); |
f8451915 AC |
207 | start_ssldaemon(start, ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params); |
208 | } | |
209 | } | |
210 | ||
211 | int | |
212 | start_ssldaemon(int count, const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params) | |
213 | { | |
214 | rb_fde_t *F1, *F2; | |
215 | rb_fde_t *P1, *P2; | |
3202e249 VY |
216 | #ifdef _WIN32 |
217 | const char *suffix = ".exe"; | |
218 | #else | |
219 | const char *suffix = ""; | |
220 | #endif | |
221 | ||
f8451915 AC |
222 | char fullpath[PATH_MAX + 1]; |
223 | char fdarg[6]; | |
224 | const char *parv[2]; | |
225 | char buf[128]; | |
3202e249 | 226 | char s_pid[10]; |
f8451915 AC |
227 | pid_t pid; |
228 | int started = 0, i; | |
229 | ||
230 | if(ssld_wait) | |
231 | return 0; | |
232 | ||
233 | if(ssld_spin_count > 20 && (rb_current_time() - last_spin < 5)) | |
234 | { | |
b9249347 | 235 | ilog(L_MAIN, "ssld helper is spinning - will attempt to restart in 1 minute"); |
3202e249 VY |
236 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
237 | "ssld helper is spinning - will attempt to restart in 1 minute"); | |
f8451915 AC |
238 | rb_event_add("restart_ssld_event", restart_ssld_event, NULL, 60); |
239 | ssld_wait = 1; | |
240 | return 0; | |
241 | } | |
242 | ||
243 | ssld_spin_count++; | |
244 | last_spin = rb_current_time(); | |
3202e249 | 245 | |
f8451915 AC |
246 | if(ssld_path == NULL) |
247 | { | |
c74836dc | 248 | rb_snprintf(fullpath, sizeof(fullpath), "%s/ssld%s", PKGLIBEXECDIR, suffix); |
3202e249 | 249 | |
f8451915 AC |
250 | if(access(fullpath, X_OK) == -1) |
251 | { | |
3202e249 VY |
252 | rb_snprintf(fullpath, sizeof(fullpath), "%s/bin/ssld%s", |
253 | ConfigFileEntry.dpath, suffix); | |
f8451915 AC |
254 | if(access(fullpath, X_OK) == -1) |
255 | { | |
3202e249 | 256 | ilog(L_MAIN, |
c74836dc NPB |
257 | "Unable to execute ssld%s in %s or %s/bin", |
258 | suffix, PKGLIBEXECDIR, ConfigFileEntry.dpath); | |
3202e249 | 259 | return 0; |
f8451915 AC |
260 | } |
261 | } | |
262 | ssld_path = rb_strdup(fullpath); | |
263 | } | |
f8451915 AC |
264 | rb_strlcpy(buf, "-ircd ssld daemon helper", sizeof(buf)); |
265 | parv[0] = buf; | |
266 | parv[1] = NULL; | |
267 | ||
268 | for(i = 0; i < count; i++) | |
269 | { | |
270 | ssl_ctl_t *ctl; | |
eda22d87 JT |
271 | if(rb_socketpair(AF_UNIX, SOCK_DGRAM, 0, &F1, &F2, "SSL/TLS handle passing socket") == -1) |
272 | { | |
273 | ilog(L_MAIN, "Unable to create ssld - rb_socketpair failed: %s", strerror(errno)); | |
274 | return started; | |
275 | } | |
55abcbb2 | 276 | |
f8451915 AC |
277 | rb_set_buffers(F1, READBUF_SIZE); |
278 | rb_set_buffers(F2, READBUF_SIZE); | |
279 | rb_snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(F2)); | |
3202e249 | 280 | rb_setenv("CTL_FD", fdarg, 1); |
cf09122b JT |
281 | if(rb_pipe(&P1, &P2, "SSL/TLS pipe") == -1) |
282 | { | |
283 | ilog(L_MAIN, "Unable to create ssld - rb_pipe failed: %s", strerror(errno)); | |
284 | return started; | |
285 | } | |
f8451915 | 286 | rb_snprintf(fdarg, sizeof(fdarg), "%d", rb_get_fd(P1)); |
3202e249 | 287 | rb_setenv("CTL_PIPE", fdarg, 1); |
bdbc9033 | 288 | rb_snprintf(s_pid, sizeof(s_pid), "%d", (int)getpid()); |
3202e249 VY |
289 | rb_setenv("CTL_PPID", s_pid, 1); |
290 | #ifdef _WIN32 | |
291 | SetHandleInformation((HANDLE) rb_get_fd(F2), HANDLE_FLAG_INHERIT, 1); | |
292 | SetHandleInformation((HANDLE) rb_get_fd(P1), HANDLE_FLAG_INHERIT, 1); | |
293 | #endif | |
294 | ||
295 | pid = rb_spawn_process(ssld_path, (const char **) parv); | |
f8451915 AC |
296 | if(pid == -1) |
297 | { | |
298 | ilog(L_MAIN, "Unable to create ssld: %s\n", strerror(errno)); | |
299 | rb_close(F1); | |
300 | rb_close(F2); | |
301 | rb_close(P1); | |
302 | rb_close(P2); | |
303 | return started; | |
304 | } | |
305 | started++; | |
306 | rb_close(F2); | |
307 | rb_close(P1); | |
308 | ctl = allocate_ssl_daemon(F1, P2, pid); | |
309 | if(ssl_ok) | |
13d8f0ed | 310 | { |
e861902f | 311 | send_init_prng(ctl, RB_PRNG_DEFAULT, NULL); |
13d8f0ed AC |
312 | send_certfp_method(ctl, ConfigFileEntry.certfp_method); |
313 | ||
314 | if(ssl_cert != NULL && ssl_private_key != NULL) | |
315 | send_new_ssl_certs_one(ctl, ssl_cert, ssl_private_key, | |
316 | ssl_dh_params != NULL ? ssl_dh_params : ""); | |
317 | } | |
f8451915 AC |
318 | ssl_read_ctl(ctl->F, ctl); |
319 | ssl_do_pipe(P2, ctl); | |
3202e249 | 320 | |
f8451915 | 321 | } |
3202e249 | 322 | return started; |
f8451915 AC |
323 | } |
324 | ||
325 | static void | |
3202e249 | 326 | ssl_process_zipstats(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf) |
f8451915 AC |
327 | { |
328 | struct Client *server; | |
329 | struct ZipStats *zips; | |
4f7a1fee | 330 | char *parv[7]; |
32fb5895 | 331 | (void) rb_string_to_array(ctl_buf->buf, parv, 6); |
f8451915 AC |
332 | server = find_server(NULL, parv[1]); |
333 | if(server == NULL || server->localClient == NULL || !IsCapable(server, CAP_ZIP)) | |
334 | return; | |
335 | if(server->localClient->zipstats == NULL) | |
336 | server->localClient->zipstats = rb_malloc(sizeof(struct ZipStats)); | |
3202e249 | 337 | |
f8451915 AC |
338 | zips = server->localClient->zipstats; |
339 | ||
340 | zips->in += strtoull(parv[2], NULL, 10); | |
341 | zips->in_wire += strtoull(parv[3], NULL, 10); | |
342 | zips->out += strtoull(parv[4], NULL, 10); | |
343 | zips->out_wire += strtoull(parv[5], NULL, 10); | |
3202e249 | 344 | |
f8451915 | 345 | if(zips->in > 0) |
3202e249 | 346 | zips->in_ratio = ((double) (zips->in - zips->in_wire) / (double) zips->in) * 100.00; |
f8451915 AC |
347 | else |
348 | zips->in_ratio = 0; | |
3202e249 | 349 | |
f8451915 | 350 | if(zips->out > 0) |
3202e249 | 351 | zips->out_ratio = ((double) (zips->out - zips->out_wire) / (double) zips->out) * 100.00; |
f8451915 AC |
352 | else |
353 | zips->out_ratio = 0; | |
354 | } | |
355 | ||
356 | static void | |
3202e249 | 357 | ssl_process_dead_fd(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf) |
f8451915 AC |
358 | { |
359 | struct Client *client_p; | |
360 | char reason[256]; | |
196740c4 | 361 | uint32_t fd; |
f8451915 AC |
362 | |
363 | if(ctl_buf->buflen < 6) | |
3202e249 VY |
364 | return; /* bogus message..drop it.. XXX should warn here */ |
365 | ||
196740c4 | 366 | fd = buf_to_uint32(&ctl_buf->buf[1]); |
f8451915 AC |
367 | rb_strlcpy(reason, &ctl_buf->buf[5], sizeof(reason)); |
368 | client_p = find_cli_fd_hash(fd); | |
369 | if(client_p == NULL) | |
370 | return; | |
42d609f6 JT |
371 | if(IsAnyServer(client_p) || IsRegistered(client_p)) |
372 | { | |
373 | /* read any last moment ERROR, QUIT or the like -- jilles */ | |
374 | if (!strcmp(reason, "Remote host closed the connection")) | |
375 | read_packet(client_p->localClient->F, client_p); | |
376 | if (IsAnyDead(client_p)) | |
377 | return; | |
378 | } | |
f8451915 | 379 | if(IsAnyServer(client_p)) |
81c5873e | 380 | { |
2a073ca2 | 381 | sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL, "ssld error for %s: %s", client_p->name, reason); |
81c5873e JT |
382 | ilog(L_SERVER, "ssld error for %s: %s", log_client_name(client_p, SHOW_IP), reason); |
383 | } | |
f8451915 AC |
384 | exit_client(client_p, client_p, &me, reason); |
385 | } | |
386 | ||
ebe33dbf AC |
387 | |
388 | static void | |
389 | ssl_process_cipher_string(ssl_ctl_t *ctl, ssl_ctl_buf_t *ctl_buf) | |
390 | { | |
391 | struct Client *client_p; | |
392 | const char *cstring; | |
393 | uint32_t fd; | |
394 | ||
395 | if(ctl_buf->buflen < 6) | |
396 | return; /* bogus message..drop it.. XXX should warn here */ | |
397 | ||
398 | fd = buf_to_uint32(&ctl_buf->buf[1]); | |
399 | cstring = (const char *)&ctl_buf->buf[5]; | |
400 | ||
401 | if(EmptyString(cstring)) | |
402 | return; | |
403 | ||
404 | client_p = find_cli_fd_hash(fd); | |
405 | if(client_p != NULL && client_p->localClient != NULL) | |
406 | { | |
407 | rb_free(client_p->localClient->cipher_string); | |
408 | client_p->localClient->cipher_string = rb_strdup(cstring); | |
409 | } | |
410 | } | |
411 | ||
412 | ||
7247337a JT |
413 | static void |
414 | ssl_process_certfp(ssl_ctl_t * ctl, ssl_ctl_buf_t * ctl_buf) | |
415 | { | |
416 | struct Client *client_p; | |
196740c4 AC |
417 | uint32_t fd; |
418 | uint32_t len; | |
7247337a | 419 | uint8_t *certfp; |
8eda114a | 420 | char *certfp_string; |
7247337a JT |
421 | int i; |
422 | ||
772c95cc | 423 | if(ctl_buf->buflen > 5 + RB_SSL_CERTFP_LEN) |
7247337a JT |
424 | return; /* bogus message..drop it.. XXX should warn here */ |
425 | ||
196740c4 AC |
426 | fd = buf_to_uint32(&ctl_buf->buf[1]); |
427 | len = buf_to_uint32(&ctl_buf->buf[5]); | |
e6bbb410 | 428 | certfp = (uint8_t *)&ctl_buf->buf[9]; |
7247337a JT |
429 | client_p = find_cli_fd_hash(fd); |
430 | if(client_p == NULL) | |
431 | return; | |
8eda114a | 432 | rb_free(client_p->certfp); |
e6bbb410 EM |
433 | certfp_string = rb_malloc(len * 2 + 1); |
434 | for(i = 0; i < len; i++) | |
7247337a JT |
435 | rb_snprintf(certfp_string + 2 * i, 3, "%02x", |
436 | certfp[i]); | |
8eda114a | 437 | client_p->certfp = certfp_string; |
7247337a JT |
438 | } |
439 | ||
f8451915 | 440 | static void |
3202e249 | 441 | ssl_process_cmd_recv(ssl_ctl_t * ctl) |
f8451915 AC |
442 | { |
443 | static const char *cannot_setup_ssl = "ssld cannot setup ssl, check your certificates and private key"; | |
444 | static const char *no_ssl_or_zlib = "ssld has neither SSL/TLS or zlib support killing all sslds"; | |
3202e249 | 445 | rb_dlink_node *ptr, *next; |
f8451915 AC |
446 | ssl_ctl_buf_t *ctl_buf; |
447 | if(ctl->dead) | |
448 | return; | |
449 | RB_DLINK_FOREACH_SAFE(ptr, next, ctl->readq.head) | |
450 | { | |
3202e249 VY |
451 | ctl_buf = ptr->data; |
452 | switch (*ctl_buf->buf) | |
f8451915 | 453 | { |
3202e249 VY |
454 | case 'N': |
455 | ssl_ok = 0; /* ssld says it can't do ssl/tls */ | |
456 | break; | |
457 | case 'D': | |
458 | ssl_process_dead_fd(ctl, ctl_buf); | |
459 | break; | |
ebe33dbf AC |
460 | case 'C': |
461 | ssl_process_cipher_string(ctl, ctl_buf); | |
462 | break; | |
7247337a JT |
463 | case 'F': |
464 | ssl_process_certfp(ctl, ctl_buf); | |
465 | break; | |
3202e249 VY |
466 | case 'S': |
467 | ssl_process_zipstats(ctl, ctl_buf); | |
468 | break; | |
469 | case 'I': | |
470 | ssl_ok = 0; | |
32ea9d3d | 471 | ilog(L_MAIN, "%s", cannot_setup_ssl); |
481b443b | 472 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", cannot_setup_ssl); |
f1709d5a | 473 | break; |
3202e249 VY |
474 | case 'U': |
475 | zlib_ok = 0; | |
476 | ssl_ok = 0; | |
32ea9d3d | 477 | ilog(L_MAIN, "%s", no_ssl_or_zlib); |
481b443b | 478 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", no_ssl_or_zlib); |
3202e249 VY |
479 | ssl_killall(); |
480 | break; | |
3202e249 VY |
481 | case 'z': |
482 | zlib_ok = 0; | |
483 | break; | |
484 | default: | |
485 | ilog(L_MAIN, "Received invalid command from ssld: %s", ctl_buf->buf); | |
486 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Received invalid command from ssld"); | |
487 | break; | |
f8451915 AC |
488 | } |
489 | rb_dlinkDelete(ptr, &ctl->readq); | |
490 | rb_free(ctl_buf->buf); | |
491 | rb_free(ctl_buf); | |
492 | } | |
493 | ||
494 | } | |
495 | ||
496 | ||
497 | static void | |
3202e249 | 498 | ssl_read_ctl(rb_fde_t * F, void *data) |
f8451915 AC |
499 | { |
500 | ssl_ctl_buf_t *ctl_buf; | |
501 | ssl_ctl_t *ctl = data; | |
502 | int retlen; | |
503 | ||
504 | if(ctl->dead) | |
505 | return; | |
506 | do | |
507 | { | |
508 | ctl_buf = rb_malloc(sizeof(ssl_ctl_buf_t)); | |
509 | ctl_buf->buf = rb_malloc(READSIZE); | |
510 | retlen = rb_recv_fd_buf(ctl->F, ctl_buf->buf, READSIZE, ctl_buf->F, 4); | |
511 | ctl_buf->buflen = retlen; | |
3202e249 VY |
512 | if(retlen <= 0) |
513 | { | |
f8451915 AC |
514 | rb_free(ctl_buf->buf); |
515 | rb_free(ctl_buf); | |
516 | } | |
517 | else | |
518 | rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->readq); | |
3202e249 VY |
519 | } |
520 | while(retlen > 0); | |
521 | ||
f8451915 AC |
522 | if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) |
523 | { | |
524 | ssl_dead(ctl); | |
525 | return; | |
3202e249 | 526 | } |
f8451915 AC |
527 | ssl_process_cmd_recv(ctl); |
528 | rb_setselect(ctl->F, RB_SELECT_READ, ssl_read_ctl, ctl); | |
529 | } | |
530 | ||
531 | static ssl_ctl_t * | |
532 | which_ssld(void) | |
533 | { | |
534 | ssl_ctl_t *ctl, *lowest = NULL; | |
535 | rb_dlink_node *ptr; | |
3202e249 | 536 | |
f8451915 AC |
537 | RB_DLINK_FOREACH(ptr, ssl_daemons.head) |
538 | { | |
539 | ctl = ptr->data; | |
540 | if(ctl->dead) | |
541 | continue; | |
3202e249 VY |
542 | if(lowest == NULL) |
543 | { | |
f8451915 AC |
544 | lowest = ctl; |
545 | continue; | |
546 | } | |
547 | if(ctl->cli_count < lowest->cli_count) | |
548 | lowest = ctl; | |
549 | } | |
3202e249 | 550 | return (lowest); |
f8451915 AC |
551 | } |
552 | ||
553 | static void | |
3202e249 | 554 | ssl_write_ctl(rb_fde_t * F, void *data) |
f8451915 AC |
555 | { |
556 | ssl_ctl_t *ctl = data; | |
557 | ssl_ctl_buf_t *ctl_buf; | |
558 | rb_dlink_node *ptr, *next; | |
559 | int retlen, x; | |
560 | ||
561 | if(ctl->dead) | |
562 | return; | |
563 | ||
564 | RB_DLINK_FOREACH_SAFE(ptr, next, ctl->writeq.head) | |
565 | { | |
566 | ctl_buf = ptr->data; | |
567 | /* in theory unix sock_dgram shouldn't ever short write this.. */ | |
3202e249 | 568 | retlen = rb_send_fd_buf(ctl->F, ctl_buf->F, ctl_buf->nfds, ctl_buf->buf, ctl_buf->buflen, ctl->pid); |
f8451915 AC |
569 | if(retlen > 0) |
570 | { | |
571 | rb_dlinkDelete(ptr, &ctl->writeq); | |
572 | for(x = 0; x < ctl_buf->nfds; x++) | |
573 | rb_close(ctl_buf->F[x]); | |
574 | rb_free(ctl_buf->buf); | |
575 | rb_free(ctl_buf); | |
3202e249 | 576 | |
f8451915 AC |
577 | } |
578 | if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) | |
579 | { | |
580 | ssl_dead(ctl); | |
581 | return; | |
3202e249 VY |
582 | } |
583 | else | |
584 | { | |
f8451915 AC |
585 | rb_setselect(ctl->F, RB_SELECT_WRITE, ssl_write_ctl, ctl); |
586 | } | |
587 | } | |
588 | } | |
589 | ||
590 | static void | |
3202e249 | 591 | ssl_cmd_write_queue(ssl_ctl_t * ctl, rb_fde_t ** F, int count, const void *buf, size_t buflen) |
f8451915 AC |
592 | { |
593 | ssl_ctl_buf_t *ctl_buf; | |
3202e249 | 594 | int x; |
f8451915 AC |
595 | |
596 | /* don't bother */ | |
597 | if(ctl->dead) | |
598 | return; | |
3202e249 | 599 | |
f8451915 AC |
600 | ctl_buf = rb_malloc(sizeof(ssl_ctl_buf_t)); |
601 | ctl_buf->buf = rb_malloc(buflen); | |
602 | memcpy(ctl_buf->buf, buf, buflen); | |
603 | ctl_buf->buflen = buflen; | |
3202e249 | 604 | |
f8451915 AC |
605 | for(x = 0; x < count && x < MAXPASSFD; x++) |
606 | { | |
3202e249 | 607 | ctl_buf->F[x] = F[x]; |
f8451915 AC |
608 | } |
609 | ctl_buf->nfds = count; | |
610 | rb_dlinkAddTail(ctl_buf, &ctl_buf->node, &ctl->writeq); | |
611 | ssl_write_ctl(ctl->F, ctl); | |
612 | } | |
613 | ||
614 | ||
615 | static void | |
3202e249 | 616 | send_new_ssl_certs_one(ssl_ctl_t * ctl, const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params) |
f8451915 AC |
617 | { |
618 | size_t len; | |
619 | ||
3202e249 | 620 | len = strlen(ssl_cert) + strlen(ssl_private_key) + strlen(ssl_dh_params) + 5; |
f8451915 AC |
621 | if(len > sizeof(tmpbuf)) |
622 | { | |
3202e249 VY |
623 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
624 | "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...", | |
625 | len, sizeof(tmpbuf)); | |
626 | ilog(L_MAIN, | |
627 | "Parameters for send_new_ssl_certs_one too long (%zu > %zu) to pass to ssld, not sending...", | |
628 | len, sizeof(tmpbuf)); | |
f8451915 AC |
629 | return; |
630 | } | |
3202e249 VY |
631 | len = rb_snprintf(tmpbuf, sizeof(tmpbuf), "K%c%s%c%s%c%s%c", nul, ssl_cert, nul, |
632 | ssl_private_key, nul, ssl_dh_params, nul); | |
f8451915 AC |
633 | ssl_cmd_write_queue(ctl, NULL, 0, tmpbuf, len); |
634 | } | |
635 | ||
636 | static void | |
3202e249 | 637 | send_init_prng(ssl_ctl_t * ctl, prng_seed_t seedtype, const char *path) |
f8451915 AC |
638 | { |
639 | size_t len; | |
640 | const char *s; | |
0862e335 | 641 | uint8_t seed = (uint8_t) seedtype; |
f8451915 AC |
642 | |
643 | if(path == NULL) | |
644 | s = ""; | |
645 | else | |
646 | s = path; | |
647 | ||
648 | len = strlen(s) + 3; | |
649 | if(len > sizeof(tmpbuf)) | |
650 | { | |
3202e249 VY |
651 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
652 | "Parameters for send_init_prng too long (%zd > %zd) to pass to ssld, not sending...", | |
653 | len, sizeof(tmpbuf)); | |
654 | ilog(L_MAIN, | |
655 | "Parameters for send_init_prng too long (%zd > %zd) to pass to ssld, not sending...", | |
656 | len, sizeof(tmpbuf)); | |
f8451915 | 657 | return; |
3202e249 VY |
658 | |
659 | } | |
f8451915 AC |
660 | len = rb_snprintf(tmpbuf, sizeof(tmpbuf), "I%c%s%c", seed, s, nul); |
661 | ssl_cmd_write_queue(ctl, NULL, 0, tmpbuf, len); | |
662 | } | |
663 | ||
13d8f0ed AC |
664 | static void |
665 | send_certfp_method(ssl_ctl_t *ctl, int method) | |
666 | { | |
667 | char buf[5]; | |
668 | ||
669 | buf[0] = 'F'; | |
196740c4 | 670 | uint32_to_buf(&buf[1], method); |
13d8f0ed AC |
671 | ssl_cmd_write_queue(ctl, NULL, 0, buf, sizeof(buf)); |
672 | } | |
673 | ||
f8451915 AC |
674 | void |
675 | send_new_ssl_certs(const char *ssl_cert, const char *ssl_private_key, const char *ssl_dh_params) | |
676 | { | |
677 | rb_dlink_node *ptr; | |
678 | if(ssl_cert == NULL || ssl_private_key == NULL || ssl_dh_params == NULL) | |
679 | { | |
680 | ssl_ok = 0; | |
681 | return; | |
682 | } | |
683 | RB_DLINK_FOREACH(ptr, ssl_daemons.head) | |
684 | { | |
685 | ssl_ctl_t *ctl = ptr->data; | |
3202e249 | 686 | send_new_ssl_certs_one(ctl, ssl_cert, ssl_private_key, ssl_dh_params); |
f8451915 AC |
687 | } |
688 | } | |
689 | ||
690 | ||
3202e249 | 691 | ssl_ctl_t * |
196740c4 | 692 | start_ssld_accept(rb_fde_t * sslF, rb_fde_t * plainF, uint32_t id) |
f8451915 AC |
693 | { |
694 | rb_fde_t *F[2]; | |
695 | ssl_ctl_t *ctl; | |
696 | char buf[5]; | |
697 | F[0] = sslF; | |
698 | F[1] = plainF; | |
699 | ||
700 | buf[0] = 'A'; | |
196740c4 | 701 | uint32_to_buf(&buf[1], id); |
f8451915 AC |
702 | ctl = which_ssld(); |
703 | ctl->cli_count++; | |
704 | ssl_cmd_write_queue(ctl, F, 2, buf, sizeof(buf)); | |
705 | return ctl; | |
706 | } | |
707 | ||
708 | ssl_ctl_t * | |
196740c4 | 709 | start_ssld_connect(rb_fde_t * sslF, rb_fde_t * plainF, uint32_t id) |
f8451915 AC |
710 | { |
711 | rb_fde_t *F[2]; | |
712 | ssl_ctl_t *ctl; | |
713 | char buf[5]; | |
714 | F[0] = sslF; | |
715 | F[1] = plainF; | |
716 | ||
717 | buf[0] = 'C'; | |
196740c4 | 718 | uint32_to_buf(&buf[1], id); |
f8451915 AC |
719 | |
720 | ctl = which_ssld(); | |
721 | ctl->cli_count++; | |
722 | ssl_cmd_write_queue(ctl, F, 2, buf, sizeof(buf)); | |
3202e249 | 723 | return ctl; |
f8451915 AC |
724 | } |
725 | ||
3202e249 VY |
726 | void |
727 | ssld_decrement_clicount(ssl_ctl_t * ctl) | |
f8451915 AC |
728 | { |
729 | if(ctl == NULL) | |
730 | return; | |
731 | ||
732 | ctl->cli_count--; | |
733 | if(ctl->dead && !ctl->cli_count) | |
734 | { | |
735 | free_ssl_daemon(ctl); | |
736 | } | |
737 | } | |
738 | ||
55abcbb2 | 739 | /* |
f8451915 | 740 | * what we end up sending to the ssld process for ziplinks is the following |
55abcbb2 KB |
741 | * Z[ourfd][level][RECVQ] |
742 | * Z = ziplinks command = buf[0] | |
f8451915 AC |
743 | * ourfd = Our end of the socketpair = buf[1..4] |
744 | * level = zip level buf[5] | |
745 | * recvqlen = our recvq len = buf[6-7] | |
746 | * recvq = any data we read prior to starting ziplinks | |
747 | */ | |
748 | void | |
749 | start_zlib_session(void *data) | |
750 | { | |
3202e249 | 751 | struct Client *server = (struct Client *) data; |
0862e335 VY |
752 | uint16_t recvqlen; |
753 | uint8_t level; | |
f8451915 AC |
754 | void *xbuf; |
755 | ||
756 | rb_fde_t *F[2]; | |
757 | rb_fde_t *xF1, *xF2; | |
758 | char *buf; | |
07c2bb75 | 759 | char buf2[9]; |
f8451915 AC |
760 | void *recvq_start; |
761 | ||
196740c4 | 762 | size_t hdr = (sizeof(uint8_t) * 2) + sizeof(uint32_t); |
f8451915 AC |
763 | size_t len; |
764 | int cpylen, left; | |
765 | ||
766 | server->localClient->event = NULL; | |
767 | ||
768 | recvqlen = rb_linebuf_len(&server->localClient->buf_recvq); | |
3202e249 | 769 | |
f8451915 AC |
770 | len = recvqlen + hdr; |
771 | ||
772 | if(len > READBUF_SIZE) | |
773 | { | |
3202e249 VY |
774 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
775 | "ssld - attempted to pass message of %zd len, max len %d, giving up", | |
776 | len, READBUF_SIZE); | |
f8451915 AC |
777 | ilog(L_MAIN, "ssld - attempted to pass message of %zd len, max len %d, giving up", len, READBUF_SIZE); |
778 | exit_client(server, server, server, "ssld readbuf exceeded"); | |
779 | return; | |
780 | } | |
781 | ||
3202e249 | 782 | buf = rb_malloc(len); |
f8451915 AC |
783 | level = ConfigFileEntry.compression_level; |
784 | ||
196740c4 | 785 | uint32_to_buf(&buf[1], rb_get_fd(server->localClient->F)); |
3202e249 | 786 | buf[5] = (char) level; |
f8451915 | 787 | |
3202e249 | 788 | recvq_start = &buf[6]; |
f8451915 AC |
789 | server->localClient->zipstats = rb_malloc(sizeof(struct ZipStats)); |
790 | ||
791 | xbuf = recvq_start; | |
792 | left = recvqlen; | |
793 | ||
794 | do | |
795 | { | |
796 | cpylen = rb_linebuf_get(&server->localClient->buf_recvq, xbuf, left, LINEBUF_PARTIAL, LINEBUF_RAW); | |
797 | left -= cpylen; | |
3202e249 VY |
798 | xbuf = (void *) (((uintptr_t) xbuf) + cpylen); |
799 | } | |
800 | while(cpylen > 0); | |
f8451915 AC |
801 | |
802 | /* Pass the socket to ssld. */ | |
803 | *buf = 'Z'; | |
eda22d87 JT |
804 | if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF1, &xF2, "Initial zlib socketpairs") == -1) |
805 | { | |
806 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Error creating zlib socketpair - %s", strerror(errno)); | |
807 | ilog(L_MAIN, "Error creating zlib socketpairs - %s", strerror(errno)); | |
808 | exit_client(server, server, server, "Error creating zlib socketpair"); | |
809 | return; | |
810 | } | |
55abcbb2 | 811 | |
07c2bb75 JT |
812 | if(IsSSL(server)) |
813 | { | |
814 | /* tell ssld the new connid for the ssl part*/ | |
815 | buf2[0] = 'Y'; | |
196740c4 AC |
816 | uint32_to_buf(&buf2[1], rb_get_fd(server->localClient->F)); |
817 | uint32_to_buf(&buf2[5], rb_get_fd(xF2)); | |
07c2bb75 JT |
818 | ssl_cmd_write_queue(server->localClient->ssl_ctl, NULL, 0, buf2, sizeof(buf2)); |
819 | } | |
820 | ||
821 | ||
3202e249 | 822 | F[0] = server->localClient->F; |
f8451915 | 823 | F[1] = xF1; |
3202e249 | 824 | del_from_cli_fd_hash(server); |
f8451915 AC |
825 | server->localClient->F = xF2; |
826 | /* need to redo as what we did before isn't valid now */ | |
196740c4 | 827 | uint32_to_buf(&buf[1], rb_get_fd(server->localClient->F)); |
f8451915 | 828 | add_to_cli_fd_hash(server); |
a4165b42 AS |
829 | |
830 | server->localClient->z_ctl = which_ssld(); | |
831 | server->localClient->z_ctl->cli_count++; | |
832 | ssl_cmd_write_queue(server->localClient->z_ctl, F, 2, buf, len); | |
f8451915 AC |
833 | rb_free(buf); |
834 | } | |
835 | ||
836 | static void | |
837 | collect_zipstats(void *unused) | |
838 | { | |
839 | rb_dlink_node *ptr; | |
840 | struct Client *target_p; | |
196740c4 | 841 | char buf[sizeof(uint8_t) + sizeof(uint32_t) + HOSTLEN]; |
f8451915 AC |
842 | void *odata; |
843 | size_t len; | |
196740c4 | 844 | uint32_t id; |
f8451915 AC |
845 | |
846 | buf[0] = 'S'; | |
196740c4 | 847 | odata = buf + sizeof(uint8_t) + sizeof(uint32_t); |
f8451915 AC |
848 | |
849 | RB_DLINK_FOREACH(ptr, serv_list.head) | |
850 | { | |
851 | target_p = ptr->data; | |
852 | if(IsCapable(target_p, CAP_ZIP)) | |
853 | { | |
0862e335 | 854 | len = sizeof(uint8_t) + sizeof(uint32_t); |
f8451915 AC |
855 | |
856 | id = rb_get_fd(target_p->localClient->F); | |
196740c4 | 857 | uint32_to_buf(&buf[1], id); |
3202e249 VY |
858 | rb_strlcpy(odata, target_p->name, (sizeof(buf) - len)); |
859 | len += strlen(odata) + 1; /* Get the \0 as well */ | |
a4165b42 | 860 | ssl_cmd_write_queue(target_p->localClient->z_ctl, NULL, 0, buf, len); |
f8451915 AC |
861 | } |
862 | } | |
863 | } | |
864 | ||
865 | static void | |
866 | cleanup_dead_ssl(void *unused) | |
867 | { | |
868 | rb_dlink_node *ptr, *next; | |
869 | ssl_ctl_t *ctl; | |
870 | RB_DLINK_FOREACH_SAFE(ptr, next, ssl_daemons.head) | |
871 | { | |
872 | ctl = ptr->data; | |
873 | if(ctl->dead && !ctl->cli_count) | |
874 | { | |
3202e249 | 875 | free_ssl_daemon(ctl); |
f8451915 AC |
876 | } |
877 | } | |
878 | } | |
879 | ||
880 | int | |
881 | get_ssld_count(void) | |
882 | { | |
883 | return ssld_count; | |
884 | } | |
885 | ||
3202e249 VY |
886 | void |
887 | init_ssld(void) | |
f8451915 AC |
888 | { |
889 | rb_event_addish("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME); | |
890 | rb_event_addish("cleanup_dead_ssld", cleanup_dead_ssl, NULL, 1200); | |
891 | } |