Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / examples / libmsrpc / test / reg / regkeycreate.c
blob50764f168225b94fdb105dd7e586c0f894e61679
1 /*tests creating a registry key*/
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 fstring key_name;
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));
30 cac_FreeHandle(hnd);
31 exit(-1);
34 printf("trying to open key %s...\n", key_name);
36 struct RegOpenKey rok;
37 ZERO_STRUCT(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));
45 goto done;
48 printf("Creating key %s...\n", key_to_create);
50 struct RegCreateKey rck;
51 ZERO_STRUCT(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));
60 goto done;
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;
68 /**enumerate all the subkeys*/
69 printf("Enumerating all subkeys:\n");
71 struct RegEnumKeys ek;
72 ZERO_STRUCT(ek);
74 ek.in.key = rok.out.key;
75 ek.in.max_keys = 50;
77 while(cac_RegEnumKeys(hnd, mem_ctx, &ek)) {
78 int j;
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));
87 goto done;
90 printf("deleting key %s\n", key_to_create);
92 struct RegDeleteKey rdk;
93 ZERO_STRUCT(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));
108 done:
109 cac_FreeHandle(hnd);
111 talloc_destroy(mem_ctx);
113 return 0;