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