zapret/nfq/hostlist.c

212 lines
4.6 KiB
C
Raw Normal View History

2021-03-04 14:30:38 +03:00
#include <stdio.h>
#include "hostlist.h"
#include "gzip.h"
#include "helpers.h"
2021-03-04 14:30:38 +03:00
2023-10-26 15:12:32 +03:00
// inplace tolower() and add to pool
static bool addpool(strpool **hostlist, char **s, const char *end, int *ct)
2021-03-04 14:30:38 +03:00
{
char *p=*s;
// comment line
if ( *p == '#' || *p == ';' || *p == '/' || *p == '\r' || *p == '\n')
2021-03-04 14:30:38 +03:00
{
// advance until eol
for (; p<end && *p && *p!='\r' && *p != '\n'; p++);
}
else
{
// advance until eol lowering all chars
for (; p<end && *p && *p!='\r' && *p != '\n'; p++) *p=tolower(*p);
if (!StrPoolAddStrLen(hostlist, *s, p-*s))
{
StrPoolDestroy(hostlist);
*hostlist = NULL;
return false;
}
(*ct)++;
2021-03-04 14:30:38 +03:00
}
// advance to the next line
for (; p<end && (!*p || *p=='\r' || *p=='\n') ; p++);
*s = p;
return true;
}
bool AppendHostList(strpool **hostlist, const char *filename)
2021-03-04 14:30:38 +03:00
{
char *p, *e, s[256], *zbuf;
size_t zsize;
int ct = 0;
FILE *F;
int r;
2024-08-23 14:42:21 +03:00
DLOG_CONDUP("Loading hostlist %s\n",filename);
2021-03-04 14:30:38 +03:00
if (!(F = fopen(filename, "rb")))
{
2024-08-23 14:42:21 +03:00
DLOG_ERR("Could not open %s\n", filename);
2021-03-04 14:30:38 +03:00
return false;
}
if (is_gzip(F))
{
r = z_readfile(F,&zbuf,&zsize);
fclose(F);
if (r==Z_OK)
{
2024-08-23 14:42:21 +03:00
DLOG_CONDUP("zlib compression detected. uncompressed size : %zu\n", zsize);
2024-10-16 14:25:16 +03:00
2021-03-04 14:30:38 +03:00
p = zbuf;
e = zbuf + zsize;
while(p<e)
{
if (!addpool(hostlist,&p,e,&ct))
2021-03-04 14:30:38 +03:00
{
2024-08-23 14:42:21 +03:00
DLOG_ERR("Not enough memory to store host list : %s\n", filename);
2021-03-04 14:30:38 +03:00
free(zbuf);
return false;
}
}
free(zbuf);
}
else
{
2024-08-23 14:42:21 +03:00
DLOG_ERR("zlib decompression failed : result %d\n",r);
2021-03-04 14:30:38 +03:00
return false;
}
}
else
{
2024-08-23 14:42:21 +03:00
DLOG_CONDUP("loading plain text list\n");
while (fgets(s, sizeof(s), F))
2021-03-04 14:30:38 +03:00
{
p = s;
if (!addpool(hostlist,&p,p+strlen(p),&ct))
2021-03-04 14:30:38 +03:00
{
2024-08-23 14:42:21 +03:00
DLOG_ERR("Not enough memory to store host list : %s\n", filename);
2021-03-04 14:30:38 +03:00
fclose(F);
return false;
}
}
fclose(F);
}
2024-08-23 14:42:21 +03:00
DLOG_CONDUP("Loaded %d hosts from %s\n", ct, filename);
2021-03-04 14:30:38 +03:00
return true;
}
bool LoadHostLists(strpool **hostlist, struct str_list_head *file_list)
{
struct str_list *file;
if (*hostlist)
{
StrPoolDestroy(hostlist);
*hostlist = NULL;
}
2021-03-04 14:30:38 +03:00
LIST_FOREACH(file, file_list, next)
{
if (!AppendHostList(hostlist, file->str)) return false;
}
return true;
}
2023-10-26 15:12:32 +03:00
bool NonEmptyHostlist(strpool **hostlist)
{
// add impossible hostname if the list is empty
return *hostlist ? true : StrPoolAddStrLen(hostlist, "@&()", 4);
}
bool SearchHostList(strpool *hostlist, const char *host)
2021-03-04 14:30:38 +03:00
{
if (hostlist)
{
const char *p = host;
bool bInHostList;
while (p)
{
bInHostList = StrPoolCheckStr(hostlist, p);
2024-08-23 14:42:21 +03:00
DLOG("Hostlist check for %s : %s\n", p, bInHostList ? "positive" : "negative");
2021-03-04 14:30:38 +03:00
if (bInHostList) return true;
p = strchr(p, '.');
if (p) p++;
}
}
return false;
}
// return : true = apply fooling, false = do not apply
static bool HostlistCheck_(strpool *hostlist, strpool *hostlist_exclude, const char *host, bool *excluded)
{
2023-10-26 15:12:32 +03:00
if (excluded) *excluded = false;
if (hostlist_exclude)
{
2024-08-23 14:42:21 +03:00
DLOG("Checking exclude hostlist\n");
2023-10-26 15:12:32 +03:00
if (SearchHostList(hostlist_exclude, host))
{
if (excluded) *excluded = true;
return false;
}
}
if (hostlist)
{
2024-08-23 14:42:21 +03:00
DLOG("Checking include hostlist\n");
return SearchHostList(hostlist, host);
}
return true;
}
2024-09-17 19:57:21 +03:00
static bool LoadIncludeHostListsForProfile(struct desync_profile *dp)
{
if (!LoadHostLists(&dp->hostlist, &dp->hostlist_files))
return false;
if (*dp->hostlist_auto_filename)
{
dp->hostlist_auto_mod_time = file_mod_time(dp->hostlist_auto_filename);
NonEmptyHostlist(&dp->hostlist);
}
return true;
}
// return : true = apply fooling, false = do not apply
2024-09-17 19:57:21 +03:00
bool HostlistCheck(struct desync_profile *dp, const char *host, bool *excluded)
{
2024-10-16 13:42:59 +03:00
DLOG("* hostlist check for profile %d\n",dp->n);
2024-09-17 19:57:21 +03:00
if (*dp->hostlist_auto_filename)
{
2024-09-17 19:57:21 +03:00
time_t t = file_mod_time(dp->hostlist_auto_filename);
if (t!=dp->hostlist_auto_mod_time)
{
2024-09-17 19:57:21 +03:00
DLOG_CONDUP("Autohostlist '%s' from profile %d was modified. Reloading include hostlists for this profile.\n",dp->hostlist_auto_filename, dp->n);
if (!LoadIncludeHostListsForProfile(dp))
{
// what will we do without hostlist ?? sure, gonna die
exit(1);
}
2024-09-17 19:57:21 +03:00
dp->hostlist_auto_mod_time = t;
NonEmptyHostlist(&dp->hostlist);
}
}
2024-09-17 19:57:21 +03:00
return HostlistCheck_(dp->hostlist, dp->hostlist_exclude, host, excluded);
}
bool LoadIncludeHostLists()
{
2024-09-17 19:57:21 +03:00
struct desync_profile_list *dpl;
LIST_FOREACH(dpl, &params.desync_profiles, next)
if (!LoadIncludeHostListsForProfile(&dpl->dp))
return false;
return true;
}
bool LoadExcludeHostLists()
{
2024-09-17 19:57:21 +03:00
struct desync_profile_list *dpl;
LIST_FOREACH(dpl, &params.desync_profiles, next)
if (!LoadHostLists(&dpl->dp.hostlist_exclude, &dpl->dp.hostlist_exclude_files))
return false;
return true;
}