2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 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.
41 #include "irc-write.h"
47 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
49 static CLIENT *This_Server, *My_Clients;
51 static WHOWAS My_Whowas[MAX_WHOWAS];
52 static int Last_Whowas = -1;
53 static long Max_Users, My_Max_Users;
56 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
57 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
59 static CLIENT *New_Client_Struct PARAMS(( void ));
60 static void Generate_MyToken PARAMS(( CLIENT *Client ));
61 static void Adjust_Counters PARAMS(( CLIENT *Client ));
63 static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
64 CLIENT *TopServer, int Type, const char *ID,
65 const char *User, const char *Hostname, const char *Info,
66 int Hops, int Token, const char *Modes,
69 static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
78 This_Server = New_Client_Struct( );
81 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
82 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
86 /* Client-Struktur dieses Servers */
87 This_Server->next = NULL;
88 This_Server->type = CLIENT_SERVER;
89 This_Server->conn_id = NONE;
90 This_Server->introducer = This_Server;
91 This_Server->mytoken = 1;
92 This_Server->hops = 0;
94 gethostname( This_Server->host, CLIENT_HOST_LEN );
96 h = gethostbyname( This_Server->host );
97 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
99 Client_SetID( This_Server, Conf_ServerName );
100 Client_SetInfo( This_Server, Conf_ServerInfo );
102 My_Clients = This_Server;
104 memset( &My_Whowas, 0, sizeof( My_Whowas ));
114 if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
115 else Client_Destroy( This_Server, "Server going down.", NULL, false );
122 next = (CLIENT *)c->next;
126 if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
131 Client_ThisServer( void )
134 } /* Client_ThisServer */
138 * Initialize new local client; wrapper function for Init_New_Client().
139 * @return New CLIENT structure.
142 Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
144 return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
145 Hostname, NULL, 0, 0, NULL, Idented);
146 } /* Client_NewLocal */
150 * Initialize new remote server; wrapper function for Init_New_Client().
151 * @return New CLIENT structure.
154 Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
155 int Hops, int Token, const char *Info, bool Idented)
157 return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
158 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
159 } /* Client_NewRemoteServer */
163 * Initialize new remote client; wrapper function for Init_New_Client().
164 * @return New CLIENT structure.
167 Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
168 const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
170 return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
171 User, Hostname, Info, Hops, Token, Modes, Idented);
172 } /* Client_NewRemoteUser */
176 * Initialize new client and set up the given parameters like client type,
177 * user name, host name, introducing server etc. ...
178 * @return New CLIENT structure.
181 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
182 int Type, const char *ID, const char *User, const char *Hostname,
183 const char *Info, int Hops, int Token, const char *Modes, bool Idented)
188 assert(Introducer != NULL);
190 client = New_Client_Struct();
194 client->starttime = time(NULL);
195 client->conn_id = Idx;
196 client->introducer = Introducer;
197 client->topserver = TopServer;
200 Client_SetID(client, ID);
202 Client_SetUser(client, User, Idented);
203 Client_SetOrigUser(client, User);
206 Client_SetHostname(client, Hostname);
208 Client_SetInfo(client, Info);
210 client->token = Token;
212 Client_SetModes(client, Modes);
213 if (Type == CLIENT_SERVER)
214 Generate_MyToken(client);
216 if (strchr(client->modes, 'a'))
217 strlcpy(client->away, DEFAULT_AWAY_MSG, sizeof(client->away));
219 client->next = (POINTER *)My_Clients;
222 Adjust_Counters(client);
225 } /* Init_New_Client */
229 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
231 /* remove a client */
237 assert( Client != NULL );
239 if( LogMsg ) txt = LogMsg;
241 if( ! txt ) txt = "Reason unknown.";
243 /* netsplit message */
244 if( Client->type == CLIENT_SERVER ) {
245 strlcpy(msg, This_Server->id, sizeof (msg));
246 strlcat(msg, " ", sizeof (msg));
247 strlcat(msg, Client->id, sizeof (msg));
254 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
257 * The client that is about to be removed is a server,
258 * the client we are checking right now is a child of that
259 * server and thus has to be removed, too.
261 * Call Client_Destroy() recursively with the server as the
262 * new "object to be removed". This starts the cycle again, until
263 * all servers that are linked via the original server have been
266 Client_Destroy( c, NULL, msg, false );
273 /* found the client: remove it */
274 if( last ) last->next = c->next;
275 else My_Clients = (CLIENT *)c->next;
277 if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
278 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
279 else if( c->type == CLIENT_SERVER )
281 if( c != This_Server )
283 if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
284 else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
287 /* inform other servers */
288 if( ! NGIRCd_SignalQuit )
290 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
291 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
296 if( c->conn_id != NONE )
298 if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
299 else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
301 Log(LOG_WARNING, "Unregistered unknown client \"%s\": %s",
302 c->id[0] ? c->id : "(No Nick)", txt );
310 c = (CLIENT *)c->next;
312 } /* Client_Destroy */
316 * Set client hostname.
318 * If global hostname cloaking is in effect, don't set the real hostname
319 * but the configured one.
321 * @param Client The client of which the hostname should be set.
322 * @param Hostname The new hostname.
325 Client_SetHostname( CLIENT *Client, const char *Hostname )
327 assert(Client != NULL);
328 assert(Hostname != NULL);
330 if (strlen(Conf_CloakHost)) {
331 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
332 Client_ID(Client), Client->host, Conf_CloakHost);
333 strlcpy(Client->host, Conf_CloakHost, sizeof(Client->host));
335 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
336 Client_ID(Client), Client->host, Hostname);
337 strlcpy(Client->host, Hostname, sizeof(Client->host));
339 } /* Client_SetHostname */
343 Client_SetID( CLIENT *Client, const char *ID )
345 assert( Client != NULL );
346 assert( ID != NULL );
348 strlcpy( Client->id, ID, sizeof( Client->id ));
350 if (Conf_CloakUserToNick) {
351 strlcpy( Client->user, ID, sizeof( Client->user ));
352 strlcpy( Client->info, ID, sizeof( Client->info ));
356 Client->hash = Hash( Client->id );
361 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
363 /* set clients username */
365 assert( Client != NULL );
366 assert( User != NULL );
368 if (Conf_CloakUserToNick) {
369 strlcpy(Client->user, Client->id, sizeof(Client->user));
370 } else if (Idented) {
371 strlcpy(Client->user, User, sizeof(Client->user));
373 Client->user[0] = '~';
374 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
376 } /* Client_SetUser */
380 * Set "original" user name of a client.
381 * This function saves the "original" user name, the user name specified by
382 * the peer using the USER command, into the CLIENT structure. This user
383 * name may be used for authentication, for example.
384 * @param Client The client.
385 * @param User User name to set.
388 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
390 assert(Client != NULL);
391 assert(User != NULL);
393 #if defined(PAM) && defined(IDENTAUTH)
394 strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
396 } /* Client_SetOrigUser */
400 Client_SetInfo( CLIENT *Client, const char *Info )
402 /* set client hostname */
404 assert( Client != NULL );
405 assert( Info != NULL );
407 if (Conf_CloakUserToNick)
408 strlcpy(Client->info, Client->id, sizeof(Client->info));
410 strlcpy(Client->info, Info, sizeof(Client->info));
411 } /* Client_SetInfo */
415 Client_SetModes( CLIENT *Client, const char *Modes )
417 assert( Client != NULL );
418 assert( Modes != NULL );
420 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
421 } /* Client_SetModes */
425 Client_SetFlags( CLIENT *Client, const char *Flags )
427 assert( Client != NULL );
428 assert( Flags != NULL );
430 strlcpy(Client->flags, Flags, sizeof(Client->flags));
431 } /* Client_SetFlags */
435 Client_SetPassword( CLIENT *Client, const char *Pwd )
437 /* set password sent by client */
439 assert( Client != NULL );
440 assert( Pwd != NULL );
442 strlcpy(Client->pwd, Pwd, sizeof(Client->pwd));
443 } /* Client_SetPassword */
447 Client_SetAway( CLIENT *Client, const char *Txt )
449 /* Set AWAY reason of client */
451 assert( Client != NULL );
452 assert( Txt != NULL );
454 strlcpy( Client->away, Txt, sizeof( Client->away ));
455 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
456 Client_Mask(Client), Txt);
457 } /* Client_SetAway */
461 Client_SetType( CLIENT *Client, int Type )
463 assert( Client != NULL );
465 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
466 Adjust_Counters( Client );
467 } /* Client_SetType */
471 Client_SetHops( CLIENT *Client, int Hops )
473 assert( Client != NULL );
475 } /* Client_SetHops */
479 Client_SetToken( CLIENT *Client, int Token )
481 assert( Client != NULL );
482 Client->token = Token;
483 } /* Client_SetToken */
487 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
489 assert( Client != NULL );
490 assert( Introducer != NULL );
491 Client->introducer = Introducer;
492 } /* Client_SetIntroducer */
496 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
498 assert( Client != NULL );
499 Client->oper_by_me = OperByMe;
500 } /* Client_SetOperByMe */
504 Client_ModeAdd( CLIENT *Client, char Mode )
507 * If Client already alread had Mode, return false.
508 * If the Mode was newly set, return true.
513 assert( Client != NULL );
515 x[0] = Mode; x[1] = '\0';
516 if (!strchr( Client->modes, x[0])) {
517 strlcat( Client->modes, x, sizeof( Client->modes ));
521 } /* Client_ModeAdd */
525 Client_ModeDel( CLIENT *Client, char Mode )
528 * If Mode was removed, return true.
529 * If Client did not have Mode, return false.
534 assert( Client != NULL );
536 x[0] = Mode; x[1] = '\0';
538 p = strchr( Client->modes, x[0] );
539 if( ! p ) return false;
541 /* Client has Mode -> delete */
548 } /* Client_ModeDel */
552 Client_Search( const char *Nick )
554 /* return Client-Structure that has the corresponding Nick.
555 * If none is found, return NULL.
558 char search_id[CLIENT_ID_LEN], *ptr;
562 assert( Nick != NULL );
564 /* copy Nick and truncate hostmask if necessary */
565 strlcpy( search_id, Nick, sizeof( search_id ));
566 ptr = strchr( search_id, '!' );
567 if( ptr ) *ptr = '\0';
569 search_hash = Hash(search_id);
573 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
575 c = (CLIENT *)c->next;
578 } /* Client_Search */
582 * Get client structure ("introducer") identfied by a server token.
583 * @return CLIENT structure or NULL if none could be found.
586 Client_GetFromToken( CLIENT *Client, int Token )
590 assert( Client != NULL );
597 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
600 c = (CLIENT *)c->next;
603 } /* Client_GetFromToken */
607 Client_Type( CLIENT *Client )
609 assert( Client != NULL );
615 Client_Conn( CLIENT *Client )
617 assert( Client != NULL );
618 return Client->conn_id;
623 Client_ID( CLIENT *Client )
625 assert( Client != NULL );
628 if(Client->type == CLIENT_USER)
629 assert(strlen(Client->id) < Conf_MaxNickLength);
632 if( Client->id[0] ) return Client->id;
638 Client_Info( CLIENT *Client )
640 assert( Client != NULL );
646 Client_User( CLIENT *Client )
648 assert( Client != NULL );
649 return Client->user[0] ? Client->user : "~";
656 * Get the "original" user name as supplied by the USER command.
657 * The user name as given by the client is used for authentication instead
658 * of the one detected using IDENT requests.
659 * @param Client The client.
660 * @return Original user name.
663 Client_OrigUser(CLIENT *Client) {
665 char *user = Client->user;
671 return Client->orig_user;
673 } /* Client_OrigUser */
679 * Return the hostname of a client.
680 * @param Client Pointer to client structure
681 * @return Pointer to client hostname
684 Client_Hostname(CLIENT *Client)
686 assert (Client != NULL);
688 } /* Client_Hostname */
692 * Get potentially cloaked hostname of a client.
693 * If the client has not enabled cloaking, the real hostname is used.
694 * @param Client Pointer to client structure
695 * @return Pointer to client hostname
698 Client_HostnameCloaked(CLIENT *Client)
700 assert(Client != NULL);
701 if (Client_HasMode(Client, 'x'))
702 return Client_ID(Client->introducer);
704 return Client_Hostname(Client);
705 } /* Client_HostnameCloaked */
709 Client_Password( CLIENT *Client )
711 assert( Client != NULL );
713 } /* Client_Password */
717 Client_Modes( CLIENT *Client )
719 assert( Client != NULL );
720 return Client->modes;
725 Client_Flags( CLIENT *Client )
727 assert( Client != NULL );
728 return Client->flags;
733 Client_OperByMe( CLIENT *Client )
735 assert( Client != NULL );
736 return Client->oper_by_me;
737 } /* Client_OperByMe */
741 Client_Hops( CLIENT *Client )
743 assert( Client != NULL );
749 Client_Token( CLIENT *Client )
751 assert( Client != NULL );
752 return Client->token;
757 Client_MyToken( CLIENT *Client )
759 assert( Client != NULL );
760 return Client->mytoken;
761 } /* Client_MyToken */
765 Client_NextHop( CLIENT *Client )
769 assert( Client != NULL );
772 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
776 } /* Client_NextHop */
780 * Return ID of a client: "client!user@host"
781 * This client ID is used for IRC prefixes, for example.
782 * Please note that this function uses a global static buffer, so you can't
783 * nest invocations without overwriting earlier results!
784 * @param Client Pointer to client structure
785 * @return Pointer to global buffer containing the client ID
788 Client_Mask( CLIENT *Client )
790 static char Mask_Buffer[GETID_LEN];
792 assert (Client != NULL);
794 /* Servers: return name only, there is no "mask" */
795 if (Client->type == CLIENT_SERVER)
798 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
799 Client->id, Client->user, Client->host);
805 * Return ID of a client with cloaked hostname: "client!user@server-name"
806 * This client ID is used for IRC prefixes, for example.
807 * Please note that this function uses a global static buffer, so you can't
808 * nest invocations without overwriting earlier results!
809 * If the client has not enabled cloaking, the real hostname is used.
810 * @param Client Pointer to client structure
811 * @return Pointer to global buffer containing the client ID
814 Client_MaskCloaked(CLIENT *Client)
816 static char Mask_Buffer[GETID_LEN];
818 assert (Client != NULL);
820 /* Is the client using cloaking at all? */
821 if (!Client_HasMode(Client, 'x'))
822 return Client_Mask(Client);
824 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
825 Client->id, Client->user, Client_ID(Client->introducer));
827 } /* Client_MaskCloaked */
831 Client_Introducer( CLIENT *Client )
833 assert( Client != NULL );
834 return Client->introducer;
835 } /* Client_Introducer */
839 Client_TopServer( CLIENT *Client )
841 assert( Client != NULL );
842 return Client->topserver;
843 } /* Client_TopServer */
847 Client_HasMode( CLIENT *Client, char Mode )
849 assert( Client != NULL );
850 return strchr( Client->modes, Mode ) != NULL;
851 } /* Client_HasMode */
855 Client_Away( CLIENT *Client )
857 assert( Client != NULL );
863 * Make sure that a given nickname is valid.
865 * If the nickname is not valid for the given client, this function sends back
866 * the appropriate error messages.
868 * @param Client Client that wants to change the nickname.
869 * @param Nick New nick name.
870 * @returns true if nickname is valid, false otherwise.
873 Client_CheckNick(CLIENT *Client, char *Nick)
875 assert(Client != NULL);
876 assert(Nick != NULL);
878 if (!Client_IsValidNick(Nick)) {
879 if (strlen(Nick ) >= Conf_MaxNickLength)
880 IRC_WriteStrClient(Client, ERR_NICKNAMETOOLONG_MSG,
881 Client_ID(Client), Nick,
882 Conf_MaxNickLength - 1);
884 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
885 Client_ID(Client), Nick);
889 /* Nickname already registered? */
890 if (Client_Search(Nick)) {
891 IRC_WriteStrClient(Client, ERR_NICKNAMEINUSE_MSG,
892 Client_ID(Client), Nick);
897 } /* Client_CheckNick */
901 Client_CheckID( CLIENT *Client, char *ID )
903 char str[COMMAND_LEN];
906 assert( Client != NULL );
907 assert( Client->conn_id > NONE );
908 assert( ID != NULL );
911 if (strlen(ID) > CLIENT_ID_LEN) {
912 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
916 /* ID already in use? */
919 if (strcasecmp(c->id, ID) == 0) {
920 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
921 if (c->conn_id != NONE)
922 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
924 Log(LOG_ERR, "%s (via network)!", str);
925 Conn_Close(Client->conn_id, str, str, true);
928 c = (CLIENT *)c->next;
932 } /* Client_CheckID */
943 Client_Next( CLIENT *c )
946 return (CLIENT *)c->next;
951 Client_UserCount( void )
953 return Count( CLIENT_USER );
954 } /* Client_UserCount */
958 Client_ServiceCount( void )
960 return Count( CLIENT_SERVICE );;
961 } /* Client_ServiceCount */
965 Client_ServerCount( void )
967 return Count( CLIENT_SERVER );
968 } /* Client_ServerCount */
972 Client_MyUserCount( void )
974 return MyCount( CLIENT_USER );
975 } /* Client_MyUserCount */
979 Client_MyServiceCount( void )
981 return MyCount( CLIENT_SERVICE );
982 } /* Client_MyServiceCount */
986 Client_MyServerCount( void )
989 unsigned long cnt = 0;
994 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
995 c = (CLIENT *)c->next;
998 } /* Client_MyServerCount */
1001 GLOBAL unsigned long
1002 Client_OperCount( void )
1005 unsigned long cnt = 0;
1010 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
1011 c = (CLIENT *)c->next;
1014 } /* Client_OperCount */
1017 GLOBAL unsigned long
1018 Client_UnknownCount( void )
1021 unsigned long cnt = 0;
1026 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1027 c = (CLIENT *)c->next;
1031 } /* Client_UnknownCount */
1035 Client_MaxUserCount( void )
1038 } /* Client_MaxUserCount */
1042 Client_MyMaxUserCount( void )
1044 return My_Max_Users;
1045 } /* Client_MyMaxUserCount */
1049 * Check that a given nickname is valid.
1051 * @param Nick the nickname to check.
1052 * @returns true if nickname is valid, false otherwise.
1055 Client_IsValidNick(const char *Nick)
1058 static const char goodchars[] = ";0123456789-";
1060 assert (Nick != NULL);
1062 if (strchr(goodchars, Nick[0]))
1064 if (strlen(Nick ) >= Conf_MaxNickLength)
1069 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1077 } /* Client_IsValidNick */
1081 * Return pointer to "My_Whowas" structure.
1084 Client_GetWhowas( void )
1087 } /* Client_GetWhowas */
1090 * Return the index of the last used WHOWAS entry.
1093 Client_GetLastWhowasIndex( void )
1096 } /* Client_GetLastWhowasIndex */
1100 * Get the start time of this client.
1101 * The result is the start time in seconds since 1970-01-01, as reported
1102 * by the C function time(NULL).
1105 Client_StartTime(CLIENT *Client)
1107 assert( Client != NULL );
1108 return Client->starttime;
1109 } /* Client_Uptime */
1113 * Reject a client when logging in.
1115 * This function is called when a client isn't allowed to connect to this
1116 * server. Possible reasons are bad server password, bad PAM password,
1117 * or that the client is G/K-Line'd.
1119 * After calling this function, the client isn't connected any more.
1121 * @param Client The client to reject.
1122 * @param Reason The reason why the client has been rejected.
1123 * @param InformClient If true, send the exact reason to the client.
1126 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1128 char info[COMMAND_LEN];
1130 assert(Client != NULL);
1131 assert(Reason != NULL);
1134 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1136 strcpy(info, "Access denied: Bad password?");
1139 "User \"%s\" rejected (connection %d): %s!",
1140 Client_Mask(Client), Client_Conn(Client), Reason);
1141 Conn_Close(Client_Conn(Client), Reason, info, true);
1145 static unsigned long
1146 Count( CLIENT_TYPE Type )
1149 unsigned long cnt = 0;
1154 if( c->type == Type ) cnt++;
1155 c = (CLIENT *)c->next;
1161 static unsigned long
1162 MyCount( CLIENT_TYPE Type )
1165 unsigned long cnt = 0;
1170 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1171 c = (CLIENT *)c->next;
1178 New_Client_Struct( void )
1180 /* Neue CLIENT-Struktur pre-initialisieren */
1184 c = (CLIENT *)malloc( sizeof( CLIENT ));
1187 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1191 memset( c, 0, sizeof ( CLIENT ));
1193 c->type = CLIENT_UNKNOWN;
1195 c->oper_by_me = false;
1205 Generate_MyToken( CLIENT *Client )
1214 if( c->mytoken == token )
1216 /* Das Token wurde bereits vergeben */
1221 else c = (CLIENT *)c->next;
1223 Client->mytoken = token;
1224 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1225 } /* Generate_MyToken */
1229 Adjust_Counters( CLIENT *Client )
1233 assert( Client != NULL );
1235 if( Client->type != CLIENT_USER ) return;
1237 if( Client->conn_id != NONE )
1239 /* Local connection */
1240 count = Client_MyUserCount( );
1241 if( count > My_Max_Users ) My_Max_Users = count;
1243 count = Client_UserCount( );
1244 if( count > Max_Users ) Max_Users = count;
1245 } /* Adjust_Counters */
1249 * Register client in My_Whowas structure for further recall by WHOWAS.
1250 * Note: Only clients that have been connected at least 30 seconds will be
1251 * registered to prevent automated IRC bots to "destroy" a nice server
1255 Client_RegisterWhowas( CLIENT *Client )
1260 assert( Client != NULL );
1262 /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1263 if (Conf_MorePrivacy)
1267 /* Don't register clients that were connected less than 30 seconds. */
1268 if( now - Client->starttime < 30 )
1271 slot = Last_Whowas + 1;
1272 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1275 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1278 My_Whowas[slot].time = now;
1279 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1280 sizeof( My_Whowas[slot].id ));
1281 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1282 sizeof( My_Whowas[slot].user ));
1283 strlcpy( My_Whowas[slot].host, Client_HostnameCloaked( Client ),
1284 sizeof( My_Whowas[slot].host ));
1285 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1286 sizeof( My_Whowas[slot].info ));
1287 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1288 sizeof( My_Whowas[slot].server ));
1291 } /* Client_RegisterWhowas */
1295 Client_TypeText(CLIENT *Client)
1297 assert(Client != NULL);
1298 switch (Client_Type(Client)) {
1302 case CLIENT_SERVICE:
1311 } /* Client_TypeText */
1315 * Destroy user or service client.
1318 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1320 if(Client->conn_id != NONE) {
1321 /* Local (directly connected) client */
1323 "%s \"%s\" unregistered (connection %d): %s",
1324 Client_TypeText(Client), Client_Mask(Client),
1325 Client->conn_id, Txt);
1326 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1327 Client_ID(Client), Client_User(Client),
1328 Client_Hostname(Client), Txt);
1331 /* Inforam all the other servers */
1333 IRC_WriteStrServersPrefix(NULL,
1334 Client, "QUIT :%s", FwdMsg );
1336 IRC_WriteStrServersPrefix(NULL,
1341 LogDebug("%s \"%s\" unregistered: %s",
1342 Client_TypeText(Client), Client_Mask(Client), Txt);
1345 /* Inform all the other servers, but the ones in the
1346 * direction we got the QUIT from */
1348 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1349 Client, "QUIT :%s", FwdMsg );
1351 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1356 /* Unregister client from channels */
1357 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1359 /* Register client in My_Whowas structure */
1360 Client_RegisterWhowas(Client);
1361 } /* Destroy_UserOrService */
1367 Client_DebugDump(void)
1371 Log(LOG_DEBUG, "Client status:");
1375 " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1376 Client_ID(c), Client_Type(c), Client_Hostname(c),
1377 Client_User(c), Client_Conn(c), Client_StartTime(c),
1379 c = (CLIENT *)c->next;
1381 } /* Client_DumpClients */