]> jfr.im git - solanum.git/blobdiff - modules/m_xline.c
Fix m_xline [ci skip]
[solanum.git] / modules / m_xline.c
index f3d87dcac4ec0bc6cf22d9918ed256ff5d340c35..02c162af44019c79baaa1a51e38c1b07b5f8dd07 100644 (file)
@@ -1,5 +1,5 @@
 /* modules/m_xline.c
- * 
+ *
  *  Copyright (C) 2002-2003 Lee Hardy <lee@leeh.co.uk>
  *  Copyright (C) 2002-2005 ircd-ratbox development team
  *
@@ -26,8 +26,6 @@
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
- *
- * $Id$
  */
 
 #include "stdinc.h"
 #include "s_newconf.h"
 #include "reject.h"
 #include "bandbi.h"
+#include "operhash.h"
+
+static const char xline_desc[] =
+       "Provides management of GECOS bans via (UN)XLINE command";
 
-static int mo_xline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
-static int ms_xline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
-static int me_xline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
-static int mo_unxline(struct Client *client_p, struct Client *source_p, int parc,
+static void mo_xline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
+static void ms_xline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
+static void me_xline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
+static void mo_unxline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc,
                      const char *parv[]);
-static int ms_unxline(struct Client *client_p, struct Client *source_p, int parc,
+static void ms_unxline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc,
                      const char *parv[]);
-static int me_unxline(struct Client *client_p, struct Client *source_p, int parc,
+static void me_unxline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc,
                      const char *parv[]);
 
