2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 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.
34 #include "conn-func.h"
36 #include "irc-write.h"
41 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
43 static CLIENT *This_Server, *My_Clients;
45 static WHOWAS My_Whowas[MAX_WHOWAS];
46 static int Last_Whowas = -1;
47 static long Max_Users, My_Max_Users;
50 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
51 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
53 static CLIENT *New_Client_Struct PARAMS(( void ));
54 static void Generate_MyToken PARAMS(( CLIENT *Client ));
55 static void Adjust_Counters PARAMS(( CLIENT *Client ));
57 static void Free_Client PARAMS(( CLIENT **Client ));
59 static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
60 CLIENT *TopServer, int Type, const char *ID,
61 const char *User, const char *Hostname, const char *Info,
62 int Hops, int Token, const char *Modes,
65 static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
68 static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
76 This_Server = New_Client_Struct( );
79 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
80 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
84 /* Client structure for this server */
85 This_Server->next = NULL;
86 This_Server->type = CLIENT_SERVER;
87 This_Server->conn_id = NONE;
88 This_Server->introducer = This_Server;
89 This_Server->mytoken = 1;
90 This_Server->hops = 0;
92 gethostname( This_Server->host, CLIENT_HOST_LEN );
94 h = gethostbyname( This_Server->host );
95 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
97 Client_SetID( This_Server, Conf_ServerName );
98 Client_SetInfo( This_Server, Conf_ServerInfo );
100 My_Clients = This_Server;
102 memset( &My_Whowas, 0, sizeof( My_Whowas ));
112 if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
113 else Client_Destroy( This_Server, "Server going down.", NULL, false );
119 next = (CLIENT *)c->next;
124 Log(LOG_INFO, "Freed %d client structure%s.",
125 cnt, cnt == 1 ? "" : "s");
130 Client_ThisServer( void )
133 } /* Client_ThisServer */
137 * Initialize new local client; wrapper function for Init_New_Client().
138 * @return New CLIENT structure.
141 Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
143 return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
144 Hostname, NULL, 0, 0, NULL, Idented);
145 } /* Client_NewLocal */
149 * Initialize new remote server; wrapper function for Init_New_Client().
150 * @return New CLIENT structure.
153 Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
154 int Hops, int Token, const char *Info, bool Idented)
156 return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
157 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
158 } /* Client_NewRemoteServer */
162 * Initialize new remote client; wrapper function for Init_New_Client().
163 * @return New CLIENT structure.
166 Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
167 const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
169 return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
170 User, Hostname, Info, Hops, Token, Modes, Idented);
171 } /* Client_NewRemoteUser */
175 * Initialize new client and set up the given parameters like client type,
176 * user name, host name, introducing server etc. ...
177 * @return New CLIENT structure.
180 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
181 int Type, const char *ID, const char *User, const char *Hostname,
182 const char *Info, int Hops, int Token, const char *Modes, bool Idented)
187 assert(Introducer != NULL);
189 client = New_Client_Struct();
193 client->starttime = time(NULL);
194 client->conn_id = Idx;
195 client->introducer = Introducer;
196 client->topserver = TopServer;
199 Client_SetID(client, ID);
201 Client_SetUser(client, User, Idented);
202 Client_SetOrigUser(client, User);
205 Client_SetHostname(client, Hostname);
207 Client_SetInfo(client, Info);
209 client->token = Token;
211 Client_SetModes(client, Modes);
212 if (Type == CLIENT_SERVER)
213 Generate_MyToken(client);
215 if (Client_HasMode(client, 'a'))
216 client->away = strndup(DEFAULT_AWAY_MSG, CLIENT_AWAY_LEN - 1);
218 client->next = (POINTER *)My_Clients;
221 Adjust_Counters(client);
224 } /* Init_New_Client */
228 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
230 /* remove a client */
233 char msg[COMMAND_LEN];
236 assert( Client != NULL );
238 txt = LogMsg ? LogMsg : FwdMsg;
240 txt = "Reason unknown";
242 /* netsplit message */
243 if( Client->type == CLIENT_SERVER ) {
244 strlcpy(msg, This_Server->id, sizeof (msg));
245 strlcat(msg, " ", sizeof (msg));
246 strlcat(msg, Client->id, sizeof (msg));
253 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
256 * The client that is about to be removed is a server,
257 * the client we are checking right now is a child of that
258 * server and thus has to be removed, too.
260 * Call Client_Destroy() recursively with the server as the
261 * new "object to be removed". This starts the cycle again, until
262 * all servers that are linked via the original server have been
265 Client_Destroy( c, NULL, msg, false );
272 /* found the client: remove it */
273 if( last ) last->next = c->next;
274 else My_Clients = (CLIENT *)c->next;
276 if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
277 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
278 else if( c->type == CLIENT_SERVER )
280 if (c != This_Server) {
281 if (c->conn_id != NONE)
282 Log(LOG_NOTICE|LOG_snotice,
283 "Server \"%s\" unregistered (connection %d): %s.",
284 c->id, c->conn_id, txt);
286 Log(LOG_NOTICE|LOG_snotice,
287 "Server \"%s\" unregistered: %s.",
291 /* inform other servers */
292 if( ! NGIRCd_SignalQuit )
294 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
295 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
300 if (c->conn_id != NONE) {
303 "Client \"%s\" unregistered (connection %d): %s.",
304 c->id, c->conn_id, txt);
307 "Client unregistered (connection %d): %s.",
311 "Unregistered unknown client \"%s\": %s",
312 c->id[0] ? c->id : "(No Nick)", txt);
320 c = (CLIENT *)c->next;
322 } /* Client_Destroy */
326 * Set client hostname.
328 * If global hostname cloaking is in effect, don't set the real hostname
329 * but the configured one.
331 * @param Client The client of which the hostname should be set.
332 * @param Hostname The new hostname.
335 Client_SetHostname( CLIENT *Client, const char *Hostname )
337 assert(Client != NULL);
338 assert(Hostname != NULL);
340 if (Conf_CloakHost[0]) {
341 char cloak[GETID_LEN];
343 strlcpy(cloak, Hostname, GETID_LEN);
344 strlcat(cloak, Conf_CloakHostSalt, GETID_LEN);
345 snprintf(cloak, GETID_LEN, Conf_CloakHost, Hash(cloak));
347 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
348 Client_ID(Client), Client->host, cloak);
349 strlcpy(Client->host, cloak, sizeof(Client->host));
351 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
352 Client_ID(Client), Client->host, Hostname);
353 strlcpy(Client->host, Hostname, sizeof(Client->host));
355 } /* Client_SetHostname */
359 * Set IP address to display for a client.
361 * @param Client The client.
362 * @param IPAText Textual representation of the IP address or NULL to unset.
365 Client_SetIPAText(CLIENT *Client, const char *IPAText)
367 assert(Client != NULL);
369 if (Client->ipa_text)
370 free(Client->ipa_text);
373 Client->ipa_text = strndup(IPAText, CLIENT_HOST_LEN - 1);
375 Client->ipa_text = NULL;
380 Client_SetID( CLIENT *Client, const char *ID )
382 assert( Client != NULL );
383 assert( ID != NULL );
385 strlcpy( Client->id, ID, sizeof( Client->id ));
387 if (Conf_CloakUserToNick) {
388 strlcpy( Client->user, ID, sizeof( Client->user ));
389 strlcpy( Client->info, ID, sizeof( Client->info ));
393 Client->hash = Hash( Client->id );
398 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
400 /* set clients username */
402 assert( Client != NULL );
403 assert( User != NULL );
405 if (Conf_CloakUserToNick) {
406 strlcpy(Client->user, Client->id, sizeof(Client->user));
407 } else if (Idented) {
408 strlcpy(Client->user, User, sizeof(Client->user));
410 Client->user[0] = '~';
411 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
413 } /* Client_SetUser */
417 * Set "original" user name of a client.
418 * This function saves the "original" user name, the user name specified by
419 * the peer using the USER command, into the CLIENT structure. This user
420 * name may be used for authentication, for example.
421 * @param Client The client.
422 * @param User User name to set.
425 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
427 assert(Client != NULL);
428 assert(User != NULL);
431 strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
433 } /* Client_SetOrigUser */
437 Client_SetInfo( CLIENT *Client, const char *Info )
439 /* set client hostname */
441 assert( Client != NULL );
442 assert( Info != NULL );
444 if (Conf_CloakUserToNick)
445 strlcpy(Client->info, Client->id, sizeof(Client->info));
447 strlcpy(Client->info, Info, sizeof(Client->info));
448 } /* Client_SetInfo */
452 Client_SetModes( CLIENT *Client, const char *Modes )
454 assert( Client != NULL );
455 assert( Modes != NULL );
457 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
458 } /* Client_SetModes */
462 Client_SetFlags( CLIENT *Client, const char *Flags )
464 assert( Client != NULL );
465 assert( Flags != NULL );
467 strlcpy(Client->flags, Flags, sizeof(Client->flags));
468 } /* Client_SetFlags */
472 Client_SetAccountName(CLIENT *Client, const char *AccountName)
474 assert(Client != NULL);
476 if (Client->account_name)
477 free(Client->account_name);
480 Client->account_name = strndup(AccountName,
481 CLIENT_NICK_LEN - 1);
483 Client->account_name = NULL;
488 Client_SetAway( CLIENT *Client, const char *Txt )
490 /* Set AWAY reason of client */
492 assert( Client != NULL );
493 assert( Txt != NULL );
498 Client->away = strndup(Txt, CLIENT_AWAY_LEN - 1);
500 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
501 Client_Mask(Client), Txt);
502 } /* Client_SetAway */
506 Client_SetType( CLIENT *Client, int Type )
508 assert( Client != NULL );
510 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
511 Adjust_Counters( Client );
512 } /* Client_SetType */
516 Client_SetHops( CLIENT *Client, int Hops )
518 assert( Client != NULL );
520 } /* Client_SetHops */
524 Client_SetToken( CLIENT *Client, int Token )
526 assert( Client != NULL );
527 Client->token = Token;
528 } /* Client_SetToken */
532 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
534 assert( Client != NULL );
535 assert( Introducer != NULL );
536 Client->introducer = Introducer;
537 } /* Client_SetIntroducer */
541 Client_ModeAdd( CLIENT *Client, char Mode )
544 * If Client already had Mode, return false.
545 * If the Mode was newly set, return true.
550 assert( Client != NULL );
552 x[0] = Mode; x[1] = '\0';
553 if (!Client_HasMode(Client, x[0])) {
554 strlcat( Client->modes, x, sizeof( Client->modes ));
558 } /* Client_ModeAdd */
562 Client_ModeDel( CLIENT *Client, char Mode )
565 * If Mode was removed, return true.
566 * If Client did not have Mode, return false.
571 assert( Client != NULL );
573 x[0] = Mode; x[1] = '\0';
575 p = strchr( Client->modes, x[0] );
576 if( ! p ) return false;
578 /* Client has Mode -> delete */
585 } /* Client_ModeDel */
589 * Search CLIENT structure of a given nick name.
591 * @return Pointer to CLIENT structure or NULL if not found.
594 Client_Search( const char *Nick )
596 char search_id[CLIENT_ID_LEN], *ptr;
600 assert( Nick != NULL );
602 /* copy Nick and truncate hostmask if necessary */
603 strlcpy( search_id, Nick, sizeof( search_id ));
604 ptr = strchr( search_id, '!' );
605 if( ptr ) *ptr = '\0';
607 search_hash = Hash(search_id);
611 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
613 c = (CLIENT *)c->next;
620 * Search first CLIENT structure matching a given mask of a server.
622 * The order of servers is arbitrary, but this function makes sure that the
623 * local server is always returned if the mask matches it.
625 * @return Pointer to CLIENT structure or NULL if no server could be found.
628 Client_SearchServer(const char *Mask)
632 assert(Mask != NULL);
634 /* First check if mask matches the local server */
635 if (MatchCaseInsensitive(Mask, Client_ID(Client_ThisServer())))
636 return Client_ThisServer();
640 if (Client_Type(c) == CLIENT_SERVER) {
641 /* This is a server: check if Mask matches */
642 if (MatchCaseInsensitive(Mask, c->id))
645 c = (CLIENT *)c->next;
652 * Get client structure ("introducer") identfied by a server token.
653 * @return CLIENT structure or NULL if none could be found.
656 Client_GetFromToken( CLIENT *Client, int Token )
660 assert( Client != NULL );
667 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
670 c = (CLIENT *)c->next;
673 } /* Client_GetFromToken */
677 Client_Type( CLIENT *Client )
679 assert( Client != NULL );
685 Client_Conn( CLIENT *Client )
687 assert( Client != NULL );
688 return Client->conn_id;
693 Client_ID( CLIENT *Client )
695 assert( Client != NULL );
698 if(Client->type == CLIENT_USER)
699 assert(strlen(Client->id) < Conf_MaxNickLength);
702 if( Client->id[0] ) return Client->id;
708 Client_Info( CLIENT *Client )
710 assert( Client != NULL );
716 Client_User( CLIENT *Client )
718 assert( Client != NULL );
719 return Client->user[0] ? Client->user : "~";
726 * Get the "original" user name as supplied by the USER command.
727 * The user name as given by the client is used for authentication instead
728 * of the one detected using IDENT requests.
729 * @param Client The client.
730 * @return Original user name.
733 Client_OrigUser(CLIENT *Client) {
734 return Client->orig_user;
735 } /* Client_OrigUser */
740 * Return the hostname of a client.
741 * @param Client Pointer to client structure
742 * @return Pointer to client hostname
745 Client_Hostname(CLIENT *Client)
747 assert (Client != NULL);
752 * Return the cloaked hostname of a client, if set.
753 * @param Client Pointer to the client structure.
754 * @return Pointer to the cloaked hostname or NULL if not set.
757 Client_HostnameCloaked(CLIENT *Client)
759 assert(Client != NULL);
760 return Client->cloaked;
764 * Get (potentially cloaked) hostname of a client to display it to other users.
766 * If the client has not enabled cloaking, the real hostname is used.
768 * @param Client Pointer to client structure
769 * @return Pointer to client hostname
772 Client_HostnameDisplayed(CLIENT *Client)
774 assert(Client != NULL);
776 /* Client isn't cloaked at all, return real hostname: */
777 if (!Client_HasMode(Client, 'x'))
778 return Client_Hostname(Client);
780 /* Use an already saved cloaked hostname, if there is one */
782 return Client->cloaked;
784 Client_UpdateCloakedHostname(Client, NULL, NULL);
785 return Client->cloaked;
789 Client_IPAText(CLIENT *Client)
791 assert(Client != NULL);
793 /* Not a local client? */
794 if (Client_Conn(Client) <= NONE)
797 if (!Client->ipa_text)
798 return Conn_GetIPAInfo(Client_Conn(Client));
800 return Client->ipa_text;
804 * Update (and generate, if necessary) the cloaked hostname of a client.
806 * The newly set cloaked hostname is announced in the network using METADATA
807 * commands to peers that support this feature.
809 * @param Client The client of which the cloaked hostname should be updated.
810 * @param Origin The originator of the hostname change, or NULL if this server.
811 * @param Hostname The new cloaked hostname, or NULL if it should be generated.
814 Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
815 const char *Hostname)
817 char Cloak_Buffer[CLIENT_HOST_LEN];
819 assert(Client != NULL);
821 Origin = Client_ThisServer();
823 if (!Client->cloaked) {
824 Client->cloaked = malloc(CLIENT_HOST_LEN);
825 if (!Client->cloaked)
830 /* Generate new cloaked hostname */
831 if (*Conf_CloakHostModeX) {
832 strlcpy(Cloak_Buffer, Client->host,
833 sizeof(Cloak_Buffer));
834 strlcat(Cloak_Buffer, Conf_CloakHostSalt,
835 sizeof(Cloak_Buffer));
836 snprintf(Client->cloaked, CLIENT_HOST_LEN,
837 Conf_CloakHostModeX, Hash(Cloak_Buffer));
839 strlcpy(Client->cloaked, Client_ID(Client->introducer),
842 strlcpy(Client->cloaked, Hostname, CLIENT_HOST_LEN);
843 LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
844 Client_ID(Client), Client->cloaked);
846 /* Inform other servers in the network */
847 IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
848 "METADATA %s cloakhost :%s",
849 Client_ID(Client), Client->cloaked);
853 Client_Modes( CLIENT *Client )
855 assert( Client != NULL );
856 return Client->modes;
861 Client_Flags( CLIENT *Client )
863 assert( Client != NULL );
864 return Client->flags;
869 Client_Hops( CLIENT *Client )
871 assert( Client != NULL );
877 Client_Token( CLIENT *Client )
879 assert( Client != NULL );
880 return Client->token;
885 Client_MyToken( CLIENT *Client )
887 assert( Client != NULL );
888 return Client->mytoken;
889 } /* Client_MyToken */
893 Client_NextHop( CLIENT *Client )
897 assert( Client != NULL );
900 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
904 } /* Client_NextHop */
908 * Return ID of a client: "client!user@host"
909 * This client ID is used for IRC prefixes, for example.
910 * Please note that this function uses a global static buffer, so you can't
911 * nest invocations without overwriting earlier results!
912 * @param Client Pointer to client structure
913 * @return Pointer to global buffer containing the client ID
916 Client_Mask( CLIENT *Client )
918 static char Mask_Buffer[GETID_LEN];
920 assert (Client != NULL);
922 /* Servers: return name only, there is no "mask" */
923 if (Client->type == CLIENT_SERVER)
926 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
927 Client->id, Client->user, Client->host);
933 * Return ID of a client with cloaked hostname: "client!user@server-name"
935 * This client ID is used for IRC prefixes, for example.
936 * Please note that this function uses a global static buffer, so you can't
937 * nest invocations without overwriting earlier results!
938 * If the client has not enabled cloaking, the real hostname is used.
940 * @param Client Pointer to client structure
941 * @return Pointer to global buffer containing the client ID
944 Client_MaskCloaked(CLIENT *Client)
946 static char Mask_Buffer[GETID_LEN];
948 assert (Client != NULL);
950 /* Is the client using cloaking at all? */
951 if (!Client_HasMode(Client, 'x'))
952 return Client_Mask(Client);
954 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
955 Client_HostnameDisplayed(Client));
958 } /* Client_MaskCloaked */
962 Client_Introducer( CLIENT *Client )
964 assert( Client != NULL );
965 return Client->introducer;
966 } /* Client_Introducer */
970 Client_TopServer( CLIENT *Client )
972 assert( Client != NULL );
973 return Client->topserver;
974 } /* Client_TopServer */
978 Client_HasMode( CLIENT *Client, char Mode )
980 assert( Client != NULL );
981 return strchr( Client->modes, Mode ) != NULL;
982 } /* Client_HasMode */
986 Client_HasFlag( CLIENT *Client, char Flag )
988 assert( Client != NULL );
989 return strchr( Client->flags, Flag ) != NULL;
990 } /* Client_HasFlag */
994 Client_Away( CLIENT *Client )
996 assert( Client != NULL );
1002 Client_AccountName(CLIENT *Client)
1004 assert(Client != NULL);
1005 return Client->account_name;
1010 * Make sure that a given nickname is valid.
1012 * If the nickname is not valid for the given client, this function sends back
1013 * the appropriate error messages.
1015 * @param Client Client that wants to change the nickname.
1016 * @param Nick New nickname.
1017 * @returns true if nickname is valid, false otherwise.
1020 Client_CheckNick(CLIENT *Client, char *Nick)
1022 assert(Client != NULL);
1023 assert(Nick != NULL);
1025 if (!Client_IsValidNick(Nick)) {
1026 if (strlen(Nick ) >= Conf_MaxNickLength)
1027 IRC_WriteErrClient(Client, ERR_NICKNAMETOOLONG_MSG,
1028 Client_ID(Client), Nick,
1029 Conf_MaxNickLength - 1);
1031 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1032 Client_ID(Client), Nick);
1036 if (Client_Type(Client) != CLIENT_SERVER
1037 && Client_Type(Client) != CLIENT_SERVICE) {
1038 /* Make sure that this isn't a restricted/forbidden nickname */
1039 if (Conf_NickIsBlocked(Nick)) {
1040 IRC_WriteErrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
1041 Client_ID(Client), Nick);
1046 /* Nickname already registered? */
1047 if (Client_Search(Nick)) {
1048 IRC_WriteErrClient(Client, ERR_NICKNAMEINUSE_MSG,
1049 Client_ID(Client), Nick);
1054 } /* Client_CheckNick */
1058 Client_CheckID( CLIENT *Client, char *ID )
1060 char str[COMMAND_LEN];
1063 assert( Client != NULL );
1064 assert( Client->conn_id > NONE );
1065 assert( ID != NULL );
1068 if (strlen(ID) > CLIENT_ID_LEN) {
1069 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1070 Client_ID(Client), ID);
1074 /* ID already in use? */
1077 if (strcasecmp(c->id, ID) == 0) {
1078 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
1079 if (c->conn_id != NONE)
1080 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
1082 Log(LOG_ERR, "%s (via network)!", str);
1083 Conn_Close(Client->conn_id, str, str, true);
1086 c = (CLIENT *)c->next;
1090 } /* Client_CheckID */
1094 Client_First( void )
1097 } /* Client_First */
1101 Client_Next( CLIENT *c )
1103 assert( c != NULL );
1104 return (CLIENT *)c->next;
1109 Client_UserCount( void )
1111 return Count( CLIENT_USER );
1112 } /* Client_UserCount */
1116 Client_ServiceCount( void )
1118 return Count( CLIENT_SERVICE );;
1119 } /* Client_ServiceCount */
1123 Client_ServerCount( void )
1125 return Count( CLIENT_SERVER );
1126 } /* Client_ServerCount */
1130 Client_MyUserCount( void )
1132 return MyCount( CLIENT_USER );
1133 } /* Client_MyUserCount */
1137 Client_MyServiceCount( void )
1139 return MyCount( CLIENT_SERVICE );
1140 } /* Client_MyServiceCount */
1143 GLOBAL unsigned long
1144 Client_MyServerCount( void )
1147 unsigned long cnt = 0;
1152 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
1153 c = (CLIENT *)c->next;
1156 } /* Client_MyServerCount */
1159 GLOBAL unsigned long
1160 Client_OperCount( void )
1163 unsigned long cnt = 0;
1168 if (c && c->type == CLIENT_USER && Client_HasMode(c, 'o' ))
1170 c = (CLIENT *)c->next;
1173 } /* Client_OperCount */
1176 GLOBAL unsigned long
1177 Client_UnknownCount( void )
1180 unsigned long cnt = 0;
1185 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1186 c = (CLIENT *)c->next;
1190 } /* Client_UnknownCount */
1194 Client_MaxUserCount( void )
1197 } /* Client_MaxUserCount */
1201 Client_MyMaxUserCount( void )
1203 return My_Max_Users;
1204 } /* Client_MyMaxUserCount */
1208 * Check that a given nickname is valid.
1210 * @param Nick the nickname to check.
1211 * @returns true if nickname is valid, false otherwise.
1214 Client_IsValidNick(const char *Nick)
1217 static const char goodchars[] = ";0123456789-";
1219 assert (Nick != NULL);
1221 if (strchr(goodchars, Nick[0]))
1223 if (strlen(Nick ) >= Conf_MaxNickLength)
1228 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1236 } /* Client_IsValidNick */
1240 * Return pointer to "My_Whowas" structure.
1243 Client_GetWhowas( void )
1246 } /* Client_GetWhowas */
1249 * Return the index of the last used WHOWAS entry.
1252 Client_GetLastWhowasIndex( void )
1255 } /* Client_GetLastWhowasIndex */
1259 * Get the start time of this client.
1260 * The result is the start time in seconds since 1970-01-01, as reported
1261 * by the C function time(NULL).
1264 Client_StartTime(CLIENT *Client)
1266 assert( Client != NULL );
1267 return Client->starttime;
1268 } /* Client_Uptime */
1272 * Reject a client when logging in.
1274 * This function is called when a client isn't allowed to connect to this
1275 * server. Possible reasons are bad server password, bad PAM password,
1276 * or that the client is G/K-Line'd.
1278 * After calling this function, the client isn't connected any more.
1280 * @param Client The client to reject.
1281 * @param Reason The reason why the client has been rejected.
1282 * @param InformClient If true, send the exact reason to the client.
1285 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1287 char info[COMMAND_LEN];
1289 assert(Client != NULL);
1290 assert(Reason != NULL);
1293 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1295 strcpy(info, "Access denied: Bad password?");
1298 "User \"%s\" rejected (connection %d): %s!",
1299 Client_Mask(Client), Client_Conn(Client), Reason);
1300 Conn_Close(Client_Conn(Client), Reason, info, true);
1305 * Introduce a new user or service client in the network.
1307 * @param From Remote server introducing the client or NULL (local).
1308 * @param Client New client.
1309 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1312 Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
1314 /* Set client type (user or service) */
1315 Client_SetType(Client, Type);
1318 if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
1320 Client_SetType(Client, CLIENT_SERVICE);
1321 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1322 Client_TypeText(Client), Client_Mask(Client),
1323 Client_Modes(Client), Client_ID(From),
1324 Client_ID(Client_Introducer(Client)),
1325 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1327 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1328 Client_TypeText(Client), Client_Mask(Client),
1329 Client_Conn(Client));
1330 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1331 Client_ID(Client), Client_User(Client),
1332 Client_Hostname(Client),
1333 Conn_IPA(Client_Conn(Client)),
1334 Client_TypeText(Client));
1337 /* Inform other servers */
1338 IRC_WriteStrServersPrefixFlag_CB(From,
1339 From != NULL ? From : Client_ThisServer(),
1340 '\0', cb_introduceClient, (void *)Client);
1341 } /* Client_Introduce */
1344 static unsigned long
1345 Count( CLIENT_TYPE Type )
1348 unsigned long cnt = 0;
1353 if( c->type == Type ) cnt++;
1354 c = (CLIENT *)c->next;
1360 static unsigned long
1361 MyCount( CLIENT_TYPE Type )
1364 unsigned long cnt = 0;
1369 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1370 c = (CLIENT *)c->next;
1377 * Allocate and initialize new CLIENT strcuture.
1379 * @return Pointer to CLIENT structure or NULL on error.
1382 New_Client_Struct( void )
1386 c = (CLIENT *)malloc( sizeof( CLIENT ));
1389 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1393 memset( c, 0, sizeof ( CLIENT ));
1395 c->type = CLIENT_UNKNOWN;
1405 * Free a CLIENT structure and its member variables.
1408 Free_Client(CLIENT **Client)
1410 assert(Client != NULL);
1411 assert(*Client != NULL);
1413 if ((*Client)->account_name)
1414 free((*Client)->account_name);
1415 if ((*Client)->away)
1416 free((*Client)->away);
1417 if ((*Client)->cloaked)
1418 free((*Client)->cloaked);
1419 if ((*Client)->ipa_text)
1420 free((*Client)->ipa_text);
1427 Generate_MyToken( CLIENT *Client )
1436 if( c->mytoken == token )
1438 /* The token is already in use */
1443 else c = (CLIENT *)c->next;
1445 Client->mytoken = token;
1446 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1447 } /* Generate_MyToken */
1451 Adjust_Counters( CLIENT *Client )
1455 assert( Client != NULL );
1457 if( Client->type != CLIENT_USER ) return;
1459 if( Client->conn_id != NONE )
1461 /* Local connection */
1462 count = Client_MyUserCount( );
1463 if( count > My_Max_Users ) My_Max_Users = count;
1465 count = Client_UserCount( );
1466 if( count > Max_Users ) Max_Users = count;
1467 } /* Adjust_Counters */
1471 * Register client in My_Whowas structure for further recall by WHOWAS.
1472 * Note: Only clients that have been connected at least 30 seconds will be
1473 * registered to prevent automated IRC bots to "destroy" a nice server
1477 Client_RegisterWhowas( CLIENT *Client )
1482 assert( Client != NULL );
1484 /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1485 if (Conf_MorePrivacy)
1489 /* Don't register clients that were connected less than 30 seconds. */
1490 if( now - Client->starttime < 30 )
1493 slot = Last_Whowas + 1;
1494 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1497 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1500 My_Whowas[slot].time = now;
1501 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1502 sizeof( My_Whowas[slot].id ));
1503 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1504 sizeof( My_Whowas[slot].user ));
1505 strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
1506 sizeof( My_Whowas[slot].host ));
1507 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1508 sizeof( My_Whowas[slot].info ));
1509 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1510 sizeof( My_Whowas[slot].server ));
1513 } /* Client_RegisterWhowas */
1517 Client_TypeText(CLIENT *Client)
1519 assert(Client != NULL);
1520 switch (Client_Type(Client)) {
1524 case CLIENT_SERVICE:
1533 } /* Client_TypeText */
1537 * Destroy user or service client.
1540 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1542 if(Client->conn_id != NONE) {
1543 /* Local (directly connected) client */
1545 "%s \"%s\" unregistered (connection %d): %s.",
1546 Client_TypeText(Client), Client_Mask(Client),
1547 Client->conn_id, Txt);
1548 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1549 Client_ID(Client), Client_User(Client),
1550 Client_Hostname(Client), Txt);
1553 /* Inforam all the other servers */
1555 IRC_WriteStrServersPrefix(NULL,
1556 Client, "QUIT :%s", FwdMsg );
1558 IRC_WriteStrServersPrefix(NULL,
1563 LogDebug("%s \"%s\" unregistered: %s.",
1564 Client_TypeText(Client), Client_Mask(Client), Txt);
1567 /* Inform all the other servers, but the ones in the
1568 * direction we got the QUIT from */
1570 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1571 Client, "QUIT :%s", FwdMsg );
1573 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1578 /* Unregister client from channels */
1579 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1581 /* Register client in My_Whowas structure */
1582 Client_RegisterWhowas(Client);
1583 } /* Destroy_UserOrService */
1587 * Introduce a new user or service client to a remote server.
1589 * @param To The remote server to inform.
1590 * @param Prefix Prefix for the generated commands.
1591 * @param data CLIENT structure of the new client.
1594 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1596 CLIENT *c = (CLIENT *)data;
1598 (void)Client_Announce(To, Prefix, c);
1600 } /* cb_introduceClient */
1604 * Announce an user or service to a server.
1606 * This function differentiates between RFC1459 and RFC2813 server links and
1607 * generates the appropriate commands to register the user or service.
1609 * @param Client Server
1610 * @param Prefix Prefix for the generated commands
1611 * @param User User to announce
1614 Client_Announce(CLIENT * Client, CLIENT * Prefix, CLIENT * User)
1617 char *modes, *user, *host;
1619 modes = Client_Modes(User);
1620 user = Client_User(User) ? Client_User(User) : "-";
1621 host = Client_Hostname(User) ? Client_Hostname(User) : "-";
1623 conn = Client_Conn(Client);
1624 if (Conn_Options(conn) & CONN_RFC1459) {
1625 /* RFC 1459 mode: separate NICK and USER commands */
1626 if (! Conn_WriteStr(conn, "NICK %s :%d",
1627 Client_ID(User), Client_Hops(User) + 1))
1628 return DISCONNECTED;
1629 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1630 Client_ID(User), user, host,
1631 Client_ID(Client_Introducer(User)),
1633 return DISCONNECTED;
1635 if (! Conn_WriteStr(conn, ":%s MODE %s +%s",
1636 Client_ID(User), Client_ID(User),
1638 return DISCONNECTED;
1641 /* RFC 2813 mode: one combined NICK or SERVICE command */
1642 if (Client_Type(User) == CLIENT_SERVICE
1643 && Client_HasFlag(Client, 'S')) {
1644 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1645 "SERVICE %s %d * +%s %d :%s",
1647 Client_MyToken(Client_Introducer(User)),
1648 modes, Client_Hops(User) + 1,
1650 return DISCONNECTED;
1652 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1653 "NICK %s %d %s %s %d +%s :%s",
1654 Client_ID(User), Client_Hops(User) + 1,
1656 Client_MyToken(Client_Introducer(User)),
1657 modes, Client_Info(User)))
1658 return DISCONNECTED;
1662 if (Client_HasFlag(Client, 'M')) {
1663 /* Synchronize metadata */
1664 if (Client_HostnameCloaked(User)) {
1665 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1666 "METADATA %s cloakhost :%s",
1668 Client_HostnameCloaked(User)))
1669 return DISCONNECTED;
1672 if (Client_AccountName(User)) {
1673 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1674 "METADATA %s accountname :%s",
1676 Client_AccountName(User)))
1677 return DISCONNECTED;
1680 if (Conn_GetCertFp(Client_Conn(User))) {
1681 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1682 "METADATA %s certfp :%s",
1684 Conn_GetCertFp(Client_Conn(User))))
1685 return DISCONNECTED;
1690 } /* Client_Announce */
1696 Client_DebugDump(void)
1700 Log(LOG_DEBUG, "Client status:");
1704 " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1705 Client_ID(c), Client_Type(c), Client_Hostname(c),
1706 Client_User(c), Client_Conn(c), Client_StartTime(c),
1708 c = (CLIENT *)c->next;
1710 } /* Client_DumpClients */