r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[samba.git] / examples / libmsrpc / test / reg / regkeyenum.c
1 /*tests enumerating keys or values*/
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     int num_keys;
12
13     int max_enum;
14
15     int i;
16
17     fstring *key_names;
18
19     mem_ctx = talloc_init("regkeyenum");
20  
21     hnd = cac_NewServerHandle(True);
22
23     printf("Enter server to connect to: ");
24     fscanf(stdin, "%s", hnd->server);
25
26     printf("How many keys do you want to open?: ");
27     fscanf(stdin, "%d", &num_keys);
28
29     printf("How many keys per enum?: ");
30     fscanf(stdin, "%d", &max_enum);
31
32     key_names = TALLOC_ARRAY(mem_ctx, fstring , num_keys);
33     if(!key_names) {
34        fprintf(stderr, "No memory\n");
35        exit(-1);
36     }
37
38     for(i = 0; i < num_keys; i++) {
39        printf("Enter key to open: ");
40        fscanf(stdin, "%s", key_names[i]);
41     }
42
43     if(!cac_Connect(hnd, NULL)) {
44        fprintf(stderr, "Could not connect to server.\n Error: %s.\n errno: %s\n", nt_errstr(hnd->status), strerror(errno));
45        cac_FreeHandle(hnd);
46        exit(-1);
47     }
48
49     for(i = 0; i < num_keys; i++) {
50        printf("trying to open key %s...\n", key_names[i]);
51
52        struct RegOpenKey rok;
53        ZERO_STRUCT(rok);
54
55        rok.in.parent_key = NULL;
56        rok.in.name   = key_names[i];
57        rok.in.access = REG_KEY_ALL;
58
59        if(!cac_RegOpenKey(hnd, mem_ctx, &rok)) {
60           fprintf(stderr, "Could not open key %s\n Error: %s\n", rok.in.name, nt_errstr(hnd->status));
61           continue;
62        }
63
64        /**enumerate all the subkeys*/
65        printf("Enumerating all subkeys:\n");
66
67        struct RegEnumKeys ek;
68        ZERO_STRUCT(ek);
69
70        ek.in.key = rok.out.key;
71        ek.in.max_keys = max_enum;
72
73        while(cac_RegEnumKeys(hnd, mem_ctx, &ek)) {
74           int j;
75
76           for(j = 0; j < ek.out.num_keys; j++) {
77              printf(" Key name: %s\n", ek.out.key_names[j]);
78           }
79        }
80
81        if(CAC_OP_FAILED(hnd->status)) {
82           fprintf(stderr, "Could not enumerate keys: %s\n", nt_errstr(hnd->status));
83           continue;
84        }
85
86        printf("closing key %s...\n", key_names[i]);
87
88        if(!cac_RegClose(hnd, mem_ctx, rok.out.key)) {
89           fprintf(stderr, "Could not close handle %s\n", nt_errstr(hnd->status));
90        }
91     }
92
93     cac_FreeHandle(hnd);
94
95     talloc_destroy(mem_ctx);
96
97     return 0;
98
99 }