Jeremy merged across my string parinoia fixes, but forgot to enable them! :-)
[kai/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  Print info from sam structure
86 **********************************************************/
87
88 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
89 {
90         uid_t uid;
91         gid_t gid;
92         time_t tmp;
93
94         /* TODO: chaeck if entry is a user or a workstation */
95         if (!sam_pwent) return -1;
96         
97         if (verbosity) {
98                 printf ("Unix username:        %s\n", pdb_get_username(sam_pwent));
99                 printf ("NT username:          %s\n", pdb_get_nt_username(sam_pwent));
100                 printf ("Account Flags:        %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
101                 
102                 if (IS_SAM_UNIX_USER(sam_pwent)) {
103                         uid = pdb_get_uid(sam_pwent);
104                         gid = pdb_get_gid(sam_pwent);
105                         printf ("User ID/Group ID:     %d/%d\n", uid, gid);
106                 }
107                 printf ("User SID:             %s\n",
108                         sid_string_static(pdb_get_user_sid(sam_pwent)));
109                 printf ("Primary Group SID:    %s\n",
110                         sid_string_static(pdb_get_group_sid(sam_pwent)));
111                 printf ("Full Name:            %s\n", pdb_get_fullname(sam_pwent));
112                 printf ("Home Directory:       %s\n", pdb_get_homedir(sam_pwent));
113                 printf ("HomeDir Drive:        %s\n", pdb_get_dir_drive(sam_pwent));
114                 printf ("Logon Script:         %s\n", pdb_get_logon_script(sam_pwent));
115                 printf ("Profile Path:         %s\n", pdb_get_profile_path(sam_pwent));
116                 printf ("Domain:               %s\n", pdb_get_domain(sam_pwent));
117                 printf ("Account desc:         %s\n", pdb_get_acct_desc(sam_pwent));
118                 printf ("Workstations:         %s\n", pdb_get_workstations(sam_pwent));
119                 printf ("Munged dial:          %s\n", pdb_get_munged_dial(sam_pwent));
120                 
121                 tmp = pdb_get_logon_time(sam_pwent);
122                 printf ("Logon time:           %s\n", tmp ? http_timestring(tmp) : "0");
123                 
124                 tmp = pdb_get_logoff_time(sam_pwent);
125                 printf ("Logoff time:          %s\n", tmp ? http_timestring(tmp) : "0");
126                 
127                 tmp = pdb_get_kickoff_time(sam_pwent);
128                 printf ("Kickoff time:         %s\n", tmp ? http_timestring(tmp) : "0");
129                 
130                 tmp = pdb_get_pass_last_set_time(sam_pwent);
131                 printf ("Password last set:    %s\n", tmp ? http_timestring(tmp) : "0");
132                 
133                 tmp = pdb_get_pass_can_change_time(sam_pwent);
134                 printf ("Password can change:  %s\n", tmp ? http_timestring(tmp) : "0");
135                 
136                 tmp = pdb_get_pass_must_change_time(sam_pwent);
137                 printf ("Password must change: %s\n", tmp ? http_timestring(tmp) : "0");
138                 
139         } else if (smbpwdstyle) {
140                 if (IS_SAM_UNIX_USER(sam_pwent)) {
141                         char lm_passwd[33];
142                         char nt_passwd[33];
143
144                         uid = pdb_get_uid(sam_pwent);
145                         pdb_sethexpwd(lm_passwd, 
146                                       pdb_get_lanman_passwd(sam_pwent), 
147                                       pdb_get_acct_ctrl(sam_pwent));
148                         pdb_sethexpwd(nt_passwd, 
149                                       pdb_get_nt_passwd(sam_pwent), 
150                                       pdb_get_acct_ctrl(sam_pwent));
151                         
152                         printf("%s:%d:%s:%s:%s:LCT-%08X:\n",
153                                pdb_get_username(sam_pwent),
154                                uid,
155                                lm_passwd,
156                                nt_passwd,
157                                pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
158                                (uint32)pdb_get_pass_last_set_time(sam_pwent));
159                 } else {
160                         fprintf(stderr, "Can't output in smbpasswd format, no uid on this record.\n");
161                 }
162         } else {
163                 if (IS_SAM_UNIX_USER(sam_pwent)) {
164                         printf ("%s:%d:%s\n", pdb_get_username(sam_pwent), pdb_get_uid(sam_pwent), 
165                                 pdb_get_fullname(sam_pwent));
166                 } else {        
167                         printf ("%s:(null):%s\n", pdb_get_username(sam_pwent), pdb_get_fullname(sam_pwent));
168                 }
169         }
170
171         return 0;       
172 }
173
174 /*********************************************************
175  Get an Print User Info
176 **********************************************************/
177
178 static int print_user_info (struct pdb_context *in, const char *username, BOOL verbosity, BOOL smbpwdstyle)
179 {
180         SAM_ACCOUNT *sam_pwent=NULL;
181         BOOL ret;
182         
183         if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
184                 return -1;
185         }
186         
187         ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
188
189         if (ret==False) {
190                 fprintf (stderr, "Username not found!\n");
191                 pdb_free_sam(&sam_pwent);
192                 return -1;
193         }
194         
195         ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
196         pdb_free_sam(&sam_pwent);
197         
198         return ret;
199 }
200         
201 /*********************************************************
202  List Users
203 **********************************************************/
204 static int print_users_list (struct pdb_context *in, BOOL verbosity, BOOL smbpwdstyle)
205 {
206         SAM_ACCOUNT *sam_pwent=NULL;
207         BOOL check, ret;
208         
209         check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False));
210         if (!check) {
211                 return 1;
212         }
213
214         check = True;
215         if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
216
217         while (check && (ret = NT_STATUS_IS_OK(in->pdb_getsampwent (in, sam_pwent)))) {
218                 if (verbosity)
219                         printf ("---------------\n");
220                 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
221                 pdb_free_sam(&sam_pwent);
222                 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
223         }
224         if (check) pdb_free_sam(&sam_pwent);
225         
226         in->pdb_endsampwent(in);
227         return 0;
228 }
229
230 /*********************************************************
231  Set User Info
232 **********************************************************/
233
234 static int set_user_info (struct pdb_context *in, const char *username, 
235                           const char *fullname, const char *homedir, 
236                           const char *drive, const char *script, 
237                           const char *profile, const char *account_control)
238 {
239         SAM_ACCOUNT *sam_pwent=NULL;
240         BOOL ret;
241         
242         pdb_init_sam(&sam_pwent);
243         
244         ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
245         if (ret==False) {
246                 fprintf (stderr, "Username not found!\n");
247                 pdb_free_sam(&sam_pwent);
248                 return -1;
249         }
250         
251         if (fullname)
252                 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
253         if (homedir)
254                 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
255         if (drive)
256                 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
257         if (script)
258                 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
259         if (profile)
260                 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
261
262         if (account_control) {
263                 uint16 not_settable = ~(ACB_DISABLED|ACB_HOMDIRREQ|ACB_PWNOTREQ|
264                                         ACB_PWNOEXP|ACB_AUTOLOCK);
265
266                 uint16 newflag = pdb_decode_acct_ctrl(account_control);
267
268                 if (newflag & not_settable) {
269                         fprintf(stderr, "Can only set [NDHLX] flags\n");
270                         pdb_free_sam(&sam_pwent);
271                         return -1;
272                 }
273
274                 pdb_set_acct_ctrl(sam_pwent,
275                                   (pdb_get_acct_ctrl(sam_pwent) & not_settable) | newflag,
276                                   PDB_CHANGED);
277         }
278         
279         if (NT_STATUS_IS_OK(in->pdb_update_sam_account (in, sam_pwent)))
280                 print_user_info (in, username, True, False);
281         else {
282                 fprintf (stderr, "Unable to modify entry!\n");
283                 pdb_free_sam(&sam_pwent);
284                 return -1;
285         }
286         pdb_free_sam(&sam_pwent);
287         return 0;
288 }
289
290 /*********************************************************
291  Add New User
292 **********************************************************/
293 static int new_user (struct pdb_context *in, const char *username, const char *fullname, const char *homedir, const char *drive, const char *script, const char *profile)
294 {
295         SAM_ACCOUNT *sam_pwent=NULL;
296         struct passwd  *pwd = NULL;
297         char *password1, *password2, *staticpass;
298         
299         ZERO_STRUCT(sam_pwent);
300
301         if ((pwd = getpwnam_alloc(username))) {
302                 pdb_init_sam_pw (&sam_pwent, pwd);
303                 passwd_free(&pwd);
304         } else {
305                 fprintf (stderr, "WARNING: user %s does not exist in system passwd\n", username);
306                 pdb_init_sam(&sam_pwent);
307                 if (!pdb_set_username(sam_pwent, username, PDB_CHANGED)) {
308                         return False;
309                 }
310         }
311
312         staticpass = getpass("new password:");
313         password1 = strdup(staticpass);
314         memset(staticpass, 0, strlen(staticpass));
315         staticpass = getpass("retype new password:");
316         password2 = strdup(staticpass);
317         memset(staticpass, 0, strlen(staticpass));
318         if (strcmp (password1, password2)) {
319                 fprintf (stderr, "Passwords does not match!\n");
320                 memset(password1, 0, strlen(password1));
321                 SAFE_FREE(password1);
322                 memset(password2, 0, strlen(password2));
323                 SAFE_FREE(password2);
324                 pdb_free_sam (&sam_pwent);
325                 return -1;
326         }
327
328         pdb_set_plaintext_passwd(sam_pwent, password1);
329         memset(password1, 0, strlen(password1));
330         SAFE_FREE(password1);
331         memset(password2, 0, strlen(password2));
332         SAFE_FREE(password2);
333
334         if (fullname)
335                 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
336         if (homedir)
337                 pdb_set_homedir (sam_pwent, homedir, PDB_CHANGED);
338         if (drive)
339                 pdb_set_dir_drive (sam_pwent, drive, PDB_CHANGED);
340         if (script)
341                 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
342         if (profile)
343                 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
344         
345         pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL, PDB_CHANGED);
346         
347         if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) { 
348                 print_user_info (in, username, True, False);
349         } else {
350                 fprintf (stderr, "Unable to add user! (does it alredy exist?)\n");
351                 pdb_free_sam (&sam_pwent);
352                 return -1;
353         }
354         pdb_free_sam (&sam_pwent);
355         return 0;
356 }
357
358 /*********************************************************
359  Add New Machine
360 **********************************************************/
361
362 static int new_machine (struct pdb_context *in, const char *machine_in)
363 {
364         SAM_ACCOUNT *sam_pwent=NULL;
365         fstring machinename;
366         fstring machineaccount;
367         struct passwd  *pwd = NULL;
368         
369         fstrcpy(machinename, machine_in); 
370         machinename[15]= '\0';
371
372         if (machinename[strlen (machinename) -1] == '$')
373                 machinename[strlen (machinename) -1] = '\0';
374         
375         strlower_m(machinename);
376         
377         fstrcpy(machineaccount, machinename);
378         fstrcat(machineaccount, "$");
379
380         if ((pwd = getpwnam_alloc(machineaccount))) {
381                 if (!NT_STATUS_IS_OK(pdb_init_sam_pw( &sam_pwent, pwd))) {
382                         fprintf(stderr, "Could not init sam from pw\n");
383                         passwd_free(&pwd);
384                         return -1;
385                 }
386                 passwd_free(&pwd);
387         } else {
388                 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
389                         fprintf(stderr, "Could not init sam from pw\n");
390                         return -1;
391                 }
392         }
393
394         pdb_set_plaintext_passwd (sam_pwent, machinename);
395
396         pdb_set_username (sam_pwent, machineaccount, PDB_CHANGED);
397         
398         pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST, PDB_CHANGED);
399         
400         pdb_set_group_sid_from_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS, PDB_CHANGED);
401         
402         if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) {
403                 print_user_info (in, machineaccount, True, False);
404         } else {
405                 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
406                 pdb_free_sam (&sam_pwent);
407                 return -1;
408         }
409         pdb_free_sam (&sam_pwent);
410         return 0;
411 }
412
413 /*********************************************************
414  Delete user entry
415 **********************************************************/
416
417 static int delete_user_entry (struct pdb_context *in, const char *username)
418 {
419         SAM_ACCOUNT *samaccount = NULL;
420
421         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
422                 return -1;
423         }
424
425         if (NT_STATUS_IS_ERR(in->pdb_getsampwnam(in, samaccount, username))) {
426                 fprintf (stderr, "user %s does not exist in the passdb\n", username);
427                 return -1;
428         }
429
430         return NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount));
431 }
432
433 /*********************************************************
434  Delete machine entry
435 **********************************************************/
436
437 static int delete_machine_entry (struct pdb_context *in, const char *machinename)
438 {
439         fstring name;
440         SAM_ACCOUNT *samaccount = NULL;
441         
442         fstrcpy(name, machinename);
443         name[15] = '\0';
444         if (name[strlen(name)-1] != '$')
445                 fstrcat (name, "$");
446
447         if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
448                 return -1;
449         }
450
451         if (NT_STATUS_IS_ERR(in->pdb_getsampwnam(in, samaccount, name))) {
452                 fprintf (stderr, "machine %s does not exist in the passdb\n", name);
453                 return -1;
454         }
455
456         return NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount));
457 }
458
459 /*********************************************************
460  Start here.
461 **********************************************************/
462
463 int main (int argc, char **argv)
464 {
465         static BOOL list_users = False;
466         static BOOL verbose = False;
467         static BOOL spstyle = False;
468         static BOOL machine = False;
469         static BOOL add_user = False;
470         static BOOL delete_user = False;
471         static BOOL modify_user = False;
472         uint32  setparms, checkparms;
473         int opt;
474         static char *full_name = NULL;
475         static const char *user_name = NULL;
476         static char *home_dir = NULL;
477         static char *home_drive = NULL;
478         static char *backend = NULL;
479         static char *backend_in = NULL;
480         static char *backend_out = NULL;
481         static char *logon_script = NULL;
482         static char *profile_path = NULL;
483         static char *account_control = NULL;
484         static char *account_policy = NULL;
485         static long int account_policy_value = 0;
486         BOOL account_policy_value_set = False;
487
488         struct pdb_context *bin;
489         struct pdb_context *bout;
490         struct pdb_context *bdef;
491         poptContext pc;
492         struct poptOption long_options[] = {
493                 POPT_AUTOHELP
494                 {"list",        'l', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
495                 {"verbose",     'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
496                 {"smbpasswd-style",     'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
497                 {"user",        'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
498                 {"fullname",    'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
499                 {"homedir",     'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
500                 {"drive",       'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
501                 {"script",      'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
502                 {"profile",     'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
503                 {"create",      'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
504                 {"modify",      'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
505                 {"machine",     'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
506                 {"delete",      'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
507                 {"backend",     'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
508                 {"import",      'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
509                 {"export",      'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
510                 {"account-policy",      'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
511                 {"value",       'V', POPT_ARG_LONG, &account_policy_value, 'V',"set the account policy to this value", NULL},
512                 {"account-control",     'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
513                 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_debug },
514                 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_configfile },
515                 {0,0,0,0}
516         };
517         
518         setup_logging("pdbedit", True);
519         
520         pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
521                             POPT_CONTEXT_KEEP_FIRST);
522         
523         while((opt = poptGetNextOpt(pc)) != -1) {
524                 switch (opt) {
525                 case 'V':
526                         account_policy_value_set = True;
527                         break;
528                 }
529         }
530
531         poptGetArg(pc); /* Drop argv[0], the program name */
532
533         if (user_name == NULL)
534                 user_name = poptGetArg(pc);
535
536         if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
537                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
538                 exit(1);
539         }
540
541         if (!init_names())
542                 exit(1);
543
544         setparms =      (backend ? BIT_BACKEND : 0) +
545                         (verbose ? BIT_VERBOSE : 0) +
546                         (spstyle ? BIT_SPSTYLE : 0) +
547                         (full_name ? BIT_FULLNAME : 0) +
548                         (home_dir ? BIT_HOMEDIR : 0) +
549                         (home_drive ? BIT_HDIRDRIVE : 0) +
550                         (logon_script ? BIT_LOGSCRIPT : 0) +
551                         (profile_path ? BIT_PROFILE : 0) +
552                         (machine ? BIT_MACHINE : 0) +
553                         (user_name ? BIT_USER : 0) +
554                         (list_users ? BIT_LIST : 0) +
555                         (modify_user ? BIT_MODIFY : 0) +
556                         (add_user ? BIT_CREATE : 0) +
557                         (delete_user ? BIT_DELETE : 0) +
558                         (account_control ? BIT_ACCTCTRL : 0) +
559                         (account_policy ? BIT_ACCPOLICY : 0) +
560                         (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
561                         (backend_in ? BIT_IMPORT : 0) +
562                         (backend_out ? BIT_EXPORT : 0);
563
564         if (setparms & BIT_BACKEND) {
565                 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bdef, backend))) {
566                         fprintf(stderr, "Can't initialize passdb backend.\n");
567                         return 1;
568                 }
569         } else {
570                 if (!NT_STATUS_IS_OK(make_pdb_context_list(&bdef, lp_passdb_backend()))) {
571                         fprintf(stderr, "Can't initialize passdb backend.\n");
572                         return 1;
573                 }
574         }
575         
576         /* the lowest bit options are always accepted */
577         checkparms = setparms & ~MASK_ALWAYS_GOOD;
578
579         /* account policy operations */
580         if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
581                 uint32 value;
582                 int field = account_policy_name_to_fieldnum(account_policy);
583                 if (field == 0) {
584                         fprintf(stderr, "No account policy by that name\n");
585                         exit(1);
586                 }
587                 if (!account_policy_get(field, &value)) {
588                         fprintf(stderr, "valid account policy, but unable to fetch value!\n");
589                         exit(1);
590                 }
591                 if (account_policy_value_set) {
592                         printf("account policy value for %s was %u\n", account_policy, value);
593                         if (!account_policy_set(field, account_policy_value)) {
594                                 fprintf(stderr, "valid account policy, but unable to set value!\n");
595                                 exit(1);
596                         }
597                         printf("account policy value for %s is now %lu\n", account_policy, account_policy_value);
598                         exit(0);
599                 } else {
600                         printf("account policy value for %s is %u\n", account_policy, value);
601                         exit(0);
602                 }
603         }
604
605         /* import and export operations */
606         if (((checkparms & BIT_IMPORT) || (checkparms & BIT_EXPORT))
607                         && !(checkparms & ~(BIT_IMPORT +BIT_EXPORT))) {
608                 if (backend_in) {
609                         if (!NT_STATUS_IS_OK(make_pdb_context_string(&bin, backend_in))) {
610                                 fprintf(stderr, "Can't initialize passdb backend.\n");
611                                 return 1;
612                         }
613                 } else {
614                         bin = bdef;
615                 }
616                 if (backend_out) {
617                         if (!NT_STATUS_IS_OK(make_pdb_context_string(&bout, backend_out))) {
618                                 fprintf(stderr, "Can't initialize %s.\n", backend_out);
619                                 return 1;
620                         }
621                 } else {
622                         bout = bdef;
623                 }
624                 return export_database(bin, bout);
625         }
626
627         /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
628         /* fake up BIT_LIST if only BIT_USER is defined */
629         if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
630                 checkparms += BIT_LIST;
631         }
632         
633         /* modify flag is optional to maintain backwards compatibility */
634         /* fake up BIT_MODIFY if BIT_USER  and at least one of MASK_USER_GOOD is defined */
635         if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
636                 checkparms += BIT_MODIFY;
637         }
638
639         /* list users operations */
640         if (checkparms & BIT_LIST) {
641                 if (!(checkparms & ~BIT_LIST)) {
642                         return print_users_list (bdef, verbose, spstyle);
643                 }
644                 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
645                         return print_user_info (bdef, user_name, verbose, spstyle);
646                 }
647         }
648         
649         /* mask out users options */
650         checkparms &= ~MASK_USER_GOOD;
651         
652         /* account operation */
653         if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
654                 /* check use of -u option */
655                 if (!(checkparms & BIT_USER)) {
656                         fprintf (stderr, "Username not specified! (use -u option)\n");
657                         return -1;
658                 }
659
660                 /* account creation operations */
661                 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
662                         if (checkparms & BIT_MACHINE) {
663                                 return new_machine (bdef, user_name);
664                         } else {
665                                 return new_user (bdef, user_name, full_name, home_dir, 
666                                                  home_drive, logon_script, 
667                                                  profile_path);
668                         }
669                 }
670
671                 /* account deletion operations */
672                 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
673                         if (checkparms & BIT_MACHINE) {
674                                 return delete_machine_entry (bdef, user_name);
675                         } else {
676                                 return delete_user_entry (bdef, user_name);
677                         }
678                 }
679
680                 /* account modification operations */
681                 if (!(checkparms & ~(BIT_MODIFY + BIT_USER))) {
682                         return set_user_info (bdef, user_name, full_name,
683                                               home_dir,
684                                               home_drive,
685                                               logon_script,
686                                               profile_path, account_control);
687                 }
688         }
689
690         if (setparms >= 0x20) {
691                 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
692         }
693         poptPrintHelp(pc, stderr, 0);
694
695         return 1;
696 }