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