-struct Message xline_msgtab = {
-       "XLINE", 0, 0, 0, MFLG_SLOW,
-       {mg_unreg, mg_not_oper, {ms_xline, 5}, {ms_xline, 5}, {me_xline, 5}, {mo_xline, 3}}
-};
-
-struct Message unxline_msgtab = {
-       "UNXLINE", 0, 0, 0, MFLG_SLOW,
-       {mg_unreg, mg_not_oper, {ms_unxline, 3}, {ms_unxline, 3}, {me_unxline, 2}, {mo_unxline, 2}}
-};
-
-mapi_clist_av1 xline_clist[] = { &xline_msgtab, &unxline_msgtab, NULL };
-
-DECLARE_MODULE_AV1(xline, NULL, NULL, xline_clist, NULL, NULL, "$Revision$");
-
-static int valid_xline(struct Client *, const char *, const char *);
+static bool valid_xline(struct Client *, const char *, const char *);
 static void apply_xline(struct Client *client_p, const char *name,
-                       const char *reason, int temp_time);
+                       const char *reason, int temp_time, bool propagated);
 static void propagate_xline(struct Client *source_p, const char *target,
                            int temp_time, const char *name, const char *type, const char *reason);
 static void cluster_xline(struct Client *source_p, int temp_time,
@@ -87,9 +75,22 @@ static void cluster_xline(struct Client *source_p, int temp_time,
 static void handle_remote_xline(struct Client *source_p, int temp_time,
                                const char *name, const char *reason);
 static void handle_remote_unxline(struct Client *source_p, const char *name);
+static void remove_xline(struct Client *source_p, const char *name,
+                        bool propagated);
+
+struct Message xline_msgtab = {
+       "XLINE", 0, 0, 0, 0,
+       {mg_unreg, mg_not_oper, {ms_xline, 5}, {ms_xline, 5}, {me_xline, 5}, {mo_xline, 3}}
+};
 
-static void remove_xline(struct Client *source_p, const char *name);
+struct Message unxline_msgtab = {
+       "UNXLINE", 0, 0, 0, 0,
+       {mg_unreg, mg_not_oper, {ms_unxline, 3}, {ms_unxline, 3}, {me_unxline, 2}, {mo_unxline, 2}}
+};
+
+mapi_clist_av1 xline_clist[] = { &xline_msgtab, &unxline_msgtab, NULL };
 
+DECLARE_MODULE_AV2(xline, NULL, NULL, xline_clist, NULL, NULL, NULL, NULL, xline_desc);
 
 /* m_xline()
  *
@@ -97,8 +98,8 @@ static void remove_xline(struct Client *source_p, const char *name);
  * parv[2] - optional type/reason
  * parv[3] - reason
  */
-static int
-mo_xline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
+static void
+mo_xline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
        struct ConfItem *aconf;
        const char *name;
@@ -106,11 +107,12 @@ mo_xline(struct Client *client_p, struct Client *source_p, int parc, const char
        const char *target_server = NULL;
        int temp_time;
        int loc = 1;
+       bool propagated = ConfigFileEntry.use_propagated_bans;
 
        if(!IsOperXline(source_p))
        {
                sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "xline");
-               return 0;
+               return;
        }
 
        if((temp_time = valid_temp_time(parv[loc])) >= 0)
@@ -129,7 +131,7 @@ mo_xline(struct Client *client_p, struct Client *source_p, int parc, const char
                {
                        sendto_one(source_p, form_str(ERR_NOPRIVS),
                                   me.name, source_p->name, "remoteban");
-                       return 0;
+                       return;
                }
 
                target_server = parv[loc + 1];
@@ -140,7 +142,7 @@ mo_xline(struct Client *client_p, struct Client *source_p, int parc, const char
        {
                sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
                           me.name, source_p->name, "XLINE");
-               return 0;
+               return;
        }
 
        reason = parv[loc];
@@ -150,32 +152,39 @@ mo_xline(struct Client *client_p, struct Client *source_p, int parc, const char
                propagate_xline(source_p, target_server, temp_time, name, "2", reason);
 
                if(!match(target_server, me.name))
-                       return 0;
+                       return;
+
+               /* Set as local-only. */
+               propagated = false;
        }
-       else if(rb_dlink_list_length(&cluster_conf_list) > 0)
+       else if(!propagated && rb_dlink_list_length(&cluster_conf_list) > 0)
                cluster_xline(source_p, temp_time, name, reason);
 
        if((aconf = find_xline_mask(name)) != NULL)
        {
                sendto_one(source_p, ":%s NOTICE %s :[%s] already X-Lined by [%s] - %s",
-                          me.name, source_p->name, name, aconf->name, aconf->passwd);
-               return 0;
+                          me.name, source_p->name, name, aconf->host, aconf->passwd);
+               return;
        }
 
        if(!valid_xline(source_p, name, reason))
-               return 0;
+               return;
 
-       apply_xline(source_p, name, reason, temp_time);
+       if(propagated && temp_time == 0)
+       {
+               sendto_one_notice(source_p, ":Cannot set a permanent global ban");
+               return;
+       }
 
-       return 0;
+       apply_xline(source_p, name, reason, temp_time, propagated);
 }
 
 /* ms_xline()
  *
  * handles a remote xline
  */
-static int
-ms_xline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
+static void
+ms_xline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
        /* parv[0]  parv[1]      parv[2]  parv[3]  parv[4]
         * oper     target serv  xline    type     reason
@@ -183,25 +192,23 @@ ms_xline(struct Client *client_p, struct Client *source_p, int parc, const char
        propagate_xline(source_p, parv[1], 0, parv[2], parv[3], parv[4]);
 
        if(!IsPerson(source_p))
-               return 0;
+               return;
 
        /* destined for me? */
        if(!match(parv[1], me.name))
-               return 0;
+               return;
 
        handle_remote_xline(source_p, 0, parv[2], parv[4]);
-       return 0;
 }
 
-static int
-me_xline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
+static void
+me_xline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
        /* time name type :reason */
        if(!IsPerson(source_p))
-               return 0;
+               return;
 
        handle_remote_xline(source_p, atoi(parv[1]), parv[2], parv[4]);
-       return 0;
 }
 
 static void
