Restore stats_spy hook that was removed in commit 401f2454671ca233e35b0e6e4f3fa4c43cd...
[seven-1.x.git] / src / client.c
blob1e361d16c014e3ecdd0e9d46793d3ef892fbcdc0
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * client.c: Controls clients.
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
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
24 #include "stdinc.h"
25 #include "config.h"
27 #include "tools.h"
28 #include "client.h"
29 #include "class.h"
30 #include "common.h"
31 #include "event.h"
32 #include "hash.h"
33 #include "irc_string.h"
34 #include "sprintf_irc.h"
35 #include "ircd.h"
36 #include "numeric.h"
37 #include "packet.h"
38 #include "s_auth.h"
39 #include "commio.h"
40 #include "s_conf.h"
41 #include "s_newconf.h"
42 #include "s_log.h"
43 #include "s_serv.h"
44 #include "s_stats.h"
45 #include "send.h"
46 #include "whowas.h"
47 #include "s_user.h"
48 #include "linebuf.h"
49 #include "hash.h"
50 #include "memory.h"
51 #include "hostmask.h"
52 #include "balloc.h"
53 #include "listener.h"
54 #include "hook.h"
55 #include "msg.h"
56 #include "monitor.h"
57 #include "blacklist.h"
59 #define DEBUG_EXITED_CLIENTS
61 static void check_pings_list(dlink_list * list);
62 static void check_unknowns_list(dlink_list * list);
63 static void free_exited_clients(void *unused);
64 static void exit_aborted_clients(void *unused);
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);
73 static EVH check_pings;
75 extern BlockHeap *client_heap;
76 extern BlockHeap *lclient_heap;
77 extern BlockHeap *pclient_heap;
79 extern char current_uid[IDLEN];
81 enum
83 D_LINED,
84 K_LINED
87 dlink_list dead_list;
88 #ifdef DEBUG_EXITED_CLIENTS
89 static dlink_list dead_remote_list;
90 #endif
92 struct abort_client
94 dlink_node node;
95 struct Client *client;
96 char notice[REASONLEN];
99 static dlink_list abort_list;
103 * init_client
105 * inputs - NONE
106 * output - NONE
107 * side effects - initialize client free memory
109 void
110 init_client(void)
113 * start off the check ping event .. -- adrian
114 * Every 30 seconds is plenty -- db
116 client_heap = BlockHeapCreate(sizeof(struct Client), CLIENT_HEAP_SIZE);
117 lclient_heap = BlockHeapCreate(sizeof(struct LocalUser), LCLIENT_HEAP_SIZE);
118 pclient_heap = BlockHeapCreate(sizeof(struct PreClient), PCLIENT_HEAP_SIZE);
119 eventAddIsh("check_pings", check_pings, NULL, 30);
120 eventAddIsh("free_exited_clients", &free_exited_clients, NULL, 4);
121 eventAddIsh("exit_aborted_clients", exit_aborted_clients, NULL, 1);
126 * make_client - create a new Client struct and set it to initial state.
128 * from == NULL, create local client (a client connected
129 * to a socket).
131 * from, create remote client (behind a socket
132 * associated with the client defined by
133 * 'from'). ('from' is a local client!!).
135 struct Client *
136 make_client(struct Client *from)
138 struct Client *client_p = NULL;
139 struct LocalUser *localClient;
141 client_p = BlockHeapAlloc(client_heap);
143 if(from == NULL)
145 client_p->from = client_p; /* 'from' of local client is self! */
147 localClient = (struct LocalUser *) BlockHeapAlloc(lclient_heap);
148 SetMyConnect(client_p);
149 client_p->localClient = localClient;
151 client_p->localClient->lasttime = client_p->localClient->firsttime = CurrentTime;
153 client_p->localClient->fd = -1;
154 client_p->localClient->ctrlfd = -1;
156 client_p->preClient = (struct PreClient *) BlockHeapAlloc(pclient_heap);
158 /* as good a place as any... */
159 dlinkAdd(client_p, &client_p->localClient->tnode, &unknown_list);
161 else
162 { /* from is not NULL */
163 client_p->localClient = NULL;
164 client_p->preClient = NULL;
165 client_p->from = from; /* 'from' of local client is self! */
168 SetUnknown(client_p);
169 strcpy(client_p->username, "unknown");
171 return client_p;
174 void
175 free_pre_client(struct Client *client_p)
177 struct Blacklist *blptr;
179 s_assert(NULL != client_p);
181 if(client_p->preClient == NULL)
182 return;
184 blptr = client_p->preClient->dnsbl_listed;
185 if (blptr != NULL)
186 unref_blacklist(blptr);
187 abort_blacklist_queries(client_p);
188 BlockHeapFree(pclient_heap, client_p->preClient);
189 client_p->preClient = NULL;
192 static void
193 free_local_client(struct Client *client_p)
195 s_assert(NULL != client_p);
196 s_assert(&me != client_p);
198 if(client_p->localClient == NULL)
199 return;
202 * clean up extra sockets from P-lines which have been discarded.
204 if(client_p->localClient->listener)
206 s_assert(0 < client_p->localClient->listener->ref_count);
207 if(0 == --client_p->localClient->listener->ref_count
208 && !client_p->localClient->listener->active)
209 free_listener(client_p->localClient->listener);
210 client_p->localClient->listener = 0;
213 if(client_p->localClient->fd >= 0)
214 comm_close(client_p->localClient->fd);
216 if(client_p->localClient->passwd)
218 memset(client_p->localClient->passwd, 0,
219 strlen(client_p->localClient->passwd));
220 MyFree(client_p->localClient->passwd);
223 MyFree(client_p->localClient->challenge);
224 MyFree(client_p->localClient->fullcaps);
225 MyFree(client_p->localClient->opername);
226 MyFree(client_p->localClient->mangledhost);
228 BlockHeapFree(lclient_heap, client_p->localClient);
229 client_p->localClient = NULL;
232 void
233 free_client(struct Client *client_p)
235 s_assert(NULL != client_p);
236 s_assert(&me != client_p);
237 free_local_client(client_p);
238 free_pre_client(client_p);
239 BlockHeapFree(client_heap, client_p);
243 * check_pings - go through the local client list and check activity
244 * kill off stuff that should die
246 * inputs - NOT USED (from event)
247 * output - next time_t when check_pings() should be called again
248 * side effects -
251 * A PING can be sent to clients as necessary.
253 * Client/Server ping outs are handled.
257 * Addon from adrian. We used to call this after nextping seconds,
258 * however I've changed it to run once a second. This is only for
259 * PING timeouts, not K/etc-line checks (thanks dianora!). Having it
260 * run once a second makes life a lot easier - when a new client connects
261 * and they need a ping in 4 seconds, if nextping was set to 20 seconds
262 * we end up waiting 20 seconds. This is stupid. :-)
263 * I will optimise (hah!) check_pings() once I've finished working on
264 * tidying up other network IO evilnesses.
265 * -- adrian
268 static void
269 check_pings(void *notused)
271 check_pings_list(&lclient_list);
272 check_pings_list(&serv_list);
273 check_unknowns_list(&unknown_list);
277 * Check_pings_list()
279 * inputs - pointer to list to check
280 * output - NONE
281 * side effects -
283 static void
284 check_pings_list(dlink_list * list)
286 char scratch[32]; /* way too generous but... */
287 struct Client *client_p; /* current local client_p being examined */
288 int ping = 0; /* ping time value from client */
289 dlink_node *ptr, *next_ptr;
291 DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
293 client_p = ptr->data;
296 ** Note: No need to notify opers here. It's
297 ** already done when "FLAGS_DEADSOCKET" is set.
299 if(!MyConnect(client_p) || IsDead(client_p))
300 continue;
302 if(IsPerson(client_p))
304 if(!IsExemptKline(client_p) &&
305 GlobalSetOptions.idletime &&
306 !IsOper(client_p) &&
307 !IsIdlelined(client_p) &&
308 ((CurrentTime - client_p->localClient->last) > GlobalSetOptions.idletime))
310 struct ConfItem *aconf;
312 aconf = make_conf();
313 aconf->status = CONF_KILL;
315 DupString(aconf->host, client_p->host);
316 DupString(aconf->passwd, "idle exceeder");
317 DupString(aconf->user, client_p->username);
318 aconf->port = 0;
319 aconf->hold = CurrentTime + 60;
320 add_temp_kline(aconf);
321 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
322 "Idle time limit exceeded for %s - temp k-lining",
323 get_client_name(client_p, HIDE_IP));
325 exit_client(client_p, client_p, &me, aconf->passwd);
326 continue;
330 if(!IsRegistered(client_p))
331 ping = ConfigFileEntry.connect_timeout;
332 else
333 ping = get_client_ping(client_p);
335 if(ping < (CurrentTime - client_p->localClient->lasttime))
338 * If the client/server hasnt talked to us in 2*ping seconds
339 * and it has a ping time, then close its connection.
341 if(((CurrentTime - client_p->localClient->lasttime) >= (2 * ping)
342 && (client_p->flags & FLAGS_PINGSENT)))
344 if(IsAnyServer(client_p))
346 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL,
347 "No response from %s, closing link",
348 get_server_name(client_p, HIDE_IP));
349 ilog(L_SERVER,
350 "No response from %s, closing link",
351 log_client_name(client_p, HIDE_IP));
354 /* Yes, this is retarded. But someone wanted it available just in case,
355 * so here it is. --spb */
356 if(IsClient(client_p) && ConfigFileEntry.no_ping_timeout)
357 continue;
359 (void) ircsnprintf(scratch, sizeof(scratch),
360 "Ping timeout: %d seconds",
361 (int) (CurrentTime - client_p->localClient->lasttime));
363 exit_client(client_p, client_p, &me, scratch);
364 continue;
366 else if((client_p->flags & FLAGS_PINGSENT) == 0)
369 * if we havent PINGed the connection and we havent
370 * heard from it in a while, PING it to make sure
371 * it is still alive.
373 client_p->flags |= FLAGS_PINGSENT;
374 /* not nice but does the job */
375 client_p->localClient->lasttime = CurrentTime - ping;
376 sendto_one(client_p, "PING :%s", me.name);
379 /* ping_timeout: */
385 * check_unknowns_list
387 * inputs - pointer to list of unknown clients
388 * output - NONE
389 * side effects - unknown clients get marked for termination after n seconds
391 static void
392 check_unknowns_list(dlink_list * list)
394 dlink_node *ptr, *next_ptr;
395 struct Client *client_p;
397 DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
399 client_p = ptr->data;
401 if(IsDead(client_p) || IsClosing(client_p))
402 continue;
405 * Check UNKNOWN connections - if they have been in this state
406 * for > 30s, close them.
409 if((CurrentTime - client_p->localClient->firsttime) > 30)
410 exit_client(client_p, client_p, &me, "Connection timed out");
414 static void
415 notify_banned_client(struct Client *client_p, struct ConfItem *aconf, int ban)
417 static const char d_lined[] = "D-lined";
418 static const char k_lined[] = "K-lined";
419 const char *reason = NULL;
421 switch (ban)
423 case D_LINED:
424 reason = d_lined;
425 break;
426 default:
427 reason = k_lined;
428 break;
431 if (ban == D_LINED && !IsPerson(client_p))
432 sendto_one(client_p, "NOTICE DLINE :*** You have been D-lined");
433 else
434 sendto_one(client_p, form_str(ERR_YOUREBANNEDCREEP), me.name,
435 client_p->name, reason);
437 exit_client(client_p, client_p, &me, reason);
441 * check_banned_lines
442 * inputs - NONE
443 * output - NONE
444 * side effects - Check all connections for a pending k/d/gline against the
445 * client, exit the client if found.
447 void
448 check_banned_lines(void)
450 struct Client *client_p; /* current local client_p being examined */
451 struct ConfItem *aconf = NULL;
452 dlink_node *ptr, *next_ptr;
454 DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
456 client_p = ptr->data;
458 if(IsMe(client_p))
459 continue;
461 /* if there is a returned struct ConfItem then kill it */
462 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip, client_p->localClient->ip.ss_family)))
464 if(aconf->status & CONF_EXEMPTDLINE)
465 continue;
467 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
468 "Confirming kill of %s (D-lined)",
469 get_client_name(client_p, HIDE_IP));
471 notify_banned_client(client_p, aconf, D_LINED);
472 continue; /* and go examine next fd/client_p */
475 if(!IsPerson(client_p))
476 continue;
478 if((aconf = find_kline(client_p)) != NULL)
480 if(IsExemptKline(client_p))
482 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
483 "Not killing %s for K-line; client matches exempt [%s@%s]",
484 get_client_name(client_p, HIDE_IP),
485 aconf->user, aconf->host);
486 continue;
489 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
490 "Confirming kill of %s (K-lined)",
491 get_client_name(client_p, HIDE_IP));
492 notify_banned_client(client_p, aconf, K_LINED);
493 continue;
495 else if((aconf = find_xline(client_p->info, 1)) != NULL)
497 if(IsExemptKline(client_p))
499 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
500 "Not killing %s for X-line; client matches exempt [%s]",
501 get_client_name(client_p, HIDE_IP),
502 aconf->name);
503 continue;
506 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
507 "Confirming kill of %s (X-lined)",
508 get_client_name(client_p, HIDE_IP));
510 (void) exit_client(client_p, client_p, &me, "Bad user info");
511 continue;
515 /* also check the unknowns list for new dlines */
516 DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
518 client_p = ptr->data;
520 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)))
522 if(aconf->status & CONF_EXEMPTDLINE)
523 continue;
525 notify_banned_client(client_p, aconf, D_LINED);
531 /* check_klines_event()
533 * inputs -
534 * outputs -
535 * side effects - check_klines() is called, kline_queued unset
537 void
538 check_klines_event(void *unused)
540 kline_queued = 0;
541 check_klines();
544 /* check_klines
546 * inputs -
547 * outputs -
548 * side effects - all clients will be checked for klines
550 void
551 check_klines(void)
553 struct Client *client_p;
554 struct ConfItem *aconf;
555 dlink_node *ptr;
556 dlink_node *next_ptr;
558 DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
560 client_p = ptr->data;
562 if(IsMe(client_p) || !IsPerson(client_p))
563 continue;
565 if((aconf = find_kline(client_p)) != NULL)
567 if(IsExemptKline(client_p))
569 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
570 "Not killing %s for K-line; client is exempt",
571 get_client_name(client_p, HIDE_IP));
572 continue;
575 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
576 "Confirming kill of %s (K-lined)",
577 get_client_name(client_p, HIDE_IP));
579 notify_banned_client(client_p, aconf, K_LINED);
580 continue;
585 /* check_dlines()
587 * inputs -
588 * outputs -
589 * side effects - all clients will be checked for dlines
591 void
592 check_dlines(void)
594 struct Client *client_p;
595 struct ConfItem *aconf;
596 dlink_node *ptr;
597 dlink_node *next_ptr;
599 DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
601 client_p = ptr->data;
603 if(IsMe(client_p))
604 continue;
606 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)) != NULL)
608 if(aconf->status & CONF_EXEMPTDLINE)
609 continue;
611 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
612 "Confirming kill of %s (D-lined)",
613 get_client_name(client_p, HIDE_IP));
615 notify_banned_client(client_p, aconf, D_LINED);
616 continue;
620 /* dlines need to be checked against unknowns too */
621 DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
623 client_p = ptr->data;
625 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)) != NULL)
627 if(aconf->status & CONF_EXEMPTDLINE)
628 continue;
630 notify_banned_client(client_p, aconf, D_LINED);
635 /* check_xlines
637 * inputs -
638 * outputs -
639 * side effects - all clients will be checked for xlines
641 void
642 check_xlines(void)
644 struct Client *client_p;
645 struct ConfItem *aconf;
646 dlink_node *ptr;
647 dlink_node *next_ptr;
649 DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
651 client_p = ptr->data;
653 if(IsMe(client_p) || !IsPerson(client_p))
654 continue;
656 if((aconf = find_xline(client_p->info, 1)) != NULL)
658 if(IsExemptKline(client_p))
660 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
661 "Not killing %s for X-line; client is K-line exempt",
662 get_client_name(client_p, HIDE_IP));
663 continue;
666 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
667 "Confirming kill of %s (X-lined)",
668 get_client_name(client_p, HIDE_IP));
670 (void) exit_client(client_p, client_p, &me, "Bad user info");
671 continue;
677 * update_client_exit_stats
679 * input - pointer to client
680 * output - NONE
681 * side effects -
683 static void
684 update_client_exit_stats(struct Client *client_p)
686 if(IsServer(client_p))
688 sendto_realops_snomask(SNO_EXTERNAL, L_ALL,
689 "Server %s split from %s",
690 client_p->name, client_p->servptr->name);
691 if(HasSentEob(client_p))
692 eob_count--;
694 else if(IsClient(client_p))
696 --Count.total;
697 if(IsOper(client_p))
698 --Count.oper;
699 if(IsInvisible(client_p))
700 --Count.invisi;
703 if(splitchecking && !splitmode)
704 check_splitmode(NULL);
708 * release_client_state
710 * input - pointer to client to release
711 * output - NONE
712 * side effects -
714 static void
715 release_client_state(struct Client *client_p)
717 if(client_p->user != NULL)
719 free_user(client_p->user, client_p); /* try this here */
721 if(client_p->serv)
723 if(client_p->serv->user != NULL)
724 free_user(client_p->serv->user, client_p);
725 if(client_p->serv->fullcaps)
726 MyFree(client_p->serv->fullcaps);
727 MyFree(client_p->serv);
732 * remove_client_from_list
733 * inputs - point to client to remove
734 * output - NONE
735 * side effects - taken the code from ExitOneClient() for this
736 * and placed it here. - avalon
738 static void
739 remove_client_from_list(struct Client *client_p)
741 s_assert(NULL != client_p);
743 if(client_p == NULL)
744 return;
746 /* A client made with make_client()
747 * is on the unknown_list until removed.
748 * If it =does= happen to exit before its removed from that list
749 * and its =not= on the global_client_list, it will core here.
750 * short circuit that case now -db
752 if(client_p->node.prev == NULL && client_p->node.next == NULL)
753 return;
755 dlinkDelete(&client_p->node, &global_client_list);
757 update_client_exit_stats(client_p);
762 * find_person - find person by (nick)name.
763 * inputs - pointer to name
764 * output - return client pointer
765 * side effects -
767 struct Client *
768 find_person(const char *name)
770 struct Client *c2ptr;
772 c2ptr = find_client(name);
774 if(c2ptr && IsPerson(c2ptr))
775 return (c2ptr);
776 return (NULL);
779 struct Client *
780 find_named_person(const char *name)
782 struct Client *c2ptr;
784 c2ptr = find_named_client(name);
786 if(c2ptr && IsPerson(c2ptr))
787 return (c2ptr);
788 return (NULL);
793 * find_chasing - find the client structure for a nick name (user)
794 * using history mechanism if necessary. If the client is not found,
795 * an error message (NO SUCH NICK) is generated. If the client was found
796 * through the history, chasing will be 1 and otherwise 0.
798 struct Client *
799 find_chasing(struct Client *source_p, const char *user, int *chasing)
801 struct Client *who;
803 if(MyClient(source_p))
804 who = find_named_person(user);
805 else
806 who = find_person(user);
808 if(chasing)
809 *chasing = 0;
811 if(who || IsDigit(*user))
812 return who;
814 if(!(who = get_history(user, (long) KILLCHASETIMELIMIT)))
816 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
817 form_str(ERR_NOSUCHNICK), user);
818 return (NULL);
820 if(chasing)
821 *chasing = 1;
822 return who;
826 * get_client_name - Return the name of the client
827 * for various tracking and
828 * admin purposes. The main purpose of this function is to
829 * return the "socket host" name of the client, if that
830 * differs from the advertised name (other than case).
831 * But, this can be used to any client structure.
833 * NOTE 1:
834 * Watch out the allocation of "nbuf", if either source_p->name
835 * or source_p->sockhost gets changed into pointers instead of
836 * directly allocated within the structure...
838 * NOTE 2:
839 * Function return either a pointer to the structure (source_p) or
840 * to internal buffer (nbuf). *NEVER* use the returned pointer
841 * to modify what it points!!!
844 const char *
845 get_client_name(struct Client *client, int showip)
847 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
849 s_assert(NULL != client);
850 if(client == NULL)
851 return NULL;
853 if(MyConnect(client))
855 if(!irccmp(client->name, client->host))
856 return client->name;
858 if(ConfigFileEntry.hide_spoof_ips &&
859 showip == SHOW_IP && IsIPSpoof(client))
860 showip = MASK_IP;
861 #ifdef HIDE_SERVERS_IPS
862 if(IsAnyServer(client))
863 showip = MASK_IP;
864 #endif
866 /* And finally, let's get the host information, ip or name */
867 switch (showip)
869 case SHOW_IP:
870 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
871 client->name, client->username,
872 client->sockhost);
873 break;
874 case MASK_IP:
875 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
876 client->name, client->username);
877 break;
878 default:
879 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
880 client->name, client->username, client->host);
882 return nbuf;
885 /* As pointed out by Adel Mezibra
886 * Neph|l|m@EFnet. Was missing a return here.
888 return client->name;
891 const char *
892 get_server_name(struct Client *target_p, int showip)
894 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
896 if(target_p == NULL)
897 return NULL;
899 if(!MyConnect(target_p) || !irccmp(target_p->name, target_p->host))
900 return target_p->name;
902 #ifdef HIDE_SERVERS_IPS
903 if(EmptyString(target_p->name))
905 ircsnprintf(nbuf, sizeof(nbuf), "[%s@255.255.255.255]",
906 target_p->username);
907 return nbuf;
909 else
910 return target_p->name;
911 #endif
913 switch (showip)
915 case SHOW_IP:
916 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
917 target_p->name, target_p->username,
918 target_p->sockhost);
919 break;
921 case MASK_IP:
922 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
923 target_p->name, target_p->username);
925 default:
926 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
927 target_p->name, target_p->username,
928 target_p->host);
931 return nbuf;
934 /* log_client_name()
936 * This version is the same as get_client_name, but doesnt contain the
937 * code that will hide IPs always. This should be used for logfiles.
939 const char *
940 log_client_name(struct Client *target_p, int showip)
942 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
944 if(target_p == NULL)
945 return NULL;
947 if(MyConnect(target_p))
949 if(irccmp(target_p->name, target_p->host) == 0)
950 return target_p->name;
952 switch (showip)
954 case SHOW_IP:
955 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", target_p->name,
956 target_p->username, target_p->sockhost);
957 break;
959 case MASK_IP:
960 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
961 target_p->name, target_p->username);
963 default:
964 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", target_p->name,
965 target_p->username, target_p->host);
968 return nbuf;
971 return target_p->name;
974 /* is_remote_connect - Returns whether a server was /connect'ed by a remote
975 * oper (send notices netwide) */
977 is_remote_connect(struct Client *client_p)
979 struct Client *oper;
981 if (client_p->serv == NULL)
982 return FALSE;
983 oper = find_named_person(client_p->serv->by);
984 return oper != NULL && IsOper(oper) && !MyConnect(oper);
987 static void
988 free_exited_clients(void *unused)
990 dlink_node *ptr, *next;
991 struct Client *target_p;
993 DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
995 target_p = ptr->data;
997 #ifdef DEBUG_EXITED_CLIENTS
999 struct abort_client *abt;
1000 dlink_node *aptr;
1001 int found = 0;
1003 DLINK_FOREACH(aptr, abort_list.head)
1005 abt = aptr->data;
1006 if(abt->client == target_p)
1008 s_assert(0);
1009 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1010 "On abort_list: %s stat: %u flags: %u/%u/%u handler: %c",
1011 target_p->name, (unsigned int) target_p->status,
1012 target_p->flags, target_p->flags2, target_p->operflags,
1013 target_p->handler);
1014 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1015 "Please report this to the ircd developers!");
1016 found++;
1020 if(found)
1022 dlinkDestroy(ptr, &dead_list);
1023 continue;
1026 #endif
1028 if(ptr->data == NULL)
1030 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1031 "Warning: null client on dead_list!");
1032 dlinkDestroy(ptr, &dead_list);
1033 continue;
1035 release_client_state(target_p);
1036 free_client(target_p);
1037 dlinkDestroy(ptr, &dead_list);
1040 #ifdef DEBUG_EXITED_CLIENTS
1041 DLINK_FOREACH_SAFE(ptr, next, dead_remote_list.head)
1043 target_p = ptr->data;
1045 if(ptr->data == NULL)
1047 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1048 "Warning: null client on dead_list!");
1049 dlinkDestroy(ptr, &dead_list);
1050 continue;
1052 release_client_state(target_p);
1053 free_client(target_p);
1054 dlinkDestroy(ptr, &dead_remote_list);
1056 #endif
1061 ** Recursively send QUITs and SQUITs for source_p and all its dependent clients
1062 ** and servers to those servers that need them. A server needs the client
1063 ** QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it
1064 ** isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once
1065 ** a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending
1066 ** on that one -orabidoo
1068 static void
1069 recurse_send_quits(struct Client *client_p, struct Client *source_p,
1070 struct Client *to, const char *comment1,
1071 const char *comment)
1073 struct Client *target_p;
1074 dlink_node *ptr, *ptr_next;
1075 /* If this server can handle quit storm (QS) removal
1076 * of dependents, just send the SQUIT
1079 if(IsCapable(to, CAP_QS))
1081 sendto_one(to, "SQUIT %s :%s",
1082 get_id(source_p, to), comment);
1084 else
1086 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
1088 target_p = ptr->data;
1089 sendto_one(to, ":%s QUIT :%s", target_p->name, comment1);
1091 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head)
1093 target_p = ptr->data;
1094 recurse_send_quits(client_p, target_p, to, comment1, comment);
1096 sendto_one(to, "SQUIT %s :%s", source_p->name, comment);
1101 ** Remove all clients that depend on source_p; assumes all (S)QUITs have
1102 ** already been sent. we make sure to exit a server's dependent clients
1103 ** and servers before the server itself; exit_one_client takes care of
1104 ** actually removing things off llists. tweaked from +CSr31 -orabidoo
1107 * added sanity test code.... source_p->serv might be NULL...
1109 static void
1110 recurse_remove_clients(struct Client *source_p, const char *comment)
1112 struct Client *target_p;
1113 dlink_node *ptr, *ptr_next;
1115 if(IsMe(source_p))
1116 return;
1118 if(source_p->serv == NULL) /* oooops. uh this is actually a major bug */
1119 return;
1121 /* this is very ugly, but it saves cpu :P */
1122 if(ConfigFileEntry.nick_delay > 0)
1124 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
1126 target_p = ptr->data;
1127 target_p->flags |= FLAGS_KILLED;
1128 add_nd_entry(target_p->name);
1130 if(!IsDead(target_p) && !IsClosing(target_p))
1131 exit_remote_client(NULL, target_p, &me, comment);
1134 else
1136 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
1138 target_p = ptr->data;
1139 target_p->flags |= FLAGS_KILLED;
1141 if(!IsDead(target_p) && !IsClosing(target_p))
1142 exit_remote_client(NULL, target_p, &me, comment);
1146 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head)
1148 target_p = ptr->data;
1149 recurse_remove_clients(target_p, comment);
1150 qs_server(NULL, target_p, &me, comment);
1155 ** Remove *everything* that depends on source_p, from all lists, and sending
1156 ** all necessary QUITs and SQUITs. source_p itself is still on the lists,
1157 ** and its SQUITs have been sent except for the upstream one -orabidoo
1159 static void
1160 remove_dependents(struct Client *client_p,
1161 struct Client *source_p,
1162 struct Client *from, const char *comment, const char *comment1)
1164 struct Client *to;
1165 dlink_node *ptr, *next;
1167 DLINK_FOREACH_SAFE(ptr, next, serv_list.head)
1169 to = ptr->data;
1171 if(IsMe(to) || to == source_p->from ||
1172 (to == client_p && IsCapable(to, CAP_QS)))
1173 continue;
1175 recurse_send_quits(client_p, source_p, to, comment1, comment);
1178 recurse_remove_clients(source_p, comment1);
1181 void
1182 exit_aborted_clients(void *unused)
1184 struct abort_client *abt;
1185 dlink_node *ptr, *next;
1186 DLINK_FOREACH_SAFE(ptr, next, abort_list.head)
1188 abt = ptr->data;
1190 #ifdef DEBUG_EXITED_CLIENTS
1192 if(dlinkFind(abt->client, &dead_list))
1194 s_assert(0);
1195 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1196 "On dead_list: %s stat: %u flags: %u/%u/%u handler: %c",
1197 abt->client->name, (unsigned int) abt->client->status,
1198 abt->client->flags, abt->client->flags2, abt->client->operflags,
1199 abt->client->handler);
1200 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1201 "Please report this to the ircd developers!");
1202 continue;
1205 #endif
1207 s_assert(*((unsigned long*)abt->client) != 0xdeadbeef); /* This is lame but its a debug thing */
1208 dlinkDelete(ptr, &abort_list);
1210 if(IsAnyServer(abt->client))
1211 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
1212 "Closing link to %s: %s",
1213 get_server_name(abt->client, HIDE_IP), abt->notice);
1215 /* its no longer on abort list - we *must* remove
1216 * FLAGS_CLOSING otherwise exit_client() will not run --fl
1218 abt->client->flags &= ~FLAGS_CLOSING;
1219 exit_client(abt->client, abt->client, &me, abt->notice);
1220 MyFree(abt);
1226 * dead_link - Adds client to a list of clients that need an exit_client()
1229 void
1230 dead_link(struct Client *client_p)
1232 struct abort_client *abt;
1234 s_assert(!IsMe(client_p));
1235 if(IsDead(client_p) || IsClosing(client_p) || IsMe(client_p))
1236 return;
1238 abt = (struct abort_client *) MyMalloc(sizeof(struct abort_client));
1240 if(client_p->flags & FLAGS_SENDQEX)
1241 strlcpy(abt->notice, "Max SendQ exceeded", sizeof(abt->notice));
1242 else
1243 ircsnprintf(abt->notice, sizeof(abt->notice), "Write error: %s", strerror(errno));
1245 abt->client = client_p;
1246 SetIOError(client_p);
1247 SetDead(client_p);
1248 SetClosing(client_p);
1249 dlinkAdd(abt, &abt->node, &abort_list);
1253 /* This does the remove of the user from channels..local or remote */
1254 static inline void
1255 exit_generic_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1256 const char *comment)
1258 dlink_node *ptr, *next_ptr;
1260 if(IsOper(source_p))
1261 dlinkFindDestroy(source_p, &oper_list);
1263 sendto_common_channels_local(source_p, ":%s!%s@%s QUIT :%s",
1264 source_p->name,
1265 source_p->username, source_p->host, comment);
1267 remove_user_from_channels(source_p);
1269 /* Should not be in any channels now */
1270 s_assert(source_p->user->channel.head == NULL);
1272 /* Clean up invitefield */
1273 DLINK_FOREACH_SAFE(ptr, next_ptr, source_p->user->invited.head)
1275 del_invite(ptr->data, source_p);
1278 /* Clean up allow lists */
1279 del_all_accepts(source_p);
1281 add_history(source_p, 0);
1282 off_history(source_p);
1284 monitor_signoff(source_p);
1286 if(has_id(source_p))
1287 del_from_id_hash(source_p->id, source_p);
1289 del_from_hostname_hash(source_p->orighost, source_p);
1290 del_from_client_hash(source_p->name, source_p);
1291 remove_client_from_list(source_p);
1295 * Assumes IsPerson(source_p) && !MyConnect(source_p)
1298 static int
1299 exit_remote_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1300 const char *comment)
1302 exit_generic_client(client_p, source_p, from, comment);
1304 if(source_p->servptr && source_p->servptr->serv)
1306 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->users);
1309 if((source_p->flags & FLAGS_KILLED) == 0)
1311 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1312 ":%s QUIT :%s", use_id(source_p), comment);
1313 sendto_server(client_p, NULL, NOCAPS, CAP_TS6,
1314 ":%s QUIT :%s", source_p->name, comment);
1317 SetDead(source_p);
1318 #ifdef DEBUG_EXITED_CLIENTS
1319 dlinkAddAlloc(source_p, &dead_remote_list);
1320 #else
1321 dlinkAddAlloc(source_p, &dead_list);
1322 #endif
1323 return(CLIENT_EXITED);
1327 * This assumes IsUnknown(source_p) == TRUE and MyConnect(source_p) == TRUE
1330 static int
1331 exit_unknown_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1332 const char *comment)
1334 delete_auth_queries(source_p);
1335 client_flush_input(source_p);
1336 dlinkDelete(&source_p->localClient->tnode, &unknown_list);
1338 if(!IsIOError(source_p))
1339 sendto_one(source_p, "ERROR :Closing Link: 127.0.0.1 (%s)", comment);
1341 close_connection(source_p);
1343 if(has_id(source_p))
1344 del_from_id_hash(source_p->id, source_p);
1346 del_from_hostname_hash(source_p->host, source_p);
1347 del_from_client_hash(source_p->name, source_p);
1348 remove_client_from_list(source_p);
1349 free_pre_client(source_p);
1350 SetDead(source_p);
1351 dlinkAddAlloc(source_p, &dead_list);
1353 /* Note that we don't need to add unknowns to the dead_list */
1354 return(CLIENT_EXITED);
1357 static int
1358 exit_remote_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1359 const char *comment)
1361 static char comment1[(HOSTLEN*2)+2];
1362 static char newcomment[BUFSIZE];
1363 struct Client *target_p;
1365 if(ConfigServerHide.flatten_links)
1366 strcpy(comment1, "*.net *.split");
1367 else
1369 if((source_p->serv) && (source_p->serv->up))
1370 strcpy(comment1, source_p->serv->up);
1371 else
1372 strcpy(comment1, "<Unknown>");
1374 strcat(comment1, " ");
1375 strcat(comment1, source_p->name);
1377 if (IsPerson(from))
1378 ircsnprintf(newcomment, sizeof(newcomment), "by %s: %s",
1379 from->name, comment);
1381 if(source_p->serv != NULL)
1382 remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1);
1384 if(source_p->servptr && source_p->servptr->serv)
1385 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1386 else
1387 s_assert(0);
1389 dlinkFindDestroy(source_p, &global_serv_list);
1390 target_p = source_p->from;
1392 if(target_p != NULL && IsServer(target_p) && target_p != client_p &&
1393 !IsMe(target_p) && (source_p->flags & FLAGS_KILLED) == 0)
1395 sendto_one(target_p, ":%s SQUIT %s :%s",
1396 get_id(from, target_p), get_id(source_p, target_p),
1397 comment);
1400 if(has_id(source_p))
1401 del_from_id_hash(source_p->id, source_p);
1403 del_from_client_hash(source_p->name, source_p);
1404 remove_client_from_list(source_p);
1406 SetDead(source_p);
1407 #ifdef DEBUG_EXITED_CLIENTS
1408 dlinkAddAlloc(source_p, &dead_remote_list);
1409 #else
1410 dlinkAddAlloc(source_p, &dead_list);
1411 #endif
1412 return 0;
1415 static int
1416 qs_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1417 const char *comment)
1419 struct Client *target_p;
1421 if(source_p->servptr && source_p->servptr->serv)
1422 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1423 else
1424 s_assert(0);
1426 dlinkFindDestroy(source_p, &global_serv_list);
1427 target_p = source_p->from;
1429 if(has_id(source_p))
1430 del_from_id_hash(source_p->id, source_p);
1432 del_from_client_hash(source_p->name, source_p);
1433 remove_client_from_list(source_p);
1435 SetDead(source_p);
1436 dlinkAddAlloc(source_p, &dead_list);
1437 return 0;
1440 static int
1441 exit_local_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1442 const char *comment)
1444 static char comment1[(HOSTLEN*2)+2];
1445 static char newcomment[BUFSIZE];
1446 unsigned int sendk, recvk;
1448 dlinkDelete(&source_p->localClient->tnode, &serv_list);
1449 dlinkFindDestroy(source_p, &global_serv_list);
1451 unset_chcap_usage_counts(source_p);
1452 sendk = source_p->localClient->sendK;
1453 recvk = source_p->localClient->receiveK;
1455 /* Always show source here, so the server notices show
1456 * which side initiated the split -- jilles
1458 ircsnprintf(newcomment, sizeof(newcomment), "by %s: %s",
1459 from == source_p ? me.name : from->name, comment);
1460 if (!IsIOError(source_p))
1461 sendto_one(source_p, "SQUIT %s :%s", use_id(source_p),
1462 newcomment);
1463 if(client_p != NULL && source_p != client_p && !IsIOError(source_p))
1465 sendto_one(source_p, "ERROR :Closing Link: 127.0.0.1 %s (%s)",
1466 source_p->name, comment);
1469 if(source_p->localClient->ctrlfd >= 0)
1471 comm_close(source_p->localClient->ctrlfd);
1472 source_p->localClient->ctrlfd = -1;
1475 if(source_p->servptr && source_p->servptr->serv)
1476 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1477 else
1478 s_assert(0);
1481 close_connection(source_p);
1483 if(ConfigServerHide.flatten_links)
1484 strcpy(comment1, "*.net *.split");
1485 else
1487 if((source_p->serv) && (source_p->serv->up))
1488 strcpy(comment1, source_p->serv->up);
1489 else
1490 strcpy(comment1, "<Unknown>");
1492 strcat(comment1, " ");
1493 strcat(comment1, source_p->name);
1496 if(source_p->serv != NULL)
1497 remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1);
1499 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s was connected"
1500 " for %ld seconds. %d/%d sendK/recvK.",
1501 source_p->name, CurrentTime - source_p->localClient->firsttime, sendk, recvk);
1503 ilog(L_SERVER, "%s was connected for %ld seconds. %d/%d sendK/recvK.",
1504 source_p->name, CurrentTime - source_p->localClient->firsttime, sendk, recvk);
1506 if(has_id(source_p))
1507 del_from_id_hash(source_p->id, source_p);
1509 del_from_client_hash(source_p->name, source_p);
1510 remove_client_from_list(source_p);
1512 SetDead(source_p);
1513 dlinkAddAlloc(source_p, &dead_list);
1514 return 0;
1519 * This assumes IsPerson(source_p) == TRUE && MyConnect(source_p) == TRUE
1522 static int
1523 exit_local_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1524 const char *comment)
1526 unsigned long on_for;
1528 exit_generic_client(client_p, source_p, from, comment);
1529 clear_monitor(source_p);
1531 s_assert(IsPerson(source_p));
1532 client_flush_input(source_p);
1533 dlinkDelete(&source_p->localClient->tnode, &lclient_list);
1534 dlinkDelete(&source_p->lnode, &me.serv->users);
1536 if(IsOper(source_p))
1537 dlinkFindDestroy(source_p, &local_oper_list);
1539 sendto_realops_snomask(SNO_CCONN, L_NETWIDE,
1540 "Client exiting: %s (%s@%s) [%s] [%s]",
1541 source_p->name,
1542 source_p->username, source_p->host, comment,
1543 show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255");
1545 sendto_realops_snomask(SNO_CCONNEXT, L_NETWIDE,
1546 "CLIEXIT %s %s %s %s 0 %s",
1547 source_p->name, source_p->username, source_p->host,
1548 show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255",
1549 comment);
1551 on_for = CurrentTime - source_p->localClient->firsttime;
1553 ilog(L_USER, "%s (%3lu:%02lu:%02lu): %s!%s@%s %d/%d",
1554 myctime(CurrentTime), on_for / 3600,
1555 (on_for % 3600) / 60, on_for % 60,
1556 source_p->name, source_p->username, source_p->host,
1557 source_p->localClient->sendK, source_p->localClient->receiveK);
1559 sendto_one(source_p, "ERROR :Closing Link: %s (%s)", source_p->host, comment);
1560 close_connection(source_p);
1562 if((source_p->flags & FLAGS_KILLED) == 0)
1564 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1565 ":%s QUIT :%s", use_id(source_p), comment);
1566 sendto_server(client_p, NULL, NOCAPS, CAP_TS6,
1567 ":%s QUIT :%s", source_p->name, comment);
1570 SetDead(source_p);
1571 dlinkAddAlloc(source_p, &dead_list);
1572 return(CLIENT_EXITED);
1577 ** exit_client - This is old "m_bye". Name changed, because this is not a
1578 ** protocol function, but a general server utility function.
1580 ** This function exits a client of *any* type (user, server, etc)
1581 ** from this server. Also, this generates all necessary prototol
1582 ** messages that this exit may cause.
1584 ** 1) If the client is a local client, then this implicitly
1585 ** exits all other clients depending on this connection (e.g.
1586 ** remote clients having 'from'-field that points to this.
1588 ** 2) If the client is a remote client, then only this is exited.
1590 ** For convenience, this function returns a suitable value for
1591 ** m_function return value:
1593 ** CLIENT_EXITED if (client_p == source_p)
1594 ** 0 if (client_p != source_p)
1597 exit_client(struct Client *client_p, /* The local client originating the
1598 * exit or NULL, if this exit is
1599 * generated by this server for
1600 * internal reasons.
1601 * This will not get any of the
1602 * generated messages. */
1603 struct Client *source_p, /* Client exiting */
1604 struct Client *from, /* Client firing off this Exit,
1605 * never NULL! */
1606 const char *comment /* Reason for the exit */
1609 hook_data_client_exit hdata;
1610 if(IsClosing(source_p))
1611 return -1;
1613 /* note, this HAS to be here, when we exit a client we attempt to
1614 * send them data, if this generates a write error we must *not* add
1615 * them to the abort list --fl
1617 SetClosing(source_p);
1619 hdata.local_link = client_p;
1620 hdata.target = source_p;
1621 hdata.from = from;
1622 hdata.comment = comment;
1623 call_hook(h_client_exit, &hdata);
1625 if(MyConnect(source_p))
1627 /* Local clients of various types */
1628 if(IsPerson(source_p))
1629 return exit_local_client(client_p, source_p, from, comment);
1630 else if(IsServer(source_p))
1631 return exit_local_server(client_p, source_p, from, comment);
1632 /* IsUnknown || IsConnecting || IsHandShake */
1633 else if(!IsReject(source_p))
1634 return exit_unknown_client(client_p, source_p, from, comment);
1636 else
1638 /* Remotes */
1639 if(IsPerson(source_p))
1640 return exit_remote_client(client_p, source_p, from, comment);
1641 else if(IsServer(source_p))
1642 return exit_remote_server(client_p, source_p, from, comment);
1645 return -1;
1649 * Count up local client memory
1652 /* XXX one common Client list now */
1653 void
1654 count_local_client_memory(size_t * count, size_t * local_client_memory_used)
1656 size_t lusage;
1657 BlockHeapUsage(lclient_heap, count, NULL, &lusage);
1658 *local_client_memory_used = lusage + (*count * (sizeof(MemBlock) + sizeof(struct Client)));
1662 * Count up remote client memory
1664 void
1665 count_remote_client_memory(size_t * count, size_t * remote_client_memory_used)
1667 size_t lcount, rcount;
1668 BlockHeapUsage(lclient_heap, &lcount, NULL, NULL);
1669 BlockHeapUsage(client_heap, &rcount, NULL, NULL);
1670 *count = rcount - lcount;
1671 *remote_client_memory_used = *count * (sizeof(MemBlock) + sizeof(struct Client));
1676 * accept processing, this adds a form of "caller ID" to ircd
1678 * If a client puts themselves into "caller ID only" mode,
1679 * only clients that match a client pointer they have put on
1680 * the accept list will be allowed to message them.
1682 * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ]
1684 * [target.allow_list] -> [ source1 ] -> [source2 ]
1686 * i.e. a target will have a link list of source pointers it will allow
1687 * each source client then has a back pointer pointing back
1688 * to the client that has it on its accept list.
1689 * This allows for exit_one_client to remove these now bogus entries
1690 * from any client having an accept on them.
1693 * del_all_accepts
1695 * inputs - pointer to exiting client
1696 * output - NONE
1697 * side effects - Walk through given clients allow_list and on_allow_list
1698 * remove all references to this client
1700 void
1701 del_all_accepts(struct Client *client_p)
1703 dlink_node *ptr;
1704 dlink_node *next_ptr;
1705 struct Client *target_p;
1707 if(MyClient(client_p) && client_p->localClient->allow_list.head)
1709 /* clear this clients accept list, and remove them from
1710 * everyones on_accept_list
1712 DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->allow_list.head)
1714 target_p = ptr->data;
1715 dlinkFindDestroy(client_p, &target_p->on_allow_list);
1716 dlinkDestroy(ptr, &client_p->localClient->allow_list);
1720 /* remove this client from everyones accept list */
1721 DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head)
1723 target_p = ptr->data;
1724 dlinkFindDestroy(client_p, &target_p->localClient->allow_list);
1725 dlinkDestroy(ptr, &client_p->on_allow_list);
1730 * show_ip() - asks if the true IP shoudl be shown when source is
1731 * askin for info about target
1733 * Inputs - source_p who is asking
1734 * - target_p who do we want the info on
1735 * Output - returns 1 if clear IP can be showed, otherwise 0
1736 * Side Effects - none
1740 show_ip(struct Client *source_p, struct Client *target_p)
1742 if(IsAnyServer(target_p))
1744 #ifndef HIDE_SERVERS_IPS
1745 if(source_p == NULL || IsOperStaffer(source_p))
1746 return 1;
1747 #endif
1748 return 0;
1750 else if(IsIPSpoof(target_p))
1752 /* source == NULL indicates message is being sent
1753 * to local opers.
1755 if(!ConfigFileEntry.hide_spoof_ips &&
1756 (source_p == NULL || MyOper(source_p)))
1757 return 1;
1758 return 0;
1760 else if(IsDynSpoof(target_p) && (source_p != NULL && !IsAuspex(source_p)))
1761 return 0;
1762 else
1763 return 1;
1767 show_ip_conf(struct ConfItem *aconf, struct Client *source_p)
1769 if(IsConfDoSpoofIp(aconf))
1771 if(!ConfigFileEntry.hide_spoof_ips && MyOper(source_p))
1772 return 1;
1774 return 0;
1776 else
1777 return 1;
1781 * initUser
1783 * inputs - none
1784 * outputs - none
1786 * side effects - Creates a block heap for struct Users
1789 static BlockHeap *user_heap;
1790 void
1791 initUser(void)
1793 user_heap = BlockHeapCreate(sizeof(struct User), USER_HEAP_SIZE);
1794 if(!user_heap)
1795 outofmemory();
1799 * make_user
1801 * inputs - pointer to client struct
1802 * output - pointer to struct User
1803 * side effects - add's an User information block to a client
1804 * if it was not previously allocated.
1806 struct User *
1807 make_user(struct Client *client_p)
1809 struct User *user;
1811 user = client_p->user;
1812 if(!user)
1814 user = (struct User *) BlockHeapAlloc(user_heap);
1815 user->refcnt = 1;
1816 client_p->user = user;
1818 return user;
1822 * make_server
1824 * inputs - pointer to client struct
1825 * output - pointer to server_t
1826 * side effects - add's an Server information block to a client
1827 * if it was not previously allocated.
1829 server_t *
1830 make_server(struct Client *client_p)
1832 server_t *serv = client_p->serv;
1834 if(!serv)
1836 serv = (server_t *) MyMalloc(sizeof(server_t));
1837 client_p->serv = serv;
1839 return client_p->serv;
1843 * free_user
1845 * inputs - pointer to user struct
1846 * - pointer to client struct
1847 * output - none
1848 * side effects - Decrease user reference count by one and release block,
1849 * if count reaches 0
1851 void
1852 free_user(struct User *user, struct Client *client_p)
1854 if(--user->refcnt <= 0)
1856 if(user->away)
1857 MyFree((char *) user->away);
1859 * sanity check
1861 if(user->refcnt < 0 || user->invited.head || user->channel.head)
1863 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1864 "* %#lx user (%s!%s@%s) %#lx %#lx %#lx %lu %d *",
1865 (unsigned long) client_p,
1866 client_p ? client_p->
1867 name : "<noname>",
1868 client_p->username,
1869 client_p->host,
1870 (unsigned long) user,
1871 (unsigned long) user->invited.head,
1872 (unsigned long) user->channel.head,
1873 dlink_list_length(&user->channel),
1874 user->refcnt);
1875 s_assert(!user->refcnt);
1876 s_assert(!user->invited.head);
1877 s_assert(!user->channel.head);
1880 BlockHeapFree(user_heap, user);
1884 void
1885 init_uid(void)
1887 int i;
1889 for(i = 0; i < 3; i++)
1890 current_uid[i] = me.id[i];
1892 for(i = 3; i < 9; i++)
1893 current_uid[i] = 'A';
1895 current_uid[9] = '\0';
1899 char *
1900 generate_uid(void)
1902 int i;
1904 for(i = 8; i > 3; i--)
1906 if(current_uid[i] == 'Z')
1908 current_uid[i] = '0';
1909 return current_uid;
1911 else if(current_uid[i] != '9')
1913 current_uid[i]++;
1914 return current_uid;
1916 else
1917 current_uid[i] = 'A';
1920 /* if this next if() triggers, we're fucked. */
1921 if(current_uid[3] == 'Z')
1923 current_uid[i] = 'A';
1924 s_assert(0);
1926 else
1927 current_uid[i]++;
1929 return current_uid;
1933 * close_connection
1934 * Close the physical connection. This function must make
1935 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
1937 void
1938 close_connection(struct Client *client_p)
1940 s_assert(client_p != NULL);
1941 if(client_p == NULL)
1942 return;
1944 s_assert(MyConnect(client_p));
1945 if(!MyConnect(client_p))
1946 return;
1948 if(IsServer(client_p))
1950 struct server_conf *server_p;
1952 ServerStats->is_sv++;
1953 ServerStats->is_sbs += client_p->localClient->sendB;
1954 ServerStats->is_sbr += client_p->localClient->receiveB;
1955 ServerStats->is_sks += client_p->localClient->sendK;
1956 ServerStats->is_skr += client_p->localClient->receiveK;
1957 ServerStats->is_sti += CurrentTime - client_p->localClient->firsttime;
1958 if(ServerStats->is_sbs > 2047)
1960 ServerStats->is_sks += (ServerStats->is_sbs >> 10);
1961 ServerStats->is_sbs &= 0x3ff;
1963 if(ServerStats->is_sbr > 2047)
1965 ServerStats->is_skr += (ServerStats->is_sbr >> 10);
1966 ServerStats->is_sbr &= 0x3ff;
1970 * If the connection has been up for a long amount of time, schedule
1971 * a 'quick' reconnect, else reset the next-connect cycle.
1973 if((server_p = find_server_conf(client_p->name)) != NULL)
1976 * Reschedule a faster reconnect, if this was a automatically
1977 * connected configuration entry. (Note that if we have had
1978 * a rehash in between, the status has been changed to
1979 * CONF_ILLEGAL). But only do this if it was a "good" link.
1981 server_p->hold = time(NULL);
1982 server_p->hold +=
1983 (server_p->hold - client_p->localClient->lasttime >
1984 HANGONGOODLINK) ? HANGONRETRYDELAY : ConFreq(server_p->class);
1988 else if(IsClient(client_p))
1990 ServerStats->is_cl++;
1991 ServerStats->is_cbs += client_p->localClient->sendB;
1992 ServerStats->is_cbr += client_p->localClient->receiveB;
1993 ServerStats->is_cks += client_p->localClient->sendK;
1994 ServerStats->is_ckr += client_p->localClient->receiveK;
1995 ServerStats->is_cti += CurrentTime - client_p->localClient->firsttime;
1996 if(ServerStats->is_cbs > 2047)
1998 ServerStats->is_cks += (ServerStats->is_cbs >> 10);
1999 ServerStats->is_cbs &= 0x3ff;
2001 if(ServerStats->is_cbr > 2047)
2003 ServerStats->is_ckr += (ServerStats->is_cbr >> 10);
2004 ServerStats->is_cbr &= 0x3ff;
2007 else
2008 ServerStats->is_ni++;
2010 if(-1 < client_p->localClient->fd)
2012 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
2013 if(!IsIOError(client_p))
2014 send_queued_write(client_p->localClient->fd, client_p);
2016 comm_close(client_p->localClient->fd);
2017 client_p->localClient->fd = -1;
2020 if(HasServlink(client_p))
2022 if(client_p->localClient->fd > -1)
2024 comm_close(client_p->localClient->ctrlfd);
2025 client_p->localClient->ctrlfd = -1;
2029 linebuf_donebuf(&client_p->localClient->buf_sendq);
2030 linebuf_donebuf(&client_p->localClient->buf_recvq);
2031 detach_conf(client_p);
2033 /* XXX shouldnt really be done here. */
2034 detach_server_conf(client_p);
2036 client_p->from = NULL; /* ...this should catch them! >:) --msa */
2037 ClearMyConnect(client_p);
2038 SetIOError(client_p);
2043 void
2044 error_exit_client(struct Client *client_p, int error)
2047 * ...hmm, with non-blocking sockets we might get
2048 * here from quite valid reasons, although.. why
2049 * would select report "data available" when there
2050 * wasn't... so, this must be an error anyway... --msa
2051 * actually, EOF occurs when read() returns 0 and
2052 * in due course, select() returns that fd as ready
2053 * for reading even though it ends up being an EOF. -avalon
2055 char errmsg[255];
2056 int current_error = comm_get_sockerr(client_p->localClient->fd);
2058 SetIOError(client_p);
2060 if(IsServer(client_p) || IsHandshake(client_p))
2062 int connected = CurrentTime - client_p->localClient->firsttime;
2064 if(error == 0)
2066 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL,
2067 "Server %s closed the connection",
2068 get_server_name(client_p, SHOW_IP));
2070 ilog(L_SERVER, "Server %s closed the connection",
2071 log_client_name(client_p, SHOW_IP));
2073 else
2075 sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL,
2076 "Lost connection to %s: %s",
2077 client_p->name, strerror(current_error));
2078 ilog(L_SERVER, "Lost connection to %s: %s",
2079 log_client_name(client_p, SHOW_IP), strerror(current_error));
2082 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
2083 "%s had been connected for %d day%s, %2d:%02d:%02d",
2084 client_p->name, connected / 86400,
2085 (connected / 86400 == 1) ? "" : "s",
2086 (connected % 86400) / 3600,
2087 (connected % 3600) / 60, connected % 60);
2090 if(error == 0)
2091 strlcpy(errmsg, "Remote host closed the connection", sizeof(errmsg));
2092 else
2093 ircsnprintf(errmsg, sizeof(errmsg), "Read error: %s", strerror(current_error));
2095 exit_client(client_p, client_p, &me, errmsg);