| 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; |
| 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 |
piphost = &iphost; |
| 114 |
break; |
| 115 |
case HM_IPV6: |
| 116 |
if (!HasFlag(source_p, FLAGS_SERVICE) && (unsigned int)bits < ConfigGeneral.kline_min_cidr6) |
| 117 |
{ |
| 118 |
if (IsClient(source_p)) |
| 119 |
sendto_one_notice(source_p, &me, ":For safety, bitmasks less than %u require conf access.", |
| 120 |
ConfigGeneral.kline_min_cidr6); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
piphost = &iphost; |
| 125 |
break; |
| 126 |
default: /* HM_HOST */ |
| 127 |
break; |
| 128 |
} |
| 129 |
|
| 130 |
struct MaskItem *conf; |
| 131 |
if ((conf = find_conf_by_address(aline->host, piphost, CONF_KLINE, aline->user, NULL, 0))) |
| 132 |
{ |
| 133 |
if (IsClient(source_p)) |
| 134 |
sendto_one_notice(source_p, &me, ":[%s@%s] already K-Lined by [%s@%s] - %s", |
| 135 |
aline->user, aline->host, conf->user, conf->host, conf->reason); |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
if (aline->duration) |
| 140 |
snprintf(buf, sizeof(buf), "Temporary K-line %ju min. - %.*s (%s)", |
| 141 |
aline->duration / 60, REASONLEN, aline->reason, date_iso8601(0)); |
| 142 |
else |
| 143 |
snprintf(buf, sizeof(buf), "%.*s (%s)", REASONLEN, aline->reason, date_iso8601(0)); |
| 144 |
|
| 145 |
conf = conf_make(CONF_KLINE); |
| 146 |
conf->user = xstrdup(aline->user); |
| 147 |
conf->host = xstrdup(aline->host); |
| 148 |
conf->setat = CurrentTime; |
| 149 |
conf->reason = xstrdup(buf); |
| 150 |
SetConfDatabase(conf); |
| 151 |
|
| 152 |
if (aline->duration) |
| 153 |
{ |
| 154 |
conf->until = CurrentTime + aline->duration; |
| 155 |
|
| 156 |
if (IsClient(source_p)) |
| 157 |
sendto_one_notice(source_p, &me, ":Added temporary %ju min. K-Line [%s@%s]", |
| 158 |
aline->duration / 60, conf->user, conf->host); |
| 159 |
|
| 160 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
| 161 |
"%s added temporary %ju min. K-Line for [%s@%s] [%s]", |
| 162 |
get_oper_name(source_p), aline->duration / 60, |
| 163 |
conf->user, conf->host, |
| 164 |
conf->reason); |
| 165 |
ilog(LOG_TYPE_KLINE, "%s added temporary %ju min. K-Line for [%s@%s] [%s]", |
| 166 |
get_oper_name(source_p), aline->duration / 60, |
| 167 |
conf->user, conf->host, conf->reason); |
| 168 |
} |
| 169 |
else |
| 170 |
{ |
| 171 |
if (IsClient(source_p)) |
| 172 |
sendto_one_notice(source_p, &me, ":Added K-Line [%s@%s]", |
| 173 |
conf->user, conf->host); |
| 174 |
|
| 175 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
| 176 |
"%s added K-Line for [%s@%s] [%s]", |
| 177 |
get_oper_name(source_p), |
| 178 |
conf->user, conf->host, conf->reason); |
| 179 |
ilog(LOG_TYPE_KLINE, "%s added K-Line for [%s@%s] [%s]", |
| 180 |
get_oper_name(source_p), conf->user, conf->host, conf->reason); |
| 181 |
} |
| 182 |
|
| 183 |
kline_check(add_conf_by_address(CONF_KLINE, conf)); |
| 184 |
} |
| 185 |
|
| 186 |
/* mo_kline() |
| 187 |
* |
| 188 |
* inputs - pointer to server |
| 189 |
* - pointer to client |
| 190 |
* - parameter count |
| 191 |
* - parameter list |
| 192 |
* output - |
| 193 |
* side effects - k line is added |
| 194 |
*/ |
| 195 |
static int |
| 196 |
mo_kline(struct Client *source_p, int parc, char *parv[]) |
| 197 |
{ |
| 198 |
struct aline_ctx aline = { .add = true, .simple_mask = false }; |
| 199 |
|
| 200 |
if (!HasOFlag(source_p, OPER_FLAG_KLINE)) |
| 201 |
{ |
| 202 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVS, "kline"); |
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
if (parse_aline("KLINE", source_p, parc, parv, &aline) == false) |
| 207 |
return 0; |
| 208 |
|
| 209 |
if (aline.server) |
| 210 |
{ |
| 211 |
sendto_match_servs(source_p, aline.server, CAPAB_KLN, "KLINE %s %ju %s %s :%s", |
| 212 |
aline.server, aline.duration, |
| 213 |
aline.user, aline.host, aline.reason); |
| 214 |
|
| 215 |
/* Allow ON to apply local kline as well if it matches */ |
| 216 |
if (match(aline.server, me.name)) |
| 217 |
return 0; |
| 218 |
} |
| 219 |
else |
| 220 |
cluster_distribute(source_p, "KLINE", CAPAB_KLN, CLUSTER_KLINE, "%ju %s %s :%s", |
| 221 |
aline.duration, aline.user, aline.host, aline.reason); |
| 222 |
|
| 223 |
kline_handle(source_p, &aline); |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
/*! \brief KLINE command handler |
| 228 |
* |
| 229 |
* \param source_p Pointer to allocated Client struct from which the message |
| 230 |
* originally comes from. This can be a local or remote client. |
| 231 |
* \param parc Integer holding the number of supplied arguments. |
| 232 |
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL |
| 233 |
* pointers. |
| 234 |
* \note Valid arguments for this command are: |
| 235 |
* - parv[0] = command |
| 236 |
* - parv[1] = target server mask |
| 237 |
* - parv[2] = duration in seconds |
| 238 |
* - parv[3] = user mask |
| 239 |
* - parv[4] = host mask |
| 240 |
* - parv[5] = reason |
| 241 |
*/ |
| 242 |
static int |
| 243 |
ms_kline(struct Client *source_p, int parc, char *parv[]) |
| 244 |
{ |
| 245 |
struct aline_ctx aline = |
| 246 |
{ |
| 247 |
.add = true, |
| 248 |
.simple_mask = false, |
| 249 |
.user = parv[3], |
| 250 |
.host = parv[4], |
| 251 |
.reason = parv[5], |
| 252 |
.server = parv[1], |
| 253 |
.duration = strtoumax(parv[2], NULL, 10) |
| 254 |
}; |
| 255 |
|
| 256 |
if (parc != 6 || EmptyString(parv[parc - 1])) |
| 257 |
return 0; |
| 258 |
|
| 259 |
sendto_match_servs(source_p, aline.server, CAPAB_KLN, "KLINE %s %ju %s %s :%s", aline.server, |
| 260 |
aline.duration, aline.user, aline.host, aline.reason); |
| 261 |
|
| 262 |
if (match(aline.server, me.name)) |
| 263 |
return 0; |
| 264 |
|
| 265 |
if (HasFlag(source_p, FLAGS_SERVICE) || |
| 266 |
shared_find(SHARED_KLINE, source_p->servptr->name, |
| 267 |
source_p->username, source_p->host)) |
| 268 |
kline_handle(source_p, &aline); |
| 269 |
|
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
static struct Message kline_msgtab = |
| 274 |
{ |
| 275 |
.cmd = "KLINE", |
| 276 |
.args_min = 2, |
| 277 |
.args_max = MAXPARA, |
| 278 |
.handlers[UNREGISTERED_HANDLER] = m_unregistered, |
| 279 |
.handlers[CLIENT_HANDLER] = m_not_oper, |
| 280 |
.handlers[SERVER_HANDLER] = ms_kline, |
| 281 |
.handlers[ENCAP_HANDLER] = m_ignore, |
| 282 |
.handlers[OPER_HANDLER] = mo_kline |
| 283 |
}; |
| 284 |
|
| 285 |
static void |
| 286 |
module_init(void) |
| 287 |
{ |
| 288 |
mod_add_cmd(&kline_msgtab); |
| 289 |
capab_add("KLN", CAPAB_KLN); |
| 290 |
} |
| 291 |
|
| 292 |
static void |
| 293 |
module_exit(void) |
| 294 |
{ |
| 295 |
mod_del_cmd(&kline_msgtab); |
| 296 |
capab_del("KLN"); |
| 297 |
} |
| 298 |
|
| 299 |
struct module module_entry = |
| 300 |
{ |
| 301 |
.version = "$Revision$", |
| 302 |
.modinit = module_init, |
| 303 |
.modexit = module_exit, |
| 304 |
}; |