forked from alliedmodders/sourcemod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-sql-prefetch.sp
More file actions
379 lines (321 loc) · 9.63 KB
/
admin-sql-prefetch.sp
File metadata and controls
379 lines (321 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod SQL Admins Plugin (Prefetch)
* Prefetches admins from an SQL database without threading.
*
* SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
/* We like semicolons */
#pragma semicolon 1
#include <sourcemod>
#pragma newdecls required
public Plugin myinfo =
{
name = "SQL Admins (Prefetch)",
author = "AlliedModders LLC",
description = "Reads all admins from SQL",
version = SOURCEMOD_VERSION,
url = "http://www.sourcemod.net/"
};
public void OnRebuildAdminCache(AdminCachePart part)
{
/* First try to get a database connection */
char error[255];
Database db;
if (SQL_CheckConfig("admins"))
{
db = SQL_Connect("admins", true, error, sizeof(error));
} else {
db = SQL_Connect("default", true, error, sizeof(error));
}
if (db == null)
{
LogError("Could not connect to database \"default\": %s", error);
return;
}
if (part == AdminCache_Overrides)
{
FetchOverrides(db);
} else if (part == AdminCache_Groups) {
FetchGroups(db);
} else if (part == AdminCache_Admins) {
FetchUsers(db);
}
delete db;
}
void FetchUsers(Database db)
{
char query[255], error[255];
DBResultSet rs;
Format(query, sizeof(query), "SELECT id, authtype, identity, password, flags, name, immunity FROM sm_admins");
if ((rs = SQL_Query(db, query)) == null)
{
SQL_GetError(db, error, sizeof(error));
LogError("FetchUsers() query failed: %s", query);
LogError("Query error: %s", error);
return;
}
char authtype[16];
char identity[80];
char password[80];
char flags[32];
char name[80];
int immunity;
AdminId adm;
GroupId grp;
int id;
/* Keep track of a mapping from admin DB IDs to internal AdminIds to
* enable group lookups en masse */
StringMap htAdmins = new StringMap();
char key[16];
while (rs.FetchRow())
{
id = rs.FetchInt(0);
IntToString(id, key, sizeof(key));
rs.FetchString(1, authtype, sizeof(authtype));
rs.FetchString(2, identity, sizeof(identity));
rs.FetchString(3, password, sizeof(password));
rs.FetchString(4, flags, sizeof(flags));
rs.FetchString(5, name, sizeof(name));
immunity = rs.FetchInt(6);
/* Use a pre-existing admin if we can */
if ((adm = FindAdminByIdentity(authtype, identity)) == INVALID_ADMIN_ID)
{
adm = CreateAdmin(name);
if (!adm.BindIdentity(authtype, identity))
{
LogError("Could not bind prefetched SQL admin (authtype \"%s\") (identity \"%s\")", authtype, identity);
continue;
}
}
htAdmins.SetValue(key, adm);
#if defined _DEBUG
PrintToServer("Found SQL admin (%d,%s,%s,%s,%s,%s,%d):%d", id, authtype, identity, password, flags, name, immunity, adm);
#endif
/* See if this admin wants a password */
if (password[0] != '\0')
{
adm.SetPassword(password);
}
/* Apply each flag */
int len = strlen(flags);
AdminFlag flag;
for (int i=0; i<len; i++)
{
if (!FindFlagByChar(flags[i], flag))
{
continue;
}
adm.SetFlag(flag, true);
}
adm.ImmunityLevel = immunity;
}
delete rs;
Format(query, sizeof(query), "SELECT ag.admin_id AS id, g.name FROM sm_admins_groups ag JOIN sm_groups g ON ag.group_id = g.id ORDER BY id, inherit_order ASC");
if ((rs = SQL_Query(db, query)) == null)
{
SQL_GetError(db, error, sizeof(error));
LogError("FetchUsers() query failed: %s", query);
LogError("Query error: %s", error);
return;
}
char group[80];
while (rs.FetchRow())
{
IntToString(rs.FetchInt(0), key, sizeof(key));
rs.FetchString(1, group, sizeof(group));
if (htAdmins.GetValue(key, adm))
{
if ((grp = FindAdmGroup(group)) == INVALID_GROUP_ID)
{
/* Group wasn't found, don't bother with it. */
continue;
}
adm.InheritGroup(grp);
}
}
delete rs;
delete htAdmins;
}
void FetchGroups(Database db)
{
char query[255];
DBResultSet rs;
Format(query, sizeof(query), "SELECT flags, name, immunity_level FROM sm_groups");
if ((rs = SQL_Query(db, query)) == null)
{
char error[255];
SQL_GetError(db, error, sizeof(error));
LogError("FetchGroups() query failed: %s", query);
LogError("Query error: %s", error);
return;
}
/* Now start fetching groups */
char flags[32];
char name[128];
int immunity;
while (rs.FetchRow())
{
rs.FetchString(0, flags, sizeof(flags));
rs.FetchString(1, name, sizeof(name));
immunity = rs.FetchInt(2);
#if defined _DEBUG
PrintToServer("Adding group (%d, %s, %s)", immunity, flags, name);
#endif
/* Find or create the group */
GroupId grp;
if ((grp = FindAdmGroup(name)) == INVALID_GROUP_ID)
{
grp = CreateAdmGroup(name);
}
/* Add flags from the database to the group */
int num_flag_chars = strlen(flags);
for (int i=0; i<num_flag_chars; i++)
{
AdminFlag flag;
if (!FindFlagByChar(flags[i], flag))
{
continue;
}
grp.SetFlag(flag, true);
}
/* Set the immunity level this group has */
grp.ImmunityLevel = immunity;
}
delete rs;
/**
* Get immunity in a big lump. This is a nasty query but it gets the job done.
*/
int len = 0;
len += Format(query[len], sizeof(query)-len, "SELECT g1.name, g2.name FROM sm_group_immunity gi");
len += Format(query[len], sizeof(query)-len, " LEFT JOIN sm_groups g1 ON g1.id = gi.group_id ");
len += Format(query[len], sizeof(query)-len, " LEFT JOIN sm_groups g2 ON g2.id = gi.other_id");
if ((rs = SQL_Query(db, query)) == null)
{
char error[255];
SQL_GetError(db, error, sizeof(error));
LogError("FetchGroups() query failed: %s", query);
LogError("Query error: %s", error);
return;
}
while (rs.FetchRow())
{
char group1[80];
char group2[80];
GroupId grp, other;
rs.FetchString(0, group1, sizeof(group1));
rs.FetchString(1, group2, sizeof(group2));
if (((grp = FindAdmGroup(group1)) == INVALID_GROUP_ID)
|| (other = FindAdmGroup(group2)) == INVALID_GROUP_ID)
{
continue;
}
grp.AddGroupImmunity(other);
#if defined _DEBUG
PrintToServer("SetAdmGroupImmuneFrom(%d, %d)", grp, other);
#endif
}
delete rs;
/**
* Fetch overrides in a lump query.
*/
Format(query, sizeof(query), "SELECT g.name, go.type, go.name, go.access FROM sm_group_overrides go LEFT JOIN sm_groups g ON go.group_id = g.id");
if ((rs = SQL_Query(db, query)) == null)
{
char error[255];
SQL_GetError(db, error, sizeof(error));
LogError("FetchGroups() query failed: %s", query);
LogError("Query error: %s", error);
return;
}
char type[16];
char cmd[64];
char access[16];
while (rs.FetchRow())
{
rs.FetchString(0, name, sizeof(name));
rs.FetchString(1, type, sizeof(type));
rs.FetchString(2, cmd, sizeof(cmd));
rs.FetchString(3, access, sizeof(access));
GroupId grp;
if ((grp = FindAdmGroup(name)) == INVALID_GROUP_ID)
{
continue;
}
OverrideType o_type = Override_Command;
if (StrEqual(type, "group"))
{
o_type = Override_CommandGroup;
}
OverrideRule o_rule = Command_Deny;
if (StrEqual(access, "allow"))
{
o_rule = Command_Allow;
}
#if defined _DEBUG
PrintToServer("AddAdmGroupCmdOverride(%d, %s, %d, %d)", grp, cmd, o_type, o_rule);
#endif
grp.AddCommandOverride(cmd, o_type, o_rule);
}
delete rs;
}
void FetchOverrides(Database db)
{
char query[255];
DBResultSet rs;
Format(query, sizeof(query), "SELECT type, name, flags FROM sm_overrides");
if ((rs = SQL_Query(db, query)) == null)
{
char error[255];
SQL_GetError(db, error, sizeof(error));
LogError("FetchOverrides() query failed: %s", query);
LogError("Query error: %s", error);
return;
}
char type[64];
char name[64];
char flags[32];
int flag_bits;
while (rs.FetchRow())
{
rs.FetchString(0, type, sizeof(type));
rs.FetchString(1, name, sizeof(name));
rs.FetchString(2, flags, sizeof(flags));
#if defined _DEBUG
PrintToServer("Adding override (%s, %s, %s)", type, name, flags);
#endif
flag_bits = ReadFlagString(flags);
if (StrEqual(type, "command"))
{
AddCommandOverride(name, Override_Command, flag_bits);
} else if (StrEqual(type, "group")) {
AddCommandOverride(name, Override_CommandGroup, flag_bits);
}
}
delete rs;
}