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