]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/client.c
Make sourcecode compatible with ansi2knr again
[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_NoDNS) {
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 GLOBAL CLIENT *
557 Client_GetFromToken( CLIENT *Client, int Token )
558 {
559         /* Client-Struktur, die den entsprechenden Introducer (=Client)
560          * und das gegebene Token hat, liefern. Wird keine gefunden,
561          * so wird NULL geliefert. */
562
563         CLIENT *c;
564
565         assert( Client != NULL );
566         assert( Token > 0 );
567
568         c = My_Clients;
569         while (c) {
570                 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
571                         (c->token == Token))
572                                 return c;
573                 c = (CLIENT *)c->next;
574         }
575         return NULL;
576 } /* Client_GetFromToken */
577
578
579 GLOBAL int
580 Client_Type( CLIENT *Client )
581 {
582         assert( Client != NULL );
583         return Client->type;
584 } /* Client_Type */
585
586
587 GLOBAL CONN_ID
588 Client_Conn( CLIENT *Client )
589 {
590         assert( Client != NULL );
591         return Client->conn_id;
592 } /* Client_Conn */
593
594
595 GLOBAL char *
596 Client_ID( CLIENT *Client )
597 {
598         assert( Client != NULL );
599
600 #ifdef DEBUG
601         if(Client->type == CLIENT_USER)
602                 assert(strlen(Client->id) < Conf_MaxNickLength);
603 #endif
604                                                    
605         if( Client->id[0] ) return Client->id;
606         else return "*";
607 } /* Client_ID */
608
609
610 GLOBAL char *
611 Client_Info( CLIENT *Client )
612 {
613         assert( Client != NULL );
614         return Client->info;
615 } /* Client_Info */
616
617
618 GLOBAL char *
619 Client_User( CLIENT *Client )
620 {
621         assert( Client != NULL );
622         return Client->user[0] ? Client->user : "~";
623 } /* Client_User */
624
625
626 #ifdef PAM
627
628 /**
629  * Get the "original" user name as supplied by the USER command.
630  * The user name as given by the client is used for authentication instead
631  * of the one detected using IDENT requests.
632  * @param Client The client.
633  * @return Original user name.
634  */
635 GLOBAL char *
636 Client_OrigUser(CLIENT *Client) {
637 #ifndef IDENTAUTH
638         char *user = Client->user;
639
640         if (user[0] == '~')
641                 user++;
642         return user;
643 #else
644         return Client->orig_user;
645 #endif
646 } /* Client_OrigUser */
647
648 #endif
649
650
651 /**
652  * Return the hostname of a client.
653  * @param Client Pointer to client structure
654  * @return Pointer to client hostname
655  */
656 GLOBAL char *
657 Client_Hostname(CLIENT *Client)
658 {
659         assert (Client != NULL);
660         return Client->host;
661 } /* Client_Hostname */
662
663
664 /**
665  * Get potentially cloaked hostname of a client.
666  * If the client has not enabled cloaking, the real hostname is used.
667  * @param Client Pointer to client structure
668  * @return Pointer to client hostname
669  */
670 GLOBAL char *
671 Client_HostnameCloaked(CLIENT *Client)
672 {
673         assert(Client != NULL);
674         if (Client_HasMode(Client, 'x'))
675                 return Client_ID(Client->introducer);
676         else
677                 return Client_Hostname(Client);
678 } /* Client_HostnameCloaked */
679
680
681 GLOBAL char *
682 Client_Password( CLIENT *Client )
683 {
684         assert( Client != NULL );
685         return Client->pwd;
686 } /* Client_Password */
687
688
689 GLOBAL char *
690 Client_Modes( CLIENT *Client )
691 {
692         assert( Client != NULL );
693         return Client->modes;
694 } /* Client_Modes */
695
696
697 GLOBAL char *
698 Client_Flags( CLIENT *Client )
699 {
700         assert( Client != NULL );
701         return Client->flags;
702 } /* Client_Flags */
703
704
705 GLOBAL bool
706 Client_OperByMe( CLIENT *Client )
707 {
708         assert( Client != NULL );
709         return Client->oper_by_me;
710 } /* Client_OperByMe */
711
712
713 GLOBAL int
714 Client_Hops( CLIENT *Client )
715 {
716         assert( Client != NULL );
717         return Client->hops;
718 } /* Client_Hops */
719
720
721 GLOBAL int
722 Client_Token( CLIENT *Client )
723 {
724         assert( Client != NULL );
725         return Client->token;
726 } /* Client_Token */
727
728
729 GLOBAL int
730 Client_MyToken( CLIENT *Client )
731 {
732         assert( Client != NULL );
733         return Client->mytoken;
734 } /* Client_MyToken */
735
736
737 GLOBAL CLIENT *
738 Client_NextHop( CLIENT *Client )
739 {
740         CLIENT *c;
741
742         assert( Client != NULL );
743
744         c = Client;
745         while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
746                 c = c->introducer;
747
748         return c;
749 } /* Client_NextHop */
750
751
752 /**
753  * Return ID of a client: "client!user@host"
754  * This client ID is used for IRC prefixes, for example.
755  * Please note that this function uses a global static buffer, so you can't
756  * nest invocations without overwriting erlier results!
757  * @param Client Pointer to client structure
758  * @return Pointer to global buffer containing the client ID
759  */
760 GLOBAL char *
761 Client_Mask( CLIENT *Client )
762 {
763         static char Mask_Buffer[GETID_LEN];
764
765         assert (Client != NULL);
766
767         /* Servers: return name only, there is no "mask" */
768         if (Client->type == CLIENT_SERVER)
769                 return Client->id;
770
771         snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
772                  Client->id, Client->user, Client->host);
773         return Mask_Buffer;
774 } /* Client_Mask */
775
776
777 /**
778  * Return ID of a client with cloaked hostname: "client!user@server-name"
779  * This client ID is used for IRC prefixes, for example.
780  * Please note that this function uses a global static buffer, so you can't
781  * nest invocations without overwriting erlier results!
782  * If the client has not enabled cloaking, the real hostname is used.
783  * @param Client Pointer to client structure
784  * @return Pointer to global buffer containing the client ID
785  */
786 GLOBAL char *
787 Client_MaskCloaked(CLIENT *Client)
788 {
789         static char Mask_Buffer[GETID_LEN];
790
791         assert (Client != NULL);
792
793         /* Is the client using cloaking at all? */
794         if (!Client_HasMode(Client, 'x'))
795             return Client_Mask(Client);
796
797         snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
798                  Client->id, Client->user, Client_ID(Client->introducer));
799         return Mask_Buffer;
800 } /* Client_MaskCloaked */
801
802
803 GLOBAL CLIENT *
804 Client_Introducer( CLIENT *Client )
805 {
806         assert( Client != NULL );
807         return Client->introducer;
808 } /* Client_Introducer */
809
810
811 GLOBAL CLIENT *
812 Client_TopServer( CLIENT *Client )
813 {
814         assert( Client != NULL );
815         return Client->topserver;
816 } /* Client_TopServer */
817
818
819 GLOBAL bool
820 Client_HasMode( CLIENT *Client, char Mode )
821 {
822         assert( Client != NULL );
823         return strchr( Client->modes, Mode ) != NULL;
824 } /* Client_HasMode */
825
826
827 GLOBAL char *
828 Client_Away( CLIENT *Client )
829 {
830         assert( Client != NULL );
831         return Client->away;
832 } /* Client_Away */
833
834
835 GLOBAL bool
836 Client_CheckNick( CLIENT *Client, char *Nick )
837 {
838         assert( Client != NULL );
839         assert( Nick != NULL );
840
841         if (! Client_IsValidNick( Nick ))
842         {
843                 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
844                 return false;
845         }
846
847         /* Nick bereits vergeben? */
848         if( Client_Search( Nick ))
849         {
850                 /* den Nick gibt es bereits */
851                 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
852                 return false;
853         }
854
855         return true;
856 } /* Client_CheckNick */
857
858
859 GLOBAL bool
860 Client_CheckID( CLIENT *Client, char *ID )
861 {
862         char str[COMMAND_LEN];
863         CLIENT *c;
864
865         assert( Client != NULL );
866         assert( Client->conn_id > NONE );
867         assert( ID != NULL );
868
869         /* ID too long? */
870         if (strlen(ID) > CLIENT_ID_LEN) {
871                 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
872                 return false;
873         }
874
875         /* ID already in use? */
876         c = My_Clients;
877         while (c) {
878                 if (strcasecmp(c->id, ID) == 0) {
879                         snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
880                         if (c->conn_id != NONE)
881                                 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
882                         else
883                                 Log(LOG_ERR, "%s (via network)!", str);
884                         Conn_Close(Client->conn_id, str, str, true);
885                         return false;
886                 }
887                 c = (CLIENT *)c->next;
888         }
889
890         return true;
891 } /* Client_CheckID */
892
893
894 GLOBAL CLIENT *
895 Client_First( void )
896 {
897         return My_Clients;
898 } /* Client_First */
899
900
901 GLOBAL CLIENT *
902 Client_Next( CLIENT *c )
903 {
904         assert( c != NULL );
905         return (CLIENT *)c->next;
906 } /* Client_Next */
907
908
909 GLOBAL long
910 Client_UserCount( void )
911 {
912         return Count( CLIENT_USER );
913 } /* Client_UserCount */
914
915
916 GLOBAL long
917 Client_ServiceCount( void )
918 {
919         return Count( CLIENT_SERVICE );;
920 } /* Client_ServiceCount */
921
922
923 GLOBAL long
924 Client_ServerCount( void )
925 {
926         return Count( CLIENT_SERVER );
927 } /* Client_ServerCount */
928
929
930 GLOBAL long
931 Client_MyUserCount( void )
932 {
933         return MyCount( CLIENT_USER );
934 } /* Client_MyUserCount */
935
936
937 GLOBAL long
938 Client_MyServiceCount( void )
939 {
940         return MyCount( CLIENT_SERVICE );
941 } /* Client_MyServiceCount */
942
943
944 GLOBAL unsigned long
945 Client_MyServerCount( void )
946 {
947         CLIENT *c;
948         unsigned long cnt = 0;
949
950         c = My_Clients;
951         while( c )
952         {
953                 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
954                 c = (CLIENT *)c->next;
955         }
956         return cnt;
957 } /* Client_MyServerCount */
958
959
960 GLOBAL unsigned long
961 Client_OperCount( void )
962 {
963         CLIENT *c;
964         unsigned long cnt = 0;
965
966         c = My_Clients;
967         while( c )
968         {
969                 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
970                 c = (CLIENT *)c->next;
971         }
972         return cnt;
973 } /* Client_OperCount */
974
975
976 GLOBAL unsigned long
977 Client_UnknownCount( void )
978 {
979         CLIENT *c;
980         unsigned long cnt = 0;
981
982         c = My_Clients;
983         while( c )
984         {
985                 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
986                 c = (CLIENT *)c->next;
987         }
988
989         return cnt;
990 } /* Client_UnknownCount */
991
992
993 GLOBAL long
994 Client_MaxUserCount( void )
995 {
996         return Max_Users;
997 } /* Client_MaxUserCount */
998
999
1000 GLOBAL long
1001 Client_MyMaxUserCount( void )
1002 {
1003         return My_Max_Users;
1004 } /* Client_MyMaxUserCount */
1005
1006
1007 GLOBAL bool
1008 Client_IsValidNick( const char *Nick )
1009 {
1010         const char *ptr;
1011         static const char goodchars[] = ";0123456789-";
1012
1013         assert( Nick != NULL );
1014
1015         if( Nick[0] == '#' ) return false;
1016         if( strchr( goodchars, Nick[0] )) return false;
1017         if( strlen( Nick ) >= Conf_MaxNickLength) return false;
1018
1019         ptr = Nick;
1020         while( *ptr )
1021         {
1022                 if (( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return false;
1023                 if ( *ptr > '}' ) return false;
1024                 ptr++;
1025         }
1026
1027         return true;
1028 } /* Client_IsValidNick */
1029
1030
1031 /**
1032  * Return pointer to "My_Whowas" structure.
1033  */
1034 GLOBAL WHOWAS *
1035 Client_GetWhowas( void )
1036 {
1037         return My_Whowas;
1038 } /* Client_GetWhowas */
1039
1040 /**
1041  * Return the index of the last used WHOWAS entry.
1042  */
1043 GLOBAL int
1044 Client_GetLastWhowasIndex( void )
1045 {
1046         return Last_Whowas;
1047 } /* Client_GetLastWhowasIndex */
1048
1049
1050 /**
1051  * Get the start time of this client.
1052  * The result is the start time in seconds since 1970-01-01, as reported
1053  * by the C function time(NULL).
1054  */
1055 GLOBAL time_t
1056 Client_StartTime(CLIENT *Client)
1057 {
1058         assert( Client != NULL );
1059         return Client->starttime;
1060 } /* Client_Uptime */
1061
1062
1063 static unsigned long
1064 Count( CLIENT_TYPE Type )
1065 {
1066         CLIENT *c;
1067         unsigned long cnt = 0;
1068
1069         c = My_Clients;
1070         while( c )
1071         {
1072                 if( c->type == Type ) cnt++;
1073                 c = (CLIENT *)c->next;
1074         }
1075         return cnt;
1076 } /* Count */
1077
1078
1079 static unsigned long
1080 MyCount( CLIENT_TYPE Type )
1081 {
1082         CLIENT *c;
1083         unsigned long cnt = 0;
1084
1085         c = My_Clients;
1086         while( c )
1087         {
1088                 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1089                 c = (CLIENT *)c->next;
1090         }
1091         return cnt;
1092 } /* MyCount */
1093
1094
1095 static CLIENT *
1096 New_Client_Struct( void )
1097 {
1098         /* Neue CLIENT-Struktur pre-initialisieren */
1099
1100         CLIENT *c;
1101
1102         c = (CLIENT *)malloc( sizeof( CLIENT ));
1103         if( ! c )
1104         {
1105                 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1106                 return NULL;
1107         }
1108
1109         memset( c, 0, sizeof ( CLIENT ));
1110
1111         c->type = CLIENT_UNKNOWN;
1112         c->conn_id = NONE;
1113         c->oper_by_me = false;
1114         c->hops = -1;
1115         c->token = -1;
1116         c->mytoken = -1;
1117
1118         return c;
1119 } /* New_Client */
1120
1121
1122 static void
1123 Generate_MyToken( CLIENT *Client )
1124 {
1125         CLIENT *c;
1126         int token;
1127
1128         c = My_Clients;
1129         token = 2;
1130         while( c )
1131         {
1132                 if( c->mytoken == token )
1133                 {
1134                         /* Das Token wurde bereits vergeben */
1135                         token++;
1136                         c = My_Clients;
1137                         continue;
1138                 }
1139                 else c = (CLIENT *)c->next;
1140         }
1141         Client->mytoken = token;
1142         LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1143 } /* Generate_MyToken */
1144
1145
1146 static void
1147 Adjust_Counters( CLIENT *Client )
1148 {
1149         long count;
1150
1151         assert( Client != NULL );
1152
1153         if( Client->type != CLIENT_USER ) return;
1154
1155         if( Client->conn_id != NONE )
1156         {
1157                 /* Local connection */
1158                 count = Client_MyUserCount( );
1159                 if( count > My_Max_Users ) My_Max_Users = count;
1160         }
1161         count = Client_UserCount( );
1162         if( count > Max_Users ) Max_Users = count;
1163 } /* Adjust_Counters */
1164
1165
1166 /**
1167  * Register client in My_Whowas structure for further recall by WHOWAS.
1168  * Note: Only clients that have been connected at least 30 seconds will be
1169  * registered to prevent automated IRC bots to "destroy" a nice server
1170  * history database.
1171  */
1172 GLOBAL void
1173 Client_RegisterWhowas( CLIENT *Client )
1174 {
1175         int slot;
1176         time_t now;
1177
1178         assert( Client != NULL );
1179
1180         now = time(NULL);
1181         /* Don't register clients that were connected less than 30 seconds. */
1182         if( now - Client->starttime < 30 )
1183                 return;
1184
1185         slot = Last_Whowas + 1;
1186         if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1187
1188 #ifdef DEBUG
1189         Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1190 #endif
1191
1192         My_Whowas[slot].time = now;
1193         strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1194                  sizeof( My_Whowas[slot].id ));
1195         strlcpy( My_Whowas[slot].user, Client_User( Client ),
1196                  sizeof( My_Whowas[slot].user ));
1197         strlcpy( My_Whowas[slot].host, Client_HostnameCloaked( Client ),
1198                  sizeof( My_Whowas[slot].host ));
1199         strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1200                  sizeof( My_Whowas[slot].info ));
1201         strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1202                  sizeof( My_Whowas[slot].server ));
1203
1204         Last_Whowas = slot;
1205 } /* Client_RegisterWhowas */
1206
1207
1208 GLOBAL const char *
1209 Client_TypeText(CLIENT *Client)
1210 {
1211         assert(Client != NULL);
1212         switch (Client_Type(Client)) {
1213                 case CLIENT_USER:
1214                         return "User";
1215                         break;
1216                 case CLIENT_SERVICE:
1217                         return "Service";
1218                         break;
1219                 case CLIENT_SERVER:
1220                         return "Server";
1221                         break;
1222                 default:
1223                         return "Client";
1224         }
1225 } /* Client_TypeText */
1226
1227
1228 /**
1229  * Destroy user or service client.
1230  */
1231 static void
1232 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1233 {
1234         if(Client->conn_id != NONE) {
1235                 /* Local (directly connected) client */
1236                 Log(LOG_NOTICE,
1237                     "%s \"%s\" unregistered (connection %d): %s",
1238                     Client_TypeText(Client), Client_Mask(Client),
1239                     Client->conn_id, Txt);
1240                 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1241                                  Client_ID(Client), Client_User(Client),
1242                                  Client_Hostname(Client), Txt);
1243
1244                 if (SendQuit) {
1245                         /* Inforam all the other servers */
1246                         if (FwdMsg)
1247                                 IRC_WriteStrServersPrefix(NULL,
1248                                                 Client, "QUIT :%s", FwdMsg );
1249                         else
1250                                 IRC_WriteStrServersPrefix(NULL,
1251                                                 Client, "QUIT :");
1252                 }
1253         } else {
1254                 /* Remote client */
1255                 LogDebug("%s \"%s\" unregistered: %s",
1256                          Client_TypeText(Client), Client_Mask(Client), Txt);
1257
1258                 if(SendQuit) {
1259                         /* Inform all the other servers, but the ones in the
1260                          * direction we got the QUIT from */
1261                         if(FwdMsg)
1262                                 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1263                                                 Client, "QUIT :%s", FwdMsg );
1264                         else
1265                                 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1266                                                 Client, "QUIT :" );
1267                 }
1268         }
1269
1270         /* Unregister client from channels */
1271         Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1272
1273         /* Register client in My_Whowas structure */
1274         Client_RegisterWhowas(Client);
1275 } /* Destroy_UserOrService */
1276
1277
1278 #ifdef DEBUG
1279
1280 GLOBAL void
1281 Client_DebugDump(void)
1282 {
1283         CLIENT *c;
1284
1285         Log(LOG_DEBUG, "Client status:");
1286         c = My_Clients;
1287         while (c) {
1288                 Log(LOG_DEBUG,
1289                     " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1290                    Client_ID(c), Client_Type(c), Client_Hostname(c),
1291                    Client_User(c), Client_Conn(c), Client_StartTime(c),
1292                    Client_Flags(c));
1293                 c = (CLIENT *)c->next;
1294         }
1295 } /* Client_DumpClients */
1296
1297 #endif
1298
1299
1300 /* -eof- */