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