2 * winrc/w_inst.h - install and remove functions
4 * Copyright (c) 2009, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * Contains install and remove functions that manipulate the
40 * windows services API and windows registry.
43 #include "winrc/w_inst.h"
44 #include "winrc/win_svc.h"
46 void wsvc_err2str(char* str, size_t len, const char* fixed, DWORD err)
49 if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
50 FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
51 NULL, err, 0, (LPTSTR)&buf, 0, NULL) == 0) {
52 /* could not format error message */
53 snprintf(str, len, "%s GetLastError=%d", fixed, (int)err);
56 snprintf(str, len, "%s (err=%d): %s", fixed, (int)err, buf);
60 /** exit with windows error */
62 fatal_win(FILE* out, const char* str)
65 wsvc_err2str(e, sizeof(e), str, (int)GetLastError());
66 if(out) fprintf(out, "%s\n", e);
67 else fprintf(stderr, "%s\n", e);
71 /** install registry entries for eventlog */
73 event_reg_install(FILE* out, const char* pathname)
78 if(out) fprintf(out, "install reg entries for %s\n", pathname);
79 snprintf(buf, sizeof(buf), "SYSTEM\\CurrentControlSet\\Services"
80 "\\EventLog\\Application\\%s", SERVICE_NAME);
81 if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR)buf,
82 0, /* reserved, mustbezero */
83 NULL, /* class of key, ignored */
84 REG_OPTION_NON_VOLATILE, /* values saved on disk */
85 KEY_WRITE, /* we want write permission */
86 NULL, /* use default security descriptor */
88 NULL)) /* not interested if key new or existing */
89 fatal_win(out, "could not create registry key");
92 if(RegSetValueEx(hk, (LPCTSTR)"EventMessageFile",
93 0, /* reserved, mustbezero */
94 REG_EXPAND_SZ, /* value type (string w env subst) */
95 (BYTE*)pathname, /* data */
96 (DWORD)strlen(pathname)+1)) /* length of data */
99 fatal_win(out, "could not registry set EventMessageFile");
103 t = EVENTLOG_SUCCESS | EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE
104 | EVENTLOG_INFORMATION_TYPE;
105 if(RegSetValueEx(hk, (LPCTSTR)"TypesSupported", 0, REG_DWORD,
106 (LPBYTE)&t, sizeof(t))) {
108 fatal_win(out, "could not registry set TypesSupported");
111 /* category message file */
112 if(RegSetValueEx(hk, (LPCTSTR)"CategoryMessageFile", 0, REG_EXPAND_SZ,
113 (BYTE*)pathname, (DWORD)strlen(pathname)+1)) {
115 fatal_win(out, "could not registry set CategoryMessageFile");
118 if(RegSetValueEx(hk, (LPCTSTR)"CategoryCount", 0, REG_DWORD,
119 (LPBYTE)&t, sizeof(t))) {
121 fatal_win(out, "could not registry set CategoryCount");
126 if(out) fprintf(out, "installed reg entries\n");
129 /** remove registry entries for eventlog */
131 event_reg_remove(FILE* out)
135 if(out) fprintf(out, "remove reg entries\n");
136 snprintf(buf, sizeof(buf), "SYSTEM\\CurrentControlSet\\Services"
137 "\\EventLog\\Application");
138 if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR)buf,
139 0, /* reserved, mustbezero */
140 NULL, /* class of key, ignored */
141 REG_OPTION_NON_VOLATILE, /* values saved on disk */
142 DELETE, /* we want key delete permission */
143 NULL, /* use default security descriptor */
145 NULL)) /* not interested if key new or existing */
146 fatal_win(out, "could not open registry key");
147 if(RegDeleteKey(hk, (LPCTSTR)SERVICE_NAME)) {
149 fatal_win(out, "could not delete registry key");
152 if(out) fprintf(out, "removed reg entries\n");
156 * put quotes around string. Needs one space in front
157 * @param out: debugfile
158 * @param str: to be quoted.
159 * @param maxlen: max length of the string buffer.
162 quote_it(FILE* out, char* str, size_t maxlen)
164 if(strlen(str) == maxlen) {
165 if(out) fprintf(out, "string too long %s", str);
169 str[strlen(str)+1]=0;
170 str[strlen(str)]='"';
175 change(FILE* out, char* path, size_t max, const char* from, const char* to)
177 size_t fromlen = strlen(from);
178 size_t tolen = strlen(to);
179 size_t pathlen = strlen(path);
180 if(pathlen - fromlen + tolen >= max) {
181 if(out) fprintf(out, "string too long %s", path);
184 snprintf(path+pathlen-fromlen, max-(pathlen-fromlen), "%s", to);
187 /* Install service in servicecontrolmanager */
189 wsvc_install(FILE* out, const char* rename)
193 TCHAR path[MAX_PATH+2+256];
194 if(out) fprintf(out, "installing unbound service\n");
195 if(!GetModuleFileName(NULL, path+1, MAX_PATH))
196 fatal_win(out, "could not GetModuleFileName");
197 /* change 'unbound-service-install' to 'unbound' */
199 change(out, path+1, sizeof(path)-1, rename, "unbound.exe");
201 event_reg_install(out, path+1);
203 /* have to quote it because of spaces in directory names */
204 /* could append arguments to be sent to ServiceMain */
205 quote_it(out, path, sizeof(path));
206 strcat(path, " -w service");
207 scm = OpenSCManager(NULL, NULL, (int)SC_MANAGER_CREATE_SERVICE);
208 if(!scm) fatal_win(out, "could not OpenSCManager");
211 SERVICE_NAME, /* name of service */
212 "Unbound DNS validator", /* display name */
213 SERVICE_ALL_ACCESS, /* desired access */
214 SERVICE_WIN32_OWN_PROCESS, /* service type */
215 SERVICE_AUTO_START, /* start type */
216 SERVICE_ERROR_NORMAL, /* error control type */
217 path, /* path to service's binary */
218 NULL, /* no load ordering group */
219 NULL, /* no tag identifier */
221 NULL, /* on LocalSystem */
222 NULL /* no password */
225 CloseServiceHandle(scm);
226 fatal_win(out, "could not CreateService");
228 CloseServiceHandle(sv);
229 CloseServiceHandle(scm);
230 if(out) fprintf(out, "unbound service installed\n");
234 /* Remove installed service from servicecontrolmanager */
236 wsvc_remove(FILE* out)
240 if(out) fprintf(out, "removing unbound service\n");
241 scm = OpenSCManager(NULL, NULL, (int)SC_MANAGER_ALL_ACCESS);
242 if(!scm) fatal_win(out, "could not OpenSCManager");
243 sv = OpenService(scm, SERVICE_NAME, DELETE);
245 CloseServiceHandle(scm);
246 fatal_win(out, "could not OpenService");
248 if(!DeleteService(sv)) {
249 CloseServiceHandle(sv);
250 CloseServiceHandle(scm);
251 fatal_win(out, "could not DeleteService");
253 CloseServiceHandle(sv);
254 CloseServiceHandle(scm);
255 event_reg_remove(out);
256 if(out) fprintf(out, "unbound service removed\n");
262 wsvc_rc_start(FILE* out)
266 if(out) fprintf(out, "start unbound service\n");
267 scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
268 if(!scm) fatal_win(out, "could not OpenSCManager");
269 sv = OpenService(scm, SERVICE_NAME, SERVICE_START);
271 CloseServiceHandle(scm);
272 fatal_win(out, "could not OpenService");
274 if(!StartService(sv, 0, NULL)) {
275 CloseServiceHandle(sv);
276 CloseServiceHandle(scm);
277 fatal_win(out, "could not StartService");
279 CloseServiceHandle(sv);
280 CloseServiceHandle(scm);
281 if(out) fprintf(out, "unbound service started\n");
287 wsvc_rc_stop(FILE* out)
292 if(out) fprintf(out, "stop unbound service\n");
293 scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
294 if(!scm) fatal_win(out, "could not OpenSCManager");
295 sv = OpenService(scm, SERVICE_NAME, SERVICE_STOP);
297 CloseServiceHandle(scm);
298 fatal_win(out, "could not OpenService");
300 if(!ControlService(sv, SERVICE_CONTROL_STOP, &st)) {
301 CloseServiceHandle(sv);
302 CloseServiceHandle(scm);
303 fatal_win(out, "could not ControlService");
305 CloseServiceHandle(sv);
306 CloseServiceHandle(scm);
307 if(out) fprintf(out, "unbound service stopped\n");