2 * charybdis: an advanced 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
8 * Copyright (C) 2007 William Pitcock
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 * $Id: client.c 3514 2007-06-06 16:25:21Z nenolod $
34 #include "irc_string.h"
35 #include "sprintf_irc.h"
42 #include "s_newconf.h"
55 #include "blacklist.h"
58 #include "irc_dictionary.h"
60 #define DEBUG_EXITED_CLIENTS
62 static void check_pings_list(rb_dlink_list
* list
);
63 static void check_unknowns_list(rb_dlink_list
* list
);
64 static void free_exited_clients(void *unused
);
65 static void exit_aborted_clients(void *unused
);
67 static int exit_remote_client(struct Client
*, struct Client
*, struct Client
*,const char *);
68 static int exit_remote_server(struct Client
*, struct Client
*, struct Client
*,const char *);
69 static int exit_local_client(struct Client
*, struct Client
*, struct Client
*,const char *);
70 static int exit_unknown_client(struct Client
*, struct Client
*, struct Client
*,const char *);
71 static int exit_local_server(struct Client
*, struct Client
*, struct Client
*,const char *);
72 static int qs_server(struct Client
*, struct Client
*, struct Client
*, const char *comment
);
74 static EVH check_pings
;
76 extern BlockHeap
*client_heap
;
77 extern BlockHeap
*lclient_heap
;
78 extern BlockHeap
*pclient_heap
;
80 extern char current_uid
[IDLEN
];
82 struct Dictionary
*nd_dict
= NULL
;
91 rb_dlink_list dead_list
;
92 #ifdef DEBUG_EXITED_CLIENTS
93 static rb_dlink_list dead_remote_list
;
99 struct Client
*client
;
100 char notice
[REASONLEN
];
103 static rb_dlink_list abort_list
;
111 * side effects - initialize client free memory
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 pclient_heap
= BlockHeapCreate(sizeof(struct PreClient
), PCLIENT_HEAP_SIZE
);
123 rb_event_addish("check_pings", check_pings
, NULL
, 30);
124 rb_event_addish("free_exited_clients", &free_exited_clients
, NULL
, 4);
125 rb_event_addish("exit_aborted_clients", exit_aborted_clients
, NULL
, 1);
127 nd_dict
= irc_dictionary_create(irccmp
);
132 * make_client - create a new Client struct and set it to initial state.
134 * from == NULL, create local client (a client connected
137 * from, create remote client (behind a socket
138 * associated with the client defined by
139 * 'from'). ('from' is a local client!!).
142 make_client(struct Client
*from
)
144 struct Client
*client_p
= NULL
;
145 struct LocalUser
*localClient
;
147 client_p
= BlockHeapAlloc(client_heap
);
151 client_p
->from
= client_p
; /* 'from' of local client is self! */
153 localClient
= (struct LocalUser
*) BlockHeapAlloc(lclient_heap
);
154 SetMyConnect(client_p
);
155 client_p
->localClient
= localClient
;
157 client_p
->localClient
->lasttime
= client_p
->localClient
->firsttime
= rb_current_time();
159 client_p
->localClient
->F
= NULL
;
160 client_p
->localClient
->ctrlfd
= -1;
162 client_p
->preClient
= (struct PreClient
*) BlockHeapAlloc(pclient_heap
);
164 /* as good a place as any... */
165 rb_dlinkAdd(client_p
, &client_p
->localClient
->tnode
, &unknown_list
);
168 { /* from is not NULL */
169 client_p
->localClient
= NULL
;
170 client_p
->preClient
= NULL
;
171 client_p
->from
= from
; /* 'from' of local client is self! */
174 SetUnknown(client_p
);
175 strcpy(client_p
->username
, "unknown");
181 free_pre_client(struct Client
*client_p
)
183 struct Blacklist
*blptr
;
185 s_assert(NULL
!= client_p
);
187 if(client_p
->preClient
== NULL
)
190 blptr
= client_p
->preClient
->dnsbl_listed
;
192 unref_blacklist(blptr
);
193 abort_blacklist_queries(client_p
);
194 BlockHeapFree(pclient_heap
, client_p
->preClient
);
195 client_p
->preClient
= NULL
;
199 free_local_client(struct Client
*client_p
)
201 s_assert(NULL
!= client_p
);
202 s_assert(&me
!= client_p
);
204 if(client_p
->localClient
== NULL
)
208 * clean up extra sockets from P-lines which have been discarded.
210 if(client_p
->localClient
->listener
)
212 s_assert(0 < client_p
->localClient
->listener
->ref_count
);
213 if(0 == --client_p
->localClient
->listener
->ref_count
214 && !client_p
->localClient
->listener
->active
)
215 free_listener(client_p
->localClient
->listener
);
216 client_p
->localClient
->listener
= 0;
219 if(client_p
->localClient
->F
)
220 rb_close(client_p
->localClient
->F
);
222 if(client_p
->localClient
->passwd
)
224 memset(client_p
->localClient
->passwd
, 0,
225 strlen(client_p
->localClient
->passwd
));
226 rb_free(client_p
->localClient
->passwd
);
229 rb_free(client_p
->localClient
->challenge
);
230 rb_free(client_p
->localClient
->fullcaps
);
231 rb_free(client_p
->localClient
->opername
);
232 rb_free(client_p
->localClient
->mangledhost
);
234 BlockHeapFree(lclient_heap
, client_p
->localClient
);
235 client_p
->localClient
= NULL
;
239 free_client(struct Client
*client_p
)
241 s_assert(NULL
!= client_p
);
242 s_assert(&me
!= client_p
);
243 free_local_client(client_p
);
244 free_pre_client(client_p
);
245 BlockHeapFree(client_heap
, client_p
);
249 * check_pings - go through the local client list and check activity
250 * kill off stuff that should die
252 * inputs - NOT USED (from event)
253 * output - next time_t when check_pings() should be called again
257 * A PING can be sent to clients as necessary.
259 * Client/Server ping outs are handled.
263 * Addon from adrian. We used to call this after nextping seconds,
264 * however I've changed it to run once a second. This is only for
265 * PING timeouts, not K/etc-line checks (thanks dianora!). Having it
266 * run once a second makes life a lot easier - when a new client connects
267 * and they need a ping in 4 seconds, if nextping was set to 20 seconds
268 * we end up waiting 20 seconds. This is stupid. :-)
269 * I will optimise (hah!) check_pings() once I've finished working on
270 * tidying up other network IO evilnesses.
275 check_pings(void *notused
)
277 check_pings_list(&lclient_list
);
278 check_pings_list(&serv_list
);
279 check_unknowns_list(&unknown_list
);
285 * inputs - pointer to list to check
290 check_pings_list(rb_dlink_list
* list
)
292 char scratch
[32]; /* way too generous but... */
293 struct Client
*client_p
; /* current local client_p being examined */
294 int ping
= 0; /* ping time value from client */
295 rb_dlink_node
*ptr
, *next_ptr
;
297 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
299 client_p
= ptr
->data
;
301 if(!MyConnect(client_p
) || IsDead(client_p
))
304 ping
= get_client_ping(client_p
);
306 if(ping
< (rb_current_time() - client_p
->localClient
->lasttime
))
309 * If the client/server hasnt talked to us in 2*ping seconds
310 * and it has a ping time, then close its connection.
312 if(((rb_current_time() - client_p
->localClient
->lasttime
) >= (2 * ping
)
313 && (client_p
->flags
& FLAGS_PINGSENT
)))
315 if(IsServer(client_p
))
317 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
318 "No response from %s, closing link",
319 get_server_name(client_p
, HIDE_IP
));
321 "No response from %s, closing link",
322 log_client_name(client_p
, HIDE_IP
));
324 (void) rb_snprintf(scratch
, sizeof(scratch
),
325 "Ping timeout: %d seconds",
326 (int) (rb_current_time() - client_p
->localClient
->lasttime
));
328 exit_client(client_p
, client_p
, &me
, scratch
);
331 else if((client_p
->flags
& FLAGS_PINGSENT
) == 0)
334 * if we havent PINGed the connection and we havent
335 * heard from it in a while, PING it to make sure
338 client_p
->flags
|= FLAGS_PINGSENT
;
339 /* not nice but does the job */
340 client_p
->localClient
->lasttime
= rb_current_time() - ping
;
341 sendto_one(client_p
, "PING :%s", me
.name
);
350 * check_unknowns_list
352 * inputs - pointer to list of unknown clients
354 * side effects - unknown clients get marked for termination after n seconds
357 check_unknowns_list(rb_dlink_list
* list
)
359 rb_dlink_node
*ptr
, *next_ptr
;
360 struct Client
*client_p
;
363 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
365 client_p
= ptr
->data
;
367 if(IsDead(client_p
) || IsClosing(client_p
))
371 * Check UNKNOWN connections - if they have been in this state
372 * for > 30s, close them.
375 timeout
= IsAnyServer(client_p
) ? ConfigFileEntry
.connect_timeout
: 30;
376 if((rb_current_time() - client_p
->localClient
->firsttime
) > timeout
)
378 if(IsAnyServer(client_p
))
380 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
381 "No response from %s, closing link",
382 get_server_name(client_p
, HIDE_IP
));
384 "No response from %s, closing link",
385 log_client_name(client_p
, HIDE_IP
));
387 exit_client(client_p
, client_p
, &me
, "Connection timed out");
393 notify_banned_client(struct Client
*client_p
, struct ConfItem
*aconf
, int ban
)
395 static const char conn_closed
[] = "Connection closed";
396 static const char d_lined
[] = "D-lined";
397 static const char k_lined
[] = "K-lined";
398 static const char g_lined
[] = "G-lined";
399 const char *reason
= NULL
;
400 const char *exit_reason
= conn_closed
;
402 if(ConfigFileEntry
.kline_with_reason
&& !EmptyString(aconf
->passwd
))
404 reason
= aconf
->passwd
;
405 exit_reason
= aconf
->passwd
;
409 switch (aconf
->status
)
423 if(ban
== D_LINED
&& !IsPerson(client_p
))
424 sendto_one(client_p
, "NOTICE DLINE :*** You have been D-lined");
426 sendto_one(client_p
, form_str(ERR_YOUREBANNEDCREEP
),
427 me
.name
, client_p
->name
, reason
);
429 exit_client(client_p
, client_p
, &me
,
430 EmptyString(ConfigFileEntry
.kline_reason
) ? exit_reason
:
431 ConfigFileEntry
.kline_reason
);
438 * side effects - Check all connections for a pending k/d/gline against the
439 * client, exit the client if found.
442 check_banned_lines(void)
444 struct Client
*client_p
; /* current local client_p being examined */
445 struct ConfItem
*aconf
= NULL
;
446 rb_dlink_node
*ptr
, *next_ptr
;
448 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
450 client_p
= ptr
->data
;
455 /* if there is a returned struct ConfItem then kill it */
456 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
, client_p
->localClient
->ip
.ss_family
)))
458 if(aconf
->status
& CONF_EXEMPTDLINE
)
461 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
462 "DLINE active for %s",
463 get_client_name(client_p
, HIDE_IP
));
465 notify_banned_client(client_p
, aconf
, D_LINED
);
466 continue; /* and go examine next fd/client_p */
469 if(!IsPerson(client_p
))
472 if((aconf
= find_kline(client_p
)) != NULL
)
474 if(IsExemptKline(client_p
))
476 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
477 "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
478 get_client_name(client_p
, HIDE_IP
),
479 aconf
->user
, aconf
->host
);
483 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
484 "KLINE active for %s",
485 get_client_name(client_p
, HIDE_IP
));
486 notify_banned_client(client_p
, aconf
, K_LINED
);
489 else if((aconf
= find_gline(client_p
)) != NULL
)
491 if(IsExemptKline(client_p
))
493 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
494 "GLINE over-ruled for %s, client is kline_exempt [%s@%s]",
495 get_client_name(client_p
, HIDE_IP
),
496 aconf
->user
, aconf
->host
);
500 if(IsExemptGline(client_p
))
502 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
503 "GLINE over-ruled for %s, client is gline_exempt [%s@%s]",
504 get_client_name(client_p
, HIDE_IP
),
505 aconf
->user
, aconf
->host
);
509 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
510 "GLINE active for %s",
511 get_client_name(client_p
, HIDE_IP
));
513 notify_banned_client(client_p
, aconf
, G_LINED
);
516 else if((aconf
= find_xline(client_p
->info
, 1)) != NULL
)
518 if(IsExemptKline(client_p
))
520 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
521 "XLINE over-ruled for %s, client is kline_exempt [%s]",
522 get_client_name(client_p
, HIDE_IP
),
527 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "XLINE active for %s",
528 get_client_name(client_p
, HIDE_IP
));
530 (void) exit_client(client_p
, client_p
, &me
, "Bad user info");
535 /* also check the unknowns list for new dlines */
536 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, unknown_list
.head
)
538 client_p
= ptr
->data
;
540 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
,client_p
->localClient
->ip
.ss_family
)))
542 if(aconf
->status
& CONF_EXEMPTDLINE
)
545 notify_banned_client(client_p
, aconf
, D_LINED
);
551 /* check_klines_event()
555 * side effects - check_klines() is called, kline_queued unset
558 check_klines_event(void *unused
)
568 * side effects - all clients will be checked for klines
573 struct Client
*client_p
;
574 struct ConfItem
*aconf
;
576 rb_dlink_node
*next_ptr
;
578 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
580 client_p
= ptr
->data
;
582 if(IsMe(client_p
) || !IsPerson(client_p
))
585 if((aconf
= find_kline(client_p
)) != NULL
)
587 if(IsExemptKline(client_p
))
589 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
590 "KLINE over-ruled for %s, client is kline_exempt",
591 get_client_name(client_p
, HIDE_IP
));
595 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
596 "KLINE active for %s",
597 get_client_name(client_p
, HIDE_IP
));
599 notify_banned_client(client_p
, aconf
, K_LINED
);
609 * side effects - all clients will be checked for glines
614 struct Client
*client_p
;
615 struct ConfItem
*aconf
;
617 rb_dlink_node
*next_ptr
;
619 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
621 client_p
= ptr
->data
;
623 if(IsMe(client_p
) || !IsPerson(client_p
))
626 if((aconf
= find_gline(client_p
)) != NULL
)
628 if(IsExemptKline(client_p
))
630 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
631 "GLINE over-ruled for %s, client is kline_exempt",
632 get_client_name(client_p
, HIDE_IP
));
636 if(IsExemptGline(client_p
))
638 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
639 "GLINE over-ruled for %s, client is gline_exempt",
640 get_client_name(client_p
, HIDE_IP
));
644 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
645 "GLINE active for %s",
646 get_client_name(client_p
, HIDE_IP
));
648 notify_banned_client(client_p
, aconf
, K_LINED
);
658 * side effects - all clients will be checked for dlines
663 struct Client
*client_p
;
664 struct ConfItem
*aconf
;
666 rb_dlink_node
*next_ptr
;
668 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
670 client_p
= ptr
->data
;
675 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
,client_p
->localClient
->ip
.ss_family
)) != NULL
)
677 if(aconf
->status
& CONF_EXEMPTDLINE
)
680 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
681 "DLINE active for %s",
682 get_client_name(client_p
, HIDE_IP
));
684 notify_banned_client(client_p
, aconf
, D_LINED
);
689 /* dlines need to be checked against unknowns too */
690 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, unknown_list
.head
)
692 client_p
= ptr
->data
;
694 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
,client_p
->localClient
->ip
.ss_family
)) != NULL
)
696 if(aconf
->status
& CONF_EXEMPTDLINE
)
699 notify_banned_client(client_p
, aconf
, D_LINED
);
708 * side effects - all clients will be checked for xlines
713 struct Client
*client_p
;
714 struct ConfItem
*aconf
;
716 rb_dlink_node
*next_ptr
;
718 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
720 client_p
= ptr
->data
;
722 if(IsMe(client_p
) || !IsPerson(client_p
))
725 if((aconf
= find_xline(client_p
->info
, 1)) != NULL
)
727 if(IsExemptKline(client_p
))
729 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
730 "XLINE over-ruled for %s, client is kline_exempt",
731 get_client_name(client_p
, HIDE_IP
));
735 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "XLINE active for %s",
736 get_client_name(client_p
, HIDE_IP
));
738 (void) exit_client(client_p
, client_p
, &me
, "Bad user info");
745 * update_client_exit_stats
747 * input - pointer to client
752 update_client_exit_stats(struct Client
*client_p
)
754 if(IsServer(client_p
))
756 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
757 "Server %s split from %s",
758 client_p
->name
, client_p
->servptr
->name
);
759 if(HasSentEob(client_p
))
762 else if(IsClient(client_p
))
767 if(IsInvisible(client_p
))
771 if(splitchecking
&& !splitmode
)
772 check_splitmode(NULL
);
776 * release_client_state
778 * input - pointer to client to release
783 release_client_state(struct Client
*client_p
)
785 if(client_p
->user
!= NULL
)
787 free_user(client_p
->user
, client_p
); /* try this here */
791 if(client_p
->serv
->user
!= NULL
)
792 free_user(client_p
->serv
->user
, client_p
);
793 if(client_p
->serv
->fullcaps
)
794 rb_free(client_p
->serv
->fullcaps
);
795 rb_free(client_p
->serv
);
800 * remove_client_from_list
801 * inputs - point to client to remove
803 * side effects - taken the code from ExitOneClient() for this
804 * and placed it here. - avalon
807 remove_client_from_list(struct Client
*client_p
)
809 s_assert(NULL
!= client_p
);
814 /* A client made with make_client()
815 * is on the unknown_list until removed.
816 * If it =does= happen to exit before its removed from that list
817 * and its =not= on the global_client_list, it will core here.
818 * short circuit that case now -db
820 if(client_p
->node
.prev
== NULL
&& client_p
->node
.next
== NULL
)
823 rb_dlinkDelete(&client_p
->node
, &global_client_list
);
825 update_client_exit_stats(client_p
);
830 * find_person - find person by (nick)name.
831 * inputs - pointer to name
832 * output - return client pointer
836 find_person(const char *name
)
838 struct Client
*c2ptr
;
840 c2ptr
= find_client(name
);
842 if(c2ptr
&& IsPerson(c2ptr
))
848 find_named_person(const char *name
)
850 struct Client
*c2ptr
;
852 c2ptr
= find_named_client(name
);
854 if(c2ptr
&& IsPerson(c2ptr
))
861 * find_chasing - find the client structure for a nick name (user)
862 * using history mechanism if necessary. If the client is not found,
863 * an error message (NO SUCH NICK) is generated. If the client was found
864 * through the history, chasing will be 1 and otherwise 0.
867 find_chasing(struct Client
*source_p
, const char *user
, int *chasing
)
871 if(MyClient(source_p
))
872 who
= find_named_person(user
);
874 who
= find_person(user
);
879 if(who
|| IsDigit(*user
))
882 if(!(who
= get_history(user
, (long) KILLCHASETIMELIMIT
)))
884 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
,
885 form_str(ERR_NOSUCHNICK
), user
);
894 * get_client_name - Return the name of the client
895 * for various tracking and
896 * admin purposes. The main purpose of this function is to
897 * return the "socket host" name of the client, if that
898 * differs from the advertised name (other than case).
899 * But, this can be used to any client structure.
902 * Watch out the allocation of "nbuf", if either source_p->name
903 * or source_p->sockhost gets changed into pointers instead of
904 * directly allocated within the structure...
907 * Function return either a pointer to the structure (source_p) or
908 * to internal buffer (nbuf). *NEVER* use the returned pointer
909 * to modify what it points!!!
913 get_client_name(struct Client
*client
, int showip
)
915 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
917 s_assert(NULL
!= client
);
921 if(MyConnect(client
))
923 if(!irccmp(client
->name
, client
->host
))
926 if(ConfigFileEntry
.hide_spoof_ips
&&
927 showip
== SHOW_IP
&& IsIPSpoof(client
))
929 #ifdef HIDE_SERVERS_IPS
930 if(IsAnyServer(client
))
934 /* And finally, let's get the host information, ip or name */
938 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
939 client
->name
, client
->username
,
943 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@255.255.255.255]",
944 client
->name
, client
->username
);
947 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
948 client
->name
, client
->username
, client
->host
);
953 /* As pointed out by Adel Mezibra
954 * Neph|l|m@EFnet. Was missing a return here.
960 get_server_name(struct Client
*target_p
, int showip
)
962 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
967 if(!MyConnect(target_p
) || !irccmp(target_p
->name
, target_p
->host
))
968 return target_p
->name
;
970 #ifdef HIDE_SERVERS_IPS
971 if(EmptyString(target_p
->name
))
973 rb_snprintf(nbuf
, sizeof(nbuf
), "[%s@255.255.255.255]",
978 return target_p
->name
;
984 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
985 target_p
->name
, target_p
->username
,
990 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@255.255.255.255]",
991 target_p
->name
, target_p
->username
);
994 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
995 target_p
->name
, target_p
->username
,
1002 /* log_client_name()
1004 * This version is the same as get_client_name, but doesnt contain the
1005 * code that will hide IPs always. This should be used for logfiles.
1008 log_client_name(struct Client
*target_p
, int showip
)
1010 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
1012 if(target_p
== NULL
)
1015 if(MyConnect(target_p
))
1017 if(irccmp(target_p
->name
, target_p
->host
) == 0)
1018 return target_p
->name
;
1023 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]", target_p
->name
,
1024 target_p
->username
, target_p
->sockhost
);
1028 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@255.255.255.255]",
1029 target_p
->name
, target_p
->username
);
1032 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]", target_p
->name
,
1033 target_p
->username
, target_p
->host
);
1039 return target_p
->name
;
1042 /* is_remote_connect - Returns whether a server was /connect'ed by a remote
1043 * oper (send notices netwide) */
1045 is_remote_connect(struct Client
*client_p
)
1047 struct Client
*oper
;
1049 if (client_p
->serv
== NULL
)
1051 oper
= find_named_person(client_p
->serv
->by
);
1052 return oper
!= NULL
&& IsOper(oper
) && !MyConnect(oper
);
1056 free_exited_clients(void *unused
)
1058 rb_dlink_node
*ptr
, *next
;
1059 struct Client
*target_p
;
1061 RB_DLINK_FOREACH_SAFE(ptr
, next
, dead_list
.head
)
1063 target_p
= ptr
->data
;
1065 #ifdef DEBUG_EXITED_CLIENTS
1067 struct abort_client
*abt
;
1068 rb_dlink_node
*aptr
;
1071 RB_DLINK_FOREACH(aptr
, abort_list
.head
)
1074 if(abt
->client
== target_p
)
1077 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1078 "On abort_list: %s stat: %u flags: %u/%u handler: %c",
1079 target_p
->name
, (unsigned int) target_p
->status
,
1080 target_p
->flags
, target_p
->flags2
, target_p
->handler
);
1081 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1082 "Please report this to the charybdis developers!");
1089 rb_dlinkDestroy(ptr
, &dead_list
);
1095 if(ptr
->data
== NULL
)
1097 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1098 "Warning: null client on dead_list!");
1099 rb_dlinkDestroy(ptr
, &dead_list
);
1102 release_client_state(target_p
);
1103 free_client(target_p
);
1104 rb_dlinkDestroy(ptr
, &dead_list
);
1107 #ifdef DEBUG_EXITED_CLIENTS
1108 RB_DLINK_FOREACH_SAFE(ptr
, next
, dead_remote_list
.head
)
1110 target_p
= ptr
->data
;
1112 if(ptr
->data
== NULL
)
1114 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1115 "Warning: null client on dead_list!");
1116 rb_dlinkDestroy(ptr
, &dead_list
);
1119 release_client_state(target_p
);
1120 free_client(target_p
);
1121 rb_dlinkDestroy(ptr
, &dead_remote_list
);
1128 ** Recursively send QUITs and SQUITs for source_p and all its dependent clients
1129 ** and servers to those servers that need them. A server needs the client
1130 ** QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it
1131 ** isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once
1132 ** a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending
1133 ** on that one -orabidoo
1136 recurse_send_quits(struct Client
*client_p
, struct Client
*source_p
,
1137 struct Client
*to
, const char *comment1
,
1138 const char *comment
)
1140 struct Client
*target_p
;
1141 rb_dlink_node
*ptr
, *ptr_next
;
1142 /* If this server can handle quit storm (QS) removal
1143 * of dependents, just send the SQUIT
1146 if(IsCapable(to
, CAP_QS
))
1148 sendto_one(to
, "SQUIT %s :%s",
1149 get_id(source_p
, to
), comment
);
1153 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1155 target_p
= ptr
->data
;
1156 sendto_one(to
, ":%s QUIT :%s", target_p
->name
, comment1
);
1158 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->servers
.head
)
1160 target_p
= ptr
->data
;
1161 recurse_send_quits(client_p
, target_p
, to
, comment1
, comment
);
1163 sendto_one(to
, "SQUIT %s :%s", source_p
->name
, comment
);
1168 ** Remove all clients that depend on source_p; assumes all (S)QUITs have
1169 ** already been sent. we make sure to exit a server's dependent clients
1170 ** and servers before the server itself; exit_one_client takes care of
1171 ** actually removing things off llists. tweaked from +CSr31 -orabidoo
1174 * added sanity test code.... source_p->serv might be NULL...
1177 recurse_remove_clients(struct Client
*source_p
, const char *comment
)
1179 struct Client
*target_p
;
1180 rb_dlink_node
*ptr
, *ptr_next
;
1185 if(source_p
->serv
== NULL
) /* oooops. uh this is actually a major bug */
1188 /* this is very ugly, but it saves cpu :P */
1189 if(ConfigFileEntry
.nick_delay
> 0)
1191 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1193 target_p
= ptr
->data
;
1194 target_p
->flags
|= FLAGS_KILLED
;
1195 add_nd_entry(target_p
->name
);
1197 if(!IsDead(target_p
) && !IsClosing(target_p
))
1198 exit_remote_client(NULL
, target_p
, &me
, comment
);
1203 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1205 target_p
= ptr
->data
;
1206 target_p
->flags
|= FLAGS_KILLED
;
1208 if(!IsDead(target_p
) && !IsClosing(target_p
))
1209 exit_remote_client(NULL
, target_p
, &me
, comment
);
1213 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->servers
.head
)
1215 target_p
= ptr
->data
;
1216 recurse_remove_clients(target_p
, comment
);
1217 qs_server(NULL
, target_p
, &me
, comment
);
1222 ** Remove *everything* that depends on source_p, from all lists, and sending
1223 ** all necessary QUITs and SQUITs. source_p itself is still on the lists,
1224 ** and its SQUITs have been sent except for the upstream one -orabidoo
1227 remove_dependents(struct Client
*client_p
,
1228 struct Client
*source_p
,
1229 struct Client
*from
, const char *comment
, const char *comment1
)
1232 rb_dlink_node
*ptr
, *next
;
1234 RB_DLINK_FOREACH_SAFE(ptr
, next
, serv_list
.head
)
1238 if(IsMe(to
) || to
== source_p
->from
||
1239 (to
== client_p
&& IsCapable(to
, CAP_QS
)))
1242 recurse_send_quits(client_p
, source_p
, to
, comment1
, comment
);
1245 recurse_remove_clients(source_p
, comment1
);
1249 exit_aborted_clients(void *unused
)
1251 struct abort_client
*abt
;
1252 rb_dlink_node
*ptr
, *next
;
1253 RB_DLINK_FOREACH_SAFE(ptr
, next
, abort_list
.head
)
1257 #ifdef DEBUG_EXITED_CLIENTS
1259 if(rb_dlinkFind(abt
->client
, &dead_list
))
1262 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1263 "On dead_list: %s stat: %u flags: %u/%u handler: %c",
1264 abt
->client
->name
, (unsigned int) abt
->client
->status
,
1265 abt
->client
->flags
, abt
->client
->flags2
, abt
->client
->handler
);
1266 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1267 "Please report this to the charybdis developers!");
1273 s_assert(*((unsigned long*)abt
->client
) != 0xdeadbeef); /* This is lame but its a debug thing */
1274 rb_dlinkDelete(ptr
, &abort_list
);
1276 if(IsAnyServer(abt
->client
))
1277 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1278 "Closing link to %s: %s",
1279 get_server_name(abt
->client
, HIDE_IP
), abt
->notice
);
1281 /* its no longer on abort list - we *must* remove
1282 * FLAGS_CLOSING otherwise exit_client() will not run --fl
1284 abt
->client
->flags
&= ~FLAGS_CLOSING
;
1285 exit_client(abt
->client
, abt
->client
, &me
, abt
->notice
);
1292 * dead_link - Adds client to a list of clients that need an exit_client()
1296 dead_link(struct Client
*client_p
)
1298 struct abort_client
*abt
;
1300 s_assert(!IsMe(client_p
));
1301 if(IsDead(client_p
) || IsClosing(client_p
) || IsMe(client_p
))
1304 abt
= (struct abort_client
*) rb_malloc(sizeof(struct abort_client
));
1306 if(client_p
->flags
& FLAGS_SENDQEX
)
1307 strlcpy(abt
->notice
, "Max SendQ exceeded", sizeof(abt
->notice
));
1309 rb_snprintf(abt
->notice
, sizeof(abt
->notice
), "Write error: %s", strerror(errno
));
1311 abt
->client
= client_p
;
1312 SetIOError(client_p
);
1314 SetClosing(client_p
);
1315 rb_dlinkAdd(abt
, &abt
->node
, &abort_list
);
1319 /* This does the remove of the user from channels..local or remote */
1321 exit_generic_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1322 const char *comment
)
1324 rb_dlink_node
*ptr
, *next_ptr
;
1326 if(IsOper(source_p
))
1327 rb_dlinkFindDestroy(source_p
, &oper_list
);
1329 sendto_common_channels_local(source_p
, ":%s!%s@%s QUIT :%s",
1331 source_p
->username
, source_p
->host
, comment
);
1333 remove_user_from_channels(source_p
);
1335 /* Should not be in any channels now */
1336 s_assert(source_p
->user
->channel
.head
== NULL
);
1338 /* Clean up invitefield */
1339 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, source_p
->user
->invited
.head
)
1341 del_invite(ptr
->data
, source_p
);
1344 /* Clean up allow lists */
1345 del_all_accepts(source_p
);
1347 add_history(source_p
, 0);
1348 off_history(source_p
);
1350 monitor_signoff(source_p
);
1352 if(has_id(source_p
))
1353 del_from_id_hash(source_p
->id
, source_p
);
1355 del_from_hostname_hash(source_p
->orighost
, source_p
);
1356 del_from_client_hash(source_p
->name
, source_p
);
1357 remove_client_from_list(source_p
);
1361 * Assumes IsPerson(source_p) && !MyConnect(source_p)
1365 exit_remote_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1366 const char *comment
)
1368 exit_generic_client(client_p
, source_p
, from
, comment
);
1370 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1372 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->users
);
1375 if((source_p
->flags
& FLAGS_KILLED
) == 0)
1377 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
1378 ":%s QUIT :%s", use_id(source_p
), comment
);
1379 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
,
1380 ":%s QUIT :%s", source_p
->name
, comment
);
1384 #ifdef DEBUG_EXITED_CLIENTS
1385 rb_dlinkAddAlloc(source_p
, &dead_remote_list
);
1387 rb_dlinkAddAlloc(source_p
, &dead_list
);
1389 return(CLIENT_EXITED
);
1393 * This assumes IsUnknown(source_p) == TRUE and MyConnect(source_p) == TRUE
1397 exit_unknown_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1398 const char *comment
)
1400 delete_auth_queries(source_p
);
1401 del_unknown_ip(source_p
);
1402 rb_dlinkDelete(&source_p
->localClient
->tnode
, &unknown_list
);
1404 if(!IsIOError(source_p
))
1405 sendto_one(source_p
, "ERROR :Closing Link: %s (%s)",
1406 source_p
->user
!= NULL
? source_p
->host
: "127.0.0.1",
1409 close_connection(source_p
);
1411 if(has_id(source_p
))
1412 del_from_id_hash(source_p
->id
, source_p
);
1414 del_from_hostname_hash(source_p
->host
, source_p
);
1415 del_from_client_hash(source_p
->name
, source_p
);
1416 remove_client_from_list(source_p
);
1418 rb_dlinkAddAlloc(source_p
, &dead_list
);
1420 /* Note that we don't need to add unknowns to the dead_list */
1421 return(CLIENT_EXITED
);
1425 exit_remote_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1426 const char *comment
)
1428 static char comment1
[(HOSTLEN
*2)+2];
1429 static char newcomment
[BUFSIZE
];
1430 struct Client
*target_p
;
1432 if(ConfigServerHide
.flatten_links
)
1433 strcpy(comment1
, "*.net *.split");
1436 strcpy(comment1
, source_p
->servptr
->name
);
1437 strcat(comment1
, " ");
1438 strcat(comment1
, source_p
->name
);
1441 rb_snprintf(newcomment
, sizeof(newcomment
), "by %s: %s",
1442 from
->name
, comment
);
1444 if(source_p
->serv
!= NULL
)
1445 remove_dependents(client_p
, source_p
, from
, IsPerson(from
) ? newcomment
: comment
, comment1
);
1447 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1448 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1452 rb_dlinkFindDestroy(source_p
, &global_serv_list
);
1453 target_p
= source_p
->from
;
1455 if(target_p
!= NULL
&& IsServer(target_p
) && target_p
!= client_p
&&
1456 !IsMe(target_p
) && (source_p
->flags
& FLAGS_KILLED
) == 0)
1458 sendto_one(target_p
, ":%s SQUIT %s :%s",
1459 get_id(from
, target_p
), get_id(source_p
, target_p
),
1463 if(has_id(source_p
))
1464 del_from_id_hash(source_p
->id
, source_p
);
1466 del_from_client_hash(source_p
->name
, source_p
);
1467 remove_client_from_list(source_p
);
1468 scache_split(source_p
->serv
->nameinfo
);
1471 #ifdef DEBUG_EXITED_CLIENTS
1472 rb_dlinkAddAlloc(source_p
, &dead_remote_list
);
1474 rb_dlinkAddAlloc(source_p
, &dead_list
);
1480 qs_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1481 const char *comment
)
1483 struct Client
*target_p
;
1485 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1486 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1490 rb_dlinkFindDestroy(source_p
, &global_serv_list
);
1491 target_p
= source_p
->from
;
1493 if(has_id(source_p
))
1494 del_from_id_hash(source_p
->id
, source_p
);
1496 del_from_client_hash(source_p
->name
, source_p
);
1497 remove_client_from_list(source_p
);
1498 scache_split(source_p
->serv
->nameinfo
);
1501 rb_dlinkAddAlloc(source_p
, &dead_list
);
1506 exit_local_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1507 const char *comment
)
1509 static char comment1
[(HOSTLEN
*2)+2];
1510 static char newcomment
[BUFSIZE
];
1511 unsigned int sendk
, recvk
;
1513 rb_dlinkDelete(&source_p
->localClient
->tnode
, &serv_list
);
1514 rb_dlinkFindDestroy(source_p
, &global_serv_list
);
1516 unset_chcap_usage_counts(source_p
);
1517 sendk
= source_p
->localClient
->sendK
;
1518 recvk
= source_p
->localClient
->receiveK
;
1520 /* Always show source here, so the server notices show
1521 * which side initiated the split -- jilles
1523 rb_snprintf(newcomment
, sizeof(newcomment
), "by %s: %s",
1524 from
== source_p
? me
.name
: from
->name
, comment
);
1525 if (!IsIOError(source_p
))
1526 sendto_one(source_p
, "SQUIT %s :%s", use_id(source_p
),
1528 if(client_p
!= NULL
&& source_p
!= client_p
&& !IsIOError(source_p
))
1530 sendto_one(source_p
, "ERROR :Closing Link: 127.0.0.1 %s (%s)",
1531 source_p
->name
, comment
);
1534 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1535 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1540 close_connection(source_p
);
1542 if(ConfigServerHide
.flatten_links
)
1543 strcpy(comment1
, "*.net *.split");
1546 strcpy(comment1
, source_p
->servptr
->name
);
1547 strcat(comment1
, " ");
1548 strcat(comment1
, source_p
->name
);
1551 if(source_p
->serv
!= NULL
)
1552 remove_dependents(client_p
, source_p
, from
, IsPerson(from
) ? newcomment
: comment
, comment1
);
1554 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s was connected"
1555 " for %ld seconds. %d/%d sendK/recvK.",
1556 source_p
->name
, rb_current_time() - source_p
->localClient
->firsttime
, sendk
, recvk
);
1558 ilog(L_SERVER
, "%s was connected for %ld seconds. %d/%d sendK/recvK.",
1559 source_p
->name
, rb_current_time() - source_p
->localClient
->firsttime
, sendk
, recvk
);
1561 if(has_id(source_p
))
1562 del_from_id_hash(source_p
->id
, source_p
);
1564 del_from_client_hash(source_p
->name
, source_p
);
1565 remove_client_from_list(source_p
);
1566 scache_split(source_p
->serv
->nameinfo
);
1569 rb_dlinkAddAlloc(source_p
, &dead_list
);
1575 * This assumes IsPerson(source_p) == TRUE && MyConnect(source_p) == TRUE
1579 exit_local_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1580 const char *comment
)
1582 unsigned long on_for
;
1584 exit_generic_client(client_p
, source_p
, from
, comment
);
1585 clear_monitor(source_p
);
1587 s_assert(IsPerson(source_p
));
1588 rb_dlinkDelete(&source_p
->localClient
->tnode
, &lclient_list
);
1589 rb_dlinkDelete(&source_p
->lnode
, &me
.serv
->users
);
1591 if(IsOper(source_p
))
1592 rb_dlinkFindDestroy(source_p
, &local_oper_list
);
1594 sendto_realops_snomask(SNO_CCONN
, L_ALL
,
1595 "Client exiting: %s (%s@%s) [%s] [%s]",
1597 source_p
->username
, source_p
->host
, comment
,
1598 show_ip(NULL
, source_p
) ? source_p
->sockhost
: "255.255.255.255");
1600 sendto_realops_snomask(SNO_CCONNEXT
, L_ALL
,
1601 "CLIEXIT %s %s %s %s 0 %s",
1602 source_p
->name
, source_p
->username
, source_p
->host
,
1603 show_ip(NULL
, source_p
) ? source_p
->sockhost
: "255.255.255.255",
1606 on_for
= rb_current_time() - source_p
->localClient
->firsttime
;
1608 ilog(L_USER
, "%s (%3lu:%02lu:%02lu): %s!%s@%s %d/%d",
1609 myctime(rb_current_time()), on_for
/ 3600,
1610 (on_for
% 3600) / 60, on_for
% 60,
1611 source_p
->name
, source_p
->username
, source_p
->host
,
1612 source_p
->localClient
->sendK
, source_p
->localClient
->receiveK
);
1614 sendto_one(source_p
, "ERROR :Closing Link: %s (%s)", source_p
->host
, comment
);
1615 close_connection(source_p
);
1617 if((source_p
->flags
& FLAGS_KILLED
) == 0)
1619 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
1620 ":%s QUIT :%s", use_id(source_p
), comment
);
1621 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
,
1622 ":%s QUIT :%s", source_p
->name
, comment
);
1626 rb_dlinkAddAlloc(source_p
, &dead_list
);
1627 return(CLIENT_EXITED
);
1632 ** exit_client - This is old "m_bye". Name changed, because this is not a
1633 ** protocol function, but a general server utility function.
1635 ** This function exits a client of *any* type (user, server, etc)
1636 ** from this server. Also, this generates all necessary prototol
1637 ** messages that this exit may cause.
1639 ** 1) If the client is a local client, then this implicitly
1640 ** exits all other clients depending on this connection (e.g.
1641 ** remote clients having 'from'-field that points to this.
1643 ** 2) If the client is a remote client, then only this is exited.
1645 ** For convenience, this function returns a suitable value for
1646 ** m_function return value:
1648 ** CLIENT_EXITED if (client_p == source_p)
1649 ** 0 if (client_p != source_p)
1652 exit_client(struct Client
*client_p
, /* The local client originating the
1653 * exit or NULL, if this exit is
1654 * generated by this server for
1656 * This will not get any of the
1657 * generated messages. */
1658 struct Client
*source_p
, /* Client exiting */
1659 struct Client
*from
, /* Client firing off this Exit,
1661 const char *comment
/* Reason for the exit */
1664 hook_data_client_exit hdata
;
1665 if(IsClosing(source_p
))
1668 /* note, this HAS to be here, when we exit a client we attempt to
1669 * send them data, if this generates a write error we must *not* add
1670 * them to the abort list --fl
1672 SetClosing(source_p
);
1674 hdata
.local_link
= client_p
;
1675 hdata
.target
= source_p
;
1677 hdata
.comment
= comment
;
1678 call_hook(h_client_exit
, &hdata
);
1680 if(MyConnect(source_p
))
1682 /* Local clients of various types */
1683 if(IsPerson(source_p
))
1684 return exit_local_client(client_p
, source_p
, from
, comment
);
1685 else if(IsServer(source_p
))
1686 return exit_local_server(client_p
, source_p
, from
, comment
);
1687 /* IsUnknown || IsConnecting || IsHandShake */
1688 else if(!IsReject(source_p
))
1689 return exit_unknown_client(client_p
, source_p
, from
, comment
);
1694 if(IsPerson(source_p
))
1695 return exit_remote_client(client_p
, source_p
, from
, comment
);
1696 else if(IsServer(source_p
))
1697 return exit_remote_server(client_p
, source_p
, from
, comment
);
1704 * Count up local client memory
1707 /* XXX one common Client list now */
1709 count_local_client_memory(size_t * count
, size_t * local_client_memory_used
)
1712 BlockHeapUsage(lclient_heap
, count
, NULL
, &lusage
);
1713 *local_client_memory_used
= lusage
+ (*count
* (sizeof(MemBlock
) + sizeof(struct Client
)));
1717 * Count up remote client memory
1720 count_remote_client_memory(size_t * count
, size_t * remote_client_memory_used
)
1722 size_t lcount
, rcount
;
1723 BlockHeapUsage(lclient_heap
, &lcount
, NULL
, NULL
);
1724 BlockHeapUsage(client_heap
, &rcount
, NULL
, NULL
);
1725 *count
= rcount
- lcount
;
1726 *remote_client_memory_used
= *count
* (sizeof(MemBlock
) + sizeof(struct Client
));
1731 * accept processing, this adds a form of "caller ID" to ircd
1733 * If a client puts themselves into "caller ID only" mode,
1734 * only clients that match a client pointer they have put on
1735 * the accept list will be allowed to message them.
1737 * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ]
1739 * [target.allow_list] -> [ source1 ] -> [source2 ]
1741 * i.e. a target will have a link list of source pointers it will allow
1742 * each source client then has a back pointer pointing back
1743 * to the client that has it on its accept list.
1744 * This allows for exit_one_client to remove these now bogus entries
1745 * from any client having an accept on them.
1750 * inputs - pointer to exiting client
1752 * side effects - Walk through given clients allow_list and on_allow_list
1753 * remove all references to this client
1756 del_all_accepts(struct Client
*client_p
)
1759 rb_dlink_node
*next_ptr
;
1760 struct Client
*target_p
;
1762 if(MyClient(client_p
) && client_p
->localClient
->allow_list
.head
)
1764 /* clear this clients accept list, and remove them from
1765 * everyones on_accept_list
1767 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->localClient
->allow_list
.head
)
1769 target_p
= ptr
->data
;
1770 rb_dlinkFindDestroy(client_p
, &target_p
->on_allow_list
);
1771 rb_dlinkDestroy(ptr
, &client_p
->localClient
->allow_list
);
1775 /* remove this client from everyones accept list */
1776 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->on_allow_list
.head
)
1778 target_p
= ptr
->data
;
1779 rb_dlinkFindDestroy(client_p
, &target_p
->localClient
->allow_list
);
1780 rb_dlinkDestroy(ptr
, &client_p
->on_allow_list
);
1785 * show_ip() - asks if the true IP shoudl be shown when source is
1786 * askin for info about target
1788 * Inputs - source_p who is asking
1789 * - target_p who do we want the info on
1790 * Output - returns 1 if clear IP can be showed, otherwise 0
1791 * Side Effects - none
1795 show_ip(struct Client
*source_p
, struct Client
*target_p
)
1797 if(IsAnyServer(target_p
))
1799 #ifndef HIDE_SERVERS_IPS
1800 if(source_p
== NULL
|| IsOper(source_p
))
1805 else if(IsIPSpoof(target_p
))
1807 /* source == NULL indicates message is being sent
1810 if(!ConfigFileEntry
.hide_spoof_ips
&&
1811 (source_p
== NULL
|| MyOper(source_p
)))
1815 else if(IsDynSpoof(target_p
) && (source_p
!= NULL
&& !IsOper(source_p
)))
1822 show_ip_conf(struct ConfItem
*aconf
, struct Client
*source_p
)
1824 if(IsConfDoSpoofIp(aconf
))
1826 if(!ConfigFileEntry
.hide_spoof_ips
&& MyOper(source_p
))
1841 * side effects - Creates a block heap for struct Users
1844 static BlockHeap
*user_heap
;
1848 user_heap
= BlockHeapCreate(sizeof(struct User
), USER_HEAP_SIZE
);
1856 * inputs - pointer to client struct
1857 * output - pointer to struct User
1858 * side effects - add's an User information block to a client
1859 * if it was not previously allocated.
1862 make_user(struct Client
*client_p
)
1866 user
= client_p
->user
;
1869 user
= (struct User
*) BlockHeapAlloc(user_heap
);
1871 client_p
->user
= user
;
1879 * inputs - pointer to client struct
1880 * output - pointer to server_t
1881 * side effects - add's an Server information block to a client
1882 * if it was not previously allocated.
1885 make_server(struct Client
*client_p
)
1887 server_t
*serv
= client_p
->serv
;
1891 serv
= (server_t
*) rb_malloc(sizeof(server_t
));
1892 client_p
->serv
= serv
;
1894 return client_p
->serv
;
1900 * inputs - pointer to user struct
1901 * - pointer to client struct
1903 * side effects - Decrease user reference count by one and release block,
1904 * if count reaches 0
1907 free_user(struct User
*user
, struct Client
*client_p
)
1909 if(--user
->refcnt
<= 0)
1912 rb_free((char *) user
->away
);
1916 if(user
->refcnt
< 0 || user
->invited
.head
|| user
->channel
.head
)
1918 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1919 "* %#lx user (%s!%s@%s) %#lx %#lx %#lx %lu %d *",
1920 (unsigned long) client_p
,
1921 client_p
? client_p
->
1925 (unsigned long) user
,
1926 (unsigned long) user
->invited
.head
,
1927 (unsigned long) user
->channel
.head
,
1928 rb_dlink_list_length(&user
->channel
),
1930 s_assert(!user
->refcnt
);
1931 s_assert(!user
->invited
.head
);
1932 s_assert(!user
->channel
.head
);
1935 BlockHeapFree(user_heap
, user
);
1940 allocate_away(struct Client
*client_p
)
1942 if(client_p
->user
->away
== NULL
)
1943 client_p
->user
->away
= rb_bh_alloc(away_heap
);
1948 free_away(struct Client
*client_p
)
1950 if(client_p
->user
->away
!= NULL
) {
1951 rb_bh_free(away_heap
, client_p
->user
->away
);
1952 client_p
->user
->away
= NULL
;
1961 for(i
= 0; i
< 3; i
++)
1962 current_uid
[i
] = me
.id
[i
];
1964 for(i
= 3; i
< 9; i
++)
1965 current_uid
[i
] = 'A';
1967 current_uid
[9] = '\0';
1976 for(i
= 8; i
> 3; i
--)
1978 if(current_uid
[i
] == 'Z')
1980 current_uid
[i
] = '0';
1983 else if(current_uid
[i
] != '9')
1989 current_uid
[i
] = 'A';
1992 /* if this next if() triggers, we're fucked. */
1993 if(current_uid
[3] == 'Z')
1995 current_uid
[i
] = 'A';
2006 * Close the physical connection. This function must make
2007 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
2010 close_connection(struct Client
*client_p
)
2012 s_assert(client_p
!= NULL
);
2013 if(client_p
== NULL
)
2016 s_assert(MyConnect(client_p
));
2017 if(!MyConnect(client_p
))
2020 if(IsServer(client_p
))
2022 struct server_conf
*server_p
;
2024 ServerStats
->is_sv
++;
2025 ServerStats
->is_sbs
+= client_p
->localClient
->sendB
;
2026 ServerStats
->is_sbr
+= client_p
->localClient
->receiveB
;
2027 ServerStats
->is_sks
+= client_p
->localClient
->sendK
;
2028 ServerStats
->is_skr
+= client_p
->localClient
->receiveK
;
2029 ServerStats
->is_sti
+= rb_current_time() - client_p
->localClient
->firsttime
;
2030 if(ServerStats
->is_sbs
> 2047)
2032 ServerStats
->is_sks
+= (ServerStats
->is_sbs
>> 10);
2033 ServerStats
->is_sbs
&= 0x3ff;
2035 if(ServerStats
->is_sbr
> 2047)
2037 ServerStats
->is_skr
+= (ServerStats
->is_sbr
>> 10);
2038 ServerStats
->is_sbr
&= 0x3ff;
2042 * If the connection has been up for a long amount of time, schedule
2043 * a 'quick' reconnect, else reset the next-connect cycle.
2045 if((server_p
= find_server_conf(client_p
->name
)) != NULL
)
2048 * Reschedule a faster reconnect, if this was a automatically
2049 * connected configuration entry. (Note that if we have had
2050 * a rehash in between, the status has been changed to
2051 * CONF_ILLEGAL). But only do this if it was a "good" link.
2053 server_p
->hold
= time(NULL
);
2055 (server_p
->hold
- client_p
->localClient
->lasttime
>
2056 HANGONGOODLINK
) ? HANGONRETRYDELAY
: ConFreq(server_p
->class);
2060 else if(IsClient(client_p
))
2062 ServerStats
->is_cl
++;
2063 ServerStats
->is_cbs
+= client_p
->localClient
->sendB
;
2064 ServerStats
->is_cbr
+= client_p
->localClient
->receiveB
;
2065 ServerStats
->is_cks
+= client_p
->localClient
->sendK
;
2066 ServerStats
->is_ckr
+= client_p
->localClient
->receiveK
;
2067 ServerStats
->is_cti
+= rb_current_time() - client_p
->localClient
->firsttime
;
2068 if(ServerStats
->is_cbs
> 2047)
2070 ServerStats
->is_cks
+= (ServerStats
->is_cbs
>> 10);
2071 ServerStats
->is_cbs
&= 0x3ff;
2073 if(ServerStats
->is_cbr
> 2047)
2075 ServerStats
->is_ckr
+= (ServerStats
->is_cbr
>> 10);
2076 ServerStats
->is_cbr
&= 0x3ff;
2080 ServerStats
->is_ni
++;
2082 if(client_p
->localClient
->F
)
2084 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
2085 if(!IsIOError(client_p
))
2086 send_queued(client_p
);
2088 rb_close(client_p
->localClient
->F
);
2089 client_p
->localClient
->F
= NULL
;
2092 if(-1 < client_p
->localClient
->ctrlfd
)
2094 rb_close(client_p
->localClient
->ctrlfd
);
2095 client_p
->localClient
->ctrlfd
= -1;
2098 rb_linebuf_donebuf(&client_p
->localClient
->buf_sendq
);
2099 rb_linebuf_donebuf(&client_p
->localClient
->buf_recvq
);
2100 detach_conf(client_p
);
2102 /* XXX shouldnt really be done here. */
2103 detach_server_conf(client_p
);
2105 client_p
->from
= NULL
; /* ...this should catch them! >:) --msa */
2106 ClearMyConnect(client_p
);
2107 SetIOError(client_p
);
2113 error_exit_client(struct Client
*client_p
, int error
)
2116 * ...hmm, with non-blocking sockets we might get
2117 * here from quite valid reasons, although.. why
2118 * would select report "data available" when there
2119 * wasn't... so, this must be an error anyway... --msa
2120 * actually, EOF occurs when read() returns 0 and
2121 * in due course, select() returns that fd as ready
2122 * for reading even though it ends up being an EOF. -avalon
2125 int current_error
= rb_get_sockerr(client_p
->localClient
->F
);
2127 SetIOError(client_p
);
2129 if(IsServer(client_p
) || IsHandshake(client_p
))
2133 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) && !IsServer(client_p
) ? L_NETWIDE
: L_ALL
,
2134 "Server %s closed the connection",
2135 get_server_name(client_p
, SHOW_IP
));
2137 ilog(L_SERVER
, "Server %s closed the connection",
2138 log_client_name(client_p
, SHOW_IP
));
2142 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) && !IsServer(client_p
) ? L_NETWIDE
: L_ALL
,
2143 "Lost connection to %s: %s",
2144 client_p
->name
, strerror(current_error
));
2145 ilog(L_SERVER
, "Lost connection to %s: %s",
2146 log_client_name(client_p
, SHOW_IP
), strerror(current_error
));
2151 strlcpy(errmsg
, "Remote host closed the connection", sizeof(errmsg
));
2153 rb_snprintf(errmsg
, sizeof(errmsg
), "Read error: %s", strerror(current_error
));
2155 exit_client(client_p
, client_p
, &me
, errmsg
);