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