]> jfr.im git - solanum.git/blame - modules/m_dline.c
Move m_locops module to extensions.
[solanum.git] / modules / m_dline.c
CommitLineData
212380e3
AC
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_dline.c: Bans/unbans a user.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
212380e3
AC
23 */
24
25#include "stdinc.h"
212380e3
AC
26#include "channel.h"
27#include "class.h"
28#include "client.h"
4562c604 29#include "match.h"
212380e3
AC
30#include "ircd.h"
31#include "hostmask.h"
32#include "numeric.h"
212380e3
AC
33#include "s_conf.h"
34#include "s_newconf.h"
4016731b 35#include "logger.h"
212380e3
AC
36#include "send.h"
37#include "hash.h"
38#include "s_serv.h"
39#include "msg.h"
40#include "parse.h"
41#include "modules.h"
8bbeb278 42#include "bandbi.h"
27f616dd 43#include "operhash.h"
212380e3 44
eeabf33a
EM
45static const char dline_desc[] = "Provides the DLINE facility to ban users via IP address";
46
3c7d6fcc
EM
47static void mo_dline(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
48static void me_dline(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
49static void mo_undline(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
50static void me_undline(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
212380e3
AC
51
52struct Message dline_msgtab = {
7baa37a9 53 "DLINE", 0, 0, 0, 0,
b647efa0 54 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_dline, 4}, {mo_dline, 2}}
212380e3 55};
8bbeb278 56
212380e3 57struct Message undline_msgtab = {
7baa37a9 58 "UNDLINE", 0, 0, 0, 0,
b647efa0 59 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, {me_undline, 2}, {mo_undline, 2}}
212380e3
AC
60};
61
62mapi_clist_av1 dline_clist[] = { &dline_msgtab, &undline_msgtab, NULL };
8bbeb278 63
9fd8e7cb 64DECLARE_MODULE_AV2(dline, NULL, NULL, dline_clist, NULL, NULL, NULL, NULL, dline_desc);
212380e3 65
3c7d6fcc
EM
66static bool remove_temp_dline(struct ConfItem *);
67static void apply_dline(struct Client *, const char *, int, char *);
68static void apply_undline(struct Client *, const char *);
212380e3
AC
69
70/* mo_dline()
55abcbb2 71 *
212380e3
AC
72 * parv[1] - dline to add
73 * parv[2] - reason
74 */
3c7d6fcc 75static void
428ca87b 76mo_dline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
77{
78 char def[] = "No Reason";
79 const char *dlhost;
212380e3 80 char *reason = def;
212380e3 81 char cidr_form_host[HOSTLEN + 1];
212380e3 82 int tdline_time = 0;
f5cb68d5 83 const char *target_server = NULL;
212380e3
AC
84 int loc = 1;
85
86 if(!IsOperK(source_p))
87 {
8bbeb278 88 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "kline");
3c7d6fcc 89 return;
212380e3
AC
90 }
91
92 if((tdline_time = valid_temp_time(parv[loc])) >= 0)
93 loc++;
94
212380e3 95 dlhost = parv[loc];
f427c8b0 96 rb_strlcpy(cidr_form_host, dlhost, sizeof(cidr_form_host));
f5cb68d5
VY
97 loc++;
98
2b843a5b
JT
99 /* would break the protocol */
100 if (*dlhost == ':')
101 {
102 sendto_one_notice(source_p, ":Invalid D-Line");
3c7d6fcc 103 return;
2b843a5b
JT
104 }
105
8bbeb278
AC
106 if(parc >= loc + 2 && !irccmp(parv[loc], "ON"))
107 {
2cfb7214
JT
108 if(!IsOperRemoteBan(source_p))
109 {
110 sendto_one(source_p, form_str(ERR_NOPRIVS),
8bbeb278 111 me.name, source_p->name, "remoteban");
3c7d6fcc 112 return;
2cfb7214
JT
113 }
114
8bbeb278 115 target_server = parv[loc + 1];
f5cb68d5
VY
116 loc += 2;
117 }
118
119 if(parc >= loc + 1 && !EmptyString(parv[loc]))
120 reason = LOCAL_COPY(parv[loc]);
121
122 if(target_server != NULL)
212380e3 123 {
f5cb68d5 124 sendto_match_servs(source_p, target_server,
8bbeb278
AC
125 CAP_ENCAP, NOCAPS,
126 "ENCAP %s DLINE %d %s :%s",
127 target_server, tdline_time, dlhost, reason);
f5cb68d5
VY
128
129 if(!match(target_server, me.name))
3c7d6fcc 130 return;
212380e3
AC
131 }
132
f5cb68d5
VY
133 apply_dline(source_p, dlhost, tdline_time, reason);
134
135 check_dlines();
f5cb68d5 136}
212380e3 137
f5cb68d5
VY
138/* mo_undline()
139 *
140 * parv[1] = dline to remove
141 */
3c7d6fcc 142static void
428ca87b 143mo_undline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
f5cb68d5
VY
144{
145 const char *cidr;
146 const char *target_server = NULL;
147
148 if(!IsOperK(source_p))
149 {
8bbeb278 150 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "unkline");
3c7d6fcc 151 return;
8bbeb278
AC
152 }
153
f5cb68d5
VY
154 cidr = parv[1];
155
156 if(parc >= 4 && !irccmp(parv[2], "ON"))
212380e3 157 {
2cfb7214
JT
158 if(!IsOperRemoteBan(source_p))
159 {
160 sendto_one(source_p, form_str(ERR_NOPRIVS),
8bbeb278 161 me.name, source_p->name, "remoteban");
3c7d6fcc 162 return;
2cfb7214
JT
163 }
164
f5cb68d5
VY
165 target_server = parv[3];
166 sendto_match_servs(source_p, target_server,
8bbeb278 167 CAP_ENCAP, NOCAPS, "ENCAP %s UNDLINE %s", target_server, cidr);
212380e3 168
f5cb68d5 169 if(!match(target_server, me.name))
3c7d6fcc 170 return;
212380e3
AC
171 }
172
f5cb68d5 173 apply_undline(source_p, cidr);
f5cb68d5
VY
174}
175
3c7d6fcc 176static void
428ca87b 177me_dline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
5f7a6a3d
JT
178{
179 int tdline_time = atoi(parv[1]);
180 /* Since this is coming over a server link, assume that the originating
181 * server did the relevant permission/sanity checks...
182 */
183
184 if(!IsPerson(source_p))
3c7d6fcc 185 return;
5f7a6a3d
JT
186
187 if(!find_shared_conf(source_p->username, source_p->host,
8bbeb278
AC
188 source_p->servptr->name,
189 tdline_time > 0 ? SHARED_TDLINE : SHARED_PDLINE))
3c7d6fcc 190 return;
5f7a6a3d
JT
191
192 apply_dline(source_p, parv[2], tdline_time, LOCAL_COPY(parv[3]));
193
194 check_dlines();
5f7a6a3d
JT
195}
196
3c7d6fcc 197static void
428ca87b 198me_undline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
5f7a6a3d
JT
199{
200 if(!IsPerson(source_p))
3c7d6fcc 201 return;
5f7a6a3d
JT
202
203 if(!find_shared_conf(source_p->username, source_p->host,
8bbeb278 204 source_p->servptr->name, SHARED_UNDLINE))
3c7d6fcc 205 return;
5f7a6a3d
JT
206
207 apply_undline(source_p, parv[1]);
f5cb68d5
VY
208}
209
3c7d6fcc 210static void
f5cb68d5
VY
211apply_dline(struct Client *source_p, const char *dlhost, int tdline_time, char *reason)
212{
213 struct ConfItem *aconf;
214 char *oper_reason;
f5cb68d5
VY
215 struct rb_sockaddr_storage daddr;
216 int t = AF_INET, ty, b;
217 const char *creason;
218
29c92cf9 219 ty = parse_netmask(dlhost, &daddr, &b);
f5cb68d5
VY
220 if(ty == HM_HOST)
221 {
8bbeb278 222 sendto_one(source_p, ":%s NOTICE %s :Invalid D-Line", me.name, source_p->name);
3c7d6fcc 223 return;
f5cb68d5
VY
224 }
225#ifdef RB_IPV6
226 if(ty == HM_IPV6)
227 t = AF_INET6;
228 else
229#endif
230 t = AF_INET;
231
232 /* This means dlines wider than /16 cannot be set remotely */
212380e3
AC
233 if(IsOperAdmin(source_p))
234 {
f5cb68d5 235 if(b < 8)
212380e3 236 {
f5cb68d5 237 sendto_one_notice(source_p,
8bbeb278 238 ":For safety, bitmasks less than 8 require conf access.");
3c7d6fcc 239 return;
212380e3
AC
240 }
241 }
242 else
243 {
f5cb68d5 244 if(b < 16)
212380e3 245 {
f5cb68d5 246 sendto_one_notice(source_p,
8bbeb278 247 ":Dline bitmasks less than 16 are for admins only.");
3c7d6fcc 248 return;
212380e3
AC
249 }
250 }
251
54ac8b60
VY
252 if(ConfigFileEntry.non_redundant_klines)
253 {
8bbeb278 254 if((aconf = find_dline((struct sockaddr *) &daddr, t)) != NULL)
54ac8b60
VY
255 {
256 int bx;
257 parse_netmask(aconf->host, NULL, &bx);
258 if(b >= bx)
259 {
260 creason = aconf->passwd ? aconf->passwd : "<No Reason>";
261 if(IsConfExemptKline(aconf))
262 sendto_one(source_p,
263 ":%s NOTICE %s :[%s] is (E)d-lined by [%s] - %s",
8bbeb278
AC
264 me.name, source_p->name, dlhost, aconf->host,
265 creason);
54ac8b60
VY
266 else
267 sendto_one(source_p,
268 ":%s NOTICE %s :[%s] already D-lined by [%s] - %s",
8bbeb278
AC
269 me.name, source_p->name, dlhost, aconf->host,
270 creason);
3c7d6fcc 271 return;
54ac8b60
VY
272 }
273 }
212380e3
AC
274 }
275
7df5fcfb 276 rb_set_time();
212380e3
AC
277
278 aconf = make_conf();
279 aconf->status = CONF_DLINE;
b52c2949 280 aconf->created = rb_current_time();
47a03750 281 aconf->host = rb_strdup(dlhost);
a12ad044 282 aconf->passwd = rb_strdup(reason);
27f616dd 283 aconf->info.oper = operhash_add(get_oper_name(source_p));
212380e3 284
9319a2e2
KB
285 if(strlen(reason) > BANREASONLEN)
286 reason[BANREASONLEN] = '\0';
287
08e8aa7a
VY
288 /* Look for an oper reason */
289 if((oper_reason = strchr(reason, '|')) != NULL)
290 {
291 *oper_reason = '\0';
292 oper_reason++;
293
294 if(!EmptyString(oper_reason))
295 aconf->spasswd = rb_strdup(oper_reason);
296 }
297
212380e3
AC
298 if(tdline_time > 0)
299 {
e3354945 300 aconf->hold = rb_current_time() + tdline_time;
212380e3
AC
301 add_temp_dline(aconf);
302
303 if(EmptyString(oper_reason))
304 {
61c096aa 305 sendto_realops_snomask(SNO_GENERAL, L_ALL,
8bbeb278
AC
306 "%s added temporary %d min. D-Line for [%s] [%s]",
307 get_oper_name(source_p), tdline_time / 60,
308 aconf->host, reason);
212380e3 309 ilog(L_KLINE, "D %s %d %s %s",
8bbeb278 310 get_oper_name(source_p), tdline_time / 60, aconf->host, reason);
212380e3
AC
311 }
312 else
313 {
61c096aa 314 sendto_realops_snomask(SNO_GENERAL, L_ALL,
8bbeb278
AC
315 "%s added temporary %d min. D-Line for [%s] [%s|%s]",
316 get_oper_name(source_p), tdline_time / 60,
317 aconf->host, reason, oper_reason);
212380e3 318 ilog(L_KLINE, "D %s %d %s %s|%s",
8bbeb278
AC
319 get_oper_name(source_p), tdline_time / 60,
320 aconf->host, reason, oper_reason);
212380e3
AC
321 }
322
323 sendto_one(source_p, ":%s NOTICE %s :Added temporary %d min. D-Line for [%s]",
324 me.name, source_p->name, tdline_time / 60, aconf->host);
325 }
326 else
327 {
40c1fd47 328 add_conf_by_address(aconf->host, CONF_DLINE, NULL, NULL, aconf);
8bbeb278
AC
329
330 bandb_add(BANDB_DLINE, source_p, aconf->host, NULL,
331 reason, EmptyString(aconf->spasswd) ? NULL : aconf->spasswd, 0);
9964e935
AC
332
333 if(EmptyString(oper_reason))
334 {
335 sendto_realops_snomask(SNO_GENERAL, L_ALL,
336 "%s added D-Line for [%s] [%s]",
337 get_oper_name(source_p), aconf->host, reason);
338 ilog(L_KLINE, "D %s 0 %s %s",
339 get_oper_name(source_p), aconf->host, reason);
340 }
341 else
342 {
343 sendto_realops_snomask(SNO_GENERAL, L_ALL,
344 "%s added D-Line for [%s] [%s|%s]",
345 get_oper_name(source_p), aconf->host, reason, oper_reason);
346 ilog(L_KLINE, "D %s 0 %s %s|%s",
347 get_oper_name(source_p),
348 aconf->host, reason, oper_reason);
349 }
212380e3 350 }
212380e3
AC
351}
352
3c7d6fcc 353static void
f5cb68d5 354apply_undline(struct Client *source_p, const char *cidr)
212380e3 355{
8bbeb278 356 char buf[BUFSIZE];
f5cb68d5 357 struct ConfItem *aconf;
212380e3
AC
358
359 if(parse_netmask(cidr, NULL, NULL) == HM_HOST)
360 {
5366977b 361 sendto_one_notice(source_p, ":Invalid D-Line");
3c7d6fcc 362 return;
212380e3
AC
363 }
364
6f3a09ff
JT
365 aconf = find_exact_conf_by_address(cidr, CONF_DLINE, NULL);
366 if(aconf == NULL)
367 {
368 sendto_one_notice(source_p, ":No D-Line for %s", cidr);
3c7d6fcc 369 return;
6f3a09ff
JT
370 }
371
f427c8b0 372 rb_strlcpy(buf, aconf->host, sizeof buf);
6f3a09ff 373 if(remove_temp_dline(aconf))
212380e3
AC
374 {
375 sendto_one(source_p,
376 ":%s NOTICE %s :Un-dlined [%s] from temporary D-lines",
f5cb68d5 377 me.name, source_p->name, buf);
212380e3 378 sendto_realops_snomask(SNO_GENERAL, L_ALL,
8bbeb278
AC
379 "%s has removed the temporary D-Line for: [%s]",
380 get_oper_name(source_p), buf);
6f3a09ff 381 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), buf);
3c7d6fcc 382 return;
212380e3
AC
383 }
384
8bbeb278 385 bandb_del(BANDB_DLINE, aconf->host, NULL);
212380e3 386
8bbeb278
AC
387 sendto_one(source_p, ":%s NOTICE %s :D-Line for [%s] is removed", me.name, source_p->name,
388 aconf->host);
389 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s has removed the D-Line for: [%s]",
390 get_oper_name(source_p), aconf->host);
37f6cc05
JT
391 ilog(L_KLINE, "UD %s %s", get_oper_name(source_p), aconf->host);
392 delete_one_address_conf(aconf->host, aconf);
212380e3
AC
393}
394
212380e3
AC
395/* remove_temp_dline()
396 *
6f3a09ff 397 * inputs - confitem to undline
3c7d6fcc 398 * outputs - true if removed, false otherwise.
212380e3
AC
399 * side effects - tries to undline anything that matches
400 */
3c7d6fcc 401static bool
6f3a09ff 402remove_temp_dline(struct ConfItem *aconf)
212380e3 403{
5b96d9a6 404 rb_dlink_node *ptr;
212380e3
AC
405 int i;
406
8bbeb278 407 for(i = 0; i < LAST_TEMP_TYPE; i++)
212380e3 408 {
5b96d9a6 409 RB_DLINK_FOREACH(ptr, temp_dlines[i].head)
212380e3 410 {
8bbeb278 411 if(aconf == ptr->data)
212380e3 412 {
555ac41f 413 rb_dlinkDestroy(ptr, &temp_dlines[i]);
212380e3 414 delete_one_address_conf(aconf->host, aconf);
3c7d6fcc 415 return true;
212380e3
AC
416 }
417 }
418 }
419
3c7d6fcc 420 return false;
212380e3 421}