2 Unix SMB/CIFS implementation.
3 passdb editing frontend
5 Copyright (C) Simo Sorce 2000
6 Copyright (C) Andrew Bartlett 2001
7 Copyright (C) Jelmer Vernooij 2002
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.
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.
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.
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 #define BIT_FIX_INIT 0x04000000
51 #define BIT_BADPWRESET 0x08000000
53 #define MASK_ALWAYS_GOOD 0x0000001F
54 #define MASK_USER_GOOD 0x00401F00
56 /*********************************************************
57 Add all currently available users to another db
58 ********************************************************/
60 static int export_database (struct pdb_context
*in
, struct pdb_context
61 *out
, const char *username
) {
62 SAM_ACCOUNT
*user
= NULL
;
64 DEBUG(3, ("called with username=\"%s\"\n", username
));
66 if (NT_STATUS_IS_ERR(in
->pdb_setsampwent(in
, 0))) {
67 fprintf(stderr
, "Can't sampwent!\n");
71 if (!NT_STATUS_IS_OK(pdb_init_sam(&user
))) {
72 fprintf(stderr
, "Can't initialize new SAM_ACCOUNT!\n");
76 while (NT_STATUS_IS_OK(in
->pdb_getsampwent(in
, user
))) {
77 DEBUG(4, ("Processing account %s\n",
78 user
->private.username
));
80 (strcmp(username
, user
->private.username
)
82 out
->pdb_add_sam_account(out
, user
);
83 if (!NT_STATUS_IS_OK(pdb_reset_sam(user
))) {
85 "Can't reset SAM_ACCOUNT!\n");
91 in
->pdb_endsampwent(in
);
96 /*********************************************************
97 Add all currently available group mappings to another db
98 ********************************************************/
100 static int export_groups (struct pdb_context
*in
, struct pdb_context
*out
) {
101 GROUP_MAP
*maps
= NULL
;
104 if (NT_STATUS_IS_ERR(in
->pdb_enum_group_mapping(in
, SID_NAME_UNKNOWN
,
107 fprintf(stderr
, "Can't get group mappings!\n");
111 for (i
=0; i
<entries
; i
++) {
112 out
->pdb_add_group_mapping_entry(out
, &(maps
[i
]));
120 /*********************************************************
121 Print info from sam structure
122 **********************************************************/
124 static int print_sam_info (SAM_ACCOUNT
*sam_pwent
, BOOL verbosity
, BOOL smbpwdstyle
)
129 /* TODO: chaeck if entry is a user or a workstation */
130 if (!sam_pwent
) return -1;
133 printf ("Unix username: %s\n", pdb_get_username(sam_pwent
));
134 printf ("NT username: %s\n", pdb_get_nt_username(sam_pwent
));
135 printf ("Account Flags: %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
136 printf ("User SID: %s\n",
137 sid_string_static(pdb_get_user_sid(sam_pwent
)));
138 printf ("Primary Group SID: %s\n",
139 sid_string_static(pdb_get_group_sid(sam_pwent
)));
140 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent
));
141 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent
));
142 printf ("HomeDir Drive: %s\n", pdb_get_dir_drive(sam_pwent
));
143 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent
));
144 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent
));
145 printf ("Domain: %s\n", pdb_get_domain(sam_pwent
));
146 printf ("Account desc: %s\n", pdb_get_acct_desc(sam_pwent
));
147 printf ("Workstations: %s\n", pdb_get_workstations(sam_pwent
));
148 printf ("Munged dial: %s\n", pdb_get_munged_dial(sam_pwent
));
150 tmp
= pdb_get_logon_time(sam_pwent
);
151 printf ("Logon time: %s\n", tmp
? http_timestring(tmp
) : "0");
153 tmp
= pdb_get_logoff_time(sam_pwent
);
154 printf ("Logoff time: %s\n", tmp
? http_timestring(tmp
) : "0");
156 tmp
= pdb_get_kickoff_time(sam_pwent
);
157 printf ("Kickoff time: %s\n", tmp
? http_timestring(tmp
) : "0");
159 tmp
= pdb_get_pass_last_set_time(sam_pwent
);
160 printf ("Password last set: %s\n", tmp
? http_timestring(tmp
) : "0");
162 tmp
= pdb_get_pass_can_change_time(sam_pwent
);
163 printf ("Password can change: %s\n", tmp
? http_timestring(tmp
) : "0");
165 tmp
= pdb_get_pass_must_change_time(sam_pwent
);
166 printf ("Password must change: %s\n", tmp
? http_timestring(tmp
) : "0");
168 tmp
= pdb_get_bad_password_time(sam_pwent
);
169 printf ("Last bad password : %s\n", tmp
? http_timestring(tmp
) : "0");
170 printf ("Bad password count : %d\n",
171 pdb_get_bad_password_count(sam_pwent
));
173 } else if (smbpwdstyle
) {
177 uid
= nametouid(pdb_get_username(sam_pwent
));
178 pdb_sethexpwd(lm_passwd
, pdb_get_lanman_passwd(sam_pwent
), pdb_get_acct_ctrl(sam_pwent
));
179 pdb_sethexpwd(nt_passwd
, pdb_get_nt_passwd(sam_pwent
), pdb_get_acct_ctrl(sam_pwent
));
181 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
182 pdb_get_username(sam_pwent
),
186 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent
),NEW_PW_FORMAT_SPACE_PADDED_LEN
),
187 (uint32
)pdb_get_pass_last_set_time(sam_pwent
));
189 uid
= nametouid(pdb_get_username(sam_pwent
));
190 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent
), (unsigned long)uid
,
191 pdb_get_fullname(sam_pwent
));
197 /*********************************************************
198 Get an Print User Info
199 **********************************************************/
201 static int print_user_info (struct pdb_context
*in
, const char *username
, BOOL verbosity
, BOOL smbpwdstyle
)
203 SAM_ACCOUNT
*sam_pwent
=NULL
;
206 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent
))) {
210 ret
= NT_STATUS_IS_OK(in
->pdb_getsampwnam (in
, sam_pwent
, username
));
213 fprintf (stderr
, "Username not found!\n");
214 pdb_free_sam(&sam_pwent
);
218 ret
=print_sam_info (sam_pwent
, verbosity
, smbpwdstyle
);
219 pdb_free_sam(&sam_pwent
);
224 /*********************************************************
226 **********************************************************/
227 static int print_users_list (struct pdb_context
*in
, BOOL verbosity
, BOOL smbpwdstyle
)
229 SAM_ACCOUNT
*sam_pwent
=NULL
;
232 check
= NT_STATUS_IS_OK(in
->pdb_setsampwent(in
, False
));
238 if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent
)))) return 1;
240 while (check
&& (ret
= NT_STATUS_IS_OK(in
->pdb_getsampwent (in
, sam_pwent
)))) {
242 printf ("---------------\n");
243 print_sam_info (sam_pwent
, verbosity
, smbpwdstyle
);
244 pdb_free_sam(&sam_pwent
);
245 check
= NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent
));
247 if (check
) pdb_free_sam(&sam_pwent
);
249 in
->pdb_endsampwent(in
);
253 /*********************************************************
254 Fix a list of Users for uninitialised passwords
255 **********************************************************/
256 static int fix_users_list (struct pdb_context
*in
)
258 SAM_ACCOUNT
*sam_pwent
=NULL
;
261 check
= NT_STATUS_IS_OK(in
->pdb_setsampwent(in
, False
));
267 if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent
)))) return 1;
269 while (check
&& (ret
= NT_STATUS_IS_OK(in
->pdb_getsampwent (in
, sam_pwent
)))) {
270 printf("Updating record for user %s\n", pdb_get_username(sam_pwent
));
272 if (!pdb_update_sam_account(sam_pwent
)) {
273 printf("Update of user %s failed!\n", pdb_get_username(sam_pwent
));
275 pdb_free_sam(&sam_pwent
);
276 check
= NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent
));
278 fprintf(stderr
, "Failed to initialise new SAM_ACCOUNT structure (out of memory?)\n");
282 if (check
) pdb_free_sam(&sam_pwent
);
284 in
->pdb_endsampwent(in
);
288 /*********************************************************
290 **********************************************************/
292 static int set_user_info (struct pdb_context
*in
, const char *username
,
293 const char *fullname
, const char *homedir
,
294 const char *drive
, const char *script
,
295 const char *profile
, const char *account_control
,
296 const char *user_sid
, const char *group_sid
,
299 BOOL updated_autolock
= False
, updated_badpw
= False
;
300 SAM_ACCOUNT
*sam_pwent
=NULL
;
303 pdb_init_sam(&sam_pwent
);
305 ret
= NT_STATUS_IS_OK(in
->pdb_getsampwnam (in
, sam_pwent
, username
));
307 fprintf (stderr
, "Username not found!\n");
308 pdb_free_sam(&sam_pwent
);
312 if (!pdb_update_autolock_flag(sam_pwent
, &updated_autolock
)) {
313 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
316 if (!pdb_update_bad_password_count(sam_pwent
, &updated_badpw
)) {
317 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
321 pdb_set_fullname(sam_pwent
, fullname
, PDB_CHANGED
);
323 pdb_set_homedir(sam_pwent
, homedir
, PDB_CHANGED
);
325 pdb_set_dir_drive(sam_pwent
,drive
, PDB_CHANGED
);
327 pdb_set_logon_script(sam_pwent
, script
, PDB_CHANGED
);
329 pdb_set_profile_path (sam_pwent
, profile
, PDB_CHANGED
);
331 if (account_control
) {
332 uint16 not_settable
= ~(ACB_DISABLED
|ACB_HOMDIRREQ
|ACB_PWNOTREQ
|
333 ACB_PWNOEXP
|ACB_AUTOLOCK
);
335 uint16 newflag
= pdb_decode_acct_ctrl(account_control
);
337 if (newflag
& not_settable
) {
338 fprintf(stderr
, "Can only set [NDHLX] flags\n");
339 pdb_free_sam(&sam_pwent
);
343 pdb_set_acct_ctrl(sam_pwent
,
344 (pdb_get_acct_ctrl(sam_pwent
) & not_settable
) | newflag
,
349 if (!string_to_sid(&u_sid
, user_sid
)) {
350 /* not a complete sid, may be a RID, try building a SID */
353 if (sscanf(user_sid
, "%d", &u_rid
) != 1) {
354 fprintf(stderr
, "Error passed string is not a complete user SID or RID!\n");
357 sid_copy(&u_sid
, get_global_sam_sid());
358 sid_append_rid(&u_sid
, u_rid
);
360 pdb_set_user_sid (sam_pwent
, &u_sid
, PDB_CHANGED
);
364 if (!string_to_sid(&g_sid
, group_sid
)) {
365 /* not a complete sid, may be a RID, try building a SID */
368 if (sscanf(group_sid
, "%d", &g_rid
) != 1) {
369 fprintf(stderr
, "Error passed string is not a complete group SID or RID!\n");
372 sid_copy(&g_sid
, get_global_sam_sid());
373 sid_append_rid(&g_sid
, g_rid
);
375 pdb_set_group_sid (sam_pwent
, &g_sid
, PDB_CHANGED
);
379 pdb_set_bad_password_count(sam_pwent
, 0, PDB_CHANGED
);
380 pdb_set_bad_password_time(sam_pwent
, 0, PDB_CHANGED
);
383 if (NT_STATUS_IS_OK(in
->pdb_update_sam_account (in
, sam_pwent
)))
384 print_user_info (in
, username
, True
, False
);
386 fprintf (stderr
, "Unable to modify entry!\n");
387 pdb_free_sam(&sam_pwent
);
390 pdb_free_sam(&sam_pwent
);
394 /*********************************************************
396 **********************************************************/
397 static int new_user (struct pdb_context
*in
, const char *username
,
398 const char *fullname
, const char *homedir
,
399 const char *drive
, const char *script
,
400 const char *profile
, char *user_sid
, char *group_sid
)
402 SAM_ACCOUNT
*sam_pwent
=NULL
;
404 char *password1
, *password2
, *staticpass
;
406 get_global_sam_sid();
408 if (!NT_STATUS_IS_OK(nt_status
= pdb_init_sam_new(&sam_pwent
, username
, 0))) {
409 DEBUG(0, ("could not create account to add new user %s\n", username
));
413 staticpass
= getpass("new password:");
414 password1
= strdup(staticpass
);
415 memset(staticpass
, 0, strlen(staticpass
));
416 staticpass
= getpass("retype new password:");
417 password2
= strdup(staticpass
);
418 memset(staticpass
, 0, strlen(staticpass
));
419 if (strcmp (password1
, password2
)) {
420 fprintf (stderr
, "Passwords does not match!\n");
421 memset(password1
, 0, strlen(password1
));
422 SAFE_FREE(password1
);
423 memset(password2
, 0, strlen(password2
));
424 SAFE_FREE(password2
);
425 pdb_free_sam (&sam_pwent
);
429 pdb_set_plaintext_passwd(sam_pwent
, password1
);
430 memset(password1
, 0, strlen(password1
));
431 SAFE_FREE(password1
);
432 memset(password2
, 0, strlen(password2
));
433 SAFE_FREE(password2
);
436 pdb_set_fullname(sam_pwent
, fullname
, PDB_CHANGED
);
438 pdb_set_homedir (sam_pwent
, homedir
, PDB_CHANGED
);
440 pdb_set_dir_drive (sam_pwent
, drive
, PDB_CHANGED
);
442 pdb_set_logon_script(sam_pwent
, script
, PDB_CHANGED
);
444 pdb_set_profile_path (sam_pwent
, profile
, PDB_CHANGED
);
447 if (!string_to_sid(&u_sid
, user_sid
)) {
448 /* not a complete sid, may be a RID, try building a SID */
451 if (sscanf(user_sid
, "%d", &u_rid
) != 1) {
452 fprintf(stderr
, "Error passed string is not a complete user SID or RID!\n");
455 sid_copy(&u_sid
, get_global_sam_sid());
456 sid_append_rid(&u_sid
, u_rid
);
458 pdb_set_user_sid (sam_pwent
, &u_sid
, PDB_CHANGED
);
462 if (!string_to_sid(&g_sid
, group_sid
)) {
463 /* not a complete sid, may be a RID, try building a SID */
466 if (sscanf(group_sid
, "%d", &g_rid
) != 1) {
467 fprintf(stderr
, "Error passed string is not a complete group SID or RID!\n");
470 sid_copy(&g_sid
, get_global_sam_sid());
471 sid_append_rid(&g_sid
, g_rid
);
473 pdb_set_group_sid (sam_pwent
, &g_sid
, PDB_CHANGED
);
476 pdb_set_acct_ctrl (sam_pwent
, ACB_NORMAL
, PDB_CHANGED
);
478 if (NT_STATUS_IS_OK(in
->pdb_add_sam_account (in
, sam_pwent
))) {
479 print_user_info (in
, username
, True
, False
);
481 fprintf (stderr
, "Unable to add user! (does it already exist?)\n");
482 pdb_free_sam (&sam_pwent
);
485 pdb_free_sam (&sam_pwent
);
489 /*********************************************************
491 **********************************************************/
493 static int new_machine (struct pdb_context
*in
, const char *machine_in
)
495 SAM_ACCOUNT
*sam_pwent
=NULL
;
497 fstring machineaccount
;
498 struct passwd
*pwd
= NULL
;
500 get_global_sam_sid();
502 fstrcpy(machinename
, machine_in
);
503 machinename
[15]= '\0';
505 if (machinename
[strlen (machinename
) -1] == '$')
506 machinename
[strlen (machinename
) -1] = '\0';
508 strlower_m(machinename
);
510 fstrcpy(machineaccount
, machinename
);
511 fstrcat(machineaccount
, "$");
513 if ((pwd
= getpwnam_alloc(machineaccount
))) {
514 if (!NT_STATUS_IS_OK(pdb_init_sam_pw( &sam_pwent
, pwd
))) {
515 fprintf(stderr
, "Could not init sam from pw\n");
521 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent
))) {
522 fprintf(stderr
, "Could not init sam from pw\n");
527 pdb_set_plaintext_passwd (sam_pwent
, machinename
);
529 pdb_set_username (sam_pwent
, machineaccount
, PDB_CHANGED
);
531 pdb_set_acct_ctrl (sam_pwent
, ACB_WSTRUST
, PDB_CHANGED
);
533 pdb_set_group_sid_from_rid(sam_pwent
, DOMAIN_GROUP_RID_COMPUTERS
, PDB_CHANGED
);
535 if (NT_STATUS_IS_OK(in
->pdb_add_sam_account (in
, sam_pwent
))) {
536 print_user_info (in
, machineaccount
, True
, False
);
538 fprintf (stderr
, "Unable to add machine! (does it already exist?)\n");
539 pdb_free_sam (&sam_pwent
);
542 pdb_free_sam (&sam_pwent
);
546 /*********************************************************
548 **********************************************************/
550 static int delete_user_entry (struct pdb_context
*in
, const char *username
)
552 SAM_ACCOUNT
*samaccount
= NULL
;
554 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount
))) {
558 if (!NT_STATUS_IS_OK(in
->pdb_getsampwnam(in
, samaccount
, username
))) {
559 fprintf (stderr
, "user %s does not exist in the passdb\n", username
);
563 if (!NT_STATUS_IS_OK(in
->pdb_delete_sam_account (in
, samaccount
))) {
564 fprintf (stderr
, "Unable to delete user %s\n", username
);
570 /*********************************************************
572 **********************************************************/
574 static int delete_machine_entry (struct pdb_context
*in
, const char *machinename
)
577 SAM_ACCOUNT
*samaccount
= NULL
;
579 fstrcpy(name
, machinename
);
581 if (name
[strlen(name
)-1] != '$')
584 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount
))) {
588 if (!NT_STATUS_IS_OK(in
->pdb_getsampwnam(in
, samaccount
, name
))) {
589 fprintf (stderr
, "machine %s does not exist in the passdb\n", name
);
593 if (!NT_STATUS_IS_OK(in
->pdb_delete_sam_account (in
, samaccount
))) {
594 fprintf (stderr
, "Unable to delete machine %s\n", name
);
601 /*********************************************************
603 **********************************************************/
605 int main (int argc
, char **argv
)
607 static BOOL list_users
= False
;
608 static BOOL verbose
= False
;
609 static BOOL spstyle
= False
;
610 static BOOL machine
= False
;
611 static BOOL add_user
= False
;
612 static BOOL delete_user
= False
;
613 static BOOL modify_user
= False
;
614 uint32 setparms
, checkparms
;
616 static char *full_name
= NULL
;
617 static const char *user_name
= NULL
;
618 static char *home_dir
= NULL
;
619 static char *home_drive
= NULL
;
620 static char *backend
= NULL
;
621 static char *backend_in
= NULL
;
622 static char *backend_out
= NULL
;
623 static BOOL transfer_groups
= False
;
624 static BOOL force_initialised_password
= False
;
625 static char *logon_script
= NULL
;
626 static char *profile_path
= NULL
;
627 static char *account_control
= NULL
;
628 static char *account_policy
= NULL
;
629 static char *user_sid
= NULL
;
630 static char *group_sid
= NULL
;
631 static long int account_policy_value
= 0;
632 BOOL account_policy_value_set
= False
;
633 static BOOL badpw_reset
= False
;
635 struct pdb_context
*bin
;
636 struct pdb_context
*bout
;
637 struct pdb_context
*bdef
;
639 struct poptOption long_options
[] = {
641 {"list", 'L', POPT_ARG_NONE
, &list_users
, 0, "list all users", NULL
},
642 {"verbose", 'v', POPT_ARG_NONE
, &verbose
, 0, "be verbose", NULL
},
643 {"smbpasswd-style", 'w',POPT_ARG_NONE
, &spstyle
, 0, "give output in smbpasswd style", NULL
},
644 {"user", 'u', POPT_ARG_STRING
, &user_name
, 0, "use username", "USER" },
645 {"fullname", 'f', POPT_ARG_STRING
, &full_name
, 0, "set full name", NULL
},
646 {"homedir", 'h', POPT_ARG_STRING
, &home_dir
, 0, "set home directory", NULL
},
647 {"drive", 'D', POPT_ARG_STRING
, &home_drive
, 0, "set home drive", NULL
},
648 {"script", 'S', POPT_ARG_STRING
, &logon_script
, 0, "set logon script", NULL
},
649 {"profile", 'p', POPT_ARG_STRING
, &profile_path
, 0, "set profile path", NULL
},
650 {"user SID", 'U', POPT_ARG_STRING
, &user_sid
, 0, "set user SID or RID", NULL
},
651 {"group SID", 'G', POPT_ARG_STRING
, &group_sid
, 0, "set group SID or RID", NULL
},
652 {"create", 'a', POPT_ARG_NONE
, &add_user
, 0, "create user", NULL
},
653 {"modify", 'r', POPT_ARG_NONE
, &modify_user
, 0, "modify user", NULL
},
654 {"machine", 'm', POPT_ARG_NONE
, &machine
, 0, "account is a machine account", NULL
},
655 {"delete", 'x', POPT_ARG_NONE
, &delete_user
, 0, "delete user", NULL
},
656 {"backend", 'b', POPT_ARG_STRING
, &backend
, 0, "use different passdb backend as default backend", NULL
},
657 {"import", 'i', POPT_ARG_STRING
, &backend_in
, 0, "import user accounts from this backend", NULL
},
658 {"export", 'e', POPT_ARG_STRING
, &backend_out
, 0, "export user accounts to this backend", NULL
},
659 {"group", 'g', POPT_ARG_NONE
, &transfer_groups
, 0, "use -i and -e for groups", NULL
},
660 {"account-policy", 'P', POPT_ARG_STRING
, &account_policy
, 0,"value of an account policy (like maximum password age)",NULL
},
661 {"value", 'C', POPT_ARG_LONG
, &account_policy_value
, 'C',"set the account policy to this value", NULL
},
662 {"account-control", 'c', POPT_ARG_STRING
, &account_control
, 0, "Values of account control", NULL
},
663 {"force-initialized-passwords", 0, POPT_ARG_NONE
, &force_initialised_password
, 0, "Force initialization of corrupt password strings in a passdb backend", NULL
},
664 {"bad-password-count-reset", 'z', POPT_ARG_NONE
, &badpw_reset
, 0, "reset bad password count", NULL
},
669 setup_logging("pdbedit", True
);
671 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
,
672 POPT_CONTEXT_KEEP_FIRST
);
674 while((opt
= poptGetNextOpt(pc
)) != -1) {
677 account_policy_value_set
= True
;
682 poptGetArg(pc
); /* Drop argv[0], the program name */
684 if (user_name
== NULL
)
685 user_name
= poptGetArg(pc
);
687 if (!lp_load(dyn_CONFIGFILE
,True
,False
,False
)) {
688 fprintf(stderr
, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE
);
692 if(!initialize_password_db(False
))
698 setparms
= (backend
? BIT_BACKEND
: 0) +
699 (verbose
? BIT_VERBOSE
: 0) +
700 (spstyle
? BIT_SPSTYLE
: 0) +
701 (full_name
? BIT_FULLNAME
: 0) +
702 (home_dir
? BIT_HOMEDIR
: 0) +
703 (home_drive
? BIT_HDIRDRIVE
: 0) +
704 (logon_script
? BIT_LOGSCRIPT
: 0) +
705 (profile_path
? BIT_PROFILE
: 0) +
706 (machine
? BIT_MACHINE
: 0) +
707 (user_name
? BIT_USER
: 0) +
708 (list_users
? BIT_LIST
: 0) +
709 (force_initialised_password
? BIT_FIX_INIT
: 0) +
710 (modify_user
? BIT_MODIFY
: 0) +
711 (add_user
? BIT_CREATE
: 0) +
712 (delete_user
? BIT_DELETE
: 0) +
713 (account_control
? BIT_ACCTCTRL
: 0) +
714 (account_policy
? BIT_ACCPOLICY
: 0) +
715 (account_policy_value_set
? BIT_ACCPOLVAL
: 0) +
716 (backend_in
? BIT_IMPORT
: 0) +
717 (backend_out
? BIT_EXPORT
: 0) +
718 (badpw_reset
? BIT_BADPWRESET
: 0);
720 if (setparms
& BIT_BACKEND
) {
721 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bdef
, backend
))) {
722 fprintf(stderr
, "Can't initialize passdb backend.\n");
726 if (!NT_STATUS_IS_OK(make_pdb_context_list(&bdef
, lp_passdb_backend()))) {
727 fprintf(stderr
, "Can't initialize passdb backend.\n");
732 /* the lowest bit options are always accepted */
733 checkparms
= setparms
& ~MASK_ALWAYS_GOOD
;
735 if (checkparms
& BIT_FIX_INIT
) {
736 return fix_users_list(bdef
);
739 /* account policy operations */
740 if ((checkparms
& BIT_ACCPOLICY
) && !(checkparms
& ~(BIT_ACCPOLICY
+ BIT_ACCPOLVAL
))) {
742 int field
= account_policy_name_to_fieldnum(account_policy
);
744 char *apn
= account_policy_names_list();
745 fprintf(stderr
, "No account policy by that name\n");
747 fprintf(stderr
, "Account policy names are :\n%s\n", apn
);
752 if (!account_policy_get(field
, &value
)) {
753 fprintf(stderr
, "valid account policy, but unable to fetch value!\n");
756 if (account_policy_value_set
) {
757 printf("account policy value for %s was %u\n", account_policy
, value
);
758 if (!account_policy_set(field
, account_policy_value
)) {
759 fprintf(stderr
, "valid account policy, but unable to set value!\n");
762 printf("account policy value for %s is now %lu\n", account_policy
, account_policy_value
);
765 printf("account policy value for %s is %u\n", account_policy
, value
);
770 /* import and export operations */
771 if (((checkparms
& BIT_IMPORT
) || (checkparms
& BIT_EXPORT
))
772 && !(checkparms
& ~(BIT_IMPORT
+BIT_EXPORT
+BIT_USER
))) {
774 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bin
, backend_in
))) {
775 fprintf(stderr
, "Can't initialize passdb backend.\n");
782 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bout
, backend_out
))) {
783 fprintf(stderr
, "Can't initialize %s.\n", backend_out
);
789 if (transfer_groups
) {
790 if (!(checkparms
& BIT_USER
))
791 return export_groups(bin
, bout
);
793 if (checkparms
& BIT_USER
)
794 return export_database(bin
, bout
,
797 return export_database(bin
, bout
,
802 /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
803 /* fake up BIT_LIST if only BIT_USER is defined */
804 if ((checkparms
& BIT_USER
) && !(checkparms
& ~BIT_USER
)) {
805 checkparms
+= BIT_LIST
;
808 /* modify flag is optional to maintain backwards compatibility */
809 /* fake up BIT_MODIFY if BIT_USER and at least one of MASK_USER_GOOD is defined */
810 if (!((checkparms
& ~MASK_USER_GOOD
) & ~BIT_USER
) && (checkparms
& MASK_USER_GOOD
)) {
811 checkparms
+= BIT_MODIFY
;
814 /* list users operations */
815 if (checkparms
& BIT_LIST
) {
816 if (!(checkparms
& ~BIT_LIST
)) {
817 return print_users_list (bdef
, verbose
, spstyle
);
819 if (!(checkparms
& ~(BIT_USER
+ BIT_LIST
))) {
820 return print_user_info (bdef
, user_name
, verbose
, spstyle
);
824 /* mask out users options */
825 checkparms
&= ~MASK_USER_GOOD
;
827 /* if bad password count is reset, we must be modifying */
828 if (checkparms
& BIT_BADPWRESET
) {
829 checkparms
|= BIT_MODIFY
;
830 checkparms
&= ~BIT_BADPWRESET
;
833 /* account operation */
834 if ((checkparms
& BIT_CREATE
) || (checkparms
& BIT_MODIFY
) || (checkparms
& BIT_DELETE
)) {
835 /* check use of -u option */
836 if (!(checkparms
& BIT_USER
)) {
837 fprintf (stderr
, "Username not specified! (use -u option)\n");
841 /* account creation operations */
842 if (!(checkparms
& ~(BIT_CREATE
+ BIT_USER
+ BIT_MACHINE
))) {
843 if (checkparms
& BIT_MACHINE
) {
844 return new_machine (bdef
, user_name
);
846 return new_user (bdef
, user_name
, full_name
, home_dir
,
847 home_drive
, logon_script
,
848 profile_path
, user_sid
, group_sid
);
852 /* account deletion operations */
853 if (!(checkparms
& ~(BIT_DELETE
+ BIT_USER
+ BIT_MACHINE
))) {
854 if (checkparms
& BIT_MACHINE
) {
855 return delete_machine_entry (bdef
, user_name
);
857 return delete_user_entry (bdef
, user_name
);
861 /* account modification operations */
862 if (!(checkparms
& ~(BIT_MODIFY
+ BIT_USER
))) {
863 return set_user_info (bdef
, user_name
, full_name
,
867 profile_path
, account_control
,
873 if (setparms
>= 0x20) {
874 fprintf (stderr
, "Incompatible or insufficient options on command line!\n");
876 poptPrintHelp(pc
, stderr
, 0);