Initial import
[ratbox-ambernet.git] / src / client.c
blobeaf2ed40893bf495fd808f6065fa17fb6ac8c504
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 * $Id: client.c 23955 2007-05-14 17:22:36Z leeh $
26 #include "stdinc.h"
27 #include "config.h"
29 #include "tools.h"
30 #include "client.h"
31 #include "class.h"
32 #include "common.h"
33 #include "event.h"
34 #include "hash.h"
35 #include "irc_string.h"
36 #include "sprintf_irc.h"
37 #include "ircd.h"
38 #include "s_gline.h"
39 #include "numeric.h"
40 #include "packet.h"
41 #include "s_auth.h"
42 #include "commio.h"
43 #include "s_conf.h"
44 #include "s_newconf.h"
45 #include "s_log.h"
46 #include "s_serv.h"
47 #include "s_stats.h"
48 #include "send.h"
49 #include "whowas.h"
50 #include "s_user.h"
51 #include "linebuf.h"
52 #include "hash.h"
53 #include "memory.h"
54 #include "hostmask.h"
55 #include "balloc.h"
56 #include "listener.h"
57 #include "hook.h"
58 #include "msg.h"
59 #include "monitor.h"
60 #include "reject.h"
62 #define DEBUG_EXITED_CLIENTS
64 static void check_pings_list(dlink_list * list);
65 static void check_unknowns_list(dlink_list * list);
66 static void free_exited_clients(void *unused);
67 static void exit_aborted_clients(void *unused);
69 static int exit_remote_client(struct Client *, struct Client *, struct Client *,const char *);
70 static int exit_remote_server(struct Client *, struct Client *, struct Client *,const char *);
71 static int exit_local_client(struct Client *, struct Client *, struct Client *,const char *);
72 static int exit_unknown_client(struct Client *, struct Client *, struct Client *,const char *);
73 static int exit_local_server(struct Client *, struct Client *, struct Client *,const char *);
74 static int qs_server(struct Client *, struct Client *, struct Client *, const char *comment);
76 static EVH check_pings;
78 static BlockHeap *client_heap = NULL;
79 static BlockHeap *lclient_heap = NULL;
80 static BlockHeap *user_heap;
82 static char current_uid[IDLEN];
84 enum
86 D_LINED,
87 K_LINED,
88 G_LINED
91 dlink_list dead_list;
92 #ifdef DEBUG_EXITED_CLIENTS
93 static dlink_list dead_remote_list;
94 #endif
96 struct abort_client
98 dlink_node node;
99 struct Client *client;
100 char notice[REASONLEN];
103 static dlink_list abort_list;
107 * init_client
109 * inputs - NONE
110 * output - NONE
111 * side effects - initialize client free memory
113 void
114 init_client(void)
117 * start off the check ping event .. -- adrian
118 * Every 30 seconds is plenty -- db
120 client_heap = BlockHeapCreate(sizeof(struct Client), CLIENT_HEAP_SIZE);
121 lclient_heap = BlockHeapCreate(sizeof(struct LocalUser), LCLIENT_HEAP_SIZE);
122 user_heap = BlockHeapCreate(sizeof(struct User), USER_HEAP_SIZE);
123 eventAddIsh("check_pings", check_pings, NULL, 30);
124 eventAddIsh("free_exited_clients", &free_exited_clients, NULL, 4);
125 eventAddIsh("exit_aborted_clients", exit_aborted_clients, NULL, 1);
126 eventAdd("flood_recalc", flood_recalc, NULL, 1);
131 * make_client - create a new Client struct and set it to initial state.
133 * from == NULL, create local client (a client connected
134 * to a socket).
136 * from, create remote client (behind a socket
137 * associated with the client defined by
138 * 'from'). ('from' is a local client!!).
140 struct Client *
141 make_client(struct Client *from)
143 struct Client *client_p = NULL;
144 struct LocalUser *localClient;
146 client_p = BlockHeapAlloc(client_heap);
148 if(from == NULL)
150 client_p->from = client_p; /* 'from' of local client is self! */
152 localClient = (struct LocalUser *) BlockHeapAlloc(lclient_heap);
153 SetMyConnect(client_p);
154 client_p->localClient = localClient;
156 client_p->localClient->lasttime = client_p->localClient->firsttime = CurrentTime;
158 client_p->localClient->fd = -1;
160 /* as good a place as any... */
161 dlinkAdd(client_p, &client_p->localClient->tnode, &unknown_list);
163 else
164 { /* from is not NULL */
165 client_p->localClient = NULL;
166 client_p->from = from; /* 'from' of local client is self! */
169 SetUnknown(client_p);
170 strcpy(client_p->username, "unknown");
172 return client_p;
175 static void
176 free_local_client(struct Client *client_p)
178 s_assert(NULL != client_p);
179 s_assert(&me != client_p);
181 if(client_p->localClient == NULL)
182 return;
185 * clean up extra sockets from P-lines which have been discarded.
187 if(client_p->localClient->listener)
189 s_assert(0 < client_p->localClient->listener->ref_count);
190 if(0 == --client_p->localClient->listener->ref_count
191 && !client_p->localClient->listener->active)
192 free_listener(client_p->localClient->listener);
193 client_p->localClient->listener = 0;
196 if(client_p->localClient->fd >= 0)
197 comm_close(client_p->localClient->fd);
199 if(client_p->localClient->passwd)
201 memset(client_p->localClient->passwd, 0,
202 strlen(client_p->localClient->passwd));
203 MyFree(client_p->localClient->passwd);
206 MyFree(client_p->localClient->challenge);
207 MyFree(client_p->localClient->fullcaps);
208 MyFree(client_p->localClient->opername);
209 MyFree(client_p->localClient->slink);
211 BlockHeapFree(lclient_heap, client_p->localClient);
212 client_p->localClient = NULL;
215 void
216 free_client(struct Client *client_p)
218 s_assert(NULL != client_p);
219 s_assert(&me != client_p);
220 free_local_client(client_p);
221 BlockHeapFree(client_heap, client_p);
225 * check_pings - go through the local client list and check activity
226 * kill off stuff that should die
228 * inputs - NOT USED (from event)
229 * output - next time_t when check_pings() should be called again
230 * side effects -
233 * A PING can be sent to clients as necessary.
235 * Client/Server ping outs are handled.
239 * Addon from adrian. We used to call this after nextping seconds,
240 * however I've changed it to run once a second. This is only for
241 * PING timeouts, not K/etc-line checks (thanks dianora!). Having it
242 * run once a second makes life a lot easier - when a new client connects
243 * and they need a ping in 4 seconds, if nextping was set to 20 seconds
244 * we end up waiting 20 seconds. This is stupid. :-)
245 * I will optimise (hah!) check_pings() once I've finished working on
246 * tidying up other network IO evilnesses.
247 * -- adrian
250 static void
251 check_pings(void *notused)
253 check_pings_list(&lclient_list);
254 check_pings_list(&serv_list);
255 check_unknowns_list(&unknown_list);
259 * Check_pings_list()
261 * inputs - pointer to list to check
262 * output - NONE
263 * side effects -
265 static void
266 check_pings_list(dlink_list * list)
268 char scratch[32]; /* way too generous but... */
269 struct Client *client_p; /* current local client_p being examined */
270 int ping = 0; /* ping time value from client */
271 dlink_node *ptr, *next_ptr;
273 DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
275 client_p = ptr->data;
278 ** Note: No need to notify opers here. It's
279 ** already done when "FLAGS_DEADSOCKET" is set.
281 if(!MyConnect(client_p) || IsDead(client_p))
282 continue;
284 if(IsPerson(client_p))
286 if(!IsExemptKline(client_p) &&
287 GlobalSetOptions.idletime &&
288 !IsOper(client_p) &&
289 ((CurrentTime - client_p->localClient->last) > GlobalSetOptions.idletime))
291 struct ConfItem *aconf;
293 aconf = make_conf();
294 aconf->status = CONF_KILL;
296 DupString(aconf->host, client_p->host);
297 DupString(aconf->passwd, "idle exceeder");
298 DupString(aconf->user, client_p->username);
299 aconf->port = 0;
300 aconf->hold = CurrentTime + 60;
301 add_temp_kline(aconf);
302 sendto_realops_flags(UMODE_ALL, L_ALL,
303 "Idle time limit exceeded for %s - temp k-lining",
304 get_client_name(client_p, HIDE_IP));
306 exit_client(client_p, client_p, &me, aconf->passwd);
307 continue;
311 if(!IsRegistered(client_p))
312 ping = ConfigFileEntry.connect_timeout;
313 else
314 ping = get_client_ping(client_p);
316 if(ping < (CurrentTime - client_p->localClient->lasttime))
319 * If the client/server hasnt talked to us in 2*ping seconds
320 * and it has a ping time, then close its connection.
322 if(((CurrentTime - client_p->localClient->lasttime) >= (2 * ping)
323 && (client_p->flags & FLAGS_PINGSENT)))
325 if(IsAnyServer(client_p))
327 sendto_realops_flags(UMODE_ALL, L_ALL,
328 "No response from %s, closing link",
329 client_p->name);
330 ilog(L_SERVER,
331 "No response from %s, closing link",
332 log_client_name(client_p, HIDE_IP));
334 (void) ircsnprintf(scratch, sizeof(scratch),
335 "Ping timeout: %d seconds",
336 (int) (CurrentTime - client_p->localClient->lasttime));
338 exit_client(client_p, client_p, &me, scratch);
339 continue;
341 else if((client_p->flags & FLAGS_PINGSENT) == 0)
344 * if we havent PINGed the connection and we havent
345 * heard from it in a while, PING it to make sure
346 * it is still alive.
348 client_p->flags |= FLAGS_PINGSENT;
349 /* not nice but does the job */
350 client_p->localClient->lasttime = CurrentTime - ping;
351 sendto_one(client_p, "PING :%s", me.name);
354 /* ping_timeout: */
360 * check_unknowns_list
362 * inputs - pointer to list of unknown clients
363 * output - NONE
364 * side effects - unknown clients get marked for termination after n seconds
366 static void
367 check_unknowns_list(dlink_list * list)
369 dlink_node *ptr, *next_ptr;
370 struct Client *client_p;
372 DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
374 client_p = ptr->data;
376 if(IsDead(client_p) || IsClosing(client_p))
377 continue;
380 * Check UNKNOWN connections - if they have been in this state
381 * for > 30s, close them.
384 if((CurrentTime - client_p->localClient->firsttime) > 30)
385 exit_client(client_p, client_p, &me, "Connection timed out");
389 static void
390 notify_banned_client(struct Client *client_p, struct ConfItem *aconf, int ban)
392 static const char conn_closed[] = "Connection closed";
393 static const char d_lined[] = "D-lined";
394 static const char k_lined[] = "K-lined";
395 static const char g_lined[] = "G-lined";
396 const char *reason = NULL;
397 const char *exit_reason = conn_closed;
399 if(ConfigFileEntry.kline_with_reason && !EmptyString(aconf->passwd))
401 reason = aconf->passwd;
402 exit_reason = aconf->passwd;
404 else
406 switch (aconf->status)
408 case D_LINED:
409 reason = d_lined;
410 break;
411 case G_LINED:
412 reason = g_lined;
413 break;
414 default:
415 reason = k_lined;
416 break;
420 if(ban == D_LINED && !IsPerson(client_p))
421 sendto_one(client_p, "NOTICE DLINE :*** You have been D-lined");
422 else
423 sendto_one(client_p, form_str(ERR_YOUREBANNEDCREEP),
424 me.name, client_p->name, reason);
426 exit_client(client_p, client_p, &me,
427 EmptyString(ConfigFileEntry.kline_reason) ? exit_reason :
428 ConfigFileEntry.kline_reason);
432 * check_banned_lines
433 * inputs - NONE
434 * output - NONE
435 * side effects - Check all connections for a pending k/d/gline against the
436 * client, exit the client if found.
438 void
439 check_banned_lines(void)
441 struct Client *client_p; /* current local client_p being examined */
442 struct ConfItem *aconf = NULL;
443 dlink_node *ptr, *next_ptr;
445 DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
447 client_p = ptr->data;
449 if(IsMe(client_p))
450 continue;
452 /* if there is a returned struct ConfItem then kill it */
453 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip, client_p->localClient->ip.ss_family)))
455 if(aconf->status & CONF_EXEMPTDLINE)
456 continue;
458 sendto_realops_flags(UMODE_ALL, L_ALL,
459 "DLINE active for %s",
460 get_client_name(client_p, HIDE_IP));
462 notify_banned_client(client_p, aconf, D_LINED);
463 continue; /* and go examine next fd/client_p */
466 if(!IsPerson(client_p))
467 continue;
469 if((aconf = find_kline(client_p)) != NULL)
471 if(IsExemptKline(client_p))
473 sendto_realops_flags(UMODE_ALL, L_ALL,
474 "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
475 get_client_name(client_p, HIDE_IP),
476 aconf->user, aconf->host);
477 continue;
480 sendto_realops_flags(UMODE_ALL, L_ALL,
481 "KLINE active for %s",
482 get_client_name(client_p, HIDE_IP));
483 notify_banned_client(client_p, aconf, K_LINED);
484 continue;
486 else if((aconf = find_gline(client_p)) != NULL)
488 if(IsExemptKline(client_p))
490 sendto_realops_flags(UMODE_ALL, L_ALL,
491 "GLINE over-ruled for %s, client is kline_exempt [%s@%s]",
492 get_client_name(client_p, HIDE_IP),
493 aconf->user, aconf->host);
494 continue;
497 if(IsExemptGline(client_p))
499 sendto_realops_flags(UMODE_ALL, L_ALL,
500 "GLINE over-ruled for %s, client is gline_exempt [%s@%s]",
501 get_client_name(client_p, HIDE_IP),
502 aconf->user, aconf->host);
503 continue;
506 sendto_realops_flags(UMODE_ALL, L_ALL,
507 "GLINE active for %s",
508 get_client_name(client_p, HIDE_IP));
510 notify_banned_client(client_p, aconf, G_LINED);
511 continue;
513 else if((aconf = find_xline(client_p->info, 1)) != NULL)
515 if(IsExemptKline(client_p))
517 sendto_realops_flags(UMODE_ALL, L_ALL,
518 "XLINE over-ruled for %s, client is kline_exempt [%s]",
519 get_client_name(client_p, HIDE_IP),
520 aconf->name);
521 continue;
524 sendto_realops_flags(UMODE_ALL, L_ALL, "XLINE active for %s",
525 get_client_name(client_p, HIDE_IP));
527 (void) exit_client(client_p, client_p, &me, "Bad user info");
528 continue;
532 /* also check the unknowns list for new dlines */
533 DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
535 client_p = ptr->data;
537 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)))
539 if(aconf->status & CONF_EXEMPTDLINE)
540 continue;
542 notify_banned_client(client_p, aconf, D_LINED);
548 /* check_klines_event()
550 * inputs -
551 * outputs -
552 * side effects - check_klines() is called, kline_queued unset
554 void
555 check_klines_event(void *unused)
557 kline_queued = 0;
558 check_klines();
561 /* check_klines
563 * inputs -
564 * outputs -
565 * side effects - all clients will be checked for klines
567 void
568 check_klines(void)
570 struct Client *client_p;
571 struct ConfItem *aconf;
572 dlink_node *ptr;
573 dlink_node *next_ptr;
575 DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
577 client_p = ptr->data;
579 if(IsMe(client_p) || !IsPerson(client_p))
580 continue;
582 if((aconf = find_kline(client_p)) != NULL)
584 if(IsExemptKline(client_p))
586 sendto_realops_flags(UMODE_ALL, L_ALL,
587 "KLINE over-ruled for %s, client is kline_exempt",
588 get_client_name(client_p, HIDE_IP));
589 continue;
592 sendto_realops_flags(UMODE_ALL, L_ALL,
593 "KLINE active for %s",
594 get_client_name(client_p, HIDE_IP));
596 notify_banned_client(client_p, aconf, K_LINED);
597 continue;
602 /* check_glines()
604 * inputs -
605 * outputs -
606 * side effects - all clients will be checked for glines
608 void
609 check_glines(void)
611 struct Client *client_p;
612 struct ConfItem *aconf;
613 dlink_node *ptr;
614 dlink_node *next_ptr;
616 DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
618 client_p = ptr->data;
620 if(IsMe(client_p) || !IsPerson(client_p))
621 continue;
623 if((aconf = find_gline(client_p)) != NULL)
625 if(IsExemptKline(client_p))
627 sendto_realops_flags(UMODE_ALL, L_ALL,
628 "GLINE over-ruled for %s, client is kline_exempt",
629 get_client_name(client_p, HIDE_IP));
630 continue;
633 if(IsExemptGline(client_p))
635 sendto_realops_flags(UMODE_ALL, L_ALL,
636 "GLINE over-ruled for %s, client is gline_exempt",
637 get_client_name(client_p, HIDE_IP));
638 continue;
641 sendto_realops_flags(UMODE_ALL, L_ALL,
642 "GLINE active for %s",
643 get_client_name(client_p, HIDE_IP));
645 notify_banned_client(client_p, aconf, K_LINED);
646 continue;
651 /* check_dlines()
653 * inputs -
654 * outputs -
655 * side effects - all clients will be checked for dlines
657 void
658 check_dlines(void)
660 struct Client *client_p;
661 struct ConfItem *aconf;
662 dlink_node *ptr;
663 dlink_node *next_ptr;
665 DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
667 client_p = ptr->data;
669 if(IsMe(client_p))
670 continue;
672 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)) != NULL)
674 if(aconf->status & CONF_EXEMPTDLINE)
675 continue;
677 sendto_realops_flags(UMODE_ALL, L_ALL,
678 "DLINE active for %s",
679 get_client_name(client_p, HIDE_IP));
681 notify_banned_client(client_p, aconf, D_LINED);
682 continue;
686 /* dlines need to be checked against unknowns too */
687 DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
689 client_p = ptr->data;
691 if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)) != NULL)
693 if(aconf->status & CONF_EXEMPTDLINE)
694 continue;
696 notify_banned_client(client_p, aconf, D_LINED);
701 /* check_xlines
703 * inputs -
704 * outputs -
705 * side effects - all clients will be checked for xlines
707 void
708 check_xlines(void)
710 struct Client *client_p;
711 struct ConfItem *aconf;
712 dlink_node *ptr;
713 dlink_node *next_ptr;
715 DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
717 client_p = ptr->data;
719 if(IsMe(client_p) || !IsPerson(client_p))
720 continue;
722 if((aconf = find_xline(client_p->info, 1)) != NULL)
724 if(IsExemptKline(client_p))
726 sendto_realops_flags(UMODE_ALL, L_ALL,
727 "XLINE over-ruled for %s, client is kline_exempt",
728 get_client_name(client_p, HIDE_IP));
729 continue;
732 sendto_realops_flags(UMODE_ALL, L_ALL, "XLINE active for %s",
733 get_client_name(client_p, HIDE_IP));
735 (void) exit_client(client_p, client_p, &me, "Bad user info");
736 continue;
742 * update_client_exit_stats
744 * input - pointer to client
745 * output - NONE
746 * side effects -
748 static void
749 update_client_exit_stats(struct Client *client_p)
751 if(IsServer(client_p))
753 sendto_realops_flags(UMODE_EXTERNAL, L_ALL,
754 "Server %s split from %s",
755 client_p->name, client_p->servptr->name);
756 if(HasSentEob(client_p))
757 eob_count--;
759 else if(IsClient(client_p))
761 --Count.total;
762 if(IsOper(client_p))
763 --Count.oper;
764 if(IsInvisible(client_p))
765 --Count.invisi;
768 if(splitchecking && !splitmode)
769 check_splitmode(NULL);
773 * release_client_state
775 * input - pointer to client to release
776 * output - NONE
777 * side effects -
779 static void
780 release_client_state(struct Client *client_p)
782 if(client_p->user != NULL)
784 free_user(client_p->user, client_p); /* try this here */
786 if(client_p->serv)
788 if(client_p->serv->fullcaps)
789 MyFree(client_p->serv->fullcaps);
790 MyFree(client_p->serv);
795 * remove_client_from_list
796 * inputs - point to client to remove
797 * output - NONE
798 * side effects - taken the code from ExitOneClient() for this
799 * and placed it here. - avalon
801 static void
802 remove_client_from_list(struct Client *client_p)
804 s_assert(NULL != client_p);
806 if(client_p == NULL)
807 return;
809 /* A client made with make_client()
810 * is on the unknown_list until removed.
811 * If it =does= happen to exit before its removed from that list
812 * and its =not= on the global_client_list, it will core here.
813 * short circuit that case now -db
815 if(client_p->node.prev == NULL && client_p->node.next == NULL)
816 return;
818 dlinkDelete(&client_p->node, &global_client_list);
820 update_client_exit_stats(client_p);
825 * find_person - find person by (nick)name.
826 * inputs - pointer to name
827 * output - return client pointer
828 * side effects -
830 struct Client *
831 find_person(const char *name)
833 struct Client *c2ptr;
835 c2ptr = find_client(name);
837 if(c2ptr && IsPerson(c2ptr))
838 return (c2ptr);
839 return (NULL);
842 struct Client *
843 find_named_person(const char *name)
845 struct Client *c2ptr;
847 c2ptr = find_named_client(name);
849 if(c2ptr && IsPerson(c2ptr))
850 return (c2ptr);
851 return (NULL);
856 * find_chasing - find the client structure for a nick name (user)
857 * using history mechanism if necessary. If the client is not found,
858 * an error message (NO SUCH NICK) is generated. If the client was found
859 * through the history, chasing will be 1 and otherwise 0.
861 struct Client *
862 find_chasing(struct Client *source_p, const char *user, int *chasing)
864 struct Client *who;
866 if(MyClient(source_p))
867 who = find_named_person(user);
868 else
869 who = find_person(user);
871 if(chasing)
872 *chasing = 0;
874 if(who || IsDigit(*user))
875 return who;
877 if(!(who = get_history(user, (long) KILLCHASETIMELIMIT)))
879 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
880 form_str(ERR_NOSUCHNICK), user);
881 return (NULL);
883 if(chasing)
884 *chasing = 1;
885 return who;
889 * get_client_name - Return the name of the client
890 * for various tracking and
891 * admin purposes. The main purpose of this function is to
892 * return the "socket host" name of the client, if that
893 * differs from the advertised name (other than case).
894 * But, this can be used to any client structure.
896 * NOTE 1:
897 * Watch out the allocation of "nbuf", if either source_p->name
898 * or source_p->sockhost gets changed into pointers instead of
899 * directly allocated within the structure...
901 * NOTE 2:
902 * Function return either a pointer to the structure (source_p) or
903 * to internal buffer (nbuf). *NEVER* use the returned pointer
904 * to modify what it points!!!
907 const char *
908 get_client_name(struct Client *client, int showip)
910 static char empty_name[] = "";
911 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
912 const char *name;
914 s_assert(NULL != client);
915 if(client == NULL)
916 return NULL;
918 if(MyConnect(client))
920 if(EmptyString(client->name))
921 name = empty_name;
922 else
923 name = client->name;
925 if(!irccmp(name, client->host))
926 return name;
928 if(ConfigFileEntry.hide_spoof_ips &&
929 showip == SHOW_IP && IsIPSpoof(client))
930 showip = MASK_IP;
931 if(IsAnyServer(client))
932 showip = MASK_IP;
934 /* And finally, let's get the host information, ip or name */
935 switch (showip)
937 case SHOW_IP:
938 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
939 name, client->username,
940 client->sockhost);
941 break;
942 case MASK_IP:
943 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
944 name, client->username);
945 break;
946 default:
947 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
948 name, client->username, client->host);
950 return nbuf;
953 /* As pointed out by Adel Mezibra
954 * Neph|l|m@EFnet. Was missing a return here.
956 return client->name;
959 /* log_client_name()
961 * This version is the same as get_client_name, but doesnt contain the
962 * code that will hide IPs always. This should be used for logfiles.
964 const char *
965 log_client_name(struct Client *target_p, int showip)
967 static const char empty_name[] = "";
968 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
969 const char *name;
971 if(target_p == NULL)
972 return NULL;
974 if(EmptyString(target_p->name))
975 name = empty_name;
976 else
977 name = target_p->name;
979 if(MyConnect(target_p))
981 if(irccmp(name, target_p->host) == 0)
982 return name;
984 switch (showip)
986 case SHOW_IP:
987 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
988 name, target_p->username, target_p->sockhost);
989 break;
991 case MASK_IP:
992 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
993 name, target_p->username);
995 default:
996 ircsnprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
997 name, target_p->username, target_p->host);
1000 return nbuf;
1003 return name;
1006 static void
1007 free_exited_clients(void *unused)
1009 dlink_node *ptr, *next;
1010 struct Client *target_p;
1012 DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
1014 target_p = ptr->data;
1016 #ifdef DEBUG_EXITED_CLIENTS
1018 struct abort_client *abt;
1019 dlink_node *aptr;
1020 int found = 0;
1022 DLINK_FOREACH(aptr, abort_list.head)
1024 abt = aptr->data;
1025 if(abt->client == target_p)
1027 s_assert(0);
1028 sendto_realops_flags(UMODE_ALL, L_ALL,
1029 "On abort_list: %s stat: %u flags: %u/%u handler: %c",
1030 target_p->name, (unsigned int) target_p->status,
1031 target_p->flags, target_p->flags2, target_p->handler);
1032 sendto_realops_flags(UMODE_ALL, L_ALL,
1033 "Please report this to the ratbox developers!");
1034 found++;
1038 if(found)
1040 dlinkDestroy(ptr, &dead_list);
1041 continue;
1044 #endif
1046 if(ptr->data == NULL)
1048 sendto_realops_flags(UMODE_ALL, L_ALL,
1049 "Warning: null client on dead_list!");
1050 dlinkDestroy(ptr, &dead_list);
1051 continue;
1053 release_client_state(target_p);
1054 free_client(target_p);
1055 dlinkDestroy(ptr, &dead_list);
1058 #ifdef DEBUG_EXITED_CLIENTS
1059 DLINK_FOREACH_SAFE(ptr, next, dead_remote_list.head)
1061 target_p = ptr->data;
1063 if(ptr->data == NULL)
1065 sendto_realops_flags(UMODE_ALL, L_ALL,
1066 "Warning: null client on dead_list!");
1067 dlinkDestroy(ptr, &dead_list);
1068 continue;
1070 release_client_state(target_p);
1071 free_client(target_p);
1072 dlinkDestroy(ptr, &dead_remote_list);
1074 #endif
1079 ** Recursively send QUITs and SQUITs for source_p and all its dependent clients
1080 ** and servers to those servers that need them. A server needs the client
1081 ** QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it
1082 ** isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once
1083 ** a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending
1084 ** on that one -orabidoo
1086 static void
1087 recurse_send_quits(struct Client *client_p, struct Client *source_p,
1088 struct Client *to, const char *comment,
1089 const char *myname)
1091 struct Client *target_p;
1092 dlink_node *ptr, *ptr_next;
1093 /* If this server can handle quit storm (QS) removal
1094 * of dependents, just send the SQUIT
1097 if(IsCapable(to, CAP_QS))
1099 sendto_one(to, "SQUIT %s :%s",
1100 get_id(source_p, to), me.name);
1102 else
1104 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
1106 target_p = ptr->data;
1107 sendto_one(to, ":%s QUIT :%s", target_p->name, comment);
1109 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head)
1111 target_p = ptr->data;
1112 recurse_send_quits(client_p, target_p, to, comment, myname);
1114 if(!match(myname, source_p->name))
1115 sendto_one(to, "SQUIT %s :%s", source_p->name, me.name);
1120 ** Remove all clients that depend on source_p; assumes all (S)QUITs have
1121 ** already been sent. we make sure to exit a server's dependent clients
1122 ** and servers before the server itself; exit_one_client takes care of
1123 ** actually removing things off llists. tweaked from +CSr31 -orabidoo
1126 * added sanity test code.... source_p->serv might be NULL...
1128 static void
1129 recurse_remove_clients(struct Client *source_p, const char *comment)
1131 struct Client *target_p;
1132 dlink_node *ptr, *ptr_next;
1134 if(IsMe(source_p))
1135 return;
1137 if(source_p->serv == NULL) /* oooops. uh this is actually a major bug */
1138 return;
1140 /* this is very ugly, but it saves cpu :P */
1141 if(ConfigFileEntry.nick_delay > 0)
1143 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
1145 target_p = ptr->data;
1146 target_p->flags |= FLAGS_KILLED;
1147 add_nd_entry(target_p->name);
1149 if(!IsDead(target_p) && !IsClosing(target_p))
1150 exit_remote_client(NULL, target_p, &me, comment);
1153 else
1155 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
1157 target_p = ptr->data;
1158 target_p->flags |= FLAGS_KILLED;
1160 if(!IsDead(target_p) && !IsClosing(target_p))
1161 exit_remote_client(NULL, target_p, &me, comment);
1165 DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head)
1167 target_p = ptr->data;
1168 recurse_remove_clients(target_p, comment);
1169 qs_server(NULL, target_p, &me, comment);
1174 ** Remove *everything* that depends on source_p, from all lists, and sending
1175 ** all necessary QUITs and SQUITs. source_p itself is still on the lists,
1176 ** and its SQUITs have been sent except for the upstream one -orabidoo
1178 static void
1179 remove_dependents(struct Client *client_p,
1180 struct Client *source_p,
1181 struct Client *from, const char *comment, const char *comment1)
1183 struct Client *to;
1184 static char myname[HOSTLEN + 1];
1185 dlink_node *ptr, *next;
1187 DLINK_FOREACH_SAFE(ptr, next, serv_list.head)
1189 to = ptr->data;
1191 if(IsMe(to) || to == source_p->from ||
1192 (to == client_p && IsCapable(to, CAP_QS)))
1193 continue;
1195 /* MyConnect(source_p) is rotten at this point: if source_p
1196 * was mine, ->from is NULL.
1198 /* The WALLOPS isn't needed here as pointed out by
1199 * comstud, since m_squit already does the notification.
1202 strlcpy(myname, me.name, sizeof(myname));
1203 recurse_send_quits(client_p, source_p, to, comment1, myname);
1206 recurse_remove_clients(source_p, comment1);
1209 void
1210 exit_aborted_clients(void *unused)
1212 struct abort_client *abt;
1213 dlink_node *ptr, *next;
1214 DLINK_FOREACH_SAFE(ptr, next, abort_list.head)
1216 abt = ptr->data;
1218 #ifdef DEBUG_EXITED_CLIENTS
1220 if(dlinkFind(abt->client, &dead_list))
1222 s_assert(0);
1223 sendto_realops_flags(UMODE_ALL, L_ALL,
1224 "On dead_list: %s stat: %u flags: %u/%u handler: %c",
1225 abt->client->name, (unsigned int) abt->client->status,
1226 abt->client->flags, abt->client->flags2, abt->client->handler);
1227 sendto_realops_flags(UMODE_ALL, L_ALL,
1228 "Please report this to the ratbox developers!");
1229 continue;
1232 #endif
1234 s_assert(*((unsigned long*)abt->client) != 0xdeadbeef); /* This is lame but its a debug thing */
1235 dlinkDelete(ptr, &abort_list);
1237 if(IsAnyServer(abt->client))
1238 sendto_realops_flags(UMODE_ALL, L_ALL,
1239 "Closing link to %s: %s",
1240 abt->client->name, abt->notice);
1242 /* its no longer on abort list - we *must* remove
1243 * FLAGS_CLOSING otherwise exit_client() will not run --fl
1245 abt->client->flags &= ~FLAGS_CLOSING;
1246 exit_client(abt->client, abt->client, &me, abt->notice);
1247 MyFree(abt);
1253 * dead_link - Adds client to a list of clients that need an exit_client()
1256 void
1257 dead_link(struct Client *client_p, int sendqex)
1259 struct abort_client *abt;
1261 s_assert(!IsMe(client_p));
1262 if(IsDead(client_p) || IsClosing(client_p) || IsMe(client_p))
1263 return;
1265 abt = (struct abort_client *) MyMalloc(sizeof(struct abort_client));
1267 if(sendqex)
1268 strlcpy(abt->notice, "Max SendQ exceeded", sizeof(abt->notice));
1269 else
1270 ircsnprintf(abt->notice, sizeof(abt->notice), "Write error: %s", strerror(errno));
1272 abt->client = client_p;
1273 SetIOError(client_p);
1274 SetDead(client_p);
1275 SetClosing(client_p);
1276 dlinkAdd(abt, &abt->node, &abort_list);
1280 /* This does the remove of the user from channels..local or remote */
1281 static inline void
1282 exit_generic_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1283 const char *comment)
1285 sendto_common_channels_local(source_p, ":%s!%s@%s QUIT :%s",
1286 source_p->name,
1287 source_p->username, source_p->host, comment);
1289 remove_user_from_channels(source_p);
1291 /* Should not be in any channels now */
1292 s_assert(source_p->user->channel.head == NULL);
1294 /* Clean up allow lists */
1295 del_all_accepts(source_p);
1297 add_history(source_p, 0);
1298 off_history(source_p);
1300 monitor_signoff(source_p);
1302 if(has_id(source_p))
1303 del_from_id_hash(source_p->id, source_p);
1305 del_from_hostname_hash(source_p->host, source_p);
1306 del_from_client_hash(source_p->name, source_p);
1307 remove_client_from_list(source_p);
1311 * Assumes IsPerson(source_p) && !MyConnect(source_p)
1314 static int
1315 exit_remote_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1316 const char *comment)
1318 exit_generic_client(client_p, source_p, from, comment);
1320 if(source_p->servptr && source_p->servptr->serv)
1322 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->users);
1325 if((source_p->flags & FLAGS_KILLED) == 0)
1327 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1328 ":%s QUIT :%s", use_id(source_p), comment);
1329 sendto_server(client_p, NULL, NOCAPS, CAP_TS6,
1330 ":%s QUIT :%s", source_p->name, comment);
1333 SetDead(source_p);
1334 #ifdef DEBUG_EXITED_CLIENTS
1335 dlinkAddAlloc(source_p, &dead_remote_list);
1336 #else
1337 dlinkAddAlloc(source_p, &dead_list);
1338 #endif
1339 return(CLIENT_EXITED);
1343 * This assumes IsUnknown(source_p) == TRUE and MyConnect(source_p) == TRUE
1346 static int
1347 exit_unknown_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1348 const char *comment)
1350 delete_auth_queries(source_p);
1351 client_flush_input(source_p);
1352 del_unknown_ip(source_p);
1353 dlinkDelete(&source_p->localClient->tnode, &unknown_list);
1355 if(!IsIOError(source_p))
1356 sendto_one(source_p, "ERROR :Closing Link: 127.0.0.1 (%s)", comment);
1358 close_connection(source_p);
1360 del_from_hostname_hash(source_p->host, source_p);
1361 del_from_client_hash(source_p->name, source_p);
1362 remove_client_from_list(source_p);
1363 SetDead(source_p);
1364 dlinkAddAlloc(source_p, &dead_list);
1366 /* Note that we don't need to add unknowns to the dead_list */
1367 return(CLIENT_EXITED);
1370 static int
1371 exit_remote_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1372 const char *comment)
1374 static char comment1[(HOSTLEN*2)+2];
1375 struct Client *target_p;
1377 if(source_p->servptr)
1378 strcpy(comment1, source_p->servptr->name);
1379 else
1380 strcpy(comment1, "<Unknown>");
1382 strcat(comment1, " ");
1383 strcat(comment1, source_p->name);
1384 if(source_p->serv != NULL)
1385 remove_dependents(client_p, source_p, from, comment, comment1);
1387 if(source_p->servptr && source_p->servptr->serv)
1388 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1389 else
1390 s_assert(0);
1392 dlinkFindDestroy(source_p, &global_serv_list);
1393 target_p = source_p->from;
1395 if(target_p != NULL && IsServer(target_p) && target_p != client_p &&
1396 !IsMe(target_p) && (source_p->flags & FLAGS_KILLED) == 0)
1398 sendto_one(target_p, ":%s SQUIT %s :%s",
1399 get_id(from, target_p), get_id(source_p, target_p),
1400 comment);
1403 if(has_id(source_p))
1404 del_from_id_hash(source_p->id, source_p);
1406 del_from_client_hash(source_p->name, source_p);
1407 remove_client_from_list(source_p);
1409 SetDead(source_p);
1410 #ifdef DEBUG_EXITED_CLIENTS
1411 dlinkAddAlloc(source_p, &dead_remote_list);
1412 #else
1413 dlinkAddAlloc(source_p, &dead_list);
1414 #endif
1415 return 0;
1418 static int
1419 qs_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1420 const char *comment)
1422 struct Client *target_p;
1424 if(source_p->servptr && source_p->servptr->serv)
1425 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1426 else
1427 s_assert(0);
1429 dlinkFindDestroy(source_p, &global_serv_list);
1430 target_p = source_p->from;
1432 if(has_id(source_p))
1433 del_from_id_hash(source_p->id, source_p);
1435 del_from_client_hash(source_p->name, source_p);
1436 remove_client_from_list(source_p);
1438 SetDead(source_p);
1439 dlinkAddAlloc(source_p, &dead_list);
1440 return 0;
1443 static int
1444 exit_local_server(struct Client *client_p, struct Client *source_p, struct Client *from,
1445 const char *comment)
1447 static char comment1[(HOSTLEN*2)+2];
1448 unsigned int sendk, recvk;
1450 dlinkDelete(&source_p->localClient->tnode, &serv_list);
1451 dlinkFindDestroy(source_p, &global_serv_list);
1453 unset_chcap_usage_counts(source_p);
1454 sendk = source_p->localClient->sendK;
1455 recvk = source_p->localClient->receiveK;
1457 if(client_p != NULL && source_p != client_p && !IsIOError(source_p))
1459 sendto_one(source_p, "ERROR :Closing Link: 127.0.0.1 %s (%s)",
1460 source_p->name, comment);
1463 if(source_p->localClient->slink && source_p->localClient->slink->ctrlfd >= 0)
1465 comm_close(source_p->localClient->slink->ctrlfd);
1466 source_p->localClient->slink->ctrlfd = -1;
1469 if(source_p->servptr && source_p->servptr->serv)
1470 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
1471 else
1472 s_assert(0);
1475 close_connection(source_p);
1477 if(source_p->servptr)
1478 strcpy(comment1, source_p->servptr->name);
1479 else
1480 strcpy(comment1, "<Unknown>");
1482 strcat(comment1, " ");
1483 strcat(comment1, source_p->name);
1485 if(source_p->serv != NULL)
1486 remove_dependents(client_p, source_p, from, comment, comment1);
1488 sendto_realops_flags(UMODE_ALL, L_ALL, "%s was connected"
1489 " for %ld seconds. %d/%d sendK/recvK.",
1490 source_p->name, CurrentTime - source_p->localClient->firsttime, sendk, recvk);
1492 ilog(L_SERVER, "%s was connected for %ld seconds. %d/%d sendK/recvK.",
1493 source_p->name, CurrentTime - source_p->localClient->firsttime, sendk, recvk);
1495 if(has_id(source_p))
1496 del_from_id_hash(source_p->id, source_p);
1498 del_from_client_hash(source_p->name, source_p);
1499 remove_client_from_list(source_p);
1501 SetDead(source_p);
1502 dlinkAddAlloc(source_p, &dead_list);
1503 return 0;
1508 * This assumes IsPerson(source_p) == TRUE && MyConnect(source_p) == TRUE
1511 static int
1512 exit_local_client(struct Client *client_p, struct Client *source_p, struct Client *from,
1513 const char *comment)
1515 dlink_node *ptr, *next_ptr;
1516 unsigned long on_for;
1518 exit_generic_client(client_p, source_p, from, comment);
1519 clear_monitor(source_p);
1521 s_assert(IsPerson(source_p));
1522 client_flush_input(source_p);
1523 dlinkDelete(&source_p->localClient->tnode, &lclient_list);
1524 dlinkDelete(&source_p->lnode, &me.serv->users);
1526 if(IsOper(source_p))
1527 dlinkFindDestroy(source_p, &oper_list);
1529 /* Clean up invitefield */
1530 DLINK_FOREACH_SAFE(ptr, next_ptr, source_p->localClient->invited.head)
1532 del_invite(ptr->data, source_p);
1535 sendto_realops_flags(UMODE_CCONN, L_ALL,
1536 "Client exiting: %s (%s@%s) [%s] [%s]",
1537 source_p->name,
1538 source_p->username, source_p->host, comment,
1539 show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255");
1541 sendto_realops_flags(UMODE_CCONNEXT, L_ALL,
1542 "CLIEXIT %s %s %s %s 0 %s",
1543 source_p->name, source_p->username, source_p->host,
1544 show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255",
1545 comment);
1547 on_for = CurrentTime - source_p->localClient->firsttime;
1549 ilog(L_USER, "%s (%3lu:%02lu:%02lu): %s!%s@%s %d/%d",
1550 myctime(CurrentTime), on_for / 3600,
1551 (on_for % 3600) / 60, on_for % 60,
1552 source_p->name, source_p->username, source_p->host,
1553 source_p->localClient->sendK, source_p->localClient->receiveK);
1555 sendto_one(source_p, "ERROR :Closing Link: %s (%s)", source_p->host, comment);
1556 close_connection(source_p);
1558 if((source_p->flags & FLAGS_KILLED) == 0)
1560 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1561 ":%s QUIT :%s", use_id(source_p), comment);
1562 sendto_server(client_p, NULL, NOCAPS, CAP_TS6,
1563 ":%s QUIT :%s", source_p->name, comment);
1566 SetDead(source_p);
1567 dlinkAddAlloc(source_p, &dead_list);
1568 return(CLIENT_EXITED);
1573 ** exit_client - This is old "m_bye". Name changed, because this is not a
1574 ** protocol function, but a general server utility function.
1576 ** This function exits a client of *any* type (user, server, etc)
1577 ** from this server. Also, this generates all necessary prototol
1578 ** messages that this exit may cause.
1580 ** 1) If the client is a local client, then this implicitly
1581 ** exits all other clients depending on this connection (e.g.
1582 ** remote clients having 'from'-field that points to this.
1584 ** 2) If the client is a remote client, then only this is exited.
1586 ** For convenience, this function returns a suitable value for
1587 ** m_function return value:
1589 ** CLIENT_EXITED if (client_p == source_p)
1590 ** 0 if (client_p != source_p)
1593 exit_client(struct Client *client_p, /* The local client originating the
1594 * exit or NULL, if this exit is
1595 * generated by this server for
1596 * internal reasons.
1597 * This will not get any of the
1598 * generated messages. */
1599 struct Client *source_p, /* Client exiting */
1600 struct Client *from, /* Client firing off this Exit,
1601 * never NULL! */
1602 const char *comment /* Reason for the exit */
1605 if(IsClosing(source_p))
1606 return -1;
1608 /* note, this HAS to be here, when we exit a client we attempt to
1609 * send them data, if this generates a write error we must *not* add
1610 * them to the abort list --fl
1612 SetClosing(source_p);
1614 if(MyConnect(source_p))
1616 /* Local clients of various types */
1617 if(IsPerson(source_p))
1618 return exit_local_client(client_p, source_p, from, comment);
1619 else if(IsServer(source_p))
1620 return exit_local_server(client_p, source_p, from, comment);
1621 /* IsUnknown || IsConnecting || IsHandShake */
1622 else if(!IsReject(source_p))
1623 return exit_unknown_client(client_p, source_p, from, comment);
1625 else
1627 /* Remotes */
1628 if(IsPerson(source_p))
1629 return exit_remote_client(client_p, source_p, from, comment);
1630 else if(IsServer(source_p))
1631 return exit_remote_server(client_p, source_p, from, comment);
1634 return -1;
1638 * Count up local client memory
1641 /* XXX one common Client list now */
1642 void
1643 count_local_client_memory(size_t * count, size_t * local_client_memory_used)
1645 size_t lusage;
1646 BlockHeapUsage(lclient_heap, count, NULL, &lusage);
1647 *local_client_memory_used = lusage + (*count * (sizeof(MemBlock) + sizeof(struct Client)));
1651 * Count up remote client memory
1653 void
1654 count_remote_client_memory(size_t * count, size_t * remote_client_memory_used)
1656 size_t lcount, rcount;
1657 BlockHeapUsage(lclient_heap, &lcount, NULL, NULL);
1658 BlockHeapUsage(client_heap, &rcount, NULL, NULL);
1659 *count = rcount - lcount;
1660 *remote_client_memory_used = *count * (sizeof(MemBlock) + sizeof(struct Client));
1665 * accept processing, this adds a form of "caller ID" to ircd
1667 * If a client puts themselves into "caller ID only" mode,
1668 * only clients that match a client pointer they have put on
1669 * the accept list will be allowed to message them.
1671 * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ]
1673 * [target.allow_list] -> [ source1 ] -> [source2 ]
1675 * i.e. a target will have a link list of source pointers it will allow
1676 * each source client then has a back pointer pointing back
1677 * to the client that has it on its accept list.
1678 * This allows for exit_one_client to remove these now bogus entries
1679 * from any client having an accept on them.
1682 * del_all_accepts
1684 * inputs - pointer to exiting client
1685 * output - NONE
1686 * side effects - Walk through given clients allow_list and on_allow_list
1687 * remove all references to this client
1689 void
1690 del_all_accepts(struct Client *client_p)
1692 dlink_node *ptr;
1693 dlink_node *next_ptr;
1694 struct Client *target_p;
1696 if(MyClient(client_p) && client_p->localClient->allow_list.head)
1698 /* clear this clients accept list, and remove them from
1699 * everyones on_accept_list
1701 DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->allow_list.head)
1703 target_p = ptr->data;
1704 dlinkFindDestroy(client_p, &target_p->on_allow_list);
1705 dlinkDestroy(ptr, &client_p->localClient->allow_list);
1709 /* remove this client from everyones accept list */
1710 DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head)
1712 target_p = ptr->data;
1713 dlinkFindDestroy(client_p, &target_p->localClient->allow_list);
1714 dlinkDestroy(ptr, &client_p->on_allow_list);
1719 show_ip(struct Client *source_p, struct Client *target_p)
1721 if(IsAnyServer(target_p))
1723 return 0;
1725 else if(IsIPSpoof(target_p))
1727 /* source == NULL indicates message is being sent
1728 * to local opers.
1730 if(!ConfigFileEntry.hide_spoof_ips &&
1731 (source_p == NULL || MyOper(source_p)))
1732 return 1;
1734 return 0;
1736 else
1737 return 1;
1741 show_ip_conf(struct ConfItem *aconf, struct Client *source_p)
1743 if(IsConfDoSpoofIp(aconf))
1745 if(!ConfigFileEntry.hide_spoof_ips && MyOper(source_p))
1746 return 1;
1748 return 0;
1750 else
1751 return 1;
1755 * make_user
1757 * inputs - pointer to client struct
1758 * output - pointer to struct User
1759 * side effects - add's an User information block to a client
1760 * if it was not previously allocated.
1762 struct User *
1763 make_user(struct Client *client_p)
1765 struct User *user;
1767 user = client_p->user;
1768 if(!user)
1770 user = (struct User *) BlockHeapAlloc(user_heap);
1771 client_p->user = user;
1773 return user;
1777 * make_server
1779 * inputs - pointer to client struct
1780 * output - pointer to struct Server
1781 * side effects - add's an Server information block to a client
1782 * if it was not previously allocated.
1784 struct Server *
1785 make_server(struct Client *client_p)
1787 struct Server *serv = client_p->serv;
1789 if(!serv)
1791 serv = (struct Server *) MyMalloc(sizeof(struct Server));
1792 client_p->serv = serv;
1794 return client_p->serv;
1798 * free_user
1800 * inputs - pointer to user struct
1801 * - pointer to client struct
1802 * output - none
1803 * side effects - Decrease user reference count by one and release block,
1804 * if count reaches 0
1806 void
1807 free_user(struct User *user, struct Client *client_p)
1809 if(user->away)
1810 MyFree(user->away);
1812 * sanity check
1814 if(user->channel.head)
1816 sendto_realops_flags(UMODE_ALL, L_ALL,
1817 "* %#lx user (%s!%s@%s) %#lx %#lx %lu *",
1818 (unsigned long) client_p,
1819 client_p ? client_p->
1820 name : "<noname>",
1821 client_p->username,
1822 client_p->host,
1823 (unsigned long) user,
1824 (unsigned long) user->channel.head,
1825 dlink_list_length(&user->channel));
1826 s_assert(!user->channel.head);
1829 BlockHeapFree(user_heap, user);
1832 void
1833 init_uid(void)
1835 int i;
1837 for(i = 0; i < 3; i++)
1838 current_uid[i] = me.id[i];
1840 for(i = 3; i < 9; i++)
1841 current_uid[i] = 'A';
1843 current_uid[9] = '\0';
1847 char *
1848 generate_uid(void)
1850 int i;
1852 for(i = 8; i > 3; i--)
1854 if(current_uid[i] == 'Z')
1856 current_uid[i] = '0';
1857 return current_uid;
1859 else if(current_uid[i] != '9')
1861 current_uid[i]++;
1862 return current_uid;
1864 else
1865 current_uid[i] = 'A';
1868 /* if this next if() triggers, we're fucked. */
1869 if(current_uid[3] == 'Z')
1871 current_uid[i] = 'A';
1872 s_assert(0);
1874 else
1875 current_uid[i]++;
1877 return current_uid;
1881 * close_connection
1882 * Close the physical connection. This function must make
1883 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
1885 void
1886 close_connection(struct Client *client_p)
1888 s_assert(client_p != NULL);
1889 if(client_p == NULL)
1890 return;
1892 s_assert(MyConnect(client_p));
1893 if(!MyConnect(client_p))
1894 return;
1896 if(IsServer(client_p))
1898 struct server_conf *server_p;
1900 ServerStats->is_sv++;
1901 ServerStats->is_sbs += client_p->localClient->sendB;
1902 ServerStats->is_sbr += client_p->localClient->receiveB;
1903 ServerStats->is_sks += client_p->localClient->sendK;
1904 ServerStats->is_skr += client_p->localClient->receiveK;
1905 ServerStats->is_sti += CurrentTime - client_p->localClient->firsttime;
1906 if(ServerStats->is_sbs > 2047)
1908 ServerStats->is_sks += (ServerStats->is_sbs >> 10);
1909 ServerStats->is_sbs &= 0x3ff;
1911 if(ServerStats->is_sbr > 2047)
1913 ServerStats->is_skr += (ServerStats->is_sbr >> 10);
1914 ServerStats->is_sbr &= 0x3ff;
1918 * If the connection has been up for a long amount of time, schedule
1919 * a 'quick' reconnect, else reset the next-connect cycle.
1921 if((server_p = find_server_conf(client_p->name)) != NULL)
1924 * Reschedule a faster reconnect, if this was a automatically
1925 * connected configuration entry. (Note that if we have had
1926 * a rehash in between, the status has been changed to
1927 * CONF_ILLEGAL). But only do this if it was a "good" link.
1929 server_p->hold = time(NULL);
1930 server_p->hold +=
1931 (server_p->hold - client_p->localClient->lasttime >
1932 HANGONGOODLINK) ? HANGONRETRYDELAY : ConFreq(server_p->class);
1936 else if(IsClient(client_p))
1938 ServerStats->is_cl++;
1939 ServerStats->is_cbs += client_p->localClient->sendB;
1940 ServerStats->is_cbr += client_p->localClient->receiveB;
1941 ServerStats->is_cks += client_p->localClient->sendK;
1942 ServerStats->is_ckr += client_p->localClient->receiveK;
1943 ServerStats->is_cti += CurrentTime - client_p->localClient->firsttime;
1944 if(ServerStats->is_cbs > 2047)
1946 ServerStats->is_cks += (ServerStats->is_cbs >> 10);
1947 ServerStats->is_cbs &= 0x3ff;
1949 if(ServerStats->is_cbr > 2047)
1951 ServerStats->is_ckr += (ServerStats->is_cbr >> 10);
1952 ServerStats->is_cbr &= 0x3ff;
1955 else
1956 ServerStats->is_ni++;
1958 if(-1 < client_p->localClient->fd)
1960 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
1961 if(!IsIOError(client_p))
1962 send_queued_write(client_p->localClient->fd, client_p);
1964 comm_close(client_p->localClient->fd);
1965 client_p->localClient->fd = -1;
1968 if(client_p->localClient->slink && client_p->localClient->slink->ctrlfd > -1)
1970 comm_close(client_p->localClient->slink->ctrlfd);
1971 client_p->localClient->slink->ctrlfd = -1;
1974 linebuf_donebuf(&client_p->localClient->buf_sendq);
1975 linebuf_donebuf(&client_p->localClient->buf_recvq);
1976 detach_conf(client_p);
1978 /* XXX shouldnt really be done here. */
1979 detach_server_conf(client_p);
1981 client_p->from = NULL; /* ...this should catch them! >:) --msa */
1982 ClearMyConnect(client_p);
1983 SetIOError(client_p);
1986 void
1987 error_exit_client(struct Client *client_p, int error)
1990 * ...hmm, with non-blocking sockets we might get
1991 * here from quite valid reasons, although.. why
1992 * would select report "data available" when there
1993 * wasn't... so, this must be an error anyway... --msa
1994 * actually, EOF occurs when read() returns 0 and
1995 * in due course, select() returns that fd as ready
1996 * for reading even though it ends up being an EOF. -avalon
1998 char errmsg[255];
1999 int current_error = comm_get_sockerr(client_p->localClient->fd);
2001 SetIOError(client_p);
2003 if(IsServer(client_p) || IsHandshake(client_p))
2005 int connected = CurrentTime - client_p->localClient->firsttime;
2007 if(error == 0)
2009 sendto_realops_flags(UMODE_ALL, L_ALL,
2010 "Server %s closed the connection",
2011 client_p->name);
2013 ilog(L_SERVER, "Server %s closed the connection",
2014 log_client_name(client_p, SHOW_IP));
2016 else
2018 sendto_realops_flags(UMODE_ALL, L_ALL,
2019 "Lost connection to %s: %s",
2020 client_p->name, strerror(current_error));
2021 ilog(L_SERVER, "Lost connection to %s: %s",
2022 log_client_name(client_p, SHOW_IP), strerror(current_error));
2025 sendto_realops_flags(UMODE_ALL, L_ALL,
2026 "%s had been connected for %d day%s, %2d:%02d:%02d",
2027 client_p->name, connected / 86400,
2028 (connected / 86400 == 1) ? "" : "s",
2029 (connected % 86400) / 3600,
2030 (connected % 3600) / 60, connected % 60);
2033 if(error == 0)
2034 strlcpy(errmsg, "Remote host closed the connection", sizeof(errmsg));
2035 else
2036 ircsnprintf(errmsg, sizeof(errmsg), "Read error: %s", strerror(current_error));
2038 exit_client(client_p, client_p, &me, errmsg);