2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
34 #include "conn-func.h"
36 #include "irc-write.h"
41 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
43 static CLIENT *This_Server, *My_Clients;
45 static WHOWAS My_Whowas[MAX_WHOWAS];
46 static int Last_Whowas = -1;
47 static long Max_Users, My_Max_Users;
50 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
51 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
53 static CLIENT *New_Client_Struct PARAMS(( void ));
54 static void Generate_MyToken PARAMS(( CLIENT *Client ));
55 static void Adjust_Counters PARAMS(( CLIENT *Client ));
57 static void Free_Client PARAMS(( CLIENT **Client ));
59 static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
60 CLIENT *TopServer, int Type, const char *ID,
61 const char *User, const char *Hostname, const char *Info,
62 int Hops, int Token, const char *Modes,
65 static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
68 static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
76 This_Server = New_Client_Struct( );
79 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
80 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
84 /* Client structure for this server */
85 This_Server->next = NULL;
86 This_Server->type = CLIENT_SERVER;
87 This_Server->conn_id = NONE;
88 This_Server->introducer = This_Server;
89 This_Server->mytoken = 1;
90 This_Server->hops = 0;
92 gethostname( This_Server->host, CLIENT_HOST_LEN );
94 h = gethostbyname( This_Server->host );
95 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
97 Client_SetID( This_Server, Conf_ServerName );
98 Client_SetInfo( This_Server, Conf_ServerInfo );
100 My_Clients = This_Server;
102 memset( &My_Whowas, 0, sizeof( My_Whowas ));
112 if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
113 else Client_Destroy( This_Server, "Server going down.", NULL, false );
119 next = (CLIENT *)c->next;
124 Log(LOG_INFO, "Freed %d client structure%s.",
125 cnt, cnt == 1 ? "" : "s");
130 Client_ThisServer( void )
133 } /* Client_ThisServer */
137 * Initialize new local client; wrapper function for Init_New_Client().
138 * @return New CLIENT structure.
141 Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
143 return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
144 Hostname, NULL, 0, 0, NULL, Idented);
145 } /* Client_NewLocal */
149 * Initialize new remote server; wrapper function for Init_New_Client().
150 * @return New CLIENT structure.
153 Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
154 int Hops, int Token, const char *Info, bool Idented)
156 return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
157 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
158 } /* Client_NewRemoteServer */
162 * Initialize new remote client; wrapper function for Init_New_Client().
163 * @return New CLIENT structure.
166 Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
167 const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
169 return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
170 User, Hostname, Info, Hops, Token, Modes, Idented);
171 } /* Client_NewRemoteUser */
175 * Initialize new client and set up the given parameters like client type,
176 * user name, host name, introducing server etc. ...
177 * @return New CLIENT structure.
180 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
181 int Type, const char *ID, const char *User, const char *Hostname,
182 const char *Info, int Hops, int Token, const char *Modes, bool Idented)
187 assert(Introducer != NULL);
189 client = New_Client_Struct();
193 client->starttime = time(NULL);
194 client->conn_id = Idx;
195 client->introducer = Introducer;
196 client->topserver = TopServer;
199 Client_SetID(client, ID);
201 Client_SetUser(client, User, Idented);
202 Client_SetOrigUser(client, User);
205 Client_SetHostname(client, Hostname);
207 Client_SetInfo(client, Info);
209 client->token = Token;
211 Client_SetModes(client, Modes);
212 if (Type == CLIENT_SERVER)
213 Generate_MyToken(client);
215 if (Client_HasMode(client, 'a'))
216 client->away = strndup(DEFAULT_AWAY_MSG, CLIENT_AWAY_LEN - 1);
218 client->next = (POINTER *)My_Clients;
221 Adjust_Counters(client);
224 } /* Init_New_Client */
228 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
230 /* remove a client */
233 char msg[COMMAND_LEN];
236 assert( Client != NULL );
238 txt = LogMsg ? LogMsg : FwdMsg;
240 txt = "Reason unknown";
242 /* netsplit message */
243 if( Client->type == CLIENT_SERVER ) {
244 strlcpy(msg, This_Server->id, sizeof (msg));
245 strlcat(msg, " ", sizeof (msg));
246 strlcat(msg, Client->id, sizeof (msg));
253 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
256 * The client that is about to be removed is a server,
257 * the client we are checking right now is a child of that
258 * server and thus has to be removed, too.
260 * Call Client_Destroy() recursively with the server as the
261 * new "object to be removed". This starts the cycle again, until
262 * all servers that are linked via the original server have been
265 Client_Destroy( c, NULL, msg, false );
272 /* found the client: remove it */
273 if( last ) last->next = c->next;
274 else My_Clients = (CLIENT *)c->next;
276 if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
277 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
278 else if( c->type == CLIENT_SERVER )
280 if (c != This_Server) {
281 if (c->conn_id != NONE)
282 Log(LOG_NOTICE|LOG_snotice,
283 "Server \"%s\" unregistered (connection %d): %s.",
284 c->id, c->conn_id, txt);
286 Log(LOG_NOTICE|LOG_snotice,
287 "Server \"%s\" unregistered: %s.",
291 /* inform other servers */
292 if( ! NGIRCd_SignalQuit )
294 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
295 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
300 if (c->conn_id != NONE) {
303 "Client \"%s\" unregistered (connection %d): %s.",
304 c->id, c->conn_id, txt);
307 "Client unregistered (connection %d): %s.",
311 "Unregistered unknown client \"%s\": %s",
312 c->id[0] ? c->id : "(No Nick)", txt);
320 c = (CLIENT *)c->next;
322 } /* Client_Destroy */
326 * Set client hostname.
328 * If global hostname cloaking is in effect, don't set the real hostname
329 * but the configured one.
331 * @param Client The client of which the hostname should be set.
332 * @param Hostname The new hostname.
335 Client_SetHostname( CLIENT *Client, const char *Hostname )
337 assert(Client != NULL);
338 assert(Hostname != NULL);
340 /* Only cloak the hostmask if it has not yet been cloaked.
341 * The period or colon indicates it's still an IP address.
342 * An empty string means a rDNS lookup did not happen (yet). */
343 if (Conf_CloakHost[0] && (!Client->host[0] || strchr(Client->host, '.')
344 || strchr(Client->host, ':'))) {
345 char cloak[GETID_LEN];
347 strlcpy(cloak, Hostname, GETID_LEN);
348 strlcat(cloak, Conf_CloakHostSalt, GETID_LEN);
349 snprintf(cloak, GETID_LEN, Conf_CloakHost, Hash(cloak));
351 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
352 Client_ID(Client), Client->host, cloak);
353 strlcpy(Client->host, cloak, sizeof(Client->host));
355 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
356 Client_ID(Client), Client->host, Hostname);
357 strlcpy(Client->host, Hostname, sizeof(Client->host));
359 } /* Client_SetHostname */
363 * Set IP address to display for a client.
365 * @param Client The client.
366 * @param IPAText Textual representation of the IP address or NULL to unset.
369 Client_SetIPAText(CLIENT *Client, const char *IPAText)
371 assert(Client != NULL);
373 if (Client->ipa_text)
374 free(Client->ipa_text);
377 Client->ipa_text = strndup(IPAText, CLIENT_HOST_LEN - 1);
379 Client->ipa_text = NULL;
384 Client_SetID( CLIENT *Client, const char *ID )
386 assert( Client != NULL );
387 assert( ID != NULL );
389 strlcpy( Client->id, ID, sizeof( Client->id ));
391 if (Conf_CloakUserToNick) {
392 strlcpy( Client->user, ID, sizeof( Client->user ));
393 strlcpy( Client->info, ID, sizeof( Client->info ));
397 Client->hash = Hash( Client->id );
402 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
404 /* set clients username */
406 assert( Client != NULL );
407 assert( User != NULL );
409 if (Conf_CloakUserToNick) {
410 strlcpy(Client->user, Client->id, sizeof(Client->user));
411 } else if (Idented) {
412 strlcpy(Client->user, User, sizeof(Client->user));
414 Client->user[0] = '~';
415 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
417 } /* Client_SetUser */
421 * Set "original" user name of a client.
422 * This function saves the "original" user name, the user name specified by
423 * the peer using the USER command, into the CLIENT structure. This user
424 * name may be used for authentication, for example.
425 * @param Client The client.
426 * @param User User name to set.
429 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
431 assert(Client != NULL);
432 assert(User != NULL);
435 strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
437 } /* Client_SetOrigUser */
441 Client_SetInfo( CLIENT *Client, const char *Info )
443 /* set client hostname */
445 assert( Client != NULL );
446 assert( Info != NULL );
448 if (Conf_CloakUserToNick)
449 strlcpy(Client->info, Client->id, sizeof(Client->info));
451 strlcpy(Client->info, Info, sizeof(Client->info));
452 } /* Client_SetInfo */
456 Client_SetModes( CLIENT *Client, const char *Modes )
458 assert( Client != NULL );
459 assert( Modes != NULL );
461 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
462 } /* Client_SetModes */
466 Client_SetFlags( CLIENT *Client, const char *Flags )
468 assert( Client != NULL );
469 assert( Flags != NULL );
471 strlcpy(Client->flags, Flags, sizeof(Client->flags));
472 } /* Client_SetFlags */
476 Client_SetAccountName(CLIENT *Client, const char *AccountName)
478 assert(Client != NULL);
480 if (Client->account_name)
481 free(Client->account_name);
484 Client->account_name = strndup(AccountName,
485 CLIENT_NICK_LEN - 1);
487 Client->account_name = NULL;
492 Client_SetAway( CLIENT *Client, const char *Txt )
494 /* Set AWAY reason of client */
496 assert( Client != NULL );
497 assert( Txt != NULL );
502 Client->away = strndup(Txt, CLIENT_AWAY_LEN - 1);
504 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
505 Client_Mask(Client), Txt);
506 } /* Client_SetAway */
510 Client_SetType( CLIENT *Client, int Type )
512 assert( Client != NULL );
514 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
515 Adjust_Counters( Client );
516 } /* Client_SetType */
520 Client_SetHops( CLIENT *Client, int Hops )
522 assert( Client != NULL );
524 } /* Client_SetHops */
528 Client_SetToken( CLIENT *Client, int Token )
530 assert( Client != NULL );
531 Client->token = Token;
532 } /* Client_SetToken */
536 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
538 assert( Client != NULL );
539 assert( Introducer != NULL );
540 Client->introducer = Introducer;
541 } /* Client_SetIntroducer */
545 Client_ModeAdd( CLIENT *Client, char Mode )
548 * If Client already had Mode, return false.
549 * If the Mode was newly set, return true.
554 assert( Client != NULL );
556 x[0] = Mode; x[1] = '\0';
557 if (!Client_HasMode(Client, x[0])) {
558 strlcat( Client->modes, x, sizeof( Client->modes ));
562 } /* Client_ModeAdd */
566 Client_ModeDel( CLIENT *Client, char Mode )
569 * If Mode was removed, return true.
570 * If Client did not have Mode, return false.
575 assert( Client != NULL );
577 x[0] = Mode; x[1] = '\0';
579 p = strchr( Client->modes, x[0] );
580 if( ! p ) return false;
582 /* Client has Mode -> delete */
589 } /* Client_ModeDel */
593 * Search CLIENT structure of a given nick name.
595 * @return Pointer to CLIENT structure or NULL if not found.
598 Client_Search( const char *Nick )
600 char search_id[CLIENT_ID_LEN], *ptr;
604 assert( Nick != NULL );
606 /* copy Nick and truncate hostmask if necessary */
607 strlcpy( search_id, Nick, sizeof( search_id ));
608 ptr = strchr( search_id, '!' );
609 if( ptr ) *ptr = '\0';
611 search_hash = Hash(search_id);
615 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
617 c = (CLIENT *)c->next;
624 * Search first CLIENT structure matching a given mask of a server.
626 * The order of servers is arbitrary, but this function makes sure that the
627 * local server is always returned if the mask matches it.
629 * @return Pointer to CLIENT structure or NULL if no server could be found.
632 Client_SearchServer(const char *Mask)
636 assert(Mask != NULL);
638 /* First check if mask matches the local server */
639 if (MatchCaseInsensitive(Mask, Client_ID(Client_ThisServer())))
640 return Client_ThisServer();
644 if (Client_Type(c) == CLIENT_SERVER) {
645 /* This is a server: check if Mask matches */
646 if (MatchCaseInsensitive(Mask, c->id))
649 c = (CLIENT *)c->next;
656 * Get client structure ("introducer") identfied by a server token.
657 * @return CLIENT structure or NULL if none could be found.
660 Client_GetFromToken( CLIENT *Client, int Token )
664 assert( Client != NULL );
671 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
674 c = (CLIENT *)c->next;
677 } /* Client_GetFromToken */
681 Client_Type( CLIENT *Client )
683 assert( Client != NULL );
689 Client_Conn( CLIENT *Client )
691 assert( Client != NULL );
692 return Client->conn_id;
697 Client_ID( CLIENT *Client )
699 assert( Client != NULL );
702 if(Client->type == CLIENT_USER)
703 assert(strlen(Client->id) < Conf_MaxNickLength);
706 if( Client->id[0] ) return Client->id;
712 Client_Info( CLIENT *Client )
714 assert( Client != NULL );
720 Client_User( CLIENT *Client )
722 assert( Client != NULL );
723 return Client->user[0] ? Client->user : "~";
730 * Get the "original" user name as supplied by the USER command.
731 * The user name as given by the client is used for authentication instead
732 * of the one detected using IDENT requests.
733 * @param Client The client.
734 * @return Original user name.
737 Client_OrigUser(CLIENT *Client) {
738 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 Client_IPAText(CLIENT *Client)
795 assert(Client != NULL);
797 /* Not a local client? */
798 if (Client_Conn(Client) <= NONE)
801 if (!Client->ipa_text)
802 return Conn_GetIPAInfo(Client_Conn(Client));
804 return Client->ipa_text;
808 * Update (and generate, if necessary) the cloaked hostname of a client.
810 * The newly set cloaked hostname is announced in the network using METADATA
811 * commands to peers that support this feature.
813 * @param Client The client of which the cloaked hostname should be updated.
814 * @param Origin The originator of the hostname change, or NULL if this server.
815 * @param Hostname The new cloaked hostname, or NULL if it should be generated.
818 Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
819 const char *Hostname)
821 char Cloak_Buffer[CLIENT_HOST_LEN];
823 assert(Client != NULL);
825 Origin = Client_ThisServer();
827 if (!Client->cloaked) {
828 Client->cloaked = malloc(CLIENT_HOST_LEN);
829 if (!Client->cloaked)
834 /* Generate new cloaked hostname */
835 if (*Conf_CloakHostModeX) {
836 strlcpy(Cloak_Buffer, Client->host,
837 sizeof(Cloak_Buffer));
838 strlcat(Cloak_Buffer, Conf_CloakHostSalt,
839 sizeof(Cloak_Buffer));
840 snprintf(Client->cloaked, CLIENT_HOST_LEN,
841 Conf_CloakHostModeX, Hash(Cloak_Buffer));
843 strlcpy(Client->cloaked, Client_ID(Client->introducer),
846 strlcpy(Client->cloaked, Hostname, CLIENT_HOST_LEN);
847 LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
848 Client_ID(Client), Client->cloaked);
850 /* Inform other servers in the network */
851 IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
852 "METADATA %s cloakhost :%s",
853 Client_ID(Client), Client->cloaked);
857 Client_Modes( CLIENT *Client )
859 assert( Client != NULL );
860 return Client->modes;
865 Client_Flags( CLIENT *Client )
867 assert( Client != NULL );
868 return Client->flags;
873 Client_Hops( CLIENT *Client )
875 assert( Client != NULL );
881 Client_Token( CLIENT *Client )
883 assert( Client != NULL );
884 return Client->token;
889 Client_MyToken( CLIENT *Client )
891 assert( Client != NULL );
892 return Client->mytoken;
893 } /* Client_MyToken */
897 Client_NextHop( CLIENT *Client )
901 assert( Client != NULL );
904 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
908 } /* Client_NextHop */
912 * Return ID of a client: "client!user@host"
913 * This client ID is used for IRC prefixes, for example.
914 * Please note that this function uses a global static buffer, so you can't
915 * nest invocations without overwriting earlier results!
916 * @param Client Pointer to client structure
917 * @return Pointer to global buffer containing the client ID
920 Client_Mask( CLIENT *Client )
922 static char Mask_Buffer[GETID_LEN];
924 assert (Client != NULL);
926 /* Servers: return name only, there is no "mask" */
927 if (Client->type == CLIENT_SERVER)
930 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
931 Client->id, Client->user, Client->host);
937 * Return ID of a client with cloaked hostname: "client!user@server-name"
939 * This client ID is used for IRC prefixes, for example.
940 * Please note that this function uses a global static buffer, so you can't
941 * nest invocations without overwriting earlier results!
942 * If the client has not enabled cloaking, the real hostname is used.
944 * @param Client Pointer to client structure
945 * @return Pointer to global buffer containing the client ID
948 Client_MaskCloaked(CLIENT *Client)
950 static char Mask_Buffer[GETID_LEN];
952 assert (Client != NULL);
954 /* Is the client using cloaking at all? */
955 if (!Client_HasMode(Client, 'x'))
956 return Client_Mask(Client);
958 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
959 Client_HostnameDisplayed(Client));
962 } /* Client_MaskCloaked */
966 Client_Introducer( CLIENT *Client )
968 assert( Client != NULL );
969 return Client->introducer;
970 } /* Client_Introducer */
974 Client_TopServer( CLIENT *Client )
976 assert( Client != NULL );
977 return Client->topserver;
978 } /* Client_TopServer */
982 Client_HasMode( CLIENT *Client, char Mode )
984 assert( Client != NULL );
985 return strchr( Client->modes, Mode ) != NULL;
986 } /* Client_HasMode */
990 Client_HasFlag( CLIENT *Client, char Flag )
992 assert( Client != NULL );
993 return strchr( Client->flags, Flag ) != NULL;
994 } /* Client_HasFlag */
998 Client_Away( CLIENT *Client )
1000 assert( Client != NULL );
1001 return Client->away;
1006 Client_AccountName(CLIENT *Client)
1008 assert(Client != NULL);
1009 return Client->account_name;
1014 * Make sure that a given nickname is valid.
1016 * If the nickname is not valid for the given client, this function sends back
1017 * the appropriate error messages.
1019 * @param Client Client that wants to change the nickname.
1020 * @param Nick New nickname.
1021 * @returns true if nickname is valid, false otherwise.
1024 Client_CheckNick(CLIENT *Client, char *Nick)
1026 assert(Client != NULL);
1027 assert(Nick != NULL);
1029 if (!Client_IsValidNick(Nick)) {
1030 if (strlen(Nick ) >= Conf_MaxNickLength)
1031 IRC_WriteErrClient(Client, ERR_NICKNAMETOOLONG_MSG,
1032 Client_ID(Client), Nick,
1033 Conf_MaxNickLength - 1);
1035 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1036 Client_ID(Client), Nick);
1040 if (Client_Type(Client) != CLIENT_SERVER
1041 && Client_Type(Client) != CLIENT_SERVICE) {
1042 /* Make sure that this isn't a restricted/forbidden nickname */
1043 if (Conf_NickIsBlocked(Nick)) {
1044 IRC_WriteErrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
1045 Client_ID(Client), Nick);
1050 /* Nickname already registered? */
1051 if (Client_Search(Nick)) {
1052 IRC_WriteErrClient(Client, ERR_NICKNAMEINUSE_MSG,
1053 Client_ID(Client), Nick);
1058 } /* Client_CheckNick */
1062 Client_CheckID( CLIENT *Client, char *ID )
1064 char str[COMMAND_LEN];
1067 assert( Client != NULL );
1068 assert( Client->conn_id > NONE );
1069 assert( ID != NULL );
1072 if (strlen(ID) > CLIENT_ID_LEN) {
1073 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1074 Client_ID(Client), ID);
1078 /* ID already in use? */
1081 if (strcasecmp(c->id, ID) == 0) {
1082 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
1083 if (c->conn_id != NONE)
1084 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
1086 Log(LOG_ERR, "%s (via network)!", str);
1087 Conn_Close(Client->conn_id, str, str, true);
1090 c = (CLIENT *)c->next;
1094 } /* Client_CheckID */
1098 Client_First( void )
1101 } /* Client_First */
1105 Client_Next( CLIENT *c )
1107 assert( c != NULL );
1108 return (CLIENT *)c->next;
1113 Client_UserCount( void )
1115 return Count( CLIENT_USER );
1116 } /* Client_UserCount */
1120 Client_ServiceCount( void )
1122 return Count( CLIENT_SERVICE );;
1123 } /* Client_ServiceCount */
1127 Client_ServerCount( void )
1129 return Count( CLIENT_SERVER );
1130 } /* Client_ServerCount */
1134 Client_MyUserCount( void )
1136 return MyCount( CLIENT_USER );
1137 } /* Client_MyUserCount */
1141 Client_MyServiceCount( void )
1143 return MyCount( CLIENT_SERVICE );
1144 } /* Client_MyServiceCount */
1147 GLOBAL unsigned long
1148 Client_MyServerCount( void )
1151 unsigned long cnt = 0;
1156 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
1157 c = (CLIENT *)c->next;
1160 } /* Client_MyServerCount */
1163 GLOBAL unsigned long
1164 Client_OperCount( void )
1167 unsigned long cnt = 0;
1172 if (c && c->type == CLIENT_USER && Client_HasMode(c, 'o' ))
1174 c = (CLIENT *)c->next;
1177 } /* Client_OperCount */
1180 GLOBAL unsigned long
1181 Client_UnknownCount( void )
1184 unsigned long cnt = 0;
1189 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1190 c = (CLIENT *)c->next;
1194 } /* Client_UnknownCount */
1198 Client_MaxUserCount( void )
1201 } /* Client_MaxUserCount */
1205 Client_MyMaxUserCount( void )
1207 return My_Max_Users;
1208 } /* Client_MyMaxUserCount */
1212 * Check that a given nickname is valid.
1214 * @param Nick the nickname to check.
1215 * @returns true if nickname is valid, false otherwise.
1218 Client_IsValidNick(const char *Nick)
1221 static const char goodchars[] = ";0123456789-";
1223 assert (Nick != NULL);
1225 if (strchr(goodchars, Nick[0]))
1227 if (strlen(Nick ) >= Conf_MaxNickLength)
1232 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1240 } /* Client_IsValidNick */
1244 * Return pointer to "My_Whowas" structure.
1247 Client_GetWhowas( void )
1250 } /* Client_GetWhowas */
1253 * Return the index of the last used WHOWAS entry.
1256 Client_GetLastWhowasIndex( void )
1259 } /* Client_GetLastWhowasIndex */
1263 * Get the start time of this client.
1264 * The result is the start time in seconds since 1970-01-01, as reported
1265 * by the C function time(NULL).
1268 Client_StartTime(CLIENT *Client)
1270 assert( Client != NULL );
1271 return Client->starttime;
1272 } /* Client_Uptime */
1276 * Reject a client when logging in.
1278 * This function is called when a client isn't allowed to connect to this
1279 * server. Possible reasons are bad server password, bad PAM password,
1280 * or that the client is G/K-Line'd.
1282 * After calling this function, the client isn't connected any more.
1284 * @param Client The client to reject.
1285 * @param Reason The reason why the client has been rejected.
1286 * @param InformClient If true, send the exact reason to the client.
1289 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1291 char info[COMMAND_LEN];
1293 assert(Client != NULL);
1294 assert(Reason != NULL);
1297 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1299 strcpy(info, "Access denied: Bad password?");
1302 "User \"%s\" rejected (connection %d): %s!",
1303 Client_Mask(Client), Client_Conn(Client), Reason);
1304 Conn_Close(Client_Conn(Client), Reason, info, true);
1309 * Introduce a new user or service client in the network.
1311 * @param From Remote server introducing the client or NULL (local).
1312 * @param Client New client.
1313 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1316 Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
1318 /* Set client type (user or service) */
1319 Client_SetType(Client, Type);
1322 if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
1324 Client_SetType(Client, CLIENT_SERVICE);
1325 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1326 Client_TypeText(Client), Client_Mask(Client),
1327 Client_Modes(Client), Client_ID(From),
1328 Client_ID(Client_Introducer(Client)),
1329 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1331 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1332 Client_TypeText(Client), Client_Mask(Client),
1333 Client_Conn(Client));
1334 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1335 Client_ID(Client), Client_User(Client),
1336 Client_Hostname(Client),
1337 Conn_IPA(Client_Conn(Client)),
1338 Client_TypeText(Client));
1341 /* Inform other servers */
1342 IRC_WriteStrServersPrefixFlag_CB(From,
1343 From != NULL ? From : Client_ThisServer(),
1344 '\0', cb_introduceClient, (void *)Client);
1345 } /* Client_Introduce */
1348 static unsigned long
1349 Count( CLIENT_TYPE Type )
1352 unsigned long cnt = 0;
1357 if( c->type == Type ) cnt++;
1358 c = (CLIENT *)c->next;
1364 static unsigned long
1365 MyCount( CLIENT_TYPE Type )
1368 unsigned long cnt = 0;
1373 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1374 c = (CLIENT *)c->next;
1381 * Allocate and initialize new CLIENT strcuture.
1383 * @return Pointer to CLIENT structure or NULL on error.
1386 New_Client_Struct( void )
1390 c = (CLIENT *)malloc( sizeof( CLIENT ));
1393 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1397 memset( c, 0, sizeof ( CLIENT ));
1399 c->type = CLIENT_UNKNOWN;
1409 * Free a CLIENT structure and its member variables.
1412 Free_Client(CLIENT **Client)
1414 assert(Client != NULL);
1415 assert(*Client != NULL);
1417 if ((*Client)->account_name)
1418 free((*Client)->account_name);
1419 if ((*Client)->away)
1420 free((*Client)->away);
1421 if ((*Client)->cloaked)
1422 free((*Client)->cloaked);
1423 if ((*Client)->ipa_text)
1424 free((*Client)->ipa_text);
1431 Generate_MyToken( CLIENT *Client )
1440 if( c->mytoken == token )
1442 /* The token is already in use */
1447 else c = (CLIENT *)c->next;
1449 Client->mytoken = token;
1450 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1451 } /* Generate_MyToken */
1455 Adjust_Counters( CLIENT *Client )
1459 assert( Client != NULL );
1461 if( Client->type != CLIENT_USER ) return;
1463 if( Client->conn_id != NONE )
1465 /* Local connection */
1466 count = Client_MyUserCount( );
1467 if( count > My_Max_Users ) My_Max_Users = count;
1469 count = Client_UserCount( );
1470 if( count > Max_Users ) Max_Users = count;
1471 } /* Adjust_Counters */
1475 * Register client in My_Whowas structure for further recall by WHOWAS.
1476 * Note: Only clients that have been connected at least 30 seconds will be
1477 * registered to prevent automated IRC bots to "destroy" a nice server
1481 Client_RegisterWhowas( CLIENT *Client )
1486 assert( Client != NULL );
1488 /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1489 if (Conf_MorePrivacy)
1493 /* Don't register clients that were connected less than 30 seconds. */
1494 if( now - Client->starttime < 30 )
1497 slot = Last_Whowas + 1;
1498 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1501 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1504 My_Whowas[slot].time = now;
1505 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1506 sizeof( My_Whowas[slot].id ));
1507 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1508 sizeof( My_Whowas[slot].user ));
1509 strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
1510 sizeof( My_Whowas[slot].host ));
1511 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1512 sizeof( My_Whowas[slot].info ));
1513 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1514 sizeof( My_Whowas[slot].server ));
1517 } /* Client_RegisterWhowas */
1521 Client_TypeText(CLIENT *Client)
1523 assert(Client != NULL);
1524 switch (Client_Type(Client)) {
1528 case CLIENT_SERVICE:
1537 } /* Client_TypeText */
1541 * Destroy user or service client.
1544 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1546 if(Client->conn_id != NONE) {
1547 /* Local (directly connected) client */
1549 "%s \"%s\" unregistered (connection %d): %s.",
1550 Client_TypeText(Client), Client_Mask(Client),
1551 Client->conn_id, Txt);
1552 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1553 Client_ID(Client), Client_User(Client),
1554 Client_Hostname(Client), Txt);
1557 /* Inforam all the other servers */
1559 IRC_WriteStrServersPrefix(NULL,
1560 Client, "QUIT :%s", FwdMsg );
1562 IRC_WriteStrServersPrefix(NULL,
1567 LogDebug("%s \"%s\" unregistered: %s.",
1568 Client_TypeText(Client), Client_Mask(Client), Txt);
1571 /* Inform all the other servers, but the ones in the
1572 * direction we got the QUIT from */
1574 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1575 Client, "QUIT :%s", FwdMsg );
1577 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1582 /* Unregister client from channels */
1583 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1585 /* Register client in My_Whowas structure */
1586 Client_RegisterWhowas(Client);
1587 } /* Destroy_UserOrService */
1591 * Introduce a new user or service client to a remote server.
1593 * @param To The remote server to inform.
1594 * @param Prefix Prefix for the generated commands.
1595 * @param data CLIENT structure of the new client.
1598 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1600 CLIENT *c = (CLIENT *)data;
1602 (void)Client_Announce(To, Prefix, c);
1604 } /* cb_introduceClient */
1608 * Announce an user or service to a server.
1610 * This function differentiates between RFC1459 and RFC2813 server links and
1611 * generates the appropriate commands to register the user or service.
1613 * @param Client Server
1614 * @param Prefix Prefix for the generated commands
1615 * @param User User to announce
1618 Client_Announce(CLIENT * Client, CLIENT * Prefix, CLIENT * User)
1621 char *modes, *user, *host;
1623 modes = Client_Modes(User);
1624 user = Client_User(User) ? Client_User(User) : "-";
1625 host = Client_Hostname(User) ? Client_Hostname(User) : "-";
1627 conn = Client_Conn(Client);
1628 if (Conn_Options(conn) & CONN_RFC1459) {
1629 /* RFC 1459 mode: separate NICK and USER commands */
1630 if (! Conn_WriteStr(conn, "NICK %s :%d",
1631 Client_ID(User), Client_Hops(User) + 1))
1632 return DISCONNECTED;
1633 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1634 Client_ID(User), user, host,
1635 Client_ID(Client_Introducer(User)),
1637 return DISCONNECTED;
1639 if (! Conn_WriteStr(conn, ":%s MODE %s +%s",
1640 Client_ID(User), Client_ID(User),
1642 return DISCONNECTED;
1645 /* RFC 2813 mode: one combined NICK or SERVICE command */
1646 if (Client_Type(User) == CLIENT_SERVICE
1647 && Client_HasFlag(Client, 'S')) {
1648 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1649 "SERVICE %s %d * +%s %d :%s",
1651 Client_MyToken(Client_Introducer(User)),
1652 modes, Client_Hops(User) + 1,
1654 return DISCONNECTED;
1656 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1657 "NICK %s %d %s %s %d +%s :%s",
1658 Client_ID(User), Client_Hops(User) + 1,
1660 Client_MyToken(Client_Introducer(User)),
1661 modes, Client_Info(User)))
1662 return DISCONNECTED;
1666 if (Client_HasFlag(Client, 'M')) {
1667 /* Synchronize metadata */
1668 if (Client_HostnameCloaked(User)) {
1669 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1670 "METADATA %s cloakhost :%s",
1672 Client_HostnameCloaked(User)))
1673 return DISCONNECTED;
1676 if (Client_AccountName(User)) {
1677 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1678 "METADATA %s accountname :%s",
1680 Client_AccountName(User)))
1681 return DISCONNECTED;
1684 if (Conn_GetCertFp(Client_Conn(User))) {
1685 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1686 "METADATA %s certfp :%s",
1688 Conn_GetCertFp(Client_Conn(User))))
1689 return DISCONNECTED;
1694 } /* Client_Announce */
1700 Client_DebugDump(void)
1704 Log(LOG_DEBUG, "Client status:");
1708 " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1709 Client_ID(c), Client_Type(c), Client_Hostname(c),
1710 Client_User(c), Client_Conn(c), Client_StartTime(c),
1712 c = (CLIENT *)c->next;
1714 } /* Client_DumpClients */