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 played automatically (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.");
175 target
= getnickbynick(cargv
[0]);
177 controlreply(np
, "Supplied nickname is not on the network.");
180 if(!IsAccount(target
)) {
181 controlreply(np
, "Supplied user is not authed with the network.");
184 newaccount
= target
->authname
;
187 au
= noperserv_get_autheduser(newaccount
);
189 controlreply(np
, "Authname already registered.");
193 au
= noperserv_new_autheduser(newaccount
);
195 controlreply(np
, "Memory allocation error.");
199 if(noperserv_get_autheduser_count() == 1) {
200 au
->authlevel
= NO_FIRST_USER_LEVEL
;
201 au
->noticelevel
= NO_FIRST_USER_DEFAULT_NOTICELEVEL
;
203 au
->authlevel
= NO_DEFAULT_LEVEL
;
204 au
->noticelevel
= NO_DEFAULT_NOTICELEVEL
;
207 au
->id
= noperserv_next_autheduser_id();
208 noperserv_update_autheduser(au
);
210 for(i
=0;i
<NICKHASHSIZE
;i
++)
211 for(np2
=nicktable
[i
];np2
;np2
=np2
->next
)
212 if(IsAccount(np2
) && !ircd_strcmp(newaccount
, np2
->authname
)) {
213 noperserv_add_to_autheduser(np2
, au
);
214 controlreply(np2
, "An account has been created for you (auth %s).", au
->authname
->content
);
215 if(NOGetAuthLevel(au
))
216 controlreply(np2
, "User flags: %s", printflags(NOGetAuthLevel(au
), no_userflags
));
217 controlreply(np2
, "Notice flags: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
220 if(ircd_strcmp(np
->authname
, newaccount
)) { /* send a message to the person who HELLO'ed if we haven't already been told */
221 controlreply(np
, "Account created for auth %s.", au
->authname
->content
);
222 if(NOGetAuthLevel(au
))
223 controlreply(np
, "User flags: %s", printflags(NOGetAuthLevel(au
), no_userflags
));
224 controlreply(np
, "Notice flags: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
225 controlreply(np
, "Instructions sent to all authed users.");
226 } else if(au
->nick
&& au
->nick
->next
) { /* if we have already been told, tell the user it was sent to more than themselves */
227 controlreply(np
, "Instructions sent to all authed users.");
230 controlwall(NO_OPERED
, NL_MANAGEMENT
, "%s/%s just HELLO'ed: %s", np
->nick
, np
->authname
, au
->authname
->content
);
234 no_autheduser
*noperserv_autheduser_from_command(nick
*np
, char *command
, int *typefound
, char **returned
) {
236 if(command
[0] == '#') {
237 au
= noperserv_get_autheduser(command
+ 1);
239 controlreply(np
, "Authname not found.");
241 *typefound
= NO_FOUND_AUTHNAME
;
242 *returned
= au
->authname
->content
;
246 nick
*np2
= getnickbynick(command
);
248 controlreply(np
, "Nickname not on the network.");
251 if(!IsAccount(np2
)) {
252 controlreply(np
, "User is not authed with the network.");
255 au
= NOGetAuthedUser(np2
);
257 controlreply(np
, "User does not have an account.");
259 *typefound
= NO_FOUND_NICKNAME
;
260 *returned
= np2
->nick
;
268 int noperserv_noticeflags(void *sender
, int cargc
, char **cargv
) {
269 nick
*np
= (nick
*)sender
;
273 if((cargv
[0][0] == '+') || (cargv
[0][0] == '-')) {
275 au
= NOGetAuthedUser(np
);
276 flag_t fwas
= NOGetNoticeLevel(au
), permittedchanges
= noperserv_policy_permitted_noticeflags(au
);
278 ret
= setflags(&au
->noticelevel
, permittedchanges
, cargv
[0], no_noticeflags
, REJECT_DISALLOWED
| REJECT_UNKNOWN
);
279 if(ret
!= REJECT_UNKNOWN
) {
280 if(ret
== REJECT_DISALLOWED
) {
282 setflags(&fnow
, NL_ALL
, cargv
[0], no_noticeflags
, REJECT_NONE
);
284 controlreply(np
, "No changes made to existing flags.");
286 char ourflags
[FLAGBUFLEN
], ournoticeflags
[FLAGBUFLEN
];
287 controlreply(np
, "Flag alterations denied.");
289 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
290 strlcpy(ournoticeflags
, printflags(NOGetNoticeLevel(au
), no_noticeflags
), sizeof(ournoticeflags
));
291 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) attempted to NOTICEFLAGS (%s): %s", np
->nick
, np
->authname
, ourflags
, ournoticeflags
, printflagdiff(fwas
, fnow
, no_noticeflags
));
294 } else if(ret
== REJECT_NONE
) {
295 if(NOGetNoticeLevel(au
) == fwas
) {
296 controlreply(np
, "No changes made to existing flags.");
298 char ourflags
[FLAGBUFLEN
], ournoticeflags
[FLAGBUFLEN
], diff
[FLAGBUFLEN
* 2 + 1], finalflags
[FLAGBUFLEN
];
299 no_nicklist
*nl
= au
->nick
;
300 noperserv_update_autheduser(au
);
301 controlreply(np
, "Flag alterations complete.");
303 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
304 strlcpy(ournoticeflags
, printflags(fwas
, no_noticeflags
), sizeof(ournoticeflags
));
305 strlcpy(diff
, printflagdiff(fwas
, NOGetNoticeLevel(au
), no_noticeflags
), sizeof(diff
));
306 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) successfully used NOTICEFLAGS (%s): %s", np
->nick
, np
->authname
, ourflags
, ournoticeflags
, diff
);
308 strlcpy(finalflags
, printflags(NOGetNoticeLevel(au
), no_noticeflags
), sizeof(finalflags
));
311 controlreply(nl
->nick
, "!!! %s just used NOTICEFLAGS (%s): %s", np
->nick
, ournoticeflags
, diff
);
312 controlreply(nl
->nick
, "Your notice flags are %s", finalflags
);
317 controlreply(np
, "Unknown flag(s) supplied.");
323 au
= noperserv_autheduser_from_command(np
, cargv
[0], &typefound
, &itemfound
);
327 if(au
!= NOGetAuthedUser(np
)) {
328 controlreply(np
, "Notice flags for %s %s are: %s", typefound
==NO_FOUND_NICKNAME
?"user":"authname", itemfound
, printflags(NOGetNoticeLevel(au
), no_noticeflags
));
333 au
= NOGetAuthedUser(np
);
336 if(!au
) /* shouldn't happen */
339 controlreply(np
, "Your notice flags are: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
345 int noperserv_deluser(void *sender
, int cargc
, char **cargv
) {
346 nick
*np
= (nick
*)sender
;
347 no_autheduser
*target
/* target user */, *au
= NOGetAuthedUser(np
); /* user executing command */
348 char *userreturned
= NULL
; /* nickname or authname of the target, pulled from the db */
349 int typefound
; /* whether it was an authname or a username */
351 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
], deleteduser
[NOMax(ACCOUNTLEN
, NICKLEN
) + 1];
356 target
= noperserv_autheduser_from_command(np
, cargv
[0], &typefound
, &userreturned
);
360 strlcpy(targetflags
, printflags(NOGetAuthLevel(target
), no_userflags
), sizeof(targetflags
));
361 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
363 /* we have to copy it as it might point to an autheduser, which we're about to delete */
364 strlcpy(deleteduser
, userreturned
, sizeof(deleteduser
));
366 /* we have to check if target != au, because if successful policy_modification_permitted just returns the flags we're allowed
367 to modify, if we have no flags we won't be able to delete ourselves */
368 if((target
!= au
) && !noperserv_policy_permitted_modifications(au
, target
)) {
369 controlreply(np
, "Deletion denied.");
370 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) attempted to DELUSER %s (%s)", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
);
375 for(nl
=target
->nick
;nl
;nl
=nl
->next
)
377 controlreply(nl
->nick
, "!!! %s/%s (%s) just DELUSERed you.", np
->nick
, np
->authname
, ourflags
);
379 noperserv_delete_autheduser(target
);
381 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) successfully used DELUSER on %s (%s)", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
);
384 controlreply(np
, "You have been deleted.");
386 controlreply(np
, "%s %s deleted.", typefound
==NO_FOUND_AUTHNAME
?"Auth":"User", deleteduser
);
393 /* this command needs LOTS of checking */
394 int noperserv_userflags(void *sender
, int cargc
, char **cargv
) {
395 nick
*np
= (nick
*)sender
;
396 no_autheduser
*au
= NOGetAuthedUser(np
), *target
= NULL
;
397 char *flags
= NULL
, *nicktarget
= NULL
;
402 } else if(cargc
== 1) {
403 if((cargv
[0][0] == '+') || (cargv
[0][0] == '-')) { /* modify our own */
406 } else { /* viewing someone elses */
407 nicktarget
= cargv
[0];
409 } else if(cargc
== 2) {
410 nicktarget
= cargv
[0];
417 target
= noperserv_autheduser_from_command(np
, nicktarget
, &typefound
, &nicktarget
);
424 flag_t permitted
= noperserv_policy_permitted_modifications(au
, target
), fwas
= NOGetAuthLevel(target
), fours
= NOGetAuthLevel(au
);
426 ret
= setflags(&target
->authlevel
, permitted
, flags
, no_userflags
, REJECT_DISALLOWED
| REJECT_UNKNOWN
);
427 if(ret
!= REJECT_UNKNOWN
) {
428 if(ret
== REJECT_DISALLOWED
) {
430 setflags(&fnow
, NO_ALL_FLAGS
, flags
, no_userflags
, REJECT_NONE
);
432 controlreply(np
, "No changes made to existing flags.");
434 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
];
435 controlreply(np
, "Flag alterations denied.");
437 strlcpy(targetflags
, printflags(fwas
, no_userflags
), sizeof(targetflags
));
438 strlcpy(ourflags
, printflags(fours
, no_userflags
), sizeof(ourflags
));
440 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
));
443 } else if(ret
== REJECT_NONE
) {
444 if(NOGetAuthLevel(target
) == fwas
) {
445 controlreply(np
, "No changes made to existing flags.");
447 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
], finalflags
[FLAGBUFLEN
];
448 no_nicklist
*nl
= target
->nick
;
450 noperserv_policy_update_noticeflags(fwas
, target
);
451 noperserv_update_autheduser(target
);
453 controlreply(np
, "Flag alterations complete.");
455 strlcpy(targetflags
, printflags(fwas
, no_userflags
), sizeof(targetflags
));
456 strlcpy(ourflags
, printflags(fours
, no_userflags
), sizeof(ourflags
));
458 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
));
460 strlcpy(finalflags
, printflags(NOGetAuthLevel(target
), no_userflags
), sizeof(finalflags
));
463 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
));
464 controlreply(nl
->nick
, "Your user flags are now: %s", finalflags
);
465 controlreply(nl
->nick
, "Your notice flags are now: %s", printflags(target
->noticelevel
, no_noticeflags
));
470 controlreply(np
, "Unknown flag(s) supplied.");
476 controlreply(np
, "User flags for %s %s: %s", typefound
==NO_FOUND_AUTHNAME
?"auth":"user", nicktarget
, printflags(NOGetAuthLevel(target
), no_userflags
));
477 controlreply(np
, "Notice flags for %s %s: %s", typefound
==NO_FOUND_AUTHNAME
?"auth":"user", nicktarget
, printflags(target
->noticelevel
, no_noticeflags
));
479 controlreply(np
, "Your user flags are: %s", printflags(NOGetAuthLevel(target
), no_userflags
));
480 controlreply(np
, "Your notice flags are: %s", printflags(target
->noticelevel
, no_noticeflags
));
486 void noperserv_oper_detection(int hooknum
, void *arg
) {
487 nick
*np
= (nick
*)arg
;
489 if(np
->umodes
& UMODE_OPER
) {
490 if(np
->opername
&& strcmp(np
->opername
->content
, "-")) {
491 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
);
493 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 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
:"");