summaryrefslogtreecommitdiffstats
path: root/examples/libmsrpc/cacusermgr/mgr_user.c
blob: bdae55ddec38b5595b58a16b1197d3cd187fa1e9 (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
 * Unix SMB/CIFS implementation. 
 * cacusermgr user 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 3 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, see <http://www.gnu.org/licenses/>.  */

#include "cacusermgr.h"

void print_user_info(CacUserInfo *info) {
   printf("\n");
   printf(" User Name      : %s\n", info->username);
   printf(" Full Name      : %s\n", info->full_name);
   printf(" Home Dir       : %s\n", info->home_dir);
   printf(" Home Drive     : %s\n", info->home_drive);
   printf(" Profile Path   : %s\n", info->profile_path);
   printf(" Logon Script   : %s\n", info->logon_script);
   printf(" Description    : %s\n", info->description);
   printf(" Workstations   : %s\n", info->workstations);
   printf(" Remote Dial    : %s\n", info->dial);

   printf(" Logon Time     : %s\n", http_timestring(info->logon_time));
   printf(" Logoff Time    : %s\n", http_timestring(info->logoff_time));
   printf(" Kickoff Time   : %s\n", http_timestring(info->kickoff_time));
   printf(" Pass last set  : %s\n", http_timestring(info->pass_last_set_time));
   printf(" Pass can set   : %s\n", http_timestring(info->pass_can_change_time));
   printf(" Pass must set  : %s\n", http_timestring(info->pass_must_change_time));

   printf(" User RID       : 0x%x\n", info->rid);
   printf(" Group RID      : 0x%x\n", info->group_rid);
   printf(" User Type      : ");

   if(info->acb_mask & ACB_NORMAL)
      printf("Normal User\n");
   else if(info->acb_mask & ACB_TEMPDUP)
      printf("Temporary Duplicate Account\n");
   else if(info->acb_mask & ACB_DOMTRUST)
      printf("Inter-Domain Trust Account\n");
   else if(info->acb_mask & ACB_WSTRUST)
      printf("Workstation Trust Account\n");
   else if(info->acb_mask & ACB_SVRTRUST)
      printf("Server Trust Account\n");
   else
      printf("\n");

   printf(" Disabled       : %s\n", (info->acb_mask & ACB_DISABLED) ? "Yes" : "No");
   printf(" Locked         : %s\n", (info->acb_mask & ACB_AUTOLOCK) ? "Yes" : "No");
   printf(" Pass Expires   : %s\n", (info->acb_mask & ACB_PWNOEXP) ? "No" : "Yes");
   printf(" Pass Required  : %s\n", (info->acb_mask & ACB_PWNOTREQ) ? "No" : "Yes");

}

CacUserInfo *modify_user_info(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *user_hnd) {
   CacUserInfo *info = NULL;
   fstring tmp;

   struct SamGetUserInfo getinfo;
   struct SamSetUserInfo setinfo;

   ZERO_STRUCT(getinfo);
   ZERO_STRUCT(setinfo);

   getinfo.in.user_hnd = user_hnd;

   if(!cac_SamGetUserInfo(hnd, mem_ctx, &getinfo)) {
      printerr("Could not get user info.", hnd->status);
      return NULL;
   }

   info = getinfo.out.info;

   printf("\n");
   printf(" User Name [%s]: ", info->username);
   mgr_getline(tmp);
   if(tmp[0] != '\0')
      info->username = talloc_strdup(mem_ctx, tmp);

   printf(" Full Name [%s]: ", info->full_name);
   mgr_getline(tmp);
   if(tmp[0] != '\0')
      info->full_name = talloc_strdup(mem_ctx, tmp);
   
   printf(" Description  [%s]: ", info->description);
   mgr_getline(tmp);
   if(tmp[0] != '\0')
      info->description = talloc_strdup(mem_ctx, tmp);
   
   printf(" Home Dir  [%s]: ", info->home_dir);
   mgr_getline(tmp);
   if(tmp[0] != '\0')
      info->home_dir = talloc_strdup(mem_ctx, tmp);

   printf(" Home Drive [%s]: ", info->home_drive);
   mgr_getline(tmp);
   if(tmp[0] != '\0')
      info->home_drive = talloc_strdup(mem_ctx, tmp);
   
   printf(" Profile Path [%s]: ", info->profile_path);
   mgr_getline(tmp);
   if(tmp[0] != '\0')
      info->profile_path = talloc_strdup(mem_ctx, tmp);

   printf(" Logon Script [%s]: ", info->logon_script);
   mgr_getline(tmp);
   if(tmp[0] != '\0')
      info->logon_script = talloc_strdup(mem_ctx, tmp);
   
   printf(" Workstations [%s]: ", info->workstations);
   mgr_getline(tmp);
   if(tmp[0] != '\0')
      info->workstations = talloc_strdup(mem_ctx, tmp);
   
   printf(" Remote Dial [%s]: ", info->dial);
   mgr_getline(tmp);
   if(tmp[0] != '\0')
      info->dial = talloc_strdup(mem_ctx, tmp);

   printf(" Disabled [%s] (y/n): ", (info->acb_mask & ACB_DISABLED) ? "Yes" : "No");
   mgr_getline(tmp);
   if(tmp[0] == 'y' || tmp[0] == 'Y')
      info->acb_mask |= ACB_DISABLED;
   else if(tmp[0] == 'n' || tmp[0] == 'N')
      info->acb_mask ^= (info->acb_mask & ACB_DISABLED) ? ACB_DISABLED : 0x0;
      
   printf(" Pass Expires [%s] (y/n): ", (info->acb_mask & ACB_PWNOEXP) ? "No" : "Yes");
   mgr_getline(tmp);
   if(tmp[0] == 'n' || tmp[0] == 'N')
      info->acb_mask |= ACB_PWNOEXP;
   else if(tmp[0] == 'y' || tmp[0] == 'Y')
      info->acb_mask ^= (info->acb_mask & ACB_PWNOEXP) ? ACB_PWNOEXP : 0x0;

   printf(" Pass Required [%s] (y/n): ", (info->acb_mask & ACB_PWNOTREQ) ? "No" : "Yes");
   mgr_getline(tmp);
   if(tmp[0] == 'n' || tmp[0] == 'N')
      info->acb_mask |= ACB_PWNOTREQ;
   else if(tmp[0] == 'y' || tmp[0] == 'Y')
      info->acb_mask ^= (info->acb_mask & ACB_PWNOTREQ) ? ACB_PWNOTREQ : 0x0;

   setinfo.in.user_hnd = user_hnd;
   setinfo.in.info     = info;

   if(!cac_SamSetUserInfo(hnd, mem_ctx, &setinfo)) {
      printerr("Could not set user info.", hnd->status);
   }

   return info;
}

void add_user_to_group(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, CacUserInfo *info, POLICY_HND *dom_hnd) {
   int rid_type = 0;

   char *tmp = NULL;

   struct SamOpenGroup og;
   struct SamAddGroupMember add;

   ZERO_STRUCT(og);
   ZERO_STRUCT(add);
   
   printf("Group RID or Name:");

   og.in.dom_hnd = dom_hnd;
   og.in.access = MAXIMUM_ALLOWED_ACCESS;
   rid_type = rid_or_name(hnd, mem_ctx, dom_hnd, &og.in.rid, &tmp);

   if(!cac_SamOpenGroup(hnd, mem_ctx, &og)) {
      printerr("Could not open group.", hnd->status);
      return;
   }

   add.in.group_hnd = og.out.group_hnd;
   add.in.rid = info->rid;

   if(!cac_SamAddGroupMember(hnd, mem_ctx, &add)) {
      printerr("Could not add user to group.", hnd->status);
   }

   cac_SamClose(hnd, mem_ctx, og.out.group_hnd);
}

void remove_user_from_group(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, CacUserInfo *info, POLICY_HND *dom_hnd) {
   int rid_type = 0;

   char *tmp = NULL;

   struct SamOpenGroup og;
   struct SamRemoveGroupMember del;

   ZERO_STRUCT(og);
   ZERO_STRUCT(del);
   
   printf("Group RID or Name:");

   og.in.dom_hnd = dom_hnd;
   og.in.access = MAXIMUM_ALLOWED_ACCESS;
   rid_type = rid_or_name(hnd, mem_ctx, dom_hnd, &og.in.rid, &tmp);

   if(!cac_SamOpenGroup(hnd, mem_ctx, &og)) {
      printerr("Could not open group.", hnd->status);
      return;
   }

   del.in.group_hnd = og.out.group_hnd;
   del.in.rid = info->rid;

   if(!cac_SamRemoveGroupMember(hnd, mem_ctx, &del)) {
      printerr("Could not add user to group.", hnd->status);
   }

   cac_SamClose(hnd, mem_ctx, og.out.group_hnd);
}

void user_menu(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd, POLICY_HND *user_hnd) {
   fstring in;

   struct SamGetUserInfo      getinfo;
   struct SamSetPassword      setpass;
   struct SamGetGroupsForUser groups;
   struct SamGetNamesFromRids gnfr;

   CacUserInfo *info = NULL;

   if(!hnd || !mem_ctx || !user_hnd) {
      printf("Must open user.\n");
      return;
   }

   /*get the userinfo and print it out*/
   ZERO_STRUCT(getinfo);
   getinfo.in.user_hnd = user_hnd;

   if(!cac_SamGetUserInfo(hnd, mem_ctx, &getinfo)) {
      printerr("Could not get info.", hnd->status);
      info = NULL;
   }
   else {
      info = getinfo.out.info;
      print_user_info(info);
   }

   /*now deal with the menu*/
   in[0] = '\0';
   while(in[0] != 'b' && in[0] != 'B' && in[0] != 'q' && in[0] != 'Q') {
      printf("\n");
      printf("[s] Set Password\n");

      if(info && (info->acb_mask & ACB_DISABLED))
         printf("[e] Enable User\n");
      else if(info)
         printf("[d] Disable User\n");

      printf("[v] View User Info\n");
      printf("[m] Modify User Info\n");
      printf("[x] Delete User\n\n");

      printf("[g] List Group Membership\n");
      printf("[a] Add User To Group\n");
      printf("[l] List Domain Groups\n");
      printf("[r] Remove User From Group\n\n");

      printf("[b] Back\n\n");

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

      printf("\n");

      switch(in[0]) {
         case 'g': /*list group membership*/
         case 'G': 
            ZERO_STRUCT(groups);
            groups.in.user_hnd = user_hnd;

            if(!cac_SamGetGroupsForUser(hnd, mem_ctx, &groups)) {
               printerr("Could not get groups.", hnd->status);
               break;
            }

            ZERO_STRUCT(gnfr);
            gnfr.in.dom_hnd = dom_hnd;
            gnfr.in.rids = groups.out.rids;
            gnfr.in.num_rids = groups.out.num_groups;

            if(!cac_SamGetNamesFromRids(hnd, mem_ctx, &gnfr)) {
               printerr("Could not map RIDs to names.", hnd->status);
               break;
            }

            print_lookup_records(gnfr.out.map, gnfr.out.num_names);

            break;
         case 's': /*reset password*/
         case 'S':
            ZERO_STRUCT(setpass);
            setpass.in.user_hnd = user_hnd;
            setpass.in.password = get_new_password(mem_ctx);
            
            if(!setpass.in.password) {
               printf("Out of memory.\n");
               break;
            }

            if(!cac_SamSetPassword(hnd, mem_ctx, &setpass)) {
               printerr("Could not set password.", hnd->status);
            }
            else {
               printf("Reset password.\n");
            }
            break;

         case 'e': /*enable user*/
         case 'E': 
            if(info && !(info->acb_mask & ACB_DISABLED))
               break;

            if(!cac_SamEnableUser(hnd, mem_ctx, user_hnd)) {
               printerr("Could not enable user.", hnd->status);
            }
            else {
               printf("Enabled User.\n");
               /*toggle the disabled ACB bit in our local copy of the info*/
               info->acb_mask ^= ACB_DISABLED;
            }
            break;

         case 'd': /*disable user*/
         case 'D':
            if(info && (info->acb_mask & ACB_DISABLED))
               break;

            if(!cac_SamDisableUser(hnd, mem_ctx, user_hnd)) {
               printerr("Could not disable user.", hnd->status);
            }
            else {
               printf("Disabled User.\n");
               /*toggle the disabled ACB bit in our local copy of the info*/
               info->acb_mask ^= ACB_DISABLED;
            }
            break;

         case 'v': /*view user info*/
         case 'V':
            ZERO_STRUCT(getinfo);
            getinfo.in.user_hnd = user_hnd;

            if(!cac_SamGetUserInfo(hnd, mem_ctx, &getinfo)) {
               printerr("Could not get info.", hnd->status);
               info = NULL;
            }
            else {
               info = getinfo.out.info;
               print_user_info(info);
            }

            break;

         case 'm': /*modify user info*/
         case 'M':
            info = modify_user_info(hnd, mem_ctx, user_hnd);

            if(info)
               printf("Updated user info.\n");
            break;

         case 'l': /*list domain groups*/
         case 'L':
            list_groups(hnd, mem_ctx, dom_hnd);
            break;

         case 'a': /*add user to group*/
         case 'A':
            add_user_to_group(hnd, mem_ctx, info, dom_hnd);
            break;

         case 'r': /*remove user from group*/
         case 'R':
            remove_user_from_group(hnd, mem_ctx, info, dom_hnd);
            break;
            
         case 'x': /*delete user*/
         case 'X':
            if(!cac_SamDeleteUser(hnd, mem_ctx, user_hnd))
               printerr("Could not delete user.", hnd->status);

            /*we want to go back to the main menu*/
            in[0] = 'b';
            break;

         case 'b': /*back*/
         case 'B':
         case 'q':
         case 'Q':
            /*do nothing*/
            break;
            
         default:
            printf("Invalid command.\n");
      }
   }

   /*close the user before returning*/
   cac_SamClose(hnd, mem_ctx, user_hnd);
}