]> jfr.im git - solanum.git/blob - modules/core/m_ban.c
add help for `chm_regmsg`
[solanum.git] / modules / core / m_ban.c
1 /*
2 * Solanum: a slightly advanced ircd
3 * m_ban.c: Propagates network bans across servers.
4 *
5 * Copyright (C) 2010 Jilles Tjoelker
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1.Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2.Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
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 "send.h"
32 #include "channel.h"
33 #include "client.h"
34 #include "defaults.h"
35 #include "ircd.h"
36 #include "match.h"
37 #include "s_conf.h"
38 #include "s_newconf.h"
39 #include "msg.h"
40 #include "modules.h"
41 #include "hash.h"
42 #include "s_serv.h"
43 #include "operhash.h"
44 #include "reject.h"
45 #include "hostmask.h"
46 #include "logger.h"
47
48 static const char ban_desc[] = "Provides the TS6 BAN command for propagating network-wide bans";
49
50 static void m_ban(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
51 static void ms_ban(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
52
53 struct Message ban_msgtab = {
54 "BAN", 0, 0, 0, 0,
55 {mg_unreg, {m_ban, 0}, {ms_ban, 9}, {ms_ban, 9}, mg_ignore, {m_ban, 0}}
56 };
57
58 mapi_clist_av1 ban_clist[] = { &ban_msgtab, NULL };
59
60 DECLARE_MODULE_AV2(ban, NULL, NULL, ban_clist, NULL, NULL, NULL, NULL, ban_desc);
61
62 static void
63 m_ban(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
64 {
65 sendto_one_notice(source_p, ":The BAN command is not user-accessible.");
66 sendto_one_notice(source_p, ":To ban a user from a channel, see /QUOTE HELP CMODE");
67 if (IsOperGeneral(source_p))
68 sendto_one_notice(source_p, ":To ban a user from a server or from the network, see /QUOTE HELP KLINE");
69 }
70
71 /* ms_ban()
72 *
73 * parv[1] - type
74 * parv[2] - username mask or *
75 * parv[3] - hostname mask
76 * parv[4] - creation TS
77 * parv[5] - duration (relative to creation)
78 * parv[6] - lifetime (relative to creation)
79 * parv[7] - oper or *
80 * parv[8] - reason (possibly with |operreason)
81 */
82 static void
83 ms_ban(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
84 {
85 struct ConfItem *aconf;
86 unsigned int ntype;
87 const char *oper, *stype;
88 time_t now, created, hold, lifetime;
89 char *p;
90 int act;
91 int valid;
92 bool new = false;
93
94 now = rb_current_time();
95 if (strlen(parv[1]) != 1)
96 {
97 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
98 "Unknown BAN type %s from %s",
99 parv[1], source_p->name);
100 return;
101 }
102 switch (parv[1][0])
103 {
104 case 'K':
105 ntype = CONF_KILL;
106 stype = "K-Line";
107 break;
108 case 'X':
109 ntype = CONF_XLINE;
110 stype = "X-Line";
111 break;
112 case 'R':
113 ntype = IsChannelName(parv[3]) ? CONF_RESV_CHANNEL :
114 CONF_RESV_NICK;
115 stype = "RESV";
116 break;
117 default:
118 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
119 "Unknown BAN type %s from %s",
120 parv[1], source_p->name);
121 return;
122 }
123 created = atol(parv[4]);
124 hold = created + atoi(parv[5]);
125 lifetime = created + atoi(parv[6]);
126 if (!strcmp(parv[7], "*"))
127 oper = IsServer(source_p) ? source_p->name : get_oper_name(source_p);
128 else
129 oper = parv[7];
130 aconf = find_prop_ban(ntype, parv[2], parv[3]);
131 if (aconf != NULL)
132 {
133 /* We already know about this ban mask. */
134 if (aconf->created > created ||
135 (aconf->created == created &&
136 aconf->lifetime >= lifetime))
137 {
138 if (IsPerson(source_p))
139 sendto_one_notice(source_p,
140 ":Your %s [%s%s%s] has been superseded",
141 stype,
142 aconf->user ? aconf->user : "",
143 aconf->user ? "@" : "",
144 aconf->host);
145 return;
146 }
147 /* act indicates if something happened (from the oper's
148 * point of view). This is the case if the ban was
149 * previously active (not deleted) or if the new ban
150 * is not a removal and not already expired.
151 */
152 act = !(aconf->status & CONF_ILLEGAL) || (hold != created &&
153 hold > now);
154 if (lifetime > aconf->lifetime)
155 aconf->lifetime = lifetime;
156 /* already expired, hmm */
157 if (aconf->lifetime <= now)
158 return;
159 /* Deactivate, it will be reactivated later if appropriate. */
160 deactivate_conf(aconf, now);
161 rb_free(aconf->user);
162 aconf->user = NULL;
163 rb_free(aconf->host);
164 aconf->host = NULL;
165 operhash_delete(aconf->info.oper);
166 aconf->info.oper = NULL;
167 rb_free(aconf->passwd);
168 aconf->passwd = NULL;
169 rb_free(aconf->spasswd);
170 aconf->spasswd = NULL;
171 }
172 else
173 {
174 /* New ban mask. */
175 aconf = make_conf();
176 aconf->status = CONF_ILLEGAL | ntype;
177 aconf->lifetime = lifetime;
178 act = hold != created && hold > now;
179 new = true;
180 }
181 aconf->flags &= ~CONF_FLAGS_MYOPER;
182 aconf->flags |= CONF_FLAGS_TEMPORARY;
183 aconf->user = ntype == CONF_KILL ? rb_strdup(parv[2]) : NULL;
184 aconf->host = rb_strdup(parv[3]);
185 aconf->info.oper = operhash_add(oper);
186 aconf->created = created;
187 aconf->hold = hold;
188 if (new)
189 add_prop_ban(aconf);
190 if (ntype != CONF_KILL || (p = strchr(parv[parc - 1], '|')) == NULL)
191 aconf->passwd = rb_strdup(parv[parc - 1]);
192 else
193 {
194 aconf->passwd = rb_strndup(parv[parc - 1], p - parv[parc - 1] + 1);
195 aconf->spasswd = rb_strdup(p + 1);
196 }
197 /* The ban is fully filled in and in the prop_bans list
198 * but still deactivated. Now determine if it should be activated
199 * and send the server notices.
200 */
201 /* We only reject *@* and the like here.
202 * Otherwise malformed bans are fairly harmless and can be removed.
203 */
204 switch (ntype)
205 {
206 case CONF_KILL:
207 valid = valid_wild_card(aconf->user, aconf->host);
208 break;
209 case CONF_RESV_CHANNEL:
210 valid = 1;
211 break;
212 default:
213 valid = valid_wild_card_simple(aconf->host);
214 break;
215 }
216 if (act && hold != created && !valid)
217 {
218 sendto_realops_snomask(SNO_GENERAL, L_ALL,
219 "Ignoring global %d min. %s from %s%s%s for [%s%s%s]: too few non-wildcard characters",
220 (int)((hold - now) / 60),
221 stype,
222 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
223 strcmp(parv[7], "*") ? " on behalf of " : "",
224 strcmp(parv[7], "*") ? parv[7] : "",
225 aconf->user ? aconf->user : "",
226 aconf->user ? "@" : "",
227 aconf->host);
228 if(IsPerson(source_p))
229 sendto_one_notice(source_p,
230 ":Your %s [%s%s%s] has too few non-wildcard characters",
231 stype,
232 aconf->user ? aconf->user : "",
233 aconf->user ? "@" : "",
234 aconf->host);
235 /* Propagate it, but do not apply it locally. */
236 }
237 else if (act && hold != created)
238 {
239 /* Keep the notices in sync with modules/m_kline.c etc. */
240 sendto_realops_snomask(SNO_GENERAL, L_ALL,
241 "%s added global %d min. %s%s%s for [%s%s%s] [%s]",
242 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
243 (int)((hold - now) / 60),
244 stype,
245 strcmp(parv[7], "*") ? " from " : "",
246 strcmp(parv[7], "*") ? parv[7] : "",
247 aconf->user ? aconf->user : "",
248 aconf->user ? "@" : "",
249 aconf->host,
250 parv[parc - 1]);
251 ilog(L_KLINE, "%s %s %d %s%s%s %s", parv[1],
252 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
253 (int)((hold - now) / 60),
254 aconf->user ? aconf->user : "",
255 aconf->user ? " " : "",
256 aconf->host,
257 parv[parc - 1]);
258 aconf->status &= ~CONF_ILLEGAL;
259 }
260 else if (act)
261 {
262 sendto_realops_snomask(SNO_GENERAL, L_ALL,
263 "%s has removed the global %s for: [%s%s%s]%s%s",
264 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
265 stype,
266 aconf->user ? aconf->user : "",
267 aconf->user ? "@" : "",
268 aconf->host,
269 strcmp(parv[7], "*") ? " on behalf of " : "",
270 strcmp(parv[7], "*") ? parv[7] : "");
271 ilog(L_KLINE, "U%s %s %s%s %s", parv[1],
272 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
273 aconf->user ? aconf->user : "",
274 aconf->user ? " " : "",
275 aconf->host);
276 }
277 /* If CONF_ILLEGAL is still set at this point, remove entries from the
278 * reject cache (for klines and xlines).
279 * If CONF_ILLEGAL is not set, add the ban to the type-specific data
280 * structure and take action on matched clients/channels.
281 */
282 switch (ntype)
283 {
284 case CONF_KILL:
285 if (aconf->status & CONF_ILLEGAL)
286 remove_reject_mask(aconf->user, aconf->host);
287 else
288 {
289 add_conf_by_address(aconf->host, CONF_KILL, aconf->user, NULL, aconf);
290 check_one_kline(aconf);
291 }
292 break;
293 case CONF_XLINE:
294 if (aconf->status & CONF_ILLEGAL)
295 remove_reject_mask(aconf->host, NULL);
296 else
297 {
298 rb_dlinkAddAlloc(aconf, &xline_conf_list);
299 check_xlines();
300 }
301 break;
302 case CONF_RESV_CHANNEL:
303 if (!(aconf->status & CONF_ILLEGAL))
304 {
305 add_to_resv_hash(aconf->host, aconf);
306 resv_chan_forcepart(aconf->host, aconf->passwd, hold - now);
307 }
308 break;
309 case CONF_RESV_NICK:
310 if (!(aconf->status & CONF_ILLEGAL))
311 rb_dlinkAddAlloc(aconf, &resv_conf_list);
312 break;
313 }
314 sendto_server(client_p, NULL, CAP_BAN|CAP_TS6, NOCAPS,
315 ":%s BAN %s %s %s %s %s %s %s :%s",
316 source_p->id,
317 parv[1],
318 parv[2],
319 parv[3],
320 parv[4],
321 parv[5],
322 parv[6],
323 parv[7],
324 parv[parc - 1]);
325 }