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