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 "../authext/authext.h"
14 #include "noperserv.h"
15 #include "noperserv_db.h"
16 #include "noperserv_hooks.h"
17 #include "noperserv_policy.h"
25 #define FLAGBUFLEN 100
27 #define NO_FOUND_NICKNAME 1
28 #define NO_FOUND_AUTHNAME 2
30 const flag no_commandflags
[] = {
35 { 'd', __NO_DEVELOPER
},
39 { 'R', __NO_ACCOUNT
},
44 const flag no_userflags
[] = {
49 { 'd', __NO_DEVELOPER
},
54 const flag no_noticeflags
[] = {
55 { 'm', NL_MANAGEMENT
}, /* hello, password, userflags, noticeflags */
56 { 't', NL_TRUSTS
}, /* trust stuff... */
57 { 'k', NL_KICKKILLS
}, /* KICK/KILL commands */
58 { 'I', NL_MISC
}, /* misc commands */
59 { 'g', NL_GLINES
}, /* GLINE commands */
60 { 'h', NL_HITS
}, /* Where a gline or kill is set automatically by the bot */
61 { 'c', NL_CLONING
}, /* Clone detection */
62 { 'C', NL_CLEARCHAN
}, /* When someone clearchans */
63 { 'f', NL_FAKEUSERS
}, /* Fakeuser addition */
64 { 'b', NL_BROADCASTS
}, /* Broadcast/mbroadcast/sbroadcast */
65 { 'o', NL_OPERATIONS
}, /* insmod/rmmod/etc */
66 { 'O', NL_OPERING
}, /* when someone opers */
67 { 'n', NL_NOTICES
}, /* turn off to receive notices instead of privmsgs */
68 { 'A', NL_ALL_COMMANDS
}, /* all commands sent */
72 int noperserv_hello(void *sender
, int cargc
, char **cargv
);
73 int noperserv_noticeflags(void *sender
, int cargc
, char **cargv
);
74 int noperserv_userflags(void *sender
, int cargc
, char **cargv
);
75 int noperserv_deluser(void *sender
, int cargc
, char **cargv
);
76 void noperserv_oper_detection(int hooknum
, void *arg
);
77 void noperserv_reply(nick
*np
, char *format
, ...) __attribute__ ((format (printf
, 2, 3)));
82 if(!noperserv_load_db())
85 noperserv_ext
= registernickext("noperserv");
87 noperserv_setup_hooks();
89 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.");
90 registercontrolhelpcmd("userflags", NO_ACCOUNT
, 2, &noperserv_userflags
,
91 "Syntax: USERFLAGS <nickname|#authname> ?modifications?\n"
92 " Views and modifies user permissions.\n"
93 " If no nickname or authname is supplied, you are substituted for it.\n"
94 " If no flags are supplied, flags are just displayed instead of modified."
98 " +S: Security team member\n"
99 " +d: NOperserv developer\n"
100 " +t: Trust queue worker\n"
102 " Additional flags may show up in SHOWCOMMANDS but are not userflags as such:\n"
104 " +R: Registered NOperserv user\n"
105 " +O: Must be /OPER'ed\n"
106 " +L: Legacy command\n"
108 registercontrolhelpcmd("noticeflags", NO_ACCOUNT
, 1, &noperserv_noticeflags
,
109 "Syntax: NOTICEFLAGS ?(nickname|#authname)|flags?\n"
110 " This command can view and modify your own notice flags, and view that of other users.\n"
112 " +m: Management (hello, password, userflags, noticeflags)\n"
114 " +k: KICK/KILL commands\n"
115 " +g: GLINE commands\n"
116 " +h: Shows when glines are set by code (hits)\n"
117 " +c: Clone information\n"
118 " +C: CLEARCHAN command\n"
119 " +f: FAKEUSER commands\n"
120 " +b: BROADCAST commands\n"
121 " +o: Operation commands, such as insmod, rmmod, die, etc\n"
123 " +I: Misc commands (resync, etc)\n"
124 " +n: Sends notices instead of privmsgs\n"
125 " +A: Every single command sent to the service (spammy)\n"
128 registercontrolhelpcmd("deluser", NO_OPERED
| NO_ACCOUNT
, 2, &noperserv_deluser
, "Syntax: DELUSER <nickname|#authname>\nDeletes the specified user.");
129 registerhook(HOOK_NICK_MODEOPER
, &noperserv_oper_detection
);
134 #ifdef BROKEN_DLCLOSE
142 deregisterhook(HOOK_NICK_MODEOPER
, &noperserv_oper_detection
);
144 deregistercontrolcmd("noticeflags", &noperserv_noticeflags
);
145 deregistercontrolcmd("userflags", &noperserv_userflags
);
146 deregistercontrolcmd("noticeflags", &noperserv_noticeflags
);
147 deregistercontrolcmd("hello", &noperserv_hello
);
148 deregistercontrolcmd("deluser", &noperserv_deluser
);
150 noperserv_cleanup_hooks();
152 noperserv_cleanup_db();
154 releasenickext(noperserv_ext
);
158 int noperserv_hello(void *sender
, int cargc
, char **cargv
) {
159 char *newaccount
= NULL
;
162 nick
*np
= (nick
*)sender
, *np2
, *target
= NULL
;
165 newaccount
= np
->authname
;
167 if(cargv
[0][0] == '#') {
168 authname
*a
= getauthbyname(cargv
[0] + 1);
170 controlreply(np
, "Cannot find anyone with that authname on the network.");
173 newaccount
= a
->name
;
176 target
= getnickbynick(cargv
[0]);
178 controlreply(np
, "Supplied nickname is not on the network.");
181 if(!IsAccount(target
)) {
182 controlreply(np
, "Supplied user is not authed with the network.");
185 newaccount
= target
->authname
;
188 au
= noperserv_get_autheduser(newaccount
);
190 controlreply(np
, "Authname already registered.");
194 au
= noperserv_new_autheduser(newaccount
);
196 controlreply(np
, "Memory allocation error.");
200 if(noperserv_get_autheduser_count() == 1) {
201 au
->authlevel
= NO_FIRST_USER_LEVEL
;
202 au
->noticelevel
= NO_FIRST_USER_DEFAULT_NOTICELEVEL
;
204 au
->authlevel
= NO_DEFAULT_LEVEL
;
205 au
->noticelevel
= NO_DEFAULT_NOTICELEVEL
;
208 au
->id
= noperserv_next_autheduser_id();
209 noperserv_update_autheduser(au
);
211 for(i
=0;i
<NICKHASHSIZE
;i
++)
212 for(np2
=nicktable
[i
];np2
;np2
=np2
->next
)
213 if(IsAccount(np2
) && !ircd_strcmp(newaccount
, np2
->authname
)) {
214 noperserv_add_to_autheduser(np2
, au
);
215 controlreply(np2
, "An account has been created for you (auth %s).", au
->authname
->content
);
216 if(NOGetAuthLevel(au
))
217 controlreply(np2
, "User flags: %s", printflags(NOGetAuthLevel(au
), no_userflags
));
218 controlreply(np2
, "Notice flags: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
221 if(ircd_strcmp(np
->authname
, newaccount
)) { /* send a message to the person who HELLO'ed if we haven't already been told */
222 controlreply(np
, "Account created for auth %s.", au
->authname
->content
);
223 if(NOGetAuthLevel(au
))
224 controlreply(np
, "User flags: %s", printflags(NOGetAuthLevel(au
), no_userflags
));
225 controlreply(np
, "Notice flags: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
226 controlreply(np
, "Instructions sent to all authed users.");
227 } else if(au
->nick
&& au
->nick
->next
) { /* if we have already been told, tell the user it was sent to more than themselves */
228 controlreply(np
, "Instructions sent to all authed users.");
231 controlwall(NO_OPERED
, NL_MANAGEMENT
, "%s/%s just HELLO'ed: %s", np
->nick
, np
->authname
, au
->authname
->content
);
235 no_autheduser
*noperserv_autheduser_from_command(nick
*np
, char *command
, int *typefound
, char **returned
) {
237 if(command
[0] == '#') {
238 au
= noperserv_get_autheduser(command
+ 1);
240 controlreply(np
, "Authname not found.");
242 *typefound
= NO_FOUND_AUTHNAME
;
243 *returned
= au
->authname
->content
;
247 nick
*np2
= getnickbynick(command
);
249 controlreply(np
, "Nickname not on the network.");
252 if(!IsAccount(np2
)) {
253 controlreply(np
, "User is not authed with the network.");
256 au
= NOGetAuthedUser(np2
);
258 controlreply(np
, "User does not have an account.");
260 *typefound
= NO_FOUND_NICKNAME
;
261 *returned
= np2
->nick
;
269 int noperserv_noticeflags(void *sender
, int cargc
, char **cargv
) {
270 nick
*np
= (nick
*)sender
;
274 if((cargv
[0][0] == '+') || (cargv
[0][0] == '-')) {
276 au
= NOGetAuthedUser(np
);
277 flag_t fwas
= NOGetNoticeLevel(au
), permittedchanges
= noperserv_policy_permitted_noticeflags(au
);
279 ret
= setflags(&au
->noticelevel
, permittedchanges
, cargv
[0], no_noticeflags
, REJECT_DISALLOWED
| REJECT_UNKNOWN
);
280 if(ret
!= REJECT_UNKNOWN
) {
281 if(ret
== REJECT_DISALLOWED
) {
283 setflags(&fnow
, NL_ALL
, cargv
[0], no_noticeflags
, REJECT_NONE
);
285 controlreply(np
, "No changes made to existing flags.");
287 char ourflags
[FLAGBUFLEN
], ournoticeflags
[FLAGBUFLEN
];
288 controlreply(np
, "Flag alterations denied.");
290 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
291 strlcpy(ournoticeflags
, printflags(NOGetNoticeLevel(au
), no_noticeflags
), sizeof(ournoticeflags
));
292 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) attempted to NOTICEFLAGS (%s): %s", np
->nick
, np
->authname
, ourflags
, ournoticeflags
, printflagdiff(fwas
, fnow
, no_noticeflags
));
295 } else if(ret
== REJECT_NONE
) {
296 if(NOGetNoticeLevel(au
) == fwas
) {
297 controlreply(np
, "No changes made to existing flags.");
299 char ourflags
[FLAGBUFLEN
], ournoticeflags
[FLAGBUFLEN
], diff
[FLAGBUFLEN
* 2 + 1], finalflags
[FLAGBUFLEN
];
300 no_nicklist
*nl
= au
->nick
;
301 noperserv_update_autheduser(au
);
302 controlreply(np
, "Flag alterations complete.");
304 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
305 strlcpy(ournoticeflags
, printflags(fwas
, no_noticeflags
), sizeof(ournoticeflags
));
306 strlcpy(diff
, printflagdiff(fwas
, NOGetNoticeLevel(au
), no_noticeflags
), sizeof(diff
));
307 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) successfully used NOTICEFLAGS (%s): %s", np
->nick
, np
->authname
, ourflags
, ournoticeflags
, diff
);
309 strlcpy(finalflags
, printflags(NOGetNoticeLevel(au
), no_noticeflags
), sizeof(finalflags
));
312 controlreply(nl
->nick
, "!!! %s just used NOTICEFLAGS (%s): %s", np
->nick
, ournoticeflags
, diff
);
313 controlreply(nl
->nick
, "Your notice flags are %s", finalflags
);
318 controlreply(np
, "Unknown flag(s) supplied.");
324 au
= noperserv_autheduser_from_command(np
, cargv
[0], &typefound
, &itemfound
);
328 if(au
!= NOGetAuthedUser(np
)) {
329 controlreply(np
, "Notice flags for %s %s are: %s", typefound
==NO_FOUND_NICKNAME
?"user":"authname", itemfound
, printflags(NOGetNoticeLevel(au
), no_noticeflags
));
334 au
= NOGetAuthedUser(np
);
337 if(!au
) /* shouldn't happen */
340 controlreply(np
, "Your notice flags are: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
346 int noperserv_deluser(void *sender
, int cargc
, char **cargv
) {
347 nick
*np
= (nick
*)sender
;
348 no_autheduser
*target
/* target user */, *au
= NOGetAuthedUser(np
); /* user executing command */
349 char *userreturned
= NULL
; /* nickname or authname of the target, pulled from the db */
350 int typefound
; /* whether it was an authname or a username */
352 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
], deleteduser
[NOMax(ACCOUNTLEN
, NICKLEN
) + 1];
357 target
= noperserv_autheduser_from_command(np
, cargv
[0], &typefound
, &userreturned
);
361 strlcpy(targetflags
, printflags(NOGetAuthLevel(target
), no_userflags
), sizeof(targetflags
));
362 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
364 /* we have to copy it as it might point to an autheduser, which we're about to delete */
365 strlcpy(deleteduser
, userreturned
, sizeof(deleteduser
));
367 /* we have to check if target != au, because if successful policy_modification_permitted just returns the flags we're allowed
368 to modify, if we have no flags we won't be able to delete ourselves */
369 if((target
!= au
) && !noperserv_policy_permitted_modifications(au
, target
)) {
370 controlreply(np
, "Deletion denied.");
371 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) attempted to DELUSER %s (%s)", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
);
376 for(nl
=target
->nick
;nl
;nl
=nl
->next
)
378 controlreply(nl
->nick
, "!!! %s/%s (%s) just DELUSERed you.", np
->nick
, np
->authname
, ourflags
);
380 noperserv_delete_autheduser(target
);
382 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) successfully used DELUSER on %s (%s)", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
);
385 controlreply(np
, "You have been deleted.");
387 controlreply(np
, "%s %s deleted.", typefound
==NO_FOUND_AUTHNAME
?"Auth":"User", deleteduser
);
394 /* this command needs LOTS of checking */
395 int noperserv_userflags(void *sender
, int cargc
, char **cargv
) {
396 nick
*np
= (nick
*)sender
;
397 no_autheduser
*au
= NOGetAuthedUser(np
), *target
= NULL
;
398 char *flags
= NULL
, *nicktarget
= NULL
;
403 } else if(cargc
== 1) {
404 if((cargv
[0][0] == '+') || (cargv
[0][0] == '-')) { /* modify our own */
407 } else { /* viewing someone elses */
408 nicktarget
= cargv
[0];
410 } else if(cargc
== 2) {
411 nicktarget
= cargv
[0];
418 target
= noperserv_autheduser_from_command(np
, nicktarget
, &typefound
, &nicktarget
);
425 flag_t permitted
= noperserv_policy_permitted_modifications(au
, target
), fwas
= NOGetAuthLevel(target
), fours
= NOGetAuthLevel(au
);
427 ret
= setflags(&target
->authlevel
, permitted
, flags
, no_userflags
, REJECT_DISALLOWED
| REJECT_UNKNOWN
);
428 if(ret
!= REJECT_UNKNOWN
) {
429 if(ret
== REJECT_DISALLOWED
) {
431 setflags(&fnow
, NO_ALL_FLAGS
, flags
, no_userflags
, REJECT_NONE
);
433 controlreply(np
, "No changes made to existing flags.");
435 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
];
436 controlreply(np
, "Flag alterations denied.");
438 strlcpy(targetflags
, printflags(fwas
, no_userflags
), sizeof(targetflags
));
439 strlcpy(ourflags
, printflags(fours
, no_userflags
), sizeof(ourflags
));
441 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
));
444 } else if(ret
== REJECT_NONE
) {
445 if(NOGetAuthLevel(target
) == fwas
) {
446 controlreply(np
, "No changes made to existing flags.");
448 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
], finalflags
[FLAGBUFLEN
];
449 no_nicklist
*nl
= target
->nick
;
451 noperserv_policy_update_noticeflags(fwas
, target
);
452 noperserv_update_autheduser(target
);
454 controlreply(np
, "Flag alterations complete.");
456 strlcpy(targetflags
, printflags(fwas
, no_userflags
), sizeof(targetflags
));
457 strlcpy(ourflags
, printflags(fours
, no_userflags
), sizeof(ourflags
));
459 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
));
461 strlcpy(finalflags
, printflags(NOGetAuthLevel(target
), no_userflags
), sizeof(finalflags
));
464 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
));
465 controlreply(nl
->nick
, "Your user flags are now: %s", finalflags
);
466 controlreply(nl
->nick
, "Your notice flags are now: %s", printflags(target
->noticelevel
, no_noticeflags
));
471 controlreply(np
, "Unknown flag(s) supplied.");
477 controlreply(np
, "User flags for %s %s: %s", typefound
==NO_FOUND_AUTHNAME
?"auth":"user", nicktarget
, printflags(NOGetAuthLevel(target
), no_userflags
));
478 controlreply(np
, "Notice flags for %s %s: %s", typefound
==NO_FOUND_AUTHNAME
?"auth":"user", nicktarget
, printflags(target
->noticelevel
, no_noticeflags
));
480 controlreply(np
, "Your user flags are: %s", printflags(NOGetAuthLevel(target
), no_userflags
));
481 controlreply(np
, "Your notice flags are: %s", printflags(target
->noticelevel
, no_noticeflags
));
487 void noperserv_oper_detection(int hooknum
, void *arg
) {
488 nick
*np
= (nick
*)arg
;
490 if(np
->umodes
& UMODE_OPER
) {
491 if(np
->opername
&& strcmp(np
->opername
->content
, "-")) {
492 controlwall(NO_OPER
, NL_OPERING
, "%s!%s@%s%s%s just OPERed as %s", np
->nick
, np
->ident
, np
->host
->name
->content
, IsAccount(np
)?"/":"", IsAccount(np
)?np
->authname
:"", np
->opername
->content
);
494 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
:"");
497 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
:"");