@@ -220,12 +227,12 @@ handle_remote_xline(struct Client *source_p, int temp_time, const char *name, co
        /* already xlined */
        if((aconf = find_xline_mask(name)) != NULL)
        {
-               sendto_one_notice(source_p, ":[%s] already X-Lined by [%s] - %s", name, aconf->name,
+               sendto_one_notice(source_p, ":[%s] already X-Lined by [%s] - %s", name, aconf->host,
                                  aconf->passwd);
                return;
        }
 
-       apply_xline(source_p, name, reason, temp_time);
+       apply_xline(source_p, name, reason, temp_time, false);
 }
 
 /* valid_xline()
@@ -234,26 +241,14 @@ handle_remote_xline(struct Client *source_p, int temp_time, const char *name, co
  * outputs     -
  * side effects - checks the xline for validity, erroring if needed
  */
-static int
+static bool
 valid_xline(struct Client *source_p, const char *gecos, const char *reason)
 {
        if(EmptyString(reason))
        {
                sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
                           get_id(&me, source_p), get_id(source_p, source_p), "XLINE");
-               return 0;
-       }
-
-       if(strchr(reason, ':') != NULL)
-       {
-               sendto_one_notice(source_p, ":Invalid character ':' in comment");
-               return 0;
-       }
-
-       if(strchr(reason, '"'))
-       {
-               sendto_one_notice(source_p, ":Invalid character '\"' in comment");
-               return 0;
+               return false;
        }
 
        if(!valid_wild_card_simple(gecos))
@@ -262,79 +257,72 @@ valid_xline(struct Client *source_p, const char *gecos, const char *reason)
                                  ":Please include at least %d non-wildcard "
                                  "characters with the xline",
                                  ConfigFileEntry.min_nonwildcard_simple);
-               return 0;
+               return false;
        }
 
-       return 1;
+       return true;
 }
 
 void
-apply_xline(struct Client *source_p, const char *name, const char *reason, int temp_time)
+apply_xline(struct Client *source_p, const char *name, const char *reason, int temp_time, bool propagated)
 {
        struct ConfItem *aconf;
 
        aconf = make_conf();
        aconf->status = CONF_XLINE;
+       aconf->created = rb_current_time();
+       aconf->host = rb_strdup(name);
+       aconf->passwd = rb_strdup(reason);
+       collapse(aconf->host);
+
+       aconf->info.oper = operhash_add(get_oper_name(source_p));
 
-       if(strstr(name, "\\s"))
+       if(propagated)
        {
-               char *tmp = LOCAL_COPY(name);
-               char *orig = tmp;
-               char *new = tmp;
+               aconf->flags |= CONF_FLAGS_MYOPER | CONF_FLAGS_TEMPORARY;
+               aconf->hold = rb_current_time() + temp_time;
+               aconf->lifetime = aconf->hold;
 
-               while(*orig)
-               {
-                       if(*orig == '\\' && *(orig + 1) != '\0')
-                       {
-                               if(*(orig + 1) == 's')
-                               {
-                                       *new++ = ' ';
-                                       orig += 2;
-                               }
-                               /* otherwise skip that and the escaped
-                                * character after it, so we dont mistake
-                                * \\s as \s --fl
-                                */
-                               else
-                               {
-                                       *new++ = *orig++;
-                                       *new++ = *orig++;
-                               }
-                       }
-                       else
-                               *new++ = *orig++;
-               }
+               replace_old_ban(aconf);
+               rb_dlinkAddAlloc(aconf, &prop_bans);
 
-               *new = '\0';
-               aconf->name = rb_strdup(tmp);
+               sendto_realops_snomask(SNO_GENERAL, L_ALL,
+                                      "%s added global %d min. X-Line for [%s] [%s]",
+                                      get_oper_name(source_p), temp_time / 60,
+                                      aconf->host, reason);
+               ilog(L_KLINE, "X %s %d %s %s",
+                    get_oper_name(source_p), temp_time / 60, name, reason);
+               sendto_one_notice(source_p, ":Added global %d min. X-Line [%s]",
+                                 temp_time / 60, aconf->host);
+               sendto_server(NULL, NULL, CAP_BAN|CAP_TS6, NOCAPS,
+                               ":%s BAN X * %s %lu %d %d * :%s",
+                               source_p->id, aconf->host,
+                               (unsigned long)aconf->created,
+                               (int)(aconf->hold - aconf->created),
+                               (int)(aconf->lifetime - aconf->created),
+                               reason);
        }
-       else
-               aconf->name = rb_strdup(name);
-
-       aconf->passwd = rb_strdup(reason);
-       collapse(aconf->name);
-
-       if(temp_time > 0)
+       else if(temp_time > 0)
        {
                aconf->hold = rb_current_time() + temp_time;
 
                sendto_realops_snomask(SNO_GENERAL, L_ALL,
                                       "%s added temporary %d min. X-Line for [%s] [%s]",
                                       get_oper_name(source_p), temp_time / 60,
-                                      aconf->name, reason);
+                                      aconf->host, reason);
                ilog(L_KLINE, "X %s %d %s %s",
                     get_oper_name(source_p), temp_time / 60, name, reason);
                sendto_one_notice(source_p, ":Added temporary %d min. X-Line [%s]",
-                                 temp_time / 60, aconf->name);
+                                 temp_time / 60, aconf->host);
        }
        else
        {
                sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s added X-Line for [%s] [%s]",
-                                      get_oper_name(source_p), aconf->name, aconf->passwd);
+                                      get_oper_name(source_p), aconf->host, aconf->passwd);
                sendto_one_notice(source_p, ":Added X-Line for [%s] [%s]",
-                                 aconf->name, aconf->passwd);
+                                 aconf->host, aconf->passwd);
 
