]> jfr.im git - solanum.git/blame - modules/um_callerid.c
MODRESTART/MODRELOAD: Defer reloading more quickly
[solanum.git] / modules / um_callerid.c
CommitLineData
921b508b
AC
1/*
2 * modules/um_callerid.c
3 * Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "stdinc.h"
31#include "modules.h"
32#include "hook.h"
33#include "client.h"
34#include "ircd.h"
35#include "send.h"
36#include "hash.h"
37#include "s_conf.h"
38#include "s_user.h"
39#include "s_serv.h"
40#include "numeric.h"
41#include "privilege.h"
42#include "s_newconf.h"
43#include "hook.h"
44#include "supported.h"
fdff78f8 45#include "logger.h"
921b508b 46
2f596395
SB
47#define IsSetStrictCallerID(c) ((c->umodes & user_modes['g']) == user_modes['g'])
48#define IsSetRelaxedCallerID(c) ((c->umodes & user_modes['G']) == user_modes['G'])
49#define IsSetAnyCallerID(c) (IsSetStrictCallerID(c) || IsSetRelaxedCallerID(c))
50#define IsSetTalkThroughCallerID(c) ((c->umodes & user_modes['M']) == user_modes['M'])
51
52static const char um_callerid_desc[] =
53 "Provides usermodes +g and +G which restrict messages from unauthorized users.";
54
55
56struct CallerIDOverrideSession {
57 rb_dlink_node node;
58
59 struct Client *client;
60 time_t deadline;
61};
62
63static rb_dlink_list callerid_overriding_opers = { NULL, NULL, 0 };
64struct ev_entry *expire_callerid_override_deadlines_ev = NULL;
65
66static void
67update_session_deadline(struct Client *source_p)
921b508b 68{
2f596395
SB
69 struct CallerIDOverrideSession *session_p = NULL;
70 rb_dlink_node *n;
fdff78f8 71
2f596395 72 RB_DLINK_FOREACH(n, callerid_overriding_opers.head)
fdff78f8 73 {
2f596395 74 struct CallerIDOverrideSession *s = n->data;
915bb561 75
2f596395
SB
76 if (s->client == source_p)
77 {
78 session_p = s;
79 break;
80 }
fdff78f8
AC
81 }
82
2f596395
SB
83 if (session_p != NULL)
84 {
85 rb_dlinkDelete(&session_p->node, &callerid_overriding_opers);
86 }
87 else
88 {
89 session_p = rb_malloc(sizeof(struct CallerIDOverrideSession));
90 session_p->client = source_p;
91 }
921b508b 92
2f596395 93 session_p->deadline = rb_current_time() + 1800;
921b508b 94
2f596395 95 rb_dlinkAddTail(session_p, &session_p->node, &callerid_overriding_opers);
921b508b
AC
96}
97
98static void
2f596395 99expire_callerid_override_deadlines(void *unused)
921b508b 100{
2f596395 101 rb_dlink_node *n, *tn;
921b508b 102
2f596395
SB
103 RB_DLINK_FOREACH_SAFE(n, tn, callerid_overriding_opers.head)
104 {
105 struct CallerIDOverrideSession *session_p = n->data;
921b508b 106
2f596395
SB
107 if (session_p->deadline >= rb_current_time())
108 {
109 break;
110 }
111 else
112 {
113 const char *parv[4] = {session_p->client->name, session_p->client->name, "-M", NULL};
114 user_mode(session_p->client, session_p->client, 3, parv);
115 }
116 }
117}
921b508b
AC
118
119static bool
120allow_message(struct Client *source_p, struct Client *target_p)
121{
122 if (!MyClient(target_p))
123 return true;
124
ace0c865 125 if (!IsSetAnyCallerID(target_p))
921b508b
AC
126 return true;
127
ad0bbd9b 128 if (!IsPerson(source_p))
7b60cca0
AC
129 return true;
130
ad0bbd9b
EK
131 if (IsSetRelaxedCallerID(target_p) &&
132 !IsSetStrictCallerID(target_p) &&
133 has_common_channel(source_p, target_p))
921b508b
AC
134 return true;
135
136 /* XXX: controversial? allow opers to send through +g */
2f596395 137 if (IsSetTalkThroughCallerID(source_p) || MayHavePrivilege(source_p, "oper:always_message"))
921b508b
AC
138 return true;
139
140 if (accept_message(source_p, target_p))
141 return true;
142
143 return false;
144}
145
146static void
147send_callerid_notice(enum message_type msgtype, struct Client *source_p, struct Client *target_p)
148{
149 if (!MyClient(target_p))
150 return;
151
152 if (msgtype == MESSAGE_TYPE_NOTICE)
153 return;
154
155 sendto_one_numeric(source_p, ERR_TARGUMODEG, form_str(ERR_TARGUMODEG),
ace0c865 156 target_p->name, IsSetStrictCallerID(target_p) ? "+g" : "+G");
921b508b
AC
157
158 if ((target_p->localClient->last_caller_id_time + ConfigFileEntry.caller_id_wait) < rb_current_time())
159 {
160 sendto_one_numeric(source_p, RPL_TARGNOTIFY, form_str(RPL_TARGNOTIFY),
161 target_p->name);
162
163 sendto_one(target_p, form_str(RPL_UMODEGMSG),
164 me.name, target_p->name, source_p->name,
ace0c865 165 source_p->username, source_p->host, IsSetStrictCallerID(target_p) ? "+g" : "+G");
921b508b
AC
166
167 target_p->localClient->last_caller_id_time = rb_current_time();
168 }
169}
170
171static bool
172add_callerid_accept_for_source(enum message_type msgtype, struct Client *source_p, struct Client *target_p)
173{
4abf8eb2
AC
174 /* only do this on source_p's server */
175 if (!MyClient(source_p))
4841ad61
AC
176 return true;
177
921b508b
AC
178 /*
179 * XXX: Controversial? Allow target users to send replies
180 * through a +g. Rationale is that people can presently use +g
181 * as a way to taunt users, e.g. harass them and hide behind +g
182 * as a way of griefing. --nenolod
183 */
184 if(msgtype != MESSAGE_TYPE_NOTICE &&
ace0c865 185 IsSetAnyCallerID(source_p) &&
4841ad61 186 !accept_message(target_p, source_p) &&
ed775e9b 187 !MayHavePrivilege(source_p, "oper:always_message"))
921b508b
AC
188 {
189 if(rb_dlink_list_length(&source_p->localClient->allow_list) <
190 (unsigned long)ConfigFileEntry.max_accept)
191 {
192 rb_dlinkAddAlloc(target_p, &source_p->localClient->allow_list);
193 rb_dlinkAddAlloc(source_p, &target_p->on_allow_list);
194 }
195 else
196 {
197 sendto_one_numeric(source_p, ERR_OWNMODE,
198 form_str(ERR_OWNMODE),
8ad8fd34 199 target_p->name, IsSetStrictCallerID(target_p) ? "+g" : "+G");
921b508b
AC
200 return false;
201 }
202 }
203
204 return true;
205}
206
207static void
db807634 208h_hdl_invite(void *vdata)
921b508b
AC
209{
210 hook_data_channel_approval *data = vdata;
211 struct Client *source_p = data->client;
212 struct Client *target_p = data->target;
c6edf654 213 static char errorbuf[BUFSIZE];
921b508b 214
6558648d
AC
215 if (data->approved)
216 return;
217
921b508b
AC
218 if (!add_callerid_accept_for_source(MESSAGE_TYPE_PRIVMSG, source_p, target_p))
219 {
220 data->approved = ERR_TARGUMODEG;
221 return;
222 }
223
224 if (allow_message(source_p, target_p))
225 return;
226
c6edf654 227 snprintf(errorbuf, sizeof errorbuf, form_str(ERR_TARGUMODEG),
c6e584dd 228 target_p->name, IsSetStrictCallerID(target_p) ? "+g" : "+G");
921b508b
AC
229
230 data->approved = ERR_TARGUMODEG;
c6edf654 231 data->error = errorbuf;
921b508b
AC
232}
233
234static void
235h_hdl_privmsg_user(void *vdata)
236{
237 hook_data_privmsg_user *data = vdata;
238 enum message_type msgtype = data->msgtype;
239 struct Client *source_p = data->source_p;
240 struct Client *target_p = data->target_p;
241
6558648d
AC
242 if (data->approved)
243 return;
244
921b508b
AC
245 if (!add_callerid_accept_for_source(msgtype, source_p, target_p))
246 {
247 data->approved = ERR_TARGUMODEG;
248 return;
249 }
250
251 if (allow_message(source_p, target_p))
252 return;
253
254 send_callerid_notice(msgtype, source_p, target_p);
255
256 data->approved = ERR_TARGUMODEG;
257}
258
2f596395
SB
259static void
260check_umode_change(void *vdata)
261{
262 hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
263 bool changed = false;
264 struct Client *source_p = data->client;
265
266 if (!MyClient(source_p))
267 return;
268
269 if (data->oldumodes & UMODE_OPER && !IsOper(source_p))
270 source_p->umodes &= ~user_modes['M'];
271
272 changed = ((data->oldumodes ^ source_p->umodes) & user_modes['M']);
273
274 if (changed && source_p->umodes & user_modes['M'])
275 {
276 if (!HasPrivilege(source_p, "oper:message"))
277 {
278 sendto_one_notice(source_p, ":*** You need oper:message privilege for +M");
279 source_p->umodes &= ~user_modes['M'];
280 return;
281 }
282
283 update_session_deadline(source_p);
284 }
285 else if (changed)
286 {
287 // Unsetting +M; remove the timeout session
288 rb_dlink_node *n, *tn;
289
290 RB_DLINK_FOREACH_SAFE(n, tn, callerid_overriding_opers.head)
291 {
292 struct CallerIDOverrideSession *session_p = n->data;
293
294 if (session_p->client != source_p)
295 continue;
296
297 rb_dlinkDelete(n, &callerid_overriding_opers);
298 rb_free(session_p);
299 }
300 }
301}
302
303static void check_priv_change(void *vdata)
304{
305 hook_data_priv_change *data = (hook_data_priv_change*)vdata;
306 struct Client *source_p = data->client;
307 const char *fakeparv[4];
308
309 if (!MyClient(source_p))
310 return;
311
312 if (source_p->umodes & user_modes['M'] && !HasPrivilege(source_p, "oper:message"))
313 {
314 sendto_one_notice(source_p, ":*** You need oper:message privilege for +M");
315 fakeparv[0] = fakeparv[1] = source_p->name;
316 fakeparv[2] = "-M";
317 fakeparv[3] = NULL;
318 user_mode(source_p, source_p, 3, fakeparv);
319 }
320}
321
322static void
323handle_client_exit(void *vdata)
324{
325 hook_data_client_exit *data = (hook_data_client_exit *) vdata;
326 rb_dlink_node *n, *tn;
327 struct Client *source_p = data->target;
328
329 RB_DLINK_FOREACH_SAFE(n, tn, callerid_overriding_opers.head)
330 {
331 struct CallerIDOverrideSession *session_p = n->data;
332
333 if (session_p->client != source_p)
334 continue;
335
336 rb_dlinkDelete(n, &callerid_overriding_opers);
337 rb_free(session_p);
338 }
339}
340
921b508b 341static mapi_hfn_list_av1 um_callerid_hfnlist[] = {
2f596395
SB
342 { "umode_changed", check_umode_change },
343 { "priv_change", check_priv_change },
2a64b1e5
DF
344 { "invite", h_hdl_invite, HOOK_HIGH },
345 { "privmsg_user", h_hdl_privmsg_user, HOOK_HIGH },
2f596395 346 { "client_exit", handle_client_exit },
921b508b
AC
347 { NULL, NULL }
348};
349
2f596395
SB
350static int
351um_callerid_modinit(void)
352{
353 rb_dlink_node *ptr;
354
355 user_modes['g'] = find_umode_slot();
356 if (!user_modes['g'])
357 {
358 ierror("um_callerid: unable to allocate usermode slot for +g; unloading module.");
359 return -1;
360 }
361
362 user_modes['G'] = find_umode_slot();
363 if (!user_modes['G'])
364 {
365 user_modes['g'] = 0;
366
367 ierror("um_callerid: unable to allocate usermode slot for +G; unloading module.");
368 return -1;
369 }
370
371 user_modes['M'] = find_umode_slot();
372 if (!user_modes['M'])
373 {
374 user_modes['g'] = 0;
375 user_modes['G'] = 0;
376
377 ierror("um_callerid: unable to allocate usermode slot for +M; unloading module.");
378 return -1;
379 }
380
381 construct_umodebuf();
382
383 add_isupport("CALLERID", isupport_umode, "g");
384
385 RB_DLINK_FOREACH(ptr, lclient_list.head)
386 {
387 struct Client *client_p = ptr->data;
388 if (IsPerson(client_p) && (client_p->umodes & user_modes['M']))
389 update_session_deadline(client_p);
390 }
391
392 expire_callerid_override_deadlines_ev = rb_event_add("expire_callerid_override_deadlines", expire_callerid_override_deadlines, NULL, 60);
393
394 return 0;
395}
396
397static void
398um_callerid_moddeinit(void)
399{
400 user_modes['g'] = 0;
401 user_modes['G'] = 0;
402 user_modes['M'] = 0;
403 construct_umodebuf();
404
405 delete_isupport("CALLERID");
406
407 rb_event_delete(expire_callerid_override_deadlines_ev);
408}
409
921b508b
AC
410DECLARE_MODULE_AV2(um_callerid, um_callerid_modinit, um_callerid_moddeinit,
411 NULL, NULL, um_callerid_hfnlist, NULL, NULL, um_callerid_desc);