#include "config.h"
#include "winrc/w_inst.h"
#include "winrc/win_svc.h"
void wsvc_err2str(char* str, size_t len, const char* fixed, DWORD err)
{
LPTSTR buf;
if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL, err, 0, (LPTSTR)&buf, 0, NULL) == 0) {
snprintf(str, len, "%s GetLastError=%d", fixed, (int)err);
return;
}
snprintf(str, len, "%s (err=%d): %s", fixed, (int)err, buf);
LocalFree(buf);
}
static void
fatal_win(FILE* out, const char* str)
{
char e[256];
wsvc_err2str(e, sizeof(e), str, (int)GetLastError());
if(out) fprintf(out, "%s\n", e);
else fprintf(stderr, "%s\n", e);
exit(1);
}
static void
event_reg_install(FILE* out, const char* pathname)
{
char buf[1024];
HKEY hk;
DWORD t;
if(out) fprintf(out, "install reg entries for %s\n", pathname);
snprintf(buf, sizeof(buf), "SYSTEM\\CurrentControlSet\\Services"
"\\EventLog\\Application\\%s", SERVICE_NAME);
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR)buf,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_WRITE,
NULL,
&hk,
NULL))
fatal_win(out, "could not create registry key");
if(RegSetValueEx(hk, (LPCTSTR)"EventMessageFile",
0,
REG_EXPAND_SZ,
(BYTE*)pathname,
(DWORD)strlen(pathname)+1))
{
RegCloseKey(hk);
fatal_win(out, "could not registry set EventMessageFile");
}
t = EVENTLOG_SUCCESS | EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE
| EVENTLOG_INFORMATION_TYPE;
if(RegSetValueEx(hk, (LPCTSTR)"TypesSupported", 0, REG_DWORD,
(LPBYTE)&t, sizeof(t))) {
RegCloseKey(hk);
fatal_win(out, "could not registry set TypesSupported");
}
if(RegSetValueEx(hk, (LPCTSTR)"CategoryMessageFile", 0, REG_EXPAND_SZ,
(BYTE*)pathname, (DWORD)strlen(pathname)+1)) {
RegCloseKey(hk);
fatal_win(out, "could not registry set CategoryMessageFile");
}
t = 1;
if(RegSetValueEx(hk, (LPCTSTR)"CategoryCount", 0, REG_DWORD,
(LPBYTE)&t, sizeof(t))) {
RegCloseKey(hk);
fatal_win(out, "could not registry set CategoryCount");
}
RegCloseKey(hk);
if(out) fprintf(out, "installed reg entries\n");
}
static void
event_reg_remove(FILE* out)
{
char buf[1024];
HKEY hk;
if(out) fprintf(out, "remove reg entries\n");
snprintf(buf, sizeof(buf), "SYSTEM\\CurrentControlSet\\Services"
"\\EventLog\\Application");
if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR)buf,
0,
NULL,
REG_OPTION_NON_VOLATILE,
DELETE,
NULL,
&hk,
NULL))
fatal_win(out, "could not open registry key");
if(RegDeleteKey(hk, (LPCTSTR)SERVICE_NAME)) {
RegCloseKey(hk);
fatal_win(out, "could not delete registry key");
}
RegCloseKey(hk);
if(out) fprintf(out, "removed reg entries\n");
}
static void
quote_it(FILE* out, char* str, size_t maxlen)
{
if(strlen(str) == maxlen) {
if(out) fprintf(out, "string too long %s", str);
exit(1);
}
str[0]='"';
str[strlen(str)+1]=0;
str[strlen(str)]='"';
}
static void
change(FILE* out, char* path, size_t max, const char* from, const char* to)
{
size_t fromlen = strlen(from);
size_t tolen = strlen(to);
size_t pathlen = strlen(path);
if(pathlen - fromlen + tolen >= max) {
if(out) fprintf(out, "string too long %s", path);
exit(1);
}
snprintf(path+pathlen-fromlen, max-(pathlen-fromlen), "%s", to);
}
void
wsvc_install(FILE* out, const char* rename)
{
SC_HANDLE scm;
SC_HANDLE sv;
TCHAR path[2*MAX_PATH+4+256];
TCHAR path_config[2*MAX_PATH+4+256];
if(out) fprintf(out, "installing unbound service\n");
if(!GetModuleFileName(NULL, path+1, MAX_PATH))
fatal_win(out, "could not GetModuleFileName");
if(rename) {
change(out, path+1, sizeof(path)-1, rename, "unbound.exe");
memmove(path_config+1, path+1, sizeof(path)-1);
change(out, path_config+1, sizeof(path_config)-1,
"unbound.exe", "service.conf");
}
event_reg_install(out, path+1);
quote_it(out, path, sizeof(path));
if(rename) {
quote_it(out, path_config, sizeof(path_config));
strcat(path, " -c ");
strcat(path, path_config);
}
strcat(path, " -w service");
scm = OpenSCManager(NULL, NULL, (int)SC_MANAGER_CREATE_SERVICE);
if(!scm) fatal_win(out, "could not OpenSCManager");
sv = CreateService(
scm,
SERVICE_NAME,
"Unbound DNS validator",
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
path,
NULL,
NULL,
NULL,
NULL,
NULL
);
if(!sv) {
CloseServiceHandle(scm);
fatal_win(out, "could not CreateService");
}
CloseServiceHandle(sv);
CloseServiceHandle(scm);
if(out) fprintf(out, "unbound service installed\n");
}
void
wsvc_remove(FILE* out)
{
SC_HANDLE scm;
SC_HANDLE sv;
if(out) fprintf(out, "removing unbound service\n");
scm = OpenSCManager(NULL, NULL, (int)SC_MANAGER_ALL_ACCESS);
if(!scm) fatal_win(out, "could not OpenSCManager");
sv = OpenService(scm, SERVICE_NAME, DELETE);
if(!sv) {
CloseServiceHandle(scm);
fatal_win(out, "could not OpenService");
}
if(!DeleteService(sv)) {
CloseServiceHandle(sv);
CloseServiceHandle(scm);
fatal_win(out, "could not DeleteService");
}
CloseServiceHandle(sv);
CloseServiceHandle(scm);
event_reg_remove(out);
if(out) fprintf(out, "unbound service removed\n");
}
void
wsvc_rc_start(FILE* out)
{
SC_HANDLE scm;
SC_HANDLE sv;
if(out) fprintf(out, "start unbound service\n");
scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(!scm) fatal_win(out, "could not OpenSCManager");
sv = OpenService(scm, SERVICE_NAME, SERVICE_START);
if(!sv) {
CloseServiceHandle(scm);
fatal_win(out, "could not OpenService");
}
if(!StartService(sv, 0, NULL)) {
CloseServiceHandle(sv);
CloseServiceHandle(scm);
fatal_win(out, "could not StartService");
}
CloseServiceHandle(sv);
CloseServiceHandle(scm);
if(out) fprintf(out, "unbound service started\n");
}
void
wsvc_rc_stop(FILE* out)
{
SC_HANDLE scm;
SC_HANDLE sv;
SERVICE_STATUS st;
if(out) fprintf(out, "stop unbound service\n");
scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(!scm) fatal_win(out, "could not OpenSCManager");
sv = OpenService(scm, SERVICE_NAME, SERVICE_STOP);
if(!sv) {
CloseServiceHandle(scm);
fatal_win(out, "could not OpenService");
}
if(!ControlService(sv, SERVICE_CONTROL_STOP, &st)) {
CloseServiceHandle(sv);
CloseServiceHandle(scm);
fatal_win(out, "could not ControlService");
}
CloseServiceHandle(sv);
CloseServiceHandle(scm);
if(out) fprintf(out, "unbound service stopped\n");
}