]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/client.c
New config option NoDNS: disables all DNS queries.
[ngircd-alex.git] / src / ngircd / client.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2005 by 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 static char UNUSED id[] = "$Id: client.c,v 1.96 2007/10/25 11:01:19 fw Exp $";
21
22 #include "imp.h"
23 #include <assert.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <strings.h>
29 #include <netdb.h>
30
31 #include "defines.h"
32 #include "conn.h"
33
34 #include "exp.h"
35 #include "client.h"
36
37 #include <imp.h>
38 #include "ngircd.h"
39 #include "channel.h"
40 #include "resolve.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 static char GetID_Buffer[GETID_LEN];
55
56 static WHOWAS My_Whowas[MAX_WHOWAS];
57 static int Last_Whowas = -1;
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, char *ID, char *User, char *Hostname,
69  char *Info, int Hops, int Token, char *Modes, bool Idented));
70
71
72 long Max_Users = 0, My_Max_Users = 0;
73
74
75 GLOBAL void
76 Client_Init( void )
77 {
78         struct hostent *h;
79         
80         This_Server = New_Client_Struct( );
81         if( ! This_Server )
82         {
83                 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
84                 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
85                 exit( 1 );
86         }
87
88         /* Client-Struktur dieses Servers */
89         This_Server->next = NULL;
90         This_Server->type = CLIENT_SERVER;
91         This_Server->conn_id = NONE;
92         This_Server->introducer = This_Server;
93         This_Server->mytoken = 1;
94         This_Server->hops = 0;
95
96         gethostname( This_Server->host, CLIENT_HOST_LEN );
97         if (!Conf_NoDNS) {
98                 h = gethostbyname( This_Server->host );
99                 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
100         }
101         Client_SetID( This_Server, Conf_ServerName );
102         Client_SetInfo( This_Server, Conf_ServerInfo );
103
104         My_Clients = This_Server;
105         
106         memset( &My_Whowas, 0, sizeof( My_Whowas ));
107 } /* Client_Init */
108
109
110 GLOBAL void
111 Client_Exit( void )
112 {
113         CLIENT *c, *next;
114         int cnt;
115
116         if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
117         else Client_Destroy( This_Server, "Server going down.", NULL, false );
118         
119         cnt = 0;
120         c = My_Clients;
121         while( c )
122         {
123                 cnt++;
124                 next = (CLIENT *)c->next;
125                 free( c );
126                 c = next;
127         }
128         if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
129 } /* Client_Exit */
130
131
132 GLOBAL CLIENT *
133 Client_ThisServer( void )
134 {
135         return This_Server;
136 } /* Client_ThisServer */
137
138
139 /**
140  * Initialize new local client; wrapper function for Init_New_Client().
141  * @return New CLIENT structure.
142  */
143 GLOBAL CLIENT *
144 Client_NewLocal(CONN_ID Idx, char *Hostname, int Type, bool Idented)
145 {
146         return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
147                 Hostname, NULL, 0, 0, NULL, Idented);
148 } /* Client_NewLocal */
149
150
151 /**
152  * Initialize new remote server; wrapper function for Init_New_Client().
153  * @return New CLIENT structure.
154  */
155 GLOBAL CLIENT *
156 Client_NewRemoteServer(CLIENT *Introducer, char *Hostname, CLIENT *TopServer,
157  int Hops, int Token, char *Info, bool Idented)
158 {
159         return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
160                 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
161 } /* Client_NewRemoteServer */
162
163
164 /**
165  * Initialize new remote client; wrapper function for Init_New_Client().
166  * @return New CLIENT structure.
167  */
168 GLOBAL CLIENT *
169 Client_NewRemoteUser(CLIENT *Introducer, char *Nick, int Hops, char *User,
170  char *Hostname, int Token, char *Modes, char *Info, bool Idented)
171 {
172         return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
173                 User, Hostname, Info, Hops, Token, Modes, Idented);
174 } /* Client_NewRemoteUser */
175
176
177 /**
178  * Initialize new client and set up the given parameters like client type,
179  * user name, host name, introducing server etc. ...
180  * @return New CLIENT structure.
181  */
182 static CLIENT *
183 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
184  int Type, char *ID, char *User, char *Hostname, char *Info, int Hops,
185  int Token, char *Modes, bool Idented)
186 {
187         CLIENT *client;
188
189         assert( Idx >= NONE );
190         assert( Introducer != NULL );
191         assert( Hostname != NULL );
192
193         client = New_Client_Struct( );
194         if( ! client ) return NULL;
195
196         /* Initialisieren */
197         client->starttime = time(NULL);
198         client->conn_id = Idx;
199         client->introducer = Introducer;
200         client->topserver = TopServer;
201         client->type = Type;
202         if( ID ) Client_SetID( client, ID );
203         if( User ) Client_SetUser( client, User, Idented );
204         if( Hostname ) Client_SetHostname( client, Hostname );
205         if( Info ) Client_SetInfo( client, Info );
206         client->hops = Hops;
207         client->token = Token;
208         if( Modes ) Client_SetModes( client, Modes );
209         if( Type == CLIENT_SERVER ) Generate_MyToken( client );
210
211         if( strchr( client->modes, 'a' ))
212                 strlcpy( client->away, DEFAULT_AWAY_MSG, sizeof( client->away ));
213
214         /* Verketten */
215         client->next = (POINTER *)My_Clients;
216         My_Clients = client;
217
218         /* Adjust counters */
219         Adjust_Counters( client );
220
221         return client;
222 } /* Init_New_Client */
223
224
225 GLOBAL void
226 Client_Destroy( CLIENT *Client, char *LogMsg, char *FwdMsg, bool SendQuit )
227 {
228         /* Client entfernen. */
229         
230         CLIENT *last, *c;
231         char msg[LINE_LEN], *txt;
232
233         assert( Client != NULL );
234
235         if( LogMsg ) txt = LogMsg;
236         else txt = FwdMsg;
237         if( ! txt ) txt = "Reason unknown.";
238
239         /* Netz-Split-Nachricht vorbereiten (noch nicht optimal) */
240         if( Client->type == CLIENT_SERVER ) {
241                 strlcpy(msg, This_Server->id, sizeof (msg));
242                 strlcat(msg, " ", sizeof (msg));
243                 strlcat(msg, Client->id, sizeof (msg));
244         }
245
246         last = NULL;
247         c = My_Clients;
248         while( c )
249         {
250                 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
251                 {
252                         /* der Client, der geloescht wird ist ein Server. Der Client, den wir gerade
253                          * pruefen, ist ein Child von diesem und muss daher auch entfernt werden */
254                         Client_Destroy( c, NULL, msg, false );
255                         last = NULL;
256                         c = My_Clients;
257                         continue;
258                 }
259                 if( c == Client )
260                 {
261                         /* Wir haben den Client gefunden: entfernen */
262                         if( last ) last->next = c->next;
263                         else My_Clients = (CLIENT *)c->next;
264
265                         if( c->type == CLIENT_USER )
266                         {
267                                 if( c->conn_id != NONE )
268                                 {
269                                         /* Ein lokaler User */
270                                         Log( LOG_NOTICE, "User \"%s\" unregistered (connection %d): %s", Client_Mask( c ), c->conn_id, txt );
271
272                                         if( SendQuit )
273                                         {
274                                                 /* Alle andere Server informieren! */
275                                                 if( FwdMsg ) IRC_WriteStrServersPrefix( NULL, c, "QUIT :%s", FwdMsg );
276                                                 else IRC_WriteStrServersPrefix( NULL, c, "QUIT :" );
277                                         }
278                                 }
279                                 else
280                                 {
281                                         /* Remote User */
282                                         Log( LOG_DEBUG, "User \"%s\" unregistered: %s", Client_Mask( c ), txt );
283
284                                         if( SendQuit )
285                                         {
286                                                 /* Andere Server informieren, ausser denen, die "in
287                                                  * Richtung dem liegen", auf dem der User registriert
288                                                  * ist. Von denen haben wir das QUIT ja wohl bekommen. */
289                                                 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "QUIT :%s", FwdMsg );
290                                                 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "QUIT :" );
291                                         }
292                                 }
293
294                                 /* Unregister client from channels */
295                                 Channel_Quit( c, FwdMsg ? FwdMsg : c->id );
296                                 
297                                 /* Register client in My_Whowas structure */
298                                 Client_RegisterWhowas( c );
299                         }
300                         else if( c->type == CLIENT_SERVER )
301                         {
302                                 if( c != This_Server )
303                                 {
304                                         if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
305                                         else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
306                                 }
307
308                                 /* andere Server informieren */
309                                 if( ! NGIRCd_SignalQuit )
310                                 {
311                                         if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
312                                         else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
313                                 }
314                         }
315                         else
316                         {
317                                 if( c->conn_id != NONE )
318                                 {
319                                         if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
320                                         else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
321                                 } else {
322                                         Log(LOG_WARNING, "Unregistered unknown client \"%s\": %s",
323                                                                 c->id[0] ? c->id : "(No Nick)", txt );
324                                 }
325                         }
326
327                         free( c );
328                         break;
329                 }
330                 last = c;
331                 c = (CLIENT *)c->next;
332         }
333 } /* Client_Destroy */
334
335
336 GLOBAL void
337 Client_SetHostname( CLIENT *Client, char *Hostname )
338 {
339         /* Hostname eines Clients setzen */
340
341         assert( Client != NULL );
342         assert( Hostname != NULL );
343
344         strlcpy( Client->host, Hostname, sizeof( Client->host ));
345 } /* Client_SetHostname */
346
347
348 GLOBAL void
349 Client_SetID( CLIENT *Client, char *ID )
350 {
351         /* Hostname eines Clients setzen, Hash-Wert berechnen */
352
353         assert( Client != NULL );
354         assert( ID != NULL );
355         
356         strlcpy( Client->id, ID, sizeof( Client->id ));
357
358         /* Hash */
359         Client->hash = Hash( Client->id );
360 } /* Client_SetID */
361
362
363 GLOBAL void
364 Client_SetUser( CLIENT *Client, char *User, bool Idented )
365 {
366         /* Username eines Clients setzen */
367
368         assert( Client != NULL );
369         assert( User != NULL );
370
371         if( Idented ) strlcpy( Client->user, User, sizeof( Client->user ));
372         else
373         {
374                 Client->user[0] = '~';
375                 strlcpy( Client->user + 1, User, sizeof( Client->user ) - 1 );
376         }
377 } /* Client_SetUser */
378
379
380 GLOBAL void
381 Client_SetInfo( CLIENT *Client, char *Info )
382 {
383         /* Hostname eines Clients setzen */
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, char *Modes )
394 {
395         /* Modes eines Clients setzen */
396
397         assert( Client != NULL );
398         assert( Modes != NULL );
399
400         strlcpy( Client->modes, Modes, sizeof( Client->modes ));
401 } /* Client_SetModes */
402
403
404 GLOBAL void
405 Client_SetFlags( CLIENT *Client, char *Flags )
406 {
407         /* Flags eines Clients setzen */
408
409         assert( Client != NULL );
410         assert( Flags != NULL );
411
412         strlcpy( Client->flags, Flags, sizeof( Client->flags ));
413 } /* Client_SetFlags */
414
415
416 GLOBAL void
417 Client_SetPassword( CLIENT *Client, char *Pwd )
418 {
419         /* Von einem Client geliefertes Passwort */
420
421         assert( Client != NULL );
422         assert( Pwd != NULL );
423
424         strlcpy( Client->pwd, Pwd, sizeof( Client->pwd ));
425 } /* Client_SetPassword */
426
427
428 GLOBAL void
429 Client_SetAway( CLIENT *Client, char *Txt )
430 {
431         /* Set AWAY reason of client */
432
433         assert( Client != NULL );
434         assert( Txt != NULL );
435
436         strlcpy( Client->away, Txt, sizeof( Client->away ));
437         Log( LOG_DEBUG, "User \"%s\" is away: %s", Client_Mask( Client ), Txt );
438 } /* Client_SetAway */
439
440
441 GLOBAL void
442 Client_SetType( CLIENT *Client, int Type )
443 {
444         assert( Client != NULL );
445         Client->type = Type;
446         if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
447         Adjust_Counters( Client );
448 } /* Client_SetType */
449
450
451 GLOBAL void
452 Client_SetHops( CLIENT *Client, int Hops )
453 {
454         assert( Client != NULL );
455         Client->hops = Hops;
456 } /* Client_SetHops */
457
458
459 GLOBAL void
460 Client_SetToken( CLIENT *Client, int Token )
461 {
462         assert( Client != NULL );
463         Client->token = Token;
464 } /* Client_SetToken */
465
466
467 GLOBAL void
468 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
469 {
470         assert( Client != NULL );
471         assert( Introducer != NULL );
472         Client->introducer = Introducer;
473 } /* Client_SetIntroducer */
474
475
476 GLOBAL void
477 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
478 {
479         assert( Client != NULL );
480         Client->oper_by_me = OperByMe;
481 } /* Client_SetOperByMe */
482
483
484 GLOBAL bool
485 Client_ModeAdd( CLIENT *Client, char Mode )
486 {
487         /* Set Mode.
488          * If Client already alread had Mode, return false.
489          * If the Mode was newly set, return true.
490          */
491
492         char x[2];
493
494         assert( Client != NULL );
495
496         x[0] = Mode; x[1] = '\0';
497         if( ! strchr( Client->modes, x[0] ))
498         {
499                 /* Client hat den Mode noch nicht -> setzen */
500                 strlcat( Client->modes, x, sizeof( Client->modes ));
501                 return true;
502         }
503         else return false;
504 } /* Client_ModeAdd */
505
506
507 GLOBAL bool
508 Client_ModeDel( CLIENT *Client, char Mode )
509 {
510         /* Delete Mode.
511          * If Mode was removed, return true.
512          * If Client did not have Mode, return false.
513          */
514
515         char x[2], *p;
516
517         assert( Client != NULL );
518
519         x[0] = Mode; x[1] = '\0';
520
521         p = strchr( Client->modes, x[0] );
522         if( ! p ) return false;
523
524         /* Client has Mode -> delete */
525         while( *p )
526         {
527                 *p = *(p + 1);
528                 p++;
529         }
530         return true;
531 } /* Client_ModeDel */
532
533
534 GLOBAL CLIENT *
535 Client_Search( char *Nick )
536 {
537         /* return Client-Structure that has the corresponding Nick.
538          * If none is found, return NULL.
539          */
540
541         char search_id[CLIENT_ID_LEN], *ptr;
542         CLIENT *c = NULL;
543         UINT32 search_hash;
544
545         assert( Nick != NULL );
546
547         /* copy Nick and truncate hostmask if necessary */
548         strlcpy( search_id, Nick, sizeof( search_id ));
549         ptr = strchr( search_id, '!' );
550         if( ptr ) *ptr = '\0';
551
552         search_hash = Hash( search_id );
553
554         c = My_Clients;
555         while( c )
556         {
557                 if( c->hash == search_hash )
558                 {
559                         /* lt. Hash-Wert: Treffer! */
560                         if( strcasecmp( c->id, search_id ) == 0 ) return c;
561                 }
562                 c = (CLIENT *)c->next;
563         }
564         return NULL;
565 } /* Client_Search */
566
567
568 GLOBAL CLIENT *
569 Client_GetFromToken( CLIENT *Client, int Token )
570 {
571         /* Client-Struktur, die den entsprechenden Introducer (=Client)
572          * und das gegebene Token hat, liefern. Wird keine gefunden,
573          * so wird NULL geliefert. */
574
575         CLIENT *c;
576
577         assert( Client != NULL );
578         assert( Token > 0 );
579
580         c = My_Clients;
581         while( c )
582         {
583                 if(( c->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c->token == Token )) return c;
584                 c = (CLIENT *)c->next;
585         }
586         return NULL;
587 } /* Client_GetFromToken */
588
589
590 GLOBAL int
591 Client_Type( CLIENT *Client )
592 {
593         assert( Client != NULL );
594         return Client->type;
595 } /* Client_Type */
596
597
598 GLOBAL CONN_ID
599 Client_Conn( CLIENT *Client )
600 {
601         assert( Client != NULL );
602         return Client->conn_id;
603 } /* Client_Conn */
604
605
606 GLOBAL char *
607 Client_ID( CLIENT *Client )
608 {
609         assert( Client != NULL );
610
611 #ifdef DEBUG
612         if( Client->type == CLIENT_USER ) assert( strlen( Client->id ) < CLIENT_NICK_LEN );
613 #endif
614                                                    
615         if( Client->id[0] ) return Client->id;
616         else return "*";
617 } /* Client_ID */
618
619
620 GLOBAL char *
621 Client_Info( CLIENT *Client )
622 {
623         assert( Client != NULL );
624         return Client->info;
625 } /* Client_Info */
626
627
628 GLOBAL char *
629 Client_User( CLIENT *Client )
630 {
631         assert( Client != NULL );
632         return Client->user[0] ? Client->user : "~";
633 } /* Client_User */
634
635
636 GLOBAL char *
637 Client_Hostname( CLIENT *Client )
638 {
639         assert( Client != NULL );
640         return Client->host;
641 } /* Client_Hostname */
642
643
644 GLOBAL char *
645 Client_Password( CLIENT *Client )
646 {
647         assert( Client != NULL );
648         return Client->pwd;
649 } /* Client_Password */
650
651
652 GLOBAL char *
653 Client_Modes( CLIENT *Client )
654 {
655         assert( Client != NULL );
656         return Client->modes;
657 } /* Client_Modes */
658
659
660 GLOBAL char *
661 Client_Flags( CLIENT *Client )
662 {
663         assert( Client != NULL );
664         return Client->flags;
665 } /* Client_Flags */
666
667
668 GLOBAL bool
669 Client_OperByMe( CLIENT *Client )
670 {
671         assert( Client != NULL );
672         return Client->oper_by_me;
673 } /* Client_OperByMe */
674
675
676 GLOBAL int
677 Client_Hops( CLIENT *Client )
678 {
679         assert( Client != NULL );
680         return Client->hops;
681 } /* Client_Hops */
682
683
684 GLOBAL int
685 Client_Token( CLIENT *Client )
686 {
687         assert( Client != NULL );
688         return Client->token;
689 } /* Client_Token */
690
691
692 GLOBAL int
693 Client_MyToken( CLIENT *Client )
694 {
695         assert( Client != NULL );
696         return Client->mytoken;
697 } /* Client_MyToken */
698
699
700 GLOBAL CLIENT *
701 Client_NextHop( CLIENT *Client )
702 {
703         CLIENT *c;
704
705         assert( Client != NULL );
706
707         c = Client;
708         while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
709                 c = c->introducer;
710
711         return c;
712 } /* Client_NextHop */
713
714
715 GLOBAL char *
716 Client_Mask( CLIENT *Client )
717 {
718         /* Client-"ID" liefern, wie sie z.B. fuer
719          * Prefixe benoetigt wird. */
720
721         assert( Client != NULL );
722
723         if( Client->type == CLIENT_SERVER ) return Client->id;
724
725         snprintf( GetID_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user, Client->host );
726         return GetID_Buffer;
727 } /* Client_Mask */
728
729
730 GLOBAL CLIENT *
731 Client_Introducer( CLIENT *Client )
732 {
733         assert( Client != NULL );
734         return Client->introducer;
735 } /* Client_Introducer */
736
737
738 GLOBAL CLIENT *
739 Client_TopServer( CLIENT *Client )
740 {
741         assert( Client != NULL );
742         return Client->topserver;
743 } /* Client_TopServer */
744
745
746 GLOBAL bool
747 Client_HasMode( CLIENT *Client, char Mode )
748 {
749         assert( Client != NULL );
750         return strchr( Client->modes, Mode ) != NULL;
751 } /* Client_HasMode */
752
753
754 GLOBAL char *
755 Client_Away( CLIENT *Client )
756 {
757         /* AWAY-Text liefern */
758
759         assert( Client != NULL );
760         return Client->away;
761 } /* Client_Away */
762
763
764 GLOBAL bool
765 Client_CheckNick( CLIENT *Client, char *Nick )
766 {
767         assert( Client != NULL );
768         assert( Nick != NULL );
769
770         if( ! Client_IsValidNick( Nick ))
771         {
772                 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
773                 return false;
774         }
775
776         /* Nick bereits vergeben? */
777         if( Client_Search( Nick ))
778         {
779                 /* den Nick gibt es bereits */
780                 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
781                 return false;
782         }
783
784         return true;
785 } /* Client_CheckNick */
786
787
788 GLOBAL bool
789 Client_CheckID( CLIENT *Client, char *ID )
790 {
791         /* Nick ueberpruefen */
792
793         char str[COMMAND_LEN];
794         CLIENT *c;
795
796         assert( Client != NULL );
797         assert( Client->conn_id > NONE );
798         assert( ID != NULL );
799
800         /* Nick zu lang? */
801         if( strlen( ID ) > CLIENT_ID_LEN )
802         {
803                 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), ID );
804                 return false;
805         }
806
807         /* ID bereits vergeben? */
808         c = My_Clients;
809         while( c )
810         {
811                 if( strcasecmp( c->id, ID ) == 0 )
812                 {
813                         /* die Server-ID gibt es bereits */
814                         snprintf( str, sizeof( str ), "ID \"%s\" already registered", ID );
815                         if( Client->conn_id != c->conn_id ) Log( LOG_ERR, "%s (on connection %d)!", str, c->conn_id );
816                         else Log( LOG_ERR, "%s (via network)!", str );
817                         Conn_Close( Client->conn_id, str, str, true);
818                         return false;
819                 }
820                 c = (CLIENT *)c->next;
821         }
822
823         return true;
824 } /* Client_CheckID */
825
826
827 GLOBAL CLIENT *
828 Client_First( void )
829 {
830         /* Ersten Client liefern. */
831
832         return My_Clients;
833 } /* Client_First */
834
835
836 GLOBAL CLIENT *
837 Client_Next( CLIENT *c )
838 {
839         /* Naechsten Client liefern. Existiert keiner,
840          * so wird NULL geliefert. */
841
842         assert( c != NULL );
843         return (CLIENT *)c->next;
844 } /* Client_Next */
845
846
847 GLOBAL long
848 Client_UserCount( void )
849 {
850         return Count( CLIENT_USER );
851 } /* Client_UserCount */
852
853
854 GLOBAL long
855 Client_ServiceCount( void )
856 {
857         return Count( CLIENT_SERVICE );;
858 } /* Client_ServiceCount */
859
860
861 GLOBAL long
862 Client_ServerCount( void )
863 {
864         return Count( CLIENT_SERVER );
865 } /* Client_ServerCount */
866
867
868 GLOBAL long
869 Client_MyUserCount( void )
870 {
871         return MyCount( CLIENT_USER );
872 } /* Client_MyUserCount */
873
874
875 GLOBAL long
876 Client_MyServiceCount( void )
877 {
878         return MyCount( CLIENT_SERVICE );
879 } /* Client_MyServiceCount */
880
881
882 GLOBAL unsigned long
883 Client_MyServerCount( void )
884 {
885         CLIENT *c;
886         unsigned long cnt = 0;
887
888         c = My_Clients;
889         while( c )
890         {
891                 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
892                 c = (CLIENT *)c->next;
893         }
894         return cnt;
895 } /* Client_MyServerCount */
896
897
898 GLOBAL unsigned long
899 Client_OperCount( void )
900 {
901         CLIENT *c;
902         unsigned long cnt = 0;
903
904         c = My_Clients;
905         while( c )
906         {
907                 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
908                 c = (CLIENT *)c->next;
909         }
910         return cnt;
911 } /* Client_OperCount */
912
913
914 GLOBAL unsigned long
915 Client_UnknownCount( void )
916 {
917         CLIENT *c;
918         unsigned long cnt = 0;
919
920         c = My_Clients;
921         while( c )
922         {
923                 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
924                 c = (CLIENT *)c->next;
925         }
926
927         return cnt;
928 } /* Client_UnknownCount */
929
930
931 GLOBAL long
932 Client_MaxUserCount( void )
933 {
934         return Max_Users;
935 } /* Client_MaxUserCount */
936
937
938 GLOBAL long
939 Client_MyMaxUserCount( void )
940 {
941         return My_Max_Users;
942 } /* Client_MyMaxUserCount */
943
944
945 GLOBAL bool
946 Client_IsValidNick( const char *Nick )
947 {
948         const char *ptr;
949         static const char goodchars[] = ";0123456789-";
950
951         assert( Nick != NULL );
952
953         if( Nick[0] == '#' ) return false;
954         if( strchr( goodchars, Nick[0] )) return false;
955         if( strlen( Nick ) >= CLIENT_NICK_LEN ) return false;
956
957         ptr = Nick;
958         while( *ptr )
959         {
960                 if (( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return false;
961                 if ( *ptr > '}' ) return false;
962                 ptr++;
963         }
964
965         return true;
966 } /* Client_IsValidNick */
967
968
969 /**
970  * Return pointer to "My_Whowas" structure.
971  */
972 GLOBAL WHOWAS *
973 Client_GetWhowas( void )
974 {
975         return My_Whowas;
976 } /* Client_GetWhowas */
977
978 /**
979  * Return the index of the last used WHOWAS entry.
980  */
981 GLOBAL int
982 Client_GetLastWhowasIndex( void )
983 {
984         return Last_Whowas;
985 } /* Client_GetLastWhowasIndex */
986
987
988 /**
989  * Get the start time of this client.
990  * The result is the start time in seconds since 1970-01-01, as reported
991  * by the C function time(NULL).
992  */
993 GLOBAL time_t
994 Client_StartTime(CLIENT *Client)
995 {
996         assert( Client != NULL );
997         return Client->starttime;
998 } /* Client_Uptime */
999
1000
1001 static unsigned long
1002 Count( CLIENT_TYPE Type )
1003 {
1004         CLIENT *c;
1005         unsigned long cnt = 0;
1006
1007         c = My_Clients;
1008         while( c )
1009         {
1010                 if( c->type == Type ) cnt++;
1011                 c = (CLIENT *)c->next;
1012         }
1013         return cnt;
1014 } /* Count */
1015
1016
1017 static unsigned long
1018 MyCount( CLIENT_TYPE Type )
1019 {
1020         CLIENT *c;
1021         unsigned long cnt = 0;
1022
1023         c = My_Clients;
1024         while( c )
1025         {
1026                 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1027                 c = (CLIENT *)c->next;
1028         }
1029         return cnt;
1030 } /* MyCount */
1031
1032
1033 static CLIENT *
1034 New_Client_Struct( void )
1035 {
1036         /* Neue CLIENT-Struktur pre-initialisieren */
1037
1038         CLIENT *c;
1039
1040         c = (CLIENT *)malloc( sizeof( CLIENT ));
1041         if( ! c )
1042         {
1043                 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1044                 return NULL;
1045         }
1046
1047         memset( c, 0, sizeof ( CLIENT ));
1048
1049         c->type = CLIENT_UNKNOWN;
1050         c->conn_id = NONE;
1051         c->oper_by_me = false;
1052         c->hops = -1;
1053         c->token = -1;
1054         c->mytoken = -1;
1055
1056         return c;
1057 } /* New_Client */
1058
1059
1060 static void
1061 Generate_MyToken( CLIENT *Client )
1062 {
1063         CLIENT *c;
1064         int token;
1065
1066         c = My_Clients;
1067         token = 2;
1068         while( c )
1069         {
1070                 if( c->mytoken == token )
1071                 {
1072                         /* Das Token wurde bereits vergeben */
1073                         token++;
1074                         c = My_Clients;
1075                         continue;
1076                 }
1077                 else c = (CLIENT *)c->next;
1078         }
1079         Client->mytoken = token;
1080         Log( LOG_DEBUG, "Assigned token %d to server \"%s\".", token, Client->id );
1081 } /* Generate_MyToken */
1082
1083
1084 static void
1085 Adjust_Counters( CLIENT *Client )
1086 {
1087         long count;
1088
1089         assert( Client != NULL );
1090
1091         if( Client->type != CLIENT_USER ) return;
1092
1093         if( Client->conn_id != NONE )
1094         {
1095                 /* Local connection */
1096                 count = Client_MyUserCount( );
1097                 if( count > My_Max_Users ) My_Max_Users = count;
1098         }
1099         count = Client_UserCount( );
1100         if( count > Max_Users ) Max_Users = count;
1101 } /* Adjust_Counters */
1102
1103
1104 /**
1105  * Register client in My_Whowas structure for further recall by WHOWAS.
1106  * Note: Only clients that have been connected at least 30 seconds will be
1107  * registered to prevent automated IRC bots to "destroy" a nice server
1108  * history database.
1109  */
1110 GLOBAL void
1111 Client_RegisterWhowas( CLIENT *Client )
1112 {
1113         int slot;
1114         time_t now;
1115
1116         assert( Client != NULL );
1117
1118         now = time(NULL);
1119         /* Don't register clients that were connected less than 30 seconds. */
1120         if( now - Client->starttime < 30 )
1121                 return;
1122
1123         slot = Last_Whowas + 1;
1124         if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1125
1126 #ifdef DEBUG
1127         Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1128 #endif
1129
1130         My_Whowas[slot].time = now;
1131         strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1132                  sizeof( My_Whowas[slot].id ));
1133         strlcpy( My_Whowas[slot].user, Client_User( Client ),
1134                  sizeof( My_Whowas[slot].user ));
1135         strlcpy( My_Whowas[slot].host, Client_Hostname( Client ),
1136                  sizeof( My_Whowas[slot].host ));
1137         strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1138                  sizeof( My_Whowas[slot].info ));
1139         strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1140                  sizeof( My_Whowas[slot].server ));
1141
1142         Last_Whowas = slot;
1143 } /* Client_RegisterWhowas */
1144
1145
1146 /* -eof- */