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