Initial import
[samba] / examples / libmsrpc / test / reg / regkeycreate.c
1 /*tests creating a registry key*/
2
3 #include "libmsrpc.h"
4
5 #define MAX_KEYS_PER_ENUM 3
6
7 int main() {
8    CacServerHandle *hnd = NULL;
9     TALLOC_CTX *mem_ctx  = NULL;
10  
11     fstring key_name;
12
13     fstring key_to_create;
14
15     mem_ctx = talloc_init("regcreatekey");
16  
17     hnd = cac_NewServerHandle(True);
18
19     printf("Enter server to connect to: ");
20     fscanf(stdin, "%s", hnd->server);
21
22     printf("Enter key to open: ");
23     fscanf(stdin, "%s", key_name);
24
25     printf("Enter key to create: ");
26     fscanf(stdin, "%s", key_to_create);
27
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));
30        cac_FreeHandle(hnd);
31        exit(-1);
32     }
33
34     printf("trying to open key %s...\n", key_name);
35
36     struct RegOpenKey rok;
37     ZERO_STRUCT(rok);
38
39     rok.in.parent_key = NULL;
40     rok.in.name   = key_name;
41     rok.in.access = REG_KEY_ALL;
42
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));
45        goto done;
46     }
47
48     printf("Creating key %s...\n", key_to_create);
49
50     struct RegCreateKey rck;
51     ZERO_STRUCT(rck);
52
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;
57
58     if(!cac_RegCreateKey(hnd, mem_ctx, &rck)) {
59        fprintf(stderr, "Could not create key. Error %s\n", nt_errstr(hnd->status));
60        goto done;
61     }
62
63     if(!cac_RegClose(hnd, mem_ctx, rck.out.key)) {
64        fprintf(stderr, "Could not close key.  Error %s\n", nt_errstr(hnd->status));
65        goto done;
66     }
67
68     /**enumerate all the subkeys*/
69     printf("Enumerating all subkeys:\n");
70
71     struct RegEnumKeys ek;
72     ZERO_STRUCT(ek);
73
74     ek.in.key = rok.out.key;
75     ek.in.max_keys = 50;
76
77     while(cac_RegEnumKeys(hnd, mem_ctx, &ek)) {
78        int j;
79
80        for(j = 0; j < ek.out.num_keys; j++) {
81           printf(" Key name: %s\n", ek.out.key_names[j]);
82        }
83     }
84
85     if(CAC_OP_FAILED(hnd->status)) {
86        fprintf(stderr, "Could not enumerate keys: %s\n", nt_errstr(hnd->status));
87        goto done;
88     }
89
90     printf("deleting key %s\n", key_to_create);
91
92     struct RegDeleteKey rdk;
93     ZERO_STRUCT(rdk);
94
95     rdk.in.parent_key = rok.out.key;
96     rdk.in.name   = key_to_create;
97
98     if(!cac_RegDeleteKey(hnd, mem_ctx, &rdk)) {
99        fprintf(stderr, "Could not delete key.  Error %s\n", nt_errstr(hnd->status));
100     }
101
102     printf("closing key %s...\n", key_name);
103
104     if(!cac_RegClose(hnd, mem_ctx, rok.out.key)) {
105        fprintf(stderr, "Could not close handle %s\n", nt_errstr(hnd->status));
106     }
107
108 done:
109     cac_FreeHandle(hnd);
110
111     talloc_destroy(mem_ctx);
112
113     return 0;
114
115 }