2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
40 #include "conn-func.h"
42 #include "irc-write.h"
49 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
51 static CLIENT *This_Server, *My_Clients;
53 static WHOWAS My_Whowas[MAX_WHOWAS];
54 static int Last_Whowas = -1;
55 static long Max_Users, My_Max_Users;
58 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
59 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
61 static CLIENT *New_Client_Struct PARAMS(( void ));
62 static void Generate_MyToken PARAMS(( CLIENT *Client ));
63 static void Adjust_Counters PARAMS(( CLIENT *Client ));
65 static void Free_Client PARAMS(( CLIENT **Client ));
67 static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
68 CLIENT *TopServer, int Type, const char *ID,
69 const char *User, const char *Hostname, const char *Info,
70 int Hops, int Token, const char *Modes,
73 static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
76 static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
84 This_Server = New_Client_Struct( );
87 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
88 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
92 /* Client structure for this server */
93 This_Server->next = NULL;
94 This_Server->type = CLIENT_SERVER;
95 This_Server->conn_id = NONE;
96 This_Server->introducer = This_Server;
97 This_Server->mytoken = 1;
98 This_Server->hops = 0;
100 gethostname( This_Server->host, CLIENT_HOST_LEN );
102 h = gethostbyname( This_Server->host );
103 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
105 Client_SetID( This_Server, Conf_ServerName );
106 Client_SetInfo( This_Server, Conf_ServerInfo );
108 My_Clients = This_Server;
110 memset( &My_Whowas, 0, sizeof( My_Whowas ));
120 if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
121 else Client_Destroy( This_Server, "Server going down.", NULL, false );
127 next = (CLIENT *)c->next;
132 Log(LOG_INFO, "Freed %d client structure%s.",
133 cnt, cnt == 1 ? "" : "s");
138 Client_ThisServer( void )
141 } /* Client_ThisServer */
145 * Initialize new local client; wrapper function for Init_New_Client().
146 * @return New CLIENT structure.
149 Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
151 return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
152 Hostname, NULL, 0, 0, NULL, Idented);
153 } /* Client_NewLocal */
157 * Initialize new remote server; wrapper function for Init_New_Client().
158 * @return New CLIENT structure.
161 Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
162 int Hops, int Token, const char *Info, bool Idented)
164 return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
165 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
166 } /* Client_NewRemoteServer */
170 * Initialize new remote client; wrapper function for Init_New_Client().
171 * @return New CLIENT structure.
174 Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
175 const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
177 return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
178 User, Hostname, Info, Hops, Token, Modes, Idented);
179 } /* Client_NewRemoteUser */
183 * Initialize new client and set up the given parameters like client type,
184 * user name, host name, introducing server etc. ...
185 * @return New CLIENT structure.
188 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
189 int Type, const char *ID, const char *User, const char *Hostname,
190 const char *Info, int Hops, int Token, const char *Modes, bool Idented)
195 assert(Introducer != NULL);
197 client = New_Client_Struct();
201 client->starttime = time(NULL);
202 client->conn_id = Idx;
203 client->introducer = Introducer;
204 client->topserver = TopServer;
207 Client_SetID(client, ID);
209 Client_SetUser(client, User, Idented);
210 Client_SetOrigUser(client, User);
213 Client_SetHostname(client, Hostname);
215 Client_SetInfo(client, Info);
217 client->token = Token;
219 Client_SetModes(client, Modes);
220 if (Type == CLIENT_SERVER)
221 Generate_MyToken(client);
223 if (Client_HasMode(client, 'a'))
224 client->away = strndup(DEFAULT_AWAY_MSG, CLIENT_AWAY_LEN - 1);
226 client->next = (POINTER *)My_Clients;
229 Adjust_Counters(client);
232 } /* Init_New_Client */
236 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
238 /* remove a client */
241 char msg[COMMAND_LEN];
244 assert( Client != NULL );
246 txt = LogMsg ? LogMsg : FwdMsg;
248 txt = "Reason unknown";
250 /* netsplit message */
251 if( Client->type == CLIENT_SERVER ) {
252 strlcpy(msg, This_Server->id, sizeof (msg));
253 strlcat(msg, " ", sizeof (msg));
254 strlcat(msg, Client->id, sizeof (msg));
261 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
264 * The client that is about to be removed is a server,
265 * the client we are checking right now is a child of that
266 * server and thus has to be removed, too.
268 * Call Client_Destroy() recursively with the server as the
269 * new "object to be removed". This starts the cycle again, until
270 * all servers that are linked via the original server have been
273 Client_Destroy( c, NULL, msg, false );
280 /* found the client: remove it */
281 if( last ) last->next = c->next;
282 else My_Clients = (CLIENT *)c->next;
284 if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
285 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
286 else if( c->type == CLIENT_SERVER )
288 if (c != This_Server) {
289 if (c->conn_id != NONE)
290 Log(LOG_NOTICE|LOG_snotice,
291 "Server \"%s\" unregistered (connection %d): %s.",
292 c->id, c->conn_id, txt);
294 Log(LOG_NOTICE|LOG_snotice,
295 "Server \"%s\" unregistered: %s.",
299 /* inform other servers */
300 if( ! NGIRCd_SignalQuit )
302 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
303 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
308 if (c->conn_id != NONE) {
311 "Client \"%s\" unregistered (connection %d): %s.",
312 c->id, c->conn_id, txt);
315 "Client unregistered (connection %d): %s.",
319 "Unregistered unknown client \"%s\": %s",
320 c->id[0] ? c->id : "(No Nick)", txt);
328 c = (CLIENT *)c->next;
330 } /* Client_Destroy */
334 * Set client hostname.
336 * If global hostname cloaking is in effect, don't set the real hostname
337 * but the configured one.
339 * @param Client The client of which the hostname should be set.
340 * @param Hostname The new hostname.
343 Client_SetHostname( CLIENT *Client, const char *Hostname )
345 assert(Client != NULL);
346 assert(Hostname != NULL);
348 if (strlen(Conf_CloakHost)) {
349 char cloak[GETID_LEN];
351 strlcpy(cloak, Hostname, GETID_LEN);
352 strlcat(cloak, Conf_CloakHostSalt, GETID_LEN);
353 snprintf(cloak, GETID_LEN, Conf_CloakHost, Hash(cloak));
355 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
356 Client_ID(Client), Client->host, cloak);
357 strlcpy(Client->host, cloak, sizeof(Client->host));
359 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
360 Client_ID(Client), Client->host, Hostname);
361 strlcpy(Client->host, Hostname, sizeof(Client->host));
363 } /* Client_SetHostname */
367 * Set IP address to display for a client.
369 * @param Client The client.
370 * @param IPAText Textual representation of the IP address or NULL to unset.
373 Client_SetIPAText(CLIENT *Client, const char *IPAText)
375 assert(Client != NULL);
377 if (Client->ipa_text)
378 free(Client->ipa_text);
381 Client->ipa_text = strndup(IPAText, CLIENT_HOST_LEN - 1);
383 Client->ipa_text = NULL;
388 Client_SetID( CLIENT *Client, const char *ID )
390 assert( Client != NULL );
391 assert( ID != NULL );
393 strlcpy( Client->id, ID, sizeof( Client->id ));
395 if (Conf_CloakUserToNick) {
396 strlcpy( Client->user, ID, sizeof( Client->user ));
397 strlcpy( Client->info, ID, sizeof( Client->info ));
401 Client->hash = Hash( Client->id );
406 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
408 /* set clients username */
410 assert( Client != NULL );
411 assert( User != NULL );
413 if (Conf_CloakUserToNick) {
414 strlcpy(Client->user, Client->id, sizeof(Client->user));
415 } else if (Idented) {
416 strlcpy(Client->user, User, sizeof(Client->user));
418 Client->user[0] = '~';
419 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
421 } /* Client_SetUser */
425 * Set "original" user name of a client.
426 * This function saves the "original" user name, the user name specified by
427 * the peer using the USER command, into the CLIENT structure. This user
428 * name may be used for authentication, for example.
429 * @param Client The client.
430 * @param User User name to set.
433 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
435 assert(Client != NULL);
436 assert(User != NULL);
438 #if defined(PAM) && defined(IDENTAUTH)
439 strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
441 } /* Client_SetOrigUser */
445 Client_SetInfo( CLIENT *Client, const char *Info )
447 /* set client hostname */
449 assert( Client != NULL );
450 assert( Info != NULL );
452 if (Conf_CloakUserToNick)
453 strlcpy(Client->info, Client->id, sizeof(Client->info));
455 strlcpy(Client->info, Info, sizeof(Client->info));
456 } /* Client_SetInfo */
460 Client_SetModes( CLIENT *Client, const char *Modes )
462 assert( Client != NULL );
463 assert( Modes != NULL );
465 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
466 } /* Client_SetModes */
470 Client_SetFlags( CLIENT *Client, const char *Flags )
472 assert( Client != NULL );
473 assert( Flags != NULL );
475 strlcpy(Client->flags, Flags, sizeof(Client->flags));
476 } /* Client_SetFlags */
480 Client_SetAccountName(CLIENT *Client, const char *AccountName)
482 assert(Client != NULL);
484 if (Client->account_name)
485 free(Client->account_name);
488 Client->account_name = strndup(AccountName,
489 CLIENT_NICK_LEN - 1);
491 Client->account_name = NULL;
496 Client_SetAway( CLIENT *Client, const char *Txt )
498 /* Set AWAY reason of client */
500 assert( Client != NULL );
501 assert( Txt != NULL );
506 Client->away = strndup(Txt, CLIENT_AWAY_LEN - 1);
508 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
509 Client_Mask(Client), Txt);
510 } /* Client_SetAway */
514 Client_SetType( CLIENT *Client, int Type )
516 assert( Client != NULL );
518 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
519 Adjust_Counters( Client );
520 } /* Client_SetType */
524 Client_SetHops( CLIENT *Client, int Hops )
526 assert( Client != NULL );
528 } /* Client_SetHops */
532 Client_SetToken( CLIENT *Client, int Token )
534 assert( Client != NULL );
535 Client->token = Token;
536 } /* Client_SetToken */
540 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
542 assert( Client != NULL );
543 assert( Introducer != NULL );
544 Client->introducer = Introducer;
545 } /* Client_SetIntroducer */
549 Client_ModeAdd( CLIENT *Client, char Mode )
552 * If Client already had Mode, return false.
553 * If the Mode was newly set, return true.
558 assert( Client != NULL );
560 x[0] = Mode; x[1] = '\0';
561 if (!Client_HasMode(Client, x[0])) {
562 strlcat( Client->modes, x, sizeof( Client->modes ));
566 } /* Client_ModeAdd */
570 Client_ModeDel( CLIENT *Client, char Mode )
573 * If Mode was removed, return true.
574 * If Client did not have Mode, return false.
579 assert( Client != NULL );
581 x[0] = Mode; x[1] = '\0';
583 p = strchr( Client->modes, x[0] );
584 if( ! p ) return false;
586 /* Client has Mode -> delete */
593 } /* Client_ModeDel */
597 * Search CLIENT structure of a given nick name.
599 * @return Pointer to CLIENT structure or NULL if not found.
602 Client_Search( const char *Nick )
604 char search_id[CLIENT_ID_LEN], *ptr;
608 assert( Nick != NULL );
610 /* copy Nick and truncate hostmask if necessary */
611 strlcpy( search_id, Nick, sizeof( search_id ));
612 ptr = strchr( search_id, '!' );
613 if( ptr ) *ptr = '\0';
615 search_hash = Hash(search_id);
619 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
621 c = (CLIENT *)c->next;
628 * Search first CLIENT structure matching a given mask of a server.
630 * The order of servers is arbitrary, but this function makes sure that the
631 * local server is always returned if the mask matches it.
633 * @return Pointer to CLIENT structure or NULL if no server could be found.
636 Client_SearchServer(const char *Mask)
640 assert(Mask != NULL);
642 /* First check if mask matches the local server */
643 if (MatchCaseInsensitive(Mask, Client_ID(Client_ThisServer())))
644 return Client_ThisServer();
648 if (Client_Type(c) == CLIENT_SERVER) {
649 /* This is a server: check if Mask matches */
650 if (MatchCaseInsensitive(Mask, c->id))
653 c = (CLIENT *)c->next;
660 * Get client structure ("introducer") identfied by a server token.
661 * @return CLIENT structure or NULL if none could be found.
664 Client_GetFromToken( CLIENT *Client, int Token )
668 assert( Client != NULL );
675 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
678 c = (CLIENT *)c->next;
681 } /* Client_GetFromToken */
685 Client_Type( CLIENT *Client )
687 assert( Client != NULL );
693 Client_Conn( CLIENT *Client )
695 assert( Client != NULL );
696 return Client->conn_id;
701 Client_ID( CLIENT *Client )
703 assert( Client != NULL );
706 if(Client->type == CLIENT_USER)
707 assert(strlen(Client->id) < Conf_MaxNickLength);
710 if( Client->id[0] ) return Client->id;
716 Client_Info( CLIENT *Client )
718 assert( Client != NULL );
724 Client_User( CLIENT *Client )
726 assert( Client != NULL );
727 return Client->user[0] ? Client->user : "~";
734 * Get the "original" user name as supplied by the USER command.
735 * The user name as given by the client is used for authentication instead
736 * of the one detected using IDENT requests.
737 * @param Client The client.
738 * @return Original user name.
741 Client_OrigUser(CLIENT *Client) {
743 char *user = Client->user;
749 return Client->orig_user;
751 } /* Client_OrigUser */
756 * Return the hostname of a client.
757 * @param Client Pointer to client structure
758 * @return Pointer to client hostname
761 Client_Hostname(CLIENT *Client)
763 assert (Client != NULL);
768 * Return the cloaked hostname of a client, if set.
769 * @param Client Pointer to the client structure.
770 * @return Pointer to the cloaked hostname or NULL if not set.
773 Client_HostnameCloaked(CLIENT *Client)
775 assert(Client != NULL);
776 return Client->cloaked;
780 * Get (potentially cloaked) hostname of a client to display it to other users.
782 * If the client has not enabled cloaking, the real hostname is used.
784 * @param Client Pointer to client structure
785 * @return Pointer to client hostname
788 Client_HostnameDisplayed(CLIENT *Client)
790 assert(Client != NULL);
792 /* Client isn't cloaked at all, return real hostname: */
793 if (!Client_HasMode(Client, 'x'))
794 return Client_Hostname(Client);
796 /* Use an already saved cloaked hostname, if there is one */
798 return Client->cloaked;
800 Client_UpdateCloakedHostname(Client, NULL, NULL);
801 return Client->cloaked;
805 Client_IPAText(CLIENT *Client)
807 assert(Client != NULL);
809 /* Not a local client? */
810 if (Client_Conn(Client) <= NONE)
813 if (!Client->ipa_text)
814 return Conn_GetIPAInfo(Client_Conn(Client));
816 return Client->ipa_text;
820 * Update (and generate, if necessary) the cloaked hostname of a client.
822 * The newly set cloaked hostname is announced in the network using METADATA
823 * commands to peers that support this feature.
825 * @param Client The client of which the cloaked hostname should be updated.
826 * @param Origin The originator of the hostname change, or NULL if this server.
827 * @param Hostname The new cloaked hostname, or NULL if it should be generated.
830 Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
831 const char *Hostname)
833 char Cloak_Buffer[CLIENT_HOST_LEN];
835 assert(Client != NULL);
837 Origin = Client_ThisServer();
839 if (!Client->cloaked) {
840 Client->cloaked = malloc(CLIENT_HOST_LEN);
841 if (!Client->cloaked)
846 /* Generate new cloaked hostname */
847 if (*Conf_CloakHostModeX) {
848 strlcpy(Cloak_Buffer, Client->host,
849 sizeof(Cloak_Buffer));
850 strlcat(Cloak_Buffer, Conf_CloakHostSalt,
851 sizeof(Cloak_Buffer));
852 snprintf(Client->cloaked, CLIENT_HOST_LEN,
853 Conf_CloakHostModeX, Hash(Cloak_Buffer));
855 strlcpy(Client->cloaked, Client_ID(Client->introducer),
858 strlcpy(Client->cloaked, Hostname, CLIENT_HOST_LEN);
859 LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
860 Client_ID(Client), Client->cloaked);
862 /* Inform other servers in the network */
863 IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
864 "METADATA %s cloakhost :%s",
865 Client_ID(Client), Client->cloaked);
869 Client_Modes( CLIENT *Client )
871 assert( Client != NULL );
872 return Client->modes;
877 Client_Flags( CLIENT *Client )
879 assert( Client != NULL );
880 return Client->flags;
885 Client_Hops( CLIENT *Client )
887 assert( Client != NULL );
893 Client_Token( CLIENT *Client )
895 assert( Client != NULL );
896 return Client->token;
901 Client_MyToken( CLIENT *Client )
903 assert( Client != NULL );
904 return Client->mytoken;
905 } /* Client_MyToken */
909 Client_NextHop( CLIENT *Client )
913 assert( Client != NULL );
916 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
920 } /* Client_NextHop */
924 * Return ID of a client: "client!user@host"
925 * This client ID is used for IRC prefixes, for example.
926 * Please note that this function uses a global static buffer, so you can't
927 * nest invocations without overwriting earlier results!
928 * @param Client Pointer to client structure
929 * @return Pointer to global buffer containing the client ID
932 Client_Mask( CLIENT *Client )
934 static char Mask_Buffer[GETID_LEN];
936 assert (Client != NULL);
938 /* Servers: return name only, there is no "mask" */
939 if (Client->type == CLIENT_SERVER)
942 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
943 Client->id, Client->user, Client->host);
949 * Return ID of a client with cloaked hostname: "client!user@server-name"
951 * This client ID is used for IRC prefixes, for example.
952 * Please note that this function uses a global static buffer, so you can't
953 * nest invocations without overwriting earlier results!
954 * If the client has not enabled cloaking, the real hostname is used.
956 * @param Client Pointer to client structure
957 * @return Pointer to global buffer containing the client ID
960 Client_MaskCloaked(CLIENT *Client)
962 static char Mask_Buffer[GETID_LEN];
964 assert (Client != NULL);
966 /* Is the client using cloaking at all? */
967 if (!Client_HasMode(Client, 'x'))
968 return Client_Mask(Client);
970 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
971 Client_HostnameDisplayed(Client));
974 } /* Client_MaskCloaked */
978 Client_Introducer( CLIENT *Client )
980 assert( Client != NULL );
981 return Client->introducer;
982 } /* Client_Introducer */
986 Client_TopServer( CLIENT *Client )
988 assert( Client != NULL );
989 return Client->topserver;
990 } /* Client_TopServer */
994 Client_HasMode( CLIENT *Client, char Mode )
996 assert( Client != NULL );
997 return strchr( Client->modes, Mode ) != NULL;
998 } /* Client_HasMode */
1002 Client_HasFlag( CLIENT *Client, char Flag )
1004 assert( Client != NULL );
1005 return strchr( Client->flags, Flag ) != NULL;
1006 } /* Client_HasFlag */
1010 Client_Away( CLIENT *Client )
1012 assert( Client != NULL );
1013 return Client->away;
1018 Client_AccountName(CLIENT *Client)
1020 assert(Client != NULL);
1021 return Client->account_name;
1026 * Make sure that a given nickname is valid.
1028 * If the nickname is not valid for the given client, this function sends back
1029 * the appropriate error messages.
1031 * @param Client Client that wants to change the nickname.
1032 * @param Nick New nickname.
1033 * @returns true if nickname is valid, false otherwise.
1036 Client_CheckNick(CLIENT *Client, char *Nick)
1038 assert(Client != NULL);
1039 assert(Nick != NULL);
1041 if (!Client_IsValidNick(Nick)) {
1042 if (strlen(Nick ) >= Conf_MaxNickLength)
1043 IRC_WriteErrClient(Client, ERR_NICKNAMETOOLONG_MSG,
1044 Client_ID(Client), Nick,
1045 Conf_MaxNickLength - 1);
1047 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1048 Client_ID(Client), Nick);
1052 if (Client_Type(Client) != CLIENT_SERVER
1053 && Client_Type(Client) != CLIENT_SERVICE) {
1054 /* Make sure that this isn't a restricted/forbidden nickname */
1055 if (Conf_NickIsBlocked(Nick)) {
1056 IRC_WriteErrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
1057 Client_ID(Client), Nick);
1062 /* Nickname already registered? */
1063 if (Client_Search(Nick)) {
1064 IRC_WriteErrClient(Client, ERR_NICKNAMEINUSE_MSG,
1065 Client_ID(Client), Nick);
1070 } /* Client_CheckNick */
1074 Client_CheckID( CLIENT *Client, char *ID )
1076 char str[COMMAND_LEN];
1079 assert( Client != NULL );
1080 assert( Client->conn_id > NONE );
1081 assert( ID != NULL );
1084 if (strlen(ID) > CLIENT_ID_LEN) {
1085 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1086 Client_ID(Client), ID);
1090 /* ID already in use? */
1093 if (strcasecmp(c->id, ID) == 0) {
1094 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
1095 if (c->conn_id != NONE)
1096 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
1098 Log(LOG_ERR, "%s (via network)!", str);
1099 Conn_Close(Client->conn_id, str, str, true);
1102 c = (CLIENT *)c->next;
1106 } /* Client_CheckID */
1110 Client_First( void )
1113 } /* Client_First */
1117 Client_Next( CLIENT *c )
1119 assert( c != NULL );
1120 return (CLIENT *)c->next;
1125 Client_UserCount( void )
1127 return Count( CLIENT_USER );
1128 } /* Client_UserCount */
1132 Client_ServiceCount( void )
1134 return Count( CLIENT_SERVICE );;
1135 } /* Client_ServiceCount */
1139 Client_ServerCount( void )
1141 return Count( CLIENT_SERVER );
1142 } /* Client_ServerCount */
1146 Client_MyUserCount( void )
1148 return MyCount( CLIENT_USER );
1149 } /* Client_MyUserCount */
1153 Client_MyServiceCount( void )
1155 return MyCount( CLIENT_SERVICE );
1156 } /* Client_MyServiceCount */
1159 GLOBAL unsigned long
1160 Client_MyServerCount( void )
1163 unsigned long cnt = 0;
1168 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
1169 c = (CLIENT *)c->next;
1172 } /* Client_MyServerCount */
1175 GLOBAL unsigned long
1176 Client_OperCount( void )
1179 unsigned long cnt = 0;
1184 if (c && c->type == CLIENT_USER && Client_HasMode(c, 'o' ))
1186 c = (CLIENT *)c->next;
1189 } /* Client_OperCount */
1192 GLOBAL unsigned long
1193 Client_UnknownCount( void )
1196 unsigned long cnt = 0;
1201 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1202 c = (CLIENT *)c->next;
1206 } /* Client_UnknownCount */
1210 Client_MaxUserCount( void )
1213 } /* Client_MaxUserCount */
1217 Client_MyMaxUserCount( void )
1219 return My_Max_Users;
1220 } /* Client_MyMaxUserCount */
1224 * Check that a given nickname is valid.
1226 * @param Nick the nickname to check.
1227 * @returns true if nickname is valid, false otherwise.
1230 Client_IsValidNick(const char *Nick)
1233 static const char goodchars[] = ";0123456789-";
1235 assert (Nick != NULL);
1237 if (strchr(goodchars, Nick[0]))
1239 if (strlen(Nick ) >= Conf_MaxNickLength)
1244 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1252 } /* Client_IsValidNick */
1256 * Return pointer to "My_Whowas" structure.
1259 Client_GetWhowas( void )
1262 } /* Client_GetWhowas */
1265 * Return the index of the last used WHOWAS entry.
1268 Client_GetLastWhowasIndex( void )
1271 } /* Client_GetLastWhowasIndex */
1275 * Get the start time of this client.
1276 * The result is the start time in seconds since 1970-01-01, as reported
1277 * by the C function time(NULL).
1280 Client_StartTime(CLIENT *Client)
1282 assert( Client != NULL );
1283 return Client->starttime;
1284 } /* Client_Uptime */
1288 * Reject a client when logging in.
1290 * This function is called when a client isn't allowed to connect to this
1291 * server. Possible reasons are bad server password, bad PAM password,
1292 * or that the client is G/K-Line'd.
1294 * After calling this function, the client isn't connected any more.
1296 * @param Client The client to reject.
1297 * @param Reason The reason why the client has been rejected.
1298 * @param InformClient If true, send the exact reason to the client.
1301 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1303 char info[COMMAND_LEN];
1305 assert(Client != NULL);
1306 assert(Reason != NULL);
1309 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1311 strcpy(info, "Access denied: Bad password?");
1314 "User \"%s\" rejected (connection %d): %s!",
1315 Client_Mask(Client), Client_Conn(Client), Reason);
1316 Conn_Close(Client_Conn(Client), Reason, info, true);
1321 * Introduce a new user or service client in the network.
1323 * @param From Remote server introducing the client or NULL (local).
1324 * @param Client New client.
1325 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1328 Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
1330 /* Set client type (user or service) */
1331 Client_SetType(Client, Type);
1334 if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
1336 Client_SetType(Client, CLIENT_SERVICE);
1337 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1338 Client_TypeText(Client), Client_Mask(Client),
1339 Client_Modes(Client), Client_ID(From),
1340 Client_ID(Client_Introducer(Client)),
1341 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1343 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1344 Client_TypeText(Client), Client_Mask(Client),
1345 Client_Conn(Client));
1346 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1347 Client_ID(Client), Client_User(Client),
1348 Client_Hostname(Client),
1349 Conn_IPA(Client_Conn(Client)),
1350 Client_TypeText(Client));
1353 /* Inform other servers */
1354 IRC_WriteStrServersPrefixFlag_CB(From,
1355 From != NULL ? From : Client_ThisServer(),
1356 '\0', cb_introduceClient, (void *)Client);
1357 } /* Client_Introduce */
1360 static unsigned long
1361 Count( CLIENT_TYPE Type )
1364 unsigned long cnt = 0;
1369 if( c->type == Type ) cnt++;
1370 c = (CLIENT *)c->next;
1376 static unsigned long
1377 MyCount( CLIENT_TYPE Type )
1380 unsigned long cnt = 0;
1385 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1386 c = (CLIENT *)c->next;
1393 * Allocate and initialize new CLIENT strcuture.
1395 * @return Pointer to CLIENT structure or NULL on error.
1398 New_Client_Struct( void )
1402 c = (CLIENT *)malloc( sizeof( CLIENT ));
1405 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1409 memset( c, 0, sizeof ( CLIENT ));
1411 c->type = CLIENT_UNKNOWN;
1421 * Free a CLIENT structure and its member variables.
1424 Free_Client(CLIENT **Client)
1426 assert(Client != NULL);
1427 assert(*Client != NULL);
1429 if ((*Client)->account_name)
1430 free((*Client)->account_name);
1431 if ((*Client)->away)
1432 free((*Client)->away);
1433 if ((*Client)->cloaked)
1434 free((*Client)->cloaked);
1435 if ((*Client)->ipa_text)
1436 free((*Client)->ipa_text);
1443 Generate_MyToken( CLIENT *Client )
1452 if( c->mytoken == token )
1454 /* The token is already in use */
1459 else c = (CLIENT *)c->next;
1461 Client->mytoken = token;
1462 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1463 } /* Generate_MyToken */
1467 Adjust_Counters( CLIENT *Client )
1471 assert( Client != NULL );
1473 if( Client->type != CLIENT_USER ) return;
1475 if( Client->conn_id != NONE )
1477 /* Local connection */
1478 count = Client_MyUserCount( );
1479 if( count > My_Max_Users ) My_Max_Users = count;
1481 count = Client_UserCount( );
1482 if( count > Max_Users ) Max_Users = count;
1483 } /* Adjust_Counters */
1487 * Register client in My_Whowas structure for further recall by WHOWAS.
1488 * Note: Only clients that have been connected at least 30 seconds will be
1489 * registered to prevent automated IRC bots to "destroy" a nice server
1493 Client_RegisterWhowas( CLIENT *Client )
1498 assert( Client != NULL );
1500 /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1501 if (Conf_MorePrivacy)
1505 /* Don't register clients that were connected less than 30 seconds. */
1506 if( now - Client->starttime < 30 )
1509 slot = Last_Whowas + 1;
1510 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1513 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1516 My_Whowas[slot].time = now;
1517 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1518 sizeof( My_Whowas[slot].id ));
1519 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1520 sizeof( My_Whowas[slot].user ));
1521 strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
1522 sizeof( My_Whowas[slot].host ));
1523 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1524 sizeof( My_Whowas[slot].info ));
1525 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1526 sizeof( My_Whowas[slot].server ));
1529 } /* Client_RegisterWhowas */
1533 Client_TypeText(CLIENT *Client)
1535 assert(Client != NULL);
1536 switch (Client_Type(Client)) {
1540 case CLIENT_SERVICE:
1549 } /* Client_TypeText */
1553 * Destroy user or service client.
1556 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1558 if(Client->conn_id != NONE) {
1559 /* Local (directly connected) client */
1561 "%s \"%s\" unregistered (connection %d): %s.",
1562 Client_TypeText(Client), Client_Mask(Client),
1563 Client->conn_id, Txt);
1564 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1565 Client_ID(Client), Client_User(Client),
1566 Client_Hostname(Client), Txt);
1569 /* Inforam all the other servers */
1571 IRC_WriteStrServersPrefix(NULL,
1572 Client, "QUIT :%s", FwdMsg );
1574 IRC_WriteStrServersPrefix(NULL,
1579 LogDebug("%s \"%s\" unregistered: %s.",
1580 Client_TypeText(Client), Client_Mask(Client), Txt);
1583 /* Inform all the other servers, but the ones in the
1584 * direction we got the QUIT from */
1586 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1587 Client, "QUIT :%s", FwdMsg );
1589 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1594 /* Unregister client from channels */
1595 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1597 /* Register client in My_Whowas structure */
1598 Client_RegisterWhowas(Client);
1599 } /* Destroy_UserOrService */
1603 * Introduce a new user or service client to a remote server.
1605 * @param To The remote server to inform.
1606 * @param Prefix Prefix for the generated commands.
1607 * @param data CLIENT structure of the new client.
1610 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1612 CLIENT *c = (CLIENT *)data;
1614 (void)Client_Announce(To, Prefix, c);
1616 } /* cb_introduceClient */
1620 * Announce an user or service to a server.
1622 * This function differentiates between RFC1459 and RFC2813 server links and
1623 * generates the appropriate commands to register the user or service.
1625 * @param Client Server
1626 * @param Prefix Prefix for the generated commands
1627 * @param User User to announce
1630 Client_Announce(CLIENT * Client, CLIENT * Prefix, CLIENT * User)
1633 char *modes, *user, *host;
1635 modes = Client_Modes(User);
1636 user = Client_User(User) ? Client_User(User) : "-";
1637 host = Client_Hostname(User) ? Client_Hostname(User) : "-";
1639 conn = Client_Conn(Client);
1640 if (Conn_Options(conn) & CONN_RFC1459) {
1641 /* RFC 1459 mode: separate NICK and USER commands */
1642 if (! Conn_WriteStr(conn, "NICK %s :%d",
1643 Client_ID(User), Client_Hops(User) + 1))
1644 return DISCONNECTED;
1645 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1646 Client_ID(User), user, host,
1647 Client_ID(Client_Introducer(User)),
1649 return DISCONNECTED;
1651 if (! Conn_WriteStr(conn, ":%s MODE %s +%s",
1652 Client_ID(User), Client_ID(User),
1654 return DISCONNECTED;
1657 /* RFC 2813 mode: one combined NICK or SERVICE command */
1658 if (Client_Type(User) == CLIENT_SERVICE
1659 && Client_HasFlag(Client, 'S')) {
1660 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1661 "SERVICE %s %d * +%s %d :%s",
1663 Client_MyToken(Client_Introducer(User)),
1664 modes, Client_Hops(User) + 1,
1666 return DISCONNECTED;
1668 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1669 "NICK %s %d %s %s %d +%s :%s",
1670 Client_ID(User), Client_Hops(User) + 1,
1672 Client_MyToken(Client_Introducer(User)),
1673 modes, Client_Info(User)))
1674 return DISCONNECTED;
1678 if (Client_HasFlag(Client, 'M')) {
1679 /* Synchronize metadata */
1680 if (Client_HostnameCloaked(User)) {
1681 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1682 "METADATA %s cloakhost :%s",
1684 Client_HostnameCloaked(User)))
1685 return DISCONNECTED;
1688 if (Client_AccountName(User)) {
1689 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1690 "METADATA %s accountname :%s",
1692 Client_AccountName(User)))
1693 return DISCONNECTED;
1696 if (Conn_GetCertFp(Client_Conn(User))) {
1697 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1698 "METADATA %s certfp :%s",
1700 Conn_GetCertFp(Client_Conn(User))))
1701 return DISCONNECTED;
1706 } /* Client_Announce */
1712 Client_DebugDump(void)
1716 Log(LOG_DEBUG, "Client status:");
1720 " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1721 Client_ID(c), Client_Type(c), Client_Hostname(c),
1722 Client_User(c), Client_Conn(c), Client_StartTime(c),
1724 c = (CLIENT *)c->next;
1726 } /* Client_DumpClients */