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