"Fossies" - the Fresh Open Source Software Archive

Member "ircd-hybrid-8.2.26/modules/m_dline.c" (31 May 2019, 8373 Bytes) of package /linux/privat/ircd-hybrid-8.2.26.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "m_dline.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 8.2.25_vs_8.2.26.

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