1 /*tests creating a registry key*/
5 #define MAX_KEYS_PER_ENUM 3
8 CacServerHandle
*hnd
= NULL
;
9 TALLOC_CTX
*mem_ctx
= NULL
;
13 fstring key_to_create
;
15 mem_ctx
= talloc_init("regcreatekey");
17 hnd
= cac_NewServerHandle(True
);
19 printf("Enter server to connect to: ");
20 fscanf(stdin
, "%s", hnd
->server
);
22 printf("Enter key to open: ");
23 fscanf(stdin
, "%s", key_name
);
25 printf("Enter key to create: ");
26 fscanf(stdin
, "%s", key_to_create
);
28 if(!cac_Connect(hnd
, NULL
)) {
29 fprintf(stderr
, "Could not connect to server.\n Error: %s.\n errno: %s\n", nt_errstr(hnd
->status
), strerror(errno
));
34 printf("trying to open key %s...\n", key_name
);
36 struct RegOpenKey rok
;
39 rok
.in
.parent_key
= NULL
;
40 rok
.in
.name
= key_name
;
41 rok
.in
.access
= REG_KEY_ALL
;
43 if(!cac_RegOpenKey(hnd
, mem_ctx
, &rok
)) {
44 fprintf(stderr
, "Could not open key %s\n Error: %s\n", rok
.in
.name
, nt_errstr(hnd
->status
));
48 printf("Creating key %s...\n", key_to_create
);
50 struct RegCreateKey rck
;
53 rck
.in
.parent_key
= rok
.out
.key
;
54 rck
.in
.key_name
= talloc_strdup(mem_ctx
, key_to_create
);
55 rck
.in
.class_name
= talloc_strdup(mem_ctx
, "");
56 rck
.in
.access
= REG_KEY_ALL
;
58 if(!cac_RegCreateKey(hnd
, mem_ctx
, &rck
)) {
59 fprintf(stderr
, "Could not create key. Error %s\n", nt_errstr(hnd
->status
));
63 if(!cac_RegClose(hnd
, mem_ctx
, rck
.out
.key
)) {
64 fprintf(stderr
, "Could not close key. Error %s\n", nt_errstr(hnd
->status
));
68 /**enumerate all the subkeys*/
69 printf("Enumerating all subkeys:\n");
71 struct RegEnumKeys ek
;
74 ek
.in
.key
= rok
.out
.key
;
77 while(cac_RegEnumKeys(hnd
, mem_ctx
, &ek
)) {
80 for(j
= 0; j
< ek
.out
.num_keys
; j
++) {
81 printf(" Key name: %s\n", ek
.out
.key_names
[j
]);
85 if(CAC_OP_FAILED(hnd
->status
)) {
86 fprintf(stderr
, "Could not enumerate keys: %s\n", nt_errstr(hnd
->status
));
90 printf("deleting key %s\n", key_to_create
);
92 struct RegDeleteKey rdk
;
95 rdk
.in
.parent_key
= rok
.out
.key
;
96 rdk
.in
.name
= key_to_create
;
98 if(!cac_RegDeleteKey(hnd
, mem_ctx
, &rdk
)) {
99 fprintf(stderr
, "Could not delete key. Error %s\n", nt_errstr(hnd
->status
));
102 printf("closing key %s...\n", key_name
);
104 if(!cac_RegClose(hnd
, mem_ctx
, rok
.out
.key
)) {
105 fprintf(stderr
, "Could not close handle %s\n", nt_errstr(hnd
->status
));
111 talloc_destroy(mem_ctx
);