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