]> jfr.im git - irc/quakenet/newserv.git/blob - noperserv/noperserv.c
r646@blue (orig r494): slug | 2006-05-15 23:35:33 +0100
[irc/quakenet/newserv.git] / noperserv / noperserv.c
1 /*
2 * NOperserv v0.01
3 *
4 * A replacement for Germania's ageing Operservice2
5 *
6 * Copyright (C) 2005 Chris Porter.
7 */
8
9 #include "../localuser/localuser.h"
10 #include "../lib/irc_string.h"
11 #include "../lib/strlfunc.h"
12 #include "noperserv.h"
13 #include "noperserv_db.h"
14 #include "noperserv_hooks.h"
15 #include "noperserv_policy.h"
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdarg.h>
20
21 #define FLAGBUFLEN 100
22
23 #define NO_FOUND_NICKNAME 1
24 #define NO_FOUND_AUTHNAME 2
25
26 const flag no_commandflags[] = {
27 { 'o', __NO_OPER },
28 { 't', __NO_TRUST },
29 { 's', __NO_STAFF },
30 { 'S', __NO_SEC },
31 { 'd', __NO_DEVELOPER },
32 { 'L', __NO_LEGACY },
33 { 'O', __NO_OPERED },
34 { 'r', __NO_AUTHED },
35 { 'R', __NO_ACCOUNT },
36 { '\0', 0 }
37 };
38
39 const flag no_userflags[] = {
40 { 'o', __NO_OPER },
41 { 't', __NO_TRUST },
42 { 's', __NO_STAFF },
43 { 'S', __NO_SEC },
44 { 'd', __NO_DEVELOPER },
45 { '\0', 0 }
46 };
47
48 const flag no_noticeflags[] = {
49 { 'm', NL_MANAGEMENT }, /* hello, password, userflags, noticeflags */
50 { 't', NL_TRUSTS }, /* trust stuff... */
51 { 'k', NL_KICKKILLS }, /* KICK/KILL commands */
52 { 'I', NL_MISC }, /* misc commands */
53 { 'g', NL_GLINES }, /* GLINE commands */
54 { 'h', NL_HITS }, /* Where a gline or kill is set automatically by the bot */
55 { 'c', NL_CLONING }, /* Clone detection */
56 { 'C', NL_CLEARCHAN }, /* When someone clearchans */
57 { 'f', NL_FAKEUSERS }, /* Fakeuser addition */
58 { 'b', NL_BROADCASTS }, /* Broadcast/mbroadcast/sbroadcast */
59 { 'o', NL_OPERATIONS }, /* insmod/rmmod/etc */
60 { 'O', NL_OPERING }, /* when someone opers */
61 { 'n', NL_NOTICES }, /* turn off to receive notices instead of privmsgs */
62 { 'A', NL_ALL_COMMANDS }, /* all commands sent */
63 { '\0', 0 }
64 };
65
66 int noperserv_hello(void *sender, int cargc, char **cargv);
67 int noperserv_noticeflags(void *sender, int cargc, char **cargv);
68 int noperserv_userflags(void *sender, int cargc, char **cargv);
69 int noperserv_deluser(void *sender, int cargc, char **cargv);
70 void noperserv_oper_detection(int hooknum, void *arg);
71 void noperserv_reply(nick *np, char *format, ...);
72
73 int init = 0;
74
75 void _init() {
76 if(!noperserv_load_db())
77 return;
78
79 noperserv_ext = registernickext("noperserv");
80
81 noperserv_setup_hooks();
82
83 registercontrolhelpcmd("hello", NO_OPERED | NO_AUTHED, 1, &noperserv_hello, "Syntax: HELLO ?nickname|#authname?\nCreates an account on the service for the specified nick, or if one isn't supplied, your nickname.");
84 registercontrolhelpcmd("userflags", NO_ACCOUNT, 2, &noperserv_userflags,
85 "Syntax: USERFLAGS <nickname|#authname> ?modifications?\n"
86 " Views and modifies user permissions.\n"
87 " If no nickname or authname is supplied, you are substituted for it.\n"
88 " If no flags are supplied, flags are just displayed instead of modified."
89 " Flags:\n"
90 " +o: Operator\n"
91 " +s: Staff member\n"
92 " +S: Security team member\n"
93 " +d: NOperserv developer\n"
94 " +t: Trust queue worker\n"
95 " Additional flags may show up in SHOWCOMMANDS but are not userflags as such:\n"
96 " +r: Authed user\n"
97 " +R: Registered NOperserv user\n"
98 " +O: Must be /OPER'ed\n"
99 " +L: Legacy command\n"
100 );
101 registercontrolhelpcmd("noticeflags", NO_ACCOUNT, 1, &noperserv_noticeflags,
102 "Syntax: NOTICEFLAGS ?(nickname|#authname)|flags?\n"
103 " This command can view and modify your own notice flags, and view that of other users.\n"
104 " Flags:\n"
105 " +m: Management (hello, password, userflags, noticeflags)\n"
106 " +t: Trusts\n"
107 " +k: KICK/KILL commands\n"
108 " +g: GLINE commands\n"
109 " +h: Shows when glines are played automatically (hits)\n"
110 " +c: Clone information\n"
111 " +C: CLEARCHAN command\n"
112 " +f: FAKEUSER commands\n"
113 " +b: BROADCAST commands\n"
114 " +o: Operation commands, such as insmod, rmmod, die, etc\n"
115 " +O: /OPER\n"
116 " +I: Misc commands (resync, etc)\n"
117 " +n: Sends notices instead of privmsgs\n"
118 " +A: Every single command sent to the service (spammy)\n"
119 );
120
121 registercontrolhelpcmd("deluser", NO_OPERED | NO_ACCOUNT, 2, &noperserv_deluser, "Syntax: DELUSER <nickname|#authname>\nDeletes the specified user.");
122 registerhook(HOOK_NICK_MODEOPER, &noperserv_oper_detection);
123
124 init = 1;
125 }
126
127 #ifdef BROKEN_DLCLOSE
128 void __fini() {
129 #else
130 void _fini() {
131 #endif
132 if(!init)
133 return;
134
135 deregisterhook(HOOK_NICK_MODEOPER, &noperserv_oper_detection);
136
137 deregistercontrolcmd("noticeflags", &noperserv_noticeflags);
138 deregistercontrolcmd("userflags", &noperserv_userflags);
139 deregistercontrolcmd("noticeflags", &noperserv_noticeflags);
140
141 noperserv_cleanup_hooks();
142
143 noperserv_cleanup_db();
144
145 releasenickext(noperserv_ext);
146 }
147
148 /* @test */
149 int noperserv_hello(void *sender, int cargc, char **cargv) {
150 char *newaccount = NULL;
151 no_autheduser *au;
152 int i;
153 nick *np = (nick *)sender, *np2, *target = NULL;
154
155 if(cargc == 0) {
156 newaccount = np->authname;
157 } else {
158 if(cargv[0][0] == '#') {
159 nick *np2;
160 for(i=0;i<NICKHASHSIZE;i++)
161 for(np2=nicktable[i];np2;np2=np2->next)
162 if(IsAccount(np2) && !ircd_strcmp(cargv[0] + 1, np2->authname)) {
163 target = np2;
164 newaccount = target->authname;
165 break;
166 }
167 if(!target) {
168 controlreply(np, "Cannot find anyone with that authname on the network.");
169 return CMD_ERROR;
170 }
171 } else {
172 target = getnickbynick(cargv[0]);
173 if(!target) {
174 controlreply(np, "Supplied nickname is not on the network.");
175 return CMD_ERROR;
176 }
177 if(!IsAccount(target)) {
178 controlreply(np, "Supplied user is not authed with the network.");
179 return CMD_ERROR;
180 }
181 newaccount = target->authname;
182 }
183 }
184 au = noperserv_get_autheduser(newaccount);
185 if(au) {
186 controlreply(np, "Authname already registered.");
187 return CMD_ERROR;
188 }
189
190 au = noperserv_new_autheduser(newaccount);
191 if(!au) {
192 controlreply(np, "Memory allocation error.");
193 return CMD_ERROR;
194 }
195
196 if(noperserv_get_autheduser_count() == 1) {
197 au->authlevel = NO_FIRST_USER_LEVEL;
198 au->noticelevel = NO_FIRST_USER_DEFAULT_NOTICELEVEL;
199 } else {
200 au->authlevel = NO_DEFAULT_LEVEL;
201 au->noticelevel = NO_DEFAULT_NOTICELEVEL;
202 }
203
204 au->id = noperserv_next_autheduser_id();
205 noperserv_update_autheduser(au);
206
207 for(i=0;i<NICKHASHSIZE;i++)
208 for(np2=nicktable[i];np2;np2=np2->next)
209 if(IsAccount(np2) && !ircd_strcmp(newaccount, np2->authname)) {
210 noperserv_add_to_autheduser(np2, au);
211 controlreply(np2, "An account has been created for you (auth %s).", au->authname->content);
212 if(NOGetAuthLevel(au))
213 controlreply(np2, "User flags: %s", printflags(NOGetAuthLevel(au), no_userflags));
214 controlreply(np2, "Notice flags: %s", printflags(NOGetNoticeLevel(au), no_noticeflags));
215 }
216
217 if(ircd_strcmp(np->authname, newaccount)) { /* send a message to the person who HELLO'ed if we haven't already been told */
218 controlreply(np, "Account created for auth %s.", au->authname->content);
219 if(NOGetAuthLevel(au))
220 controlreply(np, "User flags: %s", printflags(NOGetAuthLevel(au), no_userflags));
221 controlreply(np, "Notice flags: %s", printflags(NOGetNoticeLevel(au), no_noticeflags));
222 controlreply(np, "Instructions sent to all authed users.");
223 } else if(au->nick && au->nick->next) { /* if we have already been told, tell the user it was sent to more than themselves */
224 controlreply(np, "Instructions sent to all authed users.");
225 }
226
227 controlwall(NO_OPERED, NL_MANAGEMENT, "%s/%s just HELLO'ed: %s", np->nick, np->authname, au->authname->content);
228 return CMD_OK;
229 }
230
231 no_autheduser *noperserv_autheduser_from_command(nick *np, char *command, int *typefound, char **returned) {
232 no_autheduser *au;
233 if(command[0] == '#') {
234 au = noperserv_get_autheduser(command + 1);
235 if(!au) {
236 controlreply(np, "Authname not found.");
237 } else {
238 *typefound = NO_FOUND_AUTHNAME;
239 *returned = au->authname->content;
240 return au;
241 }
242 } else {
243 nick *np2 = getnickbynick(command);
244 if(!np2) {
245 controlreply(np, "Nickname not on the network.");
246 return CMD_OK;
247 }
248 if(!IsAccount(np2)) {
249 controlreply(np, "User is not authed with the network.");
250 return CMD_OK;
251 }
252 au = NOGetAuthedUser(np2);
253 if(!au) {
254 controlreply(np, "User does not have an account.");
255 } else {
256 *typefound = NO_FOUND_NICKNAME;
257 *returned = np2->nick;
258 return au;
259 }
260 }
261
262 return NULL;
263 }
264
265 int noperserv_noticeflags(void *sender, int cargc, char **cargv) {
266 nick *np = (nick *)sender;
267 no_autheduser *au;
268
269 if(cargc == 1) {
270 if((cargv[0][0] == '+') || (cargv[0][0] == '-')) {
271 int ret;
272 au = NOGetAuthedUser(np);
273 flag_t fwas = NOGetNoticeLevel(au), permittedchanges = noperserv_policy_permitted_noticeflags(au);
274
275 ret = setflags(&au->noticelevel, permittedchanges, cargv[0], no_noticeflags, REJECT_DISALLOWED | REJECT_UNKNOWN);
276 if(ret != REJECT_UNKNOWN) {
277 if(ret == REJECT_DISALLOWED) {
278 flag_t fnow = fwas;
279 setflags(&fnow, NL_ALL, cargv[0], no_noticeflags, REJECT_NONE);
280 if(fnow == fwas) {
281 controlreply(np, "No changes made to existing flags.");
282 } else {
283 char ourflags[FLAGBUFLEN], ournoticeflags[FLAGBUFLEN];
284 controlreply(np, "Flag alterations denied.");
285
286 strlcpy(ourflags, printflags(NOGetAuthLevel(au), no_userflags), sizeof(ourflags));
287 strlcpy(ournoticeflags, printflags(NOGetNoticeLevel(au), no_noticeflags), sizeof(ournoticeflags));
288 controlwall(NO_OPER, NL_MANAGEMENT, "%s/%s (%s) attempted to NOTICEFLAGS (%s): %s", np->nick, np->authname, ourflags, ournoticeflags, printflagdiff(fwas, fnow, no_noticeflags));
289 return CMD_ERROR;
290 }
291 } else if(ret == REJECT_NONE) {
292 if(NOGetNoticeLevel(au) == fwas) {
293 controlreply(np, "No changes made to existing flags.");
294 } else {
295 char ourflags[FLAGBUFLEN], ournoticeflags[FLAGBUFLEN], diff[FLAGBUFLEN * 2 + 1], finalflags[FLAGBUFLEN];
296 no_nicklist *nl = au->nick;
297 noperserv_update_autheduser(au);
298 controlreply(np, "Flag alterations complete.");
299
300 strlcpy(ourflags, printflags(NOGetAuthLevel(au), no_userflags), sizeof(ourflags));
301 strlcpy(ournoticeflags, printflags(fwas, no_noticeflags), sizeof(ournoticeflags));
302 strlcpy(diff, printflagdiff(fwas, NOGetNoticeLevel(au), no_noticeflags), sizeof(diff));
303 controlwall(NO_OPER, NL_MANAGEMENT, "%s/%s (%s) successfully used NOTICEFLAGS (%s): %s", np->nick, np->authname, ourflags, ournoticeflags, diff);
304
305 strlcpy(finalflags, printflags(NOGetNoticeLevel(au), no_noticeflags), sizeof(finalflags));
306 for(;nl;nl=nl->next)
307 if(nl->nick != np) {
308 controlreply(nl->nick, "!!! %s just used NOTICEFLAGS (%s): %s", np->nick, ournoticeflags, diff);
309 controlreply(nl->nick, "Your notice flags are %s", finalflags);
310 }
311 }
312 }
313 } else {
314 controlreply(np, "Unknown flag(s) supplied.");
315 return CMD_ERROR;
316 }
317 } else {
318 int typefound;
319 char *itemfound;
320 au = noperserv_autheduser_from_command(np, cargv[0], &typefound, &itemfound);
321 if(!au)
322 return CMD_ERROR;
323
324 if(au != NOGetAuthedUser(np)) {
325 controlreply(np, "Notice flags for %s %s are: %s", typefound==NO_FOUND_NICKNAME?"user":"authname", itemfound, printflags(NOGetNoticeLevel(au), no_noticeflags));
326 return CMD_OK;
327 }
328 }
329 } else {
330 au = NOGetAuthedUser(np);
331 }
332
333 if(!au) /* shouldn't happen */
334 return CMD_ERROR;
335
336 controlreply(np, "Your notice flags are: %s", printflags(NOGetNoticeLevel(au), no_noticeflags));
337
338 return CMD_OK;
339 }
340
341 /* @test */
342 int noperserv_deluser(void *sender, int cargc, char **cargv) {
343 nick *np = (nick *)sender;
344 no_autheduser *target /* target user */, *au = NOGetAuthedUser(np); /* user executing command */
345 char *userreturned = NULL; /* nickname or authname of the target, pulled from the db */
346 int typefound; /* whether it was an authname or a username */
347 no_nicklist *nl;
348 char targetflags[FLAGBUFLEN], ourflags[FLAGBUFLEN], deleteduser[NOMax(ACCOUNTLEN, NICKLEN) + 1];
349
350 if(cargc != 1)
351 return CMD_USAGE;
352
353 target = noperserv_autheduser_from_command(np, cargv[0], &typefound, &userreturned);
354 if(!target)
355 return CMD_ERROR;
356
357 strlcpy(targetflags, printflags(NOGetAuthLevel(target), no_userflags), sizeof(targetflags));
358 strlcpy(ourflags, printflags(NOGetAuthLevel(au), no_userflags), sizeof(ourflags));
359
360 /* we have to copy it as it might point to an autheduser, which we're about to delete */
361 strlcpy(deleteduser, userreturned, sizeof(deleteduser));
362
363 /* we have to check if target != au, because if successful policy_modification_permitted just returns the flags we're allowed
364 to modify, if we have no flags we won't be able to delete ourselves */
365 if((target != au) && !noperserv_policy_permitted_modifications(au, target)) {
366 controlreply(np, "Deletion denied.");
367 controlwall(NO_OPER, NL_MANAGEMENT, "%s/%s (%s) attempted to DELUSER %s (%s)", np->nick, np->authname, ourflags, target->authname->content, targetflags);
368
369 return CMD_ERROR;
370 }
371
372 for(nl=target->nick;nl;nl=nl->next)
373 if(nl->nick != np)
374 controlreply(nl->nick, "!!! %s/%s (%s) just DELUSERed you.", np->nick, np->authname, ourflags);
375
376 noperserv_delete_autheduser(target);
377
378 controlwall(NO_OPER, NL_MANAGEMENT, "%s/%s (%s) successfully used DELUSER on %s (%s)", np->nick, np->authname, ourflags, target->authname->content, targetflags);
379
380 if(target == au) {
381 controlreply(np, "You have been deleted.");
382 } else {
383 controlreply(np, "%s %s deleted.", typefound==NO_FOUND_AUTHNAME?"Auth":"User", deleteduser);
384 }
385
386 return CMD_OK;
387 }
388
389 /* @test */
390 /* this command needs LOTS of checking */
391 int noperserv_userflags(void *sender, int cargc, char **cargv) {
392 nick *np = (nick *)sender;
393 no_autheduser *au = NOGetAuthedUser(np), *target = NULL;
394 char *flags = NULL, *nicktarget = NULL;
395 int typefound;
396
397 if(cargc == 0) {
398 target = au;
399 } else if(cargc == 1) {
400 if((cargv[0][0] == '+') || (cargv[0][0] == '-')) { /* modify our own */
401 flags = cargv[0];
402 target = au;
403 } else { /* viewing someone elses */
404 nicktarget = cargv[0];
405 }
406 } else if(cargc == 2) {
407 nicktarget = cargv[0];
408 flags = cargv[1];
409 } else {
410 return CMD_USAGE;
411 }
412
413 if(nicktarget) {
414 target = noperserv_autheduser_from_command(np, nicktarget, &typefound, &nicktarget);
415 if(!target)
416 return CMD_ERROR;
417 }
418
419 if(flags) {
420 int ret;
421 flag_t permitted = noperserv_policy_permitted_modifications(au, target), fwas = NOGetAuthLevel(target), fours = NOGetAuthLevel(au);
422
423 ret = setflags(&target->authlevel, permitted, flags, no_userflags, REJECT_DISALLOWED | REJECT_UNKNOWN);
424 if(ret != REJECT_UNKNOWN) {
425 if(ret == REJECT_DISALLOWED) {
426 flag_t fnow = fwas;
427 setflags(&fnow, NO_ALL_FLAGS, flags, no_userflags, REJECT_NONE);
428 if(fnow == fwas) {
429 controlreply(np, "No changes made to existing flags.");
430 } else {
431 char targetflags[FLAGBUFLEN], ourflags[FLAGBUFLEN];
432 controlreply(np, "Flag alterations denied.");
433
434 strlcpy(targetflags, printflags(fwas, no_userflags), sizeof(targetflags));
435 strlcpy(ourflags, printflags(fours, no_userflags), sizeof(ourflags));
436
437 controlwall(NO_OPER, NL_MANAGEMENT, "%s/%s (%s) attempted to use USERFLAGS on %s (%s): %s", np->nick, np->authname, ourflags, target->authname->content, targetflags, printflagdiff(fwas, fnow, no_userflags));
438 return CMD_ERROR;
439 }
440 } else if(ret == REJECT_NONE) {
441 if(NOGetAuthLevel(target) == fwas) {
442 controlreply(np, "No changes made to existing flags.");
443 } else {
444 char targetflags[FLAGBUFLEN], ourflags[FLAGBUFLEN], finalflags[FLAGBUFLEN];
445 no_nicklist *nl = target->nick;
446
447 noperserv_policy_update_noticeflags(fwas, target);
448 noperserv_update_autheduser(target);
449
450 controlreply(np, "Flag alterations complete.");
451
452 strlcpy(targetflags, printflags(fwas, no_userflags), sizeof(targetflags));
453 strlcpy(ourflags, printflags(fours, no_userflags), sizeof(ourflags));
454
455 controlwall(NO_OPER, NL_MANAGEMENT, "%s/%s (%s) successfully used USERFLAGS on %s (%s): %s", np->nick, np->authname, ourflags, target->authname->content, targetflags, printflagdiff(fwas, NOGetAuthLevel(target), no_userflags));
456
457 strlcpy(finalflags, printflags(NOGetAuthLevel(target), no_userflags), sizeof(finalflags));
458 for(;nl;nl=nl->next)
459 if(nl->nick != np) {
460 controlreply(nl->nick, "!!! %s/%s (%s) just used USERFLAGS on you (%s): %s", np->nick, np->authname, ourflags, targetflags, printflagdiff(fwas, NOGetAuthLevel(target), no_userflags));
461 controlreply(nl->nick, "Your user flags are now: %s", finalflags);
462 controlreply(nl->nick, "Your notice flags are now: %s", printflags(target->noticelevel, no_noticeflags));
463 }
464 }
465 }
466 } else {
467 controlreply(np, "Unknown flag(s) supplied.");
468 return CMD_ERROR;
469 }
470 }
471
472 if(target != au) {
473 controlreply(np, "User flags for %s %s: %s", typefound==NO_FOUND_AUTHNAME?"auth":"user", nicktarget, printflags(NOGetAuthLevel(target), no_userflags));
474 controlreply(np, "Notice flags for %s %s: %s", typefound==NO_FOUND_AUTHNAME?"auth":"user", nicktarget, printflags(target->noticelevel, no_noticeflags));
475 } else {
476 controlreply(np, "Your user flags are: %s", printflags(NOGetAuthLevel(target), no_userflags));
477 controlreply(np, "Your notice flags are: %s", printflags(target->noticelevel, no_noticeflags));
478 }
479
480 return CMD_OK;
481 }
482
483 void noperserv_oper_detection(int hooknum, void *arg) {
484 void **args = (void **)arg;
485 nick *np = args[0];
486 char *modestr = args[1];
487 flag_t after = np->umodes;
488
489 setflags(&after, UMODE_ALL, modestr, umodeflags, REJECT_NONE);
490 if(np->umodes & UMODE_OPER) {
491 if(!(after & UMODE_OPER))
492 controlwall(NO_OPER, NL_OPERING, "%s!%s@%s%s%s just DEOPERed", np->nick, np->ident, np->host->name->content, IsAccount(np)?"/":"", IsAccount(np)?np->authname:"");
493 } else {
494 if(after & UMODE_OPER)
495 controlwall(NO_OPER, NL_OPERING, "%s!%s@%s%s%s just OPERed", np->nick, np->ident, np->host->name->content, IsAccount(np)?"/":"", IsAccount(np)?np->authname:"");
496 }
497 }