ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/client.c
Revision: 1158
Committed: Wed Aug 10 19:46:00 2011 UTC (14 years ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/client.c
File size: 39834 byte(s)
Log Message:
- UMODE_REJ goes to usermode 'j'
- add UMODE_REGISTERED ('r') (registered nickname)

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 michael 1126 dlinkAdd(client_p, &client_p->localClient->lclient_node, &unknown_list);
127 adx 30 }
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 michael 1143 if (IsClient(client_p))
557 adx 30 {
558 michael 1013 assert(Count.total > 0);
559 adx 30 --Count.total;
560     if (IsOper(client_p))
561     --Count.oper;
562     if (IsInvisible(client_p))
563     --Count.invisi;
564     }
565 michael 1143 else if (IsServer(client_p))
566     sendto_realops_flags(UMODE_EXTERNAL, L_ALL, "Server %s split from %s",
567     client_p->name, client_p->servptr->name);
568 adx 30
569     if (splitchecking && !splitmode)
570     check_splitmode(NULL);
571     }
572    
573     /* find_person()
574     *
575     * inputs - pointer to name
576     * output - return client pointer
577     * side effects - find person by (nick)name
578     */
579     /* XXX - ugly wrapper */
580     struct Client *
581     find_person(const struct Client *client_p, const char *name)
582     {
583     struct Client *c2ptr;
584    
585     if (IsDigit(*name))
586     {
587     if ((c2ptr = hash_find_id(name)) != NULL)
588     {
589     /* invisible users shall not be found by UID guessing */
590     if (IsInvisible(c2ptr) && !IsServer(client_p))
591     c2ptr = NULL;
592     }
593     }
594     else
595     c2ptr = find_client(name);
596    
597     return ((c2ptr != NULL && IsClient(c2ptr)) ? c2ptr : NULL);
598     }
599    
600     /*
601     * find_chasing - find the client structure for a nick name (user)
602     * using history mechanism if necessary. If the client is not found,
603     * an error message (NO SUCH NICK) is generated. If the client was found
604     * through the history, chasing will be 1 and otherwise 0.
605     */
606     struct Client *
607     find_chasing(struct Client *client_p, struct Client *source_p, const char *user, int *chasing)
608     {
609     struct Client *who = find_person(client_p, user);
610    
611     if (chasing)
612     *chasing = 0;
613    
614     if (who)
615     return(who);
616    
617     if (IsDigit(*user))
618     return(NULL);
619    
620     if ((who = get_history(user,
621     (time_t)ConfigFileEntry.kill_chase_time_limit))
622     == NULL)
623     {
624     sendto_one(source_p, form_str(ERR_NOSUCHNICK),
625     me.name, source_p->name, user);
626     return(NULL);
627     }
628    
629     if (chasing)
630     *chasing = 1;
631    
632     return(who);
633     }
634    
635     /*
636     * get_client_name - Return the name of the client
637     * for various tracking and
638     * admin purposes. The main purpose of this function is to
639     * return the "socket host" name of the client, if that
640     * differs from the advertised name (other than case).
641     * But, this can be used to any client structure.
642     *
643     * NOTE 1:
644     * Watch out the allocation of "nbuf", if either source_p->name
645     * or source_p->sockhost gets changed into pointers instead of
646     * directly allocated within the structure...
647     *
648     * NOTE 2:
649     * Function return either a pointer to the structure (source_p) or
650     * to internal buffer (nbuf). *NEVER* use the returned pointer
651     * to modify what it points!!!
652     */
653     const char *
654 michael 1124 get_client_name(const struct Client *client, int showip)
655 adx 30 {
656     static char nbuf[HOSTLEN * 2 + USERLEN + 5];
657    
658     assert(client != NULL);
659    
660     if (irccmp(client->name, client->host) == 0)
661 michael 1124 return client->name;
662 adx 30
663     if (ConfigServerHide.hide_server_ips)
664     if (IsServer(client) || IsConnecting(client) || IsHandshake(client))
665     showip = MASK_IP;
666    
667     if (ConfigFileEntry.hide_spoof_ips)
668     if (showip == SHOW_IP && IsIPSpoof(client))
669     showip = MASK_IP;
670    
671     /* And finally, let's get the host information, ip or name */
672     switch (showip)
673     {
674     case SHOW_IP:
675     if (MyConnect(client))
676     {
677 michael 1124 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
678     client->name,
679     client->username, client->sockhost);
680 adx 30 break;
681     }
682     case MASK_IP:
683 michael 1124 snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
684     client->name, client->username);
685 adx 30 break;
686     default:
687 michael 1124 snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
688     client->name,
689     client->username, client->host);
690 adx 30 }
691    
692 michael 1124 return nbuf;
693 adx 30 }
694    
695     void
696     free_exited_clients(void)
697     {
698 michael 887 dlink_node *ptr = NULL, *next = NULL;
699 adx 30
700     DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
701     {
702 michael 887 free_client(ptr->data);
703 adx 30 dlinkDelete(ptr, &dead_list);
704     free_dlink_node(ptr);
705     }
706     }
707    
708     /*
709     * Exit one client, local or remote. Assuming all dependents have
710     * been already removed, and socket closed for local client.
711     *
712     * The only messages generated are QUITs on channels.
713     */
714     static void
715     exit_one_client(struct Client *source_p, const char *quitmsg)
716     {
717     dlink_node *lp = NULL, *next_lp = NULL;
718    
719     assert(!IsMe(source_p));
720    
721 michael 1118 if (IsClient(source_p))
722 adx 30 {
723     if (source_p->servptr->serv != NULL)
724 michael 889 dlinkDelete(&source_p->lnode, &source_p->servptr->serv->client_list);
725 adx 30
726 michael 1118 /*
727     * If a person is on a channel, send a QUIT notice
728     * to every client (person) on the same channel (so
729     * that the client can show the "**signoff" message).
730     * (Note: The notice is to the local clients *only*)
731     */
732 adx 30 sendto_common_channels_local(source_p, 0, ":%s!%s@%s QUIT :%s",
733     source_p->name, source_p->username,
734     source_p->host, quitmsg);
735     DLINK_FOREACH_SAFE(lp, next_lp, source_p->channel.head)
736     remove_user_from_channel(lp->data);
737    
738     add_history(source_p, 0);
739     off_history(source_p);
740    
741 michael 876 watch_check_hash(source_p, RPL_LOGOFF);
742    
743 michael 889 if (MyConnect(source_p))
744 adx 30 {
745 michael 317 /* Clean up invitefield */
746     DLINK_FOREACH_SAFE(lp, next_lp, source_p->localClient->invited.head)
747     del_invite(lp->data, source_p);
748 michael 887
749     del_all_accepts(source_p);
750 michael 317 }
751 adx 30 }
752 michael 1118 else if (IsServer(source_p))
753     {
754     dlinkDelete(&source_p->lnode, &source_p->servptr->serv->server_list);
755 adx 30
756 michael 1118 if ((lp = dlinkFindDelete(&global_serv_list, source_p)) != NULL)
757     free_dlink_node(lp);
758     }
759    
760 adx 30 /* Remove source_p from the client lists */
761     if (HasID(source_p))
762     hash_del_id(source_p);
763     if (source_p->name[0])
764     hash_del_client(source_p);
765    
766     if (IsUserHostIp(source_p))
767     delete_user_host(source_p->username, source_p->host, !MyConnect(source_p));
768    
769     /* remove from global client list
770     * NOTE: source_p->node.next cannot be NULL if the client is added
771     * to global_client_list (there is always &me at its end)
772     */
773     if (source_p != NULL && source_p->node.next != NULL)
774     dlinkDelete(&source_p->node, &global_client_list);
775    
776     update_client_exit_stats(source_p);
777    
778     /* Check to see if the client isn't already on the dead list */
779     assert(dlinkFind(&dead_list, source_p) == NULL);
780    
781     /* add to dead client dlist */
782     SetDead(source_p);
783     dlinkAdd(source_p, make_dlink_node(), &dead_list);
784     }
785    
786     /* Recursively send QUITs and SQUITs for source_p and all its dependent clients
787     * and servers to those servers that need them. A server needs the client
788     * QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it
789     * isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once
790     * a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending
791     * on that one -orabidoo
792     *
793     * This is now called on each local server -adx
794     */
795     static void
796     recurse_send_quits(struct Client *original_source_p, struct Client *source_p,
797     struct Client *from, struct Client *to, const char *comment,
798 michael 1118 const char *splitstr)
799 adx 30 {
800     dlink_node *ptr, *next;
801     struct Client *target_p;
802 michael 1118 int hidden = match(me.name, source_p->name); /* XXX */
803 adx 30
804     assert(to != source_p); /* should be already removed from serv_list */
805    
806     /* If this server can handle quit storm (QS) removal
807     * of dependents, just send the SQUIT
808     *
809     * Always check *all* dependent servers if some of them are
810     * hidden behind fakename. If so, send out the QUITs -adx
811     */
812     if (hidden || !IsCapable(to, CAP_QS))
813 michael 889 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->client_list.head)
814 adx 30 {
815     target_p = ptr->data;
816     sendto_one(to, ":%s QUIT :%s", target_p->name, splitstr);
817     }
818    
819 michael 889 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->server_list.head)
820 adx 30 recurse_send_quits(original_source_p, ptr->data, from, to,
821 michael 1118 comment, splitstr);
822 adx 30
823     if (!hidden && ((source_p == original_source_p && to != from) ||
824     !IsCapable(to, CAP_QS)))
825     {
826     /* don't use a prefix here - we have to be 100% sure the message
827     * will be accepted without Unknown prefix etc.. */
828     sendto_one(to, "SQUIT %s :%s", ID_or_name(source_p, to), comment);
829     }
830     }
831    
832     /*
833     * Remove all clients that depend on source_p; assumes all (S)QUITs have
834     * already been sent. we make sure to exit a server's dependent clients
835     * and servers before the server itself; exit_one_client takes care of
836     * actually removing things off llists. tweaked from +CSr31 -orabidoo
837     */
838     static void
839     recurse_remove_clients(struct Client *source_p, const char *quitmsg)
840     {
841     dlink_node *ptr, *next;
842    
843 michael 889 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->client_list.head)
844 adx 30 exit_one_client(ptr->data, quitmsg);
845    
846 michael 889 DLINK_FOREACH_SAFE(ptr, next, source_p->serv->server_list.head)
847 adx 30 {
848     recurse_remove_clients(ptr->data, quitmsg);
849     exit_one_client(ptr->data, quitmsg);
850     }
851     }
852    
853     /*
854     ** Remove *everything* that depends on source_p, from all lists, and sending
855     ** all necessary QUITs and SQUITs. source_p itself is still on the lists,
856     ** and its SQUITs have been sent except for the upstream one -orabidoo
857     */
858     static void
859     remove_dependents(struct Client *source_p, struct Client *from,
860     const char *comment, const char *splitstr)
861     {
862 michael 1118 dlink_node *ptr = NULL;
863 adx 30
864     DLINK_FOREACH(ptr, serv_list.head)
865 michael 1118 recurse_send_quits(source_p, source_p, from, ptr->data,
866     comment, splitstr);
867 adx 30
868     recurse_remove_clients(source_p, splitstr);
869     }
870    
871     /*
872     * exit_client - exit a client of any type. Generally, you can use
873     * this on any struct Client, regardless of its state.
874     *
875     * Note, you shouldn't exit remote _users_ without first doing
876     * SetKilled and propagating a kill or similar message. However,
877     * it is perfectly correct to call exit_client to force a _server_
878     * quit (either local or remote one).
879     *
880     * inputs: - a client pointer that is going to be exited
881     * - for servers, the second argument is a pointer to who
882     * is firing the server. This side won't get any generated
883     * messages. NEVER NULL!
884     * output: none
885     * side effects: the client is delinked from all lists, disconnected,
886     * and the rest of IRC network is notified of the exit.
887     * Client memory is scheduled to be freed
888     */
889     void
890     exit_client(struct Client *source_p, struct Client *from, const char *comment)
891     {
892 michael 1118 dlink_node *m = NULL;
893 adx 30
894     if (MyConnect(source_p))
895     {
896     /* DO NOT REMOVE. exit_client can be called twice after a failed
897     * read/write.
898     */
899     if (IsClosing(source_p))
900     return;
901    
902     SetClosing(source_p);
903    
904     if (IsIpHash(source_p))
905     remove_one_ip(&source_p->localClient->ip);
906    
907 michael 992 if (source_p->localClient->auth)
908     {
909     delete_auth(source_p->localClient->auth);
910     source_p->localClient->auth = NULL;
911     }
912 adx 30
913     /* This source_p could have status of one of STAT_UNKNOWN, STAT_CONNECTING
914     * STAT_HANDSHAKE or STAT_UNKNOWN
915     * all of which are lumped together into unknown_list
916     *
917     * In all above cases IsRegistered() will not be true.
918     */
919     if (!IsRegistered(source_p))
920     {
921 michael 1126 assert(dlinkFind(&unknown_list, source_p));
922    
923     dlinkDelete(&source_p->localClient->lclient_node, &unknown_list);
924 adx 30 }
925     else if (IsClient(source_p))
926     {
927 michael 1013 assert(Count.local > 0);
928 adx 30 Count.local--;
929    
930     if (IsOper(source_p))
931     {
932     if ((m = dlinkFindDelete(&oper_list, source_p)) != NULL)
933     free_dlink_node(m);
934     }
935    
936 michael 1126 assert(dlinkFind(&local_client_list, source_p));
937 adx 30 dlinkDelete(&source_p->localClient->lclient_node, &local_client_list);
938 michael 1126
939 adx 30 if (source_p->localClient->list_task != NULL)
940     free_list_task(source_p->localClient->list_task, source_p);
941    
942 michael 876 watch_del_watch_list(source_p);
943 adx 30 sendto_realops_flags(UMODE_CCONN, L_ALL, "Client exiting: %s (%s@%s) [%s] [%s]",
944     source_p->name, source_p->username, source_p->host, comment,
945     ConfigFileEntry.hide_spoof_ips && IsIPSpoof(source_p) ?
946     "255.255.255.255" : source_p->sockhost);
947 db 853 sendto_realops_flags(UMODE_CCONN_FULL, L_ALL, "CLIEXIT: %s %s %s %s 0 %s",
948     source_p->name,
949     source_p->username,
950     source_p->host,
951    
952 db 849 ConfigFileEntry.hide_spoof_ips && IsIPSpoof(source_p) ?
953 db 853 "255.255.255.255" : source_p->sockhost,
954     comment);
955 adx 30 }
956    
957     /* As soon as a client is known to be a server of some sort
958     * it has to be put on the serv_list, or SJOIN's to this new server
959     * from the connect burst will not be seen.
960     */
961     if (IsServer(source_p) || IsConnecting(source_p) ||
962     IsHandshake(source_p))
963     {
964     if ((m = dlinkFindDelete(&serv_list, source_p)) != NULL)
965     {
966     free_dlink_node(m);
967     unset_chcap_usage_counts(source_p);
968     }
969    
970     if (IsServer(source_p))
971     Count.myserver--;
972     }
973    
974     log_user_exit(source_p);
975    
976     if (!IsDead(source_p))
977     {
978     if (IsServer(source_p))
979     {
980     /* for them, we are exiting the network */
981     sendto_one(source_p, ":%s SQUIT %s :%s",
982     ID_or_name(from, source_p), me.name, comment);
983     }
984    
985     sendto_one(source_p, "ERROR :Closing Link: %s (%s)",
986     source_p->host, comment);
987     }
988    
989     /*
990     ** Currently only server connections can have
991     ** depending remote clients here, but it does no
992     ** harm to check for all local clients. In
993     ** future some other clients than servers might
994     ** have remotes too...
995     **
996     ** Close the Client connection first and mark it
997     ** so that no messages are attempted to send to it.
998     ** Remember it makes source_p->from == NULL.
999     */
1000     close_connection(source_p);
1001     }
1002    
1003     if (IsServer(source_p))
1004     {
1005     char splitstr[HOSTLEN + HOSTLEN + 2];
1006    
1007     /* This shouldn't ever happen */
1008     assert(source_p->serv != NULL && source_p->servptr != NULL);
1009    
1010     if (ConfigServerHide.hide_servers)
1011 michael 887 /*
1012     * Set netsplit message to "*.net *.split" to still show
1013 adx 30 * that its a split, but hide the servers splitting
1014     */
1015     strcpy(splitstr, "*.net *.split");
1016     else
1017     snprintf(splitstr, sizeof(splitstr), "%s %s",
1018     source_p->servptr->name, source_p->name);
1019    
1020     remove_dependents(source_p, from->from, comment, splitstr);
1021    
1022     if (source_p->servptr == &me)
1023     {
1024     sendto_realops_flags(UMODE_ALL, L_ALL,
1025     "%s was connected for %d seconds. %llu/%llu sendK/recvK.",
1026     source_p->name, (int)(CurrentTime - source_p->firsttime),
1027     source_p->localClient->send.bytes >> 10,
1028     source_p->localClient->recv.bytes >> 10);
1029     ilog(L_NOTICE, "%s was connected for %d seconds. %llu/%llu sendK/recvK.",
1030     source_p->name, (int)(CurrentTime - source_p->firsttime),
1031     source_p->localClient->send.bytes >> 10,
1032     source_p->localClient->recv.bytes >> 10);
1033     }
1034     }
1035     else if (IsClient(source_p) && !IsKilled(source_p))
1036     {
1037 michael 885 sendto_server(from->from, NULL, CAP_TS6, NOCAPS,
1038 adx 30 ":%s QUIT :%s", ID(source_p), comment);
1039 michael 885 sendto_server(from->from, NULL, NOCAPS, CAP_TS6,
1040 adx 30 ":%s QUIT :%s", source_p->name, comment);
1041     }
1042    
1043     /* The client *better* be off all of the lists */
1044     assert(dlinkFind(&unknown_list, source_p) == NULL);
1045     assert(dlinkFind(&local_client_list, source_p) == NULL);
1046     assert(dlinkFind(&serv_list, source_p) == NULL);
1047     assert(dlinkFind(&oper_list, source_p) == NULL);
1048    
1049     exit_one_client(source_p, comment);
1050     }
1051    
1052     /*
1053     * dead_link_on_write - report a write error if not already dead,
1054     * mark it as dead then exit it
1055     */
1056     void
1057     dead_link_on_write(struct Client *client_p, int ierrno)
1058     {
1059     dlink_node *ptr;
1060    
1061     if (IsDefunct(client_p))
1062     return;
1063    
1064     dbuf_clear(&client_p->localClient->buf_recvq);
1065     dbuf_clear(&client_p->localClient->buf_sendq);
1066    
1067     assert(dlinkFind(&abort_list, client_p) == NULL);
1068     ptr = make_dlink_node();
1069     /* don't let exit_aborted_clients() finish yet */
1070     dlinkAddTail(client_p, ptr, &abort_list);
1071    
1072     if (eac_next == NULL)
1073     eac_next = ptr;
1074    
1075     SetDead(client_p); /* You are dead my friend */
1076     }
1077    
1078     /*
1079     * dead_link_on_read - report a read error if not already dead,
1080     * mark it as dead then exit it
1081     */
1082     void
1083     dead_link_on_read(struct Client *client_p, int error)
1084     {
1085     char errmsg[255];
1086     int current_error;
1087    
1088     if (IsDefunct(client_p))
1089     return;
1090    
1091     dbuf_clear(&client_p->localClient->buf_recvq);
1092     dbuf_clear(&client_p->localClient->buf_sendq);
1093    
1094     current_error = get_sockerr(client_p->localClient->fd.fd);
1095    
1096     if (IsServer(client_p) || IsHandshake(client_p))
1097     {
1098     int connected = CurrentTime - client_p->firsttime;
1099    
1100     if (error == 0)
1101     {
1102     /* Admins get the real IP */
1103     sendto_realops_flags(UMODE_ALL, L_ADMIN,
1104     "Server %s closed the connection",
1105     get_client_name(client_p, SHOW_IP));
1106    
1107     /* Opers get a masked IP */
1108     sendto_realops_flags(UMODE_ALL, L_OPER,
1109     "Server %s closed the connection",
1110     get_client_name(client_p, MASK_IP));
1111    
1112     ilog(L_NOTICE, "Server %s closed the connection",
1113     get_client_name(client_p, SHOW_IP));
1114     }
1115     else
1116     {
1117 michael 617 report_error(L_ADMIN, "Lost connection to %s: %s",
1118 adx 30 get_client_name(client_p, SHOW_IP), current_error);
1119 michael 617 report_error(L_OPER, "Lost connection to %s: %s",
1120 adx 30 get_client_name(client_p, MASK_IP), current_error);
1121     }
1122    
1123     sendto_realops_flags(UMODE_ALL, L_ALL,
1124     "%s had been connected for %d day%s, %2d:%02d:%02d",
1125     client_p->name, connected/86400,
1126     (connected/86400 == 1) ? "" : "s",
1127     (connected % 86400) / 3600, (connected % 3600) / 60,
1128     connected % 60);
1129     }
1130    
1131     if (error == 0)
1132     strlcpy(errmsg, "Remote host closed the connection",
1133     sizeof(errmsg));
1134     else
1135 michael 1124 snprintf(errmsg, sizeof(errmsg), "Read error: %s",
1136     strerror(current_error));
1137 adx 30
1138     exit_client(client_p, &me, errmsg);
1139     }
1140    
1141     void
1142     exit_aborted_clients(void)
1143     {
1144     dlink_node *ptr;
1145     struct Client *target_p;
1146     const char *notice;
1147    
1148     DLINK_FOREACH_SAFE(ptr, eac_next, abort_list.head)
1149     {
1150     target_p = ptr->data;
1151     eac_next = ptr->next;
1152    
1153     if (target_p == NULL)
1154     {
1155     sendto_realops_flags(UMODE_ALL, L_ALL,
1156     "Warning: null client on abort_list!");
1157     dlinkDelete(ptr, &abort_list);
1158     free_dlink_node(ptr);
1159     continue;
1160     }
1161    
1162     dlinkDelete(ptr, &abort_list);
1163    
1164     if (IsSendQExceeded(target_p))
1165     notice = "Max SendQ exceeded";
1166     else
1167     notice = "Write error: connection closed";
1168    
1169     exit_client(target_p, &me, notice);
1170     free_dlink_node(ptr);
1171     }
1172     }
1173    
1174     /*
1175     * accept processing, this adds a form of "caller ID" to ircd
1176 michael 887 *
1177 adx 30 * If a client puts themselves into "caller ID only" mode,
1178 michael 887 * only clients that match a client pointer they have put on
1179 adx 30 * the accept list will be allowed to message them.
1180     *
1181 michael 887 * Diane Bruce, "Dianora" db@db.net
1182 adx 30 */
1183    
1184 michael 887 void
1185     del_accept(struct split_nuh_item *accept_p, struct Client *client_p)
1186 adx 30 {
1187 michael 887 dlinkDelete(&accept_p->node, &client_p->localClient->acceptlist);
1188 adx 30
1189 michael 887 MyFree(accept_p->nickptr);
1190     MyFree(accept_p->userptr);
1191     MyFree(accept_p->hostptr);
1192     MyFree(accept_p);
1193     }
1194 adx 30
1195 michael 887 struct split_nuh_item *
1196     find_accept(const char *nick, const char *user,
1197     const char *host, struct Client *client_p, int do_match)
1198     {
1199     dlink_node *ptr = NULL;
1200     /* XXX We wouldn't need that if match() would return 0 on match */
1201     int (*cmpfunc)(const char *, const char *) = do_match ? match : irccmp;
1202 adx 30
1203 michael 887 DLINK_FOREACH(ptr, client_p->localClient->acceptlist.head)
1204 adx 30 {
1205 michael 887 struct split_nuh_item *accept_p = ptr->data;
1206    
1207     if (cmpfunc(accept_p->nickptr, nick) == do_match &&
1208     cmpfunc(accept_p->userptr, user) == do_match &&
1209     cmpfunc(accept_p->hostptr, host) == do_match)
1210     return accept_p;
1211 adx 30 }
1212    
1213 michael 887 return NULL;
1214 adx 30 }
1215    
1216 michael 887 /* accept_message()
1217 adx 30 *
1218 michael 887 * inputs - pointer to source client
1219     * - pointer to target client
1220     * output - 1 if accept this message 0 if not
1221     * side effects - See if source is on target's allow list
1222 adx 30 */
1223 michael 887 int
1224     accept_message(struct Client *source,
1225     struct Client *target)
1226 adx 30 {
1227 michael 887 dlink_node *ptr = NULL;
1228 adx 30
1229 michael 887 if (source == target || find_accept(source->name, source->username,
1230     source->host, target, 1))
1231     return 1;
1232 adx 30
1233 michael 887 if (IsSoftCallerId(target))
1234     DLINK_FOREACH(ptr, target->channel.head)
1235     if (IsMember(source, ((struct Membership *)ptr->data)->chptr))
1236     return 1;
1237 adx 30
1238 michael 887 return 0;
1239 adx 30 }
1240    
1241     /* del_all_accepts()
1242     *
1243 michael 887 * inputs - pointer to exiting client
1244     * output - NONE
1245     * side effects - Walk through given clients acceptlist and remove all entries
1246 adx 30 */
1247     void
1248     del_all_accepts(struct Client *client_p)
1249     {
1250 michael 887 dlink_node *ptr = NULL, *next_ptr = NULL;
1251 adx 30
1252 michael 887 DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->acceptlist.head)
1253     del_accept(ptr->data, client_p);
1254 adx 30 }
1255    
1256     /* change_local_nick()
1257     *
1258     * inputs - pointer to server
1259     * - pointer to client
1260     * - nick
1261     * output -
1262     * side effects - changes nick of a LOCAL user
1263     */
1264     void
1265     change_local_nick(struct Client *client_p, struct Client *source_p, const char *nick)
1266     {
1267 michael 876 int samenick = 0;
1268    
1269 michael 881 assert(source_p->name[0] && !EmptyString(nick));
1270    
1271 adx 30 /*
1272 michael 881 * Client just changing his/her nick. If he/she is
1273     * on a channel, send note of change to all clients
1274     * on that channel. Propagate notice to other servers.
1275     */
1276 adx 30 if ((source_p->localClient->last_nick_change +
1277     ConfigFileEntry.max_nick_time) < CurrentTime)
1278     source_p->localClient->number_of_nick_changes = 0;
1279     source_p->localClient->last_nick_change = CurrentTime;
1280     source_p->localClient->number_of_nick_changes++;
1281    
1282     if ((ConfigFileEntry.anti_nick_flood &&
1283     (source_p->localClient->number_of_nick_changes
1284     <= ConfigFileEntry.max_nick_changes)) ||
1285     !ConfigFileEntry.anti_nick_flood ||
1286     (IsOper(source_p) && ConfigFileEntry.no_oper_flood))
1287     {
1288 michael 876 samenick = !irccmp(source_p->name, nick);
1289    
1290     if (!samenick)
1291 michael 706 {
1292 adx 30 source_p->tsinfo = CurrentTime;
1293 michael 759 clear_ban_cache_client(source_p);
1294 michael 881 watch_check_hash(source_p, RPL_LOGOFF);
1295 michael 1158
1296     if (HasUMode(source_p, UMODE_REGISTERED))
1297     {
1298     unsigned int oldmodes = source_p->umodes;
1299     char modebuf[IRCD_BUFSIZE] = { '\0' };
1300    
1301     DelUMode(source_p, UMODE_REGISTERED);
1302     send_umode(source_p, source_p, oldmodes, 0xffffffff, modebuf);
1303     }
1304 michael 706 }
1305 adx 30
1306     /* XXX - the format of this notice should eventually be changed
1307     * to either %s[%s@%s], or even better would be get_client_name() -bill
1308     */
1309     sendto_realops_flags(UMODE_NCHANGE, L_ALL, "Nick change: From %s to %s [%s@%s]",
1310     source_p->name, nick, source_p->username, source_p->host);
1311     sendto_common_channels_local(source_p, 1, ":%s!%s@%s NICK :%s",
1312     source_p->name, source_p->username,
1313     source_p->host, nick);
1314 michael 706 add_history(source_p, 1);
1315 adx 30
1316 michael 885 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
1317 adx 30 ":%s NICK %s :%lu",
1318     ID(source_p), nick, (unsigned long)source_p->tsinfo);
1319 michael 885 sendto_server(client_p, NULL, NOCAPS, CAP_TS6,
1320 adx 30 ":%s NICK %s :%lu",
1321     source_p->name, nick, (unsigned long)source_p->tsinfo);
1322 michael 881
1323     hash_del_client(source_p);
1324     strcpy(source_p->name, nick);
1325     hash_add_client(source_p);
1326    
1327     if (!samenick)
1328     watch_check_hash(source_p, RPL_LOGON);
1329    
1330     /* fd_desc is long enough */
1331     fd_note(&client_p->localClient->fd, "Nick: %s", nick);
1332 adx 30 }
1333     else
1334     sendto_one(source_p, form_str(ERR_NICKTOOFAST),
1335     me.name, source_p->name, source_p->name,
1336     nick, ConfigFileEntry.max_nick_time);
1337     }

Properties

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