ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 1124
Committed: Mon Feb 7 11:45:27 2011 UTC (13 years, 1 month ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.3/src/client.c
File size: 39458 byte(s)
Log Message:
- replace several ircsprintf with snprintf

File Contents

# User Rev Content
1 adx 30 /*
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 knight 31 * $Id$
23 adx 30 */
24    
25     #include "stdinc.h"
26 michael 1011 #include "list.h"
27 adx 30 #include "client.h"
28     #include "channel_mode.h"
29     #include "common.h"
30     #include "event.h"
31     #include "fdlist.h"
32     #include "hash.h"
33     #include "irc_string.h"
34     #include "ircd.h"
35     #include "s_gline.h"
36     #include "numeric.h"
37     #include "packet.h"
38     #include "s_auth.h"
39     #include "s_bsd.h"
40     #include "s_conf.h"
41     #include "s_log.h"
42     #include "s_misc.h"
43     #include "s_serv.h"
44     #include "send.h"
45     #include "whowas.h"
46     #include "s_user.h"
47     #include "dbuf.h"
48     #include "memory.h"
49     #include "hostmask.h"
50     #include "balloc.h"
51     #include "listener.h"
52     #include "irc_res.h"
53     #include "userhost.h"
54 michael 876 #include "watch.h"
55 adx 30
56     dlink_list listing_client_list = { NULL, NULL, 0 };
57     /* Pointer to beginning of Client list */
58     dlink_list global_client_list = {NULL, NULL, 0};
59     /* unknown/client pointer lists */
60     dlink_list unknown_list = {NULL, NULL, 0};
61     dlink_list local_client_list = {NULL, NULL, 0};
62     dlink_list serv_list = {NULL, NULL, 0};
63     dlink_list global_serv_list = {NULL, NULL, 0};
64     dlink_list oper_list = {NULL, NULL, 0};
65    
66     static EVH check_pings;
67    
68     static BlockHeap *client_heap = NULL;
69     static BlockHeap *lclient_heap = NULL;
70    
71     static dlink_list dead_list = { NULL, NULL, 0};
72     static dlink_list abort_list = { NULL, NULL, 0};
73    
74     static dlink_node *eac_next; /* next aborted client to exit */
75    
76     static void check_pings_list(dlink_list *);
77     static void check_unknowns_list(void);
78 michael 1124 static void ban_them(struct Client *, struct ConfItem *);
79 adx 30
80    
81     /* init_client()
82     *
83     * inputs - NONE
84     * output - NONE
85     * side effects - initialize client free memory
86     */
87     void
88     init_client(void)
89     {
90     /* start off the check ping event .. -- adrian
91     * Every 30 seconds is plenty -- db
92     */
93     client_heap = BlockHeapCreate("client", sizeof(struct Client), CLIENT_HEAP_SIZE);
94     lclient_heap = BlockHeapCreate("local client", sizeof(struct LocalUser), LCLIENT_HEAP_SIZE);
95     eventAdd("check_pings", check_pings, NULL, 5);
96     }
97    
98     /*
99     * make_client - create a new Client struct and set it to initial state.
100     *
101     * from == NULL, create local client (a client connected
102     * to a socket).
103     * WARNING: This leaves the client in a dangerous
104     * state where fd == -1, dead flag is not set and
105     * the client is on the unknown_list; therefore,
106     * the first thing to do after calling make_client(NULL)
107     * is setting fd to something reasonable. -adx
108     *
109     * from, create remote client (behind a socket
110     * associated with the client defined by
111     * 'from'). ('from' is a local client!!).
112     */
113     struct Client *
114     make_client(struct Client *from)
115     {
116     struct Client *client_p = BlockHeapAlloc(client_heap);
117    
118     if (from == NULL)
119     {
120     client_p->from = client_p; /* 'from' of local client is self! */
121     client_p->since = client_p->lasttime = client_p->firsttime = CurrentTime;
122    
123     client_p->localClient = BlockHeapAlloc(lclient_heap);
124 michael 503 client_p->localClient->registration = REG_INIT;
125 adx 30 /* as good a place as any... */
126     dlinkAdd(client_p, make_dlink_node(), &unknown_list);
127     }
128     else
129     client_p->from = from; /* 'from' of local client is self! */
130    
131     client_p->hnext = client_p;
132     client_p->status = STAT_UNKNOWN;
133     strcpy(client_p->username, "unknown");
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 != NULL);
149     assert(client_p != &me);
150     assert(client_p->hnext == client_p);
151     assert(client_p->channel.head == NULL);
152     assert(dlink_list_length(&client_p->channel) == 0);
153    
154     MyFree(client_p->away);
155     MyFree(client_p->serv);
156    
157     if (MyConnect(client_p))
158     {
159 michael 317 assert(client_p->localClient->invited.head == NULL);
160     assert(dlink_list_length(&client_p->localClient->invited) == 0);
161 adx 30 assert(IsClosing(client_p) && IsDead(client_p));
162    
163     MyFree(client_p->localClient->response);
164     MyFree(client_p->localClient->auth_oper);
165    
166     /*
167     * clean up extra sockets from P-lines which have been discarded.
168     */
169     if (client_p->localClient->listener)
170     {
171     assert(0 < client_p->localClient->listener->ref_count);
172     if (0 == --client_p->localClient->listener->ref_count &&
173     !client_p->localClient->listener->active)
174     free_listener(client_p->localClient->listener);
175     }
176    
177     dbuf_clear(&client_p->localClient->buf_recvq);
178     dbuf_clear(&client_p->localClient->buf_sendq);
179    
180     BlockHeapFree(lclient_heap, client_p->localClient);
181     }
182    
183     BlockHeapFree(client_heap, client_p);
184     }
185    
186     /*
187     * check_pings - go through the local client list and check activity
188     * kill off stuff that should die
189     *
190     * inputs - NOT USED (from event)
191     * output - next time_t when check_pings() should be called again
192     * side effects -
193     *
194     *
195     * A PING can be sent to clients as necessary.
196     *
197     * Client/Server ping outs are handled.
198     */
199    
200     /*
201     * Addon from adrian. We used to call this after nextping seconds,
202     * however I've changed it to run once a second. This is only for
203     * PING timeouts, not K/etc-line checks (thanks dianora!). Having it
204     * run once a second makes life a lot easier - when a new client connects
205     * and they need a ping in 4 seconds, if nextping was set to 20 seconds
206     * we end up waiting 20 seconds. This is stupid. :-)
207     * I will optimise (hah!) check_pings() once I've finished working on
208     * tidying up other network IO evilnesses.
209     * -- adrian
210     */
211    
212     static void
213     check_pings(void *notused)
214     {
215     check_pings_list(&local_client_list);
216     check_pings_list(&serv_list);
217     check_unknowns_list();
218     }
219    
220     /* check_pings_list()
221     *
222     * inputs - pointer to list to check
223     * output - NONE
224     * side effects -
225     */
226     static void
227     check_pings_list(dlink_list *list)
228     {
229     char scratch[32]; /* way too generous but... */
230     struct Client *client_p; /* current local client_p being examined */
231     int ping, pingwarn; /* ping time value from client */
232     dlink_node *ptr, *next_ptr;
233    
234     DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
235     {
236     client_p = ptr->data;
237    
238     /*
239     ** Note: No need to notify opers here. It's
240     ** already done when "FLAGS_DEADSOCKET" is set.
241     */
242     if (IsDead(client_p))
243     {
244     /* Ignore it, its been exited already */
245     continue;
246     }
247    
248     if (client_p->localClient->reject_delay > 0)
249     {
250     if (client_p->localClient->reject_delay <= CurrentTime)
251     exit_client(client_p, &me, "Rejected");
252     continue;
253     }
254    
255     if (GlobalSetOptions.idletime && IsClient(client_p))
256     {
257     if (!IsExemptKline(client_p) && !IsOper(client_p) &&
258     !IsIdlelined(client_p) &&
259     ((CurrentTime - client_p->localClient->last) > GlobalSetOptions.idletime))
260     {
261     struct ConfItem *conf;
262     struct AccessItem *aconf;
263    
264     conf = make_conf_item(KLINE_TYPE);
265 michael 1013 aconf = map_to_conf(conf);
266 adx 30
267     DupString(aconf->host, client_p->host);
268     DupString(aconf->reason, "idle exceeder");
269     DupString(aconf->user, client_p->username);
270     aconf->hold = CurrentTime + 60;
271     add_temp_line(conf);
272    
273     sendto_realops_flags(UMODE_ALL, L_ALL,
274     "Idle time limit exceeded for %s - temp k-lining",
275     get_client_name(client_p, HIDE_IP));
276     exit_client(client_p, &me, aconf->reason);
277     continue;
278     }
279     }
280    
281     if (!IsRegistered(client_p))
282     ping = CONNECTTIMEOUT, pingwarn = 0;
283     else
284     ping = get_client_ping(client_p, &pingwarn);
285    
286     if (ping < CurrentTime - client_p->lasttime)
287     {
288     if (!IsPingSent(client_p))
289     {
290     /*
291     * if we havent PINGed the connection and we havent
292     * heard from it in a while, PING it to make sure
293     * it is still alive.
294     */
295     SetPingSent(client_p);
296     ClearPingWarning(client_p);
297     client_p->lasttime = CurrentTime - ping;
298     sendto_one(client_p, "PING :%s", ID_or_name(&me, client_p));
299     }
300     else
301     {
302     if (CurrentTime - client_p->lasttime >= 2 * ping)
303     {
304     /*
305     * If the client/server hasn't talked to us in 2*ping seconds
306     * and it has a ping time, then close its connection.
307     */
308     if (IsServer(client_p) || IsHandshake(client_p))
309     {
310     sendto_realops_flags(UMODE_ALL, L_ADMIN,
311     "No response from %s, closing link",
312     get_client_name(client_p, HIDE_IP));
313     sendto_realops_flags(UMODE_ALL, L_OPER,
314     "No response from %s, closing link",
315     get_client_name(client_p, MASK_IP));
316     ilog(L_NOTICE, "No response from %s, closing link",
317     get_client_name(client_p, HIDE_IP));
318     }
319 michael 650
320 michael 1124 snprintf(scratch, sizeof(scratch), "Ping timeout: %d seconds",
321     (int)(CurrentTime - client_p->lasttime));
322 adx 30 exit_client(client_p, &me, scratch);
323     }
324     else if (!IsPingWarning(client_p) && pingwarn > 0 &&
325     (IsServer(client_p) || IsHandshake(client_p)) &&
326     CurrentTime - client_p->lasttime >= ping + pingwarn)
327     {
328     /*
329     * If the server hasn't replied in pingwarn seconds after sending
330     * the PING, notify the opers so that they are aware of the problem.
331     */
332     SetPingWarning(client_p);
333     sendto_realops_flags(UMODE_ALL, L_ADMIN,
334     "Warning, no response from %s in %d seconds",
335     get_client_name(client_p, HIDE_IP), pingwarn);
336     sendto_realops_flags(UMODE_ALL, L_OPER,
337     "Warning, no response from %s in %d seconds",
338     get_client_name(client_p, MASK_IP), pingwarn);
339     ilog(L_NOTICE, "No response from %s in %d seconds",
340     get_client_name(client_p, HIDE_IP), pingwarn);
341     }
342     }
343     }
344     }
345     }
346    
347     /* check_unknowns_list()
348     *
349     * inputs - pointer to list of unknown clients
350     * output - NONE
351     * side effects - unknown clients get marked for termination after n seconds
352     */
353     static void
354     check_unknowns_list(void)
355     {
356     dlink_node *ptr, *next_ptr;
357    
358     DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
359     {
360     struct Client *client_p = ptr->data;
361    
362     if (client_p->localClient->reject_delay > 0)
363     {
364     if (client_p->localClient->reject_delay <= CurrentTime)
365 michael 650 exit_client(client_p, &me, "Rejected");
366 adx 30 continue;
367     }
368    
369 michael 650 /*
370     * Check UNKNOWN connections - if they have been in this state
371 adx 30 * for > 30s, close them.
372     */
373 michael 650 if (IsAuthFinished(client_p) && (CurrentTime - client_p->firsttime) > 30)
374     exit_client(client_p, &me, "Registration timed out");
375 adx 30 }
376     }
377    
378     /* check_conf_klines()
379     *
380     * inputs - NONE
381     * output - NONE
382     * side effects - Check all connections for a pending kline against the
383     * client, exit the client if a kline matches.
384     */
385     void
386     check_conf_klines(void)
387     {
388     struct Client *client_p = NULL; /* current local client_p being examined */
389     struct AccessItem *aconf = NULL;
390     struct ConfItem *conf = NULL;
391     dlink_node *ptr, *next_ptr;
392    
393     DLINK_FOREACH_SAFE(ptr, next_ptr, local_client_list.head)
394     {
395     client_p = ptr->data;
396    
397     /* If a client is already being exited
398     */
399     if (IsDead(client_p) || !IsClient(client_p))
400     continue;
401    
402     /* if there is a returned struct ConfItem then kill it */
403     if ((aconf = find_dline_conf(&client_p->localClient->ip,
404     client_p->localClient->aftype)) != NULL)
405     {
406     if (aconf->status & CONF_EXEMPTDLINE)
407     continue;
408    
409     conf = unmap_conf_item(aconf);
410     ban_them(client_p, conf);
411     continue; /* and go examine next fd/client_p */
412     }
413    
414     if (ConfigFileEntry.glines && (aconf = find_gline(client_p)))
415     {
416     if (IsExemptKline(client_p) ||
417     IsExemptGline(client_p))
418     {
419     sendto_realops_flags(UMODE_ALL, L_ALL,
420     "GLINE over-ruled for %s, client is %sline_exempt",
421     get_client_name(client_p, HIDE_IP), IsExemptKline(client_p) ? "k" : "g");
422     continue;
423     }
424    
425     conf = unmap_conf_item(aconf);
426     ban_them(client_p, conf);
427     /* and go examine next fd/client_p */
428     continue;
429     }
430    
431     if ((aconf = find_kill(client_p)) != NULL)
432     {
433    
434     /* if there is a returned struct AccessItem.. then kill it */
435     if (IsExemptKline(client_p))
436     {
437     sendto_realops_flags(UMODE_ALL, L_ALL,
438     "KLINE over-ruled for %s, client is kline_exempt",
439     get_client_name(client_p, HIDE_IP));
440     continue;
441     }
442    
443     conf = unmap_conf_item(aconf);
444     ban_them(client_p, conf);
445     continue;
446     }
447    
448     /* if there is a returned struct MatchItem then kill it */
449     if ((conf = find_matching_name_conf(XLINE_TYPE, client_p->info,
450     NULL, NULL, 0)) != NULL ||
451     (conf = find_matching_name_conf(RXLINE_TYPE, client_p->info,
452     NULL, NULL, 0)) != NULL)
453     {
454     ban_them(client_p, conf);
455     continue;
456     }
457     }
458    
459     /* also check the unknowns list for new dlines */
460     DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
461     {
462     client_p = ptr->data;
463    
464     if ((aconf = find_dline_conf(&client_p->localClient->ip,
465     client_p->localClient->aftype)))
466     {
467     if (aconf->status & CONF_EXEMPTDLINE)
468     continue;
469    
470     exit_client(client_p, &me, "D-lined");
471     }
472     }
473     }
474    
475     /*
476     * ban_them
477     *
478     * inputs - pointer to client to ban
479     * - pointer to ConfItem
480     * output - NONE
481     * side effects - given client_p is banned
482     */
483     static void
484     ban_them(struct Client *client_p, struct ConfItem *conf)
485     {
486     const char *user_reason = NULL; /* What is sent to user */
487     const char *channel_reason = NULL; /* What is sent to channel */
488     struct AccessItem *aconf = NULL;
489     struct MatchItem *xconf = NULL;
490     const char *type_string = NULL;
491     const char dline_string[] = "D-line";
492     const char kline_string[] = "K-line";
493     const char gline_string[] = "G-line";
494     const char xline_string[] = "X-line";
495    
496     switch (conf->type)
497     {
498     case RKLINE_TYPE:
499     case KLINE_TYPE:
500     type_string = kline_string;
501     aconf = map_to_conf(conf);
502     break;
503     case DLINE_TYPE:
504     type_string = dline_string;
505     aconf = map_to_conf(conf);
506     break;
507     case GLINE_TYPE:
508     type_string = gline_string;
509     aconf = map_to_conf(conf);
510     break;
511     case RXLINE_TYPE:
512     case XLINE_TYPE:
513     type_string = xline_string;
514     xconf = map_to_conf(conf);
515     ++xconf->count;
516     break;
517     default:
518     assert(0);
519     break;
520     }
521    
522     if (ConfigFileEntry.kline_with_reason)
523     {
524     if (aconf != NULL)
525     user_reason = aconf->reason ? aconf->reason : type_string;
526     if (xconf != NULL)
527     user_reason = xconf->reason ? xconf->reason : type_string;
528     }
529     else
530     user_reason = type_string;
531    
532     if (ConfigFileEntry.kline_reason != NULL)
533     channel_reason = ConfigFileEntry.kline_reason;
534     else
535     channel_reason = user_reason;
536    
537     sendto_realops_flags(UMODE_ALL, L_ALL, "%s active for %s",
538     type_string, get_client_name(client_p, HIDE_IP));
539    
540     if (IsClient(client_p))
541     sendto_one(client_p, form_str(ERR_YOUREBANNEDCREEP),
542     me.name, client_p->name, user_reason);
543    
544     exit_client(client_p, &me, channel_reason);
545     }
546    
547     /* update_client_exit_stats()
548     *
549     * input - pointer to client
550     * output - NONE
551     * side effects -
552     */
553     static void
554     update_client_exit_stats(struct Client *client_p)
555     {
556     if (IsServer(client_p))
557     {
558 michael 1013 sendto_realops_flags(UMODE_EXTERNAL, L_ALL, "Server %s split from %s",
559     client_p->name, client_p->servptr->name);
560 adx 30 }
561     else if (IsClient(client_p))
562     {
563 michael 1013 assert(Count.total > 0);
564 adx 30 --Count.total;
565     if (IsOper(client_p))
566     --Count.oper;
567     if (IsInvisible(client_p))
568     --Count.invisi;
569     }
570    
571     if (splitchecking && !splitmode)
572     check_splitmode(NULL);
573     }
574    
575     /* find_person()
576     *
577     * inputs - pointer to name
578     * output - return client pointer
579     * side effects - find person by (nick)name
580     */
581     /* XXX - ugly wrapper */
582     struct Client *
583     find_person(const struct Client *client_p, const char *name)
584     {
585     struct Client *c2ptr;
586    
587     if (IsDigit(*name))
588     {
589     if ((c2ptr = hash_find_id(name)) != NULL)
590     {
591     /* invisible users shall not be found by UID guessing */
592     if (IsInvisible(c2ptr) && !IsServer(client_p))
593     c2ptr = NULL;
594     }
595     }
596     else
597     c2ptr = find_client(name);
598    
599     return ((c2ptr != NULL && IsClient(c2ptr)) ? c2ptr : NULL);
600     }
601    
602     /*
603     * find_chasing - find the client structure for a nick name (user)
604     * using history mechanism if necessary. If the client is not found,
605     * an error message (NO SUCH NICK) is generated. If the client was found
606     * through the history, chasing will be 1 and otherwise 0.
607     */
608     struct Client *
609     find_chasing(struct Client *client_p, struct Client *source_p, const char *user, int *chasing)
610     {
611     struct Client *who = find_person(client_p, user);
612    
613     if (chasing)
614     *chasing = 0;
615    
616     if (who)
617     return(who);
618    
619     if (IsDigit(*user))
620     return(NULL);
621    
622     if ((who = get_history(user,
623     (time_t)ConfigFileEntry.kill_chase_time_limit))
624     == NULL)
625     {
626     sendto_one(source_p, form_str(ERR_NOSUCHNICK),
627     me.name, source_p->name, user);
628     return(NULL);
629     }
630    
631     if (chasing)
632     *chasing = 1;
633    
634     return(who);
635     }
636    
637     /*
638     * get_client_name - Return the name of the client
639     * for various tracking and
640     * admin purposes. The main purpose of this function is to
641     * return the "socket host" name of the client, if that
642     * differs from the advertised name (other than case).
643     * But, this can be used to any client structure.
644     *
645     * NOTE 1:
646     * Watch out the allocation of "nbuf", if either source_p->name
647     * or source_p->sockhost gets changed into pointers instead of
648     * directly allocated within the structure...
649     *
650     * NOTE 2:
651     * Function return either a pointer to the structure (source_p) or
652     * to internal buffer (nbuf). *NEVER* use the returned pointer
653     * to modify what it points!!!
654     */
655     const char *
656 michael 1124 get_client_name(const struct Client *client, int showip)
657 adx 30 {
658     static char nbuf[HOSTLEN * 2 + USERLEN + 5];
659    
660     assert(client != NULL);
661    
662     if (irccmp(client->name, client->host) == 0)
663 michael 1124 return client->name;
664 adx 30
665     if (ConfigServerHide.hide_server_ips)
666     if (IsServer(client) || IsConnecting(client) || IsHandshake(client))
667     showip = MASK_IP;
668    
669     if (ConfigFileEntry.hide_spoof_ips)
670     if (showip == SHOW_IP && IsIPSpoof(client))
671     showip = MASK_IP;
672    
673     /* And finally, let's get the host information, ip or name */
674     switch (showip)
675     {
676     case SHOW_IP:
677     if (MyConnect(client))
678     {
679 michael 1124 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
680     client->name,
681     client->username, client->sockhost);
682 adx 30 break;
683     }
684     case MASK_IP:
685 michael 1124 snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
686     client->name, client->username);
687 adx 30 break;
688     default:
689 michael 1124 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
690     client->name,
691     client->username, client->host);
692 adx 30 }
693    
694 michael 1124 return nbuf;
695 adx 30 }
696    
697     void
698     free_exited_clients(void)
699     {
700 michael 887 dlink_node *ptr = NULL, *next = NULL;
701 adx 30
702     DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
703     {
704 michael 887 free_client(ptr->data);
705 adx 30 dlinkDelete(ptr, &dead_list);
706     free_dlink_node(ptr);
707     }
708     }
709    
710     /*
711     * Exit one client, local or remote. Assuming all dependents have
712     * been already removed, and socket closed for local client.
713     *
714     * The only messages generated are QUITs on channels.
715     */
716     static void
717     exit_one_client(struct Client *source_p, const char *quitmsg)
718     {
719     dlink_node *lp = NULL, *next_lp = NULL;
720    
721     assert(!IsMe(source_p));
722    
723 michael 1118 if (IsClient(source_p))
724 adx 30 {
725     if (source_p->servptr->serv != NULL)
726 michael 889 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->client_list);
727 adx 30
728 michael 1118 /*
729     * If a person is on a channel, send a QUIT notice
730     * to every client (person) on the same channel (so
731     * that the client can show the "**signoff" message).
732     * (Note: The notice is to the local clients *only*)
733     */
734 adx 30 sendto_common_channels_local(source_p, 0, ":%s!%s@%s QUIT :%s",
735     source_p->name, source_p->username,
736     source_p->host, quitmsg);
737     DLINK_FOREACH_SAFE(lp, next_lp, source_p->channel.head)
738     remove_user_from_channel(lp->data);
739    
740     add_history(source_p, 0);
741     off_history(source_p);
742    
743 michael 876 watch_check_hash(source_p, RPL_LOGOFF);
744    
745 michael 889 if (MyConnect(source_p))
746 adx 30 {
747 michael 317 /* Clean up invitefield */
748     DLINK_FOREACH_SAFE(lp, next_lp, source_p->localClient->invited.head)
749     del_invite(lp->data, source_p);
750 michael 887
751     del_all_accepts(source_p);
752 michael 317 }
753 adx 30 }
754 michael 1118 else if (IsServer(source_p))
755     {
756     dlinkDelete(&source_p->lnode, &source_p->servptr->serv->server_list);
757 adx 30
758 michael 1118 if ((lp = dlinkFindDelete(&global_serv_list, source_p)) != NULL)
759     free_dlink_node(lp);
760     }
761    
762 adx 30 /* Remove source_p from the client lists */
763     if (HasID(source_p))
764     hash_del_id(source_p);
765     if (source_p->name[0])
766     hash_del_client(source_p);
767    
768     if (IsUserHostIp(source_p))
769     delete_user_host(source_p->username, source_p->host, !MyConnect(source_p));
770    
771     /* remove from global client list
772     * NOTE: source_p->node.next cannot be NULL if the client is added
773     * to global_client_list (there is always &me at its end)
774     */
775     if (source_p != NULL && source_p->node.next != NULL)
776     dlinkDelete(&source_p->node, &global_client_list);
777    
778     update_client_exit_stats(source_p);
779    
780     /* Check to see if the client isn't already on the dead list */
781     assert(dlinkFind(&dead_list, source_p) == NULL);
782    
783     /* add to dead client dlist */
784     SetDead(source_p);
785     dlinkAdd(source_p, make_dlink_node(), &dead_list);
786     }
787    
788     /* Recursively send QUITs and SQUITs for source_p and all its dependent clients
789     * and servers to those servers that need them. A server needs the client
790     * QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it
791     * isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once
792     * a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending
793     * on that one -orabidoo
794     *
795     * This is now called on each local server -adx
796     */
797     static void
798     recurse_send_quits(struct Client *original_source_p, struct Client *source_p,
799     struct Client *from, struct Client *to, const char *comment,
800 michael 1118 const char *splitstr)
801 adx 30 {
802     dlink_node *ptr, *next;
803     struct Client *target_p;
804 michael 1118 int hidden = match(me.name, source_p->name); /* XXX */
805 adx 30
806     assert(to != source_p); /* should be already removed from serv_list */
807    
808     /* If this server can handle quit storm (QS) removal
809     * of dependents, just send the SQUIT
810     *
811     * Always check *all* dependent servers if some of them are
812     * hidden behind fakename. If so, send out the QUITs -adx
813     */
814     if (hidden || !IsCapable(to, CAP_QS))
815 michael 889 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->client_list.head)
816 adx 30 {
817     target_p = ptr->data;
818     sendto_one(to, ":%s QUIT :%s", target_p->name, splitstr);
819     }
820    
821 michael 889 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->server_list.head)
822 adx 30 recurse_send_quits(original_source_p, ptr->data, from, to,
823 michael 1118 comment, splitstr);
824 adx 30
825     if (!hidden && ((source_p == original_source_p && to != from) ||
826     !IsCapable(to, CAP_QS)))
827     {
828     /* don't use a prefix here - we have to be 100% sure the message
829     * will be accepted without Unknown prefix etc.. */
830     sendto_one(to, "SQUIT %s :%s", ID_or_name(source_p, to), comment);
831     }
832     }
833    
834     /*
835     * Remove all clients that depend on source_p; assumes all (S)QUITs have
836     * already been sent. we make sure to exit a server's dependent clients
837     * and servers before the server itself; exit_one_client takes care of
838     * actually removing things off llists. tweaked from +CSr31 -orabidoo
839     */
840     static void
841     recurse_remove_clients(struct Client *source_p, const char *quitmsg)
842     {
843     dlink_node *ptr, *next;
844    
845 michael 889 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->client_list.head)
846 adx 30 exit_one_client(ptr->data, quitmsg);
847    
848 michael 889 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->server_list.head)
849 adx 30 {
850     recurse_remove_clients(ptr->data, quitmsg);
851     exit_one_client(ptr->data, quitmsg);
852     }
853     }
854    
855     /*
856     ** Remove *everything* that depends on source_p, from all lists, and sending
857     ** all necessary QUITs and SQUITs. source_p itself is still on the lists,
858     ** and its SQUITs have been sent except for the upstream one -orabidoo
859     */
860     static void
861     remove_dependents(struct Client *source_p, struct Client *from,
862     const char *comment, const char *splitstr)
863     {
864 michael 1118 dlink_node *ptr = NULL;
865 adx 30
866     DLINK_FOREACH(ptr, serv_list.head)
867 michael 1118 recurse_send_quits(source_p, source_p, from, ptr->data,
868     comment, splitstr);
869 adx 30
870     recurse_remove_clients(source_p, splitstr);
871     }
872    
873     /*
874     * exit_client - exit a client of any type. Generally, you can use
875     * this on any struct Client, regardless of its state.
876     *
877     * Note, you shouldn't exit remote _users_ without first doing
878     * SetKilled and propagating a kill or similar message. However,
879     * it is perfectly correct to call exit_client to force a _server_
880     * quit (either local or remote one).
881     *
882     * inputs: - a client pointer that is going to be exited
883     * - for servers, the second argument is a pointer to who
884     * is firing the server. This side won't get any generated
885     * messages. NEVER NULL!
886     * output: none
887     * side effects: the client is delinked from all lists, disconnected,
888     * and the rest of IRC network is notified of the exit.
889     * Client memory is scheduled to be freed
890     */
891     void
892     exit_client(struct Client *source_p, struct Client *from, const char *comment)
893     {
894 michael 1118 dlink_node *m = NULL;
895 adx 30
896     if (MyConnect(source_p))
897     {
898     /* DO NOT REMOVE. exit_client can be called twice after a failed
899     * read/write.
900     */
901     if (IsClosing(source_p))
902     return;
903    
904     SetClosing(source_p);
905    
906     if (IsIpHash(source_p))
907     remove_one_ip(&source_p->localClient->ip);
908    
909 michael 992 if (source_p->localClient->auth)
910     {
911     delete_auth(source_p->localClient->auth);
912     source_p->localClient->auth = NULL;
913     }
914 adx 30
915     /* This source_p could have status of one of STAT_UNKNOWN, STAT_CONNECTING
916     * STAT_HANDSHAKE or STAT_UNKNOWN
917     * all of which are lumped together into unknown_list
918     *
919     * In all above cases IsRegistered() will not be true.
920     */
921     if (!IsRegistered(source_p))
922     {
923     if ((m = dlinkFindDelete(&unknown_list, source_p)) != NULL)
924     free_dlink_node(m);
925     }
926     else if (IsClient(source_p))
927     {
928 michael 1013 assert(Count.local > 0);
929 adx 30 Count.local--;
930    
931     if (IsOper(source_p))
932     {
933     if ((m = dlinkFindDelete(&oper_list, source_p)) != NULL)
934     free_dlink_node(m);
935     }
936    
937     dlinkDelete(&source_p->localClient->lclient_node, &local_client_list);
938     if (source_p->localClient->list_task != NULL)
939     free_list_task(source_p->localClient->list_task, source_p);
940    
941 michael 876 watch_del_watch_list(source_p);
942 adx 30 sendto_realops_flags(UMODE_CCONN, L_ALL, "Client exiting: %s (%s@%s) [%s] [%s]",
943     source_p->name, source_p->username, source_p->host, comment,
944     ConfigFileEntry.hide_spoof_ips && IsIPSpoof(source_p) ?
945     "255.255.255.255" : source_p->sockhost);
946 db 853 sendto_realops_flags(UMODE_CCONN_FULL, L_ALL, "CLIEXIT: %s %s %s %s 0 %s",
947     source_p->name,
948     source_p->username,
949     source_p->host,
950    
951 db 849 ConfigFileEntry.hide_spoof_ips && IsIPSpoof(source_p) ?
952 db 853 "255.255.255.255" : source_p->sockhost,
953     comment);
954 adx 30 }
955    
956     /* As soon as a client is known to be a server of some sort
957     * it has to be put on the serv_list, or SJOIN's to this new server
958     * from the connect burst will not be seen.
959     */
960     if (IsServer(source_p) || IsConnecting(source_p) ||
961     IsHandshake(source_p))
962     {
963     if ((m = dlinkFindDelete(&serv_list, source_p)) != NULL)
964     {
965     free_dlink_node(m);
966     unset_chcap_usage_counts(source_p);
967     }
968    
969     if (IsServer(source_p))
970     Count.myserver--;
971     }
972    
973     log_user_exit(source_p);
974    
975     if (!IsDead(source_p))
976     {
977     if (IsServer(source_p))
978     {
979     /* for them, we are exiting the network */
980     sendto_one(source_p, ":%s SQUIT %s :%s",
981     ID_or_name(from, source_p), me.name, comment);
982     }
983    
984     sendto_one(source_p, "ERROR :Closing Link: %s (%s)",
985     source_p->host, comment);
986     }
987    
988     /*
989     ** Currently only server connections can have
990     ** depending remote clients here, but it does no
991     ** harm to check for all local clients. In
992     ** future some other clients than servers might
993     ** have remotes too...
994     **
995     ** Close the Client connection first and mark it
996     ** so that no messages are attempted to send to it.
997     ** Remember it makes source_p->from == NULL.
998     */
999     close_connection(source_p);
1000     }
1001    
1002     if (IsServer(source_p))
1003     {
1004     char splitstr[HOSTLEN + HOSTLEN + 2];
1005    
1006     /* This shouldn't ever happen */
1007     assert(source_p->serv != NULL && source_p->servptr != NULL);
1008    
1009     if (ConfigServerHide.hide_servers)
1010 michael 887 /*
1011     * Set netsplit message to "*.net *.split" to still show
1012 adx 30 * that its a split, but hide the servers splitting
1013     */
1014     strcpy(splitstr, "*.net *.split");
1015     else
1016     snprintf(splitstr, sizeof(splitstr), "%s %s",
1017     source_p->servptr->name, source_p->name);
1018    
1019     remove_dependents(source_p, from->from, comment, splitstr);
1020    
1021     if (source_p->servptr == &me)
1022     {
1023     sendto_realops_flags(UMODE_ALL, L_ALL,
1024     "%s was connected for %d seconds. %llu/%llu sendK/recvK.",
1025     source_p->name, (int)(CurrentTime - source_p->firsttime),
1026     source_p->localClient->send.bytes >> 10,
1027     source_p->localClient->recv.bytes >> 10);
1028     ilog(L_NOTICE, "%s was connected for %d seconds. %llu/%llu sendK/recvK.",
1029     source_p->name, (int)(CurrentTime - source_p->firsttime),
1030     source_p->localClient->send.bytes >> 10,
1031     source_p->localClient->recv.bytes >> 10);
1032     }
1033     }
1034     else if (IsClient(source_p) && !IsKilled(source_p))
1035     {
1036 michael 885 sendto_server(from->from, NULL, CAP_TS6, NOCAPS,
1037 adx 30 ":%s QUIT :%s", ID(source_p), comment);
1038 michael 885 sendto_server(from->from, NULL, NOCAPS, CAP_TS6,
1039 adx 30 ":%s QUIT :%s", source_p->name, comment);
1040     }
1041    
1042     /* The client *better* be off all of the lists */
1043     assert(dlinkFind(&unknown_list, source_p) == NULL);
1044     assert(dlinkFind(&local_client_list, source_p) == NULL);
1045     assert(dlinkFind(&serv_list, source_p) == NULL);
1046     assert(dlinkFind(&oper_list, source_p) == NULL);
1047    
1048     exit_one_client(source_p, comment);
1049     }
1050    
1051     /*
1052     * dead_link_on_write - report a write error if not already dead,
1053     * mark it as dead then exit it
1054     */
1055     void
1056     dead_link_on_write(struct Client *client_p, int ierrno)
1057     {
1058     dlink_node *ptr;
1059    
1060     if (IsDefunct(client_p))
1061     return;
1062    
1063     dbuf_clear(&client_p->localClient->buf_recvq);
1064     dbuf_clear(&client_p->localClient->buf_sendq);
1065    
1066     assert(dlinkFind(&abort_list, client_p) == NULL);
1067     ptr = make_dlink_node();
1068     /* don't let exit_aborted_clients() finish yet */
1069     dlinkAddTail(client_p, ptr, &abort_list);
1070    
1071     if (eac_next == NULL)
1072     eac_next = ptr;
1073    
1074     SetDead(client_p); /* You are dead my friend */
1075     }
1076    
1077     /*
1078     * dead_link_on_read - report a read error if not already dead,
1079     * mark it as dead then exit it
1080     */
1081     void
1082     dead_link_on_read(struct Client *client_p, int error)
1083     {
1084     char errmsg[255];
1085     int current_error;
1086    
1087     if (IsDefunct(client_p))
1088     return;
1089    
1090     dbuf_clear(&client_p->localClient->buf_recvq);
1091     dbuf_clear(&client_p->localClient->buf_sendq);
1092    
1093     current_error = get_sockerr(client_p->localClient->fd.fd);
1094    
1095     if (IsServer(client_p) || IsHandshake(client_p))
1096     {
1097     int connected = CurrentTime - client_p->firsttime;
1098    
1099     if (error == 0)
1100     {
1101     /* Admins get the real IP */
1102     sendto_realops_flags(UMODE_ALL, L_ADMIN,
1103     "Server %s closed the connection",
1104     get_client_name(client_p, SHOW_IP));
1105    
1106     /* Opers get a masked IP */
1107     sendto_realops_flags(UMODE_ALL, L_OPER,
1108     "Server %s closed the connection",
1109     get_client_name(client_p, MASK_IP));
1110    
1111     ilog(L_NOTICE, "Server %s closed the connection",
1112     get_client_name(client_p, SHOW_IP));
1113     }
1114     else
1115     {
1116 michael 617 report_error(L_ADMIN, "Lost connection to %s: %s",
1117 adx 30 get_client_name(client_p, SHOW_IP), current_error);
1118 michael 617 report_error(L_OPER, "Lost connection to %s: %s",
1119 adx 30 get_client_name(client_p, MASK_IP), current_error);
1120     }
1121    
1122     sendto_realops_flags(UMODE_ALL, L_ALL,
1123     "%s had been connected for %d day%s, %2d:%02d:%02d",
1124     client_p->name, connected/86400,
1125     (connected/86400 == 1) ? "" : "s",
1126     (connected % 86400) / 3600, (connected % 3600) / 60,
1127     connected % 60);
1128     }
1129    
1130     if (error == 0)
1131     strlcpy(errmsg, "Remote host closed the connection",
1132     sizeof(errmsg));
1133     else
1134 michael 1124 snprintf(errmsg, sizeof(errmsg), "Read error: %s",
1135     strerror(current_error));
1136 adx 30
1137     exit_client(client_p, &me, errmsg);
1138     }
1139    
1140     void
1141     exit_aborted_clients(void)
1142     {
1143     dlink_node *ptr;
1144     struct Client *target_p;
1145     const char *notice;
1146    
1147     DLINK_FOREACH_SAFE(ptr, eac_next, abort_list.head)
1148     {
1149     target_p = ptr->data;
1150     eac_next = ptr->next;
1151    
1152     if (target_p == NULL)
1153     {
1154     sendto_realops_flags(UMODE_ALL, L_ALL,
1155     "Warning: null client on abort_list!");
1156     dlinkDelete(ptr, &abort_list);
1157     free_dlink_node(ptr);
1158     continue;
1159     }
1160    
1161     dlinkDelete(ptr, &abort_list);
1162    
1163     if (IsSendQExceeded(target_p))
1164     notice = "Max SendQ exceeded";
1165     else
1166     notice = "Write error: connection closed";
1167    
1168     exit_client(target_p, &me, notice);
1169     free_dlink_node(ptr);
1170     }
1171     }
1172    
1173     /*
1174     * accept processing, this adds a form of "caller ID" to ircd
1175 michael 887 *
1176 adx 30 * If a client puts themselves into "caller ID only" mode,
1177 michael 887 * only clients that match a client pointer they have put on
1178 adx 30 * the accept list will be allowed to message them.
1179     *
1180 michael 887 * Diane Bruce, "Dianora" db@db.net
1181 adx 30 */
1182    
1183 michael 887 void
1184     del_accept(struct split_nuh_item *accept_p, struct Client *client_p)
1185 adx 30 {
1186 michael 887 dlinkDelete(&accept_p->node, &client_p->localClient->acceptlist);
1187 adx 30
1188 michael 887 MyFree(accept_p->nickptr);
1189     MyFree(accept_p->userptr);
1190     MyFree(accept_p->hostptr);
1191     MyFree(accept_p);
1192     }
1193 adx 30
1194 michael 887 struct split_nuh_item *
1195     find_accept(const char *nick, const char *user,
1196     const char *host, struct Client *client_p, int do_match)
1197     {
1198     dlink_node *ptr = NULL;
1199     /* XXX We wouldn't need that if match() would return 0 on match */
1200     int (*cmpfunc)(const char *, const char *) = do_match ? match : irccmp;
1201 adx 30
1202 michael 887 DLINK_FOREACH(ptr, client_p->localClient->acceptlist.head)
1203 adx 30 {
1204 michael 887 struct split_nuh_item *accept_p = ptr->data;
1205    
1206     if (cmpfunc(accept_p->nickptr, nick) == do_match &&
1207     cmpfunc(accept_p->userptr, user) == do_match &&
1208     cmpfunc(accept_p->hostptr, host) == do_match)
1209     return accept_p;
1210 adx 30 }
1211    
1212 michael 887 return NULL;
1213 adx 30 }
1214    
1215 michael 887 /* accept_message()
1216 adx 30 *
1217 michael 887 * inputs - pointer to source client
1218     * - pointer to target client
1219     * output - 1 if accept this message 0 if not
1220     * side effects - See if source is on target's allow list
1221 adx 30 */
1222 michael 887 int
1223     accept_message(struct Client *source,
1224     struct Client *target)
1225 adx 30 {
1226 michael 887 dlink_node *ptr = NULL;
1227 adx 30
1228 michael 887 if (source == target || find_accept(source->name, source->username,
1229     source->host, target, 1))
1230     return 1;
1231 adx 30
1232 michael 887 if (IsSoftCallerId(target))
1233     DLINK_FOREACH(ptr, target->channel.head)
1234     if (IsMember(source, ((struct Membership *)ptr->data)->chptr))
1235     return 1;
1236 adx 30
1237 michael 887 return 0;
1238 adx 30 }
1239    
1240     /* del_all_accepts()
1241     *
1242 michael 887 * inputs - pointer to exiting client
1243     * output - NONE
1244     * side effects - Walk through given clients acceptlist and remove all entries
1245 adx 30 */
1246     void
1247     del_all_accepts(struct Client *client_p)
1248     {
1249 michael 887 dlink_node *ptr = NULL, *next_ptr = NULL;
1250 adx 30
1251 michael 887 DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->acceptlist.head)
1252     del_accept(ptr->data, client_p);
1253 adx 30 }
1254    
1255     /* change_local_nick()
1256     *
1257     * inputs - pointer to server
1258     * - pointer to client
1259     * - nick
1260     * output -
1261     * side effects - changes nick of a LOCAL user
1262     */
1263     void
1264     change_local_nick(struct Client *client_p, struct Client *source_p, const char *nick)
1265     {
1266 michael 876 int samenick = 0;
1267    
1268 michael 881 assert(source_p->name[0] && !EmptyString(nick));
1269    
1270 adx 30 /*
1271 michael 881 * Client just changing his/her nick. If he/she is
1272     * on a channel, send note of change to all clients
1273     * on that channel. Propagate notice to other servers.
1274     */
1275 adx 30 if ((source_p->localClient->last_nick_change +
1276     ConfigFileEntry.max_nick_time) < CurrentTime)
1277     source_p->localClient->number_of_nick_changes = 0;
1278     source_p->localClient->last_nick_change = CurrentTime;
1279     source_p->localClient->number_of_nick_changes++;
1280    
1281     if ((ConfigFileEntry.anti_nick_flood &&
1282     (source_p->localClient->number_of_nick_changes
1283     <= ConfigFileEntry.max_nick_changes)) ||
1284     !ConfigFileEntry.anti_nick_flood ||
1285     (IsOper(source_p) && ConfigFileEntry.no_oper_flood))
1286     {
1287 michael 876 samenick = !irccmp(source_p->name, nick);
1288    
1289     if (!samenick)
1290 michael 706 {
1291 adx 30 source_p->tsinfo = CurrentTime;
1292 michael 759 clear_ban_cache_client(source_p);
1293 michael 881 watch_check_hash(source_p, RPL_LOGOFF);
1294 michael 706 }
1295 adx 30
1296     /* XXX - the format of this notice should eventually be changed
1297     * to either %s[%s@%s], or even better would be get_client_name() -bill
1298     */
1299     sendto_realops_flags(UMODE_NCHANGE, L_ALL, "Nick change: From %s to %s [%s@%s]",
1300     source_p->name, nick, source_p->username, source_p->host);
1301     sendto_common_channels_local(source_p, 1, ":%s!%s@%s NICK :%s",
1302     source_p->name, source_p->username,
1303     source_p->host, nick);
1304 michael 706 add_history(source_p, 1);
1305 adx 30
1306 michael 885 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1307 adx 30 ":%s NICK %s :%lu",
1308     ID(source_p), nick, (unsigned long)source_p->tsinfo);
1309 michael 885 sendto_server(client_p, NULL, NOCAPS, CAP_TS6,
1310 adx 30 ":%s NICK %s :%lu",
1311     source_p->name, nick, (unsigned long)source_p->tsinfo);
1312 michael 881
1313     hash_del_client(source_p);
1314     strcpy(source_p->name, nick);
1315     hash_add_client(source_p);
1316    
1317     if (!samenick)
1318     watch_check_hash(source_p, RPL_LOGON);
1319    
1320     /* fd_desc is long enough */
1321     fd_note(&client_p->localClient->fd, "Nick: %s", nick);
1322 adx 30 }
1323     else
1324     sendto_one(source_p, form_str(ERR_NICKTOOFAST),
1325     me.name, source_p->name, source_p->name,
1326     nick, ConfigFileEntry.max_nick_time);
1327     }

Properties

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