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 "noperserv.h"
13 #include "noperserv_db.h"
14 #include "noperserv_hooks.h"
15 #include "noperserv_policy.h"
21 #define FLAGBUFLEN 100
23 #define NO_FOUND_NICKNAME 1
24 #define NO_FOUND_AUTHNAME 2
26 const flag no_commandflags
[] = {
31 { 'd', __NO_DEVELOPER
},
35 { 'R', __NO_ACCOUNT
},
39 const flag no_userflags
[] = {
44 { 'd', __NO_DEVELOPER
},
48 const flag no_noticeflags
[] = {
49 { 'm', NL_MANAGEMENT
}, /* hello, password, userflags, noticeflags */
50 { 't', NL_TRUSTS
}, /* trust stuff... */
51 { 'k', NL_KICKKILLS
}, /* KICK/KILL commands */
52 { 'I', NL_MISC
}, /* misc commands */
53 { 'g', NL_GLINES
}, /* GLINE commands */
54 { 'h', NL_HITS
}, /* Where a gline or kill is set automatically by the bot */
55 { 'c', NL_CLONING
}, /* Clone detection */
56 { 'C', NL_CLEARCHAN
}, /* When someone clearchans */
57 { 'f', NL_FAKEUSERS
}, /* Fakeuser addition */
58 { 'b', NL_BROADCASTS
}, /* Broadcast/mbroadcast/sbroadcast */
59 { 'o', NL_OPERATIONS
}, /* insmod/rmmod/etc */
60 { 'O', NL_OPERING
}, /* when someone opers */
61 { 'n', NL_NOTICES
}, /* turn off to receive notices instead of privmsgs */
62 { 'A', NL_ALL_COMMANDS
}, /* all commands sent */
66 int noperserv_hello(void *sender
, int cargc
, char **cargv
);
67 int noperserv_noticeflags(void *sender
, int cargc
, char **cargv
);
68 int noperserv_userflags(void *sender
, int cargc
, char **cargv
);
69 int noperserv_deluser(void *sender
, int cargc
, char **cargv
);
70 void noperserv_oper_detection(int hooknum
, void *arg
);
71 void noperserv_reply(nick
*np
, char *format
, ...);
76 if(!noperserv_load_db())
79 noperserv_ext
= registernickext("noperserv");
81 noperserv_setup_hooks();
83 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.");
84 registercontrolhelpcmd("userflags", NO_ACCOUNT
, 2, &noperserv_userflags
,
85 "Syntax: USERFLAGS <nickname|#authname> ?modifications?\n"
86 " Views and modifies user permissions.\n"
87 " If no nickname or authname is supplied, you are substituted for it.\n"
88 " If no flags are supplied, flags are just displayed instead of modified."
92 " +S: Security team member\n"
93 " +d: NOperserv developer\n"
94 " +t: Trust queue worker\n"
95 " Additional flags may show up in SHOWCOMMANDS but are not userflags as such:\n"
97 " +R: Registered NOperserv user\n"
98 " +O: Must be /OPER'ed\n"
99 " +L: Legacy command\n"
101 registercontrolhelpcmd("noticeflags", NO_ACCOUNT
, 1, &noperserv_noticeflags
,
102 "Syntax: NOTICEFLAGS ?(nickname|#authname)|flags?\n"
103 " This command can view and modify your own notice flags, and view that of other users.\n"
105 " +m: Management (hello, password, userflags, noticeflags)\n"
107 " +k: KICK/KILL commands\n"
108 " +g: GLINE commands\n"
109 " +h: Shows when glines are played automatically (hits)\n"
110 " +c: Clone information\n"
111 " +C: CLEARCHAN command\n"
112 " +f: FAKEUSER commands\n"
113 " +b: BROADCAST commands\n"
114 " +o: Operation commands, such as insmod, rmmod, die, etc\n"
116 " +I: Misc commands (resync, etc)\n"
117 " +n: Sends notices instead of privmsgs\n"
118 " +A: Every single command sent to the service (spammy)\n"
121 registercontrolhelpcmd("deluser", NO_OPERED
| NO_ACCOUNT
, 2, &noperserv_deluser
, "Syntax: DELUSER <nickname|#authname>\nDeletes the specified user.");
122 registerhook(HOOK_NICK_MODEOPER
, &noperserv_oper_detection
);
127 #ifdef BROKEN_DLCLOSE
135 deregisterhook(HOOK_NICK_MODEOPER
, &noperserv_oper_detection
);
137 deregistercontrolcmd("noticeflags", &noperserv_noticeflags
);
138 deregistercontrolcmd("userflags", &noperserv_userflags
);
139 deregistercontrolcmd("noticeflags", &noperserv_noticeflags
);
141 noperserv_cleanup_hooks();
143 noperserv_cleanup_db();
145 releasenickext(noperserv_ext
);
149 int noperserv_hello(void *sender
, int cargc
, char **cargv
) {
150 char *newaccount
= NULL
;
153 nick
*np
= (nick
*)sender
, *np2
, *target
= NULL
;
156 newaccount
= np
->authname
;
158 if(cargv
[0][0] == '#') {
160 for(i
=0;i
<NICKHASHSIZE
;i
++)
161 for(np2
=nicktable
[i
];np2
;np2
=np2
->next
)
162 if(IsAccount(np2
) && !ircd_strcmp(cargv
[0] + 1, np2
->authname
)) {
164 newaccount
= target
->authname
;
168 controlreply(np
, "Cannot find anyone with that authname on the network.");
172 target
= getnickbynick(cargv
[0]);
174 controlreply(np
, "Supplied nickname is not on the network.");
177 if(!IsAccount(target
)) {
178 controlreply(np
, "Supplied user is not authed with the network.");
181 newaccount
= target
->authname
;
184 au
= noperserv_get_autheduser(newaccount
);
186 controlreply(np
, "Authname already registered.");
190 au
= noperserv_new_autheduser(newaccount
);
192 controlreply(np
, "Memory allocation error.");
196 if(noperserv_get_autheduser_count() == 1) {
197 au
->authlevel
= NO_FIRST_USER_LEVEL
;
198 au
->noticelevel
= NO_FIRST_USER_DEFAULT_NOTICELEVEL
;
200 au
->authlevel
= NO_DEFAULT_LEVEL
;
201 au
->noticelevel
= NO_DEFAULT_NOTICELEVEL
;
204 au
->id
= noperserv_next_autheduser_id();
205 noperserv_update_autheduser(au
);
207 for(i
=0;i
<NICKHASHSIZE
;i
++)
208 for(np2
=nicktable
[i
];np2
;np2
=np2
->next
)
209 if(IsAccount(np2
) && !ircd_strcmp(newaccount
, np2
->authname
)) {
210 noperserv_add_to_autheduser(np2
, au
);
211 controlreply(np2
, "An account has been created for you (auth %s).", au
->authname
->content
);
212 if(NOGetAuthLevel(au
))
213 controlreply(np2
, "User flags: %s", printflags(NOGetAuthLevel(au
), no_userflags
));
214 controlreply(np2
, "Notice flags: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
217 if(ircd_strcmp(np
->authname
, newaccount
)) { /* send a message to the person who HELLO'ed if we haven't already been told */
218 controlreply(np
, "Account created for auth %s.", au
->authname
->content
);
219 if(NOGetAuthLevel(au
))
220 controlreply(np
, "User flags: %s", printflags(NOGetAuthLevel(au
), no_userflags
));
221 controlreply(np
, "Notice flags: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
222 controlreply(np
, "Instructions sent to all authed users.");
223 } else if(au
->nick
&& au
->nick
->next
) { /* if we have already been told, tell the user it was sent to more than themselves */
224 controlreply(np
, "Instructions sent to all authed users.");
227 controlwall(NO_OPERED
, NL_MANAGEMENT
, "%s/%s just HELLO'ed: %s", np
->nick
, np
->authname
, au
->authname
->content
);
231 no_autheduser
*noperserv_autheduser_from_command(nick
*np
, char *command
, int *typefound
, char **returned
) {
233 if(command
[0] == '#') {
234 au
= noperserv_get_autheduser(command
+ 1);
236 controlreply(np
, "Authname not found.");
238 *typefound
= NO_FOUND_AUTHNAME
;
239 *returned
= au
->authname
->content
;
243 nick
*np2
= getnickbynick(command
);
245 controlreply(np
, "Nickname not on the network.");
248 if(!IsAccount(np2
)) {
249 controlreply(np
, "User is not authed with the network.");
252 au
= NOGetAuthedUser(np2
);
254 controlreply(np
, "User does not have an account.");
256 *typefound
= NO_FOUND_NICKNAME
;
257 *returned
= np2
->nick
;
265 int noperserv_noticeflags(void *sender
, int cargc
, char **cargv
) {
266 nick
*np
= (nick
*)sender
;
270 if((cargv
[0][0] == '+') || (cargv
[0][0] == '-')) {
272 au
= NOGetAuthedUser(np
);
273 flag_t fwas
= NOGetNoticeLevel(au
), permittedchanges
= noperserv_policy_permitted_noticeflags(au
);
275 ret
= setflags(&au
->noticelevel
, permittedchanges
, cargv
[0], no_noticeflags
, REJECT_DISALLOWED
| REJECT_UNKNOWN
);
276 if(ret
!= REJECT_UNKNOWN
) {
277 if(ret
== REJECT_DISALLOWED
) {
279 setflags(&fnow
, NL_ALL
, cargv
[0], no_noticeflags
, REJECT_NONE
);
281 controlreply(np
, "No changes made to existing flags.");
283 char ourflags
[FLAGBUFLEN
], ournoticeflags
[FLAGBUFLEN
];
284 controlreply(np
, "Flag alterations denied.");
286 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
287 strlcpy(ournoticeflags
, printflags(NOGetNoticeLevel(au
), no_noticeflags
), sizeof(ournoticeflags
));
288 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) attempted to NOTICEFLAGS (%s): %s", np
->nick
, np
->authname
, ourflags
, ournoticeflags
, printflagdiff(fwas
, fnow
, no_noticeflags
));
291 } else if(ret
== REJECT_NONE
) {
292 if(NOGetNoticeLevel(au
) == fwas
) {
293 controlreply(np
, "No changes made to existing flags.");
295 char ourflags
[FLAGBUFLEN
], ournoticeflags
[FLAGBUFLEN
], diff
[FLAGBUFLEN
* 2 + 1], finalflags
[FLAGBUFLEN
];
296 no_nicklist
*nl
= au
->nick
;
297 noperserv_update_autheduser(au
);
298 controlreply(np
, "Flag alterations complete.");
300 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
301 strlcpy(ournoticeflags
, printflags(fwas
, no_noticeflags
), sizeof(ournoticeflags
));
302 strlcpy(diff
, printflagdiff(fwas
, NOGetNoticeLevel(au
), no_noticeflags
), sizeof(diff
));
303 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) successfully used NOTICEFLAGS (%s): %s", np
->nick
, np
->authname
, ourflags
, ournoticeflags
, diff
);
305 strlcpy(finalflags
, printflags(NOGetNoticeLevel(au
), no_noticeflags
), sizeof(finalflags
));
308 controlreply(nl
->nick
, "!!! %s just used NOTICEFLAGS (%s): %s", np
->nick
, ournoticeflags
, diff
);
309 controlreply(nl
->nick
, "Your notice flags are %s", finalflags
);
314 controlreply(np
, "Unknown flag(s) supplied.");
320 au
= noperserv_autheduser_from_command(np
, cargv
[0], &typefound
, &itemfound
);
324 if(au
!= NOGetAuthedUser(np
)) {
325 controlreply(np
, "Notice flags for %s %s are: %s", typefound
==NO_FOUND_NICKNAME
?"user":"authname", itemfound
, printflags(NOGetNoticeLevel(au
), no_noticeflags
));
330 au
= NOGetAuthedUser(np
);
333 if(!au
) /* shouldn't happen */
336 controlreply(np
, "Your notice flags are: %s", printflags(NOGetNoticeLevel(au
), no_noticeflags
));
342 int noperserv_deluser(void *sender
, int cargc
, char **cargv
) {
343 nick
*np
= (nick
*)sender
;
344 no_autheduser
*target
/* target user */, *au
= NOGetAuthedUser(np
); /* user executing command */
345 char *userreturned
= NULL
; /* nickname or authname of the target, pulled from the db */
346 int typefound
; /* whether it was an authname or a username */
348 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
], deleteduser
[NOMax(ACCOUNTLEN
, NICKLEN
) + 1];
353 target
= noperserv_autheduser_from_command(np
, cargv
[0], &typefound
, &userreturned
);
357 strlcpy(targetflags
, printflags(NOGetAuthLevel(target
), no_userflags
), sizeof(targetflags
));
358 strlcpy(ourflags
, printflags(NOGetAuthLevel(au
), no_userflags
), sizeof(ourflags
));
360 /* we have to copy it as it might point to an autheduser, which we're about to delete */
361 strlcpy(deleteduser
, userreturned
, sizeof(deleteduser
));
363 /* we have to check if target != au, because if successful policy_modification_permitted just returns the flags we're allowed
364 to modify, if we have no flags we won't be able to delete ourselves */
365 if((target
!= au
) && !noperserv_policy_permitted_modifications(au
, target
)) {
366 controlreply(np
, "Deletion denied.");
367 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) attempted to DELUSER %s (%s)", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
);
372 for(nl
=target
->nick
;nl
;nl
=nl
->next
)
374 controlreply(nl
->nick
, "!!! %s/%s (%s) just DELUSERed you.", np
->nick
, np
->authname
, ourflags
);
376 noperserv_delete_autheduser(target
);
378 controlwall(NO_OPER
, NL_MANAGEMENT
, "%s/%s (%s) successfully used DELUSER on %s (%s)", np
->nick
, np
->authname
, ourflags
, target
->authname
->content
, targetflags
);
381 controlreply(np
, "You have been deleted.");
383 controlreply(np
, "%s %s deleted.", typefound
==NO_FOUND_AUTHNAME
?"Auth":"User", deleteduser
);
390 /* this command needs LOTS of checking */
391 int noperserv_userflags(void *sender
, int cargc
, char **cargv
) {
392 nick
*np
= (nick
*)sender
;
393 no_autheduser
*au
= NOGetAuthedUser(np
), *target
= NULL
;
394 char *flags
= NULL
, *nicktarget
= NULL
;
399 } else if(cargc
== 1) {
400 if((cargv
[0][0] == '+') || (cargv
[0][0] == '-')) { /* modify our own */
403 } else { /* viewing someone elses */
404 nicktarget
= cargv
[0];
406 } else if(cargc
== 2) {
407 nicktarget
= cargv
[0];
414 target
= noperserv_autheduser_from_command(np
, nicktarget
, &typefound
, &nicktarget
);
421 flag_t permitted
= noperserv_policy_permitted_modifications(au
, target
), fwas
= NOGetAuthLevel(target
), fours
= NOGetAuthLevel(au
);
423 ret
= setflags(&target
->authlevel
, permitted
, flags
, no_userflags
, REJECT_DISALLOWED
| REJECT_UNKNOWN
);
424 if(ret
!= REJECT_UNKNOWN
) {
425 if(ret
== REJECT_DISALLOWED
) {
427 setflags(&fnow
, NO_ALL_FLAGS
, flags
, no_userflags
, REJECT_NONE
);
429 controlreply(np
, "No changes made to existing flags.");
431 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
];
432 controlreply(np
, "Flag alterations denied.");
434 strlcpy(targetflags
, printflags(fwas
, no_userflags
), sizeof(targetflags
));
435 strlcpy(ourflags
, printflags(fours
, no_userflags
), sizeof(ourflags
));
437 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
));
440 } else if(ret
== REJECT_NONE
) {
441 if(NOGetAuthLevel(target
) == fwas
) {
442 controlreply(np
, "No changes made to existing flags.");
444 char targetflags
[FLAGBUFLEN
], ourflags
[FLAGBUFLEN
], finalflags
[FLAGBUFLEN
];
445 no_nicklist
*nl
= target
->nick
;
447 noperserv_policy_update_noticeflags(fwas
, target
);
448 noperserv_update_autheduser(target
);
450 controlreply(np
, "Flag alterations complete.");
452 strlcpy(targetflags
, printflags(fwas
, no_userflags
), sizeof(targetflags
));
453 strlcpy(ourflags
, printflags(fours
, no_userflags
), sizeof(ourflags
));
455 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
));
457 strlcpy(finalflags
, printflags(NOGetAuthLevel(target
), no_userflags
), sizeof(finalflags
));
460 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
));
461 controlreply(nl
->nick
, "Your user flags are now: %s", finalflags
);
462 controlreply(nl
->nick
, "Your notice flags are now: %s", printflags(target
->noticelevel
, no_noticeflags
));
467 controlreply(np
, "Unknown flag(s) supplied.");
473 controlreply(np
, "User flags for %s %s: %s", typefound
==NO_FOUND_AUTHNAME
?"auth":"user", nicktarget
, printflags(NOGetAuthLevel(target
), no_userflags
));
474 controlreply(np
, "Notice flags for %s %s: %s", typefound
==NO_FOUND_AUTHNAME
?"auth":"user", nicktarget
, printflags(target
->noticelevel
, no_noticeflags
));
476 controlreply(np
, "Your user flags are: %s", printflags(NOGetAuthLevel(target
), no_userflags
));
477 controlreply(np
, "Your notice flags are: %s", printflags(target
->noticelevel
, no_noticeflags
));
483 void noperserv_oper_detection(int hooknum
, void *arg
) {
484 void **args
= (void **)arg
;
486 char *modestr
= args
[1];
487 flag_t after
= np
->umodes
;
489 setflags(&after
, UMODE_ALL
, modestr
, umodeflags
, REJECT_NONE
);
490 if(np
->umodes
& UMODE_OPER
) {
491 if(!(after
& UMODE_OPER
))
492 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
:"");
494 if(after
& UMODE_OPER
)
495 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
:"");