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