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