From: Wouter Wijngaards Date: Mon, 27 Feb 2012 13:20:29 +0000 (+0000) Subject: - Fix bug#434: on windows check registry for config file location X-Git-Tag: release-1.4.17rc1~38 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d7172c55edc2036aa3472517031c5a67cd9461e0;p=thirdparty%2Funbound.git - Fix bug#434: on windows check registry for config file location for unbound-control.exe, and unbound-checkconf.exe. git-svn-id: file:///svn/unbound/trunk@2635 be551aaa-1e26-0410-a405-d3ace91eadb9 --- diff --git a/doc/Changelog b/doc/Changelog index b0a5697ea..0d7b1d44a 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,3 +1,7 @@ +27 February 2012: Wouter + - Fix bug#434: on windows check registry for config file location + for unbound-control.exe, and unbound-checkconf.exe. + 23 February 2012: Wouter - Fix to squelch 'network unreachable' errors from tcp connect in logs, high verbosity will show them. diff --git a/smallapp/unbound-checkconf.c b/smallapp/unbound-checkconf.c index 10b23bebd..c73d8bdc7 100644 --- a/smallapp/unbound-checkconf.c +++ b/smallapp/unbound-checkconf.c @@ -483,9 +483,15 @@ int main(int argc, char* argv[]) int c; const char* f; const char* opt = NULL; + const char* cfgfile = CONFIGFILE; log_ident_set("unbound-checkconf"); log_init(NULL, 0, NULL); checklock_start(); +#ifdef USE_WINSOCK + /* use registry config file in preference to compiletime location */ + if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile"))) + cfgfile = CONFIGFILE; +#endif /* USE_WINSOCK */ /* parse the options */ while( (c=getopt(argc, argv, "ho:")) != -1) { switch(c) { @@ -504,7 +510,7 @@ int main(int argc, char* argv[]) usage(); if(argc == 1) f = argv[0]; - else f = CONFIGFILE; + else f = cfgfile; checkconf(f, opt); checklock_stop(); return 0; diff --git a/smallapp/unbound-control.c b/smallapp/unbound-control.c index 757d27988..58be7b7ab 100644 --- a/smallapp/unbound-control.c +++ b/smallapp/unbound-control.c @@ -366,6 +366,9 @@ int main(int argc, char* argv[]) #ifdef USE_WINSOCK if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0) fatal_exit("WSAStartup failed: %s", wsa_strerror(r)); + /* use registry config file in preference to compiletime location */ + if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile"))) + cfgfile = CONFIGFILE; #endif ERR_load_crypto_strings(); diff --git a/util/config_file.c b/util/config_file.c index 41ae87e9c..8108cd946 100644 --- a/util/config_file.c +++ b/util/config_file.c @@ -1333,6 +1333,42 @@ char* cfg_ptr_reverse(char* str) return result; } +#ifdef UB_ON_WINDOWS +char* +w_lookup_reg_str(const char* key, const char* name) +{ + HKEY hk = NULL; + DWORD type = 0; + BYTE buf[1024]; + DWORD len = (DWORD)sizeof(buf); + LONG ret; + char* result = NULL; + ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hk); + if(ret == ERROR_FILE_NOT_FOUND) + return NULL; /* key does not exist */ + else if(ret != ERROR_SUCCESS) { + log_err("RegOpenKeyEx failed"); + return NULL; + } + ret = RegQueryValueEx(hk, (LPCTSTR)name, 0, &type, buf, &len); + if(RegCloseKey(hk)) + log_err("RegCloseKey"); + if(ret == ERROR_FILE_NOT_FOUND) + return NULL; /* name does not exist */ + else if(ret != ERROR_SUCCESS) { + log_err("RegQueryValueEx failed"); + return NULL; + } + if(type == REG_SZ || type == REG_MULTI_SZ || type == REG_EXPAND_SZ) { + buf[sizeof(buf)-1] = 0; + buf[sizeof(buf)-2] = 0; /* for multi_sz */ + result = strdup((char*)buf); + if(!result) log_err("out of memory"); + } + return result; +} +#endif /* UB_ON_WINDOWS */ + void errinf(struct module_qstate* qstate, const char* str) { struct config_strlist* p; diff --git a/util/config_file.h b/util/config_file.h index 050c3a00b..be5324446 100644 --- a/util/config_file.h +++ b/util/config_file.h @@ -629,4 +629,15 @@ void ub_c_error(const char* msg); /** parsing helpers: print error with file and line numbers */ void ub_c_error_msg(const char* fmt, ...) ATTR_FORMAT(printf, 1, 2); +#ifdef UB_ON_WINDOWS +/** + * Obtain registry string (if it exists). + * @param key: key string + * @param name: name of value to fetch. + * @return malloced string with the result or NULL if it did not + * exist on an error (logged with log_err) was encountered. + */ +char* w_lookup_reg_str(const char* key, const char* name); +#endif /* UB_ON_WINDOWS */ + #endif /* UTIL_CONFIG_FILE_H */