1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2014 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file client.c |
23 |
* \brief Controls clients. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "client.h" |
30 |
#include "event.h" |
31 |
#include "hash.h" |
32 |
#include "irc_string.h" |
33 |
#include "ircd.h" |
34 |
#include "numeric.h" |
35 |
#include "s_auth.h" |
36 |
#include "s_bsd.h" |
37 |
#include "conf.h" |
38 |
#include "log.h" |
39 |
#include "s_misc.h" |
40 |
#include "s_serv.h" |
41 |
#include "send.h" |
42 |
#include "whowas.h" |
43 |
#include "s_user.h" |
44 |
#include "memory.h" |
45 |
#include "mempool.h" |
46 |
#include "hostmask.h" |
47 |
#include "listener.h" |
48 |
#include "userhost.h" |
49 |
#include "watch.h" |
50 |
#include "rng_mt.h" |
51 |
#include "parse.h" |
52 |
|
53 |
dlink_list listing_client_list = { NULL, NULL, 0 }; |
54 |
/* Pointer to beginning of Client list */ |
55 |
dlink_list global_client_list = {NULL, NULL, 0}; |
56 |
/* unknown/client pointer lists */ |
57 |
dlink_list unknown_list = {NULL, NULL, 0}; |
58 |
dlink_list local_client_list = {NULL, NULL, 0}; |
59 |
dlink_list serv_list = {NULL, NULL, 0}; |
60 |
dlink_list global_serv_list = {NULL, NULL, 0}; |
61 |
dlink_list oper_list = {NULL, NULL, 0}; |
62 |
|
63 |
static EVH check_pings; |
64 |
|
65 |
static mp_pool_t *client_pool = NULL; |
66 |
static mp_pool_t *lclient_pool = NULL; |
67 |
|
68 |
static dlink_list dead_list = { NULL, NULL, 0}; |
69 |
static dlink_list abort_list = { NULL, NULL, 0}; |
70 |
|
71 |
static dlink_node *eac_next; /* next aborted client to exit */ |
72 |
|
73 |
static void check_pings_list(dlink_list *); |
74 |
static void check_unknowns_list(void); |
75 |
|
76 |
|
77 |
/* client_init() |
78 |
* |
79 |
* inputs - NONE |
80 |
* output - NONE |
81 |
* side effects - initialize client free memory |
82 |
*/ |
83 |
void |
84 |
client_init(void) |
85 |
{ |
86 |
/* start off the check ping event .. -- adrian |
87 |
* Every 30 seconds is plenty -- db |
88 |
*/ |
89 |
client_pool = mp_pool_new(sizeof(struct Client), MP_CHUNK_SIZE_CLIENT); |
90 |
lclient_pool = mp_pool_new(sizeof(struct LocalUser), MP_CHUNK_SIZE_LCLIENT); |
91 |
eventAdd("check_pings", check_pings, NULL, 5); |
92 |
} |
93 |
|
94 |
/* |
95 |
* make_client - create a new Client struct and set it to initial state. |
96 |
* |
97 |
* from == NULL, create local client (a client connected |
98 |
* to a socket). |
99 |
* WARNING: This leaves the client in a dangerous |
100 |
* state where fd == -1, dead flag is not set and |
101 |
* the client is on the unknown_list; therefore, |
102 |
* the first thing to do after calling make_client(NULL) |
103 |
* is setting fd to something reasonable. -adx |
104 |
* |
105 |
* from, create remote client (behind a socket |
106 |
* associated with the client defined by |
107 |
* 'from'). ('from' is a local client!!). |
108 |
*/ |
109 |
struct Client * |
110 |
make_client(struct Client *from) |
111 |
{ |
112 |
struct Client *client_p = mp_pool_get(client_pool); |
113 |
|
114 |
memset(client_p, 0, sizeof(*client_p)); |
115 |
|
116 |
if (!from) |
117 |
{ |
118 |
client_p->from = client_p; /* 'from' of local client is self! */ |
119 |
client_p->localClient = mp_pool_get(lclient_pool); |
120 |
|
121 |
memset(client_p->localClient, 0, sizeof(*client_p->localClient)); |
122 |
|
123 |
client_p->localClient->since = CurrentTime; |
124 |
client_p->localClient->lasttime = CurrentTime; |
125 |
client_p->localClient->firsttime = CurrentTime; |
126 |
client_p->localClient->registration = REG_INIT; |
127 |
|
128 |
/* as good a place as any... */ |
129 |
dlinkAdd(client_p, &client_p->localClient->lclient_node, &unknown_list); |
130 |
} |
131 |
else |
132 |
client_p->from = from; /* 'from' of local client is self! */ |
133 |
|
134 |
client_p->idhnext = client_p; |
135 |
client_p->hnext = client_p; |
136 |
SetUnknown(client_p); |
137 |
strcpy(client_p->username, "unknown"); |
138 |
strcpy(client_p->svid, "0"); |
139 |
|
140 |
return client_p; |
141 |
} |
142 |
|
143 |
/* |
144 |
* free_client |
145 |
* |
146 |
* inputs - pointer to client |
147 |
* output - NONE |
148 |
* side effects - client pointed to has its memory freed |
149 |
*/ |
150 |
static void |
151 |
free_client(struct Client *client_p) |
152 |
{ |
153 |
assert(client_p); |
154 |
assert(client_p != &me); |
155 |
assert(client_p->hnext == client_p); |
156 |
assert(client_p->idhnext == client_p); |
157 |
assert(client_p->channel.head == NULL); |
158 |
assert(dlink_list_length(&client_p->channel) == 0); |
159 |
assert(dlink_list_length(&client_p->whowas) == 0); |
160 |
assert(!IsServer(client_p) || (IsServer(client_p) && client_p->serv)); |
161 |
|
162 |
MyFree(client_p->serv); |
163 |
MyFree(client_p->certfp); |
164 |
|
165 |
if (MyConnect(client_p)) |
166 |
{ |
167 |
assert(client_p->localClient->invited.head == NULL); |
168 |
assert(dlink_list_length(&client_p->localClient->invited) == 0); |
169 |
assert(dlink_list_length(&client_p->localClient->watches) == 0); |
170 |
assert(IsClosing(client_p) && IsDead(client_p)); |
171 |
|
172 |
MyFree(client_p->localClient->response); |
173 |
MyFree(client_p->localClient->auth_oper); |
174 |
|
175 |
/* |
176 |
* clean up extra sockets from P-lines which have been discarded. |
177 |
*/ |
178 |
if (client_p->localClient->listener) |
179 |
{ |
180 |
assert(0 < client_p->localClient->listener->ref_count); |
181 |
if (0 == --client_p->localClient->listener->ref_count && |
182 |
!client_p->localClient->listener->active) |
183 |
free_listener(client_p->localClient->listener); |
184 |
} |
185 |
|
186 |
dbuf_clear(&client_p->localClient->buf_recvq); |
187 |
dbuf_clear(&client_p->localClient->buf_sendq); |
188 |
|
189 |
mp_pool_release(client_p->localClient); |
190 |
} |
191 |
|
192 |
mp_pool_release(client_p); |
193 |
} |
194 |
|
195 |
/* |
196 |
* check_pings - go through the local client list and check activity |
197 |
* kill off stuff that should die |
198 |
* |
199 |
* inputs - NOT USED (from event) |
200 |
* output - next time_t when check_pings() should be called again |
201 |
* side effects - |
202 |
* |
203 |
* |
204 |
* A PING can be sent to clients as necessary. |
205 |
* |
206 |
* Client/Server ping outs are handled. |
207 |
*/ |
208 |
|
209 |
/* |
210 |
* Addon from adrian. We used to call this after nextping seconds, |
211 |
* however I've changed it to run once a second. This is only for |
212 |
* PING timeouts, not K/etc-line checks (thanks dianora!). Having it |
213 |
* run once a second makes life a lot easier - when a new client connects |
214 |
* and they need a ping in 4 seconds, if nextping was set to 20 seconds |
215 |
* we end up waiting 20 seconds. This is stupid. :-) |
216 |
* I will optimise (hah!) check_pings() once I've finished working on |
217 |
* tidying up other network IO evilnesses. |
218 |
* -- adrian |
219 |
*/ |
220 |
|
221 |
static void |
222 |
check_pings(void *notused) |
223 |
{ |
224 |
check_pings_list(&local_client_list); |
225 |
check_pings_list(&serv_list); |
226 |
check_unknowns_list(); |
227 |
} |
228 |
|
229 |
/* check_pings_list() |
230 |
* |
231 |
* inputs - pointer to list to check |
232 |
* output - NONE |
233 |
* side effects - |
234 |
*/ |
235 |
static void |
236 |
check_pings_list(dlink_list *list) |
237 |
{ |
238 |
char scratch[IRCD_BUFSIZE]; |
239 |
int ping = 0; /* ping time value from client */ |
240 |
dlink_node *ptr = NULL, *next_ptr = NULL; |
241 |
|
242 |
DLINK_FOREACH_SAFE(ptr, next_ptr, list->head) |
243 |
{ |
244 |
struct Client *client_p = ptr->data; |
245 |
|
246 |
/* |
247 |
** Note: No need to notify opers here. It's |
248 |
** already done when "FLAGS_DEADSOCKET" is set. |
249 |
*/ |
250 |
if (IsDead(client_p)) |
251 |
{ |
252 |
/* Ignore it, its been exited already */ |
253 |
continue; |
254 |
} |
255 |
|
256 |
if (!IsRegistered(client_p)) |
257 |
ping = CONNECTTIMEOUT; |
258 |
else |
259 |
ping = get_client_ping(&client_p->localClient->confs); |
260 |
|
261 |
if (ping < CurrentTime - client_p->localClient->lasttime) |
262 |
{ |
263 |
if (!IsPingSent(client_p)) |
264 |
{ |
265 |
/* |
266 |
* if we havent PINGed the connection and we havent |
267 |
* heard from it in a while, PING it to make sure |
268 |
* it is still alive. |
269 |
*/ |
270 |
SetPingSent(client_p); |
271 |
client_p->localClient->lasttime = CurrentTime - ping; |
272 |
sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p)); |
273 |
} |
274 |
else |
275 |
{ |
276 |
if (CurrentTime - client_p->localClient->lasttime >= 2 * ping) |
277 |
{ |
278 |
/* |
279 |
* If the client/server hasn't talked to us in 2*ping seconds |
280 |
* and it has a ping time, then close its connection. |
281 |
*/ |
282 |
if (IsServer(client_p) || IsHandshake(client_p)) |
283 |
{ |
284 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE, |
285 |
"No response from %s, closing link", |
286 |
get_client_name(client_p, HIDE_IP)); |
287 |
sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE, |
288 |
"No response from %s, closing link", |
289 |
get_client_name(client_p, MASK_IP)); |
290 |
ilog(LOG_TYPE_IRCD, "No response from %s, closing link", |
291 |
get_client_name(client_p, HIDE_IP)); |
292 |
} |
293 |
|
294 |
snprintf(scratch, sizeof(scratch), "Ping timeout: %d seconds", |
295 |
(int)(CurrentTime - client_p->localClient->lasttime)); |
296 |
exit_client(client_p, scratch); |
297 |
} |
298 |
} |
299 |
} |
300 |
} |
301 |
} |
302 |
|
303 |
/* check_unknowns_list() |
304 |
* |
305 |
* inputs - pointer to list of unknown clients |
306 |
* output - NONE |
307 |
* side effects - unknown clients get marked for termination after n seconds |
308 |
*/ |
309 |
static void |
310 |
check_unknowns_list(void) |
311 |
{ |
312 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
313 |
|
314 |
DLINK_FOREACH_SAFE(ptr, ptr_next, unknown_list.head) |
315 |
{ |
316 |
struct Client *client_p = ptr->data; |
317 |
|
318 |
/* |
319 |
* Check UNKNOWN connections - if they have been in this state |
320 |
* for > 30s, close them. |
321 |
*/ |
322 |
if (IsAuthFinished(client_p) && (CurrentTime - client_p->localClient->firsttime) > 30) |
323 |
exit_client(client_p, "Registration timed out"); |
324 |
} |
325 |
} |
326 |
|
327 |
/* check_conf_klines() |
328 |
* |
329 |
* inputs - NONE |
330 |
* output - NONE |
331 |
* side effects - Check all connections for a pending kline against the |
332 |
* client, exit the client if a kline matches. |
333 |
*/ |
334 |
void |
335 |
check_conf_klines(void) |
336 |
{ |
337 |
struct Client *client_p = NULL; /* current local client_p being examined */ |
338 |
struct MaskItem *conf = NULL; |
339 |
dlink_node *ptr, *next_ptr; |
340 |
|
341 |
DLINK_FOREACH_SAFE(ptr, next_ptr, local_client_list.head) |
342 |
{ |
343 |
client_p = ptr->data; |
344 |
|
345 |
/* If a client is already being exited |
346 |
*/ |
347 |
if (IsDead(client_p) || !IsClient(client_p)) |
348 |
continue; |
349 |
|
350 |
if ((conf = find_dline_conf(&client_p->localClient->ip, |
351 |
client_p->localClient->aftype))) |
352 |
{ |
353 |
if (conf->type == CONF_EXEMPT) |
354 |
continue; |
355 |
|
356 |
conf_try_ban(client_p, conf); |
357 |
continue; /* and go examine next fd/client_p */ |
358 |
} |
359 |
|
360 |
if (ConfigFileEntry.glines) |
361 |
{ |
362 |
if ((conf = find_conf_by_address(client_p->host, &client_p->localClient->ip, |
363 |
CONF_GLINE, client_p->localClient->aftype, |
364 |
client_p->username, NULL, 1))) |
365 |
{ |
366 |
conf_try_ban(client_p, conf); |
367 |
/* and go examine next fd/client_p */ |
368 |
continue; |
369 |
} |
370 |
} |
371 |
|
372 |
if ((conf = find_conf_by_address(client_p->host, &client_p->localClient->ip, |
373 |
CONF_KLINE, client_p->localClient->aftype, |
374 |
client_p->username, NULL, 1))) |
375 |
{ |
376 |
conf_try_ban(client_p, conf); |
377 |
continue; |
378 |
} |
379 |
|
380 |
if ((conf = find_matching_name_conf(CONF_XLINE, client_p->info, |
381 |
NULL, NULL, 0))) |
382 |
{ |
383 |
conf_try_ban(client_p, conf); |
384 |
continue; |
385 |
} |
386 |
} |
387 |
|
388 |
/* also check the unknowns list for new dlines */ |
389 |
DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head) |
390 |
{ |
391 |
client_p = ptr->data; |
392 |
|
393 |
if ((conf = find_dline_conf(&client_p->localClient->ip, |
394 |
client_p->localClient->aftype))) |
395 |
{ |
396 |
if (conf->type == CONF_EXEMPT) |
397 |
continue; |
398 |
|
399 |
exit_client(client_p, "D-lined"); |
400 |
} |
401 |
} |
402 |
} |
403 |
|
404 |
/* |
405 |
* conf_try_ban |
406 |
* |
407 |
* inputs - pointer to client to ban |
408 |
* - pointer to MaskItem |
409 |
* output - NONE |
410 |
* side effects - given client_p is banned |
411 |
*/ |
412 |
void |
413 |
conf_try_ban(struct Client *client_p, struct MaskItem *conf) |
414 |
{ |
415 |
const char *user_reason = NULL; /* What is sent to user */ |
416 |
const char *type_string = NULL; |
417 |
const char dline_string[] = "D-line"; |
418 |
const char kline_string[] = "K-line"; |
419 |
const char gline_string[] = "G-line"; |
420 |
const char xline_string[] = "X-line"; |
421 |
|
422 |
switch (conf->type) |
423 |
{ |
424 |
case CONF_KLINE: |
425 |
if (IsExemptKline(client_p)) |
426 |
{ |
427 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
428 |
"KLINE over-ruled for %s, client is kline_exempt", |
429 |
get_client_name(client_p, HIDE_IP)); |
430 |
return; |
431 |
} |
432 |
|
433 |
type_string = kline_string; |
434 |
break; |
435 |
case CONF_DLINE: |
436 |
type_string = dline_string; |
437 |
break; |
438 |
case CONF_GLINE: |
439 |
if (IsExemptKline(client_p) || |
440 |
IsExemptGline(client_p)) |
441 |
{ |
442 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
443 |
"GLINE over-ruled for %s, client is %sline_exempt", |
444 |
get_client_name(client_p, HIDE_IP), IsExemptKline(client_p) ? "k" : "g"); |
445 |
return; |
446 |
} |
447 |
|
448 |
type_string = gline_string; |
449 |
break; |
450 |
case CONF_XLINE: |
451 |
type_string = xline_string; |
452 |
++conf->count; |
453 |
break; |
454 |
default: |
455 |
assert(0); |
456 |
break; |
457 |
} |
458 |
|
459 |
user_reason = conf->reason ? conf->reason : type_string; |
460 |
|
461 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, "%s active for %s", |
462 |
type_string, get_client_name(client_p, HIDE_IP)); |
463 |
|
464 |
if (IsClient(client_p)) |
465 |
sendto_one_numeric(client_p, &me, ERR_YOUREBANNEDCREEP, user_reason); |
466 |
|
467 |
exit_client(client_p, user_reason); |
468 |
} |
469 |
|
470 |
/* update_client_exit_stats() |
471 |
* |
472 |
* input - pointer to client |
473 |
* output - NONE |
474 |
* side effects - |
475 |
*/ |
476 |
static void |
477 |
update_client_exit_stats(struct Client *client_p) |
478 |
{ |
479 |
if (IsClient(client_p)) |
480 |
{ |
481 |
assert(Count.total > 0); |
482 |
--Count.total; |
483 |
if (HasUMode(client_p, UMODE_OPER)) |
484 |
--Count.oper; |
485 |
if (HasUMode(client_p, UMODE_INVISIBLE)) |
486 |
--Count.invisi; |
487 |
} |
488 |
else if (IsServer(client_p)) |
489 |
sendto_realops_flags(UMODE_EXTERNAL, L_ALL, SEND_NOTICE, |
490 |
"Server %s split from %s", |
491 |
client_p->name, client_p->servptr->name); |
492 |
|
493 |
if (splitchecking && !splitmode) |
494 |
check_splitmode(NULL); |
495 |
} |
496 |
|
497 |
/* find_person() |
498 |
* |
499 |
* inputs - pointer to name |
500 |
* output - return client pointer |
501 |
* side effects - find person by (nick)name |
502 |
*/ |
503 |
struct Client * |
504 |
find_person(const struct Client *const source_p, const char *name) |
505 |
{ |
506 |
struct Client *target_p = NULL; |
507 |
|
508 |
if (IsDigit(*name)) |
509 |
{ |
510 |
if (IsServer(source_p->from)) |
511 |
target_p = hash_find_id(name); |
512 |
} |
513 |
else |
514 |
target_p = hash_find_client(name); |
515 |
|
516 |
return (target_p && IsClient(target_p)) ? target_p : NULL; |
517 |
} |
518 |
|
519 |
/* |
520 |
* find_chasing - find the client structure for a nick name (name) |
521 |
* using history mechanism if necessary. If the client is not found, |
522 |
* an error message (NO SUCH NICK) is generated. |
523 |
*/ |
524 |
struct Client * |
525 |
find_chasing(struct Client *source_p, const char *name) |
526 |
{ |
527 |
struct Client *who = find_person(source_p, name); |
528 |
|
529 |
if (who) |
530 |
return who; |
531 |
|
532 |
if (IsDigit(*name)) |
533 |
return NULL; |
534 |
|
535 |
if ((who = whowas_get_history(name, |
536 |
(time_t)ConfigFileEntry.kill_chase_time_limit)) |
537 |
== NULL) |
538 |
{ |
539 |
sendto_one_numeric(source_p, &me, ERR_NOSUCHNICK, name); |
540 |
return NULL; |
541 |
} |
542 |
|
543 |
return who; |
544 |
} |
545 |
|
546 |
/* |
547 |
* get_client_name - Return the name of the client |
548 |
* for various tracking and |
549 |
* admin purposes. The main purpose of this function is to |
550 |
* return the "socket host" name of the client, if that |
551 |
* differs from the advertised name (other than case). |
552 |
* But, this can be used to any client structure. |
553 |
* |
554 |
* NOTE 1: |
555 |
* Watch out the allocation of "nbuf", if either source_p->name |
556 |
* or source_p->sockhost gets changed into pointers instead of |
557 |
* directly allocated within the structure... |
558 |
* |
559 |
* NOTE 2: |
560 |
* Function return either a pointer to the structure (source_p) or |
561 |
* to internal buffer (nbuf). *NEVER* use the returned pointer |
562 |
* to modify what it points!!! |
563 |
*/ |
564 |
const char * |
565 |
get_client_name(const struct Client *client_p, enum addr_mask_type type) |
566 |
{ |
567 |
static char nbuf[HOSTLEN * 2 + USERLEN + 5]; |
568 |
|
569 |
assert(client_p); |
570 |
|
571 |
if (!MyConnect(client_p)) |
572 |
return client_p->name; |
573 |
|
574 |
if (IsServer(client_p) || IsConnecting(client_p) || IsHandshake(client_p)) |
575 |
{ |
576 |
if (!irccmp(client_p->name, client_p->host)) |
577 |
return client_p->name; |
578 |
else if (ConfigServerHide.hide_server_ips) |
579 |
type = MASK_IP; |
580 |
} |
581 |
|
582 |
if (ConfigFileEntry.hide_spoof_ips) |
583 |
if (type == SHOW_IP && IsIPSpoof(client_p)) |
584 |
type = MASK_IP; |
585 |
|
586 |
/* And finally, let's get the host information, ip or name */ |
587 |
switch (type) |
588 |
{ |
589 |
case SHOW_IP: |
590 |
snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", |
591 |
client_p->name, |
592 |
client_p->username, client_p->sockhost); |
593 |
break; |
594 |
case MASK_IP: |
595 |
if (client_p->localClient->aftype == AF_INET) |
596 |
snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]", |
597 |
client_p->name, client_p->username); |
598 |
else |
599 |
snprintf(nbuf, sizeof(nbuf), "%s[%s@ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]", |
600 |
client_p->name, client_p->username); |
601 |
break; |
602 |
default: |
603 |
snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]", |
604 |
client_p->name, |
605 |
client_p->username, client_p->host); |
606 |
} |
607 |
|
608 |
return nbuf; |
609 |
} |
610 |
|
611 |
void |
612 |
free_exited_clients(void) |
613 |
{ |
614 |
dlink_node *ptr = NULL, *next = NULL; |
615 |
|
616 |
DLINK_FOREACH_SAFE(ptr, next, dead_list.head) |
617 |
{ |
618 |
free_client(ptr->data); |
619 |
dlinkDelete(ptr, &dead_list); |
620 |
free_dlink_node(ptr); |
621 |
} |
622 |
} |
623 |
|
624 |
/* |
625 |
* Exit one client, local or remote. Assuming all dependents have |
626 |
* been already removed, and socket closed for local client. |
627 |
* |
628 |
* The only messages generated are QUITs on channels. |
629 |
*/ |
630 |
static void |
631 |
exit_one_client(struct Client *source_p, const char *quitmsg) |
632 |
{ |
633 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
634 |
|
635 |
assert(!IsMe(source_p)); |
636 |
|
637 |
if (IsClient(source_p)) |
638 |
{ |
639 |
dlinkDelete(&source_p->lnode, &source_p->servptr->serv->client_list); |
640 |
|
641 |
/* |
642 |
* If a person is on a channel, send a QUIT notice |
643 |
* to every client (person) on the same channel (so |
644 |
* that the client can show the "**signoff" message). |
645 |
* (Note: The notice is to the local clients *only*) |
646 |
*/ |
647 |
sendto_common_channels_local(source_p, 0, 0, ":%s!%s@%s QUIT :%s", |
648 |
source_p->name, source_p->username, |
649 |
source_p->host, quitmsg); |
650 |
DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->channel.head) |
651 |
remove_user_from_channel(ptr->data); |
652 |
|
653 |
whowas_add_history(source_p, 0); |
654 |
whowas_off_history(source_p); |
655 |
|
656 |
watch_check_hash(source_p, RPL_LOGOFF); |
657 |
|
658 |
if (MyConnect(source_p)) |
659 |
{ |
660 |
/* Clean up invitefield */ |
661 |
DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->localClient->invited.head) |
662 |
del_invite(ptr->data, source_p); |
663 |
|
664 |
del_all_accepts(source_p); |
665 |
} |
666 |
} |
667 |
else if (IsServer(source_p)) |
668 |
{ |
669 |
dlinkDelete(&source_p->lnode, &source_p->servptr->serv->server_list); |
670 |
|
671 |
if ((ptr = dlinkFindDelete(&global_serv_list, source_p))) |
672 |
free_dlink_node(ptr); |
673 |
} |
674 |
|
675 |
/* Remove source_p from the client lists */ |
676 |
if (source_p->id[0]) |
677 |
hash_del_id(source_p); |
678 |
if (source_p->name[0]) |
679 |
hash_del_client(source_p); |
680 |
|
681 |
if (IsUserHostIp(source_p)) |
682 |
delete_user_host(source_p->username, source_p->host, !MyConnect(source_p)); |
683 |
|
684 |
/* remove from global client list |
685 |
* NOTE: source_p->node.next cannot be NULL if the client is added |
686 |
* to global_client_list (there is always &me at its end) |
687 |
*/ |
688 |
if (source_p->node.next) /* XXX: not needed? */ |
689 |
dlinkDelete(&source_p->node, &global_client_list); |
690 |
|
691 |
update_client_exit_stats(source_p); |
692 |
|
693 |
/* Check to see if the client isn't already on the dead list */ |
694 |
assert(dlinkFind(&dead_list, source_p) == NULL); |
695 |
|
696 |
/* add to dead client dlist */ |
697 |
SetDead(source_p); |
698 |
dlinkAdd(source_p, make_dlink_node(), &dead_list); |
699 |
} |
700 |
|
701 |
/* |
702 |
* Remove all clients that depend on source_p; assumes all (S)QUITs have |
703 |
* already been sent. we make sure to exit a server's dependent clients |
704 |
* and servers before the server itself; exit_one_client takes care of |
705 |
* actually removing things off llists. tweaked from +CSr31 -orabidoo |
706 |
*/ |
707 |
static void |
708 |
recurse_remove_clients(struct Client *source_p, const char *quitmsg) |
709 |
{ |
710 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
711 |
|
712 |
DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->client_list.head) |
713 |
exit_one_client(ptr->data, quitmsg); |
714 |
|
715 |
DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->server_list.head) |
716 |
{ |
717 |
recurse_remove_clients(ptr->data, quitmsg); |
718 |
exit_one_client(ptr->data, quitmsg); |
719 |
} |
720 |
} |
721 |
|
722 |
/* |
723 |
* exit_client - exit a client of any type. Generally, you can use |
724 |
* this on any struct Client, regardless of its state. |
725 |
* |
726 |
* Note, you shouldn't exit remote _users_ without first doing |
727 |
* AddFlag(x, FLAGS_KILLED) and propagating a kill or similar message. |
728 |
* |
729 |
* However, it is perfectly correct to call exit_client to force a _server_ |
730 |
* quit (either local or remote one). |
731 |
* |
732 |
* |
733 |
* inputs: - a client pointer that is going to be exited |
734 |
* output: none |
735 |
* side effects: the client is delinked from all lists, disconnected, |
736 |
* and the rest of IRC network is notified of the exit. |
737 |
* Client memory is scheduled to be freed |
738 |
*/ |
739 |
void |
740 |
exit_client(struct Client *source_p, const char *comment) |
741 |
{ |
742 |
dlink_node *m = NULL; |
743 |
|
744 |
if (MyConnect(source_p)) |
745 |
{ |
746 |
/* DO NOT REMOVE. exit_client can be called twice after a failed |
747 |
* read/write. |
748 |
*/ |
749 |
if (IsClosing(source_p)) |
750 |
return; |
751 |
|
752 |
SetClosing(source_p); |
753 |
|
754 |
if (IsIpHash(source_p)) |
755 |
remove_one_ip(&source_p->localClient->ip); |
756 |
|
757 |
delete_auth(&source_p->localClient->auth); |
758 |
|
759 |
/* |
760 |
* This source_p could have status of one of STAT_UNKNOWN, STAT_CONNECTING |
761 |
* STAT_HANDSHAKE or STAT_UNKNOWN |
762 |
* all of which are lumped together into unknown_list |
763 |
* |
764 |
* In all above cases IsRegistered() will not be true. |
765 |
*/ |
766 |
if (!IsRegistered(source_p)) |
767 |
{ |
768 |
assert(dlinkFind(&unknown_list, source_p)); |
769 |
|
770 |
dlinkDelete(&source_p->localClient->lclient_node, &unknown_list); |
771 |
} |
772 |
else if (IsClient(source_p)) |
773 |
{ |
774 |
time_t on_for = CurrentTime - source_p->localClient->firsttime; |
775 |
assert(Count.local > 0); |
776 |
Count.local--; |
777 |
|
778 |
if (HasUMode(source_p, UMODE_OPER)) |
779 |
if ((m = dlinkFindDelete(&oper_list, source_p))) |
780 |
free_dlink_node(m); |
781 |
|
782 |
assert(dlinkFind(&local_client_list, source_p)); |
783 |
dlinkDelete(&source_p->localClient->lclient_node, &local_client_list); |
784 |
|
785 |
if (source_p->localClient->list_task) |
786 |
free_list_task(source_p); |
787 |
|
788 |
watch_del_watch_list(source_p); |
789 |
sendto_realops_flags(UMODE_CCONN, L_ALL, SEND_NOTICE, |
790 |
"Client exiting: %s (%s@%s) [%s] [%s]", |
791 |
source_p->name, source_p->username, source_p->host, comment, |
792 |
ConfigFileEntry.hide_spoof_ips && IsIPSpoof(source_p) ? |
793 |
"255.255.255.255" : source_p->sockhost); |
794 |
ilog(LOG_TYPE_USER, "%s (%3u:%02u:%02u): %s!%s@%s %llu/%llu", |
795 |
myctime(source_p->localClient->firsttime), (unsigned int)(on_for / 3600), |
796 |
(unsigned int)((on_for % 3600)/60), (unsigned int)(on_for % 60), |
797 |
source_p->name, source_p->username, source_p->host, |
798 |
source_p->localClient->send.bytes>>10, |
799 |
source_p->localClient->recv.bytes>>10); |
800 |
} |
801 |
else if (IsServer(source_p)) |
802 |
{ |
803 |
assert(Count.myserver > 0); |
804 |
--Count.myserver; |
805 |
|
806 |
assert(dlinkFind(&serv_list, source_p)); |
807 |
dlinkDelete(&source_p->localClient->lclient_node, &serv_list); |
808 |
} |
809 |
|
810 |
if (!IsDead(source_p)) |
811 |
{ |
812 |
if (IsServer(source_p)) |
813 |
{ |
814 |
if (!HasFlag(source_p, FLAGS_SQUIT)) |
815 |
{ |
816 |
/* for them, we are exiting the network */ |
817 |
sendto_one(source_p, ":%s SQUIT %s :%s", |
818 |
me.id, me.id, comment); |
819 |
} |
820 |
} |
821 |
|
822 |
sendto_one(source_p, "ERROR :Closing Link: %s (%s)", |
823 |
source_p->host, comment); |
824 |
} |
825 |
|
826 |
close_connection(source_p); |
827 |
} |
828 |
else if (IsClient(source_p) && HasFlag(source_p->servptr, FLAGS_EOB)) |
829 |
sendto_realops_flags(UMODE_FARCONNECT, L_ALL, SEND_NOTICE, |
830 |
"Client exiting at %s: %s (%s@%s) [%s]", |
831 |
source_p->servptr->name, source_p->name, |
832 |
source_p->username, source_p->host, comment); |
833 |
|
834 |
if (IsServer(source_p)) |
835 |
{ |
836 |
char splitstr[HOSTLEN + HOSTLEN + 2] = ""; |
837 |
|
838 |
/* This shouldn't ever happen */ |
839 |
assert(source_p->serv && source_p->servptr); |
840 |
|
841 |
if (ConfigServerHide.hide_servers) |
842 |
/* |
843 |
* Set netsplit message to "*.net *.split" to still show |
844 |
* that its a split, but hide the servers splitting |
845 |
*/ |
846 |
strlcpy(splitstr, "*.net *.split", sizeof(splitstr)); |
847 |
else |
848 |
snprintf(splitstr, sizeof(splitstr), "%s %s", |
849 |
source_p->servptr->name, source_p->name); |
850 |
|
851 |
/* Send SQUIT for source_p in every direction. source_p is already off of serv_list here */ |
852 |
if (!HasFlag(source_p, FLAGS_SQUIT)) |
853 |
sendto_server(NULL, NOCAPS, NOCAPS, "SQUIT %s :%s", source_p->id, comment); |
854 |
|
855 |
/* Now exit the clients internally */ |
856 |
recurse_remove_clients(source_p, splitstr); |
857 |
|
858 |
if (MyConnect(source_p)) |
859 |
{ |
860 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
861 |
"%s was connected for %d seconds. %llu/%llu sendK/recvK.", |
862 |
source_p->name, (int)(CurrentTime - source_p->localClient->firsttime), |
863 |
source_p->localClient->send.bytes >> 10, |
864 |
source_p->localClient->recv.bytes >> 10); |
865 |
ilog(LOG_TYPE_IRCD, "%s was connected for %d seconds. %llu/%llu sendK/recvK.", |
866 |
source_p->name, (int)(CurrentTime - source_p->localClient->firsttime), |
867 |
source_p->localClient->send.bytes >> 10, |
868 |
source_p->localClient->recv.bytes >> 10); |
869 |
} |
870 |
} |
871 |
else if (IsClient(source_p) && !HasFlag(source_p, FLAGS_KILLED)) |
872 |
sendto_server(source_p->from, NOCAPS, NOCAPS, ":%s QUIT :%s", |
873 |
source_p->id, comment); |
874 |
|
875 |
/* The client *better* be off all of the lists */ |
876 |
assert(dlinkFind(&unknown_list, source_p) == NULL); |
877 |
assert(dlinkFind(&local_client_list, source_p) == NULL); |
878 |
assert(dlinkFind(&serv_list, source_p) == NULL); |
879 |
assert(dlinkFind(&oper_list, source_p) == NULL); |
880 |
|
881 |
exit_one_client(source_p, comment); |
882 |
} |
883 |
|
884 |
/* |
885 |
* dead_link_on_write - report a write error if not already dead, |
886 |
* mark it as dead then exit it |
887 |
*/ |
888 |
void |
889 |
dead_link_on_write(struct Client *client_p, int ierrno) |
890 |
{ |
891 |
dlink_node *ptr; |
892 |
|
893 |
if (IsDefunct(client_p)) |
894 |
return; |
895 |
|
896 |
dbuf_clear(&client_p->localClient->buf_recvq); |
897 |
dbuf_clear(&client_p->localClient->buf_sendq); |
898 |
|
899 |
assert(dlinkFind(&abort_list, client_p) == NULL); |
900 |
ptr = make_dlink_node(); |
901 |
/* don't let exit_aborted_clients() finish yet */ |
902 |
dlinkAddTail(client_p, ptr, &abort_list); |
903 |
|
904 |
if (eac_next == NULL) |
905 |
eac_next = ptr; |
906 |
|
907 |
SetDead(client_p); /* You are dead my friend */ |
908 |
} |
909 |
|
910 |
/* |
911 |
* dead_link_on_read - report a read error if not already dead, |
912 |
* mark it as dead then exit it |
913 |
*/ |
914 |
void |
915 |
dead_link_on_read(struct Client *client_p, int error) |
916 |
{ |
917 |
char errmsg[IRCD_BUFSIZE]; |
918 |
int current_error; |
919 |
|
920 |
if (IsDefunct(client_p)) |
921 |
return; |
922 |
|
923 |
dbuf_clear(&client_p->localClient->buf_recvq); |
924 |
dbuf_clear(&client_p->localClient->buf_sendq); |
925 |
|
926 |
current_error = get_sockerr(client_p->localClient->fd.fd); |
927 |
|
928 |
if (IsServer(client_p) || IsHandshake(client_p)) |
929 |
{ |
930 |
int connected = CurrentTime - client_p->localClient->firsttime; |
931 |
|
932 |
if (error == 0) |
933 |
{ |
934 |
/* Admins get the real IP */ |
935 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE, |
936 |
"Server %s closed the connection", |
937 |
get_client_name(client_p, SHOW_IP)); |
938 |
|
939 |
/* Opers get a masked IP */ |
940 |
sendto_realops_flags(UMODE_ALL, L_OPER, SEND_NOTICE, |
941 |
"Server %s closed the connection", |
942 |
get_client_name(client_p, MASK_IP)); |
943 |
|
944 |
ilog(LOG_TYPE_IRCD, "Server %s closed the connection", |
945 |
get_client_name(client_p, SHOW_IP)); |
946 |
} |
947 |
else |
948 |
{ |
949 |
report_error(L_ADMIN, "Lost connection to %s: %s", |
950 |
get_client_name(client_p, SHOW_IP), current_error); |
951 |
report_error(L_OPER, "Lost connection to %s: %s", |
952 |
get_client_name(client_p, MASK_IP), current_error); |
953 |
} |
954 |
|
955 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
956 |
"%s had been connected for %d day%s, %2d:%02d:%02d", |
957 |
client_p->name, connected/86400, |
958 |
(connected/86400 == 1) ? "" : "s", |
959 |
(connected % 86400) / 3600, (connected % 3600) / 60, |
960 |
connected % 60); |
961 |
} |
962 |
|
963 |
if (error == 0) |
964 |
strlcpy(errmsg, "Remote host closed the connection", |
965 |
sizeof(errmsg)); |
966 |
else |
967 |
snprintf(errmsg, sizeof(errmsg), "Read error: %s", |
968 |
strerror(current_error)); |
969 |
|
970 |
exit_client(client_p, errmsg); |
971 |
} |
972 |
|
973 |
void |
974 |
exit_aborted_clients(void) |
975 |
{ |
976 |
dlink_node *ptr; |
977 |
struct Client *target_p; |
978 |
const char *notice; |
979 |
|
980 |
DLINK_FOREACH_SAFE(ptr, eac_next, abort_list.head) |
981 |
{ |
982 |
target_p = ptr->data; |
983 |
eac_next = ptr->next; |
984 |
|
985 |
if (target_p == NULL) |
986 |
{ |
987 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
988 |
"Warning: null client on abort_list!"); |
989 |
dlinkDelete(ptr, &abort_list); |
990 |
free_dlink_node(ptr); |
991 |
continue; |
992 |
} |
993 |
|
994 |
dlinkDelete(ptr, &abort_list); |
995 |
|
996 |
if (IsSendQExceeded(target_p)) |
997 |
notice = "Max SendQ exceeded"; |
998 |
else |
999 |
notice = "Write error: connection closed"; |
1000 |
|
1001 |
exit_client(target_p, notice); |
1002 |
free_dlink_node(ptr); |
1003 |
} |
1004 |
} |
1005 |
|
1006 |
/* |
1007 |
* accept processing, this adds a form of "caller ID" to ircd |
1008 |
* |
1009 |
* If a client puts themselves into "caller ID only" mode, |
1010 |
* only clients that match a client pointer they have put on |
1011 |
* the accept list will be allowed to message them. |
1012 |
* |
1013 |
* Diane Bruce, "Dianora" db@db.net |
1014 |
*/ |
1015 |
|
1016 |
void |
1017 |
del_accept(struct split_nuh_item *accept_p, struct Client *client_p) |
1018 |
{ |
1019 |
dlinkDelete(&accept_p->node, &client_p->localClient->acceptlist); |
1020 |
|
1021 |
MyFree(accept_p->nickptr); |
1022 |
MyFree(accept_p->userptr); |
1023 |
MyFree(accept_p->hostptr); |
1024 |
MyFree(accept_p); |
1025 |
} |
1026 |
|
1027 |
struct split_nuh_item * |
1028 |
find_accept(const char *nick, const char *user, |
1029 |
const char *host, struct Client *client_p, |
1030 |
int (*cmpfunc)(const char *, const char *)) |
1031 |
{ |
1032 |
dlink_node *ptr = NULL; |
1033 |
|
1034 |
DLINK_FOREACH(ptr, client_p->localClient->acceptlist.head) |
1035 |
{ |
1036 |
struct split_nuh_item *accept_p = ptr->data; |
1037 |
|
1038 |
if (!cmpfunc(accept_p->nickptr, nick) && |
1039 |
!cmpfunc(accept_p->userptr, user) && |
1040 |
!cmpfunc(accept_p->hostptr, host)) |
1041 |
return accept_p; |
1042 |
} |
1043 |
|
1044 |
return NULL; |
1045 |
} |
1046 |
|
1047 |
/* accept_message() |
1048 |
* |
1049 |
* inputs - pointer to source client |
1050 |
* - pointer to target client |
1051 |
* output - 1 if accept this message 0 if not |
1052 |
* side effects - See if source is on target's allow list |
1053 |
*/ |
1054 |
int |
1055 |
accept_message(struct Client *source, |
1056 |
struct Client *target) |
1057 |
{ |
1058 |
dlink_node *ptr = NULL; |
1059 |
|
1060 |
if (source == target || find_accept(source->name, source->username, |
1061 |
source->host, target, match)) |
1062 |
return 1; |
1063 |
|
1064 |
if (HasUMode(target, UMODE_SOFTCALLERID)) |
1065 |
DLINK_FOREACH(ptr, target->channel.head) |
1066 |
if (IsMember(source, ((struct Membership *)ptr->data)->chptr)) |
1067 |
return 1; |
1068 |
|
1069 |
return 0; |
1070 |
} |
1071 |
|
1072 |
/* del_all_accepts() |
1073 |
* |
1074 |
* inputs - pointer to exiting client |
1075 |
* output - NONE |
1076 |
* side effects - Walk through given clients acceptlist and remove all entries |
1077 |
*/ |
1078 |
void |
1079 |
del_all_accepts(struct Client *client_p) |
1080 |
{ |
1081 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
1082 |
|
1083 |
DLINK_FOREACH_SAFE(ptr, ptr_next, client_p->localClient->acceptlist.head) |
1084 |
del_accept(ptr->data, client_p); |
1085 |
} |
1086 |
|
1087 |
unsigned int |
1088 |
idle_time_get(const struct Client *source_p, const struct Client *target_p) |
1089 |
{ |
1090 |
unsigned int idle = 0; |
1091 |
unsigned int min_idle = 0; |
1092 |
unsigned int max_idle = 0; |
1093 |
const struct ClassItem *class = get_class_ptr(&target_p->localClient->confs); |
1094 |
|
1095 |
if (!(class->flags & CLASS_FLAGS_FAKE_IDLE) || target_p == source_p) |
1096 |
return CurrentTime - target_p->localClient->last_privmsg; |
1097 |
if (HasUMode(source_p, UMODE_OPER) && |
1098 |
!(class->flags & CLASS_FLAGS_HIDE_IDLE_FROM_OPERS)) |
1099 |
return CurrentTime - target_p->localClient->last_privmsg; |
1100 |
|
1101 |
min_idle = class->min_idle; |
1102 |
max_idle = class->max_idle; |
1103 |
|
1104 |
if (min_idle == max_idle) |
1105 |
return min_idle; |
1106 |
|
1107 |
if (class->flags & CLASS_FLAGS_RANDOM_IDLE) |
1108 |
idle = genrand_int32(); |
1109 |
else |
1110 |
idle = CurrentTime - target_p->localClient->last_privmsg; |
1111 |
|
1112 |
if (!max_idle) |
1113 |
idle = 0; |
1114 |
else |
1115 |
idle %= max_idle; |
1116 |
|
1117 |
if (idle < min_idle) |
1118 |
idle = min_idle + (idle % (max_idle - min_idle)); |
1119 |
|
1120 |
return idle; |
1121 |
} |