Debian Codegraph @

modules/contrib/src/os_defcon.c

raw
1 /*
2 * Copyright (c) 2016 Atheme Development Group <http://atheme.github.io>
3 * Copyright (c) 2010 JD Horelick <[email protected]>
4 * Rights to this code are as defined in doc/LICENSE.
5 *
6 * DEFCON implementation.
7 *
8 * By default, any setting except 5 will expire after 15 minutes,
9 * to change this, add a defcon_timeout = X; option to the operserv{}
10 * block in your atheme.con. X = amount of time in minutes for a defcon
11 * setting to time out/expire.
12 */
13
14 #include "atheme-compat.h"
15
16 #define DEFCON_CMODE "R"
17
18 static int level = 5;
19 static unsigned int defcon_timeout = 900;
20 static mowgli_eventloop_timer_t *defcon_timer = NULL;
21
22 static void
23 defcon_nouserreg(hook_user_register_check_t *hdata)
24 {
25 return_if_fail(hdata != NULL);
26 return_if_fail(hdata->si != NULL);
27
28 if (level < 5)
29 {
30 command_fail(hdata->si, fault_badparams, _("Registrations are currently disabled on this network, please try again later."));
31 hdata->approved++;
32 }
33 }
34
35 static void
36 defcon_nochanreg(hook_channel_register_check_t *hdatac)
37 {
38 return_if_fail(hdatac != NULL);
39 return_if_fail(hdatac->si != NULL);
40
41 if (level < 5)
42 {
43 command_fail(hdatac->si, fault_badparams, _("Registrations are currently disabled on this network, please try again later."));
44 hdatac->approved++;
45 }
46 }
47
48 static void
49 defcon_useradd(hook_user_nick_t *data)
50 {
51 user_t *u = data->u;
52 kline_t *k;
53
54 if (!u)
55 return;
56
57 if (is_internal_client(u))
58 return;
59
60 if (level == 1)
61 {
62 slog(LG_INFO, "DEFCON:KLINE: %s!%s@%s", u->nick, u->user, u->host);
63 if (! (u->flags & UF_KLINESENT)) {
64 k = kline_add(u->user, u->host, "This network is currently not accepting connections, please try again later.", 900, "*");
65 u->flags |= UF_KLINESENT;
66 }
67 }
68 }
69
70 static void
71 defcon_svsignore(void)
72 {
73 svsignore_t *svsignore;
74 mowgli_node_t *n, *tn;
75
76 if (level <= 2)
77 {
78 MOWGLI_ITER_FOREACH(n, svs_ignore_list.head)
79 {
80 svsignore = (svsignore_t *)n->data;
81
82 if (!strcasecmp(svsignore->mask, "*@*"))
83 return;
84 }
85
86 slog(LG_INFO, "DEFCON:IGNORE:ADD");
87 svsignore = svsignore_add("*@*", "DEFCON Level 1 or 2 activated");
88 svsignore->setby = "DEFCON";
89 svsignore->settime = CURRTIME;
90 }
91 else if (level >= 3)
92 {
93 MOWGLI_ITER_FOREACH_SAFE(n, tn, svs_ignore_list.head)
94 {
95 svsignore = (svsignore_t *)n->data;
96
97 if (!strcasecmp(svsignore->mask,"*@*"))
98 {
99 slog(LG_INFO, "DEFCON:IGNORE:REMOVE");
100 svsignore_delete(svsignore);
101 }
102 }
103 }
104 }
105
106 static void
107 defcon_forcechanmodes(void)
108 {
109 channel_t *chptr;
110 mowgli_patricia_iteration_state_t state;
111 service_t *svs;
112 char modesetbuf[256];
113 char modeunsetbuf[256];
114
115 svs = service_find("operserv");
116
117 if (level <= 3)
118 {
119 snprintf(modesetbuf, sizeof modesetbuf, "+%s", DEFCON_CMODE);
120 slog(LG_INFO, "DEFCON:MODE: %s", modesetbuf);
121 MOWGLI_PATRICIA_FOREACH(chptr, &state, chanlist)
122 {
123 channel_mode_va(svs->me, chptr, 1, modesetbuf);
124 }
125 }
126 else if (level >= 4)
127 {
128 snprintf(modeunsetbuf, sizeof modeunsetbuf, "-%s", DEFCON_CMODE);
129 slog(LG_INFO, "DEFCON:MODE: %s", modeunsetbuf);
130 MOWGLI_PATRICIA_FOREACH(chptr, &state, chanlist)
131 {
132 channel_mode_va(svs->me, chptr, 1, modeunsetbuf);
133 }
134 }
135 }
136
137 static void
138 defcon_timeoutfunc(void *dummy)
139 {
140 service_t *svs;
141 char buf[BUFSIZE];
142
143 svs = service_find("operserv");
144
145 level = 5;
146 defcon_svsignore();
147 defcon_forcechanmodes();
148 slog(LG_INFO, "DEFCON:TIMEOUT");
149
150 snprintf(buf, sizeof buf, "The DEFCON level is now back to normal (\2%d\2). Sorry for any inconvenience this caused.", level);
151 notice_global_sts(svs->me, "*", buf);
152 }
153
154 static void
155 os_cmd_defcon(sourceinfo_t *si, int parc, char *parv[])
156 {
157 char *defcon = parv[0];
158 char buf[BUFSIZE];
159
160 if (!defcon)
161 {
162 command_success_nodata(si, _("Defense condition is currently level \2%d\2."), level);
163 return;
164 }
165
166 level = atoi(defcon);
167
168 if ((level <= 0) || (level > 5))
169 {
170 command_fail(si, fault_badparams, _("Defcon level must be between 1 and 5"));
171 level = 5;
172 return;
173 }
174
175 /* Call the 2 functions that don't use hooks */
176 defcon_svsignore();
177 defcon_forcechanmodes();
178
179 if (level < 5)
180 {
181 snprintf(buf, sizeof buf, "The DEFCON level has been changed to \2%d\2. We apologize for any inconvenience.", level);
182
183 if (defcon_timer == NULL)
184 defcon_timer = mowgli_timer_add_once(base_eventloop, "defcon_timeout", defcon_timeoutfunc, NULL, defcon_timeout);
185 }
186 else
187 {
188 snprintf(buf, sizeof buf, "The DEFCON level is now back to normal (\2%d\2). Sorry for any inconvenience this caused.", level);
189
190 mowgli_timer_destroy(base_eventloop, defcon_timer);
191 defcon_timer = NULL;
192 }
193
194 notice_global_sts(si->service->me, "*", buf);
195 command_success_nodata(si, _("Defense condition set to level \2%d\2."), level);
196 wallops("\2%s\2 set Defense condition to level \2%d\2.", get_oper_name(si), level);
197 logcommand(si, CMDLOG_ADMIN, "DEFCON: \2%d\2", level);
198 }
199
200 static command_t os_defcon = {
201 .name = "DEFCON",
202 .desc = N_("Implements Defense Condition lockdowns."),
203 .access = PRIV_ADMIN,
204 .maxparc = 1,
205 .cmd = &os_cmd_defcon,
206 .help = { .path = "contrib/defcon" },
207 };
208
209 static void
210 mod_init(module_t *const restrict m)
211 {
212 TAINT_ON("Using os_defcon", "Use of os_defcon is unsupported and not recommended. Use only at your own risk.");
213
214 service_named_bind_command("operserv", &os_defcon);
215
216 /* Hooks for all the stuff defcon disables */
217 hook_add_event("user_can_register");
218 hook_add_user_can_register(defcon_nouserreg);
219 hook_add_event("channel_can_register");
220 hook_add_channel_can_register(defcon_nochanreg);
221 hook_add_event("user_add");
222 hook_add_user_add(defcon_useradd);
223
224 service_t *svs;
225 svs = service_find("operserv");
226 add_duration_conf_item("DEFCON_TIMEOUT", &svs->conf_table, 0, &defcon_timeout, "m", 900);
227 }
228
229 static void
230 mod_deinit(const module_unload_intent_t intent)
231 {
232 service_named_unbind_command("operserv", &os_defcon);
233
234 hook_del_user_can_register(defcon_nouserreg);
235 hook_del_channel_can_register(defcon_nochanreg);
236 hook_del_user_add(defcon_useradd);
237
238 service_t *svs;
239 svs = service_find("operserv");
240 del_conf_item("DEFCON_TIMEOUT", &svs->conf_table);
241
242 if (defcon_timer != NULL)
243 mowgli_timer_destroy(base_eventloop, defcon_timer);
244 }
245
246 SIMPLE_DECLARE_MODULE_V1("contrib/os_defcon", MODULE_UNLOAD_CAPABILITY_OK)