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.
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 if (Conf_CloakHost[0]) {
341 char cloak[GETID_LEN];
343 strlcpy(cloak, Hostname, GETID_LEN);
344 strlcat(cloak, Conf_CloakHostSalt, GETID_LEN);
345 snprintf(cloak, GETID_LEN, Conf_CloakHost, Hash(cloak));
347 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
348 Client_ID(Client), Client->host, cloak);
349 strlcpy(Client->host, cloak, sizeof(Client->host));
351 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
352 Client_ID(Client), Client->host, Hostname);
353 strlcpy(Client->host, Hostname, sizeof(Client->host));
355 } /* Client_SetHostname */
359 * Set IP address to display for a client.
361 * @param Client The client.
362 * @param IPAText Textual representation of the IP address or NULL to unset.
365 Client_SetIPAText(CLIENT *Client, const char *IPAText)
367 assert(Client != NULL);
369 if (Client->ipa_text)
370 free(Client->ipa_text);
373 Client->ipa_text = strndup(IPAText, CLIENT_HOST_LEN - 1);
375 Client->ipa_text = NULL;
380 Client_SetID( CLIENT *Client, const char *ID )
382 assert( Client != NULL );
383 assert( ID != NULL );
385 strlcpy( Client->id, ID, sizeof( Client->id ));
387 if (Conf_CloakUserToNick) {
388 strlcpy( Client->user, ID, sizeof( Client->user ));
389 strlcpy( Client->info, ID, sizeof( Client->info ));
393 Client->hash = Hash( Client->id );
398 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
400 /* set clients username */
402 assert( Client != NULL );
403 assert( User != NULL );
405 if (Conf_CloakUserToNick) {
406 strlcpy(Client->user, Client->id, sizeof(Client->user));
407 } else if (Idented) {
408 strlcpy(Client->user, User, sizeof(Client->user));
410 Client->user[0] = '~';
411 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
413 } /* Client_SetUser */
417 * Set "original" user name of a client.
418 * This function saves the "original" user name, the user name specified by
419 * the peer using the USER command, into the CLIENT structure. This user
420 * name may be used for authentication, for example.
421 * @param Client The client.
422 * @param User User name to set.
425 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
427 assert(Client != NULL);
428 assert(User != NULL);
430 #if defined(PAM) && defined(IDENTAUTH)
431 strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
433 } /* Client_SetOrigUser */
437 Client_SetInfo( CLIENT *Client, const char *Info )
439 /* set client hostname */
441 assert( Client != NULL );
442 assert( Info != NULL );
444 if (Conf_CloakUserToNick)
445 strlcpy(Client->info, Client->id, sizeof(Client->info));
447 strlcpy(Client->info, Info, sizeof(Client->info));
448 } /* Client_SetInfo */
452 Client_SetModes( CLIENT *Client, const char *Modes )
454 assert( Client != NULL );
455 assert( Modes != NULL );
457 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
458 } /* Client_SetModes */
462 Client_SetFlags( CLIENT *Client, const char *Flags )
464 assert( Client != NULL );
465 assert( Flags != NULL );
467 strlcpy(Client->flags, Flags, sizeof(Client->flags));
468 } /* Client_SetFlags */
472 Client_SetAccountName(CLIENT *Client, const char *AccountName)
474 assert(Client != NULL);
476 if (Client->account_name)
477 free(Client->account_name);
480 Client->account_name = strndup(AccountName,
481 CLIENT_NICK_LEN - 1);
483 Client->account_name = NULL;
488 Client_SetAway( CLIENT *Client, const char *Txt )
490 /* Set AWAY reason of client */
492 assert( Client != NULL );
493 assert( Txt != NULL );
498 Client->away = strndup(Txt, CLIENT_AWAY_LEN - 1);
500 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
501 Client_Mask(Client), Txt);
502 } /* Client_SetAway */
506 Client_SetType( CLIENT *Client, int Type )
508 assert( Client != NULL );
510 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
511 Adjust_Counters( Client );
512 } /* Client_SetType */
516 Client_SetHops( CLIENT *Client, int Hops )
518 assert( Client != NULL );
520 } /* Client_SetHops */
524 Client_SetToken( CLIENT *Client, int Token )
526 assert( Client != NULL );
527 Client->token = Token;
528 } /* Client_SetToken */
532 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
534 assert( Client != NULL );
535 assert( Introducer != NULL );
536 Client->introducer = Introducer;
537 } /* Client_SetIntroducer */
541 Client_ModeAdd( CLIENT *Client, char Mode )
544 * If Client already had Mode, return false.
545 * If the Mode was newly set, return true.
550 assert( Client != NULL );
552 x[0] = Mode; x[1] = '\0';
553 if (!Client_HasMode(Client, x[0])) {
554 strlcat( Client->modes, x, sizeof( Client->modes ));
558 } /* Client_ModeAdd */
562 Client_ModeDel( CLIENT *Client, char Mode )
565 * If Mode was removed, return true.
566 * If Client did not have Mode, return false.
571 assert( Client != NULL );
573 x[0] = Mode; x[1] = '\0';
575 p = strchr( Client->modes, x[0] );
576 if( ! p ) return false;
578 /* Client has Mode -> delete */
585 } /* Client_ModeDel */
589 * Search CLIENT structure of a given nick name.
591 * @return Pointer to CLIENT structure or NULL if not found.
594 Client_Search( const char *Nick )
596 char search_id[CLIENT_ID_LEN], *ptr;
600 assert( Nick != NULL );
602 /* copy Nick and truncate hostmask if necessary */
603 strlcpy( search_id, Nick, sizeof( search_id ));
604 ptr = strchr( search_id, '!' );
605 if( ptr ) *ptr = '\0';
607 search_hash = Hash(search_id);
611 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
613 c = (CLIENT *)c->next;
620 * Search first CLIENT structure matching a given mask of a server.
622 * The order of servers is arbitrary, but this function makes sure that the
623 * local server is always returned if the mask matches it.
625 * @return Pointer to CLIENT structure or NULL if no server could be found.
628 Client_SearchServer(const char *Mask)
632 assert(Mask != NULL);
634 /* First check if mask matches the local server */
635 if (MatchCaseInsensitive(Mask, Client_ID(Client_ThisServer())))
636 return Client_ThisServer();
640 if (Client_Type(c) == CLIENT_SERVER) {
641 /* This is a server: check if Mask matches */
642 if (MatchCaseInsensitive(Mask, c->id))
645 c = (CLIENT *)c->next;
652 * Get client structure ("introducer") identfied by a server token.
653 * @return CLIENT structure or NULL if none could be found.
656 Client_GetFromToken( CLIENT *Client, int Token )
660 assert( Client != NULL );
667 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
670 c = (CLIENT *)c->next;
673 } /* Client_GetFromToken */
677 Client_Type( CLIENT *Client )
679 assert( Client != NULL );
685 Client_Conn( CLIENT *Client )
687 assert( Client != NULL );
688 return Client->conn_id;
693 Client_ID( CLIENT *Client )
695 assert( Client != NULL );
698 if(Client->type == CLIENT_USER)
699 assert(strlen(Client->id) < Conf_MaxNickLength);
702 if( Client->id[0] ) return Client->id;
708 Client_Info( CLIENT *Client )
710 assert( Client != NULL );
716 Client_User( CLIENT *Client )
718 assert( Client != NULL );
719 return Client->user[0] ? Client->user : "~";
726 * Get the "original" user name as supplied by the USER command.
727 * The user name as given by the client is used for authentication instead
728 * of the one detected using IDENT requests.
729 * @param Client The client.
730 * @return Original user name.
733 Client_OrigUser(CLIENT *Client) {
735 char *user = Client->user;
741 return Client->orig_user;
743 } /* Client_OrigUser */
748 * Return the hostname of a client.
749 * @param Client Pointer to client structure
750 * @return Pointer to client hostname
753 Client_Hostname(CLIENT *Client)
755 assert (Client != NULL);
760 * Return the cloaked hostname of a client, if set.
761 * @param Client Pointer to the client structure.
762 * @return Pointer to the cloaked hostname or NULL if not set.
765 Client_HostnameCloaked(CLIENT *Client)
767 assert(Client != NULL);
768 return Client->cloaked;
772 * Get (potentially cloaked) hostname of a client to display it to other users.
774 * If the client has not enabled cloaking, the real hostname is used.
776 * @param Client Pointer to client structure
777 * @return Pointer to client hostname
780 Client_HostnameDisplayed(CLIENT *Client)
782 assert(Client != NULL);
784 /* Client isn't cloaked at all, return real hostname: */
785 if (!Client_HasMode(Client, 'x'))
786 return Client_Hostname(Client);
788 /* Use an already saved cloaked hostname, if there is one */
790 return Client->cloaked;
792 Client_UpdateCloakedHostname(Client, NULL, NULL);
793 return Client->cloaked;
797 Client_IPAText(CLIENT *Client)
799 assert(Client != NULL);
801 /* Not a local client? */
802 if (Client_Conn(Client) <= NONE)
805 if (!Client->ipa_text)
806 return Conn_GetIPAInfo(Client_Conn(Client));
808 return Client->ipa_text;
812 * Update (and generate, if necessary) the cloaked hostname of a client.
814 * The newly set cloaked hostname is announced in the network using METADATA
815 * commands to peers that support this feature.
817 * @param Client The client of which the cloaked hostname should be updated.
818 * @param Origin The originator of the hostname change, or NULL if this server.
819 * @param Hostname The new cloaked hostname, or NULL if it should be generated.
822 Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
823 const char *Hostname)
825 char Cloak_Buffer[CLIENT_HOST_LEN];
827 assert(Client != NULL);
829 Origin = Client_ThisServer();
831 if (!Client->cloaked) {
832 Client->cloaked = malloc(CLIENT_HOST_LEN);
833 if (!Client->cloaked)
838 /* Generate new cloaked hostname */
839 if (*Conf_CloakHostModeX) {
840 strlcpy(Cloak_Buffer, Client->host,
841 sizeof(Cloak_Buffer));
842 strlcat(Cloak_Buffer, Conf_CloakHostSalt,
843 sizeof(Cloak_Buffer));
844 snprintf(Client->cloaked, CLIENT_HOST_LEN,
845 Conf_CloakHostModeX, Hash(Cloak_Buffer));
847 strlcpy(Client->cloaked, Client_ID(Client->introducer),
850 strlcpy(Client->cloaked, Hostname, CLIENT_HOST_LEN);
851 LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
852 Client_ID(Client), Client->cloaked);
854 /* Inform other servers in the network */
855 IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
856 "METADATA %s cloakhost :%s",
857 Client_ID(Client), Client->cloaked);
861 Client_Modes( CLIENT *Client )
863 assert( Client != NULL );
864 return Client->modes;
869 Client_Flags( CLIENT *Client )
871 assert( Client != NULL );
872 return Client->flags;
877 Client_Hops( CLIENT *Client )
879 assert( Client != NULL );
885 Client_Token( CLIENT *Client )
887 assert( Client != NULL );
888 return Client->token;
893 Client_MyToken( CLIENT *Client )
895 assert( Client != NULL );
896 return Client->mytoken;
897 } /* Client_MyToken */
901 Client_NextHop( CLIENT *Client )
905 assert( Client != NULL );
908 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
912 } /* Client_NextHop */
916 * Return ID of a client: "client!user@host"
917 * This client ID is used for IRC prefixes, for example.
918 * Please note that this function uses a global static buffer, so you can't
919 * nest invocations without overwriting earlier results!
920 * @param Client Pointer to client structure
921 * @return Pointer to global buffer containing the client ID
924 Client_Mask( CLIENT *Client )
926 static char Mask_Buffer[GETID_LEN];
928 assert (Client != NULL);
930 /* Servers: return name only, there is no "mask" */
931 if (Client->type == CLIENT_SERVER)
934 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
935 Client->id, Client->user, Client->host);
941 * Return ID of a client with cloaked hostname: "client!user@server-name"
943 * This client ID is used for IRC prefixes, for example.
944 * Please note that this function uses a global static buffer, so you can't
945 * nest invocations without overwriting earlier results!
946 * If the client has not enabled cloaking, the real hostname is used.
948 * @param Client Pointer to client structure
949 * @return Pointer to global buffer containing the client ID
952 Client_MaskCloaked(CLIENT *Client)
954 static char Mask_Buffer[GETID_LEN];
956 assert (Client != NULL);
958 /* Is the client using cloaking at all? */
959 if (!Client_HasMode(Client, 'x'))
960 return Client_Mask(Client);
962 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
963 Client_HostnameDisplayed(Client));
966 } /* Client_MaskCloaked */
970 Client_Introducer( CLIENT *Client )
972 assert( Client != NULL );
973 return Client->introducer;
974 } /* Client_Introducer */
978 Client_TopServer( CLIENT *Client )
980 assert( Client != NULL );
981 return Client->topserver;
982 } /* Client_TopServer */
986 Client_HasMode( CLIENT *Client, char Mode )
988 assert( Client != NULL );
989 return strchr( Client->modes, Mode ) != NULL;
990 } /* Client_HasMode */
994 Client_HasFlag( CLIENT *Client, char Flag )
996 assert( Client != NULL );
997 return strchr( Client->flags, Flag ) != NULL;
998 } /* Client_HasFlag */
1002 Client_Away( CLIENT *Client )
1004 assert( Client != NULL );
1005 return Client->away;
1010 Client_AccountName(CLIENT *Client)
1012 assert(Client != NULL);
1013 return Client->account_name;
1018 * Make sure that a given nickname is valid.
1020 * If the nickname is not valid for the given client, this function sends back
1021 * the appropriate error messages.
1023 * @param Client Client that wants to change the nickname.
1024 * @param Nick New nickname.
1025 * @returns true if nickname is valid, false otherwise.
1028 Client_CheckNick(CLIENT *Client, char *Nick)
1030 assert(Client != NULL);
1031 assert(Nick != NULL);
1033 if (!Client_IsValidNick(Nick)) {
1034 if (strlen(Nick ) >= Conf_MaxNickLength)
1035 IRC_WriteErrClient(Client, ERR_NICKNAMETOOLONG_MSG,
1036 Client_ID(Client), Nick,
1037 Conf_MaxNickLength - 1);
1039 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1040 Client_ID(Client), Nick);
1044 if (Client_Type(Client) != CLIENT_SERVER
1045 && Client_Type(Client) != CLIENT_SERVICE) {
1046 /* Make sure that this isn't a restricted/forbidden nickname */
1047 if (Conf_NickIsBlocked(Nick)) {
1048 IRC_WriteErrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
1049 Client_ID(Client), Nick);
1054 /* Nickname already registered? */
1055 if (Client_Search(Nick)) {
1056 IRC_WriteErrClient(Client, ERR_NICKNAMEINUSE_MSG,
1057 Client_ID(Client), Nick);
1062 } /* Client_CheckNick */
1066 Client_CheckID( CLIENT *Client, char *ID )
1068 char str[COMMAND_LEN];
1071 assert( Client != NULL );
1072 assert( Client->conn_id > NONE );
1073 assert( ID != NULL );
1076 if (strlen(ID) > CLIENT_ID_LEN) {
1077 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1078 Client_ID(Client), ID);
1082 /* ID already in use? */
1085 if (strcasecmp(c->id, ID) == 0) {
1086 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
1087 if (c->conn_id != NONE)
1088 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
1090 Log(LOG_ERR, "%s (via network)!", str);
1091 Conn_Close(Client->conn_id, str, str, true);
1094 c = (CLIENT *)c->next;
1098 } /* Client_CheckID */
1102 Client_First( void )
1105 } /* Client_First */
1109 Client_Next( CLIENT *c )
1111 assert( c != NULL );
1112 return (CLIENT *)c->next;
1117 Client_UserCount( void )
1119 return Count( CLIENT_USER );
1120 } /* Client_UserCount */
1124 Client_ServiceCount( void )
1126 return Count( CLIENT_SERVICE );;
1127 } /* Client_ServiceCount */
1131 Client_ServerCount( void )
1133 return Count( CLIENT_SERVER );
1134 } /* Client_ServerCount */
1138 Client_MyUserCount( void )
1140 return MyCount( CLIENT_USER );
1141 } /* Client_MyUserCount */
1145 Client_MyServiceCount( void )
1147 return MyCount( CLIENT_SERVICE );
1148 } /* Client_MyServiceCount */
1151 GLOBAL unsigned long
1152 Client_MyServerCount( void )
1155 unsigned long cnt = 0;
1160 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
1161 c = (CLIENT *)c->next;
1164 } /* Client_MyServerCount */
1167 GLOBAL unsigned long
1168 Client_OperCount( void )
1171 unsigned long cnt = 0;
1176 if (c && c->type == CLIENT_USER && Client_HasMode(c, 'o' ))
1178 c = (CLIENT *)c->next;
1181 } /* Client_OperCount */
1184 GLOBAL unsigned long
1185 Client_UnknownCount( void )
1188 unsigned long cnt = 0;
1193 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1194 c = (CLIENT *)c->next;
1198 } /* Client_UnknownCount */
1202 Client_MaxUserCount( void )
1205 } /* Client_MaxUserCount */
1209 Client_MyMaxUserCount( void )
1211 return My_Max_Users;
1212 } /* Client_MyMaxUserCount */
1216 * Check that a given nickname is valid.
1218 * @param Nick the nickname to check.
1219 * @returns true if nickname is valid, false otherwise.
1222 Client_IsValidNick(const char *Nick)
1225 static const char goodchars[] = ";0123456789-";
1227 assert (Nick != NULL);
1229 if (strchr(goodchars, Nick[0]))
1231 if (strlen(Nick ) >= Conf_MaxNickLength)
1236 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1244 } /* Client_IsValidNick */
1248 * Return pointer to "My_Whowas" structure.
1251 Client_GetWhowas( void )
1254 } /* Client_GetWhowas */
1257 * Return the index of the last used WHOWAS entry.
1260 Client_GetLastWhowasIndex( void )
1263 } /* Client_GetLastWhowasIndex */
1267 * Get the start time of this client.
1268 * The result is the start time in seconds since 1970-01-01, as reported
1269 * by the C function time(NULL).
1272 Client_StartTime(CLIENT *Client)
1274 assert( Client != NULL );
1275 return Client->starttime;
1276 } /* Client_Uptime */
1280 * Reject a client when logging in.
1282 * This function is called when a client isn't allowed to connect to this
1283 * server. Possible reasons are bad server password, bad PAM password,
1284 * or that the client is G/K-Line'd.
1286 * After calling this function, the client isn't connected any more.
1288 * @param Client The client to reject.
1289 * @param Reason The reason why the client has been rejected.
1290 * @param InformClient If true, send the exact reason to the client.
1293 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1295 char info[COMMAND_LEN];
1297 assert(Client != NULL);
1298 assert(Reason != NULL);
1301 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1303 strcpy(info, "Access denied: Bad password?");
1306 "User \"%s\" rejected (connection %d): %s!",
1307 Client_Mask(Client), Client_Conn(Client), Reason);
1308 Conn_Close(Client_Conn(Client), Reason, info, true);
1313 * Introduce a new user or service client in the network.
1315 * @param From Remote server introducing the client or NULL (local).
1316 * @param Client New client.
1317 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1320 Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
1322 /* Set client type (user or service) */
1323 Client_SetType(Client, Type);
1326 if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
1328 Client_SetType(Client, CLIENT_SERVICE);
1329 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1330 Client_TypeText(Client), Client_Mask(Client),
1331 Client_Modes(Client), Client_ID(From),
1332 Client_ID(Client_Introducer(Client)),
1333 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1335 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1336 Client_TypeText(Client), Client_Mask(Client),
1337 Client_Conn(Client));
1338 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1339 Client_ID(Client), Client_User(Client),
1340 Client_Hostname(Client),
1341 Conn_IPA(Client_Conn(Client)),
1342 Client_TypeText(Client));
1345 /* Inform other servers */
1346 IRC_WriteStrServersPrefixFlag_CB(From,
1347 From != NULL ? From : Client_ThisServer(),
1348 '\0', cb_introduceClient, (void *)Client);
1349 } /* Client_Introduce */
1352 static unsigned long
1353 Count( CLIENT_TYPE Type )
1356 unsigned long cnt = 0;
1361 if( c->type == Type ) cnt++;
1362 c = (CLIENT *)c->next;
1368 static unsigned long
1369 MyCount( CLIENT_TYPE Type )
1372 unsigned long cnt = 0;
1377 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1378 c = (CLIENT *)c->next;
1385 * Allocate and initialize new CLIENT strcuture.
1387 * @return Pointer to CLIENT structure or NULL on error.
1390 New_Client_Struct( void )
1394 c = (CLIENT *)malloc( sizeof( CLIENT ));
1397 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1401 memset( c, 0, sizeof ( CLIENT ));
1403 c->type = CLIENT_UNKNOWN;
1413 * Free a CLIENT structure and its member variables.
1416 Free_Client(CLIENT **Client)
1418 assert(Client != NULL);
1419 assert(*Client != NULL);
1421 if ((*Client)->account_name)
1422 free((*Client)->account_name);
1423 if ((*Client)->away)
1424 free((*Client)->away);
1425 if ((*Client)->cloaked)
1426 free((*Client)->cloaked);
1427 if ((*Client)->ipa_text)
1428 free((*Client)->ipa_text);
1435 Generate_MyToken( CLIENT *Client )
1444 if( c->mytoken == token )
1446 /* The token is already in use */
1451 else c = (CLIENT *)c->next;
1453 Client->mytoken = token;
1454 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1455 } /* Generate_MyToken */
1459 Adjust_Counters( CLIENT *Client )
1463 assert( Client != NULL );
1465 if( Client->type != CLIENT_USER ) return;
1467 if( Client->conn_id != NONE )
1469 /* Local connection */
1470 count = Client_MyUserCount( );
1471 if( count > My_Max_Users ) My_Max_Users = count;
1473 count = Client_UserCount( );
1474 if( count > Max_Users ) Max_Users = count;
1475 } /* Adjust_Counters */
1479 * Register client in My_Whowas structure for further recall by WHOWAS.
1480 * Note: Only clients that have been connected at least 30 seconds will be
1481 * registered to prevent automated IRC bots to "destroy" a nice server
1485 Client_RegisterWhowas( CLIENT *Client )
1490 assert( Client != NULL );
1492 /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1493 if (Conf_MorePrivacy)
1497 /* Don't register clients that were connected less than 30 seconds. */
1498 if( now - Client->starttime < 30 )
1501 slot = Last_Whowas + 1;
1502 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1505 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1508 My_Whowas[slot].time = now;
1509 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1510 sizeof( My_Whowas[slot].id ));
1511 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1512 sizeof( My_Whowas[slot].user ));
1513 strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
1514 sizeof( My_Whowas[slot].host ));
1515 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1516 sizeof( My_Whowas[slot].info ));
1517 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1518 sizeof( My_Whowas[slot].server ));
1521 } /* Client_RegisterWhowas */
1525 Client_TypeText(CLIENT *Client)
1527 assert(Client != NULL);
1528 switch (Client_Type(Client)) {
1532 case CLIENT_SERVICE:
1541 } /* Client_TypeText */
1545 * Destroy user or service client.
1548 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1550 if(Client->conn_id != NONE) {
1551 /* Local (directly connected) client */
1553 "%s \"%s\" unregistered (connection %d): %s.",
1554 Client_TypeText(Client), Client_Mask(Client),
1555 Client->conn_id, Txt);
1556 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1557 Client_ID(Client), Client_User(Client),
1558 Client_Hostname(Client), Txt);
1561 /* Inforam all the other servers */
1563 IRC_WriteStrServersPrefix(NULL,
1564 Client, "QUIT :%s", FwdMsg );
1566 IRC_WriteStrServersPrefix(NULL,
1571 LogDebug("%s \"%s\" unregistered: %s.",
1572 Client_TypeText(Client), Client_Mask(Client), Txt);
1575 /* Inform all the other servers, but the ones in the
1576 * direction we got the QUIT from */
1578 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1579 Client, "QUIT :%s", FwdMsg );
1581 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1586 /* Unregister client from channels */
1587 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1589 /* Register client in My_Whowas structure */
1590 Client_RegisterWhowas(Client);
1591 } /* Destroy_UserOrService */
1595 * Introduce a new user or service client to a remote server.
1597 * @param To The remote server to inform.
1598 * @param Prefix Prefix for the generated commands.
1599 * @param data CLIENT structure of the new client.
1602 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1604 CLIENT *c = (CLIENT *)data;
1606 (void)Client_Announce(To, Prefix, c);
1608 } /* cb_introduceClient */
1612 * Announce an user or service to a server.
1614 * This function differentiates between RFC1459 and RFC2813 server links and
1615 * generates the appropriate commands to register the user or service.
1617 * @param Client Server
1618 * @param Prefix Prefix for the generated commands
1619 * @param User User to announce
1622 Client_Announce(CLIENT * Client, CLIENT * Prefix, CLIENT * User)
1625 char *modes, *user, *host;
1627 modes = Client_Modes(User);
1628 user = Client_User(User) ? Client_User(User) : "-";
1629 host = Client_Hostname(User) ? Client_Hostname(User) : "-";
1631 conn = Client_Conn(Client);
1632 if (Conn_Options(conn) & CONN_RFC1459) {
1633 /* RFC 1459 mode: separate NICK and USER commands */
1634 if (! Conn_WriteStr(conn, "NICK %s :%d",
1635 Client_ID(User), Client_Hops(User) + 1))
1636 return DISCONNECTED;
1637 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1638 Client_ID(User), user, host,
1639 Client_ID(Client_Introducer(User)),
1641 return DISCONNECTED;
1643 if (! Conn_WriteStr(conn, ":%s MODE %s +%s",
1644 Client_ID(User), Client_ID(User),
1646 return DISCONNECTED;
1649 /* RFC 2813 mode: one combined NICK or SERVICE command */
1650 if (Client_Type(User) == CLIENT_SERVICE
1651 && Client_HasFlag(Client, 'S')) {
1652 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1653 "SERVICE %s %d * +%s %d :%s",
1655 Client_MyToken(Client_Introducer(User)),
1656 modes, Client_Hops(User) + 1,
1658 return DISCONNECTED;
1660 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1661 "NICK %s %d %s %s %d +%s :%s",
1662 Client_ID(User), Client_Hops(User) + 1,
1664 Client_MyToken(Client_Introducer(User)),
1665 modes, Client_Info(User)))
1666 return DISCONNECTED;
1670 if (Client_HasFlag(Client, 'M')) {
1671 /* Synchronize metadata */
1672 if (Client_HostnameCloaked(User)) {
1673 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1674 "METADATA %s cloakhost :%s",
1676 Client_HostnameCloaked(User)))
1677 return DISCONNECTED;
1680 if (Client_AccountName(User)) {
1681 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1682 "METADATA %s accountname :%s",
1684 Client_AccountName(User)))
1685 return DISCONNECTED;
1688 if (Conn_GetCertFp(Client_Conn(User))) {
1689 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1690 "METADATA %s certfp :%s",
1692 Conn_GetCertFp(Client_Conn(User))))
1693 return DISCONNECTED;
1698 } /* Client_Announce */
1704 Client_DebugDump(void)
1708 Log(LOG_DEBUG, "Client status:");
1712 " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1713 Client_ID(c), Client_Type(c), Client_Hostname(c),
1714 Client_User(c), Client_Conn(c), Client_StartTime(c),
1716 c = (CLIENT *)c->next;
1718 } /* Client_DumpClients */