1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2019 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_capab.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_list *tab[] = { &local_client_list, &unknown_list, NULL }; |
50 |
|
51 |
for (dlink_list **list = tab; *list; ++list) |
52 |
{ |
53 |
dlink_node *node, *node_next; |
54 |
|
55 |
DLINK_FOREACH_SAFE(node, node_next, (*list)->head) |
56 |
{ |
57 |
struct Client *client_p = node->data; |
58 |
|
59 |
if (IsDead(client_p)) |
60 |
continue; |
61 |
|
62 |
switch (arec->masktype) |
63 |
{ |
64 |
case HM_IPV4: |
65 |
if (client_p->ip.ss.ss_family == AF_INET) |
66 |
if (match_ipv4(&client_p->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits)) |
67 |
conf_try_ban(client_p, CLIENT_BAN_DLINE, arec->conf->reason); |
68 |
break; |
69 |
case HM_IPV6: |
70 |
if (client_p->ip.ss.ss_family == AF_INET6) |
71 |
if (match_ipv6(&client_p->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits)) |
72 |
conf_try_ban(client_p, CLIENT_BAN_DLINE, arec->conf->reason); |
73 |
break; |
74 |
default: break; |
75 |
} |
76 |
} |
77 |
} |
78 |
} |
79 |
|
80 |
/* dline_add() |
81 |
* |
82 |
* inputs - |
83 |
* output - NONE |
84 |
* side effects - dline as given is placed |
85 |
*/ |
86 |
static void |
87 |
dline_handle(struct Client *source_p, struct aline_ctx *aline) |
88 |
{ |
89 |
char buf[IRCD_BUFSIZE]; |
90 |
struct irc_ssaddr addr; |
91 |
int bits = 0, aftype = 0; |
92 |
|
93 |
switch (parse_netmask(aline->host, &addr, &bits)) |
94 |
{ |
95 |
case HM_IPV4: |
96 |
if (!HasFlag(source_p, FLAGS_SERVICE) && (unsigned int)bits < ConfigGeneral.dline_min_cidr) |
97 |
{ |
98 |
if (IsClient(source_p)) |
99 |
sendto_one_notice(source_p, &me, ":For safety, bitmasks less than %u require conf access.", |
100 |
ConfigGeneral.dline_min_cidr); |
101 |
return; |
102 |
} |
103 |
|
104 |
aftype = AF_INET; |
105 |
break; |
106 |
case HM_IPV6: |
107 |
if (!HasFlag(source_p, FLAGS_SERVICE) && (unsigned int)bits < ConfigGeneral.dline_min_cidr6) |
108 |
{ |
109 |
if (IsClient(source_p)) |
110 |
sendto_one_notice(source_p, &me, ":For safety, bitmasks less than %u require conf access.", |
111 |
ConfigGeneral.dline_min_cidr6); |
112 |
return; |
113 |
} |
114 |
|
115 |
aftype = AF_INET6; |
116 |
break; |
117 |
default: /* HM_HOST */ |
118 |
if (IsClient(source_p)) |
119 |
sendto_one_notice(source_p, &me, ":Invalid D-Line"); |
120 |
|
121 |
return; |
122 |
} |
123 |
|
124 |
struct MaskItem *conf; |
125 |
if ((conf = find_conf_by_address(NULL, &addr, CONF_DLINE, aftype, NULL, NULL, 1))) |
126 |
{ |
127 |
if (IsClient(source_p)) |
128 |
sendto_one_notice(source_p, &me, ":[%s] already D-lined by [%s] - %s", |
129 |
aline->host, conf->host, conf->reason); |
130 |
return; |
131 |
} |
132 |
|
133 |
if (aline->duration) |
134 |
snprintf(buf, sizeof(buf), "Temporary D-line %ju min. - %.*s (%s)", |
135 |
aline->duration / 60, REASONLEN, aline->reason, date_iso8601(0)); |
136 |
else |
137 |
snprintf(buf, sizeof(buf), "%.*s (%s)", REASONLEN, aline->reason, date_iso8601(0)); |
138 |
|
139 |
conf = conf_make(CONF_DLINE); |
140 |
conf->host = xstrdup(aline->host); |
141 |
conf->reason = xstrdup(buf); |
142 |
conf->setat = CurrentTime; |
143 |
SetConfDatabase(conf); |
144 |
|
145 |
if (aline->duration) |
146 |
{ |
147 |
conf->until = CurrentTime + aline->duration; |
148 |
|
149 |
if (IsClient(source_p)) |
150 |
sendto_one_notice(source_p, &me, ":Added temporary %ju min. D-Line [%s]", |
151 |
aline->duration / 60, conf->host); |
152 |
|
153 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
154 |
"%s added temporary %ju min. D-Line for [%s] [%s]", |
155 |
get_oper_name(source_p), aline->duration / 60, |
156 |
conf->host, conf->reason); |
157 |
ilog(LOG_TYPE_DLINE, "%s added temporary %ju min. D-Line for [%s] [%s]", |
158 |
get_oper_name(source_p), aline->duration / 60, conf->host, conf->reason); |
159 |
} |
160 |
else |
161 |
{ |
162 |
if (IsClient(source_p)) |
163 |
sendto_one_notice(source_p, &me, ":Added D-Line [%s]", conf->host); |
164 |
|
165 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
166 |
"%s added D-Line for [%s] [%s]", |
167 |
get_oper_name(source_p), conf->host, conf->reason); |
168 |
ilog(LOG_TYPE_DLINE, "%s added D-Line for [%s] [%s]", |
169 |
get_oper_name(source_p), conf->host, conf->reason); |
170 |
} |
171 |
|
172 |
dline_check(add_conf_by_address(CONF_DLINE, conf)); |
173 |
} |
174 |
|
175 |
/* mo_dline() |
176 |
* |
177 |
* inputs - pointer to server |
178 |
* - pointer to client |
179 |
* - parameter count |
180 |
* - parameter list |
181 |
* output - |
182 |
* side effects - D line is added |
183 |
* |
184 |
*/ |
185 |
static int |
186 |
mo_dline(struct Client *source_p, int parc, char *parv[]) |
187 |
{ |
188 |
struct aline_ctx aline = { .add = true, .simple_mask = false }; |
189 |
|
190 |
if (!HasOFlag(source_p, OPER_FLAG_DLINE)) |
191 |
{ |
192 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVS, "dline"); |
193 |
return 0; |
194 |
} |
195 |
|
196 |
if (parse_aline("DLINE", source_p, parc, parv, &aline) == false) |
197 |
return 0; |
198 |
|
199 |
if (aline.server) |
200 |
{ |
201 |
sendto_match_servs(source_p, aline.server, CAPAB_DLN, "DLINE %s %ju %s :%s", |
202 |
aline.server, aline.duration, aline.host, aline.reason); |
203 |
|
204 |
/* Allow ON to apply local dline as well if it matches */ |
205 |
if (match(aline.server, me.name)) |
206 |
return 0; |
207 |
} |
208 |
else |
209 |
cluster_distribute(source_p, "DLINE", CAPAB_DLN, CLUSTER_DLINE, |
210 |
"%ju %s :%s", aline.duration, aline.host, aline.reason); |
211 |
|
212 |
dline_handle(source_p, &aline); |
213 |
return 0; |
214 |
} |
215 |
|
216 |
/*! \brief DLINE command handler |
217 |
* |
218 |
* \param source_p Pointer to allocated Client struct from which the message |
219 |
* originally comes from. This can be a local or remote client. |
220 |
* \param parc Integer holding the number of supplied arguments. |
221 |
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL |
222 |
* pointers. |
223 |
* \note Valid arguments for this command are: |
224 |
* - parv[0] = command |
225 |
* - parv[1] = target server mask |
226 |
* - parv[2] = duration in seconds |
227 |
* - parv[3] = IP address |
228 |
* - parv[4] = reason |
229 |
*/ |
230 |
static int |
231 |
ms_dline(struct Client *source_p, int parc, char *parv[]) |
232 |
{ |
233 |
struct aline_ctx aline = |
234 |
{ |
235 |
.add = true, |
236 |
.simple_mask = false, |
237 |
.host = parv[3], |
238 |
.reason = parv[4], |
239 |
.server = parv[1], |
240 |
.duration = strtoumax(parv[2], NULL, 10) |
241 |
}; |
242 |
|
243 |
if (parc != 5 || EmptyString(parv[parc - 1])) |
244 |
return 0; |
245 |
|
246 |
sendto_match_servs(source_p, aline.server, CAPAB_DLN, "DLINE %s %ju %s :%s", |
247 |
aline.server, aline.duration, aline.host, aline.reason); |
248 |
|
249 |
if (match(aline.server, me.name)) |
250 |
return 0; |
251 |
|
252 |
if (HasFlag(source_p, FLAGS_SERVICE) || |
253 |
shared_find(SHARED_DLINE, source_p->servptr->name, |
254 |
source_p->username, source_p->host)) |
255 |
dline_handle(source_p, &aline); |
256 |
|
257 |
return 0; |
258 |
} |
259 |
|
260 |
static struct Message dline_msgtab = |
261 |
{ |
262 |
.cmd = "DLINE", |
263 |
.args_min = 2, |
264 |
.args_max = MAXPARA, |
265 |
.handlers[UNREGISTERED_HANDLER] = m_unregistered, |
266 |
.handlers[CLIENT_HANDLER] = m_not_oper, |
267 |
.handlers[SERVER_HANDLER] = ms_dline, |
268 |
.handlers[ENCAP_HANDLER] = m_ignore, |
269 |
.handlers[OPER_HANDLER] = mo_dline |
270 |
}; |
271 |
|
272 |
static void |
273 |
module_init(void) |
274 |
{ |
275 |
mod_add_cmd(&dline_msgtab); |
276 |
capab_add("DLN", CAPAB_DLN); |
277 |
} |
278 |
|
279 |
static void |
280 |
module_exit(void) |
281 |
{ |
282 |
mod_del_cmd(&dline_msgtab); |
283 |
capab_del("DLN"); |
284 |
} |
285 |
|
286 |
struct module module_entry = |
287 |
{ |
288 |
.version = "$Revision$", |
289 |
.modinit = module_init, |
290 |
.modexit = module_exit, |
291 |
}; |