ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/client.c
Revision: 8836
Committed: Sat Feb 2 22:26:46 2019 UTC (5 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 33827 byte(s)
Log Message:
- Fixed bug where handshaking servers wouldn't timeout

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id