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