-               bandb_add(BANDB_XLINE, source_p, aconf->name, NULL, aconf->passwd, NULL, 0);
+               bandb_add(BANDB_XLINE, source_p, aconf->host, NULL, aconf->passwd, NULL, 0);
                ilog(L_KLINE, "X %s 0 %s %s", get_oper_name(source_p), name, aconf->passwd);
        }
 
@@ -394,13 +382,15 @@ cluster_xline(struct Client *source_p, int temp_time, const char *name, const ch
  *
  * parv[1] - thing to unxline
  */
-static int
-mo_unxline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
+static void
+mo_unxline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
+       bool propagated = true;
+
        if(!IsOperXline(source_p))
        {
                sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "xline");
-               return 0;
+               return;
        }
 
        if(parc == 4 && !(irccmp(parv[2], "ON")))
@@ -409,28 +399,27 @@ mo_unxline(struct Client *client_p, struct Client *source_p, int parc, const cha
                {
                        sendto_one(source_p, form_str(ERR_NOPRIVS),
                                   me.name, source_p->name, "remoteban");
-                       return 0;
+                       return;
                }
 
                propagate_generic(source_p, "UNXLINE", parv[3], CAP_CLUSTER, "%s", parv[1]);
 
                if(match(parv[3], me.name) == 0)
-                       return 0;
-       }
-       else if(rb_dlink_list_length(&cluster_conf_list))
-               cluster_generic(source_p, "UNXLINE", SHARED_UNXLINE, CAP_CLUSTER, "%s", parv[1]);
+                       return;
 
-       remove_xline(source_p, parv[1]);
+               propagated = false;
+       }
+       /* cluster{} moved to remove_xline */
 
-       return 0;
+       remove_xline(source_p, parv[1], propagated);
 }
 
 /* ms_unxline()
  *
  * handles a remote unxline
  */
-static int
-ms_unxline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
+static void
+ms_unxline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
        /* parv[0]  parv[1]        parv[2]
         * oper     target server  gecos
@@ -438,24 +427,22 @@ ms_unxline(struct Client *client_p, struct Client *source_p, int parc, const cha
        propagate_generic(source_p, "UNXLINE", parv[1], CAP_CLUSTER, "%s", parv[2]);
 
        if(!match(parv[1], me.name))
-               return 0;
+               return;
 
        if(!IsPerson(source_p))
-               return 0;
+               return;
 
        handle_remote_unxline(source_p, parv[2]);
-       return 0;
 }
 
-static int
-me_unxline(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
+static void
+me_unxline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
        /* name */
        if(!IsPerson(source_p))
