Initial import
[samba] / examples / libmsrpc / test / sam / samenum.c
1 /*enumerate users/groups/aliases*/
2
3 #include "libmsrpc.h"
4 #include "test_util.h"
5
6 int main(int argc, char **argv) {
7    CacServerHandle *hnd = NULL;
8    TALLOC_CTX *mem_ctx = NULL;
9             
10    
11    struct SamEnumUsers eu;
12    struct SamEnumGroups eg;
13    struct SamEnumAliases ea;
14
15    fstring tmp;
16    
17    int i;
18
19    mem_ctx = talloc_init("cac_samenum");
20
21    hnd = cac_NewServerHandle(True);
22
23    cac_SetAuthDataFn(hnd, cactest_GetAuthDataFn);
24
25    cac_parse_cmd_line(argc, argv, hnd);
26
27    if(!cac_Connect(hnd, NULL)) {
28       fprintf(stderr, "Could not connect to server %s. Error: %s\n", hnd->server, nt_errstr(hnd->status));
29       exit(-1);
30    }
31
32    struct SamOpenDomain sod;
33    ZERO_STRUCT(sod);
34
35    sod.in.access = MAXIMUM_ALLOWED_ACCESS; 
36
37    if(!cac_SamOpenDomain(hnd, mem_ctx, &sod)) {
38       fprintf(stderr, "Could not open domain. Error: %s\n", nt_errstr(hnd->status));
39       goto done;
40    }
41
42    tmp[0] = 0x00;
43    while(tmp[0] != 'q') {
44       printf("Enumerate [u]sers, [g]roups or [a]liases or [q]uit: ");
45       cactest_readline(stdin, tmp);
46
47       switch(tmp[0]) {
48          case 'u':
49             ZERO_STRUCT(eu);
50
51             eu.in.dom_hnd = sod.out.dom_hnd;
52             
53             printf("ACB mask (can be 0): ");
54             scanf("%hx", &eu.in.acb_mask);
55
56             while(cac_SamEnumUsers(hnd, mem_ctx, &eu)) {
57                printf("Enumerated %d users:\n", eu.out.num_users);
58                for(i = 0; i < eu.out.num_users; i++) {
59                   printf(" Name: %s\n", eu.out.names[i]);
60                   printf(" RID:  %d\n", eu.out.rids[i]);
61                }
62             }
63
64             if(CAC_OP_FAILED(hnd->status)) {
65                printf("Could not enumerate users. Error: %s\n", nt_errstr(hnd->status));
66             }
67             break;
68          case 'g':
69             ZERO_STRUCT(eg);
70             eg.in.dom_hnd = sod.out.dom_hnd;
71
72             printf("Enumerating groups...\n");
73             while(cac_SamEnumGroups(hnd, mem_ctx, &eg)) {
74                printf("Enumerated %d groups:\n", eg.out.num_groups);
75                for(i = 0; i < eg.out.num_groups; i++) {
76                   printf("RID:  %d\n", eg.out.rids[i]);
77                   printf("Name: %s\n", eg.out.names[i]);
78                   printf("Desc: %s\n", eg.out.descriptions[i]);
79                }
80             }
81
82             if(CAC_OP_FAILED(hnd->status)) {
83                printf("Could not enumerate Groups. Error: %s\n", nt_errstr(hnd->status));
84             }
85             break;
86          case 'a':
87             ZERO_STRUCT(ea);
88             ea.in.dom_hnd = sod.out.dom_hnd;
89
90             printf("Enumerating Aliases...\n");
91             while(cac_SamEnumAliases(hnd, mem_ctx, &ea)) {
92                printf("Enumerated %d aliases:\n", ea.out.num_aliases);
93
94                for(i = 0; i < ea.out.num_aliases; i++) {
95                   printf("RID:  %d\n", ea.out.rids[i]);
96                   printf("Name: %s\n", ea.out.names[i]);
97                   printf("Desc: %s\n", ea.out.descriptions[i]);
98                }
99             }
100             if(CAC_OP_FAILED(hnd->status)) {
101                printf("Could not enumerate Aliases. Error: %s\n", nt_errstr(hnd->status));
102             }
103             break;
104       }
105    }
106
107    cac_SamClose(hnd, mem_ctx, sod.out.dom_hnd);
108    cac_SamClose(hnd, mem_ctx, sod.out.sam);
109
110 done:
111    talloc_destroy(mem_ctx);
112    cac_FreeHandle(hnd);
113
114    return 0;
115
116 }
117