[253] | 1 | using System.Collections.Generic;
|
---|
| 2 | using System.IO;
|
---|
| 3 | using System.Xml;
|
---|
| 4 |
|
---|
[325] | 5 | namespace AllocsFixes.NetConnections.Servers.Web {
|
---|
[253] | 6 | public class WebPermissions {
|
---|
[325] | 7 | private const string PERMISSIONS_FILE = "webpermissions.xml";
|
---|
| 8 | private static WebPermissions instance;
|
---|
| 9 | private readonly WebModulePermission defaultModulePermission = new WebModulePermission ("", 0);
|
---|
[253] | 10 |
|
---|
[325] | 11 | private readonly Dictionary<string, WebModulePermission> knownModules =
|
---|
| 12 | new Dictionary<string, WebModulePermission> ();
|
---|
| 13 |
|
---|
| 14 | private Dictionary<string, AdminToken> admintokens;
|
---|
| 15 | private FileSystemWatcher fileWatcher;
|
---|
| 16 |
|
---|
| 17 | private Dictionary<string, WebModulePermission> modules;
|
---|
| 18 |
|
---|
| 19 | public WebPermissions () {
|
---|
| 20 | Directory.CreateDirectory (GetFilePath ());
|
---|
| 21 | InitFileWatcher ();
|
---|
| 22 | Load ();
|
---|
| 23 | }
|
---|
| 24 |
|
---|
[253] | 25 | public static WebPermissions Instance {
|
---|
| 26 | get {
|
---|
[325] | 27 | lock (typeof (WebPermissions)) {
|
---|
[253] | 28 | if (instance == null) {
|
---|
| 29 | instance = new WebPermissions ();
|
---|
| 30 | }
|
---|
[325] | 31 |
|
---|
[253] | 32 | return instance;
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public bool ModuleAllowedWithLevel (string _module, int _level) {
|
---|
| 38 | WebModulePermission permInfo = GetModulePermission (_module);
|
---|
| 39 | return permInfo.permissionLevel >= _level;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public AdminToken GetWebAdmin (string _name, string _token) {
|
---|
| 43 | if (IsAdmin (_name) && admintokens [_name].token == _token) {
|
---|
| 44 | return admintokens [_name];
|
---|
| 45 | }
|
---|
[325] | 46 |
|
---|
| 47 | return null;
|
---|
[253] | 48 | }
|
---|
| 49 |
|
---|
| 50 | public WebModulePermission GetModulePermission (string _module) {
|
---|
| 51 | if (modules.ContainsKey (_module.ToLower ())) {
|
---|
| 52 | return modules [_module.ToLower ()];
|
---|
| 53 | }
|
---|
[325] | 54 |
|
---|
[253] | 55 | return defaultModulePermission;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | // Admins
|
---|
| 60 | public void AddAdmin (string _name, string _token, int _permissionLevel, bool _save = true) {
|
---|
| 61 | AdminToken c = new AdminToken (_name, _token, _permissionLevel);
|
---|
| 62 | lock (this) {
|
---|
| 63 | admintokens [_name] = c;
|
---|
| 64 | if (_save) {
|
---|
| 65 | Save ();
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public void RemoveAdmin (string _name, bool _save = true) {
|
---|
| 71 | lock (this) {
|
---|
| 72 | admintokens.Remove (_name);
|
---|
| 73 | if (_save) {
|
---|
| 74 | Save ();
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public bool IsAdmin (string _name) {
|
---|
| 80 | return admintokens.ContainsKey (_name);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | public AdminToken[] GetAdmins () {
|
---|
| 84 | AdminToken[] result = new AdminToken[admintokens.Count];
|
---|
| 85 | admintokens.Values.CopyTo (result, 0);
|
---|
| 86 | return result;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[325] | 89 |
|
---|
[253] | 90 | // Commands
|
---|
| 91 | public void AddModulePermission (string _module, int _permissionLevel, bool _save = true) {
|
---|
| 92 | WebModulePermission p = new WebModulePermission (_module.ToLower (), _permissionLevel);
|
---|
| 93 | lock (this) {
|
---|
| 94 | modules [_module] = p;
|
---|
| 95 | if (_save) {
|
---|
| 96 | Save ();
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[279] | 101 | public void AddKnownModule (string _module, int _defaultPermission) {
|
---|
[253] | 102 | if (!string.IsNullOrEmpty (_module)) {
|
---|
| 103 | lock (this) {
|
---|
[325] | 104 | if (!IsKnownModule (_module)) {
|
---|
[279] | 105 | knownModules.Add (_module, new WebModulePermission (_module, _defaultPermission));
|
---|
[325] | 106 | }
|
---|
| 107 |
|
---|
[279] | 108 | if (_defaultPermission > 0 && !modules.ContainsKey (_module.ToLower ())) {
|
---|
| 109 | AddModulePermission (_module, _defaultPermission);
|
---|
| 110 | }
|
---|
[253] | 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | public bool IsKnownModule (string _module) {
|
---|
| 116 | if (!string.IsNullOrEmpty (_module)) {
|
---|
| 117 | lock (this) {
|
---|
| 118 | return knownModules.ContainsKey (_module);
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
[325] | 121 |
|
---|
| 122 | return false;
|
---|
[253] | 123 | }
|
---|
[325] | 124 |
|
---|
[253] | 125 | public void RemoveModulePermission (string _module, bool _save = true) {
|
---|
| 126 | lock (this) {
|
---|
| 127 | modules.Remove (_module.ToLower ());
|
---|
| 128 | if (_save) {
|
---|
| 129 | Save ();
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | public List<WebModulePermission> GetModules () {
|
---|
| 135 | List<WebModulePermission> result = new List<WebModulePermission> ();
|
---|
| 136 | foreach (string module in knownModules.Keys) {
|
---|
| 137 | if (modules.ContainsKey (module)) {
|
---|
| 138 | result.Add (modules [module]);
|
---|
| 139 | } else {
|
---|
| 140 | result.Add (knownModules [module]);
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | return result;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[325] | 147 |
|
---|
[253] | 148 | //IO Tasks
|
---|
| 149 |
|
---|
| 150 | private void InitFileWatcher () {
|
---|
| 151 | fileWatcher = new FileSystemWatcher (GetFilePath (), GetFileName ());
|
---|
[325] | 152 | fileWatcher.Changed += OnFileChanged;
|
---|
| 153 | fileWatcher.Created += OnFileChanged;
|
---|
| 154 | fileWatcher.Deleted += OnFileChanged;
|
---|
[253] | 155 | fileWatcher.EnableRaisingEvents = true;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | private void OnFileChanged (object source, FileSystemEventArgs e) {
|
---|
| 159 | Log.Out ("Reloading " + PERMISSIONS_FILE);
|
---|
| 160 | Load ();
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | private string GetFilePath () {
|
---|
| 164 | return GamePrefs.GetString (EnumGamePrefs.SaveGameFolder);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | private string GetFileName () {
|
---|
| 168 | return PERMISSIONS_FILE;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | private string GetFullPath () {
|
---|
| 172 | return GetFilePath () + "/" + GetFileName ();
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | public void Load () {
|
---|
| 176 | admintokens = new Dictionary<string, AdminToken> ();
|
---|
| 177 | modules = new Dictionary<string, WebModulePermission> ();
|
---|
| 178 |
|
---|
| 179 | if (!Utils.FileExists (GetFullPath ())) {
|
---|
| 180 | Log.Out (string.Format ("Permissions file '{0}' not found, creating.", GetFileName ()));
|
---|
| 181 | Save ();
|
---|
| 182 | return;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | Log.Out (string.Format ("Loading permissions file at '{0}'", GetFullPath ()));
|
---|
| 186 |
|
---|
| 187 | XmlDocument xmlDoc = new XmlDocument ();
|
---|
| 188 |
|
---|
| 189 | try {
|
---|
| 190 | xmlDoc.Load (GetFullPath ());
|
---|
| 191 | } catch (XmlException e) {
|
---|
| 192 | Log.Error (string.Format ("Failed loading permissions file: {0}", e.Message));
|
---|
| 193 | return;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | XmlNode adminToolsNode = xmlDoc.DocumentElement;
|
---|
| 197 |
|
---|
| 198 | foreach (XmlNode childNode in adminToolsNode.ChildNodes) {
|
---|
| 199 | if (childNode.Name == "admintokens") {
|
---|
| 200 | foreach (XmlNode subChild in childNode.ChildNodes) {
|
---|
| 201 | if (subChild.NodeType == XmlNodeType.Comment) {
|
---|
| 202 | continue;
|
---|
| 203 | }
|
---|
[325] | 204 |
|
---|
[253] | 205 | if (subChild.NodeType != XmlNodeType.Element) {
|
---|
| 206 | Log.Warning ("Unexpected XML node found in 'admintokens' section: " + subChild.OuterXml);
|
---|
| 207 | continue;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[325] | 210 | XmlElement lineItem = (XmlElement) subChild;
|
---|
[253] | 211 |
|
---|
| 212 | if (!lineItem.HasAttribute ("name")) {
|
---|
[325] | 213 | Log.Warning ("Ignoring admintoken-entry because of missing 'name' attribute: " +
|
---|
| 214 | subChild.OuterXml);
|
---|
[253] | 215 | continue;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | if (!lineItem.HasAttribute ("token")) {
|
---|
[325] | 219 | Log.Warning ("Ignoring admintoken-entry because of missing 'token' attribute: " +
|
---|
| 220 | subChild.OuterXml);
|
---|
[253] | 221 | continue;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | if (!lineItem.HasAttribute ("permission_level")) {
|
---|
[325] | 225 | Log.Warning ("Ignoring admintoken-entry because of missing 'permission_level' attribute: " +
|
---|
| 226 | subChild.OuterXml);
|
---|
[253] | 227 | continue;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | string name = lineItem.GetAttribute ("name");
|
---|
| 231 | string token = lineItem.GetAttribute ("token");
|
---|
| 232 | int permissionLevel = 2000;
|
---|
| 233 | if (!int.TryParse (lineItem.GetAttribute ("permission_level"), out permissionLevel)) {
|
---|
[325] | 234 | Log.Warning (
|
---|
| 235 | "Ignoring admintoken-entry because of invalid (non-numeric) value for 'permission_level' attribute: " +
|
---|
| 236 | subChild.OuterXml);
|
---|
[253] | 237 | continue;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[325] | 240 | AddAdmin (name, token, permissionLevel, false);
|
---|
[253] | 241 | }
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | if (childNode.Name == "permissions") {
|
---|
| 245 | foreach (XmlNode subChild in childNode.ChildNodes) {
|
---|
| 246 | if (subChild.NodeType == XmlNodeType.Comment) {
|
---|
| 247 | continue;
|
---|
| 248 | }
|
---|
[325] | 249 |
|
---|
[253] | 250 | if (subChild.NodeType != XmlNodeType.Element) {
|
---|
| 251 | Log.Warning ("Unexpected XML node found in 'permissions' section: " + subChild.OuterXml);
|
---|
| 252 | continue;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[325] | 255 | XmlElement lineItem = (XmlElement) subChild;
|
---|
[253] | 256 |
|
---|
| 257 | if (!lineItem.HasAttribute ("module")) {
|
---|
[325] | 258 | Log.Warning ("Ignoring permission-entry because of missing 'module' attribute: " +
|
---|
| 259 | subChild.OuterXml);
|
---|
[253] | 260 | continue;
|
---|
| 261 | }
|
---|
[325] | 262 |
|
---|
[253] | 263 | if (!lineItem.HasAttribute ("permission_level")) {
|
---|
[325] | 264 | Log.Warning ("Ignoring permission-entry because of missing 'permission_level' attribute: " +
|
---|
| 265 | subChild.OuterXml);
|
---|
[253] | 266 | continue;
|
---|
| 267 | }
|
---|
[325] | 268 |
|
---|
[253] | 269 | int permissionLevel = 0;
|
---|
| 270 | if (!int.TryParse (lineItem.GetAttribute ("permission_level"), out permissionLevel)) {
|
---|
[325] | 271 | Log.Warning (
|
---|
| 272 | "Ignoring permission-entry because of invalid (non-numeric) value for 'permission_level' attribute: " +
|
---|
| 273 | subChild.OuterXml);
|
---|
[253] | 274 | continue;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[325] | 277 | AddModulePermission (lineItem.GetAttribute ("module").ToLower (), permissionLevel, false);
|
---|
[253] | 278 | }
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | Log.Out ("Loading permissions file done.");
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | public void Save () {
|
---|
| 286 | fileWatcher.EnableRaisingEvents = false;
|
---|
| 287 |
|
---|
[325] | 288 | using (StreamWriter sw = new StreamWriter (GetFullPath ())) {
|
---|
| 289 | sw.WriteLine ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
---|
| 290 | sw.WriteLine ("<webpermissions>");
|
---|
| 291 | sw.WriteLine ();
|
---|
| 292 | sw.WriteLine (" <admintokens>");
|
---|
| 293 | sw.WriteLine (
|
---|
| 294 | " <!-- <token name=\"adminuser1\" token=\"supersecrettoken\" permission_level=\"0\" /> -->");
|
---|
| 295 | foreach (AdminToken at in admintokens.Values) {
|
---|
| 296 | sw.WriteLine (" <token name=\"{0}\" token=\"{1}\" permission_level=\"{2}\" />", at.name, at.token,
|
---|
| 297 | at.permissionLevel);
|
---|
| 298 | }
|
---|
[253] | 299 |
|
---|
[325] | 300 | sw.WriteLine (" </admintokens>");
|
---|
| 301 | sw.WriteLine ();
|
---|
| 302 | sw.WriteLine (" <permissions>");
|
---|
| 303 | foreach (WebModulePermission wap in modules.Values) {
|
---|
| 304 | sw.WriteLine (" <permission module=\"{0}\" permission_level=\"{1}\" />", wap.module,
|
---|
| 305 | wap.permissionLevel);
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | sw.WriteLine (" <!-- <permission module=\"web.map\" permission_level=\"1000\" /> -->");
|
---|
| 309 | sw.WriteLine ();
|
---|
| 310 | sw.WriteLine (" <!-- <permission module=\"webapi.getlog\" permission_level=\"0\" /> -->");
|
---|
| 311 | sw.WriteLine (
|
---|
| 312 | " <!-- <permission module=\"webapi.executeconsolecommand\" permission_level=\"0\" /> -->");
|
---|
| 313 | sw.WriteLine ();
|
---|
| 314 | sw.WriteLine (" <!-- <permission module=\"webapi.getstats\" permission_level=\"1000\" /> -->");
|
---|
| 315 | sw.WriteLine (" <!-- <permission module=\"webapi.getplayersonline\" permission_level=\"1000\" /> -->");
|
---|
| 316 | sw.WriteLine ();
|
---|
| 317 | sw.WriteLine (
|
---|
| 318 | " <!-- <permission module=\"webapi.getplayerslocation\" permission_level=\"1000\" /> -->");
|
---|
| 319 | sw.WriteLine (" <!-- <permission module=\"webapi.viewallplayers\" permission_level=\"1\" /> -->");
|
---|
| 320 | sw.WriteLine ();
|
---|
| 321 | sw.WriteLine (" <!-- <permission module=\"webapi.getlandclaims\" permission_level=\"1000\" /> -->");
|
---|
| 322 | sw.WriteLine (" <!-- <permission module=\"webapi.viewallclaims\" permission_level=\"1\" /> -->");
|
---|
| 323 | sw.WriteLine ();
|
---|
| 324 | sw.WriteLine (" <!-- <permission module=\"webapi.getplayerinventory\" permission_level=\"1\" /> -->");
|
---|
| 325 | sw.WriteLine ();
|
---|
| 326 | sw.WriteLine (" <!-- <permission module=\"webapi.gethostilelocation\" permission_level=\"1\" /> -->");
|
---|
| 327 | sw.WriteLine (" <!-- <permission module=\"webapi.getanimalslocation\" permission_level=\"1\" /> -->");
|
---|
| 328 | sw.WriteLine (" </permissions>");
|
---|
| 329 | sw.WriteLine ();
|
---|
| 330 | sw.WriteLine ("</webpermissions>");
|
---|
| 331 |
|
---|
[253] | 332 | sw.Flush ();
|
---|
| 333 | sw.Close ();
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | fileWatcher.EnableRaisingEvents = true;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 |
|
---|
| 340 | public class AdminToken {
|
---|
| 341 | public string name;
|
---|
[325] | 342 | public int permissionLevel;
|
---|
[253] | 343 | public string token;
|
---|
| 344 |
|
---|
| 345 | public AdminToken (string _name, string _token, int _permissionLevel) {
|
---|
| 346 | name = _name;
|
---|
| 347 | token = _token;
|
---|
| 348 | permissionLevel = _permissionLevel;
|
---|
| 349 | }
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | public struct WebModulePermission {
|
---|
| 353 | public string module;
|
---|
| 354 | public int permissionLevel;
|
---|
| 355 |
|
---|
| 356 | public WebModulePermission (string _module, int _permissionLevel) {
|
---|
| 357 | module = _module;
|
---|
| 358 | permissionLevel = _permissionLevel;
|
---|
| 359 | }
|
---|
| 360 | }
|
---|
| 361 | }
|
---|
[325] | 362 | }
|
---|