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_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
52 #define BIT_LOGONHOURS 0x10000000
54 #define MASK_ALWAYS_GOOD 0x0000001F
55 #define MASK_USER_GOOD 0x00401F60
57 /*********************************************************
58 Add all currently available users to another db
59 ********************************************************/
61 static int export_database (struct pdb_context
*in
, struct pdb_context
62 *out
, const char *username
) {
63 SAM_ACCOUNT
*user
= NULL
;
65 DEBUG(3, ("called with username=\"%s\"\n", username
));
67 if (NT_STATUS_IS_ERR(in
->pdb_setsampwent(in
, 0, 0))) {
68 fprintf(stderr
, "Can't sampwent!\n");
72 if (!NT_STATUS_IS_OK(pdb_init_sam(&user
))) {
73 fprintf(stderr
, "Can't initialize new SAM_ACCOUNT!\n");
77 while (NT_STATUS_IS_OK(in
->pdb_getsampwent(in
, user
))) {
78 DEBUG(4, ("Processing account %s\n",
79 user
->private.username
));
81 (strcmp(username
, user
->private.username
)
83 out
->pdb_add_sam_account(out
, user
);
84 if (!NT_STATUS_IS_OK(pdb_reset_sam(user
))) {
86 "Can't reset SAM_ACCOUNT!\n");
92 in
->pdb_endsampwent(in
);
97 /*********************************************************
98 Add all currently available group mappings to another db
99 ********************************************************/
101 static int export_groups (struct pdb_context
*in
, struct pdb_context
*out
) {
102 GROUP_MAP
*maps
= NULL
;
105 if (NT_STATUS_IS_ERR(in
->pdb_enum_group_mapping(in
, SID_NAME_UNKNOWN
,
108 fprintf(stderr
, "Can't get group mappings!\n");
112 for (i
=0; i
<entries
; i
++) {
113 out
->pdb_add_group_mapping_entry(out
, &(maps
[i
]));
121 /*********************************************************
122 Print info from sam structure
123 **********************************************************/
125 static int print_sam_info (SAM_ACCOUNT
*sam_pwent
, BOOL verbosity
, BOOL smbpwdstyle
)
130 /* TODO: chaeck if entry is a user or a workstation */
131 if (!sam_pwent
) return -1;
137 printf ("Unix username: %s\n", pdb_get_username(sam_pwent
));
138 printf ("NT username: %s\n", pdb_get_nt_username(sam_pwent
));
139 printf ("Account Flags: %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent
), NEW_PW_FORMAT_SPACE_PADDED_LEN
));
140 printf ("User SID: %s\n",
141 sid_string_static(pdb_get_user_sid(sam_pwent
)));
142 printf ("Primary Group SID: %s\n",
143 sid_string_static(pdb_get_group_sid(sam_pwent
)));
144 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent
));
145 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent
));
146 printf ("HomeDir Drive: %s\n", pdb_get_dir_drive(sam_pwent
));
147 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent
));
148 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent
));
149 printf ("Domain: %s\n", pdb_get_domain(sam_pwent
));
150 printf ("Account desc: %s\n", pdb_get_acct_desc(sam_pwent
));
151 printf ("Workstations: %s\n", pdb_get_workstations(sam_pwent
));
152 printf ("Munged dial: %s\n", pdb_get_munged_dial(sam_pwent
));
154 tmp
= pdb_get_logon_time(sam_pwent
);
155 printf ("Logon time: %s\n", tmp
? http_timestring(tmp
) : "0");
157 tmp
= pdb_get_logoff_time(sam_pwent
);
158 printf ("Logoff time: %s\n", tmp
? http_timestring(tmp
) : "0");
160 tmp
= pdb_get_kickoff_time(sam_pwent
);
161 printf ("Kickoff time: %s\n", tmp
? http_timestring(tmp
) : "0");
163 tmp
= pdb_get_pass_last_set_time(sam_pwent
);
164 printf ("Password last set: %s\n", tmp
? http_timestring(tmp
) : "0");
166 tmp
= pdb_get_pass_can_change_time(sam_pwent
);
167 printf ("Password can change: %s\n", tmp
? http_timestring(tmp
) : "0");
169 tmp
= pdb_get_pass_must_change_time(sam_pwent
);
170 printf ("Password must change: %s\n", tmp
? http_timestring(tmp
) : "0");
172 tmp
= pdb_get_bad_password_time(sam_pwent
);
173 printf ("Last bad password : %s\n", tmp
? http_timestring(tmp
) : "0");
174 printf ("Bad password count : %d\n",
175 pdb_get_bad_password_count(sam_pwent
));
177 hours
= pdb_get_hours(sam_pwent
);
178 pdb_sethexhours(temp
, (const char *)hours
);
179 printf ("Logon hours : %s\n", temp
);
181 } else if (smbpwdstyle
) {
185 uid
= nametouid(pdb_get_username(sam_pwent
));
186 pdb_sethexpwd(lm_passwd
, pdb_get_lanman_passwd(sam_pwent
), pdb_get_acct_ctrl(sam_pwent
));
187 pdb_sethexpwd(nt_passwd
, pdb_get_nt_passwd(sam_pwent
), pdb_get_acct_ctrl(sam_pwent
));
189 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
190 pdb_get_username(sam_pwent
),
194 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent
),NEW_PW_FORMAT_SPACE_PADDED_LEN
),
195 (uint32
)pdb_get_pass_last_set_time(sam_pwent
));
197 uid
= nametouid(pdb_get_username(sam_pwent
));
198 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent
), (unsigned long)uid
,
199 pdb_get_fullname(sam_pwent
));
205 /*********************************************************
206 Get an Print User Info
207 **********************************************************/
209 static int print_user_info (struct pdb_context
*in
, const char *username
, BOOL verbosity
, BOOL smbpwdstyle
)
211 SAM_ACCOUNT
*sam_pwent
=NULL
;
214 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent
))) {
218 ret
= NT_STATUS_IS_OK(in
->pdb_getsampwnam (in
, sam_pwent
, username
));
221 fprintf (stderr
, "Username not found!\n");
222 pdb_free_sam(&sam_pwent
);
226 ret
=print_sam_info (sam_pwent
, verbosity
, smbpwdstyle
);
227 pdb_free_sam(&sam_pwent
);
232 /*********************************************************
234 **********************************************************/
235 static int print_users_list (struct pdb_context
*in
, BOOL verbosity
, BOOL smbpwdstyle
)
237 SAM_ACCOUNT
*sam_pwent
=NULL
;
240 check
= NT_STATUS_IS_OK(in
->pdb_setsampwent(in
, False
, 0));
246 if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent
)))) return 1;
248 while (check
&& (ret
= NT_STATUS_IS_OK(in
->pdb_getsampwent (in
, sam_pwent
)))) {
250 printf ("---------------\n");
251 print_sam_info (sam_pwent
, verbosity
, smbpwdstyle
);
252 pdb_free_sam(&sam_pwent
);
253 check
= NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent
));
255 if (check
) pdb_free_sam(&sam_pwent
);
257 in
->pdb_endsampwent(in
);
261 /*********************************************************
262 Fix a list of Users for uninitialised passwords
263 **********************************************************/
264 static int fix_users_list (struct pdb_context
*in
)
266 SAM_ACCOUNT
*sam_pwent
=NULL
;
269 check
= NT_STATUS_IS_OK(in
->pdb_setsampwent(in
, False
, 0));
275 if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent
)))) return 1;
277 while (check
&& (ret
= NT_STATUS_IS_OK(in
->pdb_getsampwent (in
, sam_pwent
)))) {
278 printf("Updating record for user %s\n", pdb_get_username(sam_pwent
));
280 if (!pdb_update_sam_account(sam_pwent
)) {
281 printf("Update of user %s failed!\n", pdb_get_username(sam_pwent
));
283 pdb_free_sam(&sam_pwent
);
284 check
= NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent
));
286 fprintf(stderr
, "Failed to initialise new SAM_ACCOUNT structure (out of memory?)\n");
290 if (check
) pdb_free_sam(&sam_pwent
);
292 in
->pdb_endsampwent(in
);
296 /*********************************************************
298 **********************************************************/
300 static int set_user_info (struct pdb_context
*in
, const char *username
,
301 const char *fullname
, const char *homedir
,
302 const char *acct_desc
,
303 const char *drive
, const char *script
,
304 const char *profile
, const char *account_control
,
305 const char *user_sid
, const char *group_sid
,
306 const BOOL badpw
, const BOOL hours
,
307 time_t pwd_can_change
, time_t pwd_must_change
)
309 BOOL updated_autolock
= False
, updated_badpw
= False
;
310 SAM_ACCOUNT
*sam_pwent
=NULL
;
313 pdb_init_sam(&sam_pwent
);
315 ret
= NT_STATUS_IS_OK(in
->pdb_getsampwnam (in
, sam_pwent
, username
));
317 fprintf (stderr
, "Username not found!\n");
318 pdb_free_sam(&sam_pwent
);
323 uint8 hours_array
[MAX_HOURS_LEN
];
326 hours_len
= pdb_get_hours_len(sam_pwent
);
327 memset(hours_array
, 0xff, hours_len
);
329 pdb_set_hours(sam_pwent
, hours_array
, PDB_CHANGED
);
332 if (pwd_can_change
!= -1) {
333 pdb_set_pass_can_change_time(sam_pwent
, pwd_can_change
, PDB_CHANGED
);
336 if (pwd_must_change
!= -1) {
337 pdb_set_pass_must_change_time(sam_pwent
, pwd_must_change
, PDB_CHANGED
);
340 if (!pdb_update_autolock_flag(sam_pwent
, &updated_autolock
)) {
341 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
344 if (!pdb_update_bad_password_count(sam_pwent
, &updated_badpw
)) {
345 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
349 pdb_set_fullname(sam_pwent
, fullname
, PDB_CHANGED
);
351 pdb_set_acct_desc(sam_pwent
, acct_desc
, PDB_CHANGED
);
353 pdb_set_homedir(sam_pwent
, homedir
, PDB_CHANGED
);
355 pdb_set_dir_drive(sam_pwent
,drive
, PDB_CHANGED
);
357 pdb_set_logon_script(sam_pwent
, script
, PDB_CHANGED
);
359 pdb_set_profile_path (sam_pwent
, profile
, PDB_CHANGED
);
361 if (account_control
) {
362 uint16 not_settable
= ~(ACB_DISABLED
|ACB_HOMDIRREQ
|ACB_PWNOTREQ
|
363 ACB_PWNOEXP
|ACB_AUTOLOCK
);
365 uint16 newflag
= pdb_decode_acct_ctrl(account_control
);
367 if (newflag
& not_settable
) {
368 fprintf(stderr
, "Can only set [NDHLX] flags\n");
369 pdb_free_sam(&sam_pwent
);
373 pdb_set_acct_ctrl(sam_pwent
,
374 (pdb_get_acct_ctrl(sam_pwent
) & not_settable
) | newflag
,
379 if (!string_to_sid(&u_sid
, user_sid
)) {
380 /* not a complete sid, may be a RID, try building a SID */
383 if (sscanf(user_sid
, "%d", &u_rid
) != 1) {
384 fprintf(stderr
, "Error passed string is not a complete user SID or RID!\n");
387 sid_copy(&u_sid
, get_global_sam_sid());
388 sid_append_rid(&u_sid
, u_rid
);
390 pdb_set_user_sid (sam_pwent
, &u_sid
, PDB_CHANGED
);
394 if (!string_to_sid(&g_sid
, group_sid
)) {
395 /* not a complete sid, may be a RID, try building a SID */
398 if (sscanf(group_sid
, "%d", &g_rid
) != 1) {
399 fprintf(stderr
, "Error passed string is not a complete group SID or RID!\n");
402 sid_copy(&g_sid
, get_global_sam_sid());
403 sid_append_rid(&g_sid
, g_rid
);
405 pdb_set_group_sid (sam_pwent
, &g_sid
, PDB_CHANGED
);
409 pdb_set_bad_password_count(sam_pwent
, 0, PDB_CHANGED
);
410 pdb_set_bad_password_time(sam_pwent
, 0, PDB_CHANGED
);
413 if (NT_STATUS_IS_OK(in
->pdb_update_sam_account (in
, sam_pwent
)))
414 print_user_info (in
, username
, True
, False
);
416 fprintf (stderr
, "Unable to modify entry!\n");
417 pdb_free_sam(&sam_pwent
);
420 pdb_free_sam(&sam_pwent
);
424 /*********************************************************
426 **********************************************************/
427 static int new_user (struct pdb_context
*in
, const char *username
,
428 const char *fullname
, const char *homedir
,
429 const char *drive
, const char *script
,
430 const char *profile
, char *user_sid
, char *group_sid
)
432 SAM_ACCOUNT
*sam_pwent
=NULL
;
434 char *password1
, *password2
, *staticpass
;
436 get_global_sam_sid();
438 if (!NT_STATUS_IS_OK(nt_status
= pdb_init_sam_new(&sam_pwent
, username
, 0))) {
439 DEBUG(0, ("could not create account to add new user %s\n", username
));
443 staticpass
= getpass("new password:");
444 password1
= SMB_STRDUP(staticpass
);
445 memset(staticpass
, 0, strlen(staticpass
));
446 staticpass
= getpass("retype new password:");
447 password2
= SMB_STRDUP(staticpass
);
448 memset(staticpass
, 0, strlen(staticpass
));
449 if (strcmp (password1
, password2
)) {
450 fprintf (stderr
, "Passwords does not match!\n");
451 memset(password1
, 0, strlen(password1
));
452 SAFE_FREE(password1
);
453 memset(password2
, 0, strlen(password2
));
454 SAFE_FREE(password2
);
455 pdb_free_sam (&sam_pwent
);
459 pdb_set_plaintext_passwd(sam_pwent
, password1
);
460 memset(password1
, 0, strlen(password1
));
461 SAFE_FREE(password1
);
462 memset(password2
, 0, strlen(password2
));
463 SAFE_FREE(password2
);
466 pdb_set_fullname(sam_pwent
, fullname
, PDB_CHANGED
);
468 pdb_set_homedir (sam_pwent
, homedir
, PDB_CHANGED
);
470 pdb_set_dir_drive (sam_pwent
, drive
, PDB_CHANGED
);
472 pdb_set_logon_script(sam_pwent
, script
, PDB_CHANGED
);
474 pdb_set_profile_path (sam_pwent
, profile
, PDB_CHANGED
);
477 if (!string_to_sid(&u_sid
, user_sid
)) {
478 /* not a complete sid, may be a RID, try building a SID */
481 if (sscanf(user_sid
, "%d", &u_rid
) != 1) {
482 fprintf(stderr
, "Error passed string is not a complete user SID or RID!\n");
485 sid_copy(&u_sid
, get_global_sam_sid());
486 sid_append_rid(&u_sid
, u_rid
);
488 pdb_set_user_sid (sam_pwent
, &u_sid
, PDB_CHANGED
);
492 if (!string_to_sid(&g_sid
, group_sid
)) {
493 /* not a complete sid, may be a RID, try building a SID */
496 if (sscanf(group_sid
, "%d", &g_rid
) != 1) {
497 fprintf(stderr
, "Error passed string is not a complete group SID or RID!\n");
500 sid_copy(&g_sid
, get_global_sam_sid());
501 sid_append_rid(&g_sid
, g_rid
);
503 pdb_set_group_sid (sam_pwent
, &g_sid
, PDB_CHANGED
);
506 pdb_set_acct_ctrl (sam_pwent
, ACB_NORMAL
, PDB_CHANGED
);
508 if (NT_STATUS_IS_OK(in
->pdb_add_sam_account (in
, sam_pwent
))) {
509 print_user_info (in
, username
, True
, False
);
511 fprintf (stderr
, "Unable to add user! (does it already exist?)\n");
512 pdb_free_sam (&sam_pwent
);
515 pdb_free_sam (&sam_pwent
);
519 /*********************************************************
521 **********************************************************/
523 static int new_machine (struct pdb_context
*in
, const char *machine_in
)
525 SAM_ACCOUNT
*sam_pwent
=NULL
;
527 fstring machineaccount
;
528 struct passwd
*pwd
= NULL
;
530 get_global_sam_sid();
532 fstrcpy(machinename
, machine_in
);
533 machinename
[15]= '\0';
535 if (machinename
[strlen (machinename
) -1] == '$')
536 machinename
[strlen (machinename
) -1] = '\0';
538 strlower_m(machinename
);
540 fstrcpy(machineaccount
, machinename
);
541 fstrcat(machineaccount
, "$");
543 if ((pwd
= getpwnam_alloc(machineaccount
))) {
544 if (!NT_STATUS_IS_OK(pdb_init_sam_pw( &sam_pwent
, pwd
))) {
545 fprintf(stderr
, "Could not init sam from pw\n");
551 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent
))) {
552 fprintf(stderr
, "Could not init sam from pw\n");
557 pdb_set_plaintext_passwd (sam_pwent
, machinename
);
559 pdb_set_username (sam_pwent
, machineaccount
, PDB_CHANGED
);
561 pdb_set_acct_ctrl (sam_pwent
, ACB_WSTRUST
, PDB_CHANGED
);
563 pdb_set_group_sid_from_rid(sam_pwent
, DOMAIN_GROUP_RID_COMPUTERS
, PDB_CHANGED
);
565 if (NT_STATUS_IS_OK(in
->pdb_add_sam_account (in
, sam_pwent
))) {
566 print_user_info (in
, machineaccount
, True
, False
);
568 fprintf (stderr
, "Unable to add machine! (does it already exist?)\n");
569 pdb_free_sam (&sam_pwent
);
572 pdb_free_sam (&sam_pwent
);
576 /*********************************************************
578 **********************************************************/
580 static int delete_user_entry (struct pdb_context
*in
, const char *username
)
582 SAM_ACCOUNT
*samaccount
= NULL
;
584 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount
))) {
588 if (!NT_STATUS_IS_OK(in
->pdb_getsampwnam(in
, samaccount
, username
))) {
589 fprintf (stderr
, "user %s does not exist in the passdb\n", username
);
593 if (!NT_STATUS_IS_OK(in
->pdb_delete_sam_account (in
, samaccount
))) {
594 fprintf (stderr
, "Unable to delete user %s\n", username
);
600 /*********************************************************
602 **********************************************************/
604 static int delete_machine_entry (struct pdb_context
*in
, const char *machinename
)
607 SAM_ACCOUNT
*samaccount
= NULL
;
609 fstrcpy(name
, machinename
);
611 if (name
[strlen(name
)-1] != '$')
614 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount
))) {
618 if (!NT_STATUS_IS_OK(in
->pdb_getsampwnam(in
, samaccount
, name
))) {
619 fprintf (stderr
, "machine %s does not exist in the passdb\n", name
);
623 if (!NT_STATUS_IS_OK(in
->pdb_delete_sam_account (in
, samaccount
))) {
624 fprintf (stderr
, "Unable to delete machine %s\n", name
);
631 /*********************************************************
633 **********************************************************/
635 int main (int argc
, char **argv
)
637 static BOOL list_users
= False
;
638 static BOOL verbose
= False
;
639 static BOOL spstyle
= False
;
640 static BOOL machine
= False
;
641 static BOOL add_user
= False
;
642 static BOOL delete_user
= False
;
643 static BOOL modify_user
= False
;
644 uint32 setparms
, checkparms
;
646 static char *full_name
= NULL
;
647 static char *acct_desc
= NULL
;
648 static const char *user_name
= NULL
;
649 static char *home_dir
= NULL
;
650 static char *home_drive
= NULL
;
651 static char *backend
= NULL
;
652 static char *backend_in
= NULL
;
653 static char *backend_out
= NULL
;
654 static BOOL transfer_groups
= False
;
655 static BOOL force_initialised_password
= False
;
656 static char *logon_script
= NULL
;
657 static char *profile_path
= NULL
;
658 static char *account_control
= NULL
;
659 static char *account_policy
= NULL
;
660 static char *user_sid
= NULL
;
661 static char *group_sid
= NULL
;
662 static long int account_policy_value
= 0;
663 BOOL account_policy_value_set
= False
;
664 static BOOL badpw_reset
= False
;
665 static BOOL hours_reset
= False
;
666 static char *pwd_can_change_time
= NULL
;
667 static char *pwd_must_change_time
= NULL
;
668 static char *pwd_time_format
= NULL
;
670 struct pdb_context
*bin
;
671 struct pdb_context
*bout
;
672 struct pdb_context
*bdef
;
674 struct poptOption long_options
[] = {
676 {"list", 'L', POPT_ARG_NONE
, &list_users
, 0, "list all users", NULL
},
677 {"verbose", 'v', POPT_ARG_NONE
, &verbose
, 0, "be verbose", NULL
},
678 {"smbpasswd-style", 'w',POPT_ARG_NONE
, &spstyle
, 0, "give output in smbpasswd style", NULL
},
679 {"user", 'u', POPT_ARG_STRING
, &user_name
, 0, "use username", "USER" },
680 {"account-desc", 'N', POPT_ARG_STRING
, &acct_desc
, 0, "set account description", NULL
},
681 {"fullname", 'f', POPT_ARG_STRING
, &full_name
, 0, "set full name", NULL
},
682 {"homedir", 'h', POPT_ARG_STRING
, &home_dir
, 0, "set home directory", NULL
},
683 {"drive", 'D', POPT_ARG_STRING
, &home_drive
, 0, "set home drive", NULL
},
684 {"script", 'S', POPT_ARG_STRING
, &logon_script
, 0, "set logon script", NULL
},
685 {"profile", 'p', POPT_ARG_STRING
, &profile_path
, 0, "set profile path", NULL
},
686 {"user-SID", 'U', POPT_ARG_STRING
, &user_sid
, 0, "set user SID or RID", NULL
},
687 {"group-SID", 'G', POPT_ARG_STRING
, &group_sid
, 0, "set group SID or RID", NULL
},
688 {"create", 'a', POPT_ARG_NONE
, &add_user
, 0, "create user", NULL
},
689 {"modify", 'r', POPT_ARG_NONE
, &modify_user
, 0, "modify user", NULL
},
690 {"machine", 'm', POPT_ARG_NONE
, &machine
, 0, "account is a machine account", NULL
},
691 {"delete", 'x', POPT_ARG_NONE
, &delete_user
, 0, "delete user", NULL
},
692 {"backend", 'b', POPT_ARG_STRING
, &backend
, 0, "use different passdb backend as default backend", NULL
},
693 {"import", 'i', POPT_ARG_STRING
, &backend_in
, 0, "import user accounts from this backend", NULL
},
694 {"export", 'e', POPT_ARG_STRING
, &backend_out
, 0, "export user accounts to this backend", NULL
},
695 {"group", 'g', POPT_ARG_NONE
, &transfer_groups
, 0, "use -i and -e for groups", NULL
},
696 {"account-policy", 'P', POPT_ARG_STRING
, &account_policy
, 0,"value of an account policy (like maximum password age)",NULL
},
697 {"value", 'C', POPT_ARG_LONG
, &account_policy_value
, 'C',"set the account policy to this value", NULL
},
698 {"account-control", 'c', POPT_ARG_STRING
, &account_control
, 0, "Values of account control", NULL
},
699 {"force-initialized-passwords", 0, POPT_ARG_NONE
, &force_initialised_password
, 0, "Force initialization of corrupt password strings in a passdb backend", NULL
},
700 {"bad-password-count-reset", 'z', POPT_ARG_NONE
, &badpw_reset
, 0, "reset bad password count", NULL
},
701 {"logon-hours-reset", 'Z', POPT_ARG_NONE
, &hours_reset
, 0, "reset logon hours", NULL
},
702 {"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
},
703 {"pwd-must-change-time", 0, POPT_ARG_STRING
, &pwd_must_change_time
, 0, "Set password can change time (unix time in seconds since 1970 if time format not provided)", NULL
},
704 {"time-format", 0, POPT_ARG_STRING
, &pwd_time_format
, 0, "The time format for time parameters", NULL
},
709 setup_logging("pdbedit", True
);
711 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
,
712 POPT_CONTEXT_KEEP_FIRST
);
714 while((opt
= poptGetNextOpt(pc
)) != -1) {
717 account_policy_value_set
= True
;
722 poptGetArg(pc
); /* Drop argv[0], the program name */
724 if (user_name
== NULL
)
725 user_name
= poptGetArg(pc
);
727 if (!lp_load(dyn_CONFIGFILE
,True
,False
,False
)) {
728 fprintf(stderr
, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE
);
732 if(!initialize_password_db(False
))
738 setparms
= (backend
? BIT_BACKEND
: 0) +
739 (verbose
? BIT_VERBOSE
: 0) +
740 (spstyle
? BIT_SPSTYLE
: 0) +
741 (full_name
? BIT_FULLNAME
: 0) +
742 (home_dir
? BIT_HOMEDIR
: 0) +
743 (home_drive
? BIT_HDIRDRIVE
: 0) +
744 (logon_script
? BIT_LOGSCRIPT
: 0) +
745 (profile_path
? BIT_PROFILE
: 0) +
746 (machine
? BIT_MACHINE
: 0) +
747 (user_name
? BIT_USER
: 0) +
748 (list_users
? BIT_LIST
: 0) +
749 (force_initialised_password
? BIT_FIX_INIT
: 0) +
750 (modify_user
? BIT_MODIFY
: 0) +
751 (add_user
? BIT_CREATE
: 0) +
752 (delete_user
? BIT_DELETE
: 0) +
753 (account_control
? BIT_ACCTCTRL
: 0) +
754 (account_policy
? BIT_ACCPOLICY
: 0) +
755 (account_policy_value_set
? BIT_ACCPOLVAL
: 0) +
756 (backend_in
? BIT_IMPORT
: 0) +
757 (backend_out
? BIT_EXPORT
: 0) +
758 (badpw_reset
? BIT_BADPWRESET
: 0) +
759 (hours_reset
? BIT_LOGONHOURS
: 0) +
760 (pwd_can_change_time
? BIT_CAN_CHANGE
: 0) +
761 (pwd_must_change_time
? BIT_MUST_CHANGE
: 0);
763 if (setparms
& BIT_BACKEND
) {
764 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bdef
, backend
))) {
765 fprintf(stderr
, "Can't initialize passdb backend.\n");
769 if (!NT_STATUS_IS_OK(make_pdb_context_list(&bdef
, lp_passdb_backend()))) {
770 fprintf(stderr
, "Can't initialize passdb backend.\n");
775 /* the lowest bit options are always accepted */
776 checkparms
= setparms
& ~MASK_ALWAYS_GOOD
;
778 if (checkparms
& BIT_FIX_INIT
) {
779 return fix_users_list(bdef
);
782 /* account policy operations */
783 if ((checkparms
& BIT_ACCPOLICY
) && !(checkparms
& ~(BIT_ACCPOLICY
+ BIT_ACCPOLVAL
))) {
785 int field
= account_policy_name_to_fieldnum(account_policy
);
787 char *apn
= account_policy_names_list();
788 fprintf(stderr
, "No account policy by that name\n");
790 fprintf(stderr
, "Account policy names are :\n%s\n", apn
);
795 if (!account_policy_get(field
, &value
)) {
796 fprintf(stderr
, "valid account policy, but unable to fetch value!\n");
799 if (account_policy_value_set
) {
800 printf("account policy value for %s was %u\n", account_policy
, value
);
801 if (!account_policy_set(field
, account_policy_value
)) {
802 fprintf(stderr
, "valid account policy, but unable to set value!\n");
805 printf("account policy value for %s is now %lu\n", account_policy
, account_policy_value
);
808 printf("account policy value for %s is %u\n", account_policy
, value
);
813 /* import and export operations */
814 if (((checkparms
& BIT_IMPORT
) || (checkparms
& BIT_EXPORT
))
815 && !(checkparms
& ~(BIT_IMPORT
+BIT_EXPORT
+BIT_USER
))) {
817 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bin
, backend_in
))) {
818 fprintf(stderr
, "Can't initialize passdb backend.\n");
825 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bout
, backend_out
))) {
826 fprintf(stderr
, "Can't initialize %s.\n", backend_out
);
832 if (transfer_groups
) {
833 if (!(checkparms
& BIT_USER
))
834 return export_groups(bin
, bout
);
836 if (checkparms
& BIT_USER
)
837 return export_database(bin
, bout
,
840 return export_database(bin
, bout
,
845 /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
846 /* fake up BIT_LIST if only BIT_USER is defined */
847 if ((checkparms
& BIT_USER
) && !(checkparms
& ~BIT_USER
)) {
848 checkparms
+= BIT_LIST
;
851 /* modify flag is optional to maintain backwards compatibility */
852 /* fake up BIT_MODIFY if BIT_USER and at least one of MASK_USER_GOOD is defined */
853 if (!((checkparms
& ~MASK_USER_GOOD
) & ~BIT_USER
) && (checkparms
& MASK_USER_GOOD
)) {
854 checkparms
+= BIT_MODIFY
;
857 /* list users operations */
858 if (checkparms
& BIT_LIST
) {
859 if (!(checkparms
& ~BIT_LIST
)) {
860 return print_users_list (bdef
, verbose
, spstyle
);
862 if (!(checkparms
& ~(BIT_USER
+ BIT_LIST
))) {
863 return print_user_info (bdef
, user_name
, verbose
, spstyle
);
867 /* mask out users options */
868 checkparms
&= ~MASK_USER_GOOD
;
870 /* if bad password count is reset, we must be modifying */
871 if (checkparms
& BIT_BADPWRESET
) {
872 checkparms
|= BIT_MODIFY
;
873 checkparms
&= ~BIT_BADPWRESET
;
876 /* if logon hours is reset, must modify */
877 if (checkparms
& BIT_LOGONHOURS
) {
878 checkparms
|= BIT_MODIFY
;
879 checkparms
&= ~BIT_LOGONHOURS
;
882 /* account operation */
883 if ((checkparms
& BIT_CREATE
) || (checkparms
& BIT_MODIFY
) || (checkparms
& BIT_DELETE
)) {
884 /* check use of -u option */
885 if (!(checkparms
& BIT_USER
)) {
886 fprintf (stderr
, "Username not specified! (use -u option)\n");
890 /* account creation operations */
891 if (!(checkparms
& ~(BIT_CREATE
+ BIT_USER
+ BIT_MACHINE
))) {
892 if (checkparms
& BIT_MACHINE
) {
893 return new_machine (bdef
, user_name
);
895 return new_user (bdef
, user_name
, full_name
, home_dir
,
896 home_drive
, logon_script
,
897 profile_path
, user_sid
, group_sid
);
901 /* account deletion operations */
902 if (!(checkparms
& ~(BIT_DELETE
+ BIT_USER
+ BIT_MACHINE
))) {
903 if (checkparms
& BIT_MACHINE
) {
904 return delete_machine_entry (bdef
, user_name
);
906 return delete_user_entry (bdef
, user_name
);
910 /* account modification operations */
911 if (!(checkparms
& ~(BIT_MODIFY
+ BIT_USER
))) {
912 time_t pwd_can_change
= -1;
913 time_t pwd_must_change
= -1;
916 if (pwd_can_change_time
) {
918 if (pwd_time_format
) {
922 memset(&tm
, 0, sizeof(struct tm
));
923 ret
= strptime(pwd_can_change_time
, pwd_time_format
, &tm
);
924 if (ret
== NULL
|| *ret
!= '\0') {
928 pwd_can_change
= mktime(&tm
);
930 if (pwd_can_change
== -1) {
933 } else { /* assume it is unix time */
935 pwd_can_change
= strtol(pwd_can_change_time
, NULL
, 10);
941 if (pwd_must_change_time
) {
943 if (pwd_time_format
) {
947 memset(&tm
, 0, sizeof(struct tm
));
948 ret
= strptime(pwd_must_change_time
, pwd_time_format
, &tm
);
949 if (ret
== NULL
|| *ret
!= '\0') {
953 pwd_must_change
= mktime(&tm
);
955 if (pwd_must_change
== -1) {
958 } else { /* assume it is unix time */
960 pwd_must_change
= strtol(pwd_must_change_time
, NULL
, 10);
966 return set_user_info (bdef
, user_name
, full_name
,
971 profile_path
, account_control
,
973 badpw_reset
, hours_reset
,
974 pwd_can_change
, pwd_must_change
);
976 fprintf (stderr
, "Error parsing the time in pwd-%s-change-time!\n", errstr
);
981 if (setparms
>= 0x20) {
982 fprintf (stderr
, "Incompatible or insufficient options on command line!\n");
984 poptPrintHelp(pc
, stderr
, 0);