From 57c1f0dd4d3dbf0952591eece4a089ad8069cc36 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Fri, 9 Feb 2007 04:41:03 +0000 Subject: [PATCH] added and tested admin authentication. WOOT. only steam-based works for now. --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40466 --- core/CPlayerManager.cpp | 2 +- core/smn_player.cpp | 17 +++++++ plugins/admin-auth.sp | 59 ++++++++++++++++++++++++ plugins/admin-flatfile/admin-flatfile.sp | 2 +- plugins/admin-flatfile/admin-users.sp | 2 +- plugins/include/helpers.inc | 45 ++++++++++++++++++ plugins/include/sourcemod.inc | 28 +++++++---- 7 files changed, 144 insertions(+), 11 deletions(-) create mode 100644 plugins/admin-auth.sp create mode 100644 plugins/include/helpers.inc diff --git a/core/CPlayerManager.cpp b/core/CPlayerManager.cpp index e23a6783..052c709c 100644 --- a/core/CPlayerManager.cpp +++ b/core/CPlayerManager.cpp @@ -59,7 +59,7 @@ void CPlayerManager::OnSourceModAllInitialized() m_cldisconnect_post = g_Forwards.CreateForward("OnClientDisconnect_Post", ET_Ignore, 1, p2); m_clcommand = g_Forwards.CreateForward("OnClientCommand", ET_Hook, 1, p2); m_clinfochanged = g_Forwards.CreateForward("OnClientSettingsChanged", ET_Ignore, 1, p2); - m_clauth = g_Forwards.CreateForward("OnClientAuthorized", ET_Ignore, 2, p2); + m_clauth = g_Forwards.CreateForward("OnClientAuthorized", ET_Ignore, 2, p3); } void CPlayerManager::OnSourceModShutdown() diff --git a/core/smn_player.cpp b/core/smn_player.cpp index fb271179..df86366a 100644 --- a/core/smn_player.cpp +++ b/core/smn_player.cpp @@ -375,6 +375,22 @@ static cell_t GetUserFlagBits(IPluginContext *pContext, const cell_t *params) return g_Admins.GetAdminFlags(id, Access_Effective); } +static cell_t GetClientUserId(IPluginContext *pContext, const cell_t *params) +{ + int client = params[1]; + CPlayer *pPlayer = g_Players.GetPlayerByIndex(client); + if (!pPlayer) + { + return pContext->ThrowNativeError("Invalid client index %d.", client); + } + if (!pPlayer->IsConnected()) + { + return pContext->ThrowNativeError("Client %d is not connected.", client); + } + + return engine->GetPlayerUserId(pPlayer->GetEdict()); +} + REGISTER_NATIVES(playernatives) { {"GetMaxClients", sm_GetMaxClients}, @@ -394,6 +410,7 @@ REGISTER_NATIVES(playernatives) {"RemoveUserFlags", RemoveUserFlags}, {"SetUserFlagBits", SetUserFlagBits}, {"GetUserFlagBits", GetUserFlagBits}, + {"GetClientUserId", GetClientUserId}, {NULL, NULL} }; diff --git a/plugins/admin-auth.sp b/plugins/admin-auth.sp new file mode 100644 index 00000000..ae599166 --- /dev/null +++ b/plugins/admin-auth.sp @@ -0,0 +1,59 @@ +/** + * admin-flatfile.sp + * Manages the standard flat files for admins. This is the file to compile. + * This file is part of SourceMod, Copyright (C) 2004-2007 AlliedModders LLC + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + + +#include + +public Plugin:myinfo = +{ + name = "Admin Auth", + author = "AlliedModders LLC", + description = "Authenticates Admins", + version = "1.0.0.0", + url = "http://www.sourcemod.net/" +}; + +public OnClientAuthorized(client, const String:auth[]) +{ + if (StrEqual(auth, "BOT") + || StrEqual(auth, "STEAM_ID_LAN")) + { + return; + } + + new AdminId:id + if ((id = FindAdminByIdentity("steam", auth)) == INVALID_ADMIN_ID) + { + return; + } + + decl String:buffer[256], String:account[64]; + + FormatUserLogText(client, buffer, sizeof(buffer)); + + if (GetAdminUsername(id, account, sizeof(account))) + { + LogMessage("%s authenticated to account \"%s\"", buffer, account); + } else { + LogMessage("%s authenticated to an anonymous account", buffer); + } + + SetUserAdmin(client, id); +} diff --git a/plugins/admin-flatfile/admin-flatfile.sp b/plugins/admin-flatfile/admin-flatfile.sp index 73566e96..97a26ee7 100644 --- a/plugins/admin-flatfile/admin-flatfile.sp +++ b/plugins/admin-flatfile/admin-flatfile.sp @@ -24,7 +24,7 @@ public Plugin:myinfo = { - name = "Admin Base", + name = "Admin File Reader", author = "AlliedModders LLC", description = "Reads admin files", version = "1.0.0.0", diff --git a/plugins/admin-flatfile/admin-users.sp b/plugins/admin-flatfile/admin-users.sp index 2dbb57c8..4c091fe9 100644 --- a/plugins/admin-flatfile/admin-users.sp +++ b/plugins/admin-flatfile/admin-users.sp @@ -76,7 +76,7 @@ public SMCResult:ReadUsers_KeyValue(Handle:smc, { auth = true; StrCopy(g_CurAuth, sizeof(g_CurAuth), value); - } else if (StrEqual(key, "ident")) { + } else if (StrEqual(key, "identity")) { auth = true; StrCopy(g_CurIdent, sizeof(g_CurIdent), value); } else if (StrEqual(key, "password")) { diff --git a/plugins/include/helpers.inc b/plugins/include/helpers.inc new file mode 100644 index 00000000..2b0427f7 --- /dev/null +++ b/plugins/include/helpers.inc @@ -0,0 +1,45 @@ +/** + * =============================================================== + * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. + * =============================================================== + * + * This file is part of the SourceMod/SourcePawn SDK. This file may only be used + * or modified under the Terms and Conditions of its License Agreement, which is found + * in LICENSE.txt. The Terms and Conditions for making SourceMod extensions/plugins + * may change at any time. To view the latest information, see: + * http://www.sourcemod.net/license.php + * + * Version: $Id$ + */ + +#if defined _helpers_included + #endinput +#endif +#define _helpers_included + +/** + * Formats a user's info as log text. + * + * @param client Client index. + * @param buffer Buffer for text. + * @param maxlength Maximum length of text. + */ +stock FormatUserLogText(client, String:buffer[], maxlength) +{ + decl String:auth[32]; + decl String:name[40]; + + new userid = GetClientUserId(client); + if (!GetClientAuthString(client, auth, sizeof(auth))) + { + StrCopy(auth, sizeof(auth), "UNKNOWN"); + } + if (!GetClientName(client, name, sizeof(name))) + { + StrCopy(name, sizeof(name), "UNKNOWN"); + } + + /** Currently, no team stuff ... */ + + Format(buffer, maxlength, "\"%s<%d><%s><>\"", name, userid, auth); +} diff --git a/plugins/include/sourcemod.inc b/plugins/include/sourcemod.inc index 131fd2e3..4ee73244 100644 --- a/plugins/include/sourcemod.inc +++ b/plugins/include/sourcemod.inc @@ -164,9 +164,9 @@ native GetClientCount(bool:inGameOnly=true); /** * Returns the client's name. * - * @param client Player index. - * @param name Buffer to store the client's name. - * @param maxlen Maximum length of string buffer (includes NULL terminator). + * @param client Player index. + * @param name Buffer to store the client's name. + * @param maxlen Maximum length of string buffer (includes NULL terminator). * @return True on success, false otherwise. * @error If the client is not connected an error will be thrown. */ @@ -179,8 +179,8 @@ native bool:GetClientName(client, String:name[], maxlen); * @param name Buffer to store the client's ip address. * @param maxlen Maximum length of string buffer (includes NULL terminator). * @param remport Remove client's port from the ip string (true by default). - * @return True on success, false otherwise. - * @error If the client is not connected an error will be thrown. + * @return True on success, false otherwise. + * @error If the client is not connected or the index is invalid. */ native bool:GetClientIP(client, String:ip[], maxlen, bool:remport=true); @@ -190,11 +190,21 @@ native bool:GetClientIP(client, String:ip[], maxlen, bool:remport=true); * @param client Player index. * @param auth Buffer to store the client's auth string. * @param maxlen Maximum length of string buffer (includes NULL terminator). - * @return True on success, false otherwise. - * @error If the client is not connected an error will be thrown. + * @return True on success, false otherwise. + * @error If the client is not connected or the index is invalid. */ native bool:GetClientAuthString(client, String:auth[], maxlen); +/** + * Retrieves a client's user id, which is an index incremented for every client + * that joins the server. + * + * @param client Player index. + * @return User id of the client. + * @error If the client is not connected or the index is invalid. + */ +native GetClientUserId(client); + /** * Returns if a certain player is connected. * @@ -346,4 +356,6 @@ native LogMessage(const String:format[], {Handle,Float,String,_}:...); * @param ... Format arguments. * @noreturn */ -native LogError(const String:format[], {Handle,Float,String,_}:...); \ No newline at end of file +native LogError(const String:format[], {Handle,Float,String,_}:...); + +#include