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