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