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