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