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