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