]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - winrc/w_inst.c
import unbound 1.4.22
[FreeBSD/FreeBSD.git] / winrc / w_inst.c
1 /*
2  * winrc/w_inst.h - install and remove functions
3  *
4  * Copyright (c) 2009, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  * 
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.
18  * 
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.
22  * 
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.
34  */
35
36 /**
37  * \file
38  *
39  * Contains install and remove functions that manipulate the
40  * windows services API and windows registry.
41  */
42 #include "config.h"
43 #include "winrc/w_inst.h"
44 #include "winrc/win_svc.h"
45
46 void wsvc_err2str(char* str, size_t len, const char* fixed, DWORD err)
47 {
48         LPTSTR buf;
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);
54                 return;
55         }
56         snprintf(str, len, "%s (err=%d): %s", fixed, (int)err, buf);
57         LocalFree(buf);
58 }
59
60 /** exit with windows error */
61 static void
62 fatal_win(FILE* out, const char* str)
63 {
64         char e[256];
65         wsvc_err2str(e, sizeof(e), str, (int)GetLastError());
66         if(out) fprintf(out, "%s\n", e);
67         else fprintf(stderr, "%s\n", e);
68         exit(1);
69 }
70
71 /** install registry entries for eventlog */
72 static void
73 event_reg_install(FILE* out, const char* pathname)
74 {
75         char buf[1024];
76         HKEY hk;
77         DWORD t;
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 */
87                 &hk, /* result */
88                 NULL)) /* not interested if key new or existing */
89                 fatal_win(out, "could not create registry key");
90         
91         /* message file */
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 */
97         {
98                 RegCloseKey(hk);
99                 fatal_win(out, "could not registry set EventMessageFile");
100         }
101
102         /* event types */
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))) {
107                 RegCloseKey(hk);
108                 fatal_win(out, "could not registry set TypesSupported");
109         }
110
111         /* category message file */
112         if(RegSetValueEx(hk, (LPCTSTR)"CategoryMessageFile", 0, REG_EXPAND_SZ, 
113                 (BYTE*)pathname, (DWORD)strlen(pathname)+1)) {
114                 RegCloseKey(hk);
115                 fatal_win(out, "could not registry set CategoryMessageFile");
116         }
117         t = 1;
118         if(RegSetValueEx(hk, (LPCTSTR)"CategoryCount", 0, REG_DWORD, 
119                 (LPBYTE)&t, sizeof(t))) {
120                 RegCloseKey(hk);
121                 fatal_win(out, "could not registry set CategoryCount");
122         }
123
124
125         RegCloseKey(hk);
126         if(out) fprintf(out, "installed reg entries\n");
127 }
128
129 /** remove registry entries for eventlog */
130 static void
131 event_reg_remove(FILE* out)
132 {
133         char buf[1024];
134         HKEY hk;
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 */
144                 &hk, /* result */
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)) {
148                 RegCloseKey(hk);
149                 fatal_win(out, "could not delete registry key");
150         }
151         RegCloseKey(hk);
152         if(out) fprintf(out, "removed reg entries\n");
153 }
154
155 /** 
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.
160  */
161 static void
162 quote_it(FILE* out, char* str, size_t maxlen)
163 {
164         if(strlen(str) == maxlen) {
165                 if(out) fprintf(out, "string too long %s", str);
166                 exit(1);
167         }
168         str[0]='"';
169         str[strlen(str)+1]=0;
170         str[strlen(str)]='"';
171 }
172
173 /** change suffix */
174 static void
175 change(FILE* out, char* path, size_t max, const char* from, const char* to)
176 {
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);
182                 exit(1);
183         }
184         snprintf(path+pathlen-fromlen, max-(pathlen-fromlen), "%s", to);
185 }
186
187 /* Install service in servicecontrolmanager */
188 void
189 wsvc_install(FILE* out, const char* rename)
190 {
191         SC_HANDLE scm;
192         SC_HANDLE sv;
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' */
198         if(rename)
199                 change(out, path+1, sizeof(path)-1, rename, "unbound.exe");
200
201         event_reg_install(out, path+1);
202
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");
209         sv = CreateService(
210                 scm,
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 */
220                 NULL, /* no deps */
221                 NULL, /* on LocalSystem */
222                 NULL /* no password */
223                 );
224         if(!sv) {
225                 CloseServiceHandle(scm);
226                 fatal_win(out, "could not CreateService");
227         }
228         CloseServiceHandle(sv);
229         CloseServiceHandle(scm);
230         if(out) fprintf(out, "unbound service installed\n");
231 }
232
233
234 /* Remove installed service from servicecontrolmanager */
235 void
236 wsvc_remove(FILE* out)
237 {
238         SC_HANDLE scm;
239         SC_HANDLE sv;
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);
244         if(!sv) {
245                 CloseServiceHandle(scm);
246                 fatal_win(out, "could not OpenService");
247         }
248         if(!DeleteService(sv)) {
249                 CloseServiceHandle(sv);
250                 CloseServiceHandle(scm);
251                 fatal_win(out, "could not DeleteService");
252         }
253         CloseServiceHandle(sv);
254         CloseServiceHandle(scm);
255         event_reg_remove(out);
256         if(out) fprintf(out, "unbound service removed\n");
257 }
258
259
260 /* Start daemon */
261 void
262 wsvc_rc_start(FILE* out)
263 {
264         SC_HANDLE scm;
265         SC_HANDLE sv;
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);
270         if(!sv) {
271                 CloseServiceHandle(scm);
272                 fatal_win(out, "could not OpenService");
273         }
274         if(!StartService(sv, 0, NULL)) {
275                 CloseServiceHandle(sv);
276                 CloseServiceHandle(scm);
277                 fatal_win(out, "could not StartService");
278         }
279         CloseServiceHandle(sv);
280         CloseServiceHandle(scm);
281         if(out) fprintf(out, "unbound service started\n");
282 }
283
284
285 /* Stop daemon */
286 void
287 wsvc_rc_stop(FILE* out)
288 {
289         SC_HANDLE scm;
290         SC_HANDLE sv;
291         SERVICE_STATUS st;
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);
296         if(!sv) {
297                 CloseServiceHandle(scm);
298                 fatal_win(out, "could not OpenService");
299         }
300         if(!ControlService(sv, SERVICE_CONTROL_STOP, &st)) {
301                 CloseServiceHandle(sv);
302                 CloseServiceHandle(scm);
303                 fatal_win(out, "could not ControlService");
304         }
305         CloseServiceHandle(sv);
306         CloseServiceHandle(scm);
307         if(out) fprintf(out, "unbound service stopped\n");
308 }