Debian Codegraph @

modules/operserv/soper.c

raw
1 /*
2 * Copyright (c) 2006 Atheme Development Group
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * Dynamic services operator privileges
6 *
7 */
8
9 #include "atheme.h"
10
11 DECLARE_MODULE_V1
12 (
13 "operserv/soper", false, _modinit, _moddeinit,
14 PACKAGE_STRING,
15 VENDOR_STRING
16 );
17
18 static void os_cmd_soper(sourceinfo_t *si, int parc, char *parv[]);
19 static void os_cmd_soper_list(sourceinfo_t *si, int parc, char *parv[]);
20 static void os_cmd_soper_listclass(sourceinfo_t *si, int parc, char *parv[]);
21 static void os_cmd_soper_add(sourceinfo_t *si, int parc, char *parv[]);
22 static void os_cmd_soper_del(sourceinfo_t *si, int parc, char *parv[]);
23 static void os_cmd_soper_setpass(sourceinfo_t *si, int parc, char *parv[]);
24
25 command_t os_soper = { "SOPER", N_("Shows and changes services operator privileges."), AC_NONE, 4, os_cmd_soper, { .path = "oservice/soper" } };
26
27 command_t os_soper_list = { "LIST", N_("Lists services operators."), PRIV_VIEWPRIVS, 0, os_cmd_soper_list, { .path = "" } };
28 command_t os_soper_listclass = { "LISTCLASS", N_("Lists operclasses."), PRIV_VIEWPRIVS, 0, os_cmd_soper_listclass, { .path = "" } };
29 command_t os_soper_add = { "ADD", N_("Grants services operator privileges to an account."), PRIV_GRANT, 3, os_cmd_soper_add, { .path = "" } };
30 command_t os_soper_del = { "DEL", N_("Removes services operator privileges from an account."), PRIV_GRANT, 1, os_cmd_soper_del, { .path = "" } };
31 command_t os_soper_setpass = { "SETPASS", N_("Changes a password for services operator privileges."), PRIV_GRANT, 2, os_cmd_soper_setpass, { .path = "" } };
32
33 mowgli_patricia_t *os_soper_cmds;
34
35 void _modinit(module_t *m)
36 {
37 service_named_bind_command("operserv", &os_soper);
38
39 os_soper_cmds = mowgli_patricia_create(strcasecanon);
40
41 command_add(&os_soper_list, os_soper_cmds);
42 command_add(&os_soper_listclass, os_soper_cmds);
43 command_add(&os_soper_add, os_soper_cmds);
44 command_add(&os_soper_del, os_soper_cmds);
45 command_add(&os_soper_setpass, os_soper_cmds);
46 }
47
48 void _moddeinit(module_unload_intent_t intent)
49 {
50 service_named_unbind_command("operserv", &os_soper);
51 command_delete(&os_soper_list, os_soper_cmds);
52 command_delete(&os_soper_listclass, os_soper_cmds);
53 command_delete(&os_soper_add, os_soper_cmds);
54 command_delete(&os_soper_del, os_soper_cmds);
55
56 mowgli_patricia_destroy(os_soper_cmds, NULL, NULL);
57 }
58
59 static void os_cmd_soper(sourceinfo_t *si, int parc, char *parv[])
60 {
61 command_t *c;
62
63 if (!has_any_privs(si))
64 {
65 command_fail(si, fault_noprivs, _("You are not authorized to perform this operation."));
66 return;
67 }
68
69 if (parc < 1)
70 {
71 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SOPER");
72 command_fail(si, fault_needmoreparams, _("Syntax: SOPER LIST|LISTCLASS|ADD|DEL [account] [operclass]"));
73 return;
74 }
75
76 c = command_find(os_soper_cmds, parv[0]);
77 if (c == NULL)
78 {
79 command_fail(si, fault_badparams, _("Invalid command. Use \2/%s%s help\2 for a command listing."), (ircd->uses_rcommand == false) ? "msg " : "", si->service->disp);
80 return;
81 }
82
83 command_exec(si->service, si, c, parc - 1, parv + 1);
84 }
85
86 static void os_cmd_soper_list(sourceinfo_t *si, int parc, char *parv[])
87 {
88 mowgli_node_t *n;
89 soper_t *soper;
90 const char *typestr;
91
92 logcommand(si, CMDLOG_GET, "SOPER:LIST");
93 command_success_nodata(si, "%-20s %-5s %-20s", _("Account"), _("Type"), _("Operclass"));
94 command_success_nodata(si, "%-20s %-5s %-20s", "--------------------", "-----", "--------------------");
95 MOWGLI_ITER_FOREACH(n, soperlist.head)
96 {
97 soper = n->data;
98 if (!(soper->flags & SOPER_CONF))
99 typestr = "DB";
100 else if (soper->myuser != NULL)
101 typestr = "Conf";
102 else
103 typestr = "Conf*";
104 command_success_nodata(si, "%-20s %-5s %-20s",
105 soper->myuser != NULL ? entity(soper->myuser)->name : soper->name,
106 typestr,
107 soper->classname);
108 }
109 command_success_nodata(si, "%-20s %-5s %-20s", "--------------------", "-----", "--------------------");
110 command_success_nodata(si, _("End of services operator list"));
111 }
112
113 static void os_cmd_soper_listclass(sourceinfo_t *si, int parc, char *parv[])
114 {
115 mowgli_node_t *n;
116 operclass_t *operclass;
117
118 logcommand(si, CMDLOG_GET, "SOPER:LISTCLASS");
119 command_success_nodata(si, _("Oper class list:"));
120 MOWGLI_ITER_FOREACH(n, operclasslist.head)
121 {
122 operclass = n->data;
123 command_success_nodata(si, "%c%c %s",
124 has_all_operclass(si, operclass) ? '+' : '-',
125 operclass->flags & OPERCLASS_NEEDOPER ? '*' : ' ',
126 operclass->name);
127 }
128 command_success_nodata(si, _("End of oper class list"));
129 }
130
131 static void os_cmd_soper_add(sourceinfo_t *si, int parc, char *parv[])
132 {
133 myuser_t *mu;
134 operclass_t *operclass;
135 char hash[PASSLEN];
136
137 if (parc < 2)
138 {
139 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SOPER ADD");
140 command_fail(si, fault_needmoreparams, _("Syntax: SOPER ADD <account> <operclass> [password]"));
141 return;
142 }
143
144 mu = myuser_find_ext(parv[0]);
145 if (mu == NULL)
146 {
147 command_fail(si, fault_nosuch_target, _("\2%s\2 is not registered."), parv[0]);
148 return;
149 }
150
151 if (is_conf_soper(mu))
152 {
153 command_fail(si, fault_noprivs, _("You may not modify \2%s\2's operclass as it is defined in the configuration file."), entity(mu)->name);
154 return;
155 }
156
157 operclass = operclass_find(parv[1]);
158 if (operclass == NULL)
159 {
160 command_fail(si, fault_nosuch_target, _("No such oper class \2%s\2."), parv[1]);
161 return;
162 }
163 else if (mu->soper != NULL && mu->soper->operclass == operclass)
164 {
165 command_fail(si, fault_nochange, _("Oper class for \2%s\2 is already set to \2%s\2."), entity(mu)->name, operclass->name);
166 return;
167 }
168
169 if (!has_all_operclass(si, operclass))
170 {
171 command_fail(si, fault_noprivs, _("Oper class \2%s\2 has more privileges than you."), operclass->name);
172 return;
173 }
174 else if (mu->soper != NULL && mu->soper->operclass != NULL && !has_all_operclass(si, mu->soper->operclass))
175 {
176 command_fail(si, fault_noprivs, _("Oper class for \2%s\2 is set to \2%s\2 which you are not authorized to change."),
177 entity(mu)->name, mu->soper->operclass->name);
178 return;
179 }
180
181 wallops("\2%s\2 is changing oper class for \2%s\2 to \2%s\2",
182 get_oper_name(si), entity(mu)->name, operclass->name);
183 logcommand(si, CMDLOG_ADMIN, "SOPER:ADD: \2%s\2 \2%s\2", entity(mu)->name, operclass->name);
184 if (is_soper(mu))
185 soper_delete(mu->soper);
186
187 if (parv[2])
188 {
189 if (crypto_module_loaded)
190 {
191 mowgli_strlcpy(hash, crypt_string(parv[2], gen_salt()), PASSLEN);
192 soper_add(entity(mu)->name, operclass->name, 0, hash);
193 }
194 else
195 soper_add(entity(mu)->name, operclass->name, 0, parv[2]);
196 }
197 else
198 soper_add(entity(mu)->name, operclass->name, 0, NULL);
199
200 command_success_nodata(si, _("Set class for \2%s\2 to \2%s\2."), entity(mu)->name, operclass->name);
201 }
202
203 static void os_cmd_soper_del(sourceinfo_t *si, int parc, char *parv[])
204 {
205 myuser_t *mu;
206
207 if (parc < 1)
208 {
209 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SOPER DEL");
210 command_fail(si, fault_needmoreparams, _("Syntax: SOPER DEL <account>"));
211 return;
212 }
213
214 mu = myuser_find_ext(parv[0]);
215 if (mu == NULL)
216 {
217 command_fail(si, fault_nosuch_target, _("\2%s\2 is not registered."), parv[0]);
218 return;
219 }
220
221 if (is_conf_soper(mu))
222 {
223 command_fail(si, fault_noprivs, _("You may not modify \2%s\2's operclass as it is defined in the configuration file."), entity(mu)->name);
224 return;
225 }
226
227 if (!is_soper(mu))
228 {
229 command_fail(si, fault_nochange, _("\2%s\2 does not have an operclass set."), entity(mu)->name);
230 return;
231 }
232 else if (mu->soper->operclass != NULL && !has_all_operclass(si, mu->soper->operclass))
233 {
234 command_fail(si, fault_noprivs, _("Oper class for \2%s\2 is set to \2%s\2 which you are not authorized to change."),
235 entity(mu)->name, mu->soper->operclass->name);
236 return;
237 }
238
239 wallops("\2%s\2 is removing oper class for \2%s\2",
240 get_oper_name(si), entity(mu)->name);
241 logcommand(si, CMDLOG_ADMIN, "SOPER:DELETE: \2%s\2", entity(mu)->name);
242 soper_delete(mu->soper);
243 command_success_nodata(si, _("Removed class for \2%s\2."), entity(mu)->name);
244 }
245
246 static void os_cmd_soper_setpass(sourceinfo_t *si, int parc, char *parv[])
247 {
248 myuser_t *mu;
249 mowgli_node_t *n;
250 user_t *u;
251
252 if (parc < 1)
253 {
254 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SOPER SETPASS");
255 command_fail(si, fault_needmoreparams, _("Syntax: SOPER SETPASS <account> [password]"));
256 return;
257 }
258
259 mu = myuser_find_ext(parv[0]);
260 if (mu == NULL)
261 {
262 command_fail(si, fault_nosuch_target, _("\2%s\2 is not registered."), parv[0]);
263 return;
264 }
265
266 if (is_conf_soper(mu))
267 {
268 command_fail(si, fault_noprivs, _("You may not modify \2%s\2's operclass as it is defined in the configuration file."), entity(mu)->name);
269 return;
270 }
271
272 if (!is_soper(mu))
273 {
274 command_fail(si, fault_nochange, _("\2%s\2 does not have an operclass set."), entity(mu)->name);
275 return;
276 }
277
278 if (mu->soper->operclass != NULL && !has_all_operclass(si, mu->soper->operclass))
279 {
280 command_fail(si, fault_noprivs, _("Oper class for \2%s\2 is set to \2%s\2 which you are not authorized to change."),
281 entity(mu)->name, mu->soper->operclass->name);
282 return;
283 }
284
285 if (parc >= 2)
286 {
287 if (mu->soper->password == NULL &&
288 !command_find(si->service->commands, "IDENTIFY"))
289 {
290 command_fail(si, fault_noprivs, _("Refusing to set a services operator password if %s IDENTIFY is not loaded."), si->service->nick);
291 return;
292 }
293 wallops("\2%s\2 is changing services operator password for \2%s\2",
294 get_oper_name(si), entity(mu)->name);
295 logcommand(si, CMDLOG_ADMIN, "SOPER:SETPASS: \2%s\2 (set)", entity(mu)->name);
296 if (mu->soper->password)
297 free(mu->soper->password);
298 mu->soper->password = sstrdup(parv[1]);
299 command_success_nodata(si, _("Set password for \2%s\2 to \2%s\2."), entity(mu)->name, parv[1]);
300 MOWGLI_ITER_FOREACH(n, mu->logins.head)
301 {
302 u = n->data;
303 if (u->flags & UF_SOPER_PASS)
304 {
305 u->flags &= ~UF_SOPER_PASS;
306 notice(si->service->nick, u->nick, "You are no longer identified to %s.", si->service->nick);
307 }
308 }
309 }
310 else
311 {
312 wallops("\2%s\2 is clearing services operator password for \2%s\2",
313 get_oper_name(si), entity(mu)->name);
314 logcommand(si, CMDLOG_ADMIN, "SOPER:SETPASS: \2%s\2 (clear)", entity(mu)->name);
315 if (mu->soper->password)
316 free(mu->soper->password);
317 mu->soper->password = NULL;
318 command_success_nodata(si, _("Cleared password for \2%s\2."), entity(mu)->name);
319 }
320 }
321
322 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
323 * vim:ts=8
324 * vim:sw=8
325 * vim:noexpandtab
326 */