venus: Remove dedebug
[openafs.git] / src / WINNT / afsreg / test / regman.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* Manual utility for testing registry access functions. Also serves
11  * as a pre Configuration Manager registry configuration utility.
12  */
13
14 #include <afs/param.h>
15 #include <afs/stds.h>
16
17 #include <windows.h>
18 #include <stddef.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23
24 #include <afs/param.h>
25 #include <afs/stds.h>
26 #include <afs/com_err.h>
27 #include <afs/cmd.h>
28 #include <afs/dirpath.h>
29
30 #include <WINNT/afsreg.h>
31 #include <WINNT/afssw.h>
32 #include <WINNT/vptab.h>
33
34
35 static char* whoami;
36
37
38 static int DoVptList(struct cmd_syndesc *as, void *arock)
39 {
40     struct vpt_iter vpiter;
41     struct vptab vpentry;
42
43     if (!vpt_Start(&vpiter)) {
44         while (!vpt_NextEntry(&vpiter, &vpentry)) {
45             printf("Partition: %s    Device: %s\n",
46                    vpentry.vp_name, vpentry.vp_dev);
47         }
48
49         (void)vpt_Finish(&vpiter);
50     }
51     return 0;
52 }
53
54 static int DoVptAdd(struct cmd_syndesc *as, void *arock)
55 {
56     char *vpName, *vpDev;
57     struct vptab vpentry;
58
59     vpName = as->parms[0].items->data;
60     vpDev = as->parms[1].items->data;
61
62     if (!vpt_PartitionNameValid(vpName)) {
63         afs_com_err(whoami, 0, "Partition name invalid");
64         return 1;
65     }
66
67     if (!vpt_DeviceNameValid(vpDev)) {
68         afs_com_err(whoami, 0, "Device name invalid");
69         return 1;
70     }
71
72     strcpy(vpentry.vp_name, vpName);
73     strcpy(vpentry.vp_dev, vpDev);
74
75     if (vpt_AddEntry(&vpentry)) {
76         afs_com_err(whoami, 0, "Unable to create vice partition table entry");
77         return 1;
78     }
79     return 0;
80 }
81
82 static int DoVptDel(struct cmd_syndesc *as, void *arock)
83 {
84     char *vpName;
85
86     vpName = as->parms[0].items->data;
87
88     if (!vpt_PartitionNameValid(vpName)) {
89         afs_com_err(whoami, 0, "Partition name invalid");
90         return 1;
91     }
92
93     if (vpt_RemoveEntry(vpName) && errno != ENOENT) {
94         afs_com_err(whoami, 0, "Unable to remove vice partition table entry");
95         return 1;
96     }
97     return 0;
98 }
99
100
101 static int DoDirGet(struct cmd_syndesc *as, void *arock)
102 {
103     char *buf;
104
105     if (afssw_GetServerInstallDir(&buf)) {
106         afs_com_err(whoami, 0,
107                 "Failed reading AFS install dir entry (or does not exist)");
108         return 1;
109     }
110
111     printf("AFS server installation directory: %s\n", buf);
112
113     free(buf);
114     return (0);
115 }
116
117
118 static int DoDirSet(struct cmd_syndesc *as, void *arock)
119 {
120     long status;
121     HKEY key;
122     char *afsPath;
123
124     afsPath = as->parms[0].items->data;
125
126     /* open AFS sw version key; create if does not exist */
127     status = RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_SVR_SW_VERSION_KEY,
128                            KEY_WRITE, 1 /* create */, &key, NULL);
129
130     if (status == ERROR_SUCCESS) {
131         /* write AFS directory value */
132         status = RegSetValueEx(key, AFSREG_SVR_SW_VERSION_DIR_VALUE,
133                                0, REG_SZ, afsPath, strlen(afsPath) + 1);
134
135         RegCloseKey(key);
136     }
137
138     if (status) {
139         afs_com_err(whoami, 0, "Unable to set AFS installation directory entry");
140     }
141
142     return (status ? 1 : 0);
143 }
144
145
146 static int DoBosCfg(struct cmd_syndesc *as, void *arock)
147 {
148     char *bosSvcPath = NULL;
149     SC_HANDLE scmHandle = NULL, svcHandle = NULL;
150     int code;
151
152     /* determine if using specified or default service binary path */
153     if (as->parms[0].items) {
154         /* BOS control service binary path specified */
155         if (asprintf(&bosSvcPath, "\"%s\"", as->parms[0].items->data) < 0) {
156             afs_com_err(whoami, 0, "out of memory building binary path");
157             return 1;
158         }
159     } else {
160         /* no BOS control service binary path specified; check for default */
161         char *dirBuf;
162
163         if (afssw_GetServerInstallDir(&dirBuf)) {
164             afs_com_err(whoami, 0,
165                     "binary path not specified and AFS server installation directory not set");
166             return 1;
167         }
168         if (asprintf(&bosSvcPath, "\"%s%s/%s\"",
169                      dirBuf,
170                      AFSDIR_CANONICAL_SERVER_BIN_DIRPATH,
171                      AFSREG_SVR_SVC_IMAGENAME_DATA) < 0) {
172             afs_com_err(whoami, 0, "out of memory building binary path");
173             return 1;
174         }
175     }
176
177     /* create BOS control service */
178     scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
179
180     if (scmHandle == NULL) {
181         DWORD status = GetLastError();
182         char *reason = "";
183
184         if (status == ERROR_ACCESS_DENIED) {
185             reason = "(insufficient privilege)";
186         }
187         afs_com_err(whoami, 0, "unable to connect to the SCM %s", reason);
188         code = 1;
189         goto out;
190     }
191
192     svcHandle = CreateService(scmHandle,
193                               AFSREG_SVR_SVC_NAME,
194                               AFSREG_SVR_SVC_DISPLAYNAME_DATA,
195                               STANDARD_RIGHTS_REQUIRED,
196                               SERVICE_WIN32_OWN_PROCESS,
197                               SERVICE_AUTO_START,
198                               SERVICE_ERROR_NORMAL,
199                               bosSvcPath,
200                               NULL,   /* load order group */
201                               NULL,   /* tag id */
202                               NULL,   /* dependencies */
203                               NULL,   /* service start name */
204                               NULL);  /* password */
205
206     if (svcHandle == NULL) {
207         DWORD status = GetLastError();
208         char *reason = "";
209
210         if (status == ERROR_SERVICE_EXISTS || status == ERROR_DUP_NAME) {
211             reason = "(service or display name already exists)";
212         }
213         afs_com_err(whoami, 0, "unable to create service %s", reason);
214         code = 1;
215     } else {
216         code = 0;
217     }
218
219 out:
220     free(bosSvcPath);
221     if (svcHandle != NULL)
222         CloseServiceHandle(svcHandle);
223     if (scmHandle != NULL)
224         CloseServiceHandle(scmHandle);
225     return code;
226 }
227
228
229 static int DoBosDel(struct cmd_syndesc *as, void *arock)
230 {
231     int rc = 0;
232     SC_HANDLE scmHandle, svcHandle;
233
234     scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
235
236     if (scmHandle == NULL) {
237         DWORD status = GetLastError();
238         char *reason = "";
239
240         if (status == ERROR_ACCESS_DENIED) {
241             reason = "(insufficient privilege)";
242         }
243         afs_com_err(whoami, 0, "unable to connect to the SCM %s", reason);
244         return 1;
245     }
246
247     svcHandle = OpenService(scmHandle,
248                             AFSREG_SVR_SVC_NAME, STANDARD_RIGHTS_REQUIRED);
249
250     if (svcHandle == NULL) {
251         DWORD status = GetLastError();
252
253         if (status != ERROR_SERVICE_DOES_NOT_EXIST) {
254             afs_com_err(whoami, 0, "unable to open service");
255             rc = 1;
256         }
257         CloseServiceHandle(scmHandle);
258         return rc;
259     }
260
261     if (!DeleteService(svcHandle)) {
262         DWORD status = GetLastError();
263
264         if (status != ERROR_SERVICE_MARKED_FOR_DELETE) {
265             afs_com_err(whoami, 0, "service delete failed");
266             rc = 1;
267         }
268     }
269     CloseServiceHandle(svcHandle);
270     CloseServiceHandle(scmHandle);
271     return (rc);
272 }
273
274 static int DoVersionGet(struct cmd_syndesc *as, void *arock)
275 {
276     unsigned major, minor, patch;
277
278     printf("\n");
279
280     if (!afssw_GetClientVersion(&major, &minor, &patch)) {
281         printf("Client: major = %u, minor = %u, patch = %u\n",
282                major, minor, patch);
283     } else {
284         printf("Client version information not available.\n");
285     }
286
287     if (!afssw_GetServerVersion(&major, &minor, &patch)) {
288         printf("Server: major = %u, minor = %u, patch = %u\n",
289                major, minor, patch);
290     } else {
291         printf("Server version information not available.\n");
292     }
293     return 0;
294 }
295
296
297 static void
298 SetupVptCmd(void)
299 {
300     struct cmd_syndesc  *ts;
301
302     ts = cmd_CreateSyntax("vptlist", DoVptList, NULL, 0,
303                           "list vice partition table");
304
305     ts = cmd_CreateSyntax("vptadd", DoVptAdd, NULL, 0,
306                           "add entry to vice partition table");
307     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED, "partition name");
308     cmd_AddParm(ts, "-dev", CMD_SINGLE, CMD_REQUIRED, "device name");
309
310     ts = cmd_CreateSyntax("vptdel", DoVptDel, NULL, 0,
311                           "remove entry from vice partition table");
312     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED, "partition name");
313 }
314
315 static void
316 SetupDirCmd(void)
317 {
318     struct cmd_syndesc  *ts;
319
320     ts = cmd_CreateSyntax("dirget", DoDirGet, NULL, 0,
321                           "display the AFS server installation directory");
322
323     ts = cmd_CreateSyntax("dirset", DoDirSet, NULL, 0,
324                           "set the AFS server installation directory");
325     cmd_AddParm(ts, "-path", CMD_SINGLE, CMD_REQUIRED, "directory path");
326 }
327
328 static void
329 SetupBosCmd(void)
330 {
331     struct cmd_syndesc  *ts;
332
333     ts = cmd_CreateSyntax("boscfg", DoBosCfg, NULL, 0,
334                           "configure the AFS BOS control service");
335     cmd_AddParm(ts, "-path", CMD_SINGLE, CMD_OPTIONAL, "service binary path");
336
337     ts = cmd_CreateSyntax("bosdel", DoBosDel, NULL, 0,
338                           "delete (unconfigure) the AFS BOS control service");
339 }
340
341 static void
342 SetupVersionCmd(void)
343 {
344     struct cmd_syndesc  *ts;
345
346     ts = cmd_CreateSyntax("version", DoVersionGet, NULL, 0,
347                           "display AFS version information");
348 }
349
350
351
352
353 int main(int argc, char *argv[])
354 {
355     int code;
356
357     whoami = argv[0];
358
359     /* initialize command syntax */
360     initialize_CMD_error_table();
361
362     SetupVptCmd();
363     SetupDirCmd();
364     SetupBosCmd();
365     SetupVersionCmd();
366
367     /* execute command */
368     code = cmd_Dispatch(argc, argv);
369
370     return (code);
371 }