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.
36 #include "conn-func.h"
38 #include "irc-write.h"
43 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
45 static CLIENT *This_Server, *My_Clients;
47 static WHOWAS My_Whowas[MAX_WHOWAS];
48 static int Last_Whowas = -1;
49 static long Max_Users, My_Max_Users;
52 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
53 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
55 static CLIENT *New_Client_Struct PARAMS(( void ));
56 static void Generate_MyToken PARAMS(( CLIENT *Client ));
57 static void Adjust_Counters PARAMS(( CLIENT *Client ));
59 static void Free_Client PARAMS(( CLIENT **Client ));
61 static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
62 CLIENT *TopServer, int Type, const char *ID,
63 const char *User, const char *Hostname, const char *Info,
64 int Hops, int Token, const char *Modes,
67 static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
70 static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
78 This_Server = New_Client_Struct( );
81 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
82 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
86 /* Client structure for this server */
87 This_Server->next = NULL;
88 This_Server->type = CLIENT_SERVER;
89 This_Server->conn_id = NONE;
90 This_Server->introducer = This_Server;
91 This_Server->mytoken = 1;
92 This_Server->hops = 0;
94 gethostname( This_Server->host, CLIENT_HOST_LEN );
96 h = gethostbyname( This_Server->host );
97 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
99 Client_SetID( This_Server, Conf_ServerName );
100 Client_SetInfo( This_Server, Conf_ServerInfo );
102 My_Clients = This_Server;
104 memset( &My_Whowas, 0, sizeof( My_Whowas ));
114 if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
115 else Client_Destroy( This_Server, "Server going down.", NULL, false );
121 next = (CLIENT *)c->next;
126 Log(LOG_INFO, "Freed %d client structure%s.",
127 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);
191 client = New_Client_Struct();
195 client->starttime = time(NULL);
196 client->conn_id = Idx;
197 client->introducer = Introducer;
198 client->topserver = TopServer;
201 Client_SetID(client, ID);
203 Client_SetUser(client, User, Idented);
204 Client_SetOrigUser(client, User);
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 (Client_HasMode(client, 'a'))
218 client->away = strndup(DEFAULT_AWAY_MSG, CLIENT_AWAY_LEN - 1);
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 */
235 char msg[COMMAND_LEN];
238 assert( Client != NULL );
240 txt = LogMsg ? LogMsg : FwdMsg;
242 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) {
283 if (c->conn_id != NONE)
284 Log(LOG_NOTICE|LOG_snotice,
285 "Server \"%s\" unregistered (connection %d): %s.",
286 c->id, c->conn_id, txt);
288 Log(LOG_NOTICE|LOG_snotice,
289 "Server \"%s\" unregistered: %s.",
293 /* inform other servers */
294 if( ! NGIRCd_SignalQuit )
296 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
297 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
302 if (c->conn_id != NONE) {
305 "Client \"%s\" unregistered (connection %d): %s.",
306 c->id, c->conn_id, txt);
309 "Client unregistered (connection %d): %s.",
313 "Unregistered unknown client \"%s\": %s",
314 c->id[0] ? c->id : "(No Nick)", txt);
322 c = (CLIENT *)c->next;
324 } /* Client_Destroy */
328 * Set client hostname.
330 * If global hostname cloaking is in effect, don't set the real hostname
331 * but the configured one.
333 * @param Client The client of which the hostname should be set.
334 * @param Hostname The new hostname.
337 Client_SetHostname( CLIENT *Client, const char *Hostname )
339 assert(Client != NULL);
340 assert(Hostname != NULL);
342 if (Conf_CloakHost[0]) {
343 char cloak[GETID_LEN];
345 strlcpy(cloak, Hostname, GETID_LEN);
346 strlcat(cloak, Conf_CloakHostSalt, GETID_LEN);
347 snprintf(cloak, GETID_LEN, Conf_CloakHost, Hash(cloak));
349 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
350 Client_ID(Client), Client->host, cloak);
351 strlcpy(Client->host, cloak, sizeof(Client->host));
353 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
354 Client_ID(Client), Client->host, Hostname);
355 strlcpy(Client->host, Hostname, sizeof(Client->host));
357 } /* Client_SetHostname */
361 * Set IP address to display for a client.
363 * @param Client The client.
364 * @param IPAText Textual representation of the IP address or NULL to unset.
367 Client_SetIPAText(CLIENT *Client, const char *IPAText)
369 assert(Client != NULL);
371 if (Client->ipa_text)
372 free(Client->ipa_text);
375 Client->ipa_text = strndup(IPAText, CLIENT_HOST_LEN - 1);
377 Client->ipa_text = NULL;
382 Client_SetID( CLIENT *Client, const char *ID )
384 assert( Client != NULL );
385 assert( ID != NULL );
387 strlcpy( Client->id, ID, sizeof( Client->id ));
389 if (Conf_CloakUserToNick) {
390 strlcpy( Client->user, ID, sizeof( Client->user ));
391 strlcpy( Client->info, ID, sizeof( Client->info ));
395 Client->hash = Hash( Client->id );
400 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
402 /* set clients username */
404 assert( Client != NULL );
405 assert( User != NULL );
407 if (Conf_CloakUserToNick) {
408 strlcpy(Client->user, Client->id, sizeof(Client->user));
409 } else if (Idented) {
410 strlcpy(Client->user, User, sizeof(Client->user));
412 Client->user[0] = '~';
413 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
415 } /* Client_SetUser */
419 * Set "original" user name of a client.
420 * This function saves the "original" user name, the user name specified by
421 * the peer using the USER command, into the CLIENT structure. This user
422 * name may be used for authentication, for example.
423 * @param Client The client.
424 * @param User User name to set.
427 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
429 assert(Client != NULL);
430 assert(User != NULL);
432 #if defined(PAM) && defined(IDENTAUTH)
433 strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
435 } /* Client_SetOrigUser */
439 Client_SetInfo( CLIENT *Client, const char *Info )
441 /* set client hostname */
443 assert( Client != NULL );
444 assert( Info != NULL );
446 if (Conf_CloakUserToNick)
447 strlcpy(Client->info, Client->id, sizeof(Client->info));
449 strlcpy(Client->info, Info, sizeof(Client->info));
450 } /* Client_SetInfo */
454 Client_SetModes( CLIENT *Client, const char *Modes )
456 assert( Client != NULL );
457 assert( Modes != NULL );
459 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
460 } /* Client_SetModes */
464 Client_SetFlags( CLIENT *Client, const char *Flags )
466 assert( Client != NULL );
467 assert( Flags != NULL );
469 strlcpy(Client->flags, Flags, sizeof(Client->flags));
470 } /* Client_SetFlags */
474 Client_SetAccountName(CLIENT *Client, const char *AccountName)
476 assert(Client != NULL);
478 if (Client->account_name)
479 free(Client->account_name);
482 Client->account_name = strndup(AccountName,
483 CLIENT_NICK_LEN - 1);
485 Client->account_name = NULL;
490 Client_SetAway( CLIENT *Client, const char *Txt )
492 /* Set AWAY reason of client */
494 assert( Client != NULL );
495 assert( Txt != NULL );
500 Client->away = strndup(Txt, CLIENT_AWAY_LEN - 1);
502 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
503 Client_Mask(Client), Txt);
504 } /* Client_SetAway */
508 Client_SetType( CLIENT *Client, int Type )
510 assert( Client != NULL );
512 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
513 Adjust_Counters( Client );
514 } /* Client_SetType */
518 Client_SetHops( CLIENT *Client, int Hops )
520 assert( Client != NULL );
522 } /* Client_SetHops */
526 Client_SetToken( CLIENT *Client, int Token )
528 assert( Client != NULL );
529 Client->token = Token;
530 } /* Client_SetToken */
534 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
536 assert( Client != NULL );
537 assert( Introducer != NULL );
538 Client->introducer = Introducer;
539 } /* Client_SetIntroducer */
543 Client_ModeAdd( CLIENT *Client, char Mode )
546 * If Client already had Mode, return false.
547 * If the Mode was newly set, return true.
552 assert( Client != NULL );
554 x[0] = Mode; x[1] = '\0';
555 if (!Client_HasMode(Client, x[0])) {
556 strlcat( Client->modes, x, sizeof( Client->modes ));
560 } /* Client_ModeAdd */
564 Client_ModeDel( CLIENT *Client, char Mode )
567 * If Mode was removed, return true.
568 * If Client did not have Mode, return false.
573 assert( Client != NULL );
575 x[0] = Mode; x[1] = '\0';
577 p = strchr( Client->modes, x[0] );
578 if( ! p ) return false;
580 /* Client has Mode -> delete */
587 } /* Client_ModeDel */
591 * Search CLIENT structure of a given nick name.
593 * @return Pointer to CLIENT structure or NULL if not found.
596 Client_Search( const char *Nick )
598 char search_id[CLIENT_ID_LEN], *ptr;
602 assert( Nick != NULL );
604 /* copy Nick and truncate hostmask if necessary */
605 strlcpy( search_id, Nick, sizeof( search_id ));
606 ptr = strchr( search_id, '!' );
607 if( ptr ) *ptr = '\0';
609 search_hash = Hash(search_id);
613 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
615 c = (CLIENT *)c->next;
622 * Search first CLIENT structure matching a given mask of a server.
624 * The order of servers is arbitrary, but this function makes sure that the
625 * local server is always returned if the mask matches it.
627 * @return Pointer to CLIENT structure or NULL if no server could be found.
630 Client_SearchServer(const char *Mask)
634 assert(Mask != NULL);
636 /* First check if mask matches the local server */
637 if (MatchCaseInsensitive(Mask, Client_ID(Client_ThisServer())))
638 return Client_ThisServer();
642 if (Client_Type(c) == CLIENT_SERVER) {
643 /* This is a server: check if Mask matches */
644 if (MatchCaseInsensitive(Mask, c->id))
647 c = (CLIENT *)c->next;
654 * Get client structure ("introducer") identfied by a server token.
655 * @return CLIENT structure or NULL if none could be found.
658 Client_GetFromToken( CLIENT *Client, int Token )
662 assert( Client != NULL );
669 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
672 c = (CLIENT *)c->next;
675 } /* Client_GetFromToken */
679 Client_Type( CLIENT *Client )
681 assert( Client != NULL );
687 Client_Conn( CLIENT *Client )
689 assert( Client != NULL );
690 return Client->conn_id;
695 Client_ID( CLIENT *Client )
697 assert( Client != NULL );
700 if(Client->type == CLIENT_USER)
701 assert(strlen(Client->id) < Conf_MaxNickLength);
704 if( Client->id[0] ) return Client->id;
710 Client_Info( CLIENT *Client )
712 assert( Client != NULL );
718 Client_User( CLIENT *Client )
720 assert( Client != NULL );
721 return Client->user[0] ? Client->user : "~";
728 * Get the "original" user name as supplied by the USER command.
729 * The user name as given by the client is used for authentication instead
730 * of the one detected using IDENT requests.
731 * @param Client The client.
732 * @return Original user name.
735 Client_OrigUser(CLIENT *Client) {
737 char *user = Client->user;
743 return Client->orig_user;
745 } /* Client_OrigUser */
750 * Return the hostname of a client.
751 * @param Client Pointer to client structure
752 * @return Pointer to client hostname
755 Client_Hostname(CLIENT *Client)
757 assert (Client != NULL);
762 * Return the cloaked hostname of a client, if set.
763 * @param Client Pointer to the client structure.
764 * @return Pointer to the cloaked hostname or NULL if not set.
767 Client_HostnameCloaked(CLIENT *Client)
769 assert(Client != NULL);
770 return Client->cloaked;
774 * Get (potentially cloaked) hostname of a client to display it to other users.
776 * If the client has not enabled cloaking, the real hostname is used.
778 * @param Client Pointer to client structure
779 * @return Pointer to client hostname
782 Client_HostnameDisplayed(CLIENT *Client)
784 assert(Client != NULL);
786 /* Client isn't cloaked at all, return real hostname: */
787 if (!Client_HasMode(Client, 'x'))
788 return Client_Hostname(Client);
790 /* Use an already saved cloaked hostname, if there is one */
792 return Client->cloaked;
794 Client_UpdateCloakedHostname(Client, NULL, NULL);
795 return Client->cloaked;
799 Client_IPAText(CLIENT *Client)
801 assert(Client != NULL);
803 /* Not a local client? */
804 if (Client_Conn(Client) <= NONE)
807 if (!Client->ipa_text)
808 return Conn_GetIPAInfo(Client_Conn(Client));
810 return Client->ipa_text;
814 * Update (and generate, if necessary) the cloaked hostname of a client.
816 * The newly set cloaked hostname is announced in the network using METADATA
817 * commands to peers that support this feature.
819 * @param Client The client of which the cloaked hostname should be updated.
820 * @param Origin The originator of the hostname change, or NULL if this server.
821 * @param Hostname The new cloaked hostname, or NULL if it should be generated.
824 Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
825 const char *Hostname)
827 char Cloak_Buffer[CLIENT_HOST_LEN];
829 assert(Client != NULL);
831 Origin = Client_ThisServer();
833 if (!Client->cloaked) {
834 Client->cloaked = malloc(CLIENT_HOST_LEN);
835 if (!Client->cloaked)
840 /* Generate new cloaked hostname */
841 if (*Conf_CloakHostModeX) {
842 strlcpy(Cloak_Buffer, Client->host,
843 sizeof(Cloak_Buffer));
844 strlcat(Cloak_Buffer, Conf_CloakHostSalt,
845 sizeof(Cloak_Buffer));
846 snprintf(Client->cloaked, CLIENT_HOST_LEN,
847 Conf_CloakHostModeX, Hash(Cloak_Buffer));
849 strlcpy(Client->cloaked, Client_ID(Client->introducer),
852 strlcpy(Client->cloaked, Hostname, CLIENT_HOST_LEN);
853 LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
854 Client_ID(Client), Client->cloaked);
856 /* Inform other servers in the network */
857 IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
858 "METADATA %s cloakhost :%s",
859 Client_ID(Client), Client->cloaked);
863 Client_Modes( CLIENT *Client )
865 assert( Client != NULL );
866 return Client->modes;
871 Client_Flags( CLIENT *Client )
873 assert( Client != NULL );
874 return Client->flags;
879 Client_Hops( CLIENT *Client )
881 assert( Client != NULL );
887 Client_Token( CLIENT *Client )
889 assert( Client != NULL );
890 return Client->token;
895 Client_MyToken( CLIENT *Client )
897 assert( Client != NULL );
898 return Client->mytoken;
899 } /* Client_MyToken */
903 Client_NextHop( CLIENT *Client )
907 assert( Client != NULL );
910 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
914 } /* Client_NextHop */
918 * Return ID of a client: "client!user@host"
919 * This client ID is used for IRC prefixes, for example.
920 * Please note that this function uses a global static buffer, so you can't
921 * nest invocations without overwriting earlier results!
922 * @param Client Pointer to client structure
923 * @return Pointer to global buffer containing the client ID
926 Client_Mask( CLIENT *Client )
928 static char Mask_Buffer[GETID_LEN];
930 assert (Client != NULL);
932 /* Servers: return name only, there is no "mask" */
933 if (Client->type == CLIENT_SERVER)
936 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
937 Client->id, Client->user, Client->host);
943 * Return ID of a client with cloaked hostname: "client!user@server-name"
945 * This client ID is used for IRC prefixes, for example.
946 * Please note that this function uses a global static buffer, so you can't
947 * nest invocations without overwriting earlier results!
948 * If the client has not enabled cloaking, the real hostname is used.
950 * @param Client Pointer to client structure
951 * @return Pointer to global buffer containing the client ID
954 Client_MaskCloaked(CLIENT *Client)
956 static char Mask_Buffer[GETID_LEN];
958 assert (Client != NULL);
960 /* Is the client using cloaking at all? */
961 if (!Client_HasMode(Client, 'x'))
962 return Client_Mask(Client);
964 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
965 Client_HostnameDisplayed(Client));
968 } /* Client_MaskCloaked */
972 Client_Introducer( CLIENT *Client )
974 assert( Client != NULL );
975 return Client->introducer;
976 } /* Client_Introducer */
980 Client_TopServer( CLIENT *Client )
982 assert( Client != NULL );
983 return Client->topserver;
984 } /* Client_TopServer */
988 Client_HasMode( CLIENT *Client, char Mode )
990 assert( Client != NULL );
991 return strchr( Client->modes, Mode ) != NULL;
992 } /* Client_HasMode */
996 Client_HasFlag( CLIENT *Client, char Flag )
998 assert( Client != NULL );
999 return strchr( Client->flags, Flag ) != NULL;
1000 } /* Client_HasFlag */
1004 Client_Away( CLIENT *Client )
1006 assert( Client != NULL );
1007 return Client->away;
1012 Client_AccountName(CLIENT *Client)
1014 assert(Client != NULL);
1015 return Client->account_name;
1020 * Make sure that a given nickname is valid.
1022 * If the nickname is not valid for the given client, this function sends back
1023 * the appropriate error messages.
1025 * @param Client Client that wants to change the nickname.
1026 * @param Nick New nickname.
1027 * @returns true if nickname is valid, false otherwise.
1030 Client_CheckNick(CLIENT *Client, char *Nick)
1032 assert(Client != NULL);
1033 assert(Nick != NULL);
1035 if (!Client_IsValidNick(Nick)) {
1036 if (strlen(Nick ) >= Conf_MaxNickLength)
1037 IRC_WriteErrClient(Client, ERR_NICKNAMETOOLONG_MSG,
1038 Client_ID(Client), Nick,
1039 Conf_MaxNickLength - 1);
1041 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1042 Client_ID(Client), Nick);
1046 if (Client_Type(Client) != CLIENT_SERVER
1047 && Client_Type(Client) != CLIENT_SERVICE) {
1048 /* Make sure that this isn't a restricted/forbidden nickname */
1049 if (Conf_NickIsBlocked(Nick)) {
1050 IRC_WriteErrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
1051 Client_ID(Client), Nick);
1056 /* Nickname already registered? */
1057 if (Client_Search(Nick)) {
1058 IRC_WriteErrClient(Client, ERR_NICKNAMEINUSE_MSG,
1059 Client_ID(Client), Nick);
1064 } /* Client_CheckNick */
1068 Client_CheckID( CLIENT *Client, char *ID )
1070 char str[COMMAND_LEN];
1073 assert( Client != NULL );
1074 assert( Client->conn_id > NONE );
1075 assert( ID != NULL );
1078 if (strlen(ID) > CLIENT_ID_LEN) {
1079 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1080 Client_ID(Client), ID);
1084 /* ID already in use? */
1087 if (strcasecmp(c->id, ID) == 0) {
1088 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
1089 if (c->conn_id != NONE)
1090 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
1092 Log(LOG_ERR, "%s (via network)!", str);
1093 Conn_Close(Client->conn_id, str, str, true);
1096 c = (CLIENT *)c->next;
1100 } /* Client_CheckID */
1104 Client_First( void )
1107 } /* Client_First */
1111 Client_Next( CLIENT *c )
1113 assert( c != NULL );
1114 return (CLIENT *)c->next;
1119 Client_UserCount( void )
1121 return Count( CLIENT_USER );
1122 } /* Client_UserCount */
1126 Client_ServiceCount( void )
1128 return Count( CLIENT_SERVICE );;
1129 } /* Client_ServiceCount */
1133 Client_ServerCount( void )
1135 return Count( CLIENT_SERVER );
1136 } /* Client_ServerCount */
1140 Client_MyUserCount( void )
1142 return MyCount( CLIENT_USER );
1143 } /* Client_MyUserCount */
1147 Client_MyServiceCount( void )
1149 return MyCount( CLIENT_SERVICE );
1150 } /* Client_MyServiceCount */
1153 GLOBAL unsigned long
1154 Client_MyServerCount( void )
1157 unsigned long cnt = 0;
1162 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
1163 c = (CLIENT *)c->next;
1166 } /* Client_MyServerCount */
1169 GLOBAL unsigned long
1170 Client_OperCount( void )
1173 unsigned long cnt = 0;
1178 if (c && c->type == CLIENT_USER && Client_HasMode(c, 'o' ))
1180 c = (CLIENT *)c->next;
1183 } /* Client_OperCount */
1186 GLOBAL unsigned long
1187 Client_UnknownCount( void )
1190 unsigned long cnt = 0;
1195 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1196 c = (CLIENT *)c->next;
1200 } /* Client_UnknownCount */
1204 Client_MaxUserCount( void )
1207 } /* Client_MaxUserCount */
1211 Client_MyMaxUserCount( void )
1213 return My_Max_Users;
1214 } /* Client_MyMaxUserCount */
1218 * Check that a given nickname is valid.
1220 * @param Nick the nickname to check.
1221 * @returns true if nickname is valid, false otherwise.
1224 Client_IsValidNick(const char *Nick)
1227 static const char goodchars[] = ";0123456789-";
1229 assert (Nick != NULL);
1231 if (strchr(goodchars, Nick[0]))
1233 if (strlen(Nick ) >= Conf_MaxNickLength)
1238 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1246 } /* Client_IsValidNick */
1250 * Return pointer to "My_Whowas" structure.
1253 Client_GetWhowas( void )
1256 } /* Client_GetWhowas */
1259 * Return the index of the last used WHOWAS entry.
1262 Client_GetLastWhowasIndex( void )
1265 } /* Client_GetLastWhowasIndex */
1269 * Get the start time of this client.
1270 * The result is the start time in seconds since 1970-01-01, as reported
1271 * by the C function time(NULL).
1274 Client_StartTime(CLIENT *Client)
1276 assert( Client != NULL );
1277 return Client->starttime;
1278 } /* Client_Uptime */
1282 * Reject a client when logging in.
1284 * This function is called when a client isn't allowed to connect to this
1285 * server. Possible reasons are bad server password, bad PAM password,
1286 * or that the client is G/K-Line'd.
1288 * After calling this function, the client isn't connected any more.
1290 * @param Client The client to reject.
1291 * @param Reason The reason why the client has been rejected.
1292 * @param InformClient If true, send the exact reason to the client.
1295 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1297 char info[COMMAND_LEN];
1299 assert(Client != NULL);
1300 assert(Reason != NULL);
1303 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1305 strcpy(info, "Access denied: Bad password?");
1308 "User \"%s\" rejected (connection %d): %s!",
1309 Client_Mask(Client), Client_Conn(Client), Reason);
1310 Conn_Close(Client_Conn(Client), Reason, info, true);
1315 * Introduce a new user or service client in the network.
1317 * @param From Remote server introducing the client or NULL (local).
1318 * @param Client New client.
1319 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1322 Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
1324 /* Set client type (user or service) */
1325 Client_SetType(Client, Type);
1328 if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
1330 Client_SetType(Client, CLIENT_SERVICE);
1331 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1332 Client_TypeText(Client), Client_Mask(Client),
1333 Client_Modes(Client), Client_ID(From),
1334 Client_ID(Client_Introducer(Client)),
1335 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1337 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1338 Client_TypeText(Client), Client_Mask(Client),
1339 Client_Conn(Client));
1340 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1341 Client_ID(Client), Client_User(Client),
1342 Client_Hostname(Client),
1343 Conn_IPA(Client_Conn(Client)),
1344 Client_TypeText(Client));
1347 /* Inform other servers */
1348 IRC_WriteStrServersPrefixFlag_CB(From,
1349 From != NULL ? From : Client_ThisServer(),
1350 '\0', cb_introduceClient, (void *)Client);
1351 } /* Client_Introduce */
1354 static unsigned long
1355 Count( CLIENT_TYPE Type )
1358 unsigned long cnt = 0;
1363 if( c->type == Type ) cnt++;
1364 c = (CLIENT *)c->next;
1370 static unsigned long
1371 MyCount( CLIENT_TYPE Type )
1374 unsigned long cnt = 0;
1379 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1380 c = (CLIENT *)c->next;
1387 * Allocate and initialize new CLIENT strcuture.
1389 * @return Pointer to CLIENT structure or NULL on error.
1392 New_Client_Struct( void )
1396 c = (CLIENT *)malloc( sizeof( CLIENT ));
1399 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1403 memset( c, 0, sizeof ( CLIENT ));
1405 c->type = CLIENT_UNKNOWN;
1415 * Free a CLIENT structure and its member variables.
1418 Free_Client(CLIENT **Client)
1420 assert(Client != NULL);
1421 assert(*Client != NULL);
1423 if ((*Client)->account_name)
1424 free((*Client)->account_name);
1425 if ((*Client)->away)
1426 free((*Client)->away);
1427 if ((*Client)->cloaked)
1428 free((*Client)->cloaked);
1429 if ((*Client)->ipa_text)
1430 free((*Client)->ipa_text);
1437 Generate_MyToken( CLIENT *Client )
1446 if( c->mytoken == token )
1448 /* The token is already in use */
1453 else c = (CLIENT *)c->next;
1455 Client->mytoken = token;
1456 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1457 } /* Generate_MyToken */
1461 Adjust_Counters( CLIENT *Client )
1465 assert( Client != NULL );
1467 if( Client->type != CLIENT_USER ) return;
1469 if( Client->conn_id != NONE )
1471 /* Local connection */
1472 count = Client_MyUserCount( );
1473 if( count > My_Max_Users ) My_Max_Users = count;
1475 count = Client_UserCount( );
1476 if( count > Max_Users ) Max_Users = count;
1477 } /* Adjust_Counters */
1481 * Register client in My_Whowas structure for further recall by WHOWAS.
1482 * Note: Only clients that have been connected at least 30 seconds will be
1483 * registered to prevent automated IRC bots to "destroy" a nice server
1487 Client_RegisterWhowas( CLIENT *Client )
1492 assert( Client != NULL );
1494 /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1495 if (Conf_MorePrivacy)
1499 /* Don't register clients that were connected less than 30 seconds. */
1500 if( now - Client->starttime < 30 )
1503 slot = Last_Whowas + 1;
1504 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1507 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1510 My_Whowas[slot].time = now;
1511 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1512 sizeof( My_Whowas[slot].id ));
1513 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1514 sizeof( My_Whowas[slot].user ));
1515 strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
1516 sizeof( My_Whowas[slot].host ));
1517 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1518 sizeof( My_Whowas[slot].info ));
1519 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1520 sizeof( My_Whowas[slot].server ));
1523 } /* Client_RegisterWhowas */
1527 Client_TypeText(CLIENT *Client)
1529 assert(Client != NULL);
1530 switch (Client_Type(Client)) {
1534 case CLIENT_SERVICE:
1543 } /* Client_TypeText */
1547 * Destroy user or service client.
1550 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1552 if(Client->conn_id != NONE) {
1553 /* Local (directly connected) client */
1555 "%s \"%s\" unregistered (connection %d): %s.",
1556 Client_TypeText(Client), Client_Mask(Client),
1557 Client->conn_id, Txt);
1558 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1559 Client_ID(Client), Client_User(Client),
1560 Client_Hostname(Client), Txt);
1563 /* Inforam all the other servers */
1565 IRC_WriteStrServersPrefix(NULL,
1566 Client, "QUIT :%s", FwdMsg );
1568 IRC_WriteStrServersPrefix(NULL,
1573 LogDebug("%s \"%s\" unregistered: %s.",
1574 Client_TypeText(Client), Client_Mask(Client), Txt);
1577 /* Inform all the other servers, but the ones in the
1578 * direction we got the QUIT from */
1580 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1581 Client, "QUIT :%s", FwdMsg );
1583 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1588 /* Unregister client from channels */
1589 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1591 /* Register client in My_Whowas structure */
1592 Client_RegisterWhowas(Client);
1593 } /* Destroy_UserOrService */
1597 * Introduce a new user or service client to a remote server.
1599 * @param To The remote server to inform.
1600 * @param Prefix Prefix for the generated commands.
1601 * @param data CLIENT structure of the new client.
1604 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1606 CLIENT *c = (CLIENT *)data;
1608 (void)Client_Announce(To, Prefix, c);
1610 } /* cb_introduceClient */
1614 * Announce an user or service to a server.
1616 * This function differentiates between RFC1459 and RFC2813 server links and
1617 * generates the appropriate commands to register the user or service.
1619 * @param Client Server
1620 * @param Prefix Prefix for the generated commands
1621 * @param User User to announce
1624 Client_Announce(CLIENT * Client, CLIENT * Prefix, CLIENT * User)
1627 char *modes, *user, *host;
1629 modes = Client_Modes(User);
1630 user = Client_User(User) ? Client_User(User) : "-";
1631 host = Client_Hostname(User) ? Client_Hostname(User) : "-";
1633 conn = Client_Conn(Client);
1634 if (Conn_Options(conn) & CONN_RFC1459) {
1635 /* RFC 1459 mode: separate NICK and USER commands */
1636 if (! Conn_WriteStr(conn, "NICK %s :%d",
1637 Client_ID(User), Client_Hops(User) + 1))
1638 return DISCONNECTED;
1639 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1640 Client_ID(User), user, host,
1641 Client_ID(Client_Introducer(User)),
1643 return DISCONNECTED;
1645 if (! Conn_WriteStr(conn, ":%s MODE %s +%s",
1646 Client_ID(User), Client_ID(User),
1648 return DISCONNECTED;
1651 /* RFC 2813 mode: one combined NICK or SERVICE command */
1652 if (Client_Type(User) == CLIENT_SERVICE
1653 && Client_HasFlag(Client, 'S')) {
1654 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1655 "SERVICE %s %d * +%s %d :%s",
1657 Client_MyToken(Client_Introducer(User)),
1658 modes, Client_Hops(User) + 1,
1660 return DISCONNECTED;
1662 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1663 "NICK %s %d %s %s %d +%s :%s",
1664 Client_ID(User), Client_Hops(User) + 1,
1666 Client_MyToken(Client_Introducer(User)),
1667 modes, Client_Info(User)))
1668 return DISCONNECTED;
1672 if (Client_HasFlag(Client, 'M')) {
1673 /* Synchronize metadata */
1674 if (Client_HostnameCloaked(User)) {
1675 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1676 "METADATA %s cloakhost :%s",
1678 Client_HostnameCloaked(User)))
1679 return DISCONNECTED;
1682 if (Client_AccountName(User)) {
1683 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1684 "METADATA %s accountname :%s",
1686 Client_AccountName(User)))
1687 return DISCONNECTED;
1690 if (Conn_GetCertFp(Client_Conn(User))) {
1691 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1692 "METADATA %s certfp :%s",
1694 Conn_GetCertFp(Client_Conn(User))))
1695 return DISCONNECTED;
1700 } /* Client_Announce */
1706 Client_DebugDump(void)
1710 Log(LOG_DEBUG, "Client status:");
1714 " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1715 Client_ID(c), Client_Type(c), Client_Hostname(c),
1716 Client_User(c), Client_Conn(c), Client_StartTime(c),
1718 c = (CLIENT *)c->next;
1720 } /* Client_DumpClients */