Template
1
0
mirror of https://github.com/bol-van/zapret2.git synced 2026-03-14 06:13:09 +00:00

nfqws2: remember absolute paths

This commit is contained in:
bol-van
2026-02-14 18:08:18 +03:00
parent eb9a1e9f6b
commit 97819327cd
5 changed files with 101 additions and 17 deletions

View File

@@ -9,9 +9,15 @@
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <libgen.h> #include <libgen.h>
#include <limits.h>
#include <errno.h> #include <errno.h>
#include <sys/param.h> #include <sys/param.h>
#ifdef __CYGWIN__
#include <sys/cygwin.h>
#endif
#define UNIQ_SORT \ #define UNIQ_SORT \
{ \ { \
size_t i, j, u; \ size_t i, j, u; \
@@ -670,6 +676,53 @@ bool set_env_exedir(const char *argv0)
return bOK; return bOK;
} }
// works for existing and new files
bool realpath_any(const char *file, char *pabs)
{
bool b = true;
char *s1=NULL, *s2=NULL;
int res;
size_t l;
#ifdef __CYGWIN__
l = cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, file, NULL, 0);
char *rp_file = (char*)malloc(l);
if (cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, file, rp_file, l))
goto err;
#else
#define rp_file file
#endif
if (!realpath(rp_file,pabs))
{
char pa[PATH_MAX], *dir, *base;
if (!(s1 = strdup(rp_file))) goto err;
dir = dirname(s1);
if (!realpath(dir,pa))
goto err;
if (!(s2 = strdup(rp_file))) goto err;
base = basename(s2);
l = strlen(pa);
if (l && pa[l-1]=='/')
res=snprintf(pabs,PATH_MAX,"%s%s",pa, base);
else
res=snprintf(pabs,PATH_MAX,"%s/%s",pa,base);
b = res>0 && res<PATH_MAX;
}
ex:
#ifdef __CYGWIN__
free(rp_file);
#else
#undef rp_file
#endif
free(s1);
free(s2);
return b;
err:
b = false;
goto ex;
}
bool parse_int16(const char *p, int16_t *v) bool parse_int16(const char *p, int16_t *v)
{ {
if (*p == '+' || *p == '-' || *p >= '0' && *p <= '9') if (*p == '+' || *p == '-' || *p >= '0' && *p <= '9')

View File

@@ -106,6 +106,7 @@ void set_console_io_buffering(void);
void close_std(void); void close_std(void);
void close_std_and_exit(int code); void close_std_and_exit(int code);
bool set_env_exedir(const char *argv0); bool set_env_exedir(const char *argv0);
bool realpath_any(const char *file, char *pabs);
bool parse_int16(const char *p, int16_t *v); bool parse_int16(const char *p, int16_t *v);

View File

@@ -280,13 +280,15 @@ bool HostlistCheck(const struct desync_profile *dp, const char *host, bool no_ma
static struct hostlist_file *RegisterHostlist_(struct hostlist_files_head *hostlists, struct hostlist_collection_head *hl_collection, const char *filename) static struct hostlist_file *RegisterHostlist_(struct hostlist_files_head *hostlists, struct hostlist_collection_head *hl_collection, const char *filename)
{ {
struct hostlist_file *hfile; struct hostlist_file *hfile;
char pabs[PATH_MAX];
if (filename) if (filename)
{ {
if (!(hfile=hostlist_files_search(hostlists, filename))) if (!realpath(filename,pabs)) return NULL;
if (!(hfile=hostlist_files_add(hostlists, filename))) if (!(hfile=hostlist_files_search(hostlists, pabs)))
if (!(hfile=hostlist_files_add(hostlists, pabs)))
return NULL; return NULL;
if (!hostlist_collection_search(hl_collection, filename)) if (!hostlist_collection_search(hl_collection, pabs))
if (!hostlist_collection_add(hl_collection, hfile)) if (!hostlist_collection_add(hl_collection, hfile))
return NULL; return NULL;
} }

View File

@@ -278,12 +278,15 @@ bool IpsetCheck(
static struct ipset_file *RegisterIpset_(struct ipset_files_head *ipsets, struct ipset_collection_head *ips_collection, const char *filename) static struct ipset_file *RegisterIpset_(struct ipset_files_head *ipsets, struct ipset_collection_head *ips_collection, const char *filename)
{ {
struct ipset_file *hfile; struct ipset_file *hfile;
char pabs[PATH_MAX];
if (filename) if (filename)
{ {
if (!(hfile=ipset_files_search(ipsets, filename))) if (!realpath(filename,pabs)) return NULL;
if (!(hfile=ipset_files_add(ipsets, filename))) if (!(hfile=ipset_files_search(ipsets, pabs)))
if (!(hfile=ipset_files_add(ipsets, pabs)))
return NULL; return NULL;
if (!ipset_collection_search(ips_collection, filename)) if (!ipset_collection_search(ips_collection, pabs))
if (!ipset_collection_add(ips_collection, hfile)) if (!ipset_collection_add(ips_collection, hfile))
return NULL; return NULL;
} }

View File

@@ -2164,8 +2164,11 @@ int main(int argc, char **argv)
{ {
if (*optarg == '@') if (*optarg == '@')
{ {
strncpy(params.debug_logfile, optarg + 1, sizeof(params.debug_logfile)); if (!realpath_any(optarg+1,params.debug_logfile))
params.debug_logfile[sizeof(params.debug_logfile) - 1] = 0; {
DLOG_ERR("bad file '%s'\n",optarg+1);
exit_clean(1);
}
FILE *F = fopen(params.debug_logfile, "wt"); FILE *F = fopen(params.debug_logfile, "wt");
if (!F) if (!F)
{ {
@@ -2269,7 +2272,11 @@ int main(int argc, char **argv)
} }
break; break;
case IDX_PIDFILE: case IDX_PIDFILE:
snprintf(params.pidfile, sizeof(params.pidfile), "%s", optarg); if (!realpath_any(optarg,params.pidfile))
{
DLOG_ERR("bad file '%s'\n",optarg);
exit_clean(1);
}
break; break;
#ifndef __CYGWIN__ #ifndef __CYGWIN__
case IDX_USER: case IDX_USER:
@@ -2373,8 +2380,11 @@ int main(int argc, char **argv)
params.writeable_dir_enable = true; params.writeable_dir_enable = true;
if (optarg) if (optarg)
{ {
strncpy(params.writeable_dir, optarg, sizeof(params.writeable_dir)); if (!realpath_any(optarg, params.writeable_dir))
params.writeable_dir[sizeof(params.writeable_dir) - 1] = 0; {
DLOG_ERR("bad file '%s'\n",optarg);
exit_clean(1);
}
} }
else else
*params.writeable_dir = 0; *params.writeable_dir = 0;
@@ -2385,10 +2395,22 @@ int main(int argc, char **argv)
break; break;
case IDX_LUA_INIT: case IDX_LUA_INIT:
if (!strlist_add_tail(&params.lua_init_scripts, optarg))
{ {
DLOG_ERR("out of memory\n"); char pabs[PATH_MAX+1], *p=optarg;
exit_clean(1); if (*p=='@')
{
if (!realpath_any(p+1,pabs+1))
{
DLOG_ERR("bad file '%s'\n",p+1);
exit_clean(1);
}
*(p=pabs)='@';
}
if (!strlist_add_tail(&params.lua_init_scripts, p))
{
DLOG_ERR("out of memory\n");
exit_clean(1);
}
} }
break; break;
case IDX_LUA_GC: case IDX_LUA_GC:
@@ -2518,15 +2540,18 @@ int main(int argc, char **argv)
break; break;
case IDX_HOSTLIST_AUTO_DEBUG: case IDX_HOSTLIST_AUTO_DEBUG:
{ {
FILE *F = fopen(optarg, "a+t"); if (!realpath_any(optarg,params.hostlist_auto_debuglog))
{
DLOG_ERR("bad file '%s'\n",optarg);
exit_clean(1);
}
FILE *F = fopen(params.hostlist_auto_debuglog, "a+t");
if (!F) if (!F)
{ {
DLOG_ERR("cannot create %s\n", optarg); DLOG_ERR("cannot create %s\n", optarg);
exit_clean(1); exit_clean(1);
} }
fclose(F); fclose(F);
strncpy(params.hostlist_auto_debuglog, optarg, sizeof(params.hostlist_auto_debuglog));
params.hostlist_auto_debuglog[sizeof(params.hostlist_auto_debuglog) - 1] = '\0';
} }
break; break;