r13517: Fix typo -- thanks to Karolin Seeger <ks@sernet.de>
[samba.git] / source3 / 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_CAN_CHANGE  0x00000020
30 #define BIT_MUST_CHANGE 0x00000040
31 #define BIT_USERSIDS    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_USERDOMAIN  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 #define BIT_LOGONHOURS  0x10000000
53
54 #define MASK_ALWAYS_GOOD        0x0000001F
55 #define MASK_USER_GOOD          0x00405FE0
56
57 /*********************************************************
58  Reset account policies to their default values and remove marker
59  ********************************************************/
60
61 static int reinit_account_policies (void) 
62 {
63         int i;
64
65         for (i=1; decode_account_policy_name(i) != NULL; i++) {
66                 uint32 policy_value;
67                 if (!account_policy_get_default(i, &policy_value)) {
68                         fprintf(stderr, "Can't get default account policy\n");
69                         return -1;
70                 }
71                 if (!account_policy_set(i, policy_value)) {
72                         fprintf(stderr, "Can't set account policy in tdb\n");
73                         return -1;
74                 }
75         }
76
77         if (!remove_account_policy_migrated()) {
78                 fprintf(stderr, "Can't remove marker from tdb\n");
79                 return -1;
80         }
81
82         return 0;
83 }
84
85 /*********************************************************
86  Print info from sam structure
87 **********************************************************/
88
89 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
90 {
91         uid_t uid;
92         time_t tmp;
93
94         /* TODO: chaeck if entry is a user or a workstation */
95         if (!sam_pwent) return -1;
96         
97         if (verbosity) {
98                 pstring temp;
99                 const uint8 *hours;
100                 
101                 printf ("Unix username:        %s\n", pdb_get_username(sam_pwent));
102                 printf ("NT username:          %s\n", pdb_get_nt_username(sam_pwent));
103                 printf ("Account Flags:        %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
104                 printf ("User SID:             %s\n",
105                         sid_string_static(pdb_get_user_sid(sam_pwent)));
106                 printf ("Primary Group SID:    %s\n",
107                         sid_string_static(pdb_get_group_sid(sam_pwent)));
108                 printf ("Full Name:            %s\n", pdb_get_fullname(sam_pwent));
109                 printf ("Home Directory:       %s\n", pdb_get_homedir(sam_pwent));
110                 printf ("HomeDir Drive:        %s\n", pdb_get_dir_drive(sam_pwent));
111                 printf ("Logon Script:         %s\n", pdb_get_logon_script(sam_pwent));
112                 printf ("Profile Path:         %s\n", pdb_get_profile_path(sam_pwent));
113                 printf ("Domain:               %s\n", pdb_get_domain(sam_pwent));
114                 printf ("Account desc:         %s\n", pdb_get_acct_desc(sam_pwent));
115                 printf ("Workstations:         %s\n", pdb_get_workstations(sam_pwent));
116                 printf ("Munged dial:          %s\n", pdb_get_munged_dial(sam_pwent));
117                 
118                 tmp = pdb_get_logon_time(sam_pwent);
119                 printf ("Logon time:           %s\n", tmp ? http_timestring(tmp) : "0");
120                 
121                 tmp = pdb_get_logoff_time(sam_pwent);
122                 printf ("Logoff time:          %s\n", tmp ? http_timestring(tmp) : "0");
123                 
124                 tmp = pdb_get_kickoff_time(sam_pwent);
125                 printf ("Kickoff time:         %s\n", tmp ? http_timestring(tmp) : "0");
126                 
127                 tmp = pdb_get_pass_last_set_time(sam_pwent);
128                 printf ("Password last set:    %s\n", tmp ? http_timestring(tmp) : "0");
129                 
130                 tmp = pdb_get_pass_can_change_time(sam_pwent);
131                 printf ("Password can change:  %s\n", tmp ? http_timestring(tmp) : "0");
132                 
133                 tmp = pdb_get_pass_must_change_time(sam_pwent);
134                 printf ("Password must change: %s\n", tmp ? http_timestring(tmp) : "0");
135
136                 tmp = pdb_get_bad_password_time(sam_pwent);
137                 printf ("Last bad password   : %s\n", tmp ? http_timestring(tmp) : "0");
138                 printf ("Bad password count  : %d\n", 
139                         pdb_get_bad_password_count(sam_pwent));
140                 
141                 hours = pdb_get_hours(sam_pwent);
142                 pdb_sethexhours(temp, hours);
143                 printf ("Logon hours         : %s\n", temp);
144                 
145         } else if (smbpwdstyle) {
146                 char lm_passwd[33];
147                 char nt_passwd[33];
148
149                 uid = nametouid(pdb_get_username(sam_pwent));
150                 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
151                 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
152                         
153                 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
154                        pdb_get_username(sam_pwent),
155                        (unsigned long)uid,
156                        lm_passwd,
157                        nt_passwd,
158                        pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
159                        (uint32)pdb_get_pass_last_set_time(sam_pwent));
160         } else {
161                 uid = nametouid(pdb_get_username(sam_pwent));
162                 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid, 
163                         pdb_get_fullname(sam_pwent));
164         }
165
166         return 0;       
167 }
168
169 /*********************************************************
170  Get an Print User Info
171 **********************************************************/
172
173 static int print_user_info (struct pdb_methods *in, const char *username, BOOL verbosity, BOOL smbpwdstyle)
174 {
175         SAM_ACCOUNT *sam_pwent=NULL;
176         BOOL ret;
177
178         if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
179                 return -1;
180         }
181
182         ret = NT_STATUS_IS_OK(in->getsampwnam (in, sam_pwent, username));
183
184         if (ret==False) {
185                 fprintf (stderr, "Username not found!\n");
186                 pdb_free_sam(&sam_pwent);
187                 return -1;
188         }
189
190         ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
191         pdb_free_sam(&sam_pwent);
192         
193         return ret;
194 }
195         
196 /*********************************************************
197  List Users
198 **********************************************************/
199 static int print_users_list (struct pdb_methods *in, BOOL verbosity, BOOL smbpwdstyle)
200 {
201         SAM_ACCOUNT *sam_pwent=NULL;
202         BOOL check;
203         
204         check = NT_STATUS_IS_OK(in->setsampwent(in, False, 0));
205         if (!check) {
206                 return 1;
207         }
208
209         check = True;
210         if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
211
212         while (check && NT_STATUS_IS_OK(in->getsampwent (in, sam_pwent))) {
213                 if (verbosity)
214                         printf ("---------------\n");
215                 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
216                 pdb_free_sam(&sam_pwent);
217                 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
218         }
219         if (check) pdb_free_sam(&sam_pwent);
220         
221         in->endsampwent(in);
222         return 0;
223 }
224
225 /*********************************************************
226  Fix a list of Users for uninitialised passwords
227 **********************************************************/
228 static int fix_users_list (struct pdb_methods *in)
229 {
230         SAM_ACCOUNT *sam_pwent=NULL;
231         BOOL check;
232         
233         check = NT_STATUS_IS_OK(in->setsampwent(in, False, 0));
234         if (!check) {
235                 return 1;
236         }
237
238         check = True;
239         if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
240
241         while (check && NT_STATUS_IS_OK(in->getsampwent (in, sam_pwent))) {
242                 printf("Updating record for user %s\n", pdb_get_username(sam_pwent));
243         
244                 if (!NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
245                         printf("Update of user %s failed!\n", pdb_get_username(sam_pwent));
246                 }
247                 pdb_free_sam(&sam_pwent);
248                 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
249                 if (!check) {
250                         fprintf(stderr, "Failed to initialise new SAM_ACCOUNT structure (out of memory?)\n");
251                 }
252                         
253         }
254         if (check) pdb_free_sam(&sam_pwent);
255         
256         in->endsampwent(in);
257         return 0;
258 }
259
260 /*********************************************************
261  Set User Info
262 **********************************************************/
263
264 static int set_user_info (struct pdb_methods *in, const char *username, 
265                           const char *fullname, const char *homedir, 
266                           const char *acct_desc, 
267                           const char *drive, const char *script, 
268                           const char *profile, const char *account_control,
269                           const char *user_sid, const char *group_sid,
270                           const char *user_domain,
271                           const BOOL badpw, const BOOL hours,
272                           time_t pwd_can_change, time_t pwd_must_change)
273 {
274         BOOL updated_autolock = False, updated_badpw = False;
275         SAM_ACCOUNT *sam_pwent=NULL;
276         BOOL ret;
277         
278         pdb_init_sam(&sam_pwent);
279         
280         ret = NT_STATUS_IS_OK(in->getsampwnam (in, sam_pwent, username));
281         if (ret==False) {
282                 fprintf (stderr, "Username not found!\n");
283                 pdb_free_sam(&sam_pwent);
284                 return -1;
285         }
286
287         if (hours) {
288                 uint8 hours_array[MAX_HOURS_LEN];
289                 uint32 hours_len;
290                 
291                 hours_len = pdb_get_hours_len(sam_pwent);
292                 memset(hours_array, 0xff, hours_len);
293                 
294                 pdb_set_hours(sam_pwent, hours_array, PDB_CHANGED);
295         }
296
297         if (pwd_can_change != -1) {
298                 pdb_set_pass_can_change_time(sam_pwent, pwd_can_change, PDB_CHANGED);
299         }
300
301         if (pwd_must_change != -1) {
302                 pdb_set_pass_must_change_time(sam_pwent, pwd_must_change, PDB_CHANGED);
303         }
304
305         if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) {
306                 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
307         }
308
309         if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) {
310                 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
311         }
312
313         if (fullname)
314                 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
315         if (acct_desc)
316                 pdb_set_acct_desc(sam_pwent, acct_desc, PDB_CHANGED);
317         if (homedir)
318                 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
319         if (drive)
320                 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
321         if (script)
322                 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
323         if (profile)
324                 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
325         if (user_domain)
326                 pdb_set_domain(sam_pwent, user_domain, PDB_CHANGED);
327
328         if (account_control) {
329                 uint16 not_settable = ~(ACB_DISABLED|ACB_HOMDIRREQ|ACB_PWNOTREQ|
330                                         ACB_PWNOEXP|ACB_AUTOLOCK);
331
332                 uint16 newflag = pdb_decode_acct_ctrl(account_control);
333
334                 if (newflag & not_settable) {
335                         fprintf(stderr, "Can only set [NDHLX] flags\n");
336                         pdb_free_sam(&sam_pwent);
337                         return -1;
338                 }
339
340                 pdb_set_acct_ctrl(sam_pwent,
341                                   (pdb_get_acct_ctrl(sam_pwent) & not_settable) | newflag,
342                                   PDB_CHANGED);
343         }
344         if (user_sid) {
345                 DOM_SID u_sid;
346                 if (!string_to_sid(&u_sid, user_sid)) {
347                         /* not a complete sid, may be a RID, try building a SID */
348                         int u_rid;
349                         
350                         if (sscanf(user_sid, "%d", &u_rid) != 1) {
351                                 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
352                                 return -1;
353                         }
354                         sid_copy(&u_sid, get_global_sam_sid());
355                         sid_append_rid(&u_sid, u_rid);
356                 }
357                 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
358         }
359         if (group_sid) {
360                 DOM_SID g_sid;
361                 if (!string_to_sid(&g_sid, group_sid)) {
362                         /* not a complete sid, may be a RID, try building a SID */
363                         int g_rid;
364                         
365                         if (sscanf(group_sid, "%d", &g_rid) != 1) {
366                                 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
367                                 return -1;
368                         }
369                         sid_copy(&g_sid, get_global_sam_sid());
370                         sid_append_rid(&g_sid, g_rid);
371                 }
372                 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
373         }
374
375         if (badpw) {
376                 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
377                 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
378         }
379
380         if (NT_STATUS_IS_OK(in->update_sam_account (in, sam_pwent)))
381                 print_user_info (in, username, True, False);
382         else {
383                 fprintf (stderr, "Unable to modify entry!\n");
384                 pdb_free_sam(&sam_pwent);
385                 return -1;
386         }
387         pdb_free_sam(&sam_pwent);
388         return 0;
389 }
390
391 /*********************************************************
392  Add New User
393 **********************************************************/
394 static int new_user (struct pdb_methods *in, const char *username,
395                         const char *fullname, const char *homedir,
396                         const char *drive, const char *script,
397                         const char *profile, char *user_sid, char *group_sid)
398 {
399         SAM_ACCOUNT *sam_pwent=NULL;
400
401         char *password1, *password2, *staticpass;
402         
403         get_global_sam_sid();
404
405         if (!NT_STATUS_IS_OK(pdb_init_sam_new(&sam_pwent, username))) {
406                 DEBUG(0, ("could not create account to add new user %s\n", username));
407                 return -1;
408         }
409
410         staticpass = getpass("new password:");
411         password1 = SMB_STRDUP(staticpass);
412         memset(staticpass, 0, strlen(staticpass));
413         staticpass = getpass("retype new password:");
414         password2 = SMB_STRDUP(staticpass);
415         memset(staticpass, 0, strlen(staticpass));
416         if (strcmp (password1, password2)) {
417                 fprintf (stderr, "Passwords does not match!\n");
418                 memset(password1, 0, strlen(password1));
419                 SAFE_FREE(password1);
420                 memset(password2, 0, strlen(password2));
421                 SAFE_FREE(password2);
422                 pdb_free_sam (&sam_pwent);
423                 return -1;
424         }
425
426         pdb_set_plaintext_passwd(sam_pwent, password1);
427         memset(password1, 0, strlen(password1));
428         SAFE_FREE(password1);
429         memset(password2, 0, strlen(password2));
430         SAFE_FREE(password2);
431
432         if (fullname)
433                 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
434         if (homedir)
435                 pdb_set_homedir (sam_pwent, homedir, PDB_CHANGED);
436         if (drive)
437                 pdb_set_dir_drive (sam_pwent, drive, PDB_CHANGED);
438         if (script)
439                 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
440         if (profile)
441                 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
442         if (user_sid) {
443                 DOM_SID u_sid;
444                 if (!string_to_sid(&u_sid, user_sid)) {
445                         /* not a complete sid, may be a RID, try building a SID */
446                         int u_rid;
447                         
448                         if (sscanf(user_sid, "%d", &u_rid) != 1) {
449                                 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
450                                 return -1;
451                         }
452                         sid_copy(&u_sid, get_global_sam_sid());
453                         sid_append_rid(&u_sid, u_rid);
454                 }
455                 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
456         }
457         if (group_sid) {
458                 DOM_SID g_sid;
459                 if (!string_to_sid(&g_sid, group_sid)) {
460                         /* not a complete sid, may be a RID, try building a SID */
461                         int g_rid;
462                         
463                         if (sscanf(group_sid, "%d", &g_rid) != 1) {
464                                 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
465                                 return -1;
466                         }
467                         sid_copy(&g_sid, get_global_sam_sid());
468                         sid_append_rid(&g_sid, g_rid);
469                 }
470                 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
471         }
472         
473         pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL, PDB_CHANGED);
474         
475         if (NT_STATUS_IS_OK(in->add_sam_account (in, sam_pwent))) { 
476                 print_user_info (in, username, True, False);
477         } else {
478                 fprintf (stderr, "Unable to add user! (does it already exist?)\n");
479                 pdb_free_sam (&sam_pwent);
480                 return -1;
481         }
482         pdb_free_sam (&sam_pwent);
483         return 0;
484 }
485
486 /*********************************************************
487  Add New Machine
488 **********************************************************/
489
490 static int new_machine (struct pdb_methods *in, const char *machine_in)
491 {
492         SAM_ACCOUNT *sam_pwent=NULL;
493         fstring machinename;
494         fstring machineaccount;
495         struct passwd  *pwd = NULL;
496         
497         get_global_sam_sid();
498
499         fstrcpy(machinename, machine_in); 
500         machinename[15]= '\0';
501
502         if (machinename[strlen (machinename) -1] == '$')
503                 machinename[strlen (machinename) -1] = '\0';
504         
505         strlower_m(machinename);
506         
507         fstrcpy(machineaccount, machinename);
508         fstrcat(machineaccount, "$");
509
510         if ((pwd = getpwnam_alloc(NULL, machineaccount))) {
511                 if (!NT_STATUS_IS_OK(pdb_init_sam_pw( &sam_pwent, pwd))) {
512                         fprintf(stderr, "Could not init sam from pw\n");
513                         talloc_free(pwd);
514                         return -1;
515                 }
516                 talloc_free(&pwd);
517         } else {
518                 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
519                         fprintf(stderr, "Could not init sam from pw\n");
520                         return -1;
521                 }
522         }
523
524         pdb_set_plaintext_passwd (sam_pwent, machinename);
525
526         pdb_set_username (sam_pwent, machineaccount, PDB_CHANGED);
527         
528         pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST, PDB_CHANGED);
529         
530         pdb_set_group_sid_from_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS, PDB_CHANGED);
531         
532         if (NT_STATUS_IS_OK(in->add_sam_account (in, sam_pwent))) {
533                 print_user_info (in, machineaccount, True, False);
534         } else {
535                 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
536                 pdb_free_sam (&sam_pwent);
537                 return -1;
538         }
539         pdb_free_sam (&sam_pwent);
540         return 0;
541 }
542
543 /*********************************************************
544  Delete user entry
545 **********************************************************/
546
547 static int delete_user_entry (struct pdb_methods *in, const char *username)
548 {
549         SAM_ACCOUNT *samaccount = NULL;
550
551         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
552                 return -1;
553         }
554
555         if (!NT_STATUS_IS_OK(in->getsampwnam(in, samaccount, username))) {
556                 fprintf (stderr, "user %s does not exist in the passdb\n", username);
557                 return -1;
558         }
559
560         if (!NT_STATUS_IS_OK(in->delete_sam_account (in, samaccount))) {
561                 fprintf (stderr, "Unable to delete user %s\n", username);
562                 return -1;
563         }
564         return 0;
565 }
566
567 /*********************************************************
568  Delete machine entry
569 **********************************************************/
570
571 static int delete_machine_entry (struct pdb_methods *in, const char *machinename)
572 {
573         fstring name;
574         SAM_ACCOUNT *samaccount = NULL;
575         
576         fstrcpy(name, machinename);
577         name[15] = '\0';
578         if (name[strlen(name)-1] != '$')
579                 fstrcat (name, "$");
580
581         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
582                 return -1;
583         }
584
585         if (!NT_STATUS_IS_OK(in->getsampwnam(in, samaccount, name))) {
586                 fprintf (stderr, "machine %s does not exist in the passdb\n", name);
587                 return -1;
588         }
589
590         if (!NT_STATUS_IS_OK(in->delete_sam_account (in, samaccount))) {
591                 fprintf (stderr, "Unable to delete machine %s\n", name);
592                 return -1;
593         }
594
595         return 0;
596 }
597
598 /*********************************************************
599  Start here.
600 **********************************************************/
601
602 int main (int argc, char **argv)
603 {
604         static BOOL list_users = False;
605         static BOOL verbose = False;
606         static BOOL spstyle = False;
607         static BOOL machine = False;
608         static BOOL add_user = False;
609         static BOOL delete_user = False;
610         static BOOL modify_user = False;
611         uint32  setparms, checkparms;
612         int opt;
613         static char *full_name = NULL;
614         static char *acct_desc = NULL;
615         static const char *user_name = NULL;
616         static char *home_dir = NULL;
617         static char *home_drive = NULL;
618         static char *backend = NULL;
619         static char *backend_in = NULL;
620         static char *backend_out = NULL;
621         static BOOL transfer_groups = False;
622         static BOOL transfer_account_policies = False;
623         static BOOL reset_account_policies = False;
624         static BOOL  force_initialised_password = False;
625         static char *logon_script = NULL;
626         static char *profile_path = NULL;
627         static char *user_domain = NULL;
628         static char *account_control = NULL;
629         static char *account_policy = NULL;
630         static char *user_sid = NULL;
631         static char *group_sid = NULL;
632         static long int account_policy_value = 0;
633         BOOL account_policy_value_set = False;
634         static BOOL badpw_reset = False;
635         static BOOL hours_reset = False;
636         static char *pwd_can_change_time = NULL;
637         static char *pwd_must_change_time = NULL;
638         static char *pwd_time_format = NULL;
639
640         struct pdb_methods *bdef = NULL;
641         poptContext pc;
642         struct poptOption long_options[] = {
643                 POPT_AUTOHELP
644                 {"list",        'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
645                 {"verbose",     'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
646                 {"smbpasswd-style",     'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
647                 {"user",        'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
648                 {"account-desc",        'N', POPT_ARG_STRING, &acct_desc, 0, "set account description", NULL},
649                 {"fullname",    'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
650                 {"homedir",     'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
651                 {"drive",       'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
652                 {"script",      'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
653                 {"profile",     'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
654                 {"domain",      'I', POPT_ARG_STRING, &user_domain, 0, "set a users' domain", 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                 {"policies",    'y', POPT_ARG_NONE, &transfer_account_policies, 0, "use -i and -e to move account policies between backends", NULL},
666                 {"policies-reset",      0, POPT_ARG_NONE, &reset_account_policies, 0, "restore default policies", NULL},
667                 {"account-policy",      'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
668                 {"value",       'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
669                 {"account-control",     'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
670                 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
671                 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
672                 {"logon-hours-reset", 'Z', POPT_ARG_NONE, &hours_reset, 0, "reset logon hours", NULL},
673                 {"pwd-can-change-time", 0, POPT_ARG_STRING, &pwd_can_change_time, 0, "Set password can change time (unix time in seconds since 1970 if time format not provided)", NULL },
674                 {"pwd-must-change-time", 0, POPT_ARG_STRING, &pwd_must_change_time, 0, "Set password must change time (unix time in seconds since 1970 if time format not provided)", NULL },
675                 {"time-format", 0, POPT_ARG_STRING, &pwd_time_format, 0, "The time format for time parameters", NULL },
676                 POPT_COMMON_SAMBA
677                 POPT_TABLEEND
678         };
679         
680         load_case_tables();
681
682         setup_logging("pdbedit", True);
683         
684         pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
685                             POPT_CONTEXT_KEEP_FIRST);
686         
687         while((opt = poptGetNextOpt(pc)) != -1) {
688                 switch (opt) {
689                 case 'C':
690                         account_policy_value_set = True;
691                         break;
692                 }
693         }
694
695         poptGetArg(pc); /* Drop argv[0], the program name */
696
697         if (user_name == NULL)
698                 user_name = poptGetArg(pc);
699
700         if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
701                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
702                 exit(1);
703         }
704
705         if(!initialize_password_db(False))
706                 exit(1);
707
708         if (!init_names())
709                 exit(1);
710
711         setparms =      (backend ? BIT_BACKEND : 0) +
712                         (verbose ? BIT_VERBOSE : 0) +
713                         (spstyle ? BIT_SPSTYLE : 0) +
714                         (full_name ? BIT_FULLNAME : 0) +
715                         (home_dir ? BIT_HOMEDIR : 0) +
716                         (home_drive ? BIT_HDIRDRIVE : 0) +
717                         (logon_script ? BIT_LOGSCRIPT : 0) +
718                         (profile_path ? BIT_PROFILE : 0) +
719                         (user_domain ? BIT_USERDOMAIN : 0) +
720                         (machine ? BIT_MACHINE : 0) +
721                         (user_name ? BIT_USER : 0) +
722                         (list_users ? BIT_LIST : 0) +
723                         (force_initialised_password ? BIT_FIX_INIT : 0) +
724                         (user_sid ? BIT_USERSIDS : 0) +
725                         (group_sid ? BIT_USERSIDS : 0) +
726                         (modify_user ? BIT_MODIFY : 0) +
727                         (add_user ? BIT_CREATE : 0) +
728                         (delete_user ? BIT_DELETE : 0) +
729                         (account_control ? BIT_ACCTCTRL : 0) +
730                         (account_policy ? BIT_ACCPOLICY : 0) +
731                         (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
732                         (backend_in ? BIT_IMPORT : 0) +
733                         (backend_out ? BIT_EXPORT : 0) +
734                         (badpw_reset ? BIT_BADPWRESET : 0) +
735                         (hours_reset ? BIT_LOGONHOURS : 0) +
736                         (pwd_can_change_time ? BIT_CAN_CHANGE: 0) +
737                         (pwd_must_change_time ? BIT_MUST_CHANGE: 0);
738
739         if (setparms & BIT_BACKEND) {
740                 if (!NT_STATUS_IS_OK(make_pdb_method_name( &bdef, backend ))) {
741                         fprintf(stderr, "Can't initialize passdb backend.\n");
742                         return 1;
743                 }
744         } else {
745                 if (!NT_STATUS_IS_OK(make_pdb_method_name(&bdef, lp_passdb_backend()))) {
746                         fprintf(stderr, "Can't initialize passdb backend.\n");
747                         return 1;
748                 }
749         }
750         
751         /* the lowest bit options are always accepted */
752         checkparms = setparms & ~MASK_ALWAYS_GOOD;
753
754         if (checkparms & BIT_FIX_INIT) {
755                 return fix_users_list(bdef);
756         }
757
758         /* account policy operations */
759         if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
760                 uint32 value;
761                 int field = account_policy_name_to_fieldnum(account_policy);
762                 if (field == 0) {
763                         char *apn = account_policy_names_list();
764                         fprintf(stderr, "No account policy by that name\n");
765                         if (apn) {
766                                 fprintf(stderr, "Account policy names are :\n%s\n", apn);
767                         }
768                         SAFE_FREE(apn);
769                         exit(1);
770                 }
771                 if (!pdb_get_account_policy(field, &value)) {
772                         fprintf(stderr, "valid account policy, but unable to fetch value!\n");
773                         if (!account_policy_value_set)
774                                 exit(1);
775                 }
776                 printf("account policy \"%s\" description: %s\n", account_policy, account_policy_get_desc(field));
777                 if (account_policy_value_set) {
778                         printf("account policy \"%s\" value was: %u\n", account_policy, value);
779                         if (!pdb_set_account_policy(field, account_policy_value)) {
780                                 fprintf(stderr, "valid account policy, but unable to set value!\n");
781                                 exit(1);
782                         }
783                         printf("account policy \"%s\" value is now: %lu\n", account_policy, account_policy_value);
784                         exit(0);
785                 } else {
786                         printf("account policy \"%s\" value is: %u\n", account_policy, value);
787                         exit(0);
788                 }
789         }
790
791         if (reset_account_policies) {
792                 if (!reinit_account_policies()) {
793                         exit(1);
794                 }
795
796                 exit(0);
797         }
798
799         /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
800         /* fake up BIT_LIST if only BIT_USER is defined */
801         if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
802                 checkparms += BIT_LIST;
803         }
804         
805         /* modify flag is optional to maintain backwards compatibility */
806         /* fake up BIT_MODIFY if BIT_USER  and at least one of MASK_USER_GOOD is defined */
807         if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
808                 checkparms += BIT_MODIFY;
809         }
810
811         /* list users operations */
812         if (checkparms & BIT_LIST) {
813                 if (!(checkparms & ~BIT_LIST)) {
814                         return print_users_list (bdef, verbose, spstyle);
815                 }
816                 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
817                         return print_user_info (bdef, user_name, verbose, spstyle);
818                 }
819         }
820         
821         /* mask out users options */
822         checkparms &= ~MASK_USER_GOOD;
823
824         /* if bad password count is reset, we must be modifying */
825         if (checkparms & BIT_BADPWRESET) {
826                 checkparms |= BIT_MODIFY;
827                 checkparms &= ~BIT_BADPWRESET;
828         }
829
830         /* if logon hours is reset, must modify */
831         if (checkparms & BIT_LOGONHOURS) {
832                 checkparms |= BIT_MODIFY;
833                 checkparms &= ~BIT_LOGONHOURS;
834         }
835         
836         /* account operation */
837         if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
838                 /* check use of -u option */
839                 if (!(checkparms & BIT_USER)) {
840                         fprintf (stderr, "Username not specified! (use -u option)\n");
841                         return -1;
842                 }
843
844                 /* account creation operations */
845                 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
846                         if (checkparms & BIT_MACHINE) {
847                                 return new_machine (bdef, user_name);
848                         } else {
849                                 return new_user (bdef, user_name, full_name, home_dir, 
850                                                  home_drive, logon_script, 
851                                                  profile_path, user_sid, group_sid);
852                         }
853                 }
854
855                 /* account deletion operations */
856                 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
857                         if (checkparms & BIT_MACHINE) {
858                                 return delete_machine_entry (bdef, user_name);
859                         } else {
860                                 return delete_user_entry (bdef, user_name);
861                         }
862                 }
863
864                 /* account modification operations */
865                 if (!(checkparms & ~(BIT_MODIFY + BIT_USER))) {
866                         time_t pwd_can_change = -1;
867                         time_t pwd_must_change = -1;
868                         const char *errstr;
869
870                         if (pwd_can_change_time) {
871                                 errstr = "can";
872                                 if (pwd_time_format) {
873                                         struct tm tm;
874                                         char *ret;
875
876                                         memset(&tm, 0, sizeof(struct tm));
877                                         ret = strptime(pwd_can_change_time, pwd_time_format, &tm);
878                                         if (ret == NULL || *ret != '\0') {
879                                                 goto error;
880                                         }
881
882                                         pwd_can_change = mktime(&tm);
883
884                                         if (pwd_can_change == -1) {
885                                                 goto error;
886                                         }
887                                 } else { /* assume it is unix time */
888                                         errno = 0;
889                                         pwd_can_change = strtol(pwd_can_change_time, NULL, 10);
890                                         if (errno) {
891                                                 goto error;
892                                         }
893                                 }       
894                         }
895                         if (pwd_must_change_time) {
896                                 errstr = "must";
897                                 if (pwd_time_format) {
898                                         struct tm tm;
899                                         char *ret;
900
901                                         memset(&tm, 0, sizeof(struct tm));
902                                         ret = strptime(pwd_must_change_time, pwd_time_format, &tm);
903                                         if (ret == NULL || *ret != '\0') {
904                                                 goto error;
905                                         }
906
907                                         pwd_must_change = mktime(&tm);
908
909                                         if (pwd_must_change == -1) {
910                                                 goto error;
911                                         }
912                                 } else { /* assume it is unix time */
913                                         errno = 0;
914                                         pwd_must_change = strtol(pwd_must_change_time, NULL, 10);
915                                         if (errno) {
916                                                 goto error;
917                                         }
918                                 }       
919                         }
920                         return set_user_info (bdef, user_name, full_name,
921                                               home_dir,
922                                               acct_desc,
923                                               home_drive,
924                                               logon_script,
925                                               profile_path, account_control,
926                                               user_sid, group_sid,
927                                               user_domain,
928                                               badpw_reset, hours_reset,
929                                               pwd_can_change, pwd_must_change);
930 error:
931                         fprintf (stderr, "Error parsing the time in pwd-%s-change-time!\n", errstr);
932                         return -1;
933                 }
934         }
935
936         if (setparms >= 0x20) {
937                 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
938         }
939         poptPrintHelp(pc, stderr, 0);
940
941         return 1;
942 }