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