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