unrealircd

- supernets unrealircd source & configuration
git clone git://git.acid.vegas/unrealircd.git
Log | Files | Refs | Archive | README | LICENSE

country.c (3194B)

      1 /*
      2  * Extended ban to ban/exempt by country/geoip info (+b ~country:UK)
      3  * (C) Copyright 2021 The UnrealIRCd Team
      4  *
      5  * This program is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation; either version 1, or (at your option)
      8  * any later version.
      9  *
     10  * This program is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License
     16  * along with this program; if not, write to the Free Software
     17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     18  */
     19 #include "unrealircd.h"
     20 
     21 ModuleHeader MOD_HEADER
     22 = {
     23 	"extbans/country",
     24 	"6.0",
     25 	"ExtBan ~country - Ban/exempt by country (geoip)",
     26 	"UnrealIRCd Team",
     27 	"unrealircd-6",
     28 };
     29 
     30 /* Forward declarations */
     31 int extban_country_is_ok(BanContext *b);
     32 const char *extban_country_conv_param(BanContext *b, Extban *extban);
     33 int extban_country_is_banned(BanContext *b);
     34 
     35 Extban *register_country_extban(ModuleInfo *modinfo)
     36 {
     37 	ExtbanInfo req;
     38 
     39 	memset(&req, 0, sizeof(req));
     40 	req.letter = 'C';
     41 	req.name = "country";
     42 	req.is_ok = extban_country_is_ok;
     43 	req.conv_param = extban_country_conv_param;
     44 	req.is_banned = extban_country_is_banned;
     45 	req.is_banned_events = BANCHK_ALL|BANCHK_TKL;
     46 	req.options = EXTBOPT_INVEX|EXTBOPT_TKL;
     47 	return ExtbanAdd(modinfo->handle, req);
     48 }
     49 
     50 /* Called upon module test */
     51 MOD_TEST()
     52 {
     53 	if (!register_country_extban(modinfo))
     54 	{
     55 		config_error("could not register extended ban type");
     56 		return MOD_FAILED;
     57 	}
     58 
     59 	return MOD_SUCCESS;
     60 }
     61 
     62 /* Called upon module init */
     63 MOD_INIT()
     64 {
     65 	if (!register_country_extban(modinfo))
     66 	{
     67 		config_error("could not register extended ban type");
     68 		return MOD_FAILED;
     69 	}
     70 
     71 	MARK_AS_OFFICIAL_MODULE(modinfo);
     72 
     73 	return MOD_SUCCESS;
     74 }
     75 
     76 /* Called upon module load */
     77 MOD_LOAD()
     78 {
     79 	return MOD_SUCCESS;
     80 }
     81 
     82 /* Called upon unload */
     83 MOD_UNLOAD()
     84 {
     85 	return MOD_SUCCESS;
     86 }
     87 
     88 int extban_country_usage(Client *client)
     89 {
     90 	sendnotice(client, "ERROR: ExtBan ~country expects a two letter country code, or * to ban unknown countries. "
     91 					 "For example: +b ~country:UK");
     92 	return EX_DENY;
     93 }
     94 
     95 int extban_country_is_ok(BanContext *b)
     96 {
     97 	if (b->is_ok_check == EXCHK_PARAM)
     98 	{
     99 		const char *p;
    100 
    101 		if (!strcmp(b->banstr, "*"))
    102 			return EX_ALLOW;
    103 
    104 		if ((strlen(b->banstr) != 2))
    105 			return extban_country_usage(b->client);
    106 
    107 		for (p = b->banstr; *p; p++)
    108 			if (!isalpha(*p))
    109 				return extban_country_usage(b->client);
    110 
    111 		return EX_ALLOW;
    112 	}
    113 	return EX_ALLOW;
    114 }
    115 
    116 /* Obtain targeted country from the ban string */
    117 const char *extban_country_conv_param(BanContext *b, Extban *extban)
    118 {
    119 	static char retbuf[EVP_MAX_MD_SIZE * 2 + 1];
    120 	char *p;
    121 
    122 	strlcpy(retbuf, b->banstr, sizeof(retbuf));
    123 
    124 	for (p = retbuf; *p; p++)
    125 		*p = toupper(*p);
    126 
    127 	return retbuf;
    128 }
    129 
    130 int extban_country_is_banned(BanContext *b)
    131 {
    132 	GeoIPResult *geo = geoip_client(b->client);
    133 	char *country;
    134 
    135 	country = geo ? geo->country_code : "*";
    136 
    137 	if (!strcmp(b->banstr, country))
    138 		return 1;
    139 
    140 	return 0;
    141 }