Windows: remove trailing whitespace
[openafs.git] / src / WINNT / afsd / afsdacl.c
1 /*
2
3 Copyright 2004 by the Massachusetts Institute of Technology
4
5 All rights reserved.
6
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the name of the Massachusetts
12 Institute of Technology (M.I.T.) not be used in advertising or publicity
13 pertaining to distribution of the software without specific, written
14 prior permission.
15
16 M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
17 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
18 M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
19 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
22 SOFTWARE.
23
24 */
25
26 /* $Id */
27
28 #include<windows.h>
29 #include<aclapi.h>
30 #include<sddl.h>
31 #include<stdio.h>
32 #include<string.h>
33
34 #define SETDACL     1
35 #define RESETDACL   2
36
37 #define AFSSERVICE                  "TransarcAFSDaemon"
38 #define AFSCLIENT_ADMIN_GROUPNAME   "AFS Client Admins"
39 #define EVERYONE_GROUPNAME          "Everyone"
40
41 char * progname = NULL;
42
43 void show_usage(void) {
44     fprintf(stderr,
45         "%s : Set or reset the DACL to allow starting or stopping\n"
46         "     the afsd service by any ordinary user.\n"
47         "\n"
48         "Usage : %s [-set | -reset] [-show]\n"
49         "      -set   : Sets the DACL\n"
50         "      -reset : Reset the DACL\n"
51         "      -show  : Show current DACL (SDSF)\n"
52         , progname, progname);
53 }
54
55 void show_last_error(DWORD code) {
56    LPVOID lpvMessageBuffer;
57
58    if(!code)
59         code = GetLastError();
60
61    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
62                  FORMAT_MESSAGE_FROM_SYSTEM,
63                  NULL, code,
64                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
65                 (LPTSTR)&lpvMessageBuffer, 0, NULL);
66
67    fprintf(stderr,
68        "%s: Error %d : %s\n",
69        progname,
70        code,
71        (LPSTR) lpvMessageBuffer);
72
73    LocalFree(lpvMessageBuffer);
74 }
75
76 int set_dacl(int action) {
77     int             rv = 1;
78     BOOL            bDaclPresent = FALSE;
79     BOOL            bDaclDefaulted = FALSE;
80     SC_HANDLE       scm = NULL;
81     SC_HANDLE       s_afs = NULL;
82     PSECURITY_DESCRIPTOR psdesc = NULL;
83     PACL            pacl = NULL;
84     PACL            pnewacl = NULL;
85     EXPLICIT_ACCESS exa[2];
86     DWORD           dwSize = 0;
87     DWORD           code = ERROR_SUCCESS;
88     SECURITY_DESCRIPTOR sd;
89
90     scm = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
91     if(!scm) {
92         show_last_error(0);
93         goto exit0;
94     }
95
96     s_afs = OpenService(scm, AFSSERVICE, READ_CONTROL | WRITE_DAC);
97     if(!s_afs) {
98         show_last_error(0);
99         goto exit0;
100     }
101
102     if (!QueryServiceObjectSecurity(s_afs, DACL_SECURITY_INFORMATION,
103         &sd, 0, &dwSize))
104     {
105         if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
106         {
107             psdesc = (PSECURITY_DESCRIPTOR)HeapAlloc(GetProcessHeap(),
108                 HEAP_ZERO_MEMORY, dwSize);
109             if (psdesc == NULL)
110             {
111                 show_last_error(0);
112                 goto exit0;
113             }
114
115             if (!QueryServiceObjectSecurity(s_afs,
116                 DACL_SECURITY_INFORMATION, psdesc, dwSize, &dwSize)) {
117                 show_last_error(0);
118                 goto exit0;
119                 }
120         }
121         else {
122             show_last_error(0);
123             goto exit0;
124         }
125     }
126     /* else : shouldn't happen. */
127
128     if (!GetSecurityDescriptorDacl(psdesc, &bDaclPresent, &pacl, &bDaclDefaulted))
129         show_last_error(0);
130
131     BuildExplicitAccessWithName(&exa[0], AFSCLIENT_ADMIN_GROUPNAME,
132         SPECIFIC_RIGHTS_ALL | STANDARD_RIGHTS_ALL,
133         SET_ACCESS,
134         NO_INHERITANCE);
135
136     BuildExplicitAccessWithName(&exa[1], EVERYONE_GROUPNAME,
137         SERVICE_START | SERVICE_STOP | READ_CONTROL,
138         ((action==RESETDACL)?REVOKE_ACCESS:SET_ACCESS),
139         NO_INHERITANCE);
140
141     code = SetEntriesInAcl(2, exa, pacl, &pnewacl);
142     if(code != ERROR_SUCCESS) {
143         show_last_error(code);
144     }
145
146     if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
147         show_last_error(0);
148
149     if(!SetSecurityDescriptorDacl(&sd, TRUE, pnewacl, FALSE))
150         show_last_error(0);
151
152     if (!SetServiceObjectSecurity(s_afs, DACL_SECURITY_INFORMATION, &sd))
153         show_last_error(0);
154
155 exit0:
156     if(pnewacl)
157         LocalFree(pnewacl);
158     if(psdesc)
159         HeapFree(GetProcessHeap(), 0, psdesc);
160     if(s_afs)
161         CloseServiceHandle(s_afs);
162     if(scm)
163         CloseServiceHandle(scm);
164
165     return rv;
166 }
167
168 int show_dacl(void) {
169     int             rv = 1;
170     BOOL            bDaclPresent = FALSE;
171     BOOL            bDaclDefaulted = FALSE;
172     SC_HANDLE       scm = NULL;
173     SC_HANDLE       s_afs = NULL;
174     PSECURITY_DESCRIPTOR psdesc = NULL;
175     DWORD           dwSize = 0;
176     DWORD           code = ERROR_SUCCESS;
177     SECURITY_DESCRIPTOR sd;
178     LPSTR           pstr = NULL;
179
180     scm = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
181     if(!scm) {
182         show_last_error(0);
183         goto exit0;
184     }
185
186     s_afs = OpenService(scm, AFSSERVICE, READ_CONTROL);
187     if(!s_afs) {
188         show_last_error(0);
189         goto exit0;
190     }
191
192     if (!QueryServiceObjectSecurity(s_afs, DACL_SECURITY_INFORMATION,
193         &sd, 0, &dwSize))
194     {
195         if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
196         {
197             psdesc = (PSECURITY_DESCRIPTOR)HeapAlloc(GetProcessHeap(),
198                 HEAP_ZERO_MEMORY, dwSize);
199             if (psdesc == NULL)
200             {
201                 show_last_error(0);
202                 goto exit0;
203             }
204
205             if (!QueryServiceObjectSecurity(s_afs,
206                 DACL_SECURITY_INFORMATION, psdesc, dwSize, &dwSize)) {
207                 show_last_error(0);
208                 goto exit0;
209                 }
210         }
211         else {
212             show_last_error(0);
213             goto exit0;
214         }
215     }
216     /* else : shouldn't happen. */
217
218     if(!ConvertSecurityDescriptorToStringSecurityDescriptor(
219         psdesc,
220         SDDL_REVISION_1,
221         DACL_SECURITY_INFORMATION,
222         &pstr,
223         NULL))
224     {
225         show_last_error(0);
226         goto exit0;
227     }
228
229     printf("DACL for AFSD service is : [%s]\n",pstr);
230
231 exit0:
232     if(pstr)
233         LocalFree(pstr);
234     if(psdesc)
235         HeapFree(GetProcessHeap(), 0, psdesc);
236     if(s_afs)
237         CloseServiceHandle(s_afs);
238     if(scm)
239         CloseServiceHandle(scm);
240
241     return rv;
242 }
243
244 int main(int argc, char ** argv) {
245     int showdacl = FALSE;
246     int action = 0;
247     int i;
248     int rv;
249
250     progname = argv[0];
251
252     for(i=1; i<argc; i++) {
253         if(!strcmp(argv[i],"-set") && !action)
254             action = SETDACL;
255         else if(!strcmp(argv[i], "-reset") && !action)
256             action = RESETDACL;
257         else if(!strcmp(argv[i], "-show"))
258             showdacl = TRUE;
259         else {
260             show_usage();
261             return 1;
262         }
263     }
264
265     if(!showdacl && action == 0) {
266         show_usage();
267         return 1;
268     }
269
270     if(action) {
271         rv = set_dacl(action);
272     }
273
274     if(showdacl) {
275         rv = show_dacl();
276     }
277
278     return rv;
279 }