From e46b546ce3077d32c0ceb28151b5bf90dbd24e74 Mon Sep 17 00:00:00 2001 From: aCaB Date: Tue, 20 Oct 2009 19:44:45 +0200 Subject: [PATCH] relocable hardcoded paths --- ChangeLog | 4 ++ clamconf/clamconf.c | 6 +++ platform.h.in | 5 +++ shared/misc.c | 2 +- shared/optparser.c | 13 +++--- shared/optparser.h | 2 - win32/clamconf.vcproj | 4 -- win32/clamscan.vcproj | 4 -- win32/compat/libclamav_main.c | 82 +++++++++++++++++++++++++++++++++++ win32/freshclam.vcproj | 4 -- win32/libclamav.def | 11 +++++ win32/libclamav.vcproj | 12 ++--- win32/platform.h | 9 ++++ 13 files changed, 129 insertions(+), 29 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0ba02d8e8..40e5343e0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Tue Oct 20 17:48:59 CEST 2009 (acab) +------------------------------------ + * shared, win32: make hardcoded paths relocable in win32 builds + Mon Oct 19 14:13:27 CEST 2009 (acab) ------------------------------------ * win32: add clamconf diff --git a/clamconf/clamconf.c b/clamconf/clamconf.c index e60c367cb..d1d2f4025 100644 --- a/clamconf/clamconf.c +++ b/clamconf/clamconf.c @@ -35,6 +35,12 @@ #include "libclamav/clamav.h" #include "libclamav/others.h" +#ifndef _WIN32 +extern const struct clam_option *clam_options; +#else +__declspec(dllimport) extern const struct clam_option *clam_options; +#endif + static struct _cfgfile { const char *name; int tool; diff --git a/platform.h.in b/platform.h.in index 871eff3e8..ce222b349 100644 --- a/platform.h.in +++ b/platform.h.in @@ -33,3 +33,8 @@ typedef unsigned int in_addr_t; #else #define PATHSEP "/" #endif + +#define CONFDIR_CLAMD CONFDIR"\\clamd.conf" +#define CONFDIR_FRESHCLAM CONFDIR"\\freshclam.conf" +#define CONFDIR_MILTER CONFDIR"\\clamav-milter.conf" + diff --git a/shared/misc.c b/shared/misc.c index a5132a022..9d5ebe79b 100644 --- a/shared/misc.c +++ b/shared/misc.c @@ -74,7 +74,7 @@ char *freshdbdir(void) /* try to find the most up-to-date db directory */ dbdir = cl_retdbdir(); - if((opts = optparse(CONFDIR PATHSEP "freshclam.conf", 0, NULL, 0, OPT_FRESHCLAM, 0, NULL))) { + if((opts = optparse(CONFDIR_FRESHCLAM, 0, NULL, 0, OPT_FRESHCLAM, 0, NULL))) { if((opt = optget(opts, "DatabaseDirectory"))->enabled) { if(strcmp(dbdir, opt->strarg)) { char *daily = (char *) malloc(strlen(opt->strarg) + strlen(dbdir) + 30); diff --git a/shared/optparser.c b/shared/optparser.c index ce43475c3..0e4852e45 100644 --- a/shared/optparser.c +++ b/shared/optparser.c @@ -57,14 +57,14 @@ #define FLAG_HIDDEN 4 /* don't print in clamconf --generate-config */ #define FLAG_REG_CASE 8 /* case-sensitive regex matching */ -const struct clam_option clam_options[] = { +const struct clam_option __clam_options[] = { /* name, longopt, sopt, argtype, regex, num, str, flags, owner, description, suggested */ /* cmdline only */ { NULL, "help", 'h', TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMD | OPT_FRESHCLAM | OPT_CLAMSCAN | OPT_CLAMDSCAN | OPT_SIGTOOL | OPT_MILTER | OPT_CLAMCONF | OPT_CLAMDTOP | OPT_CLAMBC, "", "" }, - { NULL, "config-file", 'c', TYPE_STRING, NULL, 0, CONFDIR PATHSEP "clamd.conf", FLAG_REQUIRED, OPT_CLAMD | OPT_CLAMDSCAN | OPT_CLAMDTOP, "", "" }, - { NULL, "config-file", 0, TYPE_STRING, NULL, 0, CONFDIR PATHSEP "freshclam.conf", FLAG_REQUIRED, OPT_FRESHCLAM, "", "" }, - { NULL, "config-file", 'c', TYPE_STRING, NULL, 0, CONFDIR PATHSEP "clamav-milter.conf", FLAG_REQUIRED, OPT_MILTER, "", "" }, + { NULL, "config-file", 'c', TYPE_STRING, NULL, 0, CONFDIR_CLAMD, FLAG_REQUIRED, OPT_CLAMD | OPT_CLAMDSCAN | OPT_CLAMDTOP, "", "" }, + { NULL, "config-file", 0, TYPE_STRING, NULL, 0, CONFDIR_FRESHCLAM, FLAG_REQUIRED, OPT_FRESHCLAM, "", "" }, + { NULL, "config-file", 'c', TYPE_STRING, NULL, 0, CONFDIR_MILTER, FLAG_REQUIRED, OPT_MILTER, "", "" }, { NULL, "version", 'V', TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMD | OPT_FRESHCLAM | OPT_CLAMSCAN | OPT_CLAMDSCAN | OPT_SIGTOOL | OPT_MILTER | OPT_CLAMCONF | OPT_CLAMDTOP | OPT_CLAMBC, "", "" }, { NULL, "debug", 0, TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMD | OPT_FRESHCLAM | OPT_CLAMSCAN | OPT_SIGTOOL, "", "" }, { NULL, "verbose", 'v', TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_FRESHCLAM | OPT_CLAMSCAN | OPT_CLAMDSCAN | OPT_SIGTOOL, "", "" }, @@ -75,7 +75,7 @@ const struct clam_option clam_options[] = { { NULL, "daemon", 'd', TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_FRESHCLAM, "", "" }, { NULL, "no-dns", 0, TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_FRESHCLAM, "", "" }, { NULL, "list-mirrors", 0, TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_FRESHCLAM, "", "" }, - { NULL, "submit-stats", 0, TYPE_STRING, NULL, 0, CONFDIR PATHSEP "clamd.conf", 0, OPT_FRESHCLAM, "", "" }, /* Don't merge this one with SubmitDetectionStats */ + { NULL, "submit-stats", 0, TYPE_STRING, NULL, 0, CONFDIR_CLAMD, 0, OPT_FRESHCLAM, "", "" }, /* Don't merge this one with SubmitDetectionStats */ { NULL, "reload", 0, TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMDSCAN, "", "" }, { NULL, "multiscan", 'm', TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMDSCAN, "", "" }, { NULL, "fdpass", 0, TYPE_BOOL, MATCH_BOOL, 0, NULL, 0, OPT_CLAMDSCAN, "", "" }, @@ -337,7 +337,7 @@ const struct clam_option clam_options[] = { { "HTTPUserAgent", NULL, 0, TYPE_STRING, NULL, -1, NULL, 0, OPT_FRESHCLAM, "If your servers are behind a firewall/proxy which does a User-Agent\nfiltering you can use this option to force the use of a different\nUser-Agent header.", "default" }, - { "NotifyClamd", "daemon-notify", 0, TYPE_STRING, NULL, -1, CONFDIR PATHSEP "clamd.conf", 0, OPT_FRESHCLAM, "Send the RELOAD command to clamd after a successful update.", "yes" }, + { "NotifyClamd", "daemon-notify", 0, TYPE_STRING, NULL, -1, CONFDIR_CLAMD, 0, OPT_FRESHCLAM, "Send the RELOAD command to clamd after a successful update.", "yes" }, { "OnUpdateExecute", "on-update-execute", 0, TYPE_STRING, NULL, -1, NULL, 0, OPT_FRESHCLAM, "Run a command after a successful database update.", "command" }, @@ -422,6 +422,7 @@ const struct clam_option clam_options[] = { { NULL, NULL, 0, 0, NULL, 0, NULL, 0, 0, NULL, NULL } }; +const struct clam_option *clam_options = __clam_options; const struct optstruct *optget(const struct optstruct *opts, const char *name) { diff --git a/shared/optparser.h b/shared/optparser.h index 0fb0d922a..b55fcf91e 100644 --- a/shared/optparser.h +++ b/shared/optparser.h @@ -67,8 +67,6 @@ struct clam_option { const char *suggested; }; -extern const struct clam_option clam_options[]; - const struct optstruct *optget(const struct optstruct *opts, const char *name); void optfree(struct optstruct *opts); diff --git a/win32/clamconf.vcproj b/win32/clamconf.vcproj index e0caeee55..b89c5e6b9 100644 --- a/win32/clamconf.vcproj +++ b/win32/clamconf.vcproj @@ -191,10 +191,6 @@ RelativePath="..\shared\misc.c" > - - - - diff --git a/win32/compat/libclamav_main.c b/win32/compat/libclamav_main.c index 7160c831c..297e274c8 100644 --- a/win32/compat/libclamav_main.c +++ b/win32/compat/libclamav_main.c @@ -4,7 +4,12 @@ #include "clamav-config.h" #endif +#include #include "pthread.h" +#include "libgen.h" +#include "shared/optparser.h" + +void fix_paths(void); BOOL APIENTRY DllMain(HMODULE hm, DWORD why, LPVOID rsrv) { WSADATA wsa; @@ -12,6 +17,7 @@ BOOL APIENTRY DllMain(HMODULE hm, DWORD why, LPVOID rsrv) { case DLL_PROCESS_ATTACH: if(WSAStartup(MAKEWORD(2,2), &wsa)) return FALSE; + fix_paths(); return pthread_win32_process_attach_np(); break; @@ -30,3 +36,79 @@ BOOL APIENTRY DllMain(HMODULE hm, DWORD why, LPVOID rsrv) { break; } } + +/* + The trick is: + 1 - Reinclude clamav-config.h which is not guarded against multiple inclusions. + In platform.h we do undef them and re-export as extern pointers, however, since + platform.h is guarded, the undef won't trigger. + This gives back to us the original CONFDIR and DATADIR macroes. + 2 - We define _static_ buffers to contain those strings. + 3 - We undef the macroes, which re-turns them back into extern pointers and we set them + to point to the above defined buffer. + 4 - We now give the original macros the names of the above buffers and include optparser.c + This result in clam_options struct in optparser be defined with proper pointers. +*/ + +#include "clamav-config.h" +char _DATADIR[MAX_PATH] = DATADIR; +char _CONFDIR[MAX_PATH] = CONFDIR; +char _CONFDIR_CLAMD[MAX_PATH] = CONFDIR"\\clamd.conf"; +char _CONFDIR_FRESHCLAM[MAX_PATH] = CONFDIR"\\freshclam.conf"; +char _CONFDIR_MILTER[MAX_PATH] = CONFDIR"\\clamav-milter.conf"; + +#undef DATADIR +#undef CONFDIR +const char *DATADIR = _DATADIR; +const char *CONFDIR = _CONFDIR; +const char *CONFDIR_CLAMD = _CONFDIR_CLAMD; +const char *CONFDIR_FRESHCLAM = _CONFDIR_FRESHCLAM; +const char *CONFDIR_MILTER = _CONFDIR_MILTER; + +#define DATADIR _DATADIR +#define CONFDIR _CONFDIR +#define CONFDIR_CLAMD _CONFDIR_CLAMD +#define CONFDIR_FRESHCLAM _CONFDIR_FRESHCLAM +#define CONFDIR_MILTER _CONFDIR_MILTER + +#include "shared/optparser.c" + +#define CLAMKEY "Software\\ClamAV" +void fix_paths(void) { + int have_ddir = 0, have_cdir = 0; + char path[MAX_PATH] = ""; + DWORD sizof; + HKEY key; + + if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, CLAMKEY, 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS || RegOpenKeyEx(HKEY_CURRENT_USER, CLAMKEY, 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) { + sizof = sizeof(path); + if(RegQueryValueEx(key, "DataDir", 0, NULL, path, &sizof) == ERROR_SUCCESS) { + have_ddir = 1; + memcpy(_DATADIR, path, sizof); + } + sizof = sizeof(path); + if(RegQueryValueEx(key, "ConfDir", 0, NULL, path, &sizof) == ERROR_SUCCESS) { + have_cdir = 1; + memcpy(_CONFDIR, path, sizof); + } + RegCloseKey(key); + } + if(!(have_ddir | have_cdir) && GetModuleFileName(NULL, path, sizeof(path))) { + char *dir; + path[sizeof(path)-1] = '\0'; + dir = dirname(path); + if(!have_ddir) + snprintf(_DATADIR, sizeof(_DATADIR), "%s\\database", dir); + if(!have_cdir) { + strncpy(_CONFDIR, dir, sizeof(_DATADIR)); + have_cdir = 1; + } + } + _DATADIR[sizeof(_DATADIR) - 1] = '\0'; + _CONFDIR[sizeof(_CONFDIR) - 1] = '\0'; + if(have_cdir) { + snprintf(_CONFDIR_CLAMD, sizeof(_CONFDIR_CLAMD), "%s\\%s", _CONFDIR, "clamd.conf"); + snprintf(_CONFDIR_FRESHCLAM, sizeof(_CONFDIR_FRESHCLAM), "%s\\%s", _CONFDIR, "freshclam.conf"); + snprintf(_CONFDIR_MILTER, sizeof(_CONFDIR_MILTER), "%s\\%s", _CONFDIR, "clamav-milter.conf"); + } +} diff --git a/win32/freshclam.vcproj b/win32/freshclam.vcproj index 564a7698f..f0601cb80 100644 --- a/win32/freshclam.vcproj +++ b/win32/freshclam.vcproj @@ -222,10 +222,6 @@ RelativePath="..\shared\misc.c" > - - diff --git a/win32/libclamav.def b/win32/libclamav.def index f3d9bc2ba..df1a9ac51 100644 --- a/win32/libclamav.def +++ b/win32/libclamav.def @@ -52,6 +52,17 @@ EXPORTS gzdopen EXPORTS gzclose EXPORTS gzwrite +EXPORTS optget +EXPORTS optparse +EXPORTS optfree +EXPORTS clam_options DATA + +EXPORTS DATADIR DATA +EXPORTS CONFDIR DATA +EXPORTS CONFDIR_CLAMD DATA +EXPORTS CONFDIR_FRESHCLAM DATA +EXPORTS CONFDIR_MILTER DATA + ; winsock - bridging for now EXPORTS htonl EXPORTS htons diff --git a/win32/libclamav.vcproj b/win32/libclamav.vcproj index 9ac4854cf..99d70497e 100644 --- a/win32/libclamav.vcproj +++ b/win32/libclamav.vcproj @@ -340,10 +340,6 @@ RelativePath="..\libclamav\mew.c" > - - @@ -759,10 +755,6 @@ RelativePath="..\shared\getopt.c" > - - + + diff --git a/win32/platform.h b/win32/platform.h index df67e01fb..b6ac953a3 100644 --- a/win32/platform.h +++ b/win32/platform.h @@ -65,4 +65,13 @@ typedef unsigned int in_addr_t; #define PATHSEP "\\" +#undef DATADIR +#undef CONFDIR +__declspec(dllimport) extern const char *DATADIR; +__declspec(dllimport) extern const char *CONFDIR; +__declspec(dllimport) extern const char *CONFDIR_CLAMD; +__declspec(dllimport) extern const char *CONFDIR_FRESHCLAM; +__declspec(dllimport) extern const char *CONFDIR_MILTER; +#undef HAVE_CONFIG_H + #endif /* __PLATFORM_H */ \ No newline at end of file