| 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 "ircd.h" |
| 33 |
#include "hostmask.h" |
| 34 |
#include "numeric.h" |
| 35 |
#include "log.h" |
| 36 |
#include "misc.h" |
| 37 |
#include "send.h" |
| 38 |
#include "server.h" |
| 39 |
#include "parse.h" |
| 40 |
#include "modules.h" |
| 41 |
#include "memory.h" |
| 42 |
|
| 43 |
|
| 44 |
static void |
| 45 |
dline_check(struct AddressRec *arec) |
| 46 |
{ |
| 47 |
dlink_node *node = NULL, *node_next = NULL; |
| 48 |
|
| 49 |
DLINK_FOREACH_SAFE(node, node_next, local_client_list.head) |
| 50 |
{ |
| 51 |
struct Client *client_p = node->data; |
| 52 |
|
| 53 |
if (IsDead(client_p)) |
| 54 |
continue; |
| 55 |
|
| 56 |
switch (arec->masktype) |
| 57 |
{ |
| 58 |
case HM_IPV4: |
| 59 |
if (client_p->connection->aftype == AF_INET) |
| 60 |
if (match_ipv4(&client_p->connection->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits)) |
| 61 |
conf_try_ban(client_p, arec->conf); |
| 62 |
break; |
| 63 |
case HM_IPV6: |
| 64 |
if (client_p->connection->aftype == AF_INET6) |
| 65 |
if (match_ipv6(&client_p->connection->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits)) |
| 66 |
conf_try_ban(client_p, arec->conf); |
| 67 |
break; |
| 68 |
default: break; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
DLINK_FOREACH_SAFE(node, node_next, unknown_list.head) |
| 73 |
{ |
| 74 |
struct Client *client_p = node->data; |
| 75 |
|
| 76 |
if (IsDead(client_p)) |
| 77 |
continue; |
| 78 |
|
| 79 |
switch (arec->masktype) |
| 80 |
{ |
| 81 |
case HM_IPV4: |
| 82 |
if (client_p->connection->aftype == AF_INET) |
| 83 |
if (match_ipv4(&client_p->connection->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits)) |
| 84 |
conf_try_ban(client_p, arec->conf); |
| 85 |
break; |
| 86 |
case HM_IPV6: |
| 87 |
if (client_p->connection->aftype == AF_INET6) |
| 88 |
if (match_ipv6(&client_p->connection->ip, &arec->Mask.ipa.addr, arec->Mask.ipa.bits)) |
| 89 |
conf_try_ban(client_p, arec->conf); |
| 90 |
break; |
| 91 |
default: break; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
/* dline_add() |
| 98 |
* |
| 99 |
* inputs - |
| 100 |
* output - NONE |
| 101 |
* side effects - dline as given is placed |
| 102 |
*/ |
| 103 |
static void |
| 104 |
dline_add(struct Client *source_p, const char *addr, const char *reason, |
| 105 |
time_t duration) |
| 106 |
{ |
| 107 |
char buf[IRCD_BUFSIZE]; |
| 108 |
struct MaskItem *conf; |
| 109 |
|
| 110 |
if (duration) |
| 111 |
snprintf(buf, sizeof(buf), "Temporary D-line %ju min. - %.*s (%s)", |
| 112 |
duration / 60, REASONLEN, reason, date_iso8601(0)); |
| 113 |
else |
| 114 |
snprintf(buf, sizeof(buf), "%.*s (%s)", REASONLEN, reason, date_iso8601(0)); |
| 115 |
|
| 116 |
conf = conf_make(CONF_DLINE); |
| 117 |
conf->host = xstrdup(addr); |
| 118 |
conf->reason = xstrdup(buf); |
| 119 |
conf->setat = CurrentTime; |
| 120 |
SetConfDatabase(conf); |
| 121 |
|
| 122 |
if (duration) |
| 123 |
{ |
| 124 |
conf->until = CurrentTime + duration; |
| 125 |
|
| 126 |
if (IsClient(source_p)) |
| 127 |
sendto_one_notice(source_p, &me, ":Added temporary %ju min. D-Line [%s]", |
| 128 |
duration / 60, conf->host); |
| 129 |
|
| 130 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
| 131 |
"%s added temporary %ju min. D-Line for [%s] [%s]", |
| 132 |
get_oper_name(source_p), duration / 60, |
| 133 |
conf->host, conf->reason); |
| 134 |
ilog(LOG_TYPE_DLINE, "%s added temporary %ju min. D-Line for [%s] [%s]", |
| 135 |
get_oper_name(source_p), duration / 60, conf->host, conf->reason); |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
if (IsClient(source_p)) |
| 140 |
sendto_one_notice(source_p, &me, ":Added D-Line [%s]", conf->host); |
| 141 |
|
| 142 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
| 143 |
"%s added D-Line for [%s] [%s]", |
| 144 |
get_oper_name(source_p), conf->host, conf->reason); |
| 145 |
ilog(LOG_TYPE_DLINE, "%s added D-Line for [%s] [%s]", |
| 146 |
get_oper_name(source_p), conf->host, conf->reason); |
| 147 |
} |
| 148 |
|
| 149 |
dline_check(add_conf_by_address(CONF_DLINE, conf)); |
| 150 |
} |
| 151 |
|
| 152 |
/* mo_dline() |
| 153 |
* |
| 154 |
* inputs - pointer to server |
| 155 |
* - pointer to client |
| 156 |
* - parameter count |
| 157 |
* - parameter list |
| 158 |
* output - |
| 159 |
* side effects - D line is added |
| 160 |
* |
| 161 |
*/ |
| 162 |
static int |
| 163 |
mo_dline(struct Client *source_p, int parc, char *parv[]) |
| 164 |
{ |
| 165 |
char *dlhost = NULL, *reason = NULL; |
| 166 |
char *target_server = NULL; |
| 167 |
const struct Client *target_p = NULL; |
| 168 |
struct irc_ssaddr daddr; |
| 169 |
struct MaskItem *conf = NULL; |
| 170 |
time_t duration = 0; |
| 171 |
int bits = 0, aftype = 0, t = 0; |
| 172 |
char hostip[HOSTIPLEN + 1]; |
| 173 |
|
| 174 |
if (!HasOFlag(source_p, OPER_FLAG_DLINE)) |
| 175 |
{ |
| 176 |
sendto_one_numeric(source_p, &me, ERR_NOPRIVS, "dline"); |
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
if (!parse_aline("DLINE", source_p, parc, parv, AWILD, &dlhost, |
| 181 |
NULL, &duration, &target_server, &reason)) |
| 182 |
return 0; |
| 183 |
|
| 184 |
if (target_server) |
| 185 |
{ |
| 186 |
sendto_match_servs(source_p, target_server, CAPAB_DLN, "DLINE %s %ju %s :%s", |
| 187 |
target_server, duration, |
| 188 |
dlhost, reason); |
| 189 |
|
| 190 |
/* Allow ON to apply local dline as well if it matches */ |
| 191 |
if (match(target_server, me.name)) |
| 192 |
return 0; |
| 193 |
} |
| 194 |
else |
| 195 |
cluster_a_line(source_p, "DLINE", CAPAB_DLN, SHARED_DLINE, |
| 196 |
"%ju %s :%s", duration, dlhost, reason); |
| 197 |
|
| 198 |
if ((t = parse_netmask(dlhost, NULL, NULL)) == HM_HOST) |
| 199 |
{ |
| 200 |
if ((target_p = find_chasing(source_p, dlhost)) == NULL) |
| 201 |
return 0; /* find_chasing sends ERR_NOSUCHNICK */ |
| 202 |
|
| 203 |
if (!MyConnect(target_p)) |
| 204 |
{ |
| 205 |
sendto_one_notice(source_p, &me, ":Cannot DLINE nick on another server"); |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
if (HasFlag(target_p, FLAGS_EXEMPTKLINE)) |
| 210 |
{ |
| 211 |
sendto_one_notice(source_p, &me, ":%s is E-lined", target_p->name); |
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
getnameinfo((const struct sockaddr *)&target_p->connection->ip, |
| 216 |
target_p->connection->ip.ss_len, hostip, |
| 217 |
sizeof(hostip), NULL, 0, NI_NUMERICHOST); |
| 218 |
dlhost = hostip; |
| 219 |
} |
| 220 |
|
| 221 |
switch (parse_netmask(dlhost, &daddr, &bits)) |
| 222 |
{ |
| 223 |
case HM_IPV4: |
| 224 |
if ((unsigned int)bits < ConfigGeneral.dline_min_cidr) |
| 225 |
{ |
| 226 |
sendto_one_notice(source_p, &me, ":For safety, bitmasks less than %u require conf access.", |
| 227 |
ConfigGeneral.dline_min_cidr); |
| 228 |
return 0; |
| 229 |
} |
| 230 |
|
| 231 |
aftype = AF_INET; |
| 232 |
break; |
| 233 |
case HM_IPV6: |
| 234 |
if ((unsigned int)bits < ConfigGeneral.dline_min_cidr6) |
| 235 |
{ |
| 236 |
sendto_one_notice(source_p, &me, ":For safety, bitmasks less than %u require conf access.", |
| 237 |
ConfigGeneral.dline_min_cidr6); |
| 238 |
return 0; |
| 239 |
} |
| 240 |
|
| 241 |
aftype = AF_INET6; |
| 242 |
break; |
| 243 |
default: /* HM_HOST */ |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
if ((conf = find_conf_by_address(NULL, &daddr, CONF_DLINE, aftype, NULL, NULL, 1))) |
| 248 |
{ |
| 249 |
sendto_one_notice(source_p, &me, ":[%s] already D-lined by [%s] - %s", |
| 250 |
dlhost, conf->host, conf->reason); |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
dline_add(source_p, dlhost, reason, duration); |
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
static int |
| 259 |
ms_dline(struct Client *source_p, int parc, char *parv[]) |
| 260 |
{ |
| 261 |
const char *dlhost, *reason; |
| 262 |
struct irc_ssaddr daddr; |
| 263 |
struct MaskItem *conf = NULL; |
| 264 |
time_t duration = 0; |
| 265 |
int bits = 0, aftype = 0; |
| 266 |
|
| 267 |
if (parc != 5 || EmptyString(parv[4])) |
| 268 |
return 0; |
| 269 |
|
| 270 |
/* parv[0] parv[1] parv[2] parv[3] parv[4] */ |
| 271 |
/* command target_server duration host reason */ |
| 272 |
sendto_match_servs(source_p, parv[1], CAPAB_DLN, "DLINE %s %s %s :%s", |
| 273 |
parv[1], parv[2], parv[3], parv[4]); |
| 274 |
|
| 275 |
if (match(parv[1], me.name)) |
| 276 |
return 0; |
| 277 |
|
| 278 |
duration = valid_tkline(parv[2], TK_SECONDS); |
| 279 |
dlhost = parv[3]; |
| 280 |
reason = parv[4]; |
| 281 |
|
| 282 |
if (HasFlag(source_p, FLAGS_SERVICE) || |
| 283 |
find_matching_name_conf(CONF_SHARED, source_p->servptr->name, |
| 284 |
source_p->username, source_p->host, |
| 285 |
SHARED_DLINE)) |
| 286 |
{ |
| 287 |
switch (parse_netmask(dlhost, &daddr, &bits)) |
| 288 |
{ |
| 289 |
case HM_IPV4: |
| 290 |
if ((unsigned int)bits < ConfigGeneral.dline_min_cidr) |
| 291 |
{ |
| 292 |
if (IsClient(source_p)) |
| 293 |
sendto_one_notice(source_p, &me, ":For safety, bitmasks less than %u require conf access.", |
| 294 |
ConfigGeneral.dline_min_cidr); |
| 295 |
return 0; |
| 296 |
} |
| 297 |
|
| 298 |
aftype = AF_INET; |
| 299 |
break; |
| 300 |
case HM_IPV6: |
| 301 |
if ((unsigned int)bits < ConfigGeneral.dline_min_cidr6) |
| 302 |
{ |
| 303 |
if (IsClient(source_p)) |
| 304 |
sendto_one_notice(source_p, &me, ":For safety, bitmasks less than %u require conf access.", |
| 305 |
ConfigGeneral.dline_min_cidr6); |
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
aftype = AF_INET6; |
| 310 |
break; |
| 311 |
default: /* HM_HOST */ |
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
if ((conf = find_conf_by_address(NULL, &daddr, CONF_DLINE, aftype, NULL, NULL, 1))) |
| 316 |
{ |
| 317 |
if (IsClient(source_p)) |
| 318 |
sendto_one_notice(source_p, &me, ":[%s] already D-lined by [%s] - %s", |
| 319 |
dlhost, conf->host, conf->reason); |
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
dline_add(source_p, dlhost, reason, duration); |
| 324 |
} |
| 325 |
|
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
static struct Message dline_msgtab = |
| 330 |
{ |
| 331 |
.cmd = "DLINE", |
| 332 |
.args_min = 2, |
| 333 |
.args_max = MAXPARA, |
| 334 |
.handlers[UNREGISTERED_HANDLER] = m_unregistered, |
| 335 |
.handlers[CLIENT_HANDLER] = m_not_oper, |
| 336 |
.handlers[SERVER_HANDLER] = ms_dline, |
| 337 |
.handlers[ENCAP_HANDLER] = m_ignore, |
| 338 |
.handlers[OPER_HANDLER] = mo_dline |
| 339 |
}; |
| 340 |
|
| 341 |
static void |
| 342 |
module_init(void) |
| 343 |
{ |
| 344 |
mod_add_cmd(&dline_msgtab); |
| 345 |
add_capability("DLN", CAPAB_DLN); |
| 346 |
} |
| 347 |
|
| 348 |
static void |
| 349 |
module_exit(void) |
| 350 |
{ |
| 351 |
mod_del_cmd(&dline_msgtab); |
| 352 |
delete_capability("DLN"); |
| 353 |
} |
| 354 |
|
| 355 |
struct module module_entry = |
| 356 |
{ |
| 357 |
.version = "$Revision$", |
| 358 |
.modinit = module_init, |
| 359 |
.modexit = module_exit, |
| 360 |
}; |