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