1/*lookup names or rids*/
2
3#include "libmsrpc.h"
4#include "test_util.h"
5
6int main(int argc, char **argv) {
7   CacServerHandle *hnd = NULL;
8   TALLOC_CTX *mem_ctx = NULL;
9
10
11   struct SamGetNamesFromRids sgn;
12   struct SamGetRidsFromNames sgr;
13
14   fstring tmp;
15   fstring input;
16
17   int i;
18
19   mem_ctx = talloc_init("cac_samenum");
20
21   hnd = cac_NewServerHandle(True);
22
23   cac_parse_cmd_line(argc, argv, hnd);
24
25   if(!cac_Connect(hnd, NULL)) {
26      fprintf(stderr, "Could not connect to server %s. Error: %s\n", hnd->server, nt_errstr(hnd->status));
27      exit(-1);
28   }
29
30   struct SamOpenDomain sod;
31   ZERO_STRUCT(sod);
32
33   sod.in.access = MAXIMUM_ALLOWED_ACCESS;
34
35   if(!cac_SamOpenDomain(hnd, mem_ctx, &sod)) {
36      fprintf(stderr, "Could not open domain. Error: %s\n", nt_errstr(hnd->status));
37      goto done;
38   }
39
40   tmp[0] = 0x00;
41   while(tmp[0] != 'q') {
42      printf("get [n]ames or get [r]ids or [q]uit: ");
43      cactest_readline(stdin, tmp);
44
45      switch(tmp[0]) {
46         case 'n':
47            ZERO_STRUCT(sgn);
48
49            sgn.in.dom_hnd = sod.out.dom_hnd;
50
51            printf("How many rids will you enter: ");
52            scanf("%d", &sgn.in.num_rids);
53
54            sgn.in.rids = talloc_array(mem_ctx, int, sgn.in.num_rids);
55
56            for(i = 0; i < sgn.in.num_rids; i++) {
57               printf("  Enter RID %d: 0x", i);
58               scanf("%x", &sgn.in.rids[i]);
59            }
60
61            printf("Getting names...\n");
62
63            if(!cac_SamGetNamesFromRids(hnd, mem_ctx, &sgn)) {
64               fprintf(stderr, "could not lookup names. Error: %s\n", nt_errstr(hnd->status));
65               talloc_free(sgn.in.rids);
66               continue;
67            }
68
69            printf("Found %d names:\n", sgn.out.num_names);
70
71            for(i = 0; i < sgn.out.num_names; i++) {
72               printf(" RID:  0x%x ", sgn.out.map[i].rid);
73
74               if(sgn.out.map[i].found) {
75                  printf("Name: %s\n", sgn.out.map[i].name);
76               }
77               else {
78                  printf("Unknown RID\n");
79               }
80
81            }
82
83            break;
84
85         case 'r':
86            ZERO_STRUCT(sgr);
87
88            sgr.in.dom_hnd = sod.out.dom_hnd;
89
90            printf("How many names will you enter: ");
91            scanf("%d", &sgr.in.num_names);
92
93            sgr.in.names = talloc_array(mem_ctx, char *, sgr.in.num_names);
94
95            for(i = 0; i < sgr.in.num_names; i++) {
96               printf(" Enter name %d: ", (i+1));
97               cactest_readline(stdin, input);
98
99               sgr.in.names[i] = talloc_strdup(mem_ctx, input);
100            }
101
102            if(!cac_SamGetRidsFromNames(hnd, mem_ctx, &sgr)) {
103               fprintf(stderr, "Could not lookup names. Error: %s\n", nt_errstr(hnd->status));
104               continue;
105            }
106
107            printf("Found %d RIDs:\n", sgr.out.num_rids);
108
109            for(i = 0; i < sgr.out.num_rids; i++) {
110               printf(" Name: %s ", sgr.out.map[i].name);
111
112               if(sgr.out.map[i].found) {
113                  printf("RID: 0x%x\n", sgr.out.map[i].rid);
114               }
115               else {
116                  printf("Unknown name\n");
117               }
118            }
119
120            break;
121         case 'q':
122            printf("\n");
123            break;
124         default:
125            printf("Invalid command!\n");
126      }
127   }
128
129
130   cac_SamClose(hnd, mem_ctx, sod.out.dom_hnd);
131   cac_SamClose(hnd, mem_ctx, sod.out.sam);
132
133done:
134   talloc_destroy(mem_ctx);
135   cac_FreeHandle(hnd);
136
137   return 0;
138
139}
140
141