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);
189 assert(Hostname != NULL);
191 client = New_Client_Struct();
195 client->starttime = time(NULL);
196 client->conn_id = Idx;
197 client->introducer = Introducer;
198 client->topserver = TopServer;
201 Client_SetID(client, ID);
203 Client_SetUser(client, User, Idented);
204 Client_SetOrigUser(client, User);
207 Client_SetHostname(client, Hostname);
209 Client_SetInfo(client, Info);
211 client->token = Token;
213 Client_SetModes(client, Modes);
214 if (Type == CLIENT_SERVER)
215 Generate_MyToken(client);
217 if (strchr(client->modes, 'a'))
218 strlcpy(client->away, DEFAULT_AWAY_MSG, sizeof(client->away));
220 client->next = (POINTER *)My_Clients;
223 Adjust_Counters(client);
226 } /* Init_New_Client */
230 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
232 /* remove a client */
238 assert( Client != NULL );
240 if( LogMsg ) txt = LogMsg;
242 if( ! txt ) txt = "Reason unknown.";
244 /* netsplit message */
245 if( Client->type == CLIENT_SERVER ) {
246 strlcpy(msg, This_Server->id, sizeof (msg));
247 strlcat(msg, " ", sizeof (msg));
248 strlcat(msg, Client->id, sizeof (msg));
255 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
258 * The client that is about to be removed is a server,
259 * the client we are checking right now is a child of that
260 * server and thus has to be removed, too.
262 * Call Client_Destroy() recursively with the server as the
263 * new "object to be removed". This starts the cycle again, until
264 * all servers that are linked via the original server have been
267 Client_Destroy( c, NULL, msg, false );
274 /* found the client: remove it */
275 if( last ) last->next = c->next;
276 else My_Clients = (CLIENT *)c->next;
278 if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
279 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
280 else if( c->type == CLIENT_SERVER )
282 if( c != This_Server )
284 if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
285 else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
288 /* inform other servers */
289 if( ! NGIRCd_SignalQuit )
291 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
292 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
297 if( c->conn_id != NONE )
299 if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
300 else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
302 Log(LOG_WARNING, "Unregistered unknown client \"%s\": %s",
303 c->id[0] ? c->id : "(No Nick)", txt );
311 c = (CLIENT *)c->next;
313 } /* Client_Destroy */
317 * Set client hostname.
319 * If global hostname cloaking is in effect, don't set the real hostname
320 * but the configured one.
322 * @param Client The client of which the hostname should be set.
323 * @param Hostname The new hostname.
326 Client_SetHostname( CLIENT *Client, const char *Hostname )
328 assert(Client != NULL);
329 assert(Hostname != NULL);
331 if (strlen(Conf_CloakHost)) {
332 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
333 Client_ID(Client), Client->host, Conf_CloakHost);
334 strlcpy(Client->host, Conf_CloakHost, sizeof(Client->host));
336 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
337 Client_ID(Client), Client->host, Hostname);
338 strlcpy(Client->host, Hostname, sizeof(Client->host));
340 } /* Client_SetHostname */
344 Client_SetID( CLIENT *Client, const char *ID )
346 assert( Client != NULL );
347 assert( ID != NULL );
349 strlcpy( Client->id, ID, sizeof( Client->id ));
351 if (Conf_CloakUserToNick) {
352 strlcpy( Client->user, ID, sizeof( Client->user ));
353 strlcpy( Client->info, ID, sizeof( Client->info ));
357 Client->hash = Hash( Client->id );
362 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
364 /* set clients username */
366 assert( Client != NULL );
367 assert( User != NULL );
369 if (Conf_CloakUserToNick) {
370 strlcpy(Client->user, Client->id, sizeof(Client->user));
371 } else if (Idented) {
372 strlcpy(Client->user, User, sizeof(Client->user));
374 Client->user[0] = '~';
375 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
377 } /* Client_SetUser */
381 * Set "original" user name of a client.
382 * This function saves the "original" user name, the user name specified by
383 * the peer using the USER command, into the CLIENT structure. This user
384 * name may be used for authentication, for example.
385 * @param Client The client.
386 * @param User User name to set.
389 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
391 assert(Client != NULL);
392 assert(User != NULL);
394 #if defined(PAM) && defined(IDENTAUTH)
395 strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
397 } /* Client_SetOrigUser */
401 Client_SetInfo( CLIENT *Client, const char *Info )
403 /* set client hostname */
405 assert( Client != NULL );
406 assert( Info != NULL );
408 if (Conf_CloakUserToNick)
409 strlcpy(Client->info, Client->id, sizeof(Client->info));
411 strlcpy(Client->info, Info, sizeof(Client->info));
412 } /* Client_SetInfo */
416 Client_SetModes( CLIENT *Client, const char *Modes )
418 assert( Client != NULL );
419 assert( Modes != NULL );
421 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
422 } /* Client_SetModes */
426 Client_SetFlags( CLIENT *Client, const char *Flags )
428 assert( Client != NULL );
429 assert( Flags != NULL );
431 strlcpy(Client->flags, Flags, sizeof(Client->flags));
432 } /* Client_SetFlags */
436 Client_SetPassword( CLIENT *Client, const char *Pwd )
438 /* set password sent by client */
440 assert( Client != NULL );
441 assert( Pwd != NULL );
443 strlcpy(Client->pwd, Pwd, sizeof(Client->pwd));
444 } /* Client_SetPassword */
448 Client_SetAway( CLIENT *Client, const char *Txt )
450 /* Set AWAY reason of client */
452 assert( Client != NULL );
453 assert( Txt != NULL );
455 strlcpy( Client->away, Txt, sizeof( Client->away ));
456 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
457 Client_Mask(Client), Txt);
458 } /* Client_SetAway */
462 Client_SetType( CLIENT *Client, int Type )
464 assert( Client != NULL );
466 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
467 Adjust_Counters( Client );
468 } /* Client_SetType */
472 Client_SetHops( CLIENT *Client, int Hops )
474 assert( Client != NULL );
476 } /* Client_SetHops */
480 Client_SetToken( CLIENT *Client, int Token )
482 assert( Client != NULL );
483 Client->token = Token;
484 } /* Client_SetToken */
488 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
490 assert( Client != NULL );
491 assert( Introducer != NULL );
492 Client->introducer = Introducer;
493 } /* Client_SetIntroducer */
497 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
499 assert( Client != NULL );
500 Client->oper_by_me = OperByMe;
501 } /* Client_SetOperByMe */
505 Client_ModeAdd( CLIENT *Client, char Mode )
508 * If Client already alread had Mode, return false.
509 * If the Mode was newly set, return true.
514 assert( Client != NULL );
516 x[0] = Mode; x[1] = '\0';
517 if (!strchr( Client->modes, x[0])) {
518 strlcat( Client->modes, x, sizeof( Client->modes ));
522 } /* Client_ModeAdd */
526 Client_ModeDel( CLIENT *Client, char Mode )
529 * If Mode was removed, return true.
530 * If Client did not have Mode, return false.
535 assert( Client != NULL );
537 x[0] = Mode; x[1] = '\0';
539 p = strchr( Client->modes, x[0] );
540 if( ! p ) return false;
542 /* Client has Mode -> delete */
549 } /* Client_ModeDel */
553 Client_Search( const char *Nick )
555 /* return Client-Structure that has the corresponding Nick.
556 * If none is found, return NULL.
559 char search_id[CLIENT_ID_LEN], *ptr;
563 assert( Nick != NULL );
565 /* copy Nick and truncate hostmask if necessary */
566 strlcpy( search_id, Nick, sizeof( search_id ));
567 ptr = strchr( search_id, '!' );
568 if( ptr ) *ptr = '\0';
570 search_hash = Hash(search_id);
574 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
576 c = (CLIENT *)c->next;
579 } /* Client_Search */
583 * Get client structure ("introducer") identfied by a server token.
584 * @return CLIENT structure or NULL if none could be found.
587 Client_GetFromToken( CLIENT *Client, int Token )
591 assert( Client != NULL );
598 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
601 c = (CLIENT *)c->next;
604 } /* Client_GetFromToken */
608 Client_Type( CLIENT *Client )
610 assert( Client != NULL );
616 Client_Conn( CLIENT *Client )
618 assert( Client != NULL );
619 return Client->conn_id;
624 Client_ID( CLIENT *Client )
626 assert( Client != NULL );
629 if(Client->type == CLIENT_USER)
630 assert(strlen(Client->id) < Conf_MaxNickLength);
633 if( Client->id[0] ) return Client->id;
639 Client_Info( CLIENT *Client )
641 assert( Client != NULL );
647 Client_User( CLIENT *Client )
649 assert( Client != NULL );
650 return Client->user[0] ? Client->user : "~";
657 * Get the "original" user name as supplied by the USER command.
658 * The user name as given by the client is used for authentication instead
659 * of the one detected using IDENT requests.
660 * @param Client The client.
661 * @return Original user name.
664 Client_OrigUser(CLIENT *Client) {
666 char *user = Client->user;
672 return Client->orig_user;
674 } /* Client_OrigUser */
680 * Return the hostname of a client.
681 * @param Client Pointer to client structure
682 * @return Pointer to client hostname
685 Client_Hostname(CLIENT *Client)
687 assert (Client != NULL);
689 } /* Client_Hostname */
693 * Get potentially cloaked hostname of a client.
694 * If the client has not enabled cloaking, the real hostname is used.
695 * @param Client Pointer to client structure
696 * @return Pointer to client hostname
699 Client_HostnameCloaked(CLIENT *Client)
701 assert(Client != NULL);
702 if (Client_HasMode(Client, 'x'))
703 return Client_ID(Client->introducer);
705 return Client_Hostname(Client);
706 } /* Client_HostnameCloaked */
710 Client_Password( CLIENT *Client )
712 assert( Client != NULL );
714 } /* Client_Password */
718 Client_Modes( CLIENT *Client )
720 assert( Client != NULL );
721 return Client->modes;
726 Client_Flags( CLIENT *Client )
728 assert( Client != NULL );
729 return Client->flags;
734 Client_OperByMe( CLIENT *Client )
736 assert( Client != NULL );
737 return Client->oper_by_me;
738 } /* Client_OperByMe */
742 Client_Hops( CLIENT *Client )
744 assert( Client != NULL );
750 Client_Token( CLIENT *Client )
752 assert( Client != NULL );
753 return Client->token;
758 Client_MyToken( CLIENT *Client )
760 assert( Client != NULL );
761 return Client->mytoken;
762 } /* Client_MyToken */
766 Client_NextHop( CLIENT *Client )
770 assert( Client != NULL );
773 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
777 } /* Client_NextHop */
781 * Return ID of a client: "client!user@host"
782 * This client ID is used for IRC prefixes, for example.
783 * Please note that this function uses a global static buffer, so you can't
784 * nest invocations without overwriting earlier results!
785 * @param Client Pointer to client structure
786 * @return Pointer to global buffer containing the client ID
789 Client_Mask( CLIENT *Client )
791 static char Mask_Buffer[GETID_LEN];
793 assert (Client != NULL);
795 /* Servers: return name only, there is no "mask" */
796 if (Client->type == CLIENT_SERVER)
799 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
800 Client->id, Client->user, Client->host);
806 * Return ID of a client with cloaked hostname: "client!user@server-name"
807 * This client ID is used for IRC prefixes, for example.
808 * Please note that this function uses a global static buffer, so you can't
809 * nest invocations without overwriting earlier results!
810 * If the client has not enabled cloaking, the real hostname is used.
811 * @param Client Pointer to client structure
812 * @return Pointer to global buffer containing the client ID
815 Client_MaskCloaked(CLIENT *Client)
817 static char Mask_Buffer[GETID_LEN];
819 assert (Client != NULL);
821 /* Is the client using cloaking at all? */
822 if (!Client_HasMode(Client, 'x'))
823 return Client_Mask(Client);
825 snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
826 Client->id, Client->user, Client_ID(Client->introducer));
828 } /* Client_MaskCloaked */
832 Client_Introducer( CLIENT *Client )
834 assert( Client != NULL );
835 return Client->introducer;
836 } /* Client_Introducer */
840 Client_TopServer( CLIENT *Client )
842 assert( Client != NULL );
843 return Client->topserver;
844 } /* Client_TopServer */
848 Client_HasMode( CLIENT *Client, char Mode )
850 assert( Client != NULL );
851 return strchr( Client->modes, Mode ) != NULL;
852 } /* Client_HasMode */
856 Client_Away( CLIENT *Client )
858 assert( Client != NULL );
864 * Make sure that a given nickname is valid.
866 * If the nickname is not valid for the given client, this function sends back
867 * the appropriate error messages.
869 * @param Client Client that wants to change the nickname.
870 * @param Nick New nick name.
871 * @returns true if nickname is valid, false otherwise.
874 Client_CheckNick(CLIENT *Client, char *Nick)
876 assert(Client != NULL);
877 assert(Nick != NULL);
879 if (!Client_IsValidNick(Nick)) {
880 if (strlen(Nick ) >= Conf_MaxNickLength)
881 IRC_WriteStrClient(Client, ERR_NICKNAMETOOLONG_MSG,
882 Client_ID(Client), Nick,
883 Conf_MaxNickLength - 1);
885 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
886 Client_ID(Client), Nick);
890 /* Nickname already registered? */
891 if (Client_Search(Nick)) {
892 IRC_WriteStrClient(Client, ERR_NICKNAMEINUSE_MSG,
893 Client_ID(Client), Nick);
898 } /* Client_CheckNick */
902 Client_CheckID( CLIENT *Client, char *ID )
904 char str[COMMAND_LEN];
907 assert( Client != NULL );
908 assert( Client->conn_id > NONE );
909 assert( ID != NULL );
912 if (strlen(ID) > CLIENT_ID_LEN) {
913 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
917 /* ID already in use? */
920 if (strcasecmp(c->id, ID) == 0) {
921 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
922 if (c->conn_id != NONE)
923 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
925 Log(LOG_ERR, "%s (via network)!", str);
926 Conn_Close(Client->conn_id, str, str, true);
929 c = (CLIENT *)c->next;
933 } /* Client_CheckID */
944 Client_Next( CLIENT *c )
947 return (CLIENT *)c->next;
952 Client_UserCount( void )
954 return Count( CLIENT_USER );
955 } /* Client_UserCount */
959 Client_ServiceCount( void )
961 return Count( CLIENT_SERVICE );;
962 } /* Client_ServiceCount */
966 Client_ServerCount( void )
968 return Count( CLIENT_SERVER );
969 } /* Client_ServerCount */
973 Client_MyUserCount( void )
975 return MyCount( CLIENT_USER );
976 } /* Client_MyUserCount */
980 Client_MyServiceCount( void )
982 return MyCount( CLIENT_SERVICE );
983 } /* Client_MyServiceCount */
987 Client_MyServerCount( void )
990 unsigned long cnt = 0;
995 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
996 c = (CLIENT *)c->next;
999 } /* Client_MyServerCount */
1002 GLOBAL unsigned long
1003 Client_OperCount( void )
1006 unsigned long cnt = 0;
1011 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
1012 c = (CLIENT *)c->next;
1015 } /* Client_OperCount */
1018 GLOBAL unsigned long
1019 Client_UnknownCount( void )
1022 unsigned long cnt = 0;
1027 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1028 c = (CLIENT *)c->next;
1032 } /* Client_UnknownCount */
1036 Client_MaxUserCount( void )
1039 } /* Client_MaxUserCount */
1043 Client_MyMaxUserCount( void )
1045 return My_Max_Users;
1046 } /* Client_MyMaxUserCount */
1050 * Check that a given nickname is valid.
1052 * @param Nick the nickname to check.
1053 * @returns true if nickname is valid, false otherwise.
1056 Client_IsValidNick(const char *Nick)
1059 static const char goodchars[] = ";0123456789-";
1061 assert (Nick != NULL);
1063 if (strchr(goodchars, Nick[0]))
1065 if (strlen(Nick ) >= Conf_MaxNickLength)
1070 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1078 } /* Client_IsValidNick */
1082 * Return pointer to "My_Whowas" structure.
1085 Client_GetWhowas( void )
1088 } /* Client_GetWhowas */
1091 * Return the index of the last used WHOWAS entry.
1094 Client_GetLastWhowasIndex( void )
1097 } /* Client_GetLastWhowasIndex */
1101 * Get the start time of this client.
1102 * The result is the start time in seconds since 1970-01-01, as reported
1103 * by the C function time(NULL).
1106 Client_StartTime(CLIENT *Client)
1108 assert( Client != NULL );
1109 return Client->starttime;
1110 } /* Client_Uptime */
1114 * Reject a client when logging in.
1116 * This function is called when a client isn't allowed to connect to this
1117 * server. Possible reasons are bad server password, bad PAM password,
1118 * or that the client is G/K-Line'd.
1120 * After calling this function, the client isn't connected any more.
1122 * @param Client The client to reject.
1123 * @param Reason The reason why the client has been rejected.
1124 * @param InformClient If true, send the exact reason to the client.
1127 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1129 char info[COMMAND_LEN];
1131 assert(Client != NULL);
1132 assert(Reason != NULL);
1135 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1137 strcpy(info, "Access denied: Bad password?");
1140 "User \"%s\" rejected (connection %d): %s!",
1141 Client_Mask(Client), Client_Conn(Client), Reason);
1142 Conn_Close(Client_Conn(Client), Reason, info, true);
1146 static unsigned long
1147 Count( CLIENT_TYPE Type )
1150 unsigned long cnt = 0;
1155 if( c->type == Type ) cnt++;
1156 c = (CLIENT *)c->next;
1162 static unsigned long
1163 MyCount( CLIENT_TYPE Type )
1166 unsigned long cnt = 0;
1171 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1172 c = (CLIENT *)c->next;
1179 New_Client_Struct( void )
1181 /* Neue CLIENT-Struktur pre-initialisieren */
1185 c = (CLIENT *)malloc( sizeof( CLIENT ));
1188 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1192 memset( c, 0, sizeof ( CLIENT ));
1194 c->type = CLIENT_UNKNOWN;
1196 c->oper_by_me = false;
1206 Generate_MyToken( CLIENT *Client )
1215 if( c->mytoken == token )
1217 /* Das Token wurde bereits vergeben */
1222 else c = (CLIENT *)c->next;
1224 Client->mytoken = token;
1225 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1226 } /* Generate_MyToken */
1230 Adjust_Counters( CLIENT *Client )
1234 assert( Client != NULL );
1236 if( Client->type != CLIENT_USER ) return;
1238 if( Client->conn_id != NONE )
1240 /* Local connection */
1241 count = Client_MyUserCount( );
1242 if( count > My_Max_Users ) My_Max_Users = count;
1244 count = Client_UserCount( );
1245 if( count > Max_Users ) Max_Users = count;
1246 } /* Adjust_Counters */
1250 * Register client in My_Whowas structure for further recall by WHOWAS.
1251 * Note: Only clients that have been connected at least 30 seconds will be
1252 * registered to prevent automated IRC bots to "destroy" a nice server
1256 Client_RegisterWhowas( CLIENT *Client )
1261 assert( Client != NULL );
1263 /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1264 if (Conf_MorePrivacy)
1268 /* Don't register clients that were connected less than 30 seconds. */
1269 if( now - Client->starttime < 30 )
1272 slot = Last_Whowas + 1;
1273 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1276 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1279 My_Whowas[slot].time = now;
1280 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1281 sizeof( My_Whowas[slot].id ));
1282 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1283 sizeof( My_Whowas[slot].user ));
1284 strlcpy( My_Whowas[slot].host, Client_HostnameCloaked( Client ),
1285 sizeof( My_Whowas[slot].host ));
1286 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1287 sizeof( My_Whowas[slot].info ));
1288 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1289 sizeof( My_Whowas[slot].server ));
1292 } /* Client_RegisterWhowas */
1296 Client_TypeText(CLIENT *Client)
1298 assert(Client != NULL);
1299 switch (Client_Type(Client)) {
1303 case CLIENT_SERVICE:
1312 } /* Client_TypeText */
1316 * Destroy user or service client.
1319 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1321 if(Client->conn_id != NONE) {
1322 /* Local (directly connected) client */
1324 "%s \"%s\" unregistered (connection %d): %s",
1325 Client_TypeText(Client), Client_Mask(Client),
1326 Client->conn_id, Txt);
1327 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1328 Client_ID(Client), Client_User(Client),
1329 Client_Hostname(Client), Txt);
1332 /* Inforam all the other servers */
1334 IRC_WriteStrServersPrefix(NULL,
1335 Client, "QUIT :%s", FwdMsg );
1337 IRC_WriteStrServersPrefix(NULL,
1342 LogDebug("%s \"%s\" unregistered: %s",
1343 Client_TypeText(Client), Client_Mask(Client), Txt);
1346 /* Inform all the other servers, but the ones in the
1347 * direction we got the QUIT from */
1349 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1350 Client, "QUIT :%s", FwdMsg );
1352 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1357 /* Unregister client from channels */
1358 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1360 /* Register client in My_Whowas structure */
1361 Client_RegisterWhowas(Client);
1362 } /* Destroy_UserOrService */
1368 Client_DebugDump(void)
1372 Log(LOG_DEBUG, "Client status:");
1376 " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1377 Client_ID(c), Client_Type(c), Client_Hostname(c),
1378 Client_User(c), Client_Conn(c), Client_StartTime(c),
1380 c = (CLIENT *)c->next;
1382 } /* Client_DumpClients */