/** * Introduce a new user or service client in the network. * * @param From Remote server introducing the client or NULL (local). * @param Client New client. * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE). */ GLOBAL void Client_Introduce(CLIENT *From, CLIENT *Client, int Type) { /* Set client type (user or service) */ Client_SetType(Client, Type); if (From) { if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)), Client_ID(Client))) Client_SetType(Client, CLIENT_SERVICE); LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).", Client_TypeText(Client), Client_Mask(Client), Client_Modes(Client), Client_ID(From), Client_ID(Client_Introducer(Client)), Client_Hops(Client), Client_Hops(Client) > 1 ? "s": ""); } else { Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).", Client_TypeText(Client), Client_Mask(Client), Client_Conn(Client)); Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s", Client_ID(Client), Client_User(Client), Client_Hostname(Client), Conn_IPA(Client_Conn(Client)), Client_TypeText(Client)); } /* Inform other servers */ IRC_WriteStrServersPrefixFlag_CB(From, From != NULL ? From : Client_ThisServer(), '\0', cb_introduceClient, (void *)Client); } /* Client_Introduce */
GLOBAL bool Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key) { char *file_name, line[COMMAND_LEN], *nick, *pass; FILE *fd; assert(Chan != NULL); assert(Client != NULL); assert(Key != NULL); if (!Channel_HasMode(Chan, 'k')) return true; if (*Key == '\0') return false; if (strcmp(Chan->key, Key) == 0) return true; file_name = array_start(&Chan->keyfile); if (!file_name) return false; fd = fopen(file_name, "r"); if (!fd) { Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s", file_name, Chan->name, strerror(errno)); return false; } while (fgets(line, (int)sizeof(line), fd) != NULL) { ngt_TrimStr(line); if (! (nick = strchr(line, ':'))) continue; *nick++ = '\0'; if (!Match(line, Client_User(Client))) continue; if (! (pass = strchr(nick, ':'))) continue; *pass++ = '\0'; if (!Match(nick, Client_ID(Client))) continue; if (strcmp(Key, pass) != 0) continue; fclose(fd); return true; } fclose(fd); return false; } /* Channel_CheckKey */
GLOBAL void Client_DebugDump(void) { CLIENT *c; Log(LOG_DEBUG, "Client status:"); c = My_Clients; while (c) { Log(LOG_DEBUG, " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s", Client_ID(c), Client_Type(c), Client_Hostname(c), Client_User(c), Client_Conn(c), Client_StartTime(c), Client_Flags(c)); c = (CLIENT *)c->next; } } /* Client_DumpClients */
/** * Destroy user or service client. */ static void Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit) { if(Client->conn_id != NONE) { /* Local (directly connected) client */ Log(LOG_NOTICE, "%s \"%s\" unregistered (connection %d): %s.", Client_TypeText(Client), Client_Mask(Client), Client->conn_id, Txt); Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]", Client_ID(Client), Client_User(Client), Client_Hostname(Client), Txt); if (SendQuit) { /* Inforam all the other servers */ if (FwdMsg) IRC_WriteStrServersPrefix(NULL, Client, "QUIT :%s", FwdMsg ); else IRC_WriteStrServersPrefix(NULL, Client, "QUIT :"); } } else { /* Remote client */ LogDebug("%s \"%s\" unregistered: %s.", Client_TypeText(Client), Client_Mask(Client), Txt); if(SendQuit) { /* Inform all the other servers, but the ones in the * direction we got the QUIT from */ if(FwdMsg) IRC_WriteStrServersPrefix(Client_NextHop(Client), Client, "QUIT :%s", FwdMsg ); else IRC_WriteStrServersPrefix(Client_NextHop(Client), Client, "QUIT :" ); } } /* Unregister client from channels */ Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id); /* Register client in My_Whowas structure */ Client_RegisterWhowas(Client); } /* Destroy_UserOrService */
/** * Authenticate a connecting client using PAM. * @param Client The client to authenticate. * @return true when authentication succeeded, false otherwise. */ GLOBAL bool PAM_Authenticate(CLIENT *Client) { pam_handle_t *pam; int retval = PAM_SUCCESS; LogDebug("PAM: Authenticate \"%s\" (%s) ...", Client_OrigUser(Client), Client_Mask(Client)); /* Set supplied client password */ if (password) free(password); password = strdup(Conn_Password(Client_Conn(Client))); conv.appdata_ptr = Conn_Password(Client_Conn(Client)); /* Initialize PAM */ retval = pam_start(Conf_PAMServiceName, Client_OrigUser(Client), &conv, &pam); if (retval != PAM_SUCCESS) { Log(LOG_ERR, "PAM: Failed to create authenticator! (%d)", retval); return false; } pam_set_item(pam, PAM_RUSER, Client_User(Client)); pam_set_item(pam, PAM_RHOST, Client_Hostname(Client)); #if defined(HAVE_PAM_FAIL_DELAY) && !defined(NO_PAM_FAIL_DELAY) pam_fail_delay(pam, 0); #endif /* PAM authentication ... */ retval = pam_authenticate(pam, 0); /* Success? */ if (retval == PAM_SUCCESS) Log(LOG_INFO, "PAM: Authenticated \"%s\" (%s).", Client_OrigUser(Client), Client_Mask(Client)); else Log(LOG_ERR, "PAM: Error on \"%s\" (%s): %s", Client_OrigUser(Client), Client_Mask(Client), pam_strerror(pam, retval)); /* Free PAM structures */ if (pam_end(pam, retval) != PAM_SUCCESS) Log(LOG_ERR, "PAM: Failed to release authenticator!"); return (retval == PAM_SUCCESS); }
/** * Register client in My_Whowas structure for further recall by WHOWAS. * Note: Only clients that have been connected at least 30 seconds will be * registered to prevent automated IRC bots to "destroy" a nice server * history database. */ GLOBAL void Client_RegisterWhowas( CLIENT *Client ) { int slot; time_t now; assert( Client != NULL ); /* Don't register WHOWAS information when "MorePrivacy" is enabled. */ if (Conf_MorePrivacy) return; now = time(NULL); /* Don't register clients that were connected less than 30 seconds. */ if( now - Client->starttime < 30 ) return; slot = Last_Whowas + 1; if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0; #ifdef DEBUG Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot ); #endif My_Whowas[slot].time = now; strlcpy( My_Whowas[slot].id, Client_ID( Client ), sizeof( My_Whowas[slot].id )); strlcpy( My_Whowas[slot].user, Client_User( Client ), sizeof( My_Whowas[slot].user )); strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ), sizeof( My_Whowas[slot].host )); strlcpy( My_Whowas[slot].info, Client_Info( Client ), sizeof( My_Whowas[slot].info )); strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )), sizeof( My_Whowas[slot].server )); Last_Whowas = slot; } /* Client_RegisterWhowas */
static bool Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors) { CLIENT *cl, *from; CL2CHAN *cl2chan; CHANNEL *chan; char *currentTarget = Req->argv[0]; char *lastCurrentTarget = NULL; assert(Client != NULL); assert(Req != NULL); if (Req->argc == 0) { if (!SendErrors) return CONNECTED; return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG, Client_ID(Client), Req->command); } if (Req->argc == 1) { if (!SendErrors) return CONNECTED; return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG, Client_ID(Client)); } if (Req->argc > 2) { if (!SendErrors) return CONNECTED; return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command); } if (Client_Type(Client) == CLIENT_SERVER) from = Client_Search(Req->prefix); else from = Client; if (!from) return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix); /* handle msgtarget = msgto *("," msgto) */ currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget); ngt_UpperStr(Req->command); while (currentTarget) { /* Check for and handle valid <msgto> of form: * RFC 2812 2.3.1: * msgto = channel / ( user [ "%" host ] "@" servername ) * msgto =/ ( user "%" host ) / targetmask * msgto =/ nickname / ( nickname "!" user "@" host ) */ if (strchr(currentTarget, '!') == NULL) /* nickname */ cl = Client_Search(currentTarget); else cl = NULL; if (cl == NULL) { /* If currentTarget isn't a nickname check for: * user ["%" host] "@" servername * user "%" host * nickname "!" user "@" host */ char target[COMMAND_LEN]; char * nick = NULL; char * user = NULL; char * host = NULL; char * server = NULL; strlcpy(target, currentTarget, COMMAND_LEN); server = strchr(target, '@'); if (server) { *server = '\0'; server++; } host = strchr(target, '%'); if (host) { *host = '\0'; host++; } user = strchr(target, '!'); if (user) { /* msgto form: nick!user@host */ *user = '******'; user++; nick = target; host = server; /* not "@server" but "@host" */ } else { user = target; } for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) { if (Client_Type(cl) != CLIENT_USER && Client_Type(cl) != CLIENT_SERVICE) continue; if (nick != NULL && host != NULL) { if (strcasecmp(nick, Client_ID(cl)) == 0 && strcasecmp(user, Client_User(cl)) == 0 && strcasecmp(host, Client_HostnameCloaked(cl)) == 0) break; else continue; } if (strcasecmp(user, Client_User(cl)) != 0) continue; if (host != NULL && strcasecmp(host, Client_HostnameCloaked(cl)) != 0) continue; if (server != NULL && strcasecmp(server, Client_ID(Client_Introducer(cl))) != 0) continue; break; } } if (cl) { /* Target is a user, enforce type */ #ifndef STRICT_RFC if (Client_Type(cl) != ForceType && !(ForceType == CLIENT_USER && (Client_Type(cl) == CLIENT_USER || Client_Type(cl) == CLIENT_SERVICE))) { #else if (Client_Type(cl) != ForceType) { #endif if (SendErrors && !IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG,Client_ID(from), currentTarget)) return DISCONNECTED; goto send_next_target; } #ifndef STRICT_RFC if (ForceType == CLIENT_SERVICE && (Conn_Options(Client_Conn(Client_NextHop(cl))) & CONN_RFC1459)) { /* SQUERY command but RFC 1459 link: convert * request to PRIVMSG command */ Req->command = "PRIVMSG"; } #endif if (Client_HasMode(cl, 'C')) { cl2chan = Channel_FirstChannelOf(cl); while (cl2chan) { chan = Channel_GetChannel(cl2chan); if (Channel_IsMemberOf(chan, from)) break; cl2chan = Channel_NextChannelOf(cl, cl2chan); } if (!cl2chan) { if (SendErrors && !IRC_WriteStrClient( from, ERR_NOTONSAMECHANNEL_MSG, Client_ID(from), Client_ID(cl))) return DISCONNECTED; goto send_next_target; } } if (SendErrors && (Client_Type(Client) != CLIENT_SERVER) && strchr(Client_Modes(cl), 'a')) { /* Target is away */ if (!IRC_WriteStrClient(from, RPL_AWAY_MSG, Client_ID(from), Client_ID(cl), Client_Away(cl))) return DISCONNECTED; } if (Client_Conn(from) > NONE) { Conn_UpdateIdle(Client_Conn(from)); } if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s", Req->command, Client_ID(cl), Req->argv[1])) return DISCONNECTED; } else if (ForceType != CLIENT_SERVICE && (chan = Channel_Search(currentTarget))) { if (!Channel_Write(chan, from, Client, Req->command, SendErrors, Req->argv[1])) return DISCONNECTED; } else if (ForceType != CLIENT_SERVICE /* $#: server/target mask, RFC 2812, sec. 3.3.1 */ && strchr("$#", currentTarget[0]) && strchr(currentTarget, '.')) { /* targetmask */ if (!Send_Message_Mask(from, Req->command, currentTarget, Req->argv[1], SendErrors)) return DISCONNECTED; } else { if (!SendErrors) return CONNECTED; if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG, Client_ID(from), currentTarget)) return DISCONNECTED; } send_next_target: currentTarget = strtok_r(NULL, ",", &lastCurrentTarget); if (currentTarget) Conn_SetPenalty(Client_Conn(Client), 1); } return CONNECTED; } /* Send_Message */ static bool Send_Message_Mask(CLIENT * from, char * command, char * targetMask, char * message, bool SendErrors) { CLIENT *cl; bool client_match; char *mask = targetMask + 1; const char *check_wildcards; cl = NULL; if (strchr(Client_Modes(from), 'o') == NULL) { if (!SendErrors) return true; return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG, Client_ID(from)); } /* * RFC 2812, sec. 3.3.1 requires that targetMask have at least one * dot (".") and no wildcards ("*", "?") following the last one. */ check_wildcards = strrchr(targetMask, '.'); assert(check_wildcards != NULL); if (check_wildcards && check_wildcards[strcspn(check_wildcards, "*?")]) { if (!SendErrors) return true; return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask); } /* #: hostmask, see RFC 2812, sec. 3.3.1 */ if (targetMask[0] == '#') { for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) { if (Client_Type(cl) != CLIENT_USER) continue; client_match = MatchCaseInsensitive(mask, Client_Hostname(cl)); if (client_match) if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s", command, Client_ID(cl), message)) return false; } } else { assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */ for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) { if (Client_Type(cl) != CLIENT_USER) continue; client_match = MatchCaseInsensitive(mask, Client_ID(Client_Introducer(cl))); if (client_match) if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s", command, Client_ID(cl), message)) return false; } } return CONNECTED; } /* Send_Message_Mask */
GLOBAL bool IRC_TRACE( CLIENT *Client, REQUEST *Req ) { CLIENT *from, *target, *c; CONN_ID idx, idx2; char user[CLIENT_USER_LEN]; assert( Client != NULL ); assert( Req != NULL ); /* Bad number of arguments? */ if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command ); /* Search sender */ if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix ); else from = Client; if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix ); /* Search target */ if( Req->argc == 1 ) target = Client_Search( Req->argv[0] ); else target = Client_ThisServer( ); /* Forward command to other server? */ if( target != Client_ThisServer( )) { if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] ); /* Send RPL_TRACELINK back to initiator */ idx = Client_Conn( Client ); assert( idx > NONE ); idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE ); if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), PACKAGE_NAME, PACKAGE_VERSION, Client_ID( target ), Client_ID( Client_NextHop( target )), Option_String( idx2 ), time( NULL ) - Conn_StartTime( idx2 ), Conn_SendQ( idx ), Conn_SendQ( idx2 ))) return DISCONNECTED; /* Forward command */ IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] ); return CONNECTED; } /* Infos about all connected servers */ c = Client_First( ); while( c ) { if( Client_Conn( c ) > NONE ) { /* Local client */ if( Client_Type( c ) == CLIENT_SERVER ) { /* Server link */ strlcpy( user, Client_User( c ), sizeof( user )); if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user )); if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Client_ID( c ), user, Client_Hostname( c ), Client_Mask( Client_ThisServer( )), Option_String( Client_Conn( c )))) return DISCONNECTED; } if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' ))) { /* IRC Operator */ if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED; } } c = Client_Next( c ); } IRC_SetPenalty( Client, 3 ); return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel ); } /* IRC_TRACE */
/** * Announce an user or service to a server. * * This function differentiates between RFC1459 and RFC2813 server links and * generates the appropriate commands to register the user or service. * * @param Client Server * @param Prefix Prefix for the generated commands * @param User User to announce */ GLOBAL bool Client_Announce(CLIENT * Client, CLIENT * Prefix, CLIENT * User) { CONN_ID conn; char *modes, *user, *host; modes = Client_Modes(User); user = Client_User(User) ? Client_User(User) : "-"; host = Client_Hostname(User) ? Client_Hostname(User) : "-"; conn = Client_Conn(Client); if (Conn_Options(conn) & CONN_RFC1459) { /* RFC 1459 mode: separate NICK and USER commands */ if (! Conn_WriteStr(conn, "NICK %s :%d", Client_ID(User), Client_Hops(User) + 1)) return DISCONNECTED; if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s", Client_ID(User), user, host, Client_ID(Client_Introducer(User)), Client_Info(User))) return DISCONNECTED; if (modes[0]) { if (! Conn_WriteStr(conn, ":%s MODE %s +%s", Client_ID(User), Client_ID(User), modes)) return DISCONNECTED; } } else { /* RFC 2813 mode: one combined NICK or SERVICE command */ if (Client_Type(User) == CLIENT_SERVICE && Client_HasFlag(Client, 'S')) { if (!IRC_WriteStrClientPrefix(Client, Prefix, "SERVICE %s %d * +%s %d :%s", Client_Mask(User), Client_MyToken(Client_Introducer(User)), modes, Client_Hops(User) + 1, Client_Info(User))) return DISCONNECTED; } else { if (!IRC_WriteStrClientPrefix(Client, Prefix, "NICK %s %d %s %s %d +%s :%s", Client_ID(User), Client_Hops(User) + 1, user, host, Client_MyToken(Client_Introducer(User)), modes, Client_Info(User))) return DISCONNECTED; } } if (Client_HasFlag(Client, 'M')) { /* Synchronize metadata */ if (Client_HostnameCloaked(User)) { if (!IRC_WriteStrClientPrefix(Client, Prefix, "METADATA %s cloakhost :%s", Client_ID(User), Client_HostnameCloaked(User))) return DISCONNECTED; } if (Client_AccountName(User)) { if (!IRC_WriteStrClientPrefix(Client, Prefix, "METADATA %s accountname :%s", Client_ID(User), Client_AccountName(User))) return DISCONNECTED; } if (Conn_GetCertFp(Client_Conn(User))) { if (!IRC_WriteStrClientPrefix(Client, Prefix, "METADATA %s certfp :%s", Client_ID(User), Conn_GetCertFp(Client_Conn(User)))) return DISCONNECTED; } } return CONNECTED; } /* Client_Announce */
/** * Get pointer to a static string representing the connection "options". * * @param Idx Connection index. * @return Pointer to static (global) string buffer. */ static char * #if defined(SSL_SUPPORT) || defined(ZLIB) Option_String(CONN_ID Idx) { static char option_txt[8]; UINT16 options; assert(Idx != NONE); options = Conn_Options(Idx); strcpy(option_txt, "F"); /* No idea what this means, but the * original ircd sends it ... */ #ifdef SSL_SUPPORT if(options & CONN_SSL) /* SSL encrypted link */ strlcat(option_txt, "s", sizeof(option_txt)); #endif #ifdef ZLIB if(options & CONN_ZIP) /* zlib compression enabled */ strlcat(option_txt, "z", sizeof(option_txt)); #endif return option_txt; #else Option_String(UNUSED CONN_ID Idx) { return ""; #endif } /* Option_String */ /** * Send a message to target(s). * * This function is used by IRC_{PRIVMSG|NOTICE|SQUERY} to actualy * send the message(s). * * @param Client The client from which this command has been received. * @param Req Request structure with prefix and all parameters. * @param ForceType Required type of the destination of the message(s). * @param SendErrors Whether to report errors back to the client or not. * @return CONNECTED or DISCONNECTED. */ static bool Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors) { CLIENT *cl, *from; CL2CHAN *cl2chan; CHANNEL *chan; char *currentTarget = Req->argv[0]; char *strtok_last = NULL; char *message = NULL; char *targets[MAX_HNDL_TARGETS]; int i, target_nr = 0; assert(Client != NULL); assert(Req != NULL); if (Req->argc == 0) { if (!SendErrors) return CONNECTED; return IRC_WriteErrClient(Client, ERR_NORECIPIENT_MSG, Client_ID(Client), Req->command); } if (Req->argc == 1) { if (!SendErrors) return CONNECTED; return IRC_WriteErrClient(Client, ERR_NOTEXTTOSEND_MSG, Client_ID(Client)); } if (Req->argc > 2) { if (!SendErrors) return CONNECTED; return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command); } if (Client_Type(Client) == CLIENT_SERVER && Req->prefix) from = Client_Search(Req->prefix); else from = Client; if (!from) return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix); #ifdef ICONV if (Client_Conn(Client) > NONE) message = Conn_EncodingFrom(Client_Conn(Client), Req->argv[1]); else #endif message = Req->argv[1]; /* handle msgtarget = msgto *("," msgto) */ currentTarget = strtok_r(currentTarget, ",", &strtok_last); ngt_UpperStr(Req->command); /* Please note that "currentTarget" is NULL when the target contains * the separator character only, e. g. "," or ",,,," etc.! */ while (currentTarget) { /* Make sure that there hasn't been such a target already: */ targets[target_nr++] = currentTarget; for(i = 0; i < target_nr - 1; i++) { if (strcasecmp(currentTarget, targets[i]) == 0) goto send_next_target; } /* Check for and handle valid <msgto> of form: * RFC 2812 2.3.1: * msgto = channel / ( user [ "%" host ] "@" servername ) * msgto =/ ( user "%" host ) / targetmask * msgto =/ nickname / ( nickname "!" user "@" host ) */ if (strchr(currentTarget, '!') == NULL) /* nickname */ cl = Client_Search(currentTarget); else cl = NULL; if (cl == NULL) { /* If currentTarget isn't a nickname check for: * user ["%" host] "@" servername * user "%" host * nickname "!" user "@" host */ char target[COMMAND_LEN]; char * nick = NULL; char * user = NULL; char * host = NULL; char * server = NULL; strlcpy(target, currentTarget, COMMAND_LEN); server = strchr(target, '@'); if (server) { *server = '\0'; server++; } host = strchr(target, '%'); if (host) { *host = '\0'; host++; } user = strchr(target, '!'); if (user) { /* msgto form: nick!user@host */ *user = '******'; user++; nick = target; host = server; /* not "@server" but "@host" */ } else { user = target; } for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) { if (Client_Type(cl) != CLIENT_USER && Client_Type(cl) != CLIENT_SERVICE) continue; if (nick != NULL && host != NULL) { if (strcasecmp(nick, Client_ID(cl)) == 0 && strcasecmp(user, Client_User(cl)) == 0 && strcasecmp(host, Client_HostnameDisplayed(cl)) == 0) break; else continue; } if (strcasecmp(user, Client_User(cl)) != 0) continue; if (host != NULL && strcasecmp(host, Client_HostnameDisplayed(cl)) != 0) continue; if (server != NULL && strcasecmp(server, Client_ID(Client_Introducer(cl))) != 0) continue; break; } } if (cl) { /* Target is a user, enforce type */ #ifndef STRICT_RFC if (Client_Type(cl) != ForceType && !(ForceType == CLIENT_USER && (Client_Type(cl) == CLIENT_USER || Client_Type(cl) == CLIENT_SERVICE))) { #else if (Client_Type(cl) != ForceType) { #endif if (SendErrors && !IRC_WriteErrClient( from, ERR_NOSUCHNICK_MSG,Client_ID(from), currentTarget)) return DISCONNECTED; goto send_next_target; } #ifndef STRICT_RFC if (ForceType == CLIENT_SERVICE && (Conn_Options(Client_Conn(Client_NextHop(cl))) & CONN_RFC1459)) { /* SQUERY command but RFC 1459 link: convert * request to PRIVMSG command */ Req->command = "PRIVMSG"; } #endif if (Client_HasMode(cl, 'b') && !Client_HasMode(from, 'R') && !Client_HasMode(from, 'o') && !(Client_Type(from) == CLIENT_SERVER) && !(Client_Type(from) == CLIENT_SERVICE)) { if (SendErrors && !IRC_WriteErrClient(from, ERR_NONONREG_MSG, Client_ID(from), Client_ID(cl))) return DISCONNECTED; goto send_next_target; } if (Client_HasMode(cl, 'C') && !Client_HasMode(from, 'o') && !(Client_Type(from) == CLIENT_SERVER) && !(Client_Type(from) == CLIENT_SERVICE)) { cl2chan = Channel_FirstChannelOf(cl); while (cl2chan) { chan = Channel_GetChannel(cl2chan); if (Channel_IsMemberOf(chan, from)) break; cl2chan = Channel_NextChannelOf(cl, cl2chan); } if (!cl2chan) { if (SendErrors && !IRC_WriteErrClient( from, ERR_NOTONSAMECHANNEL_MSG, Client_ID(from), Client_ID(cl))) return DISCONNECTED; goto send_next_target; } } if (SendErrors && (Client_Type(Client) != CLIENT_SERVER) && Client_HasMode(cl, 'a')) { /* Target is away */ if (!IRC_WriteStrClient(from, RPL_AWAY_MSG, Client_ID(from), Client_ID(cl), Client_Away(cl))) return DISCONNECTED; } if (Client_Conn(from) > NONE) { Conn_UpdateIdle(Client_Conn(from)); } if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s", Req->command, Client_ID(cl), message)) return DISCONNECTED; } else if (ForceType != CLIENT_SERVICE && (chan = Channel_Search(currentTarget))) { /* Target is a channel */ if (!Channel_Write(chan, from, Client, Req->command, SendErrors, message)) return DISCONNECTED; } else if (ForceType != CLIENT_SERVICE && strchr("$#", currentTarget[0]) && strchr(currentTarget, '.')) { /* $#: server/host mask, RFC 2812, sec. 3.3.1 */ if (!Send_Message_Mask(from, Req->command, currentTarget, message, SendErrors)) return DISCONNECTED; } else { if (!SendErrors) return CONNECTED; if (!IRC_WriteErrClient(from, ERR_NOSUCHNICK_MSG, Client_ID(from), currentTarget)) return DISCONNECTED; } send_next_target: currentTarget = strtok_r(NULL, ",", &strtok_last); if (!currentTarget) break; Conn_SetPenalty(Client_Conn(Client), 1); if (target_nr >= MAX_HNDL_TARGETS) { /* Too many targets given! */ return IRC_WriteErrClient(Client, ERR_TOOMANYTARGETS_MSG, currentTarget); } } return CONNECTED; } /* Send_Message */ /** * Send a message to "target mask" target(s). * * See RFC 2812, sec. 3.3.1 for details. * * @param from The client from which this command has been received. * @param command The command to use (PRIVMSG, NOTICE, ...). * @param targetMask The "target mask" (will be verified by this function). * @param message The message to send. * @param SendErrors Whether to report errors back to the client or not. * @return CONNECTED or DISCONNECTED. */ static bool Send_Message_Mask(CLIENT * from, char * command, char * targetMask, char * message, bool SendErrors) { CLIENT *cl; bool client_match; char *mask = targetMask + 1; const char *check_wildcards; cl = NULL; if (!Client_HasMode(from, 'o')) { if (!SendErrors) return true; return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG, Client_ID(from)); } /* * RFC 2812, sec. 3.3.1 requires that targetMask have at least one * dot (".") and no wildcards ("*", "?") following the last one. */ check_wildcards = strrchr(targetMask, '.'); if (!check_wildcards || check_wildcards[strcspn(check_wildcards, "*?")]) { if (!SendErrors) return true; return IRC_WriteErrClient(from, ERR_WILDTOPLEVEL_MSG, targetMask); } if (targetMask[0] == '#') { /* #: hostmask, see RFC 2812, sec. 3.3.1 */ for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) { if (Client_Type(cl) != CLIENT_USER) continue; client_match = MatchCaseInsensitive(mask, Client_Hostname(cl)); if (client_match) if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s", command, Client_ID(cl), message)) return false; } } else { /* $: server mask, see RFC 2812, sec. 3.3.1 */ assert(targetMask[0] == '$'); for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) { if (Client_Type(cl) != CLIENT_USER) continue; client_match = MatchCaseInsensitive(mask, Client_ID(Client_Introducer(cl))); if (client_match) if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s", command, Client_ID(cl), message)) return false; } } return CONNECTED; } /* Send_Message_Mask */
/* * Handler for the IRC "TRACE" command. * * @param Client The client from which this command has been received. * @param Req Request structure with prefix and all parameters. * @return CONNECTED or DISCONNECTED. */ GLOBAL bool IRC_TRACE(CLIENT *Client, REQUEST *Req) { CLIENT *from, *target, *c; CONN_ID idx, idx2; char user[CLIENT_USER_LEN]; assert(Client != NULL); assert(Req != NULL); _IRC_GET_SENDER_OR_RETURN_(from, Req, Client) _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from) /* Forward command to other server? */ if (target != Client_ThisServer()) { /* Send RPL_TRACELINK back to initiator */ idx = Client_Conn(Client); assert(idx > NONE); idx2 = Client_Conn(Client_NextHop(target)); assert(idx2 > NONE); if (!IRC_WriteStrClient(from, RPL_TRACELINK_MSG, Client_ID(from), PACKAGE_NAME, PACKAGE_VERSION, Client_ID(target), Client_ID(Client_NextHop(target)), Option_String(idx2), (long)(time(NULL) - Conn_StartTime(idx2)), Conn_SendQ(idx), Conn_SendQ(idx2))) return DISCONNECTED; /* Forward command */ IRC_WriteStrClientPrefix(target, from, "TRACE %s", Req->argv[0]); return CONNECTED; } /* Infos about all connected servers */ c = Client_First(); while (c) { if (Client_Conn(c) > NONE) { /* Local client */ if (Client_Type(c) == CLIENT_SERVER) { /* Server link */ strlcpy(user, Client_User(c), sizeof(user)); if (user[0] == '~') strlcpy(user, "unknown", sizeof(user)); if (!IRC_WriteStrClient(from, RPL_TRACESERVER_MSG, Client_ID(from), Client_ID(c), user, Client_Hostname(c), Client_Mask(Client_ThisServer()), Option_String(Client_Conn(c)))) return DISCONNECTED; } if (Client_Type(c) == CLIENT_USER && Client_HasMode(c, 'o')) { /* IRC Operator */ if (!IRC_WriteStrClient(from, RPL_TRACEOPERATOR_MSG, Client_ID(from), Client_ID(c))) return DISCONNECTED; } } c = Client_Next( c ); } return IRC_WriteStrClient(from, RPL_TRACEEND_MSG, Client_ID(from), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel); } /* IRC_TRACE */
/** * Handler for the IRC "USER" command. * * See RFC 2812, 3.1.3 "User message". * * @param Client The client from which this command has been received. * @param Req Request structure with prefix and all parameters. * @returns CONNECTED or DISCONNECTED. */ GLOBAL bool IRC_USER(CLIENT * Client, REQUEST * Req) { CLIENT *c; char *ptr; assert(Client != NULL); assert(Req != NULL); if (Client_Type(Client) == CLIENT_GOTNICK || #ifndef STRICT_RFC Client_Type(Client) == CLIENT_UNKNOWN || #endif Client_Type(Client) == CLIENT_GOTPASS) { /* New connection */ if (Req->argc != 4) return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command); /* User name: only alphanumeric characters and limited punctuation is allowed.*/ ptr = Req->argv[0]; while (*ptr) { if (!isalnum(*ptr) && *ptr != '+' && *ptr != '-' && *ptr != '.' && *ptr != '_') { Conn_Close(Client_Conn(Client), NULL, "Invalid user name", true); return DISCONNECTED; } ptr++; } #ifdef IDENTAUTH ptr = Client_User(Client); if (!ptr || !*ptr || *ptr == '~') Client_SetUser(Client, Req->argv[0], false); #else Client_SetUser(Client, Req->argv[0], false); #endif Client_SetOrigUser(Client, Req->argv[0]); /* "Real name" or user info text: Don't set it to the empty * string, the original ircd can't deal with such "real names" * (e. g. "USER user * * :") ... */ if (*Req->argv[3]) Client_SetInfo(Client, Req->argv[3]); else Client_SetInfo(Client, "-"); LogDebug("Connection %d: got valid USER command ...", Client_Conn(Client)); if (Client_Type(Client) == CLIENT_GOTNICK) return Login_User(Client); else Client_SetType(Client, CLIENT_GOTUSER); return CONNECTED; } else if (Client_Type(Client) == CLIENT_SERVER || Client_Type(Client) == CLIENT_SERVICE) { /* Server/service updating an user */ if (Req->argc != 4) return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command); c = Client_Search(Req->prefix); if (!c) return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix); Client_SetUser(c, Req->argv[0], true); Client_SetOrigUser(c, Req->argv[0]); Client_SetHostname(c, Req->argv[1]); Client_SetInfo(c, Req->argv[3]); LogDebug("Connection %d: got valid USER command for \"%s\".", Client_Conn(Client), Client_Mask(c)); /* RFC 1459 style user registration? * Introduce client to network: */ if (Client_Type(c) == CLIENT_GOTNICK) Client_Introduce(Client, c, CLIENT_USER); return CONNECTED; } else if (Client_Type(Client) == CLIENT_USER) { /* Already registered connection */ return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG, Client_ID(Client)); } else { /* Unexpected/invalid connection state? */ return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG, Client_ID(Client)); } } /* IRC_USER */