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