2 * ircd-ratbox: A slightly useful ircd.
3 * client.c: Controls clients.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 * $Id: client.c 3514 2007-06-06 16:25:21Z nenolod $
35 #include "irc_string.h"
36 #include "sprintf_irc.h"
44 #include "s_newconf.h"
60 #include "blacklist.h"
64 #define DEBUG_EXITED_CLIENTS
66 static void check_pings_list(dlink_list
* list
);
67 static void check_unknowns_list(dlink_list
* list
);
68 static void free_exited_clients(void *unused
);
69 static void exit_aborted_clients(void *unused
);
71 static int exit_remote_client(struct Client
*, struct Client
*, struct Client
*,const char *);
72 static int exit_remote_server(struct Client
*, struct Client
*, struct Client
*,const char *);
73 static int exit_local_client(struct Client
*, struct Client
*, struct Client
*,const char *);
74 static int exit_unknown_client(struct Client
*, struct Client
*, struct Client
*,const char *);
75 static int exit_local_server(struct Client
*, struct Client
*, struct Client
*,const char *);
76 static int qs_server(struct Client
*, struct Client
*, struct Client
*, const char *comment
);
78 static EVH check_pings
;
80 extern BlockHeap
*client_heap
;
81 extern BlockHeap
*lclient_heap
;
82 extern BlockHeap
*pclient_heap
;
84 extern char current_uid
[IDLEN
];
94 #ifdef DEBUG_EXITED_CLIENTS
95 static dlink_list dead_remote_list
;
101 struct Client
*client
;
102 char notice
[REASONLEN
];
105 static 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
= BlockHeapCreate(sizeof(struct Client
), CLIENT_HEAP_SIZE
);
123 lclient_heap
= BlockHeapCreate(sizeof(struct LocalUser
), LCLIENT_HEAP_SIZE
);
124 pclient_heap
= BlockHeapCreate(sizeof(struct PreClient
), PCLIENT_HEAP_SIZE
);
125 eventAddIsh("check_pings", check_pings
, NULL
, 30);
126 eventAddIsh("free_exited_clients", &free_exited_clients
, NULL
, 4);
127 eventAddIsh("exit_aborted_clients", exit_aborted_clients
, NULL
, 1);
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
= CurrentTime
;
159 client_p
->localClient
->fd
= -1;
160 client_p
->localClient
->ctrlfd
= -1;
162 client_p
->preClient
= (struct PreClient
*) BlockHeapAlloc(pclient_heap
);
164 /* as good a place as any... */
165 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
->fd
>= 0)
220 comm_close(client_p
->localClient
->fd
);
222 if(client_p
->localClient
->passwd
)
224 memset(client_p
->localClient
->passwd
, 0,
225 strlen(client_p
->localClient
->passwd
));
226 MyFree(client_p
->localClient
->passwd
);
229 MyFree(client_p
->localClient
->challenge
);
230 MyFree(client_p
->localClient
->fullcaps
);
231 MyFree(client_p
->localClient
->opername
);
232 MyFree(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(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 dlink_node
*ptr
, *next_ptr
;
297 DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
299 client_p
= ptr
->data
;
302 ** Note: No need to notify opers here. It's
303 ** already done when "FLAGS_DEADSOCKET" is set.
305 if(!MyConnect(client_p
) || IsDead(client_p
))
308 if(IsPerson(client_p
))
310 if(!IsExemptKline(client_p
) &&
311 GlobalSetOptions
.idletime
&&
313 !IsIdlelined(client_p
) &&
314 ((CurrentTime
- client_p
->localClient
->last
) > GlobalSetOptions
.idletime
))
316 struct ConfItem
*aconf
;
319 aconf
->status
= CONF_KILL
;
321 DupString(aconf
->host
, client_p
->host
);
322 DupString(aconf
->passwd
, "idle exceeder");
323 DupString(aconf
->user
, client_p
->username
);
325 aconf
->hold
= CurrentTime
+ 60;
326 add_temp_kline(aconf
);
327 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
328 "Idle time limit exceeded for %s - temp k-lining",
329 get_client_name(client_p
, HIDE_IP
));
331 exit_client(client_p
, client_p
, &me
, aconf
->passwd
);
336 if(!IsRegistered(client_p
))
337 ping
= ConfigFileEntry
.connect_timeout
;
339 ping
= get_client_ping(client_p
);
341 if(ping
< (CurrentTime
- client_p
->localClient
->lasttime
))
344 * If the client/server hasnt talked to us in 2*ping seconds
345 * and it has a ping time, then close its connection.
347 if(((CurrentTime
- client_p
->localClient
->lasttime
) >= (2 * ping
)
348 && (client_p
->flags
& FLAGS_PINGSENT
)))
350 if(IsAnyServer(client_p
))
352 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) && !IsServer(client_p
) ? L_NETWIDE
: L_ALL
,
353 "No response from %s, closing link",
354 get_server_name(client_p
, HIDE_IP
));
356 "No response from %s, closing link",
357 log_client_name(client_p
, HIDE_IP
));
359 (void) ircsnprintf(scratch
, sizeof(scratch
),
360 "Ping timeout: %d seconds",
361 (int) (CurrentTime
- client_p
->localClient
->lasttime
));
363 exit_client(client_p
, client_p
, &me
, scratch
);
366 else if((client_p
->flags
& FLAGS_PINGSENT
) == 0)
369 * if we havent PINGed the connection and we havent
370 * heard from it in a while, PING it to make sure
373 client_p
->flags
|= FLAGS_PINGSENT
;
374 /* not nice but does the job */
375 client_p
->localClient
->lasttime
= CurrentTime
- ping
;
376 sendto_one(client_p
, "PING :%s", me
.name
);
385 * check_unknowns_list
387 * inputs - pointer to list of unknown clients
389 * side effects - unknown clients get marked for termination after n seconds
392 check_unknowns_list(dlink_list
* list
)
394 dlink_node
*ptr
, *next_ptr
;
395 struct Client
*client_p
;
397 DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
399 client_p
= ptr
->data
;
401 if(IsDead(client_p
) || IsClosing(client_p
))
405 * Check UNKNOWN connections - if they have been in this state
406 * for > 30s, close them.
409 if((CurrentTime
- client_p
->localClient
->firsttime
) > 30)
410 exit_client(client_p
, client_p
, &me
, "Connection timed out");
415 notify_banned_client(struct Client
*client_p
, struct ConfItem
*aconf
, int ban
)
417 static const char conn_closed
[] = "Connection closed";
418 static const char d_lined
[] = "D-lined";
419 static const char k_lined
[] = "K-lined";
420 static const char g_lined
[] = "G-lined";
421 const char *reason
= NULL
;
422 const char *exit_reason
= conn_closed
;
424 if(ConfigFileEntry
.kline_with_reason
&& !EmptyString(aconf
->passwd
))
426 reason
= aconf
->passwd
;
427 exit_reason
= aconf
->passwd
;
431 switch (aconf
->status
)
445 if(ban
== D_LINED
&& !IsPerson(client_p
))
446 sendto_one(client_p
, "NOTICE DLINE :*** You have been D-lined");
448 sendto_one(client_p
, form_str(ERR_YOUREBANNEDCREEP
),
449 me
.name
, client_p
->name
, reason
);
451 exit_client(client_p
, client_p
, &me
,
452 EmptyString(ConfigFileEntry
.kline_reason
) ? exit_reason
:
453 ConfigFileEntry
.kline_reason
);
460 * side effects - Check all connections for a pending k/d/gline against the
461 * client, exit the client if found.
464 check_banned_lines(void)
466 struct Client
*client_p
; /* current local client_p being examined */
467 struct ConfItem
*aconf
= NULL
;
468 dlink_node
*ptr
, *next_ptr
;
470 DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
472 client_p
= ptr
->data
;
477 /* if there is a returned struct ConfItem then kill it */
478 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
, client_p
->localClient
->ip
.ss_family
)))
480 if(aconf
->status
& CONF_EXEMPTDLINE
)
483 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
484 "DLINE active for %s",
485 get_client_name(client_p
, HIDE_IP
));
487 notify_banned_client(client_p
, aconf
, D_LINED
);
488 continue; /* and go examine next fd/client_p */
491 if(!IsPerson(client_p
))
494 if((aconf
= find_kline(client_p
)) != NULL
)
496 if(IsExemptKline(client_p
))
498 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
499 "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
500 get_client_name(client_p
, HIDE_IP
),
501 aconf
->user
, aconf
->host
);
505 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
506 "KLINE active for %s",
507 get_client_name(client_p
, HIDE_IP
));
508 notify_banned_client(client_p
, aconf
, K_LINED
);
511 else if((aconf
= find_gline(client_p
)) != NULL
)
513 if(IsExemptKline(client_p
))
515 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
516 "GLINE over-ruled for %s, client is kline_exempt [%s@%s]",
517 get_client_name(client_p
, HIDE_IP
),
518 aconf
->user
, aconf
->host
);
522 if(IsExemptGline(client_p
))
524 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
525 "GLINE over-ruled for %s, client is gline_exempt [%s@%s]",
526 get_client_name(client_p
, HIDE_IP
),
527 aconf
->user
, aconf
->host
);
531 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
532 "GLINE active for %s",
533 get_client_name(client_p
, HIDE_IP
));
535 notify_banned_client(client_p
, aconf
, G_LINED
);
538 else if((aconf
= find_xline(client_p
->info
, 1)) != NULL
)
540 if(IsExemptKline(client_p
))
542 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
543 "XLINE over-ruled for %s, client is kline_exempt [%s]",
544 get_client_name(client_p
, HIDE_IP
),
549 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "XLINE active for %s",
550 get_client_name(client_p
, HIDE_IP
));
552 (void) exit_client(client_p
, client_p
, &me
, "Bad user info");
557 /* also check the unknowns list for new dlines */
558 DLINK_FOREACH_SAFE(ptr
, next_ptr
, unknown_list
.head
)
560 client_p
= ptr
->data
;
562 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
,client_p
->localClient
->ip
.ss_family
)))
564 if(aconf
->status
& CONF_EXEMPTDLINE
)
567 notify_banned_client(client_p
, aconf
, D_LINED
);
573 /* check_klines_event()
577 * side effects - check_klines() is called, kline_queued unset
580 check_klines_event(void *unused
)
590 * side effects - all clients will be checked for klines
595 struct Client
*client_p
;
596 struct ConfItem
*aconf
;
598 dlink_node
*next_ptr
;
600 DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
602 client_p
= ptr
->data
;
604 if(IsMe(client_p
) || !IsPerson(client_p
))
607 if((aconf
= find_kline(client_p
)) != NULL
)
609 if(IsExemptKline(client_p
))
611 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
612 "KLINE over-ruled for %s, client is kline_exempt",
613 get_client_name(client_p
, HIDE_IP
));
617 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
618 "KLINE active for %s",
619 get_client_name(client_p
, HIDE_IP
));
621 notify_banned_client(client_p
, aconf
, K_LINED
);
631 * side effects - all clients will be checked for glines
636 struct Client
*client_p
;
637 struct ConfItem
*aconf
;
639 dlink_node
*next_ptr
;
641 DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
643 client_p
= ptr
->data
;
645 if(IsMe(client_p
) || !IsPerson(client_p
))
648 if((aconf
= find_gline(client_p
)) != NULL
)
650 if(IsExemptKline(client_p
))
652 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
653 "GLINE over-ruled for %s, client is kline_exempt",
654 get_client_name(client_p
, HIDE_IP
));
658 if(IsExemptGline(client_p
))
660 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
661 "GLINE over-ruled for %s, client is gline_exempt",
662 get_client_name(client_p
, HIDE_IP
));
666 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
667 "GLINE active for %s",
668 get_client_name(client_p
, HIDE_IP
));
670 notify_banned_client(client_p
, aconf
, K_LINED
);
680 * side effects - all clients will be checked for dlines
685 struct Client
*client_p
;
686 struct ConfItem
*aconf
;
688 dlink_node
*next_ptr
;
690 DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
692 client_p
= ptr
->data
;
697 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
,client_p
->localClient
->ip
.ss_family
)) != NULL
)
699 if(aconf
->status
& CONF_EXEMPTDLINE
)
702 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
703 "DLINE active for %s",
704 get_client_name(client_p
, HIDE_IP
));
706 notify_banned_client(client_p
, aconf
, D_LINED
);
711 /* dlines need to be checked against unknowns too */
712 DLINK_FOREACH_SAFE(ptr
, next_ptr
, unknown_list
.head
)
714 client_p
= ptr
->data
;
716 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
,client_p
->localClient
->ip
.ss_family
)) != NULL
)
718 if(aconf
->status
& CONF_EXEMPTDLINE
)
721 notify_banned_client(client_p
, aconf
, D_LINED
);
730 * side effects - all clients will be checked for xlines
735 struct Client
*client_p
;
736 struct ConfItem
*aconf
;
738 dlink_node
*next_ptr
;
740 DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
742 client_p
= ptr
->data
;
744 if(IsMe(client_p
) || !IsPerson(client_p
))
747 if((aconf
= find_xline(client_p
->info
, 1)) != NULL
)
749 if(IsExemptKline(client_p
))
751 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
752 "XLINE over-ruled for %s, client is kline_exempt",
753 get_client_name(client_p
, HIDE_IP
));
757 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "XLINE active for %s",
758 get_client_name(client_p
, HIDE_IP
));
760 (void) exit_client(client_p
, client_p
, &me
, "Bad user info");
767 * update_client_exit_stats
769 * input - pointer to client
774 update_client_exit_stats(struct Client
*client_p
)
776 if(IsServer(client_p
))
778 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
779 "Server %s split from %s",
780 client_p
->name
, client_p
->servptr
->name
);
781 if(HasSentEob(client_p
))
784 else if(IsClient(client_p
))
789 if(IsInvisible(client_p
))
793 if(splitchecking
&& !splitmode
)
794 check_splitmode(NULL
);
798 * release_client_state
800 * input - pointer to client to release
805 release_client_state(struct Client
*client_p
)
807 if(client_p
->user
!= NULL
)
809 free_user(client_p
->user
, client_p
); /* try this here */
813 if(client_p
->serv
->user
!= NULL
)
814 free_user(client_p
->serv
->user
, client_p
);
815 if(client_p
->serv
->fullcaps
)
816 MyFree(client_p
->serv
->fullcaps
);
817 MyFree(client_p
->serv
);
822 * remove_client_from_list
823 * inputs - point to client to remove
825 * side effects - taken the code from ExitOneClient() for this
826 * and placed it here. - avalon
829 remove_client_from_list(struct Client
*client_p
)
831 s_assert(NULL
!= client_p
);
836 /* A client made with make_client()
837 * is on the unknown_list until removed.
838 * If it =does= happen to exit before its removed from that list
839 * and its =not= on the global_client_list, it will core here.
840 * short circuit that case now -db
842 if(client_p
->node
.prev
== NULL
&& client_p
->node
.next
== NULL
)
845 dlinkDelete(&client_p
->node
, &global_client_list
);
847 update_client_exit_stats(client_p
);
852 * find_person - find person by (nick)name.
853 * inputs - pointer to name
854 * output - return client pointer
858 find_person(const char *name
)
860 struct Client
*c2ptr
;
862 c2ptr
= find_client(name
);
864 if(c2ptr
&& IsPerson(c2ptr
))
870 find_named_person(const char *name
)
872 struct Client
*c2ptr
;
874 c2ptr
= find_named_client(name
);
876 if(c2ptr
&& IsPerson(c2ptr
))
883 * find_chasing - find the client structure for a nick name (user)
884 * using history mechanism if necessary. If the client is not found,
885 * an error message (NO SUCH NICK) is generated. If the client was found
886 * through the history, chasing will be 1 and otherwise 0.
889 find_chasing(struct Client
*source_p
, const char *user
, int *chasing
)
893 if(MyClient(source_p
))
894 who
= find_named_person(user
);
896 who
= find_person(user
);
901 if(who
|| IsDigit(*user
))
904 if(!(who
= get_history(user
, (long) KILLCHASETIMELIMIT
)))
906 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
,
907 form_str(ERR_NOSUCHNICK
), user
);
916 * get_client_name - Return the name of the client
917 * for various tracking and
918 * admin purposes. The main purpose of this function is to
919 * return the "socket host" name of the client, if that
920 * differs from the advertised name (other than case).
921 * But, this can be used to any client structure.
924 * Watch out the allocation of "nbuf", if either source_p->name
925 * or source_p->sockhost gets changed into pointers instead of
926 * directly allocated within the structure...
929 * Function return either a pointer to the structure (source_p) or
930 * to internal buffer (nbuf). *NEVER* use the returned pointer
931 * to modify what it points!!!
935 get_client_name(struct Client
*client
, int showip
)
937 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
939 s_assert(NULL
!= client
);
943 if(MyConnect(client
))
945 if(!irccmp(client
->name
, client
->host
))
948 if(ConfigFileEntry
.hide_spoof_ips
&&
949 showip
== SHOW_IP
&& IsIPSpoof(client
))
951 #ifdef HIDE_SERVERS_IPS
952 if(IsAnyServer(client
))
956 /* And finally, let's get the host information, ip or name */
960 ircsnprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
961 client
->name
, client
->username
,
965 ircsnprintf(nbuf
, sizeof(nbuf
), "%s[%s@255.255.255.255]",
966 client
->name
, client
->username
);
969 ircsnprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
970 client
->name
, client
->username
, client
->host
);
975 /* As pointed out by Adel Mezibra
976 * Neph|l|m@EFnet. Was missing a return here.
982 get_server_name(struct Client
*target_p
, int showip
)
984 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
989 if(!MyConnect(target_p
) || !irccmp(target_p
->name
, target_p
->host
))
990 return target_p
->name
;
992 #ifdef HIDE_SERVERS_IPS
993 if(EmptyString(target_p
->name
))
995 ircsnprintf(nbuf
, sizeof(nbuf
), "[%s@255.255.255.255]",
1000 return target_p
->name
;
1006 ircsnprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
1007 target_p
->name
, target_p
->username
,
1008 target_p
->sockhost
);
1012 ircsnprintf(nbuf
, sizeof(nbuf
), "%s[%s@255.255.255.255]",
1013 target_p
->name
, target_p
->username
);
1016 ircsnprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
1017 target_p
->name
, target_p
->username
,
1024 /* log_client_name()
1026 * This version is the same as get_client_name, but doesnt contain the
1027 * code that will hide IPs always. This should be used for logfiles.
1030 log_client_name(struct Client
*target_p
, int showip
)
1032 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
1034 if(target_p
== NULL
)
1037 if(MyConnect(target_p
))
1039 if(irccmp(target_p
->name
, target_p
->host
) == 0)
1040 return target_p
->name
;
1045 ircsnprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]", target_p
->name
,
1046 target_p
->username
, target_p
->sockhost
);
1050 ircsnprintf(nbuf
, sizeof(nbuf
), "%s[%s@255.255.255.255]",
1051 target_p
->name
, target_p
->username
);
1054 ircsnprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]", target_p
->name
,
1055 target_p
->username
, target_p
->host
);
1061 return target_p
->name
;
1064 /* is_remote_connect - Returns whether a server was /connect'ed by a remote
1065 * oper (send notices netwide) */
1067 is_remote_connect(struct Client
*client_p
)
1069 struct Client
*oper
;
1071 if (client_p
->serv
== NULL
)
1073 oper
= find_named_person(client_p
->serv
->by
);
1074 return oper
!= NULL
&& IsOper(oper
) && !MyConnect(oper
);
1078 free_exited_clients(void *unused
)
1080 dlink_node
*ptr
, *next
;
1081 struct Client
*target_p
;
1083 DLINK_FOREACH_SAFE(ptr
, next
, dead_list
.head
)
1085 target_p
= ptr
->data
;
1087 #ifdef DEBUG_EXITED_CLIENTS
1089 struct abort_client
*abt
;
1093 DLINK_FOREACH(aptr
, abort_list
.head
)
1096 if(abt
->client
== target_p
)
1099 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1100 "On abort_list: %s stat: %u flags: %u/%u handler: %c",
1101 target_p
->name
, (unsigned int) target_p
->status
,
1102 target_p
->flags
, target_p
->flags2
, target_p
->handler
);
1103 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1104 "Please report this to the charybdis developers!");
1111 dlinkDestroy(ptr
, &dead_list
);
1117 if(ptr
->data
== NULL
)
1119 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1120 "Warning: null client on dead_list!");
1121 dlinkDestroy(ptr
, &dead_list
);
1124 release_client_state(target_p
);
1125 free_client(target_p
);
1126 dlinkDestroy(ptr
, &dead_list
);
1129 #ifdef DEBUG_EXITED_CLIENTS
1130 DLINK_FOREACH_SAFE(ptr
, next
, dead_remote_list
.head
)
1132 target_p
= ptr
->data
;
1134 if(ptr
->data
== NULL
)
1136 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1137 "Warning: null client on dead_list!");
1138 dlinkDestroy(ptr
, &dead_list
);
1141 release_client_state(target_p
);
1142 free_client(target_p
);
1143 dlinkDestroy(ptr
, &dead_remote_list
);
1150 ** Recursively send QUITs and SQUITs for source_p and all its dependent clients
1151 ** and servers to those servers that need them. A server needs the client
1152 ** QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it
1153 ** isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once
1154 ** a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending
1155 ** on that one -orabidoo
1158 recurse_send_quits(struct Client
*client_p
, struct Client
*source_p
,
1159 struct Client
*to
, const char *comment1
,
1160 const char *comment
)
1162 struct Client
*target_p
;
1163 dlink_node
*ptr
, *ptr_next
;
1164 /* If this server can handle quit storm (QS) removal
1165 * of dependents, just send the SQUIT
1168 if(IsCapable(to
, CAP_QS
))
1170 sendto_one(to
, "SQUIT %s :%s",
1171 get_id(source_p
, to
), comment
);
1175 DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1177 target_p
= ptr
->data
;
1178 sendto_one(to
, ":%s QUIT :%s", target_p
->name
, comment1
);
1180 DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->servers
.head
)
1182 target_p
= ptr
->data
;
1183 recurse_send_quits(client_p
, target_p
, to
, comment1
, comment
);
1185 sendto_one(to
, "SQUIT %s :%s", source_p
->name
, comment
);
1190 ** Remove all clients that depend on source_p; assumes all (S)QUITs have
1191 ** already been sent. we make sure to exit a server's dependent clients
1192 ** and servers before the server itself; exit_one_client takes care of
1193 ** actually removing things off llists. tweaked from +CSr31 -orabidoo
1196 * added sanity test code.... source_p->serv might be NULL...
1199 recurse_remove_clients(struct Client
*source_p
, const char *comment
)
1201 struct Client
*target_p
;
1202 dlink_node
*ptr
, *ptr_next
;
1207 if(source_p
->serv
== NULL
) /* oooops. uh this is actually a major bug */
1210 /* this is very ugly, but it saves cpu :P */
1211 if(ConfigFileEntry
.nick_delay
> 0)
1213 DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1215 target_p
= ptr
->data
;
1216 target_p
->flags
|= FLAGS_KILLED
;
1217 add_nd_entry(target_p
->name
);
1219 if(!IsDead(target_p
) && !IsClosing(target_p
))
1220 exit_remote_client(NULL
, target_p
, &me
, comment
);
1225 DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1227 target_p
= ptr
->data
;
1228 target_p
->flags
|= FLAGS_KILLED
;
1230 if(!IsDead(target_p
) && !IsClosing(target_p
))
1231 exit_remote_client(NULL
, target_p
, &me
, comment
);
1235 DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->servers
.head
)
1237 target_p
= ptr
->data
;
1238 recurse_remove_clients(target_p
, comment
);
1239 qs_server(NULL
, target_p
, &me
, comment
);
1244 ** Remove *everything* that depends on source_p, from all lists, and sending
1245 ** all necessary QUITs and SQUITs. source_p itself is still on the lists,
1246 ** and its SQUITs have been sent except for the upstream one -orabidoo
1249 remove_dependents(struct Client
*client_p
,
1250 struct Client
*source_p
,
1251 struct Client
*from
, const char *comment
, const char *comment1
)
1254 dlink_node
*ptr
, *next
;
1256 DLINK_FOREACH_SAFE(ptr
, next
, serv_list
.head
)
1260 if(IsMe(to
) || to
== source_p
->from
||
1261 (to
== client_p
&& IsCapable(to
, CAP_QS
)))
1264 recurse_send_quits(client_p
, source_p
, to
, comment1
, comment
);
1267 recurse_remove_clients(source_p
, comment1
);
1271 exit_aborted_clients(void *unused
)
1273 struct abort_client
*abt
;
1274 dlink_node
*ptr
, *next
;
1275 DLINK_FOREACH_SAFE(ptr
, next
, abort_list
.head
)
1279 #ifdef DEBUG_EXITED_CLIENTS
1281 if(dlinkFind(abt
->client
, &dead_list
))
1284 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1285 "On dead_list: %s stat: %u flags: %u/%u handler: %c",
1286 abt
->client
->name
, (unsigned int) abt
->client
->status
,
1287 abt
->client
->flags
, abt
->client
->flags2
, abt
->client
->handler
);
1288 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1289 "Please report this to the charybdis developers!");
1295 s_assert(*((unsigned long*)abt
->client
) != 0xdeadbeef); /* This is lame but its a debug thing */
1296 dlinkDelete(ptr
, &abort_list
);
1298 if(IsAnyServer(abt
->client
))
1299 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1300 "Closing link to %s: %s",
1301 get_server_name(abt
->client
, HIDE_IP
), abt
->notice
);
1303 /* its no longer on abort list - we *must* remove
1304 * FLAGS_CLOSING otherwise exit_client() will not run --fl
1306 abt
->client
->flags
&= ~FLAGS_CLOSING
;
1307 exit_client(abt
->client
, abt
->client
, &me
, abt
->notice
);
1314 * dead_link - Adds client to a list of clients that need an exit_client()
1318 dead_link(struct Client
*client_p
)
1320 struct abort_client
*abt
;
1322 s_assert(!IsMe(client_p
));
1323 if(IsDead(client_p
) || IsClosing(client_p
) || IsMe(client_p
))
1326 abt
= (struct abort_client
*) MyMalloc(sizeof(struct abort_client
));
1328 if(client_p
->flags
& FLAGS_SENDQEX
)
1329 strlcpy(abt
->notice
, "Max SendQ exceeded", sizeof(abt
->notice
));
1331 ircsnprintf(abt
->notice
, sizeof(abt
->notice
), "Write error: %s", strerror(errno
));
1333 abt
->client
= client_p
;
1334 SetIOError(client_p
);
1336 SetClosing(client_p
);
1337 dlinkAdd(abt
, &abt
->node
, &abort_list
);
1341 /* This does the remove of the user from channels..local or remote */
1343 exit_generic_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1344 const char *comment
)
1346 dlink_node
*ptr
, *next_ptr
;
1348 if(IsOper(source_p
))
1349 dlinkFindDestroy(source_p
, &oper_list
);
1351 sendto_common_channels_local(source_p
, ":%s!%s@%s QUIT :%s",
1353 source_p
->username
, source_p
->host
, comment
);
1355 remove_user_from_channels(source_p
);
1357 /* Should not be in any channels now */
1358 s_assert(source_p
->user
->channel
.head
== NULL
);
1360 /* Clean up invitefield */
1361 DLINK_FOREACH_SAFE(ptr
, next_ptr
, source_p
->user
->invited
.head
)
1363 del_invite(ptr
->data
, source_p
);
1366 /* Clean up allow lists */
1367 del_all_accepts(source_p
);
1369 add_history(source_p
, 0);
1370 off_history(source_p
);
1372 monitor_signoff(source_p
);
1374 if(has_id(source_p
))
1375 del_from_id_hash(source_p
->id
, source_p
);
1377 del_from_hostname_hash(source_p
->orighost
, source_p
);
1378 del_from_client_hash(source_p
->name
, source_p
);
1379 remove_client_from_list(source_p
);
1383 * Assumes IsPerson(source_p) && !MyConnect(source_p)
1387 exit_remote_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1388 const char *comment
)
1390 exit_generic_client(client_p
, source_p
, from
, comment
);
1392 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1394 dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->users
);
1397 if((source_p
->flags
& FLAGS_KILLED
) == 0)
1399 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
1400 ":%s QUIT :%s", use_id(source_p
), comment
);
1401 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
,
1402 ":%s QUIT :%s", source_p
->name
, comment
);
1406 #ifdef DEBUG_EXITED_CLIENTS
1407 dlinkAddAlloc(source_p
, &dead_remote_list
);
1409 dlinkAddAlloc(source_p
, &dead_list
);
1411 return(CLIENT_EXITED
);
1415 * This assumes IsUnknown(source_p) == TRUE and MyConnect(source_p) == TRUE
1419 exit_unknown_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1420 const char *comment
)
1422 delete_auth_queries(source_p
);
1423 client_flush_input(source_p
);
1424 del_unknown_ip(source_p
);
1425 dlinkDelete(&source_p
->localClient
->tnode
, &unknown_list
);
1427 if(!IsIOError(source_p
))
1428 sendto_one(source_p
, "ERROR :Closing Link: %s (%s)",
1429 source_p
->user
!= NULL
? source_p
->host
: "127.0.0.1",
1432 close_connection(source_p
);
1434 if(has_id(source_p
))
1435 del_from_id_hash(source_p
->id
, source_p
);
1437 del_from_hostname_hash(source_p
->host
, source_p
);
1438 del_from_client_hash(source_p
->name
, source_p
);
1439 remove_client_from_list(source_p
);
1441 dlinkAddAlloc(source_p
, &dead_list
);
1443 /* Note that we don't need to add unknowns to the dead_list */
1444 return(CLIENT_EXITED
);
1448 exit_remote_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1449 const char *comment
)
1451 static char comment1
[(HOSTLEN
*2)+2];
1452 static char newcomment
[BUFSIZE
];
1453 struct Client
*target_p
;
1455 if(ConfigServerHide
.flatten_links
)
1456 strcpy(comment1
, "*.net *.split");
1459 strcpy(comment1
, source_p
->servptr
->name
);
1460 strcat(comment1
, " ");
1461 strcat(comment1
, source_p
->name
);
1464 ircsnprintf(newcomment
, sizeof(newcomment
), "by %s: %s",
1465 from
->name
, comment
);
1467 if(source_p
->serv
!= NULL
)
1468 remove_dependents(client_p
, source_p
, from
, IsPerson(from
) ? newcomment
: comment
, comment1
);
1470 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1471 dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1475 dlinkFindDestroy(source_p
, &global_serv_list
);
1476 target_p
= source_p
->from
;
1478 if(target_p
!= NULL
&& IsServer(target_p
) && target_p
!= client_p
&&
1479 !IsMe(target_p
) && (source_p
->flags
& FLAGS_KILLED
) == 0)
1481 sendto_one(target_p
, ":%s SQUIT %s :%s",
1482 get_id(from
, target_p
), get_id(source_p
, target_p
),
1486 if(has_id(source_p
))
1487 del_from_id_hash(source_p
->id
, source_p
);
1489 del_from_client_hash(source_p
->name
, source_p
);
1490 remove_client_from_list(source_p
);
1491 scache_split(source_p
->serv
->nameinfo
);
1494 #ifdef DEBUG_EXITED_CLIENTS
1495 dlinkAddAlloc(source_p
, &dead_remote_list
);
1497 dlinkAddAlloc(source_p
, &dead_list
);
1503 qs_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1504 const char *comment
)
1506 struct Client
*target_p
;
1508 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1509 dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1513 dlinkFindDestroy(source_p
, &global_serv_list
);
1514 target_p
= source_p
->from
;
1516 if(has_id(source_p
))
1517 del_from_id_hash(source_p
->id
, source_p
);
1519 del_from_client_hash(source_p
->name
, source_p
);
1520 remove_client_from_list(source_p
);
1523 dlinkAddAlloc(source_p
, &dead_list
);
1528 exit_local_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1529 const char *comment
)
1531 static char comment1
[(HOSTLEN
*2)+2];
1532 static char newcomment
[BUFSIZE
];
1533 unsigned int sendk
, recvk
;
1535 dlinkDelete(&source_p
->localClient
->tnode
, &serv_list
);
1536 dlinkFindDestroy(source_p
, &global_serv_list
);
1538 unset_chcap_usage_counts(source_p
);
1539 sendk
= source_p
->localClient
->sendK
;
1540 recvk
= source_p
->localClient
->receiveK
;
1542 /* Always show source here, so the server notices show
1543 * which side initiated the split -- jilles
1545 ircsnprintf(newcomment
, sizeof(newcomment
), "by %s: %s",
1546 from
== source_p
? me
.name
: from
->name
, comment
);
1547 if (!IsIOError(source_p
))
1548 sendto_one(source_p
, "SQUIT %s :%s", use_id(source_p
),
1550 if(client_p
!= NULL
&& source_p
!= client_p
&& !IsIOError(source_p
))
1552 sendto_one(source_p
, "ERROR :Closing Link: 127.0.0.1 %s (%s)",
1553 source_p
->name
, comment
);
1556 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1557 dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1562 close_connection(source_p
);
1564 if(ConfigServerHide
.flatten_links
)
1565 strcpy(comment1
, "*.net *.split");
1568 strcpy(comment1
, source_p
->servptr
->name
);
1569 strcat(comment1
, " ");
1570 strcat(comment1
, source_p
->name
);
1573 if(source_p
->serv
!= NULL
)
1574 remove_dependents(client_p
, source_p
, from
, IsPerson(from
) ? newcomment
: comment
, comment1
);
1576 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s was connected"
1577 " for %ld seconds. %d/%d sendK/recvK.",
1578 source_p
->name
, CurrentTime
- source_p
->localClient
->firsttime
, sendk
, recvk
);
1580 ilog(L_SERVER
, "%s was connected for %ld seconds. %d/%d sendK/recvK.",
1581 source_p
->name
, CurrentTime
- source_p
->localClient
->firsttime
, sendk
, recvk
);
1583 if(has_id(source_p
))
1584 del_from_id_hash(source_p
->id
, source_p
);
1586 del_from_client_hash(source_p
->name
, source_p
);
1587 remove_client_from_list(source_p
);
1588 scache_split(source_p
->serv
->nameinfo
);
1591 dlinkAddAlloc(source_p
, &dead_list
);
1597 * This assumes IsPerson(source_p) == TRUE && MyConnect(source_p) == TRUE
1601 exit_local_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1602 const char *comment
)
1604 unsigned long on_for
;
1606 exit_generic_client(client_p
, source_p
, from
, comment
);
1607 clear_monitor(source_p
);
1609 s_assert(IsPerson(source_p
));
1610 client_flush_input(source_p
);
1611 dlinkDelete(&source_p
->localClient
->tnode
, &lclient_list
);
1612 dlinkDelete(&source_p
->lnode
, &me
.serv
->users
);
1614 if(IsOper(source_p
))
1615 dlinkFindDestroy(source_p
, &local_oper_list
);
1617 sendto_realops_snomask(SNO_CCONN
, L_ALL
,
1618 "Client exiting: %s (%s@%s) [%s] [%s]",
1620 source_p
->username
, source_p
->host
, comment
,
1621 show_ip(NULL
, source_p
) ? source_p
->sockhost
: "255.255.255.255");
1623 sendto_realops_snomask(SNO_CCONNEXT
, L_ALL
,
1624 "CLIEXIT %s %s %s %s 0 %s",
1625 source_p
->name
, source_p
->username
, source_p
->host
,
1626 show_ip(NULL
, source_p
) ? source_p
->sockhost
: "255.255.255.255",
1629 on_for
= CurrentTime
- source_p
->localClient
->firsttime
;
1631 ilog(L_USER
, "%s (%3lu:%02lu:%02lu): %s!%s@%s %d/%d",
1632 myctime(CurrentTime
), on_for
/ 3600,
1633 (on_for
% 3600) / 60, on_for
% 60,
1634 source_p
->name
, source_p
->username
, source_p
->host
,
1635 source_p
->localClient
->sendK
, source_p
->localClient
->receiveK
);
1637 sendto_one(source_p
, "ERROR :Closing Link: %s (%s)", source_p
->host
, comment
);
1638 close_connection(source_p
);
1640 if((source_p
->flags
& FLAGS_KILLED
) == 0)
1642 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
1643 ":%s QUIT :%s", use_id(source_p
), comment
);
1644 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
,
1645 ":%s QUIT :%s", source_p
->name
, comment
);
1649 dlinkAddAlloc(source_p
, &dead_list
);
1650 return(CLIENT_EXITED
);
1655 ** exit_client - This is old "m_bye". Name changed, because this is not a
1656 ** protocol function, but a general server utility function.
1658 ** This function exits a client of *any* type (user, server, etc)
1659 ** from this server. Also, this generates all necessary prototol
1660 ** messages that this exit may cause.
1662 ** 1) If the client is a local client, then this implicitly
1663 ** exits all other clients depending on this connection (e.g.
1664 ** remote clients having 'from'-field that points to this.
1666 ** 2) If the client is a remote client, then only this is exited.
1668 ** For convenience, this function returns a suitable value for
1669 ** m_function return value:
1671 ** CLIENT_EXITED if (client_p == source_p)
1672 ** 0 if (client_p != source_p)
1675 exit_client(struct Client
*client_p
, /* The local client originating the
1676 * exit or NULL, if this exit is
1677 * generated by this server for
1679 * This will not get any of the
1680 * generated messages. */
1681 struct Client
*source_p
, /* Client exiting */
1682 struct Client
*from
, /* Client firing off this Exit,
1684 const char *comment
/* Reason for the exit */
1687 hook_data_client_exit hdata
;
1688 if(IsClosing(source_p
))
1691 /* note, this HAS to be here, when we exit a client we attempt to
1692 * send them data, if this generates a write error we must *not* add
1693 * them to the abort list --fl
1695 SetClosing(source_p
);
1697 hdata
.local_link
= client_p
;
1698 hdata
.target
= source_p
;
1700 hdata
.comment
= comment
;
1701 call_hook(h_client_exit
, &hdata
);
1703 if(MyConnect(source_p
))
1705 /* Local clients of various types */
1706 if(IsPerson(source_p
))
1707 return exit_local_client(client_p
, source_p
, from
, comment
);
1708 else if(IsServer(source_p
))
1709 return exit_local_server(client_p
, source_p
, from
, comment
);
1710 /* IsUnknown || IsConnecting || IsHandShake */
1711 else if(!IsReject(source_p
))
1712 return exit_unknown_client(client_p
, source_p
, from
, comment
);
1717 if(IsPerson(source_p
))
1718 return exit_remote_client(client_p
, source_p
, from
, comment
);
1719 else if(IsServer(source_p
))
1720 return exit_remote_server(client_p
, source_p
, from
, comment
);
1727 * Count up local client memory
1730 /* XXX one common Client list now */
1732 count_local_client_memory(size_t * count
, size_t * local_client_memory_used
)
1735 BlockHeapUsage(lclient_heap
, count
, NULL
, &lusage
);
1736 *local_client_memory_used
= lusage
+ (*count
* (sizeof(MemBlock
) + sizeof(struct Client
)));
1740 * Count up remote client memory
1743 count_remote_client_memory(size_t * count
, size_t * remote_client_memory_used
)
1745 size_t lcount
, rcount
;
1746 BlockHeapUsage(lclient_heap
, &lcount
, NULL
, NULL
);
1747 BlockHeapUsage(client_heap
, &rcount
, NULL
, NULL
);
1748 *count
= rcount
- lcount
;
1749 *remote_client_memory_used
= *count
* (sizeof(MemBlock
) + sizeof(struct Client
));
1754 * accept processing, this adds a form of "caller ID" to ircd
1756 * If a client puts themselves into "caller ID only" mode,
1757 * only clients that match a client pointer they have put on
1758 * the accept list will be allowed to message them.
1760 * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ]
1762 * [target.allow_list] -> [ source1 ] -> [source2 ]
1764 * i.e. a target will have a link list of source pointers it will allow
1765 * each source client then has a back pointer pointing back
1766 * to the client that has it on its accept list.
1767 * This allows for exit_one_client to remove these now bogus entries
1768 * from any client having an accept on them.
1773 * inputs - pointer to exiting client
1775 * side effects - Walk through given clients allow_list and on_allow_list
1776 * remove all references to this client
1779 del_all_accepts(struct Client
*client_p
)
1782 dlink_node
*next_ptr
;
1783 struct Client
*target_p
;
1785 if(MyClient(client_p
) && client_p
->localClient
->allow_list
.head
)
1787 /* clear this clients accept list, and remove them from
1788 * everyones on_accept_list
1790 DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->localClient
->allow_list
.head
)
1792 target_p
= ptr
->data
;
1793 dlinkFindDestroy(client_p
, &target_p
->on_allow_list
);
1794 dlinkDestroy(ptr
, &client_p
->localClient
->allow_list
);
1798 /* remove this client from everyones accept list */
1799 DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->on_allow_list
.head
)
1801 target_p
= ptr
->data
;
1802 dlinkFindDestroy(client_p
, &target_p
->localClient
->allow_list
);
1803 dlinkDestroy(ptr
, &client_p
->on_allow_list
);
1808 * show_ip() - asks if the true IP shoudl be shown when source is
1809 * askin for info about target
1811 * Inputs - source_p who is asking
1812 * - target_p who do we want the info on
1813 * Output - returns 1 if clear IP can be showed, otherwise 0
1814 * Side Effects - none
1818 show_ip(struct Client
*source_p
, struct Client
*target_p
)
1820 if(IsAnyServer(target_p
))
1822 #ifndef HIDE_SERVERS_IPS
1823 if(source_p
== NULL
|| IsOper(source_p
))
1828 else if(IsIPSpoof(target_p
))
1830 /* source == NULL indicates message is being sent
1833 if(!ConfigFileEntry
.hide_spoof_ips
&&
1834 (source_p
== NULL
|| MyOper(source_p
)))
1838 else if(IsDynSpoof(target_p
) && (source_p
!= NULL
&& !IsOper(source_p
)))
1845 show_ip_conf(struct ConfItem
*aconf
, struct Client
*source_p
)
1847 if(IsConfDoSpoofIp(aconf
))
1849 if(!ConfigFileEntry
.hide_spoof_ips
&& MyOper(source_p
))
1864 * side effects - Creates a block heap for struct Users
1867 static BlockHeap
*user_heap
;
1871 user_heap
= BlockHeapCreate(sizeof(struct User
), USER_HEAP_SIZE
);
1879 * inputs - pointer to client struct
1880 * output - pointer to struct User
1881 * side effects - add's an User information block to a client
1882 * if it was not previously allocated.
1885 make_user(struct Client
*client_p
)
1889 user
= client_p
->user
;
1892 user
= (struct User
*) BlockHeapAlloc(user_heap
);
1894 client_p
->user
= user
;
1902 * inputs - pointer to client struct
1903 * output - pointer to server_t
1904 * side effects - add's an Server information block to a client
1905 * if it was not previously allocated.
1908 make_server(struct Client
*client_p
)
1910 server_t
*serv
= client_p
->serv
;
1914 serv
= (server_t
*) MyMalloc(sizeof(server_t
));
1915 client_p
->serv
= serv
;
1917 return client_p
->serv
;
1923 * inputs - pointer to user struct
1924 * - pointer to client struct
1926 * side effects - Decrease user reference count by one and release block,
1927 * if count reaches 0
1930 free_user(struct User
*user
, struct Client
*client_p
)
1932 if(--user
->refcnt
<= 0)
1935 MyFree((char *) user
->away
);
1939 if(user
->refcnt
< 0 || user
->invited
.head
|| user
->channel
.head
)
1941 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1942 "* %#lx user (%s!%s@%s) %#lx %#lx %#lx %lu %d *",
1943 (unsigned long) client_p
,
1944 client_p
? client_p
->
1948 (unsigned long) user
,
1949 (unsigned long) user
->invited
.head
,
1950 (unsigned long) user
->channel
.head
,
1951 dlink_list_length(&user
->channel
),
1953 s_assert(!user
->refcnt
);
1954 s_assert(!user
->invited
.head
);
1955 s_assert(!user
->channel
.head
);
1958 BlockHeapFree(user_heap
, user
);
1967 for(i
= 0; i
< 3; i
++)
1968 current_uid
[i
] = me
.id
[i
];
1970 for(i
= 3; i
< 9; i
++)
1971 current_uid
[i
] = 'A';
1973 current_uid
[9] = '\0';
1982 for(i
= 8; i
> 3; i
--)
1984 if(current_uid
[i
] == 'Z')
1986 current_uid
[i
] = '0';
1989 else if(current_uid
[i
] != '9')
1995 current_uid
[i
] = 'A';
1998 /* if this next if() triggers, we're fucked. */
1999 if(current_uid
[3] == 'Z')
2001 current_uid
[i
] = 'A';
2012 * Close the physical connection. This function must make
2013 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
2016 close_connection(struct Client
*client_p
)
2018 s_assert(client_p
!= NULL
);
2019 if(client_p
== NULL
)
2022 s_assert(MyConnect(client_p
));
2023 if(!MyConnect(client_p
))
2026 if(IsServer(client_p
))
2028 struct server_conf
*server_p
;
2030 ServerStats
->is_sv
++;
2031 ServerStats
->is_sbs
+= client_p
->localClient
->sendB
;
2032 ServerStats
->is_sbr
+= client_p
->localClient
->receiveB
;
2033 ServerStats
->is_sks
+= client_p
->localClient
->sendK
;
2034 ServerStats
->is_skr
+= client_p
->localClient
->receiveK
;
2035 ServerStats
->is_sti
+= CurrentTime
- client_p
->localClient
->firsttime
;
2036 if(ServerStats
->is_sbs
> 2047)
2038 ServerStats
->is_sks
+= (ServerStats
->is_sbs
>> 10);
2039 ServerStats
->is_sbs
&= 0x3ff;
2041 if(ServerStats
->is_sbr
> 2047)
2043 ServerStats
->is_skr
+= (ServerStats
->is_sbr
>> 10);
2044 ServerStats
->is_sbr
&= 0x3ff;
2048 * If the connection has been up for a long amount of time, schedule
2049 * a 'quick' reconnect, else reset the next-connect cycle.
2051 if((server_p
= find_server_conf(client_p
->name
)) != NULL
)
2054 * Reschedule a faster reconnect, if this was a automatically
2055 * connected configuration entry. (Note that if we have had
2056 * a rehash in between, the status has been changed to
2057 * CONF_ILLEGAL). But only do this if it was a "good" link.
2059 server_p
->hold
= time(NULL
);
2061 (server_p
->hold
- client_p
->localClient
->lasttime
>
2062 HANGONGOODLINK
) ? HANGONRETRYDELAY
: ConFreq(server_p
->class);
2066 else if(IsClient(client_p
))
2068 ServerStats
->is_cl
++;
2069 ServerStats
->is_cbs
+= client_p
->localClient
->sendB
;
2070 ServerStats
->is_cbr
+= client_p
->localClient
->receiveB
;
2071 ServerStats
->is_cks
+= client_p
->localClient
->sendK
;
2072 ServerStats
->is_ckr
+= client_p
->localClient
->receiveK
;
2073 ServerStats
->is_cti
+= CurrentTime
- client_p
->localClient
->firsttime
;
2074 if(ServerStats
->is_cbs
> 2047)
2076 ServerStats
->is_cks
+= (ServerStats
->is_cbs
>> 10);
2077 ServerStats
->is_cbs
&= 0x3ff;
2079 if(ServerStats
->is_cbr
> 2047)
2081 ServerStats
->is_ckr
+= (ServerStats
->is_cbr
>> 10);
2082 ServerStats
->is_cbr
&= 0x3ff;
2086 ServerStats
->is_ni
++;
2088 if(-1 < client_p
->localClient
->fd
)
2090 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
2091 if(!IsIOError(client_p
))
2092 send_queued_write(client_p
->localClient
->fd
, client_p
);
2094 comm_close(client_p
->localClient
->fd
);
2095 client_p
->localClient
->fd
= -1;
2098 if(-1 < client_p
->localClient
->ctrlfd
)
2100 comm_close(client_p
->localClient
->ctrlfd
);
2101 client_p
->localClient
->ctrlfd
= -1;
2104 linebuf_donebuf(&client_p
->localClient
->buf_sendq
);
2105 linebuf_donebuf(&client_p
->localClient
->buf_recvq
);
2106 detach_conf(client_p
);
2108 /* XXX shouldnt really be done here. */
2109 detach_server_conf(client_p
);
2111 client_p
->from
= NULL
; /* ...this should catch them! >:) --msa */
2112 ClearMyConnect(client_p
);
2113 SetIOError(client_p
);
2119 error_exit_client(struct Client
*client_p
, int error
)
2122 * ...hmm, with non-blocking sockets we might get
2123 * here from quite valid reasons, although.. why
2124 * would select report "data available" when there
2125 * wasn't... so, this must be an error anyway... --msa
2126 * actually, EOF occurs when read() returns 0 and
2127 * in due course, select() returns that fd as ready
2128 * for reading even though it ends up being an EOF. -avalon
2131 int current_error
= comm_get_sockerr(client_p
->localClient
->fd
);
2133 SetIOError(client_p
);
2135 if(IsServer(client_p
) || IsHandshake(client_p
))
2137 int connected
= CurrentTime
- client_p
->localClient
->firsttime
;
2141 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) && !IsServer(client_p
) ? L_NETWIDE
: L_ALL
,
2142 "Server %s closed the connection",
2143 get_server_name(client_p
, SHOW_IP
));
2145 ilog(L_SERVER
, "Server %s closed the connection",
2146 log_client_name(client_p
, SHOW_IP
));
2150 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) && !IsServer(client_p
) ? L_NETWIDE
: L_ALL
,
2151 "Lost connection to %s: %s",
2152 client_p
->name
, strerror(current_error
));
2153 ilog(L_SERVER
, "Lost connection to %s: %s",
2154 log_client_name(client_p
, SHOW_IP
), strerror(current_error
));
2157 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
2158 "%s had been connected for %d day%s, %2d:%02d:%02d",
2159 client_p
->name
, connected
/ 86400,
2160 (connected
/ 86400 == 1) ? "" : "s",
2161 (connected
% 86400) / 3600,
2162 (connected
% 3600) / 60, connected
% 60);
2166 strlcpy(errmsg
, "Remote host closed the connection", sizeof(errmsg
));
2168 ircsnprintf(errmsg
, sizeof(errmsg
), "Read error: %s", strerror(current_error
));
2170 exit_client(client_p
, client_p
, &me
, errmsg
);