/[svn]/ircd-hybrid/src/client.c
ViewVC logotype

Contents of /ircd-hybrid/src/client.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 502 - (show annotations)
Fri Mar 3 19:49:25 2006 UTC (15 years, 3 months ago) by michael
File MIME type: text/x-chdr
File size: 49692 byte(s)
- Implemented CAP command handler based uppon ircu's m_cap()
- Added somewhat outdated draft-mitchell-irc-capabilities-01.txt until
  I get the latest version from kev.
- Added "multi-prefix" cap so clients supporting "multi-prefix"
  may recieve multi prefixed NAMES replies, e.g. @%+nick1 @+nick2 ..
- Fixed "make clean" for src/conf/

1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * client.c: Controls clients.
4 *
5 * Copyright (C) 2002 by the past and present ircd coders, and others.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 *
22 * $Id$
23 */
24
25 #include "stdinc.h"
26 #include "client.h"
27 #include "channel_mode.h"
28 #include "common.h"
29 #include "hash.h"
30 #include "ircd.h"
31 #include "s_gline.h"
32 #include "numeric.h"
33 #include "packet.h"
34 #include "s_auth.h"
35 #include "s_conf.h"
36 #include "parse_aline.h"
37 #include "s_serv.h"
38 #include "send.h"
39 #include "whowas.h"
40 #include "s_user.h"
41 #include "hostmask.h"
42 #include "listener.h"
43 #include "userhost.h"
44 #include "s_stats.h"
45 #include "watch.h"
46
47 dlink_list listing_client_list = { NULL, NULL, 0 };
48 /* Pointer to beginning of Client list */
49 dlink_list global_client_list = {NULL, NULL, 0};
50 /* unknown/client pointer lists */
51 dlink_list unknown_list = {NULL, NULL, 0};
52 dlink_list local_client_list = {NULL, NULL, 0};
53 dlink_list serv_list = {NULL, NULL, 0};
54 dlink_list global_serv_list = {NULL, NULL, 0};
55 dlink_list oper_list = {NULL, NULL, 0};
56
57 static EVH check_pings;
58
59 static BlockHeap *client_heap = NULL;
60 static BlockHeap *lclient_heap = NULL;
61
62 static dlink_list dead_list = { NULL, NULL, 0};
63 static dlink_list abort_list = { NULL, NULL, 0};
64
65 static dlink_node *eac_next; /* next aborted client to exit */
66
67 static void check_pings_list(dlink_list *);
68 static void check_unknowns_list(void);
69 static void close_connection(struct Client *);
70 static void ban_them(struct Client *client_p, struct ConfItem *conf);
71 static void del_all_accepts(struct Client *);
72
73 /* init_client()
74 *
75 * inputs - NONE
76 * output - NONE
77 * side effects - initialize client free memory
78 */
79 void
80 init_client(void)
81 {
82 /* start off the check ping event .. -- adrian
83 * Every 30 seconds is plenty -- db
84 */
85 client_heap = BlockHeapCreate("client", sizeof(struct Client), CLIENT_HEAP_SIZE);
86 lclient_heap = BlockHeapCreate("local client", sizeof(struct LocalUser), LCLIENT_HEAP_SIZE);
87 eventAdd("check_pings", check_pings, NULL, 5);
88 }
89
90 /*
91 * make_client - create a new Client struct and set it to initial state.
92 *
93 * from == NULL, create local client (a client connected
94 * to a socket).
95 * WARNING: This leaves the client in a dangerous
96 * state where fd == -1, dead flag is not set and
97 * the client is on the unknown_list; therefore,
98 * the first thing to do after calling make_client(NULL)
99 * is setting fd to something reasonable. -adx
100 *
101 * from, create remote client (behind a socket
102 * associated with the client defined by
103 * 'from'). ('from' is a local client!!).
104 */
105 struct Client *
106 make_client(struct Client *from)
107 {
108 struct Client *client_p = BlockHeapAlloc(client_heap);
109
110 if (from == NULL)
111 {
112 client_p->from = client_p; /* 'from' of local client is self! */
113 client_p->since = client_p->lasttime = client_p->firsttime = CurrentTime;
114
115 client_p->localClient = BlockHeapAlloc(lclient_heap);
116 client_p->localClient->registration = REG_INIT;
117 /* as good a place as any... */
118 dlinkAdd(client_p, &client_p->localClient->lclient_node, &unknown_list);
119 }
120 else
121 client_p->from = from;
122
123 client_p->hnext = client_p;
124 client_p->status = STAT_UNKNOWN;
125 strcpy(client_p->username, "unknown");
126
127 return client_p;
128 }
129
130 /*
131 * free_client
132 *
133 * inputs - pointer to client
134 * output - NONE
135 * side effects - client pointed to has its memory freed
136 */
137 static void
138 free_client(struct Client *client_p)
139 {
140 assert(client_p != NULL);
141 assert(client_p != &me);
142 assert(client_p->hnext == client_p);
143 assert(client_p->channel.head == NULL);
144 assert(dlink_list_length(&client_p->channel) == 0);
145
146 MyFree(client_p->away);
147 MyFree(client_p->serv);
148
149 if (MyConnect(client_p))
150 {
151 assert(dlink_list_length(&client_p->localClient->invited) == 0);
152 assert(client_p->localClient->invited.head == NULL);
153 assert(IsClosing(client_p) && IsDead(client_p));
154
155 MyFree(client_p->localClient->response);
156 MyFree(client_p->localClient->auth_oper);
157
158 /*
159 * clean up extra sockets from P-lines which have been discarded.
160 */
161 if (client_p->localClient->listener)
162 {
163 assert(0 < client_p->localClient->listener->ref_count);
164 if (0 == --client_p->localClient->listener->ref_count &&
165 !client_p->localClient->listener->active)
166 free_listener(client_p->localClient->listener);
167 }
168
169 dbuf_clear(&client_p->localClient->buf_recvq);
170 dbuf_clear(&client_p->localClient->buf_sendq);
171
172 BlockHeapFree(lclient_heap, client_p->localClient);
173 }
174
175 BlockHeapFree(client_heap, client_p);
176 }
177
178 /*
179 * check_pings - go through the local client list and check activity
180 * kill off stuff that should die
181 *
182 * inputs - NOT USED (from event)
183 * output - next time_t when check_pings() should be called again
184 * side effects -
185 *
186 *
187 * A PING can be sent to clients as necessary.
188 *
189 * Client/Server ping outs are handled.
190 */
191
192 /*
193 * Addon from adrian. We used to call this after nextping seconds,
194 * however I've changed it to run once a second. This is only for
195 * PING timeouts, not K/etc-line checks (thanks dianora!). Having it
196 * run once a second makes life a lot easier - when a new client connects
197 * and they need a ping in 4 seconds, if nextping was set to 20 seconds
198 * we end up waiting 20 seconds. This is stupid. :-)
199 * I will optimise (hah!) check_pings() once I've finished working on
200 * tidying up other network IO evilnesses.
201 * -- adrian
202 */
203
204 static void
205 check_pings(void *notused)
206 {
207 check_pings_list(&local_client_list);
208 check_pings_list(&serv_list);
209 check_unknowns_list();
210 }
211
212 /* check_pings_list()
213 *
214 * inputs - pointer to list to check
215 * output - NONE
216 * side effects -
217 */
218 static void
219 check_pings_list(dlink_list *list)
220 {
221 char scratch[32]; /* way too generous but... */
222 struct Client *client_p; /* current local client_p being examined */
223 int ping, pingwarn; /* ping time value from client */
224 dlink_node *ptr, *next_ptr;
225
226 DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
227 {
228 client_p = ptr->data;
229
230 /*
231 ** Note: No need to notify opers here. It's
232 ** already done when "FLAGS_DEADSOCKET" is set.
233 */
234 if (IsDead(client_p))
235 {
236 /* Ignore it, its been exited already */
237 continue;
238 }
239
240 if (client_p->localClient->reject_delay > 0)
241 {
242 if (client_p->localClient->reject_delay <= CurrentTime)
243 exit_client(client_p, &me, "Rejected");
244 continue;
245 }
246
247 if (GlobalSetOptions.idletime && IsClient(client_p))
248 {
249 if (!IsExemptKline(client_p) && !IsOper(client_p) &&
250 !IsIdlelined(client_p) &&
251 ((CurrentTime - client_p->localClient->last) > GlobalSetOptions.idletime))
252 {
253 struct ConfItem *conf;
254 struct AccessItem *aconf;
255
256 conf = make_conf_item(KLINE_TYPE);
257 aconf = &conf->conf.AccessItem;
258
259 DupString(aconf->host, client_p->host);
260 DupString(aconf->reason, "idle exceeder");
261 DupString(aconf->user, client_p->username);
262 aconf->hold = CurrentTime + 60;
263 add_temp_line(conf);
264
265 sendto_realops_flags(UMODE_ALL, L_ALL,
266 "Idle time limit exceeded for %s - temp k-lining",
267 get_client_name(client_p, HIDE_IP));
268 exit_client(client_p, &me, aconf->reason);
269 continue;
270 }
271 }
272
273 if (!IsRegistered(client_p))
274 ping = CONNECTTIMEOUT, pingwarn = 0;
275 else
276 ping = get_client_ping(client_p, &pingwarn);
277
278 if (ping < CurrentTime - client_p->lasttime)
279 {
280 if (!IsPingSent(client_p))
281 {
282 /*
283 * if we havent PINGed the connection and we havent
284 * heard from it in a while, PING it to make sure
285 * it is still alive.
286 */
287 SetPingSent(client_p);
288 ClearPingWarning(client_p);
289 client_p->lasttime = CurrentTime - ping;
290 sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p));
291 }
292 else
293 {
294 if (CurrentTime - client_p->lasttime >= 2 * ping)
295 {
296 /*
297 * If the client/server hasn't talked to us in 2*ping seconds
298 * and it has a ping time, then close its connection.
299 */
300 if (IsServer(client_p) || IsHandshake(client_p))
301 {
302 sendto_realops_flags(UMODE_ALL, L_ADMIN,
303 "No response from %s, closing link",
304 get_client_name(client_p, HIDE_IP));
305 sendto_realops_flags(UMODE_ALL, L_OPER,
306 "No response from %s, closing link",
307 get_client_name(client_p, MASK_IP));
308 ilog(L_NOTICE, "No response from %s, closing link",
309 get_client_name(client_p, HIDE_IP));
310 }
311 ircsprintf(scratch, "Ping timeout: %d seconds",
312 (int)(CurrentTime - client_p->lasttime));
313
314 exit_client(client_p, &me, scratch);
315 }
316 else if (!IsPingWarning(client_p) && pingwarn > 0 &&
317 (IsServer(client_p) || IsHandshake(client_p)) &&
318 CurrentTime - client_p->lasttime >= ping + pingwarn)
319 {
320 /*
321 * If the server hasn't replied in pingwarn seconds after sending
322 * the PING, notify the opers so that they are aware of the problem.
323 */
324 SetPingWarning(client_p);
325 sendto_realops_flags(UMODE_ALL, L_ADMIN,
326 "Warning, no response from %s in %d seconds",
327 get_client_name(client_p, HIDE_IP), pingwarn);
328 sendto_realops_flags(UMODE_ALL, L_OPER,
329 "Warning, no response from %s in %d seconds",
330 get_client_name(client_p, MASK_IP), pingwarn);
331 ilog(L_NOTICE, "No response from %s in %d seconds",
332 get_client_name(client_p, HIDE_IP), pingwarn);
333 }
334 }
335 }
336 }
337 }
338
339 /* check_unknowns_list()
340 *
341 * inputs - pointer to list of unknown clients
342 * output - NONE
343 * side effects - unknown clients get marked for termination after n seconds
344 */
345 static void
346 check_unknowns_list(void)
347 {
348 dlink_node *ptr, *next_ptr;
349
350 DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
351 {
352 struct Client *client_p = ptr->data;
353
354 if (client_p->localClient->reject_delay > 0)
355 {
356 if (client_p->localClient->reject_delay <= CurrentTime)
357 exit_client(client_p, &me, "Rejected");
358 continue;
359 }
360
361 /* Check UNKNOWN connections - if they have been in this state
362 * for > 30s, close them.
363 */
364 if (client_p->firsttime ? ((CurrentTime - client_p->firsttime) > 30) : 0)
365 exit_client(client_p, &me, "Connection timed out");
366 }
367 }
368
369 /* check_conf_klines()
370 *
371 * inputs - NONE
372 * output - NONE
373 * side effects - Check all connections for a pending kline against the
374 * client, exit the client if a kline matches.
375 */
376 void
377 check_conf_klines(void)
378 {
379 struct Client *client_p = NULL; /* current local client_p being examined */
380 struct AccessItem *aconf = NULL;
381 struct ConfItem *conf = NULL;
382 dlink_node *ptr, *next_ptr;
383
384 DLINK_FOREACH_SAFE(ptr, next_ptr, local_client_list.head)
385 {
386 client_p = ptr->data;
387
388 /* If a client is already being exited
389 */
390 if (IsDead(client_p) || !IsClient(client_p))
391 continue;
392
393 /* if there is a returned struct ConfItem then kill it */
394 if ((aconf = find_dline_conf(&client_p->localClient->ip,
395 client_p->localClient->aftype)) != NULL)
396 {
397 if (aconf->status & CONF_EXEMPTDLINE)
398 continue;
399
400 conf = aconf->conf_ptr;
401 ban_them(client_p, conf);
402 continue; /* and go examine next fd/client_p */
403 }
404
405 if (ConfigFileEntry.glines && (aconf = find_gline(client_p)) != NULL)
406 {
407 if (IsExemptKline(client_p) || IsExemptGline(client_p))
408 {
409 sendto_realops_flags(UMODE_ALL, L_ALL,
410 "GLINE over-ruled for %s, client is %cline_exempt",
411 get_client_name(client_p, HIDE_IP),
412 IsExemptKline(client_p) ? 'k' : 'g');
413 continue;
414 }
415
416 conf = aconf->conf_ptr;
417 ban_them(client_p, conf);
418 /* and go examine next fd/client_p */
419 continue;
420 }
421
422 if ((aconf = find_kill(client_p)) != NULL)
423 {
424
425 /* if there is a returned struct AccessItem.. then kill it */
426 if (IsExemptKline(client_p))
427 {
428 sendto_realops_flags(UMODE_ALL, L_ALL,
429 "KLINE over-ruled for %s, client is kline_exempt",
430 get_client_name(client_p, HIDE_IP));
431 continue;
432 }
433
434 conf = aconf->conf_ptr;
435 ban_them(client_p, conf);
436 continue;
437 }
438
439 /* if there is a returned struct MatchItem then kill it */
440 if ((conf = find_matching_name_conf(XLINE_TYPE, client_p->info,
441 NULL, NULL, 0)) != NULL ||
442 (conf = find_matching_name_conf(RXLINE_TYPE, client_p->info,
443 NULL, NULL, 0)) != NULL)
444 {
445 ban_them(client_p, conf);
446 continue;
447 }
448 }
449
450 /* also check the unknowns list for new dlines */
451 DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
452 {
453 client_p = ptr->data;
454
455 if ((aconf = find_dline_conf(&client_p->localClient->ip,
456 client_p->localClient->aftype)))
457 {
458 if (aconf->status & CONF_EXEMPTDLINE)
459 continue;
460
461 exit_client(client_p, &me, "D-lined");
462 }
463 }
464 }
465
466 /*
467 * ban_them
468 *
469 * inputs - pointer to client to ban
470 * - pointer to ConfItem
471 * output - NONE
472 * side effects - given client_p is banned
473 */
474 static void
475 ban_them(struct Client *client_p, struct ConfItem *conf)
476 {
477 const char *user_reason = NULL; /* What is sent to user */
478 const char *channel_reason = NULL; /* What is sent to channel */
479 struct AccessItem *aconf = NULL;
480 struct MatchItem *xconf = NULL;
481 const char *type_string = NULL;
482 const char dline_string[] = "D-line";
483 const char kline_string[] = "K-line";
484 const char gline_string[] = "G-line";
485 const char xline_string[] = "X-line";
486
487 switch (conf->type)
488 {
489 case RKLINE_TYPE:
490 case KLINE_TYPE:
491 type_string = kline_string;
492 aconf = &conf->conf.AccessItem;
493 break;
494 case DLINE_TYPE:
495 type_string = dline_string;
496 aconf = &conf->conf.AccessItem;
497 break;
498 case GLINE_TYPE:
499 type_string = gline_string;
500 aconf = &conf->conf.AccessItem;
501 break;
502 case RXLINE_TYPE:
503 case XLINE_TYPE:
504 type_string = xline_string;
505 xconf = &conf->conf.MatchItem;
506 ++xconf->count;
507 break;
508 default:
509 assert(0);
510 break;
511 }
512
513 if (ConfigFileEntry.kline_with_reason)
514 {
515 if (aconf != NULL)
516 user_reason = aconf->reason ? aconf->reason : type_string;
517 if (xconf != NULL)
518 user_reason = xconf->reason ? xconf->reason : type_string;
519 }
520 else
521 user_reason = type_string;
522
523 if (ConfigFileEntry.kline_reason != NULL)
524 channel_reason = ConfigFileEntry.kline_reason;
525 else
526 channel_reason = user_reason;
527
528 sendto_realops_flags(UMODE_ALL, L_ALL, "%s active for %s",
529 type_string, get_client_name(client_p, HIDE_IP));
530
531 if (IsClient(client_p))
532 sendto_one(client_p, form_str(ERR_YOUREBANNEDCREEP),
533 me.name, client_p->name, user_reason);
534
535 exit_client(client_p, &me, channel_reason);
536 }
537
538 /* update_client_exit_stats()
539 *
540 * input - pointer to client
541 * output - NONE
542 * side effects -
543 */
544 static void
545 update_client_exit_stats(struct Client *client_p)
546 {
547 if (IsServer(client_p))
548 {
549 sendto_realops_flags(UMODE_EXTERNAL, L_ALL,
550 "Server %s split from %s",
551 client_p->name, client_p->servptr->name);
552 }
553 else if (IsClient(client_p))
554 {
555 --Count.total;
556 if (IsOper(client_p))
557 --Count.oper;
558 if (IsInvisible(client_p))
559 --Count.invisi;
560 }
561
562 if (splitchecking && !splitmode)
563 check_splitmode(NULL);
564 }
565
566 /* find_person()
567 *
568 * inputs - pointer to name
569 * output - return client pointer
570 * side effects - find person by (nick)name
571 */
572 /* XXX - ugly wrapper */
573 struct Client *
574 find_person(const struct Client *client_p, const char *name)
575 {
576 struct Client *c2ptr;
577
578 if (IsDigit(*name))
579 {
580 if ((c2ptr = hash_find_id(name)) != NULL)
581 {
582 /* invisible users shall not be found by UID guessing */
583 if (IsInvisible(c2ptr) && !IsServer(client_p))
584 c2ptr = NULL;
585 }
586 }
587 else
588 c2ptr = find_client(name);
589
590 return ((c2ptr != NULL && IsClient(c2ptr)) ? c2ptr : NULL);
591 }
592
593 /*
594 * find_chasing - find the client structure for a nick name (user)
595 * using history mechanism if necessary. If the client is not found,
596 * an error message (NO SUCH NICK) is generated. If the client was found
597 * through the history, chasing will be 1 and otherwise 0.
598 */
599 struct Client *
600 find_chasing(struct Client *client_p, struct Client *source_p, const char *user, int *chasing)
601 {
602 struct Client *who = find_person(client_p, user);
603
604 if (chasing)
605 *chasing = 0;
606
607 if (who)
608 return who;
609
610 if (IsDigit(*user))
611 return NULL;
612
613 if ((who = get_history(user,
614 (time_t)ConfigFileEntry.kill_chase_time_limit))
615 == NULL)
616 {
617 sendto_one(source_p, form_str(ERR_NOSUCHNICK),
618 me.name, source_p->name, user);
619 return NULL;
620 }
621
622 if (chasing)
623 *chasing = 1;
624
625 return who;
626 }
627
628 /*
629 * get_client_name - Return the name of the client
630 * for various tracking and
631 * admin purposes. The main purpose of this function is to
632 * return the "socket host" name of the client, if that
633 * differs from the advertised name (other than case).
634 * But, this can be used to any client structure.
635 *
636 * NOTE 1:
637 * Watch out the allocation of "nbuf", if either source_p->name
638 * or source_p->sockhost gets changed into pointers instead of
639 * directly allocated within the structure...
640 *
641 * NOTE 2:
642 * Function return either a pointer to the structure (source_p) or
643 * to internal buffer (nbuf). *NEVER* use the returned pointer
644 * to modify what it points!!!
645 */
646 const char *
647 get_client_name(struct Client *client, int showip)
648 {
649 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
650
651 assert(client != NULL);
652
653 if (irccmp(client->name, client->host) == 0)
654 return client->name;
655
656 if (ConfigServerHide.hide_server_ips)
657 if (IsServer(client) || IsConnecting(client) || IsHandshake(client))
658 showip = MASK_IP;
659
660 if (ConfigFileEntry.hide_spoof_ips)
661 if (showip == SHOW_IP && IsIPSpoof(client))
662 showip = MASK_IP;
663
664 /* And finally, let's get the host information, ip or name */
665 switch (showip)
666 {
667 case SHOW_IP:
668 if (MyConnect(client))
669 {
670 ircsprintf(nbuf, "%s[%s@%s]", client->name, client->username,
671 client->sockhost);
672 break;
673 }
674 case MASK_IP:
675 ircsprintf(nbuf, "%s[%s@255.255.255.255]", client->name,
676 client->username);
677 break;
678 default:
679 ircsprintf(nbuf, "%s[%s@%s]", client->name, client->username,
680 client->host);
681 }
682
683 return nbuf;
684 }
685
686 void
687 free_exited_clients(void)
688 {
689 dlink_node *ptr, *next;
690 struct Client *target_p;
691
692 DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
693 {
694 target_p = ptr->data;
695
696 if (ptr->data == NULL)
697 {
698 sendto_realops_flags(UMODE_ALL, L_ALL,
699 "Warning: null client on dead_list!");
700 dlinkDelete(ptr, &dead_list);
701 free_dlink_node(ptr);
702 continue;
703 }
704
705 free_client(target_p);
706 dlinkDelete(ptr, &dead_list);
707 free_dlink_node(ptr);
708 }
709 }
710
711 /*
712 * Exit one client, local or remote. Assuming all dependents have
713 * been already removed, and socket closed for local client.
714 *
715 * The only messages generated are QUITs on channels.
716 */
717 static void
718 exit_one_client(struct Client *source_p, const char *quitmsg)
719 {
720 dlink_node *lp = NULL, *next_lp = NULL;
721
722 assert(!IsMe(source_p));
723
724 if (IsServer(source_p))
725 {
726 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
727
728 if ((lp = dlinkFindDelete(&global_serv_list, source_p)) != NULL)
729 free_dlink_node(lp);
730
731 if (!MyConnect(source_p))
732 {
733 source_p->from->serv->dep_servers--;
734 assert(source_p->from->serv->dep_servers > 0);
735 }
736 }
737 else if (IsClient(source_p))
738 {
739 if (source_p->servptr->serv != NULL)
740 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->users);
741
742 /*
743 * If a person is on a channel, send a QUIT notice
744 * to every client (person) on the same channel (so
745 * that the client can show the "**signoff" message).
746 * (Note: The notice is to the local clients *only*)
747 */
748 sendto_common_channels_local(source_p, 0, ":%s!%s@%s QUIT :%s",
749 source_p->name, source_p->username,
750 source_p->host, quitmsg);
751 DLINK_FOREACH_SAFE(lp, next_lp, source_p->channel.head)
752 remove_user_from_channel(lp->data);
753
754 add_history(source_p, 0);
755 off_history(source_p);
756
757 assert(source_p->whowas.head == NULL);
758 assert(source_p->whowas.tail == NULL);
759
760 /* Do local vs. remote processing here */
761 if (MyConnect(source_p))
762 {
763 /* Clean up invitefield */
764 DLINK_FOREACH_SAFE(lp, next_lp, source_p->localClient->invited.head)
765 del_invite(lp->data, source_p);
766
767 /* Clean up allow lists */
768 del_all_accepts(source_p);
769 }
770 else
771 {
772 source_p->from->serv->dep_users--;
773 assert(source_p->from->serv->dep_users >= 0);
774 }
775 }
776
777 /* Remove source_p from the client lists */
778 if (HasID(source_p))
779 hash_del_id(source_p);
780 if (source_p->name[0])
781 hash_del_client(source_p);
782
783 hash_check_watch(source_p, RPL_LOGOFF);
784
785 if (IsUserHostIp(source_p))
786 delete_user_host(source_p->username, source_p->host, !MyConnect(source_p));
787
788 /* remove from global client list
789 * NOTE: source_p->node.next cannot be NULL if the client is added
790 * to global_client_list (there is always &me at its end)
791 */
792 if (source_p != NULL && source_p->node.next != NULL)
793 dlinkDelete(&source_p->node, &global_client_list);
794
795 update_client_exit_stats(source_p);
796
797 /* Check to see if the client isn't already on the dead list */
798 assert(dlinkFind(&dead_list, source_p) == NULL);
799
800 /* add to dead client dlist */
801 SetDead(source_p);
802 dlinkAdd(source_p, make_dlink_node(), &dead_list);
803 }
804
805 /* Recursively send QUITs and SQUITs for source_p and all its dependent clients
806 * and servers to those servers that need them. A server needs the client
807 * QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it
808 * isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once
809 * a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending
810 * on that one -orabidoo
811 *
812 * This is now called on each local server -adx
813 */
814 static void
815 recurse_send_quits(struct Client *original_source_p, struct Client *source_p,
816 struct Client *from, struct Client *to, const char *comment,
817 const char *splitstr, const char *myname)
818 {
819 dlink_node *ptr, *next;
820 struct Client *target_p;
821 int hidden = match(myname, source_p->name);
822
823 assert(to != source_p); /* should be already removed from serv_list */
824
825 /* If this server can handle quit storm (QS) removal
826 * of dependents, just send the SQUIT
827 *
828 * Always check *all* dependent servers if some of them are
829 * hidden behind fakename. If so, send out the QUITs -adx
830 */
831 if (hidden || !IsCapable(to, CAP_QS))
832 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->users.head)
833 {
834 target_p = ptr->data;
835 sendto_one(to, ":%s QUIT :%s", target_p->name, splitstr);
836 }
837
838 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->servers.head)
839 recurse_send_quits(original_source_p, ptr->data, from, to,
840 comment, splitstr, myname);
841
842 if (!hidden && ((source_p == original_source_p && to != from) ||
843 !IsCapable(to, CAP_QS)))
844 {
845 /* don't use a prefix here - we have to be 100% sure the message
846 * will be accepted without Unknown prefix etc.. */
847 sendto_one(to, "SQUIT %s :%s", ID_or_name(source_p, to), comment);
848 }
849 }
850
851 /*
852 * Remove all clients that depend on source_p; assumes all (S)QUITs have
853 * already been sent. we make sure to exit a server's dependent clients
854 * and servers before the server itself; exit_one_client takes care of
855 * actually removing things off llists. tweaked from +CSr31 -orabidoo
856 */
857 static void
858 recurse_remove_clients(struct Client *source_p, const char *quitmsg)
859 {
860 dlink_node *ptr, *next;
861
862 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->users.head)
863 exit_one_client(ptr->data, quitmsg);
864
865 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->servers.head)
866 {
867 recurse_remove_clients(ptr->data, quitmsg);
868 exit_one_client(ptr->data, quitmsg);
869 }
870
871 assert(source_p->serv->dep_servers == 1);
872 assert(source_p->serv->dep_users == 0);
873 }
874
875 /*
876 ** Remove *everything* that depends on source_p, from all lists, and sending
877 ** all necessary QUITs and SQUITs. source_p itself is still on the lists,
878 ** and its SQUITs have been sent except for the upstream one -orabidoo
879 */
880 static void
881 remove_dependents(struct Client *source_p, struct Client *from,
882 const char *comment, const char *splitstr)
883 {
884 struct Client *to;
885 struct ConfItem *conf;
886 struct AccessItem *aconf;
887 static char myname[HOSTLEN+1];
888 dlink_node *ptr;
889
890 DLINK_FOREACH(ptr, serv_list.head)
891 {
892 to = ptr->data;
893
894 if ((conf = to->serv->sconf) != NULL)
895 {
896 aconf = &conf->conf.AccessItem;
897 strlcpy(myname, my_name_for_link(aconf), sizeof(myname));
898 }
899 else
900 strlcpy(myname, me.name, sizeof(myname));
901 recurse_send_quits(source_p, source_p, from, to,
902 comment, splitstr, myname);
903 }
904
905 recurse_remove_clients(source_p, splitstr);
906 }
907
908 /*
909 * exit_client - exit a client of any type. Generally, you can use
910 * this on any struct Client, regardless of its state.
911 *
912 * Note, you shouldn't exit remote _users_ without first doing
913 * SetKilled and propagating a kill or similar message. However,
914 * it is perfectly correct to call exit_client to force a _server_
915 * quit (either local or remote one).
916 *
917 * inputs: - a client pointer that is going to be exited
918 * - for servers, the second argument is a pointer to who
919 * is firing the server. This side won't get any generated
920 * messages. NEVER NULL!
921 * output: none
922 * side effects: the client is delinked from all lists, disconnected,
923 * and the rest of IRC network is notified of the exit.
924 * Client memory is scheduled to be freed
925 */
926 void
927 exit_client(struct Client *source_p, struct Client *from, const char *comment)
928 {
929 dlink_node *m;
930
931 if (MyConnect(source_p))
932 {
933 /* DO NOT REMOVE. exit_client can be called twice after a failed
934 * read/write.
935 */
936 if (IsClosing(source_p))
937 return;
938
939 SetClosing(source_p);
940
941 if (IsIpHash(source_p))
942 remove_one_ip(&source_p->localClient->ip);
943
944 delete_auth(source_p);
945
946 if (IsClient(source_p))
947 {
948 --Count.local;
949
950 if (IsOper(source_p))
951 {
952 if ((m = dlinkFindDelete(&oper_list, source_p)) != NULL)
953 free_dlink_node(m);
954 }
955
956 dlinkDelete(&source_p->localClient->lclient_node, &local_client_list);
957 if (source_p->localClient->list_task != NULL)
958 free_list_task(source_p->localClient->list_task, source_p);
959
960 hash_del_watch_list(source_p);
961 sendto_realops_flags(UMODE_CCONN, L_ALL, "Client exiting: %s (%s@%s) [%s] [%s]",
962 source_p->name, source_p->username, source_p->host, comment,
963 ConfigFileEntry.hide_spoof_ips && IsIPSpoof(source_p) ?
964 "255.255.255.255" : source_p->sockhost);
965 }
966 else if (!IsRegistered(source_p))
967 {
968 /*
969 * This source_p could have status of one of STAT_UNKNOWN, STAT_CONNECTING
970 * STAT_HANDSHAKE or STAT_UNKNOWN
971 * all of which are lumped together into unknown_list
972 *
973 * In all above cases IsRegistered() will not be true.
974 */
975 dlinkDelete(&source_p->localClient->lclient_node, &unknown_list);
976 }
977
978 /* As soon as a client is known to be a server of some sort
979 * it has to be put on the serv_list, or SJOIN's to this new server
980 * from the connect burst will not be seen.
981 */
982 if (IsServer(source_p) || IsConnecting(source_p) || IsHandshake(source_p))
983 {
984 if ((m = dlinkFindDelete(&serv_list, source_p)) != NULL)
985 {
986 free_dlink_node(m);
987 unset_chcap_usage_counts(source_p);
988 }
989
990 if (IsServer(source_p))
991 --Count.myserver;
992 }
993
994 log_user_exit(source_p);
995
996 if (!IsDead(source_p))
997 {
998 if (IsServer(source_p))
999 {
1000 /* for them, we are exiting the network */
1001 sendto_one(source_p, ":%s SQUIT %s :%s",
1002 ID_or_name(from, source_p), me.name, comment);
1003 }
1004
1005 sendto_one(source_p, "ERROR :Closing Link: %s (%s)",
1006 source_p->host, comment);
1007 }
1008
1009 /*
1010 ** Currently only server connections can have
1011 ** depending remote clients here, but it does no
1012 ** harm to check for all local clients. In
1013 ** future some other clients than servers might
1014 ** have remotes too...
1015 **
1016 ** Close the Client connection first and mark it
1017 ** so that no messages are attempted to send to it.
1018 ** Remember it makes source_p->from == NULL.
1019 */
1020 close_connection(source_p);
1021 }
1022
1023 if (IsServer(source_p))
1024 {
1025 char splitstr[HOSTLEN + HOSTLEN + 2];
1026
1027 /* This shouldn't ever happen */
1028 assert(source_p->serv != NULL && source_p->servptr != NULL);
1029
1030 if (ConfigServerHide.hide_servers)
1031 /*
1032 * set netsplit message to "*.net *.split" to still show
1033 * that its a split, but hide the servers splitting
1034 */
1035 strcpy(splitstr, "*.net *.split");
1036 else
1037 snprintf(splitstr, sizeof(splitstr), "%s %s",
1038 source_p->servptr->name, source_p->name);
1039
1040 remove_dependents(source_p, from->from, comment, splitstr);
1041
1042 if (source_p->servptr == &me)
1043 {
1044 sendto_realops_flags(UMODE_ALL, L_ALL,
1045 "%s was connected for %d seconds. %llu/%llu sendK/recvK.",
1046 source_p->name, (int)(CurrentTime - source_p->firsttime),
1047 source_p->localClient->send.bytes >> 10,
1048 source_p->localClient->recv.bytes >> 10);
1049 ilog(L_NOTICE, "%s was connected for %d seconds. %llu/%llu sendK/recvK.",
1050 source_p->name, (int)(CurrentTime - source_p->firsttime),
1051 source_p->localClient->send.bytes >> 10,
1052 source_p->localClient->recv.bytes >> 10);
1053 }
1054 }
1055 else if (IsClient(source_p) && !IsKilled(source_p))
1056 {
1057 sendto_server(from->from, source_p, NULL, CAP_TS6, NOCAPS,
1058 ":%s QUIT :%s", ID(source_p), comment);
1059 sendto_server(from->from, source_p, NULL, NOCAPS, CAP_TS6,
1060 ":%s QUIT :%s", source_p->name, comment);
1061 }
1062
1063 /* The client *better* be off all of the lists */
1064 assert(dlinkFind(&unknown_list, source_p) == NULL);
1065 assert(dlinkFind(&local_client_list, source_p) == NULL);
1066 assert(dlinkFind(&serv_list, source_p) == NULL);
1067 assert(dlinkFind(&oper_list, source_p) == NULL);
1068
1069 exit_one_client(source_p, comment);
1070 }
1071
1072 /*
1073 * close_connection
1074 * Close the physical connection. This function must make
1075 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
1076 */
1077 static void
1078 close_connection(struct Client *client_p)
1079 {
1080 struct ConfItem *conf;
1081 struct AccessItem *aconf;
1082 struct ClassItem *aclass;
1083
1084 assert(NULL != client_p);
1085
1086 if (IsServer(client_p))
1087 {
1088 ServerStats->is_sv++;
1089 ServerStats->is_sbs += client_p->localClient->send.bytes;
1090 ServerStats->is_sbr += client_p->localClient->recv.bytes;
1091 ServerStats->is_sti += CurrentTime - client_p->firsttime;
1092
1093 /* XXX Does this even make any sense at all anymore?
1094 * scheduling a 'quick' reconnect could cause a pile of
1095 * nick collides under TSora protocol... -db
1096 */
1097 /*
1098 * If the connection has been up for a long amount of time, schedule
1099 * a 'quick' reconnect, else reset the next-connect cycle.
1100 */
1101 if ((conf = find_exact_name_conf(SERVER_TYPE,
1102 client_p->name, client_p->username,
1103 client_p->host)))
1104 {
1105 /*
1106 * Reschedule a faster reconnect, if this was a automatically
1107 * connected configuration entry. (Note that if we have had
1108 * a rehash in between, the status has been changed to
1109 * CONF_ILLEGAL). But only do this if it was a "good" link.
1110 */
1111 aconf = &conf->conf.AccessItem;
1112 aclass = &((struct ConfItem *)aconf->class_ptr)->conf.ClassItem;
1113 aconf->hold = time(NULL);
1114 aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ?
1115 HANGONRETRYDELAY : ConFreq(aclass);
1116 }
1117 }
1118 else if (IsClient(client_p))
1119 {
1120 ServerStats->is_cl++;
1121 ServerStats->is_cbs += client_p->localClient->send.bytes;
1122 ServerStats->is_cbr += client_p->localClient->recv.bytes;
1123 ServerStats->is_cti += CurrentTime - client_p->firsttime;
1124 }
1125 else
1126 ServerStats->is_ni++;
1127
1128 if (!IsDead(client_p))
1129 {
1130 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
1131 /* there is still a chance that we might send data to this socket
1132 * even if it is marked as blocked (COMM_SELECT_READ handler is called
1133 * before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx
1134 */
1135 ClearSendqBlocked(client_p);
1136 send_queued_write(client_p);
1137 }
1138
1139 #ifdef HAVE_LIBCRYPTO
1140 if (client_p->localClient->fd.ssl)
1141 {
1142 SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN);
1143
1144 if (!SSL_shutdown(client_p->localClient->fd.ssl))
1145 SSL_shutdown(client_p->localClient->fd.ssl);
1146 }
1147 #endif
1148 if (client_p->localClient->fd.flags.open)
1149 fd_close(&client_p->localClient->fd);
1150
1151 if (HasServlink(client_p))
1152 {
1153 if (client_p->localClient->ctrlfd.flags.open)
1154 fd_close(&client_p->localClient->ctrlfd);
1155 }
1156
1157 dbuf_clear(&client_p->localClient->buf_sendq);
1158 dbuf_clear(&client_p->localClient->buf_recvq);
1159
1160 MyFree(client_p->localClient->passwd);
1161 detach_confs(client_p);
1162 client_p->from = NULL; /* ...this should catch them! >:) --msa */
1163 }
1164
1165 /*
1166 * report_error - report an error from an errno.
1167 * Record error to log and also send a copy to all *LOCAL* opers online.
1168 *
1169 * text is a *format* string for outputing error. It must
1170 * contain only two '%s', the first will be replaced
1171 * by the sockhost from the client_p, and the latter will
1172 * be taken from sys_errlist[errno].
1173 *
1174 * client_p if not NULL, is the *LOCAL* client associated with
1175 * the error.
1176 *
1177 * Cannot use perror() within daemon. stderr is closed in
1178 * ircd and cannot be used. And, worse yet, it might have
1179 * been reassigned to a normal connection...
1180 *
1181 * Actually stderr is still there IFF ircd was run with -s --Rodder
1182 */
1183 void
1184 report_error(int level, const char *text, const char *who, int error)
1185 {
1186 who = (who != NULL) ? who : "";
1187
1188 sendto_realops_flags(UMODE_DEBUG, level, text, who, strerror(error));
1189 log_oper_action(LOG_IOERR_TYPE, NULL, "%s %s %s\n", who, text, strerror(error));
1190 ilog(L_ERROR, text, who, strerror(error));
1191 }
1192
1193 /*
1194 * dead_link_on_write - report a write error if not already dead,
1195 * mark it as dead then exit it
1196 */
1197 void
1198 dead_link_on_write(struct Client *client_p, int ierrno)
1199 {
1200 dlink_node *ptr;
1201
1202 if (IsDefunct(client_p))
1203 return;
1204
1205 dbuf_clear(&client_p->localClient->buf_recvq);
1206 dbuf_clear(&client_p->localClient->buf_sendq);
1207
1208 assert(dlinkFind(&abort_list, client_p) == NULL);
1209 ptr = make_dlink_node();
1210 /* don't let exit_aborted_clients() finish yet */
1211 dlinkAddTail(client_p, ptr, &abort_list);
1212
1213 if (eac_next == NULL)
1214 eac_next = ptr;
1215
1216 SetDead(client_p); /* You are dead my friend */
1217 }
1218
1219 /*
1220 * dead_link_on_read - report a read error if not already dead,
1221 * mark it as dead then exit it
1222 */
1223 void
1224 dead_link_on_read(struct Client *client_p, int error)
1225 {
1226 char errmsg[255];
1227 int current_error;
1228
1229 if (IsDefunct(client_p))
1230 return;
1231
1232 dbuf_clear(&client_p->localClient->buf_recvq);
1233 dbuf_clear(&client_p->localClient->buf_sendq);
1234
1235 current_error = get_sockerr(client_p->localClient->fd.fd);
1236
1237 if (IsServer(client_p) || IsHandshake(client_p))
1238 {
1239 int connected = CurrentTime - client_p->firsttime;
1240
1241 if (error == 0)
1242 {
1243 /* Admins get the real IP */
1244 sendto_realops_flags(UMODE_ALL, L_ADMIN,
1245 "Server %s closed the connection",
1246 get_client_name(client_p, SHOW_IP));
1247
1248 /* Opers get a masked IP */
1249 sendto_realops_flags(UMODE_ALL, L_OPER,
1250 "Server %s closed the connection",
1251 get_client_name(client_p, MASK_IP));
1252
1253 ilog(L_NOTICE, "Server %s closed the connection",
1254 get_client_name(client_p, SHOW_IP));
1255 }
1256 else
1257 {
1258 report_error(L_ADMIN, "Lost connection to %s: %d",
1259 get_client_name(client_p, SHOW_IP), current_error);
1260 report_error(L_OPER, "Lost connection to %s: %d",
1261 get_client_name(client_p, MASK_IP), current_error);
1262 }
1263
1264 sendto_realops_flags(UMODE_ALL, L_ALL,
1265 "%s had been connected for %d day%s, %2d:%02d:%02d",
1266 client_p->name, connected/86400,
1267 (connected/86400 == 1) ? "" : "s",
1268 (connected % 86400) / 3600, (connected % 3600) / 60,
1269 connected % 60);
1270 }
1271
1272 if (error == 0)
1273 strlcpy(errmsg, "Remote host closed the connection",
1274 sizeof(errmsg));
1275 else
1276 ircsprintf(errmsg, "Read error: %s",
1277 strerror(current_error));
1278
1279 exit_client(client_p, &me, errmsg);
1280 }
1281
1282 void
1283 exit_aborted_clients(void)
1284 {
1285 dlink_node *ptr;
1286 struct Client *target_p;
1287 const char *notice;
1288
1289 DLINK_FOREACH_SAFE(ptr, eac_next, abort_list.head)
1290 {
1291 target_p = ptr->data;
1292 eac_next = ptr->next;
1293
1294 if (target_p == NULL)
1295 {
1296 sendto_realops_flags(UMODE_ALL, L_ALL,
1297 "Warning: null client on abort_list!");
1298 dlinkDelete(ptr, &abort_list);
1299 free_dlink_node(ptr);
1300 continue;
1301 }
1302
1303 dlinkDelete(ptr, &abort_list);
1304
1305 if (IsSendQExceeded(target_p))
1306 notice = "Max SendQ exceeded";
1307 else
1308 notice = "Write error: connection closed";
1309
1310 exit_client(target_p, &me, notice);
1311 free_dlink_node(ptr);
1312 }
1313 }
1314
1315 /*
1316 * accept processing, this adds a form of "caller ID" to ircd
1317 *
1318 * If a client puts themselves into "caller ID only" mode,
1319 * only clients that match a client pointer they have put on
1320 * the accept list will be allowed to message them.
1321 *
1322 * Diane Bruce, "Dianora" db@db.net
1323 */
1324
1325 void
1326 del_accept(struct Accept *acceptvar, struct Client *client_p)
1327 {
1328 dlinkDelete(&acceptvar->node, &client_p->localClient->acceptlist);
1329
1330 MyFree(acceptvar->nick);
1331 MyFree(acceptvar->user);
1332 MyFree(acceptvar->host);
1333 MyFree(acceptvar);
1334 }
1335
1336 struct Accept *
1337 find_accept(const char *nick, const char *user,
1338 const char *host, struct Client *client_p, int do_match)
1339 {
1340 dlink_node *ptr = NULL;
1341 /* XXX We wouldn't need that if match() would return 0 on match */
1342 int (*cmpfunc)(const char *, const char *) = do_match ? match : irccmp;
1343
1344 DLINK_FOREACH(ptr, client_p->localClient->acceptlist.head)
1345 {
1346 struct Accept *acceptvar = ptr->data;
1347
1348 if (cmpfunc(acceptvar->nick, nick) == do_match &&
1349 cmpfunc(acceptvar->user, user) == do_match &&
1350 cmpfunc(acceptvar->host, host) == do_match)
1351 return acceptvar;
1352 }
1353
1354 return NULL;
1355 }
1356
1357 /* accept_message()
1358 *
1359 * inputs - pointer to source client
1360 * - pointer to target client
1361 * output - 1 if accept this message 0 if not
1362 * side effects - See if source is on target's allow list
1363 */
1364 int
1365 accept_message(struct Client *source,
1366 struct Client *target)
1367 {
1368 dlink_node *ptr = NULL;
1369
1370 if (source == target || find_accept(source->name, source->username,
1371 source->host, target, 1))
1372 return 1;
1373
1374 if (IsSoftCallerId(target))
1375 DLINK_FOREACH(ptr, target->channel.head)
1376 if (IsMember(source, ((struct Membership *)ptr->data)->chptr))
1377 return 1;
1378
1379 return 0;
1380 }
1381
1382 /* del_all_accepts()
1383 *
1384 * inputs - pointer to exiting client
1385 * output - NONE
1386 * side effects - Walk through given clients acceptlist and remove all entries
1387 */
1388 void
1389 del_all_accepts(struct Client *client_p)
1390 {
1391 dlink_node *ptr = NULL, *next_ptr = NULL;
1392
1393 DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->acceptlist.head)
1394 del_accept(ptr->data, client_p);
1395 }
1396
1397 /* set_initial_nick()
1398 *
1399 * inputs
1400 * output
1401 * side effects -
1402 *
1403 * This function is only called to set up an initially registering
1404 * client.
1405 */
1406 void
1407 set_initial_nick(struct Client *source_p, const char *nick)
1408 {
1409 char buf[USERLEN + 1];
1410
1411 /* This had to be copied here to avoid problems.. */
1412 source_p->tsinfo = CurrentTime;
1413 source_p->localClient->registration &= ~REG_NEED_NICK;
1414
1415 if (source_p->name[0])
1416 hash_del_client(source_p);
1417
1418 strlcpy(source_p->name, nick, sizeof(source_p->name));
1419 hash_add_client(source_p);
1420
1421 /* fd_desc is long enough */
1422 fd_note(&source_p->localClient->fd, "Nick: %s", nick);
1423
1424 if (!source_p->localClient->registration)
1425 {
1426 strlcpy(buf, source_p->username, sizeof(buf));
1427
1428 /*
1429 * USER already received, now we have NICK.
1430 * *NOTE* For servers "NICK" *must* precede the
1431 * user message (giving USER before NICK is possible
1432 * only for local client connection!). register_user
1433 * may reject the client and call exit_client for it
1434 * --must test this and exit m_nick too!!!
1435 */
1436 register_local_user(source_p, buf);
1437 }
1438 }
1439
1440 /* change_local_nick()
1441 *
1442 * inputs - pointer to server
1443 * - pointer to client
1444 * - nick
1445 * output -
1446 * side effects - changes nick of a LOCAL user
1447 */
1448 void
1449 change_local_nick(struct Client *client_p, struct Client *source_p, const char *nick)
1450 {
1451 int samenick = 0;
1452
1453 /*
1454 * Client just changing his/her nick. If he/she is
1455 * on a channel, send note of change to all clients
1456 * on that channel. Propagate notice to other servers.
1457 */
1458 if ((source_p->localClient->last_nick_change +
1459 ConfigFileEntry.max_nick_time) < CurrentTime)
1460 source_p->localClient->number_of_nick_changes = 0;
1461 source_p->localClient->last_nick_change = CurrentTime;
1462 source_p->localClient->number_of_nick_changes++;
1463
1464 if ((ConfigFileEntry.anti_nick_flood &&
1465 (source_p->localClient->number_of_nick_changes
1466 <= ConfigFileEntry.max_nick_changes)) ||
1467 !ConfigFileEntry.anti_nick_flood ||
1468 (IsOper(source_p) && ConfigFileEntry.no_oper_flood))
1469 {
1470 samenick = !irccmp(source_p->name, nick);
1471 if (!samenick)
1472 source_p->tsinfo = CurrentTime;
1473
1474 /* XXX - the format of this notice should eventually be changed
1475 * to either %s[%s@%s], or even better would be get_client_name() -bill
1476 */
1477 sendto_realops_flags(UMODE_NCHANGE, L_ALL, "Nick change: From %s to %s [%s@%s]",
1478 source_p->name, nick, source_p->username, source_p->host);
1479 sendto_common_channels_local(source_p, 1, ":%s!%s@%s NICK :%s",
1480 source_p->name, source_p->username,
1481 source_p->host, nick);
1482
1483 add_history(source_p, 1);
1484
1485 /* Only hubs care about lazy link nicks not being sent on yet
1486 * lazylink leafs/leafs always send their nicks up to hub,
1487 * hence must always propagate nick changes.
1488 * hubs might not propagate a nick change, if the leaf
1489 * does not know about that client yet.
1490 */
1491 sendto_server(client_p, source_p, NULL, CAP_TS6, NOCAPS,
1492 ":%s NICK %s :%lu",
1493 ID(source_p), nick, (unsigned long)source_p->tsinfo);
1494 sendto_server(client_p, source_p, NULL, NOCAPS, CAP_TS6,
1495 ":%s NICK %s :%lu",
1496 source_p->name, nick, (unsigned long)source_p->tsinfo);
1497 }
1498 else
1499 {
1500 sendto_one(source_p, form_str(ERR_NICKTOOFAST),
1501 me.name, source_p->name, source_p->name,
1502 nick, ConfigFileEntry.max_nick_time);
1503 return;
1504 }
1505
1506 /* Finally, add to hash */
1507 assert(source_p->name[0]);
1508
1509 hash_del_client(source_p);
1510 if (!samenick)
1511 hash_check_watch(source_p, RPL_LOGOFF);
1512 strcpy(source_p->name, nick);
1513 hash_add_client(source_p);
1514
1515 if (!samenick)
1516 hash_check_watch(source_p, RPL_LOGON);
1517
1518 /* fd_desc is long enough */
1519 fd_note(&client_p->localClient->fd, "Nick: %s", nick);
1520 }
1521
1522 /* log_user_exit()
1523 *
1524 * inputs - pointer to connecting client
1525 * output - NONE
1526 * side effects - Current exiting client is logged to
1527 * either SYSLOG or to file.
1528 */
1529 void
1530 log_user_exit(struct Client *source_p)
1531 {
1532 time_t on_for = CurrentTime - source_p->firsttime;
1533 #ifdef SYSLOG_USERS
1534 if (IsClient(source_p))
1535 {
1536 ilog(L_INFO, "%s (%3ld:%02ld:%02ld): %s!%s@%s %llu/%llu\n",
1537 myctime(source_p->firsttime),
1538 (signed long) on_for / 3600,
1539 (signed long) (on_for % 3600)/60,
1540 (signed long) on_for % 60,
1541 source_p->name, source_p->username, source_p->host,
1542 source_p->localClient->send.bytes>>10,
1543 source_p->localClient->recv.bytes>>10);
1544 }
1545 #else
1546 {
1547 char linebuf[BUFSIZ];
1548
1549 /*
1550 * This conditional makes the logfile active only after
1551 * it's been created - thus logging can be turned off by
1552 * removing the file.
1553 * -Taner
1554 */
1555 if (IsClient(source_p))
1556 {
1557 if (user_log_fb == NULL)
1558 {
1559 if ((ConfigLoggingEntry.userlog[0] != '\0') &&
1560 (user_log_fb = fbopen(ConfigLoggingEntry.userlog, "r")) != NULL)
1561 {
1562 fbclose(user_log_fb);
1563 user_log_fb = fbopen(ConfigLoggingEntry.userlog, "a");
1564 }
1565 }
1566
1567 if (user_log_fb != NULL)
1568 {
1569 size_t nbytes = ircsprintf(linebuf,
1570 "%s (%3ld:%02ld:%02ld): %s!%s@%s %llu/%llu\n",
1571 myctime(source_p->firsttime),
1572 (signed long) on_for / 3600,
1573 (signed long) (on_for % 3600)/60,
1574 (signed long) on_for % 60,
1575 source_p->name, source_p->username, source_p->host,
1576 source_p->localClient->send.bytes>>10,
1577 source_p->localClient->recv.bytes>>10);
1578 fbputs(linebuf, user_log_fb, nbytes);
1579 }
1580 }
1581 }
1582 #endif
1583 }
1584
1585
1586 /* log_oper_action()
1587 *
1588 * inputs - type of oper log entry
1589 * - pointer to oper
1590 * - const char *pattern == format string
1591 * - var args for format string
1592 * output - none
1593 * side effects - corresponding log is written to, if its present.
1594 *
1595 * rewritten sept 5 2005 - Dianora
1596 */
1597 void
1598 log_oper_action(int log_type, const struct Client *source_p,
1599 const char *pattern, ...)
1600 {
1601 va_list args;
1602 char linebuf[IRCD_BUFSIZE];
1603 FBFILE *log_fb;
1604 char *logfile;
1605 const char *log_message;
1606 size_t nbytes;
1607 size_t n_preamble;
1608 char *p;
1609
1610 switch(log_type)
1611 {
1612 case LOG_OPER_TYPE:
1613 logfile = ConfigLoggingEntry.operlog;
1614 log_message = "OPER";
1615 break;
1616 case LOG_FAILED_OPER_TYPE:
1617 logfile = ConfigLoggingEntry.failed_operlog;
1618 log_message = "FAILED OPER";
1619 break;
1620 case LOG_KLINE_TYPE:
1621 logfile = ConfigLoggingEntry.klinelog;
1622 log_message = "KLINE";
1623 break;
1624 case LOG_RKLINE_TYPE:
1625 logfile = ConfigLoggingEntry.klinelog;
1626 log_message = "RKLINE";
1627 break;
1628 case LOG_DLINE_TYPE:
1629 logfile = ConfigLoggingEntry.klinelog;
1630 log_message = "DLINE";
1631 break;
1632 case LOG_TEMP_DLINE_TYPE:
1633 logfile = ConfigLoggingEntry.klinelog;
1634 log_message = "TEMP DLINE";
1635 break;
1636 case LOG_TEMP_KLINE_TYPE:
1637 logfile = ConfigLoggingEntry.klinelog;
1638 log_message = "TEMP KLINE";
1639 break;
1640 case LOG_GLINE_TYPE:
1641 logfile = ConfigLoggingEntry.glinelog;
1642 log_message = "GLINE";
1643 break;
1644 case LOG_KILL_TYPE:
1645 logfile = ConfigLoggingEntry.killlog;
1646 log_message = "KILL";
1647 break;
1648 case LOG_IOERR_TYPE:
1649 logfile = ConfigLoggingEntry.ioerrlog;
1650 log_message = "IO ERR";
1651 break;
1652 default:
1653 return;
1654 }
1655
1656 if (*logfile == '\0')
1657 return;
1658
1659 p = linebuf;
1660 if (source_p != NULL)
1661 {
1662 n_preamble = ircsprintf(linebuf, "%s %s by (%s!%s@%s) :",
1663 myctime(CurrentTime), log_message,
1664 source_p->name, source_p->username, source_p->host);
1665
1666 }
1667 else
1668 {
1669 n_preamble = ircsprintf(linebuf, "%s %s :",
1670 myctime(CurrentTime), log_message);
1671 }
1672
1673 p += n_preamble;
1674
1675 if ((log_fb = fbopen(logfile, "r")) != NULL)
1676 {
1677 fbclose(log_fb);
1678 log_fb = fbopen(logfile, "a");
1679 if (log_fb == NULL)
1680 return;
1681 va_start(args, pattern);
1682 /* XXX add check for IRCD_BUFSIZE-(n_preamble+1) < 0 ? -db */
1683 nbytes = vsnprintf(p, IRCD_BUFSIZE-(n_preamble+1), pattern, args);
1684 nbytes += n_preamble;
1685 va_end(args);
1686 fbputs(linebuf, log_fb, nbytes);
1687 fbclose(log_fb);
1688 }
1689 }

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28