ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/client.c
Revision: 221
Committed: Thu Nov 3 15:36:27 2005 UTC (19 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 50855 byte(s)
Log Message:
- Finished WATCH implementation
- Added missing 'return' after register_remote_user() call in
  nick_from_server() which would cause an extra hash_del/strcpy/hash_add
  combination.

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

Properties

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