]>
Commit | Line | Data |
---|---|---|
212380e3 | 1 | /* |
b37021a4 | 2 | * charybdis: an advanced ircd. |
212380e3 AC |
3 | * client.c: Controls clients. |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
7 | * Copyright (C) 2002-2005 ircd-ratbox development team | |
b37021a4 | 8 | * Copyright (C) 2007 William Pitcock |
212380e3 AC |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
23 | * USA | |
24 | * | |
f80a1823 | 25 | * $Id: client.c 3514 2007-06-06 16:25:21Z nenolod $ |
212380e3 AC |
26 | */ |
27 | #include "stdinc.h" | |
28 | #include "config.h" | |
29 | ||
212380e3 AC |
30 | #include "client.h" |
31 | #include "class.h" | |
32 | #include "common.h" | |
212380e3 AC |
33 | #include "hash.h" |
34 | #include "irc_string.h" | |
35 | #include "sprintf_irc.h" | |
36 | #include "ircd.h" | |
37 | #include "s_gline.h" | |
38 | #include "numeric.h" | |
39 | #include "packet.h" | |
40 | #include "s_auth.h" | |
212380e3 AC |
41 | #include "s_conf.h" |
42 | #include "s_newconf.h" | |
4016731b | 43 | #include "logger.h" |
212380e3 AC |
44 | #include "s_serv.h" |
45 | #include "s_stats.h" | |
46 | #include "send.h" | |
47 | #include "whowas.h" | |
48 | #include "s_user.h" | |
212380e3 | 49 | #include "hash.h" |
212380e3 | 50 | #include "hostmask.h" |
212380e3 AC |
51 | #include "listener.h" |
52 | #include "hook.h" | |
53 | #include "msg.h" | |
54 | #include "monitor.h" | |
55 | #include "blacklist.h" | |
54015b5f | 56 | #include "reject.h" |
994544c2 | 57 | #include "scache.h" |
b37021a4 | 58 | #include "irc_dictionary.h" |
c6d72037 | 59 | #include "sslproc.h" |
212380e3 AC |
60 | |
61 | #define DEBUG_EXITED_CLIENTS | |
62 | ||
330fc5c1 AC |
63 | static void check_pings_list(rb_dlink_list * list); |
64 | static void check_unknowns_list(rb_dlink_list * list); | |
212380e3 AC |
65 | static void free_exited_clients(void *unused); |
66 | static void exit_aborted_clients(void *unused); | |
67 | ||
68 | static int exit_remote_client(struct Client *, struct Client *, struct Client *,const char *); | |
69 | static int exit_remote_server(struct Client *, struct Client *, struct Client *,const char *); | |
70 | static int exit_local_client(struct Client *, struct Client *, struct Client *,const char *); | |
71 | static int exit_unknown_client(struct Client *, struct Client *, struct Client *,const char *); | |
72 | static int exit_local_server(struct Client *, struct Client *, struct Client *,const char *); | |
73 | static int qs_server(struct Client *, struct Client *, struct Client *, const char *comment); | |
74 | ||
75 | static EVH check_pings; | |
76 | ||
398b6a73 VY |
77 | extern rb_bh *client_heap; |
78 | extern rb_bh *lclient_heap; | |
79 | extern rb_bh *pclient_heap; | |
adc6cc42 | 80 | static rb_bh *away_heap = NULL; |
212380e3 AC |
81 | |
82 | extern char current_uid[IDLEN]; | |
83 | ||
43de0f45 AC |
84 | struct Dictionary *nd_dict = NULL; |
85 | ||
212380e3 AC |
86 | enum |
87 | { | |
88 | D_LINED, | |
89 | K_LINED, | |
90 | G_LINED | |
91 | }; | |
92 | ||
330fc5c1 | 93 | rb_dlink_list dead_list; |
212380e3 | 94 | #ifdef DEBUG_EXITED_CLIENTS |
330fc5c1 | 95 | static rb_dlink_list dead_remote_list; |
212380e3 AC |
96 | #endif |
97 | ||
98 | struct abort_client | |
99 | { | |
330fc5c1 | 100 | rb_dlink_node node; |
212380e3 AC |
101 | struct Client *client; |
102 | char notice[REASONLEN]; | |
103 | }; | |
104 | ||
330fc5c1 | 105 | static rb_dlink_list abort_list; |
212380e3 AC |
106 | |
107 | ||
108 | /* | |
109 | * init_client | |
110 | * | |
111 | * inputs - NONE | |
112 | * output - NONE | |
113 | * side effects - initialize client free memory | |
114 | */ | |
115 | void | |
116 | init_client(void) | |
117 | { | |
118 | /* | |
119 | * start off the check ping event .. -- adrian | |
120 | * Every 30 seconds is plenty -- db | |
121 | */ | |
1087485c | 122 | client_heap = rb_bh_create(sizeof(struct Client), CLIENT_HEAP_SIZE, "client_heap"); |
adc6cc42 VY |
123 | lclient_heap = rb_bh_create(sizeof(struct LocalUser), LCLIENT_HEAP_SIZE, "lclient_heap"); |
124 | pclient_heap = rb_bh_create(sizeof(struct PreClient), PCLIENT_HEAP_SIZE, "pclient_heap"); | |
125 | away_heap = rb_bh_create(AWAYLEN, AWAY_HEAP_SIZE, "away_heap"); | |
126 | ||
3b2ebd04 JT |
127 | rb_event_addish("check_pings", check_pings, NULL, 30); |
128 | rb_event_addish("free_exited_clients", &free_exited_clients, NULL, 4); | |
129 | rb_event_addish("exit_aborted_clients", exit_aborted_clients, NULL, 1); | |
5a606a8f | 130 | rb_event_add("flood_recalc", flood_recalc, NULL, 1); |
43de0f45 AC |
131 | |
132 | nd_dict = irc_dictionary_create(irccmp); | |
212380e3 AC |
133 | } |
134 | ||
135 | ||
136 | /* | |
137 | * make_client - create a new Client struct and set it to initial state. | |
138 | * | |
139 | * from == NULL, create local client (a client connected | |
140 | * to a socket). | |
141 | * | |
142 | * from, create remote client (behind a socket | |
143 | * associated with the client defined by | |
144 | * 'from'). ('from' is a local client!!). | |
145 | */ | |
146 | struct Client * | |
147 | make_client(struct Client *from) | |
148 | { | |
149 | struct Client *client_p = NULL; | |
150 | struct LocalUser *localClient; | |
151 | ||
398b6a73 | 152 | client_p = rb_bh_alloc(client_heap); |
212380e3 AC |
153 | |
154 | if(from == NULL) | |
155 | { | |
156 | client_p->from = client_p; /* 'from' of local client is self! */ | |
157 | ||
398b6a73 | 158 | localClient = (struct LocalUser *) rb_bh_alloc(lclient_heap); |
212380e3 AC |
159 | SetMyConnect(client_p); |
160 | client_p->localClient = localClient; | |
161 | ||
e3354945 | 162 | client_p->localClient->lasttime = client_p->localClient->firsttime = rb_current_time(); |
212380e3 | 163 | |
c5c2f506 | 164 | client_p->localClient->F = NULL; |
212380e3 | 165 | |
398b6a73 | 166 | client_p->preClient = (struct PreClient *) rb_bh_alloc(pclient_heap); |
212380e3 AC |
167 | |
168 | /* as good a place as any... */ | |
330fc5c1 | 169 | rb_dlinkAdd(client_p, &client_p->localClient->tnode, &unknown_list); |
212380e3 AC |
170 | } |
171 | else | |
172 | { /* from is not NULL */ | |
173 | client_p->localClient = NULL; | |
174 | client_p->preClient = NULL; | |
175 | client_p->from = from; /* 'from' of local client is self! */ | |
176 | } | |
177 | ||
178 | SetUnknown(client_p); | |
179 | strcpy(client_p->username, "unknown"); | |
180 | ||
181 | return client_p; | |
182 | } | |
183 | ||
184 | void | |
185 | free_pre_client(struct Client *client_p) | |
186 | { | |
187 | struct Blacklist *blptr; | |
188 | ||
189 | s_assert(NULL != client_p); | |
190 | ||
191 | if(client_p->preClient == NULL) | |
192 | return; | |
193 | ||
194 | blptr = client_p->preClient->dnsbl_listed; | |
195 | if (blptr != NULL) | |
196 | unref_blacklist(blptr); | |
197 | abort_blacklist_queries(client_p); | |
398b6a73 | 198 | rb_bh_free(pclient_heap, client_p->preClient); |
212380e3 AC |
199 | client_p->preClient = NULL; |
200 | } | |
201 | ||
202 | static void | |
203 | free_local_client(struct Client *client_p) | |
204 | { | |
205 | s_assert(NULL != client_p); | |
206 | s_assert(&me != client_p); | |
207 | ||
208 | if(client_p->localClient == NULL) | |
209 | return; | |
210 | ||
211 | /* | |
212 | * clean up extra sockets from P-lines which have been discarded. | |
213 | */ | |
214 | if(client_p->localClient->listener) | |
215 | { | |
216 | s_assert(0 < client_p->localClient->listener->ref_count); | |
217 | if(0 == --client_p->localClient->listener->ref_count | |
218 | && !client_p->localClient->listener->active) | |
219 | free_listener(client_p->localClient->listener); | |
220 | client_p->localClient->listener = 0; | |
221 | } | |
222 | ||
c6d72037 VY |
223 | if(client_p->localClient->F != NULL) |
224 | { | |
225 | del_from_cli_fd_hash(client_p); | |
3b2ebd04 | 226 | rb_close(client_p->localClient->F); |
c6d72037 | 227 | } |
212380e3 AC |
228 | |
229 | if(client_p->localClient->passwd) | |
230 | { | |
231 | memset(client_p->localClient->passwd, 0, | |
232 | strlen(client_p->localClient->passwd)); | |
637c4932 | 233 | rb_free(client_p->localClient->passwd); |
212380e3 AC |
234 | } |
235 | ||
637c4932 VY |
236 | rb_free(client_p->localClient->challenge); |
237 | rb_free(client_p->localClient->fullcaps); | |
238 | rb_free(client_p->localClient->opername); | |
239 | rb_free(client_p->localClient->mangledhost); | |
212380e3 | 240 | |
c6d72037 VY |
241 | ssld_decrement_clicount(client_p->localClient->ssl_ctl); |
242 | ||
398b6a73 | 243 | rb_bh_free(lclient_heap, client_p->localClient); |
212380e3 AC |
244 | client_p->localClient = NULL; |
245 | } | |
246 | ||
247 | void | |
248 | free_client(struct Client *client_p) | |
249 | { | |
250 | s_assert(NULL != client_p); | |
251 | s_assert(&me != client_p); | |
252 | free_local_client(client_p); | |
253 | free_pre_client(client_p); | |
398b6a73 | 254 | rb_bh_free(client_heap, client_p); |
212380e3 AC |
255 | } |
256 | ||
257 | /* | |
258 | * check_pings - go through the local client list and check activity | |
259 | * kill off stuff that should die | |
260 | * | |
261 | * inputs - NOT USED (from event) | |
262 | * output - next time_t when check_pings() should be called again | |
263 | * side effects - | |
264 | * | |
265 | * | |
266 | * A PING can be sent to clients as necessary. | |
267 | * | |
268 | * Client/Server ping outs are handled. | |
269 | */ | |
270 | ||
271 | /* | |
272 | * Addon from adrian. We used to call this after nextping seconds, | |
273 | * however I've changed it to run once a second. This is only for | |
274 | * PING timeouts, not K/etc-line checks (thanks dianora!). Having it | |
275 | * run once a second makes life a lot easier - when a new client connects | |
276 | * and they need a ping in 4 seconds, if nextping was set to 20 seconds | |
277 | * we end up waiting 20 seconds. This is stupid. :-) | |
278 | * I will optimise (hah!) check_pings() once I've finished working on | |
279 | * tidying up other network IO evilnesses. | |
280 | * -- adrian | |
281 | */ | |
282 | ||
283 | static void | |
284 | check_pings(void *notused) | |
285 | { | |
286 | check_pings_list(&lclient_list); | |
287 | check_pings_list(&serv_list); | |
288 | check_unknowns_list(&unknown_list); | |
289 | } | |
290 | ||
291 | /* | |
292 | * Check_pings_list() | |
293 | * | |
294 | * inputs - pointer to list to check | |
295 | * output - NONE | |
296 | * side effects - | |
297 | */ | |
298 | static void | |
330fc5c1 | 299 | check_pings_list(rb_dlink_list * list) |
212380e3 AC |
300 | { |
301 | char scratch[32]; /* way too generous but... */ | |
302 | struct Client *client_p; /* current local client_p being examined */ | |
303 | int ping = 0; /* ping time value from client */ | |
637c4932 | 304 | rb_dlink_node *ptr, *next_ptr; |
212380e3 | 305 | |
637c4932 | 306 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head) |
212380e3 AC |
307 | { |
308 | client_p = ptr->data; | |
309 | ||
212380e3 AC |
310 | if(!MyConnect(client_p) || IsDead(client_p)) |
311 | continue; | |
312 | ||
e2a98043 | 313 | ping = get_client_ping(client_p); |
212380e3 | 314 | |
e3354945 | 315 | if(ping < (rb_current_time() - client_p->localClient->lasttime)) |
212380e3 AC |
316 | { |
317 | /* | |
318 | * If the client/server hasnt talked to us in 2*ping seconds | |
319 | * and it has a ping time, then close its connection. | |
320 | */ | |
e3354945 | 321 | if(((rb_current_time() - client_p->localClient->lasttime) >= (2 * ping) |
212380e3 AC |
322 | && (client_p->flags & FLAGS_PINGSENT))) |
323 | { | |
e2a98043 | 324 | if(IsServer(client_p)) |
212380e3 | 325 | { |
e2a98043 | 326 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
212380e3 AC |
327 | "No response from %s, closing link", |
328 | get_server_name(client_p, HIDE_IP)); | |
329 | ilog(L_SERVER, | |
330 | "No response from %s, closing link", | |
331 | log_client_name(client_p, HIDE_IP)); | |
332 | } | |
b2f0da88 | 333 | (void) rb_snprintf(scratch, sizeof(scratch), |
212380e3 | 334 | "Ping timeout: %d seconds", |
e3354945 | 335 | (int) (rb_current_time() - client_p->localClient->lasttime)); |
212380e3 AC |
336 | |
337 | exit_client(client_p, client_p, &me, scratch); | |
338 | continue; | |
339 | } | |
340 | else if((client_p->flags & FLAGS_PINGSENT) == 0) | |
341 | { | |
342 | /* | |
343 | * if we havent PINGed the connection and we havent | |
344 | * heard from it in a while, PING it to make sure | |
345 | * it is still alive. | |
346 | */ | |
347 | client_p->flags |= FLAGS_PINGSENT; | |
348 | /* not nice but does the job */ | |
e3354945 | 349 | client_p->localClient->lasttime = rb_current_time() - ping; |
212380e3 AC |
350 | sendto_one(client_p, "PING :%s", me.name); |
351 | } | |
352 | } | |
353 | /* ping_timeout: */ | |
354 | ||
355 | } | |
356 | } | |
357 | ||
358 | /* | |
359 | * check_unknowns_list | |
360 | * | |
361 | * inputs - pointer to list of unknown clients | |
362 | * output - NONE | |
363 | * side effects - unknown clients get marked for termination after n seconds | |
364 | */ | |
365 | static void | |
330fc5c1 | 366 | check_unknowns_list(rb_dlink_list * list) |
212380e3 | 367 | { |
637c4932 | 368 | rb_dlink_node *ptr, *next_ptr; |
212380e3 | 369 | struct Client *client_p; |
35cf4c79 | 370 | int timeout; |
212380e3 | 371 | |
637c4932 | 372 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head) |
212380e3 AC |
373 | { |
374 | client_p = ptr->data; | |
375 | ||
376 | if(IsDead(client_p) || IsClosing(client_p)) | |
377 | continue; | |
378 | ||
379 | /* | |
380 | * Check UNKNOWN connections - if they have been in this state | |
381 | * for > 30s, close them. | |
382 | */ | |
383 | ||
35cf4c79 | 384 | timeout = IsAnyServer(client_p) ? ConfigFileEntry.connect_timeout : 30; |
e3354945 | 385 | if((rb_current_time() - client_p->localClient->firsttime) > timeout) |
35cf4c79 JT |
386 | { |
387 | if(IsAnyServer(client_p)) | |
388 | { | |
389 | sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL, | |
390 | "No response from %s, closing link", | |
391 | get_server_name(client_p, HIDE_IP)); | |
392 | ilog(L_SERVER, | |
393 | "No response from %s, closing link", | |
394 | log_client_name(client_p, HIDE_IP)); | |
395 | } | |
212380e3 | 396 | exit_client(client_p, client_p, &me, "Connection timed out"); |
35cf4c79 | 397 | } |
212380e3 AC |
398 | } |
399 | } | |
400 | ||
401 | static void | |
402 | notify_banned_client(struct Client *client_p, struct ConfItem *aconf, int ban) | |
403 | { | |
404 | static const char conn_closed[] = "Connection closed"; | |
405 | static const char d_lined[] = "D-lined"; | |
406 | static const char k_lined[] = "K-lined"; | |
407 | static const char g_lined[] = "G-lined"; | |
408 | const char *reason = NULL; | |
409 | const char *exit_reason = conn_closed; | |
410 | ||
411 | if(ConfigFileEntry.kline_with_reason && !EmptyString(aconf->passwd)) | |
412 | { | |
413 | reason = aconf->passwd; | |
414 | exit_reason = aconf->passwd; | |
415 | } | |
416 | else | |
417 | { | |
418 | switch (aconf->status) | |
419 | { | |
420 | case D_LINED: | |
421 | reason = d_lined; | |
422 | break; | |
423 | case G_LINED: | |
424 | reason = g_lined; | |
425 | break; | |
426 | default: | |
427 | reason = k_lined; | |
428 | break; | |
429 | } | |
430 | } | |
431 | ||
432 | if(ban == D_LINED && !IsPerson(client_p)) | |
433 | sendto_one(client_p, "NOTICE DLINE :*** You have been D-lined"); | |
434 | else | |
435 | sendto_one(client_p, form_str(ERR_YOUREBANNEDCREEP), | |
436 | me.name, client_p->name, reason); | |
437 | ||
438 | exit_client(client_p, client_p, &me, | |
439 | EmptyString(ConfigFileEntry.kline_reason) ? exit_reason : | |
440 | ConfigFileEntry.kline_reason); | |
441 | } | |
442 | ||
443 | /* | |
444 | * check_banned_lines | |
445 | * inputs - NONE | |
446 | * output - NONE | |
447 | * side effects - Check all connections for a pending k/d/gline against the | |
448 | * client, exit the client if found. | |
449 | */ | |
450 | void | |
451 | check_banned_lines(void) | |
452 | { | |
453 | struct Client *client_p; /* current local client_p being examined */ | |
454 | struct ConfItem *aconf = NULL; | |
637c4932 | 455 | rb_dlink_node *ptr, *next_ptr; |
212380e3 | 456 | |
637c4932 | 457 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head) |
212380e3 AC |
458 | { |
459 | client_p = ptr->data; | |
460 | ||
461 | if(IsMe(client_p)) | |
462 | continue; | |
463 | ||
464 | /* if there is a returned struct ConfItem then kill it */ | |
54ac8b60 | 465 | if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip, client_p->localClient->ip.ss_family))) |
212380e3 AC |
466 | { |
467 | if(aconf->status & CONF_EXEMPTDLINE) | |
468 | continue; | |
469 | ||
470 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
471 | "DLINE active for %s", | |
472 | get_client_name(client_p, HIDE_IP)); | |
473 | ||
474 | notify_banned_client(client_p, aconf, D_LINED); | |
475 | continue; /* and go examine next fd/client_p */ | |
476 | } | |
477 | ||
478 | if(!IsPerson(client_p)) | |
479 | continue; | |
480 | ||
481 | if((aconf = find_kline(client_p)) != NULL) | |
482 | { | |
483 | if(IsExemptKline(client_p)) | |
484 | { | |
485 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
486 | "KLINE over-ruled for %s, client is kline_exempt [%s@%s]", | |
487 | get_client_name(client_p, HIDE_IP), | |
488 | aconf->user, aconf->host); | |
489 | continue; | |
490 | } | |
491 | ||
492 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
493 | "KLINE active for %s", | |
494 | get_client_name(client_p, HIDE_IP)); | |
495 | notify_banned_client(client_p, aconf, K_LINED); | |
496 | continue; | |
497 | } | |
498 | else if((aconf = find_gline(client_p)) != NULL) | |
499 | { | |
500 | if(IsExemptKline(client_p)) | |
501 | { | |
502 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
503 | "GLINE over-ruled for %s, client is kline_exempt [%s@%s]", | |
504 | get_client_name(client_p, HIDE_IP), | |
505 | aconf->user, aconf->host); | |
506 | continue; | |
507 | } | |
508 | ||
509 | if(IsExemptGline(client_p)) | |
510 | { | |
511 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
512 | "GLINE over-ruled for %s, client is gline_exempt [%s@%s]", | |
513 | get_client_name(client_p, HIDE_IP), | |
514 | aconf->user, aconf->host); | |
515 | continue; | |
516 | } | |
517 | ||
518 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
519 | "GLINE active for %s", | |
520 | get_client_name(client_p, HIDE_IP)); | |
521 | ||
522 | notify_banned_client(client_p, aconf, G_LINED); | |
523 | continue; | |
524 | } | |
525 | else if((aconf = find_xline(client_p->info, 1)) != NULL) | |
526 | { | |
527 | if(IsExemptKline(client_p)) | |
528 | { | |
529 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
530 | "XLINE over-ruled for %s, client is kline_exempt [%s]", | |
531 | get_client_name(client_p, HIDE_IP), | |
532 | aconf->name); | |
533 | continue; | |
534 | } | |
535 | ||
536 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "XLINE active for %s", | |
537 | get_client_name(client_p, HIDE_IP)); | |
538 | ||
539 | (void) exit_client(client_p, client_p, &me, "Bad user info"); | |
540 | continue; | |
541 | } | |
542 | } | |
543 | ||
544 | /* also check the unknowns list for new dlines */ | |
637c4932 | 545 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head) |
212380e3 AC |
546 | { |
547 | client_p = ptr->data; | |
548 | ||
54ac8b60 | 549 | if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family))) |
212380e3 AC |
550 | { |
551 | if(aconf->status & CONF_EXEMPTDLINE) | |
552 | continue; | |
553 | ||
554 | notify_banned_client(client_p, aconf, D_LINED); | |
555 | } | |
556 | } | |
557 | ||
558 | } | |
559 | ||
560 | /* check_klines_event() | |
561 | * | |
562 | * inputs - | |
563 | * outputs - | |
564 | * side effects - check_klines() is called, kline_queued unset | |
565 | */ | |
566 | void | |
567 | check_klines_event(void *unused) | |
568 | { | |
569 | kline_queued = 0; | |
570 | check_klines(); | |
571 | } | |
572 | ||
573 | /* check_klines | |
574 | * | |
575 | * inputs - | |
576 | * outputs - | |
577 | * side effects - all clients will be checked for klines | |
578 | */ | |
579 | void | |
580 | check_klines(void) | |
581 | { | |
582 | struct Client *client_p; | |
583 | struct ConfItem *aconf; | |
330fc5c1 | 584 | rb_dlink_node *ptr; |
637c4932 | 585 | rb_dlink_node *next_ptr; |
212380e3 | 586 | |
637c4932 | 587 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head) |
212380e3 AC |
588 | { |
589 | client_p = ptr->data; | |
590 | ||
591 | if(IsMe(client_p) || !IsPerson(client_p)) | |
592 | continue; | |
593 | ||
594 | if((aconf = find_kline(client_p)) != NULL) | |
595 | { | |
596 | if(IsExemptKline(client_p)) | |
597 | { | |
598 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
599 | "KLINE over-ruled for %s, client is kline_exempt", | |
600 | get_client_name(client_p, HIDE_IP)); | |
601 | continue; | |
602 | } | |
603 | ||
604 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
605 | "KLINE active for %s", | |
606 | get_client_name(client_p, HIDE_IP)); | |
607 | ||
608 | notify_banned_client(client_p, aconf, K_LINED); | |
609 | continue; | |
610 | } | |
611 | } | |
612 | } | |
613 | ||
614 | /* check_glines() | |
615 | * | |
616 | * inputs - | |
617 | * outputs - | |
618 | * side effects - all clients will be checked for glines | |
619 | */ | |
620 | void | |
621 | check_glines(void) | |
622 | { | |
623 | struct Client *client_p; | |
624 | struct ConfItem *aconf; | |
330fc5c1 | 625 | rb_dlink_node *ptr; |
637c4932 | 626 | rb_dlink_node *next_ptr; |
212380e3 | 627 | |
637c4932 | 628 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head) |
212380e3 AC |
629 | { |
630 | client_p = ptr->data; | |
631 | ||
632 | if(IsMe(client_p) || !IsPerson(client_p)) | |
633 | continue; | |
634 | ||
635 | if((aconf = find_gline(client_p)) != NULL) | |
636 | { | |
637 | if(IsExemptKline(client_p)) | |
638 | { | |
639 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
640 | "GLINE over-ruled for %s, client is kline_exempt", | |
641 | get_client_name(client_p, HIDE_IP)); | |
642 | continue; | |
643 | } | |
644 | ||
645 | if(IsExemptGline(client_p)) | |
646 | { | |
647 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
648 | "GLINE over-ruled for %s, client is gline_exempt", | |
649 | get_client_name(client_p, HIDE_IP)); | |
650 | continue; | |
651 | } | |
652 | ||
653 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
654 | "GLINE active for %s", | |
655 | get_client_name(client_p, HIDE_IP)); | |
656 | ||
657 | notify_banned_client(client_p, aconf, K_LINED); | |
658 | continue; | |
659 | } | |
660 | } | |
661 | } | |
662 | ||
663 | /* check_dlines() | |
664 | * | |
665 | * inputs - | |
666 | * outputs - | |
667 | * side effects - all clients will be checked for dlines | |
668 | */ | |
669 | void | |
670 | check_dlines(void) | |
671 | { | |
672 | struct Client *client_p; | |
673 | struct ConfItem *aconf; | |
330fc5c1 | 674 | rb_dlink_node *ptr; |
637c4932 | 675 | rb_dlink_node *next_ptr; |
212380e3 | 676 | |
637c4932 | 677 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head) |
212380e3 AC |
678 | { |
679 | client_p = ptr->data; | |
680 | ||
681 | if(IsMe(client_p)) | |
682 | continue; | |
683 | ||
54ac8b60 | 684 | if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)) != NULL) |
212380e3 AC |
685 | { |
686 | if(aconf->status & CONF_EXEMPTDLINE) | |
687 | continue; | |
688 | ||
689 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
690 | "DLINE active for %s", | |
691 | get_client_name(client_p, HIDE_IP)); | |
692 | ||
693 | notify_banned_client(client_p, aconf, D_LINED); | |
694 | continue; | |
695 | } | |
696 | } | |
697 | ||
698 | /* dlines need to be checked against unknowns too */ | |
637c4932 | 699 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head) |
212380e3 AC |
700 | { |
701 | client_p = ptr->data; | |
702 | ||
54ac8b60 | 703 | if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)) != NULL) |
212380e3 AC |
704 | { |
705 | if(aconf->status & CONF_EXEMPTDLINE) | |
706 | continue; | |
707 | ||
708 | notify_banned_client(client_p, aconf, D_LINED); | |
709 | } | |
710 | } | |
711 | } | |
712 | ||
713 | /* check_xlines | |
714 | * | |
715 | * inputs - | |
716 | * outputs - | |
717 | * side effects - all clients will be checked for xlines | |
718 | */ | |
719 | void | |
720 | check_xlines(void) | |
721 | { | |
722 | struct Client *client_p; | |
723 | struct ConfItem *aconf; | |
330fc5c1 | 724 | rb_dlink_node *ptr; |
637c4932 | 725 | rb_dlink_node *next_ptr; |
212380e3 | 726 | |
637c4932 | 727 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head) |
212380e3 AC |
728 | { |
729 | client_p = ptr->data; | |
730 | ||
731 | if(IsMe(client_p) || !IsPerson(client_p)) | |
732 | continue; | |
733 | ||
734 | if((aconf = find_xline(client_p->info, 1)) != NULL) | |
735 | { | |
736 | if(IsExemptKline(client_p)) | |
737 | { | |
738 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
739 | "XLINE over-ruled for %s, client is kline_exempt", | |
740 | get_client_name(client_p, HIDE_IP)); | |
741 | continue; | |
742 | } | |
743 | ||
744 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "XLINE active for %s", | |
745 | get_client_name(client_p, HIDE_IP)); | |
746 | ||
747 | (void) exit_client(client_p, client_p, &me, "Bad user info"); | |
748 | continue; | |
749 | } | |
750 | } | |
751 | } | |
752 | ||
753 | /* | |
754 | * update_client_exit_stats | |
755 | * | |
756 | * input - pointer to client | |
757 | * output - NONE | |
758 | * side effects - | |
759 | */ | |
760 | static void | |
761 | update_client_exit_stats(struct Client *client_p) | |
762 | { | |
763 | if(IsServer(client_p)) | |
764 | { | |
765 | sendto_realops_snomask(SNO_EXTERNAL, L_ALL, | |
766 | "Server %s split from %s", | |
767 | client_p->name, client_p->servptr->name); | |
768 | if(HasSentEob(client_p)) | |
769 | eob_count--; | |
770 | } | |
771 | else if(IsClient(client_p)) | |
772 | { | |
773 | --Count.total; | |
774 | if(IsOper(client_p)) | |
775 | --Count.oper; | |
776 | if(IsInvisible(client_p)) | |
777 | --Count.invisi; | |
778 | } | |
779 | ||
780 | if(splitchecking && !splitmode) | |
781 | check_splitmode(NULL); | |
782 | } | |
783 | ||
784 | /* | |
785 | * release_client_state | |
786 | * | |
787 | * input - pointer to client to release | |
788 | * output - NONE | |
789 | * side effects - | |
790 | */ | |
791 | static void | |
792 | release_client_state(struct Client *client_p) | |
793 | { | |
794 | if(client_p->user != NULL) | |
795 | { | |
796 | free_user(client_p->user, client_p); /* try this here */ | |
797 | } | |
798 | if(client_p->serv) | |
799 | { | |
800 | if(client_p->serv->user != NULL) | |
801 | free_user(client_p->serv->user, client_p); | |
802 | if(client_p->serv->fullcaps) | |
637c4932 VY |
803 | rb_free(client_p->serv->fullcaps); |
804 | rb_free(client_p->serv); | |
212380e3 AC |
805 | } |
806 | } | |
807 | ||
808 | /* | |
809 | * remove_client_from_list | |
810 | * inputs - point to client to remove | |
811 | * output - NONE | |
812 | * side effects - taken the code from ExitOneClient() for this | |
813 | * and placed it here. - avalon | |
814 | */ | |
815 | static void | |
816 | remove_client_from_list(struct Client *client_p) | |
817 | { | |
818 | s_assert(NULL != client_p); | |
819 | ||
820 | if(client_p == NULL) | |
821 | return; | |
822 | ||
823 | /* A client made with make_client() | |
824 | * is on the unknown_list until removed. | |
825 | * If it =does= happen to exit before its removed from that list | |
826 | * and its =not= on the global_client_list, it will core here. | |
827 | * short circuit that case now -db | |
828 | */ | |
829 | if(client_p->node.prev == NULL && client_p->node.next == NULL) | |
830 | return; | |
831 | ||
330fc5c1 | 832 | rb_dlinkDelete(&client_p->node, &global_client_list); |
212380e3 AC |
833 | |
834 | update_client_exit_stats(client_p); | |
835 | } | |
836 | ||
837 | ||
838 | /* | |
839 | * find_person - find person by (nick)name. | |
840 | * inputs - pointer to name | |
841 | * output - return client pointer | |
842 | * side effects - | |
843 | */ | |
844 | struct Client * | |
845 | find_person(const char *name) | |
846 | { | |
847 | struct Client *c2ptr; | |
848 | ||
849 | c2ptr = find_client(name); | |
850 | ||
851 | if(c2ptr && IsPerson(c2ptr)) | |
852 | return (c2ptr); | |
853 | return (NULL); | |
854 | } | |
855 | ||
856 | struct Client * | |
857 | find_named_person(const char *name) | |
858 | { | |
859 | struct Client *c2ptr; | |
860 | ||
861 | c2ptr = find_named_client(name); | |
862 | ||
863 | if(c2ptr && IsPerson(c2ptr)) | |
864 | return (c2ptr); | |
865 | return (NULL); | |
866 | } | |
867 | ||
868 | ||
869 | /* | |
870 | * find_chasing - find the client structure for a nick name (user) | |
871 | * using history mechanism if necessary. If the client is not found, | |
872 | * an error message (NO SUCH NICK) is generated. If the client was found | |
873 | * through the history, chasing will be 1 and otherwise 0. | |
874 | */ | |
875 | struct Client * | |
876 | find_chasing(struct Client *source_p, const char *user, int *chasing) | |
877 | { | |
878 | struct Client *who; | |
879 | ||
880 | if(MyClient(source_p)) | |
881 | who = find_named_person(user); | |
882 | else | |
883 | who = find_person(user); | |
884 | ||
885 | if(chasing) | |
886 | *chasing = 0; | |
887 | ||
888 | if(who || IsDigit(*user)) | |
889 | return who; | |
890 | ||
891 | if(!(who = get_history(user, (long) KILLCHASETIMELIMIT))) | |
892 | { | |
893 | sendto_one_numeric(source_p, ERR_NOSUCHNICK, | |
894 | form_str(ERR_NOSUCHNICK), user); | |
895 | return (NULL); | |
896 | } | |
897 | if(chasing) | |
898 | *chasing = 1; | |
899 | return who; | |
900 | } | |
901 | ||
902 | /* | |
903 | * get_client_name - Return the name of the client | |
904 | * for various tracking and | |
905 | * admin purposes. The main purpose of this function is to | |
906 | * return the "socket host" name of the client, if that | |
907 | * differs from the advertised name (other than case). | |
908 | * But, this can be used to any client structure. | |
909 | * | |
910 | * NOTE 1: | |
911 | * Watch out the allocation of "nbuf", if either source_p->name | |
912 | * or source_p->sockhost gets changed into pointers instead of | |
913 | * directly allocated within the structure... | |
914 | * | |
915 | * NOTE 2: | |
916 | * Function return either a pointer to the structure (source_p) or | |
917 | * to internal buffer (nbuf). *NEVER* use the returned pointer | |
918 | * to modify what it points!!! | |
919 | */ | |
920 | ||
921 | const char * | |
922 | get_client_name(struct Client *client, int showip) | |
923 | { | |
924 | static char nbuf[HOSTLEN * 2 + USERLEN + 5]; | |
925 | ||
926 | s_assert(NULL != client); | |
927 | if(client == NULL) | |
928 | return NULL; | |
929 | ||
930 | if(MyConnect(client)) | |
931 | { | |
932 | if(!irccmp(client->name, client->host)) | |
933 | return client->name; | |
934 | ||
935 | if(ConfigFileEntry.hide_spoof_ips && | |
936 | showip == SHOW_IP && IsIPSpoof(client)) | |
937 | showip = MASK_IP; | |
938 | #ifdef HIDE_SERVERS_IPS | |
939 | if(IsAnyServer(client)) | |
940 | showip = MASK_IP; | |
941 | #endif | |
942 | ||
943 | /* And finally, let's get the host information, ip or name */ | |
944 | switch (showip) | |
945 | { | |
946 | case SHOW_IP: | |
b2f0da88 | 947 | rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", |
212380e3 AC |
948 | client->name, client->username, |
949 | client->sockhost); | |
950 | break; | |
951 | case MASK_IP: | |
b2f0da88 | 952 | rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]", |
212380e3 AC |
953 | client->name, client->username); |
954 | break; | |
955 | default: | |
b2f0da88 | 956 | rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", |
212380e3 AC |
957 | client->name, client->username, client->host); |
958 | } | |
959 | return nbuf; | |
960 | } | |
961 | ||
962 | /* As pointed out by Adel Mezibra | |
963 | * Neph|l|m@EFnet. Was missing a return here. | |
964 | */ | |
965 | return client->name; | |
966 | } | |
967 | ||
968 | const char * | |
969 | get_server_name(struct Client *target_p, int showip) | |
970 | { | |
971 | static char nbuf[HOSTLEN * 2 + USERLEN + 5]; | |
972 | ||
973 | if(target_p == NULL) | |
974 | return NULL; | |
975 | ||
976 | if(!MyConnect(target_p) || !irccmp(target_p->name, target_p->host)) | |
977 | return target_p->name; | |
978 | ||
979 | #ifdef HIDE_SERVERS_IPS | |
980 | if(EmptyString(target_p->name)) | |
981 | { | |
b2f0da88 | 982 | rb_snprintf(nbuf, sizeof(nbuf), "[%s@255.255.255.255]", |
212380e3 AC |
983 | target_p->username); |
984 | return nbuf; | |
985 | } | |
986 | else | |
987 | return target_p->name; | |
988 | #endif | |
989 | ||
990 | switch (showip) | |
991 | { | |
992 | case SHOW_IP: | |
b2f0da88 | 993 | rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", |
212380e3 AC |
994 | target_p->name, target_p->username, |
995 | target_p->sockhost); | |
996 | break; | |
997 | ||
998 | case MASK_IP: | |
b2f0da88 | 999 | rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]", |
212380e3 AC |
1000 | target_p->name, target_p->username); |
1001 | ||
1002 | default: | |
b2f0da88 | 1003 | rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", |
212380e3 AC |
1004 | target_p->name, target_p->username, |
1005 | target_p->host); | |
1006 | } | |
1007 | ||
1008 | return nbuf; | |
1009 | } | |
1010 | ||
1011 | /* log_client_name() | |
1012 | * | |
1013 | * This version is the same as get_client_name, but doesnt contain the | |
1014 | * code that will hide IPs always. This should be used for logfiles. | |
1015 | */ | |
1016 | const char * | |
1017 | log_client_name(struct Client *target_p, int showip) | |
1018 | { | |
1019 | static char nbuf[HOSTLEN * 2 + USERLEN + 5]; | |
1020 | ||
1021 | if(target_p == NULL) | |
1022 | return NULL; | |
1023 | ||
1024 | if(MyConnect(target_p)) | |
1025 | { | |
1026 | if(irccmp(target_p->name, target_p->host) == 0) | |
1027 | return target_p->name; | |
1028 | ||
1029 | switch (showip) | |
1030 | { | |
1031 | case SHOW_IP: | |
b2f0da88 | 1032 | rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", target_p->name, |
212380e3 AC |
1033 | target_p->username, target_p->sockhost); |
1034 | break; | |
1035 | ||
1036 | case MASK_IP: | |
b2f0da88 | 1037 | rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]", |
212380e3 AC |
1038 | target_p->name, target_p->username); |
1039 | ||
1040 | default: | |
b2f0da88 | 1041 | rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", target_p->name, |
212380e3 AC |
1042 | target_p->username, target_p->host); |
1043 | } | |
1044 | ||
1045 | return nbuf; | |
1046 | } | |
1047 | ||
1048 | return target_p->name; | |
1049 | } | |
1050 | ||
1051 | /* is_remote_connect - Returns whether a server was /connect'ed by a remote | |
1052 | * oper (send notices netwide) */ | |
1053 | int | |
1054 | is_remote_connect(struct Client *client_p) | |
1055 | { | |
1056 | struct Client *oper; | |
1057 | ||
1058 | if (client_p->serv == NULL) | |
1059 | return FALSE; | |
1060 | oper = find_named_person(client_p->serv->by); | |
1061 | return oper != NULL && IsOper(oper) && !MyConnect(oper); | |
1062 | } | |
1063 | ||
1064 | static void | |
1065 | free_exited_clients(void *unused) | |
1066 | { | |
330fc5c1 | 1067 | rb_dlink_node *ptr, *next; |
212380e3 AC |
1068 | struct Client *target_p; |
1069 | ||
5cefa1d6 | 1070 | RB_DLINK_FOREACH_SAFE(ptr, next, dead_list.head) |
212380e3 AC |
1071 | { |
1072 | target_p = ptr->data; | |
1073 | ||
1074 | #ifdef DEBUG_EXITED_CLIENTS | |
1075 | { | |
1076 | struct abort_client *abt; | |
330fc5c1 | 1077 | rb_dlink_node *aptr; |
212380e3 AC |
1078 | int found = 0; |
1079 | ||
5cefa1d6 | 1080 | RB_DLINK_FOREACH(aptr, abort_list.head) |
212380e3 AC |
1081 | { |
1082 | abt = aptr->data; | |
1083 | if(abt->client == target_p) | |
1084 | { | |
1085 | s_assert(0); | |
1086 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
1087 | "On abort_list: %s stat: %u flags: %u/%u handler: %c", | |
1088 | target_p->name, (unsigned int) target_p->status, | |
1089 | target_p->flags, target_p->flags2, target_p->handler); | |
1090 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
f80a1823 | 1091 | "Please report this to the charybdis developers!"); |
212380e3 AC |
1092 | found++; |
1093 | } | |
1094 | } | |
1095 | ||
1096 | if(found) | |
1097 | { | |
330fc5c1 | 1098 | rb_dlinkDestroy(ptr, &dead_list); |
212380e3 AC |
1099 | continue; |
1100 | } | |
1101 | } | |
1102 | #endif | |
1103 | ||
1104 | if(ptr->data == NULL) | |
1105 | { | |
1106 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
1107 | "Warning: null client on dead_list!"); | |
330fc5c1 | 1108 | rb_dlinkDestroy(ptr, &dead_list); |
212380e3 AC |
1109 | continue; |
1110 | } | |
1111 | release_client_state(target_p); | |
1112 | free_client(target_p); | |
330fc5c1 | 1113 | rb_dlinkDestroy(ptr, &dead_list); |
212380e3 AC |
1114 | } |
1115 | ||
1116 | #ifdef DEBUG_EXITED_CLIENTS | |
5cefa1d6 | 1117 | RB_DLINK_FOREACH_SAFE(ptr, next, dead_remote_list.head) |
212380e3 AC |
1118 | { |
1119 | target_p = ptr->data; | |
1120 | ||
1121 | if(ptr->data == NULL) | |
1122 | { | |
1123 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
1124 | "Warning: null client on dead_list!"); | |
330fc5c1 | 1125 | rb_dlinkDestroy(ptr, &dead_list); |
212380e3 AC |
1126 | continue; |
1127 | } | |
1128 | release_client_state(target_p); | |
1129 | free_client(target_p); | |
330fc5c1 | 1130 | rb_dlinkDestroy(ptr, &dead_remote_list); |
212380e3 AC |
1131 | } |
1132 | #endif | |
1133 | ||
1134 | } | |
1135 | ||
1136 | /* | |
1137 | ** Recursively send QUITs and SQUITs for source_p and all its dependent clients | |
1138 | ** and servers to those servers that need them. A server needs the client | |
1139 | ** QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it | |
1140 | ** isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once | |
1141 | ** a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending | |
1142 | ** on that one -orabidoo | |
1143 | */ | |
1144 | static void | |
1145 | recurse_send_quits(struct Client *client_p, struct Client *source_p, | |
1146 | struct Client *to, const char *comment1, | |
1147 | const char *comment) | |
1148 | { | |
1149 | struct Client *target_p; | |
330fc5c1 | 1150 | rb_dlink_node *ptr, *ptr_next; |
212380e3 AC |
1151 | /* If this server can handle quit storm (QS) removal |
1152 | * of dependents, just send the SQUIT | |
1153 | */ | |
1154 | ||
1155 | if(IsCapable(to, CAP_QS)) | |
1156 | { | |
1157 | sendto_one(to, "SQUIT %s :%s", | |
1158 | get_id(source_p, to), comment); | |
1159 | } | |
1160 | else | |
1161 | { | |
5cefa1d6 | 1162 | RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head) |
212380e3 AC |
1163 | { |
1164 | target_p = ptr->data; | |
1165 | sendto_one(to, ":%s QUIT :%s", target_p->name, comment1); | |
1166 | } | |
5cefa1d6 | 1167 | RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head) |
212380e3 AC |
1168 | { |
1169 | target_p = ptr->data; | |
1170 | recurse_send_quits(client_p, target_p, to, comment1, comment); | |
1171 | } | |
1172 | sendto_one(to, "SQUIT %s :%s", source_p->name, comment); | |
1173 | } | |
1174 | } | |
1175 | ||
1176 | /* | |
1177 | ** Remove all clients that depend on source_p; assumes all (S)QUITs have | |
1178 | ** already been sent. we make sure to exit a server's dependent clients | |
1179 | ** and servers before the server itself; exit_one_client takes care of | |
1180 | ** actually removing things off llists. tweaked from +CSr31 -orabidoo | |
1181 | */ | |
1182 | /* | |
1183 | * added sanity test code.... source_p->serv might be NULL... | |
1184 | */ | |
1185 | static void | |
1186 | recurse_remove_clients(struct Client *source_p, const char *comment) | |
1187 | { | |
1188 | struct Client *target_p; | |
330fc5c1 | 1189 | rb_dlink_node *ptr, *ptr_next; |
212380e3 AC |
1190 | |
1191 | if(IsMe(source_p)) | |
1192 | return; | |
1193 | ||
1194 | if(source_p->serv == NULL) /* oooops. uh this is actually a major bug */ | |
1195 | return; | |
1196 | ||
1197 | /* this is very ugly, but it saves cpu :P */ | |
1198 | if(ConfigFileEntry.nick_delay > 0) | |
1199 | { | |
5cefa1d6 | 1200 | RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head) |
212380e3 AC |
1201 | { |
1202 | target_p = ptr->data; | |
1203 | target_p->flags |= FLAGS_KILLED; | |
1204 | add_nd_entry(target_p->name); | |
1205 | ||
1206 | if(!IsDead(target_p) && !IsClosing(target_p)) | |
1207 | exit_remote_client(NULL, target_p, &me, comment); | |
1208 | } | |
1209 | } | |
1210 | else | |
1211 | { | |
5cefa1d6 | 1212 | RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head) |
212380e3 AC |
1213 | { |
1214 | target_p = ptr->data; | |
1215 | target_p->flags |= FLAGS_KILLED; | |
1216 | ||
1217 | if(!IsDead(target_p) && !IsClosing(target_p)) | |
1218 | exit_remote_client(NULL, target_p, &me, comment); | |
1219 | } | |
1220 | } | |
1221 | ||
5cefa1d6 | 1222 | RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head) |
212380e3 AC |
1223 | { |
1224 | target_p = ptr->data; | |
1225 | recurse_remove_clients(target_p, comment); | |
1226 | qs_server(NULL, target_p, &me, comment); | |
1227 | } | |
1228 | } | |
1229 | ||
1230 | /* | |
1231 | ** Remove *everything* that depends on source_p, from all lists, and sending | |
1232 | ** all necessary QUITs and SQUITs. source_p itself is still on the lists, | |
1233 | ** and its SQUITs have been sent except for the upstream one -orabidoo | |
1234 | */ | |
1235 | static void | |
1236 | remove_dependents(struct Client *client_p, | |
1237 | struct Client *source_p, | |
1238 | struct Client *from, const char *comment, const char *comment1) | |
1239 | { | |
1240 | struct Client *to; | |
330fc5c1 | 1241 | rb_dlink_node *ptr, *next; |
212380e3 | 1242 | |
5cefa1d6 | 1243 | RB_DLINK_FOREACH_SAFE(ptr, next, serv_list.head) |
212380e3 AC |
1244 | { |
1245 | to = ptr->data; | |
1246 | ||
1247 | if(IsMe(to) || to == source_p->from || | |
1248 | (to == client_p && IsCapable(to, CAP_QS))) | |
1249 | continue; | |
1250 | ||
1251 | recurse_send_quits(client_p, source_p, to, comment1, comment); | |
1252 | } | |
1253 | ||
1254 | recurse_remove_clients(source_p, comment1); | |
1255 | } | |
1256 | ||
1257 | void | |
1258 | exit_aborted_clients(void *unused) | |
1259 | { | |
1260 | struct abort_client *abt; | |
330fc5c1 | 1261 | rb_dlink_node *ptr, *next; |
5cefa1d6 | 1262 | RB_DLINK_FOREACH_SAFE(ptr, next, abort_list.head) |
212380e3 AC |
1263 | { |
1264 | abt = ptr->data; | |
1265 | ||
1266 | #ifdef DEBUG_EXITED_CLIENTS | |
1267 | { | |
330fc5c1 | 1268 | if(rb_dlinkFind(abt->client, &dead_list)) |
212380e3 AC |
1269 | { |
1270 | s_assert(0); | |
1271 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
1272 | "On dead_list: %s stat: %u flags: %u/%u handler: %c", | |
1273 | abt->client->name, (unsigned int) abt->client->status, | |
1274 | abt->client->flags, abt->client->flags2, abt->client->handler); | |
1275 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
f80a1823 | 1276 | "Please report this to the charybdis developers!"); |
212380e3 AC |
1277 | continue; |
1278 | } | |
1279 | } | |
1280 | #endif | |
1281 | ||
1282 | s_assert(*((unsigned long*)abt->client) != 0xdeadbeef); /* This is lame but its a debug thing */ | |
330fc5c1 | 1283 | rb_dlinkDelete(ptr, &abort_list); |
212380e3 AC |
1284 | |
1285 | if(IsAnyServer(abt->client)) | |
1286 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
1287 | "Closing link to %s: %s", | |
1288 | get_server_name(abt->client, HIDE_IP), abt->notice); | |
1289 | ||
1290 | /* its no longer on abort list - we *must* remove | |
1291 | * FLAGS_CLOSING otherwise exit_client() will not run --fl | |
1292 | */ | |
1293 | abt->client->flags &= ~FLAGS_CLOSING; | |
1294 | exit_client(abt->client, abt->client, &me, abt->notice); | |
637c4932 | 1295 | rb_free(abt); |
212380e3 AC |
1296 | } |
1297 | } | |
1298 | ||
1299 | ||
1300 | /* | |
1301 | * dead_link - Adds client to a list of clients that need an exit_client() | |
1302 | * | |
1303 | */ | |
1304 | void | |
1305 | dead_link(struct Client *client_p) | |
1306 | { | |
1307 | struct abort_client *abt; | |
1308 | ||
1309 | s_assert(!IsMe(client_p)); | |
1310 | if(IsDead(client_p) || IsClosing(client_p) || IsMe(client_p)) | |
1311 | return; | |
1312 | ||
eddc2ab6 | 1313 | abt = (struct abort_client *) rb_malloc(sizeof(struct abort_client)); |
212380e3 AC |
1314 | |
1315 | if(client_p->flags & FLAGS_SENDQEX) | |
1316 | strlcpy(abt->notice, "Max SendQ exceeded", sizeof(abt->notice)); | |
1317 | else | |
b2f0da88 | 1318 | rb_snprintf(abt->notice, sizeof(abt->notice), "Write error: %s", strerror(errno)); |
212380e3 AC |
1319 | |
1320 | abt->client = client_p; | |
1321 | SetIOError(client_p); | |
1322 | SetDead(client_p); | |
1323 | SetClosing(client_p); | |
330fc5c1 | 1324 | rb_dlinkAdd(abt, &abt->node, &abort_list); |
212380e3 AC |
1325 | } |
1326 | ||
1327 | ||
1328 | /* This does the remove of the user from channels..local or remote */ | |
1329 | static inline void | |
1330 | exit_generic_client(struct Client *client_p, struct Client *source_p, struct Client *from, | |
1331 | const char *comment) | |
1332 | { | |
637c4932 | 1333 | rb_dlink_node *ptr, *next_ptr; |
212380e3 AC |
1334 | |
1335 | if(IsOper(source_p)) | |
330fc5c1 | 1336 | rb_dlinkFindDestroy(source_p, &oper_list); |
212380e3 AC |
1337 | |
1338 | sendto_common_channels_local(source_p, ":%s!%s@%s QUIT :%s", | |
1339 | source_p->name, | |
1340 | source_p->username, source_p->host, comment); | |
1341 | ||
1342 | remove_user_from_channels(source_p); | |
1343 | ||
1344 | /* Should not be in any channels now */ | |
1345 | s_assert(source_p->user->channel.head == NULL); | |
1346 | ||
1347 | /* Clean up invitefield */ | |
637c4932 | 1348 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, source_p->user->invited.head) |
212380e3 AC |
1349 | { |
1350 | del_invite(ptr->data, source_p); | |
1351 | } | |
1352 | ||
1353 | /* Clean up allow lists */ | |
1354 | del_all_accepts(source_p); | |
1355 | ||
1356 | add_history(source_p, 0); | |
1357 | off_history(source_p); | |
1358 | ||
1359 | monitor_signoff(source_p); | |
1360 | ||
1361 | if(has_id(source_p)) | |
1362 | del_from_id_hash(source_p->id, source_p); | |
1363 | ||
1364 | del_from_hostname_hash(source_p->orighost, source_p); | |
1365 | del_from_client_hash(source_p->name, source_p); | |
1366 | remove_client_from_list(source_p); | |
1367 | } | |
1368 | ||
1369 | /* | |
1370 | * Assumes IsPerson(source_p) && !MyConnect(source_p) | |
1371 | */ | |
1372 | ||
1373 | static int | |
1374 | exit_remote_client(struct Client *client_p, struct Client *source_p, struct Client *from, | |
1375 | const char *comment) | |
1376 | { | |
1377 | exit_generic_client(client_p, source_p, from, comment); | |
1378 | ||
1379 | if(source_p->servptr && source_p->servptr->serv) | |
1380 | { | |
330fc5c1 | 1381 | rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->users); |
212380e3 AC |
1382 | } |
1383 | ||
1384 | if((source_p->flags & FLAGS_KILLED) == 0) | |
1385 | { | |
1386 | sendto_server(client_p, NULL, CAP_TS6, NOCAPS, | |
1387 | ":%s QUIT :%s", use_id(source_p), comment); | |
1388 | sendto_server(client_p, NULL, NOCAPS, CAP_TS6, | |
1389 | ":%s QUIT :%s", source_p->name, comment); | |
1390 | } | |
1391 | ||
1392 | SetDead(source_p); | |
1393 | #ifdef DEBUG_EXITED_CLIENTS | |
330fc5c1 | 1394 | rb_dlinkAddAlloc(source_p, &dead_remote_list); |
212380e3 | 1395 | #else |
330fc5c1 | 1396 | rb_dlinkAddAlloc(source_p, &dead_list); |
212380e3 AC |
1397 | #endif |
1398 | return(CLIENT_EXITED); | |
1399 | } | |
1400 | ||
1401 | /* | |
1402 | * This assumes IsUnknown(source_p) == TRUE and MyConnect(source_p) == TRUE | |
1403 | */ | |
1404 | ||
1405 | static int | |
1406 | exit_unknown_client(struct Client *client_p, struct Client *source_p, struct Client *from, | |
1407 | const char *comment) | |
1408 | { | |
1409 | delete_auth_queries(source_p); | |
abe5dd20 JT |
1410 | if (source_p->localClient->dnsquery) |
1411 | { | |
1412 | delete_resolver_queries(source_p->localClient->dnsquery); | |
1413 | rb_free(source_p->localClient->dnsquery); | |
1414 | } | |
54ac8b60 | 1415 | del_unknown_ip(source_p); |
330fc5c1 | 1416 | rb_dlinkDelete(&source_p->localClient->tnode, &unknown_list); |
212380e3 AC |
1417 | |
1418 | if(!IsIOError(source_p)) | |
dd12a19c JT |
1419 | sendto_one(source_p, "ERROR :Closing Link: %s (%s)", |
1420 | source_p->user != NULL ? source_p->host : "127.0.0.1", | |
1421 | comment); | |
212380e3 AC |
1422 | |
1423 | close_connection(source_p); | |
1424 | ||
1425 | if(has_id(source_p)) | |
1426 | del_from_id_hash(source_p->id, source_p); | |
1427 | ||
1428 | del_from_hostname_hash(source_p->host, source_p); | |
1429 | del_from_client_hash(source_p->name, source_p); | |
1430 | remove_client_from_list(source_p); | |
212380e3 | 1431 | SetDead(source_p); |
330fc5c1 | 1432 | rb_dlinkAddAlloc(source_p, &dead_list); |
212380e3 AC |
1433 | |
1434 | /* Note that we don't need to add unknowns to the dead_list */ | |
1435 | return(CLIENT_EXITED); | |
1436 | } | |
1437 | ||
1438 | static int | |
1439 | exit_remote_server(struct Client *client_p, struct Client *source_p, struct Client *from, | |
1440 | const char *comment) | |
1441 | { | |
1442 | static char comment1[(HOSTLEN*2)+2]; | |
1443 | static char newcomment[BUFSIZE]; | |
1444 | struct Client *target_p; | |
1445 | ||
1446 | if(ConfigServerHide.flatten_links) | |
1447 | strcpy(comment1, "*.net *.split"); | |
1448 | else | |
1449 | { | |
66c8fdd2 | 1450 | strcpy(comment1, source_p->servptr->name); |
212380e3 AC |
1451 | strcat(comment1, " "); |
1452 | strcat(comment1, source_p->name); | |
1453 | } | |
1454 | if (IsPerson(from)) | |
b2f0da88 | 1455 | rb_snprintf(newcomment, sizeof(newcomment), "by %s: %s", |
212380e3 AC |
1456 | from->name, comment); |
1457 | ||
1458 | if(source_p->serv != NULL) | |
1459 | remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1); | |
1460 | ||
1461 | if(source_p->servptr && source_p->servptr->serv) | |
330fc5c1 | 1462 | rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers); |
212380e3 AC |
1463 | else |
1464 | s_assert(0); | |
1465 | ||
330fc5c1 | 1466 | rb_dlinkFindDestroy(source_p, &global_serv_list); |
212380e3 AC |
1467 | target_p = source_p->from; |
1468 | ||
1469 | if(target_p != NULL && IsServer(target_p) && target_p != client_p && | |
1470 | !IsMe(target_p) && (source_p->flags & FLAGS_KILLED) == 0) | |
1471 | { | |
1472 | sendto_one(target_p, ":%s SQUIT %s :%s", | |
1473 | get_id(from, target_p), get_id(source_p, target_p), | |
1474 | comment); | |
1475 | } | |
1476 | ||
1477 | if(has_id(source_p)) | |
1478 | del_from_id_hash(source_p->id, source_p); | |
1479 | ||
1480 | del_from_client_hash(source_p->name, source_p); | |
1481 | remove_client_from_list(source_p); | |
994544c2 | 1482 | scache_split(source_p->serv->nameinfo); |
212380e3 AC |
1483 | |
1484 | SetDead(source_p); | |
1485 | #ifdef DEBUG_EXITED_CLIENTS | |
330fc5c1 | 1486 | rb_dlinkAddAlloc(source_p, &dead_remote_list); |
212380e3 | 1487 | #else |
330fc5c1 | 1488 | rb_dlinkAddAlloc(source_p, &dead_list); |
212380e3 AC |
1489 | #endif |
1490 | return 0; | |
1491 | } | |
1492 | ||
1493 | static int | |
1494 | qs_server(struct Client *client_p, struct Client *source_p, struct Client *from, | |
1495 | const char *comment) | |
1496 | { | |
1497 | struct Client *target_p; | |
1498 | ||
1499 | if(source_p->servptr && source_p->servptr->serv) | |
330fc5c1 | 1500 | rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers); |
212380e3 AC |
1501 | else |
1502 | s_assert(0); | |
1503 | ||
330fc5c1 | 1504 | rb_dlinkFindDestroy(source_p, &global_serv_list); |
212380e3 AC |
1505 | target_p = source_p->from; |
1506 | ||
1507 | if(has_id(source_p)) | |
1508 | del_from_id_hash(source_p->id, source_p); | |
1509 | ||
1510 | del_from_client_hash(source_p->name, source_p); | |
1511 | remove_client_from_list(source_p); | |
19807b5b | 1512 | scache_split(source_p->serv->nameinfo); |
212380e3 AC |
1513 | |
1514 | SetDead(source_p); | |
330fc5c1 | 1515 | rb_dlinkAddAlloc(source_p, &dead_list); |
212380e3 AC |
1516 | return 0; |
1517 | } | |
1518 | ||
1519 | static int | |
1520 | exit_local_server(struct Client *client_p, struct Client *source_p, struct Client *from, | |
1521 | const char *comment) | |
1522 | { | |
1523 | static char comment1[(HOSTLEN*2)+2]; | |
1524 | static char newcomment[BUFSIZE]; | |
1525 | unsigned int sendk, recvk; | |
1526 | ||
330fc5c1 AC |
1527 | rb_dlinkDelete(&source_p->localClient->tnode, &serv_list); |
1528 | rb_dlinkFindDestroy(source_p, &global_serv_list); | |
212380e3 AC |
1529 | |
1530 | unset_chcap_usage_counts(source_p); | |
1531 | sendk = source_p->localClient->sendK; | |
1532 | recvk = source_p->localClient->receiveK; | |
1533 | ||
1534 | /* Always show source here, so the server notices show | |
1535 | * which side initiated the split -- jilles | |
1536 | */ | |
b2f0da88 | 1537 | rb_snprintf(newcomment, sizeof(newcomment), "by %s: %s", |
212380e3 AC |
1538 | from == source_p ? me.name : from->name, comment); |
1539 | if (!IsIOError(source_p)) | |
1540 | sendto_one(source_p, "SQUIT %s :%s", use_id(source_p), | |
1541 | newcomment); | |
1542 | if(client_p != NULL && source_p != client_p && !IsIOError(source_p)) | |
1543 | { | |
1544 | sendto_one(source_p, "ERROR :Closing Link: 127.0.0.1 %s (%s)", | |
1545 | source_p->name, comment); | |
1546 | } | |
1547 | ||
212380e3 | 1548 | if(source_p->servptr && source_p->servptr->serv) |
330fc5c1 | 1549 | rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers); |
212380e3 AC |
1550 | else |
1551 | s_assert(0); | |
1552 | ||
1553 | ||
1554 | close_connection(source_p); | |
1555 | ||
1556 | if(ConfigServerHide.flatten_links) | |
1557 | strcpy(comment1, "*.net *.split"); | |
1558 | else | |
1559 | { | |
66c8fdd2 | 1560 | strcpy(comment1, source_p->servptr->name); |
212380e3 AC |
1561 | strcat(comment1, " "); |
1562 | strcat(comment1, source_p->name); | |
1563 | } | |
1564 | ||
1565 | if(source_p->serv != NULL) | |
1566 | remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1); | |
1567 | ||
1568 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s was connected" | |
1569 | " for %ld seconds. %d/%d sendK/recvK.", | |
adc6cc42 | 1570 | source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk); |
212380e3 AC |
1571 | |
1572 | ilog(L_SERVER, "%s was connected for %ld seconds. %d/%d sendK/recvK.", | |
adc6cc42 | 1573 | source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk); |
212380e3 AC |
1574 | |
1575 | if(has_id(source_p)) | |
1576 | del_from_id_hash(source_p->id, source_p); | |
1577 | ||
1578 | del_from_client_hash(source_p->name, source_p); | |
1579 | remove_client_from_list(source_p); | |
994544c2 | 1580 | scache_split(source_p->serv->nameinfo); |
212380e3 AC |
1581 | |
1582 | SetDead(source_p); | |
330fc5c1 | 1583 | rb_dlinkAddAlloc(source_p, &dead_list); |
212380e3 AC |
1584 | return 0; |
1585 | } | |
1586 | ||
1587 | ||
1588 | /* | |
1589 | * This assumes IsPerson(source_p) == TRUE && MyConnect(source_p) == TRUE | |
1590 | */ | |
1591 | ||
1592 | static int | |
1593 | exit_local_client(struct Client *client_p, struct Client *source_p, struct Client *from, | |
1594 | const char *comment) | |
1595 | { | |
1596 | unsigned long on_for; | |
1597 | ||
1598 | exit_generic_client(client_p, source_p, from, comment); | |
1599 | clear_monitor(source_p); | |
1600 | ||
1601 | s_assert(IsPerson(source_p)); | |
330fc5c1 AC |
1602 | rb_dlinkDelete(&source_p->localClient->tnode, &lclient_list); |
1603 | rb_dlinkDelete(&source_p->lnode, &me.serv->users); | |
212380e3 AC |
1604 | |
1605 | if(IsOper(source_p)) | |
330fc5c1 | 1606 | rb_dlinkFindDestroy(source_p, &local_oper_list); |
212380e3 AC |
1607 | |
1608 | sendto_realops_snomask(SNO_CCONN, L_ALL, | |
1609 | "Client exiting: %s (%s@%s) [%s] [%s]", | |
1610 | source_p->name, | |
1611 | source_p->username, source_p->host, comment, | |
1612 | show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255"); | |
1613 | ||
1614 | sendto_realops_snomask(SNO_CCONNEXT, L_ALL, | |
1615 | "CLIEXIT %s %s %s %s 0 %s", | |
1616 | source_p->name, source_p->username, source_p->host, | |
1617 | show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255", | |
1618 | comment); | |
1619 | ||
e3354945 | 1620 | on_for = rb_current_time() - source_p->localClient->firsttime; |
212380e3 AC |
1621 | |
1622 | ilog(L_USER, "%s (%3lu:%02lu:%02lu): %s!%s@%s %d/%d", | |
e3354945 | 1623 | myctime(rb_current_time()), on_for / 3600, |
212380e3 AC |
1624 | (on_for % 3600) / 60, on_for % 60, |
1625 | source_p->name, source_p->username, source_p->host, | |
1626 | source_p->localClient->sendK, source_p->localClient->receiveK); | |
1627 | ||
1628 | sendto_one(source_p, "ERROR :Closing Link: %s (%s)", source_p->host, comment); | |
1629 | close_connection(source_p); | |
1630 | ||
1631 | if((source_p->flags & FLAGS_KILLED) == 0) | |
1632 | { | |
1633 | sendto_server(client_p, NULL, CAP_TS6, NOCAPS, | |
1634 | ":%s QUIT :%s", use_id(source_p), comment); | |
1635 | sendto_server(client_p, NULL, NOCAPS, CAP_TS6, | |
1636 | ":%s QUIT :%s", source_p->name, comment); | |
1637 | } | |
1638 | ||
1639 | SetDead(source_p); | |
330fc5c1 | 1640 | rb_dlinkAddAlloc(source_p, &dead_list); |
212380e3 AC |
1641 | return(CLIENT_EXITED); |
1642 | } | |
1643 | ||
1644 | ||
1645 | /* | |
1646 | ** exit_client - This is old "m_bye". Name changed, because this is not a | |
1647 | ** protocol function, but a general server utility function. | |
1648 | ** | |
1649 | ** This function exits a client of *any* type (user, server, etc) | |
1650 | ** from this server. Also, this generates all necessary prototol | |
1651 | ** messages that this exit may cause. | |
1652 | ** | |
1653 | ** 1) If the client is a local client, then this implicitly | |
1654 | ** exits all other clients depending on this connection (e.g. | |
1655 | ** remote clients having 'from'-field that points to this. | |
1656 | ** | |
1657 | ** 2) If the client is a remote client, then only this is exited. | |
1658 | ** | |
1659 | ** For convenience, this function returns a suitable value for | |
1660 | ** m_function return value: | |
1661 | ** | |
1662 | ** CLIENT_EXITED if (client_p == source_p) | |
1663 | ** 0 if (client_p != source_p) | |
1664 | */ | |
1665 | int | |
1666 | exit_client(struct Client *client_p, /* The local client originating the | |
1667 | * exit or NULL, if this exit is | |
1668 | * generated by this server for | |
1669 | * internal reasons. | |
1670 | * This will not get any of the | |
1671 | * generated messages. */ | |
1672 | struct Client *source_p, /* Client exiting */ | |
1673 | struct Client *from, /* Client firing off this Exit, | |
1674 | * never NULL! */ | |
1675 | const char *comment /* Reason for the exit */ | |
1676 | ) | |
1677 | { | |
1678 | hook_data_client_exit hdata; | |
1679 | if(IsClosing(source_p)) | |
1680 | return -1; | |
1681 | ||
1682 | /* note, this HAS to be here, when we exit a client we attempt to | |
1683 | * send them data, if this generates a write error we must *not* add | |
1684 | * them to the abort list --fl | |
1685 | */ | |
1686 | SetClosing(source_p); | |
1687 | ||
1688 | hdata.local_link = client_p; | |
1689 | hdata.target = source_p; | |
1690 | hdata.from = from; | |
1691 | hdata.comment = comment; | |
1692 | call_hook(h_client_exit, &hdata); | |
1693 | ||
1694 | if(MyConnect(source_p)) | |
1695 | { | |
1696 | /* Local clients of various types */ | |
1697 | if(IsPerson(source_p)) | |
1698 | return exit_local_client(client_p, source_p, from, comment); | |
1699 | else if(IsServer(source_p)) | |
1700 | return exit_local_server(client_p, source_p, from, comment); | |
1701 | /* IsUnknown || IsConnecting || IsHandShake */ | |
1702 | else if(!IsReject(source_p)) | |
1703 | return exit_unknown_client(client_p, source_p, from, comment); | |
1704 | } | |
1705 | else | |
1706 | { | |
1707 | /* Remotes */ | |
1708 | if(IsPerson(source_p)) | |
1709 | return exit_remote_client(client_p, source_p, from, comment); | |
1710 | else if(IsServer(source_p)) | |
1711 | return exit_remote_server(client_p, source_p, from, comment); | |
1712 | } | |
1713 | ||
1714 | return -1; | |
1715 | } | |
1716 | ||
1717 | /* | |
1718 | * Count up local client memory | |
1719 | */ | |
1720 | ||
1721 | /* XXX one common Client list now */ | |
1722 | void | |
1723 | count_local_client_memory(size_t * count, size_t * local_client_memory_used) | |
1724 | { | |
1087485c JT |
1725 | size_t lusage; |
1726 | rb_bh_usage(lclient_heap, count, NULL, &lusage, NULL); | |
adc6cc42 | 1727 | *local_client_memory_used = lusage + (*count * (sizeof(void *) + sizeof(struct Client))); |
212380e3 AC |
1728 | } |
1729 | ||
1730 | /* | |
1731 | * Count up remote client memory | |
1732 | */ | |
1733 | void | |
1734 | count_remote_client_memory(size_t * count, size_t * remote_client_memory_used) | |
1735 | { | |
1087485c JT |
1736 | size_t lcount, rcount; |
1737 | rb_bh_usage(lclient_heap, &lcount, NULL, NULL, NULL); | |
1738 | rb_bh_usage(client_heap, &rcount, NULL, NULL, NULL); | |
1739 | *count = rcount - lcount; | |
adc6cc42 | 1740 | *remote_client_memory_used = *count * (sizeof(void *) + sizeof(struct Client)); |
212380e3 AC |
1741 | } |
1742 | ||
1743 | ||
1744 | /* | |
1745 | * accept processing, this adds a form of "caller ID" to ircd | |
1746 | * | |
1747 | * If a client puts themselves into "caller ID only" mode, | |
1748 | * only clients that match a client pointer they have put on | |
1749 | * the accept list will be allowed to message them. | |
1750 | * | |
1751 | * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ] | |
1752 | * | |
1753 | * [target.allow_list] -> [ source1 ] -> [source2 ] | |
1754 | * | |
1755 | * i.e. a target will have a link list of source pointers it will allow | |
1756 | * each source client then has a back pointer pointing back | |
1757 | * to the client that has it on its accept list. | |
1758 | * This allows for exit_one_client to remove these now bogus entries | |
1759 | * from any client having an accept on them. | |
1760 | */ | |
1761 | /* | |
1762 | * del_all_accepts | |
1763 | * | |
1764 | * inputs - pointer to exiting client | |
1765 | * output - NONE | |
1766 | * side effects - Walk through given clients allow_list and on_allow_list | |
1767 | * remove all references to this client | |
1768 | */ | |
1769 | void | |
1770 | del_all_accepts(struct Client *client_p) | |
1771 | { | |
330fc5c1 | 1772 | rb_dlink_node *ptr; |
637c4932 | 1773 | rb_dlink_node *next_ptr; |
212380e3 AC |
1774 | struct Client *target_p; |
1775 | ||
1776 | if(MyClient(client_p) && client_p->localClient->allow_list.head) | |
1777 | { | |
1778 | /* clear this clients accept list, and remove them from | |
1779 | * everyones on_accept_list | |
1780 | */ | |
637c4932 | 1781 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->allow_list.head) |
212380e3 AC |
1782 | { |
1783 | target_p = ptr->data; | |
330fc5c1 AC |
1784 | rb_dlinkFindDestroy(client_p, &target_p->on_allow_list); |
1785 | rb_dlinkDestroy(ptr, &client_p->localClient->allow_list); | |
212380e3 AC |
1786 | } |
1787 | } | |
1788 | ||
1789 | /* remove this client from everyones accept list */ | |
637c4932 | 1790 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head) |
212380e3 AC |
1791 | { |
1792 | target_p = ptr->data; | |
330fc5c1 AC |
1793 | rb_dlinkFindDestroy(client_p, &target_p->localClient->allow_list); |
1794 | rb_dlinkDestroy(ptr, &client_p->on_allow_list); | |
212380e3 AC |
1795 | } |
1796 | } | |
1797 | ||
1798 | /* | |
1799 | * show_ip() - asks if the true IP shoudl be shown when source is | |
1800 | * askin for info about target | |
1801 | * | |
1802 | * Inputs - source_p who is asking | |
1803 | * - target_p who do we want the info on | |
1804 | * Output - returns 1 if clear IP can be showed, otherwise 0 | |
1805 | * Side Effects - none | |
1806 | */ | |
1807 | ||
1808 | int | |
1809 | show_ip(struct Client *source_p, struct Client *target_p) | |
1810 | { | |
1811 | if(IsAnyServer(target_p)) | |
1812 | { | |
1813 | #ifndef HIDE_SERVERS_IPS | |
1814 | if(source_p == NULL || IsOper(source_p)) | |
1815 | return 1; | |
1816 | #endif | |
1817 | return 0; | |
1818 | } | |
1819 | else if(IsIPSpoof(target_p)) | |
1820 | { | |
1821 | /* source == NULL indicates message is being sent | |
1822 | * to local opers. | |
1823 | */ | |
1824 | if(!ConfigFileEntry.hide_spoof_ips && | |
1825 | (source_p == NULL || MyOper(source_p))) | |
1826 | return 1; | |
1827 | return 0; | |
1828 | } | |
1829 | else if(IsDynSpoof(target_p) && (source_p != NULL && !IsOper(source_p))) | |
1830 | return 0; | |
1831 | else | |
1832 | return 1; | |
1833 | } | |
1834 | ||
1835 | int | |
1836 | show_ip_conf(struct ConfItem *aconf, struct Client *source_p) | |
1837 | { | |
1838 | if(IsConfDoSpoofIp(aconf)) | |
1839 | { | |
1840 | if(!ConfigFileEntry.hide_spoof_ips && MyOper(source_p)) | |
1841 | return 1; | |
1842 | ||
1843 | return 0; | |
1844 | } | |
1845 | else | |
1846 | return 1; | |
1847 | } | |
1848 | ||
1849 | /* | |
1850 | * initUser | |
1851 | * | |
1852 | * inputs - none | |
1853 | * outputs - none | |
1854 | * | |
1855 | * side effects - Creates a block heap for struct Users | |
1856 | * | |
1857 | */ | |
398b6a73 | 1858 | static rb_bh *user_heap; |
212380e3 AC |
1859 | void |
1860 | initUser(void) | |
1861 | { | |
adc6cc42 | 1862 | user_heap = rb_bh_create(sizeof(struct User), USER_HEAP_SIZE, "user_heap"); |
212380e3 | 1863 | if(!user_heap) |
adc6cc42 | 1864 | rb_outofmemory(); |
212380e3 AC |
1865 | } |
1866 | ||
1867 | /* | |
1868 | * make_user | |
1869 | * | |
1870 | * inputs - pointer to client struct | |
1871 | * output - pointer to struct User | |
1872 | * side effects - add's an User information block to a client | |
1873 | * if it was not previously allocated. | |
1874 | */ | |
1875 | struct User * | |
1876 | make_user(struct Client *client_p) | |
1877 | { | |
1878 | struct User *user; | |
1879 | ||
1880 | user = client_p->user; | |
1881 | if(!user) | |
1882 | { | |
398b6a73 | 1883 | user = (struct User *) rb_bh_alloc(user_heap); |
212380e3 AC |
1884 | user->refcnt = 1; |
1885 | client_p->user = user; | |
1886 | } | |
1887 | return user; | |
1888 | } | |
1889 | ||
1890 | /* | |
1891 | * make_server | |
1892 | * | |
1893 | * inputs - pointer to client struct | |
1894 | * output - pointer to server_t | |
1895 | * side effects - add's an Server information block to a client | |
1896 | * if it was not previously allocated. | |
1897 | */ | |
1898 | server_t * | |
1899 | make_server(struct Client *client_p) | |
1900 | { | |
1901 | server_t *serv = client_p->serv; | |
1902 | ||
1903 | if(!serv) | |
1904 | { | |
eddc2ab6 | 1905 | serv = (server_t *) rb_malloc(sizeof(server_t)); |
212380e3 AC |
1906 | client_p->serv = serv; |
1907 | } | |
1908 | return client_p->serv; | |
1909 | } | |
1910 | ||
1911 | /* | |
1912 | * free_user | |
1913 | * | |
1914 | * inputs - pointer to user struct | |
1915 | * - pointer to client struct | |
1916 | * output - none | |
1917 | * side effects - Decrease user reference count by one and release block, | |
1918 | * if count reaches 0 | |
1919 | */ | |
1920 | void | |
1921 | free_user(struct User *user, struct Client *client_p) | |
1922 | { | |
adc6cc42 VY |
1923 | free_away(client_p); |
1924 | ||
212380e3 AC |
1925 | if(--user->refcnt <= 0) |
1926 | { | |
1927 | if(user->away) | |
637c4932 | 1928 | rb_free((char *) user->away); |
212380e3 AC |
1929 | /* |
1930 | * sanity check | |
1931 | */ | |
1932 | if(user->refcnt < 0 || user->invited.head || user->channel.head) | |
1933 | { | |
1934 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
1935 | "* %#lx user (%s!%s@%s) %#lx %#lx %#lx %lu %d *", | |
1936 | (unsigned long) client_p, | |
1937 | client_p ? client_p-> | |
1938 | name : "<noname>", | |
1939 | client_p->username, | |
1940 | client_p->host, | |
1941 | (unsigned long) user, | |
1942 | (unsigned long) user->invited.head, | |
1943 | (unsigned long) user->channel.head, | |
330fc5c1 | 1944 | rb_dlink_list_length(&user->channel), |
212380e3 AC |
1945 | user->refcnt); |
1946 | s_assert(!user->refcnt); | |
1947 | s_assert(!user->invited.head); | |
1948 | s_assert(!user->channel.head); | |
1949 | } | |
1950 | ||
398b6a73 | 1951 | rb_bh_free(user_heap, user); |
212380e3 AC |
1952 | } |
1953 | } | |
1954 | ||
adc6cc42 VY |
1955 | void |
1956 | allocate_away(struct Client *client_p) | |
1957 | { | |
1958 | if(client_p->user->away == NULL) | |
1959 | client_p->user->away = rb_bh_alloc(away_heap); | |
1960 | } | |
1961 | ||
1962 | ||
1963 | void | |
1964 | free_away(struct Client *client_p) | |
1965 | { | |
d18a9c05 | 1966 | if(client_p->user != NULL && client_p->user->away != NULL) { |
adc6cc42 VY |
1967 | rb_bh_free(away_heap, client_p->user->away); |
1968 | client_p->user->away = NULL; | |
1969 | } | |
300a5433 VY |
1970 | } |
1971 | ||
212380e3 AC |
1972 | void |
1973 | init_uid(void) | |
1974 | { | |
1975 | int i; | |
1976 | ||
1977 | for(i = 0; i < 3; i++) | |
1978 | current_uid[i] = me.id[i]; | |
1979 | ||
1980 | for(i = 3; i < 9; i++) | |
1981 | current_uid[i] = 'A'; | |
1982 | ||
1983 | current_uid[9] = '\0'; | |
1984 | } | |
1985 | ||
1986 | ||
1987 | char * | |
1988 | generate_uid(void) | |
1989 | { | |
1990 | int i; | |
1991 | ||
1992 | for(i = 8; i > 3; i--) | |
1993 | { | |
1994 | if(current_uid[i] == 'Z') | |
1995 | { | |
1996 | current_uid[i] = '0'; | |
1997 | return current_uid; | |
1998 | } | |
1999 | else if(current_uid[i] != '9') | |
2000 | { | |
2001 | current_uid[i]++; | |
2002 | return current_uid; | |
2003 | } | |
2004 | else | |
2005 | current_uid[i] = 'A'; | |
2006 | } | |
2007 | ||
2008 | /* if this next if() triggers, we're fucked. */ | |
2009 | if(current_uid[3] == 'Z') | |
2010 | { | |
2011 | current_uid[i] = 'A'; | |
2012 | s_assert(0); | |
2013 | } | |
2014 | else | |
2015 | current_uid[i]++; | |
2016 | ||
2017 | return current_uid; | |
2018 | } | |
2019 | ||
2020 | /* | |
2021 | * close_connection | |
2022 | * Close the physical connection. This function must make | |
2023 | * MyConnect(client_p) == FALSE, and set client_p->from == NULL. | |
2024 | */ | |
2025 | void | |
2026 | close_connection(struct Client *client_p) | |
2027 | { | |
2028 | s_assert(client_p != NULL); | |
2029 | if(client_p == NULL) | |
2030 | return; | |
2031 | ||
2032 | s_assert(MyConnect(client_p)); | |
2033 | if(!MyConnect(client_p)) | |
2034 | return; | |
2035 | ||
2036 | if(IsServer(client_p)) | |
2037 | { | |
2038 | struct server_conf *server_p; | |
2039 | ||
4c79bbd3 VY |
2040 | ServerStats.is_sv++;\r |
2041 | ServerStats.is_sbs += client_p->localClient->sendB;\r | |
2042 | ServerStats.is_sbr += client_p->localClient->receiveB;\r | |
47adde3d | 2043 | ServerStats.is_sti += rb_current_time() - client_p->localClient->firsttime; |
212380e3 AC |
2044 | |
2045 | /* | |
2046 | * If the connection has been up for a long amount of time, schedule | |
2047 | * a 'quick' reconnect, else reset the next-connect cycle. | |
2048 | */ | |
2049 | if((server_p = find_server_conf(client_p->name)) != NULL) | |
2050 | { | |
2051 | /* | |
2052 | * Reschedule a faster reconnect, if this was a automatically | |
2053 | * connected configuration entry. (Note that if we have had | |
2054 | * a rehash in between, the status has been changed to | |
2055 | * CONF_ILLEGAL). But only do this if it was a "good" link. | |
2056 | */ | |
2057 | server_p->hold = time(NULL); | |
2058 | server_p->hold += | |
2059 | (server_p->hold - client_p->localClient->lasttime > | |
2060 | HANGONGOODLINK) ? HANGONRETRYDELAY : ConFreq(server_p->class); | |
2061 | } | |
2062 | ||
2063 | } | |
2064 | else if(IsClient(client_p)) | |
2065 | { | |
4c79bbd3 VY |
2066 | ServerStats.is_cl++;\r |
2067 | ServerStats.is_cbs += client_p->localClient->sendB;\r | |
2068 | ServerStats.is_cbr += client_p->localClient->receiveB;\r | |
47adde3d | 2069 | ServerStats.is_cti += rb_current_time() - client_p->localClient->firsttime; |
212380e3 AC |
2070 | } |
2071 | else | |
47adde3d | 2072 | ServerStats.is_ni++; |
54ac8b60 | 2073 | |
c6d72037 | 2074 | if(client_p->localClient->F != NULL) |
54ac8b60 VY |
2075 | { |
2076 | /* attempt to flush any pending dbufs. Evil, but .. -- adrian */ | |
2077 | if(!IsIOError(client_p)) | |
2078 | send_queued(client_p); | |
2079 | ||
c6d72037 | 2080 | del_from_cli_fd_hash(client_p); |
54ac8b60 VY |
2081 | rb_close(client_p->localClient->F); |
2082 | client_p->localClient->F = NULL; | |
2083 | } | |
2084 | ||
734d420e JT |
2085 | rb_linebuf_donebuf(&client_p->localClient->buf_sendq); |
2086 | rb_linebuf_donebuf(&client_p->localClient->buf_recvq); | |
212380e3 AC |
2087 | detach_conf(client_p); |
2088 | ||
2089 | /* XXX shouldnt really be done here. */ | |
2090 | detach_server_conf(client_p); | |
2091 | ||
2092 | client_p->from = NULL; /* ...this should catch them! >:) --msa */ | |
2093 | ClearMyConnect(client_p); | |
2094 | SetIOError(client_p); | |
2095 | } | |
2096 | ||
2097 | ||
2098 | ||
2099 | void | |
2100 | error_exit_client(struct Client *client_p, int error) | |
2101 | { | |
2102 | /* | |
2103 | * ...hmm, with non-blocking sockets we might get | |
2104 | * here from quite valid reasons, although.. why | |
2105 | * would select report "data available" when there | |
2106 | * wasn't... so, this must be an error anyway... --msa | |
2107 | * actually, EOF occurs when read() returns 0 and | |
2108 | * in due course, select() returns that fd as ready | |
2109 | * for reading even though it ends up being an EOF. -avalon | |
2110 | */ | |
2111 | char errmsg[255]; | |
734d420e | 2112 | int current_error = rb_get_sockerr(client_p->localClient->F); |
212380e3 AC |
2113 | |
2114 | SetIOError(client_p); | |
2115 | ||
2116 | if(IsServer(client_p) || IsHandshake(client_p)) | |
2117 | { | |
212380e3 AC |
2118 | if(error == 0) |
2119 | { | |
2120 | sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL, | |
2121 | "Server %s closed the connection", | |
2122 | get_server_name(client_p, SHOW_IP)); | |
2123 | ||
2124 | ilog(L_SERVER, "Server %s closed the connection", | |
2125 | log_client_name(client_p, SHOW_IP)); | |
2126 | } | |
2127 | else | |
2128 | { | |
2129 | sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL, | |
2130 | "Lost connection to %s: %s", | |
2131 | client_p->name, strerror(current_error)); | |
2132 | ilog(L_SERVER, "Lost connection to %s: %s", | |
2133 | log_client_name(client_p, SHOW_IP), strerror(current_error)); | |
2134 | } | |
212380e3 AC |
2135 | } |
2136 | ||
2137 | if(error == 0) | |
2138 | strlcpy(errmsg, "Remote host closed the connection", sizeof(errmsg)); | |
2139 | else | |
b2f0da88 | 2140 | rb_snprintf(errmsg, sizeof(errmsg), "Read error: %s", strerror(current_error)); |
212380e3 AC |
2141 | |
2142 | exit_client(client_p, client_p, &me, errmsg); | |
2143 | } |