c8d95cbb361631cf823e7b11be383b79ee1fed4c
[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, char *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, char *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         com_err(whoami, 0, "Partition name invalid");
64         return 1;
65     }
66
67     if (!vpt_DeviceNameValid(vpDev)) {
68         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         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, char *arock)
83 {
84     char *vpName;
85
86     vpName = as->parms[0].items->data;
87
88     if (!vpt_PartitionNameValid(vpName)) {
89         com_err(whoami, 0, "Partition name invalid");
90         return 1;
91     }
92
93     if (vpt_RemoveEntry(vpName) && errno != ENOENT) {
94         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, char *arock)
102 {
103     char *buf;
104
105     if (afssw_GetServerInstallDir(&buf)) {
106         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, char *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         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, char *arock)
147 {
148     char bosSvcPath[AFSDIR_PATH_MAX];
149     SC_HANDLE scmHandle, svcHandle;
150
151     /* determine if using specified or default service binary path */
152     if (as->parms[0].items) {
153         /* BOS control service binary path specified */
154         sprintf(bosSvcPath, "\"%s\"", as->parms[0].items->data);
155     } else {
156         /* no BOS control service binary path specified; check for default */
157         char *dirBuf;
158
159         if (afssw_GetServerInstallDir(&dirBuf)) {
160             com_err(whoami, 0,
161                     "binary path not specified and AFS server installation directory not set");
162             return 1;
163         }
164         sprintf(bosSvcPath, "\"%s%s/%s\"",
165                 dirBuf,
166                 AFSDIR_CANONICAL_SERVER_BIN_DIRPATH,
167                 AFSREG_SVR_SVC_IMAGENAME_DATA);
168     }
169
170     /* create BOS control service */
171     scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
172
173     if (scmHandle == NULL) {
174         DWORD status = GetLastError();
175         char *reason = "";
176
177         if (status == ERROR_ACCESS_DENIED) {
178             reason = "(insufficient privilege)";
179         }
180         com_err(whoami, 0, "unable to connect to the SCM %s", reason);
181         return 1;
182     }
183
184     svcHandle = CreateService(scmHandle,
185                               AFSREG_SVR_SVC_NAME,
186                               AFSREG_SVR_SVC_DISPLAYNAME_DATA,
187                               STANDARD_RIGHTS_REQUIRED,
188                               SERVICE_WIN32_OWN_PROCESS,
189                               SERVICE_AUTO_START,
190                               SERVICE_ERROR_NORMAL,
191                               bosSvcPath,
192                               NULL,   /* load order group */
193                               NULL,   /* tag id */
194                               NULL,   /* dependencies */
195                               NULL,   /* service start name */
196                               NULL);  /* password */
197
198     if (svcHandle == NULL) {
199         DWORD status = GetLastError();
200         char *reason = "";
201
202         if (status == ERROR_SERVICE_EXISTS || status == ERROR_DUP_NAME) {
203             reason = "(service or display name already exists)";
204         }
205         com_err(whoami, 0, "unable to create service %s", reason);
206         CloseServiceHandle(scmHandle);
207         return 1;
208     }
209
210     CloseServiceHandle(svcHandle);
211     CloseServiceHandle(scmHandle);
212     return (0);
213 }
214
215
216 static int DoBosDel(struct cmd_syndesc *as, char *arock)
217 {
218     int rc = 0;
219     SC_HANDLE scmHandle, svcHandle;
220
221     scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
222
223     if (scmHandle == NULL) {
224         DWORD status = GetLastError();
225         char *reason = "";
226
227         if (status == ERROR_ACCESS_DENIED) {
228             reason = "(insufficient privilege)";
229         }
230         com_err(whoami, 0, "unable to connect to the SCM %s", reason);
231         return 1;
232     }
233
234     svcHandle = OpenService(scmHandle,
235                             AFSREG_SVR_SVC_NAME, STANDARD_RIGHTS_REQUIRED);
236
237     if (svcHandle == NULL) {
238         DWORD status = GetLastError();
239
240         if (status != ERROR_SERVICE_DOES_NOT_EXIST) {
241             com_err(whoami, 0, "unable to open service");
242             rc = 1;
243         }
244         CloseServiceHandle(scmHandle);
245         return rc;
246     }
247
248     if (!DeleteService(svcHandle)) {
249         DWORD status = GetLastError();
250
251         if (status != ERROR_SERVICE_MARKED_FOR_DELETE) {
252             com_err(whoami, 0, "service delete failed");
253             rc = 1;
254         }
255     }
256     CloseServiceHandle(svcHandle);
257     CloseServiceHandle(scmHandle);
258     return (rc);
259 }
260
261 static int DoVersionGet(struct cmd_syndesc *as, char *arock)
262 {
263     unsigned major, minor, patch;
264
265     printf("\n");
266
267     if (!afssw_GetClientVersion(&major, &minor, &patch)) {
268         printf("Client: major = %u, minor = %u, patch = %u\n",
269                major, minor, patch);
270     } else {
271         printf("Client version information not available.\n");
272     }
273
274     if (!afssw_GetServerVersion(&major, &minor, &patch)) {
275         printf("Server: major = %u, minor = %u, patch = %u\n",
276                major, minor, patch);
277     } else {
278         printf("Server version information not available.\n");
279     }
280     return 0;
281 }
282
283
284 static void
285 SetupVptCmd(void)
286 {
287     struct cmd_syndesc  *ts;
288
289     ts = cmd_CreateSyntax("vptlist", DoVptList, 0,
290                           "list vice partition table");
291
292     ts = cmd_CreateSyntax("vptadd", DoVptAdd, 0,
293                           "add entry to vice partition table");
294     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED, "partition name");
295     cmd_AddParm(ts, "-dev", CMD_SINGLE, CMD_REQUIRED, "device name");
296
297     ts = cmd_CreateSyntax("vptdel", DoVptDel, 0,
298                           "remove entry from vice partition table");
299     cmd_AddParm(ts, "-partition", CMD_SINGLE, CMD_REQUIRED, "partition name");
300 }
301
302 static void
303 SetupDirCmd(void)
304 {
305     struct cmd_syndesc  *ts;
306
307     ts = cmd_CreateSyntax("dirget", DoDirGet, 0,
308                           "display the AFS server installation directory");
309
310     ts = cmd_CreateSyntax("dirset", DoDirSet, 0,
311                           "set the AFS server installation directory");
312     cmd_AddParm(ts, "-path", CMD_SINGLE, CMD_REQUIRED, "directory path");
313 }
314
315 static void
316 SetupBosCmd(void)
317 {
318     struct cmd_syndesc  *ts;
319
320     ts = cmd_CreateSyntax("boscfg", DoBosCfg, 0,
321                           "configure the AFS BOS control service");
322     cmd_AddParm(ts, "-path", CMD_SINGLE, CMD_OPTIONAL, "service binary path");
323
324     ts = cmd_CreateSyntax("bosdel", DoBosDel, 0,
325                           "delete (unconfigure) the AFS BOS control service");
326 }
327
328 static void
329 SetupVersionCmd(void)
330 {
331     struct cmd_syndesc  *ts;
332
333     ts = cmd_CreateSyntax("version", DoVersionGet, 0,
334                           "display AFS version information");
335 }
336
337
338
339
340 int main(int argc, char *argv[])
341 {
342     int code;
343
344     whoami = argv[0];
345
346     /* initialize command syntax */
347     initialize_cmd_error_table();
348
349     SetupVptCmd();
350     SetupDirCmd();
351     SetupBosCmd();
352     SetupVersionCmd();
353
354     /* execute command */
355     code = cmd_Dispatch(argc, argv);
356
357     return (code);
358 }