forked from packetflinger/q2admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathg_admin.c
More file actions
379 lines (346 loc) · 12.2 KB
/
Copy pathg_admin.c
File metadata and controls
379 lines (346 loc) · 12.2 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
/**
* Q2Admin
* ref/admin related stuff
*/
#include "g_local.h"
admin_t admin_pass[MAX_ADMINS];
admin_t bypass_pass[MAX_ADMINS];
int num_admins = 0;
int num_bypasses = 0;
/**
* Load admin and bypass users from the config files on disk.
*/
void readAdminConfig(void) {
FILE *f;
char name[256];
int i, i2;
Q_snprintf(name, sizeof(name), "%s/%s", moddir, configfile_login->string);
f = fopen(name, "rb");
if (!f) {
gi.dprintf("WARNING: %s could not be found\n", name);
goto file2;
return;
}
i = 0;
while ((!feof(f)) && (i < MAX_ADMINS)) {
fscanf(f, "%s %s %d", admin_pass[i].name, admin_pass[i].password, &admin_pass[i].level);
if (admin_pass[i].level > 0) {
i++;
}
}
num_admins = i;
if (i < MAX_ADMINS) {
for (i2 = i; i2 < MAX_ADMINS; i2++) {
admin_pass[i2].level = 0;
}
}
gi.cprintf(NULL, PRINT_HIGH, "%d admin users loaded\n", i);
fclose(f);
file2:
;
Q_snprintf(name, sizeof(name), "%s/%s", moddir, configfile_bypass->string);
f = fopen(name, "rb");
if (!f) {
gi.dprintf("WARNING: %s could not be found\n", name);
return;
}
i = 0;
while ((!feof(f)) && (i < MAX_ADMINS)) {
fscanf(f, "%s %s %d", bypass_pass[i].name, bypass_pass[i].password, &bypass_pass[i].level);
if (bypass_pass[i].level > 0) {
i++;
}
}
num_bypasses = i;
if (i < MAX_ADMINS) {
for (i2 = i; i2 < MAX_ADMINS; i2++) {
bypass_pass[i2].level = 0;
}
}
gi.cprintf(NULL, PRINT_HIGH, "%d bypass users loaded\n", i);
fclose(f);
}
/**
* Show an admin what commands they are permitted to use
*/
void listAdminCommands(edict_t *ent, int client) {
if (proxyinfo[client].admin_level & ADMIN_LEVEL1) {
gi.cprintf(ent, PRINT_HIGH, " - !boot <number>\n");
}
if (proxyinfo[client].admin_level & ADMIN_LEVEL2) {
gi.cprintf(ent, PRINT_HIGH, " - !dumpmsec\n");
}
if (proxyinfo[client].admin_level & ADMIN_LEVEL3) {
gi.cprintf(ent, PRINT_HIGH, " - !changemap <mapname>\n");
}
if (proxyinfo[client].admin_level & ADMIN_LEVEL4) {
gi.cprintf(ent, PRINT_HIGH, " - !dumpuser <num>\n");
}
if (proxyinfo[client].admin_level & ADMIN_LEVEL5) {
gi.cprintf(ent, PRINT_HIGH, " - !auth\n");
gi.cprintf(ent, PRINT_HIGH, " - !gfx\n");
}
if (proxyinfo[client].admin_level & ADMIN_LEVEL6) {
gi.cprintf(ent, PRINT_HIGH, " - !dostuff <num> <commands>\n");
}
if (proxyinfo[client].admin_level & ADMIN_LEVEL8) {
if (whois_active) {
gi.cprintf(ent, PRINT_HIGH, " - !writewhois\n");
}
}
gi.cprintf(ent, PRINT_HIGH, "\n");
}
/**
* Reload the admin/bypass users from disk.
*/
void reloadLoginFileRun(int startarg, edict_t *ent, int client) {
readAdminConfig();
gi.cprintf(ent, PRINT_HIGH, "Login file reloaded.\n");
}
/**
* Get the bypass level associated with a particular user entry
*/
int getBypassLevel(char *givenpass, char *givenname) {
int got_level = 0;
unsigned int i;
for (i = 0; i < num_bypasses; i++) {
if (!bypass_pass[i].level)
break;
if ((strcmp(givenpass, bypass_pass[i].password) == 0) && (strcmp(givenname, bypass_pass[i].name) == 0)) {
got_level = bypass_pass[i].level;
break;
}
}
return got_level;
}
/**
* Get the admin level associated with a particular user entry
*/
int getAdminLevel(char *givenpass, char *givenname) {
int got_level = 0;
unsigned int i;
for (i = 0; i < num_admins; i++) {
if (!admin_pass[i].level)
break;
if ((strcmp(givenpass, admin_pass[i].password) == 0) && (strcmp(givenname, admin_pass[i].name) == 0)) {
got_level = admin_pass[i].level;
break;
}
}
return got_level;
}
/**
* List out the players and their index. This probably shouldn't be an admin
* command, most mods provide a command like this, it's not providing any
* privileged information.
*/
void adm_players(edict_t *ent, int client) {
gi.cprintf(ent, PRINT_HIGH, "Player List\n");
for (int i = 0; i < maxclients->value; i++) {
if (proxyinfo[i].inuse) {
gi.cprintf(ent, PRINT_HIGH, " %2i : %s\n", i, NAME(i));
}
}
}
/**
* Admin command to display the current (from the previous ClientThink run)
* msec value for each player connected.
*/
void adm_dumpmsec(edict_t *ent, int client) {
gi.cprintf(ent, PRINT_HIGH, "Player MSEC Values:\n");
for (int i = 0; i < maxclients->value; i++) {
if (proxyinfo[i].inuse) {
gi.cprintf(ent, PRINT_HIGH, " %2i : %-16s %d\n", i, NAME(i), proxyinfo[i].msec.previous);
}
}
}
/**
* Display detailed info about a particular player, mostly from their userinfo
* string.
*/
void adm_dumpuser(edict_t *ent, int client, int user, bool check) {
if (gi.argc() < 2) {
adm_players(ent, client);
return;
}
if (check) {
if (!proxyinfo[user].inuse) {
return;
}
}
proxyinfo_t *pi = &proxyinfo[user];
char *ui = pi->userinfo.raw;
gi.cprintf(ent, PRINT_HIGH, "\nUser Info for \"%s\" [%d]\n",NAME(user), user);
gi.cprintf(ent, PRINT_HIGH, " ip address %s\n", IP(user));
gi.cprintf(ent, PRINT_HIGH, " sw version %s\n", pi->client_version);
if (FEATURE_SUPPORTED(GMF_EXTRA_USERINFO)) {
gi.cprintf(ent, PRINT_HIGH, " Protocol %d\n", pi->protocol_major);
gi.cprintf(ent, PRINT_HIGH, " protocol %d\n", pi->protocol_minor);
gi.cprintf(ent, PRINT_HIGH, " challenge %d\n", pi->challenge);
gi.cprintf(ent, PRINT_HIGH, " mtu %d\n", pi->mtu);
gi.cprintf(ent, PRINT_HIGH, " qport %d\n", pi->qport);
gi.cprintf(ent, PRINT_HIGH, " zlib %s\n", pi->zlib ? "yes" : "no");
}
gi.cprintf(ent, PRINT_HIGH, " msg level %s\n", Info_ValueForKey(ui, "msg"));
gi.cprintf(ent, PRINT_HIGH, " spectator %s\n", Info_ValueForKey(ui, "spectator"));
gi.cprintf(ent, PRINT_HIGH, " cl_maxfps %s\n", Info_ValueForKey(ui, "cl_maxfps"));
gi.cprintf(ent, PRINT_HIGH, " gender %s\n", Info_ValueForKey(ui, "gender"));
gi.cprintf(ent, PRINT_HIGH, " fov %s\n", Info_ValueForKey(ui, "fov"));
gi.cprintf(ent, PRINT_HIGH, " rate %s\n", Info_ValueForKey(ui, "rate"));
gi.cprintf(ent, PRINT_HIGH, " skin %s\n", Info_ValueForKey(ui, "skin"));
gi.cprintf(ent, PRINT_HIGH, " hand %s\n", Info_ValueForKey(ui, "hand"));
if (strlen(pi->gl_driver)) {
gi.cprintf(ent, PRINT_HIGH, " gl_driver %s\n", pi->gl_driver);
}
if (proxyinfo[client].admin_level & ADMIN_LEVEL8) {
gi.cprintf(ent, PRINT_HIGH, "\nFull Userinfo:\n \"%s\"\n", ui);
}
}
/**
* Make each client say their version info. This is pointless given the
* !versions command built-in to modern clients.
*/
void adm_auth(edict_t *ent) {
for (int i = 0; i < maxclients->value; i++) {
if (proxyinfo[i].inuse) {
stuffcmd(getEnt((i + 1)), "say I'm using $version\n");
}
}
}
/**
* Force all players to display their GL driver and mode.
*/
void adm_gfx(edict_t *ent) {
unsigned int i;
for (i = 0; i < maxclients->value; i++) {
if (proxyinfo[i].inuse) {
stuffcmd(getEnt((i + 1)), "say I'm using $gl_driver ( $vid_ref ) / $gl_mode\n");
}
}
}
/**
* Kick a player
*/
void adm_boot(edict_t *ent, int client, int user) {
char tmptext[100];
if (gi.argc() < 2) {
adm_players(ent, client);
return;
}
if ((user >= 0) && (user < maxclients->value)) {
if (proxyinfo[user].inuse) {
gi.bprintf(PRINT_HIGH, "%s was kicked by %s.\n", proxyinfo[user].name, proxyinfo[client].name);
Q_snprintf(tmptext, sizeof(tmptext), "\nkick %d\n", user);
gi.AddCommandString(tmptext);
}
}
}
/**
* Force a map change
*/
void adm_changemap(edict_t *ent, int client, char *mname) {
char tmptext[100];
if (q2a_strstr(mname, "\"")) {
return;
}
if (q2a_strstr(mname, ";")) {
return;
}
gi.bprintf(PRINT_HIGH, "%s is changing map to %s.\n", proxyinfo[client].name, mname);
Q_snprintf(tmptext, sizeof(tmptext), "\nmap %s\n", mname);
gi.AddCommandString(tmptext);
}
/**
* Handles running of admin commands by admin players
*/
int doAdminCommand(edict_t *ent, int client) {
unsigned int i, done = 0;
int send_to_client;
edict_t *send_to_ent;
char send_string[512];
char abuffer[256];
if (strlen(gi.args())) {
Q_snprintf(abuffer, sizeof(abuffer), "COMMAND - %s %s", gi.argv(0), gi.args());
logEvent(LT_ADMINLOG, client, ent, abuffer, 0, 0.0, true);
gi.dprintf("%s\n", abuffer);
}
if (proxyinfo[client].admin_level & ADMIN_LEVEL1) {
if (strcmp(gi.argv(0), "!boot") == 0) {
adm_boot(ent, client, atoi(gi.argv(1)));
done = 1;
}
}
if (proxyinfo[client].admin_level & ADMIN_LEVEL2) {
if (strcmp(gi.argv(0), "!dumpmsec") == 0) {
adm_dumpmsec(ent, client);
done = 1;
}
}
if (proxyinfo[client].admin_level & ADMIN_LEVEL3) {
if (strcmp(gi.argv(0), "!changemap") == 0) {
adm_changemap(ent, client, gi.argv(1));
done = 1;
}
}
if (proxyinfo[client].admin_level & ADMIN_LEVEL4) {
if (strcmp(gi.argv(0), "!dumpuser") == 0) {
adm_dumpuser(ent, client, atoi(gi.argv(1)), true);
done = 1;
} else if (strcmp(gi.argv(0), "!dumpuser_any") == 0) {
adm_dumpuser(ent, client, atoi(gi.argv(1)), false);
done = 1;
}
}
if (proxyinfo[client].admin_level & ADMIN_LEVEL5) {
if (strcmp(gi.argv(0), "!auth") == 0) {
adm_auth(ent);
done = 1;
gi.cprintf(ent, PRINT_HIGH, "A new auth command has been issued.\n");
} else if (strcmp(gi.argv(0), "!gfx") == 0) {
adm_gfx(ent);
done = 1;
gi.cprintf(ent, PRINT_HIGH, "Graphics command issued.\n");
}
}
if (proxyinfo[client].admin_level & ADMIN_LEVEL6) {
if (strcmp(gi.argv(0), "!dostuff") == 0) {
if (gi.argc() > 2) {
send_to_client = atoi(gi.argv(1));
if (strcmp(gi.argv(1), "all") == 0) {
for (send_to_client = 0; send_to_client < maxclients->value; send_to_client++)
if (proxyinfo[send_to_client].inuse) {
q2a_strncpy(send_string, gi.argv(2), sizeof(send_string));
if (gi.argc() > 3)
for (i = 3; i < gi.argc(); i++) {
strcat(send_string, " ");
strcat(send_string, gi.argv(i));
}
send_to_ent = getEnt((send_to_client + 1));
stuffcmd(send_to_ent, send_string);
gi.cprintf(ent, PRINT_HIGH, "Client %d (%s) has been stuffed!\n", send_to_client, proxyinfo[send_to_client].name);
}
} else
if (proxyinfo[send_to_client].inuse) {
q2a_strncpy(send_string, gi.argv(2), sizeof(send_string));
if (gi.argc() > 3)
for (i = 3; i < gi.argc(); i++) {
strcat(send_string, " ");
strcat(send_string, gi.argv(i));
}
send_to_ent = getEnt((send_to_client + 1));
stuffcmd(send_to_ent, send_string);
gi.cprintf(ent, PRINT_HIGH, "Client %d (%s) has been stuffed!\n", send_to_client, proxyinfo[send_to_client].name);
}
}
done = 2;
}
}
if (proxyinfo[client].admin_level & ADMIN_LEVEL8) {
if ((strcmp(gi.argv(0), "!writewhois") == 0) && (whois_active)) {
whois_write_file();
done = 1;
gi.cprintf(ent, PRINT_HIGH, "Whois file written.\n");
}
}
return done;
}