ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 4086
Committed: Sat Jun 28 16:44:20 2014 UTC (11 years, 2 months ago) by michael
Content type: text/x-csrc
File size: 33054 byte(s)
Log Message:
- Let mp_pool_get() clear memory

File Contents

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

Properties

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