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);
134 if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
139 Client_ThisServer( void )
142 } /* Client_ThisServer */
146 * Initialize new local client; wrapper function for Init_New_Client().
147 * @return New CLIENT structure.
150 Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
152 return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
153 Hostname, NULL, 0, 0, NULL, Idented);
154 } /* Client_NewLocal */
158 * Initialize new remote server; wrapper function for Init_New_Client().
159 * @return New CLIENT structure.
162 Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
163 int Hops, int Token, const char *Info, bool Idented)
165 return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
166 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
167 } /* Client_NewRemoteServer */
171 * Initialize new remote client; wrapper function for Init_New_Client().
172 * @return New CLIENT structure.
175 Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
176 const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
178 return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
179 User, Hostname, Info, Hops, Token, Modes, Idented);
180 } /* Client_NewRemoteUser */
184 * Initialize new client and set up the given parameters like client type,
185 * user name, host name, introducing server etc. ...
186 * @return New CLIENT structure.
189 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
190 int Type, const char *ID, const char *User, const char *Hostname,
191 const char *Info, int Hops, int Token, const char *Modes, bool Idented)
196 assert(Introducer != NULL);
198 client = New_Client_Struct();
202 client->starttime = time(NULL);
203 client->conn_id = Idx;
204 client->introducer = Introducer;
205 client->topserver = TopServer;
208 Client_SetID(client, ID);
210 Client_SetUser(client, User, Idented);
211 Client_SetOrigUser(client, User);
214 Client_SetHostname(client, Hostname);
216 Client_SetInfo(client, Info);
218 client->token = Token;
220 Client_SetModes(client, Modes);
221 if (Type == CLIENT_SERVER)
222 Generate_MyToken(client);
224 if (Client_HasMode(client, 'a'))
225 strlcpy(client->away, DEFAULT_AWAY_MSG, sizeof(client->away));
227 client->next = (POINTER *)My_Clients;
230 Adjust_Counters(client);
233 } /* Init_New_Client */
237 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
239 /* remove a client */
245 assert( Client != NULL );
247 txt = LogMsg ? LogMsg : FwdMsg;
249 txt = "Reason unknown";
251 /* netsplit message */
252 if( Client->type == CLIENT_SERVER ) {
253 strlcpy(msg, This_Server->id, sizeof (msg));
254 strlcat(msg, " ", sizeof (msg));
255 strlcat(msg, Client->id, sizeof (msg));
262 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
265 * The client that is about to be removed is a server,
266 * the client we are checking right now is a child of that
267 * server and thus has to be removed, too.
269 * Call Client_Destroy() recursively with the server as the
270 * new "object to be removed". This starts the cycle again, until
271 * all servers that are linked via the original server have been
274 Client_Destroy( c, NULL, msg, false );
281 /* found the client: remove it */
282 if( last ) last->next = c->next;
283 else My_Clients = (CLIENT *)c->next;
285 if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
286 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
287 else if( c->type == CLIENT_SERVER )
289 if (c != This_Server) {
290 if (c->conn_id != NONE)
291 Log(LOG_NOTICE|LOG_snotice,
292 "Server \"%s\" unregistered (connection %d): %s.",
293 c->id, c->conn_id, txt);
295 Log(LOG_NOTICE|LOG_snotice,
296 "Server \"%s\" unregistered: %s.",
300 /* inform other servers */
301 if( ! NGIRCd_SignalQuit )
303 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
304 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
309 if (c->conn_id != NONE) {
312 "Client \"%s\" unregistered (connection %d): %s.",
313 c->id, c->conn_id, txt);
316 "Client unregistered (connection %d): %s.",
320 "Unregistered unknown client \"%s\": %s",
321 c->id[0] ? c->id : "(No Nick)", txt);
326 free(c->account_name);
333 c = (CLIENT *)c->next;
335 } /* Client_Destroy */
339 * Set client hostname.
341 * If global hostname cloaking is in effect, don't set the real hostname
342 * but the configured one.
344 * @param Client The client of which the hostname should be set.
345 * @param Hostname The new hostname.
348 Client_SetHostname( CLIENT *Client, const char *Hostname )
350 assert(Client != NULL);
351 assert(Hostname != NULL);
353 if (strlen(Conf_CloakHost)) {
354 char cloak[GETID_LEN];
356 strlcpy(cloak, Hostname, GETID_LEN);
357 strlcat(cloak, Conf_CloakHostSalt, GETID_LEN);
358 snprintf(cloak, GETID_LEN, Conf_CloakHost, Hash(cloak));
360 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
361 Client_ID(Client), Client->host, cloak);
362 strlcpy(Client->host, cloak, sizeof(Client->host));
364 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
365 Client_ID(Client), Client->host, Hostname);
366 strlcpy(Client->host, Hostname, sizeof(Client->host));
368 } /* Client_SetHostname */
372 Client_SetID( CLIENT *Client, const char *ID )
374 assert( Client != NULL );
375 assert( ID != NULL );
377 strlcpy( Client->id, ID, sizeof( Client->id ));
379 if (Conf_CloakUserToNick) {
380 strlcpy( Client->user, ID, sizeof( Client->user ));
381 strlcpy( Client->info, ID, sizeof( Client->info ));
385 Client->hash = Hash( Client->id );
390 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
392 /* set clients username */
394 assert( Client != NULL );
395 assert( User != NULL );
397 if (Conf_CloakUserToNick) {
398 strlcpy(Client->user, Client->id, sizeof(Client->user));
399 } else if (Idented) {
400 strlcpy(Client->user, User, sizeof(Client->user));
402 Client->user[0] = '~';
403 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
405 } /* Client_SetUser */
409 * Set "original" user name of a client.
410 * This function saves the "original" user name, the user name specified by
411 * the peer using the USER command, into the CLIENT structure. This user
412 * name may be used for authentication, for example.
413 * @param Client The client.
414 * @param User User name to set.
417 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
419 assert(Client != NULL);
420 assert(User != NULL);
422 #if defined(PAM) && defined(IDENTAUTH)
423 strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
425 } /* Client_SetOrigUser */
429 Client_SetInfo( CLIENT *Client, const char *Info )
431 /* set client hostname */
433 assert( Client != NULL );
434 assert( Info != NULL );
436 if (Conf_CloakUserToNick)
437 strlcpy(Client->info, Client->id, sizeof(Client->info));
439 strlcpy(Client->info, Info, sizeof(Client->info));
440 } /* Client_SetInfo */
444 Client_SetModes( CLIENT *Client, const char *Modes )
446 assert( Client != NULL );
447 assert( Modes != NULL );
449 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
450 } /* Client_SetModes */
454 Client_SetFlags( CLIENT *Client, const char *Flags )
456 assert( Client != NULL );
457 assert( Flags != NULL );
459 strlcpy(Client->flags, Flags, sizeof(Client->flags));
460 } /* Client_SetFlags */
464 Client_SetAccountName(CLIENT *Client, const char *AccountName)
466 assert(Client != NULL);
468 if (Client->account_name)
469 free(Client->account_name);
472 Client->account_name = strndup(AccountName,
473 CLIENT_NICK_LEN - 1);
475 Client->account_name = NULL;
480 Client_SetAway( CLIENT *Client, const char *Txt )
482 /* Set AWAY reason of client */
484 assert( Client != NULL );
485 assert( Txt != NULL );
487 strlcpy( Client->away, Txt, sizeof( Client->away ));
488 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
489 Client_Mask(Client), Txt);
490 } /* Client_SetAway */
494 Client_SetType( CLIENT *Client, int Type )
496 assert( Client != NULL );
498 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
499 Adjust_Counters( Client );
500 } /* Client_SetType */
504 Client_SetHops( CLIENT *Client, int Hops )
506 assert( Client != NULL );
508 } /* Client_SetHops */
512 Client_SetToken( CLIENT *Client, int Token )
514 assert( Client != NULL );
515 Client->token = Token;
516 } /* Client_SetToken */
520 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
522 assert( Client != NULL );
523 assert( Introducer != NULL );
524 Client->introducer = Introducer;
525 } /* Client_SetIntroducer */
529 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
531 assert( Client != NULL );
532 Client->oper_by_me = OperByMe;
533 } /* Client_SetOperByMe */
537 Client_ModeAdd( CLIENT *Client, char Mode )
540 * If Client already had Mode, return false.
541 * If the Mode was newly set, return true.
546 assert( Client != NULL );
548 x[0] = Mode; x[1] = '\0';
549 if (!Client_HasMode(Client, x[0])) {
550 strlcat( Client->modes, x, sizeof( Client->modes ));
554 } /* Client_ModeAdd */
558 Client_ModeDel( CLIENT *Client, char Mode )
561 * If Mode was removed, return true.
562 * If Client did not have Mode, return false.
567 assert( Client != NULL );
569 x[0] = Mode; x[1] = '\0';
571 p = strchr( Client->modes, x[0] );
572 if( ! p ) return false;
574 /* Client has Mode -> delete */
581 } /* Client_ModeDel */
585 * Search CLIENT structure of a given nick name.
587 * @return Pointer to CLIENT structure or NULL if not found.
590 Client_Search( const char *Nick )
592 char search_id[CLIENT_ID_LEN], *ptr;
596 assert( Nick != NULL );
598 /* copy Nick and truncate hostmask if necessary */
599 strlcpy( search_id, Nick, sizeof( search_id ));
600 ptr = strchr( search_id, '!' );
601 if( ptr ) *ptr = '\0';
603 search_hash = Hash(search_id);
607 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
609 c = (CLIENT *)c->next;
616 * Search first CLIENT structure matching a given mask of a server.
618 * The order of servers is arbitrary, but this function makes sure that the
619 * local server is always returned if the mask matches it.
621 * @return Pointer to CLIENT structure or NULL if no server could be found.
624 Client_SearchServer(const char *Mask)
628 assert(Mask != NULL);
630 /* First check if mask matches the local server */
631 if (MatchCaseInsensitive(Mask, Client_ID(Client_ThisServer())))
632 return Client_ThisServer();
636 if (Client_Type(c) == CLIENT_SERVER) {
637 /* This is a server: check if Mask matches */
638 if (MatchCaseInsensitive(Mask, c->id))
641 c = (CLIENT *)c->next;
648 * Get client structure ("introducer") identfied by a server token.
649 * @return CLIENT structure or NULL if none could be found.
652 Client_GetFromToken( CLIENT *Client, int Token )
656 assert( Client != NULL );
663 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
666 c = (CLIENT *)c->next;
669 } /* Client_GetFromToken */
673 Client_Type( CLIENT *Client )
675 assert( Client != NULL );
681 Client_Conn( CLIENT *Client )
683 assert( Client != NULL );
684 return Client->conn_id;
689 Client_ID( CLIENT *Client )
691 assert( Client != NULL );
694 if(Client->type == CLIENT_USER)
695 assert(strlen(Client->id) < Conf_MaxNickLength);
698 if( Client->id[0] ) return Client->id;
704 Client_Info( CLIENT *Client )
706 assert( Client != NULL );
712 Client_User( CLIENT *Client )
714 assert( Client != NULL );
715 return Client->user[0] ? Client->user : "~";
722 * Get the "original" user name as supplied by the USER command.
723 * The user name as given by the client is used for authentication instead
724 * of the one detected using IDENT requests.
725 * @param Client The client.
726 * @return Original user name.
729 Client_OrigUser(CLIENT *Client) {
731 char *user = Client->user;
737 return Client->orig_user;
739 } /* Client_OrigUser */
744 * Return the hostname of a client.
745 * @param Client Pointer to client structure
746 * @return Pointer to client hostname
749 Client_Hostname(CLIENT *Client)
751 assert (Client != NULL);
756 * Return the cloaked hostname of a client, if set.
757 * @param Client Pointer to the client structure.
758 * @return Pointer to the cloaked hostname or NULL if not set.
761 Client_HostnameCloaked(CLIENT *Client)
763 assert(Client != NULL);
764 return Client->cloaked;
768 * Get (potentially cloaked) hostname of a client to display it to other users.
770 * If the client has not enabled cloaking, the real hostname is used.
772 * @param Client Pointer to client structure
773 * @return Pointer to client hostname
776 Client_HostnameDisplayed(CLIENT *Client)
778 assert(Client != NULL);
780 /* Client isn't cloaked at all, return real hostname: */
781 if (!Client_HasMode(Client, 'x'))
782 return Client_Hostname(Client);
784 /* Use an already saved cloaked hostname, if there is one */
786 return Client->cloaked;
788 Client_UpdateCloakedHostname(Client, NULL, NULL);
789 return Client->cloaked;
793 * Update (and generate, if necessary) the cloaked hostname of a client.
795 * The newly set cloaked hostname is announced in the network using METADATA
796 * commands to peers that support this feature.
798 * @param Client The client of which the cloaked hostname should be updated.
799 * @param Origin The originator of the hostname change, or NULL if this server.
800 * @param Hostname The new cloaked hostname, or NULL if it should be generated.
803 Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
804 const char *Hostname)
806 char Cloak_Buffer[CLIENT_HOST_LEN];
808 assert(Client != NULL);
810 Origin = Client_ThisServer();
812 if (!Client->cloaked) {
813 Client->cloaked = malloc(CLIENT_HOST_LEN);
814 if (!Client->cloaked)
819 /* Generate new cloaked hostname */
820 if (*Conf_CloakHostModeX) {
821 strlcpy(Cloak_Buffer, Client->host,
822 sizeof(Cloak_Buffer));
823 strlcat(Cloak_Buffer, Conf_CloakHostSalt,
824 sizeof(Cloak_Buffer));
825 snprintf(Client->cloaked, CLIENT_HOST_LEN,
826 Conf_CloakHostModeX, Hash(Cloak_Buffer));
828 strlcpy(Client->cloaked, Client_ID(Client->introducer),
831 strlcpy(Client->cloaked, Hostname, CLIENT_HOST_LEN);
832 LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
833 Client_ID(Client), Client->cloaked);
835 /* Inform other servers in the network */
836 IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
837 "METADATA %s cloakhost :%s",
838 Client_ID(Client), Client->cloaked);
842 Client_Modes( CLIENT *Client )
844 assert( Client != NULL );
845 return Client->modes;
850 Client_Flags( CLIENT *Client )
852 assert( Client != NULL );
853 return Client->flags;
858 Client_OperByMe( CLIENT *Client )
860 assert( Client != NULL );
861 return Client->oper_by_me;
862 } /* Client_OperByMe */
866 Client_Hops( CLIENT *Client )
868 assert( Client != NULL );
874 Client_Token( CLIENT *Client )
876 assert( Client != NULL );
877 return Client->token;
882 Client_MyToken( CLIENT *Client )
884 assert( Client != NULL );
885 return Client->mytoken;
886 } /* Client_MyToken */
890 Client_NextHop( CLIENT *Client )
894 assert( Client != NULL );
897 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
901 } /* Client_NextHop */
905 * Return ID of a client: "client!user@host"
906 * This client ID is used for IRC prefixes, for example.
907 * Please note that this function uses a global static buffer, so you can't
908 * nest invocations without overwriting earlier results!
909 * @param Client Pointer to client structure
910 * @return Pointer to global buffer containing the client ID
913 Client_Mask( CLIENT *Client )
915 static char Mask_Buffer[GETID_LEN];
917 assert (Client != NULL);
919 /* Servers: return name only, there is no "mask" */
920 if (Client->type == CLIENT_SERVER)
923 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
924 Client->id, Client->user, Client->host);
930 * Return ID of a client with cloaked hostname: "client!user@server-name"
932 * This client ID is used for IRC prefixes, for example.
933 * Please note that this function uses a global static buffer, so you can't
934 * nest invocations without overwriting earlier results!
935 * If the client has not enabled cloaking, the real hostname is used.
937 * @param Client Pointer to client structure
938 * @return Pointer to global buffer containing the client ID
941 Client_MaskCloaked(CLIENT *Client)
943 static char Mask_Buffer[GETID_LEN];
945 assert (Client != NULL);
947 /* Is the client using cloaking at all? */
948 if (!Client_HasMode(Client, 'x'))
949 return Client_Mask(Client);
951 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
952 Client_HostnameDisplayed(Client));
955 } /* Client_MaskCloaked */
959 Client_Introducer( CLIENT *Client )
961 assert( Client != NULL );
962 return Client->introducer;
963 } /* Client_Introducer */
967 Client_TopServer( CLIENT *Client )
969 assert( Client != NULL );
970 return Client->topserver;
971 } /* Client_TopServer */
975 Client_HasMode( CLIENT *Client, char Mode )
977 assert( Client != NULL );
978 return strchr( Client->modes, Mode ) != NULL;
979 } /* Client_HasMode */
983 Client_HasFlag( CLIENT *Client, char Flag )
985 assert( Client != NULL );
986 return strchr( Client->flags, Flag ) != NULL;
987 } /* Client_HasFlag */
991 Client_Away( CLIENT *Client )
993 assert( Client != NULL );
999 Client_AccountName(CLIENT *Client)
1001 assert(Client != NULL);
1002 return Client->account_name;
1007 * Make sure that a given nickname is valid.
1009 * If the nickname is not valid for the given client, this function sends back
1010 * the appropriate error messages.
1012 * @param Client Client that wants to change the nickname.
1013 * @param Nick New nickname.
1014 * @returns true if nickname is valid, false otherwise.
1017 Client_CheckNick(CLIENT *Client, char *Nick)
1019 assert(Client != NULL);
1020 assert(Nick != NULL);
1022 if (!Client_IsValidNick(Nick)) {
1023 if (strlen(Nick ) >= Conf_MaxNickLength)
1024 IRC_WriteErrClient(Client, ERR_NICKNAMETOOLONG_MSG,
1025 Client_ID(Client), Nick,
1026 Conf_MaxNickLength - 1);
1028 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1029 Client_ID(Client), Nick);
1033 if (Client_Type(Client) != CLIENT_SERVER
1034 && Client_Type(Client) != CLIENT_SERVICE) {
1035 /* Make sure that this isn't a restricted/forbidden nickname */
1036 if (Conf_NickIsBlocked(Nick)) {
1037 IRC_WriteErrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
1038 Client_ID(Client), Nick);
1043 /* Nickname already registered? */
1044 if (Client_Search(Nick)) {
1045 IRC_WriteErrClient(Client, ERR_NICKNAMEINUSE_MSG,
1046 Client_ID(Client), Nick);
1051 } /* Client_CheckNick */
1055 Client_CheckID( CLIENT *Client, char *ID )
1057 char str[COMMAND_LEN];
1060 assert( Client != NULL );
1061 assert( Client->conn_id > NONE );
1062 assert( ID != NULL );
1065 if (strlen(ID) > CLIENT_ID_LEN) {
1066 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1067 Client_ID(Client), ID);
1071 /* ID already in use? */
1074 if (strcasecmp(c->id, ID) == 0) {
1075 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
1076 if (c->conn_id != NONE)
1077 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
1079 Log(LOG_ERR, "%s (via network)!", str);
1080 Conn_Close(Client->conn_id, str, str, true);
1083 c = (CLIENT *)c->next;
1087 } /* Client_CheckID */
1091 Client_First( void )
1094 } /* Client_First */
1098 Client_Next( CLIENT *c )
1100 assert( c != NULL );
1101 return (CLIENT *)c->next;
1106 Client_UserCount( void )
1108 return Count( CLIENT_USER );
1109 } /* Client_UserCount */
1113 Client_ServiceCount( void )
1115 return Count( CLIENT_SERVICE );;
1116 } /* Client_ServiceCount */
1120 Client_ServerCount( void )
1122 return Count( CLIENT_SERVER );
1123 } /* Client_ServerCount */
1127 Client_MyUserCount( void )
1129 return MyCount( CLIENT_USER );
1130 } /* Client_MyUserCount */
1134 Client_MyServiceCount( void )
1136 return MyCount( CLIENT_SERVICE );
1137 } /* Client_MyServiceCount */
1140 GLOBAL unsigned long
1141 Client_MyServerCount( void )
1144 unsigned long cnt = 0;
1149 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
1150 c = (CLIENT *)c->next;
1153 } /* Client_MyServerCount */
1156 GLOBAL unsigned long
1157 Client_OperCount( void )
1160 unsigned long cnt = 0;
1165 if (c && c->type == CLIENT_USER && Client_HasMode(c, 'o' ))
1167 c = (CLIENT *)c->next;
1170 } /* Client_OperCount */
1173 GLOBAL unsigned long
1174 Client_UnknownCount( void )
1177 unsigned long cnt = 0;
1182 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1183 c = (CLIENT *)c->next;
1187 } /* Client_UnknownCount */
1191 Client_MaxUserCount( void )
1194 } /* Client_MaxUserCount */
1198 Client_MyMaxUserCount( void )
1200 return My_Max_Users;
1201 } /* Client_MyMaxUserCount */
1205 * Check that a given nickname is valid.
1207 * @param Nick the nickname to check.
1208 * @returns true if nickname is valid, false otherwise.
1211 Client_IsValidNick(const char *Nick)
1214 static const char goodchars[] = ";0123456789-";
1216 assert (Nick != NULL);
1218 if (strchr(goodchars, Nick[0]))
1220 if (strlen(Nick ) >= Conf_MaxNickLength)
1225 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1233 } /* Client_IsValidNick */
1237 * Return pointer to "My_Whowas" structure.
1240 Client_GetWhowas( void )
1243 } /* Client_GetWhowas */
1246 * Return the index of the last used WHOWAS entry.
1249 Client_GetLastWhowasIndex( void )
1252 } /* Client_GetLastWhowasIndex */
1256 * Get the start time of this client.
1257 * The result is the start time in seconds since 1970-01-01, as reported
1258 * by the C function time(NULL).
1261 Client_StartTime(CLIENT *Client)
1263 assert( Client != NULL );
1264 return Client->starttime;
1265 } /* Client_Uptime */
1269 * Reject a client when logging in.
1271 * This function is called when a client isn't allowed to connect to this
1272 * server. Possible reasons are bad server password, bad PAM password,
1273 * or that the client is G/K-Line'd.
1275 * After calling this function, the client isn't connected any more.
1277 * @param Client The client to reject.
1278 * @param Reason The reason why the client has been rejected.
1279 * @param InformClient If true, send the exact reason to the client.
1282 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1284 char info[COMMAND_LEN];
1286 assert(Client != NULL);
1287 assert(Reason != NULL);
1290 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1292 strcpy(info, "Access denied: Bad password?");
1295 "User \"%s\" rejected (connection %d): %s!",
1296 Client_Mask(Client), Client_Conn(Client), Reason);
1297 Conn_Close(Client_Conn(Client), Reason, info, true);
1302 * Introduce a new user or service client in the network.
1304 * @param From Remote server introducing the client or NULL (local).
1305 * @param Client New client.
1306 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1309 Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
1311 /* Set client type (user or service) */
1312 Client_SetType(Client, Type);
1315 if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
1317 Client_SetType(Client, CLIENT_SERVICE);
1318 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1319 Client_TypeText(Client), Client_Mask(Client),
1320 Client_Modes(Client), Client_ID(From),
1321 Client_ID(Client_Introducer(Client)),
1322 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1324 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1325 Client_TypeText(Client), Client_Mask(Client),
1326 Client_Conn(Client));
1327 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1328 Client_ID(Client), Client_User(Client),
1329 Client_Hostname(Client),
1330 Conn_IPA(Client_Conn(Client)),
1331 Client_TypeText(Client));
1334 /* Inform other servers */
1335 IRC_WriteStrServersPrefixFlag_CB(From,
1336 From != NULL ? From : Client_ThisServer(),
1337 '\0', cb_introduceClient, (void *)Client);
1338 } /* Client_Introduce */
1341 static unsigned long
1342 Count( CLIENT_TYPE Type )
1345 unsigned long cnt = 0;
1350 if( c->type == Type ) cnt++;
1351 c = (CLIENT *)c->next;
1357 static unsigned long
1358 MyCount( CLIENT_TYPE Type )
1361 unsigned long cnt = 0;
1366 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1367 c = (CLIENT *)c->next;
1374 New_Client_Struct( void )
1378 c = (CLIENT *)malloc( sizeof( CLIENT ));
1381 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1385 memset( c, 0, sizeof ( CLIENT ));
1387 c->type = CLIENT_UNKNOWN;
1389 c->oper_by_me = false;
1399 Generate_MyToken( CLIENT *Client )
1408 if( c->mytoken == token )
1410 /* The token is already in use */
1415 else c = (CLIENT *)c->next;
1417 Client->mytoken = token;
1418 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1419 } /* Generate_MyToken */
1423 Adjust_Counters( CLIENT *Client )
1427 assert( Client != NULL );
1429 if( Client->type != CLIENT_USER ) return;
1431 if( Client->conn_id != NONE )
1433 /* Local connection */
1434 count = Client_MyUserCount( );
1435 if( count > My_Max_Users ) My_Max_Users = count;
1437 count = Client_UserCount( );
1438 if( count > Max_Users ) Max_Users = count;
1439 } /* Adjust_Counters */
1443 * Register client in My_Whowas structure for further recall by WHOWAS.
1444 * Note: Only clients that have been connected at least 30 seconds will be
1445 * registered to prevent automated IRC bots to "destroy" a nice server
1449 Client_RegisterWhowas( CLIENT *Client )
1454 assert( Client != NULL );
1456 /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1457 if (Conf_MorePrivacy)
1461 /* Don't register clients that were connected less than 30 seconds. */
1462 if( now - Client->starttime < 30 )
1465 slot = Last_Whowas + 1;
1466 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1469 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1472 My_Whowas[slot].time = now;
1473 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1474 sizeof( My_Whowas[slot].id ));
1475 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1476 sizeof( My_Whowas[slot].user ));
1477 strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
1478 sizeof( My_Whowas[slot].host ));
1479 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1480 sizeof( My_Whowas[slot].info ));
1481 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1482 sizeof( My_Whowas[slot].server ));
1485 } /* Client_RegisterWhowas */
1489 Client_TypeText(CLIENT *Client)
1491 assert(Client != NULL);
1492 switch (Client_Type(Client)) {
1496 case CLIENT_SERVICE:
1505 } /* Client_TypeText */
1509 * Destroy user or service client.
1512 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1514 if(Client->conn_id != NONE) {
1515 /* Local (directly connected) client */
1517 "%s \"%s\" unregistered (connection %d): %s.",
1518 Client_TypeText(Client), Client_Mask(Client),
1519 Client->conn_id, Txt);
1520 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1521 Client_ID(Client), Client_User(Client),
1522 Client_Hostname(Client), Txt);
1525 /* Inforam all the other servers */
1527 IRC_WriteStrServersPrefix(NULL,
1528 Client, "QUIT :%s", FwdMsg );
1530 IRC_WriteStrServersPrefix(NULL,
1535 LogDebug("%s \"%s\" unregistered: %s.",
1536 Client_TypeText(Client), Client_Mask(Client), Txt);
1539 /* Inform all the other servers, but the ones in the
1540 * direction we got the QUIT from */
1542 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1543 Client, "QUIT :%s", FwdMsg );
1545 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1550 /* Unregister client from channels */
1551 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1553 /* Register client in My_Whowas structure */
1554 Client_RegisterWhowas(Client);
1555 } /* Destroy_UserOrService */
1559 * Introduce a new user or service client to a remote server.
1561 * @param To The remote server to inform.
1562 * @param Prefix Prefix for the generated commands.
1563 * @param data CLIENT structure of the new client.
1566 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1568 CLIENT *c = (CLIENT *)data;
1570 (void)Client_Announce(To, Prefix, c);
1572 } /* cb_introduceClient */
1576 * Announce an user or service to a server.
1578 * This function differentiates between RFC1459 and RFC2813 server links and
1579 * generates the appropriate commands to register the user or service.
1581 * @param Client Server
1582 * @param Prefix Prefix for the generated commands
1583 * @param User User to announce
1586 Client_Announce(CLIENT * Client, CLIENT * Prefix, CLIENT * User)
1589 char *modes, *user, *host;
1591 modes = Client_Modes(User);
1592 user = Client_User(User) ? Client_User(User) : "-";
1593 host = Client_Hostname(User) ? Client_Hostname(User) : "-";
1595 conn = Client_Conn(Client);
1596 if (Conn_Options(conn) & CONN_RFC1459) {
1597 /* RFC 1459 mode: separate NICK and USER commands */
1598 if (! Conn_WriteStr(conn, "NICK %s :%d",
1599 Client_ID(User), Client_Hops(User) + 1))
1600 return DISCONNECTED;
1601 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1602 Client_ID(User), user, host,
1603 Client_ID(Client_Introducer(User)),
1605 return DISCONNECTED;
1607 if (! Conn_WriteStr(conn, ":%s MODE %s +%s",
1608 Client_ID(User), Client_ID(User),
1610 return DISCONNECTED;
1613 /* RFC 2813 mode: one combined NICK or SERVICE command */
1614 if (Client_Type(User) == CLIENT_SERVICE
1615 && Client_HasFlag(Client, 'S')) {
1616 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1617 "SERVICE %s %d * +%s %d :%s",
1619 Client_MyToken(Client_Introducer(User)),
1620 modes, Client_Hops(User) + 1,
1622 return DISCONNECTED;
1624 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1625 "NICK %s %d %s %s %d +%s :%s",
1626 Client_ID(User), Client_Hops(User) + 1,
1628 Client_MyToken(Client_Introducer(User)),
1629 modes, Client_Info(User)))
1630 return DISCONNECTED;
1634 if (Client_HasFlag(Client, 'M')) {
1635 /* Synchronize metadata */
1636 if (Client_HostnameCloaked(User)) {
1637 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1638 "METADATA %s cloakhost :%s",
1640 Client_HostnameCloaked(User)))
1641 return DISCONNECTED;
1644 if (Client_AccountName(User)) {
1645 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1646 "METADATA %s accountname :%s",
1648 Client_AccountName(User)))
1649 return DISCONNECTED;
1652 if (Conn_GetCertFp(Client_Conn(User))) {
1653 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1654 "METADATA %s certfp :%s",
1656 Conn_GetCertFp(Client_Conn(User))))
1657 return DISCONNECTED;
1662 } /* Client_Announce */
1668 Client_DebugDump(void)
1672 Log(LOG_DEBUG, "Client status:");
1676 " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1677 Client_ID(c), Client_Type(c), Client_Hostname(c),
1678 Client_User(c), Client_Conn(c), Client_StartTime(c),
1680 c = (CLIENT *)c->next;
1682 } /* Client_DumpClients */