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