r1478: Useful patch from Tom Alsberg <alsbergt@cs.huji.ac.il>, to export a single...
[ira/wip.git] / source / utils / pdbedit.c
1 /* 
2    Unix SMB/CIFS implementation.
3    passdb editing frontend
4    
5    Copyright (C) Simo Sorce      2000
6    Copyright (C) Andrew Bartlett 2001   
7    Copyright (C) Jelmer Vernooij 2002
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 #define BIT_BACKEND     0x00000004
27 #define BIT_VERBOSE     0x00000008
28 #define BIT_SPSTYLE     0x00000010
29 #define BIT_RESERV_1    0x00000020
30 #define BIT_RESERV_2    0x00000040
31 #define BIT_RESERV_3    0x00000080
32 #define BIT_FULLNAME    0x00000100
33 #define BIT_HOMEDIR     0x00000200
34 #define BIT_HDIRDRIVE   0x00000400
35 #define BIT_LOGSCRIPT   0x00000800
36 #define BIT_PROFILE     0x00001000
37 #define BIT_MACHINE     0x00002000
38 #define BIT_RESERV_4    0x00004000
39 #define BIT_USER        0x00008000
40 #define BIT_LIST        0x00010000
41 #define BIT_MODIFY      0x00020000
42 #define BIT_CREATE      0x00040000
43 #define BIT_DELETE      0x00080000
44 #define BIT_ACCPOLICY   0x00100000
45 #define BIT_ACCPOLVAL   0x00200000
46 #define BIT_ACCTCTRL    0x00400000
47 #define BIT_RESERV_7    0x00800000
48 #define BIT_IMPORT      0x01000000
49 #define BIT_EXPORT      0x02000000
50 #define BIT_FIX_INIT    0x04000000
51 #define BIT_BADPWRESET  0x08000000
52
53 #define MASK_ALWAYS_GOOD        0x0000001F
54 #define MASK_USER_GOOD          0x00401F00
55
56 /*********************************************************
57  Add all currently available users to another db
58  ********************************************************/
59
60 static int export_database (struct pdb_context *in, struct pdb_context
61                             *out, const char *username) {
62         SAM_ACCOUNT *user = NULL;
63
64         DEBUG(3, ("called with username=\"%s\"\n", username));
65
66         if (NT_STATUS_IS_ERR(in->pdb_setsampwent(in, 0))) {
67                 fprintf(stderr, "Can't sampwent!\n");
68                 return 1;
69         }
70
71         if (!NT_STATUS_IS_OK(pdb_init_sam(&user))) {
72                 fprintf(stderr, "Can't initialize new SAM_ACCOUNT!\n");
73                 return 1;
74         }
75
76         while (NT_STATUS_IS_OK(in->pdb_getsampwent(in, user))) {
77                 DEBUG(4, ("Processing account %s\n",
78                           user->private.username));
79                 if (!username || 
80                     (strcmp(username, user->private.username)
81                      == 0)) {
82                         out->pdb_add_sam_account(out, user);
83                         if (!NT_STATUS_IS_OK(pdb_reset_sam(user))) {
84                                 fprintf(stderr,
85                                         "Can't reset SAM_ACCOUNT!\n");
86                                 return 1;
87                         }
88                 }
89         }
90
91         in->pdb_endsampwent(in);
92
93         return 0;
94 }
95
96 /*********************************************************
97  Add all currently available group mappings to another db
98  ********************************************************/
99
100 static int export_groups (struct pdb_context *in, struct pdb_context *out) {
101         GROUP_MAP *maps = NULL;
102         int i, entries = 0;
103
104         if (NT_STATUS_IS_ERR(in->pdb_enum_group_mapping(in, SID_NAME_UNKNOWN,
105                                                         &maps, &entries,
106                                                         False))) {
107                 fprintf(stderr, "Can't get group mappings!\n");
108                 return 1;
109         }
110
111         for (i=0; i<entries; i++) {
112                 out->pdb_add_group_mapping_entry(out, &(maps[i]));
113         }
114
115         SAFE_FREE(maps);
116
117         return 0;
118 }
119
120 /*********************************************************
121  Print info from sam structure
122 **********************************************************/
123
124 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
125 {
126         uid_t uid;
127         time_t tmp;
128
129         /* TODO: chaeck if entry is a user or a workstation */
130         if (!sam_pwent) return -1;
131         
132         if (verbosity) {
133                 printf ("Unix username:        %s\n", pdb_get_username(sam_pwent));
134                 printf ("NT username:          %s\n", pdb_get_nt_username(sam_pwent));
135                 printf ("Account Flags:        %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
136                 printf ("User SID:             %s\n",
137                         sid_string_static(pdb_get_user_sid(sam_pwent)));
138                 printf ("Primary Group SID:    %s\n",
139                         sid_string_static(pdb_get_group_sid(sam_pwent)));
140                 printf ("Full Name:            %s\n", pdb_get_fullname(sam_pwent));
141                 printf ("Home Directory:       %s\n", pdb_get_homedir(sam_pwent));
142                 printf ("HomeDir Drive:        %s\n", pdb_get_dir_drive(sam_pwent));
143                 printf ("Logon Script:         %s\n", pdb_get_logon_script(sam_pwent));
144                 printf ("Profile Path:         %s\n", pdb_get_profile_path(sam_pwent));
145                 printf ("Domain:               %s\n", pdb_get_domain(sam_pwent));
146                 printf ("Account desc:         %s\n", pdb_get_acct_desc(sam_pwent));
147                 printf ("Workstations:         %s\n", pdb_get_workstations(sam_pwent));
148                 printf ("Munged dial:          %s\n", pdb_get_munged_dial(sam_pwent));
149                 
150                 tmp = pdb_get_logon_time(sam_pwent);
151                 printf ("Logon time:           %s\n", tmp ? http_timestring(tmp) : "0");
152                 
153                 tmp = pdb_get_logoff_time(sam_pwent);
154                 printf ("Logoff time:          %s\n", tmp ? http_timestring(tmp) : "0");
155                 
156                 tmp = pdb_get_kickoff_time(sam_pwent);
157                 printf ("Kickoff time:         %s\n", tmp ? http_timestring(tmp) : "0");
158                 
159                 tmp = pdb_get_pass_last_set_time(sam_pwent);
160                 printf ("Password last set:    %s\n", tmp ? http_timestring(tmp) : "0");
161                 
162                 tmp = pdb_get_pass_can_change_time(sam_pwent);
163                 printf ("Password can change:  %s\n", tmp ? http_timestring(tmp) : "0");
164                 
165                 tmp = pdb_get_pass_must_change_time(sam_pwent);
166                 printf ("Password must change: %s\n", tmp ? http_timestring(tmp) : "0");
167
168                 tmp = pdb_get_bad_password_time(sam_pwent);
169                 printf ("Last bad password   : %s\n", tmp ? http_timestring(tmp) : "0");
170                 printf ("Bad password count  : %d\n", 
171                         pdb_get_bad_password_count(sam_pwent));
172                 
173         } else if (smbpwdstyle) {
174                 char lm_passwd[33];
175                 char nt_passwd[33];
176
177                 uid = nametouid(pdb_get_username(sam_pwent));
178                 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
179                 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
180                         
181                 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
182                        pdb_get_username(sam_pwent),
183                        (unsigned long)uid,
184                        lm_passwd,
185                        nt_passwd,
186                        pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
187                        (uint32)pdb_get_pass_last_set_time(sam_pwent));
188         } else {
189                 uid = nametouid(pdb_get_username(sam_pwent));
190                 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid, 
191                         pdb_get_fullname(sam_pwent));
192         }
193
194         return 0;       
195 }
196
197 /*********************************************************
198  Get an Print User Info
199 **********************************************************/
200
201 static int print_user_info (struct pdb_context *in, const char *username, BOOL verbosity, BOOL smbpwdstyle)
202 {
203         SAM_ACCOUNT *sam_pwent=NULL;
204         BOOL ret;
205         BOOL updated_autolock = False, updated_badpw = False;
206
207         if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
208                 return -1;
209         }
210
211         ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
212
213         if (ret==False) {
214                 fprintf (stderr, "Username not found!\n");
215                 pdb_free_sam(&sam_pwent);
216                 return -1;
217         }
218
219         if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock))
220                 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
221
222         if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw))
223                 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
224
225         if (updated_autolock || updated_badpw) {
226                 become_root();
227                 if(!pdb_update_sam_account(sam_pwent))
228                         DEBUG(1, ("Failed to modify entry.\n"));
229                 unbecome_root();
230         }
231
232         ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
233         pdb_free_sam(&sam_pwent);
234         
235         return ret;
236 }
237         
238 /*********************************************************
239  List Users
240 **********************************************************/
241 static int print_users_list (struct pdb_context *in, BOOL verbosity, BOOL smbpwdstyle)
242 {
243         SAM_ACCOUNT *sam_pwent=NULL;
244         BOOL check, ret;
245         
246         check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False));
247         if (!check) {
248                 return 1;
249         }
250
251         check = True;
252         if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
253
254         while (check && (ret = NT_STATUS_IS_OK(in->pdb_getsampwent (in, sam_pwent)))) {
255                 if (verbosity)
256                         printf ("---------------\n");
257                 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
258                 pdb_free_sam(&sam_pwent);
259                 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
260         }
261         if (check) pdb_free_sam(&sam_pwent);
262         
263         in->pdb_endsampwent(in);
264         return 0;
265 }
266
267 /*********************************************************
268  Fix a list of Users for uninitialised passwords
269 **********************************************************/
270 static int fix_users_list (struct pdb_context *in)
271 {
272         SAM_ACCOUNT *sam_pwent=NULL;
273         BOOL check, ret;
274         
275         check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False));
276         if (!check) {
277                 return 1;
278         }
279
280         check = True;
281         if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
282
283         while (check && (ret = NT_STATUS_IS_OK(in->pdb_getsampwent (in, sam_pwent)))) {
284                 printf("Updating record for user %s\n", pdb_get_username(sam_pwent));
285         
286                 if (!pdb_update_sam_account(sam_pwent)) {
287                         printf("Update of user %s failed!\n", pdb_get_username(sam_pwent));
288                 }
289                 pdb_free_sam(&sam_pwent);
290                 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
291                 if (!check) {
292                         fprintf(stderr, "Failed to initialise new SAM_ACCOUNT structure (out of memory?)\n");
293                 }
294                         
295         }
296         if (check) pdb_free_sam(&sam_pwent);
297         
298         in->pdb_endsampwent(in);
299         return 0;
300 }
301
302 /*********************************************************
303  Set User Info
304 **********************************************************/
305
306 static int set_user_info (struct pdb_context *in, const char *username, 
307                           const char *fullname, const char *homedir, 
308                           const char *drive, const char *script, 
309                           const char *profile, const char *account_control,
310                           const char *user_sid, const char *group_sid,
311                           const BOOL badpw)
312 {
313         SAM_ACCOUNT *sam_pwent=NULL;
314         BOOL ret;
315         
316         pdb_init_sam(&sam_pwent);
317         
318         ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
319         if (ret==False) {
320                 fprintf (stderr, "Username not found!\n");
321                 pdb_free_sam(&sam_pwent);
322                 return -1;
323         }
324         
325         if (fullname)
326                 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
327         if (homedir)
328                 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
329         if (drive)
330                 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
331         if (script)
332                 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
333         if (profile)
334                 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
335
336         if (account_control) {
337                 uint16 not_settable = ~(ACB_DISABLED|ACB_HOMDIRREQ|ACB_PWNOTREQ|
338                                         ACB_PWNOEXP|ACB_AUTOLOCK);
339
340                 uint16 newflag = pdb_decode_acct_ctrl(account_control);
341
342                 if (newflag & not_settable) {
343                         fprintf(stderr, "Can only set [NDHLX] flags\n");
344                         pdb_free_sam(&sam_pwent);
345                         return -1;
346                 }
347
348                 pdb_set_acct_ctrl(sam_pwent,
349                                   (pdb_get_acct_ctrl(sam_pwent) & not_settable) | newflag,
350                                   PDB_CHANGED);
351         }
352         if (user_sid) {
353                 DOM_SID u_sid;
354                 if (!string_to_sid(&u_sid, user_sid)) {
355                         /* not a complete sid, may be a RID, try building a SID */
356                         int u_rid;
357                         
358                         if (sscanf(user_sid, "%d", &u_rid) != 1) {
359                                 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
360                                 return -1;
361                         }
362                         sid_copy(&u_sid, get_global_sam_sid());
363                         sid_append_rid(&u_sid, u_rid);
364                 }
365                 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
366         }
367         if (group_sid) {
368                 DOM_SID g_sid;
369                 if (!string_to_sid(&g_sid, group_sid)) {
370                         /* not a complete sid, may be a RID, try building a SID */
371                         int g_rid;
372                         
373                         if (sscanf(group_sid, "%d", &g_rid) != 1) {
374                                 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
375                                 return -1;
376                         }
377                         sid_copy(&g_sid, get_global_sam_sid());
378                         sid_append_rid(&g_sid, g_rid);
379                 }
380                 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
381         }
382
383         if (badpw) {
384                 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
385                 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
386         }
387         
388         if (NT_STATUS_IS_OK(in->pdb_update_sam_account (in, sam_pwent)))
389                 print_user_info (in, username, True, False);
390         else {
391                 fprintf (stderr, "Unable to modify entry!\n");
392                 pdb_free_sam(&sam_pwent);
393                 return -1;
394         }
395         pdb_free_sam(&sam_pwent);
396         return 0;
397 }
398
399 /*********************************************************
400  Add New User
401 **********************************************************/
402 static int new_user (struct pdb_context *in, const char *username,
403                         const char *fullname, const char *homedir,
404                         const char *drive, const char *script,
405                         const char *profile, char *user_sid, char *group_sid)
406 {
407         SAM_ACCOUNT *sam_pwent=NULL;
408         NTSTATUS nt_status;
409         char *password1, *password2, *staticpass;
410         
411         get_global_sam_sid();
412
413         if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_new(&sam_pwent, username, 0))) {
414                 DEBUG(0, ("could not create account to add new user %s\n", username));
415                 return -1;
416         }
417
418         staticpass = getpass("new password:");
419         password1 = strdup(staticpass);
420         memset(staticpass, 0, strlen(staticpass));
421         staticpass = getpass("retype new password:");
422         password2 = strdup(staticpass);
423         memset(staticpass, 0, strlen(staticpass));
424         if (strcmp (password1, password2)) {
425                 fprintf (stderr, "Passwords does not match!\n");
426                 memset(password1, 0, strlen(password1));
427                 SAFE_FREE(password1);
428                 memset(password2, 0, strlen(password2));
429                 SAFE_FREE(password2);
430                 pdb_free_sam (&sam_pwent);
431                 return -1;
432         }
433
434         pdb_set_plaintext_passwd(sam_pwent, password1);
435         memset(password1, 0, strlen(password1));
436         SAFE_FREE(password1);
437         memset(password2, 0, strlen(password2));
438         SAFE_FREE(password2);
439
440         if (fullname)
441                 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
442         if (homedir)
443                 pdb_set_homedir (sam_pwent, homedir, PDB_CHANGED);
444         if (drive)
445                 pdb_set_dir_drive (sam_pwent, drive, PDB_CHANGED);
446         if (script)
447                 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
448         if (profile)
449                 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
450         if (user_sid) {
451                 DOM_SID u_sid;
452                 if (!string_to_sid(&u_sid, user_sid)) {
453                         /* not a complete sid, may be a RID, try building a SID */
454                         int u_rid;
455                         
456                         if (sscanf(user_sid, "%d", &u_rid) != 1) {
457                                 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
458                                 return -1;
459                         }
460                         sid_copy(&u_sid, get_global_sam_sid());
461                         sid_append_rid(&u_sid, u_rid);
462                 }
463                 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
464         }
465         if (group_sid) {
466                 DOM_SID g_sid;
467                 if (!string_to_sid(&g_sid, group_sid)) {
468                         /* not a complete sid, may be a RID, try building a SID */
469                         int g_rid;
470                         
471                         if (sscanf(group_sid, "%d", &g_rid) != 1) {
472                                 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
473                                 return -1;
474                         }
475                         sid_copy(&g_sid, get_global_sam_sid());
476                         sid_append_rid(&g_sid, g_rid);
477                 }
478                 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
479         }
480         
481         pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL, PDB_CHANGED);
482         
483         if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) { 
484                 print_user_info (in, username, True, False);
485         } else {
486                 fprintf (stderr, "Unable to add user! (does it already exist?)\n");
487                 pdb_free_sam (&sam_pwent);
488                 return -1;
489         }
490         pdb_free_sam (&sam_pwent);
491         return 0;
492 }
493
494 /*********************************************************
495  Add New Machine
496 **********************************************************/
497
498 static int new_machine (struct pdb_context *in, const char *machine_in)
499 {
500         SAM_ACCOUNT *sam_pwent=NULL;
501         fstring machinename;
502         fstring machineaccount;
503         struct passwd  *pwd = NULL;
504         
505         get_global_sam_sid();
506
507         fstrcpy(machinename, machine_in); 
508         machinename[15]= '\0';
509
510         if (machinename[strlen (machinename) -1] == '$')
511                 machinename[strlen (machinename) -1] = '\0';
512         
513         strlower_m(machinename);
514         
515         fstrcpy(machineaccount, machinename);
516         fstrcat(machineaccount, "$");
517
518         if ((pwd = getpwnam_alloc(machineaccount))) {
519                 if (!NT_STATUS_IS_OK(pdb_init_sam_pw( &sam_pwent, pwd))) {
520                         fprintf(stderr, "Could not init sam from pw\n");
521                         passwd_free(&pwd);
522                         return -1;
523                 }
524                 passwd_free(&pwd);
525         } else {
526                 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
527                         fprintf(stderr, "Could not init sam from pw\n");
528                         return -1;
529                 }
530         }
531
532         pdb_set_plaintext_passwd (sam_pwent, machinename);
533
534         pdb_set_username (sam_pwent, machineaccount, PDB_CHANGED);
535         
536         pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST, PDB_CHANGED);
537         
538         pdb_set_group_sid_from_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS, PDB_CHANGED);
539         
540         if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) {
541                 print_user_info (in, machineaccount, True, False);
542         } else {
543                 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
544                 pdb_free_sam (&sam_pwent);
545                 return -1;
546         }
547         pdb_free_sam (&sam_pwent);
548         return 0;
549 }
550
551 /*********************************************************
552  Delete user entry
553 **********************************************************/
554
555 static int delete_user_entry (struct pdb_context *in, const char *username)
556 {
557         SAM_ACCOUNT *samaccount = NULL;
558
559         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
560                 return -1;
561         }
562
563         if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, username))) {
564                 fprintf (stderr, "user %s does not exist in the passdb\n", username);
565                 return -1;
566         }
567
568         if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) {
569                 fprintf (stderr, "Unable to delete user %s\n", username);
570                 return -1;
571         }
572         return 0;
573 }
574
575 /*********************************************************
576  Delete machine entry
577 **********************************************************/
578
579 static int delete_machine_entry (struct pdb_context *in, const char *machinename)
580 {
581         fstring name;
582         SAM_ACCOUNT *samaccount = NULL;
583         
584         fstrcpy(name, machinename);
585         name[15] = '\0';
586         if (name[strlen(name)-1] != '$')
587                 fstrcat (name, "$");
588
589         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
590                 return -1;
591         }
592
593         if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, name))) {
594                 fprintf (stderr, "machine %s does not exist in the passdb\n", name);
595                 return -1;
596         }
597
598         if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) {
599                 fprintf (stderr, "Unable to delete machine %s\n", name);
600                 return -1;
601         }
602
603         return 0;
604 }
605
606 /*********************************************************
607  Start here.
608 **********************************************************/
609
610 int main (int argc, char **argv)
611 {
612         static BOOL list_users = False;
613         static BOOL verbose = False;
614         static BOOL spstyle = False;
615         static BOOL machine = False;
616         static BOOL add_user = False;
617         static BOOL delete_user = False;
618         static BOOL modify_user = False;
619         uint32  setparms, checkparms;
620         int opt;
621         static char *full_name = NULL;
622         static const char *user_name = NULL;
623         static char *home_dir = NULL;
624         static char *home_drive = NULL;
625         static char *backend = NULL;
626         static char *backend_in = NULL;
627         static char *backend_out = NULL;
628         static BOOL transfer_groups = False;
629         static BOOL  force_initialised_password = False;
630         static char *logon_script = NULL;
631         static char *profile_path = NULL;
632         static char *account_control = NULL;
633         static char *account_policy = NULL;
634         static char *user_sid = NULL;
635         static char *group_sid = NULL;
636         static long int account_policy_value = 0;
637         BOOL account_policy_value_set = False;
638         static BOOL badpw_reset = False;
639
640         struct pdb_context *bin;
641         struct pdb_context *bout;
642         struct pdb_context *bdef;
643         poptContext pc;
644         struct poptOption long_options[] = {
645                 POPT_AUTOHELP
646                 {"list",        'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
647                 {"verbose",     'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
648                 {"smbpasswd-style",     'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
649                 {"user",        'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
650                 {"fullname",    'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
651                 {"homedir",     'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
652                 {"drive",       'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
653                 {"script",      'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
654                 {"profile",     'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
655                 {"user SID",    'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
656                 {"group SID",   'G', POPT_ARG_STRING, &group_sid, 0, "set group SID or RID", NULL},
657                 {"create",      'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
658                 {"modify",      'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
659                 {"machine",     'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
660                 {"delete",      'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
661                 {"backend",     'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
662                 {"import",      'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
663                 {"export",      'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
664                 {"group",       'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
665                 {"account-policy",      'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
666                 {"value",       'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
667                 {"account-control",     'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
668                 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
669                 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
670                 POPT_COMMON_SAMBA
671                 POPT_TABLEEND
672         };
673         
674         setup_logging("pdbedit", True);
675         
676         pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
677                             POPT_CONTEXT_KEEP_FIRST);
678         
679         while((opt = poptGetNextOpt(pc)) != -1) {
680                 switch (opt) {
681                 case 'C':
682                         account_policy_value_set = True;
683                         break;
684                 }
685         }
686
687         poptGetArg(pc); /* Drop argv[0], the program name */
688
689         if (user_name == NULL)
690                 user_name = poptGetArg(pc);
691
692         if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
693                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
694                 exit(1);
695         }
696
697         if(!initialize_password_db(False))
698                 exit(1);
699
700         if (!init_names())
701                 exit(1);
702
703         setparms =      (backend ? BIT_BACKEND : 0) +
704                         (verbose ? BIT_VERBOSE : 0) +
705                         (spstyle ? BIT_SPSTYLE : 0) +
706                         (full_name ? BIT_FULLNAME : 0) +
707                         (home_dir ? BIT_HOMEDIR : 0) +
708                         (home_drive ? BIT_HDIRDRIVE : 0) +
709                         (logon_script ? BIT_LOGSCRIPT : 0) +
710                         (profile_path ? BIT_PROFILE : 0) +
711                         (machine ? BIT_MACHINE : 0) +
712                         (user_name ? BIT_USER : 0) +
713                         (list_users ? BIT_LIST : 0) +
714                         (force_initialised_password ? BIT_FIX_INIT : 0) +
715                         (modify_user ? BIT_MODIFY : 0) +
716                         (add_user ? BIT_CREATE : 0) +
717                         (delete_user ? BIT_DELETE : 0) +
718                         (account_control ? BIT_ACCTCTRL : 0) +
719                         (account_policy ? BIT_ACCPOLICY : 0) +
720                         (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
721                         (backend_in ? BIT_IMPORT : 0) +
722                         (backend_out ? BIT_EXPORT : 0) +
723                         (badpw_reset ? BIT_BADPWRESET : 0);
724
725         if (setparms & BIT_BACKEND) {
726                 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bdef, backend))) {
727                         fprintf(stderr, "Can't initialize passdb backend.\n");
728                         return 1;
729                 }
730         } else {
731                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&bdef, lp_passdb_backend()))) {
732                         fprintf(stderr, "Can't initialize passdb backend.\n");
733                         return 1;
734                 }
735         }
736         
737         /* the lowest bit options are always accepted */
738         checkparms = setparms & ~MASK_ALWAYS_GOOD;
739
740         if (checkparms & BIT_FIX_INIT) {
741                 return fix_users_list(bdef);
742         }
743
744         /* account policy operations */
745         if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
746                 uint32 value;
747                 int field = account_policy_name_to_fieldnum(account_policy);
748                 if (field == 0) {
749                         char *apn = account_policy_names_list();
750                         fprintf(stderr, "No account policy by that name\n");
751                         if (apn) {
752                                 fprintf(stderr, "Account policy names are :\n%s\n", apn);
753                         }
754                         SAFE_FREE(apn);
755                         exit(1);
756                 }
757                 if (!account_policy_get(field, &value)) {
758                         fprintf(stderr, "valid account policy, but unable to fetch value!\n");
759                         exit(1);
760                 }
761                 if (account_policy_value_set) {
762                         printf("account policy value for %s was %u\n", account_policy, value);
763                         if (!account_policy_set(field, account_policy_value)) {
764                                 fprintf(stderr, "valid account policy, but unable to set value!\n");
765                                 exit(1);
766                         }
767                         printf("account policy value for %s is now %lu\n", account_policy, account_policy_value);
768                         exit(0);
769                 } else {
770                         printf("account policy value for %s is %u\n", account_policy, value);
771                         exit(0);
772                 }
773         }
774
775         /* import and export operations */
776         if (((checkparms & BIT_IMPORT) || (checkparms & BIT_EXPORT))
777             && !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
778                 if (backend_in) {
779                         if (!NT_STATUS_IS_OK(make_pdb_context_string(&bin, backend_in))) {
780                                 fprintf(stderr, "Can't initialize passdb backend.\n");
781                                 return 1;
782                         }
783                 } else {
784                         bin = bdef;
785                 }
786                 if (backend_out) {
787                         if (!NT_STATUS_IS_OK(make_pdb_context_string(&bout, backend_out))) {
788                                 fprintf(stderr, "Can't initialize %s.\n", backend_out);
789                                 return 1;
790                         }
791                 } else {
792                         bout = bdef;
793                 }
794                 if (transfer_groups) {
795                         if (!(checkparms & BIT_USER))
796                                 return export_groups(bin, bout);
797                 } else {
798                         if (checkparms & BIT_USER)
799                                 return export_database(bin, bout,
800                                                        user_name);
801                         else
802                                 return export_database(bin, bout,
803                                                        NULL);
804                 }
805         }
806
807         /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
808         /* fake up BIT_LIST if only BIT_USER is defined */
809         if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
810                 checkparms += BIT_LIST;
811         }
812         
813         /* modify flag is optional to maintain backwards compatibility */
814         /* fake up BIT_MODIFY if BIT_USER  and at least one of MASK_USER_GOOD is defined */
815         if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
816                 checkparms += BIT_MODIFY;
817         }
818
819         /* list users operations */
820         if (checkparms & BIT_LIST) {
821                 if (!(checkparms & ~BIT_LIST)) {
822                         return print_users_list (bdef, verbose, spstyle);
823                 }
824                 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
825                         return print_user_info (bdef, user_name, verbose, spstyle);
826                 }
827         }
828         
829         /* mask out users options */
830         checkparms &= ~MASK_USER_GOOD;
831
832         /* if bad password count is reset, we must be modifying */
833         if (checkparms & BIT_BADPWRESET) {
834                 checkparms |= BIT_MODIFY;
835                 checkparms &= ~BIT_BADPWRESET;
836         }
837         
838         /* account operation */
839         if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
840                 /* check use of -u option */
841                 if (!(checkparms & BIT_USER)) {
842                         fprintf (stderr, "Username not specified! (use -u option)\n");
843                         return -1;
844                 }
845
846                 /* account creation operations */
847                 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
848                         if (checkparms & BIT_MACHINE) {
849                                 return new_machine (bdef, user_name);
850                         } else {
851                                 return new_user (bdef, user_name, full_name, home_dir, 
852                                                  home_drive, logon_script, 
853                                                  profile_path, user_sid, group_sid);
854                         }
855                 }
856
857                 /* account deletion operations */
858                 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
859                         if (checkparms & BIT_MACHINE) {
860                                 return delete_machine_entry (bdef, user_name);
861                         } else {
862                                 return delete_user_entry (bdef, user_name);
863                         }
864                 }
865
866                 /* account modification operations */
867                 if (!(checkparms & ~(BIT_MODIFY + BIT_USER))) {
868                         return set_user_info (bdef, user_name, full_name,
869                                               home_dir,
870                                               home_drive,
871                                               logon_script,
872                                               profile_path, account_control,
873                                               user_sid, group_sid,
874                                               badpw_reset);
875                 }
876         }
877
878         if (setparms >= 0x20) {
879                 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
880         }
881         poptPrintHelp(pc, stderr, 0);
882
883         return 1;
884 }