summaryrefslogtreecommitdiff
path: root/examples/libmsrpc/test/reg/regkey.c
blob: a90d06c6efe3886f3f7e948b2a40fea23f8c70b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*opens and closes a key*/

#include "libmsrpc.h"

int main() {
   CacServerHandle *hnd = NULL;
    TALLOC_CTX *mem_ctx  = NULL;
 
    fstring key;

    mem_ctx = talloc_init("regkey");
 
    hnd = cac_NewServerHandle(False);

    /*allocate some memory so get_auth_data_fn can do it's magic*/
    hnd->username = SMB_MALLOC_ARRAY(char, sizeof(fstring));
    hnd->domain   = SMB_MALLOC_ARRAY(char, sizeof(fstring));
    hnd->netbios_name = SMB_MALLOC_ARRAY(char, sizeof(fstring));
    hnd->password = SMB_MALLOC_ARRAY(char, sizeof(fstring));
 
    hnd->server   = SMB_MALLOC_ARRAY(char, sizeof(fstring));
 
    printf("Enter server to connect to: ");
    fscanf(stdin, "%s", hnd->server);

    printf("Enter key to open: ");
    fscanf(stdin, "%s", key);

    if(!cac_Connect(hnd, NULL)) {
       fprintf(stderr, "Could not connect to server.\n Error: %s.\n errno: %s\n", nt_errstr(hnd->status), strerror(errno));
       cac_FreeHandle(hnd);
       exit(-1);
    }

    struct RegConnect rc;
    ZERO_STRUCT(rc);

    rc.in.access = REG_KEY_ALL;
    rc.in.root   = HKEY_LOCAL_MACHINE;

    if(!cac_RegConnect(hnd, mem_ctx, &rc)) {
       fprintf(stderr, " Could not connect to registry. %s\n", nt_errstr(hnd->status));
       goto done;
    }

    printf("trying to open key %s...\n", key);

    
    struct RegOpenKey rok;
    ZERO_STRUCT(rok);

    rok.in.parent_key = rc.out.key;
    rok.in.name   = key;
    rok.in.access = REG_KEY_ALL;

    if(!cac_RegOpenKey(hnd, mem_ctx, &rok)) {
       fprintf(stderr, "Could not open key %s\n Error: %s\n", rok.in.name, nt_errstr(hnd->status));
       goto done;
    }

    if(!cac_RegClose(hnd, mem_ctx, rok.out.key)) {
       fprintf(stderr, "Could not close handle %s\n", nt_errstr(hnd->status));
    }

    if(!cac_RegClose(hnd, mem_ctx, rc.out.key)) {
       fprintf(stderr, " Could not close handle. %s\n", nt_errstr(hnd->status));
    }

done:
    cac_FreeHandle(hnd);

    talloc_destroy(mem_ctx);

    return 0;

}