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.
41 #include "irc-write.h"
48 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
51 static CLIENT *This_Server, *My_Clients;
53 static WHOWAS My_Whowas[MAX_WHOWAS];
54 static int Last_Whowas = -1;
55 static long Max_Users, My_Max_Users;
58 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
59 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
61 static CLIENT *New_Client_Struct PARAMS(( void ));
62 static void Generate_MyToken PARAMS(( CLIENT *Client ));
63 static void Adjust_Counters PARAMS(( CLIENT *Client ));
65 static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
66 CLIENT *TopServer, int Type, const char *ID,
67 const char *User, const char *Hostname, const char *Info,
68 int Hops, int Token, const char *Modes,
71 static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
80 This_Server = New_Client_Struct( );
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 );
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;
96 gethostname( This_Server->host, CLIENT_HOST_LEN );
98 h = gethostbyname( This_Server->host );
99 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
101 Client_SetID( This_Server, Conf_ServerName );
102 Client_SetInfo( This_Server, Conf_ServerInfo );
104 My_Clients = This_Server;
106 memset( &My_Whowas, 0, sizeof( My_Whowas ));
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 );
124 next = (CLIENT *)c->next;
128 if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
133 Client_ThisServer( void )
136 } /* Client_ThisServer */
140 * Initialize new local client; wrapper function for Init_New_Client().
141 * @return New CLIENT structure.
144 Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
146 return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
147 Hostname, NULL, 0, 0, NULL, Idented);
148 } /* Client_NewLocal */
152 * Initialize new remote server; wrapper function for Init_New_Client().
153 * @return New CLIENT structure.
156 Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
157 int Hops, int Token, const char *Info, bool Idented)
159 return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
160 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
161 } /* Client_NewRemoteServer */
165 * Initialize new remote client; wrapper function for Init_New_Client().
166 * @return New CLIENT structure.
169 Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
170 const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
172 return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
173 User, Hostname, Info, Hops, Token, Modes, Idented);
174 } /* Client_NewRemoteUser */
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.
183 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
184 int Type, const char *ID, const char *User, const char *Hostname,
185 const char *Info, int Hops, int Token, const char *Modes, bool Idented)
190 assert(Introducer != NULL);
191 assert(Hostname != NULL);
193 client = New_Client_Struct();
197 client->starttime = time(NULL);
198 client->conn_id = Idx;
199 client->introducer = Introducer;
200 client->topserver = TopServer;
203 Client_SetID(client, ID);
205 Client_SetUser(client, User, Idented);
207 Client_SetHostname(client, Hostname);
209 Client_SetInfo(client, Info);
211 client->token = Token;
213 Client_SetModes(client, Modes);
214 if (Type == CLIENT_SERVER)
215 Generate_MyToken(client);
217 if (strchr(client->modes, 'a'))
218 strlcpy(client->away, DEFAULT_AWAY_MSG, sizeof(client->away));
220 client->next = (POINTER *)My_Clients;
223 Adjust_Counters(client);
226 } /* Init_New_Client */
230 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
232 /* remove a client */
238 assert( Client != NULL );
240 if( LogMsg ) txt = LogMsg;
242 if( ! txt ) txt = "Reason unknown.";
244 /* netsplit message */
245 if( Client->type == CLIENT_SERVER ) {
246 strlcpy(msg, This_Server->id, sizeof (msg));
247 strlcat(msg, " ", sizeof (msg));
248 strlcat(msg, Client->id, sizeof (msg));
255 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
258 * The client that is about to be removed is a server,
259 * the client we are checking right now is a child of that
260 * server and thus has to be removed, too.
262 * Call Client_Destroy() recursively with the server as the
263 * new "object to be removed". This starts the cycle again, until
264 * all servers that are linked via the original server have been
267 Client_Destroy( c, NULL, msg, false );
274 /* found the client: remove it */
275 if( last ) last->next = c->next;
276 else My_Clients = (CLIENT *)c->next;
278 if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
279 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
280 else if( c->type == CLIENT_SERVER )
282 if( c != This_Server )
284 if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
285 else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
288 /* inform other servers */
289 if( ! NGIRCd_SignalQuit )
291 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
292 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
297 if( c->conn_id != NONE )
299 if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
300 else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
302 Log(LOG_WARNING, "Unregistered unknown client \"%s\": %s",
303 c->id[0] ? c->id : "(No Nick)", txt );
311 c = (CLIENT *)c->next;
313 } /* Client_Destroy */
317 Client_SetHostname( CLIENT *Client, const char *Hostname )
319 assert( Client != NULL );
320 assert( Hostname != NULL );
322 strlcpy( Client->host, Hostname, sizeof( Client->host ));
323 } /* Client_SetHostname */
327 Client_SetID( CLIENT *Client, const char *ID )
329 assert( Client != NULL );
330 assert( ID != NULL );
332 strlcpy( Client->id, ID, sizeof( Client->id ));
335 Client->hash = Hash( Client->id );
340 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
342 /* set clients username */
344 assert( Client != NULL );
345 assert( User != NULL );
348 strlcpy(Client->user, User, sizeof(Client->user));
350 Client->user[0] = '~';
351 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
353 } /* Client_SetUser */
357 Client_SetInfo( CLIENT *Client, const char *Info )
359 /* set client hostname */
361 assert( Client != NULL );
362 assert( Info != NULL );
364 strlcpy(Client->info, Info, sizeof(Client->info));
365 } /* Client_SetInfo */
369 Client_SetModes( CLIENT *Client, const char *Modes )
371 assert( Client != NULL );
372 assert( Modes != NULL );
374 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
375 } /* Client_SetModes */
379 Client_SetFlags( CLIENT *Client, const char *Flags )
381 assert( Client != NULL );
382 assert( Flags != NULL );
384 strlcpy(Client->flags, Flags, sizeof(Client->flags));
385 } /* Client_SetFlags */
389 Client_SetPassword( CLIENT *Client, const char *Pwd )
391 /* set password sent by client */
393 assert( Client != NULL );
394 assert( Pwd != NULL );
396 strlcpy(Client->pwd, Pwd, sizeof(Client->pwd));
397 } /* Client_SetPassword */
401 Client_SetAway( CLIENT *Client, const char *Txt )
403 /* Set AWAY reason of client */
405 assert( Client != NULL );
406 assert( Txt != NULL );
408 strlcpy( Client->away, Txt, sizeof( Client->away ));
409 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
410 Client_Mask(Client), Txt);
411 } /* Client_SetAway */
415 Client_SetType( CLIENT *Client, int Type )
417 assert( Client != NULL );
419 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
420 Adjust_Counters( Client );
421 } /* Client_SetType */
425 Client_SetHops( CLIENT *Client, int Hops )
427 assert( Client != NULL );
429 } /* Client_SetHops */
433 Client_SetToken( CLIENT *Client, int Token )
435 assert( Client != NULL );
436 Client->token = Token;
437 } /* Client_SetToken */
441 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
443 assert( Client != NULL );
444 assert( Introducer != NULL );
445 Client->introducer = Introducer;
446 } /* Client_SetIntroducer */
450 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
452 assert( Client != NULL );
453 Client->oper_by_me = OperByMe;
454 } /* Client_SetOperByMe */
458 Client_ModeAdd( CLIENT *Client, char Mode )
461 * If Client already alread had Mode, return false.
462 * If the Mode was newly set, return true.
467 assert( Client != NULL );
469 x[0] = Mode; x[1] = '\0';
470 if (!strchr( Client->modes, x[0])) {
471 strlcat( Client->modes, x, sizeof( Client->modes ));
475 } /* Client_ModeAdd */
479 Client_ModeDel( CLIENT *Client, char Mode )
482 * If Mode was removed, return true.
483 * If Client did not have Mode, return false.
488 assert( Client != NULL );
490 x[0] = Mode; x[1] = '\0';
492 p = strchr( Client->modes, x[0] );
493 if( ! p ) return false;
495 /* Client has Mode -> delete */
502 } /* Client_ModeDel */
506 Client_Search( const char *Nick )
508 /* return Client-Structure that has the corresponding Nick.
509 * If none is found, return NULL.
512 char search_id[CLIENT_ID_LEN], *ptr;
516 assert( Nick != NULL );
518 /* copy Nick and truncate hostmask if necessary */
519 strlcpy( search_id, Nick, sizeof( search_id ));
520 ptr = strchr( search_id, '!' );
521 if( ptr ) *ptr = '\0';
523 search_hash = Hash(search_id);
527 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
529 c = (CLIENT *)c->next;
532 } /* Client_Search */
536 Client_GetFromToken( CLIENT *Client, int Token )
538 /* Client-Struktur, die den entsprechenden Introducer (=Client)
539 * und das gegebene Token hat, liefern. Wird keine gefunden,
540 * so wird NULL geliefert. */
544 assert( Client != NULL );
549 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
552 c = (CLIENT *)c->next;
555 } /* Client_GetFromToken */
559 Client_Type( CLIENT *Client )
561 assert( Client != NULL );
567 Client_Conn( CLIENT *Client )
569 assert( Client != NULL );
570 return Client->conn_id;
575 Client_ID( CLIENT *Client )
577 assert( Client != NULL );
580 if(Client->type == CLIENT_USER)
581 assert(strlen(Client->id) < Conf_MaxNickLength);
584 if( Client->id[0] ) return Client->id;
590 Client_Info( CLIENT *Client )
592 assert( Client != NULL );
598 Client_User( CLIENT *Client )
600 assert( Client != NULL );
601 return Client->user[0] ? Client->user : "~";
606 Client_Hostname( CLIENT *Client )
608 assert( Client != NULL );
610 } /* Client_Hostname */
614 Client_Password( CLIENT *Client )
616 assert( Client != NULL );
618 } /* Client_Password */
622 Client_Modes( CLIENT *Client )
624 assert( Client != NULL );
625 return Client->modes;
630 Client_Flags( CLIENT *Client )
632 assert( Client != NULL );
633 return Client->flags;
638 Client_OperByMe( CLIENT *Client )
640 assert( Client != NULL );
641 return Client->oper_by_me;
642 } /* Client_OperByMe */
646 Client_Hops( CLIENT *Client )
648 assert( Client != NULL );
654 Client_Token( CLIENT *Client )
656 assert( Client != NULL );
657 return Client->token;
662 Client_MyToken( CLIENT *Client )
664 assert( Client != NULL );
665 return Client->mytoken;
666 } /* Client_MyToken */
670 Client_NextHop( CLIENT *Client )
674 assert( Client != NULL );
677 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
681 } /* Client_NextHop */
685 * return Client-ID ("client!user@host"), this ID is needed for e.g.
686 * prefixes. Returnes pointer to static buffer.
689 Client_Mask( CLIENT *Client )
691 static char GetID_Buffer[GETID_LEN];
693 assert( Client != NULL );
695 if( Client->type == CLIENT_SERVER ) return Client->id;
697 snprintf(GetID_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user, Client->host);
703 Client_Introducer( CLIENT *Client )
705 assert( Client != NULL );
706 return Client->introducer;
707 } /* Client_Introducer */
711 Client_TopServer( CLIENT *Client )
713 assert( Client != NULL );
714 return Client->topserver;
715 } /* Client_TopServer */
719 Client_HasMode( CLIENT *Client, char Mode )
721 assert( Client != NULL );
722 return strchr( Client->modes, Mode ) != NULL;
723 } /* Client_HasMode */
727 Client_Away( CLIENT *Client )
729 assert( Client != NULL );
735 Client_CheckNick( CLIENT *Client, char *Nick )
737 assert( Client != NULL );
738 assert( Nick != NULL );
740 if (! Client_IsValidNick( Nick ))
742 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
746 /* Nick bereits vergeben? */
747 if( Client_Search( Nick ))
749 /* den Nick gibt es bereits */
750 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
755 } /* Client_CheckNick */
759 Client_CheckID( CLIENT *Client, char *ID )
761 char str[COMMAND_LEN];
764 assert( Client != NULL );
765 assert( Client->conn_id > NONE );
766 assert( ID != NULL );
769 if (strlen(ID) > CLIENT_ID_LEN) {
770 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
774 /* ID already in use? */
777 if (strcasecmp(c->id, ID) == 0) {
778 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
779 if (c->conn_id != NONE)
780 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
782 Log(LOG_ERR, "%s (via network)!", str);
783 Conn_Close(Client->conn_id, str, str, true);
786 c = (CLIENT *)c->next;
790 } /* Client_CheckID */
801 Client_Next( CLIENT *c )
804 return (CLIENT *)c->next;
809 Client_UserCount( void )
811 return Count( CLIENT_USER );
812 } /* Client_UserCount */
816 Client_ServiceCount( void )
818 return Count( CLIENT_SERVICE );;
819 } /* Client_ServiceCount */
823 Client_ServerCount( void )
825 return Count( CLIENT_SERVER );
826 } /* Client_ServerCount */
830 Client_MyUserCount( void )
832 return MyCount( CLIENT_USER );
833 } /* Client_MyUserCount */
837 Client_MyServiceCount( void )
839 return MyCount( CLIENT_SERVICE );
840 } /* Client_MyServiceCount */
844 Client_MyServerCount( void )
847 unsigned long cnt = 0;
852 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
853 c = (CLIENT *)c->next;
856 } /* Client_MyServerCount */
860 Client_OperCount( void )
863 unsigned long cnt = 0;
868 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
869 c = (CLIENT *)c->next;
872 } /* Client_OperCount */
876 Client_UnknownCount( void )
879 unsigned long cnt = 0;
884 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
885 c = (CLIENT *)c->next;
889 } /* Client_UnknownCount */
893 Client_MaxUserCount( void )
896 } /* Client_MaxUserCount */
900 Client_MyMaxUserCount( void )
903 } /* Client_MyMaxUserCount */
907 Client_IsValidNick( const char *Nick )
910 static const char goodchars[] = ";0123456789-";
912 assert( Nick != NULL );
914 if( Nick[0] == '#' ) return false;
915 if( strchr( goodchars, Nick[0] )) return false;
916 if( strlen( Nick ) >= Conf_MaxNickLength) return false;
921 if (( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return false;
922 if ( *ptr > '}' ) return false;
927 } /* Client_IsValidNick */
931 * Return pointer to "My_Whowas" structure.
934 Client_GetWhowas( void )
937 } /* Client_GetWhowas */
940 * Return the index of the last used WHOWAS entry.
943 Client_GetLastWhowasIndex( void )
946 } /* Client_GetLastWhowasIndex */
950 * Get the start time of this client.
951 * The result is the start time in seconds since 1970-01-01, as reported
952 * by the C function time(NULL).
955 Client_StartTime(CLIENT *Client)
957 assert( Client != NULL );
958 return Client->starttime;
959 } /* Client_Uptime */
963 Count( CLIENT_TYPE Type )
966 unsigned long cnt = 0;
971 if( c->type == Type ) cnt++;
972 c = (CLIENT *)c->next;
979 MyCount( CLIENT_TYPE Type )
982 unsigned long cnt = 0;
987 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
988 c = (CLIENT *)c->next;
995 New_Client_Struct( void )
997 /* Neue CLIENT-Struktur pre-initialisieren */
1001 c = (CLIENT *)malloc( sizeof( CLIENT ));
1004 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1008 memset( c, 0, sizeof ( CLIENT ));
1010 c->type = CLIENT_UNKNOWN;
1012 c->oper_by_me = false;
1022 Generate_MyToken( CLIENT *Client )
1031 if( c->mytoken == token )
1033 /* Das Token wurde bereits vergeben */
1038 else c = (CLIENT *)c->next;
1040 Client->mytoken = token;
1041 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1042 } /* Generate_MyToken */
1046 Adjust_Counters( CLIENT *Client )
1050 assert( Client != NULL );
1052 if( Client->type != CLIENT_USER ) return;
1054 if( Client->conn_id != NONE )
1056 /* Local connection */
1057 count = Client_MyUserCount( );
1058 if( count > My_Max_Users ) My_Max_Users = count;
1060 count = Client_UserCount( );
1061 if( count > Max_Users ) Max_Users = count;
1062 } /* Adjust_Counters */
1066 * Register client in My_Whowas structure for further recall by WHOWAS.
1067 * Note: Only clients that have been connected at least 30 seconds will be
1068 * registered to prevent automated IRC bots to "destroy" a nice server
1072 Client_RegisterWhowas( CLIENT *Client )
1077 assert( Client != NULL );
1080 /* Don't register clients that were connected less than 30 seconds. */
1081 if( now - Client->starttime < 30 )
1084 slot = Last_Whowas + 1;
1085 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1088 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1091 My_Whowas[slot].time = now;
1092 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1093 sizeof( My_Whowas[slot].id ));
1094 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1095 sizeof( My_Whowas[slot].user ));
1096 strlcpy( My_Whowas[slot].host, Client_Hostname( Client ),
1097 sizeof( My_Whowas[slot].host ));
1098 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1099 sizeof( My_Whowas[slot].info ));
1100 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1101 sizeof( My_Whowas[slot].server ));
1104 } /* Client_RegisterWhowas */
1108 Client_TypeText(CLIENT *Client)
1110 assert(Client != NULL);
1111 switch (Client_Type(Client)) {
1115 case CLIENT_SERVICE:
1124 } /* Client_TypeText */
1128 * Destroy user or service client.
1131 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1133 if(Client->conn_id != NONE) {
1134 /* Local (directly connected) client */
1136 "%s \"%s\" unregistered (connection %d): %s",
1137 Client_TypeText(Client), Client_Mask(Client),
1138 Client->conn_id, Txt);
1139 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1140 Client_ID(Client), Client_User(Client),
1141 Client_Hostname(Client), Txt);
1144 /* Inforam all the other servers */
1146 IRC_WriteStrServersPrefix(NULL,
1147 Client, "QUIT :%s", FwdMsg );
1149 IRC_WriteStrServersPrefix(NULL,
1154 LogDebug("%s \"%s\" unregistered: %s",
1155 Client_TypeText(Client), Client_Mask(Client), Txt);
1158 /* Inform all the other servers, but the ones in the
1159 * direction we got the QUIT from */
1161 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1162 Client, "QUIT :%s", FwdMsg );
1164 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1169 /* Unregister client from channels */
1170 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1172 /* Register client in My_Whowas structure */
1173 Client_RegisterWhowas(Client);
1174 } /* Destroy_UserOrService */