2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
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.
40 #include "irc-write.h"
47 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
50 static CLIENT *This_Server, *My_Clients;
52 static WHOWAS My_Whowas[MAX_WHOWAS];
53 static int Last_Whowas = -1;
54 static long Max_Users, My_Max_Users;
57 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
58 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
60 static CLIENT *New_Client_Struct PARAMS(( void ));
61 static void Generate_MyToken PARAMS(( CLIENT *Client ));
62 static void Adjust_Counters PARAMS(( CLIENT *Client ));
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,
70 static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
79 This_Server = New_Client_Struct( );
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 );
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;
95 gethostname( This_Server->host, CLIENT_HOST_LEN );
97 h = gethostbyname( This_Server->host );
98 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
100 Client_SetID( This_Server, Conf_ServerName );
101 Client_SetInfo( This_Server, Conf_ServerInfo );
103 My_Clients = This_Server;
105 memset( &My_Whowas, 0, sizeof( My_Whowas ));
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 );
123 next = (CLIENT *)c->next;
127 if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
132 Client_ThisServer( void )
135 } /* Client_ThisServer */
139 * Initialize new local client; wrapper function for Init_New_Client().
140 * @return New CLIENT structure.
143 Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
145 return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
146 Hostname, NULL, 0, 0, NULL, Idented);
147 } /* Client_NewLocal */
151 * Initialize new remote server; wrapper function for Init_New_Client().
152 * @return New CLIENT structure.
155 Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
156 int Hops, int Token, const char *Info, bool Idented)
158 return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
159 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
160 } /* Client_NewRemoteServer */
164 * Initialize new remote client; wrapper function for Init_New_Client().
165 * @return New CLIENT structure.
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)
171 return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
172 User, Hostname, Info, Hops, Token, Modes, Idented);
173 } /* Client_NewRemoteUser */
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.
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)
189 assert(Introducer != NULL);
190 assert(Hostname != NULL);
192 client = New_Client_Struct();
196 client->starttime = time(NULL);
197 client->conn_id = Idx;
198 client->introducer = Introducer;
199 client->topserver = TopServer;
202 Client_SetID(client, ID);
204 Client_SetUser(client, User, Idented);
206 Client_SetHostname(client, Hostname);
208 Client_SetInfo(client, Info);
210 client->token = Token;
212 Client_SetModes(client, Modes);
213 if (Type == CLIENT_SERVER)
214 Generate_MyToken(client);
216 if (strchr(client->modes, 'a'))
217 strlcpy(client->away, DEFAULT_AWAY_MSG, sizeof(client->away));
219 client->next = (POINTER *)My_Clients;
222 Adjust_Counters(client);
225 } /* Init_New_Client */
229 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
231 /* remove a client */
237 assert( Client != NULL );
239 if( LogMsg ) txt = LogMsg;
241 if( ! txt ) txt = "Reason unknown.";
243 /* netsplit message */
244 if( Client->type == CLIENT_SERVER ) {
245 strlcpy(msg, This_Server->id, sizeof (msg));
246 strlcat(msg, " ", sizeof (msg));
247 strlcat(msg, Client->id, sizeof (msg));
254 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
257 * The client that is about to be removed is a server,
258 * the client we are checking right now is a child of that
259 * server and thus has to be removed, too.
261 * Call Client_Destroy() recursively with the server as the
262 * new "object to be removed". This starts the cycle again, until
263 * all servers that are linked via the original server have been
266 Client_Destroy( c, NULL, msg, false );
273 /* found the client: remove it */
274 if( last ) last->next = c->next;
275 else My_Clients = (CLIENT *)c->next;
277 if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
278 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
279 else if( c->type == CLIENT_SERVER )
281 if( c != This_Server )
283 if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
284 else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
287 /* inform other servers */
288 if( ! NGIRCd_SignalQuit )
290 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
291 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
296 if( c->conn_id != NONE )
298 if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
299 else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
301 Log(LOG_WARNING, "Unregistered unknown client \"%s\": %s",
302 c->id[0] ? c->id : "(No Nick)", txt );
310 c = (CLIENT *)c->next;
312 } /* Client_Destroy */
316 Client_SetHostname( CLIENT *Client, const char *Hostname )
318 assert( Client != NULL );
319 assert( Hostname != NULL );
321 strlcpy( Client->host, Hostname, sizeof( Client->host ));
322 } /* Client_SetHostname */
326 Client_SetID( CLIENT *Client, const char *ID )
328 assert( Client != NULL );
329 assert( ID != NULL );
331 strlcpy( Client->id, ID, sizeof( Client->id ));
334 Client->hash = Hash( Client->id );
339 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
341 /* set clients username */
343 assert( Client != NULL );
344 assert( User != NULL );
347 strlcpy(Client->user, User, sizeof(Client->user));
349 Client->user[0] = '~';
350 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
352 } /* Client_SetUser */
356 Client_SetInfo( CLIENT *Client, const char *Info )
358 /* set client hostname */
360 assert( Client != NULL );
361 assert( Info != NULL );
363 strlcpy(Client->info, Info, sizeof(Client->info));
364 } /* Client_SetInfo */
368 Client_SetModes( CLIENT *Client, const char *Modes )
370 assert( Client != NULL );
371 assert( Modes != NULL );
373 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
374 } /* Client_SetModes */
378 Client_SetFlags( CLIENT *Client, const char *Flags )
380 assert( Client != NULL );
381 assert( Flags != NULL );
383 strlcpy(Client->flags, Flags, sizeof(Client->flags));
384 } /* Client_SetFlags */
388 Client_SetPassword( CLIENT *Client, const char *Pwd )
390 /* set password sent by client */
392 assert( Client != NULL );
393 assert( Pwd != NULL );
395 strlcpy(Client->pwd, Pwd, sizeof(Client->pwd));
396 } /* Client_SetPassword */
400 Client_SetAway( CLIENT *Client, const char *Txt )
402 /* Set AWAY reason of client */
404 assert( Client != NULL );
405 assert( Txt != NULL );
407 strlcpy( Client->away, Txt, sizeof( Client->away ));
408 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
409 Client_Mask(Client), Txt);
410 } /* Client_SetAway */
414 Client_SetType( CLIENT *Client, int Type )
416 assert( Client != NULL );
418 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
419 Adjust_Counters( Client );
420 } /* Client_SetType */
424 Client_SetHops( CLIENT *Client, int Hops )
426 assert( Client != NULL );
428 } /* Client_SetHops */
432 Client_SetToken( CLIENT *Client, int Token )
434 assert( Client != NULL );
435 Client->token = Token;
436 } /* Client_SetToken */
440 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
442 assert( Client != NULL );
443 assert( Introducer != NULL );
444 Client->introducer = Introducer;
445 } /* Client_SetIntroducer */
449 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
451 assert( Client != NULL );
452 Client->oper_by_me = OperByMe;
453 } /* Client_SetOperByMe */
457 Client_ModeAdd( CLIENT *Client, char Mode )
460 * If Client already alread had Mode, return false.
461 * If the Mode was newly set, return true.
466 assert( Client != NULL );
468 x[0] = Mode; x[1] = '\0';
469 if (!strchr( Client->modes, x[0])) {
470 strlcat( Client->modes, x, sizeof( Client->modes ));
474 } /* Client_ModeAdd */
478 Client_ModeDel( CLIENT *Client, char Mode )
481 * If Mode was removed, return true.
482 * If Client did not have Mode, return false.
487 assert( Client != NULL );
489 x[0] = Mode; x[1] = '\0';
491 p = strchr( Client->modes, x[0] );
492 if( ! p ) return false;
494 /* Client has Mode -> delete */
501 } /* Client_ModeDel */
505 Client_Search( const char *Nick )
507 /* return Client-Structure that has the corresponding Nick.
508 * If none is found, return NULL.
511 char search_id[CLIENT_ID_LEN], *ptr;
515 assert( Nick != NULL );
517 /* copy Nick and truncate hostmask if necessary */
518 strlcpy( search_id, Nick, sizeof( search_id ));
519 ptr = strchr( search_id, '!' );
520 if( ptr ) *ptr = '\0';
522 search_hash = Hash(search_id);
526 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
528 c = (CLIENT *)c->next;
531 } /* Client_Search */
535 Client_GetFromToken( CLIENT *Client, int Token )
537 /* Client-Struktur, die den entsprechenden Introducer (=Client)
538 * und das gegebene Token hat, liefern. Wird keine gefunden,
539 * so wird NULL geliefert. */
543 assert( Client != NULL );
548 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
551 c = (CLIENT *)c->next;
554 } /* Client_GetFromToken */
558 Client_Type( CLIENT *Client )
560 assert( Client != NULL );
566 Client_Conn( CLIENT *Client )
568 assert( Client != NULL );
569 return Client->conn_id;
574 Client_ID( CLIENT *Client )
576 assert( Client != NULL );
579 if(Client->type == CLIENT_USER)
580 assert(strlen(Client->id) < Conf_MaxNickLength);
583 if( Client->id[0] ) return Client->id;
589 Client_Info( CLIENT *Client )
591 assert( Client != NULL );
597 Client_User( CLIENT *Client )
599 assert( Client != NULL );
600 return Client->user[0] ? Client->user : "~";
605 Client_Hostname( CLIENT *Client )
607 assert( Client != NULL );
609 } /* Client_Hostname */
613 Client_Password( CLIENT *Client )
615 assert( Client != NULL );
617 } /* Client_Password */
621 Client_Modes( CLIENT *Client )
623 assert( Client != NULL );
624 return Client->modes;
629 Client_Flags( CLIENT *Client )
631 assert( Client != NULL );
632 return Client->flags;
637 Client_OperByMe( CLIENT *Client )
639 assert( Client != NULL );
640 return Client->oper_by_me;
641 } /* Client_OperByMe */
645 Client_Hops( CLIENT *Client )
647 assert( Client != NULL );
653 Client_Token( CLIENT *Client )
655 assert( Client != NULL );
656 return Client->token;
661 Client_MyToken( CLIENT *Client )
663 assert( Client != NULL );
664 return Client->mytoken;
665 } /* Client_MyToken */
669 Client_NextHop( CLIENT *Client )
673 assert( Client != NULL );
676 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
680 } /* Client_NextHop */
684 * return Client-ID ("client!user@host"), this ID is needed for e.g.
685 * prefixes. Returnes pointer to static buffer.
688 Client_Mask( CLIENT *Client )
690 static char GetID_Buffer[GETID_LEN];
692 assert( Client != NULL );
694 if( Client->type == CLIENT_SERVER ) return Client->id;
696 snprintf(GetID_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user, Client->host);
702 Client_Introducer( CLIENT *Client )
704 assert( Client != NULL );
705 return Client->introducer;
706 } /* Client_Introducer */
710 Client_TopServer( CLIENT *Client )
712 assert( Client != NULL );
713 return Client->topserver;
714 } /* Client_TopServer */
718 Client_HasMode( CLIENT *Client, char Mode )
720 assert( Client != NULL );
721 return strchr( Client->modes, Mode ) != NULL;
722 } /* Client_HasMode */
726 Client_Away( CLIENT *Client )
728 assert( Client != NULL );
734 Client_CheckNick( CLIENT *Client, char *Nick )
736 assert( Client != NULL );
737 assert( Nick != NULL );
739 if (! Client_IsValidNick( Nick ))
741 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
745 /* Nick bereits vergeben? */
746 if( Client_Search( Nick ))
748 /* den Nick gibt es bereits */
749 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
754 } /* Client_CheckNick */
758 Client_CheckID( CLIENT *Client, char *ID )
760 char str[COMMAND_LEN];
763 assert( Client != NULL );
764 assert( Client->conn_id > NONE );
765 assert( ID != NULL );
768 if (strlen(ID) > CLIENT_ID_LEN) {
769 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
773 /* ID already in use? */
776 if (strcasecmp(c->id, ID) == 0) {
777 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
778 if (c->conn_id != NONE)
779 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
781 Log(LOG_ERR, "%s (via network)!", str);
782 Conn_Close(Client->conn_id, str, str, true);
785 c = (CLIENT *)c->next;
789 } /* Client_CheckID */
800 Client_Next( CLIENT *c )
803 return (CLIENT *)c->next;
808 Client_UserCount( void )
810 return Count( CLIENT_USER );
811 } /* Client_UserCount */
815 Client_ServiceCount( void )
817 return Count( CLIENT_SERVICE );;
818 } /* Client_ServiceCount */
822 Client_ServerCount( void )
824 return Count( CLIENT_SERVER );
825 } /* Client_ServerCount */
829 Client_MyUserCount( void )
831 return MyCount( CLIENT_USER );
832 } /* Client_MyUserCount */
836 Client_MyServiceCount( void )
838 return MyCount( CLIENT_SERVICE );
839 } /* Client_MyServiceCount */
843 Client_MyServerCount( void )
846 unsigned long cnt = 0;
851 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
852 c = (CLIENT *)c->next;
855 } /* Client_MyServerCount */
859 Client_OperCount( void )
862 unsigned long cnt = 0;
867 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
868 c = (CLIENT *)c->next;
871 } /* Client_OperCount */
875 Client_UnknownCount( void )
878 unsigned long cnt = 0;
883 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
884 c = (CLIENT *)c->next;
888 } /* Client_UnknownCount */
892 Client_MaxUserCount( void )
895 } /* Client_MaxUserCount */
899 Client_MyMaxUserCount( void )
902 } /* Client_MyMaxUserCount */
906 Client_IsValidNick( const char *Nick )
909 static const char goodchars[] = ";0123456789-";
911 assert( Nick != NULL );
913 if( Nick[0] == '#' ) return false;
914 if( strchr( goodchars, Nick[0] )) return false;
915 if( strlen( Nick ) >= Conf_MaxNickLength) return false;
920 if (( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return false;
921 if ( *ptr > '}' ) return false;
926 } /* Client_IsValidNick */
930 * Return pointer to "My_Whowas" structure.
933 Client_GetWhowas( void )
936 } /* Client_GetWhowas */
939 * Return the index of the last used WHOWAS entry.
942 Client_GetLastWhowasIndex( void )
945 } /* Client_GetLastWhowasIndex */
949 * Get the start time of this client.
950 * The result is the start time in seconds since 1970-01-01, as reported
951 * by the C function time(NULL).
954 Client_StartTime(CLIENT *Client)
956 assert( Client != NULL );
957 return Client->starttime;
958 } /* Client_Uptime */
962 Count( CLIENT_TYPE Type )
965 unsigned long cnt = 0;
970 if( c->type == Type ) cnt++;
971 c = (CLIENT *)c->next;
978 MyCount( CLIENT_TYPE Type )
981 unsigned long cnt = 0;
986 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
987 c = (CLIENT *)c->next;
994 New_Client_Struct( void )
996 /* Neue CLIENT-Struktur pre-initialisieren */
1000 c = (CLIENT *)malloc( sizeof( CLIENT ));
1003 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1007 memset( c, 0, sizeof ( CLIENT ));
1009 c->type = CLIENT_UNKNOWN;
1011 c->oper_by_me = false;
1021 Generate_MyToken( CLIENT *Client )
1030 if( c->mytoken == token )
1032 /* Das Token wurde bereits vergeben */
1037 else c = (CLIENT *)c->next;
1039 Client->mytoken = token;
1040 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1041 } /* Generate_MyToken */
1045 Adjust_Counters( CLIENT *Client )
1049 assert( Client != NULL );
1051 if( Client->type != CLIENT_USER ) return;
1053 if( Client->conn_id != NONE )
1055 /* Local connection */
1056 count = Client_MyUserCount( );
1057 if( count > My_Max_Users ) My_Max_Users = count;
1059 count = Client_UserCount( );
1060 if( count > Max_Users ) Max_Users = count;
1061 } /* Adjust_Counters */
1065 * Register client in My_Whowas structure for further recall by WHOWAS.
1066 * Note: Only clients that have been connected at least 30 seconds will be
1067 * registered to prevent automated IRC bots to "destroy" a nice server
1071 Client_RegisterWhowas( CLIENT *Client )
1076 assert( Client != NULL );
1079 /* Don't register clients that were connected less than 30 seconds. */
1080 if( now - Client->starttime < 30 )
1083 slot = Last_Whowas + 1;
1084 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1087 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1090 My_Whowas[slot].time = now;
1091 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1092 sizeof( My_Whowas[slot].id ));
1093 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1094 sizeof( My_Whowas[slot].user ));
1095 strlcpy( My_Whowas[slot].host, Client_Hostname( Client ),
1096 sizeof( My_Whowas[slot].host ));
1097 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1098 sizeof( My_Whowas[slot].info ));
1099 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1100 sizeof( My_Whowas[slot].server ));
1103 } /* Client_RegisterWhowas */
1107 Client_TypeText(CLIENT *Client)
1109 assert(Client != NULL);
1110 switch (Client_Type(Client)) {
1114 case CLIENT_SERVICE:
1123 } /* Client_TypeText */
1127 * Destroy user or service client.
1130 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1132 if(Client->conn_id != NONE) {
1133 /* Local (directly connected) client */
1135 "%s \"%s\" unregistered (connection %d): %s",
1136 Client_TypeText(Client), Client_Mask(Client),
1137 Client->conn_id, Txt);
1138 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1139 Client_ID(Client), Client_User(Client),
1140 Client_Hostname(Client), Txt);
1143 /* Inforam all the other servers */
1145 IRC_WriteStrServersPrefix(NULL,
1146 Client, "QUIT :%s", FwdMsg );
1148 IRC_WriteStrServersPrefix(NULL,
1153 LogDebug("%s \"%s\" unregistered: %s",
1154 Client_TypeText(Client), Client_Mask(Client), Txt);
1157 /* Inform all the other servers, but the ones in the
1158 * direction we got the QUIT from */
1160 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1161 Client, "QUIT :%s", FwdMsg );
1163 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1168 /* Unregister client from channels */
1169 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1171 /* Register client in My_Whowas structure */
1172 Client_RegisterWhowas(Client);
1173 } /* Destroy_UserOrService */