summaryrefslogtreecommitdiff
path: root/examples/libmsrpc/cacusermgr/cacusermgr.c
blob: 77dd2505f6e5b736b641011621ebf42f2cbf45ce (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * Unix SMB/CIFS implementation. 
 * cacusermgr main implementation.
 *
 * Copyright (C) Chris Nicholls     2005
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 675
 * Mass Ave, Cambridge, MA 02139, USA.  */

#include "cacusermgr.h"

#define DEFAULT_MENU_LINES 15


void create_menu(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd) {
   struct SamCreateUser  cu;
   struct SamCreateGroup cg;

   fstring in;
   fstring tmp;

   if(!hnd || !mem_ctx || !dom_hnd) {
      printf("No Handle to SAM.\n");
      return;
   }

   /*the menu*/
   in[0] = '\0';
   while(in[0] != 'c' && in[0] != 'C' && in[0] != 'q' && in[0] != 'Q') {
      printf("\n");
      printf("[u] Create User\n");
      printf("[g] Create Group\n");
      printf("[m] Create Machine Account\n");
      printf("[c] Cancel\n\n");

      printf("Command: ");
      mgr_getline(in);

      printf("\n");

      switch(in[0]) {
         case 'u': /*create user*/
         case 'U':
            ZERO_STRUCT(cu);
            cu.in.dom_hnd = dom_hnd;
            cu.in.acb_mask = ACB_NORMAL;

            printf("Enter name: ");
            mgr_getline(tmp);
            cu.in.name = talloc_strdup(mem_ctx, tmp);

            if(!cac_SamCreateUser(hnd, mem_ctx, &cu)) {
               printerr("Could not create user.", hnd->status);
            }
            else {
               user_menu(hnd, mem_ctx, dom_hnd, cu.out.user_hnd);
            }

            /*this will break the loop and send us back to the main menu*/
            in[0] = 'c';
            break;

         case 'g': /*create group*/
         case 'G':
            ZERO_STRUCT(cg);
            cg.in.dom_hnd = dom_hnd;
            cg.in.access  = MAXIMUM_ALLOWED_ACCESS;

            printf("Enter name: ");
            mgr_getline(tmp);
            cg.in.name = talloc_strdup(mem_ctx, tmp);

            if(!cac_SamCreateGroup(hnd, mem_ctx, &cg)) {
               printerr("Could not create group.", hnd->status);
            }
            else {
               group_menu(hnd, mem_ctx, dom_hnd, cg.out.group_hnd);
            }

            /*this will break the loop and send us back to the main menu*/
            in[0] = 'c';
            break;

         case 'm': /*create machine account*/
         case 'M':
            ZERO_STRUCT(cu);
            cu.in.dom_hnd  = dom_hnd;
            cu.in.acb_mask = ACB_WSTRUST;

            printf("Enter machine name: ");
            mgr_getline(tmp);

            /*make sure we have a $ on the end*/
            if(tmp[strlen(tmp) - 1] != '$')
               cu.in.name = talloc_asprintf(mem_ctx, "%s$", tmp);
            else
               cu.in.name = talloc_strdup(mem_ctx, tmp);

            strlower_m(cu.in.name);

            printf("Creating account: %s\n", cu.in.name);

            if(!cac_SamCreateUser(hnd, mem_ctx, &cu)) {
               printerr("Could not create account.", hnd->status);
            }
            else {
               user_menu(hnd, mem_ctx, dom_hnd, cu.out.user_hnd);
            }

            /*this will break the loop and send us back to the main menu*/
            in[0] = 'c';
            break;

         case 'c': /*cancel*/
         case 'C':
         case 'q':
         case 'Q':
            /*do nothing*/
            break;

         default:
            printf("Invalid option\n");
      }
   }

   return;
}

void main_menu(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd) {
   fstring in;
   
   uint32 rid_type = 0;

   struct SamOpenUser   openu;
   struct SamOpenGroup  openg;
   struct SamEnumUsers  enumu;
   struct SamEnumGroups enumg;
   struct SamFlush      flush;

   char *name = NULL;
   uint32 rid = 0;

   if(!hnd || !mem_ctx || !dom_hnd) {
      printf("No handle to SAM.\n");
      return;
   }

   /*initialize this here and don't worry about it later*/
   ZERO_STRUCT(flush);
   flush.in.dom_hnd = dom_hnd;

   in[0] = '\0';

   /*handle the menu and commands*/
   while(in[0] != 'q' && in[0] != 'Q') {
      printf("\n");

      printf("[o] Open User or Group\n");
      printf("[c] Create Account or Group\n");
      printf("[u] List Users\n");
      printf("[g] List Groups\n");
      printf("[m] List Machine Accounts\n");
      printf("[q] Quit\n\n");

      printf("Command: ");

      mgr_getline(in);

      printf("\n");

      switch(in[0]) {
         case 'o': /*open user or group*/
         case 'O':
            printf("Enter RID or Name: ");
            rid_type = rid_or_name(hnd, mem_ctx, dom_hnd, &rid, &name);

            if(rid_type == CAC_USER_RID) {
               ZERO_STRUCT(openu);
               openu.in.dom_hnd = dom_hnd;
               openu.in.rid     = rid;
               openu.in.access  = MAXIMUM_ALLOWED_ACCESS;

               if(!cac_SamOpenUser(hnd, mem_ctx, &openu))
                  printerr("Could not open user.", hnd->status);
               else {
                  user_menu(hnd, mem_ctx, dom_hnd, openu.out.user_hnd);

                  if(!cac_SamFlush(hnd, mem_ctx, &flush)) {
                     printerr("Lost handle while flushing SAM.", hnd->status);
                     /*we want to quit*/
                     in[0] = 'q';
                  }
               }
            }
            else if(rid_type == CAC_GROUP_RID) {
               ZERO_STRUCT(openg);
               openg.in.dom_hnd = dom_hnd;
               openg.in.rid     = rid;
               openg.in.access  = MAXIMUM_ALLOWED_ACCESS;

               if(!cac_SamOpenGroup(hnd, mem_ctx, &openg))
                  printerr("Could not open group.", hnd->status);
               else {
                  group_menu(hnd, mem_ctx, dom_hnd, openg.out.group_hnd);

                  if(!cac_SamFlush(hnd, mem_ctx, &flush)) {
                     printerr("Lost handle while flushing SAM.", hnd->status);
                     /*we want to quit*/
                     in[0] = 'q';
                  }
               }
            }
            else {
               printf("Unknown RID/Name.\n");
            }
               
            break;

         case 'c': /*create account/group*/
         case 'C':
            create_menu(hnd, mem_ctx, dom_hnd);
            if(!cac_SamFlush(hnd, mem_ctx, &flush)) {
               printerr("Lost handle while flushing SAM.", hnd->status);
               /*we want to quit*/
               in[0] = 'q';
            }
            break;

         case 'u': /*list users*/
         case 'U':
            ZERO_STRUCT(enumu);
            enumu.in.dom_hnd = dom_hnd;
            enumu.in.acb_mask = ACB_NORMAL;

            printf("Users:\n");
            while(cac_SamEnumUsers(hnd, mem_ctx, &enumu)) {
               print_rid_list(enumu.out.rids, enumu.out.names, enumu.out.num_users);
            }
            if(CAC_OP_FAILED(hnd->status))
               printerr("Error occured while enumerating users.", hnd->status);
            break;

         case 'g': /*list groups*/
         case 'G':
            ZERO_STRUCT(enumg);
            enumg.in.dom_hnd = dom_hnd;

            while(cac_SamEnumGroups(hnd, mem_ctx, &enumg)) {
               print_rid_list( enumg.out.rids, enumg.out.names, enumg.out.num_groups);
            }

            if(CAC_OP_FAILED(hnd->status))
               printerr("Error occured while enumerating groups.", hnd->status);
            break;

         case 'm': /*list machine accounts*/
         case 'M':
            ZERO_STRUCT(enumu);
            enumu.in.dom_hnd = dom_hnd;
            enumu.in.acb_mask = ACB_WSTRUST;

            printf("Users:\n");
            while(cac_SamEnumUsers(hnd, mem_ctx, &enumu)) {
               print_rid_list( enumu.out.rids, enumu.out.names, enumu.out.num_users);
            }
            if(CAC_OP_FAILED(hnd->status))
               printerr("Error occured while enumerating accounts.", hnd->status);
            break;

         case 'q': /*quit*/
         case 'Q':
            /*just do nothing*/
            break;

         default:
            printf("Invalid Command.\n");
      }
   }
}

int main(int argc, char **argv) {
   CacServerHandle *hnd = NULL;
   TALLOC_CTX *mem_ctx  = NULL;

   struct SamOpenDomain sod;

   mem_ctx = talloc_init("cacusermgr");
   if(!mem_ctx) {
      printf("Could not initialize Talloc Context\n");
      exit(-1);
   }

   /**first initialize the server handle with what we have*/
   hnd = cac_NewServerHandle(True);
   if(!hnd) {
      printf("Could not create server handle\n");
      exit(-1);
   }

   /*fill in the blanks*/
   if(!process_cmd_line(hnd, mem_ctx, argc, argv))
      usage();

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

   /*open the domain sam*/
   ZERO_STRUCT(sod);
   sod.in.access = MAXIMUM_ALLOWED_ACCESS;

   if(!cac_SamOpenDomain(hnd, mem_ctx, &sod)) {
      printf("Could not open handle to domain SAM. %s\n", nt_errstr(hnd->status));
      goto cleanup;
   }

   main_menu(hnd, mem_ctx, sod.out.dom_hnd);

cleanup:

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

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

   cac_FreeHandle(hnd);

   talloc_destroy(mem_ctx);

   return 0;
}