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"
48 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
50 static CLIENT *This_Server, *My_Clients;
52 static WHOWAS My_Whowas[MAX_WHOWAS];
53 static int Last_Whowas = -1;
54 static long Max_Users, My_Max_Users;
57 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
58 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
60 static CLIENT *New_Client_Struct PARAMS(( void ));
61 static void Generate_MyToken PARAMS(( CLIENT *Client ));
62 static void Adjust_Counters PARAMS(( CLIENT *Client ));
64 static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
65 CLIENT *TopServer, int Type, const char *ID,
66 const char *User, const char *Hostname, const char *Info,
67 int Hops, int Token, const char *Modes,
70 static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
73 static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
81 This_Server = New_Client_Struct( );
84 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
85 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
89 /* Client-Struktur dieses Servers */
90 This_Server->next = NULL;
91 This_Server->type = CLIENT_SERVER;
92 This_Server->conn_id = NONE;
93 This_Server->introducer = This_Server;
94 This_Server->mytoken = 1;
95 This_Server->hops = 0;
97 gethostname( This_Server->host, CLIENT_HOST_LEN );
99 h = gethostbyname( This_Server->host );
100 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
102 Client_SetID( This_Server, Conf_ServerName );
103 Client_SetInfo( This_Server, Conf_ServerInfo );
105 My_Clients = This_Server;
107 memset( &My_Whowas, 0, sizeof( My_Whowas ));
117 if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
118 else Client_Destroy( This_Server, "Server going down.", NULL, false );
125 next = (CLIENT *)c->next;
129 if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
134 Client_ThisServer( void )
137 } /* Client_ThisServer */
141 * Initialize new local client; wrapper function for Init_New_Client().
142 * @return New CLIENT structure.
145 Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
147 return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
148 Hostname, NULL, 0, 0, NULL, Idented);
149 } /* Client_NewLocal */
153 * Initialize new remote server; wrapper function for Init_New_Client().
154 * @return New CLIENT structure.
157 Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
158 int Hops, int Token, const char *Info, bool Idented)
160 return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
161 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
162 } /* Client_NewRemoteServer */
166 * Initialize new remote client; wrapper function for Init_New_Client().
167 * @return New CLIENT structure.
170 Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
171 const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
173 return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
174 User, Hostname, Info, Hops, Token, Modes, Idented);
175 } /* Client_NewRemoteUser */
179 * Initialize new client and set up the given parameters like client type,
180 * user name, host name, introducing server etc. ...
181 * @return New CLIENT structure.
184 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
185 int Type, const char *ID, const char *User, const char *Hostname,
186 const char *Info, int Hops, int Token, const char *Modes, bool Idented)
191 assert(Introducer != NULL);
193 client = New_Client_Struct();
197 client->starttime = time(NULL);
198 client->conn_id = Idx;
199 client->introducer = Introducer;
200 client->topserver = TopServer;
203 Client_SetID(client, ID);
205 Client_SetUser(client, User, Idented);
206 Client_SetOrigUser(client, User);
209 Client_SetHostname(client, Hostname);
211 Client_SetInfo(client, Info);
213 client->token = Token;
215 Client_SetModes(client, Modes);
216 if (Type == CLIENT_SERVER)
217 Generate_MyToken(client);
219 if (strchr(client->modes, 'a'))
220 strlcpy(client->away, DEFAULT_AWAY_MSG, sizeof(client->away));
222 client->next = (POINTER *)My_Clients;
225 Adjust_Counters(client);
228 } /* Init_New_Client */
232 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
234 /* remove a client */
240 assert( Client != NULL );
242 if( LogMsg ) txt = LogMsg;
244 if( ! txt ) txt = "Reason unknown.";
246 /* netsplit message */
247 if( Client->type == CLIENT_SERVER ) {
248 strlcpy(msg, This_Server->id, sizeof (msg));
249 strlcat(msg, " ", sizeof (msg));
250 strlcat(msg, Client->id, sizeof (msg));
257 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
260 * The client that is about to be removed is a server,
261 * the client we are checking right now is a child of that
262 * server and thus has to be removed, too.
264 * Call Client_Destroy() recursively with the server as the
265 * new "object to be removed". This starts the cycle again, until
266 * all servers that are linked via the original server have been
269 Client_Destroy( c, NULL, msg, false );
276 /* found the client: remove it */
277 if( last ) last->next = c->next;
278 else My_Clients = (CLIENT *)c->next;
280 if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
281 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
282 else if( c->type == CLIENT_SERVER )
284 if( c != This_Server )
286 if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
287 else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
290 /* inform other servers */
291 if( ! NGIRCd_SignalQuit )
293 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
294 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
299 if( c->conn_id != NONE )
301 if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
302 else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
304 Log(LOG_WARNING, "Unregistered unknown client \"%s\": %s",
305 c->id[0] ? c->id : "(No Nick)", txt );
313 c = (CLIENT *)c->next;
315 } /* Client_Destroy */
319 * Set client hostname.
321 * If global hostname cloaking is in effect, don't set the real hostname
322 * but the configured one.
324 * @param Client The client of which the hostname should be set.
325 * @param Hostname The new hostname.
328 Client_SetHostname( CLIENT *Client, const char *Hostname )
330 assert(Client != NULL);
331 assert(Hostname != NULL);
333 if (strlen(Conf_CloakHost)) {
334 char cloak[GETID_LEN];
336 strlcpy(cloak, Hostname, GETID_LEN);
337 strlcat(cloak, Conf_CloakHostSalt, GETID_LEN);
338 snprintf(cloak, GETID_LEN, Conf_CloakHost, Hash(cloak));
340 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
341 Client_ID(Client), Client->host, cloak);
342 strlcpy(Client->host, cloak, sizeof(Client->host));
344 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
345 Client_ID(Client), Client->host, Hostname);
346 strlcpy(Client->host, Hostname, sizeof(Client->host));
348 } /* Client_SetHostname */
352 Client_SetID( CLIENT *Client, const char *ID )
354 assert( Client != NULL );
355 assert( ID != NULL );
357 strlcpy( Client->id, ID, sizeof( Client->id ));
359 if (Conf_CloakUserToNick) {
360 strlcpy( Client->user, ID, sizeof( Client->user ));
361 strlcpy( Client->info, ID, sizeof( Client->info ));
365 Client->hash = Hash( Client->id );
370 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
372 /* set clients username */
374 assert( Client != NULL );
375 assert( User != NULL );
377 if (Conf_CloakUserToNick) {
378 strlcpy(Client->user, Client->id, sizeof(Client->user));
379 } else if (Idented) {
380 strlcpy(Client->user, User, sizeof(Client->user));
382 Client->user[0] = '~';
383 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
385 } /* Client_SetUser */
389 * Set "original" user name of a client.
390 * This function saves the "original" user name, the user name specified by
391 * the peer using the USER command, into the CLIENT structure. This user
392 * name may be used for authentication, for example.
393 * @param Client The client.
394 * @param User User name to set.
397 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
399 assert(Client != NULL);
400 assert(User != NULL);
402 #if defined(PAM) && defined(IDENTAUTH)
403 strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
405 } /* Client_SetOrigUser */
409 Client_SetInfo( CLIENT *Client, const char *Info )
411 /* set client hostname */
413 assert( Client != NULL );
414 assert( Info != NULL );
416 if (Conf_CloakUserToNick)
417 strlcpy(Client->info, Client->id, sizeof(Client->info));
419 strlcpy(Client->info, Info, sizeof(Client->info));
420 } /* Client_SetInfo */
424 Client_SetModes( CLIENT *Client, const char *Modes )
426 assert( Client != NULL );
427 assert( Modes != NULL );
429 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
430 } /* Client_SetModes */
434 Client_SetFlags( CLIENT *Client, const char *Flags )
436 assert( Client != NULL );
437 assert( Flags != NULL );
439 strlcpy(Client->flags, Flags, sizeof(Client->flags));
440 } /* Client_SetFlags */
444 Client_SetAway( CLIENT *Client, const char *Txt )
446 /* Set AWAY reason of client */
448 assert( Client != NULL );
449 assert( Txt != NULL );
451 strlcpy( Client->away, Txt, sizeof( Client->away ));
452 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
453 Client_Mask(Client), Txt);
454 } /* Client_SetAway */
458 Client_SetType( CLIENT *Client, int Type )
460 assert( Client != NULL );
462 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
463 Adjust_Counters( Client );
464 } /* Client_SetType */
468 Client_SetHops( CLIENT *Client, int Hops )
470 assert( Client != NULL );
472 } /* Client_SetHops */
476 Client_SetToken( CLIENT *Client, int Token )
478 assert( Client != NULL );
479 Client->token = Token;
480 } /* Client_SetToken */
484 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
486 assert( Client != NULL );
487 assert( Introducer != NULL );
488 Client->introducer = Introducer;
489 } /* Client_SetIntroducer */
493 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
495 assert( Client != NULL );
496 Client->oper_by_me = OperByMe;
497 } /* Client_SetOperByMe */
501 Client_ModeAdd( CLIENT *Client, char Mode )
504 * If Client already alread had Mode, return false.
505 * If the Mode was newly set, return true.
510 assert( Client != NULL );
512 x[0] = Mode; x[1] = '\0';
513 if (!strchr( Client->modes, x[0])) {
514 strlcat( Client->modes, x, sizeof( Client->modes ));
518 } /* Client_ModeAdd */
522 Client_ModeDel( CLIENT *Client, char Mode )
525 * If Mode was removed, return true.
526 * If Client did not have Mode, return false.
531 assert( Client != NULL );
533 x[0] = Mode; x[1] = '\0';
535 p = strchr( Client->modes, x[0] );
536 if( ! p ) return false;
538 /* Client has Mode -> delete */
545 } /* Client_ModeDel */
549 Client_Search( const char *Nick )
551 /* return Client-Structure that has the corresponding Nick.
552 * If none is found, return NULL.
555 char search_id[CLIENT_ID_LEN], *ptr;
559 assert( Nick != NULL );
561 /* copy Nick and truncate hostmask if necessary */
562 strlcpy( search_id, Nick, sizeof( search_id ));
563 ptr = strchr( search_id, '!' );
564 if( ptr ) *ptr = '\0';
566 search_hash = Hash(search_id);
570 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
572 c = (CLIENT *)c->next;
575 } /* Client_Search */
579 * Get client structure ("introducer") identfied by a server token.
580 * @return CLIENT structure or NULL if none could be found.
583 Client_GetFromToken( CLIENT *Client, int Token )
587 assert( Client != NULL );
594 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
597 c = (CLIENT *)c->next;
600 } /* Client_GetFromToken */
604 Client_Type( CLIENT *Client )
606 assert( Client != NULL );
612 Client_Conn( CLIENT *Client )
614 assert( Client != NULL );
615 return Client->conn_id;
620 Client_ID( CLIENT *Client )
622 assert( Client != NULL );
625 if(Client->type == CLIENT_USER)
626 assert(strlen(Client->id) < Conf_MaxNickLength);
629 if( Client->id[0] ) return Client->id;
635 Client_Info( CLIENT *Client )
637 assert( Client != NULL );
643 Client_User( CLIENT *Client )
645 assert( Client != NULL );
646 return Client->user[0] ? Client->user : "~";
653 * Get the "original" user name as supplied by the USER command.
654 * The user name as given by the client is used for authentication instead
655 * of the one detected using IDENT requests.
656 * @param Client The client.
657 * @return Original user name.
660 Client_OrigUser(CLIENT *Client) {
662 char *user = Client->user;
668 return Client->orig_user;
670 } /* Client_OrigUser */
676 * Return the hostname of a client.
677 * @param Client Pointer to client structure
678 * @return Pointer to client hostname
681 Client_Hostname(CLIENT *Client)
683 assert (Client != NULL);
685 } /* Client_Hostname */
689 * Get (potentially cloaked) hostname of a client to display it to other users.
691 * If the client has not enabled cloaking, the real hostname is used.
692 * Please note that this function uses a global static buffer, so you can't
693 * nest invocations without overwriting earlier results!
695 * @param Client Pointer to client structure
696 * @return Pointer to client hostname
699 Client_HostnameDisplayed(CLIENT *Client)
701 static char Cloak_Buffer[CLIENT_HOST_LEN];
703 assert(Client != NULL);
705 /* Client isn't cloaked at all, return real hostname: */
706 if (!Client_HasMode(Client, 'x'))
707 return Client_Hostname(Client);
709 /* Client has received METADATA command, so it got the eventually
710 * cloaked hostname set correctly and this server doesn't need
711 * to cloak it on its own: */
712 if (strchr(Client_Flags(Client), 'M'))
713 return Client_Hostname(Client);
715 /* Do simple mapping to the server ID? */
716 if (!*Conf_CloakHostModeX)
717 return Client_ID(Client->introducer);
719 strlcpy(Cloak_Buffer, Client->host, CLIENT_HOST_LEN);
720 strlcat(Cloak_Buffer, Conf_CloakHostSalt, CLIENT_HOST_LEN);
722 snprintf(Cloak_Buffer, CLIENT_HOST_LEN, Conf_CloakHostModeX,
726 } /* Client_HostnameCloaked */
730 Client_Modes( CLIENT *Client )
732 assert( Client != NULL );
733 return Client->modes;
738 Client_Flags( CLIENT *Client )
740 assert( Client != NULL );
741 return Client->flags;
746 Client_OperByMe( CLIENT *Client )
748 assert( Client != NULL );
749 return Client->oper_by_me;
750 } /* Client_OperByMe */
754 Client_Hops( CLIENT *Client )
756 assert( Client != NULL );
762 Client_Token( CLIENT *Client )
764 assert( Client != NULL );
765 return Client->token;
770 Client_MyToken( CLIENT *Client )
772 assert( Client != NULL );
773 return Client->mytoken;
774 } /* Client_MyToken */
778 Client_NextHop( CLIENT *Client )
782 assert( Client != NULL );
785 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
789 } /* Client_NextHop */
793 * Return ID of a client: "client!user@host"
794 * This client ID is used for IRC prefixes, for example.
795 * Please note that this function uses a global static buffer, so you can't
796 * nest invocations without overwriting earlier results!
797 * @param Client Pointer to client structure
798 * @return Pointer to global buffer containing the client ID
801 Client_Mask( CLIENT *Client )
803 static char Mask_Buffer[GETID_LEN];
805 assert (Client != NULL);
807 /* Servers: return name only, there is no "mask" */
808 if (Client->type == CLIENT_SERVER)
811 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
812 Client->id, Client->user, Client->host);
818 * Return ID of a client with cloaked hostname: "client!user@server-name"
820 * This client ID is used for IRC prefixes, for example.
821 * Please note that this function uses a global static buffer, so you can't
822 * nest invocations without overwriting earlier results!
823 * If the client has not enabled cloaking, the real hostname is used.
825 * @param Client Pointer to client structure
826 * @return Pointer to global buffer containing the client ID
829 Client_MaskCloaked(CLIENT *Client)
831 static char Mask_Buffer[GETID_LEN];
833 assert (Client != NULL);
835 /* Is the client using cloaking at all? */
836 if (!Client_HasMode(Client, 'x'))
837 return Client_Mask(Client);
839 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
840 Client_HostnameDisplayed(Client));
843 } /* Client_MaskCloaked */
847 Client_Introducer( CLIENT *Client )
849 assert( Client != NULL );
850 return Client->introducer;
851 } /* Client_Introducer */
855 Client_TopServer( CLIENT *Client )
857 assert( Client != NULL );
858 return Client->topserver;
859 } /* Client_TopServer */
863 Client_HasMode( CLIENT *Client, char Mode )
865 assert( Client != NULL );
866 return strchr( Client->modes, Mode ) != NULL;
867 } /* Client_HasMode */
871 Client_Away( CLIENT *Client )
873 assert( Client != NULL );
879 * Make sure that a given nickname is valid.
881 * If the nickname is not valid for the given client, this function sends back
882 * the appropriate error messages.
884 * @param Client Client that wants to change the nickname.
885 * @param Nick New nickname.
886 * @returns true if nickname is valid, false otherwise.
889 Client_CheckNick(CLIENT *Client, char *Nick)
891 assert(Client != NULL);
892 assert(Nick != NULL);
894 if (!Client_IsValidNick(Nick)) {
895 if (strlen(Nick ) >= Conf_MaxNickLength)
896 IRC_WriteStrClient(Client, ERR_NICKNAMETOOLONG_MSG,
897 Client_ID(Client), Nick,
898 Conf_MaxNickLength - 1);
900 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
901 Client_ID(Client), Nick);
905 if (Client_Type(Client) != CLIENT_SERVER
906 && Client_Type(Client) != CLIENT_SERVICE) {
907 /* Make sure that this isn't a restricted/forbidden nickname */
908 if (Conf_NickIsBlocked(Nick)) {
909 IRC_WriteStrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
910 Client_ID(Client), Nick);
915 /* Nickname already registered? */
916 if (Client_Search(Nick)) {
917 IRC_WriteStrClient(Client, ERR_NICKNAMEINUSE_MSG,
918 Client_ID(Client), Nick);
923 } /* Client_CheckNick */
927 Client_CheckID( CLIENT *Client, char *ID )
929 char str[COMMAND_LEN];
932 assert( Client != NULL );
933 assert( Client->conn_id > NONE );
934 assert( ID != NULL );
937 if (strlen(ID) > CLIENT_ID_LEN) {
938 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
942 /* ID already in use? */
945 if (strcasecmp(c->id, ID) == 0) {
946 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
947 if (c->conn_id != NONE)
948 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
950 Log(LOG_ERR, "%s (via network)!", str);
951 Conn_Close(Client->conn_id, str, str, true);
954 c = (CLIENT *)c->next;
958 } /* Client_CheckID */
969 Client_Next( CLIENT *c )
972 return (CLIENT *)c->next;
977 Client_UserCount( void )
979 return Count( CLIENT_USER );
980 } /* Client_UserCount */
984 Client_ServiceCount( void )
986 return Count( CLIENT_SERVICE );;
987 } /* Client_ServiceCount */
991 Client_ServerCount( void )
993 return Count( CLIENT_SERVER );
994 } /* Client_ServerCount */
998 Client_MyUserCount( void )
1000 return MyCount( CLIENT_USER );
1001 } /* Client_MyUserCount */
1005 Client_MyServiceCount( void )
1007 return MyCount( CLIENT_SERVICE );
1008 } /* Client_MyServiceCount */
1011 GLOBAL unsigned long
1012 Client_MyServerCount( void )
1015 unsigned long cnt = 0;
1020 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
1021 c = (CLIENT *)c->next;
1024 } /* Client_MyServerCount */
1027 GLOBAL unsigned long
1028 Client_OperCount( void )
1031 unsigned long cnt = 0;
1036 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
1037 c = (CLIENT *)c->next;
1040 } /* Client_OperCount */
1043 GLOBAL unsigned long
1044 Client_UnknownCount( void )
1047 unsigned long cnt = 0;
1052 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1053 c = (CLIENT *)c->next;
1057 } /* Client_UnknownCount */
1061 Client_MaxUserCount( void )
1064 } /* Client_MaxUserCount */
1068 Client_MyMaxUserCount( void )
1070 return My_Max_Users;
1071 } /* Client_MyMaxUserCount */
1075 * Check that a given nickname is valid.
1077 * @param Nick the nickname to check.
1078 * @returns true if nickname is valid, false otherwise.
1081 Client_IsValidNick(const char *Nick)
1084 static const char goodchars[] = ";0123456789-";
1086 assert (Nick != NULL);
1088 if (strchr(goodchars, Nick[0]))
1090 if (strlen(Nick ) >= Conf_MaxNickLength)
1095 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1103 } /* Client_IsValidNick */
1107 * Return pointer to "My_Whowas" structure.
1110 Client_GetWhowas( void )
1113 } /* Client_GetWhowas */
1116 * Return the index of the last used WHOWAS entry.
1119 Client_GetLastWhowasIndex( void )
1122 } /* Client_GetLastWhowasIndex */
1126 * Get the start time of this client.
1127 * The result is the start time in seconds since 1970-01-01, as reported
1128 * by the C function time(NULL).
1131 Client_StartTime(CLIENT *Client)
1133 assert( Client != NULL );
1134 return Client->starttime;
1135 } /* Client_Uptime */
1139 * Reject a client when logging in.
1141 * This function is called when a client isn't allowed to connect to this
1142 * server. Possible reasons are bad server password, bad PAM password,
1143 * or that the client is G/K-Line'd.
1145 * After calling this function, the client isn't connected any more.
1147 * @param Client The client to reject.
1148 * @param Reason The reason why the client has been rejected.
1149 * @param InformClient If true, send the exact reason to the client.
1152 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1154 char info[COMMAND_LEN];
1156 assert(Client != NULL);
1157 assert(Reason != NULL);
1160 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1162 strcpy(info, "Access denied: Bad password?");
1165 "User \"%s\" rejected (connection %d): %s!",
1166 Client_Mask(Client), Client_Conn(Client), Reason);
1167 Conn_Close(Client_Conn(Client), Reason, info, true);
1172 * Introduce a new user or service client in the network.
1174 * @param From Remote server introducing the client or NULL (local).
1175 * @param Client New client.
1176 * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1179 Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
1181 /* Set client type (user or service) */
1182 Client_SetType(Client, Type);
1185 if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
1187 Client_SetType(Client, CLIENT_SERVICE);
1188 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1189 Client_TypeText(Client), Client_Mask(Client),
1190 Client_Modes(Client), Client_ID(From),
1191 Client_ID(Client_Introducer(Client)),
1192 Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1194 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1195 Client_TypeText(Client), Client_Mask(Client),
1196 Client_Conn(Client));
1197 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1198 Client_ID(Client), Client_User(Client),
1199 Client_Hostname(Client),
1200 Conn_IPA(Client_Conn(Client)),
1201 Client_TypeText(Client));
1204 /* Inform other servers */
1205 IRC_WriteStrServersPrefixFlag_CB(From,
1206 From != NULL ? From : Client_ThisServer(),
1207 '\0', cb_introduceClient, (void *)Client);
1208 } /* Client_Introduce */
1211 static unsigned long
1212 Count( CLIENT_TYPE Type )
1215 unsigned long cnt = 0;
1220 if( c->type == Type ) cnt++;
1221 c = (CLIENT *)c->next;
1227 static unsigned long
1228 MyCount( CLIENT_TYPE Type )
1231 unsigned long cnt = 0;
1236 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1237 c = (CLIENT *)c->next;
1244 New_Client_Struct( void )
1246 /* Neue CLIENT-Struktur pre-initialisieren */
1250 c = (CLIENT *)malloc( sizeof( CLIENT ));
1253 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1257 memset( c, 0, sizeof ( CLIENT ));
1259 c->type = CLIENT_UNKNOWN;
1261 c->oper_by_me = false;
1271 Generate_MyToken( CLIENT *Client )
1280 if( c->mytoken == token )
1282 /* Das Token wurde bereits vergeben */
1287 else c = (CLIENT *)c->next;
1289 Client->mytoken = token;
1290 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1291 } /* Generate_MyToken */
1295 Adjust_Counters( CLIENT *Client )
1299 assert( Client != NULL );
1301 if( Client->type != CLIENT_USER ) return;
1303 if( Client->conn_id != NONE )
1305 /* Local connection */
1306 count = Client_MyUserCount( );
1307 if( count > My_Max_Users ) My_Max_Users = count;
1309 count = Client_UserCount( );
1310 if( count > Max_Users ) Max_Users = count;
1311 } /* Adjust_Counters */
1315 * Register client in My_Whowas structure for further recall by WHOWAS.
1316 * Note: Only clients that have been connected at least 30 seconds will be
1317 * registered to prevent automated IRC bots to "destroy" a nice server
1321 Client_RegisterWhowas( CLIENT *Client )
1326 assert( Client != NULL );
1328 /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1329 if (Conf_MorePrivacy)
1333 /* Don't register clients that were connected less than 30 seconds. */
1334 if( now - Client->starttime < 30 )
1337 slot = Last_Whowas + 1;
1338 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1341 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1344 My_Whowas[slot].time = now;
1345 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1346 sizeof( My_Whowas[slot].id ));
1347 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1348 sizeof( My_Whowas[slot].user ));
1349 strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
1350 sizeof( My_Whowas[slot].host ));
1351 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1352 sizeof( My_Whowas[slot].info ));
1353 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1354 sizeof( My_Whowas[slot].server ));
1357 } /* Client_RegisterWhowas */
1361 Client_TypeText(CLIENT *Client)
1363 assert(Client != NULL);
1364 switch (Client_Type(Client)) {
1368 case CLIENT_SERVICE:
1377 } /* Client_TypeText */
1381 * Destroy user or service client.
1384 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1386 if(Client->conn_id != NONE) {
1387 /* Local (directly connected) client */
1389 "%s \"%s\" unregistered (connection %d): %s",
1390 Client_TypeText(Client), Client_Mask(Client),
1391 Client->conn_id, Txt);
1392 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1393 Client_ID(Client), Client_User(Client),
1394 Client_Hostname(Client), Txt);
1397 /* Inforam all the other servers */
1399 IRC_WriteStrServersPrefix(NULL,
1400 Client, "QUIT :%s", FwdMsg );
1402 IRC_WriteStrServersPrefix(NULL,
1407 LogDebug("%s \"%s\" unregistered: %s",
1408 Client_TypeText(Client), Client_Mask(Client), Txt);
1411 /* Inform all the other servers, but the ones in the
1412 * direction we got the QUIT from */
1414 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1415 Client, "QUIT :%s", FwdMsg );
1417 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1422 /* Unregister client from channels */
1423 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1425 /* Register client in My_Whowas structure */
1426 Client_RegisterWhowas(Client);
1427 } /* Destroy_UserOrService */
1431 * Introduce a new user or service client to a remote server.
1433 * This function differentiates between RFC1459 and RFC2813 server links and
1434 * generates the appropriate commands to register the new user or service.
1436 * @param To The remote server to inform.
1437 * @param Prefix Prefix for the generated commands.
1438 * @param data CLIENT structure of the new client.
1441 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1443 CLIENT *c = (CLIENT *)data;
1445 char *modes, *user, *host;
1447 modes = Client_Modes(c);
1448 user = Client_User(c) ? Client_User(c) : "-";
1449 host = Client_Hostname(c) ? Client_Hostname(c) : "-";
1451 conn = Client_Conn(To);
1452 if (Conn_Options(conn) & CONN_RFC1459) {
1453 /* RFC 1459 mode: separate NICK and USER commands */
1454 Conn_WriteStr(conn, "NICK %s :%d", Client_ID(c),
1455 Client_Hops(c) + 1);
1456 Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1457 Client_ID(c), user, host,
1458 Client_ID(Client_Introducer(c)), Client_Info(c));
1460 Conn_WriteStr(conn, ":%s MODE %s +%s",
1461 Client_ID(c), Client_ID(c), modes);
1463 /* RFC 2813 mode: one combined NICK or SERVICE command */
1464 if (Client_Type(c) == CLIENT_SERVICE
1465 && strchr(Client_Flags(To), 'S'))
1466 IRC_WriteStrClientPrefix(To, Prefix,
1467 "SERVICE %s %d * +%s %d :%s",
1469 Client_MyToken(Client_Introducer(c)),
1470 Client_Modes(c), Client_Hops(c) + 1,
1473 IRC_WriteStrClientPrefix(To, Prefix,
1474 "NICK %s %d %s %s %d +%s :%s",
1475 Client_ID(c), Client_Hops(c) + 1,
1477 Client_MyToken(Client_Introducer(c)),
1478 modes, Client_Info(c));
1480 } /* cb_introduceClient */
1486 Client_DebugDump(void)
1490 Log(LOG_DEBUG, "Client status:");
1494 " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1495 Client_ID(c), Client_Type(c), Client_Hostname(c),
1496 Client_User(c), Client_Conn(c), Client_StartTime(c),
1498 c = (CLIENT *)c->next;
1500 } /* Client_DumpClients */