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