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