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