1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2016 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file m_dline.c |
23 |
* \brief Includes required functions for processing the DLINE command. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "client.h" |
30 |
#include "irc_string.h" |
31 |
#include "conf.h" |
32 |
#include "conf_cluster.h" |
33 |
#include "conf_shared.h" |
34 |
#include "ircd.h" |
35 |
#include "hostmask.h" |
36 |
#include "numeric.h" |
37 |
#include "log.h" |
38 |
#include "misc.h" |
39 |
#include "send.h" |
40 |
#include "server.h" |
41 |
#include "parse.h" |
42 |
#include "modules.h" |
43 |
#include "memory.h" |
44 |
|
45 |
|
46 |
static void |
47 |
dline_check(const struct AddressRec *arec) |
48 |
{ |
49 |
dlink_node *node = NULL, *node_next = NULL; |
50 |
|
51 |
DLINK_FOREACH_SAFE(node, node_next, local_client_list.head) |
52 |
{ |
53 |
struct Client *client_p = node->data; |
54 |
|
55 |
if (IsDead(client_p)) |
56 |
continue; |
57 |
|
58 |
switch (arec->masktype) |
59 |
{ |
60 |
case HM_IPV4: |
61 |
if (client_p->connection->aftype == AF_INET) |
62 |
if (match_ipv4(&client_p->connection->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits)) |
63 |
conf_try_ban(client_p, CLIENT_BAN_DLINE, arec->conf->reason); |
64 |
break; |
65 |
case HM_IPV6: |
66 |
if (client_p->connection->aftype == AF_INET6) |
67 |
if (match_ipv6(&client_p->connection->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits)) |
68 |
conf_try_ban(client_p, CLIENT_BAN_DLINE, arec->conf->reason); |
69 |
break; |
70 |
default: break; |
71 |
} |
72 |
} |
73 |
|
74 |
DLINK_FOREACH_SAFE(node, node_next, unknown_list.head) |
75 |
{ |
76 |
struct Client *client_p = node->data; |
77 |
|
78 |
if (IsDead(client_p)) |
79 |
continue; |
80 |
|
81 |
switch (arec->masktype) |
82 |
{ |
83 |
case HM_IPV4: |
84 |
if (client_p->connection->aftype == AF_INET) |
85 |
if (match_ipv4(&client_p->connection->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits)) |
86 |
conf_try_ban(client_p, CLIENT_BAN_DLINE, arec->conf->reason); |
87 |
break; |
88 |
case HM_IPV6: |
89 |
if (client_p->connection->aftype == AF_INET6) |
90 |
if (match_ipv6(&client_p->connection->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits)) |
91 |
conf_try_ban(client_p, CLIENT_BAN_DLINE, arec->conf->reason); |
92 |
break; |
93 |
default: break; |
94 |
} |
95 |
} |
96 |
} |
97 |
|
98 |
/* dline_add() |
99 |
* |
100 |
* inputs - |
101 |
* output - NONE |
102 |
* side effects - dline as given is placed |
103 |
*/ |
104 |
static void |
105 |
dline_handle(struct Client *source_p, const char *addr, const char *reason, |
106 |
uintmax_t duration) |
107 |
{ |
108 |
char buf[IRCD_BUFSIZE]; |
109 |
struct irc_ssaddr daddr; |
110 |
int bits = 0, aftype = 0; |
111 |
struct MaskItem *conf = NULL; |
112 |
|
113 |
if (!HasFlag(source_p, FLAGS_SERVICE) && !valid_wild_card(1, addr)) |
114 |
{ |
115 |
sendto_one_notice(source_p, &me, |
116 |
":Please include at least %u non-wildcard characters with the mask", |
117 |
ConfigGeneral.min_nonwildcard); |
118 |
return; |
119 |
} |
120 |
|
121 |
switch (parse_netmask(addr, &daddr, &bits)) |
122 |
{ |
123 |
case HM_IPV4: |
124 |
if (!HasFlag(source_p, FLAGS_SERVICE) && (unsigned int)bits < ConfigGeneral.dline_min_cidr) |
125 |
{ |
126 |
if (IsClient(source_p)) |
127 |
sendto_one_notice(source_p, &me, ":For safety, bitmasks less than %u require conf access.", |
128 |
ConfigGeneral.dline_min_cidr); |
129 |
return; |
130 |
} |
131 |
|
132 |
aftype = AF_INET; |
133 |
break; |
134 |
case HM_IPV6: |
135 |
if (!HasFlag(source_p, FLAGS_SERVICE) && (unsigned int)bits < ConfigGeneral.dline_min_cidr6) |
136 |
{ |
137 |
if (IsClient(source_p)) |
138 |
sendto_one_notice(source_p, &me, ":For safety, bitmasks less than %u require conf access.", |
139 |
ConfigGeneral.dline_min_cidr6); |
140 |
return; |
141 |
} |
142 |
|
143 |
aftype = AF_INET6; |
144 |
break; |
145 |
default: /* HM_HOST */ |
146 |
return; |
147 |
} |
148 |
|
149 |
if ((conf = find_conf_by_address(NULL, &daddr, CONF_DLINE, aftype, NULL, NULL, 1))) |
150 |
{ |
151 |
if (IsClient(source_p)) |
152 |
sendto_one_notice(source_p, &me, ":[%s] already D-lined by [%s] - %s", |
153 |
addr, conf->host, conf->reason); |
154 |
return; |
155 |
} |
156 |
|
157 |
if (duration) |
158 |
snprintf(buf, sizeof(buf), "Temporary D-line %ju min. - %.*s (%s)", |
159 |
duration / 60, REASONLEN, reason, date_iso8601(0)); |
160 |
else |
161 |
snprintf(buf, sizeof(buf), "%.*s (%s)", REASONLEN, reason, date_iso8601(0)); |
162 |
|
163 |
conf = conf_make(CONF_DLINE); |
164 |
conf->host = xstrdup(addr); |
165 |
conf->reason = xstrdup(buf); |
166 |
conf->setat = CurrentTime; |
167 |
SetConfDatabase(conf); |
168 |
|
169 |
if (duration) |
170 |
{ |
171 |
conf->until = CurrentTime + duration; |
172 |
|
173 |
if (IsClient(source_p)) |
174 |
sendto_one_notice(source_p, &me, ":Added temporary %ju min. D-Line [%s]", |
175 |
duration / 60, conf->host); |
176 |
|
177 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
178 |
"%s added temporary %ju min. D-Line for [%s] [%s]", |
179 |
get_oper_name(source_p), duration / 60, |
180 |
conf->host, conf->reason); |
181 |
ilog(LOG_TYPE_DLINE, "%s added temporary %ju min. D-Line for [%s] [%s]", |
182 |
get_oper_name(source_p), duration / 60, conf->host, conf->reason); |
183 |
} |
184 |
else |
185 |
{ |
186 |
if (IsClient(source_p)) |
187 |
sendto_one_notice(source_p, &me, ":Added D-Line [%s]", conf->host); |
188 |
|
189 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
190 |
"%s added D-Line for [%s] [%s]", |
191 |
get_oper_name(source_p), conf->host, conf->reason); |
192 |
ilog(LOG_TYPE_DLINE, "%s added D-Line for [%s] [%s]", |
193 |
get_oper_name(source_p), conf->host, conf->reason); |
194 |
} |
195 |
|
196 |
dline_check(add_conf_by_address(CONF_DLINE, conf)); |
197 |
} |
198 |
|
199 |
/* mo_dline() |
200 |
* |
201 |
* inputs - pointer to server |
202 |
* - pointer to client |
203 |
* - parameter count |
204 |
* - parameter list |
205 |
* output - |
206 |
* side effects - D line is added |
207 |
* |
208 |
*/ |
209 |
static int |
210 |
mo_dline(struct Client *source_p, int parc, char *parv[]) |
211 |
{ |
212 |
char *dlhost = NULL, *reason = NULL; |
213 |
char *target_server = NULL; |
214 |
const struct Client *target_p = NULL; |
215 |
uintmax_t duration = 0; |
216 |
int t = 0; |
217 |
char hostip[HOSTIPLEN + 1]; |
218 |
|
219 |
if (!HasOFlag(source_p, OPER_FLAG_DLINE)) |
220 |
{ |
221 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVS, "dline"); |
222 |
return 0; |
223 |
} |
224 |
|
225 |
if (!parse_aline("DLINE", source_p, parc, parv, &dlhost, |
226 |
NULL, &duration, &target_server, &reason)) |
227 |
return 0; |
228 |
|
229 |
if (target_server) |
230 |
{ |
231 |
sendto_match_servs(source_p, target_server, CAPAB_DLN, "DLINE %s %ju %s :%s", |
232 |
target_server, duration, |
233 |
dlhost, reason); |
234 |
|
235 |
/* Allow ON to apply local dline as well if it matches */ |
236 |
if (match(target_server, me.name)) |
237 |
return 0; |
238 |
} |
239 |
else |
240 |
cluster_distribute(source_p, "DLINE", CAPAB_DLN, CLUSTER_DLINE, |
241 |
"%ju %s :%s", duration, dlhost, reason); |
242 |
|
243 |
if ((t = parse_netmask(dlhost, NULL, NULL)) == HM_HOST) |
244 |
{ |
245 |
if ((target_p = find_chasing(source_p, dlhost)) == NULL) |
246 |
return 0; /* find_chasing sends ERR_NOSUCHNICK */ |
247 |
|
248 |
if (!MyConnect(target_p)) |
249 |
{ |
250 |
sendto_one_notice(source_p, &me, ":Cannot DLINE nick on another server"); |
251 |
return 0; |
252 |
} |
253 |
|
254 |
if (HasFlag(target_p, FLAGS_EXEMPTKLINE)) |
255 |
{ |
256 |
sendto_one_notice(source_p, &me, ":%s is E-lined", target_p->name); |
257 |
return 0; |
258 |
} |
259 |
|
260 |
getnameinfo((const struct sockaddr *)&target_p->connection->ip, |
261 |
target_p->connection->ip.ss_len, hostip, |
262 |
sizeof(hostip), NULL, 0, NI_NUMERICHOST); |
263 |
dlhost = hostip; |
264 |
} |
265 |
|
266 |
dline_handle(source_p, dlhost, reason, duration); |
267 |
return 0; |
268 |
} |
269 |
|
270 |
/*! \brief DLINE command handler |
271 |
* |
272 |
* \param source_p Pointer to allocated Client struct from which the message |
273 |
* originally comes from. This can be a local or remote client. |
274 |
* \param parc Integer holding the number of supplied arguments. |
275 |
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL |
276 |
* pointers. |
277 |
* \note Valid arguments for this command are: |
278 |
* - parv[0] = command |
279 |
* - parv[1] = target server mask |
280 |
* - parv[2] = duration in seconds |
281 |
* - parv[3] = IP address |
282 |
* - parv[4] = reason |
283 |
*/ |
284 |
static int |
285 |
ms_dline(struct Client *source_p, int parc, char *parv[]) |
286 |
{ |
287 |
const char *dlhost, *reason; |
288 |
|
289 |
if (parc != 5 || EmptyString(parv[4])) |
290 |
return 0; |
291 |
|
292 |
sendto_match_servs(source_p, parv[1], CAPAB_DLN, "DLINE %s %s %s :%s", |
293 |
parv[1], parv[2], parv[3], parv[4]); |
294 |
|
295 |
if (match(parv[1], me.name)) |
296 |
return 0; |
297 |
|
298 |
dlhost = parv[3]; |
299 |
reason = parv[4]; |
300 |
|
301 |
if (HasFlag(source_p, FLAGS_SERVICE) || |
302 |
shared_find(SHARED_DLINE, source_p->servptr->name, |
303 |
source_p->username, source_p->host)) |
304 |
dline_handle(source_p, dlhost, reason, strtoumax(parv[2], NULL, 10)); |
305 |
|
306 |
return 0; |
307 |
} |
308 |
|
309 |
static struct Message dline_msgtab = |
310 |
{ |
311 |
.cmd = "DLINE", |
312 |
.args_min = 2, |
313 |
.args_max = MAXPARA, |
314 |
.handlers[UNREGISTERED_HANDLER] = m_unregistered, |
315 |
.handlers[CLIENT_HANDLER] = m_not_oper, |
316 |
.handlers[SERVER_HANDLER] = ms_dline, |
317 |
.handlers[ENCAP_HANDLER] = m_ignore, |
318 |
.handlers[OPER_HANDLER] = mo_dline |
319 |
}; |
320 |
|
321 |
static void |
322 |
module_init(void) |
323 |
{ |
324 |
mod_add_cmd(&dline_msgtab); |
325 |
add_capability("DLN", CAPAB_DLN); |
326 |
} |
327 |
|
328 |
static void |
329 |
module_exit(void) |
330 |
{ |
331 |
mod_del_cmd(&dline_msgtab); |
332 |
delete_capability("DLN"); |
333 |
} |
334 |
|
335 |
struct module module_entry = |
336 |
{ |
337 |
.version = "$Revision$", |
338 |
.modinit = module_init, |
339 |
.modexit = module_exit, |
340 |
}; |