]>
Commit | Line | Data |
---|---|---|
212380e3 | 1 | /* |
b37021a4 | 2 | * charybdis: an advanced ircd. |
212380e3 AC |
3 | * client.c: Controls clients. |
4 | * | |
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 | |
b37021a4 | 8 | * Copyright (C) 2007 William Pitcock |
212380e3 AC |
9 | * |
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. | |
14 | * | |
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. | |
19 | * | |
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 | |
23 | * USA | |
212380e3 AC |
24 | */ |
25 | #include "stdinc.h" | |
9b8e9eb3 | 26 | #include "defaults.h" |
212380e3 | 27 | |
212380e3 AC |
28 | #include "client.h" |
29 | #include "class.h" | |
212380e3 | 30 | #include "hash.h" |
4562c604 | 31 | #include "match.h" |
212380e3 | 32 | #include "ircd.h" |
212380e3 AC |
33 | #include "numeric.h" |
34 | #include "packet.h" | |
64fae260 | 35 | #include "authproc.h" |
212380e3 AC |
36 | #include "s_conf.h" |
37 | #include "s_newconf.h" | |
4016731b | 38 | #include "logger.h" |
212380e3 AC |
39 | #include "s_serv.h" |
40 | #include "s_stats.h" | |
41 | #include "send.h" | |
42 | #include "whowas.h" | |
43 | #include "s_user.h" | |
212380e3 | 44 | #include "hash.h" |
212380e3 | 45 | #include "hostmask.h" |
212380e3 AC |
46 | #include "listener.h" |
47 | #include "hook.h" | |
48 | #include "msg.h" | |
49 | #include "monitor.h" | |
54015b5f | 50 | #include "reject.h" |
994544c2 | 51 | #include "scache.h" |
a4bf26dd | 52 | #include "rb_dictionary.h" |
c6d72037 | 53 | #include "sslproc.h" |
c53ca1e0 | 54 | #include "wsproc.h" |
77d3d2db | 55 | #include "s_assert.h" |
212380e3 AC |
56 | |
57 | #define DEBUG_EXITED_CLIENTS | |
58 | ||
330fc5c1 AC |
59 | static void check_pings_list(rb_dlink_list * list); |
60 | static void check_unknowns_list(rb_dlink_list * list); | |
212380e3 AC |
61 | static void free_exited_clients(void *unused); |
62 | static void exit_aborted_clients(void *unused); | |
63 | ||
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); | |
70 | ||
71 | static EVH check_pings; | |
72 | ||
5433b83e VY |
73 | static rb_bh *client_heap = NULL; |
74 | static rb_bh *lclient_heap = NULL; | |
b09cbaa3 | 75 | static rb_bh *pclient_heap = NULL; |
5433b83e | 76 | static rb_bh *user_heap = NULL; |
c127b45b | 77 | static rb_bh *away_heap = NULL; |
5433b83e | 78 | static char current_uid[IDLEN]; |
de7cf7e0 | 79 | static uint32_t current_connid = 0; |
212380e3 | 80 | |
4177311e | 81 | rb_dictionary *nd_dict = NULL; |
43de0f45 | 82 | |
212380e3 AC |
83 | enum |
84 | { | |
85 | D_LINED, | |
aae358c0 | 86 | K_LINED |
212380e3 AC |
87 | }; |
88 | ||
330fc5c1 | 89 | rb_dlink_list dead_list; |
212380e3 | 90 | #ifdef DEBUG_EXITED_CLIENTS |
330fc5c1 | 91 | static rb_dlink_list dead_remote_list; |
212380e3 AC |
92 | #endif |
93 | ||
94 | struct abort_client | |
95 | { | |
330fc5c1 | 96 | rb_dlink_node node; |
212380e3 AC |
97 | struct Client *client; |
98 | char notice[REASONLEN]; | |
99 | }; | |
100 | ||
330fc5c1 | 101 | static rb_dlink_list abort_list; |
212380e3 AC |
102 | |
103 | ||
104 | /* | |
105 | * init_client | |
106 | * | |
107 | * inputs - NONE | |
108 | * output - NONE | |
109 | * side effects - initialize client free memory | |
110 | */ | |
111 | void | |
112 | init_client(void) | |
113 | { | |
114 | /* | |
115 | * start off the check ping event .. -- adrian | |
116 | * Every 30 seconds is plenty -- db | |
117 | */ | |
1087485c | 118 | client_heap = rb_bh_create(sizeof(struct Client), CLIENT_HEAP_SIZE, "client_heap"); |
adc6cc42 VY |
119 | lclient_heap = rb_bh_create(sizeof(struct LocalUser), LCLIENT_HEAP_SIZE, "lclient_heap"); |
120 | pclient_heap = rb_bh_create(sizeof(struct PreClient), PCLIENT_HEAP_SIZE, "pclient_heap"); | |
b09cbaa3 | 121 | user_heap = rb_bh_create(sizeof(struct User), USER_HEAP_SIZE, "user_heap"); |
c127b45b | 122 | away_heap = rb_bh_create(AWAYLEN, AWAY_HEAP_SIZE, "away_heap"); |
adc6cc42 | 123 | |
3b2ebd04 JT |
124 | rb_event_addish("check_pings", check_pings, NULL, 30); |
125 | rb_event_addish("free_exited_clients", &free_exited_clients, NULL, 4); | |
126 | rb_event_addish("exit_aborted_clients", exit_aborted_clients, NULL, 1); | |
5a606a8f | 127 | rb_event_add("flood_recalc", flood_recalc, NULL, 1); |
43de0f45 | 128 | |
a4bf26dd | 129 | nd_dict = rb_dictionary_create("nickdelay", irccmp); |
212380e3 AC |
130 | } |
131 | ||
de7cf7e0 AC |
132 | /* |
133 | * connid_get - allocate a connid | |
134 | * | |
135 | * inputs - none | |
136 | * outputs - a connid token which is used to represent a logical circuit | |
137 | * side effects - current_connid is incremented, possibly multiple times. | |
138 | * the association of the connid to it's client is committed. | |
139 | */ | |
140 | uint32_t | |
141 | connid_get(struct Client *client_p) | |
142 | { | |
143 | s_assert(MyClient(client_p)); | |
144 | if (!MyClient(client_p)) | |
145 | return 0; | |
146 | ||
147 | /* find a connid that is available */ | |
148 | while (find_cli_connid_hash(++current_connid) != NULL) | |
149 | { | |
150 | /* handle wraparound, current_connid must NEVER be 0 */ | |
151 | if (current_connid == 0) | |
152 | ++current_connid; | |
153 | } | |
154 | ||
155 | add_to_cli_connid_hash(client_p, current_connid); | |
156 | rb_dlinkAddAlloc(RB_UINT_TO_POINTER(current_connid), &client_p->localClient->connids); | |
157 | ||
158 | return current_connid; | |
159 | } | |
160 | ||
161 | /* | |
162 | * connid_put - free a connid | |
163 | * | |
164 | * inputs - connid to free | |
165 | * outputs - nothing | |
166 | * side effects - connid bookkeeping structures are freed | |
167 | */ | |
168 | void | |
169 | connid_put(uint32_t id) | |
170 | { | |
171 | struct Client *client_p; | |
172 | ||
173 | s_assert(id != 0); | |
174 | if (id == 0) | |
175 | return; | |
176 | ||
177 | client_p = find_cli_connid_hash(id); | |
178 | if (client_p == NULL) | |
179 | return; | |
180 | ||
181 | del_from_cli_connid_hash(id); | |
182 | rb_dlinkFindDestroy(RB_UINT_TO_POINTER(id), &client_p->localClient->connids); | |
183 | } | |
184 | ||
185 | /* | |
186 | * client_release_connids - release any connids still attached to a client | |
187 | * | |
188 | * inputs - client to garbage collect | |
189 | * outputs - none | |
190 | * side effects - client's connids are garbage collected | |
191 | */ | |
192 | void | |
193 | client_release_connids(struct Client *client_p) | |
194 | { | |
195 | rb_dlink_node *ptr, *ptr2; | |
196 | ||
197 | s_assert(MyClient(client_p)); | |
198 | if (!MyClient(client_p)) | |
199 | return; | |
200 | ||
201 | RB_DLINK_FOREACH_SAFE(ptr, ptr2, client_p->localClient->connids.head) | |
202 | connid_put(RB_POINTER_TO_UINT(ptr->data)); | |
203 | } | |
212380e3 AC |
204 | |
205 | /* | |
206 | * make_client - create a new Client struct and set it to initial state. | |
207 | * | |
208 | * from == NULL, create local client (a client connected | |
209 | * to a socket). | |
210 | * | |
211 | * from, create remote client (behind a socket | |
212 | * associated with the client defined by | |
213 | * 'from'). ('from' is a local client!!). | |
214 | */ | |
215 | struct Client * | |
216 | make_client(struct Client *from) | |
217 | { | |
218 | struct Client *client_p = NULL; | |
219 | struct LocalUser *localClient; | |
220 | ||
398b6a73 | 221 | client_p = rb_bh_alloc(client_heap); |
212380e3 AC |
222 | |
223 | if(from == NULL) | |
224 | { | |
225 | client_p->from = client_p; /* 'from' of local client is self! */ | |
226 | ||
b09cbaa3 | 227 | localClient = rb_bh_alloc(lclient_heap); |
212380e3 AC |
228 | SetMyConnect(client_p); |
229 | client_p->localClient = localClient; | |
230 | ||
e3354945 | 231 | client_p->localClient->lasttime = client_p->localClient->firsttime = rb_current_time(); |
212380e3 | 232 | |
c5c2f506 | 233 | client_p->localClient->F = NULL; |
212380e3 | 234 | |
5ef68b13 | 235 | client_p->preClient = rb_bh_alloc(pclient_heap); |
212380e3 AC |
236 | |
237 | /* as good a place as any... */ | |
330fc5c1 | 238 | rb_dlinkAdd(client_p, &client_p->localClient->tnode, &unknown_list); |
212380e3 AC |
239 | } |
240 | else | |
241 | { /* from is not NULL */ | |
242 | client_p->localClient = NULL; | |
243 | client_p->preClient = NULL; | |
244 | client_p->from = from; /* 'from' of local client is self! */ | |
245 | } | |
55abcbb2 | 246 | |
212380e3 AC |
247 | SetUnknown(client_p); |
248 | strcpy(client_p->username, "unknown"); | |
249 | ||
250 | return client_p; | |
251 | } | |
252 | ||
253 | void | |
254 | free_pre_client(struct Client *client_p) | |
255 | { | |
212380e3 AC |
256 | s_assert(NULL != client_p); |
257 | ||
258 | if(client_p->preClient == NULL) | |
259 | return; | |
260 | ||
154dc91e | 261 | s_assert(client_p->preClient->auth.cid == 0); |
d3f6b808 | 262 | |
154dc91e EM |
263 | rb_free(client_p->preClient->auth.data); |
264 | rb_free(client_p->preClient->auth.reason); | |
7fc09bc4 | 265 | |
398b6a73 | 266 | rb_bh_free(pclient_heap, client_p->preClient); |
212380e3 AC |
267 | client_p->preClient = NULL; |
268 | } | |
269 | ||
270 | static void | |
271 | free_local_client(struct Client *client_p) | |
272 | { | |
273 | s_assert(NULL != client_p); | |
274 | s_assert(&me != client_p); | |
275 | ||
276 | if(client_p->localClient == NULL) | |
277 | return; | |
278 | ||
279 | /* | |
280 | * clean up extra sockets from P-lines which have been discarded. | |
281 | */ | |
282 | if(client_p->localClient->listener) | |
283 | { | |
284 | s_assert(0 < client_p->localClient->listener->ref_count); | |
285 | if(0 == --client_p->localClient->listener->ref_count | |
286 | && !client_p->localClient->listener->active) | |
287 | free_listener(client_p->localClient->listener); | |
288 | client_p->localClient->listener = 0; | |
289 | } | |
290 | ||
de7cf7e0 | 291 | client_release_connids(client_p); |
c6d72037 VY |
292 | if(client_p->localClient->F != NULL) |
293 | { | |
3b2ebd04 | 294 | rb_close(client_p->localClient->F); |
c6d72037 | 295 | } |
212380e3 AC |
296 | |
297 | if(client_p->localClient->passwd) | |
298 | { | |
299 | memset(client_p->localClient->passwd, 0, | |
300 | strlen(client_p->localClient->passwd)); | |
637c4932 | 301 | rb_free(client_p->localClient->passwd); |
212380e3 AC |
302 | } |
303 | ||
90afc118 | 304 | rb_free(client_p->localClient->auth_user); |
637c4932 VY |
305 | rb_free(client_p->localClient->challenge); |
306 | rb_free(client_p->localClient->fullcaps); | |
307 | rb_free(client_p->localClient->opername); | |
308 | rb_free(client_p->localClient->mangledhost); | |
0e52d0d4 JT |
309 | if (client_p->localClient->privset) |
310 | privilegeset_unref(client_p->localClient->privset); | |
212380e3 | 311 | |
c53ca1e0 AC |
312 | if (IsSSL(client_p)) |
313 | ssld_decrement_clicount(client_p->localClient->ssl_ctl); | |
55abcbb2 | 314 | |
c53ca1e0 AC |
315 | if (IsCapable(client_p, CAP_ZIP)) |
316 | ssld_decrement_clicount(client_p->localClient->z_ctl); | |
317 | ||
318 | if (client_p->localClient->ws_ctl != NULL) | |
319 | wsockd_decrement_clicount(client_p->localClient->ws_ctl); | |
c6d72037 | 320 | |
398b6a73 | 321 | rb_bh_free(lclient_heap, client_p->localClient); |
212380e3 AC |
322 | client_p->localClient = NULL; |
323 | } | |
324 | ||
325 | void | |
326 | free_client(struct Client *client_p) | |
327 | { | |
328 | s_assert(NULL != client_p); | |
329 | s_assert(&me != client_p); | |
330 | free_local_client(client_p); | |
331 | free_pre_client(client_p); | |
8eda114a | 332 | rb_free(client_p->certfp); |
398b6a73 | 333 | rb_bh_free(client_heap, client_p); |
212380e3 AC |
334 | } |
335 | ||
336 | /* | |
337 | * check_pings - go through the local client list and check activity | |
338 | * kill off stuff that should die | |
339 | * | |
340 | * inputs - NOT USED (from event) | |
341 | * output - next time_t when check_pings() should be called again | |
55abcbb2 | 342 | * side effects - |
212380e3 AC |
343 | * |
344 | * | |
345 | * A PING can be sent to clients as necessary. | |
346 | * | |
347 | * Client/Server ping outs are handled. | |
348 | */ | |
349 | ||
350 | /* | |
351 | * Addon from adrian. We used to call this after nextping seconds, | |
352 | * however I've changed it to run once a second. This is only for | |
353 | * PING timeouts, not K/etc-line checks (thanks dianora!). Having it | |
354 | * run once a second makes life a lot easier - when a new client connects | |
355 | * and they need a ping in 4 seconds, if nextping was set to 20 seconds | |
356 | * we end up waiting 20 seconds. This is stupid. :-) | |
357 | * I will optimise (hah!) check_pings() once I've finished working on | |
358 | * tidying up other network IO evilnesses. | |
359 | * -- adrian | |
360 | */ | |
361 | ||
362 | static void | |
363 | check_pings(void *notused) | |
364 | { | |
365 | check_pings_list(&lclient_list); | |
366 | check_pings_list(&serv_list); | |
367 | check_unknowns_list(&unknown_list); | |
368 | } | |
369 | ||
370 | /* | |
371 | * Check_pings_list() | |
372 | * | |
373 | * inputs - pointer to list to check | |
374 | * output - NONE | |
55abcbb2 | 375 | * side effects - |
212380e3 AC |
376 | */ |
377 | static void | |
330fc5c1 | 378 | check_pings_list(rb_dlink_list * list) |
212380e3 AC |
379 | { |
380 | char scratch[32]; /* way too generous but... */ | |
381 | struct Client *client_p; /* current local client_p being examined */ | |
382 | int ping = 0; /* ping time value from client */ | |
637c4932 | 383 | rb_dlink_node *ptr, *next_ptr; |
212380e3 | 384 | |
637c4932 | 385 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head) |
212380e3 AC |
386 | { |
387 | client_p = ptr->data; | |
388 | ||
212380e3 AC |
389 | if(!MyConnect(client_p) || IsDead(client_p)) |
390 | continue; | |
391 | ||
e2a98043 | 392 | ping = get_client_ping(client_p); |
212380e3 | 393 | |
e3354945 | 394 | if(ping < (rb_current_time() - client_p->localClient->lasttime)) |
212380e3 AC |
395 | { |
396 | /* | |
397 | * If the client/server hasnt talked to us in 2*ping seconds | |
398 | * and it has a ping time, then close its connection. | |
399 | */ | |
e3354945 | 400 | if(((rb_current_time() - client_p->localClient->lasttime) >= (2 * ping) |
212380e3 AC |
401 | && (client_p->flags & FLAGS_PINGSENT))) |
402 | { | |
e2a98043 | 403 | if(IsServer(client_p)) |
212380e3 | 404 | { |
e2a98043 | 405 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
212380e3 | 406 | "No response from %s, closing link", |
b3ebc7ab | 407 | client_p->name); |
212380e3 AC |
408 | ilog(L_SERVER, |
409 | "No response from %s, closing link", | |
410 | log_client_name(client_p, HIDE_IP)); | |
411 | } | |
5203cba5 | 412 | (void) snprintf(scratch, sizeof(scratch), |
212380e3 | 413 | "Ping timeout: %d seconds", |
e3354945 | 414 | (int) (rb_current_time() - client_p->localClient->lasttime)); |
212380e3 AC |
415 | |
416 | exit_client(client_p, client_p, &me, scratch); | |
417 | continue; | |
418 | } | |
419 | else if((client_p->flags & FLAGS_PINGSENT) == 0) | |
420 | { | |
421 | /* | |
422 | * if we havent PINGed the connection and we havent | |
423 | * heard from it in a while, PING it to make sure | |
424 | * it is still alive. | |
425 | */ | |
426 | client_p->flags |= FLAGS_PINGSENT; | |
427 | /* not nice but does the job */ | |
e3354945 | 428 | client_p->localClient->lasttime = rb_current_time() - ping; |
212380e3 AC |
429 | sendto_one(client_p, "PING :%s", me.name); |
430 | } | |
431 | } | |
432 | /* ping_timeout: */ | |
433 | ||
434 | } | |
435 | } | |
436 | ||
437 | /* | |
438 | * check_unknowns_list | |
439 | * | |
440 | * inputs - pointer to list of unknown clients | |
441 | * output - NONE | |
442 | * side effects - unknown clients get marked for termination after n seconds | |
443 | */ | |
444 | static void | |
330fc5c1 | 445 | check_unknowns_list(rb_dlink_list * list) |
212380e3 | 446 | { |
637c4932 | 447 | rb_dlink_node *ptr, *next_ptr; |
212380e3 | 448 | struct Client *client_p; |
35cf4c79 | 449 | int timeout; |
212380e3 | 450 | |
637c4932 | 451 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head) |
212380e3 AC |
452 | { |
453 | client_p = ptr->data; | |
454 | ||
455 | if(IsDead(client_p) || IsClosing(client_p)) | |
456 | continue; | |
457 | ||
d3f6b808 | 458 | /* Still querying with authd */ |
154dc91e | 459 | if(client_p->preClient != NULL && client_p->preClient->auth.cid != 0) |
6bb4fb83 JT |
460 | continue; |
461 | ||
212380e3 AC |
462 | /* |
463 | * Check UNKNOWN connections - if they have been in this state | |
464 | * for > 30s, close them. | |
465 | */ | |
466 | ||
35cf4c79 | 467 | timeout = IsAnyServer(client_p) ? ConfigFileEntry.connect_timeout : 30; |
e3354945 | 468 | if((rb_current_time() - client_p->localClient->firsttime) > timeout) |
35cf4c79 JT |
469 | { |
470 | if(IsAnyServer(client_p)) | |
471 | { | |
472 | sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL, | |
473 | "No response from %s, closing link", | |
b3ebc7ab | 474 | client_p->name); |
35cf4c79 JT |
475 | ilog(L_SERVER, |
476 | "No response from %s, closing link", | |
477 | log_client_name(client_p, HIDE_IP)); | |
478 | } | |
212380e3 | 479 | exit_client(client_p, client_p, &me, "Connection timed out"); |
35cf4c79 | 480 | } |
212380e3 AC |
481 | } |
482 | } | |
483 | ||
484 | static void | |
485 | notify_banned_client(struct Client *client_p, struct ConfItem *aconf, int ban) | |
486 | { | |
487 | static const char conn_closed[] = "Connection closed"; | |
488 | static const char d_lined[] = "D-lined"; | |
489 | static const char k_lined[] = "K-lined"; | |
212380e3 AC |
490 | const char *reason = NULL; |
491 | const char *exit_reason = conn_closed; | |
492 | ||
a12ad044 | 493 | if(ConfigFileEntry.kline_with_reason) |
212380e3 | 494 | { |
a12ad044 | 495 | reason = get_user_ban_reason(aconf); |
b52c2949 | 496 | exit_reason = reason; |
212380e3 AC |
497 | } |
498 | else | |
499 | { | |
405ae5ce | 500 | reason = aconf->status == D_LINED ? d_lined : k_lined; |
212380e3 AC |
501 | } |
502 | ||
503 | if(ban == D_LINED && !IsPerson(client_p)) | |
504 | sendto_one(client_p, "NOTICE DLINE :*** You have been D-lined"); | |
505 | else | |
506 | sendto_one(client_p, form_str(ERR_YOUREBANNEDCREEP), | |
507 | me.name, client_p->name, reason); | |
55abcbb2 KB |
508 | |
509 | exit_client(client_p, client_p, &me, | |
212380e3 AC |
510 | EmptyString(ConfigFileEntry.kline_reason) ? exit_reason : |
511 | ConfigFileEntry.kline_reason); | |
512 | } | |
513 | ||
514 | /* | |
515 | * check_banned_lines | |
516 | * inputs - NONE | |
517 | * output - NONE | |
170703fe | 518 | * side effects - Check all connections for a pending k/dline against the |
212380e3 AC |
519 | * client, exit the client if found. |
520 | */ | |
521 | void | |
522 | check_banned_lines(void) | |
523 | { | |
1bb1f1b0 JT |
524 | check_dlines(); |
525 | check_klines(); | |
526 | check_xlines(); | |
212380e3 AC |
527 | } |
528 | ||
529 | /* check_klines_event() | |
530 | * | |
531 | * inputs - | |
532 | * outputs - | |
533 | * side effects - check_klines() is called, kline_queued unset | |
534 | */ | |
535 | void | |
536 | check_klines_event(void *unused) | |
537 | { | |
f66f0baa | 538 | kline_queued = false; |
212380e3 AC |
539 | check_klines(); |
540 | } | |
541 | ||
542 | /* check_klines | |
543 | * | |
544 | * inputs - | |
545 | * outputs - | |
546 | * side effects - all clients will be checked for klines | |
547 | */ | |
548 | void | |
549 | check_klines(void) | |
550 | { | |
551 | struct Client *client_p; | |
552 | struct ConfItem *aconf; | |
330fc5c1 | 553 | rb_dlink_node *ptr; |
637c4932 | 554 | rb_dlink_node *next_ptr; |
212380e3 | 555 | |
637c4932 | 556 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head) |
212380e3 AC |
557 | { |
558 | client_p = ptr->data; | |
559 | ||
560 | if(IsMe(client_p) || !IsPerson(client_p)) | |
561 | continue; | |
562 | ||
563 | if((aconf = find_kline(client_p)) != NULL) | |
564 | { | |
565 | if(IsExemptKline(client_p)) | |
566 | { | |
567 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
408024fa JT |
568 | "KLINE over-ruled for %s, client is kline_exempt [%s@%s]", |
569 | get_client_name(client_p, HIDE_IP), | |
570 | aconf->user, aconf->host); | |
212380e3 AC |
571 | continue; |
572 | } | |
573 | ||
574 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
575 | "KLINE active for %s", | |
576 | get_client_name(client_p, HIDE_IP)); | |
577 | ||
578 | notify_banned_client(client_p, aconf, K_LINED); | |
579 | continue; | |
580 | } | |
581 | } | |
582 | } | |
583 | ||
212380e3 AC |
584 | /* check_dlines() |
585 | * | |
586 | * inputs - | |
587 | * outputs - | |
588 | * side effects - all clients will be checked for dlines | |
589 | */ | |
590 | void | |
591 | check_dlines(void) | |
592 | { | |
593 | struct Client *client_p; | |
594 | struct ConfItem *aconf; | |
330fc5c1 | 595 | rb_dlink_node *ptr; |
637c4932 | 596 | rb_dlink_node *next_ptr; |
212380e3 | 597 | |
637c4932 | 598 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head) |
212380e3 AC |
599 | { |
600 | client_p = ptr->data; | |
601 | ||
602 | if(IsMe(client_p)) | |
603 | continue; | |
604 | ||
e867208d | 605 | if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip, GET_SS_FAMILY(&client_p->localClient->ip))) != NULL) |
212380e3 AC |
606 | { |
607 | if(aconf->status & CONF_EXEMPTDLINE) | |
608 | continue; | |
609 | ||
610 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
611 | "DLINE active for %s", | |
612 | get_client_name(client_p, HIDE_IP)); | |
613 | ||
614 | notify_banned_client(client_p, aconf, D_LINED); | |
615 | continue; | |
616 | } | |
617 | } | |
618 | ||
619 | /* dlines need to be checked against unknowns too */ | |
637c4932 | 620 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head) |
212380e3 AC |
621 | { |
622 | client_p = ptr->data; | |
623 | ||
e867208d | 624 | if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip, GET_SS_FAMILY(&client_p->localClient->ip))) != NULL) |
212380e3 AC |
625 | { |
626 | if(aconf->status & CONF_EXEMPTDLINE) | |
627 | continue; | |
628 | ||
629 | notify_banned_client(client_p, aconf, D_LINED); | |
630 | } | |
631 | } | |
632 | } | |
633 | ||
634 | /* check_xlines | |
635 | * | |
636 | * inputs - | |
637 | * outputs - | |
638 | * side effects - all clients will be checked for xlines | |
639 | */ | |
640 | void | |
641 | check_xlines(void) | |
642 | { | |
643 | struct Client *client_p; | |
644 | struct ConfItem *aconf; | |
330fc5c1 | 645 | rb_dlink_node *ptr; |
637c4932 | 646 | rb_dlink_node *next_ptr; |
212380e3 | 647 | |
637c4932 | 648 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head) |
212380e3 AC |
649 | { |
650 | client_p = ptr->data; | |
651 | ||
652 | if(IsMe(client_p) || !IsPerson(client_p)) | |
653 | continue; | |
654 | ||
655 | if((aconf = find_xline(client_p->info, 1)) != NULL) | |
656 | { | |
657 | if(IsExemptKline(client_p)) | |
658 | { | |
659 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
408024fa JT |
660 | "XLINE over-ruled for %s, client is kline_exempt [%s]", |
661 | get_client_name(client_p, HIDE_IP), | |
70ea02eb | 662 | aconf->host); |
212380e3 AC |
663 | continue; |
664 | } | |
665 | ||
666 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "XLINE active for %s", | |
667 | get_client_name(client_p, HIDE_IP)); | |
668 | ||
669 | (void) exit_client(client_p, client_p, &me, "Bad user info"); | |
670 | continue; | |
671 | } | |
672 | } | |
673 | } | |
674 | ||
330692a1 KB |
675 | /* resv_nick_fnc |
676 | * | |
677 | * inputs - resv, reason, time | |
678 | * outputs - NONE | |
679 | * side effects - all local clients matching resv will be FNC'd | |
680 | */ | |
681 | void | |
682 | resv_nick_fnc(const char *mask, const char *reason, int temp_time) | |
683 | { | |
684 | struct Client *client_p, *target_p; | |
685 | rb_dlink_node *ptr; | |
686 | rb_dlink_node *next_ptr; | |
687 | char *nick; | |
688 | char note[NICKLEN+10]; | |
689 | ||
690 | if (!ConfigFileEntry.resv_fnc) | |
691 | return; | |
692 | ||
693 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head) | |
694 | { | |
695 | client_p = ptr->data; | |
696 | ||
697 | if(IsMe(client_p) || !IsPerson(client_p) || IsExemptResv(client_p)) | |
698 | continue; | |
699 | ||
07d3283f KB |
700 | /* Skip users that already have UID nicks. */ |
701 | if(IsDigit(client_p->name[0])) | |
702 | continue; | |
703 | ||
330692a1 KB |
704 | if(match_esc(mask, client_p->name)) |
705 | { | |
706 | nick = client_p->id; | |
707 | ||
708 | /* Tell opers. */ | |
709 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
710 | "RESV forced nick change for %s!%s@%s to %s; nick matched [%s] (%s)", | |
711 | client_p->name, client_p->username, client_p->host, nick, mask, reason); | |
712 | ||
713 | sendto_realops_snomask(SNO_NCHANGE, L_ALL, | |
714 | "Nick change: From %s to %s [%s@%s]", | |
715 | client_p->name, nick, client_p->username, client_p->host); | |
716 | ||
717 | /* Tell the user. */ | |
718 | if (temp_time > 0) | |
719 | { | |
720 | sendto_one_notice(client_p, | |
721 | ":*** Nick %s is temporarily unavailable on this server.", | |
722 | client_p->name); | |
723 | } | |
724 | else | |
725 | { | |
726 | sendto_one_notice(client_p, | |
727 | ":*** Nick %s is no longer available on this server.", | |
728 | client_p->name); | |
729 | } | |
730 | ||
731 | /* Do all of the nick-changing gymnastics. */ | |
732 | client_p->tsinfo = rb_current_time(); | |
b47f8a4f | 733 | whowas_add_history(client_p, 1); |
330692a1 | 734 | |
07d3283f KB |
735 | monitor_signoff(client_p); |
736 | ||
330692a1 KB |
737 | invalidate_bancache_user(client_p); |
738 | ||
583f064f | 739 | sendto_common_channels_local(client_p, NOCAPS, NOCAPS, ":%s!%s@%s NICK :%s", |
330692a1 KB |
740 | client_p->name, client_p->username, client_p->host, nick); |
741 | sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld", | |
742 | use_id(client_p), nick, (long) client_p->tsinfo); | |
743 | ||
744 | del_from_client_hash(client_p->name, client_p); | |
745 | rb_strlcpy(client_p->name, nick, sizeof(client_p->name)); | |
746 | add_to_client_hash(nick, client_p); | |
747 | ||
07d3283f KB |
748 | monitor_signon(client_p); |
749 | ||
330692a1 KB |
750 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head) |
751 | { | |
752 | target_p = ptr->data; | |
753 | rb_dlinkFindDestroy(client_p, &target_p->localClient->allow_list); | |
754 | rb_dlinkDestroy(ptr, &client_p->on_allow_list); | |
755 | } | |
756 | ||
5203cba5 | 757 | snprintf(note, sizeof(note), "Nick: %s", nick); |
330692a1 KB |
758 | rb_note(client_p->localClient->F, note); |
759 | } | |
760 | } | |
761 | } | |
762 | ||
212380e3 AC |
763 | /* |
764 | * update_client_exit_stats | |
765 | * | |
766 | * input - pointer to client | |
767 | * output - NONE | |
55abcbb2 | 768 | * side effects - |
212380e3 AC |
769 | */ |
770 | static void | |
771 | update_client_exit_stats(struct Client *client_p) | |
772 | { | |
773 | if(IsServer(client_p)) | |
774 | { | |
775 | sendto_realops_snomask(SNO_EXTERNAL, L_ALL, | |
776 | "Server %s split from %s", | |
777 | client_p->name, client_p->servptr->name); | |
778 | if(HasSentEob(client_p)) | |
779 | eob_count--; | |
780 | } | |
781 | else if(IsClient(client_p)) | |
782 | { | |
783 | --Count.total; | |
784 | if(IsOper(client_p)) | |
785 | --Count.oper; | |
786 | if(IsInvisible(client_p)) | |
787 | --Count.invisi; | |
788 | } | |
789 | ||
790 | if(splitchecking && !splitmode) | |
791 | check_splitmode(NULL); | |
792 | } | |
793 | ||
794 | /* | |
795 | * release_client_state | |
796 | * | |
797 | * input - pointer to client to release | |
798 | * output - NONE | |
55abcbb2 | 799 | * side effects - |
212380e3 AC |
800 | */ |
801 | static void | |
802 | release_client_state(struct Client *client_p) | |
803 | { | |
804 | if(client_p->user != NULL) | |
805 | { | |
806 | free_user(client_p->user, client_p); /* try this here */ | |
807 | } | |
808 | if(client_p->serv) | |
809 | { | |
810 | if(client_p->serv->user != NULL) | |
811 | free_user(client_p->serv->user, client_p); | |
812 | if(client_p->serv->fullcaps) | |
637c4932 VY |
813 | rb_free(client_p->serv->fullcaps); |
814 | rb_free(client_p->serv); | |
212380e3 AC |
815 | } |
816 | } | |
817 | ||
818 | /* | |
819 | * remove_client_from_list | |
820 | * inputs - point to client to remove | |
821 | * output - NONE | |
822 | * side effects - taken the code from ExitOneClient() for this | |
823 | * and placed it here. - avalon | |
824 | */ | |
825 | static void | |
826 | remove_client_from_list(struct Client *client_p) | |
827 | { | |
828 | s_assert(NULL != client_p); | |
829 | ||
830 | if(client_p == NULL) | |
831 | return; | |
832 | ||
833 | /* A client made with make_client() | |
834 | * is on the unknown_list until removed. | |
835 | * If it =does= happen to exit before its removed from that list | |
836 | * and its =not= on the global_client_list, it will core here. | |
837 | * short circuit that case now -db | |
838 | */ | |
839 | if(client_p->node.prev == NULL && client_p->node.next == NULL) | |
840 | return; | |
841 | ||
330fc5c1 | 842 | rb_dlinkDelete(&client_p->node, &global_client_list); |
212380e3 AC |
843 | |
844 | update_client_exit_stats(client_p); | |
845 | } | |
846 | ||
847 | ||
2d28539c JT |
848 | /* clean_nick() |
849 | * | |
850 | * input - nickname to check, flag for nick from local client | |
851 | * output - 0 if erroneous, else 1 | |
852 | * side effects - | |
853 | */ | |
854 | int | |
855 | clean_nick(const char *nick, int loc_client) | |
856 | { | |
857 | int len = 0; | |
858 | ||
859 | /* nicks cant start with a digit or -, and must have a length */ | |
860 | if(*nick == '-' || *nick == '\0') | |
861 | return 0; | |
862 | ||
863 | if(loc_client && IsDigit(*nick)) | |
864 | return 0; | |
865 | ||
866 | for(; *nick; nick++) | |
867 | { | |
868 | len++; | |
869 | if(!IsNickChar(*nick)) | |
870 | return 0; | |
871 | } | |
872 | ||
873 | /* nicklen is +1 */ | |
874 | if(len >= NICKLEN && (unsigned int)len >= ConfigFileEntry.nicklen) | |
875 | return 0; | |
876 | ||
877 | return 1; | |
878 | } | |
879 | ||
212380e3 AC |
880 | /* |
881 | * find_person - find person by (nick)name. | |
882 | * inputs - pointer to name | |
883 | * output - return client pointer | |
884 | * side effects - | |
885 | */ | |
886 | struct Client * | |
887 | find_person(const char *name) | |
888 | { | |
889 | struct Client *c2ptr; | |
890 | ||
891 | c2ptr = find_client(name); | |
892 | ||
893 | if(c2ptr && IsPerson(c2ptr)) | |
894 | return (c2ptr); | |
895 | return (NULL); | |
896 | } | |
897 | ||
898 | struct Client * | |
899 | find_named_person(const char *name) | |
900 | { | |
901 | struct Client *c2ptr; | |
902 | ||
903 | c2ptr = find_named_client(name); | |
904 | ||
905 | if(c2ptr && IsPerson(c2ptr)) | |
906 | return (c2ptr); | |
907 | return (NULL); | |
908 | } | |
909 | ||
910 | ||
911 | /* | |
55abcbb2 KB |
912 | * find_chasing - find the client structure for a nick name (user) |
913 | * using history mechanism if necessary. If the client is not found, | |
212380e3 AC |
914 | * an error message (NO SUCH NICK) is generated. If the client was found |
915 | * through the history, chasing will be 1 and otherwise 0. | |
916 | */ | |
917 | struct Client * | |
918 | find_chasing(struct Client *source_p, const char *user, int *chasing) | |
919 | { | |
920 | struct Client *who; | |
921 | ||
922 | if(MyClient(source_p)) | |
923 | who = find_named_person(user); | |
924 | else | |
925 | who = find_person(user); | |
926 | ||
927 | if(chasing) | |
928 | *chasing = 0; | |
929 | ||
930 | if(who || IsDigit(*user)) | |
931 | return who; | |
932 | ||
b47f8a4f | 933 | if(!(who = whowas_get_history(user, (long) KILLCHASETIMELIMIT))) |
212380e3 AC |
934 | { |
935 | sendto_one_numeric(source_p, ERR_NOSUCHNICK, | |
936 | form_str(ERR_NOSUCHNICK), user); | |
937 | return (NULL); | |
938 | } | |
939 | if(chasing) | |
940 | *chasing = 1; | |
941 | return who; | |
942 | } | |
943 | ||
944 | /* | |
945 | * get_client_name - Return the name of the client | |
946 | * for various tracking and | |
947 | * admin purposes. The main purpose of this function is to | |
948 | * return the "socket host" name of the client, if that | |
949 | * differs from the advertised name (other than case). | |
950 | * But, this can be used to any client structure. | |
951 | * | |
952 | * NOTE 1: | |
953 | * Watch out the allocation of "nbuf", if either source_p->name | |
954 | * or source_p->sockhost gets changed into pointers instead of | |
955 | * directly allocated within the structure... | |
956 | * | |
957 | * NOTE 2: | |
958 | * Function return either a pointer to the structure (source_p) or | |
959 | * to internal buffer (nbuf). *NEVER* use the returned pointer | |
960 | * to modify what it points!!! | |
961 | */ | |
962 | ||
963 | const char * | |
964 | get_client_name(struct Client *client, int showip) | |
965 | { | |
966 | static char nbuf[HOSTLEN * 2 + USERLEN + 5]; | |
967 | ||
968 | s_assert(NULL != client); | |
969 | if(client == NULL) | |
970 | return NULL; | |
971 | ||
972 | if(MyConnect(client)) | |
973 | { | |
974 | if(!irccmp(client->name, client->host)) | |
975 | return client->name; | |
976 | ||
55abcbb2 | 977 | if(ConfigFileEntry.hide_spoof_ips && |
212380e3 AC |
978 | showip == SHOW_IP && IsIPSpoof(client)) |
979 | showip = MASK_IP; | |
212380e3 AC |
980 | if(IsAnyServer(client)) |
981 | showip = MASK_IP; | |
212380e3 AC |
982 | |
983 | /* And finally, let's get the host information, ip or name */ | |
984 | switch (showip) | |
985 | { | |
986 | case SHOW_IP: | |
5203cba5 | 987 | snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", |
55abcbb2 | 988 | client->name, client->username, |
212380e3 AC |
989 | client->sockhost); |
990 | break; | |
991 | case MASK_IP: | |
5203cba5 | 992 | snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]", |
212380e3 AC |
993 | client->name, client->username); |
994 | break; | |
995 | default: | |
5203cba5 | 996 | snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", |
212380e3 AC |
997 | client->name, client->username, client->host); |
998 | } | |
999 | return nbuf; | |
1000 | } | |
1001 | ||
55abcbb2 | 1002 | /* As pointed out by Adel Mezibra |
212380e3 AC |
1003 | * Neph|l|m@EFnet. Was missing a return here. |
1004 | */ | |
1005 | return client->name; | |
1006 | } | |
55abcbb2 | 1007 | |
212380e3 AC |
1008 | /* log_client_name() |
1009 | * | |
1010 | * This version is the same as get_client_name, but doesnt contain the | |
1011 | * code that will hide IPs always. This should be used for logfiles. | |
1012 | */ | |
1013 | const char * | |
1014 | log_client_name(struct Client *target_p, int showip) | |
1015 | { | |
1016 | static char nbuf[HOSTLEN * 2 + USERLEN + 5]; | |
1017 | ||
1018 | if(target_p == NULL) | |
1019 | return NULL; | |
1020 | ||
1021 | if(MyConnect(target_p)) | |
1022 | { | |
1023 | if(irccmp(target_p->name, target_p->host) == 0) | |
1024 | return target_p->name; | |
1025 | ||
1026 | switch (showip) | |
1027 | { | |
1028 | case SHOW_IP: | |
5203cba5 | 1029 | snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", target_p->name, |
212380e3 AC |
1030 | target_p->username, target_p->sockhost); |
1031 | break; | |
1032 | ||
212380e3 | 1033 | default: |
5203cba5 | 1034 | snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", target_p->name, |
212380e3 AC |
1035 | target_p->username, target_p->host); |
1036 | } | |
1037 | ||
1038 | return nbuf; | |
1039 | } | |
1040 | ||
1041 | return target_p->name; | |
1042 | } | |
1043 | ||
1044 | /* is_remote_connect - Returns whether a server was /connect'ed by a remote | |
1045 | * oper (send notices netwide) */ | |
1046 | int | |
1047 | is_remote_connect(struct Client *client_p) | |
1048 | { | |
1049 | struct Client *oper; | |
1050 | ||
1051 | if (client_p->serv == NULL) | |
1052 | return FALSE; | |
1053 | oper = find_named_person(client_p->serv->by); | |
1054 | return oper != NULL && IsOper(oper) && !MyConnect(oper); | |
1055 | } | |
1056 | ||
1057 | static void | |
1058 | free_exited_clients(void *unused) | |
1059 | { | |
330fc5c1 | 1060 | rb_dlink_node *ptr, *next; |
212380e3 AC |
1061 | struct Client *target_p; |
1062 | ||
5cefa1d6 | 1063 | RB_DLINK_FOREACH_SAFE(ptr, next, dead_list.head) |
212380e3 AC |
1064 | { |
1065 | target_p = ptr->data; | |
1066 | ||
1067 | #ifdef DEBUG_EXITED_CLIENTS | |
1068 | { | |
1069 | struct abort_client *abt; | |
330fc5c1 | 1070 | rb_dlink_node *aptr; |
212380e3 AC |
1071 | int found = 0; |
1072 | ||
5cefa1d6 | 1073 | RB_DLINK_FOREACH(aptr, abort_list.head) |
212380e3 AC |
1074 | { |
1075 | abt = aptr->data; | |
1076 | if(abt->client == target_p) | |
1077 | { | |
1078 | s_assert(0); | |
55abcbb2 | 1079 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
66f7fe67 | 1080 | "On abort_list: %s stat: %u flags: %llu handler: %c", |
212380e3 | 1081 | target_p->name, (unsigned int) target_p->status, |
d19aab33 | 1082 | (unsigned long long)target_p->flags, target_p->handler); |
212380e3 | 1083 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
f80a1823 | 1084 | "Please report this to the charybdis developers!"); |
212380e3 AC |
1085 | found++; |
1086 | } | |
1087 | } | |
1088 | ||
1089 | if(found) | |
1090 | { | |
330fc5c1 | 1091 | rb_dlinkDestroy(ptr, &dead_list); |
212380e3 AC |
1092 | continue; |
1093 | } | |
1094 | } | |
1095 | #endif | |
1096 | ||
1097 | if(ptr->data == NULL) | |
1098 | { | |
1099 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
1100 | "Warning: null client on dead_list!"); | |
330fc5c1 | 1101 | rb_dlinkDestroy(ptr, &dead_list); |
212380e3 AC |
1102 | continue; |
1103 | } | |
1104 | release_client_state(target_p); | |
1105 | free_client(target_p); | |
330fc5c1 | 1106 | rb_dlinkDestroy(ptr, &dead_list); |
212380e3 AC |
1107 | } |
1108 | ||
1109 | #ifdef DEBUG_EXITED_CLIENTS | |
5cefa1d6 | 1110 | RB_DLINK_FOREACH_SAFE(ptr, next, dead_remote_list.head) |
212380e3 AC |
1111 | { |
1112 | target_p = ptr->data; | |
1113 | ||
1114 | if(ptr->data == NULL) | |
1115 | { | |
1116 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
1117 | "Warning: null client on dead_list!"); | |
330fc5c1 | 1118 | rb_dlinkDestroy(ptr, &dead_list); |
212380e3 AC |
1119 | continue; |
1120 | } | |
1121 | release_client_state(target_p); | |
1122 | free_client(target_p); | |
330fc5c1 | 1123 | rb_dlinkDestroy(ptr, &dead_remote_list); |
212380e3 AC |
1124 | } |
1125 | #endif | |
55abcbb2 | 1126 | |
212380e3 AC |
1127 | } |
1128 | ||
55abcbb2 | 1129 | /* |
212380e3 | 1130 | ** Remove all clients that depend on source_p; assumes all (S)QUITs have |
55abcbb2 KB |
1131 | ** already been sent. we make sure to exit a server's dependent clients |
1132 | ** and servers before the server itself; exit_one_client takes care of | |
212380e3 | 1133 | ** actually removing things off llists. tweaked from +CSr31 -orabidoo |
cbeab4bc | 1134 | */ |
212380e3 AC |
1135 | /* |
1136 | * added sanity test code.... source_p->serv might be NULL... | |
1137 | */ | |
1138 | static void | |
1139 | recurse_remove_clients(struct Client *source_p, const char *comment) | |
1140 | { | |
1141 | struct Client *target_p; | |
330fc5c1 | 1142 | rb_dlink_node *ptr, *ptr_next; |
212380e3 AC |
1143 | |
1144 | if(IsMe(source_p)) | |
1145 | return; | |
1146 | ||
1147 | if(source_p->serv == NULL) /* oooops. uh this is actually a major bug */ | |
1148 | return; | |
1149 | ||
1150 | /* this is very ugly, but it saves cpu :P */ | |
1151 | if(ConfigFileEntry.nick_delay > 0) | |
1152 | { | |
5cefa1d6 | 1153 | RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head) |
212380e3 AC |
1154 | { |
1155 | target_p = ptr->data; | |
1156 | target_p->flags |= FLAGS_KILLED; | |
1157 | add_nd_entry(target_p->name); | |
1158 | ||
1159 | if(!IsDead(target_p) && !IsClosing(target_p)) | |
1160 | exit_remote_client(NULL, target_p, &me, comment); | |
1161 | } | |
1162 | } | |
1163 | else | |
1164 | { | |
5cefa1d6 | 1165 | RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head) |
212380e3 AC |
1166 | { |
1167 | target_p = ptr->data; | |
1168 | target_p->flags |= FLAGS_KILLED; | |
1169 | ||
1170 | if(!IsDead(target_p) && !IsClosing(target_p)) | |
1171 | exit_remote_client(NULL, target_p, &me, comment); | |
1172 | } | |
55abcbb2 | 1173 | } |
212380e3 | 1174 | |
5cefa1d6 | 1175 | RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head) |
212380e3 AC |
1176 | { |
1177 | target_p = ptr->data; | |
1178 | recurse_remove_clients(target_p, comment); | |
1179 | qs_server(NULL, target_p, &me, comment); | |
1180 | } | |
1181 | } | |
1182 | ||
1183 | /* | |
1184 | ** Remove *everything* that depends on source_p, from all lists, and sending | |
856df9a3 | 1185 | ** all necessary SQUITs. source_p itself is still on the lists, |
212380e3 | 1186 | ** and its SQUITs have been sent except for the upstream one -orabidoo |
cbeab4bc | 1187 | */ |
212380e3 AC |
1188 | static void |
1189 | remove_dependents(struct Client *client_p, | |
1190 | struct Client *source_p, | |
1191 | struct Client *from, const char *comment, const char *comment1) | |
1192 | { | |
1193 | struct Client *to; | |
330fc5c1 | 1194 | rb_dlink_node *ptr, *next; |
212380e3 | 1195 | |
5cefa1d6 | 1196 | RB_DLINK_FOREACH_SAFE(ptr, next, serv_list.head) |
212380e3 AC |
1197 | { |
1198 | to = ptr->data; | |
1199 | ||
856df9a3 | 1200 | if(IsMe(to) || to == source_p->from || to == client_p) |
212380e3 AC |
1201 | continue; |
1202 | ||
856df9a3 | 1203 | sendto_one(to, "SQUIT %s :%s", get_id(source_p, to), comment); |
212380e3 AC |
1204 | } |
1205 | ||
1206 | recurse_remove_clients(source_p, comment1); | |
1207 | } | |
1208 | ||
1209 | void | |
1210 | exit_aborted_clients(void *unused) | |
1211 | { | |
1212 | struct abort_client *abt; | |
330fc5c1 | 1213 | rb_dlink_node *ptr, *next; |
5cefa1d6 | 1214 | RB_DLINK_FOREACH_SAFE(ptr, next, abort_list.head) |
212380e3 AC |
1215 | { |
1216 | abt = ptr->data; | |
1217 | ||
1218 | #ifdef DEBUG_EXITED_CLIENTS | |
1219 | { | |
330fc5c1 | 1220 | if(rb_dlinkFind(abt->client, &dead_list)) |
212380e3 AC |
1221 | { |
1222 | s_assert(0); | |
55abcbb2 | 1223 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
66f7fe67 | 1224 | "On dead_list: %s stat: %u flags: %llu handler: %c", |
212380e3 | 1225 | abt->client->name, (unsigned int) abt->client->status, |
d19aab33 | 1226 | (unsigned long long)abt->client->flags, abt->client->handler); |
212380e3 | 1227 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
f80a1823 | 1228 | "Please report this to the charybdis developers!"); |
212380e3 AC |
1229 | continue; |
1230 | } | |
1231 | } | |
1232 | #endif | |
1233 | ||
1234 | s_assert(*((unsigned long*)abt->client) != 0xdeadbeef); /* This is lame but its a debug thing */ | |
330fc5c1 | 1235 | rb_dlinkDelete(ptr, &abort_list); |
212380e3 AC |
1236 | |
1237 | if(IsAnyServer(abt->client)) | |
1238 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
1239 | "Closing link to %s: %s", | |
b3ebc7ab | 1240 | abt->client->name, abt->notice); |
212380e3 AC |
1241 | |
1242 | /* its no longer on abort list - we *must* remove | |
1243 | * FLAGS_CLOSING otherwise exit_client() will not run --fl | |
1244 | */ | |
1245 | abt->client->flags &= ~FLAGS_CLOSING; | |
1246 | exit_client(abt->client, abt->client, &me, abt->notice); | |
637c4932 | 1247 | rb_free(abt); |
212380e3 AC |
1248 | } |
1249 | } | |
1250 | ||
1251 | ||
1252 | /* | |
1253 | * dead_link - Adds client to a list of clients that need an exit_client() | |
212380e3 AC |
1254 | */ |
1255 | void | |
cef7a7bc | 1256 | dead_link(struct Client *client_p, int sendqex) |
212380e3 AC |
1257 | { |
1258 | struct abort_client *abt; | |
1259 | ||
1260 | s_assert(!IsMe(client_p)); | |
1261 | if(IsDead(client_p) || IsClosing(client_p) || IsMe(client_p)) | |
1262 | return; | |
1263 | ||
eddc2ab6 | 1264 | abt = (struct abort_client *) rb_malloc(sizeof(struct abort_client)); |
212380e3 | 1265 | |
cef7a7bc | 1266 | if(sendqex) |
f427c8b0 | 1267 | rb_strlcpy(abt->notice, "Max SendQ exceeded", sizeof(abt->notice)); |
212380e3 | 1268 | else |
5203cba5 | 1269 | snprintf(abt->notice, sizeof(abt->notice), "Write error: %s", strerror(errno)); |
212380e3 AC |
1270 | |
1271 | abt->client = client_p; | |
1272 | SetIOError(client_p); | |
1273 | SetDead(client_p); | |
1274 | SetClosing(client_p); | |
330fc5c1 | 1275 | rb_dlinkAdd(abt, &abt->node, &abort_list); |
212380e3 AC |
1276 | } |
1277 | ||
1278 | ||
1279 | /* This does the remove of the user from channels..local or remote */ | |
1280 | static inline void | |
1281 | exit_generic_client(struct Client *client_p, struct Client *source_p, struct Client *from, | |
1282 | const char *comment) | |
1283 | { | |
637c4932 | 1284 | rb_dlink_node *ptr, *next_ptr; |
212380e3 AC |
1285 | |
1286 | if(IsOper(source_p)) | |
330fc5c1 | 1287 | rb_dlinkFindDestroy(source_p, &oper_list); |
212380e3 | 1288 | |
583f064f | 1289 | sendto_common_channels_local(source_p, NOCAPS, NOCAPS, ":%s!%s@%s QUIT :%s", |
212380e3 AC |
1290 | source_p->name, |
1291 | source_p->username, source_p->host, comment); | |
1292 | ||
1293 | remove_user_from_channels(source_p); | |
1294 | ||
1295 | /* Should not be in any channels now */ | |
1296 | s_assert(source_p->user->channel.head == NULL); | |
1297 | ||
1298 | /* Clean up invitefield */ | |
637c4932 | 1299 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, source_p->user->invited.head) |
212380e3 AC |
1300 | { |
1301 | del_invite(ptr->data, source_p); | |
1302 | } | |
1303 | ||
1304 | /* Clean up allow lists */ | |
1305 | del_all_accepts(source_p); | |
1306 | ||
b47f8a4f AC |
1307 | whowas_add_history(source_p, 0); |
1308 | whowas_off_history(source_p); | |
212380e3 AC |
1309 | |
1310 | monitor_signoff(source_p); | |
1311 | ||
1312 | if(has_id(source_p)) | |
1313 | del_from_id_hash(source_p->id, source_p); | |
1314 | ||
1315 | del_from_hostname_hash(source_p->orighost, source_p); | |
1316 | del_from_client_hash(source_p->name, source_p); | |
1317 | remove_client_from_list(source_p); | |
1318 | } | |
1319 | ||
55abcbb2 | 1320 | /* |
212380e3 AC |
1321 | * Assumes IsPerson(source_p) && !MyConnect(source_p) |
1322 | */ | |
1323 | ||
1324 | static int | |
1325 | exit_remote_client(struct Client *client_p, struct Client *source_p, struct Client *from, | |
1326 | const char *comment) | |
1327 | { | |
1328 | exit_generic_client(client_p, source_p, from, comment); | |
55abcbb2 | 1329 | |
212380e3 AC |
1330 | if(source_p->servptr && source_p->servptr->serv) |
1331 | { | |
330fc5c1 | 1332 | rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->users); |
212380e3 AC |
1333 | } |
1334 | ||
1335 | if((source_p->flags & FLAGS_KILLED) == 0) | |
1336 | { | |
1337 | sendto_server(client_p, NULL, CAP_TS6, NOCAPS, | |
1338 | ":%s QUIT :%s", use_id(source_p), comment); | |
212380e3 AC |
1339 | } |
1340 | ||
1341 | SetDead(source_p); | |
1342 | #ifdef DEBUG_EXITED_CLIENTS | |
330fc5c1 | 1343 | rb_dlinkAddAlloc(source_p, &dead_remote_list); |
212380e3 | 1344 | #else |
330fc5c1 | 1345 | rb_dlinkAddAlloc(source_p, &dead_list); |
212380e3 AC |
1346 | #endif |
1347 | return(CLIENT_EXITED); | |
1348 | } | |
1349 | ||
1350 | /* | |
07554369 | 1351 | * This assumes IsUnknown(source_p) == true and MyConnect(source_p) == true |
212380e3 AC |
1352 | */ |
1353 | ||
1354 | static int | |
1355 | exit_unknown_client(struct Client *client_p, struct Client *source_p, struct Client *from, | |
1356 | const char *comment) | |
1357 | { | |
d3f6b808 | 1358 | authd_abort_client(client_p); |
330fc5c1 | 1359 | rb_dlinkDelete(&source_p->localClient->tnode, &unknown_list); |
212380e3 AC |
1360 | |
1361 | if(!IsIOError(source_p)) | |
dd12a19c JT |
1362 | sendto_one(source_p, "ERROR :Closing Link: %s (%s)", |
1363 | source_p->user != NULL ? source_p->host : "127.0.0.1", | |
1364 | comment); | |
212380e3 AC |
1365 | |
1366 | close_connection(source_p); | |
1367 | ||
1368 | if(has_id(source_p)) | |
1369 | del_from_id_hash(source_p->id, source_p); | |
1370 | ||
1371 | del_from_hostname_hash(source_p->host, source_p); | |
1372 | del_from_client_hash(source_p->name, source_p); | |
1373 | remove_client_from_list(source_p); | |
212380e3 | 1374 | SetDead(source_p); |
330fc5c1 | 1375 | rb_dlinkAddAlloc(source_p, &dead_list); |
212380e3 AC |
1376 | |
1377 | /* Note that we don't need to add unknowns to the dead_list */ | |
1378 | return(CLIENT_EXITED); | |
1379 | } | |
1380 | ||
1381 | static int | |
55abcbb2 | 1382 | exit_remote_server(struct Client *client_p, struct Client *source_p, struct Client *from, |
212380e3 AC |
1383 | const char *comment) |
1384 | { | |
1385 | static char comment1[(HOSTLEN*2)+2]; | |
1386 | static char newcomment[BUFSIZE]; | |
1387 | struct Client *target_p; | |
55abcbb2 | 1388 | |
212380e3 AC |
1389 | if(ConfigServerHide.flatten_links) |
1390 | strcpy(comment1, "*.net *.split"); | |
1391 | else | |
1392 | { | |
66c8fdd2 | 1393 | strcpy(comment1, source_p->servptr->name); |
212380e3 AC |
1394 | strcat(comment1, " "); |
1395 | strcat(comment1, source_p->name); | |
1396 | } | |
1397 | if (IsPerson(from)) | |
5203cba5 | 1398 | snprintf(newcomment, sizeof(newcomment), "by %s: %s", |
212380e3 AC |
1399 | from->name, comment); |
1400 | ||
1401 | if(source_p->serv != NULL) | |
1402 | remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1); | |
1403 | ||
1404 | if(source_p->servptr && source_p->servptr->serv) | |
330fc5c1 | 1405 | rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers); |
212380e3 AC |
1406 | else |
1407 | s_assert(0); | |
1408 | ||
330fc5c1 | 1409 | rb_dlinkFindDestroy(source_p, &global_serv_list); |
212380e3 | 1410 | target_p = source_p->from; |
55abcbb2 | 1411 | |
212380e3 AC |
1412 | if(target_p != NULL && IsServer(target_p) && target_p != client_p && |
1413 | !IsMe(target_p) && (source_p->flags & FLAGS_KILLED) == 0) | |
1414 | { | |
55abcbb2 | 1415 | sendto_one(target_p, ":%s SQUIT %s :%s", |
212380e3 AC |
1416 | get_id(from, target_p), get_id(source_p, target_p), |
1417 | comment); | |
1418 | } | |
1419 | ||
1420 | if(has_id(source_p)) | |
1421 | del_from_id_hash(source_p->id, source_p); | |
1422 | ||
1423 | del_from_client_hash(source_p->name, source_p); | |
55abcbb2 | 1424 | remove_client_from_list(source_p); |
994544c2 | 1425 | scache_split(source_p->serv->nameinfo); |
55abcbb2 | 1426 | |
212380e3 AC |
1427 | SetDead(source_p); |
1428 | #ifdef DEBUG_EXITED_CLIENTS | |
330fc5c1 | 1429 | rb_dlinkAddAlloc(source_p, &dead_remote_list); |
212380e3 | 1430 | #else |
330fc5c1 | 1431 | rb_dlinkAddAlloc(source_p, &dead_list); |
212380e3 AC |
1432 | #endif |
1433 | return 0; | |
1434 | } | |
1435 | ||
1436 | static int | |
55abcbb2 | 1437 | qs_server(struct Client *client_p, struct Client *source_p, struct Client *from, |
212380e3 AC |
1438 | const char *comment) |
1439 | { | |
212380e3 | 1440 | if(source_p->servptr && source_p->servptr->serv) |
330fc5c1 | 1441 | rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers); |
212380e3 AC |
1442 | else |
1443 | s_assert(0); | |
1444 | ||
330fc5c1 | 1445 | rb_dlinkFindDestroy(source_p, &global_serv_list); |
55abcbb2 | 1446 | |
212380e3 AC |
1447 | if(has_id(source_p)) |
1448 | del_from_id_hash(source_p->id, source_p); | |
1449 | ||
1450 | del_from_client_hash(source_p->name, source_p); | |
55abcbb2 | 1451 | remove_client_from_list(source_p); |
19807b5b | 1452 | scache_split(source_p->serv->nameinfo); |
55abcbb2 | 1453 | |
212380e3 | 1454 | SetDead(source_p); |
55abcbb2 | 1455 | rb_dlinkAddAlloc(source_p, &dead_list); |
212380e3 AC |
1456 | return 0; |
1457 | } | |
1458 | ||
1459 | static int | |
55abcbb2 | 1460 | exit_local_server(struct Client *client_p, struct Client *source_p, struct Client *from, |
212380e3 AC |
1461 | const char *comment) |
1462 | { | |
1463 | static char comment1[(HOSTLEN*2)+2]; | |
1464 | static char newcomment[BUFSIZE]; | |
1465 | unsigned int sendk, recvk; | |
55abcbb2 | 1466 | |
330fc5c1 AC |
1467 | rb_dlinkDelete(&source_p->localClient->tnode, &serv_list); |
1468 | rb_dlinkFindDestroy(source_p, &global_serv_list); | |
55abcbb2 | 1469 | |
212380e3 AC |
1470 | sendk = source_p->localClient->sendK; |
1471 | recvk = source_p->localClient->receiveK; | |
1472 | ||
1473 | /* Always show source here, so the server notices show | |
1474 | * which side initiated the split -- jilles | |
1475 | */ | |
5203cba5 | 1476 | snprintf(newcomment, sizeof(newcomment), "by %s: %s", |
212380e3 AC |
1477 | from == source_p ? me.name : from->name, comment); |
1478 | if (!IsIOError(source_p)) | |
1479 | sendto_one(source_p, "SQUIT %s :%s", use_id(source_p), | |
1480 | newcomment); | |
1481 | if(client_p != NULL && source_p != client_p && !IsIOError(source_p)) | |
1482 | { | |
1483 | sendto_one(source_p, "ERROR :Closing Link: 127.0.0.1 %s (%s)", | |
1484 | source_p->name, comment); | |
1485 | } | |
55abcbb2 | 1486 | |
212380e3 | 1487 | if(source_p->servptr && source_p->servptr->serv) |
330fc5c1 | 1488 | rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers); |
212380e3 AC |
1489 | else |
1490 | s_assert(0); | |
1491 | ||
1492 | ||
1493 | close_connection(source_p); | |
55abcbb2 | 1494 | |
212380e3 AC |
1495 | if(ConfigServerHide.flatten_links) |
1496 | strcpy(comment1, "*.net *.split"); | |
1497 | else | |
1498 | { | |
66c8fdd2 | 1499 | strcpy(comment1, source_p->servptr->name); |
212380e3 AC |
1500 | strcat(comment1, " "); |
1501 | strcat(comment1, source_p->name); | |
1502 | } | |
1503 | ||
1504 | if(source_p->serv != NULL) | |
1505 | remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1); | |
1506 | ||
1507 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s was connected" | |
1508 | " for %ld seconds. %d/%d sendK/recvK.", | |
adc6cc42 | 1509 | source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk); |
212380e3 AC |
1510 | |
1511 | ilog(L_SERVER, "%s was connected for %ld seconds. %d/%d sendK/recvK.", | |
adc6cc42 | 1512 | source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk); |
55abcbb2 | 1513 | |
212380e3 AC |
1514 | if(has_id(source_p)) |
1515 | del_from_id_hash(source_p->id, source_p); | |
1516 | ||
1517 | del_from_client_hash(source_p->name, source_p); | |
1518 | remove_client_from_list(source_p); | |
994544c2 | 1519 | scache_split(source_p->serv->nameinfo); |
55abcbb2 | 1520 | |
212380e3 | 1521 | SetDead(source_p); |
330fc5c1 | 1522 | rb_dlinkAddAlloc(source_p, &dead_list); |
212380e3 AC |
1523 | return 0; |
1524 | } | |
1525 | ||
1526 | ||
55abcbb2 | 1527 | /* |
07554369 | 1528 | * This assumes IsPerson(source_p) == true && MyConnect(source_p) == true |
212380e3 AC |
1529 | */ |
1530 | ||
1531 | static int | |
1532 | exit_local_client(struct Client *client_p, struct Client *source_p, struct Client *from, | |
1533 | const char *comment) | |
1534 | { | |
1535 | unsigned long on_for; | |
08d75d97 | 1536 | char tbuf[26]; |
212380e3 AC |
1537 | |
1538 | exit_generic_client(client_p, source_p, from, comment); | |
1539 | clear_monitor(source_p); | |
1540 | ||
1541 | s_assert(IsPerson(source_p)); | |
330fc5c1 AC |
1542 | rb_dlinkDelete(&source_p->localClient->tnode, &lclient_list); |
1543 | rb_dlinkDelete(&source_p->lnode, &me.serv->users); | |
212380e3 AC |
1544 | |
1545 | if(IsOper(source_p)) | |
330fc5c1 | 1546 | rb_dlinkFindDestroy(source_p, &local_oper_list); |
212380e3 AC |
1547 | |
1548 | sendto_realops_snomask(SNO_CCONN, L_ALL, | |
1549 | "Client exiting: %s (%s@%s) [%s] [%s]", | |
1550 | source_p->name, | |
1551 | source_p->username, source_p->host, comment, | |
1552 | show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255"); | |
1553 | ||
1554 | sendto_realops_snomask(SNO_CCONNEXT, L_ALL, | |
1555 | "CLIEXIT %s %s %s %s 0 %s", | |
1556 | source_p->name, source_p->username, source_p->host, | |
1557 | show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255", | |
1558 | comment); | |
1559 | ||
e3354945 | 1560 | on_for = rb_current_time() - source_p->localClient->firsttime; |
212380e3 | 1561 | |
9641f156 | 1562 | ilog(L_USER, "%s (%3lu:%02lu:%02lu): %s!%s@%s %s %d/%d", |
08d75d97 | 1563 | rb_ctime(rb_current_time(), tbuf, sizeof(tbuf)), on_for / 3600, |
212380e3 AC |
1564 | (on_for % 3600) / 60, on_for % 60, |
1565 | source_p->name, source_p->username, source_p->host, | |
9641f156 | 1566 | source_p->sockhost, |
212380e3 AC |
1567 | source_p->localClient->sendK, source_p->localClient->receiveK); |
1568 | ||
1569 | sendto_one(source_p, "ERROR :Closing Link: %s (%s)", source_p->host, comment); | |
1570 | close_connection(source_p); | |
1571 | ||
1572 | if((source_p->flags & FLAGS_KILLED) == 0) | |
1573 | { | |
1574 | sendto_server(client_p, NULL, CAP_TS6, NOCAPS, | |
1575 | ":%s QUIT :%s", use_id(source_p), comment); | |
212380e3 AC |
1576 | } |
1577 | ||
1578 | SetDead(source_p); | |
330fc5c1 | 1579 | rb_dlinkAddAlloc(source_p, &dead_list); |
212380e3 AC |
1580 | return(CLIENT_EXITED); |
1581 | } | |
1582 | ||
1583 | ||
1584 | /* | |
1585 | ** exit_client - This is old "m_bye". Name changed, because this is not a | |
1586 | ** protocol function, but a general server utility function. | |
1587 | ** | |
1588 | ** This function exits a client of *any* type (user, server, etc) | |
1589 | ** from this server. Also, this generates all necessary prototol | |
1590 | ** messages that this exit may cause. | |
1591 | ** | |
1592 | ** 1) If the client is a local client, then this implicitly | |
1593 | ** exits all other clients depending on this connection (e.g. | |
1594 | ** remote clients having 'from'-field that points to this. | |
1595 | ** | |
1596 | ** 2) If the client is a remote client, then only this is exited. | |
1597 | ** | |
1598 | ** For convenience, this function returns a suitable value for | |
1599 | ** m_function return value: | |
1600 | ** | |
1601 | ** CLIENT_EXITED if (client_p == source_p) | |
1602 | ** 0 if (client_p != source_p) | |
cbeab4bc | 1603 | */ |
212380e3 AC |
1604 | int |
1605 | exit_client(struct Client *client_p, /* The local client originating the | |
1606 | * exit or NULL, if this exit is | |
1607 | * generated by this server for | |
1608 | * internal reasons. | |
1609 | * This will not get any of the | |
1610 | * generated messages. */ | |
1611 | struct Client *source_p, /* Client exiting */ | |
1612 | struct Client *from, /* Client firing off this Exit, | |
1613 | * never NULL! */ | |
1614 | const char *comment /* Reason for the exit */ | |
1615 | ) | |
1616 | { | |
1617 | hook_data_client_exit hdata; | |
1618 | if(IsClosing(source_p)) | |
1619 | return -1; | |
1620 | ||
1621 | /* note, this HAS to be here, when we exit a client we attempt to | |
1622 | * send them data, if this generates a write error we must *not* add | |
1623 | * them to the abort list --fl | |
1624 | */ | |
1625 | SetClosing(source_p); | |
1626 | ||
1627 | hdata.local_link = client_p; | |
1628 | hdata.target = source_p; | |
1629 | hdata.from = from; | |
1630 | hdata.comment = comment; | |
1631 | call_hook(h_client_exit, &hdata); | |
1632 | ||
1633 | if(MyConnect(source_p)) | |
1634 | { | |
1635 | /* Local clients of various types */ | |
1636 | if(IsPerson(source_p)) | |
1637 | return exit_local_client(client_p, source_p, from, comment); | |
1638 | else if(IsServer(source_p)) | |
1639 | return exit_local_server(client_p, source_p, from, comment); | |
1640 | /* IsUnknown || IsConnecting || IsHandShake */ | |
1641 | else if(!IsReject(source_p)) | |
1642 | return exit_unknown_client(client_p, source_p, from, comment); | |
55abcbb2 KB |
1643 | } |
1644 | else | |
212380e3 AC |
1645 | { |
1646 | /* Remotes */ | |
1647 | if(IsPerson(source_p)) | |
1648 | return exit_remote_client(client_p, source_p, from, comment); | |
1649 | else if(IsServer(source_p)) | |
1650 | return exit_remote_server(client_p, source_p, from, comment); | |
1651 | } | |
1652 | ||
1653 | return -1; | |
1654 | } | |
1655 | ||
1656 | /* | |
1657 | * Count up local client memory | |
1658 | */ | |
1659 | ||
1660 | /* XXX one common Client list now */ | |
1661 | void | |
1662 | count_local_client_memory(size_t * count, size_t * local_client_memory_used) | |
1663 | { | |
1087485c JT |
1664 | size_t lusage; |
1665 | rb_bh_usage(lclient_heap, count, NULL, &lusage, NULL); | |
adc6cc42 | 1666 | *local_client_memory_used = lusage + (*count * (sizeof(void *) + sizeof(struct Client))); |
212380e3 AC |
1667 | } |
1668 | ||
1669 | /* | |
1670 | * Count up remote client memory | |
1671 | */ | |
1672 | void | |
1673 | count_remote_client_memory(size_t * count, size_t * remote_client_memory_used) | |
1674 | { | |
1087485c JT |
1675 | size_t lcount, rcount; |
1676 | rb_bh_usage(lclient_heap, &lcount, NULL, NULL, NULL); | |
1677 | rb_bh_usage(client_heap, &rcount, NULL, NULL, NULL); | |
1678 | *count = rcount - lcount; | |
adc6cc42 | 1679 | *remote_client_memory_used = *count * (sizeof(void *) + sizeof(struct Client)); |
212380e3 AC |
1680 | } |
1681 | ||
1682 | ||
1683 | /* | |
1684 | * accept processing, this adds a form of "caller ID" to ircd | |
55abcbb2 | 1685 | * |
212380e3 | 1686 | * If a client puts themselves into "caller ID only" mode, |
55abcbb2 | 1687 | * only clients that match a client pointer they have put on |
212380e3 AC |
1688 | * the accept list will be allowed to message them. |
1689 | * | |
1690 | * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ] | |
1691 | * | |
1692 | * [target.allow_list] -> [ source1 ] -> [source2 ] | |
1693 | * | |
1694 | * i.e. a target will have a link list of source pointers it will allow | |
1695 | * each source client then has a back pointer pointing back | |
1696 | * to the client that has it on its accept list. | |
1697 | * This allows for exit_one_client to remove these now bogus entries | |
55abcbb2 | 1698 | * from any client having an accept on them. |
212380e3 AC |
1699 | */ |
1700 | /* | |
1701 | * del_all_accepts | |
1702 | * | |
1703 | * inputs - pointer to exiting client | |
1704 | * output - NONE | |
1705 | * side effects - Walk through given clients allow_list and on_allow_list | |
1706 | * remove all references to this client | |
1707 | */ | |
1708 | void | |
1709 | del_all_accepts(struct Client *client_p) | |
1710 | { | |
330fc5c1 | 1711 | rb_dlink_node *ptr; |
637c4932 | 1712 | rb_dlink_node *next_ptr; |
212380e3 AC |
1713 | struct Client *target_p; |
1714 | ||
1715 | if(MyClient(client_p) && client_p->localClient->allow_list.head) | |
1716 | { | |
1717 | /* clear this clients accept list, and remove them from | |
1718 | * everyones on_accept_list | |
1719 | */ | |
637c4932 | 1720 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->allow_list.head) |
212380e3 AC |
1721 | { |
1722 | target_p = ptr->data; | |
330fc5c1 AC |
1723 | rb_dlinkFindDestroy(client_p, &target_p->on_allow_list); |
1724 | rb_dlinkDestroy(ptr, &client_p->localClient->allow_list); | |
212380e3 AC |
1725 | } |
1726 | } | |
1727 | ||
1728 | /* remove this client from everyones accept list */ | |
637c4932 | 1729 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head) |
212380e3 AC |
1730 | { |
1731 | target_p = ptr->data; | |
330fc5c1 AC |
1732 | rb_dlinkFindDestroy(client_p, &target_p->localClient->allow_list); |
1733 | rb_dlinkDestroy(ptr, &client_p->on_allow_list); | |
212380e3 AC |
1734 | } |
1735 | } | |
1736 | ||
1737 | /* | |
521f9d63 | 1738 | * show_ip() - asks if the true IP should be shown when source is |
55abcbb2 | 1739 | * asking for info about target |
212380e3 AC |
1740 | * |
1741 | * Inputs - source_p who is asking | |
1742 | * - target_p who do we want the info on | |
521f9d63 | 1743 | * Output - returns 1 if clear IP can be shown, otherwise 0 |
212380e3 AC |
1744 | * Side Effects - none |
1745 | */ | |
1746 | ||
1747 | int | |
1748 | show_ip(struct Client *source_p, struct Client *target_p) | |
1749 | { | |
1750 | if(IsAnyServer(target_p)) | |
1751 | { | |
212380e3 AC |
1752 | return 0; |
1753 | } | |
1754 | else if(IsIPSpoof(target_p)) | |
1755 | { | |
1756 | /* source == NULL indicates message is being sent | |
1757 | * to local opers. | |
1758 | */ | |
1759 | if(!ConfigFileEntry.hide_spoof_ips && | |
1760 | (source_p == NULL || MyOper(source_p))) | |
1761 | return 1; | |
1762 | return 0; | |
1763 | } | |
1764 | else if(IsDynSpoof(target_p) && (source_p != NULL && !IsOper(source_p))) | |
1765 | return 0; | |
1766 | else | |
1767 | return 1; | |
1768 | } | |
1769 | ||
1770 | int | |
1771 | show_ip_conf(struct ConfItem *aconf, struct Client *source_p) | |
1772 | { | |
1773 | if(IsConfDoSpoofIp(aconf)) | |
1774 | { | |
1775 | if(!ConfigFileEntry.hide_spoof_ips && MyOper(source_p)) | |
1776 | return 1; | |
1777 | ||
1778 | return 0; | |
1779 | } | |
1780 | else | |
1781 | return 1; | |
1782 | } | |
1783 | ||
364e59f8 JT |
1784 | int |
1785 | show_ip_whowas(struct Whowas *whowas, struct Client *source_p) | |
1786 | { | |
1787 | if(whowas->flags & WHOWAS_IP_SPOOFING) | |
1788 | if(ConfigFileEntry.hide_spoof_ips || !MyOper(source_p)) | |
1789 | return 0; | |
1790 | if(whowas->flags & WHOWAS_DYNSPOOF) | |
1791 | if(!IsOper(source_p)) | |
1792 | return 0; | |
1793 | return 1; | |
1794 | } | |
1795 | ||
212380e3 AC |
1796 | /* |
1797 | * make_user | |
1798 | * | |
1799 | * inputs - pointer to client struct | |
1800 | * output - pointer to struct User | |
1801 | * side effects - add's an User information block to a client | |
1802 | * if it was not previously allocated. | |
1803 | */ | |
1804 | struct User * | |
1805 | make_user(struct Client *client_p) | |
1806 | { | |
1807 | struct User *user; | |
1808 | ||
1809 | user = client_p->user; | |
1810 | if(!user) | |
1811 | { | |
398b6a73 | 1812 | user = (struct User *) rb_bh_alloc(user_heap); |
212380e3 AC |
1813 | user->refcnt = 1; |
1814 | client_p->user = user; | |
1815 | } | |
1816 | return user; | |
1817 | } | |
1818 | ||
1819 | /* | |
1820 | * make_server | |
1821 | * | |
1822 | * inputs - pointer to client struct | |
1823 | * output - pointer to server_t | |
1824 | * side effects - add's an Server information block to a client | |
1825 | * if it was not previously allocated. | |
1826 | */ | |
8f103562 | 1827 | struct Server * |
212380e3 AC |
1828 | make_server(struct Client *client_p) |
1829 | { | |
8f103562 | 1830 | struct Server *serv = client_p->serv; |
212380e3 AC |
1831 | |
1832 | if(!serv) | |
1833 | { | |
8f103562 | 1834 | serv = (struct Server *) rb_malloc(sizeof(struct Server)); |
212380e3 AC |
1835 | client_p->serv = serv; |
1836 | } | |
1837 | return client_p->serv; | |
1838 | } | |
1839 | ||
1840 | /* | |
1841 | * free_user | |
55abcbb2 | 1842 | * |
212380e3 AC |
1843 | * inputs - pointer to user struct |
1844 | * - pointer to client struct | |
1845 | * output - none | |
1846 | * side effects - Decrease user reference count by one and release block, | |
1847 | * if count reaches 0 | |
1848 | */ | |
1849 | void | |
1850 | free_user(struct User *user, struct Client *client_p) | |
1851 | { | |
c127b45b SB |
1852 | free_away(client_p); |
1853 | ||
212380e3 AC |
1854 | if(--user->refcnt <= 0) |
1855 | { | |
c127b45b SB |
1856 | if(user->away) |
1857 | rb_free((char *) user->away); | |
212380e3 AC |
1858 | /* |
1859 | * sanity check | |
1860 | */ | |
1861 | if(user->refcnt < 0 || user->invited.head || user->channel.head) | |
1862 | { | |
1863 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
0b9aca38 AC |
1864 | "* %p user (%s!%s@%s) %p %p %p %lu %d *", |
1865 | client_p, | |
212380e3 AC |
1866 | client_p ? client_p-> |
1867 | name : "<noname>", | |
1868 | client_p->username, | |
1869 | client_p->host, | |
0b9aca38 AC |
1870 | user, |
1871 | user->invited.head, | |
1872 | user->channel.head, | |
330fc5c1 | 1873 | rb_dlink_list_length(&user->channel), |
212380e3 AC |
1874 | user->refcnt); |
1875 | s_assert(!user->refcnt); | |
1876 | s_assert(!user->invited.head); | |
1877 | s_assert(!user->channel.head); | |
1878 | } | |
1879 | ||
398b6a73 | 1880 | rb_bh_free(user_heap, user); |
212380e3 AC |
1881 | } |
1882 | } | |
1883 | ||
e581ea8e | 1884 | void |
c127b45b | 1885 | allocate_away(struct Client *client_p) |
e581ea8e | 1886 | { |
c127b45b | 1887 | if(client_p->user->away == NULL) |
55abcbb2 | 1888 | client_p->user->away = rb_bh_alloc(away_heap); |
e581ea8e AC |
1889 | } |
1890 | ||
c127b45b | 1891 | |
e581ea8e | 1892 | void |
c127b45b | 1893 | free_away(struct Client *client_p) |
e581ea8e | 1894 | { |
c127b45b SB |
1895 | if(client_p->user != NULL && client_p->user->away != NULL) { |
1896 | rb_bh_free(away_heap, client_p->user->away); | |
1897 | client_p->user->away = NULL; | |
e581ea8e AC |
1898 | } |
1899 | } | |
1900 | ||
212380e3 AC |
1901 | void |
1902 | init_uid(void) | |
1903 | { | |
1904 | int i; | |
1905 | ||
1906 | for(i = 0; i < 3; i++) | |
1907 | current_uid[i] = me.id[i]; | |
1908 | ||
1909 | for(i = 3; i < 9; i++) | |
1910 | current_uid[i] = 'A'; | |
1911 | ||
1912 | current_uid[9] = '\0'; | |
1913 | } | |
1914 | ||
1915 | ||
1916 | char * | |
1917 | generate_uid(void) | |
1918 | { | |
428f9bc1 | 1919 | static int flipped = 0; |
212380e3 AC |
1920 | int i; |
1921 | ||
428f9bc1 | 1922 | uid_restart: |
212380e3 AC |
1923 | for(i = 8; i > 3; i--) |
1924 | { | |
1925 | if(current_uid[i] == 'Z') | |
1926 | { | |
1927 | current_uid[i] = '0'; | |
428f9bc1 | 1928 | goto out; |
212380e3 AC |
1929 | } |
1930 | else if(current_uid[i] != '9') | |
1931 | { | |
1932 | current_uid[i]++; | |
428f9bc1 | 1933 | goto out; |
212380e3 AC |
1934 | } |
1935 | else | |
1936 | current_uid[i] = 'A'; | |
1937 | } | |
1938 | ||
1939 | /* if this next if() triggers, we're fucked. */ | |
1940 | if(current_uid[3] == 'Z') | |
1941 | { | |
1942 | current_uid[i] = 'A'; | |
428f9bc1 | 1943 | flipped = 1; |
212380e3 AC |
1944 | } |
1945 | else | |
1946 | current_uid[i]++; | |
428f9bc1 AC |
1947 | out: |
1948 | /* if this happens..well, i'm not sure what to say, but lets handle it correctly */ | |
1949 | if(rb_unlikely(flipped)) | |
1950 | { | |
1951 | /* this slows down uid generation a bit... */ | |
1952 | if(find_id(current_uid) != NULL) | |
1953 | goto uid_restart; | |
1954 | } | |
212380e3 AC |
1955 | return current_uid; |
1956 | } | |
1957 | ||
1958 | /* | |
1959 | * close_connection | |
1960 | * Close the physical connection. This function must make | |
1961 | * MyConnect(client_p) == FALSE, and set client_p->from == NULL. | |
1962 | */ | |
1963 | void | |
1964 | close_connection(struct Client *client_p) | |
1965 | { | |
1966 | s_assert(client_p != NULL); | |
1967 | if(client_p == NULL) | |
1968 | return; | |
1969 | ||
1970 | s_assert(MyConnect(client_p)); | |
1971 | if(!MyConnect(client_p)) | |
1972 | return; | |
55abcbb2 | 1973 | |
212380e3 AC |
1974 | if(IsServer(client_p)) |
1975 | { | |
1976 | struct server_conf *server_p; | |
1977 | ||
8bd5767b JT |
1978 | ServerStats.is_sv++; |
1979 | ServerStats.is_sbs += client_p->localClient->sendB; | |
1980 | ServerStats.is_sbr += client_p->localClient->receiveB; | |
679ccbe5 | 1981 | ServerStats.is_sti += (unsigned long long)(rb_current_time() - client_p->localClient->firsttime); |
212380e3 AC |
1982 | |
1983 | /* | |
1984 | * If the connection has been up for a long amount of time, schedule | |
1985 | * a 'quick' reconnect, else reset the next-connect cycle. | |
1986 | */ | |
1987 | if((server_p = find_server_conf(client_p->name)) != NULL) | |
1988 | { | |
1989 | /* | |
1990 | * Reschedule a faster reconnect, if this was a automatically | |
1991 | * connected configuration entry. (Note that if we have had | |
1992 | * a rehash in between, the status has been changed to | |
1993 | * CONF_ILLEGAL). But only do this if it was a "good" link. | |
1994 | */ | |
1995 | server_p->hold = time(NULL); | |
1996 | server_p->hold += | |
1997 | (server_p->hold - client_p->localClient->lasttime > | |
1998 | HANGONGOODLINK) ? HANGONRETRYDELAY : ConFreq(server_p->class); | |
1999 | } | |
2000 | ||
2001 | } | |
2002 | else if(IsClient(client_p)) | |
2003 | { | |
8bd5767b JT |
2004 | ServerStats.is_cl++; |
2005 | ServerStats.is_cbs += client_p->localClient->sendB; | |
2006 | ServerStats.is_cbr += client_p->localClient->receiveB; | |
679ccbe5 | 2007 | ServerStats.is_cti += (unsigned long long)(rb_current_time() - client_p->localClient->firsttime); |
212380e3 AC |
2008 | } |
2009 | else | |
47adde3d | 2010 | ServerStats.is_ni++; |
54ac8b60 | 2011 | |
de7cf7e0 | 2012 | client_release_connids(client_p); |
36f0ec74 | 2013 | |
c6d72037 | 2014 | if(client_p->localClient->F != NULL) |
54ac8b60 VY |
2015 | { |
2016 | /* attempt to flush any pending dbufs. Evil, but .. -- adrian */ | |
2017 | if(!IsIOError(client_p)) | |
2018 | send_queued(client_p); | |
2019 | ||
2020 | rb_close(client_p->localClient->F); | |
2021 | client_p->localClient->F = NULL; | |
2022 | } | |
2023 | ||
734d420e JT |
2024 | rb_linebuf_donebuf(&client_p->localClient->buf_sendq); |
2025 | rb_linebuf_donebuf(&client_p->localClient->buf_recvq); | |
212380e3 AC |
2026 | detach_conf(client_p); |
2027 | ||
2028 | /* XXX shouldnt really be done here. */ | |
2029 | detach_server_conf(client_p); | |
2030 | ||
2031 | client_p->from = NULL; /* ...this should catch them! >:) --msa */ | |
2032 | ClearMyConnect(client_p); | |
2033 | SetIOError(client_p); | |
2034 | } | |
2035 | ||
2036 | ||
2037 | ||
2038 | void | |
2039 | error_exit_client(struct Client *client_p, int error) | |
2040 | { | |
2041 | /* | |
2042 | * ...hmm, with non-blocking sockets we might get | |
2043 | * here from quite valid reasons, although.. why | |
2044 | * would select report "data available" when there | |
2045 | * wasn't... so, this must be an error anyway... --msa | |
2046 | * actually, EOF occurs when read() returns 0 and | |
2047 | * in due course, select() returns that fd as ready | |
2048 | * for reading even though it ends up being an EOF. -avalon | |
2049 | */ | |
2050 | char errmsg[255]; | |
734d420e | 2051 | int current_error = rb_get_sockerr(client_p->localClient->F); |
212380e3 AC |
2052 | |
2053 | SetIOError(client_p); | |
2054 | ||
2055 | if(IsServer(client_p) || IsHandshake(client_p)) | |
2056 | { | |
212380e3 AC |
2057 | if(error == 0) |
2058 | { | |
2059 | sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL, | |
2060 | "Server %s closed the connection", | |
b3ebc7ab | 2061 | client_p->name); |
212380e3 AC |
2062 | |
2063 | ilog(L_SERVER, "Server %s closed the connection", | |
2064 | log_client_name(client_p, SHOW_IP)); | |
2065 | } | |
2066 | else | |
2067 | { | |
2068 | sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL, | |
2069 | "Lost connection to %s: %s", | |
2070 | client_p->name, strerror(current_error)); | |
2071 | ilog(L_SERVER, "Lost connection to %s: %s", | |
2072 | log_client_name(client_p, SHOW_IP), strerror(current_error)); | |
2073 | } | |
212380e3 AC |
2074 | } |
2075 | ||
2076 | if(error == 0) | |
f427c8b0 | 2077 | rb_strlcpy(errmsg, "Remote host closed the connection", sizeof(errmsg)); |
212380e3 | 2078 | else |
5203cba5 | 2079 | snprintf(errmsg, sizeof(errmsg), "Read error: %s", strerror(current_error)); |
212380e3 AC |
2080 | |
2081 | exit_client(client_p, client_p, &me, errmsg); | |
2082 | } |