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
[2*MAX_PATH
+4+256];
194 TCHAR path_config
[2*MAX_PATH
+4+256];
195 if(out
) fprintf(out
, "installing unbound service\n");
196 if(!GetModuleFileName(NULL
, path
+1, MAX_PATH
))
197 fatal_win(out
, "could not GetModuleFileName");
198 /* change 'unbound-service-install' to 'unbound' */
200 change(out
, path
+1, sizeof(path
)-1, rename
, "unbound.exe");
201 memmove(path_config
+1, path
+1, sizeof(path
)-1);
202 change(out
, path_config
+1, sizeof(path_config
)-1,
203 "unbound.exe", "service.conf");
206 event_reg_install(out
, path
+1);
208 /* have to quote it because of spaces in directory names */
209 /* could append arguments to be sent to ServiceMain */
210 quote_it(out
, path
, sizeof(path
));
212 /* if we started in a different directory, also read config from it. */
214 quote_it(out
, path_config
, sizeof(path_config
));
215 strcat(path
, " -c ");
216 strcat(path
, path_config
);
219 strcat(path
, " -w service");
220 scm
= OpenSCManager(NULL
, NULL
, (int)SC_MANAGER_CREATE_SERVICE
);
221 if(!scm
) fatal_win(out
, "could not OpenSCManager");
224 SERVICE_NAME
, /* name of service */
225 "Unbound DNS validator", /* display name */
226 SERVICE_ALL_ACCESS
, /* desired access */
227 SERVICE_WIN32_OWN_PROCESS
, /* service type */
228 SERVICE_AUTO_START
, /* start type */
229 SERVICE_ERROR_NORMAL
, /* error control type */
230 path
, /* path to service's binary */
231 NULL
, /* no load ordering group */
232 NULL
, /* no tag identifier */
234 NULL
, /* on LocalSystem */
235 NULL
/* no password */
238 CloseServiceHandle(scm
);
239 fatal_win(out
, "could not CreateService");
241 CloseServiceHandle(sv
);
242 CloseServiceHandle(scm
);
243 if(out
) fprintf(out
, "unbound service installed\n");
247 /* Remove installed service from servicecontrolmanager */
249 wsvc_remove(FILE* out
)
253 if(out
) fprintf(out
, "removing unbound service\n");
254 scm
= OpenSCManager(NULL
, NULL
, (int)SC_MANAGER_ALL_ACCESS
);
255 if(!scm
) fatal_win(out
, "could not OpenSCManager");
256 sv
= OpenService(scm
, SERVICE_NAME
, DELETE
);
258 CloseServiceHandle(scm
);
259 fatal_win(out
, "could not OpenService");
261 if(!DeleteService(sv
)) {
262 CloseServiceHandle(sv
);
263 CloseServiceHandle(scm
);
264 fatal_win(out
, "could not DeleteService");
266 CloseServiceHandle(sv
);
267 CloseServiceHandle(scm
);
268 event_reg_remove(out
);
269 if(out
) fprintf(out
, "unbound service removed\n");
275 wsvc_rc_start(FILE* out
)
279 if(out
) fprintf(out
, "start unbound service\n");
280 scm
= OpenSCManager(NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
281 if(!scm
) fatal_win(out
, "could not OpenSCManager");
282 sv
= OpenService(scm
, SERVICE_NAME
, SERVICE_START
);
284 CloseServiceHandle(scm
);
285 fatal_win(out
, "could not OpenService");
287 if(!StartService(sv
, 0, NULL
)) {
288 CloseServiceHandle(sv
);
289 CloseServiceHandle(scm
);
290 fatal_win(out
, "could not StartService");
292 CloseServiceHandle(sv
);
293 CloseServiceHandle(scm
);
294 if(out
) fprintf(out
, "unbound service started\n");
300 wsvc_rc_stop(FILE* out
)
305 if(out
) fprintf(out
, "stop unbound service\n");
306 scm
= OpenSCManager(NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
307 if(!scm
) fatal_win(out
, "could not OpenSCManager");
308 sv
= OpenService(scm
, SERVICE_NAME
, SERVICE_STOP
);
310 CloseServiceHandle(scm
);
311 fatal_win(out
, "could not OpenService");
313 if(!ControlService(sv
, SERVICE_CONTROL_STOP
, &st
)) {
314 CloseServiceHandle(sv
);
315 CloseServiceHandle(scm
);
316 fatal_win(out
, "could not ControlService");
318 CloseServiceHandle(sv
);
319 CloseServiceHandle(scm
);
320 if(out
) fprintf(out
, "unbound service stopped\n");