2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 Alexander Barton (alex@barton.de)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
40 #include "conn-func.h"
42 #include "irc-write.h"
49 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
51 static CLIENT *This_Server, *My_Clients;
53 static WHOWAS My_Whowas[MAX_WHOWAS];
54 static int Last_Whowas = -1;
55 static long Max_Users, My_Max_Users;
58 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
59 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
61 static CLIENT *New_Client_Struct PARAMS(( void ));
62 static void Generate_MyToken PARAMS(( CLIENT *Client ));
63 static void Adjust_Counters PARAMS(( CLIENT *Client ));
65 static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
66 CLIENT *TopServer, int Type, const char *ID,
67 const char *User, const char *Hostname, const char *Info,
68 int Hops, int Token, const char *Modes,
71 static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
74 static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
82 This_Server = New_Client_Struct( );
85 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
86 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
90 /* Client-Struktur dieses Servers */
91 This_Server->next = NULL;
92 This_Server->type = CLIENT_SERVER;
93 This_Server->conn_id = NONE;
94 This_Server->introducer = This_Server;
95 This_Server->mytoken = 1;
96 This_Server->hops = 0;
98 gethostname( This_Server->host, CLIENT_HOST_LEN );
100 h = gethostbyname( This_Server->host );
101 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
103 Client_SetID( This_Server, Conf_ServerName );
104 Client_SetInfo( This_Server, Conf_ServerInfo );
106 My_Clients = This_Server;
108 memset( &My_Whowas, 0, sizeof( My_Whowas ));
118 if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
119 else Client_Destroy( This_Server, "Server going down.", NULL, false );
126 next = (CLIENT *)c->next;
130 if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
135 Client_ThisServer( void )
138 } /* Client_ThisServer */
142 * Initialize new local client; wrapper function for Init_New_Client().
143 * @return New CLIENT structure.
146 Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
148 return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
149 Hostname, NULL, 0, 0, NULL, Idented);
150 } /* Client_NewLocal */
154 * Initialize new remote server; wrapper function for Init_New_Client().
155 * @return New CLIENT structure.
158 Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
159 int Hops, int Token, const char *Info, bool Idented)
161 return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
162 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
163 } /* Client_NewRemoteServer */
167 * Initialize new remote client; wrapper function for Init_New_Client().
168 * @return New CLIENT structure.
171 Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
172 const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
174 return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
175 User, Hostname, Info, Hops, Token, Modes, Idented);
176 } /* Client_NewRemoteUser */
180 * Initialize new client and set up the given parameters like client type,
181 * user name, host name, introducing server etc. ...
182 * @return New CLIENT structure.
185 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
186 int Type, const char *ID, const char *User, const char *Hostname,
187 const char *Info, int Hops, int Token, const char *Modes, bool Idented)
192 assert(Introducer != NULL);
194 client = New_Client_Struct();
198 client->starttime = time(NULL);
199 client->conn_id = Idx;
200 client->introducer = Introducer;
201 client->topserver = TopServer;
204 Client_SetID(client, ID);
206 Client_SetUser(client, User, Idented);
207 Client_SetOrigUser(client, User);
210 Client_SetHostname(client, Hostname);
212 Client_SetInfo(client, Info);
214 client->token = Token;
216 Client_SetModes(client, Modes);
217 if (Type == CLIENT_SERVER)
218 Generate_MyToken(client);
220 if (strchr(client->modes, 'a'))
221 strlcpy(client->away, DEFAULT_AWAY_MSG, sizeof(client->away));
223 client->next = (POINTER *)My_Clients;
226 Adjust_Counters(client);
229 } /* Init_New_Client */
233 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
235 /* remove a client */
241 assert( Client != NULL );
243 txt = LogMsg ? LogMsg : FwdMsg;
245 txt = "Reason unknown";
247 /* netsplit message */
248 if( Client->type == CLIENT_SERVER ) {
249 strlcpy(msg, This_Server->id, sizeof (msg));
250 strlcat(msg, " ", sizeof (msg));
251 strlcat(msg, Client->id, sizeof (msg));
258 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
261 * The client that is about to be removed is a server,
262 * the client we are checking right now is a child of that
263 * server and thus has to be removed, too.
265 * Call Client_Destroy() recursively with the server as the
266 * new "object to be removed". This starts the cycle again, until
267 * all servers that are linked via the original server have been
270 Client_Destroy( c, NULL, msg, false );
277 /* found the client: remove it */
278 if( last ) last->next = c->next;
279 else My_Clients = (CLIENT *)c->next;
281 if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
282 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
283 else if( c->type == CLIENT_SERVER )
285 if (c != This_Server) {
286 if (c->conn_id != NONE)
287 Log(LOG_NOTICE|LOG_snotice,
288 "Server \"%s\" unregistered (connection %d): %s.",
289 c->id, c->conn_id, txt);
291 Log(LOG_NOTICE|LOG_snotice,
292 "Server \"%s\" unregistered: %s.",
296 /* inform other servers */
297 if( ! NGIRCd_SignalQuit )
299 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
300 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
305 if (c->conn_id != NONE) {
308 "Client \"%s\" unregistered (connection %d): %s.",
309 c->id, c->conn_id, txt);
312 "Client unregistered (connection %d): %s.",
316 "Unregistered unknown client \"%s\": %s",
317 c->id[0] ? c->id : "(No Nick)", txt);
325 c = (CLIENT *)c->next;
327 } /* Client_Destroy */
331 * Set client hostname.
333 * If global hostname cloaking is in effect, don't set the real hostname
334 * but the configured one.
336 * @param Client The client of which the hostname should be set.
337 * @param Hostname The new hostname.
340 Client_SetHostname( CLIENT *Client, const char *Hostname )
342 assert(Client != NULL);
343 assert(Hostname != NULL);
345 if (strlen(Conf_CloakHost)) {
346 char cloak[GETID_LEN];
348 strlcpy(cloak, Hostname, GETID_LEN);
349 strlcat(cloak, Conf_CloakHostSalt, GETID_LEN);
350 snprintf(cloak, GETID_LEN, Conf_CloakHost, Hash(cloak));
352 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
353 Client_ID(Client), Client->host, cloak);
354 strlcpy(Client->host, cloak, sizeof(Client->host));
356 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
357 Client_ID(Client), Client->host, Hostname);
358 strlcpy(Client->host, Hostname, sizeof(Client->host));
360 } /* Client_SetHostname */
364 Client_SetID( CLIENT *Client, const char *ID )
366 assert( Client != NULL );
367 assert( ID != NULL );
369 strlcpy( Client->id, ID, sizeof( Client->id ));
371 if (Conf_CloakUserToNick) {
372 strlcpy( Client->user, ID, sizeof( Client->user ));
373 strlcpy( Client->info, ID, sizeof( Client->info ));
377 Client->hash = Hash( Client->id );
382 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
384 /* set clients username */
386 assert( Client != NULL );
387 assert( User != NULL );
389 if (Conf_CloakUserToNick) {
390 strlcpy(Client->user, Client->id, sizeof(Client->user));
391 } else if (Idented) {
392 strlcpy(Client->user, User, sizeof(Client->user));
394 Client->user[0] = '~';
395 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
397 } /* Client_SetUser */
401 * Set "original" user name of a client.
402 * This function saves the "original" user name, the user name specified by
403 * the peer using the USER command, into the CLIENT structure. This user
404 * name may be used for authentication, for example.
405 * @param Client The client.
406 * @param User User name to set.
409 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
411 assert(Client != NULL);
412 assert(User != NULL);
414 #if defined(PAM) && defined(IDENTAUTH)
415 strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
417 } /* Client_SetOrigUser */
421 Client_SetInfo( CLIENT *Client, const char *Info )
423 /* set client hostname */
425 assert( Client != NULL );
426 assert( Info != NULL );
428 if (Conf_CloakUserToNick)
429 strlcpy(Client->info, Client->id, sizeof(Client->info));
431 strlcpy(Client->info, Info, sizeof(Client->info));
432 } /* Client_SetInfo */
436 Client_SetModes( CLIENT *Client, const char *Modes )
438 assert( Client != NULL );
439 assert( Modes != NULL );
441 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
442 } /* Client_SetModes */
446 Client_SetFlags( CLIENT *Client, const char *Flags )
448 assert( Client != NULL );
449 assert( Flags != NULL );
451 strlcpy(Client->flags, Flags, sizeof(Client->flags));
452 } /* Client_SetFlags */
456 Client_SetAway( CLIENT *Client, const char *Txt )
458 /* Set AWAY reason of client */
460 assert( Client != NULL );
461 assert( Txt != NULL );
463 strlcpy( Client->away, Txt, sizeof( Client->away ));
464 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
465 Client_Mask(Client), Txt);
466 } /* Client_SetAway */
470 Client_SetType( CLIENT *Client, int Type )
472 assert( Client != NULL );
474 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
475 Adjust_Counters( Client );
476 } /* Client_SetType */
480 Client_SetHops( CLIENT *Client, int Hops )
482 assert( Client != NULL );
484 } /* Client_SetHops */
488 Client_SetToken( CLIENT *Client, int Token )
490 assert( Client != NULL );
491 Client->token = Token;
492 } /* Client_SetToken */
496 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
498 assert( Client != NULL );
499 assert( Introducer != NULL );
500 Client->introducer = Introducer;
501 } /* Client_SetIntroducer */
505 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
507 assert( Client != NULL );
508 Client->oper_by_me = OperByMe;
509 } /* Client_SetOperByMe */
513 Client_ModeAdd( CLIENT *Client, char Mode )
516 * If Client already alread had Mode, return false.
517 * If the Mode was newly set, return true.
522 assert( Client != NULL );
524 x[0] = Mode; x[1] = '\0';
525 if (!strchr( Client->modes, x[0])) {
526 strlcat( Client->modes, x, sizeof( Client->modes ));
530 } /* Client_ModeAdd */
534 Client_ModeDel( CLIENT *Client, char Mode )
537 * If Mode was removed, return true.
538 * If Client did not have Mode, return false.
543 assert( Client != NULL );
545 x[0] = Mode; x[1] = '\0';
547 p = strchr( Client->modes, x[0] );
548 if( ! p ) return false;
550 /* Client has Mode -> delete */
557 } /* Client_ModeDel */
561 * Search CLIENT structure of a given nick name.
563 * @return Pointer to CLIENT structure or NULL if not found.
566 Client_Search( const char *Nick )
568 char search_id[CLIENT_ID_LEN], *ptr;
572 assert( Nick != NULL );
574 /* copy Nick and truncate hostmask if necessary */
575 strlcpy( search_id, Nick, sizeof( search_id ));
576 ptr = strchr( search_id, '!' );
577 if( ptr ) *ptr = '\0';
579 search_hash = Hash(search_id);
583 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
585 c = (CLIENT *)c->next;
592 * Serach first CLIENT structure matching a given mask of a server.
594 * The order of servers is arbitrary, but this function makes sure that the
595 * local server is always returned if the mask matches it.
597 * @return Pointer to CLIENT structure or NULL if no server could be found.
600 Client_SearchServer(const char *Mask)
604 assert(Mask != NULL);
606 /* First check if mask matches the local server */
607 if (MatchCaseInsensitive(Mask, Client_ID(Client_ThisServer())))
608 return Client_ThisServer();
612 if (Client_Type(c) == CLIENT_SERVER) {
613 /* This is a server: check if Mask matches */
614 if (MatchCaseInsensitive(Mask, c->id))
617 c = (CLIENT *)c->next;
624 * Get client structure ("introducer") identfied by a server token.
625 * @return CLIENT structure or NULL if none could be found.
628 Client_GetFromToken( CLIENT *Client, int Token )
632 assert( Client != NULL );
639 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
642 c = (CLIENT *)c->next;
645 } /* Client_GetFromToken */
649 Client_Type( CLIENT *Client )
651 assert( Client != NULL );
657 Client_Conn( CLIENT *Client )
659 assert( Client != NULL );
660 return Client->conn_id;
665 Client_ID( CLIENT *Client )
667 assert( Client != NULL );
670 if(Client->type == CLIENT_USER)
671 assert(strlen(Client->id) < Conf_MaxNickLength);
674 if( Client->id[0] ) return Client->id;
680 Client_Info( CLIENT *Client )
682 assert( Client != NULL );
688 Client_User( CLIENT *Client )
690 assert( Client != NULL );
691 return Client->user[0] ? Client->user : "~";
698 * Get the "original" user name as supplied by the USER command.
699 * The user name as given by the client is used for authentication instead
700 * of the one detected using IDENT requests.
701 * @param Client The client.
702 * @return Original user name.
705 Client_OrigUser(CLIENT *Client) {
707 char *user = Client->user;
713 return Client->orig_user;
715 } /* Client_OrigUser */
720 * Return the hostname of a client.
721 * @param Client Pointer to client structure
722 * @return Pointer to client hostname
725 Client_Hostname(CLIENT *Client)
727 assert (Client != NULL);
732 * Return the cloaked hostname of a client, if set.
733 * @param Client Pointer to the client structure.
734 * @return Pointer to the cloaked hostname or NULL if not set.
737 Client_HostnameCloaked(CLIENT *Client)
739 assert(Client != NULL);
740 return Client->cloaked;
744 * Get (potentially cloaked) hostname of a client to display it to other users.
746 * If the client has not enabled cloaking, the real hostname is used.
747 * Please note that this function uses a global static buffer, so you can't
748 * nest invocations without overwriting earlier results!
750 * @param Client Pointer to client structure
751 * @return Pointer to client hostname
754 Client_HostnameDisplayed(CLIENT *Client)
756 assert(Client != NULL);
758 /* Client isn't cloaked at all, return real hostname: */
759 if (!Client_HasMode(Client, 'x'))
760 return Client_Hostname(Client);
762 /* Use an already saved cloaked hostname, if there is one */
763 if (Client->cloaked[0])
764 return Client->cloaked;
766 Client_UpdateCloakedHostname(Client, NULL, NULL);
767 return Client->cloaked;
771 * Update (and generate, if necessary) the cloaked hostname of a client.
773 * The newly set cloaked hostname is announced in the network using METADATA
774 * commands to peers that support this feature.
776 * @param Client The client of which the cloaked hostname should be updated.
777 * @param Origin The originator of the hostname change, or NULL if this server.
778 * @param Hostname The new cloaked hostname, or NULL if it should be generated.
781 Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
782 const char *Hostname)
784 static char Cloak_Buffer[CLIENT_HOST_LEN];
786 assert(Client != NULL);
788 Origin = Client_ThisServer();
791 /* Generate new cloaked hostname */
792 if (*Conf_CloakHostModeX) {
793 strlcpy(Cloak_Buffer, Client->host, CLIENT_HOST_LEN);
794 strlcat(Cloak_Buffer, Conf_CloakHostSalt,
796 snprintf(Client->cloaked, sizeof(Client->cloaked),
797 Conf_CloakHostModeX, Hash(Cloak_Buffer));
799 strlcpy(Client->cloaked, Client_ID(Client->introducer),
800 sizeof(Client->cloaked));
802 strlcpy(Client->cloaked, Hostname, sizeof(Client->cloaked));
803 LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
804 Client_ID(Client), Client->cloaked);
806 /* Inform other servers in the network */
807 IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
808 "METADATA %s cloakhost :%s",
809 Client_ID(Client), Client->cloaked);
813 Client_Modes( CLIENT *Client )
815 assert( Client != NULL );
816 return Client->modes;
821 Client_Flags( CLIENT *Client )
823 assert( Client != NULL );
824 return Client->flags;
829 Client_OperByMe( CLIENT *Client )
831 assert( Client != NULL );
832 return Client->oper_by_me;
833 } /* Client_OperByMe */
837 Client_Hops( CLIENT *Client )
839 assert( Client != NULL );
845 Client_Token( CLIENT *Client )
847 assert( Client != NULL );
848 return Client->token;
853 Client_MyToken( CLIENT *Client )
855 assert( Client != NULL );
856 return Client->mytoken;
857 } /* Client_MyToken */
861 Client_NextHop( CLIENT *Client )
865 assert( Client != NULL );
868 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
872 } /* Client_NextHop */
876 * Return ID of a client: "client!user@host"
877 * This client ID is used for IRC prefixes, for example.
878 * Please note that this function uses a global static buffer, so you can't
879 * nest invocations without overwriting earlier results!
880 * @param Client Pointer to client structure
881 * @return Pointer to global buffer containing the client ID
884 Client_Mask( CLIENT *Client )
886 static char Mask_Buffer[GETID_LEN];
888 assert (Client != NULL);
890 /* Servers: return name only, there is no "mask" */
891 if (Client->type == CLIENT_SERVER)
894 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
895 Client->id, Client->user, Client->host);
901 * Return ID of a client with cloaked hostname: "client!user@server-name"
903 * This client ID is used for IRC prefixes, for example.
904 * Please note that this function uses a global static buffer, so you can't
905 * nest invocations without overwriting earlier results!
906 * If the client has not enabled cloaking, the real hostname is used.
908 * @param Client Pointer to client structure
909 * @return Pointer to global buffer containing the client ID
912 Client_MaskCloaked(CLIENT *Client)
914 static char Mask_Buffer[GETID_LEN];
916 assert (Client != NULL);
918 /* Is the client using cloaking at all? */
919 if (!Client_HasMode(Client, 'x'))
920 return Client_Mask(Client);
922 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
923 Client_HostnameDisplayed(Client));
926 } /* Client_MaskCloaked */
930 Client_Introducer( CLIENT *Client )
932 assert( Client != NULL );
933 return Client->introducer;
934 } /* Client_Introducer */
938 Client_TopServer( CLIENT *Client )
940 assert( Client != NULL );
941 return Client->topserver;
942 } /* Client_TopServer */
946 Client_HasMode( CLIENT *Client, char Mode )
948 assert( Client != NULL );
949 return strchr( Client->modes, Mode ) != NULL;
950 } /* Client_HasMode */
954 Client_HasFlag( CLIENT *Client, char Flag )
956 assert( Client != NULL );
957 return strchr( Client->flags, Flag ) != NULL;
958 } /* Client_HasFlag */
962 Client_Away( CLIENT *Client )
964 assert( Client != NULL );
970 * Make sure that a given nickname is valid.
972 * If the nickname is not valid for the given client, this function sends back
973 * the appropriate error messages.
975 * @param Client Client that wants to change the nickname.
976 * @param Nick New nickname.
977 * @returns true if nickname is valid, false otherwise.
980 Client_CheckNick(CLIENT *Client, char *Nick)
982 assert(Client != NULL);
983 assert(Nick != NULL);
985 if (!Client_IsValidNick(Nick)) {
986 if (strlen(Nick ) >= Conf_MaxNickLength)
987 IRC_WriteStrClient(Client, ERR_NICKNAMETOOLONG_MSG,
988 Client_ID(Client), Nick,
989 Conf_MaxNickLength - 1);
991 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
992 Client_ID(Client), Nick);
996 if (Client_Type(Client) != CLIENT_SERVER
997 && Client_Type(Client) != CLIENT_SERVICE) {
998 /* Make sure that this isn't a restricted/forbidden nickname */
999 if (Conf_NickIsBlocked(Nick)) {
1000 IRC_WriteStrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
1001 Client_ID(Client), Nick);
1006 /* Nickname already registered? */
1007 if (Client_Search(Nick)) {
1008 IRC_WriteStrClient(Client, ERR_NICKNAMEINUSE_MSG,
1009 Client_ID(Client), Nick);
1014 } /* Client_CheckNick */
1018 Client_CheckID( CLIENT *Client, char *ID )
1020 char str[COMMAND_LEN];
1023 assert( Client != NULL );
1024 assert( Client->conn_id > NONE );
1025 assert( ID != NULL );
1028 if (strlen(ID) > CLIENT_ID_LEN) {
1029 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
1033 /* ID already in use? */
1036 if (strcasecmp(c->id, ID) == 0) {
1037 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
1038 if (c->conn_id != NONE)
1039 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
1041 Log(LOG_ERR, "%s (via network)!", str);
1042 Conn_Close(Client->conn_id, str, str, true);
1045 c = (CLIENT *)c->next;
1049 } /* Client_CheckID */
1053 Client_First( void )
1056 } /* Client_First */
1060 Client_Next( CLIENT *c )
1062 assert( c != NULL );
1063 return (CLIENT *)c->next;
1068 Client_UserCount( void )
1070 return Count( CLIENT_USER );
1071 } /* Client_UserCount */
1075 Client_ServiceCount( void )
1077 return Count( CLIENT_SERVICE );;
1078 } /* Client_ServiceCount */
1082 Client_ServerCount( void )
1084 return Count( CLIENT_SERVER );
1085 } /* Client_ServerCount */
1089 Client_MyUserCount( void )
1091 return MyCount( CLIENT_USER );
1092 } /* Client_MyUserCount */
1096 Client_MyServiceCount( void )
1098 return MyCount( CLIENT_SERVICE );
1099 } /* Client_MyServiceCount */
1102 GLOBAL unsigned long
1103 Client_MyServerCount( void )
1106 unsigned long cnt = 0;
1111 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
1112 c = (CLIENT *)c->next;
1115 } /* Client_MyServerCount */
1118 GLOBAL unsigned long
1119 Client_OperCount( void )
1122 unsigned long cnt = 0;
1127 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
1128 c = (CLIENT *)c->next;
1131 } /* Client_OperCount */
1134 GLOBAL unsigned long
1135 Client_UnknownCount( void )
1138 unsigned long cnt = 0;
1143 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1144 c = (CLIENT *)c->next;
1148 } /* Client_UnknownCount */
1152 Client_MaxUserCount( void )
1155 } /* Client_MaxUserCount */
1159 Client_MyMaxUserCount( void )
1161 return My_Max_Users;
1162 } /* Client_MyMaxUserCount */
1166 * Check that a given nickname is valid.
1168 * @param Nick the nickname to check.
1169 * @returns true if nickname is valid, false otherwise.
1172 Client_IsValidNick(const char *Nick)
1175 static const char goodchars[] = ";0123456789-";
1177 assert (Nick != NULL);
1179 if (strchr(goodchars, Nick[0]))
1181 if (strlen(Nick ) >= Conf_MaxNickLength)
1186 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1194 } /* Client_IsValidNick */
1198 * Return pointer to "My_Whowas" structure.
1201 Client_GetWhowas( void )
1204 } /* Client_GetWhowas */
1207 * Return the index of the last used WHOWAS entry.
1210 Client_GetLastWhowasIndex( void )
1213 } /* Client_GetLastWhowasIndex */
1217 * Get the start time of this client.
1218 * The result is the start time in seconds since 1970-01-01, as reported
1219 * by the C function time(NULL).
1222 Client_StartTime(CLIENT *Client)
1224 assert( Client != NULL );
1225 return Client->starttime;
1226 } /* Client_Uptime */
1230 * Reject a client when logging in.
1232 * This function is called when a client isn't allowed to connect to this
1233 * server. Possible reasons are bad server password, bad PAM password,
1234 * or that the client is G/K-Line'd.
1236 * After calling this function, the client isn't connected any more.
1238 * @param Client The client to reject.
1239 * @param Reason The reason why the client has been rejected.
1240 * @param InformClient If true, send the exact reason to the client.
1243 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1245 char info[COMMAND_LEN];
1247 assert(Client != NULL);
1248 assert(Reason != NULL);
1251 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1253 strcpy(info, "Access denied: Bad password?");
1256 "User \"%s\" rejected (connection %d): %s!",
1257 Client_Mask(Client), Client_Conn(Client), Reason);
1258 Conn_Close(Client_Conn(Client), Reason, info, true);
1263 * Introduce a new user or service client in the network.
1265 * @param From Remote server introducing the client or NULL (local).
1266 * @param Client New client.
1267 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1270 Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
1272 /* Set client type (user or service) */
1273 Client_SetType(Client, Type);
1276 if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
1278 Client_SetType(Client, CLIENT_SERVICE);
1279 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1280 Client_TypeText(Client), Client_Mask(Client),
1281 Client_Modes(Client), Client_ID(From),
1282 Client_ID(Client_Introducer(Client)),
1283 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1285 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1286 Client_TypeText(Client), Client_Mask(Client),
1287 Client_Conn(Client));
1288 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1289 Client_ID(Client), Client_User(Client),
1290 Client_Hostname(Client),
1291 Conn_IPA(Client_Conn(Client)),
1292 Client_TypeText(Client));
1295 /* Inform other servers */
1296 IRC_WriteStrServersPrefixFlag_CB(From,
1297 From != NULL ? From : Client_ThisServer(),
1298 '\0', cb_introduceClient, (void *)Client);
1299 } /* Client_Introduce */
1302 static unsigned long
1303 Count( CLIENT_TYPE Type )
1306 unsigned long cnt = 0;
1311 if( c->type == Type ) cnt++;
1312 c = (CLIENT *)c->next;
1318 static unsigned long
1319 MyCount( CLIENT_TYPE Type )
1322 unsigned long cnt = 0;
1327 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1328 c = (CLIENT *)c->next;
1335 New_Client_Struct( void )
1337 /* Neue CLIENT-Struktur pre-initialisieren */
1341 c = (CLIENT *)malloc( sizeof( CLIENT ));
1344 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1348 memset( c, 0, sizeof ( CLIENT ));
1350 c->type = CLIENT_UNKNOWN;
1352 c->oper_by_me = false;
1362 Generate_MyToken( CLIENT *Client )
1371 if( c->mytoken == token )
1373 /* Das Token wurde bereits vergeben */
1378 else c = (CLIENT *)c->next;
1380 Client->mytoken = token;
1381 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1382 } /* Generate_MyToken */
1386 Adjust_Counters( CLIENT *Client )
1390 assert( Client != NULL );
1392 if( Client->type != CLIENT_USER ) return;
1394 if( Client->conn_id != NONE )
1396 /* Local connection */
1397 count = Client_MyUserCount( );
1398 if( count > My_Max_Users ) My_Max_Users = count;
1400 count = Client_UserCount( );
1401 if( count > Max_Users ) Max_Users = count;
1402 } /* Adjust_Counters */
1406 * Register client in My_Whowas structure for further recall by WHOWAS.
1407 * Note: Only clients that have been connected at least 30 seconds will be
1408 * registered to prevent automated IRC bots to "destroy" a nice server
1412 Client_RegisterWhowas( CLIENT *Client )
1417 assert( Client != NULL );
1419 /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1420 if (Conf_MorePrivacy)
1424 /* Don't register clients that were connected less than 30 seconds. */
1425 if( now - Client->starttime < 30 )
1428 slot = Last_Whowas + 1;
1429 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1432 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1435 My_Whowas[slot].time = now;
1436 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1437 sizeof( My_Whowas[slot].id ));
1438 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1439 sizeof( My_Whowas[slot].user ));
1440 strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
1441 sizeof( My_Whowas[slot].host ));
1442 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1443 sizeof( My_Whowas[slot].info ));
1444 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1445 sizeof( My_Whowas[slot].server ));
1448 } /* Client_RegisterWhowas */
1452 Client_TypeText(CLIENT *Client)
1454 assert(Client != NULL);
1455 switch (Client_Type(Client)) {
1459 case CLIENT_SERVICE:
1468 } /* Client_TypeText */
1472 * Destroy user or service client.
1475 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1477 if(Client->conn_id != NONE) {
1478 /* Local (directly connected) client */
1480 "%s \"%s\" unregistered (connection %d): %s.",
1481 Client_TypeText(Client), Client_Mask(Client),
1482 Client->conn_id, Txt);
1483 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1484 Client_ID(Client), Client_User(Client),
1485 Client_Hostname(Client), Txt);
1488 /* Inforam all the other servers */
1490 IRC_WriteStrServersPrefix(NULL,
1491 Client, "QUIT :%s", FwdMsg );
1493 IRC_WriteStrServersPrefix(NULL,
1498 LogDebug("%s \"%s\" unregistered: %s.",
1499 Client_TypeText(Client), Client_Mask(Client), Txt);
1502 /* Inform all the other servers, but the ones in the
1503 * direction we got the QUIT from */
1505 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1506 Client, "QUIT :%s", FwdMsg );
1508 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1513 /* Unregister client from channels */
1514 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1516 /* Register client in My_Whowas structure */
1517 Client_RegisterWhowas(Client);
1518 } /* Destroy_UserOrService */
1522 * Introduce a new user or service client to a remote server.
1524 * @param To The remote server to inform.
1525 * @param Prefix Prefix for the generated commands.
1526 * @param data CLIENT structure of the new client.
1529 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1531 CLIENT *c = (CLIENT *)data;
1533 (void)Client_Announce(To, Prefix, c);
1535 } /* cb_introduceClient */
1539 * Announce an user or service to a server.
1541 * This function differentiates between RFC1459 and RFC2813 server links and
1542 * generates the appropriate commands to register the user or service.
1544 * @param Client Server
1545 * @param Prefix Prefix for the generated commands
1546 * @param User User to announce
1549 Client_Announce(CLIENT * Client, CLIENT * Prefix, CLIENT * User)
1552 char *modes, *user, *host;
1554 modes = Client_Modes(User);
1555 user = Client_User(User) ? Client_User(User) : "-";
1556 host = Client_Hostname(User) ? Client_Hostname(User) : "-";
1558 conn = Client_Conn(Client);
1559 if (Conn_Options(conn) & CONN_RFC1459) {
1560 /* RFC 1459 mode: separate NICK and USER commands */
1561 if (! Conn_WriteStr(conn, "NICK %s :%d",
1562 Client_ID(User), Client_Hops(User) + 1))
1563 return DISCONNECTED;
1564 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1565 Client_ID(User), user, host,
1566 Client_ID(Client_Introducer(User)),
1568 return DISCONNECTED;
1570 if (! Conn_WriteStr(conn, ":%s MODE %s +%s",
1571 Client_ID(User), Client_ID(User),
1573 return DISCONNECTED;
1576 /* RFC 2813 mode: one combined NICK or SERVICE command */
1577 if (Client_Type(User) == CLIENT_SERVICE
1578 && strchr(Client_Flags(Client), 'S')) {
1579 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1580 "SERVICE %s %d * +%s %d :%s",
1582 Client_MyToken(Client_Introducer(User)),
1583 modes, Client_Hops(User) + 1,
1585 return DISCONNECTED;
1587 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1588 "NICK %s %d %s %s %d +%s :%s",
1589 Client_ID(User), Client_Hops(User) + 1,
1591 Client_MyToken(Client_Introducer(User)),
1592 modes, Client_Info(User)))
1593 return DISCONNECTED;
1597 if (strchr(Client_Flags(Client), 'M')) {
1598 /* Synchronize metadata */
1599 if (Client_HostnameCloaked(User)) {
1600 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1601 "METADATA %s cloakhost :%s",
1603 Client_HostnameCloaked(User)))
1604 return DISCONNECTED;
1607 if (Conn_GetCertFp(Client_Conn(User))) {
1608 if (!IRC_WriteStrClientPrefix(Client, Prefix,
1609 "METADATA %s certfp :%s",
1611 Conn_GetCertFp(Client_Conn(User))))
1612 return DISCONNECTED;
1617 } /* Client_Announce */
1623 Client_DebugDump(void)
1627 Log(LOG_DEBUG, "Client status:");
1631 " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1632 Client_ID(c), Client_Type(c), Client_Hostname(c),
1633 Client_User(c), Client_Conn(c), Client_StartTime(c),
1635 c = (CLIENT *)c->next;
1637 } /* Client_DumpClients */