-               return 0;
+               return;
 
        handle_remote_unxline(source_p, parv[1]);
-       return 0;
 }
 
 static void
@@ -465,23 +452,58 @@ handle_remote_unxline(struct Client *source_p, const char *name)
                             source_p->servptr->name, SHARED_UNXLINE))
                return;
 
-       remove_xline(source_p, name);
-
-       return;
+       remove_xline(source_p, name, false);
 }
 
 static void
-remove_xline(struct Client *source_p, const char *name)
+remove_xline(struct Client *source_p, const char *name, bool propagated)
 {
        struct ConfItem *aconf;
        rb_dlink_node *ptr;
+       time_t now;
 
        RB_DLINK_FOREACH(ptr, xline_conf_list.head)
        {
                aconf = ptr->data;
 
-               if(!irccmp(aconf->name, name))
+               if(!irccmp(aconf->host, name))
                {
+                       if(aconf->lifetime)
+                       {
+                               if(!propagated)
+                               {
+                                       sendto_one_notice(source_p, ":Cannot remove global X-Line %s on specific servers", name);
+                                       return;
+                               }
+                               ptr = rb_dlinkFind(aconf, &prop_bans);
+                               if(ptr == NULL)
+                                       return;
+                               sendto_one_notice(source_p, ":X-Line for [%s] is removed", name);
+                               sendto_realops_snomask(SNO_GENERAL, L_ALL,
+                                                      "%s has removed the global X-Line for: [%s]",
+                                                      get_oper_name(source_p), name);
+                               ilog(L_KLINE, "UX %s %s", get_oper_name(source_p), name);
+                               now = rb_current_time();
+                               if(aconf->created < now)
+                                       aconf->created = now;
+                               else
+                                       aconf->created++;
+                               aconf->hold = aconf->created;
+                               operhash_delete(aconf->info.oper);
+                               aconf->info.oper = operhash_add(get_oper_name(source_p));
+                               aconf->flags |= CONF_FLAGS_MYOPER | CONF_FLAGS_TEMPORARY;
+                               sendto_server(NULL, NULL, CAP_BAN|CAP_TS6, NOCAPS,
+                                               ":%s BAN X * %s %lu %d %d * :*",
+                                               source_p->id, aconf->host,
+                                               (unsigned long)aconf->created,
+                                               0,
+                                               (int)(aconf->lifetime - aconf->created));
+                               remove_reject_mask(aconf->host, NULL);
+                               deactivate_conf(aconf, ptr, now);
+                               return;
+                       }
+                       else if(propagated && rb_dlink_list_length(&cluster_conf_list))
+                               cluster_generic(source_p, "UNXLINE", SHARED_UNXLINE, CAP_CLUSTER, "%s", name);
                        if(!aconf->hold)
                        {
                                bandb_del(BANDB_XLINE, aconf->host, NULL);
@@ -501,14 +523,15 @@ remove_xline(struct Client *source_p, const char *name)
                                ilog(L_KLINE, "UX %s %s", get_oper_name(source_p), name);
                        }
 
-                       remove_reject_mask(aconf->name, NULL);
+                       remove_reject_mask(aconf->host, NULL);
                        free_conf(aconf);
                        rb_dlinkDestroy(ptr, &xline_conf_list);
                        return;
                }
        }
 
-       sendto_one_notice(source_p, ":No X-Line for %s", name);
+       if(propagated && rb_dlink_list_length(&cluster_conf_list))
+               cluster_generic(source_p, "UNXLINE", SHARED_UNXLINE, CAP_CLUSTER, "%s", name);
 
-       return;
+       sendto_one_notice(source_p, ":No X-Line for %s", name);
 }