Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / examples / libmsrpc / test / reg / regkeyenum.c
blobf140d95723a564f35689c46d7ff6dd16584a2937
1 /*tests enumerating keys or values*/
3 #include "libmsrpc.h"
5 #define MAX_KEYS_PER_ENUM 3
7 int main() {
8 CacServerHandle *hnd = NULL;
9 TALLOC_CTX *mem_ctx = NULL;
11 int num_keys;
13 int max_enum;
15 int i;
17 fstring *key_names;
19 mem_ctx = talloc_init("regkeyenum");
21 hnd = cac_NewServerHandle(True);
23 printf("Enter server to connect to: ");
24 fscanf(stdin, "%s", hnd->server);
26 printf("How many keys do you want to open?: ");
27 fscanf(stdin, "%d", &num_keys);
29 printf("How many keys per enum?: ");
30 fscanf(stdin, "%d", &max_enum);
32 key_names = TALLOC_ARRAY(mem_ctx, fstring , num_keys);
33 if(!key_names) {
34 fprintf(stderr, "No memory\n");
35 exit(-1);
38 for(i = 0; i < num_keys; i++) {
39 printf("Enter key to open: ");
40 fscanf(stdin, "%s", key_names[i]);
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);
49 for(i = 0; i < num_keys; i++) {
50 printf("trying to open key %s...\n", key_names[i]);
52 struct RegOpenKey rok;
53 ZERO_STRUCT(rok);
55 rok.in.parent_key = NULL;
56 rok.in.name = key_names[i];
57 rok.in.access = REG_KEY_ALL;
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;
64 /**enumerate all the subkeys*/
65 printf("Enumerating all subkeys:\n");
67 struct RegEnumKeys ek;
68 ZERO_STRUCT(ek);
70 ek.in.key = rok.out.key;
71 ek.in.max_keys = max_enum;
73 while(cac_RegEnumKeys(hnd, mem_ctx, &ek)) {
74 int j;
76 for(j = 0; j < ek.out.num_keys; j++) {
77 printf(" Key name: %s\n", ek.out.key_names[j]);
81 if(CAC_OP_FAILED(hnd->status)) {
82 fprintf(stderr, "Could not enumerate keys: %s\n", nt_errstr(hnd->status));
83 continue;
86 printf("closing key %s...\n", key_names[i]);
88 if(!cac_RegClose(hnd, mem_ctx, rok.out.key)) {
89 fprintf(stderr, "Could not close handle %s\n", nt_errstr(hnd->status));
93 cac_FreeHandle(hnd);
95 talloc_destroy(mem_ctx);
97 return 0;