ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/client.c
Revision: 8384
Committed: Fri Mar 16 20:06:38 2018 UTC (6 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 32772 byte(s)
Log Message:
- Rip out mempool

File Contents

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

Properties

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