summaryrefslogtreecommitdiff
path: root/examples/libmsrpc/test/sam/samlookup.c
blob: 32be50d4b925efba0af94b841b32737a0bd4f3d0 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*lookup names or rids*/

#include "libmsrpc.h"
#include "test_util.h"

int main(int argc, char **argv) {
   CacServerHandle *hnd = NULL;
   TALLOC_CTX *mem_ctx = NULL;
            
   
   struct SamGetNamesFromRids sgn;
   struct SamGetRidsFromNames sgr;

   fstring tmp;
   fstring input;
   
   int i;

   mem_ctx = talloc_init("cac_samenum");

   hnd = cac_NewServerHandle(True);

   cac_parse_cmd_line(argc, argv, hnd);

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

   struct SamOpenDomain sod;
   ZERO_STRUCT(sod);

   sod.in.access = MAXIMUM_ALLOWED_ACCESS; 

   if(!cac_SamOpenDomain(hnd, mem_ctx, &sod)) {
      fprintf(stderr, "Could not open domain. Error: %s\n", nt_errstr(hnd->status));
      goto done;
   }

   tmp[0] = 0x00;
   while(tmp[0] != 'q') {
      printf("get [n]ames or get [r]ids or [q]uit: ");
      cactest_readline(stdin, tmp);

      switch(tmp[0]) {
         case 'n':
            ZERO_STRUCT(sgn);

            sgn.in.dom_hnd = sod.out.dom_hnd;

            printf("How many rids will you enter: ");
            scanf("%d", &sgn.in.num_rids);

            sgn.in.rids = talloc_array(mem_ctx, int, sgn.in.num_rids);
            
            for(i = 0; i < sgn.in.num_rids; i++) {
               printf("  Enter RID %d: 0x", i);
               scanf("%x", &sgn.in.rids[i]);
            }

            printf("Getting names...\n");

            if(!cac_SamGetNamesFromRids(hnd, mem_ctx, &sgn)) {
               fprintf(stderr, "could not lookup names. Error: %s\n", nt_errstr(hnd->status));
               talloc_free(sgn.in.rids);
               continue;
            }

            printf("Found %d names:\n", sgn.out.num_names);

            for(i = 0; i < sgn.out.num_names; i++) {
               printf(" RID:  0x%x ", sgn.out.map[i].rid);

               if(sgn.out.map[i].found) {
                  printf("Name: %s\n", sgn.out.map[i].name);
               }
               else {
                  printf("Unknown RID\n");
               }

            }

            break;

         case 'r':
            ZERO_STRUCT(sgr);

            sgr.in.dom_hnd = sod.out.dom_hnd;

            printf("How many names will you enter: ");
            scanf("%d", &sgr.in.num_names);

            sgr.in.names = talloc_array(mem_ctx, char *, sgr.in.num_names);

            for(i = 0; i < sgr.in.num_names; i++) {
               printf(" Enter name %d: ", (i+1));
               cactest_readline(stdin, input);

               sgr.in.names[i] = talloc_strdup(mem_ctx, input);
            }

            if(!cac_SamGetRidsFromNames(hnd, mem_ctx, &sgr)) {
               fprintf(stderr, "Could not lookup names. Error: %s\n", nt_errstr(hnd->status));
               continue;
            }

            printf("Found %d RIDs:\n", sgr.out.num_rids);

            for(i = 0; i < sgr.out.num_rids; i++) {
               printf(" Name: %s ", sgr.out.map[i].name);

               if(sgr.out.map[i].found) {
                  printf("RID: 0x%x\n", sgr.out.map[i].rid);
               }
               else {
                  printf("Unknown name\n");
               }
            }

            break;
         case 'q':
            printf("\n");
            break;
         default:
            printf("Invalid command!\n");
      }
   }


   cac_SamClose(hnd, mem_ctx, sod.out.dom_hnd);
   cac_SamClose(hnd, mem_ctx, sod.out.sam);

done:
   talloc_destroy(mem_ctx);
   cac_FreeHandle(hnd);

   return 0;

}