]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/client.c
Client_CheckNick(), Client_IsValidNick(): code cleanup
[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 /**
851  * Make sure that a given nickname is valid.
852  *
853  * If the nickname is not valid for the given client, this function sends back
854  * the appropriate error messages.
855  *
856  * @param       Client Client that wants to change the nickname.
857  * @param       Nick New nick name.
858  * @returns     true if nickname is valid, false otherwise.
859  */
860 GLOBAL bool
861 Client_CheckNick(CLIENT *Client, char *Nick)
862 {
863         assert(Client != NULL);
864         assert(Nick != NULL);
865
866         if (!Client_IsValidNick(Nick)) {
867                 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
868                                    Client_ID(Client), Nick);
869                 return false;
870         }
871
872         /* Nickname already registered? */
873         if (Client_Search(Nick)) {
874                 IRC_WriteStrClient(Client, ERR_NICKNAMEINUSE_MSG,
875                         Client_ID(Client), Nick);
876                 return false;
877         }
878
879         return true;
880 } /* Client_CheckNick */
881
882
883 GLOBAL bool
884 Client_CheckID( CLIENT *Client, char *ID )
885 {
886         char str[COMMAND_LEN];
887         CLIENT *c;
888
889         assert( Client != NULL );
890         assert( Client->conn_id > NONE );
891         assert( ID != NULL );
892
893         /* ID too long? */
894         if (strlen(ID) > CLIENT_ID_LEN) {
895                 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
896                 return false;
897         }
898
899         /* ID already in use? */
900         c = My_Clients;
901         while (c) {
902                 if (strcasecmp(c->id, ID) == 0) {
903                         snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
904                         if (c->conn_id != NONE)
905                                 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
906                         else
907                                 Log(LOG_ERR, "%s (via network)!", str);
908                         Conn_Close(Client->conn_id, str, str, true);
909                         return false;
910                 }
911                 c = (CLIENT *)c->next;
912         }
913
914         return true;
915 } /* Client_CheckID */
916
917
918 GLOBAL CLIENT *
919 Client_First( void )
920 {
921         return My_Clients;
922 } /* Client_First */
923
924
925 GLOBAL CLIENT *
926 Client_Next( CLIENT *c )
927 {
928         assert( c != NULL );
929         return (CLIENT *)c->next;
930 } /* Client_Next */
931
932
933 GLOBAL long
934 Client_UserCount( void )
935 {
936         return Count( CLIENT_USER );
937 } /* Client_UserCount */
938
939
940 GLOBAL long
941 Client_ServiceCount( void )
942 {
943         return Count( CLIENT_SERVICE );;
944 } /* Client_ServiceCount */
945
946
947 GLOBAL long
948 Client_ServerCount( void )
949 {
950         return Count( CLIENT_SERVER );
951 } /* Client_ServerCount */
952
953
954 GLOBAL long
955 Client_MyUserCount( void )
956 {
957         return MyCount( CLIENT_USER );
958 } /* Client_MyUserCount */
959
960
961 GLOBAL long
962 Client_MyServiceCount( void )
963 {
964         return MyCount( CLIENT_SERVICE );
965 } /* Client_MyServiceCount */
966
967
968 GLOBAL unsigned long
969 Client_MyServerCount( void )
970 {
971         CLIENT *c;
972         unsigned long cnt = 0;
973
974         c = My_Clients;
975         while( c )
976         {
977                 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
978                 c = (CLIENT *)c->next;
979         }
980         return cnt;
981 } /* Client_MyServerCount */
982
983
984 GLOBAL unsigned long
985 Client_OperCount( void )
986 {
987         CLIENT *c;
988         unsigned long cnt = 0;
989
990         c = My_Clients;
991         while( c )
992         {
993                 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
994                 c = (CLIENT *)c->next;
995         }
996         return cnt;
997 } /* Client_OperCount */
998
999
1000 GLOBAL unsigned long
1001 Client_UnknownCount( void )
1002 {
1003         CLIENT *c;
1004         unsigned long cnt = 0;
1005
1006         c = My_Clients;
1007         while( c )
1008         {
1009                 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1010                 c = (CLIENT *)c->next;
1011         }
1012
1013         return cnt;
1014 } /* Client_UnknownCount */
1015
1016
1017 GLOBAL long
1018 Client_MaxUserCount( void )
1019 {
1020         return Max_Users;
1021 } /* Client_MaxUserCount */
1022
1023
1024 GLOBAL long
1025 Client_MyMaxUserCount( void )
1026 {
1027         return My_Max_Users;
1028 } /* Client_MyMaxUserCount */
1029
1030
1031 /**
1032  * Check that a given nickname is valid.
1033  *
1034  * @param       Nick the nickname to check.
1035  * @returns     true if nickname is valid, false otherwise.
1036  */
1037 GLOBAL bool
1038 Client_IsValidNick(const char *Nick)
1039 {
1040         const char *ptr;
1041         static const char goodchars[] = ";0123456789-";
1042
1043         assert (Nick != NULL);
1044
1045         if (strchr(goodchars, Nick[0]))
1046                 return false;
1047         if (strlen(Nick ) >= Conf_MaxNickLength)
1048                 return false;
1049
1050         ptr = Nick;
1051         while (*ptr) {
1052                 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1053                         return false;
1054                 if (*ptr > '}')
1055                         return false;
1056                 ptr++;
1057         }
1058
1059         return true;
1060 } /* Client_IsValidNick */
1061
1062
1063 /**
1064  * Return pointer to "My_Whowas" structure.
1065  */
1066 GLOBAL WHOWAS *
1067 Client_GetWhowas( void )
1068 {
1069         return My_Whowas;
1070 } /* Client_GetWhowas */
1071
1072 /**
1073  * Return the index of the last used WHOWAS entry.
1074  */
1075 GLOBAL int
1076 Client_GetLastWhowasIndex( void )
1077 {
1078         return Last_Whowas;
1079 } /* Client_GetLastWhowasIndex */
1080
1081
1082 /**
1083  * Get the start time of this client.
1084  * The result is the start time in seconds since 1970-01-01, as reported
1085  * by the C function time(NULL).
1086  */
1087 GLOBAL time_t
1088 Client_StartTime(CLIENT *Client)
1089 {
1090         assert( Client != NULL );
1091         return Client->starttime;
1092 } /* Client_Uptime */
1093
1094
1095 static unsigned long
1096 Count( CLIENT_TYPE Type )
1097 {
1098         CLIENT *c;
1099         unsigned long cnt = 0;
1100
1101         c = My_Clients;
1102         while( c )
1103         {
1104                 if( c->type == Type ) cnt++;
1105                 c = (CLIENT *)c->next;
1106         }
1107         return cnt;
1108 } /* Count */
1109
1110
1111 static unsigned long
1112 MyCount( CLIENT_TYPE Type )
1113 {
1114         CLIENT *c;
1115         unsigned long cnt = 0;
1116
1117         c = My_Clients;
1118         while( c )
1119         {
1120                 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1121                 c = (CLIENT *)c->next;
1122         }
1123         return cnt;
1124 } /* MyCount */
1125
1126
1127 static CLIENT *
1128 New_Client_Struct( void )
1129 {
1130         /* Neue CLIENT-Struktur pre-initialisieren */
1131
1132         CLIENT *c;
1133
1134         c = (CLIENT *)malloc( sizeof( CLIENT ));
1135         if( ! c )
1136         {
1137                 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1138                 return NULL;
1139         }
1140
1141         memset( c, 0, sizeof ( CLIENT ));
1142
1143         c->type = CLIENT_UNKNOWN;
1144         c->conn_id = NONE;
1145         c->oper_by_me = false;
1146         c->hops = -1;
1147         c->token = -1;
1148         c->mytoken = -1;
1149
1150         return c;
1151 } /* New_Client */
1152
1153
1154 static void
1155 Generate_MyToken( CLIENT *Client )
1156 {
1157         CLIENT *c;
1158         int token;
1159
1160         c = My_Clients;
1161         token = 2;
1162         while( c )
1163         {
1164                 if( c->mytoken == token )
1165                 {
1166                         /* Das Token wurde bereits vergeben */
1167                         token++;
1168                         c = My_Clients;
1169                         continue;
1170                 }
1171                 else c = (CLIENT *)c->next;
1172         }
1173         Client->mytoken = token;
1174         LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1175 } /* Generate_MyToken */
1176
1177
1178 static void
1179 Adjust_Counters( CLIENT *Client )
1180 {
1181         long count;
1182
1183         assert( Client != NULL );
1184
1185         if( Client->type != CLIENT_USER ) return;
1186
1187         if( Client->conn_id != NONE )
1188         {
1189                 /* Local connection */
1190                 count = Client_MyUserCount( );
1191                 if( count > My_Max_Users ) My_Max_Users = count;
1192         }
1193         count = Client_UserCount( );
1194         if( count > Max_Users ) Max_Users = count;
1195 } /* Adjust_Counters */
1196
1197
1198 /**
1199  * Register client in My_Whowas structure for further recall by WHOWAS.
1200  * Note: Only clients that have been connected at least 30 seconds will be
1201  * registered to prevent automated IRC bots to "destroy" a nice server
1202  * history database.
1203  */
1204 GLOBAL void
1205 Client_RegisterWhowas( CLIENT *Client )
1206 {
1207         int slot;
1208         time_t now;
1209
1210         assert( Client != NULL );
1211
1212         /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1213         if (Conf_MorePrivacy)
1214                 return;
1215
1216         now = time(NULL);
1217         /* Don't register clients that were connected less than 30 seconds. */
1218         if( now - Client->starttime < 30 )
1219                 return;
1220
1221         slot = Last_Whowas + 1;
1222         if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1223
1224 #ifdef DEBUG
1225         Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1226 #endif
1227
1228         My_Whowas[slot].time = now;
1229         strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1230                  sizeof( My_Whowas[slot].id ));
1231         strlcpy( My_Whowas[slot].user, Client_User( Client ),
1232                  sizeof( My_Whowas[slot].user ));
1233         strlcpy( My_Whowas[slot].host, Client_HostnameCloaked( Client ),
1234                  sizeof( My_Whowas[slot].host ));
1235         strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1236                  sizeof( My_Whowas[slot].info ));
1237         strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1238                  sizeof( My_Whowas[slot].server ));
1239
1240         Last_Whowas = slot;
1241 } /* Client_RegisterWhowas */
1242
1243
1244 GLOBAL const char *
1245 Client_TypeText(CLIENT *Client)
1246 {
1247         assert(Client != NULL);
1248         switch (Client_Type(Client)) {
1249                 case CLIENT_USER:
1250                         return "User";
1251                         break;
1252                 case CLIENT_SERVICE:
1253                         return "Service";
1254                         break;
1255                 case CLIENT_SERVER:
1256                         return "Server";
1257                         break;
1258                 default:
1259                         return "Client";
1260         }
1261 } /* Client_TypeText */
1262
1263
1264 /**
1265  * Destroy user or service client.
1266  */
1267 static void
1268 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1269 {
1270         if(Client->conn_id != NONE) {
1271                 /* Local (directly connected) client */
1272                 Log(LOG_NOTICE,
1273                     "%s \"%s\" unregistered (connection %d): %s",
1274                     Client_TypeText(Client), Client_Mask(Client),
1275                     Client->conn_id, Txt);
1276                 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1277                                  Client_ID(Client), Client_User(Client),
1278                                  Client_Hostname(Client), Txt);
1279
1280                 if (SendQuit) {
1281                         /* Inforam all the other servers */
1282                         if (FwdMsg)
1283                                 IRC_WriteStrServersPrefix(NULL,
1284                                                 Client, "QUIT :%s", FwdMsg );
1285                         else
1286                                 IRC_WriteStrServersPrefix(NULL,
1287                                                 Client, "QUIT :");
1288                 }
1289         } else {
1290                 /* Remote client */
1291                 LogDebug("%s \"%s\" unregistered: %s",
1292                          Client_TypeText(Client), Client_Mask(Client), Txt);
1293
1294                 if(SendQuit) {
1295                         /* Inform all the other servers, but the ones in the
1296                          * direction we got the QUIT from */
1297                         if(FwdMsg)
1298                                 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1299                                                 Client, "QUIT :%s", FwdMsg );
1300                         else
1301                                 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1302                                                 Client, "QUIT :" );
1303                 }
1304         }
1305
1306         /* Unregister client from channels */
1307         Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1308
1309         /* Register client in My_Whowas structure */
1310         Client_RegisterWhowas(Client);
1311 } /* Destroy_UserOrService */
1312
1313
1314 #ifdef DEBUG
1315
1316 GLOBAL void
1317 Client_DebugDump(void)
1318 {
1319         CLIENT *c;
1320
1321         Log(LOG_DEBUG, "Client status:");
1322         c = My_Clients;
1323         while (c) {
1324                 Log(LOG_DEBUG,
1325                     " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1326                    Client_ID(c), Client_Type(c), Client_Hostname(c),
1327                    Client_User(c), Client_Conn(c), Client_StartTime(c),
1328                    Client_Flags(c));
1329                 c = (CLIENT *)c->next;
1330         }
1331 } /* Client_DumpClients */
1332
1333 #endif
1334
1335
1336 /* -eof- */