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_kline.c |
23 |
* \brief Includes required functions for processing the KLINE 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 "ircd.h" |
32 |
#include "conf.h" |
33 |
#include "conf_cluster.h" |
34 |
#include "conf_shared.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 |
kline_check(const struct AddressRec *arec) |
48 |
{ |
49 |
dlink_node *node, *node_next; |
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 |
if (match(arec->username, client_p->username)) |
59 |
continue; |
60 |
|
61 |
switch (arec->masktype) |
62 |
{ |
63 |
case HM_IPV4: |
64 |
if (client_p->ip.ss.ss_family == AF_INET) |
65 |
if (match_ipv4(&client_p->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits)) |
66 |
conf_try_ban(client_p, CLIENT_BAN_KLINE, arec->conf->reason); |
67 |
break; |
68 |
case HM_IPV6: |
69 |
if (client_p->ip.ss.ss_family == AF_INET6) |
70 |
if (match_ipv6(&client_p->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits)) |
71 |
conf_try_ban(client_p, CLIENT_BAN_KLINE, arec->conf->reason); |
72 |
break; |
73 |
default: /* HM_HOST */ |
74 |
if (match(arec->Mask.hostname, client_p->host) == 0 || match(arec->Mask.hostname, client_p->sockhost) == 0) |
75 |
conf_try_ban(client_p, CLIENT_BAN_KLINE, arec->conf->reason); |
76 |
break; |
77 |
} |
78 |
} |
79 |
} |
80 |
|
81 |
/* apply_tkline() |
82 |
* |
83 |
* inputs - |
84 |
* output - NONE |
85 |
* side effects - tkline as given is placed |
86 |
*/ |
87 |
static void |
88 |
kline_handle(struct Client *source_p, struct aline_ctx *aline) |
89 |
{ |
90 |
char buf[IRCD_BUFSIZE]; |
91 |
int bits = 0, aftype = 0; |
92 |
struct irc_ssaddr iphost, *piphost = NULL; |
93 |
|
94 |
if (!HasFlag(source_p, FLAGS_SERVICE) && valid_wild_card(2, aline->user, aline->host) == false) |
95 |
{ |
96 |
sendto_one_notice(source_p, &me, |
97 |
":Please include at least %u non-wildcard characters with the mask", |
98 |
ConfigGeneral.min_nonwildcard); |
99 |
return; |
100 |
} |
101 |
|
102 |
switch (parse_netmask(aline->host, &iphost, &bits)) |
103 |
{ |
104 |
case HM_IPV4: |
105 |
if (!HasFlag(source_p, FLAGS_SERVICE) && (unsigned int)bits < ConfigGeneral.kline_min_cidr) |
106 |
{ |
107 |
if (IsClient(source_p)) |
108 |
sendto_one_notice(source_p, &me, ":For safety, bitmasks less than %u require conf access.", |
109 |
ConfigGeneral.kline_min_cidr); |
110 |
return; |
111 |
} |
112 |
|
113 |
aftype = AF_INET; |
114 |
piphost = &iphost; |
115 |
break; |
116 |
case HM_IPV6: |
117 |
if (!HasFlag(source_p, FLAGS_SERVICE) && (unsigned int)bits < ConfigGeneral.kline_min_cidr6) |
118 |
{ |
119 |
if (IsClient(source_p)) |
120 |
sendto_one_notice(source_p, &me, ":For safety, bitmasks less than %u require conf access.", |
121 |
ConfigGeneral.kline_min_cidr6); |
122 |
return; |
123 |
} |
124 |
|
125 |
aftype = AF_INET6; |
126 |
piphost = &iphost; |
127 |
break; |
128 |
default: /* HM_HOST */ |
129 |
break; |
130 |
} |
131 |
|
132 |
struct MaskItem *conf; |
133 |
if ((conf = find_conf_by_address(aline->host, piphost, CONF_KLINE, aftype, aline->user, NULL, 0))) |
134 |
{ |
135 |
if (IsClient(source_p)) |
136 |
sendto_one_notice(source_p, &me, ":[%s@%s] already K-Lined by [%s@%s] - %s", |
137 |
aline->user, aline->host, conf->user, conf->host, conf->reason); |
138 |
return; |
139 |
} |
140 |
|
141 |
if (aline->duration) |
142 |
snprintf(buf, sizeof(buf), "Temporary K-line %ju min. - %.*s (%s)", |
143 |
aline->duration / 60, REASONLEN, aline->reason, date_iso8601(0)); |
144 |
else |
145 |
snprintf(buf, sizeof(buf), "%.*s (%s)", REASONLEN, aline->reason, date_iso8601(0)); |
146 |
|
147 |
conf = conf_make(CONF_KLINE); |
148 |
conf->user = xstrdup(aline->user); |
149 |
conf->host = xstrdup(aline->host); |
150 |
conf->setat = CurrentTime; |
151 |
conf->reason = xstrdup(buf); |
152 |
SetConfDatabase(conf); |
153 |
|
154 |
if (aline->duration) |
155 |
{ |
156 |
conf->until = CurrentTime + aline->duration; |
157 |
|
158 |
if (IsClient(source_p)) |
159 |
sendto_one_notice(source_p, &me, ":Added temporary %ju min. K-Line [%s@%s]", |
160 |
aline->duration / 60, conf->user, conf->host); |
161 |
|
162 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
163 |
"%s added temporary %ju min. K-Line for [%s@%s] [%s]", |
164 |
get_oper_name(source_p), aline->duration / 60, |
165 |
conf->user, conf->host, |
166 |
conf->reason); |
167 |
ilog(LOG_TYPE_KLINE, "%s added temporary %ju min. K-Line for [%s@%s] [%s]", |
168 |
get_oper_name(source_p), aline->duration / 60, |
169 |
conf->user, conf->host, conf->reason); |
170 |
} |
171 |
else |
172 |
{ |
173 |
if (IsClient(source_p)) |
174 |
sendto_one_notice(source_p, &me, ":Added K-Line [%s@%s]", |
175 |
conf->user, conf->host); |
176 |
|
177 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
178 |
"%s added K-Line for [%s@%s] [%s]", |
179 |
get_oper_name(source_p), |
180 |
conf->user, conf->host, conf->reason); |
181 |
ilog(LOG_TYPE_KLINE, "%s added K-Line for [%s@%s] [%s]", |
182 |
get_oper_name(source_p), conf->user, conf->host, conf->reason); |
183 |
} |
184 |
|
185 |
kline_check(add_conf_by_address(CONF_KLINE, conf)); |
186 |
} |
187 |
|
188 |
/* mo_kline() |
189 |
* |
190 |
* inputs - pointer to server |
191 |
* - pointer to client |
192 |
* - parameter count |
193 |
* - parameter list |
194 |
* output - |
195 |
* side effects - k line is added |
196 |
*/ |
197 |
static int |
198 |
mo_kline(struct Client *source_p, int parc, char *parv[]) |
199 |
{ |
200 |
struct aline_ctx aline = { .add = true, .simple_mask = false }; |
201 |
|
202 |
if (!HasOFlag(source_p, OPER_FLAG_KLINE)) |
203 |
{ |
204 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVS, "kline"); |
205 |
return 0; |
206 |
} |
207 |
|
208 |
if (parse_aline("KLINE", source_p, parc, parv, &aline) == false) |
209 |
return 0; |
210 |
|
211 |
if (aline.server) |
212 |
{ |
213 |
sendto_match_servs(source_p, aline.server, CAPAB_KLN, "KLINE %s %ju %s %s :%s", |
214 |
aline.server, aline.duration, |
215 |
aline.user, aline.host, aline.reason); |
216 |
|
217 |
/* Allow ON to apply local kline as well if it matches */ |
218 |
if (match(aline.server, me.name)) |
219 |
return 0; |
220 |
} |
221 |
else |
222 |
cluster_distribute(source_p, "KLINE", CAPAB_KLN, CLUSTER_KLINE, "%ju %s %s :%s", |
223 |
aline.duration, aline.user, aline.host, aline.reason); |
224 |
|
225 |
kline_handle(source_p, &aline); |
226 |
return 0; |
227 |
} |
228 |
|
229 |
/*! \brief KLINE command handler |
230 |
* |
231 |
* \param source_p Pointer to allocated Client struct from which the message |
232 |
* originally comes from. This can be a local or remote client. |
233 |
* \param parc Integer holding the number of supplied arguments. |
234 |
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL |
235 |
* pointers. |
236 |
* \note Valid arguments for this command are: |
237 |
* - parv[0] = command |
238 |
* - parv[1] = target server mask |
239 |
* - parv[2] = duration in seconds |
240 |
* - parv[3] = user mask |
241 |
* - parv[4] = host mask |
242 |
* - parv[5] = reason |
243 |
*/ |
244 |
static int |
245 |
ms_kline(struct Client *source_p, int parc, char *parv[]) |
246 |
{ |
247 |
struct aline_ctx aline = |
248 |
{ |
249 |
.add = true, |
250 |
.simple_mask = false, |
251 |
.user = parv[3], |
252 |
.host = parv[4], |
253 |
.reason = parv[5], |
254 |
.server = parv[1], |
255 |
.duration = strtoumax(parv[2], NULL, 10) |
256 |
}; |
257 |
|
258 |
if (parc != 6 || EmptyString(parv[parc - 1])) |
259 |
return 0; |
260 |
|
261 |
sendto_match_servs(source_p, aline.server, CAPAB_KLN, "KLINE %s %ju %s %s :%s", aline.server, |
262 |
aline.duration, aline.user, aline.host, aline.reason); |
263 |
|
264 |
if (match(aline.server, me.name)) |
265 |
return 0; |
266 |
|
267 |
if (HasFlag(source_p, FLAGS_SERVICE) || |
268 |
shared_find(SHARED_KLINE, source_p->servptr->name, |
269 |
source_p->username, source_p->host)) |
270 |
kline_handle(source_p, &aline); |
271 |
|
272 |
return 0; |
273 |
} |
274 |
|
275 |
static struct Message kline_msgtab = |
276 |
{ |
277 |
.cmd = "KLINE", |
278 |
.args_min = 2, |
279 |
.args_max = MAXPARA, |
280 |
.handlers[UNREGISTERED_HANDLER] = m_unregistered, |
281 |
.handlers[CLIENT_HANDLER] = m_not_oper, |
282 |
.handlers[SERVER_HANDLER] = ms_kline, |
283 |
.handlers[ENCAP_HANDLER] = m_ignore, |
284 |
.handlers[OPER_HANDLER] = mo_kline |
285 |
}; |
286 |
|
287 |
static void |
288 |
module_init(void) |
289 |
{ |
290 |
mod_add_cmd(&kline_msgtab); |
291 |
capab_add("KLN", CAPAB_KLN); |
292 |
} |
293 |
|
294 |
static void |
295 |
module_exit(void) |
296 |
{ |
297 |
mod_del_cmd(&kline_msgtab); |
298 |
capab_del("KLN"); |
299 |
} |
300 |
|
301 |
struct module module_entry = |
302 |
{ |
303 |
.version = "$Revision$", |
304 |
.modinit = module_init, |
305 |
.modexit = module_exit, |
306 |
}; |