/*
* winrc/w_inst.h - install and remove functions
*
* Copyright (c) 2009, NLnet Labs. All rights reserved.
*
* This software is open source.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the NLNET LABS nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* \file
*
* Contains install and remove functions that manipulate the
* windows services API and windows registry.
*/
#include "config.h"
#include "winrc/w_inst.h"
#include "winrc/win_svc.h"
/** install registry entries for eventlog */
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, /* reserved, mustbezero */
NULL, /* class of key, ignored */
REG_OPTION_NON_VOLATILE, /* values saved on disk */
KEY_WRITE, /* we want write permission */
NULL, /* use default security descriptor */
&hk, /* result */
NULL)) /* not interested if key new or existing */
fatal_win(out, "could not create registry key");
/* message file */
if(RegSetValueEx(hk, (LPCTSTR)"EventMessageFile",
0, /* reserved, mustbezero */
REG_EXPAND_SZ, /* value type (string w env subst) */
(BYTE*)pathname, /* data */
(DWORD)strlen(pathname)+1)) /* length of data */
{
RegCloseKey(hk);
fatal_win(out, "could not registry set EventMessageFile");
}
/* event types */
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");
}
/* category message file */
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");
}
/** remove registry entries for eventlog */
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, /* reserved, mustbezero */
NULL, /* class of key, ignored */
REG_OPTION_NON_VOLATILE, /* values saved on disk */
DELETE, /* we want key delete permission */
NULL, /* use default security descriptor */
&hk, /* result */
NULL)) /* not interested if key new or existing */
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");
}
/**
* put quotes around string. Needs one space in front
* @param out: debugfile
* @param str: to be quoted.
* @param maxlen: max length of the string buffer.
*/
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)]='"';
}
/* have to quote it because of spaces in directory names */
/* could append arguments to be sent to ServiceMain */
quote_it(out, path, sizeof(path));
/* if we started in a different directory, also read config from it. */
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, /* name of service */
"Unbound DNS validator", /* display name */
SERVICE_ALL_ACCESS, /* desired access */
SERVICE_WIN32_OWN_PROCESS, /* service type */
SERVICE_AUTO_START, /* start type */
SERVICE_ERROR_NORMAL, /* error control type */
path, /* path to service's binary */
NULL, /* no load ordering group */
NULL, /* no tag identifier */
NULL, /* no deps */
NULL, /* on LocalSystem */
NULL /* no password */
);
if(!sv) {
CloseServiceHandle(scm);
fatal_win(out, "could not CreateService");
}
CloseServiceHandle(sv);
CloseServiceHandle(scm);
if(out) fprintf(out, "unbound service installed\n");
}