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 (the period
341 * or colon indicates it's still an IP address). */
342 if (Conf_CloakHost[0] && strpbrk(Client->host, ".:")) {
343 char cloak[GETID_LEN];
345 strlcpy(cloak, Hostname, GETID_LEN);
346 strlcat(cloak, Conf_CloakHostSalt, GETID_LEN);
347 snprintf(cloak, GETID_LEN, Conf_CloakHost, Hash(cloak));
349 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
350 Client_ID(Client), Client->host, cloak);
351 strlcpy(Client->host, cloak, sizeof(Client->host));
353 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
354 Client_ID(Client), Client->host, Hostname);
355 strlcpy(Client->host, Hostname, sizeof(Client->host));
357 } /* Client_SetHostname */
361 * Set IP address to display for a client.
363 * @param Client The client.
364 * @param IPAText Textual representation of the IP address or NULL to unset.
367 Client_SetIPAText(CLIENT *Client, const char *IPAText)
369 assert(Client != NULL);
371 if (Client->ipa_text)
372 free(Client->ipa_text);
375 Client->ipa_text = strndup(IPAText, CLIENT_HOST_LEN - 1);
377 Client->ipa_text = NULL;
382 Client_SetID( CLIENT *Client, const char *ID )
384 assert( Client != NULL );
385 assert( ID != NULL );
387 strlcpy( Client->id, ID, sizeof( Client->id ));
389 if (Conf_CloakUserToNick) {
390 strlcpy( Client->user, ID, sizeof( Client->user ));
391 strlcpy( Client->info, ID, sizeof( Client->info ));
395 Client->hash = Hash( Client->id );
400 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
402 /* set clients username */
404 assert( Client != NULL );
405 assert( User != NULL );
407 if (Conf_CloakUserToNick) {
408 strlcpy(Client->user, Client->id, sizeof(Client->user));
409 } else if (Idented) {
410 strlcpy(Client->user, User, sizeof(Client->user));
412 Client->user[0] = '~';
413 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
415 } /* Client_SetUser */
419 * Set "original" user name of a client.
420 * This function saves the "original" user name, the user name specified by
421 * the peer using the USER command, into the CLIENT structure. This user
422 * name may be used for authentication, for example.
423 * @param Client The client.
424 * @param User User name to set.
427 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
429 assert(Client != NULL);
430 assert(User != NULL);
433 strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
435 } /* Client_SetOrigUser */
439 Client_SetInfo( CLIENT *Client, const char *Info )
441 /* set client hostname */
443 assert( Client != NULL );
444 assert( Info != NULL );
446 if (Conf_CloakUserToNick)
447 strlcpy(Client->info, Client->id, sizeof(Client->info));
449 strlcpy(Client->info, Info, sizeof(Client->info));
450 } /* Client_SetInfo */
454 Client_SetModes( CLIENT *Client, const char *Modes )
456 assert( Client != NULL );
457 assert( Modes != NULL );
459 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
460 } /* Client_SetModes */
464 Client_SetFlags( CLIENT *Client, const char *Flags )
466 assert( Client != NULL );
467 assert( Flags != NULL );
469 strlcpy(Client->flags, Flags, sizeof(Client->flags));
470 } /* Client_SetFlags */
474 Client_SetAccountName(CLIENT *Client, const char *AccountName)
476 assert(Client != NULL);
478 if (Client->account_name)
479 free(Client->account_name);
482 Client->account_name = strndup(AccountName,
483 CLIENT_NICK_LEN - 1);
485 Client->account_name = NULL;
490 Client_SetAway( CLIENT *Client, const char *Txt )
492 /* Set AWAY reason of client */
494 assert( Client != NULL );
495 assert( Txt != NULL );
500 Client->away = strndup(Txt, CLIENT_AWAY_LEN - 1);
502 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
503 Client_Mask(Client), Txt);
504 } /* Client_SetAway */
508 Client_SetType( CLIENT *Client, int Type )
510 assert( Client != NULL );
512 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
513 Adjust_Counters( Client );
514 } /* Client_SetType */
518 Client_SetHops( CLIENT *Client, int Hops )
520 assert( Client != NULL );
522 } /* Client_SetHops */
526 Client_SetToken( CLIENT *Client, int Token )
528 assert( Client != NULL );
529 Client->token = Token;
530 } /* Client_SetToken */
534 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
536 assert( Client != NULL );
537 assert( Introducer != NULL );
538 Client->introducer = Introducer;
539 } /* Client_SetIntroducer */
543 Client_ModeAdd( CLIENT *Client, char Mode )
546 * If Client already had Mode, return false.
547 * If the Mode was newly set, return true.
552 assert( Client != NULL );
554 x[0] = Mode; x[1] = '\0';
555 if (!Client_HasMode(Client, x[0])) {
556 strlcat( Client->modes, x, sizeof( Client->modes ));
560 } /* Client_ModeAdd */
564 Client_ModeDel( CLIENT *Client, char Mode )
567 * If Mode was removed, return true.
568 * If Client did not have Mode, return false.
573 assert( Client != NULL );
575 x[0] = Mode; x[1] = '\0';
577 p = strchr( Client->modes, x[0] );
578 if( ! p ) return false;
580 /* Client has Mode -> delete */
587 } /* Client_ModeDel */
591 * Search CLIENT structure of a given nick name.
593 * @return Pointer to CLIENT structure or NULL if not found.
596 Client_Search( const char *Nick )
598 char search_id[CLIENT_ID_LEN], *ptr;
602 assert( Nick != NULL );
604 /* copy Nick and truncate hostmask if necessary */
605 strlcpy( search_id, Nick, sizeof( search_id ));
606 ptr = strchr( search_id, '!' );
607 if( ptr ) *ptr = '\0';
609 search_hash = Hash(search_id);
613 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
615 c = (CLIENT *)c->next;
622 * Search first CLIENT structure matching a given mask of a server.
624 * The order of servers is arbitrary, but this function makes sure that the
625 * local server is always returned if the mask matches it.
627 * @return Pointer to CLIENT structure or NULL if no server could be found.
630 Client_SearchServer(const char *Mask)
634 assert(Mask != NULL);
636 /* First check if mask matches the local server */
637 if (MatchCaseInsensitive(Mask, Client_ID(Client_ThisServer())))
638 return Client_ThisServer();
642 if (Client_Type(c) == CLIENT_SERVER) {
643 /* This is a server: check if Mask matches */
644 if (MatchCaseInsensitive(Mask, c->id))
647 c = (CLIENT *)c->next;
654 * Get client structure ("introducer") identfied by a server token.
655 * @return CLIENT structure or NULL if none could be found.
658 Client_GetFromToken( CLIENT *Client, int Token )
662 assert( Client != NULL );
669 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
672 c = (CLIENT *)c->next;
675 } /* Client_GetFromToken */
679 Client_Type( CLIENT *Client )
681 assert( Client != NULL );
687 Client_Conn( CLIENT *Client )
689 assert( Client != NULL );
690 return Client->conn_id;
695 Client_ID( CLIENT *Client )
697 assert( Client != NULL );
700 if(Client->type == CLIENT_USER)
701 assert(strlen(Client->id) < Conf_MaxNickLength);
704 if( Client->id[0] ) return Client->id;
710 Client_Info( CLIENT *Client )
712 assert( Client != NULL );
718 Client_User( CLIENT *Client )
720 assert( Client != NULL );
721 return Client->user[0] ? Client->user : "~";
728 * Get the "original" user name as supplied by the USER command.
729 * The user name as given by the client is used for authentication instead
730 * of the one detected using IDENT requests.
731 * @param Client The client.
732 * @return Original user name.
735 Client_OrigUser(CLIENT *Client) {
736 return Client->orig_user;
737 } /* Client_OrigUser */
742 * Return the hostname of a client.
743 * @param Client Pointer to client structure
744 * @return Pointer to client hostname
747 Client_Hostname(CLIENT *Client)
749 assert (Client != NULL);
754 * Return the cloaked hostname of a client, if set.
755 * @param Client Pointer to the client structure.
756 * @return Pointer to the cloaked hostname or NULL if not set.
759 Client_HostnameCloaked(CLIENT *Client)
761 assert(Client != NULL);
762 return Client->cloaked;
766 * Get (potentially cloaked) hostname of a client to display it to other users.
768 * If the client has not enabled cloaking, the real hostname is used.
770 * @param Client Pointer to client structure
771 * @return Pointer to client hostname
774 Client_HostnameDisplayed(CLIENT *Client)
776 assert(Client != NULL);
778 /* Client isn't cloaked at all, return real hostname: */
779 if (!Client_HasMode(Client, 'x'))
780 return Client_Hostname(Client);
782 /* Use an already saved cloaked hostname, if there is one */
784 return Client->cloaked;
786 Client_UpdateCloakedHostname(Client, NULL, NULL);
787 return Client->cloaked;
791 Client_IPAText(CLIENT *Client)
793 assert(Client != NULL);
795 /* Not a local client? */
796 if (Client_Conn(Client) <= NONE)
799 if (!Client->ipa_text)
800 return Conn_GetIPAInfo(Client_Conn(Client));
802 return Client->ipa_text;
806 * Update (and generate, if necessary) the cloaked hostname of a client.
808 * The newly set cloaked hostname is announced in the network using METADATA
809 * commands to peers that support this feature.
811 * @param Client The client of which the cloaked hostname should be updated.
812 * @param Origin The originator of the hostname change, or NULL if this server.
813 * @param Hostname The new cloaked hostname, or NULL if it should be generated.
816 Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
817 const char *Hostname)
819 char Cloak_Buffer[CLIENT_HOST_LEN];
821 assert(Client != NULL);
823 Origin = Client_ThisServer();
825 if (!Client->cloaked) {
826 Client->cloaked = malloc(CLIENT_HOST_LEN);
827 if (!Client->cloaked)
832 /* Generate new cloaked hostname */
833 if (*Conf_CloakHostModeX) {
834 strlcpy(Cloak_Buffer, Client->host,
835 sizeof(Cloak_Buffer));
836 strlcat(Cloak_Buffer, Conf_CloakHostSalt,
837 sizeof(Cloak_Buffer));
838 snprintf(Client->cloaked, CLIENT_HOST_LEN,
839 Conf_CloakHostModeX, Hash(Cloak_Buffer));
841 strlcpy(Client->cloaked, Client_ID(Client->introducer),
844 strlcpy(Client->cloaked, Hostname, CLIENT_HOST_LEN);
845 LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
846 Client_ID(Client), Client->cloaked);
848 /* Inform other servers in the network */
849 IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
850 "METADATA %s cloakhost :%s",
851 Client_ID(Client), Client->cloaked);
855 Client_Modes( CLIENT *Client )
857 assert( Client != NULL );
858 return Client->modes;
863 Client_Flags( CLIENT *Client )
865 assert( Client != NULL );
866 return Client->flags;
871 Client_Hops( CLIENT *Client )
873 assert( Client != NULL );
879 Client_Token( CLIENT *Client )
881 assert( Client != NULL );
882 return Client->token;
887 Client_MyToken( CLIENT *Client )
889 assert( Client != NULL );
890 return Client->mytoken;
891 } /* Client_MyToken */
895 Client_NextHop( CLIENT *Client )
899 assert( Client != NULL );
902 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
906 } /* Client_NextHop */
910 * Return ID of a client: "client!user@host"
911 * This client ID is used for IRC prefixes, for example.
912 * Please note that this function uses a global static buffer, so you can't
913 * nest invocations without overwriting earlier results!
914 * @param Client Pointer to client structure
915 * @return Pointer to global buffer containing the client ID
918 Client_Mask( CLIENT *Client )
920 static char Mask_Buffer[GETID_LEN];
922 assert (Client != NULL);
924 /* Servers: return name only, there is no "mask" */
925 if (Client->type == CLIENT_SERVER)
928 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
929 Client->id, Client->user, Client->host);
935 * Return ID of a client with cloaked hostname: "client!user@server-name"
937 * This client ID is used for IRC prefixes, for example.
938 * Please note that this function uses a global static buffer, so you can't
939 * nest invocations without overwriting earlier results!
940 * If the client has not enabled cloaking, the real hostname is used.
942 * @param Client Pointer to client structure
943 * @return Pointer to global buffer containing the client ID
946 Client_MaskCloaked(CLIENT *Client)
948 static char Mask_Buffer[GETID_LEN];
950 assert (Client != NULL);
952 /* Is the client using cloaking at all? */
953 if (!Client_HasMode(Client, 'x'))
954 return Client_Mask(Client);
956 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
957 Client_HostnameDisplayed(Client));
960 } /* Client_MaskCloaked */
964 Client_Introducer( CLIENT *Client )
966 assert( Client != NULL );
967 return Client->introducer;
968 } /* Client_Introducer */
972 Client_TopServer( CLIENT *Client )
974 assert( Client != NULL );
975 return Client->topserver;
976 } /* Client_TopServer */
980 Client_HasMode( CLIENT *Client, char Mode )
982 assert( Client != NULL );
983 return strchr( Client->modes, Mode ) != NULL;
984 } /* Client_HasMode */
988 Client_HasFlag( CLIENT *Client, char Flag )
990 assert( Client != NULL );
991 return strchr( Client->flags, Flag ) != NULL;
992 } /* Client_HasFlag */
996 Client_Away( CLIENT *Client )
998 assert( Client != NULL );
1004 Client_AccountName(CLIENT *Client)
1006 assert(Client != NULL);
1007 return Client->account_name;
1012 * Make sure that a given nickname is valid.
1014 * If the nickname is not valid for the given client, this function sends back
1015 * the appropriate error messages.
1017 * @param Client Client that wants to change the nickname.
1018 * @param Nick New nickname.
1019 * @returns true if nickname is valid, false otherwise.
1022 Client_CheckNick(CLIENT *Client, char *Nick)
1024 assert(Client != NULL);
1025 assert(Nick != NULL);
1027 if (!Client_IsValidNick(Nick)) {
1028 if (strlen(Nick ) >= Conf_MaxNickLength)
1029 IRC_WriteErrClient(Client, ERR_NICKNAMETOOLONG_MSG,
1030 Client_ID(Client), Nick,
1031 Conf_MaxNickLength - 1);
1033 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1034 Client_ID(Client), Nick);
1038 if (Client_Type(Client) != CLIENT_SERVER
1039 && Client_Type(Client) != CLIENT_SERVICE) {
1040 /* Make sure that this isn't a restricted/forbidden nickname */
1041 if (Conf_NickIsBlocked(Nick)) {
1042 IRC_WriteErrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
1043 Client_ID(Client), Nick);
1048 /* Nickname already registered? */
1049 if (Client_Search(Nick)) {
1050 IRC_WriteErrClient(Client, ERR_NICKNAMEINUSE_MSG,
1051 Client_ID(Client), Nick);
1056 } /* Client_CheckNick */
1060 Client_CheckID( CLIENT *Client, char *ID )
1062 char str[COMMAND_LEN];
1065 assert( Client != NULL );
1066 assert( Client->conn_id > NONE );
1067 assert( ID != NULL );
1070 if (strlen(ID) > CLIENT_ID_LEN) {
1071 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1072 Client_ID(Client), ID);
1076 /* ID already in use? */
1079 if (strcasecmp(c->id, ID) == 0) {
1080 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
1081 if (c->conn_id != NONE)
1082 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
1084 Log(LOG_ERR, "%s (via network)!", str);
1085 Conn_Close(Client->conn_id, str, str, true);
1088 c = (CLIENT *)c->next;
1092 } /* Client_CheckID */
1096 Client_First( void )
1099 } /* Client_First */
1103 Client_Next( CLIENT *c )
1105 assert( c != NULL );
1106 return (CLIENT *)c->next;
1111 Client_UserCount( void )
1113 return Count( CLIENT_USER );
1114 } /* Client_UserCount */
1118 Client_ServiceCount( void )
1120 return Count( CLIENT_SERVICE );;
1121 } /* Client_ServiceCount */
1125 Client_ServerCount( void )
1127 return Count( CLIENT_SERVER );
1128 } /* Client_ServerCount */
1132 Client_MyUserCount( void )
1134 return MyCount( CLIENT_USER );
1135 } /* Client_MyUserCount */
1139 Client_MyServiceCount( void )
1141 return MyCount( CLIENT_SERVICE );
1142 } /* Client_MyServiceCount */
1145 GLOBAL unsigned long
1146 Client_MyServerCount( void )
1149 unsigned long cnt = 0;
1154 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
1155 c = (CLIENT *)c->next;
1158 } /* Client_MyServerCount */
1161 GLOBAL unsigned long
1162 Client_OperCount( void )
1165 unsigned long cnt = 0;
1170 if (c && c->type == CLIENT_USER && Client_HasMode(c, 'o' ))
1172 c = (CLIENT *)c->next;
1175 } /* Client_OperCount */
1178 GLOBAL unsigned long
1179 Client_UnknownCount( void )
1182 unsigned long cnt = 0;
1187 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1188 c = (CLIENT *)c->next;
1192 } /* Client_UnknownCount */
1196 Client_MaxUserCount( void )
1199 } /* Client_MaxUserCount */
1203 Client_MyMaxUserCount( void )
1205 return My_Max_Users;
1206 } /* Client_MyMaxUserCount */
1210 * Check that a given nickname is valid.
1212 * @param Nick the nickname to check.
1213 * @returns true if nickname is valid, false otherwise.
1216 Client_IsValidNick(const char *Nick)
1219 static const char goodchars[] = ";0123456789-";
1221 assert (Nick != NULL);
1223 if (strchr(goodchars, Nick[0]))
1225 if (strlen(Nick ) >= Conf_MaxNickLength)
1230 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1238 } /* Client_IsValidNick */
1242 * Return pointer to "My_Whowas" structure.
1245 Client_GetWhowas( void )
1248 } /* Client_GetWhowas */
1251 * Return the index of the last used WHOWAS entry.
1254 Client_GetLastWhowasIndex( void )
1257 } /* Client_GetLastWhowasIndex */
1261 * Get the start time of this client.
1262 * The result is the start time in seconds since 1970-01-01, as reported
1263 * by the C function time(NULL).
1266 Client_StartTime(CLIENT *Client)
1268 assert( Client != NULL );
1269 return Client->starttime;
1270 } /* Client_Uptime */
1274 * Reject a client when logging in.
1276 * This function is called when a client isn't allowed to connect to this
1277 * server. Possible reasons are bad server password, bad PAM password,
1278 * or that the client is G/K-Line'd.
1280 * After calling this function, the client isn't connected any more.
1282 * @param Client The client to reject.
1283 * @param Reason The reason why the client has been rejected.
1284 * @param InformClient If true, send the exact reason to the client.
1287 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1289 char info[COMMAND_LEN];
1291 assert(Client != NULL);
1292 assert(Reason != NULL);
1295 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1297 strcpy(info, "Access denied: Bad password?");
1300 "User \"%s\" rejected (connection %d): %s!",
1301 Client_Mask(Client), Client_Conn(Client), Reason);
1302 Conn_Close(Client_Conn(Client), Reason, info, true);
1307 * Introduce a new user or service client in the network.
1309 * @param From Remote server introducing the client or NULL (local).
1310 * @param Client New client.
1311 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1314 Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
1316 /* Set client type (user or service) */
1317 Client_SetType(Client, Type);
1320 if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
1322 Client_SetType(Client, CLIENT_SERVICE);
1323 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1324 Client_TypeText(Client), Client_Mask(Client),
1325 Client_Modes(Client), Client_ID(From),
1326 Client_ID(Client_Introducer(Client)),
1327 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1329 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1330 Client_TypeText(Client), Client_Mask(Client),
1331 Client_Conn(Client));
1332 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1333 Client_ID(Client), Client_User(Client),
1334 Client_Hostname(Client),
1335 Conn_IPA(Client_Conn(Client)),
1336 Client_TypeText(Client));
1339 /* Inform other servers */
1340 IRC_WriteStrServersPrefixFlag_CB(From,
1341 From != NULL ? From : Client_ThisServer(),
1342 '\0', cb_introduceClient, (void *)Client);
1343 } /* Client_Introduce */
1346 static unsigned long
1347 Count( CLIENT_TYPE Type )
1350 unsigned long cnt = 0;
1355 if( c->type == Type ) cnt++;
1356 c = (CLIENT *)c->next;
1362 static unsigned long
1363 MyCount( CLIENT_TYPE Type )
1366 unsigned long cnt = 0;
1371 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1372 c = (CLIENT *)c->next;
1379 * Allocate and initialize new CLIENT strcuture.
1381 * @return Pointer to CLIENT structure or NULL on error.
1384 New_Client_Struct( void )
1388 c = (CLIENT *)malloc( sizeof( CLIENT ));
1391 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1395 memset( c, 0, sizeof ( CLIENT ));
1397 c->type = CLIENT_UNKNOWN;
1407 * Free a CLIENT structure and its member variables.
1410 Free_Client(CLIENT **Client)
1412 assert(Client != NULL);
1413 assert(*Client != NULL);
1415 if ((*Client)->account_name)
1416 free((*Client)->account_name);
1417 if ((*Client)->away)
1418 free((*Client)->away);
1419 if ((*Client)->cloaked)
1420 free((*Client)->cloaked);
1421 if ((*Client)->ipa_text)
1422 free((*Client)->ipa_text);
1429 Generate_MyToken( CLIENT *Client )
1438 if( c->mytoken == token )
1440 /* The token is already in use */
1445 else c = (CLIENT *)c->next;
1447 Client->mytoken = token;
1448 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1449 } /* Generate_MyToken */
1453 Adjust_Counters( CLIENT *Client )
1457 assert( Client != NULL );
1459 if( Client->type != CLIENT_USER ) return;
1461 if( Client->conn_id != NONE )
1463 /* Local connection */
1464 count = Client_MyUserCount( );
1465 if( count > My_Max_Users ) My_Max_Users = count;
1467 count = Client_UserCount( );
1468 if( count > Max_Users ) Max_Users = count;
1469 } /* Adjust_Counters */
1473 * Register client in My_Whowas structure for further recall by WHOWAS.
1474 * Note: Only clients that have been connected at least 30 seconds will be
1475 * registered to prevent automated IRC bots to "destroy" a nice server
1479 Client_RegisterWhowas( CLIENT *Client )
1484 assert( Client != NULL );
1486 /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1487 if (Conf_MorePrivacy)
1491 /* Don't register clients that were connected less than 30 seconds. */
1492 if( now - Client->starttime < 30 )
1495 slot = Last_Whowas + 1;
1496 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1499 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1502 My_Whowas[slot].time = now;
1503 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1504 sizeof( My_Whowas[slot].id ));
1505 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1506 sizeof( My_Whowas[slot].user ));
1507 strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
1508 sizeof( My_Whowas[slot].host ));
1509 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1510 sizeof( My_Whowas[slot].info ));
1511 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1512 sizeof( My_Whowas[slot].server ));
1515 } /* Client_RegisterWhowas */
1519 Client_TypeText(CLIENT *Client)
1521 assert(Client != NULL);
1522 switch (Client_Type(Client)) {
1526 case CLIENT_SERVICE:
1535 } /* Client_TypeText */
1539 * Destroy user or service client.
1542 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1544 if(Client->conn_id != NONE) {
1545 /* Local (directly connected) client */
1547 "%s \"%s\" unregistered (connection %d): %s.",
1548 Client_TypeText(Client), Client_Mask(Client),
1549 Client->conn_id, Txt);
1550 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1551 Client_ID(Client), Client_User(Client),
1552 Client_Hostname(Client), Txt);
1555 /* Inforam all the other servers */
1557 IRC_WriteStrServersPrefix(NULL,
1558 Client, "QUIT :%s", FwdMsg );
1560 IRC_WriteStrServersPrefix(NULL,
1565 LogDebug("%s \"%s\" unregistered: %s.",
1566 Client_TypeText(Client), Client_Mask(Client), Txt);
1569 /* Inform all the other servers, but the ones in the
1570 * direction we got the QUIT from */
1572 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1573 Client, "QUIT :%s", FwdMsg );
1575 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1580 /* Unregister client from channels */
1581 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1583 /* Register client in My_Whowas structure */
1584 Client_RegisterWhowas(Client);
1585 } /* Destroy_UserOrService */
1589 * Introduce a new user or service client to a remote server.
1591 * @param To The remote server to inform.
1592 * @param Prefix Prefix for the generated commands.
1593 * @param data CLIENT structure of the new client.
1596 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1598 CLIENT *c = (CLIENT *)data;
1600 (void)Client_Announce(To, Prefix, c);
1602 } /* cb_introduceClient */
1606 * Announce an user or service to a server.
1608 * This function differentiates between RFC1459 and RFC2813 server links and
1609 * generates the appropriate commands to register the user or service.
1611 * @param Client Server
1612 * @param Prefix Prefix for the generated commands
1613 * @param User User to announce
1616 Client_Announce(CLIENT * Client, CLIENT * Prefix, CLIENT * User)
1619 char *modes, *user, *host;
1621 modes = Client_Modes(User);
1622 user = Client_User(User) ? Client_User(User) : "-";
1623 host = Client_Hostname(User) ? Client_Hostname(User) : "-";
1625 conn = Client_Conn(Client);
1626 if (Conn_Options(conn) & CONN_RFC1459) {
1627 /* RFC 1459 mode: separate NICK and USER commands */
1628 if (! Conn_WriteStr(conn, "NICK %s :%d",
1629 Client_ID(User), Client_Hops(User) + 1))
1630 return DISCONNECTED;
1631 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1632 Client_ID(User), user, host,
1633 Client_ID(Client_Introducer(User)),
1635 return DISCONNECTED;
1637 if (! Conn_WriteStr(conn, ":%s MODE %s +%s",
1638 Client_ID(User), Client_ID(User),
1640 return DISCONNECTED;
1643 /* RFC 2813 mode: one combined NICK or SERVICE command */
1644 if (Client_Type(User) == CLIENT_SERVICE
1645 && Client_HasFlag(Client, 'S')) {
1646 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1647 "SERVICE %s %d * +%s %d :%s",
1649 Client_MyToken(Client_Introducer(User)),
1650 modes, Client_Hops(User) + 1,
1652 return DISCONNECTED;
1654 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1655 "NICK %s %d %s %s %d +%s :%s",
1656 Client_ID(User), Client_Hops(User) + 1,
1658 Client_MyToken(Client_Introducer(User)),
1659 modes, Client_Info(User)))
1660 return DISCONNECTED;
1664 if (Client_HasFlag(Client, 'M')) {
1665 /* Synchronize metadata */
1666 if (Client_HostnameCloaked(User)) {
1667 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1668 "METADATA %s cloakhost :%s",
1670 Client_HostnameCloaked(User)))
1671 return DISCONNECTED;
1674 if (Client_AccountName(User)) {
1675 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1676 "METADATA %s accountname :%s",
1678 Client_AccountName(User)))
1679 return DISCONNECTED;
1682 if (Conn_GetCertFp(Client_Conn(User))) {
1683 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1684 "METADATA %s certfp :%s",
1686 Conn_GetCertFp(Client_Conn(User))))
1687 return DISCONNECTED;
1692 } /* Client_Announce */
1698 Client_DebugDump(void)
1702 Log(LOG_DEBUG, "Client status:");
1706 " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1707 Client_ID(c), Client_Type(c), Client_Hostname(c),
1708 Client_User(c), Client_Conn(c), Client_StartTime(c),
1710 c = (CLIENT *)c->next;
1712 } /* Client_DumpClients */