m_unauthban

z k4be, 5 lata temu, napisane w C, wyświetlone 407 razy.
URL https://pastebin.k4be.pl/view/085eec9e Udostępnij
Pobierz wklejkę lub Pokaż surowy tekst
  1. /*
  2.  * Based on timedban.c. Created by k4be.
  3.  * (C) Copyright 2009-2017 Bram Matthys (Syzop) and the UnrealIRCd team.
  4.  * License: GPLv2
  5.  *
  6.  * This module adds an extended ban ~I:mask
  7.  * Ban prefixed with ~I will match only users that are not identified
  8.  * to services.
  9.  *
  10.  * Note that this extended ban is rather special in the sense that
  11.  * it permits (crazy) triple-extbans to be set, such as:
  12.  * +b ~I:~q:~c:#channel
  13.  * (=mute a user that is also on #channel, unless he's idenitifed to services)
  14.  *
  15.  * The triple-extbans / double-stacking requires special routines that
  16.  * are based on parts of the core and special recursion checks.
  17.  * If you are looking for inspiration of coding your own extended ban
  18.  * then look at another extended ban * module as this module is not a
  19.  * good starting point ;)
  20.  */
  21.    
  22. #include "unrealircd.h"
  23.  
  24. /* Maximum length of a ban */
  25. #define MAX_LENGTH 128
  26.  
  27. ModuleHeader MOD_HEADER(m_unauthban)
  28.   = {
  29.         "m_unauthban",
  30.         "$Id: v1.00 2018/11/05 k4be$",
  31.         "ExtBan ~I: bans that match only users that are not logged in",
  32.         "3.2-b8-1",
  33.         NULL
  34.     };
  35.  
  36. /* Forward declarations */
  37. char *unauthban_extban_conv_param(char *para_in);
  38. int unauthban_extban_is_ok(aClient* sptr, aChannel* chptr, char* para_in, int checkt, int what, int what2);
  39. int unauthban_is_banned(aClient *sptr, aChannel *chptr, char *ban, int chktype);
  40. void add_send_mode_param(aChannel *chptr, aClient *from, char what, char mode, char *param);
  41. char *unauthban_chanmsg(aClient *, aClient *, aChannel *, char *, int);
  42.  
  43. MOD_TEST(m_unauthban)
  44. {
  45.         return MOD_SUCCESS;
  46. }
  47.  
  48. MOD_INIT(m_unauthban)
  49. {
  50. ExtbanInfo extban;
  51.  
  52.         memset(&extban, 0, sizeof(ExtbanInfo));
  53.         extban.flag = 'I';
  54.         extban.options |= EXTBOPT_ACTMODIFIER; /* not really, but ours shouldn't be stacked from group 1 */
  55.         extban.options |= EXTBOPT_CHSVSMODE; /* so "SVSMODE -nick" will unset affected ~t extbans */
  56.         //extban.options |= EXTBOPT_INVEX; /* invex probably would be useless */
  57.         extban.conv_param = unauthban_extban_conv_param;
  58.         extban.is_ok = unauthban_extban_is_ok;
  59.         extban.is_banned = unauthban_is_banned;
  60.  
  61.         if (!ExtbanAdd(modinfo->handle, extban))
  62.         {
  63.                 config_error("m_unauthban: unable to register 'I' extban type!!");
  64.                 return MOD_FAILED;
  65.         }
  66.                
  67.         return MOD_SUCCESS;
  68. }
  69.  
  70. MOD_LOAD(timedban)
  71. {
  72.         return MOD_SUCCESS;
  73. }
  74.  
  75. MOD_UNLOAD(timedban)
  76. {
  77.         return MOD_SUCCESS;
  78. }
  79.  
  80. /** Generic helper for our conv_param extban function.
  81.  * Mostly copied from clean_ban_mask()
  82.  */
  83. char *generic_clean_ban_mask(char *mask)
  84. {
  85.         char *cp, *x;
  86.         char *user;
  87.         char *host;
  88.         Extban *p;
  89.         static char maskbuf[512];
  90.  
  91.         /* Work on a copy */
  92.         strlcpy(maskbuf, mask, sizeof(maskbuf));
  93.         mask = maskbuf;
  94.  
  95.         cp = index(mask, ' ');
  96.         if (cp)
  97.                 *cp = '\0';
  98.  
  99.         /* Strip any ':' at beginning since that would cause a desynch */
  100.         for (; (*mask && (*mask == ':')); mask++);
  101.         if (!*mask)
  102.                 return NULL;
  103.  
  104.         /* Forbid ASCII <= 32 in all bans */
  105.         for (x = mask; *x; x++)
  106.                 if (*x <= ' ')
  107.                         return NULL;
  108.  
  109.         /* Extended ban? */
  110.         if ((*mask == '~') && mask[1] && (mask[2] == ':'))
  111.         {
  112.                 p = findmod_by_bantype(mask[1]);
  113.                 if (!p)
  114.                         return NULL; /* reject unknown extban */
  115.                 if (p->conv_param)
  116.                         return p->conv_param(mask);
  117.                 /* else, do some basic sanity checks and cut it off at 80 bytes */
  118.                 if ((mask[1] != ':') || (mask[2] == '\0'))
  119.                     return NULL; /* require a ":<char>" after extban type */
  120.                 if (strlen(mask) > 80)
  121.                         mask[80] = '\0';
  122.                 return mask;
  123.         }
  124.  
  125.         if ((*mask == '~') && !strchr(mask, '@'))
  126.                 return NULL; /* not an extended ban and not a ~user@host ban either. */
  127.  
  128.         if ((user = index((cp = mask), '!')))
  129.                 *user++ = '\0';
  130.         if ((host = rindex(user ? user : cp, '@')))
  131.         {
  132.                 *host++ = '\0';
  133.  
  134.                 if (!user)
  135.                         return make_nick_user_host(NULL, trim_str(cp,USERLEN),
  136.                                 trim_str(host,HOSTLEN));
  137.         }
  138.         else if (!user && index(cp, '.'))
  139.                 return make_nick_user_host(NULL, NULL, trim_str(cp,HOSTLEN));
  140.         return make_nick_user_host(trim_str(cp,NICKLEN), trim_str(user,USERLEN),
  141.                 trim_str(host,HOSTLEN));
  142. }
  143.  
  144. /** Convert ban to an acceptable format (or return NULL to fully reject it) */
  145. char *unauthban_extban_conv_param(char *para_in)
  146. {
  147.         static char retbuf[MAX_LENGTH+1];
  148.         char para[MAX_LENGTH+1];
  149.         char tmpmask[MAX_LENGTH+1];
  150.         char *newmask; /**< Cleaned matching method, such as 'n!u@h' */
  151.         static int unauthban_extban_conv_param_recursion = 0;
  152.        
  153.         if (unauthban_extban_conv_param_recursion)
  154.                 return NULL; /* reject: recursion detected! */
  155.  
  156.         strlcpy(para, para_in+3, sizeof(para)); /* work on a copy (and truncate it) */
  157.        
  158.         /* ~I:n!u@h   for direct matching
  159.          * ~I:~x:.... when calling another bantype
  160.          */
  161.  
  162.         strlcpy(tmpmask, para, sizeof(tmpmask));
  163.         unauthban_extban_conv_param_recursion++;
  164.         //newmask = extban_conv_param_nuh_or_extban(tmpmask);
  165.         newmask = generic_clean_ban_mask(tmpmask);
  166.         unauthban_extban_conv_param_recursion--;
  167.         if (!newmask || (strlen(newmask) <= 1))
  168.                 return NULL;
  169.  
  170.         snprintf(retbuf, sizeof(retbuf), "~I:%s", newmask);
  171.         return retbuf;
  172. }
  173.  
  174. int unauthban_extban_syntax(aClient *sptr, int checkt, char *reason)
  175. {
  176.         if (MyClient(sptr) && (checkt == EXBCHK_PARAM))
  177.         {
  178.                 sendnotice(sptr, "Error when setting unauth ban: %s", reason);
  179.                 sendnotice(sptr, " Syntax: +b ~I:mask");
  180.                 sendnotice(sptr, "Example: +b ~I:nick!user@host");
  181.                 sendnotice(sptr, "Valid masks are: nick!user@host or another extban type such as ~a, ~c, ~S, ..");
  182.         }
  183.         return 0; /* FAIL: ban rejected */
  184. }
  185.  
  186. /** Generic helper for sub-bans, used by our "is this ban ok?" function */
  187. int generic_ban_is_ok(aClient *sptr, aChannel *chptr, char *mask, int checkt, int what, int what2)
  188. {
  189.         if ((mask[0] == '~') && MyClient(sptr))
  190.         {
  191.                 Extban *p;
  192.  
  193.                 /* This portion is copied from clean_ban_mask() */
  194.                 if (mask[1] && (mask[2] == ':') &&
  195.                     RESTRICT_EXTENDEDBANS && MyClient(sptr) &&
  196.                     !ValidatePermissionsForPath("channel:extbans",sptr,NULL,NULL,NULL))
  197.                 {
  198.                         if (!strcmp(RESTRICT_EXTENDEDBANS, "*"))
  199.                         {
  200.                                 if (checkt == EXBCHK_ACCESS_ERR)
  201.                                         sendnotice(sptr, "Setting/removing of extended bans has been disabled");
  202.                                 return 0; /* REJECT */
  203.                         }
  204.                         if (strchr(RESTRICT_EXTENDEDBANS, mask[1]))
  205.                         {
  206.                                 if (checkt == EXBCHK_ACCESS_ERR)
  207.                                         sendnotice(sptr, "Setting/removing of extended bantypes '%s' has been disabled", RESTRICT_EXTENDEDBANS);
  208.                                 return 0; /* REJECT */
  209.                         }
  210.                 }
  211.                 /* End of portion */
  212.  
  213.                 /* This portion is inspired by m_mode */
  214.                 p = findmod_by_bantype(mask[1]);
  215.                 if (checkt == EXBCHK_ACCESS)
  216.                 {
  217.                         if (p && p->is_ok && !p->is_ok(sptr, chptr, mask, EXBCHK_ACCESS, what, what2) &&
  218.                             !ValidatePermissionsForPath("override:extban",sptr,NULL,chptr,NULL))
  219.                         {
  220.                                 return 0; /* REJECT */
  221.                         }
  222.                 } else
  223.                 if (checkt == EXBCHK_ACCESS_ERR)
  224.                 {
  225.                         if (p && p->is_ok && !p->is_ok(sptr, chptr, mask, EXBCHK_ACCESS, what, what2) &&
  226.                             !ValidatePermissionsForPath("override:extban",sptr,NULL,chptr,NULL))
  227.                         {
  228.                                 p->is_ok(sptr, chptr, mask, EXBCHK_ACCESS_ERR, what, what2);
  229.                                 return 0; /* REJECT */
  230.                         }
  231.                 } else
  232.                 if (checkt == EXBCHK_PARAM)
  233.                 {
  234.                         if (p && p->is_ok && !p->is_ok(sptr, chptr, mask, EXBCHK_PARAM, what, what2))
  235.                         {
  236.                                 return 0; /* REJECT */
  237.                         }
  238.                 }
  239.                 /* End of portion */
  240.         }
  241.        
  242.         /* ACCEPT:
  243.          * - not an extban; OR
  244.          * - extban with NULL is_ok; OR
  245.          * - non-existing extban character (handled by conv_param?)
  246.          */
  247.         return 1;
  248. }
  249.  
  250. /** Validate ban ("is this ban ok?") */
  251. int unauthban_extban_is_ok(aClient* sptr, aChannel* chptr, char* para_in, int checkt, int what, int what2)
  252. {
  253.         char para[MAX_LENGTH+1];
  254.         char tmpmask[MAX_LENGTH+1];
  255.         char *newmask; /**< Cleaned matching method, such as 'n!u@h' */
  256.         static int unauthban_extban_is_ok_recursion = 0;
  257.         int res;
  258.  
  259.         /* Always permit deletion */
  260.         if (what == MODE_DEL)
  261.                 return 1;
  262.  
  263.         if (unauthban_extban_is_ok_recursion)
  264.                 return 0; /* Recursion detected (~I:~I:....) */
  265.  
  266.         strlcpy(para, para_in+3, sizeof(para)); /* work on a copy (and truncate it) */
  267.        
  268.         /* ~I:n!u@h   for direct matching
  269.          * ~I:~x:.... when calling another bantype
  270.          */
  271.         strlcpy(tmpmask, para, sizeof(tmpmask));
  272.         unauthban_extban_is_ok_recursion++;
  273.         //res = extban_is_ok_nuh_extban(sptr, chptr, tmpmask, checkt, what, what2);
  274.         res = generic_ban_is_ok(sptr, chptr, tmpmask, checkt, what, what2);
  275.         unauthban_extban_is_ok_recursion--;
  276.         if (res == 0)
  277.         {
  278.                 /* This could be anything ranging from:
  279.                  * invalid n!u@h syntax, unknown (sub)extbantype,
  280.                  * disabled extban type in conf, too much recursion, etc.
  281.                  */
  282.                 return unauthban_extban_syntax(sptr, checkt, "Invalid matcher");
  283.         }
  284.  
  285.         return 1; /* OK */
  286. }
  287.  
  288. /** Check if the user is currently banned */
  289. int unauthban_is_banned(aClient *sptr, aChannel *chptr, char *ban, int chktype)
  290. {
  291.         if (strncmp(ban, "~I:", 3))
  292.                 return 0; /* not for us */
  293.  
  294.         if(IsLoggedIn(sptr)) return 0; // this is the magic
  295.         ban += 3; // skip extban prefix
  296.         return ban_check_mask(sptr, chptr, ban, chktype, 0); // if not logged in, process as normal ban
  297. }
  298.  
  299. static char mbuf[512];
  300. static char pbuf[512];
  301.  
  302. #if MODEBUFLEN > 512
  303.  #error "add_send_mode_param() is not made for MODEBUFLEN > 512"
  304. #endif
  305.  
  306. void add_send_mode_param(aChannel *chptr, aClient *from, char what, char mode, char *param) {
  307.         static char *modes = NULL, lastwhat;
  308.         static short count = 0;
  309.         short send = 0;
  310.        
  311.         if (!modes) modes = mbuf;
  312.        
  313.         if (!mbuf[0]) {
  314.                 modes = mbuf;
  315.                 *modes++ = what;
  316.                 *modes = 0;
  317.                 lastwhat = what;
  318.                 *pbuf = 0;
  319.                 count = 0;
  320.         }
  321.         if (lastwhat != what) {
  322.                 *modes++ = what;
  323.                 *modes = 0;
  324.                 lastwhat = what;
  325.         }
  326.         if (strlen(pbuf) + strlen(param) + 11 < MODEBUFLEN) {
  327.                 if (*pbuf)
  328.                         strcat(pbuf, " ");
  329.                 strcat(pbuf, param);
  330.                 *modes++ = mode;
  331.                 *modes = 0;
  332.                 count++;
  333.         }
  334.         else if (*pbuf)
  335.                 send = 1;
  336.  
  337.         if (count == MAXMODEPARAMS)
  338.                 send = 1;
  339.  
  340.         if (send) {
  341.                 sendto_channel_butserv(chptr, &me, ":%s MODE %s %s %s", me.name, chptr->chname, mbuf, pbuf);
  342.                 sendto_server(NULL, 0, 0, ":%s MODE %s %s %s 0", me.name, chptr->chname, mbuf, pbuf);
  343.                 send = 0;
  344.                 *pbuf = 0;
  345.                 modes = mbuf;
  346.                 *modes++ = what;
  347.                 lastwhat = what;
  348.                 if (count != MAXMODEPARAMS) {
  349.                         strlcpy(pbuf, param, sizeof(pbuf));
  350.                         *modes++ = mode;
  351.                         count = 1;
  352.                 }
  353.                 else
  354.                         count = 0;
  355.                 *modes = 0;
  356.         }
  357. }
  358.  

odpowiedź "m_unauthban"

Tutaj możesz odpowiedzieć na wklejkę z góry

captcha