1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2014 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 "hostmask.h" |
34 |
#include "numeric.h" |
35 |
#include "log.h" |
36 |
#include "misc.h" |
37 |
#include "send.h" |
38 |
#include "hash.h" |
39 |
#include "server.h" |
40 |
#include "parse.h" |
41 |
#include "modules.h" |
42 |
#include "conf_db.h" |
43 |
#include "memory.h" |
44 |
|
45 |
|
46 |
static void |
47 |
check_kline(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 |
if (match(arec->username, client_p->username)) |
59 |
continue; |
60 |
|
61 |
switch (arec->masktype) |
62 |
{ |
63 |
case HM_IPV4: |
64 |
if (client_p->connection->aftype == AF_INET) |
65 |
if (match_ipv4(&client_p->connection->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits)) |
66 |
conf_try_ban(client_p, arec->conf); |
67 |
break; |
68 |
case HM_IPV6: |
69 |
if (client_p->connection->aftype == AF_INET6) |
70 |
if (match_ipv6(&client_p->connection->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits)) |
71 |
conf_try_ban(client_p, arec->conf); |
72 |
break; |
73 |
default: /* HM_HOST */ |
74 |
if (!match(arec->Mask.hostname, client_p->host)) |
75 |
conf_try_ban(client_p, arec->conf); |
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 |
m_kline_add_kline(struct Client *source_p, struct MaskItem *conf, |
89 |
time_t tkline_time) |
90 |
{ |
91 |
if (tkline_time) |
92 |
{ |
93 |
conf->until = CurrentTime + tkline_time; |
94 |
|
95 |
if (IsClient(source_p)) |
96 |
sendto_one_notice(source_p, &me, ":Added temporary %d min. K-Line [%s@%s]", |
97 |
tkline_time/60, conf->user, conf->host); |
98 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
99 |
"%s added temporary %d min. K-Line for [%s@%s] [%s]", |
100 |
get_oper_name(source_p), tkline_time/60, |
101 |
conf->user, conf->host, |
102 |
conf->reason); |
103 |
ilog(LOG_TYPE_KLINE, "%s added temporary %d min. K-Line for [%s@%s] [%s]", |
104 |
get_oper_name(source_p), tkline_time/60, |
105 |
conf->user, conf->host, conf->reason); |
106 |
} |
107 |
else |
108 |
{ |
109 |
if (IsClient(source_p)) |
110 |
sendto_one_notice(source_p, &me, ":Added K-Line [%s@%s]", |
111 |
conf->user, conf->host); |
112 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
113 |
"%s added K-Line for [%s@%s] [%s]", |
114 |
get_oper_name(source_p), |
115 |
conf->user, conf->host, conf->reason); |
116 |
ilog(LOG_TYPE_KLINE, "%s added K-Line for [%s@%s] [%s]", |
117 |
get_oper_name(source_p), conf->user, conf->host, conf->reason); |
118 |
} |
119 |
|
120 |
conf->setat = CurrentTime; |
121 |
SetConfDatabase(conf); |
122 |
|
123 |
check_kline(add_conf_by_address(CONF_KLINE, conf)); |
124 |
} |
125 |
|
126 |
/* already_placed_kline() |
127 |
* inputs - user to complain to, username & host to check for |
128 |
* outputs - returns 1 on existing K-line, 0 if doesn't exist |
129 |
* side effects - notifies source_p if the K-line already exists |
130 |
*/ |
131 |
/* |
132 |
* Note: This currently works if the new K-line is a special case of an |
133 |
* existing K-line, but not the other way round. To do that we would |
134 |
* have to walk the hash and check every existing K-line. -A1kmm. |
135 |
*/ |
136 |
static int |
137 |
already_placed_kline(struct Client *source_p, const char *luser, const char *lhost, int warn) |
138 |
{ |
139 |
struct irc_ssaddr iphost, *piphost; |
140 |
struct MaskItem *conf = NULL; |
141 |
int t = 0; |
142 |
int aftype = 0; |
143 |
|
144 |
if ((t = parse_netmask(lhost, &iphost, NULL)) != HM_HOST) |
145 |
{ |
146 |
if (t == HM_IPV6) |
147 |
aftype = AF_INET6; |
148 |
else |
149 |
aftype = AF_INET; |
150 |
piphost = &iphost; |
151 |
} |
152 |
else |
153 |
piphost = NULL; |
154 |
|
155 |
if ((conf = find_conf_by_address(lhost, piphost, CONF_KLINE, aftype, luser, NULL, 0))) |
156 |
{ |
157 |
if (IsClient(source_p) && warn) |
158 |
sendto_one_notice(source_p, &me, ":[%s@%s] already K-Lined by [%s@%s] - %s", |
159 |
luser, lhost, conf->user, conf->host, conf->reason); |
160 |
return 1; |
161 |
} |
162 |
|
163 |
return 0; |
164 |
} |
165 |
|
166 |
/* mo_kline() |
167 |
* |
168 |
* inputs - pointer to server |
169 |
* - pointer to client |
170 |
* - parameter count |
171 |
* - parameter list |
172 |
* output - |
173 |
* side effects - k line is added |
174 |
*/ |
175 |
static int |
176 |
mo_kline(struct Client *source_p, int parc, char *parv[]) |
177 |
{ |
178 |
char buffer[IRCD_BUFSIZE]; |
179 |
char *reason = NULL; |
180 |
char *user = NULL; |
181 |
char *host = NULL; |
182 |
const char *current_date; |
183 |
char *target_server = NULL; |
184 |
struct MaskItem *conf; |
185 |
time_t tkline_time = 0; |
186 |
|
187 |
if (!HasOFlag(source_p, OPER_FLAG_KLINE)) |
188 |
{ |
189 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVS, "kline"); |
190 |
return 0; |
191 |
} |
192 |
|
193 |
if (parse_aline("KLINE", source_p, parc, parv, AWILD, &user, &host, |
194 |
&tkline_time, &target_server, &reason) < 0) |
195 |
return 0; |
196 |
|
197 |
if (target_server) |
198 |
{ |
199 |
sendto_match_servs(source_p, target_server, CAP_KLN, "KLINE %s %lu %s %s :%s", |
200 |
target_server, (unsigned long)tkline_time, |
201 |
user, host, reason); |
202 |
|
203 |
/* Allow ON to apply local kline as well if it matches */ |
204 |
if (match(target_server, me.name)) |
205 |
return 0; |
206 |
} |
207 |
else |
208 |
cluster_a_line(source_p, "KLINE", CAP_KLN, SHARED_KLINE, |
209 |
"%d %s %s :%s", tkline_time, user, host, reason); |
210 |
|
211 |
if (already_placed_kline(source_p, user, host, 1)) |
212 |
return 0; |
213 |
|
214 |
current_date = smalldate(0); |
215 |
conf = conf_make(CONF_KLINE); |
216 |
|
217 |
conf->host = xstrdup(host); |
218 |
conf->user = xstrdup(user); |
219 |
|
220 |
if (tkline_time) |
221 |
snprintf(buffer, sizeof(buffer), "Temporary K-line %d min. - %.*s (%s)", |
222 |
(int)(tkline_time/60), REASONLEN, reason, current_date); |
223 |
else |
224 |
snprintf(buffer, sizeof(buffer), "%.*s (%s)", REASONLEN, reason, current_date); |
225 |
|
226 |
conf->reason = xstrdup(buffer); |
227 |
m_kline_add_kline(source_p, conf, tkline_time); |
228 |
return 0; |
229 |
} |
230 |
|
231 |
/* me_kline - handle remote kline. no propagation */ |
232 |
static int |
233 |
me_kline(struct Client *source_p, int parc, char *parv[]) |
234 |
{ |
235 |
char buffer[IRCD_BUFSIZE]; |
236 |
struct MaskItem *conf = NULL; |
237 |
time_t tkline_time = 0; |
238 |
const char *current_date; |
239 |
char *kuser, *khost, *kreason; |
240 |
|
241 |
if (parc != 6 || EmptyString(parv[5])) |
242 |
return 0; |
243 |
|
244 |
if (match(parv[1], me.name)) |
245 |
return 0; |
246 |
|
247 |
tkline_time = valid_tkline(parv[2], TK_SECONDS); |
248 |
kuser = parv[3]; |
249 |
khost = parv[4]; |
250 |
kreason = parv[5]; |
251 |
|
252 |
current_date = smalldate(0); |
253 |
|
254 |
if (HasFlag(source_p, FLAGS_SERVICE) || |
255 |
find_matching_name_conf(CONF_ULINE, source_p->servptr->name, |
256 |
source_p->username, source_p->host, |
257 |
SHARED_KLINE)) |
258 |
{ |
259 |
if (already_placed_kline(source_p, kuser, khost, 1)) |
260 |
return 0; |
261 |
|
262 |
conf = conf_make(CONF_KLINE); |
263 |
conf->host = xstrdup(khost); |
264 |
conf->user = xstrdup(kuser); |
265 |
|
266 |
if (tkline_time) |
267 |
snprintf(buffer, sizeof(buffer), "Temporary K-line %u min. - %.*s (%s)", |
268 |
(unsigned int)(tkline_time/60), REASONLEN, kreason, current_date); |
269 |
else |
270 |
snprintf(buffer, sizeof(buffer), "%.*s (%s)", REASONLEN, kreason, current_date); |
271 |
|
272 |
conf->reason = xstrdup(buffer); |
273 |
m_kline_add_kline(source_p, conf, tkline_time); |
274 |
} |
275 |
|
276 |
return 0; |
277 |
} |
278 |
|
279 |
static int |
280 |
ms_kline(struct Client *source_p, int parc, char *parv[]) |
281 |
{ |
282 |
if (parc != 6 || EmptyString(parv[5])) |
283 |
return 0; |
284 |
|
285 |
/* parv[0] parv[1] parv[2] parv[3] parv[4] parv[5] */ |
286 |
/* command target_server tkline_time user host reason */ |
287 |
sendto_match_servs(source_p, parv[1], CAP_KLN, "KLINE %s %s %s %s :%s", |
288 |
parv[1], parv[2], parv[3], parv[4], parv[5]); |
289 |
|
290 |
return me_kline(source_p, parc, parv); |
291 |
} |
292 |
|
293 |
static struct Message kline_msgtab = |
294 |
{ |
295 |
"KLINE", NULL, 0, 0, 2, MAXPARA, MFLG_SLOW, 0, |
296 |
{ m_unregistered, m_not_oper, ms_kline, me_kline, mo_kline, m_ignore } |
297 |
}; |
298 |
|
299 |
static void |
300 |
module_init(void) |
301 |
{ |
302 |
mod_add_cmd(&kline_msgtab); |
303 |
add_capability("KLN", CAP_KLN, 1); |
304 |
} |
305 |
|
306 |
static void |
307 |
module_exit(void) |
308 |
{ |
309 |
mod_del_cmd(&kline_msgtab); |
310 |
delete_capability("KLN"); |
311 |
} |
312 |
|
313 |
struct module module_entry = |
314 |
{ |
315 |
.node = { NULL, NULL, NULL }, |
316 |
.name = NULL, |
317 |
.version = "$Revision$", |
318 |
.handle = NULL, |
319 |
.modinit = module_init, |
320 |
.modexit = module_exit, |
321 |
.flags = 0 |
322 |
}; |