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