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