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
37 #include "s_newconf.h"
52 #include "rb_dictionary.h"
57 #define DEBUG_EXITED_CLIENTS
59 static void check_pings_list(rb_dlink_list
* list
);
60 static void check_unknowns_list(rb_dlink_list
* list
);
61 static void free_exited_clients(void *unused
);
62 static void exit_aborted_clients(void *unused
);
64 static int exit_remote_client(struct Client
*, struct Client
*, struct Client
*,const char *);
65 static int exit_remote_server(struct Client
*, struct Client
*, struct Client
*,const char *);
66 static int exit_local_client(struct Client
*, struct Client
*, struct Client
*,const char *);
67 static int exit_unknown_client(struct Client
*, struct Client
*, struct Client
*,const char *);
68 static int exit_local_server(struct Client
*, struct Client
*, struct Client
*,const char *);
69 static int qs_server(struct Client
*, struct Client
*, struct Client
*, const char *comment
);
71 static EVH check_pings
;
73 static rb_bh
*client_heap
= NULL
;
74 static rb_bh
*lclient_heap
= NULL
;
75 static rb_bh
*pclient_heap
= NULL
;
76 static rb_bh
*user_heap
= NULL
;
77 static rb_bh
*away_heap
= NULL
;
78 static char current_uid
[IDLEN
];
79 static uint32_t current_connid
= 0;
81 rb_dictionary
*nd_dict
= NULL
;
83 rb_dlink_list dead_list
;
84 #ifdef DEBUG_EXITED_CLIENTS
85 static rb_dlink_list dead_remote_list
;
91 struct Client
*client
;
92 char notice
[REASONLEN
];
95 static rb_dlink_list abort_list
;
103 * side effects - initialize client free memory
109 * start off the check ping event .. -- adrian
110 * Every 30 seconds is plenty -- db
112 client_heap
= rb_bh_create(sizeof(struct Client
), CLIENT_HEAP_SIZE
, "client_heap");
113 lclient_heap
= rb_bh_create(sizeof(struct LocalUser
), LCLIENT_HEAP_SIZE
, "lclient_heap");
114 pclient_heap
= rb_bh_create(sizeof(struct PreClient
), PCLIENT_HEAP_SIZE
, "pclient_heap");
115 user_heap
= rb_bh_create(sizeof(struct User
), USER_HEAP_SIZE
, "user_heap");
116 away_heap
= rb_bh_create(AWAYLEN
, AWAY_HEAP_SIZE
, "away_heap");
118 rb_event_addish("check_pings", check_pings
, NULL
, 30);
119 rb_event_addish("free_exited_clients", &free_exited_clients
, NULL
, 4);
120 rb_event_addish("exit_aborted_clients", exit_aborted_clients
, NULL
, 1);
121 rb_event_add("flood_recalc", flood_recalc
, NULL
, 1);
123 nd_dict
= rb_dictionary_create("nickdelay", irccmp
);
127 * connid_get - allocate a connid
130 * outputs - a connid token which is used to represent a logical circuit
131 * side effects - current_connid is incremented, possibly multiple times.
132 * the association of the connid to it's client is committed.
135 connid_get(struct Client
*client_p
)
137 s_assert(MyConnect(client_p
));
138 if (!MyConnect(client_p
))
141 /* find a connid that is available */
142 while (find_cli_connid_hash(++current_connid
) != NULL
)
144 /* handle wraparound, current_connid must NEVER be 0 */
145 if (current_connid
== 0)
149 add_to_cli_connid_hash(client_p
, current_connid
);
150 rb_dlinkAddAlloc(RB_UINT_TO_POINTER(current_connid
), &client_p
->localClient
->connids
);
152 return current_connid
;
156 * connid_put - free a connid
158 * inputs - connid to free
160 * side effects - connid bookkeeping structures are freed
163 connid_put(uint32_t id
)
165 struct Client
*client_p
;
171 client_p
= find_cli_connid_hash(id
);
172 if (client_p
== NULL
)
175 del_from_cli_connid_hash(id
);
176 rb_dlinkFindDestroy(RB_UINT_TO_POINTER(id
), &client_p
->localClient
->connids
);
180 * client_release_connids - release any connids still attached to a client
182 * inputs - client to garbage collect
184 * side effects - client's connids are garbage collected
187 client_release_connids(struct Client
*client_p
)
189 rb_dlink_node
*ptr
, *ptr2
;
191 if (client_p
->localClient
->connids
.head
)
192 s_assert(MyConnect(client_p
));
194 if (!MyConnect(client_p
))
197 RB_DLINK_FOREACH_SAFE(ptr
, ptr2
, client_p
->localClient
->connids
.head
)
198 connid_put(RB_POINTER_TO_UINT(ptr
->data
));
202 * make_client - create a new Client struct and set it to initial state.
204 * from == NULL, create local client (a client connected
207 * from, create remote client (behind a socket
208 * associated with the client defined by
209 * 'from'). ('from' is a local client!!).
212 make_client(struct Client
*from
)
214 struct Client
*client_p
= NULL
;
215 struct LocalUser
*localClient
;
217 client_p
= rb_bh_alloc(client_heap
);
221 client_p
->from
= client_p
; /* 'from' of local client is self! */
223 localClient
= rb_bh_alloc(lclient_heap
);
224 SetMyConnect(client_p
);
225 client_p
->localClient
= localClient
;
227 client_p
->localClient
->lasttime
= client_p
->localClient
->firsttime
= rb_current_time();
229 client_p
->localClient
->F
= NULL
;
231 client_p
->preClient
= rb_bh_alloc(pclient_heap
);
233 /* as good a place as any... */
234 rb_dlinkAdd(client_p
, &client_p
->localClient
->tnode
, &unknown_list
);
237 { /* from is not NULL */
238 client_p
->localClient
= NULL
;
239 client_p
->preClient
= NULL
;
240 client_p
->from
= from
; /* 'from' of local client is self! */
243 SetUnknown(client_p
);
244 rb_strlcpy(client_p
->username
, "unknown", sizeof(client_p
->username
));
250 free_pre_client(struct Client
*client_p
)
252 s_assert(NULL
!= client_p
);
254 if(client_p
->preClient
== NULL
)
257 s_assert(client_p
->preClient
->auth
.cid
== 0);
259 rb_free(client_p
->preClient
->auth
.data
);
260 rb_free(client_p
->preClient
->auth
.reason
);
262 rb_bh_free(pclient_heap
, client_p
->preClient
);
263 client_p
->preClient
= NULL
;
267 free_local_client(struct Client
*client_p
)
269 s_assert(NULL
!= client_p
);
270 s_assert(&me
!= client_p
);
272 if(client_p
->localClient
== NULL
)
276 * clean up extra sockets from P-lines which have been discarded.
278 if(client_p
->localClient
->listener
)
280 s_assert(0 < client_p
->localClient
->listener
->ref_count
);
281 if(0 == --client_p
->localClient
->listener
->ref_count
282 && !client_p
->localClient
->listener
->active
)
283 free_listener(client_p
->localClient
->listener
);
284 client_p
->localClient
->listener
= 0;
287 client_release_connids(client_p
);
288 if(client_p
->localClient
->F
!= NULL
)
290 rb_close(client_p
->localClient
->F
);
293 if(client_p
->localClient
->passwd
)
295 memset(client_p
->localClient
->passwd
, 0,
296 strlen(client_p
->localClient
->passwd
));
297 rb_free(client_p
->localClient
->passwd
);
300 rb_free(client_p
->localClient
->auth_user
);
301 rb_free(client_p
->localClient
->challenge
);
302 rb_free(client_p
->localClient
->fullcaps
);
303 rb_free(client_p
->localClient
->mangledhost
);
306 ssld_decrement_clicount(client_p
->localClient
->ssl_ctl
);
308 rb_free(client_p
->localClient
->cipher_string
);
310 if (IsCapable(client_p
, CAP_ZIP
))
311 ssld_decrement_clicount(client_p
->localClient
->z_ctl
);
313 rb_free(client_p
->localClient
->zipstats
);
315 if (client_p
->localClient
->ws_ctl
!= NULL
)
316 wsockd_decrement_clicount(client_p
->localClient
->ws_ctl
);
318 rb_bh_free(lclient_heap
, client_p
->localClient
);
319 client_p
->localClient
= NULL
;
323 free_client(struct Client
*client_p
)
325 s_assert(NULL
!= client_p
);
326 s_assert(&me
!= client_p
);
327 free_local_client(client_p
);
328 free_pre_client(client_p
);
329 rb_free(client_p
->certfp
);
330 rb_bh_free(client_heap
, client_p
);
334 * check_pings - go through the local client list and check activity
335 * kill off stuff that should die
337 * inputs - NOT USED (from event)
338 * output - next time_t when check_pings() should be called again
342 * A PING can be sent to clients as necessary.
344 * Client/Server ping outs are handled.
348 * Addon from adrian. We used to call this after nextping seconds,
349 * however I've changed it to run once a second. This is only for
350 * PING timeouts, not K/etc-line checks (thanks dianora!). Having it
351 * run once a second makes life a lot easier - when a new client connects
352 * and they need a ping in 4 seconds, if nextping was set to 20 seconds
353 * we end up waiting 20 seconds. This is stupid. :-)
354 * I will optimise (hah!) check_pings() once I've finished working on
355 * tidying up other network IO evilnesses.
360 check_pings(void *notused
)
362 check_pings_list(&lclient_list
);
363 check_pings_list(&serv_list
);
364 check_unknowns_list(&unknown_list
);
370 * inputs - pointer to list to check
375 check_pings_list(rb_dlink_list
* list
)
377 char scratch
[32]; /* way too generous but... */
378 struct Client
*client_p
; /* current local client_p being examined */
379 int ping
= 0; /* ping time value from client */
380 rb_dlink_node
*ptr
, *next_ptr
;
382 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
384 client_p
= ptr
->data
;
386 if(!MyConnect(client_p
) || IsDead(client_p
))
389 ping
= get_client_ping(client_p
);
391 if(ping
< (rb_current_time() - client_p
->localClient
->lasttime
))
394 * If the client/server hasnt talked to us in 2*ping seconds
395 * and it has a ping time, then close its connection.
397 if(((rb_current_time() - client_p
->localClient
->lasttime
) >= (2 * ping
)
398 && (client_p
->flags
& FLAGS_PINGSENT
)))
400 if(IsServer(client_p
))
402 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
403 "No response from %s, closing link",
406 "No response from %s, closing link",
407 log_client_name(client_p
, HIDE_IP
));
409 (void) snprintf(scratch
, sizeof(scratch
),
410 "Ping timeout: %d seconds",
411 (int) (rb_current_time() - client_p
->localClient
->lasttime
));
413 exit_client(client_p
, client_p
, &me
, scratch
);
416 else if((client_p
->flags
& FLAGS_PINGSENT
) == 0)
419 * if we havent PINGed the connection and we havent
420 * heard from it in a while, PING it to make sure
423 client_p
->flags
|= FLAGS_PINGSENT
;
424 /* not nice but does the job */
425 client_p
->localClient
->lasttime
= rb_current_time() - ping
;
426 sendto_one(client_p
, "PING :%s", me
.name
);
435 * check_unknowns_list
437 * inputs - pointer to list of unknown clients
439 * side effects - unknown clients get marked for termination after n seconds
442 check_unknowns_list(rb_dlink_list
* list
)
444 rb_dlink_node
*ptr
, *next_ptr
;
445 struct Client
*client_p
;
448 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
450 client_p
= ptr
->data
;
452 if(IsDead(client_p
) || IsClosing(client_p
))
455 /* Still querying with authd */
456 if(client_p
->preClient
!= NULL
&& client_p
->preClient
->auth
.cid
!= 0)
460 * Check UNKNOWN connections - if they have been in this state
461 * for > 30s, close them.
464 timeout
= IsAnyServer(client_p
) ? ConfigFileEntry
.connect_timeout
: 30;
465 if((rb_current_time() - client_p
->localClient
->firsttime
) > timeout
)
467 if(IsAnyServer(client_p
))
469 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
470 "No response from %s, closing link",
473 "No response from %s, closing link",
474 log_client_name(client_p
, HIDE_IP
));
476 exit_client(client_p
, client_p
, &me
, "Connection timed out");
482 notify_banned_client(struct Client
*client_p
, struct ConfItem
*aconf
, int ban
)
484 static const char conn_closed
[] = "Connection closed";
485 static const char d_lined
[] = "D-lined";
486 static const char k_lined
[] = "K-lined";
487 const char *reason
= NULL
;
488 const char *exit_reason
= conn_closed
;
490 if(ConfigFileEntry
.kline_with_reason
)
492 reason
= get_user_ban_reason(aconf
);
493 exit_reason
= reason
;
497 reason
= aconf
->status
== D_LINED
? d_lined
: k_lined
;
500 if(ban
== D_LINED
&& !IsPerson(client_p
))
501 sendto_one(client_p
, "NOTICE DLINE :*** You have been D-lined");
503 sendto_one(client_p
, form_str(ERR_YOUREBANNEDCREEP
),
504 me
.name
, client_p
->name
, reason
);
506 exit_client(client_p
, client_p
, &me
,
507 EmptyString(ConfigFileEntry
.kline_reason
) ? exit_reason
:
508 ConfigFileEntry
.kline_reason
);
515 * side effects - Check all connections for a pending k/dline against the
516 * client, exit the client if found.
519 check_banned_lines(void)
530 * side effects - all clients will be checked for klines
535 struct Client
*client_p
;
536 struct ConfItem
*aconf
;
538 rb_dlink_node
*next_ptr
;
540 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
542 client_p
= ptr
->data
;
544 if(IsMe(client_p
) || !IsPerson(client_p
))
547 if((aconf
= find_kline(client_p
)) != NULL
)
549 if(IsExemptKline(client_p
))
551 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
552 "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
553 get_client_name(client_p
, HIDE_IP
),
554 aconf
->user
, aconf
->host
);
558 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
559 "KLINE active for %s",
560 get_client_name(client_p
, HIDE_IP
));
562 notify_banned_client(client_p
, aconf
, K_LINED
);
571 * This process needs to be kept in sync with find_kline() aka find_conf_by_address().
573 * inputs - pointer to kline to check
575 * side effects - all clients will be checked against given kline
578 check_one_kline(struct ConfItem
*kline
)
580 struct Client
*client_p
;
582 rb_dlink_node
*next_ptr
;
585 struct rb_sockaddr_storage sockaddr
;
586 struct sockaddr_in ip4
;
588 masktype
= parse_netmask(kline
->host
, (struct sockaddr_storage
*)&sockaddr
, &bits
);
590 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
594 client_p
= ptr
->data
;
596 if(IsMe(client_p
) || !IsPerson(client_p
))
599 if(!match(kline
->user
, client_p
->username
))
602 /* match one kline */
605 if (client_p
->localClient
->ip
.ss_family
== AF_INET6
&&
606 rb_ipv4_from_ipv6((struct sockaddr_in6
*)&client_p
->localClient
->ip
, &ip4
)
607 && comp_with_mask_sock((struct sockaddr
*)&ip4
, (struct sockaddr
*)&sockaddr
, bits
))
611 if (client_p
->localClient
->ip
.ss_family
== sockaddr
.ss_family
&&
612 comp_with_mask_sock((struct sockaddr
*)&client_p
->localClient
->ip
,
613 (struct sockaddr
*)&sockaddr
, bits
))
617 if (match(kline
->host
, client_p
->orighost
))
619 if (match(kline
->host
, client_p
->sockhost
))
627 if(IsExemptKline(client_p
))
629 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
,
630 "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
631 get_client_name(client_p
, HIDE_IP
),
632 kline
->user
, kline
->host
);
636 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
637 "KLINE active for %s",
638 get_client_name(client_p
, HIDE_IP
));
640 notify_banned_client(client_p
, kline
, K_LINED
);
649 * side effects - all clients will be checked for dlines
654 struct Client
*client_p
;
655 struct ConfItem
*aconf
;
657 rb_dlink_node
*next_ptr
;
659 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
661 client_p
= ptr
->data
;
666 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
, GET_SS_FAMILY(&client_p
->localClient
->ip
))) != NULL
)
668 if(aconf
->status
& CONF_EXEMPTDLINE
)
671 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
672 "DLINE active for %s",
673 get_client_name(client_p
, HIDE_IP
));
675 notify_banned_client(client_p
, aconf
, D_LINED
);
680 /* dlines need to be checked against unknowns too */
681 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, unknown_list
.head
)
683 client_p
= ptr
->data
;
685 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
, GET_SS_FAMILY(&client_p
->localClient
->ip
))) != NULL
)
687 if(aconf
->status
& CONF_EXEMPTDLINE
)
690 notify_banned_client(client_p
, aconf
, D_LINED
);
699 * side effects - all clients will be checked for xlines
704 struct Client
*client_p
;
705 struct ConfItem
*aconf
;
707 rb_dlink_node
*next_ptr
;
709 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
711 client_p
= ptr
->data
;
713 if(IsMe(client_p
) || !IsPerson(client_p
))
716 if((aconf
= find_xline(client_p
->info
, 1)) != NULL
)
718 if(IsExemptKline(client_p
))
720 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
721 "XLINE over-ruled for %s, client is kline_exempt [%s]",
722 get_client_name(client_p
, HIDE_IP
),
727 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "XLINE active for %s",
728 get_client_name(client_p
, HIDE_IP
));
730 (void) exit_client(client_p
, client_p
, &me
, "Bad user info");
738 * inputs - resv, reason, time
740 * side effects - all local clients matching resv will be FNC'd
743 resv_nick_fnc(const char *mask
, const char *reason
, int temp_time
)
745 struct Client
*client_p
, *target_p
;
747 rb_dlink_node
*next_ptr
;
749 char note
[NICKLEN
+10];
751 if (!ConfigFileEntry
.resv_fnc
)
754 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
756 client_p
= ptr
->data
;
758 if(IsMe(client_p
) || !IsPerson(client_p
) || IsExemptResv(client_p
))
761 /* Skip users that already have UID nicks. */
762 if(IsDigit(client_p
->name
[0]))
765 if(match_esc(mask
, client_p
->name
))
770 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
771 "RESV forced nick change for %s!%s@%s to %s; nick matched [%s] (%s)",
772 client_p
->name
, client_p
->username
, client_p
->host
, nick
, mask
, reason
);
774 sendto_realops_snomask(SNO_NCHANGE
, L_ALL
,
775 "Nick change: From %s to %s [%s@%s]",
776 client_p
->name
, nick
, client_p
->username
, client_p
->host
);
781 sendto_one_notice(client_p
,
782 ":*** Nick %s is temporarily unavailable on this server.",
787 sendto_one_notice(client_p
,
788 ":*** Nick %s is no longer available on this server.",
792 /* Do all of the nick-changing gymnastics. */
793 client_p
->tsinfo
= rb_current_time();
794 whowas_add_history(client_p
, 1);
796 monitor_signoff(client_p
);
798 invalidate_bancache_user(client_p
);
800 sendto_common_channels_local(client_p
, NOCAPS
, NOCAPS
, ":%s!%s@%s NICK :%s",
801 client_p
->name
, client_p
->username
, client_p
->host
, nick
);
802 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s NICK %s :%ld",
803 use_id(client_p
), nick
, (long) client_p
->tsinfo
);
805 del_from_client_hash(client_p
->name
, client_p
);
806 rb_strlcpy(client_p
->name
, nick
, sizeof(client_p
->name
));
807 add_to_client_hash(nick
, client_p
);
809 monitor_signon(client_p
);
811 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->on_allow_list
.head
)
813 target_p
= ptr
->data
;
814 rb_dlinkFindDestroy(client_p
, &target_p
->localClient
->allow_list
);
815 rb_dlinkDestroy(ptr
, &client_p
->on_allow_list
);
818 snprintf(note
, sizeof(note
), "Nick: %s", nick
);
819 rb_note(client_p
->localClient
->F
, note
);
825 * update_client_exit_stats
827 * input - pointer to client
832 update_client_exit_stats(struct Client
*client_p
)
834 if(IsServer(client_p
))
836 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
837 "Server %s split from %s",
838 client_p
->name
, client_p
->servptr
->name
);
839 if(HasSentEob(client_p
))
842 else if(IsClient(client_p
))
847 if(IsInvisible(client_p
))
851 if(splitchecking
&& !splitmode
)
852 check_splitmode(NULL
);
856 * release_client_state
858 * input - pointer to client to release
863 release_client_state(struct Client
*client_p
)
865 if(client_p
->user
!= NULL
)
867 free_user(client_p
->user
, client_p
); /* try this here */
871 if(client_p
->serv
->user
!= NULL
)
872 free_user(client_p
->serv
->user
, client_p
);
873 if(client_p
->serv
->fullcaps
)
874 rb_free(client_p
->serv
->fullcaps
);
875 rb_free(client_p
->serv
);
880 * remove_client_from_list
881 * inputs - point to client to remove
883 * side effects - taken the code from ExitOneClient() for this
884 * and placed it here. - avalon
887 remove_client_from_list(struct Client
*client_p
)
889 s_assert(NULL
!= client_p
);
894 /* A client made with make_client()
895 * is on the unknown_list until removed.
896 * If it =does= happen to exit before its removed from that list
897 * and its =not= on the global_client_list, it will core here.
898 * short circuit that case now -db
900 if(client_p
->node
.prev
== NULL
&& client_p
->node
.next
== NULL
)
903 rb_dlinkDelete(&client_p
->node
, &global_client_list
);
905 update_client_exit_stats(client_p
);
911 * input - nickname to check, flag for nick from local client
912 * output - 0 if erroneous, else 1
916 clean_nick(const char *nick
, int loc_client
)
920 /* nicks cant start with a digit or -, and must have a length */
921 if(*nick
== '-' || *nick
== '\0')
924 if(loc_client
&& IsDigit(*nick
))
930 if(!IsNickChar(*nick
))
935 if(len
>= NICKLEN
&& (unsigned int)len
>= ConfigFileEntry
.nicklen
)
942 * find_person - find person by (nick)name.
943 * inputs - pointer to name
944 * output - return client pointer
948 find_person(const char *name
)
950 struct Client
*c2ptr
;
952 c2ptr
= find_client(name
);
954 if(c2ptr
&& IsPerson(c2ptr
))
960 find_named_person(const char *name
)
962 struct Client
*c2ptr
;
964 c2ptr
= find_named_client(name
);
966 if(c2ptr
&& IsPerson(c2ptr
))
973 * find_chasing - find the client structure for a nick name (user)
974 * using history mechanism if necessary. If the client is not found,
975 * an error message (NO SUCH NICK) is generated. If the client was found
976 * through the history, chasing will be 1 and otherwise 0.
979 find_chasing(struct Client
*source_p
, const char *user
, int *chasing
)
983 if(MyClient(source_p
))
984 who
= find_named_person(user
);
986 who
= find_person(user
);
991 if(who
|| IsDigit(*user
))
994 if(!(who
= whowas_get_history(user
, (long) KILLCHASETIMELIMIT
)))
996 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
,
997 form_str(ERR_NOSUCHNICK
), user
);
1006 * get_client_name - Return the name of the client
1007 * for various tracking and
1008 * admin purposes. The main purpose of this function is to
1009 * return the "socket host" name of the client, if that
1010 * differs from the advertised name (other than case).
1011 * But, this can be used to any client structure.
1014 * Watch out the allocation of "nbuf", if either source_p->name
1015 * or source_p->sockhost gets changed into pointers instead of
1016 * directly allocated within the structure...
1019 * Function return either a pointer to the structure (source_p) or
1020 * to internal buffer (nbuf). *NEVER* use the returned pointer
1021 * to modify what it points!!!
1025 get_client_name(struct Client
*client
, int showip
)
1027 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
1029 s_assert(NULL
!= client
);
1033 if(MyConnect(client
))
1035 if(!irccmp(client
->name
, client
->host
))
1036 return client
->name
;
1038 if(ConfigFileEntry
.hide_spoof_ips
&&
1039 showip
== SHOW_IP
&& IsIPSpoof(client
))
1041 if(IsAnyServer(client
))
1044 /* And finally, let's get the host information, ip or name */
1048 snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
1049 client
->name
, client
->username
,
1053 snprintf(nbuf
, sizeof(nbuf
), "%s[%s@255.255.255.255]",
1054 client
->name
, client
->username
);
1057 snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
1058 client
->name
, client
->username
, client
->host
);
1063 /* As pointed out by Adel Mezibra
1064 * Neph|l|m@EFnet. Was missing a return here.
1066 return client
->name
;
1069 /* log_client_name()
1071 * This version is the same as get_client_name, but doesnt contain the
1072 * code that will hide IPs always. This should be used for logfiles.
1075 log_client_name(struct Client
*target_p
, int showip
)
1077 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
1079 if(target_p
== NULL
)
1082 if(MyConnect(target_p
))
1084 if(irccmp(target_p
->name
, target_p
->host
) == 0)
1085 return target_p
->name
;
1090 snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]", target_p
->name
,
1091 target_p
->username
, target_p
->sockhost
);
1095 snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]", target_p
->name
,
1096 target_p
->username
, target_p
->host
);
1102 return target_p
->name
;
1105 /* is_remote_connect - Returns whether a server was /connect'ed by a remote
1106 * oper (send notices netwide) */
1108 is_remote_connect(struct Client
*client_p
)
1110 struct Client
*oper
;
1112 if (client_p
->serv
== NULL
)
1114 oper
= find_named_person(client_p
->serv
->by
);
1115 return oper
!= NULL
&& IsOper(oper
) && !MyConnect(oper
);
1119 free_exited_clients(void *unused
)
1121 rb_dlink_node
*ptr
, *next
;
1122 struct Client
*target_p
;
1124 RB_DLINK_FOREACH_SAFE(ptr
, next
, dead_list
.head
)
1126 target_p
= ptr
->data
;
1128 #ifdef DEBUG_EXITED_CLIENTS
1130 struct abort_client
*abt
;
1131 rb_dlink_node
*aptr
;
1134 RB_DLINK_FOREACH(aptr
, abort_list
.head
)
1137 if(abt
->client
== target_p
)
1140 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1141 "On abort_list: %s stat: %u flags: %llu handler: %c",
1142 target_p
->name
, (unsigned int) target_p
->status
,
1143 (unsigned long long)target_p
->flags
, target_p
->handler
);
1144 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1145 "Please report this to the charybdis developers!");
1152 rb_dlinkDestroy(ptr
, &dead_list
);
1158 if(ptr
->data
== NULL
)
1160 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1161 "Warning: null client on dead_list!");
1162 rb_dlinkDestroy(ptr
, &dead_list
);
1165 release_client_state(target_p
);
1166 free_client(target_p
);
1167 rb_dlinkDestroy(ptr
, &dead_list
);
1170 #ifdef DEBUG_EXITED_CLIENTS
1171 RB_DLINK_FOREACH_SAFE(ptr
, next
, dead_remote_list
.head
)
1173 target_p
= ptr
->data
;
1175 if(ptr
->data
== NULL
)
1177 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1178 "Warning: null client on dead_list!");
1179 rb_dlinkDestroy(ptr
, &dead_list
);
1182 release_client_state(target_p
);
1183 free_client(target_p
);
1184 rb_dlinkDestroy(ptr
, &dead_remote_list
);
1191 ** Remove all clients that depend on source_p; assumes all (S)QUITs have
1192 ** already been sent. we make sure to exit a server's dependent clients
1193 ** and servers before the server itself; exit_one_client takes care of
1194 ** actually removing things off llists. tweaked from +CSr31 -orabidoo
1197 * added sanity test code.... source_p->serv might be NULL...
1200 recurse_remove_clients(struct Client
*source_p
, const char *comment
)
1202 struct Client
*target_p
;
1203 rb_dlink_node
*ptr
, *ptr_next
;
1208 if(source_p
->serv
== NULL
) /* oooops. uh this is actually a major bug */
1211 /* this is very ugly, but it saves cpu :P */
1212 if(ConfigFileEntry
.nick_delay
> 0)
1214 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1216 target_p
= ptr
->data
;
1217 target_p
->flags
|= FLAGS_KILLED
;
1218 add_nd_entry(target_p
->name
);
1220 if(!IsDead(target_p
) && !IsClosing(target_p
))
1221 exit_remote_client(NULL
, target_p
, &me
, comment
);
1226 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1228 target_p
= ptr
->data
;
1229 target_p
->flags
|= FLAGS_KILLED
;
1231 if(!IsDead(target_p
) && !IsClosing(target_p
))
1232 exit_remote_client(NULL
, target_p
, &me
, comment
);
1236 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->servers
.head
)
1238 target_p
= ptr
->data
;
1239 recurse_remove_clients(target_p
, comment
);
1240 qs_server(NULL
, target_p
, &me
, comment
);
1245 ** Remove *everything* that depends on source_p, from all lists, and sending
1246 ** all necessary SQUITs. source_p itself is still on the lists,
1247 ** and its SQUITs have been sent except for the upstream one -orabidoo
1250 remove_dependents(struct Client
*client_p
,
1251 struct Client
*source_p
,
1252 struct Client
*from
, const char *comment
, const char *comment1
)
1255 rb_dlink_node
*ptr
, *next
;
1257 RB_DLINK_FOREACH_SAFE(ptr
, next
, serv_list
.head
)
1261 if(IsMe(to
) || to
== source_p
->from
|| to
== client_p
)
1264 sendto_one(to
, "SQUIT %s :%s", get_id(source_p
, to
), comment
);
1267 recurse_remove_clients(source_p
, comment1
);
1271 exit_aborted_clients(void *unused
)
1273 struct abort_client
*abt
;
1274 rb_dlink_node
*ptr
, *next
;
1275 RB_DLINK_FOREACH_SAFE(ptr
, next
, abort_list
.head
)
1279 #ifdef DEBUG_EXITED_CLIENTS
1281 if(rb_dlinkFind(abt
->client
, &dead_list
))
1284 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1285 "On dead_list: %s stat: %u flags: %llu handler: %c",
1286 abt
->client
->name
, (unsigned int) abt
->client
->status
,
1287 (unsigned long long)abt
->client
->flags
, 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 rb_dlinkDelete(ptr
, &abort_list
);
1298 if(IsAnyServer(abt
->client
))
1299 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1300 "Closing link to %s: %s",
1301 abt
->client
->name
, 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()
1317 dead_link(struct Client
*client_p
, int sendqex
)
1319 struct abort_client
*abt
;
1321 s_assert(!IsMe(client_p
));
1322 if(IsDead(client_p
) || IsClosing(client_p
) || IsMe(client_p
))
1325 abt
= (struct abort_client
*) rb_malloc(sizeof(struct abort_client
));
1328 rb_strlcpy(abt
->notice
, "Max SendQ exceeded", sizeof(abt
->notice
));
1330 snprintf(abt
->notice
, sizeof(abt
->notice
), "Write error: %s", strerror(errno
));
1332 abt
->client
= client_p
;
1333 SetIOError(client_p
);
1335 SetClosing(client_p
);
1336 rb_dlinkAdd(abt
, &abt
->node
, &abort_list
);
1340 /* This does the remove of the user from channels..local or remote */
1342 exit_generic_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1343 const char *comment
)
1345 rb_dlink_node
*ptr
, *next_ptr
;
1347 if(IsOper(source_p
))
1348 rb_dlinkFindDestroy(source_p
, &oper_list
);
1350 sendto_common_channels_local(source_p
, NOCAPS
, NOCAPS
, ":%s!%s@%s QUIT :%s",
1352 source_p
->username
, source_p
->host
, comment
);
1354 remove_user_from_channels(source_p
);
1356 /* Should not be in any channels now */
1357 s_assert(source_p
->user
->channel
.head
== NULL
);
1359 /* Clean up invitefield */
1360 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, source_p
->user
->invited
.head
)
1362 del_invite(ptr
->data
, source_p
);
1365 /* Clean up allow lists */
1366 del_all_accepts(source_p
);
1368 whowas_add_history(source_p
, 0);
1369 whowas_off_history(source_p
);
1371 monitor_signoff(source_p
);
1373 if(has_id(source_p
))
1374 del_from_id_hash(source_p
->id
, source_p
);
1376 del_from_hostname_hash(source_p
->orighost
, source_p
);
1377 del_from_client_hash(source_p
->name
, source_p
);
1378 remove_client_from_list(source_p
);
1382 * Assumes IsPerson(source_p) && !MyConnect(source_p)
1386 exit_remote_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1387 const char *comment
)
1389 exit_generic_client(client_p
, source_p
, from
, comment
);
1391 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1393 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->users
);
1396 if((source_p
->flags
& FLAGS_KILLED
) == 0)
1398 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
1399 ":%s QUIT :%s", use_id(source_p
), comment
);
1403 #ifdef DEBUG_EXITED_CLIENTS
1404 rb_dlinkAddAlloc(source_p
, &dead_remote_list
);
1406 rb_dlinkAddAlloc(source_p
, &dead_list
);
1408 return(CLIENT_EXITED
);
1412 * This assumes IsUnknown(source_p) == true and MyConnect(source_p) == true
1416 exit_unknown_client(struct Client
*client_p
, /* The local client originating the
1417 * exit or NULL, if this exit is
1418 * generated by this server for
1420 * This will not get any of the
1421 * generated messages. */
1422 struct Client
*source_p
, /* Client exiting */
1423 struct Client
*from
, /* Client firing off this Exit,
1425 const char *comment
)
1427 authd_abort_client(source_p
);
1428 rb_dlinkDelete(&source_p
->localClient
->tnode
, &unknown_list
);
1430 if(!IsIOError(source_p
))
1431 sendto_one(source_p
, "ERROR :Closing Link: %s (%s)",
1432 source_p
->user
!= NULL
? source_p
->host
: "127.0.0.1",
1435 close_connection(source_p
);
1437 if(has_id(source_p
))
1438 del_from_id_hash(source_p
->id
, source_p
);
1440 del_from_hostname_hash(source_p
->host
, source_p
);
1441 if (!IsAnyServer(source_p
))
1442 del_from_client_hash(source_p
->name
, source_p
);
1443 remove_client_from_list(source_p
);
1445 rb_dlinkAddAlloc(source_p
, &dead_list
);
1447 /* Note that we don't need to add unknowns to the dead_list */
1448 return(CLIENT_EXITED
);
1452 exit_remote_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1453 const char *comment
)
1455 static char comment1
[(HOSTLEN
*2)+2];
1456 static char newcomment
[BUFSIZE
];
1457 struct Client
*target_p
;
1459 if(ConfigServerHide
.flatten_links
)
1460 strcpy(comment1
, "*.net *.split");
1463 strcpy(comment1
, source_p
->servptr
->name
);
1464 strcat(comment1
, " ");
1465 strcat(comment1
, source_p
->name
);
1468 snprintf(newcomment
, sizeof(newcomment
), "by %s: %s",
1469 from
->name
, comment
);
1471 remove_dependents(client_p
, source_p
, from
, IsPerson(from
) ? newcomment
: comment
, comment1
);
1473 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1475 rb_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
);
1492 scache_split(source_p
->serv
->nameinfo
);
1495 #ifdef DEBUG_EXITED_CLIENTS
1496 rb_dlinkAddAlloc(source_p
, &dead_remote_list
);
1498 rb_dlinkAddAlloc(source_p
, &dead_list
);
1504 qs_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1505 const char *comment
)
1507 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1508 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1512 rb_dlinkFindDestroy(source_p
, &global_serv_list
);
1514 if(has_id(source_p
))
1515 del_from_id_hash(source_p
->id
, source_p
);
1517 del_from_client_hash(source_p
->name
, source_p
);
1518 remove_client_from_list(source_p
);
1519 scache_split(source_p
->serv
->nameinfo
);
1522 rb_dlinkAddAlloc(source_p
, &dead_list
);
1527 exit_local_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1528 const char *comment
)
1530 static char comment1
[(HOSTLEN
*2)+2];
1531 static char newcomment
[BUFSIZE
];
1532 unsigned int sendk
, recvk
;
1534 rb_dlinkDelete(&source_p
->localClient
->tnode
, &serv_list
);
1535 rb_dlinkFindDestroy(source_p
, &global_serv_list
);
1537 sendk
= source_p
->localClient
->sendK
;
1538 recvk
= source_p
->localClient
->receiveK
;
1540 /* Always show source here, so the server notices show
1541 * which side initiated the split -- jilles
1543 snprintf(newcomment
, sizeof(newcomment
), "by %s: %s",
1544 from
== source_p
? me
.name
: from
->name
, comment
);
1545 if (!IsIOError(source_p
))
1546 sendto_one(source_p
, "SQUIT %s :%s", use_id(source_p
),
1548 if(client_p
!= NULL
&& source_p
!= client_p
&& !IsIOError(source_p
))
1550 sendto_one(source_p
, "ERROR :Closing Link: 127.0.0.1 %s (%s)",
1551 source_p
->name
, comment
);
1554 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1555 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1560 close_connection(source_p
);
1562 if(ConfigServerHide
.flatten_links
)
1563 strcpy(comment1
, "*.net *.split");
1566 strcpy(comment1
, source_p
->servptr
->name
);
1567 strcat(comment1
, " ");
1568 strcat(comment1
, source_p
->name
);
1571 if(source_p
->serv
!= NULL
)
1572 remove_dependents(client_p
, source_p
, from
, IsPerson(from
) ? newcomment
: comment
, comment1
);
1574 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s was connected"
1575 " for %ld seconds. %d/%d sendK/recvK.",
1576 source_p
->name
, (long) rb_current_time() - source_p
->localClient
->firsttime
, sendk
, recvk
);
1578 ilog(L_SERVER
, "%s was connected for %ld seconds. %d/%d sendK/recvK.",
1579 source_p
->name
, (long) rb_current_time() - source_p
->localClient
->firsttime
, sendk
, recvk
);
1581 if(has_id(source_p
))
1582 del_from_id_hash(source_p
->id
, source_p
);
1584 del_from_client_hash(source_p
->name
, source_p
);
1585 remove_client_from_list(source_p
);
1586 scache_split(source_p
->serv
->nameinfo
);
1589 rb_dlinkAddAlloc(source_p
, &dead_list
);
1595 * This assumes IsPerson(source_p) == true && MyConnect(source_p) == true
1599 exit_local_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1600 const char *comment
)
1602 unsigned long on_for
;
1605 exit_generic_client(client_p
, source_p
, from
, comment
);
1606 clear_monitor(source_p
);
1608 s_assert(IsPerson(source_p
));
1609 rb_dlinkDelete(&source_p
->localClient
->tnode
, &lclient_list
);
1610 rb_dlinkDelete(&source_p
->lnode
, &me
.serv
->users
);
1612 if(IsOper(source_p
))
1613 rb_dlinkFindDestroy(source_p
, &local_oper_list
);
1615 sendto_realops_snomask(SNO_CCONN
, L_ALL
,
1616 "Client exiting: %s (%s@%s) [%s] [%s]",
1618 source_p
->username
, source_p
->host
, comment
,
1619 show_ip(NULL
, source_p
) ? source_p
->sockhost
: "255.255.255.255");
1621 sendto_realops_snomask(SNO_CCONNEXT
, L_ALL
,
1622 "CLIEXIT %s %s %s %s 0 %s",
1623 source_p
->name
, source_p
->username
, source_p
->host
,
1624 show_ip(NULL
, source_p
) ? source_p
->sockhost
: "255.255.255.255",
1627 on_for
= rb_current_time() - source_p
->localClient
->firsttime
;
1629 ilog(L_USER
, "%s (%3lu:%02lu:%02lu): %s!%s@%s %s %d/%d",
1630 rb_ctime(rb_current_time(), tbuf
, sizeof(tbuf
)), on_for
/ 3600,
1631 (on_for
% 3600) / 60, on_for
% 60,
1632 source_p
->name
, source_p
->username
, source_p
->host
,
1634 source_p
->localClient
->sendK
, source_p
->localClient
->receiveK
);
1636 sendto_one(source_p
, "ERROR :Closing Link: %s (%s)", source_p
->host
, comment
);
1637 close_connection(source_p
);
1639 if((source_p
->flags
& FLAGS_KILLED
) == 0)
1641 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
1642 ":%s QUIT :%s", use_id(source_p
), comment
);
1646 rb_dlinkAddAlloc(source_p
, &dead_list
);
1647 return(CLIENT_EXITED
);
1652 ** exit_client - This is old "m_bye". Name changed, because this is not a
1653 ** protocol function, but a general server utility function.
1655 ** This function exits a client of *any* type (user, server, etc)
1656 ** from this server. Also, this generates all necessary prototol
1657 ** messages that this exit may cause.
1659 ** 1) If the client is a local client, then this implicitly
1660 ** exits all other clients depending on this connection (e.g.
1661 ** remote clients having 'from'-field that points to this.
1663 ** 2) If the client is a remote client, then only this is exited.
1665 ** For convenience, this function returns a suitable value for
1666 ** m_function return value:
1668 ** CLIENT_EXITED if (client_p == source_p)
1669 ** 0 if (client_p != source_p)
1672 exit_client(struct Client
*client_p
, /* The local client originating the
1673 * exit or NULL, if this exit is
1674 * generated by this server for
1676 * This will not get any of the
1677 * generated messages. */
1678 struct Client
*source_p
, /* Client exiting */
1679 struct Client
*from
, /* Client firing off this Exit,
1681 const char *comment
/* Reason for the exit */
1686 hook_data_client_exit hdata
;
1687 if(IsClosing(source_p
))
1690 /* note, this HAS to be here, when we exit a client we attempt to
1691 * send them data, if this generates a write error we must *not* add
1692 * them to the abort list --fl
1694 SetClosing(source_p
);
1696 hdata
.local_link
= client_p
;
1697 hdata
.target
= source_p
;
1699 hdata
.comment
= comment
;
1700 call_hook(h_client_exit
, &hdata
);
1702 if(MyConnect(source_p
))
1704 /* Local clients of various types */
1705 if(IsPerson(source_p
))
1706 ret
= exit_local_client(client_p
, source_p
, from
, comment
);
1707 else if(IsServer(source_p
))
1708 ret
= exit_local_server(client_p
, source_p
, from
, comment
);
1709 /* IsUnknown || IsConnecting || IsHandShake */
1710 else if(!IsReject(source_p
))
1711 ret
= exit_unknown_client(client_p
, source_p
, from
, comment
);
1716 if(IsPerson(source_p
))
1717 ret
= exit_remote_client(client_p
, source_p
, from
, comment
);
1718 else if(IsServer(source_p
))
1719 ret
= exit_remote_server(client_p
, source_p
, from
, comment
);
1722 call_hook(h_after_client_exit
, NULL
);
1728 * Count up local client memory
1731 /* XXX one common Client list now */
1733 count_local_client_memory(size_t * count
, size_t * local_client_memory_used
)
1736 rb_bh_usage(lclient_heap
, count
, NULL
, &lusage
, NULL
);
1737 *local_client_memory_used
= lusage
+ (*count
* (sizeof(void *) + sizeof(struct Client
)));
1741 * Count up remote client memory
1744 count_remote_client_memory(size_t * count
, size_t * remote_client_memory_used
)
1746 size_t lcount
, rcount
;
1747 rb_bh_usage(lclient_heap
, &lcount
, NULL
, NULL
, NULL
);
1748 rb_bh_usage(client_heap
, &rcount
, NULL
, NULL
, NULL
);
1749 *count
= rcount
- lcount
;
1750 *remote_client_memory_used
= *count
* (sizeof(void *) + sizeof(struct Client
));
1755 * accept processing, this adds a form of "caller ID" to ircd
1757 * If a client puts themselves into "caller ID only" mode,
1758 * only clients that match a client pointer they have put on
1759 * the accept list will be allowed to message them.
1761 * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ]
1763 * [target.allow_list] -> [ source1 ] -> [source2 ]
1765 * i.e. a target will have a link list of source pointers it will allow
1766 * each source client then has a back pointer pointing back
1767 * to the client that has it on its accept list.
1768 * This allows for exit_one_client to remove these now bogus entries
1769 * from any client having an accept on them.
1774 * inputs - pointer to exiting client
1776 * side effects - Walk through given clients allow_list and on_allow_list
1777 * remove all references to this client
1780 del_all_accepts(struct Client
*client_p
)
1783 rb_dlink_node
*next_ptr
;
1784 struct Client
*target_p
;
1786 if(MyClient(client_p
) && client_p
->localClient
->allow_list
.head
)
1788 /* clear this clients accept list, and remove them from
1789 * everyones on_accept_list
1791 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->localClient
->allow_list
.head
)
1793 target_p
= ptr
->data
;
1794 rb_dlinkFindDestroy(client_p
, &target_p
->on_allow_list
);
1795 rb_dlinkDestroy(ptr
, &client_p
->localClient
->allow_list
);
1799 /* remove this client from everyones accept list */
1800 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->on_allow_list
.head
)
1802 target_p
= ptr
->data
;
1803 rb_dlinkFindDestroy(client_p
, &target_p
->localClient
->allow_list
);
1804 rb_dlinkDestroy(ptr
, &client_p
->on_allow_list
);
1809 * show_ip() - asks if the true IP should be shown when source is
1810 * asking for info about target
1812 * Inputs - source_p who is asking
1813 * - target_p who do we want the info on
1814 * Output - returns 1 if clear IP can be shown, otherwise 0
1815 * Side Effects - none
1819 show_ip(struct Client
*source_p
, struct Client
*target_p
)
1821 if(IsAnyServer(target_p
))
1825 else if(IsIPSpoof(target_p
))
1827 /* source == NULL indicates message is being sent
1830 if(!ConfigFileEntry
.hide_spoof_ips
&&
1831 (source_p
== NULL
|| MyOper(source_p
)))
1835 else if(IsDynSpoof(target_p
) && (source_p
!= NULL
&& !IsOper(source_p
)))
1842 show_ip_conf(struct ConfItem
*aconf
, struct Client
*source_p
)
1844 if(IsConfDoSpoofIp(aconf
))
1846 if(!ConfigFileEntry
.hide_spoof_ips
&& MyOper(source_p
))
1856 show_ip_whowas(struct Whowas
*whowas
, struct Client
*source_p
)
1858 if(whowas
->flags
& WHOWAS_IP_SPOOFING
)
1859 if(ConfigFileEntry
.hide_spoof_ips
|| !MyOper(source_p
))
1861 if(whowas
->flags
& WHOWAS_DYNSPOOF
)
1862 if(!IsOper(source_p
))
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 struct Server
*serv
= client_p
->serv
;
1905 serv
= (struct Server
*) rb_malloc(sizeof(struct Server
));
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
);
1929 rb_free(user
->opername
);
1931 privilegeset_unref(user
->privset
);
1935 if(user
->refcnt
< 0 || user
->invited
.head
|| user
->channel
.head
)
1937 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1938 "* %p user (%s!%s@%s) %p %p %p %lu %d *",
1940 client_p
? client_p
->
1947 rb_dlink_list_length(&user
->channel
),
1949 s_assert(!user
->refcnt
);
1950 s_assert(!user
->invited
.head
);
1951 s_assert(!user
->channel
.head
);
1954 rb_bh_free(user_heap
, user
);
1959 allocate_away(struct Client
*client_p
)
1961 if(client_p
->user
->away
== NULL
)
1962 client_p
->user
->away
= rb_bh_alloc(away_heap
);
1967 free_away(struct Client
*client_p
)
1969 if(client_p
->user
!= NULL
&& client_p
->user
->away
!= NULL
) {
1970 rb_bh_free(away_heap
, client_p
->user
->away
);
1971 client_p
->user
->away
= NULL
;
1980 for(i
= 0; i
< 3; i
++)
1981 current_uid
[i
] = me
.id
[i
];
1983 for(i
= 3; i
< 9; i
++)
1984 current_uid
[i
] = 'A';
1986 current_uid
[9] = '\0';
1993 static int flipped
= 0;
1997 for(i
= 8; i
> 3; i
--)
1999 if(current_uid
[i
] == 'Z')
2001 current_uid
[i
] = '0';
2004 else if(current_uid
[i
] != '9')
2010 current_uid
[i
] = 'A';
2013 /* if this next if() triggers, we're fucked. */
2014 if(current_uid
[3] == 'Z')
2016 current_uid
[i
] = 'A';
2022 /* if this happens..well, i'm not sure what to say, but lets handle it correctly */
2023 if(rb_unlikely(flipped
))
2025 /* this slows down uid generation a bit... */
2026 if(find_id(current_uid
) != NULL
)
2034 * Close the physical connection. This function must make
2035 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
2038 close_connection(struct Client
*client_p
)
2040 s_assert(client_p
!= NULL
);
2041 if(client_p
== NULL
)
2044 s_assert(MyConnect(client_p
));
2045 if(!MyConnect(client_p
))
2048 if(IsServer(client_p
))
2050 struct server_conf
*server_p
;
2052 ServerStats
.is_sv
++;
2053 ServerStats
.is_sbs
+= client_p
->localClient
->sendB
;
2054 ServerStats
.is_sbr
+= client_p
->localClient
->receiveB
;
2055 ServerStats
.is_sti
+= (unsigned long long)(rb_current_time() - client_p
->localClient
->firsttime
);
2058 * If the connection has been up for a long amount of time, schedule
2059 * a 'quick' reconnect, else reset the next-connect cycle.
2061 if((server_p
= find_server_conf(client_p
->name
)) != NULL
)
2064 * Reschedule a faster reconnect, if this was a automatically
2065 * connected configuration entry. (Note that if we have had
2066 * a rehash in between, the status has been changed to
2067 * CONF_ILLEGAL). But only do this if it was a "good" link.
2069 server_p
->hold
= time(NULL
);
2071 (server_p
->hold
- client_p
->localClient
->lasttime
>
2072 HANGONGOODLINK
) ? HANGONRETRYDELAY
: ConFreq(server_p
->class);
2076 else if(IsClient(client_p
))
2078 ServerStats
.is_cl
++;
2079 ServerStats
.is_cbs
+= client_p
->localClient
->sendB
;
2080 ServerStats
.is_cbr
+= client_p
->localClient
->receiveB
;
2081 ServerStats
.is_cti
+= (unsigned long long)(rb_current_time() - client_p
->localClient
->firsttime
);
2084 ServerStats
.is_ni
++;
2086 client_release_connids(client_p
);
2088 if(client_p
->localClient
->F
!= NULL
)
2090 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
2091 if(!IsIOError(client_p
))
2092 send_queued(client_p
);
2094 rb_close(client_p
->localClient
->F
);
2095 client_p
->localClient
->F
= NULL
;
2098 rb_linebuf_donebuf(&client_p
->localClient
->buf_sendq
);
2099 rb_linebuf_donebuf(&client_p
->localClient
->buf_recvq
);
2100 detach_conf(client_p
);
2102 /* XXX shouldnt really be done here. */
2103 detach_server_conf(client_p
);
2105 client_p
->from
= NULL
; /* ...this should catch them! >:) --msa */
2106 ClearMyConnect(client_p
);
2107 SetIOError(client_p
);
2113 error_exit_client(struct Client
*client_p
, int error
)
2116 * ...hmm, with non-blocking sockets we might get
2117 * here from quite valid reasons, although.. why
2118 * would select report "data available" when there
2119 * wasn't... so, this must be an error anyway... --msa
2120 * actually, EOF occurs when read() returns 0 and
2121 * in due course, select() returns that fd as ready
2122 * for reading even though it ends up being an EOF. -avalon
2125 int current_error
= rb_get_sockerr(client_p
->localClient
->F
);
2127 SetIOError(client_p
);
2129 if(IsServer(client_p
) || IsHandshake(client_p
))
2133 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) && !IsServer(client_p
) ? L_NETWIDE
: L_ALL
,
2134 "Server %s closed the connection",
2137 ilog(L_SERVER
, "Server %s closed the connection",
2138 log_client_name(client_p
, SHOW_IP
));
2142 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) && !IsServer(client_p
) ? L_NETWIDE
: L_ALL
,
2143 "Lost connection to %s: %s",
2144 client_p
->name
, strerror(current_error
));
2145 ilog(L_SERVER
, "Lost connection to %s: %s",
2146 log_client_name(client_p
, SHOW_IP
), strerror(current_error
));
2151 rb_strlcpy(errmsg
, "Remote host closed the connection", sizeof(errmsg
));
2153 snprintf(errmsg
, sizeof(errmsg
), "Read error: %s", strerror(current_error
));
2155 exit_client(client_p
, client_p
, &me
, errmsg
);