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"
61 #define DEBUG_EXITED_CLIENTS
63 static void check_pings_list(rb_dlink_list
* list
);
64 static void check_unknowns_list(rb_dlink_list
* list
);
65 static void free_exited_clients(void *unused
);
66 static void exit_aborted_clients(void *unused
);
68 static int exit_remote_client(struct Client
*, struct Client
*, struct Client
*,const char *);
69 static int exit_remote_server(struct Client
*, struct Client
*, struct Client
*,const char *);
70 static int exit_local_client(struct Client
*, struct Client
*, struct Client
*,const char *);
71 static int exit_unknown_client(struct Client
*, struct Client
*, struct Client
*,const char *);
72 static int exit_local_server(struct Client
*, struct Client
*, struct Client
*,const char *);
73 static int qs_server(struct Client
*, struct Client
*, struct Client
*, const char *comment
);
75 static EVH check_pings
;
77 extern rb_bh
*client_heap
;
78 extern rb_bh
*lclient_heap
;
79 extern rb_bh
*pclient_heap
;
80 static rb_bh
*away_heap
= NULL
;
82 extern char current_uid
[IDLEN
];
84 struct Dictionary
*nd_dict
= NULL
;
93 rb_dlink_list dead_list
;
94 #ifdef DEBUG_EXITED_CLIENTS
95 static rb_dlink_list dead_remote_list
;
101 struct Client
*client
;
102 char notice
[REASONLEN
];
105 static rb_dlink_list abort_list
;
113 * side effects - initialize client free memory
119 * start off the check ping event .. -- adrian
120 * Every 30 seconds is plenty -- db
122 client_heap
= rb_bh_create(sizeof(struct Client
), CLIENT_HEAP_SIZE
, "client_heap");
123 lclient_heap
= rb_bh_create(sizeof(struct LocalUser
), LCLIENT_HEAP_SIZE
, "lclient_heap");
124 pclient_heap
= rb_bh_create(sizeof(struct PreClient
), PCLIENT_HEAP_SIZE
, "pclient_heap");
125 away_heap
= rb_bh_create(AWAYLEN
, AWAY_HEAP_SIZE
, "away_heap");
127 rb_event_addish("check_pings", check_pings
, NULL
, 30);
128 rb_event_addish("free_exited_clients", &free_exited_clients
, NULL
, 4);
129 rb_event_addish("exit_aborted_clients", exit_aborted_clients
, NULL
, 1);
130 rb_event_add("flood_recalc", flood_recalc
, NULL
, 1);
132 nd_dict
= irc_dictionary_create(irccmp
);
137 * make_client - create a new Client struct and set it to initial state.
139 * from == NULL, create local client (a client connected
142 * from, create remote client (behind a socket
143 * associated with the client defined by
144 * 'from'). ('from' is a local client!!).
147 make_client(struct Client
*from
)
149 struct Client
*client_p
= NULL
;
150 struct LocalUser
*localClient
;
152 client_p
= rb_bh_alloc(client_heap
);
156 client_p
->from
= client_p
; /* 'from' of local client is self! */
158 localClient
= (struct LocalUser
*) rb_bh_alloc(lclient_heap
);
159 SetMyConnect(client_p
);
160 client_p
->localClient
= localClient
;
162 client_p
->localClient
->lasttime
= client_p
->localClient
->firsttime
= rb_current_time();
164 client_p
->localClient
->F
= NULL
;
166 client_p
->preClient
= (struct PreClient
*) rb_bh_alloc(pclient_heap
);
168 /* as good a place as any... */
169 rb_dlinkAdd(client_p
, &client_p
->localClient
->tnode
, &unknown_list
);
172 { /* from is not NULL */
173 client_p
->localClient
= NULL
;
174 client_p
->preClient
= NULL
;
175 client_p
->from
= from
; /* 'from' of local client is self! */
178 SetUnknown(client_p
);
179 strcpy(client_p
->username
, "unknown");
185 free_pre_client(struct Client
*client_p
)
187 struct Blacklist
*blptr
;
189 s_assert(NULL
!= client_p
);
191 if(client_p
->preClient
== NULL
)
194 blptr
= client_p
->preClient
->dnsbl_listed
;
196 unref_blacklist(blptr
);
197 abort_blacklist_queries(client_p
);
198 rb_bh_free(pclient_heap
, client_p
->preClient
);
199 client_p
->preClient
= NULL
;
203 free_local_client(struct Client
*client_p
)
205 s_assert(NULL
!= client_p
);
206 s_assert(&me
!= client_p
);
208 if(client_p
->localClient
== NULL
)
212 * clean up extra sockets from P-lines which have been discarded.
214 if(client_p
->localClient
->listener
)
216 s_assert(0 < client_p
->localClient
->listener
->ref_count
);
217 if(0 == --client_p
->localClient
->listener
->ref_count
218 && !client_p
->localClient
->listener
->active
)
219 free_listener(client_p
->localClient
->listener
);
220 client_p
->localClient
->listener
= 0;
223 if(client_p
->localClient
->F
!= NULL
)
225 del_from_cli_fd_hash(client_p
);
226 rb_close(client_p
->localClient
->F
);
229 if(client_p
->localClient
->passwd
)
231 memset(client_p
->localClient
->passwd
, 0,
232 strlen(client_p
->localClient
->passwd
));
233 rb_free(client_p
->localClient
->passwd
);
236 rb_free(client_p
->localClient
->challenge
);
237 rb_free(client_p
->localClient
->fullcaps
);
238 rb_free(client_p
->localClient
->opername
);
239 rb_free(client_p
->localClient
->mangledhost
);
241 ssld_decrement_clicount(client_p
->localClient
->ssl_ctl
);
243 rb_bh_free(lclient_heap
, client_p
->localClient
);
244 client_p
->localClient
= NULL
;
248 free_client(struct Client
*client_p
)
250 s_assert(NULL
!= client_p
);
251 s_assert(&me
!= client_p
);
252 free_local_client(client_p
);
253 free_pre_client(client_p
);
254 rb_bh_free(client_heap
, client_p
);
258 * check_pings - go through the local client list and check activity
259 * kill off stuff that should die
261 * inputs - NOT USED (from event)
262 * output - next time_t when check_pings() should be called again
266 * A PING can be sent to clients as necessary.
268 * Client/Server ping outs are handled.
272 * Addon from adrian. We used to call this after nextping seconds,
273 * however I've changed it to run once a second. This is only for
274 * PING timeouts, not K/etc-line checks (thanks dianora!). Having it
275 * run once a second makes life a lot easier - when a new client connects
276 * and they need a ping in 4 seconds, if nextping was set to 20 seconds
277 * we end up waiting 20 seconds. This is stupid. :-)
278 * I will optimise (hah!) check_pings() once I've finished working on
279 * tidying up other network IO evilnesses.
284 check_pings(void *notused
)
286 check_pings_list(&lclient_list
);
287 check_pings_list(&serv_list
);
288 check_unknowns_list(&unknown_list
);
294 * inputs - pointer to list to check
299 check_pings_list(rb_dlink_list
* list
)
301 char scratch
[32]; /* way too generous but... */
302 struct Client
*client_p
; /* current local client_p being examined */
303 int ping
= 0; /* ping time value from client */
304 rb_dlink_node
*ptr
, *next_ptr
;
306 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
308 client_p
= ptr
->data
;
310 if(!MyConnect(client_p
) || IsDead(client_p
))
313 ping
= get_client_ping(client_p
);
315 if(ping
< (rb_current_time() - client_p
->localClient
->lasttime
))
318 * If the client/server hasnt talked to us in 2*ping seconds
319 * and it has a ping time, then close its connection.
321 if(((rb_current_time() - client_p
->localClient
->lasttime
) >= (2 * ping
)
322 && (client_p
->flags
& FLAGS_PINGSENT
)))
324 if(IsServer(client_p
))
326 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
327 "No response from %s, closing link",
328 get_server_name(client_p
, HIDE_IP
));
330 "No response from %s, closing link",
331 log_client_name(client_p
, HIDE_IP
));
333 (void) rb_snprintf(scratch
, sizeof(scratch
),
334 "Ping timeout: %d seconds",
335 (int) (rb_current_time() - client_p
->localClient
->lasttime
));
337 exit_client(client_p
, client_p
, &me
, scratch
);
340 else if((client_p
->flags
& FLAGS_PINGSENT
) == 0)
343 * if we havent PINGed the connection and we havent
344 * heard from it in a while, PING it to make sure
347 client_p
->flags
|= FLAGS_PINGSENT
;
348 /* not nice but does the job */
349 client_p
->localClient
->lasttime
= rb_current_time() - ping
;
350 sendto_one(client_p
, "PING :%s", me
.name
);
359 * check_unknowns_list
361 * inputs - pointer to list of unknown clients
363 * side effects - unknown clients get marked for termination after n seconds
366 check_unknowns_list(rb_dlink_list
* list
)
368 rb_dlink_node
*ptr
, *next_ptr
;
369 struct Client
*client_p
;
372 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
374 client_p
= ptr
->data
;
376 if(IsDead(client_p
) || IsClosing(client_p
))
380 * Check UNKNOWN connections - if they have been in this state
381 * for > 30s, close them.
384 timeout
= IsAnyServer(client_p
) ? ConfigFileEntry
.connect_timeout
: 30;
385 if((rb_current_time() - client_p
->localClient
->firsttime
) > timeout
)
387 if(IsAnyServer(client_p
))
389 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
390 "No response from %s, closing link",
391 get_server_name(client_p
, HIDE_IP
));
393 "No response from %s, closing link",
394 log_client_name(client_p
, HIDE_IP
));
396 exit_client(client_p
, client_p
, &me
, "Connection timed out");
402 notify_banned_client(struct Client
*client_p
, struct ConfItem
*aconf
, int ban
)
404 static const char conn_closed
[] = "Connection closed";
405 static const char d_lined
[] = "D-lined";
406 static const char k_lined
[] = "K-lined";
407 static const char g_lined
[] = "G-lined";
408 const char *reason
= NULL
;
409 const char *exit_reason
= conn_closed
;
411 if(ConfigFileEntry
.kline_with_reason
&& !EmptyString(aconf
->passwd
))
413 reason
= aconf
->passwd
;
414 exit_reason
= aconf
->passwd
;
418 switch (aconf
->status
)
432 if(ban
== D_LINED
&& !IsPerson(client_p
))
433 sendto_one(client_p
, "NOTICE DLINE :*** You have been D-lined");
435 sendto_one(client_p
, form_str(ERR_YOUREBANNEDCREEP
),
436 me
.name
, client_p
->name
, reason
);
438 exit_client(client_p
, client_p
, &me
,
439 EmptyString(ConfigFileEntry
.kline_reason
) ? exit_reason
:
440 ConfigFileEntry
.kline_reason
);
447 * side effects - Check all connections for a pending k/d/gline against the
448 * client, exit the client if found.
451 check_banned_lines(void)
453 struct Client
*client_p
; /* current local client_p being examined */
454 struct ConfItem
*aconf
= NULL
;
455 rb_dlink_node
*ptr
, *next_ptr
;
457 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
459 client_p
= ptr
->data
;
464 /* if there is a returned struct ConfItem then kill it */
465 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
, client_p
->localClient
->ip
.ss_family
)))
467 if(aconf
->status
& CONF_EXEMPTDLINE
)
470 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
471 "DLINE active for %s",
472 get_client_name(client_p
, HIDE_IP
));
474 notify_banned_client(client_p
, aconf
, D_LINED
);
475 continue; /* and go examine next fd/client_p */
478 if(!IsPerson(client_p
))
481 if((aconf
= find_kline(client_p
)) != NULL
)
483 if(IsExemptKline(client_p
))
485 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
486 "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
487 get_client_name(client_p
, HIDE_IP
),
488 aconf
->user
, aconf
->host
);
492 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
493 "KLINE active for %s",
494 get_client_name(client_p
, HIDE_IP
));
495 notify_banned_client(client_p
, aconf
, K_LINED
);
498 else if((aconf
= find_gline(client_p
)) != NULL
)
500 if(IsExemptKline(client_p
))
502 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
503 "GLINE over-ruled for %s, client is kline_exempt [%s@%s]",
504 get_client_name(client_p
, HIDE_IP
),
505 aconf
->user
, aconf
->host
);
509 if(IsExemptGline(client_p
))
511 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
512 "GLINE over-ruled for %s, client is gline_exempt [%s@%s]",
513 get_client_name(client_p
, HIDE_IP
),
514 aconf
->user
, aconf
->host
);
518 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
519 "GLINE active for %s",
520 get_client_name(client_p
, HIDE_IP
));
522 notify_banned_client(client_p
, aconf
, G_LINED
);
525 else if((aconf
= find_xline(client_p
->info
, 1)) != NULL
)
527 if(IsExemptKline(client_p
))
529 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
530 "XLINE over-ruled for %s, client is kline_exempt [%s]",
531 get_client_name(client_p
, HIDE_IP
),
536 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "XLINE active for %s",
537 get_client_name(client_p
, HIDE_IP
));
539 (void) exit_client(client_p
, client_p
, &me
, "Bad user info");
544 /* also check the unknowns list for new dlines */
545 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, unknown_list
.head
)
547 client_p
= ptr
->data
;
549 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
,client_p
->localClient
->ip
.ss_family
)))
551 if(aconf
->status
& CONF_EXEMPTDLINE
)
554 notify_banned_client(client_p
, aconf
, D_LINED
);
560 /* check_klines_event()
564 * side effects - check_klines() is called, kline_queued unset
567 check_klines_event(void *unused
)
577 * side effects - all clients will be checked for klines
582 struct Client
*client_p
;
583 struct ConfItem
*aconf
;
585 rb_dlink_node
*next_ptr
;
587 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
589 client_p
= ptr
->data
;
591 if(IsMe(client_p
) || !IsPerson(client_p
))
594 if((aconf
= find_kline(client_p
)) != NULL
)
596 if(IsExemptKline(client_p
))
598 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
599 "KLINE over-ruled for %s, client is kline_exempt",
600 get_client_name(client_p
, HIDE_IP
));
604 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
605 "KLINE active for %s",
606 get_client_name(client_p
, HIDE_IP
));
608 notify_banned_client(client_p
, aconf
, K_LINED
);
618 * side effects - all clients will be checked for glines
623 struct Client
*client_p
;
624 struct ConfItem
*aconf
;
626 rb_dlink_node
*next_ptr
;
628 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
630 client_p
= ptr
->data
;
632 if(IsMe(client_p
) || !IsPerson(client_p
))
635 if((aconf
= find_gline(client_p
)) != NULL
)
637 if(IsExemptKline(client_p
))
639 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
640 "GLINE over-ruled for %s, client is kline_exempt",
641 get_client_name(client_p
, HIDE_IP
));
645 if(IsExemptGline(client_p
))
647 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
648 "GLINE over-ruled for %s, client is gline_exempt",
649 get_client_name(client_p
, HIDE_IP
));
653 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
654 "GLINE active for %s",
655 get_client_name(client_p
, HIDE_IP
));
657 notify_banned_client(client_p
, aconf
, K_LINED
);
667 * side effects - all clients will be checked for dlines
672 struct Client
*client_p
;
673 struct ConfItem
*aconf
;
675 rb_dlink_node
*next_ptr
;
677 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
679 client_p
= ptr
->data
;
684 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
,client_p
->localClient
->ip
.ss_family
)) != NULL
)
686 if(aconf
->status
& CONF_EXEMPTDLINE
)
689 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
690 "DLINE active for %s",
691 get_client_name(client_p
, HIDE_IP
));
693 notify_banned_client(client_p
, aconf
, D_LINED
);
698 /* dlines need to be checked against unknowns too */
699 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, unknown_list
.head
)
701 client_p
= ptr
->data
;
703 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
,client_p
->localClient
->ip
.ss_family
)) != NULL
)
705 if(aconf
->status
& CONF_EXEMPTDLINE
)
708 notify_banned_client(client_p
, aconf
, D_LINED
);
717 * side effects - all clients will be checked for xlines
722 struct Client
*client_p
;
723 struct ConfItem
*aconf
;
725 rb_dlink_node
*next_ptr
;
727 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
729 client_p
= ptr
->data
;
731 if(IsMe(client_p
) || !IsPerson(client_p
))
734 if((aconf
= find_xline(client_p
->info
, 1)) != NULL
)
736 if(IsExemptKline(client_p
))
738 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
739 "XLINE over-ruled for %s, client is kline_exempt",
740 get_client_name(client_p
, HIDE_IP
));
744 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "XLINE active for %s",
745 get_client_name(client_p
, HIDE_IP
));
747 (void) exit_client(client_p
, client_p
, &me
, "Bad user info");
754 * update_client_exit_stats
756 * input - pointer to client
761 update_client_exit_stats(struct Client
*client_p
)
763 if(IsServer(client_p
))
765 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
766 "Server %s split from %s",
767 client_p
->name
, client_p
->servptr
->name
);
768 if(HasSentEob(client_p
))
771 else if(IsClient(client_p
))
776 if(IsInvisible(client_p
))
780 if(splitchecking
&& !splitmode
)
781 check_splitmode(NULL
);
785 * release_client_state
787 * input - pointer to client to release
792 release_client_state(struct Client
*client_p
)
794 if(client_p
->user
!= NULL
)
796 free_user(client_p
->user
, client_p
); /* try this here */
800 if(client_p
->serv
->user
!= NULL
)
801 free_user(client_p
->serv
->user
, client_p
);
802 if(client_p
->serv
->fullcaps
)
803 rb_free(client_p
->serv
->fullcaps
);
804 rb_free(client_p
->serv
);
809 * remove_client_from_list
810 * inputs - point to client to remove
812 * side effects - taken the code from ExitOneClient() for this
813 * and placed it here. - avalon
816 remove_client_from_list(struct Client
*client_p
)
818 s_assert(NULL
!= client_p
);
823 /* A client made with make_client()
824 * is on the unknown_list until removed.
825 * If it =does= happen to exit before its removed from that list
826 * and its =not= on the global_client_list, it will core here.
827 * short circuit that case now -db
829 if(client_p
->node
.prev
== NULL
&& client_p
->node
.next
== NULL
)
832 rb_dlinkDelete(&client_p
->node
, &global_client_list
);
834 update_client_exit_stats(client_p
);
839 * find_person - find person by (nick)name.
840 * inputs - pointer to name
841 * output - return client pointer
845 find_person(const char *name
)
847 struct Client
*c2ptr
;
849 c2ptr
= find_client(name
);
851 if(c2ptr
&& IsPerson(c2ptr
))
857 find_named_person(const char *name
)
859 struct Client
*c2ptr
;
861 c2ptr
= find_named_client(name
);
863 if(c2ptr
&& IsPerson(c2ptr
))
870 * find_chasing - find the client structure for a nick name (user)
871 * using history mechanism if necessary. If the client is not found,
872 * an error message (NO SUCH NICK) is generated. If the client was found
873 * through the history, chasing will be 1 and otherwise 0.
876 find_chasing(struct Client
*source_p
, const char *user
, int *chasing
)
880 if(MyClient(source_p
))
881 who
= find_named_person(user
);
883 who
= find_person(user
);
888 if(who
|| IsDigit(*user
))
891 if(!(who
= get_history(user
, (long) KILLCHASETIMELIMIT
)))
893 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
,
894 form_str(ERR_NOSUCHNICK
), user
);
903 * get_client_name - Return the name of the client
904 * for various tracking and
905 * admin purposes. The main purpose of this function is to
906 * return the "socket host" name of the client, if that
907 * differs from the advertised name (other than case).
908 * But, this can be used to any client structure.
911 * Watch out the allocation of "nbuf", if either source_p->name
912 * or source_p->sockhost gets changed into pointers instead of
913 * directly allocated within the structure...
916 * Function return either a pointer to the structure (source_p) or
917 * to internal buffer (nbuf). *NEVER* use the returned pointer
918 * to modify what it points!!!
922 get_client_name(struct Client
*client
, int showip
)
924 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
926 s_assert(NULL
!= client
);
930 if(MyConnect(client
))
932 if(!irccmp(client
->name
, client
->host
))
935 if(ConfigFileEntry
.hide_spoof_ips
&&
936 showip
== SHOW_IP
&& IsIPSpoof(client
))
938 #ifdef HIDE_SERVERS_IPS
939 if(IsAnyServer(client
))
943 /* And finally, let's get the host information, ip or name */
947 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
948 client
->name
, client
->username
,
952 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@255.255.255.255]",
953 client
->name
, client
->username
);
956 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
957 client
->name
, client
->username
, client
->host
);
962 /* As pointed out by Adel Mezibra
963 * Neph|l|m@EFnet. Was missing a return here.
969 get_server_name(struct Client
*target_p
, int showip
)
971 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
976 if(!MyConnect(target_p
) || !irccmp(target_p
->name
, target_p
->host
))
977 return target_p
->name
;
979 #ifdef HIDE_SERVERS_IPS
980 if(EmptyString(target_p
->name
))
982 rb_snprintf(nbuf
, sizeof(nbuf
), "[%s@255.255.255.255]",
987 return target_p
->name
;
993 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
994 target_p
->name
, target_p
->username
,
999 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@255.255.255.255]",
1000 target_p
->name
, target_p
->username
);
1003 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
1004 target_p
->name
, target_p
->username
,
1011 /* log_client_name()
1013 * This version is the same as get_client_name, but doesnt contain the
1014 * code that will hide IPs always. This should be used for logfiles.
1017 log_client_name(struct Client
*target_p
, int showip
)
1019 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
1021 if(target_p
== NULL
)
1024 if(MyConnect(target_p
))
1026 if(irccmp(target_p
->name
, target_p
->host
) == 0)
1027 return target_p
->name
;
1032 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]", target_p
->name
,
1033 target_p
->username
, target_p
->sockhost
);
1037 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@255.255.255.255]",
1038 target_p
->name
, target_p
->username
);
1041 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]", target_p
->name
,
1042 target_p
->username
, target_p
->host
);
1048 return target_p
->name
;
1051 /* is_remote_connect - Returns whether a server was /connect'ed by a remote
1052 * oper (send notices netwide) */
1054 is_remote_connect(struct Client
*client_p
)
1056 struct Client
*oper
;
1058 if (client_p
->serv
== NULL
)
1060 oper
= find_named_person(client_p
->serv
->by
);
1061 return oper
!= NULL
&& IsOper(oper
) && !MyConnect(oper
);
1065 free_exited_clients(void *unused
)
1067 rb_dlink_node
*ptr
, *next
;
1068 struct Client
*target_p
;
1070 RB_DLINK_FOREACH_SAFE(ptr
, next
, dead_list
.head
)
1072 target_p
= ptr
->data
;
1074 #ifdef DEBUG_EXITED_CLIENTS
1076 struct abort_client
*abt
;
1077 rb_dlink_node
*aptr
;
1080 RB_DLINK_FOREACH(aptr
, abort_list
.head
)
1083 if(abt
->client
== target_p
)
1086 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1087 "On abort_list: %s stat: %u flags: %u/%u handler: %c",
1088 target_p
->name
, (unsigned int) target_p
->status
,
1089 target_p
->flags
, target_p
->flags2
, target_p
->handler
);
1090 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1091 "Please report this to the charybdis developers!");
1098 rb_dlinkDestroy(ptr
, &dead_list
);
1104 if(ptr
->data
== NULL
)
1106 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1107 "Warning: null client on dead_list!");
1108 rb_dlinkDestroy(ptr
, &dead_list
);
1111 release_client_state(target_p
);
1112 free_client(target_p
);
1113 rb_dlinkDestroy(ptr
, &dead_list
);
1116 #ifdef DEBUG_EXITED_CLIENTS
1117 RB_DLINK_FOREACH_SAFE(ptr
, next
, dead_remote_list
.head
)
1119 target_p
= ptr
->data
;
1121 if(ptr
->data
== NULL
)
1123 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1124 "Warning: null client on dead_list!");
1125 rb_dlinkDestroy(ptr
, &dead_list
);
1128 release_client_state(target_p
);
1129 free_client(target_p
);
1130 rb_dlinkDestroy(ptr
, &dead_remote_list
);
1137 ** Recursively send QUITs and SQUITs for source_p and all its dependent clients
1138 ** and servers to those servers that need them. A server needs the client
1139 ** QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it
1140 ** isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once
1141 ** a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending
1142 ** on that one -orabidoo
1145 recurse_send_quits(struct Client
*client_p
, struct Client
*source_p
,
1146 struct Client
*to
, const char *comment1
,
1147 const char *comment
)
1149 struct Client
*target_p
;
1150 rb_dlink_node
*ptr
, *ptr_next
;
1151 /* If this server can handle quit storm (QS) removal
1152 * of dependents, just send the SQUIT
1155 if(IsCapable(to
, CAP_QS
))
1157 sendto_one(to
, "SQUIT %s :%s",
1158 get_id(source_p
, to
), comment
);
1162 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1164 target_p
= ptr
->data
;
1165 sendto_one(to
, ":%s QUIT :%s", target_p
->name
, comment1
);
1167 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->servers
.head
)
1169 target_p
= ptr
->data
;
1170 recurse_send_quits(client_p
, target_p
, to
, comment1
, comment
);
1172 sendto_one(to
, "SQUIT %s :%s", source_p
->name
, comment
);
1177 ** Remove all clients that depend on source_p; assumes all (S)QUITs have
1178 ** already been sent. we make sure to exit a server's dependent clients
1179 ** and servers before the server itself; exit_one_client takes care of
1180 ** actually removing things off llists. tweaked from +CSr31 -orabidoo
1183 * added sanity test code.... source_p->serv might be NULL...
1186 recurse_remove_clients(struct Client
*source_p
, const char *comment
)
1188 struct Client
*target_p
;
1189 rb_dlink_node
*ptr
, *ptr_next
;
1194 if(source_p
->serv
== NULL
) /* oooops. uh this is actually a major bug */
1197 /* this is very ugly, but it saves cpu :P */
1198 if(ConfigFileEntry
.nick_delay
> 0)
1200 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1202 target_p
= ptr
->data
;
1203 target_p
->flags
|= FLAGS_KILLED
;
1204 add_nd_entry(target_p
->name
);
1206 if(!IsDead(target_p
) && !IsClosing(target_p
))
1207 exit_remote_client(NULL
, target_p
, &me
, comment
);
1212 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1214 target_p
= ptr
->data
;
1215 target_p
->flags
|= FLAGS_KILLED
;
1217 if(!IsDead(target_p
) && !IsClosing(target_p
))
1218 exit_remote_client(NULL
, target_p
, &me
, comment
);
1222 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->servers
.head
)
1224 target_p
= ptr
->data
;
1225 recurse_remove_clients(target_p
, comment
);
1226 qs_server(NULL
, target_p
, &me
, comment
);
1231 ** Remove *everything* that depends on source_p, from all lists, and sending
1232 ** all necessary QUITs and SQUITs. source_p itself is still on the lists,
1233 ** and its SQUITs have been sent except for the upstream one -orabidoo
1236 remove_dependents(struct Client
*client_p
,
1237 struct Client
*source_p
,
1238 struct Client
*from
, const char *comment
, const char *comment1
)
1241 rb_dlink_node
*ptr
, *next
;
1243 RB_DLINK_FOREACH_SAFE(ptr
, next
, serv_list
.head
)
1247 if(IsMe(to
) || to
== source_p
->from
||
1248 (to
== client_p
&& IsCapable(to
, CAP_QS
)))
1251 recurse_send_quits(client_p
, source_p
, to
, comment1
, comment
);
1254 recurse_remove_clients(source_p
, comment1
);
1258 exit_aborted_clients(void *unused
)
1260 struct abort_client
*abt
;
1261 rb_dlink_node
*ptr
, *next
;
1262 RB_DLINK_FOREACH_SAFE(ptr
, next
, abort_list
.head
)
1266 #ifdef DEBUG_EXITED_CLIENTS
1268 if(rb_dlinkFind(abt
->client
, &dead_list
))
1271 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1272 "On dead_list: %s stat: %u flags: %u/%u handler: %c",
1273 abt
->client
->name
, (unsigned int) abt
->client
->status
,
1274 abt
->client
->flags
, abt
->client
->flags2
, abt
->client
->handler
);
1275 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1276 "Please report this to the charybdis developers!");
1282 s_assert(*((unsigned long*)abt
->client
) != 0xdeadbeef); /* This is lame but its a debug thing */
1283 rb_dlinkDelete(ptr
, &abort_list
);
1285 if(IsAnyServer(abt
->client
))
1286 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1287 "Closing link to %s: %s",
1288 get_server_name(abt
->client
, HIDE_IP
), abt
->notice
);
1290 /* its no longer on abort list - we *must* remove
1291 * FLAGS_CLOSING otherwise exit_client() will not run --fl
1293 abt
->client
->flags
&= ~FLAGS_CLOSING
;
1294 exit_client(abt
->client
, abt
->client
, &me
, abt
->notice
);
1301 * dead_link - Adds client to a list of clients that need an exit_client()
1305 dead_link(struct Client
*client_p
)
1307 struct abort_client
*abt
;
1309 s_assert(!IsMe(client_p
));
1310 if(IsDead(client_p
) || IsClosing(client_p
) || IsMe(client_p
))
1313 abt
= (struct abort_client
*) rb_malloc(sizeof(struct abort_client
));
1315 if(client_p
->flags
& FLAGS_SENDQEX
)
1316 strlcpy(abt
->notice
, "Max SendQ exceeded", sizeof(abt
->notice
));
1318 rb_snprintf(abt
->notice
, sizeof(abt
->notice
), "Write error: %s", strerror(errno
));
1320 abt
->client
= client_p
;
1321 SetIOError(client_p
);
1323 SetClosing(client_p
);
1324 rb_dlinkAdd(abt
, &abt
->node
, &abort_list
);
1328 /* This does the remove of the user from channels..local or remote */
1330 exit_generic_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1331 const char *comment
)
1333 rb_dlink_node
*ptr
, *next_ptr
;
1335 if(IsOper(source_p
))
1336 rb_dlinkFindDestroy(source_p
, &oper_list
);
1338 sendto_common_channels_local(source_p
, ":%s!%s@%s QUIT :%s",
1340 source_p
->username
, source_p
->host
, comment
);
1342 remove_user_from_channels(source_p
);
1344 /* Should not be in any channels now */
1345 s_assert(source_p
->user
->channel
.head
== NULL
);
1347 /* Clean up invitefield */
1348 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, source_p
->user
->invited
.head
)
1350 del_invite(ptr
->data
, source_p
);
1353 /* Clean up allow lists */
1354 del_all_accepts(source_p
);
1356 add_history(source_p
, 0);
1357 off_history(source_p
);
1359 monitor_signoff(source_p
);
1361 if(has_id(source_p
))
1362 del_from_id_hash(source_p
->id
, source_p
);
1364 del_from_hostname_hash(source_p
->orighost
, source_p
);
1365 del_from_client_hash(source_p
->name
, source_p
);
1366 remove_client_from_list(source_p
);
1370 * Assumes IsPerson(source_p) && !MyConnect(source_p)
1374 exit_remote_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1375 const char *comment
)
1377 exit_generic_client(client_p
, source_p
, from
, comment
);
1379 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1381 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->users
);
1384 if((source_p
->flags
& FLAGS_KILLED
) == 0)
1386 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
1387 ":%s QUIT :%s", use_id(source_p
), comment
);
1388 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
,
1389 ":%s QUIT :%s", source_p
->name
, comment
);
1393 #ifdef DEBUG_EXITED_CLIENTS
1394 rb_dlinkAddAlloc(source_p
, &dead_remote_list
);
1396 rb_dlinkAddAlloc(source_p
, &dead_list
);
1398 return(CLIENT_EXITED
);
1402 * This assumes IsUnknown(source_p) == TRUE and MyConnect(source_p) == TRUE
1406 exit_unknown_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1407 const char *comment
)
1409 delete_auth_queries(source_p
);
1410 if (source_p
->localClient
->dnsquery
)
1412 delete_resolver_queries(source_p
->localClient
->dnsquery
);
1413 rb_free(source_p
->localClient
->dnsquery
);
1415 del_unknown_ip(source_p
);
1416 rb_dlinkDelete(&source_p
->localClient
->tnode
, &unknown_list
);
1418 if(!IsIOError(source_p
))
1419 sendto_one(source_p
, "ERROR :Closing Link: %s (%s)",
1420 source_p
->user
!= NULL
? source_p
->host
: "127.0.0.1",
1423 close_connection(source_p
);
1425 if(has_id(source_p
))
1426 del_from_id_hash(source_p
->id
, source_p
);
1428 del_from_hostname_hash(source_p
->host
, source_p
);
1429 del_from_client_hash(source_p
->name
, source_p
);
1430 remove_client_from_list(source_p
);
1432 rb_dlinkAddAlloc(source_p
, &dead_list
);
1434 /* Note that we don't need to add unknowns to the dead_list */
1435 return(CLIENT_EXITED
);
1439 exit_remote_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1440 const char *comment
)
1442 static char comment1
[(HOSTLEN
*2)+2];
1443 static char newcomment
[BUFSIZE
];
1444 struct Client
*target_p
;
1446 if(ConfigServerHide
.flatten_links
)
1447 strcpy(comment1
, "*.net *.split");
1450 strcpy(comment1
, source_p
->servptr
->name
);
1451 strcat(comment1
, " ");
1452 strcat(comment1
, source_p
->name
);
1455 rb_snprintf(newcomment
, sizeof(newcomment
), "by %s: %s",
1456 from
->name
, comment
);
1458 if(source_p
->serv
!= NULL
)
1459 remove_dependents(client_p
, source_p
, from
, IsPerson(from
) ? newcomment
: comment
, comment1
);
1461 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1462 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1466 rb_dlinkFindDestroy(source_p
, &global_serv_list
);
1467 target_p
= source_p
->from
;
1469 if(target_p
!= NULL
&& IsServer(target_p
) && target_p
!= client_p
&&
1470 !IsMe(target_p
) && (source_p
->flags
& FLAGS_KILLED
) == 0)
1472 sendto_one(target_p
, ":%s SQUIT %s :%s",
1473 get_id(from
, target_p
), get_id(source_p
, target_p
),
1477 if(has_id(source_p
))
1478 del_from_id_hash(source_p
->id
, source_p
);
1480 del_from_client_hash(source_p
->name
, source_p
);
1481 remove_client_from_list(source_p
);
1482 scache_split(source_p
->serv
->nameinfo
);
1485 #ifdef DEBUG_EXITED_CLIENTS
1486 rb_dlinkAddAlloc(source_p
, &dead_remote_list
);
1488 rb_dlinkAddAlloc(source_p
, &dead_list
);
1494 qs_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1495 const char *comment
)
1497 struct Client
*target_p
;
1499 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1500 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1504 rb_dlinkFindDestroy(source_p
, &global_serv_list
);
1505 target_p
= source_p
->from
;
1507 if(has_id(source_p
))
1508 del_from_id_hash(source_p
->id
, source_p
);
1510 del_from_client_hash(source_p
->name
, source_p
);
1511 remove_client_from_list(source_p
);
1512 scache_split(source_p
->serv
->nameinfo
);
1515 rb_dlinkAddAlloc(source_p
, &dead_list
);
1520 exit_local_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1521 const char *comment
)
1523 static char comment1
[(HOSTLEN
*2)+2];
1524 static char newcomment
[BUFSIZE
];
1525 unsigned int sendk
, recvk
;
1527 rb_dlinkDelete(&source_p
->localClient
->tnode
, &serv_list
);
1528 rb_dlinkFindDestroy(source_p
, &global_serv_list
);
1530 unset_chcap_usage_counts(source_p
);
1531 sendk
= source_p
->localClient
->sendK
;
1532 recvk
= source_p
->localClient
->receiveK
;
1534 /* Always show source here, so the server notices show
1535 * which side initiated the split -- jilles
1537 rb_snprintf(newcomment
, sizeof(newcomment
), "by %s: %s",
1538 from
== source_p
? me
.name
: from
->name
, comment
);
1539 if (!IsIOError(source_p
))
1540 sendto_one(source_p
, "SQUIT %s :%s", use_id(source_p
),
1542 if(client_p
!= NULL
&& source_p
!= client_p
&& !IsIOError(source_p
))
1544 sendto_one(source_p
, "ERROR :Closing Link: 127.0.0.1 %s (%s)",
1545 source_p
->name
, comment
);
1548 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1549 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1554 close_connection(source_p
);
1556 if(ConfigServerHide
.flatten_links
)
1557 strcpy(comment1
, "*.net *.split");
1560 strcpy(comment1
, source_p
->servptr
->name
);
1561 strcat(comment1
, " ");
1562 strcat(comment1
, source_p
->name
);
1565 if(source_p
->serv
!= NULL
)
1566 remove_dependents(client_p
, source_p
, from
, IsPerson(from
) ? newcomment
: comment
, comment1
);
1568 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s was connected"
1569 " for %ld seconds. %d/%d sendK/recvK.",
1570 source_p
->name
, (long) rb_current_time() - source_p
->localClient
->firsttime
, sendk
, recvk
);
1572 ilog(L_SERVER
, "%s was connected for %ld seconds. %d/%d sendK/recvK.",
1573 source_p
->name
, (long) rb_current_time() - source_p
->localClient
->firsttime
, sendk
, recvk
);
1575 if(has_id(source_p
))
1576 del_from_id_hash(source_p
->id
, source_p
);
1578 del_from_client_hash(source_p
->name
, source_p
);
1579 remove_client_from_list(source_p
);
1580 scache_split(source_p
->serv
->nameinfo
);
1583 rb_dlinkAddAlloc(source_p
, &dead_list
);
1589 * This assumes IsPerson(source_p) == TRUE && MyConnect(source_p) == TRUE
1593 exit_local_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1594 const char *comment
)
1596 unsigned long on_for
;
1598 exit_generic_client(client_p
, source_p
, from
, comment
);
1599 clear_monitor(source_p
);
1601 s_assert(IsPerson(source_p
));
1602 rb_dlinkDelete(&source_p
->localClient
->tnode
, &lclient_list
);
1603 rb_dlinkDelete(&source_p
->lnode
, &me
.serv
->users
);
1605 if(IsOper(source_p
))
1606 rb_dlinkFindDestroy(source_p
, &local_oper_list
);
1608 sendto_realops_snomask(SNO_CCONN
, L_ALL
,
1609 "Client exiting: %s (%s@%s) [%s] [%s]",
1611 source_p
->username
, source_p
->host
, comment
,
1612 show_ip(NULL
, source_p
) ? source_p
->sockhost
: "255.255.255.255");
1614 sendto_realops_snomask(SNO_CCONNEXT
, L_ALL
,
1615 "CLIEXIT %s %s %s %s 0 %s",
1616 source_p
->name
, source_p
->username
, source_p
->host
,
1617 show_ip(NULL
, source_p
) ? source_p
->sockhost
: "255.255.255.255",
1620 on_for
= rb_current_time() - source_p
->localClient
->firsttime
;
1622 ilog(L_USER
, "%s (%3lu:%02lu:%02lu): %s!%s@%s %d/%d",
1623 myctime(rb_current_time()), on_for
/ 3600,
1624 (on_for
% 3600) / 60, on_for
% 60,
1625 source_p
->name
, source_p
->username
, source_p
->host
,
1626 source_p
->localClient
->sendK
, source_p
->localClient
->receiveK
);
1628 sendto_one(source_p
, "ERROR :Closing Link: %s (%s)", source_p
->host
, comment
);
1629 close_connection(source_p
);
1631 if((source_p
->flags
& FLAGS_KILLED
) == 0)
1633 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
1634 ":%s QUIT :%s", use_id(source_p
), comment
);
1635 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
,
1636 ":%s QUIT :%s", source_p
->name
, comment
);
1640 rb_dlinkAddAlloc(source_p
, &dead_list
);
1641 return(CLIENT_EXITED
);
1646 ** exit_client - This is old "m_bye". Name changed, because this is not a
1647 ** protocol function, but a general server utility function.
1649 ** This function exits a client of *any* type (user, server, etc)
1650 ** from this server. Also, this generates all necessary prototol
1651 ** messages that this exit may cause.
1653 ** 1) If the client is a local client, then this implicitly
1654 ** exits all other clients depending on this connection (e.g.
1655 ** remote clients having 'from'-field that points to this.
1657 ** 2) If the client is a remote client, then only this is exited.
1659 ** For convenience, this function returns a suitable value for
1660 ** m_function return value:
1662 ** CLIENT_EXITED if (client_p == source_p)
1663 ** 0 if (client_p != source_p)
1666 exit_client(struct Client
*client_p
, /* The local client originating the
1667 * exit or NULL, if this exit is
1668 * generated by this server for
1670 * This will not get any of the
1671 * generated messages. */
1672 struct Client
*source_p
, /* Client exiting */
1673 struct Client
*from
, /* Client firing off this Exit,
1675 const char *comment
/* Reason for the exit */
1678 hook_data_client_exit hdata
;
1679 if(IsClosing(source_p
))
1682 /* note, this HAS to be here, when we exit a client we attempt to
1683 * send them data, if this generates a write error we must *not* add
1684 * them to the abort list --fl
1686 SetClosing(source_p
);
1688 hdata
.local_link
= client_p
;
1689 hdata
.target
= source_p
;
1691 hdata
.comment
= comment
;
1692 call_hook(h_client_exit
, &hdata
);
1694 if(MyConnect(source_p
))
1696 /* Local clients of various types */
1697 if(IsPerson(source_p
))
1698 return exit_local_client(client_p
, source_p
, from
, comment
);
1699 else if(IsServer(source_p
))
1700 return exit_local_server(client_p
, source_p
, from
, comment
);
1701 /* IsUnknown || IsConnecting || IsHandShake */
1702 else if(!IsReject(source_p
))
1703 return exit_unknown_client(client_p
, source_p
, from
, comment
);
1708 if(IsPerson(source_p
))
1709 return exit_remote_client(client_p
, source_p
, from
, comment
);
1710 else if(IsServer(source_p
))
1711 return exit_remote_server(client_p
, source_p
, from
, comment
);
1718 * Count up local client memory
1721 /* XXX one common Client list now */
1723 count_local_client_memory(size_t * count
, size_t * local_client_memory_used
)
1726 rb_bh_usage(lclient_heap
, count
, NULL
, &lusage
, NULL
);
1727 *local_client_memory_used
= lusage
+ (*count
* (sizeof(void *) + sizeof(struct Client
)));
1731 * Count up remote client memory
1734 count_remote_client_memory(size_t * count
, size_t * remote_client_memory_used
)
1736 size_t lcount
, rcount
;
1737 rb_bh_usage(lclient_heap
, &lcount
, NULL
, NULL
, NULL
);
1738 rb_bh_usage(client_heap
, &rcount
, NULL
, NULL
, NULL
);
1739 *count
= rcount
- lcount
;
1740 *remote_client_memory_used
= *count
* (sizeof(void *) + sizeof(struct Client
));
1745 * accept processing, this adds a form of "caller ID" to ircd
1747 * If a client puts themselves into "caller ID only" mode,
1748 * only clients that match a client pointer they have put on
1749 * the accept list will be allowed to message them.
1751 * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ]
1753 * [target.allow_list] -> [ source1 ] -> [source2 ]
1755 * i.e. a target will have a link list of source pointers it will allow
1756 * each source client then has a back pointer pointing back
1757 * to the client that has it on its accept list.
1758 * This allows for exit_one_client to remove these now bogus entries
1759 * from any client having an accept on them.
1764 * inputs - pointer to exiting client
1766 * side effects - Walk through given clients allow_list and on_allow_list
1767 * remove all references to this client
1770 del_all_accepts(struct Client
*client_p
)
1773 rb_dlink_node
*next_ptr
;
1774 struct Client
*target_p
;
1776 if(MyClient(client_p
) && client_p
->localClient
->allow_list
.head
)
1778 /* clear this clients accept list, and remove them from
1779 * everyones on_accept_list
1781 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->localClient
->allow_list
.head
)
1783 target_p
= ptr
->data
;
1784 rb_dlinkFindDestroy(client_p
, &target_p
->on_allow_list
);
1785 rb_dlinkDestroy(ptr
, &client_p
->localClient
->allow_list
);
1789 /* remove this client from everyones accept list */
1790 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->on_allow_list
.head
)
1792 target_p
= ptr
->data
;
1793 rb_dlinkFindDestroy(client_p
, &target_p
->localClient
->allow_list
);
1794 rb_dlinkDestroy(ptr
, &client_p
->on_allow_list
);
1799 * show_ip() - asks if the true IP shoudl be shown when source is
1800 * askin for info about target
1802 * Inputs - source_p who is asking
1803 * - target_p who do we want the info on
1804 * Output - returns 1 if clear IP can be showed, otherwise 0
1805 * Side Effects - none
1809 show_ip(struct Client
*source_p
, struct Client
*target_p
)
1811 if(IsAnyServer(target_p
))
1813 #ifndef HIDE_SERVERS_IPS
1814 if(source_p
== NULL
|| IsOper(source_p
))
1819 else if(IsIPSpoof(target_p
))
1821 /* source == NULL indicates message is being sent
1824 if(!ConfigFileEntry
.hide_spoof_ips
&&
1825 (source_p
== NULL
|| MyOper(source_p
)))
1829 else if(IsDynSpoof(target_p
) && (source_p
!= NULL
&& !IsOper(source_p
)))
1836 show_ip_conf(struct ConfItem
*aconf
, struct Client
*source_p
)
1838 if(IsConfDoSpoofIp(aconf
))
1840 if(!ConfigFileEntry
.hide_spoof_ips
&& MyOper(source_p
))
1855 * side effects - Creates a block heap for struct Users
1858 static rb_bh
*user_heap
;
1862 user_heap
= rb_bh_create(sizeof(struct User
), USER_HEAP_SIZE
, "user_heap");
1870 * inputs - pointer to client struct
1871 * output - pointer to struct User
1872 * side effects - add's an User information block to a client
1873 * if it was not previously allocated.
1876 make_user(struct Client
*client_p
)
1880 user
= client_p
->user
;
1883 user
= (struct User
*) rb_bh_alloc(user_heap
);
1885 client_p
->user
= user
;
1893 * inputs - pointer to client struct
1894 * output - pointer to server_t
1895 * side effects - add's an Server information block to a client
1896 * if it was not previously allocated.
1899 make_server(struct Client
*client_p
)
1901 server_t
*serv
= client_p
->serv
;
1905 serv
= (server_t
*) rb_malloc(sizeof(server_t
));
1906 client_p
->serv
= serv
;
1908 return client_p
->serv
;
1914 * inputs - pointer to user struct
1915 * - pointer to client struct
1917 * side effects - Decrease user reference count by one and release block,
1918 * if count reaches 0
1921 free_user(struct User
*user
, struct Client
*client_p
)
1923 free_away(client_p
);
1925 if(--user
->refcnt
<= 0)
1928 rb_free((char *) user
->away
);
1932 if(user
->refcnt
< 0 || user
->invited
.head
|| user
->channel
.head
)
1934 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1935 "* %#lx user (%s!%s@%s) %#lx %#lx %#lx %lu %d *",
1936 (unsigned long) client_p
,
1937 client_p
? client_p
->
1941 (unsigned long) user
,
1942 (unsigned long) user
->invited
.head
,
1943 (unsigned long) user
->channel
.head
,
1944 rb_dlink_list_length(&user
->channel
),
1946 s_assert(!user
->refcnt
);
1947 s_assert(!user
->invited
.head
);
1948 s_assert(!user
->channel
.head
);
1951 rb_bh_free(user_heap
, user
);
1956 allocate_away(struct Client
*client_p
)
1958 if(client_p
->user
->away
== NULL
)
1959 client_p
->user
->away
= rb_bh_alloc(away_heap
);
1964 free_away(struct Client
*client_p
)
1966 if(client_p
->user
!= NULL
&& client_p
->user
->away
!= NULL
) {
1967 rb_bh_free(away_heap
, client_p
->user
->away
);
1968 client_p
->user
->away
= NULL
;
1977 for(i
= 0; i
< 3; i
++)
1978 current_uid
[i
] = me
.id
[i
];
1980 for(i
= 3; i
< 9; i
++)
1981 current_uid
[i
] = 'A';
1983 current_uid
[9] = '\0';
1992 for(i
= 8; i
> 3; i
--)
1994 if(current_uid
[i
] == 'Z')
1996 current_uid
[i
] = '0';
1999 else if(current_uid
[i
] != '9')
2005 current_uid
[i
] = 'A';
2008 /* if this next if() triggers, we're fucked. */
2009 if(current_uid
[3] == 'Z')
2011 current_uid
[i
] = 'A';
2022 * Close the physical connection. This function must make
2023 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
2026 close_connection(struct Client
*client_p
)
2028 s_assert(client_p
!= NULL
);
2029 if(client_p
== NULL
)
2032 s_assert(MyConnect(client_p
));
2033 if(!MyConnect(client_p
))
2036 if(IsServer(client_p
))
2038 struct server_conf
*server_p
;
2040 ServerStats
.is_sv
++;
2041 ServerStats
.is_sbs
+= client_p
->localClient
->sendB
;
2042 ServerStats
.is_sbr
+= client_p
->localClient
->receiveB
;
2043 ServerStats
.is_sti
+= rb_current_time() - client_p
->localClient
->firsttime
;
2046 * If the connection has been up for a long amount of time, schedule
2047 * a 'quick' reconnect, else reset the next-connect cycle.
2049 if((server_p
= find_server_conf(client_p
->name
)) != NULL
)
2052 * Reschedule a faster reconnect, if this was a automatically
2053 * connected configuration entry. (Note that if we have had
2054 * a rehash in between, the status has been changed to
2055 * CONF_ILLEGAL). But only do this if it was a "good" link.
2057 server_p
->hold
= time(NULL
);
2059 (server_p
->hold
- client_p
->localClient
->lasttime
>
2060 HANGONGOODLINK
) ? HANGONRETRYDELAY
: ConFreq(server_p
->class);
2064 else if(IsClient(client_p
))
2066 ServerStats
.is_cl
++;
2067 ServerStats
.is_cbs
+= client_p
->localClient
->sendB
;
2068 ServerStats
.is_cbr
+= client_p
->localClient
->receiveB
;
2069 ServerStats
.is_cti
+= rb_current_time() - client_p
->localClient
->firsttime
;
2072 ServerStats
.is_ni
++;
2074 if(client_p
->localClient
->F
!= NULL
)
2076 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
2077 if(!IsIOError(client_p
))
2078 send_queued(client_p
);
2080 del_from_cli_fd_hash(client_p
);
2081 rb_close(client_p
->localClient
->F
);
2082 client_p
->localClient
->F
= NULL
;
2085 rb_linebuf_donebuf(&client_p
->localClient
->buf_sendq
);
2086 rb_linebuf_donebuf(&client_p
->localClient
->buf_recvq
);
2087 detach_conf(client_p
);
2089 /* XXX shouldnt really be done here. */
2090 detach_server_conf(client_p
);
2092 client_p
->from
= NULL
; /* ...this should catch them! >:) --msa */
2093 ClearMyConnect(client_p
);
2094 SetIOError(client_p
);
2100 error_exit_client(struct Client
*client_p
, int error
)
2103 * ...hmm, with non-blocking sockets we might get
2104 * here from quite valid reasons, although.. why
2105 * would select report "data available" when there
2106 * wasn't... so, this must be an error anyway... --msa
2107 * actually, EOF occurs when read() returns 0 and
2108 * in due course, select() returns that fd as ready
2109 * for reading even though it ends up being an EOF. -avalon
2112 int current_error
= rb_get_sockerr(client_p
->localClient
->F
);
2114 SetIOError(client_p
);
2116 if(IsServer(client_p
) || IsHandshake(client_p
))
2120 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) && !IsServer(client_p
) ? L_NETWIDE
: L_ALL
,
2121 "Server %s closed the connection",
2122 get_server_name(client_p
, SHOW_IP
));
2124 ilog(L_SERVER
, "Server %s closed the connection",
2125 log_client_name(client_p
, SHOW_IP
));
2129 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) && !IsServer(client_p
) ? L_NETWIDE
: L_ALL
,
2130 "Lost connection to %s: %s",
2131 client_p
->name
, strerror(current_error
));
2132 ilog(L_SERVER
, "Lost connection to %s: %s",
2133 log_client_name(client_p
, SHOW_IP
), strerror(current_error
));
2138 strlcpy(errmsg
, "Remote host closed the connection", sizeof(errmsg
));
2140 rb_snprintf(errmsg
, sizeof(errmsg
), "Read error: %s", strerror(current_error
));
2142 exit_client(client_p
, client_p
, &me
, errmsg
);