]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/client.c
New configuration opion "MorePrivacy" to "censor" some user information
[ngircd-alex.git] / src / ngircd / client.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #define __client_c__
13
14 #include "portab.h"
15
16 /**
17  * @file
18  * Client management.
19  */
20
21 #include "imp.h"
22 #include <assert.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <strings.h>
28 #include <netdb.h>
29
30 #include "defines.h"
31 #include "conn.h"
32
33 #include "exp.h"
34 #include "client.h"
35
36 #include <imp.h>
37 #include "ngircd.h"
38 #include "channel.h"
39 #include "conf.h"
40 #include "hash.h"
41 #include "irc-write.h"
42 #include "log.h"
43 #include "messages.h"
44
45 #include <exp.h>
46
47 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
48
49 static CLIENT *This_Server, *My_Clients;
50
51 static WHOWAS My_Whowas[MAX_WHOWAS];
52 static int Last_Whowas = -1;
53 static long Max_Users, My_Max_Users;
54
55
56 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
57 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
58
59 static CLIENT *New_Client_Struct PARAMS(( void ));
60 static void Generate_MyToken PARAMS(( CLIENT *Client ));
61 static void Adjust_Counters PARAMS(( CLIENT *Client ));
62
63 static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
64                                        CLIENT *TopServer, int Type, const char *ID,
65                                        const char *User, const char *Hostname, const char *Info,
66                                        int Hops, int Token, const char *Modes,
67                                        bool Idented));
68
69 static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
70                                         bool SendQuit));
71
72
73 GLOBAL void
74 Client_Init( void )
75 {
76         struct hostent *h;
77         
78         This_Server = New_Client_Struct( );
79         if( ! This_Server )
80         {
81                 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
82                 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
83                 exit( 1 );
84         }
85
86         /* Client-Struktur dieses Servers */
87         This_Server->next = NULL;
88         This_Server->type = CLIENT_SERVER;
89         This_Server->conn_id = NONE;
90         This_Server->introducer = This_Server;
91         This_Server->mytoken = 1;
92         This_Server->hops = 0;
93
94         gethostname( This_Server->host, CLIENT_HOST_LEN );
95         if (Conf_DNS) {
96                 h = gethostbyname( This_Server->host );
97                 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
98         }
99         Client_SetID( This_Server, Conf_ServerName );
100         Client_SetInfo( This_Server, Conf_ServerInfo );
101
102         My_Clients = This_Server;
103         
104         memset( &My_Whowas, 0, sizeof( My_Whowas ));
105 } /* Client_Init */
106
107
108 GLOBAL void
109 Client_Exit( void )
110 {
111         CLIENT *c, *next;
112         int cnt;
113
114         if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
115         else Client_Destroy( This_Server, "Server going down.", NULL, false );
116         
117         cnt = 0;
118         c = My_Clients;
119         while( c )
120         {
121                 cnt++;
122                 next = (CLIENT *)c->next;
123                 free( c );
124                 c = next;
125         }
126         if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
127 } /* Client_Exit */
128
129
130 GLOBAL CLIENT *
131 Client_ThisServer( void )
132 {
133         return This_Server;
134 } /* Client_ThisServer */
135
136
137 /**
138  * Initialize new local client; wrapper function for Init_New_Client().
139  * @return New CLIENT structure.
140  */
141 GLOBAL CLIENT *
142 Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
143 {
144         return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
145                 Hostname, NULL, 0, 0, NULL, Idented);
146 } /* Client_NewLocal */
147
148
149 /**
150  * Initialize new remote server; wrapper function for Init_New_Client().
151  * @return New CLIENT structure.
152  */
153 GLOBAL CLIENT *
154 Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
155  int Hops, int Token, const char *Info, bool Idented)
156 {
157         return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
158                 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
159 } /* Client_NewRemoteServer */
160
161
162 /**
163  * Initialize new remote client; wrapper function for Init_New_Client().
164  * @return New CLIENT structure.
165  */
166 GLOBAL CLIENT *
167 Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
168  const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
169 {
170         return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
171                 User, Hostname, Info, Hops, Token, Modes, Idented);
172 } /* Client_NewRemoteUser */
173
174
175 /**
176  * Initialize new client and set up the given parameters like client type,
177  * user name, host name, introducing server etc. ...
178  * @return New CLIENT structure.
179  */
180 static CLIENT *
181 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
182   int Type, const char *ID, const char *User, const char *Hostname,
183   const char *Info, int Hops, int Token, const char *Modes, bool Idented)
184 {
185         CLIENT *client;
186
187         assert(Idx >= NONE);
188         assert(Introducer != NULL);
189         assert(Hostname != NULL);
190
191         client = New_Client_Struct();
192         if (!client)
193                 return NULL;
194
195         client->starttime = time(NULL);
196         client->conn_id = Idx;
197         client->introducer = Introducer;
198         client->topserver = TopServer;
199         client->type = Type;
200         if (ID)
201                 Client_SetID(client, ID);
202         if (User) {
203                 Client_SetUser(client, User, Idented);
204                 Client_SetOrigUser(client, User);
205         }
206         if (Hostname)
207                 Client_SetHostname(client, Hostname);
208         if (Info)
209                 Client_SetInfo(client, Info);
210         client->hops = Hops;
211         client->token = Token;
212         if (Modes)
213                 Client_SetModes(client, Modes);
214         if (Type == CLIENT_SERVER)
215                 Generate_MyToken(client);
216
217         if (strchr(client->modes, 'a'))
218                 strlcpy(client->away, DEFAULT_AWAY_MSG, sizeof(client->away));
219
220         client->next = (POINTER *)My_Clients;
221         My_Clients = client;
222
223         Adjust_Counters(client);
224
225         return client;
226 } /* Init_New_Client */
227
228
229 GLOBAL void
230 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
231 {
232         /* remove a client */
233         
234         CLIENT *last, *c;
235         char msg[LINE_LEN];
236         const char *txt;
237
238         assert( Client != NULL );
239
240         if( LogMsg ) txt = LogMsg;
241         else txt = FwdMsg;
242         if( ! txt ) txt = "Reason unknown.";
243
244         /* netsplit message */
245         if( Client->type == CLIENT_SERVER ) {
246                 strlcpy(msg, This_Server->id, sizeof (msg));
247                 strlcat(msg, " ", sizeof (msg));
248                 strlcat(msg, Client->id, sizeof (msg));
249         }
250
251         last = NULL;
252         c = My_Clients;
253         while( c )
254         {
255                 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
256                 {
257                         /*
258                          * The client that is about to be removed is a server,
259                          * the client we are checking right now is a child of that
260                          * server and thus has to be removed, too.
261                          *
262                          * Call Client_Destroy() recursively with the server as the
263                          * new "object to be removed". This starts the cycle again, until
264                          * all servers that are linked via the original server have been
265                          * removed.
266                          */
267                         Client_Destroy( c, NULL, msg, false );
268                         last = NULL;
269                         c = My_Clients;
270                         continue;
271                 }
272                 if( c == Client )
273                 {
274                         /* found  the client: remove it */
275                         if( last ) last->next = c->next;
276                         else My_Clients = (CLIENT *)c->next;
277
278                         if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
279                                 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
280                         else if( c->type == CLIENT_SERVER )
281                         {
282                                 if( c != This_Server )
283                                 {
284                                         if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
285                                         else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
286                                 }
287
288                                 /* inform other servers */
289                                 if( ! NGIRCd_SignalQuit )
290                                 {
291                                         if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
292                                         else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
293                                 }
294                         }
295                         else
296                         {
297                                 if( c->conn_id != NONE )
298                                 {
299                                         if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
300                                         else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
301                                 } else {
302                                         Log(LOG_WARNING, "Unregistered unknown client \"%s\": %s",
303                                                                 c->id[0] ? c->id : "(No Nick)", txt );
304                                 }
305                         }
306
307                         free( c );
308                         break;
309                 }
310                 last = c;
311                 c = (CLIENT *)c->next;
312         }
313 } /* Client_Destroy */
314
315
316 GLOBAL void
317 Client_SetHostname( CLIENT *Client, const char *Hostname )
318 {
319         assert( Client != NULL );
320         assert( Hostname != NULL );
321
322         if (strlen(Conf_CloakHost)) {
323                 strlcpy( Client->host, Conf_CloakHost, sizeof( Client->host ));
324         } else {
325                 strlcpy( Client->host, Hostname, sizeof( Client->host ));
326         }
327 } /* Client_SetHostname */
328
329
330 GLOBAL void
331 Client_SetID( CLIENT *Client, const char *ID )
332 {
333         assert( Client != NULL );
334         assert( ID != NULL );
335         
336         strlcpy( Client->id, ID, sizeof( Client->id ));
337
338         if (Conf_CloakUserToNick) {
339                 strlcpy( Client->user, ID, sizeof( Client->user ));
340                 strlcpy( Client->info, ID, sizeof( Client->info ));
341         }
342
343         /* Hash */
344         Client->hash = Hash( Client->id );
345 } /* Client_SetID */
346
347
348 GLOBAL void
349 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
350 {
351         /* set clients username */
352
353         assert( Client != NULL );
354         assert( User != NULL );
355
356         if (Conf_CloakUserToNick) {
357                 strlcpy(Client->user, Client->id, sizeof(Client->user));
358         } else if (Idented) {
359                 strlcpy(Client->user, User, sizeof(Client->user));
360         } else {
361                 Client->user[0] = '~';
362                 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
363         }
364 } /* Client_SetUser */
365
366
367 /**
368  * Set "original" user name of a client.
369  * This function saves the "original" user name, the user name specified by
370  * the peer using the USER command, into the CLIENT structure. This user
371  * name may be used for authentication, for example.
372  * @param Client The client.
373  * @param User User name to set.
374  */
375 GLOBAL void
376 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
377 {
378         assert(Client != NULL);
379         assert(User != NULL);
380
381 #if defined(PAM) && defined(IDENTAUTH)
382         strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
383 #endif
384 } /* Client_SetOrigUser */
385
386
387 GLOBAL void
388 Client_SetInfo( CLIENT *Client, const char *Info )
389 {
390         /* set client hostname */
391
392         assert( Client != NULL );
393         assert( Info != NULL );
394
395         if (Conf_CloakUserToNick)
396                 strlcpy(Client->info, Client->id, sizeof(Client->info));
397         else
398                 strlcpy(Client->info, Info, sizeof(Client->info));
399 } /* Client_SetInfo */
400
401
402 GLOBAL void
403 Client_SetModes( CLIENT *Client, const char *Modes )
404 {
405         assert( Client != NULL );
406         assert( Modes != NULL );
407
408         strlcpy(Client->modes, Modes, sizeof( Client->modes ));
409 } /* Client_SetModes */
410
411
412 GLOBAL void
413 Client_SetFlags( CLIENT *Client, const char *Flags )
414 {
415         assert( Client != NULL );
416         assert( Flags != NULL );
417
418         strlcpy(Client->flags, Flags, sizeof(Client->flags));
419 } /* Client_SetFlags */
420
421
422 GLOBAL void
423 Client_SetPassword( CLIENT *Client, const char *Pwd )
424 {
425         /* set password sent by client */
426
427         assert( Client != NULL );
428         assert( Pwd != NULL );
429
430         strlcpy(Client->pwd, Pwd, sizeof(Client->pwd));
431 } /* Client_SetPassword */
432
433
434 GLOBAL void
435 Client_SetAway( CLIENT *Client, const char *Txt )
436 {
437         /* Set AWAY reason of client */
438
439         assert( Client != NULL );
440         assert( Txt != NULL );
441
442         strlcpy( Client->away, Txt, sizeof( Client->away ));
443         LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
444                  Client_Mask(Client), Txt);
445 } /* Client_SetAway */
446
447
448 GLOBAL void
449 Client_SetType( CLIENT *Client, int Type )
450 {
451         assert( Client != NULL );
452         Client->type = Type;
453         if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
454         Adjust_Counters( Client );
455 } /* Client_SetType */
456
457
458 GLOBAL void
459 Client_SetHops( CLIENT *Client, int Hops )
460 {
461         assert( Client != NULL );
462         Client->hops = Hops;
463 } /* Client_SetHops */
464
465
466 GLOBAL void
467 Client_SetToken( CLIENT *Client, int Token )
468 {
469         assert( Client != NULL );
470         Client->token = Token;
471 } /* Client_SetToken */
472
473
474 GLOBAL void
475 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
476 {
477         assert( Client != NULL );
478         assert( Introducer != NULL );
479         Client->introducer = Introducer;
480 } /* Client_SetIntroducer */
481
482
483 GLOBAL void
484 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
485 {
486         assert( Client != NULL );
487         Client->oper_by_me = OperByMe;
488 } /* Client_SetOperByMe */
489
490
491 GLOBAL bool
492 Client_ModeAdd( CLIENT *Client, char Mode )
493 {
494         /* Set Mode.
495          * If Client already alread had Mode, return false.
496          * If the Mode was newly set, return true.
497          */
498
499         char x[2];
500
501         assert( Client != NULL );
502
503         x[0] = Mode; x[1] = '\0';
504         if (!strchr( Client->modes, x[0])) {
505                 strlcat( Client->modes, x, sizeof( Client->modes ));
506                 return true;
507         }
508         else return false;
509 } /* Client_ModeAdd */
510
511
512 GLOBAL bool
513 Client_ModeDel( CLIENT *Client, char Mode )
514 {
515         /* Delete Mode.
516          * If Mode was removed, return true.
517          * If Client did not have Mode, return false.
518          */
519
520         char x[2], *p;
521
522         assert( Client != NULL );
523
524         x[0] = Mode; x[1] = '\0';
525
526         p = strchr( Client->modes, x[0] );
527         if( ! p ) return false;
528
529         /* Client has Mode -> delete */
530         while( *p )
531         {
532                 *p = *(p + 1);
533                 p++;
534         }
535         return true;
536 } /* Client_ModeDel */
537
538
539 GLOBAL CLIENT *
540 Client_Search( const char *Nick )
541 {
542         /* return Client-Structure that has the corresponding Nick.
543          * If none is found, return NULL.
544          */
545
546         char search_id[CLIENT_ID_LEN], *ptr;
547         CLIENT *c = NULL;
548         UINT32 search_hash;
549
550         assert( Nick != NULL );
551
552         /* copy Nick and truncate hostmask if necessary */
553         strlcpy( search_id, Nick, sizeof( search_id ));
554         ptr = strchr( search_id, '!' );
555         if( ptr ) *ptr = '\0';
556
557         search_hash = Hash(search_id);
558
559         c = My_Clients;
560         while (c) {
561                 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
562                         return c;
563                 c = (CLIENT *)c->next;
564         }
565         return NULL;
566 } /* Client_Search */
567
568
569 /**
570  * Get client structure ("introducer") identfied by a server token.
571  * @return CLIENT structure or NULL if none could be found.
572  */
573 GLOBAL CLIENT *
574 Client_GetFromToken( CLIENT *Client, int Token )
575 {
576         CLIENT *c;
577
578         assert( Client != NULL );
579
580         if (!Token)
581                 return NULL;
582
583         c = My_Clients;
584         while (c) {
585                 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
586                         (c->token == Token))
587                                 return c;
588                 c = (CLIENT *)c->next;
589         }
590         return NULL;
591 } /* Client_GetFromToken */
592
593
594 GLOBAL int
595 Client_Type( CLIENT *Client )
596 {
597         assert( Client != NULL );
598         return Client->type;
599 } /* Client_Type */
600
601
602 GLOBAL CONN_ID
603 Client_Conn( CLIENT *Client )
604 {
605         assert( Client != NULL );
606         return Client->conn_id;
607 } /* Client_Conn */
608
609
610 GLOBAL char *
611 Client_ID( CLIENT *Client )
612 {
613         assert( Client != NULL );
614
615 #ifdef DEBUG
616         if(Client->type == CLIENT_USER)
617                 assert(strlen(Client->id) < Conf_MaxNickLength);
618 #endif
619                                                    
620         if( Client->id[0] ) return Client->id;
621         else return "*";
622 } /* Client_ID */
623
624
625 GLOBAL char *
626 Client_Info( CLIENT *Client )
627 {
628         assert( Client != NULL );
629         return Client->info;
630 } /* Client_Info */
631
632
633 GLOBAL char *
634 Client_User( CLIENT *Client )
635 {
636         assert( Client != NULL );
637         return Client->user[0] ? Client->user : "~";
638 } /* Client_User */
639
640
641 #ifdef PAM
642
643 /**
644  * Get the "original" user name as supplied by the USER command.
645  * The user name as given by the client is used for authentication instead
646  * of the one detected using IDENT requests.
647  * @param Client The client.
648  * @return Original user name.
649  */
650 GLOBAL char *
651 Client_OrigUser(CLIENT *Client) {
652 #ifndef IDENTAUTH
653         char *user = Client->user;
654
655         if (user[0] == '~')
656                 user++;
657         return user;
658 #else
659         return Client->orig_user;
660 #endif
661 } /* Client_OrigUser */
662
663 #endif
664
665
666 /**
667  * Return the hostname of a client.
668  * @param Client Pointer to client structure
669  * @return Pointer to client hostname
670  */
671 GLOBAL char *
672 Client_Hostname(CLIENT *Client)
673 {
674         assert (Client != NULL);
675         return Client->host;
676 } /* Client_Hostname */
677
678
679 /**
680  * Get potentially cloaked hostname of a client.
681  * If the client has not enabled cloaking, the real hostname is used.
682  * @param Client Pointer to client structure
683  * @return Pointer to client hostname
684  */
685 GLOBAL char *
686 Client_HostnameCloaked(CLIENT *Client)
687 {
688         assert(Client != NULL);
689         if (Client_HasMode(Client, 'x'))
690                 return Client_ID(Client->introducer);
691         else
692                 return Client_Hostname(Client);
693 } /* Client_HostnameCloaked */
694
695
696 GLOBAL char *
697 Client_Password( CLIENT *Client )
698 {
699         assert( Client != NULL );
700         return Client->pwd;
701 } /* Client_Password */
702
703
704 GLOBAL char *
705 Client_Modes( CLIENT *Client )
706 {
707         assert( Client != NULL );
708         return Client->modes;
709 } /* Client_Modes */
710
711
712 GLOBAL char *
713 Client_Flags( CLIENT *Client )
714 {
715         assert( Client != NULL );
716         return Client->flags;
717 } /* Client_Flags */
718
719
720 GLOBAL bool
721 Client_OperByMe( CLIENT *Client )
722 {
723         assert( Client != NULL );
724         return Client->oper_by_me;
725 } /* Client_OperByMe */
726
727
728 GLOBAL int
729 Client_Hops( CLIENT *Client )
730 {
731         assert( Client != NULL );
732         return Client->hops;
733 } /* Client_Hops */
734
735
736 GLOBAL int
737 Client_Token( CLIENT *Client )
738 {
739         assert( Client != NULL );
740         return Client->token;
741 } /* Client_Token */
742
743
744 GLOBAL int
745 Client_MyToken( CLIENT *Client )
746 {
747         assert( Client != NULL );
748         return Client->mytoken;
749 } /* Client_MyToken */
750
751
752 GLOBAL CLIENT *
753 Client_NextHop( CLIENT *Client )
754 {
755         CLIENT *c;
756
757         assert( Client != NULL );
758
759         c = Client;
760         while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
761                 c = c->introducer;
762
763         return c;
764 } /* Client_NextHop */
765
766
767 /**
768  * Return ID of a client: "client!user@host"
769  * This client ID is used for IRC prefixes, for example.
770  * Please note that this function uses a global static buffer, so you can't
771  * nest invocations without overwriting erlier results!
772  * @param Client Pointer to client structure
773  * @return Pointer to global buffer containing the client ID
774  */
775 GLOBAL char *
776 Client_Mask( CLIENT *Client )
777 {
778         static char Mask_Buffer[GETID_LEN];
779
780         assert (Client != NULL);
781
782         /* Servers: return name only, there is no "mask" */
783         if (Client->type == CLIENT_SERVER)
784                 return Client->id;
785
786         snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
787                  Client->id, Client->user, Client->host);
788         return Mask_Buffer;
789 } /* Client_Mask */
790
791
792 /**
793  * Return ID of a client with cloaked hostname: "client!user@server-name"
794  * This client ID is used for IRC prefixes, for example.
795  * Please note that this function uses a global static buffer, so you can't
796  * nest invocations without overwriting erlier results!
797  * If the client has not enabled cloaking, the real hostname is used.
798  * @param Client Pointer to client structure
799  * @return Pointer to global buffer containing the client ID
800  */
801 GLOBAL char *
802 Client_MaskCloaked(CLIENT *Client)
803 {
804         static char Mask_Buffer[GETID_LEN];
805
806         assert (Client != NULL);
807
808         /* Is the client using cloaking at all? */
809         if (!Client_HasMode(Client, 'x'))
810             return Client_Mask(Client);
811
812         snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
813                  Client->id, Client->user, Client_ID(Client->introducer));
814         return Mask_Buffer;
815 } /* Client_MaskCloaked */
816
817
818 GLOBAL CLIENT *
819 Client_Introducer( CLIENT *Client )
820 {
821         assert( Client != NULL );
822         return Client->introducer;
823 } /* Client_Introducer */
824
825
826 GLOBAL CLIENT *
827 Client_TopServer( CLIENT *Client )
828 {
829         assert( Client != NULL );
830         return Client->topserver;
831 } /* Client_TopServer */
832
833
834 GLOBAL bool
835 Client_HasMode( CLIENT *Client, char Mode )
836 {
837         assert( Client != NULL );
838         return strchr( Client->modes, Mode ) != NULL;
839 } /* Client_HasMode */
840
841
842 GLOBAL char *
843 Client_Away( CLIENT *Client )
844 {
845         assert( Client != NULL );
846         return Client->away;
847 } /* Client_Away */
848
849
850 GLOBAL bool
851 Client_CheckNick( CLIENT *Client, char *Nick )
852 {
853         assert( Client != NULL );
854         assert( Nick != NULL );
855
856         if (! Client_IsValidNick( Nick ))
857         {
858                 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
859                 return false;
860         }
861
862         /* Nick bereits vergeben? */
863         if( Client_Search( Nick ))
864         {
865                 /* den Nick gibt es bereits */
866                 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
867                 return false;
868         }
869
870         return true;
871 } /* Client_CheckNick */
872
873
874 GLOBAL bool
875 Client_CheckID( CLIENT *Client, char *ID )
876 {
877         char str[COMMAND_LEN];
878         CLIENT *c;
879
880         assert( Client != NULL );
881         assert( Client->conn_id > NONE );
882         assert( ID != NULL );
883
884         /* ID too long? */
885         if (strlen(ID) > CLIENT_ID_LEN) {
886                 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
887                 return false;
888         }
889
890         /* ID already in use? */
891         c = My_Clients;
892         while (c) {
893                 if (strcasecmp(c->id, ID) == 0) {
894                         snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
895                         if (c->conn_id != NONE)
896                                 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
897                         else
898                                 Log(LOG_ERR, "%s (via network)!", str);
899                         Conn_Close(Client->conn_id, str, str, true);
900                         return false;
901                 }
902                 c = (CLIENT *)c->next;
903         }
904
905         return true;
906 } /* Client_CheckID */
907
908
909 GLOBAL CLIENT *
910 Client_First( void )
911 {
912         return My_Clients;
913 } /* Client_First */
914
915
916 GLOBAL CLIENT *
917 Client_Next( CLIENT *c )
918 {
919         assert( c != NULL );
920         return (CLIENT *)c->next;
921 } /* Client_Next */
922
923
924 GLOBAL long
925 Client_UserCount( void )
926 {
927         return Count( CLIENT_USER );
928 } /* Client_UserCount */
929
930
931 GLOBAL long
932 Client_ServiceCount( void )
933 {
934         return Count( CLIENT_SERVICE );;
935 } /* Client_ServiceCount */
936
937
938 GLOBAL long
939 Client_ServerCount( void )
940 {
941         return Count( CLIENT_SERVER );
942 } /* Client_ServerCount */
943
944
945 GLOBAL long
946 Client_MyUserCount( void )
947 {
948         return MyCount( CLIENT_USER );
949 } /* Client_MyUserCount */
950
951
952 GLOBAL long
953 Client_MyServiceCount( void )
954 {
955         return MyCount( CLIENT_SERVICE );
956 } /* Client_MyServiceCount */
957
958
959 GLOBAL unsigned long
960 Client_MyServerCount( void )
961 {
962         CLIENT *c;
963         unsigned long cnt = 0;
964
965         c = My_Clients;
966         while( c )
967         {
968                 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
969                 c = (CLIENT *)c->next;
970         }
971         return cnt;
972 } /* Client_MyServerCount */
973
974
975 GLOBAL unsigned long
976 Client_OperCount( void )
977 {
978         CLIENT *c;
979         unsigned long cnt = 0;
980
981         c = My_Clients;
982         while( c )
983         {
984                 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
985                 c = (CLIENT *)c->next;
986         }
987         return cnt;
988 } /* Client_OperCount */
989
990
991 GLOBAL unsigned long
992 Client_UnknownCount( void )
993 {
994         CLIENT *c;
995         unsigned long cnt = 0;
996
997         c = My_Clients;
998         while( c )
999         {
1000                 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1001                 c = (CLIENT *)c->next;
1002         }
1003
1004         return cnt;
1005 } /* Client_UnknownCount */
1006
1007
1008 GLOBAL long
1009 Client_MaxUserCount( void )
1010 {
1011         return Max_Users;
1012 } /* Client_MaxUserCount */
1013
1014
1015 GLOBAL long
1016 Client_MyMaxUserCount( void )
1017 {
1018         return My_Max_Users;
1019 } /* Client_MyMaxUserCount */
1020
1021
1022 GLOBAL bool
1023 Client_IsValidNick( const char *Nick )
1024 {
1025         const char *ptr;
1026         static const char goodchars[] = ";0123456789-";
1027
1028         assert( Nick != NULL );
1029
1030         if( Nick[0] == '#' ) return false;
1031         if( strchr( goodchars, Nick[0] )) return false;
1032         if( strlen( Nick ) >= Conf_MaxNickLength) return false;
1033
1034         ptr = Nick;
1035         while( *ptr )
1036         {
1037                 if (( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return false;
1038                 if ( *ptr > '}' ) return false;
1039                 ptr++;
1040         }
1041
1042         return true;
1043 } /* Client_IsValidNick */
1044
1045
1046 /**
1047  * Return pointer to "My_Whowas" structure.
1048  */
1049 GLOBAL WHOWAS *
1050 Client_GetWhowas( void )
1051 {
1052         return My_Whowas;
1053 } /* Client_GetWhowas */
1054
1055 /**
1056  * Return the index of the last used WHOWAS entry.
1057  */
1058 GLOBAL int
1059 Client_GetLastWhowasIndex( void )
1060 {
1061         return Last_Whowas;
1062 } /* Client_GetLastWhowasIndex */
1063
1064
1065 /**
1066  * Get the start time of this client.
1067  * The result is the start time in seconds since 1970-01-01, as reported
1068  * by the C function time(NULL).
1069  */
1070 GLOBAL time_t
1071 Client_StartTime(CLIENT *Client)
1072 {
1073         assert( Client != NULL );
1074         return Client->starttime;
1075 } /* Client_Uptime */
1076
1077
1078 static unsigned long
1079 Count( CLIENT_TYPE Type )
1080 {
1081         CLIENT *c;
1082         unsigned long cnt = 0;
1083
1084         c = My_Clients;
1085         while( c )
1086         {
1087                 if( c->type == Type ) cnt++;
1088                 c = (CLIENT *)c->next;
1089         }
1090         return cnt;
1091 } /* Count */
1092
1093
1094 static unsigned long
1095 MyCount( CLIENT_TYPE Type )
1096 {
1097         CLIENT *c;
1098         unsigned long cnt = 0;
1099
1100         c = My_Clients;
1101         while( c )
1102         {
1103                 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1104                 c = (CLIENT *)c->next;
1105         }
1106         return cnt;
1107 } /* MyCount */
1108
1109
1110 static CLIENT *
1111 New_Client_Struct( void )
1112 {
1113         /* Neue CLIENT-Struktur pre-initialisieren */
1114
1115         CLIENT *c;
1116
1117         c = (CLIENT *)malloc( sizeof( CLIENT ));
1118         if( ! c )
1119         {
1120                 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1121                 return NULL;
1122         }
1123
1124         memset( c, 0, sizeof ( CLIENT ));
1125
1126         c->type = CLIENT_UNKNOWN;
1127         c->conn_id = NONE;
1128         c->oper_by_me = false;
1129         c->hops = -1;
1130         c->token = -1;
1131         c->mytoken = -1;
1132
1133         return c;
1134 } /* New_Client */
1135
1136
1137 static void
1138 Generate_MyToken( CLIENT *Client )
1139 {
1140         CLIENT *c;
1141         int token;
1142
1143         c = My_Clients;
1144         token = 2;
1145         while( c )
1146         {
1147                 if( c->mytoken == token )
1148                 {
1149                         /* Das Token wurde bereits vergeben */
1150                         token++;
1151                         c = My_Clients;
1152                         continue;
1153                 }
1154                 else c = (CLIENT *)c->next;
1155         }
1156         Client->mytoken = token;
1157         LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1158 } /* Generate_MyToken */
1159
1160
1161 static void
1162 Adjust_Counters( CLIENT *Client )
1163 {
1164         long count;
1165
1166         assert( Client != NULL );
1167
1168         if( Client->type != CLIENT_USER ) return;
1169
1170         if( Client->conn_id != NONE )
1171         {
1172                 /* Local connection */
1173                 count = Client_MyUserCount( );
1174                 if( count > My_Max_Users ) My_Max_Users = count;
1175         }
1176         count = Client_UserCount( );
1177         if( count > Max_Users ) Max_Users = count;
1178 } /* Adjust_Counters */
1179
1180
1181 /**
1182  * Register client in My_Whowas structure for further recall by WHOWAS.
1183  * Note: Only clients that have been connected at least 30 seconds will be
1184  * registered to prevent automated IRC bots to "destroy" a nice server
1185  * history database.
1186  */
1187 GLOBAL void
1188 Client_RegisterWhowas( CLIENT *Client )
1189 {
1190         int slot;
1191         time_t now;
1192
1193         assert( Client != NULL );
1194
1195         now = time(NULL);
1196         /* Don't register clients that were connected less than 30 seconds. */
1197         if( now - Client->starttime < 30 )
1198                 return;
1199
1200         slot = Last_Whowas + 1;
1201         if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1202
1203 #ifdef DEBUG
1204         Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1205 #endif
1206
1207         My_Whowas[slot].time = now;
1208         strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1209                  sizeof( My_Whowas[slot].id ));
1210         strlcpy( My_Whowas[slot].user, Client_User( Client ),
1211                  sizeof( My_Whowas[slot].user ));
1212         strlcpy( My_Whowas[slot].host, Client_HostnameCloaked( Client ),
1213                  sizeof( My_Whowas[slot].host ));
1214         strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1215                  sizeof( My_Whowas[slot].info ));
1216         strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1217                  sizeof( My_Whowas[slot].server ));
1218
1219         Last_Whowas = slot;
1220 } /* Client_RegisterWhowas */
1221
1222
1223 GLOBAL const char *
1224 Client_TypeText(CLIENT *Client)
1225 {
1226         assert(Client != NULL);
1227         switch (Client_Type(Client)) {
1228                 case CLIENT_USER:
1229                         return "User";
1230                         break;
1231                 case CLIENT_SERVICE:
1232                         return "Service";
1233                         break;
1234                 case CLIENT_SERVER:
1235                         return "Server";
1236                         break;
1237                 default:
1238                         return "Client";
1239         }
1240 } /* Client_TypeText */
1241
1242
1243 /**
1244  * Destroy user or service client.
1245  */
1246 static void
1247 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1248 {
1249         if(Client->conn_id != NONE) {
1250                 /* Local (directly connected) client */
1251                 Log(LOG_NOTICE,
1252                     "%s \"%s\" unregistered (connection %d): %s",
1253                     Client_TypeText(Client), Client_Mask(Client),
1254                     Client->conn_id, Txt);
1255                 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1256                                  Client_ID(Client), Client_User(Client),
1257                                  Client_Hostname(Client), Txt);
1258
1259                 if (SendQuit) {
1260                         /* Inforam all the other servers */
1261                         if (FwdMsg)
1262                                 IRC_WriteStrServersPrefix(NULL,
1263                                                 Client, "QUIT :%s", FwdMsg );
1264                         else
1265                                 IRC_WriteStrServersPrefix(NULL,
1266                                                 Client, "QUIT :");
1267                 }
1268         } else {
1269                 /* Remote client */
1270                 LogDebug("%s \"%s\" unregistered: %s",
1271                          Client_TypeText(Client), Client_Mask(Client), Txt);
1272
1273                 if(SendQuit) {
1274                         /* Inform all the other servers, but the ones in the
1275                          * direction we got the QUIT from */
1276                         if(FwdMsg)
1277                                 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1278                                                 Client, "QUIT :%s", FwdMsg );
1279                         else
1280                                 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1281                                                 Client, "QUIT :" );
1282                 }
1283         }
1284
1285         /* Unregister client from channels */
1286         Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1287
1288         /* Register client in My_Whowas structure */
1289         Client_RegisterWhowas(Client);
1290 } /* Destroy_UserOrService */
1291
1292
1293 #ifdef DEBUG
1294
1295 GLOBAL void
1296 Client_DebugDump(void)
1297 {
1298         CLIENT *c;
1299
1300         Log(LOG_DEBUG, "Client status:");
1301         c = My_Clients;
1302         while (c) {
1303                 Log(LOG_DEBUG,
1304                     " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1305                    Client_ID(c), Client_Type(c), Client_Hostname(c),
1306                    Client_User(c), Client_Conn(c), Client_StartTime(c),
1307                    Client_Flags(c));
1308                 c = (CLIENT *)c->next;
1309         }
1310 } /* Client_DumpClients */
1311
1312 #endif
1313
1314
1315 /* -eof- */