2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
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 "conn-func.h"
42 #include "irc-write.h"
49 #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,
74 static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
82 This_Server = New_Client_Struct( );
85 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
86 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
90 /* Client structure for this server */
91 This_Server->next = NULL;
92 This_Server->type = CLIENT_SERVER;
93 This_Server->conn_id = NONE;
94 This_Server->introducer = This_Server;
95 This_Server->mytoken = 1;
96 This_Server->hops = 0;
98 gethostname( This_Server->host, CLIENT_HOST_LEN );
100 h = gethostbyname( This_Server->host );
101 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
103 Client_SetID( This_Server, Conf_ServerName );
104 Client_SetInfo( This_Server, Conf_ServerInfo );
106 My_Clients = This_Server;
108 memset( &My_Whowas, 0, sizeof( My_Whowas ));
118 if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
119 else Client_Destroy( This_Server, "Server going down.", NULL, false );
126 next = (CLIENT *)c->next;
128 free(c->account_name);
132 if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
137 Client_ThisServer( void )
140 } /* Client_ThisServer */
144 * Initialize new local client; wrapper function for Init_New_Client().
145 * @return New CLIENT structure.
148 Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
150 return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
151 Hostname, NULL, 0, 0, NULL, Idented);
152 } /* Client_NewLocal */
156 * Initialize new remote server; wrapper function for Init_New_Client().
157 * @return New CLIENT structure.
160 Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
161 int Hops, int Token, const char *Info, bool Idented)
163 return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
164 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
165 } /* Client_NewRemoteServer */
169 * Initialize new remote client; wrapper function for Init_New_Client().
170 * @return New CLIENT structure.
173 Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
174 const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
176 return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
177 User, Hostname, Info, Hops, Token, Modes, Idented);
178 } /* Client_NewRemoteUser */
182 * Initialize new client and set up the given parameters like client type,
183 * user name, host name, introducing server etc. ...
184 * @return New CLIENT structure.
187 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
188 int Type, const char *ID, const char *User, const char *Hostname,
189 const char *Info, int Hops, int Token, const char *Modes, bool Idented)
194 assert(Introducer != NULL);
196 client = New_Client_Struct();
200 client->starttime = time(NULL);
201 client->conn_id = Idx;
202 client->introducer = Introducer;
203 client->topserver = TopServer;
206 Client_SetID(client, ID);
208 Client_SetUser(client, User, Idented);
209 Client_SetOrigUser(client, User);
212 Client_SetHostname(client, Hostname);
214 Client_SetInfo(client, Info);
216 client->token = Token;
218 Client_SetModes(client, Modes);
219 if (Type == CLIENT_SERVER)
220 Generate_MyToken(client);
222 if (Client_HasMode(client, 'a'))
223 strlcpy(client->away, DEFAULT_AWAY_MSG, sizeof(client->away));
225 client->next = (POINTER *)My_Clients;
228 Adjust_Counters(client);
231 } /* Init_New_Client */
235 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
237 /* remove a client */
243 assert( Client != NULL );
245 txt = LogMsg ? LogMsg : FwdMsg;
247 txt = "Reason unknown";
249 /* netsplit message */
250 if( Client->type == CLIENT_SERVER ) {
251 strlcpy(msg, This_Server->id, sizeof (msg));
252 strlcat(msg, " ", sizeof (msg));
253 strlcat(msg, Client->id, sizeof (msg));
260 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
263 * The client that is about to be removed is a server,
264 * the client we are checking right now is a child of that
265 * server and thus has to be removed, too.
267 * Call Client_Destroy() recursively with the server as the
268 * new "object to be removed". This starts the cycle again, until
269 * all servers that are linked via the original server have been
272 Client_Destroy( c, NULL, msg, false );
279 /* found the client: remove it */
280 if( last ) last->next = c->next;
281 else My_Clients = (CLIENT *)c->next;
283 if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
284 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
285 else if( c->type == CLIENT_SERVER )
287 if (c != This_Server) {
288 if (c->conn_id != NONE)
289 Log(LOG_NOTICE|LOG_snotice,
290 "Server \"%s\" unregistered (connection %d): %s.",
291 c->id, c->conn_id, txt);
293 Log(LOG_NOTICE|LOG_snotice,
294 "Server \"%s\" unregistered: %s.",
298 /* inform other servers */
299 if( ! NGIRCd_SignalQuit )
301 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
302 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
307 if (c->conn_id != NONE) {
310 "Client \"%s\" unregistered (connection %d): %s.",
311 c->id, c->conn_id, txt);
314 "Client unregistered (connection %d): %s.",
318 "Unregistered unknown client \"%s\": %s",
319 c->id[0] ? c->id : "(No Nick)", txt);
324 free(c->account_name);
331 c = (CLIENT *)c->next;
333 } /* Client_Destroy */
337 * Set client hostname.
339 * If global hostname cloaking is in effect, don't set the real hostname
340 * but the configured one.
342 * @param Client The client of which the hostname should be set.
343 * @param Hostname The new hostname.
346 Client_SetHostname( CLIENT *Client, const char *Hostname )
348 assert(Client != NULL);
349 assert(Hostname != NULL);
351 if (strlen(Conf_CloakHost)) {
352 char cloak[GETID_LEN];
354 strlcpy(cloak, Hostname, GETID_LEN);
355 strlcat(cloak, Conf_CloakHostSalt, GETID_LEN);
356 snprintf(cloak, GETID_LEN, Conf_CloakHost, Hash(cloak));
358 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
359 Client_ID(Client), Client->host, cloak);
360 strlcpy(Client->host, cloak, sizeof(Client->host));
362 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
363 Client_ID(Client), Client->host, Hostname);
364 strlcpy(Client->host, Hostname, sizeof(Client->host));
366 } /* Client_SetHostname */
370 Client_SetID( CLIENT *Client, const char *ID )
372 assert( Client != NULL );
373 assert( ID != NULL );
375 strlcpy( Client->id, ID, sizeof( Client->id ));
377 if (Conf_CloakUserToNick) {
378 strlcpy( Client->user, ID, sizeof( Client->user ));
379 strlcpy( Client->info, ID, sizeof( Client->info ));
383 Client->hash = Hash( Client->id );
388 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
390 /* set clients username */
392 assert( Client != NULL );
393 assert( User != NULL );
395 if (Conf_CloakUserToNick) {
396 strlcpy(Client->user, Client->id, sizeof(Client->user));
397 } else if (Idented) {
398 strlcpy(Client->user, User, sizeof(Client->user));
400 Client->user[0] = '~';
401 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
403 } /* Client_SetUser */
407 * Set "original" user name of a client.
408 * This function saves the "original" user name, the user name specified by
409 * the peer using the USER command, into the CLIENT structure. This user
410 * name may be used for authentication, for example.
411 * @param Client The client.
412 * @param User User name to set.
415 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
417 assert(Client != NULL);
418 assert(User != NULL);
420 #if defined(PAM) && defined(IDENTAUTH)
421 strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
423 } /* Client_SetOrigUser */
427 Client_SetInfo( CLIENT *Client, const char *Info )
429 /* set client hostname */
431 assert( Client != NULL );
432 assert( Info != NULL );
434 if (Conf_CloakUserToNick)
435 strlcpy(Client->info, Client->id, sizeof(Client->info));
437 strlcpy(Client->info, Info, sizeof(Client->info));
438 } /* Client_SetInfo */
442 Client_SetModes( CLIENT *Client, const char *Modes )
444 assert( Client != NULL );
445 assert( Modes != NULL );
447 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
448 } /* Client_SetModes */
452 Client_SetFlags( CLIENT *Client, const char *Flags )
454 assert( Client != NULL );
455 assert( Flags != NULL );
457 strlcpy(Client->flags, Flags, sizeof(Client->flags));
458 } /* Client_SetFlags */
462 Client_SetAccountName(CLIENT *Client, const char *AccountName)
464 assert(Client != NULL);
466 if (Client->account_name)
467 free(Client->account_name);
470 Client->account_name = strdup(AccountName);
472 Client->account_name = NULL;
477 Client_SetAway( CLIENT *Client, const char *Txt )
479 /* Set AWAY reason of client */
481 assert( Client != NULL );
482 assert( Txt != NULL );
484 strlcpy( Client->away, Txt, sizeof( Client->away ));
485 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
486 Client_Mask(Client), Txt);
487 } /* Client_SetAway */
491 Client_SetType( CLIENT *Client, int Type )
493 assert( Client != NULL );
495 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
496 Adjust_Counters( Client );
497 } /* Client_SetType */
501 Client_SetHops( CLIENT *Client, int Hops )
503 assert( Client != NULL );
505 } /* Client_SetHops */
509 Client_SetToken( CLIENT *Client, int Token )
511 assert( Client != NULL );
512 Client->token = Token;
513 } /* Client_SetToken */
517 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
519 assert( Client != NULL );
520 assert( Introducer != NULL );
521 Client->introducer = Introducer;
522 } /* Client_SetIntroducer */
526 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
528 assert( Client != NULL );
529 Client->oper_by_me = OperByMe;
530 } /* Client_SetOperByMe */
534 Client_ModeAdd( CLIENT *Client, char Mode )
537 * If Client already had Mode, return false.
538 * If the Mode was newly set, return true.
543 assert( Client != NULL );
545 x[0] = Mode; x[1] = '\0';
546 if (!Client_HasMode(Client, x[0])) {
547 strlcat( Client->modes, x, sizeof( Client->modes ));
551 } /* Client_ModeAdd */
555 Client_ModeDel( CLIENT *Client, char Mode )
558 * If Mode was removed, return true.
559 * If Client did not have Mode, return false.
564 assert( Client != NULL );
566 x[0] = Mode; x[1] = '\0';
568 p = strchr( Client->modes, x[0] );
569 if( ! p ) return false;
571 /* Client has Mode -> delete */
578 } /* Client_ModeDel */
582 * Search CLIENT structure of a given nick name.
584 * @return Pointer to CLIENT structure or NULL if not found.
587 Client_Search( const char *Nick )
589 char search_id[CLIENT_ID_LEN], *ptr;
593 assert( Nick != NULL );
595 /* copy Nick and truncate hostmask if necessary */
596 strlcpy( search_id, Nick, sizeof( search_id ));
597 ptr = strchr( search_id, '!' );
598 if( ptr ) *ptr = '\0';
600 search_hash = Hash(search_id);
604 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
606 c = (CLIENT *)c->next;
613 * Search first CLIENT structure matching a given mask of a server.
615 * The order of servers is arbitrary, but this function makes sure that the
616 * local server is always returned if the mask matches it.
618 * @return Pointer to CLIENT structure or NULL if no server could be found.
621 Client_SearchServer(const char *Mask)
625 assert(Mask != NULL);
627 /* First check if mask matches the local server */
628 if (MatchCaseInsensitive(Mask, Client_ID(Client_ThisServer())))
629 return Client_ThisServer();
633 if (Client_Type(c) == CLIENT_SERVER) {
634 /* This is a server: check if Mask matches */
635 if (MatchCaseInsensitive(Mask, c->id))
638 c = (CLIENT *)c->next;
645 * Get client structure ("introducer") identfied by a server token.
646 * @return CLIENT structure or NULL if none could be found.
649 Client_GetFromToken( CLIENT *Client, int Token )
653 assert( Client != NULL );
660 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
663 c = (CLIENT *)c->next;
666 } /* Client_GetFromToken */
670 Client_Type( CLIENT *Client )
672 assert( Client != NULL );
678 Client_Conn( CLIENT *Client )
680 assert( Client != NULL );
681 return Client->conn_id;
686 Client_ID( CLIENT *Client )
688 assert( Client != NULL );
691 if(Client->type == CLIENT_USER)
692 assert(strlen(Client->id) < Conf_MaxNickLength);
695 if( Client->id[0] ) return Client->id;
701 Client_Info( CLIENT *Client )
703 assert( Client != NULL );
709 Client_User( CLIENT *Client )
711 assert( Client != NULL );
712 return Client->user[0] ? Client->user : "~";
719 * Get the "original" user name as supplied by the USER command.
720 * The user name as given by the client is used for authentication instead
721 * of the one detected using IDENT requests.
722 * @param Client The client.
723 * @return Original user name.
726 Client_OrigUser(CLIENT *Client) {
728 char *user = Client->user;
734 return Client->orig_user;
736 } /* Client_OrigUser */
741 * Return the hostname of a client.
742 * @param Client Pointer to client structure
743 * @return Pointer to client hostname
746 Client_Hostname(CLIENT *Client)
748 assert (Client != NULL);
753 * Return the cloaked hostname of a client, if set.
754 * @param Client Pointer to the client structure.
755 * @return Pointer to the cloaked hostname or NULL if not set.
758 Client_HostnameCloaked(CLIENT *Client)
760 assert(Client != NULL);
761 return Client->cloaked;
765 * Get (potentially cloaked) hostname of a client to display it to other users.
767 * If the client has not enabled cloaking, the real hostname is used.
769 * @param Client Pointer to client structure
770 * @return Pointer to client hostname
773 Client_HostnameDisplayed(CLIENT *Client)
775 assert(Client != NULL);
777 /* Client isn't cloaked at all, return real hostname: */
778 if (!Client_HasMode(Client, 'x'))
779 return Client_Hostname(Client);
781 /* Use an already saved cloaked hostname, if there is one */
783 return Client->cloaked;
785 Client_UpdateCloakedHostname(Client, NULL, NULL);
786 return Client->cloaked;
790 * Update (and generate, if necessary) the cloaked hostname of a client.
792 * The newly set cloaked hostname is announced in the network using METADATA
793 * commands to peers that support this feature.
795 * @param Client The client of which the cloaked hostname should be updated.
796 * @param Origin The originator of the hostname change, or NULL if this server.
797 * @param Hostname The new cloaked hostname, or NULL if it should be generated.
800 Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
801 const char *Hostname)
803 char Cloak_Buffer[CLIENT_HOST_LEN];
805 assert(Client != NULL);
807 Origin = Client_ThisServer();
809 if (!Client->cloaked) {
810 Client->cloaked = malloc(CLIENT_HOST_LEN);
811 if (!Client->cloaked)
816 /* Generate new cloaked hostname */
817 if (*Conf_CloakHostModeX) {
818 strlcpy(Cloak_Buffer, Client->host,
819 sizeof(Cloak_Buffer));
820 strlcat(Cloak_Buffer, Conf_CloakHostSalt,
821 sizeof(Cloak_Buffer));
822 snprintf(Client->cloaked, CLIENT_HOST_LEN,
823 Conf_CloakHostModeX, Hash(Cloak_Buffer));
825 strlcpy(Client->cloaked, Client_ID(Client->introducer),
828 strlcpy(Client->cloaked, Hostname, CLIENT_HOST_LEN);
829 LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
830 Client_ID(Client), Client->cloaked);
832 /* Inform other servers in the network */
833 IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
834 "METADATA %s cloakhost :%s",
835 Client_ID(Client), Client->cloaked);
839 Client_Modes( CLIENT *Client )
841 assert( Client != NULL );
842 return Client->modes;
847 Client_Flags( CLIENT *Client )
849 assert( Client != NULL );
850 return Client->flags;
855 Client_OperByMe( CLIENT *Client )
857 assert( Client != NULL );
858 return Client->oper_by_me;
859 } /* Client_OperByMe */
863 Client_Hops( CLIENT *Client )
865 assert( Client != NULL );
871 Client_Token( CLIENT *Client )
873 assert( Client != NULL );
874 return Client->token;
879 Client_MyToken( CLIENT *Client )
881 assert( Client != NULL );
882 return Client->mytoken;
883 } /* Client_MyToken */
887 Client_NextHop( CLIENT *Client )
891 assert( Client != NULL );
894 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
898 } /* Client_NextHop */
902 * Return ID of a client: "client!user@host"
903 * This client ID is used for IRC prefixes, for example.
904 * Please note that this function uses a global static buffer, so you can't
905 * nest invocations without overwriting earlier results!
906 * @param Client Pointer to client structure
907 * @return Pointer to global buffer containing the client ID
910 Client_Mask( CLIENT *Client )
912 static char Mask_Buffer[GETID_LEN];
914 assert (Client != NULL);
916 /* Servers: return name only, there is no "mask" */
917 if (Client->type == CLIENT_SERVER)
920 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
921 Client->id, Client->user, Client->host);
927 * Return ID of a client with cloaked hostname: "client!user@server-name"
929 * This client ID is used for IRC prefixes, for example.
930 * Please note that this function uses a global static buffer, so you can't
931 * nest invocations without overwriting earlier results!
932 * If the client has not enabled cloaking, the real hostname is used.
934 * @param Client Pointer to client structure
935 * @return Pointer to global buffer containing the client ID
938 Client_MaskCloaked(CLIENT *Client)
940 static char Mask_Buffer[GETID_LEN];
942 assert (Client != NULL);
944 /* Is the client using cloaking at all? */
945 if (!Client_HasMode(Client, 'x'))
946 return Client_Mask(Client);
948 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
949 Client_HostnameDisplayed(Client));
952 } /* Client_MaskCloaked */
956 Client_Introducer( CLIENT *Client )
958 assert( Client != NULL );
959 return Client->introducer;
960 } /* Client_Introducer */
964 Client_TopServer( CLIENT *Client )
966 assert( Client != NULL );
967 return Client->topserver;
968 } /* Client_TopServer */
972 Client_HasMode( CLIENT *Client, char Mode )
974 assert( Client != NULL );
975 return strchr( Client->modes, Mode ) != NULL;
976 } /* Client_HasMode */
980 Client_HasFlag( CLIENT *Client, char Flag )
982 assert( Client != NULL );
983 return strchr( Client->flags, Flag ) != NULL;
984 } /* Client_HasFlag */
988 Client_Away( CLIENT *Client )
990 assert( Client != NULL );
996 Client_AccountName(CLIENT *Client)
998 assert(Client != NULL);
999 return Client->account_name;
1004 * Make sure that a given nickname is valid.
1006 * If the nickname is not valid for the given client, this function sends back
1007 * the appropriate error messages.
1009 * @param Client Client that wants to change the nickname.
1010 * @param Nick New nickname.
1011 * @returns true if nickname is valid, false otherwise.
1014 Client_CheckNick(CLIENT *Client, char *Nick)
1016 assert(Client != NULL);
1017 assert(Nick != NULL);
1019 if (!Client_IsValidNick(Nick)) {
1020 if (strlen(Nick ) >= Conf_MaxNickLength)
1021 IRC_WriteErrClient(Client, ERR_NICKNAMETOOLONG_MSG,
1022 Client_ID(Client), Nick,
1023 Conf_MaxNickLength - 1);
1025 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1026 Client_ID(Client), Nick);
1030 if (Client_Type(Client) != CLIENT_SERVER
1031 && Client_Type(Client) != CLIENT_SERVICE) {
1032 /* Make sure that this isn't a restricted/forbidden nickname */
1033 if (Conf_NickIsBlocked(Nick)) {
1034 IRC_WriteErrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
1035 Client_ID(Client), Nick);
1040 /* Nickname already registered? */
1041 if (Client_Search(Nick)) {
1042 IRC_WriteErrClient(Client, ERR_NICKNAMEINUSE_MSG,
1043 Client_ID(Client), Nick);
1048 } /* Client_CheckNick */
1052 Client_CheckID( CLIENT *Client, char *ID )
1054 char str[COMMAND_LEN];
1057 assert( Client != NULL );
1058 assert( Client->conn_id > NONE );
1059 assert( ID != NULL );
1062 if (strlen(ID) > CLIENT_ID_LEN) {
1063 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1064 Client_ID(Client), ID);
1068 /* ID already in use? */
1071 if (strcasecmp(c->id, ID) == 0) {
1072 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
1073 if (c->conn_id != NONE)
1074 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
1076 Log(LOG_ERR, "%s (via network)!", str);
1077 Conn_Close(Client->conn_id, str, str, true);
1080 c = (CLIENT *)c->next;
1084 } /* Client_CheckID */
1088 Client_First( void )
1091 } /* Client_First */
1095 Client_Next( CLIENT *c )
1097 assert( c != NULL );
1098 return (CLIENT *)c->next;
1103 Client_UserCount( void )
1105 return Count( CLIENT_USER );
1106 } /* Client_UserCount */
1110 Client_ServiceCount( void )
1112 return Count( CLIENT_SERVICE );;
1113 } /* Client_ServiceCount */
1117 Client_ServerCount( void )
1119 return Count( CLIENT_SERVER );
1120 } /* Client_ServerCount */
1124 Client_MyUserCount( void )
1126 return MyCount( CLIENT_USER );
1127 } /* Client_MyUserCount */
1131 Client_MyServiceCount( void )
1133 return MyCount( CLIENT_SERVICE );
1134 } /* Client_MyServiceCount */
1137 GLOBAL unsigned long
1138 Client_MyServerCount( void )
1141 unsigned long cnt = 0;
1146 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
1147 c = (CLIENT *)c->next;
1150 } /* Client_MyServerCount */
1153 GLOBAL unsigned long
1154 Client_OperCount( void )
1157 unsigned long cnt = 0;
1162 if (c && c->type == CLIENT_USER && Client_HasMode(c, 'o' ))
1164 c = (CLIENT *)c->next;
1167 } /* Client_OperCount */
1170 GLOBAL unsigned long
1171 Client_UnknownCount( void )
1174 unsigned long cnt = 0;
1179 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1180 c = (CLIENT *)c->next;
1184 } /* Client_UnknownCount */
1188 Client_MaxUserCount( void )
1191 } /* Client_MaxUserCount */
1195 Client_MyMaxUserCount( void )
1197 return My_Max_Users;
1198 } /* Client_MyMaxUserCount */
1202 * Check that a given nickname is valid.
1204 * @param Nick the nickname to check.
1205 * @returns true if nickname is valid, false otherwise.
1208 Client_IsValidNick(const char *Nick)
1211 static const char goodchars[] = ";0123456789-";
1213 assert (Nick != NULL);
1215 if (strchr(goodchars, Nick[0]))
1217 if (strlen(Nick ) >= Conf_MaxNickLength)
1222 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1230 } /* Client_IsValidNick */
1234 * Return pointer to "My_Whowas" structure.
1237 Client_GetWhowas( void )
1240 } /* Client_GetWhowas */
1243 * Return the index of the last used WHOWAS entry.
1246 Client_GetLastWhowasIndex( void )
1249 } /* Client_GetLastWhowasIndex */
1253 * Get the start time of this client.
1254 * The result is the start time in seconds since 1970-01-01, as reported
1255 * by the C function time(NULL).
1258 Client_StartTime(CLIENT *Client)
1260 assert( Client != NULL );
1261 return Client->starttime;
1262 } /* Client_Uptime */
1266 * Reject a client when logging in.
1268 * This function is called when a client isn't allowed to connect to this
1269 * server. Possible reasons are bad server password, bad PAM password,
1270 * or that the client is G/K-Line'd.
1272 * After calling this function, the client isn't connected any more.
1274 * @param Client The client to reject.
1275 * @param Reason The reason why the client has been rejected.
1276 * @param InformClient If true, send the exact reason to the client.
1279 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1281 char info[COMMAND_LEN];
1283 assert(Client != NULL);
1284 assert(Reason != NULL);
1287 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1289 strcpy(info, "Access denied: Bad password?");
1292 "User \"%s\" rejected (connection %d): %s!",
1293 Client_Mask(Client), Client_Conn(Client), Reason);
1294 Conn_Close(Client_Conn(Client), Reason, info, true);
1299 * Introduce a new user or service client in the network.
1301 * @param From Remote server introducing the client or NULL (local).
1302 * @param Client New client.
1303 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1306 Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
1308 /* Set client type (user or service) */
1309 Client_SetType(Client, Type);
1312 if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
1314 Client_SetType(Client, CLIENT_SERVICE);
1315 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1316 Client_TypeText(Client), Client_Mask(Client),
1317 Client_Modes(Client), Client_ID(From),
1318 Client_ID(Client_Introducer(Client)),
1319 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1321 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1322 Client_TypeText(Client), Client_Mask(Client),
1323 Client_Conn(Client));
1324 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1325 Client_ID(Client), Client_User(Client),
1326 Client_Hostname(Client),
1327 Conn_IPA(Client_Conn(Client)),
1328 Client_TypeText(Client));
1331 /* Inform other servers */
1332 IRC_WriteStrServersPrefixFlag_CB(From,
1333 From != NULL ? From : Client_ThisServer(),
1334 '\0', cb_introduceClient, (void *)Client);
1335 } /* Client_Introduce */
1338 static unsigned long
1339 Count( CLIENT_TYPE Type )
1342 unsigned long cnt = 0;
1347 if( c->type == Type ) cnt++;
1348 c = (CLIENT *)c->next;
1354 static unsigned long
1355 MyCount( CLIENT_TYPE Type )
1358 unsigned long cnt = 0;
1363 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1364 c = (CLIENT *)c->next;
1371 New_Client_Struct( void )
1375 c = (CLIENT *)malloc( sizeof( CLIENT ));
1378 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1382 memset( c, 0, sizeof ( CLIENT ));
1384 c->type = CLIENT_UNKNOWN;
1386 c->oper_by_me = false;
1396 Generate_MyToken( CLIENT *Client )
1405 if( c->mytoken == token )
1407 /* The token is already in use */
1412 else c = (CLIENT *)c->next;
1414 Client->mytoken = token;
1415 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1416 } /* Generate_MyToken */
1420 Adjust_Counters( CLIENT *Client )
1424 assert( Client != NULL );
1426 if( Client->type != CLIENT_USER ) return;
1428 if( Client->conn_id != NONE )
1430 /* Local connection */
1431 count = Client_MyUserCount( );
1432 if( count > My_Max_Users ) My_Max_Users = count;
1434 count = Client_UserCount( );
1435 if( count > Max_Users ) Max_Users = count;
1436 } /* Adjust_Counters */
1440 * Register client in My_Whowas structure for further recall by WHOWAS.
1441 * Note: Only clients that have been connected at least 30 seconds will be
1442 * registered to prevent automated IRC bots to "destroy" a nice server
1446 Client_RegisterWhowas( CLIENT *Client )
1451 assert( Client != NULL );
1453 /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1454 if (Conf_MorePrivacy)
1458 /* Don't register clients that were connected less than 30 seconds. */
1459 if( now - Client->starttime < 30 )
1462 slot = Last_Whowas + 1;
1463 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1466 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1469 My_Whowas[slot].time = now;
1470 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1471 sizeof( My_Whowas[slot].id ));
1472 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1473 sizeof( My_Whowas[slot].user ));
1474 strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
1475 sizeof( My_Whowas[slot].host ));
1476 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1477 sizeof( My_Whowas[slot].info ));
1478 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1479 sizeof( My_Whowas[slot].server ));
1482 } /* Client_RegisterWhowas */
1486 Client_TypeText(CLIENT *Client)
1488 assert(Client != NULL);
1489 switch (Client_Type(Client)) {
1493 case CLIENT_SERVICE:
1502 } /* Client_TypeText */
1506 * Destroy user or service client.
1509 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1511 if(Client->conn_id != NONE) {
1512 /* Local (directly connected) client */
1514 "%s \"%s\" unregistered (connection %d): %s.",
1515 Client_TypeText(Client), Client_Mask(Client),
1516 Client->conn_id, Txt);
1517 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1518 Client_ID(Client), Client_User(Client),
1519 Client_Hostname(Client), Txt);
1522 /* Inforam all the other servers */
1524 IRC_WriteStrServersPrefix(NULL,
1525 Client, "QUIT :%s", FwdMsg );
1527 IRC_WriteStrServersPrefix(NULL,
1532 LogDebug("%s \"%s\" unregistered: %s.",
1533 Client_TypeText(Client), Client_Mask(Client), Txt);
1536 /* Inform all the other servers, but the ones in the
1537 * direction we got the QUIT from */
1539 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1540 Client, "QUIT :%s", FwdMsg );
1542 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1547 /* Unregister client from channels */
1548 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1550 /* Register client in My_Whowas structure */
1551 Client_RegisterWhowas(Client);
1552 } /* Destroy_UserOrService */
1556 * Introduce a new user or service client to a remote server.
1558 * @param To The remote server to inform.
1559 * @param Prefix Prefix for the generated commands.
1560 * @param data CLIENT structure of the new client.
1563 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1565 CLIENT *c = (CLIENT *)data;
1567 (void)Client_Announce(To, Prefix, c);
1569 } /* cb_introduceClient */
1573 * Announce an user or service to a server.
1575 * This function differentiates between RFC1459 and RFC2813 server links and
1576 * generates the appropriate commands to register the user or service.
1578 * @param Client Server
1579 * @param Prefix Prefix for the generated commands
1580 * @param User User to announce
1583 Client_Announce(CLIENT * Client, CLIENT * Prefix, CLIENT * User)
1586 char *modes, *user, *host;
1588 modes = Client_Modes(User);
1589 user = Client_User(User) ? Client_User(User) : "-";
1590 host = Client_Hostname(User) ? Client_Hostname(User) : "-";
1592 conn = Client_Conn(Client);
1593 if (Conn_Options(conn) & CONN_RFC1459) {
1594 /* RFC 1459 mode: separate NICK and USER commands */
1595 if (! Conn_WriteStr(conn, "NICK %s :%d",
1596 Client_ID(User), Client_Hops(User) + 1))
1597 return DISCONNECTED;
1598 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1599 Client_ID(User), user, host,
1600 Client_ID(Client_Introducer(User)),
1602 return DISCONNECTED;
1604 if (! Conn_WriteStr(conn, ":%s MODE %s +%s",
1605 Client_ID(User), Client_ID(User),
1607 return DISCONNECTED;
1610 /* RFC 2813 mode: one combined NICK or SERVICE command */
1611 if (Client_Type(User) == CLIENT_SERVICE
1612 && Client_HasFlag(Client, 'S')) {
1613 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1614 "SERVICE %s %d * +%s %d :%s",
1616 Client_MyToken(Client_Introducer(User)),
1617 modes, Client_Hops(User) + 1,
1619 return DISCONNECTED;
1621 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1622 "NICK %s %d %s %s %d +%s :%s",
1623 Client_ID(User), Client_Hops(User) + 1,
1625 Client_MyToken(Client_Introducer(User)),
1626 modes, Client_Info(User)))
1627 return DISCONNECTED;
1631 if (Client_HasFlag(Client, 'M')) {
1632 /* Synchronize metadata */
1633 if (Client_HostnameCloaked(User)) {
1634 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1635 "METADATA %s cloakhost :%s",
1637 Client_HostnameCloaked(User)))
1638 return DISCONNECTED;
1641 if (Client_AccountName(User)) {
1642 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1643 "METADATA %s accountname :%s",
1645 Client_AccountName(User)))
1646 return DISCONNECTED;
1649 if (Conn_GetCertFp(Client_Conn(User))) {
1650 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1651 "METADATA %s certfp :%s",
1653 Conn_GetCertFp(Client_Conn(User))))
1654 return DISCONNECTED;
1659 } /* Client_Announce */
1665 Client_DebugDump(void)
1669 Log(LOG_DEBUG, "Client status:");
1673 " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1674 Client_ID(c), Client_Type(c), Client_Hostname(c),
1675 Client_User(c), Client_Conn(c), Client_StartTime(c),
1677 c = (CLIENT *)c->next;
1679 } /* Client_DumpClients */