4 * A replacement for Germania's ageing Operservice2
6 * Copyright (C) 2005 Chris Porter.
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"
24 #define FLAGBUFLEN 100
26 #define NO_FOUND_NICKNAME 1
27 #define NO_FOUND_AUTHNAME 2
29 const flag no_commandflags
[] = {
34 { 'd', __NO_DEVELOPER
},
38 { 'R', __NO_ACCOUNT
},
43 const flag no_userflags
[] = {
48 { 'd', __NO_DEVELOPER
},
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 */
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
, ...);
81 if(!noperserv_load_db())
84 noperserv_ext
= registernickext("noperserv");
86 noperserv_setup_hooks();
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."
97 " +S: Security team member\n"
98 " +d: NOperserv developer\n"
99 " +t: Trust queue worker\n"
101 " Additional flags may show up in SHOWCOMMANDS but are not userflags as such:\n"
103 " +R: Registered NOperserv user\n"
104 " +O: Must be /OPER'ed\n"
105 " +L: Legacy command\n"
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"
111 " +m: Management (hello, password, userflags, noticeflags)\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"
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"
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
);
133 #ifdef BROKEN_DLCLOSE
141 deregisterhook(HOOK_NICK_MODEOPER
, &noperserv_oper_detection
);
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
);
149 noperserv_cleanup_hooks();
151 noperserv_cleanup_db();
153 releasenickext(noperserv_ext
);
157 int noperserv_hello(void *sender
, int cargc
, char **cargv
) {
158 char *newaccount
= NULL
;
161 nick
*np
= (nick
*)sender
, *np2
, *target
= NULL
;
164 newaccount
= np
->authname
;
166 if(cargv
[0][0] == '#') {
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
)) {
172 newaccount
= target
->authname
;
176 controlreply(np
, "Cannot find anyone with that authname on the network.");
180 target
= getnickbynick(cargv
[0]);
182 controlreply(np
, "Supplied nickname is not on the network.");
185 if(!IsAccount(target
)) {
186 controlreply(np
, "Supplied user is not authed with the network.");
189 newaccount
= target
->authname
;
192 au
= noperserv_get_autheduser(newaccount
);
194 controlreply(np
, "Authname already registered.");
198 au
= noperserv_new_autheduser(newaccount
);
200 controlreply(np
, "Memory allocation error.");
204 if(noperserv_get_autheduser_count() == 1) {
205 au
->authlevel
= NO_FIRST_USER_LEVEL
;
206 au
->noticelevel
= NO_FIRST_USER_DEFAULT_NOTICELEVEL
;
208 au
->authlevel
= NO_DEFAULT_LEVEL
;
209 au
->noticelevel
= NO_DEFAULT_NOTICELEVEL
;
212 au
->id
= noperserv_next_autheduser_id();
213 noperserv_update_autheduser(au
);
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
));
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.");
235 controlwall(NO_OPERED
, NL_MANAGEMENT
, "%s/%s just HELLO'ed: %s", np
->nick
, np
->authname
, au
->authname
->content
);
239 no_autheduser
*noperserv_autheduser_from_command(nick
*np
, char *command
, int *typefound
, char **returned
) {
241 if(command
[0] == '#') {
242 au
= noperserv_get_autheduser(command
+ 1);
244 controlreply(np
, "Authname not found.");
246 *typefound
= NO_FOUND_AUTHNAME
;
247 *returned
= au
->authname
->content
;
251 nick
*np2
= getnickbynick(command
);
253 controlreply(np
, "Nickname not on the network.");
256 if(!IsAccount(np2
)) {
257 controlreply(np
, "User is not authed with the network.");
260 au
= NOGetAuthedUser(np2
);
262 controlreply(np
, "User does not have an account.");
264 *typefound
= NO_FOUND_NICKNAME
;
265 *returned
= np2
->nick
;
273 int noperserv_noticeflags(void *sender
, int cargc
, char **cargv
) {
274 nick
*np
= (nick
*)sender
;
278 if((cargv
[0][0] == '+') || (cargv
[0][0] == '-')) {
280 au
= NOGetAuthedUser(np
);
281 flag_t fwas
= NOGetNoticeLevel(au
), permittedchanges
= noperserv_policy_permitted_noticeflags(au
);
283 ret
= setflags(&au
->noticelevel
, permittedchanges
, cargv
[0], no_noticeflags
, REJECT_DISALLOWED
| REJECT_UNKNOWN
);
284 if(ret
!= REJECT_UNKNOWN
) {
285 if(ret
== REJECT_DISALLOWED
) {
287 setflags(&fnow
, NL_ALL
, cargv
[0], no_noticeflags
, REJECT_NONE
);
289 controlreply(np
, "No changes made to existing flags.");
291 char ourflags
[FLAGBUFLEN
], ournoticeflags
[FLAGBUFLEN
];
292 controlreply(np
, "Flag alterations denied.");
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
));
299 } else if(ret
== REJECT_NONE
) {
300 if(NOGetNoticeLevel(au
) == fwas
) {
301 controlreply(np
, "No changes made to existing flags.");
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.");
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
);
313 strlcpy(finalflags
, printflags(NOGetNoticeLevel(au
), no_noticeflags
), sizeof(finalflags
));
316 controlreply(nl
->nick
, "!!! %s just used NOTICEFLAGS (%s): %s", np
->nick
, ournoticeflags
, diff
);
317 controlreply(nl
->nick
, "Your notice flags are %s", finalflags
);
322 controlreply(np
, "Unknown flag(s) supplied.");
328 au
= noperserv_autheduser_from_command(np
, cargv
[0], &typefound
, &itemfound
);
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
));
338 au
= NOGetAuthedUser(np
);
341 if(!au
) /* shouldn't happen */
344 controlreply(np
, "Your notice flags are: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
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 */
356 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
], deleteduser
[NOMax(ACCOUNTLEN
, NICKLEN
) + 1];
361 target
= noperserv_autheduser_from_command(np
, cargv
[0], &typefound
, &userreturned
);
365 strlcpy(targetflags
, printflags(NOGetAuthLevel(target
), no_userflags
), sizeof(targetflags
));
366 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
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
));
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
);
380 for(nl
=target
->nick
;nl
;nl
=nl
->next
)
382 controlreply(nl
->nick
, "!!! %s/%s (%s) just DELUSERed you.", np
->nick
, np
->authname
, ourflags
);
384 noperserv_delete_autheduser(target
);
386 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) successfully used DELUSER on %s (%s)", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
);
389 controlreply(np
, "You have been deleted.");
391 controlreply(np
, "%s %s deleted.", typefound
==NO_FOUND_AUTHNAME
?"Auth":"User", deleteduser
);
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
;
407 } else if(cargc
== 1) {
408 if((cargv
[0][0] == '+') || (cargv
[0][0] == '-')) { /* modify our own */
411 } else { /* viewing someone elses */
412 nicktarget
= cargv
[0];
414 } else if(cargc
== 2) {
415 nicktarget
= cargv
[0];
422 target
= noperserv_autheduser_from_command(np
, nicktarget
, &typefound
, &nicktarget
);
429 flag_t permitted
= noperserv_policy_permitted_modifications(au
, target
), fwas
= NOGetAuthLevel(target
), fours
= NOGetAuthLevel(au
);
431 ret
= setflags(&target
->authlevel
, permitted
, flags
, no_userflags
, REJECT_DISALLOWED
| REJECT_UNKNOWN
);
432 if(ret
!= REJECT_UNKNOWN
) {
433 if(ret
== REJECT_DISALLOWED
) {
435 setflags(&fnow
, NO_ALL_FLAGS
, flags
, no_userflags
, REJECT_NONE
);
437 controlreply(np
, "No changes made to existing flags.");
439 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
];
440 controlreply(np
, "Flag alterations denied.");
442 strlcpy(targetflags
, printflags(fwas
, no_userflags
), sizeof(targetflags
));
443 strlcpy(ourflags
, printflags(fours
, no_userflags
), sizeof(ourflags
));
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
));
448 } else if(ret
== REJECT_NONE
) {
449 if(NOGetAuthLevel(target
) == fwas
) {
450 controlreply(np
, "No changes made to existing flags.");
452 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
], finalflags
[FLAGBUFLEN
];
453 no_nicklist
*nl
= target
->nick
;
455 noperserv_policy_update_noticeflags(fwas
, target
);
456 noperserv_update_autheduser(target
);
458 controlreply(np
, "Flag alterations complete.");
460 strlcpy(targetflags
, printflags(fwas
, no_userflags
), sizeof(targetflags
));
461 strlcpy(ourflags
, printflags(fours
, no_userflags
), sizeof(ourflags
));
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
));
465 strlcpy(finalflags
, printflags(NOGetAuthLevel(target
), no_userflags
), sizeof(finalflags
));
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
));
475 controlreply(np
, "Unknown flag(s) supplied.");
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
));
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
));
491 void noperserv_oper_detection(int hooknum
, void *arg
) {
492 void **args
= (void **)arg
;
494 char *modestr
= args
[1];
495 flag_t after
= np
->umodes
;
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
:"");
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
:"");