]> jfr.im git - solanum.git/blame_incremental - extensions/override.c
Merge branch 'master' into authd-framework-2
[solanum.git] / extensions / override.c
... / ...
CommitLineData
1/*
2 * oper-override for charybdis.
3 *
4 * adds usermode +p and has a timer event that is iterated over to disable
5 * usermode +p after a while...
6 *
7 * you need to have oper:override permission on the opers you want to be
8 * able to use this extension.
9 */
10
11#include "stdinc.h"
12#include "modules.h"
13#include "hook.h"
14#include "client.h"
15#include "ircd.h"
16#include "send.h"
17#include "hash.h"
18#include "s_conf.h"
19#include "s_user.h"
20#include "s_serv.h"
21#include "numeric.h"
22#include "privilege.h"
23#include "s_newconf.h"
24
25static const char override_desc[] =
26 "Adds user mode +p, an operator-only user mode that grants temporary privileges to override anything";
27
28static void check_umode_change(void *data);
29static void hack_channel_access(void *data);
30static void hack_can_join(void *data);
31static void hack_can_kick(void *data);
32static void hack_can_send(void *data);
33static void handle_client_exit(void *data);
34
35mapi_hfn_list_av1 override_hfnlist[] = {
36 { "umode_changed", (hookfn) check_umode_change },
37 { "get_channel_access", (hookfn) hack_channel_access },
38 { "can_join", (hookfn) hack_can_join },
39 { "can_kick", (hookfn) hack_can_kick },
40 { "can_send", (hookfn) hack_can_send },
41 { "client_exit", (hookfn) handle_client_exit },
42 { NULL, NULL }
43};
44
45#define CHFL_OVERRIDE 0x0004
46#define IsOperOverride(x) (HasPrivilege((x), "oper:override"))
47
48struct OverrideSession {
49 rb_dlink_node node;
50
51 struct Client *client;
52 time_t deadline;
53};
54
55rb_dlink_list overriding_opers = { NULL, NULL, 0 };
56
57static void
58update_session_deadline(struct Client *source_p, struct OverrideSession *session_p)
59{
60 if (session_p == NULL)
61 {
62 rb_dlink_node *n;
63
64 RB_DLINK_FOREACH(n, overriding_opers.head)
65 {
66 struct OverrideSession *s = n->data;
67
68 if (s->client == source_p)
69 {
70 session_p = s;
71 break;
72 }
73 }
74 }
75
76 if (session_p == NULL)
77 {
78 session_p = rb_malloc(sizeof(struct OverrideSession));
79 session_p->client = source_p;
80 }
81
82 session_p->deadline = rb_current_time() + 1800;
83
84 rb_dlinkDelete(&session_p->node, &overriding_opers);
85 rb_dlinkAdd(session_p, &session_p->node, &overriding_opers);
86}
87
88static void
89expire_override_deadlines(void *unused)
90{
91 rb_dlink_node *n, *tn;
92
93 RB_DLINK_FOREACH_SAFE(n, tn, overriding_opers.head)
94 {
95 struct OverrideSession *session_p = n->data;
96
97 if (session_p->deadline > rb_current_time())
98 break;
99 else if (session_p->deadline < rb_current_time())
100 {
101 const char *parv[4] = {session_p->client->name, session_p->client->name, "-p", NULL};
102 user_mode(session_p->client, session_p->client, 3, parv);
103 }
104 }
105}
106
107static void
108check_umode_change(void *vdata)
109{
110 hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
111 struct Client *source_p = data->client;
112
113 if (!MyClient(source_p))
114 return;
115
116 if (data->oldumodes & UMODE_OPER && !IsOper(source_p))
117 source_p->umodes &= ~user_modes['p'];
118
119 /* didn't change +p umode, we don't need to do anything */
120 if (!((data->oldumodes ^ source_p->umodes) & user_modes['p']))
121 return;
122
123 if (source_p->umodes & user_modes['p'])
124 {
125 if (!IsOperOverride(source_p))
126 {
127 sendto_one_notice(source_p, ":*** You need oper:override privilege for +p");
128 source_p->umodes &= ~user_modes['p'];
129 return;
130 }
131
132 update_session_deadline(source_p, NULL);
133
134 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s has enabled oper-override (+p)",
135 get_oper_name(source_p));
136 }
137 else if (!(source_p->umodes & user_modes['p']))
138 {
139 rb_dlink_node *n, *tn;
140
141 RB_DLINK_FOREACH_SAFE(n, tn, overriding_opers.head)
142 {
143 struct OverrideSession *session_p = n->data;
144
145 if (session_p->client != source_p)
146 continue;
147
148 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s has disabled oper-override (+p)",
149 get_oper_name(session_p->client));
150
151 rb_dlinkDelete(n, &overriding_opers);
152 rb_free(session_p);
153 }
154 }
155}
156
157static void
158hack_channel_access(void *vdata)
159{
160 hook_data_channel_approval *data = (hook_data_channel_approval *) vdata;
161
162 if (data->dir == MODE_QUERY)
163 return;
164
165 if (data->approved == CHFL_CHANOP)
166 return;
167
168 if (data->client->umodes & user_modes['p'])
169 {
170 update_session_deadline(data->client, NULL);
171 data->approved = CHFL_OVERRIDE;
172
173 /* we only want to report modehacks, which are always non-NULL */
174 if (data->modestr)
175 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s is using oper-override on %s (modehacking: %s)",
176 get_oper_name(data->client), data->chptr->chname, data->modestr);
177 }
178}
179
180static void
181hack_can_join(void *vdata)
182{
183 hook_data_channel *data = (hook_data_channel *) vdata;
184
185 if (data->approved == 0)
186 return;
187
188 if (data->client->umodes & user_modes['p'])
189 {
190 update_session_deadline(data->client, NULL);
191 data->approved = 0;
192
193 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s is using oper-override on %s (banwalking)",
194 get_oper_name(data->client), data->chptr->chname);
195 }
196}
197
198static void
199hack_can_kick(void *vdata)
200{
201 hook_data_channel_approval *data = (hook_data_channel_approval *) vdata;
202 int alevel;
203
204 alevel = get_channel_access(data->client, data->chptr, data->msptr, data->dir, NULL);
205 if (alevel != CHFL_OVERRIDE)
206 return;
207
208 if (data->client->umodes & user_modes['p'])
209 {
210 update_session_deadline(data->client, NULL);
211 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s is using oper-override on %s (KICK %s)",
212 get_oper_name(data->client), data->chptr->chname, data->target->name);
213 }
214}
215
216static void
217hack_can_send(void *vdata)
218{
219 hook_data_channel_approval *data = (hook_data_channel_approval *) vdata;
220
221 if (data->dir == MODE_QUERY)
222 return;
223
224 if (data->approved == CAN_SEND_NONOP || data->approved == CAN_SEND_OPV)
225 return;
226
227 if (data->client->umodes & user_modes['p'])
228 {
229 data->approved = CAN_SEND_NONOP;
230
231 if (MyClient(data->client))
232 {
233 update_session_deadline(data->client, NULL);
234 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s is using oper-override on %s (forcing message)",
235 get_oper_name(data->client), data->chptr->chname);
236 }
237 }
238}
239
240static void
241handle_client_exit(void *vdata)
242{
243 hook_data_client_exit *data = (hook_data_client_exit *) vdata;
244 rb_dlink_node *n, *tn;
245 struct Client *source_p = data->target;
246
247 RB_DLINK_FOREACH_SAFE(n, tn, overriding_opers.head)
248 {
249 struct OverrideSession *session_p = n->data;
250
251 if (session_p->client != source_p)
252 continue;
253
254 rb_dlinkDelete(n, &overriding_opers);
255 rb_free(session_p);
256 }
257}
258
259struct ev_entry *expire_override_deadlines_ev = NULL;
260
261static int
262_modinit(void)
263{
264 /* add the usermode to the available slot */
265 user_modes['p'] = find_umode_slot();
266 construct_umodebuf();
267
268 expire_override_deadlines_ev = rb_event_add("expire_override_deadlines", expire_override_deadlines, NULL, 60);
269
270 return 0;
271}
272
273static void
274_moddeinit(void)
275{
276 /* disable the umode and remove it from the available list */
277 user_modes['p'] = 0;
278 construct_umodebuf();
279
280 rb_event_delete(expire_override_deadlines_ev);
281}
282
283DECLARE_MODULE_AV2(override, _modinit, _moddeinit, NULL, NULL,
284 override_hfnlist, NULL, NULL, override_desc);