]> jfr.im git - solanum.git/blob - modules/core/m_ban.c
Add propagated xlines, like klines.
[solanum.git] / modules / core / m_ban.c
1 /*
2 * charybdis: An 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 "client.h"
33 #include "common.h"
34 #include "config.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
47 static int ms_ban(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
48
49 struct Message ban_msgtab = {
50 "BAN", 0, 0, 0, MFLG_SLOW,
51 {mg_unreg, mg_ignore, {ms_ban, 9}, {ms_ban, 9}, mg_ignore, mg_ignore}
52 };
53
54 mapi_clist_av1 ban_clist[] = { &ban_msgtab, NULL };
55 DECLARE_MODULE_AV1(ban, NULL, NULL, ban_clist, NULL, NULL, "$Revision: 1349 $");
56
57 /* ms_ban()
58 *
59 * parv[1] - type
60 * parv[2] - username mask or *
61 * parv[3] - hostname mask
62 * parv[4] - creation TS
63 * parv[5] - duration (relative to creation)
64 * parv[6] - lifetime (relative to creation)
65 * parv[7] - oper or *
66 * parv[8] - reason (possibly with |operreason)
67 */
68 static int
69 ms_ban(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
70 {
71 rb_dlink_node *ptr;
72 struct ConfItem *aconf;
73 unsigned int ntype;
74 const char *oper, *stype;
75 time_t created, hold, lifetime;
76 char *p;
77 int act;
78
79 if (strlen(parv[1]) != 1)
80 {
81 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
82 "Unknown BAN type %s from %s",
83 parv[1], source_p->name);
84 return 0;
85 }
86 switch (parv[1][0])
87 {
88 case 'K':
89 ntype = CONF_KILL;
90 stype = "K-Line";
91 break;
92 case 'X':
93 ntype = CONF_XLINE;
94 stype = "X-Line";
95 break;
96 default:
97 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE,
98 "Unknown BAN type %s from %s",
99 parv[1], source_p->name);
100 return 0;
101 }
102 created = atol(parv[4]);
103 hold = created + atoi(parv[5]);
104 lifetime = created + atoi(parv[6]);
105 if (!strcmp(parv[7], "*"))
106 oper = IsServer(source_p) ? source_p->name : get_oper_name(source_p);
107 else
108 oper = parv[7];
109 ptr = find_prop_ban(ntype, parv[2], parv[3]);
110 if (ptr != NULL)
111 {
112 aconf = ptr->data;
113 if (aconf->created > created ||
114 (aconf->created == created &&
115 aconf->lifetime >= lifetime))
116 {
117 if (IsPerson(source_p))
118 sendto_one_notice(source_p,
119 ":Your %s [%s%s%s] has been superseded",
120 stype,
121 aconf->user ? aconf->user : "",
122 aconf->user ? "@" : "",
123 aconf->host);
124 return 0;
125 }
126 act = !(aconf->status & CONF_ILLEGAL) || (hold != created &&
127 hold > rb_current_time());
128 if (lifetime > aconf->lifetime)
129 aconf->lifetime = lifetime;
130 /* already expired, hmm */
131 if (aconf->lifetime <= rb_current_time())
132 return 0;
133 deactivate_conf(aconf, ptr);
134 rb_free(aconf->user);
135 aconf->user = NULL;
136 rb_free(aconf->host);
137 aconf->host = NULL;
138 operhash_delete(aconf->info.oper);
139 aconf->info.oper = NULL;
140 rb_free(aconf->passwd);
141 aconf->passwd = NULL;
142 rb_free(aconf->spasswd);
143 aconf->spasswd = NULL;
144 }
145 else
146 {
147 aconf = make_conf();
148 aconf->status = CONF_ILLEGAL | ntype;
149 aconf->lifetime = lifetime;
150 rb_dlinkAddAlloc(aconf, &prop_bans);
151 act = hold != created && hold > rb_current_time();
152 }
153 aconf->flags &= ~CONF_FLAGS_MYOPER;
154 aconf->flags |= CONF_FLAGS_TEMPORARY;
155 aconf->user = ntype == CONF_KILL ? rb_strdup(parv[2]) : NULL;
156 aconf->host = rb_strdup(parv[3]);
157 aconf->info.oper = operhash_add(oper);
158 aconf->created = created;
159 aconf->hold = hold;
160 p = strchr(parv[parc - 1], '|');
161 if (p == NULL)
162 aconf->passwd = rb_strdup(parv[parc - 1]);
163 else
164 {
165 aconf->passwd = rb_strndup(parv[parc - 1], p - parv[parc - 1] + 1);
166 aconf->spasswd = rb_strdup(p + 1);
167 }
168 if (act && hold != created &&
169 !(ntype == CONF_KILL ?
170 valid_wild_card(aconf->user, aconf->host) :
171 valid_wild_card_simple(aconf->host)))
172 {
173 sendto_realops_snomask(SNO_GENERAL, L_ALL,
174 "Ignoring global %d min. %s from %s%s%s for [%s%s%s]: too few non-wildcard characters",
175 (hold - rb_current_time()) / 60,
176 stype,
177 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
178 strcmp(parv[7], "*") ? " on behalf of " : "",
179 strcmp(parv[7], "*") ? parv[7] : "",
180 aconf->user ? aconf->user : "",
181 aconf->user ? "@" : "",
182 aconf->host);
183 if(IsPerson(source_p))
184 sendto_one_notice(source_p,
185 ":Your %s [%s%s%s] has too few non-wildcard characters",
186 stype,
187 aconf->user ? aconf->user : "",
188 aconf->user ? "@" : "",
189 aconf->host);
190 /* Propagate it, but do not apply it locally. */
191 }
192 else if (act && hold != created)
193 {
194 /* Keep the notices in sync with modules/m_kline.c etc. */
195 sendto_realops_snomask(SNO_GENERAL, L_ALL,
196 "%s added global %d min. %s%s%s for [%s%s%s] [%s]",
197 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
198 (hold - rb_current_time()) / 60,
199 stype,
200 strcmp(parv[7], "*") ? " from " : "",
201 strcmp(parv[7], "*") ? parv[7] : "",
202 aconf->user ? aconf->user : "",
203 aconf->user ? "@" : "",
204 aconf->host,
205 parv[parc - 1]);
206 ilog(L_KLINE, "%s %s %d %s%s%s %s", parv[1],
207 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
208 (hold - rb_current_time()) / 60,
209 aconf->user ? aconf->user : "",
210 aconf->user ? " " : "",
211 aconf->host,
212 parv[parc - 1]);
213 aconf->status &= ~CONF_ILLEGAL;
214 }
215 else if (act)
216 {
217 sendto_realops_snomask(SNO_GENERAL, L_ALL,
218 "%s has removed the global %s for: [%s%s%s]%s%s",
219 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
220 stype,
221 aconf->user ? aconf->user : "",
222 aconf->user ? "@" : "",
223 aconf->host,
224 strcmp(parv[7], "*") ? " on behalf of " : "",
225 strcmp(parv[7], "*") ? parv[7] : "");
226 ilog(L_KLINE, "U%s %s %s%s %s", parv[1],
227 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
228 aconf->user ? aconf->user : "",
229 aconf->user ? " " : "",
230 aconf->host);
231 }
232 switch (ntype)
233 {
234 case CONF_KILL:
235 if (aconf->status & CONF_ILLEGAL)
236 remove_reject_mask(aconf->user, aconf->host);
237 else
238 {
239 add_conf_by_address(aconf->host, CONF_KILL, aconf->user, NULL, aconf);
240 if(ConfigFileEntry.kline_delay ||
241 (IsServer(source_p) &&
242 !HasSentEob(source_p)))
243 {
244 if(kline_queued == 0)
245 {
246 rb_event_addonce("check_klines", check_klines_event, NULL,
247 ConfigFileEntry.kline_delay);
248 kline_queued = 1;
249 }
250 }
251 else
252 check_klines();
253 }
254 break;
255 case CONF_XLINE:
256 if (aconf->status & CONF_ILLEGAL)
257 remove_reject_mask(aconf->host, NULL);
258 else
259 {
260 rb_dlinkAddAlloc(aconf, &xline_conf_list);
261 check_xlines();
262 }
263 break;
264 }
265 sendto_server(client_p, NULL, CAP_BAN|CAP_TS6, NOCAPS,
266 ":%s BAN %s %s %s %s %s %s %s :%s",
267 source_p->id,
268 parv[1],
269 parv[2],
270 parv[3],
271 parv[4],
272 parv[5],
273 parv[6],
274 parv[7],
275 parv[parc - 1]);
276 return 0;
277 }