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_CAN_CHANGE 0x00000020
30 #define BIT_MUST_CHANGE 0x00000040
31 #define BIT_USERSIDS 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_USERDOMAIN 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
52 #define BIT_LOGONHOURS 0x10000000
54 #define MASK_ALWAYS_GOOD 0x0000001F
55 #define MASK_USER_GOOD 0x00405FE0
57 /*********************************************************
58 Reset account policies to their default values and remove marker
59 ********************************************************/
61 static int reinit_account_policies (void)
65 for (i=1; decode_account_policy_name(i) != NULL; i++) {
67 if (!account_policy_get_default(i, &policy_value)) {
68 fprintf(stderr, "Can't get default account policy\n");
71 if (!account_policy_set(i, policy_value)) {
72 fprintf(stderr, "Can't set account policy in tdb\n");
77 if (!remove_account_policy_migrated()) {
78 fprintf(stderr, "Can't remove marker from tdb\n");
85 /*********************************************************
86 Print info from sam structure
87 **********************************************************/
89 static int print_sam_info (struct samu *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
94 /* TODO: chaeck if entry is a user or a workstation */
95 if (!sam_pwent) return -1;
101 printf ("Unix username: %s\n", pdb_get_username(sam_pwent));
102 printf ("NT username: %s\n", pdb_get_nt_username(sam_pwent));
103 printf ("Account Flags: %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
104 printf ("User SID: %s\n",
105 sid_string_static(pdb_get_user_sid(sam_pwent)));
106 printf ("Primary Group SID: %s\n",
107 sid_string_static(pdb_get_group_sid(sam_pwent)));
108 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent));
109 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
110 printf ("HomeDir Drive: %s\n", pdb_get_dir_drive(sam_pwent));
111 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent));
112 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent));
113 printf ("Domain: %s\n", pdb_get_domain(sam_pwent));
114 printf ("Account desc: %s\n", pdb_get_acct_desc(sam_pwent));
115 printf ("Workstations: %s\n", pdb_get_workstations(sam_pwent));
116 printf ("Munged dial: %s\n", pdb_get_munged_dial(sam_pwent));
118 tmp = pdb_get_logon_time(sam_pwent);
119 printf ("Logon time: %s\n", tmp ? http_timestring(tmp) : "0");
121 tmp = pdb_get_logoff_time(sam_pwent);
122 printf ("Logoff time: %s\n", tmp ? http_timestring(tmp) : "0");
124 tmp = pdb_get_kickoff_time(sam_pwent);
125 printf ("Kickoff time: %s\n", tmp ? http_timestring(tmp) : "0");
127 tmp = pdb_get_pass_last_set_time(sam_pwent);
128 printf ("Password last set: %s\n", tmp ? http_timestring(tmp) : "0");
130 tmp = pdb_get_pass_can_change_time(sam_pwent);
131 printf ("Password can change: %s\n", tmp ? http_timestring(tmp) : "0");
133 tmp = pdb_get_pass_must_change_time(sam_pwent);
134 printf ("Password must change: %s\n", tmp ? http_timestring(tmp) : "0");
136 tmp = pdb_get_bad_password_time(sam_pwent);
137 printf ("Last bad password : %s\n", tmp ? http_timestring(tmp) : "0");
138 printf ("Bad password count : %d\n",
139 pdb_get_bad_password_count(sam_pwent));
141 hours = pdb_get_hours(sam_pwent);
142 pdb_sethexhours(temp, hours);
143 printf ("Logon hours : %s\n", temp);
145 } else if (smbpwdstyle) {
149 uid = nametouid(pdb_get_username(sam_pwent));
150 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
151 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
153 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
154 pdb_get_username(sam_pwent),
158 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
159 (uint32)pdb_get_pass_last_set_time(sam_pwent));
161 uid = nametouid(pdb_get_username(sam_pwent));
162 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid,
163 pdb_get_fullname(sam_pwent));
169 /*********************************************************
170 Get an Print User Info
171 **********************************************************/
173 static int print_user_info (struct pdb_methods *in, const char *username, BOOL verbosity, BOOL smbpwdstyle)
175 struct samu *sam_pwent=NULL;
178 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
182 ret = NT_STATUS_IS_OK(in->getsampwnam (in, sam_pwent, username));
185 fprintf (stderr, "Username not found!\n");
186 TALLOC_FREE(sam_pwent);
190 ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
191 TALLOC_FREE(sam_pwent);
196 /*********************************************************
198 **********************************************************/
199 static int print_users_list (struct pdb_methods *in, BOOL verbosity, BOOL smbpwdstyle)
201 struct samu *sam_pwent=NULL;
204 check = NT_STATUS_IS_OK(in->setsampwent(in, False, 0));
210 if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
212 while (check && NT_STATUS_IS_OK(in->getsampwent (in, sam_pwent))) {
214 printf ("---------------\n");
215 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
216 TALLOC_FREE(sam_pwent);
217 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
219 if (check) TALLOC_FREE(sam_pwent);
225 /*********************************************************
226 Fix a list of Users for uninitialised passwords
227 **********************************************************/
228 static int fix_users_list (struct pdb_methods *in)
230 struct samu *sam_pwent=NULL;
233 check = NT_STATUS_IS_OK(in->setsampwent(in, False, 0));
239 if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
241 while (check && NT_STATUS_IS_OK(in->getsampwent (in, sam_pwent))) {
242 printf("Updating record for user %s\n", pdb_get_username(sam_pwent));
244 if (!NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
245 printf("Update of user %s failed!\n", pdb_get_username(sam_pwent));
247 TALLOC_FREE(sam_pwent);
248 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
250 fprintf(stderr, "Failed to initialise new struct samu structure (out of memory?)\n");
254 if (check) TALLOC_FREE(sam_pwent);
260 /*********************************************************
262 **********************************************************/
264 static int set_user_info (struct pdb_methods *in, const char *username,
265 const char *fullname, const char *homedir,
266 const char *acct_desc,
267 const char *drive, const char *script,
268 const char *profile, const char *account_control,
269 const char *user_sid, const char *group_sid,
270 const char *user_domain,
271 const BOOL badpw, const BOOL hours,
272 time_t pwd_can_change, time_t pwd_must_change)
274 BOOL updated_autolock = False, updated_badpw = False;
275 struct samu *sam_pwent=NULL;
278 pdb_init_sam(&sam_pwent);
280 ret = NT_STATUS_IS_OK(in->getsampwnam (in, sam_pwent, username));
282 fprintf (stderr, "Username not found!\n");
283 TALLOC_FREE(sam_pwent);
288 uint8 hours_array[MAX_HOURS_LEN];
291 hours_len = pdb_get_hours_len(sam_pwent);
292 memset(hours_array, 0xff, hours_len);
294 pdb_set_hours(sam_pwent, hours_array, PDB_CHANGED);
297 if (pwd_can_change != -1) {
298 pdb_set_pass_can_change_time(sam_pwent, pwd_can_change, PDB_CHANGED);
301 if (pwd_must_change != -1) {
302 pdb_set_pass_must_change_time(sam_pwent, pwd_must_change, PDB_CHANGED);
305 if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) {
306 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
309 if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) {
310 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
314 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
316 pdb_set_acct_desc(sam_pwent, acct_desc, PDB_CHANGED);
318 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
320 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
322 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
324 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
326 pdb_set_domain(sam_pwent, user_domain, PDB_CHANGED);
328 if (account_control) {
329 uint16 not_settable = ~(ACB_DISABLED|ACB_HOMDIRREQ|ACB_PWNOTREQ|
330 ACB_PWNOEXP|ACB_AUTOLOCK);
332 uint16 newflag = pdb_decode_acct_ctrl(account_control);
334 if (newflag & not_settable) {
335 fprintf(stderr, "Can only set [NDHLX] flags\n");
336 TALLOC_FREE(sam_pwent);
340 pdb_set_acct_ctrl(sam_pwent,
341 (pdb_get_acct_ctrl(sam_pwent) & not_settable) | newflag,
346 if (!string_to_sid(&u_sid, user_sid)) {
347 /* not a complete sid, may be a RID, try building a SID */
350 if (sscanf(user_sid, "%d", &u_rid) != 1) {
351 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
354 sid_copy(&u_sid, get_global_sam_sid());
355 sid_append_rid(&u_sid, u_rid);
357 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
361 if (!string_to_sid(&g_sid, group_sid)) {
362 /* not a complete sid, may be a RID, try building a SID */
365 if (sscanf(group_sid, "%d", &g_rid) != 1) {
366 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
369 sid_copy(&g_sid, get_global_sam_sid());
370 sid_append_rid(&g_sid, g_rid);
372 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
376 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
377 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
380 if (NT_STATUS_IS_OK(in->update_sam_account (in, sam_pwent)))
381 print_user_info (in, username, True, False);
383 fprintf (stderr, "Unable to modify entry!\n");
384 TALLOC_FREE(sam_pwent);
387 TALLOC_FREE(sam_pwent);
391 /*********************************************************
393 **********************************************************/
394 static int new_user (struct pdb_methods *in, const char *username,
395 const char *fullname, const char *homedir,
396 const char *drive, const char *script,
397 const char *profile, char *user_sid, char *group_sid,
400 struct samu *sam_pwent=NULL;
402 char *password1, *password2;
405 get_global_sam_sid();
407 if (!NT_STATUS_IS_OK(pdb_init_sam_new(&sam_pwent, username))) {
408 DEBUG(0, ("could not create account to add new user %s\n", username));
412 password1 = get_pass( "new password:", stdin_get);
413 password2 = get_pass( "retype new password:", stdin_get);
414 if ((rc_pwd_cmp = strcmp (password1, password2))) {
415 fprintf (stderr, "Passwords do not match!\n");
416 TALLOC_FREE(sam_pwent);
418 pdb_set_plaintext_passwd(sam_pwent, password1);
421 memset(password1, 0, strlen(password1));
422 SAFE_FREE(password1);
423 memset(password2, 0, strlen(password2));
424 SAFE_FREE(password2);
426 /* pwds do _not_ match? */
431 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
433 pdb_set_homedir (sam_pwent, homedir, PDB_CHANGED);
435 pdb_set_dir_drive (sam_pwent, drive, PDB_CHANGED);
437 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
439 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
442 if (!string_to_sid(&u_sid, user_sid)) {
443 /* not a complete sid, may be a RID, try building a SID */
446 if (sscanf(user_sid, "%d", &u_rid) != 1) {
447 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
450 sid_copy(&u_sid, get_global_sam_sid());
451 sid_append_rid(&u_sid, u_rid);
453 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
457 if (!string_to_sid(&g_sid, group_sid)) {
458 /* not a complete sid, may be a RID, try building a SID */
461 if (sscanf(group_sid, "%d", &g_rid) != 1) {
462 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
465 sid_copy(&g_sid, get_global_sam_sid());
466 sid_append_rid(&g_sid, g_rid);
468 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
471 pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL, PDB_CHANGED);
473 if (NT_STATUS_IS_OK(in->add_sam_account (in, sam_pwent))) {
474 print_user_info (in, username, True, False);
476 fprintf (stderr, "Unable to add user! (does it already exist?)\n");
477 TALLOC_FREE(sam_pwent);
480 TALLOC_FREE(sam_pwent);
484 /*********************************************************
486 **********************************************************/
488 static int new_machine (struct pdb_methods *in, const char *machine_in)
490 struct samu *sam_pwent=NULL;
492 fstring machineaccount;
493 struct passwd *pwd = NULL;
495 get_global_sam_sid();
497 fstrcpy(machinename, machine_in);
498 machinename[15]= '\0';
500 if (machinename[strlen (machinename) -1] == '$')
501 machinename[strlen (machinename) -1] = '\0';
503 strlower_m(machinename);
505 fstrcpy(machineaccount, machinename);
506 fstrcat(machineaccount, "$");
508 if ((pwd = getpwnam_alloc(NULL, machineaccount))) {
509 if (!NT_STATUS_IS_OK(pdb_init_sam_pw( &sam_pwent, pwd))) {
510 fprintf(stderr, "Could not init sam from pw\n");
516 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
517 fprintf(stderr, "Could not init sam from pw\n");
522 pdb_set_plaintext_passwd (sam_pwent, machinename);
524 pdb_set_username (sam_pwent, machineaccount, PDB_CHANGED);
526 pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST, PDB_CHANGED);
528 pdb_set_group_sid_from_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS, PDB_CHANGED);
530 if (NT_STATUS_IS_OK(in->add_sam_account (in, sam_pwent))) {
531 print_user_info (in, machineaccount, True, False);
533 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
534 TALLOC_FREE(sam_pwent);
537 TALLOC_FREE(sam_pwent);
541 /*********************************************************
543 **********************************************************/
545 static int delete_user_entry (struct pdb_methods *in, const char *username)
547 struct samu *samaccount = NULL;
549 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
553 if (!NT_STATUS_IS_OK(in->getsampwnam(in, samaccount, username))) {
554 fprintf (stderr, "user %s does not exist in the passdb\n", username);
558 if (!NT_STATUS_IS_OK(in->delete_sam_account (in, samaccount))) {
559 fprintf (stderr, "Unable to delete user %s\n", username);
565 /*********************************************************
567 **********************************************************/
569 static int delete_machine_entry (struct pdb_methods *in, const char *machinename)
572 struct samu *samaccount = NULL;
574 fstrcpy(name, machinename);
576 if (name[strlen(name)-1] != '$')
579 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
583 if (!NT_STATUS_IS_OK(in->getsampwnam(in, samaccount, name))) {
584 fprintf (stderr, "machine %s does not exist in the passdb\n", name);
588 if (!NT_STATUS_IS_OK(in->delete_sam_account (in, samaccount))) {
589 fprintf (stderr, "Unable to delete machine %s\n", name);
596 /*********************************************************
598 **********************************************************/
600 int main (int argc, char **argv)
602 static BOOL list_users = False;
603 static BOOL verbose = False;
604 static BOOL spstyle = False;
605 static BOOL machine = False;
606 static BOOL add_user = False;
607 static BOOL delete_user = False;
608 static BOOL modify_user = False;
609 uint32 setparms, checkparms;
611 static char *full_name = NULL;
612 static char *acct_desc = NULL;
613 static const char *user_name = NULL;
614 static char *home_dir = NULL;
615 static char *home_drive = NULL;
616 static char *backend = NULL;
617 static char *backend_in = NULL;
618 static char *backend_out = NULL;
619 static BOOL transfer_groups = False;
620 static BOOL transfer_account_policies = False;
621 static BOOL reset_account_policies = False;
622 static BOOL force_initialised_password = False;
623 static char *logon_script = NULL;
624 static char *profile_path = NULL;
625 static char *user_domain = NULL;
626 static char *account_control = NULL;
627 static char *account_policy = NULL;
628 static char *user_sid = NULL;
629 static char *group_sid = NULL;
630 static long int account_policy_value = 0;
631 BOOL account_policy_value_set = False;
632 static BOOL badpw_reset = False;
633 static BOOL hours_reset = False;
634 static char *pwd_can_change_time = NULL;
635 static char *pwd_must_change_time = NULL;
636 static char *pwd_time_format = NULL;
637 BOOL pw_from_stdin = False;
639 struct pdb_methods *bdef = NULL;
641 struct poptOption long_options[] = {
643 {"list", 'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
644 {"verbose", 'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
645 {"smbpasswd-style", 'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
646 {"user", 'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
647 {"account-desc", 'N', POPT_ARG_STRING, &acct_desc, 0, "set account description", NULL},
648 {"fullname", 'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
649 {"homedir", 'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
650 {"drive", 'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
651 {"script", 'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
652 {"profile", 'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
653 {"domain", 'I', POPT_ARG_STRING, &user_domain, 0, "set a users' domain", NULL},
654 {"user SID", 'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
655 {"group SID", 'G', POPT_ARG_STRING, &group_sid, 0, "set group SID or RID", NULL},
656 {"create", 'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
657 {"modify", 'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
658 {"machine", 'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
659 {"delete", 'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
660 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
661 {"import", 'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
662 {"export", 'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
663 {"group", 'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
664 {"policies", 'y', POPT_ARG_NONE, &transfer_account_policies, 0, "use -i and -e to move account policies between backends", NULL},
665 {"policies-reset", 0, POPT_ARG_NONE, &reset_account_policies, 0, "restore default policies", NULL},
666 {"account-policy", 'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
667 {"value", 'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
668 {"account-control", 'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
669 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
670 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
671 {"logon-hours-reset", 'Z', POPT_ARG_NONE, &hours_reset, 0, "reset logon hours", NULL},
672 {"pwd-can-change-time", 0, POPT_ARG_STRING, &pwd_can_change_time, 0, "Set password can change time (unix time in seconds since 1970 if time format not provided)", NULL },
673 {"pwd-must-change-time", 0, POPT_ARG_STRING, &pwd_must_change_time, 0, "Set password must change time (unix time in seconds since 1970 if time format not provided)", NULL },
674 {"time-format", 0, POPT_ARG_STRING, &pwd_time_format, 0, "The time format for time parameters", NULL },
675 {"password-from-stdin", 't', POPT_ARG_NONE, &pw_from_stdin, 0, "get password from standard in", NULL},
682 setup_logging("pdbedit", True);
684 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
685 POPT_CONTEXT_KEEP_FIRST);
687 while((opt = poptGetNextOpt(pc)) != -1) {
690 account_policy_value_set = True;
695 poptGetArg(pc); /* Drop argv[0], the program name */
697 if (user_name == NULL)
698 user_name = poptGetArg(pc);
700 if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
701 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
705 if(!initialize_password_db(False))
711 setparms = (backend ? BIT_BACKEND : 0) +
712 (verbose ? BIT_VERBOSE : 0) +
713 (spstyle ? BIT_SPSTYLE : 0) +
714 (full_name ? BIT_FULLNAME : 0) +
715 (home_dir ? BIT_HOMEDIR : 0) +
716 (home_drive ? BIT_HDIRDRIVE : 0) +
717 (logon_script ? BIT_LOGSCRIPT : 0) +
718 (profile_path ? BIT_PROFILE : 0) +
719 (user_domain ? BIT_USERDOMAIN : 0) +
720 (machine ? BIT_MACHINE : 0) +
721 (user_name ? BIT_USER : 0) +
722 (list_users ? BIT_LIST : 0) +
723 (force_initialised_password ? BIT_FIX_INIT : 0) +
724 (user_sid ? BIT_USERSIDS : 0) +
725 (group_sid ? BIT_USERSIDS : 0) +
726 (modify_user ? BIT_MODIFY : 0) +
727 (add_user ? BIT_CREATE : 0) +
728 (delete_user ? BIT_DELETE : 0) +
729 (account_control ? BIT_ACCTCTRL : 0) +
730 (account_policy ? BIT_ACCPOLICY : 0) +
731 (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
732 (backend_in ? BIT_IMPORT : 0) +
733 (backend_out ? BIT_EXPORT : 0) +
734 (badpw_reset ? BIT_BADPWRESET : 0) +
735 (hours_reset ? BIT_LOGONHOURS : 0) +
736 (pwd_can_change_time ? BIT_CAN_CHANGE: 0) +
737 (pwd_must_change_time ? BIT_MUST_CHANGE: 0);
739 if (setparms & BIT_BACKEND) {
740 if (!NT_STATUS_IS_OK(make_pdb_method_name( &bdef, backend ))) {
741 fprintf(stderr, "Can't initialize passdb backend.\n");
745 if (!NT_STATUS_IS_OK(make_pdb_method_name(&bdef, lp_passdb_backend()))) {
746 fprintf(stderr, "Can't initialize passdb backend.\n");
751 /* the lowest bit options are always accepted */
752 checkparms = setparms & ~MASK_ALWAYS_GOOD;
754 if (checkparms & BIT_FIX_INIT) {
755 return fix_users_list(bdef);
758 /* account policy operations */
759 if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
761 int field = account_policy_name_to_fieldnum(account_policy);
763 char *apn = account_policy_names_list();
764 fprintf(stderr, "No account policy by that name\n");
766 fprintf(stderr, "Account policy names are :\n%s\n", apn);
771 if (!pdb_get_account_policy(field, &value)) {
772 fprintf(stderr, "valid account policy, but unable to fetch value!\n");
773 if (!account_policy_value_set)
776 printf("account policy \"%s\" description: %s\n", account_policy, account_policy_get_desc(field));
777 if (account_policy_value_set) {
778 printf("account policy \"%s\" value was: %u\n", account_policy, value);
779 if (!pdb_set_account_policy(field, account_policy_value)) {
780 fprintf(stderr, "valid account policy, but unable to set value!\n");
783 printf("account policy \"%s\" value is now: %lu\n", account_policy, account_policy_value);
786 printf("account policy \"%s\" value is: %u\n", account_policy, value);
791 if (reset_account_policies) {
792 if (!reinit_account_policies()) {
799 /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
800 /* fake up BIT_LIST if only BIT_USER is defined */
801 if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
802 checkparms += BIT_LIST;
805 /* modify flag is optional to maintain backwards compatibility */
806 /* fake up BIT_MODIFY if BIT_USER and at least one of MASK_USER_GOOD is defined */
807 if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
808 checkparms += BIT_MODIFY;
811 /* list users operations */
812 if (checkparms & BIT_LIST) {
813 if (!(checkparms & ~BIT_LIST)) {
814 return print_users_list (bdef, verbose, spstyle);
816 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
817 return print_user_info (bdef, user_name, verbose, spstyle);
821 /* mask out users options */
822 checkparms &= ~MASK_USER_GOOD;
824 /* if bad password count is reset, we must be modifying */
825 if (checkparms & BIT_BADPWRESET) {
826 checkparms |= BIT_MODIFY;
827 checkparms &= ~BIT_BADPWRESET;
830 /* if logon hours is reset, must modify */
831 if (checkparms & BIT_LOGONHOURS) {
832 checkparms |= BIT_MODIFY;
833 checkparms &= ~BIT_LOGONHOURS;
836 /* account operation */
837 if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
838 /* check use of -u option */
839 if (!(checkparms & BIT_USER)) {
840 fprintf (stderr, "Username not specified! (use -u option)\n");
844 /* account creation operations */
845 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
846 if (checkparms & BIT_MACHINE) {
847 return new_machine (bdef, user_name);
849 return new_user (bdef, user_name, full_name, home_dir,
850 home_drive, logon_script,
851 profile_path, user_sid, group_sid,
856 /* account deletion operations */
857 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
858 if (checkparms & BIT_MACHINE) {
859 return delete_machine_entry (bdef, user_name);
861 return delete_user_entry (bdef, user_name);
865 /* account modification operations */
866 if (!(checkparms & ~(BIT_MODIFY + BIT_USER))) {
867 time_t pwd_can_change = -1;
868 time_t pwd_must_change = -1;
871 if (pwd_can_change_time) {
873 if (pwd_time_format) {
877 memset(&tm, 0, sizeof(struct tm));
878 ret = strptime(pwd_can_change_time, pwd_time_format, &tm);
879 if (ret == NULL || *ret != '\0') {
883 pwd_can_change = mktime(&tm);
885 if (pwd_can_change == -1) {
888 } else { /* assume it is unix time */
890 pwd_can_change = strtol(pwd_can_change_time, NULL, 10);
896 if (pwd_must_change_time) {
898 if (pwd_time_format) {
902 memset(&tm, 0, sizeof(struct tm));
903 ret = strptime(pwd_must_change_time, pwd_time_format, &tm);
904 if (ret == NULL || *ret != '\0') {
908 pwd_must_change = mktime(&tm);
910 if (pwd_must_change == -1) {
913 } else { /* assume it is unix time */
915 pwd_must_change = strtol(pwd_must_change_time, NULL, 10);
921 return set_user_info (bdef, user_name, full_name,
926 profile_path, account_control,
929 badpw_reset, hours_reset,
930 pwd_can_change, pwd_must_change);
932 fprintf (stderr, "Error parsing the time in pwd-%s-change-time!\n", errstr);
937 if (setparms >= 0x20) {
938 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
940 poptPrintHelp(pc, stderr, 0);