ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.1.x/src/client.c
Revision: 1241
Committed: Thu Sep 29 20:26:09 2011 UTC (14 years, 9 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/client.c
File size: 36887 byte(s)
Log Message:
- move firsttime, since, and lasttime Client struct members to LocalUser struct
- m_watch.c: show_watch() now properly reports signon times. This also fixes
  an invalid signon time of zero for remote clients.

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

Properties

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