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