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 = strdup(AccountName);
474 Client->account_name = NULL;
479 Client_SetAway( CLIENT *Client, const char *Txt )
481 /* Set AWAY reason of client */
483 assert( Client != NULL );
484 assert( Txt != NULL );
486 strlcpy( Client->away, Txt, sizeof( Client->away ));
487 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
488 Client_Mask(Client), Txt);
489 } /* Client_SetAway */
493 Client_SetType( CLIENT *Client, int Type )
495 assert( Client != NULL );
497 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
498 Adjust_Counters( Client );
499 } /* Client_SetType */
503 Client_SetHops( CLIENT *Client, int Hops )
505 assert( Client != NULL );
507 } /* Client_SetHops */
511 Client_SetToken( CLIENT *Client, int Token )
513 assert( Client != NULL );
514 Client->token = Token;
515 } /* Client_SetToken */
519 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
521 assert( Client != NULL );
522 assert( Introducer != NULL );
523 Client->introducer = Introducer;
524 } /* Client_SetIntroducer */
528 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
530 assert( Client != NULL );
531 Client->oper_by_me = OperByMe;
532 } /* Client_SetOperByMe */
536 Client_ModeAdd( CLIENT *Client, char Mode )
539 * If Client already had Mode, return false.
540 * If the Mode was newly set, return true.
545 assert( Client != NULL );
547 x[0] = Mode; x[1] = '\0';
548 if (!Client_HasMode(Client, x[0])) {
549 strlcat( Client->modes, x, sizeof( Client->modes ));
553 } /* Client_ModeAdd */
557 Client_ModeDel( CLIENT *Client, char Mode )
560 * If Mode was removed, return true.
561 * If Client did not have Mode, return false.
566 assert( Client != NULL );
568 x[0] = Mode; x[1] = '\0';
570 p = strchr( Client->modes, x[0] );
571 if( ! p ) return false;
573 /* Client has Mode -> delete */
580 } /* Client_ModeDel */
584 * Search CLIENT structure of a given nick name.
586 * @return Pointer to CLIENT structure or NULL if not found.
589 Client_Search( const char *Nick )
591 char search_id[CLIENT_ID_LEN], *ptr;
595 assert( Nick != NULL );
597 /* copy Nick and truncate hostmask if necessary */
598 strlcpy( search_id, Nick, sizeof( search_id ));
599 ptr = strchr( search_id, '!' );
600 if( ptr ) *ptr = '\0';
602 search_hash = Hash(search_id);
606 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
608 c = (CLIENT *)c->next;
615 * Search first CLIENT structure matching a given mask of a server.
617 * The order of servers is arbitrary, but this function makes sure that the
618 * local server is always returned if the mask matches it.
620 * @return Pointer to CLIENT structure or NULL if no server could be found.
623 Client_SearchServer(const char *Mask)
627 assert(Mask != NULL);
629 /* First check if mask matches the local server */
630 if (MatchCaseInsensitive(Mask, Client_ID(Client_ThisServer())))
631 return Client_ThisServer();
635 if (Client_Type(c) == CLIENT_SERVER) {
636 /* This is a server: check if Mask matches */
637 if (MatchCaseInsensitive(Mask, c->id))
640 c = (CLIENT *)c->next;
647 * Get client structure ("introducer") identfied by a server token.
648 * @return CLIENT structure or NULL if none could be found.
651 Client_GetFromToken( CLIENT *Client, int Token )
655 assert( Client != NULL );
662 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
665 c = (CLIENT *)c->next;
668 } /* Client_GetFromToken */
672 Client_Type( CLIENT *Client )
674 assert( Client != NULL );
680 Client_Conn( CLIENT *Client )
682 assert( Client != NULL );
683 return Client->conn_id;
688 Client_ID( CLIENT *Client )
690 assert( Client != NULL );
693 if(Client->type == CLIENT_USER)
694 assert(strlen(Client->id) < Conf_MaxNickLength);
697 if( Client->id[0] ) return Client->id;
703 Client_Info( CLIENT *Client )
705 assert( Client != NULL );
711 Client_User( CLIENT *Client )
713 assert( Client != NULL );
714 return Client->user[0] ? Client->user : "~";
721 * Get the "original" user name as supplied by the USER command.
722 * The user name as given by the client is used for authentication instead
723 * of the one detected using IDENT requests.
724 * @param Client The client.
725 * @return Original user name.
728 Client_OrigUser(CLIENT *Client) {
730 char *user = Client->user;
736 return Client->orig_user;
738 } /* Client_OrigUser */
743 * Return the hostname of a client.
744 * @param Client Pointer to client structure
745 * @return Pointer to client hostname
748 Client_Hostname(CLIENT *Client)
750 assert (Client != NULL);
755 * Return the cloaked hostname of a client, if set.
756 * @param Client Pointer to the client structure.
757 * @return Pointer to the cloaked hostname or NULL if not set.
760 Client_HostnameCloaked(CLIENT *Client)
762 assert(Client != NULL);
763 return Client->cloaked;
767 * Get (potentially cloaked) hostname of a client to display it to other users.
769 * If the client has not enabled cloaking, the real hostname is used.
771 * @param Client Pointer to client structure
772 * @return Pointer to client hostname
775 Client_HostnameDisplayed(CLIENT *Client)
777 assert(Client != NULL);
779 /* Client isn't cloaked at all, return real hostname: */
780 if (!Client_HasMode(Client, 'x'))
781 return Client_Hostname(Client);
783 /* Use an already saved cloaked hostname, if there is one */
785 return Client->cloaked;
787 Client_UpdateCloakedHostname(Client, NULL, NULL);
788 return Client->cloaked;
792 * Update (and generate, if necessary) the cloaked hostname of a client.
794 * The newly set cloaked hostname is announced in the network using METADATA
795 * commands to peers that support this feature.
797 * @param Client The client of which the cloaked hostname should be updated.
798 * @param Origin The originator of the hostname change, or NULL if this server.
799 * @param Hostname The new cloaked hostname, or NULL if it should be generated.
802 Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
803 const char *Hostname)
805 char Cloak_Buffer[CLIENT_HOST_LEN];
807 assert(Client != NULL);
809 Origin = Client_ThisServer();
811 if (!Client->cloaked) {
812 Client->cloaked = malloc(CLIENT_HOST_LEN);
813 if (!Client->cloaked)
818 /* Generate new cloaked hostname */
819 if (*Conf_CloakHostModeX) {
820 strlcpy(Cloak_Buffer, Client->host,
821 sizeof(Cloak_Buffer));
822 strlcat(Cloak_Buffer, Conf_CloakHostSalt,
823 sizeof(Cloak_Buffer));
824 snprintf(Client->cloaked, CLIENT_HOST_LEN,
825 Conf_CloakHostModeX, Hash(Cloak_Buffer));
827 strlcpy(Client->cloaked, Client_ID(Client->introducer),
830 strlcpy(Client->cloaked, Hostname, CLIENT_HOST_LEN);
831 LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
832 Client_ID(Client), Client->cloaked);
834 /* Inform other servers in the network */
835 IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
836 "METADATA %s cloakhost :%s",
837 Client_ID(Client), Client->cloaked);
841 Client_Modes( CLIENT *Client )
843 assert( Client != NULL );
844 return Client->modes;
849 Client_Flags( CLIENT *Client )
851 assert( Client != NULL );
852 return Client->flags;
857 Client_OperByMe( CLIENT *Client )
859 assert( Client != NULL );
860 return Client->oper_by_me;
861 } /* Client_OperByMe */
865 Client_Hops( CLIENT *Client )
867 assert( Client != NULL );
873 Client_Token( CLIENT *Client )
875 assert( Client != NULL );
876 return Client->token;
881 Client_MyToken( CLIENT *Client )
883 assert( Client != NULL );
884 return Client->mytoken;
885 } /* Client_MyToken */
889 Client_NextHop( CLIENT *Client )
893 assert( Client != NULL );
896 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
900 } /* Client_NextHop */
904 * Return ID of a client: "client!user@host"
905 * This client ID is used for IRC prefixes, for example.
906 * Please note that this function uses a global static buffer, so you can't
907 * nest invocations without overwriting earlier results!
908 * @param Client Pointer to client structure
909 * @return Pointer to global buffer containing the client ID
912 Client_Mask( CLIENT *Client )
914 static char Mask_Buffer[GETID_LEN];
916 assert (Client != NULL);
918 /* Servers: return name only, there is no "mask" */
919 if (Client->type == CLIENT_SERVER)
922 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
923 Client->id, Client->user, Client->host);
929 * Return ID of a client with cloaked hostname: "client!user@server-name"
931 * This client ID is used for IRC prefixes, for example.
932 * Please note that this function uses a global static buffer, so you can't
933 * nest invocations without overwriting earlier results!
934 * If the client has not enabled cloaking, the real hostname is used.
936 * @param Client Pointer to client structure
937 * @return Pointer to global buffer containing the client ID
940 Client_MaskCloaked(CLIENT *Client)
942 static char Mask_Buffer[GETID_LEN];
944 assert (Client != NULL);
946 /* Is the client using cloaking at all? */
947 if (!Client_HasMode(Client, 'x'))
948 return Client_Mask(Client);
950 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
951 Client_HostnameDisplayed(Client));
954 } /* Client_MaskCloaked */
958 Client_Introducer( CLIENT *Client )
960 assert( Client != NULL );
961 return Client->introducer;
962 } /* Client_Introducer */
966 Client_TopServer( CLIENT *Client )
968 assert( Client != NULL );
969 return Client->topserver;
970 } /* Client_TopServer */
974 Client_HasMode( CLIENT *Client, char Mode )
976 assert( Client != NULL );
977 return strchr( Client->modes, Mode ) != NULL;
978 } /* Client_HasMode */
982 Client_HasFlag( CLIENT *Client, char Flag )
984 assert( Client != NULL );
985 return strchr( Client->flags, Flag ) != NULL;
986 } /* Client_HasFlag */
990 Client_Away( CLIENT *Client )
992 assert( Client != NULL );
998 Client_AccountName(CLIENT *Client)
1000 assert(Client != NULL);
1001 return Client->account_name;
1006 * Make sure that a given nickname is valid.
1008 * If the nickname is not valid for the given client, this function sends back
1009 * the appropriate error messages.
1011 * @param Client Client that wants to change the nickname.
1012 * @param Nick New nickname.
1013 * @returns true if nickname is valid, false otherwise.
1016 Client_CheckNick(CLIENT *Client, char *Nick)
1018 assert(Client != NULL);
1019 assert(Nick != NULL);
1021 if (!Client_IsValidNick(Nick)) {
1022 if (strlen(Nick ) >= Conf_MaxNickLength)
1023 IRC_WriteErrClient(Client, ERR_NICKNAMETOOLONG_MSG,
1024 Client_ID(Client), Nick,
1025 Conf_MaxNickLength - 1);
1027 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1028 Client_ID(Client), Nick);
1032 if (Client_Type(Client) != CLIENT_SERVER
1033 && Client_Type(Client) != CLIENT_SERVICE) {
1034 /* Make sure that this isn't a restricted/forbidden nickname */
1035 if (Conf_NickIsBlocked(Nick)) {
1036 IRC_WriteErrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
1037 Client_ID(Client), Nick);
1042 /* Nickname already registered? */
1043 if (Client_Search(Nick)) {
1044 IRC_WriteErrClient(Client, ERR_NICKNAMEINUSE_MSG,
1045 Client_ID(Client), Nick);
1050 } /* Client_CheckNick */
1054 Client_CheckID( CLIENT *Client, char *ID )
1056 char str[COMMAND_LEN];
1059 assert( Client != NULL );
1060 assert( Client->conn_id > NONE );
1061 assert( ID != NULL );
1064 if (strlen(ID) > CLIENT_ID_LEN) {
1065 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1066 Client_ID(Client), ID);
1070 /* ID already in use? */
1073 if (strcasecmp(c->id, ID) == 0) {
1074 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
1075 if (c->conn_id != NONE)
1076 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
1078 Log(LOG_ERR, "%s (via network)!", str);
1079 Conn_Close(Client->conn_id, str, str, true);
1082 c = (CLIENT *)c->next;
1086 } /* Client_CheckID */
1090 Client_First( void )
1093 } /* Client_First */
1097 Client_Next( CLIENT *c )
1099 assert( c != NULL );
1100 return (CLIENT *)c->next;
1105 Client_UserCount( void )
1107 return Count( CLIENT_USER );
1108 } /* Client_UserCount */
1112 Client_ServiceCount( void )
1114 return Count( CLIENT_SERVICE );;
1115 } /* Client_ServiceCount */
1119 Client_ServerCount( void )
1121 return Count( CLIENT_SERVER );
1122 } /* Client_ServerCount */
1126 Client_MyUserCount( void )
1128 return MyCount( CLIENT_USER );
1129 } /* Client_MyUserCount */
1133 Client_MyServiceCount( void )
1135 return MyCount( CLIENT_SERVICE );
1136 } /* Client_MyServiceCount */
1139 GLOBAL unsigned long
1140 Client_MyServerCount( void )
1143 unsigned long cnt = 0;
1148 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
1149 c = (CLIENT *)c->next;
1152 } /* Client_MyServerCount */
1155 GLOBAL unsigned long
1156 Client_OperCount( void )
1159 unsigned long cnt = 0;
1164 if (c && c->type == CLIENT_USER && Client_HasMode(c, 'o' ))
1166 c = (CLIENT *)c->next;
1169 } /* Client_OperCount */
1172 GLOBAL unsigned long
1173 Client_UnknownCount( void )
1176 unsigned long cnt = 0;
1181 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1182 c = (CLIENT *)c->next;
1186 } /* Client_UnknownCount */
1190 Client_MaxUserCount( void )
1193 } /* Client_MaxUserCount */
1197 Client_MyMaxUserCount( void )
1199 return My_Max_Users;
1200 } /* Client_MyMaxUserCount */
1204 * Check that a given nickname is valid.
1206 * @param Nick the nickname to check.
1207 * @returns true if nickname is valid, false otherwise.
1210 Client_IsValidNick(const char *Nick)
1213 static const char goodchars[] = ";0123456789-";
1215 assert (Nick != NULL);
1217 if (strchr(goodchars, Nick[0]))
1219 if (strlen(Nick ) >= Conf_MaxNickLength)
1224 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1232 } /* Client_IsValidNick */
1236 * Return pointer to "My_Whowas" structure.
1239 Client_GetWhowas( void )
1242 } /* Client_GetWhowas */
1245 * Return the index of the last used WHOWAS entry.
1248 Client_GetLastWhowasIndex( void )
1251 } /* Client_GetLastWhowasIndex */
1255 * Get the start time of this client.
1256 * The result is the start time in seconds since 1970-01-01, as reported
1257 * by the C function time(NULL).
1260 Client_StartTime(CLIENT *Client)
1262 assert( Client != NULL );
1263 return Client->starttime;
1264 } /* Client_Uptime */
1268 * Reject a client when logging in.
1270 * This function is called when a client isn't allowed to connect to this
1271 * server. Possible reasons are bad server password, bad PAM password,
1272 * or that the client is G/K-Line'd.
1274 * After calling this function, the client isn't connected any more.
1276 * @param Client The client to reject.
1277 * @param Reason The reason why the client has been rejected.
1278 * @param InformClient If true, send the exact reason to the client.
1281 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1283 char info[COMMAND_LEN];
1285 assert(Client != NULL);
1286 assert(Reason != NULL);
1289 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1291 strcpy(info, "Access denied: Bad password?");
1294 "User \"%s\" rejected (connection %d): %s!",
1295 Client_Mask(Client), Client_Conn(Client), Reason);
1296 Conn_Close(Client_Conn(Client), Reason, info, true);
1301 * Introduce a new user or service client in the network.
1303 * @param From Remote server introducing the client or NULL (local).
1304 * @param Client New client.
1305 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1308 Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
1310 /* Set client type (user or service) */
1311 Client_SetType(Client, Type);
1314 if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
1316 Client_SetType(Client, CLIENT_SERVICE);
1317 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1318 Client_TypeText(Client), Client_Mask(Client),
1319 Client_Modes(Client), Client_ID(From),
1320 Client_ID(Client_Introducer(Client)),
1321 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1323 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1324 Client_TypeText(Client), Client_Mask(Client),
1325 Client_Conn(Client));
1326 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1327 Client_ID(Client), Client_User(Client),
1328 Client_Hostname(Client),
1329 Conn_IPA(Client_Conn(Client)),
1330 Client_TypeText(Client));
1333 /* Inform other servers */
1334 IRC_WriteStrServersPrefixFlag_CB(From,
1335 From != NULL ? From : Client_ThisServer(),
1336 '\0', cb_introduceClient, (void *)Client);
1337 } /* Client_Introduce */
1340 static unsigned long
1341 Count( CLIENT_TYPE Type )
1344 unsigned long cnt = 0;
1349 if( c->type == Type ) cnt++;
1350 c = (CLIENT *)c->next;
1356 static unsigned long
1357 MyCount( CLIENT_TYPE Type )
1360 unsigned long cnt = 0;
1365 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1366 c = (CLIENT *)c->next;
1373 New_Client_Struct( void )
1377 c = (CLIENT *)malloc( sizeof( CLIENT ));
1380 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1384 memset( c, 0, sizeof ( CLIENT ));
1386 c->type = CLIENT_UNKNOWN;
1388 c->oper_by_me = false;
1398 Generate_MyToken( CLIENT *Client )
1407 if( c->mytoken == token )
1409 /* The token is already in use */
1414 else c = (CLIENT *)c->next;
1416 Client->mytoken = token;
1417 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1418 } /* Generate_MyToken */
1422 Adjust_Counters( CLIENT *Client )
1426 assert( Client != NULL );
1428 if( Client->type != CLIENT_USER ) return;
1430 if( Client->conn_id != NONE )
1432 /* Local connection */
1433 count = Client_MyUserCount( );
1434 if( count > My_Max_Users ) My_Max_Users = count;
1436 count = Client_UserCount( );
1437 if( count > Max_Users ) Max_Users = count;
1438 } /* Adjust_Counters */
1442 * Register client in My_Whowas structure for further recall by WHOWAS.
1443 * Note: Only clients that have been connected at least 30 seconds will be
1444 * registered to prevent automated IRC bots to "destroy" a nice server
1448 Client_RegisterWhowas( CLIENT *Client )
1453 assert( Client != NULL );
1455 /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1456 if (Conf_MorePrivacy)
1460 /* Don't register clients that were connected less than 30 seconds. */
1461 if( now - Client->starttime < 30 )
1464 slot = Last_Whowas + 1;
1465 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1468 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1471 My_Whowas[slot].time = now;
1472 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1473 sizeof( My_Whowas[slot].id ));
1474 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1475 sizeof( My_Whowas[slot].user ));
1476 strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
1477 sizeof( My_Whowas[slot].host ));
1478 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1479 sizeof( My_Whowas[slot].info ));
1480 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1481 sizeof( My_Whowas[slot].server ));
1484 } /* Client_RegisterWhowas */
1488 Client_TypeText(CLIENT *Client)
1490 assert(Client != NULL);
1491 switch (Client_Type(Client)) {
1495 case CLIENT_SERVICE:
1504 } /* Client_TypeText */
1508 * Destroy user or service client.
1511 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1513 if(Client->conn_id != NONE) {
1514 /* Local (directly connected) client */
1516 "%s \"%s\" unregistered (connection %d): %s.",
1517 Client_TypeText(Client), Client_Mask(Client),
1518 Client->conn_id, Txt);
1519 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1520 Client_ID(Client), Client_User(Client),
1521 Client_Hostname(Client), Txt);
1524 /* Inforam all the other servers */
1526 IRC_WriteStrServersPrefix(NULL,
1527 Client, "QUIT :%s", FwdMsg );
1529 IRC_WriteStrServersPrefix(NULL,
1534 LogDebug("%s \"%s\" unregistered: %s.",
1535 Client_TypeText(Client), Client_Mask(Client), Txt);
1538 /* Inform all the other servers, but the ones in the
1539 * direction we got the QUIT from */
1541 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1542 Client, "QUIT :%s", FwdMsg );
1544 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1549 /* Unregister client from channels */
1550 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1552 /* Register client in My_Whowas structure */
1553 Client_RegisterWhowas(Client);
1554 } /* Destroy_UserOrService */
1558 * Introduce a new user or service client to a remote server.
1560 * @param To The remote server to inform.
1561 * @param Prefix Prefix for the generated commands.
1562 * @param data CLIENT structure of the new client.
1565 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1567 CLIENT *c = (CLIENT *)data;
1569 (void)Client_Announce(To, Prefix, c);
1571 } /* cb_introduceClient */
1575 * Announce an user or service to a server.
1577 * This function differentiates between RFC1459 and RFC2813 server links and
1578 * generates the appropriate commands to register the user or service.
1580 * @param Client Server
1581 * @param Prefix Prefix for the generated commands
1582 * @param User User to announce
1585 Client_Announce(CLIENT * Client, CLIENT * Prefix, CLIENT * User)
1588 char *modes, *user, *host;
1590 modes = Client_Modes(User);
1591 user = Client_User(User) ? Client_User(User) : "-";
1592 host = Client_Hostname(User) ? Client_Hostname(User) : "-";
1594 conn = Client_Conn(Client);
1595 if (Conn_Options(conn) & CONN_RFC1459) {
1596 /* RFC 1459 mode: separate NICK and USER commands */
1597 if (! Conn_WriteStr(conn, "NICK %s :%d",
1598 Client_ID(User), Client_Hops(User) + 1))
1599 return DISCONNECTED;
1600 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1601 Client_ID(User), user, host,
1602 Client_ID(Client_Introducer(User)),
1604 return DISCONNECTED;
1606 if (! Conn_WriteStr(conn, ":%s MODE %s +%s",
1607 Client_ID(User), Client_ID(User),
1609 return DISCONNECTED;
1612 /* RFC 2813 mode: one combined NICK or SERVICE command */
1613 if (Client_Type(User) == CLIENT_SERVICE
1614 && Client_HasFlag(Client, 'S')) {
1615 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1616 "SERVICE %s %d * +%s %d :%s",
1618 Client_MyToken(Client_Introducer(User)),
1619 modes, Client_Hops(User) + 1,
1621 return DISCONNECTED;
1623 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1624 "NICK %s %d %s %s %d +%s :%s",
1625 Client_ID(User), Client_Hops(User) + 1,
1627 Client_MyToken(Client_Introducer(User)),
1628 modes, Client_Info(User)))
1629 return DISCONNECTED;
1633 if (Client_HasFlag(Client, 'M')) {
1634 /* Synchronize metadata */
1635 if (Client_HostnameCloaked(User)) {
1636 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1637 "METADATA %s cloakhost :%s",
1639 Client_HostnameCloaked(User)))
1640 return DISCONNECTED;
1643 if (Client_AccountName(User)) {
1644 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1645 "METADATA %s accountname :%s",
1647 Client_AccountName(User)))
1648 return DISCONNECTED;
1651 if (Conn_GetCertFp(Client_Conn(User))) {
1652 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1653 "METADATA %s certfp :%s",
1655 Conn_GetCertFp(Client_Conn(User))))
1656 return DISCONNECTED;
1661 } /* Client_Announce */
1667 Client_DebugDump(void)
1671 Log(LOG_DEBUG, "Client status:");
1675 " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1676 Client_ID(c), Client_Type(c), Client_Hostname(c),
1677 Client_User(c), Client_Conn(c), Client_StartTime(c),
1679 c = (CLIENT *)c->next;
1681 } /* Client_DumpClients */