r13679: Commiting the rm_primary_group.patch posted on samba-technical
[Samba/bb.git] / source3 / utils / pdbedit.c
blob471b8988776e4a431a817fda7b381d52713b925f
1 /*
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.
24 #include "includes.h"
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)
63 int i;
65 for (i=1; decode_account_policy_name(i) != NULL; i++) {
66 uint32 policy_value;
67 if (!account_policy_get_default(i, &policy_value)) {
68 fprintf(stderr, "Can't get default account policy\n");
69 return -1;
71 if (!account_policy_set(i, policy_value)) {
72 fprintf(stderr, "Can't set account policy in tdb\n");
73 return -1;
77 if (!remove_account_policy_migrated()) {
78 fprintf(stderr, "Can't remove marker from tdb\n");
79 return -1;
82 return 0;
85 /*********************************************************
86 Print info from sam structure
87 **********************************************************/
89 static int print_sam_info (struct samu *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
91 uid_t uid;
92 time_t tmp;
94 /* TODO: chaeck if entry is a user or a workstation */
95 if (!sam_pwent) return -1;
97 if (verbosity) {
98 pstring temp;
99 const uint8 *hours;
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) {
146 char lm_passwd[33];
147 char nt_passwd[33];
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),
155 (unsigned long)uid,
156 lm_passwd,
157 nt_passwd,
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));
160 } else {
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));
166 return 0;
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;
176 BOOL ret;
178 if ( !(sam_pwent = samu_new( NULL )) ) {
179 return -1;
182 ret = NT_STATUS_IS_OK(in->getsampwnam (in, sam_pwent, username));
184 if (ret==False) {
185 fprintf (stderr, "Username not found!\n");
186 TALLOC_FREE(sam_pwent);
187 return -1;
190 ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
191 TALLOC_FREE(sam_pwent);
193 return ret;
196 /*********************************************************
197 List Users
198 **********************************************************/
199 static int print_users_list (struct pdb_methods *in, BOOL verbosity, BOOL smbpwdstyle)
201 struct samu *sam_pwent=NULL;
202 BOOL check;
204 check = NT_STATUS_IS_OK(in->setsampwent(in, False, 0));
205 if (!check) {
206 return 1;
209 check = True;
210 if ( !(sam_pwent = samu_new( NULL )) ) {
211 return 1;
214 while (check && NT_STATUS_IS_OK(in->getsampwent (in, sam_pwent))) {
215 if (verbosity)
216 printf ("---------------\n");
217 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
218 TALLOC_FREE(sam_pwent);
220 if ( !(sam_pwent = samu_new( NULL )) ) {
221 check = False;
224 if (check)
225 TALLOC_FREE(sam_pwent);
227 in->endsampwent(in);
228 return 0;
231 /*********************************************************
232 Fix a list of Users for uninitialised passwords
233 **********************************************************/
234 static int fix_users_list (struct pdb_methods *in)
236 struct samu *sam_pwent=NULL;
237 BOOL check;
239 check = NT_STATUS_IS_OK(in->setsampwent(in, False, 0));
240 if (!check) {
241 return 1;
244 check = True;
245 if ( !(sam_pwent = samu_new( NULL )) ) {
246 return 1;
249 while (check && NT_STATUS_IS_OK(in->getsampwent (in, sam_pwent))) {
250 printf("Updating record for user %s\n", pdb_get_username(sam_pwent));
252 if (!NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
253 printf("Update of user %s failed!\n", pdb_get_username(sam_pwent));
255 TALLOC_FREE(sam_pwent);
256 if ( !(sam_pwent = samu_new( NULL )) ) {
257 check = False;
259 if (!check) {
260 fprintf(stderr, "Failed to initialise new struct samu structure (out of memory?)\n");
264 if (check)
265 TALLOC_FREE(sam_pwent);
267 in->endsampwent(in);
268 return 0;
271 /*********************************************************
272 Set User Info
273 **********************************************************/
275 static int set_user_info (struct pdb_methods *in, const char *username,
276 const char *fullname, const char *homedir,
277 const char *acct_desc,
278 const char *drive, const char *script,
279 const char *profile, const char *account_control,
280 const char *user_sid, const char *user_domain,
281 const BOOL badpw, const BOOL hours,
282 time_t pwd_can_change, time_t pwd_must_change)
284 BOOL updated_autolock = False, updated_badpw = False;
285 struct samu *sam_pwent=NULL;
286 BOOL ret;
288 if ( !(sam_pwent = samu_new( NULL )) ) {
289 return 1;
292 ret = NT_STATUS_IS_OK(in->getsampwnam (in, sam_pwent, username));
293 if (ret==False) {
294 fprintf (stderr, "Username not found!\n");
295 TALLOC_FREE(sam_pwent);
296 return -1;
299 if (hours) {
300 uint8 hours_array[MAX_HOURS_LEN];
301 uint32 hours_len;
303 hours_len = pdb_get_hours_len(sam_pwent);
304 memset(hours_array, 0xff, hours_len);
306 pdb_set_hours(sam_pwent, hours_array, PDB_CHANGED);
309 if (pwd_can_change != -1) {
310 pdb_set_pass_can_change_time(sam_pwent, pwd_can_change, PDB_CHANGED);
313 if (pwd_must_change != -1) {
314 pdb_set_pass_must_change_time(sam_pwent, pwd_must_change, PDB_CHANGED);
317 if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) {
318 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
321 if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) {
322 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
325 if (fullname)
326 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
327 if (acct_desc)
328 pdb_set_acct_desc(sam_pwent, acct_desc, PDB_CHANGED);
329 if (homedir)
330 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
331 if (drive)
332 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
333 if (script)
334 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
335 if (profile)
336 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
337 if (user_domain)
338 pdb_set_domain(sam_pwent, user_domain, PDB_CHANGED);
340 if (account_control) {
341 uint16 not_settable = ~(ACB_DISABLED|ACB_HOMDIRREQ|ACB_PWNOTREQ|
342 ACB_PWNOEXP|ACB_AUTOLOCK);
344 uint16 newflag = pdb_decode_acct_ctrl(account_control);
346 if (newflag & not_settable) {
347 fprintf(stderr, "Can only set [NDHLX] flags\n");
348 TALLOC_FREE(sam_pwent);
349 return -1;
352 pdb_set_acct_ctrl(sam_pwent,
353 (pdb_get_acct_ctrl(sam_pwent) & not_settable) | newflag,
354 PDB_CHANGED);
356 if (user_sid) {
357 DOM_SID u_sid;
358 if (!string_to_sid(&u_sid, user_sid)) {
359 /* not a complete sid, may be a RID, try building a SID */
360 int u_rid;
362 if (sscanf(user_sid, "%d", &u_rid) != 1) {
363 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
364 return -1;
366 sid_copy(&u_sid, get_global_sam_sid());
367 sid_append_rid(&u_sid, u_rid);
369 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
372 if (badpw) {
373 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
374 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
377 if (NT_STATUS_IS_OK(in->update_sam_account (in, sam_pwent)))
378 print_user_info (in, username, True, False);
379 else {
380 fprintf (stderr, "Unable to modify entry!\n");
381 TALLOC_FREE(sam_pwent);
382 return -1;
384 TALLOC_FREE(sam_pwent);
385 return 0;
388 /*********************************************************
389 Add New User
390 **********************************************************/
391 static int new_user (struct pdb_methods *in, const char *username,
392 const char *fullname, const char *homedir,
393 const char *drive, const char *script,
394 const char *profile, char *user_sid, BOOL stdin_get)
396 struct samu *sam_pwent;
397 char *password1, *password2;
398 int rc_pwd_cmp;
399 struct passwd *pwd;
401 get_global_sam_sid();
403 if ( !(pwd = getpwnam_alloc( NULL, username )) ) {
404 DEBUG(0,("Cannot locate Unix account for %s\n", username));
405 return -1;
408 if ( !(sam_pwent = samu_new( NULL )) ) {
409 DEBUG(0, ("Memory allocation failure!\n"));
410 return -1;
413 if (!NT_STATUS_IS_OK(samu_alloc_rid_unix(sam_pwent, pwd ))) {
414 TALLOC_FREE( sam_pwent );
415 TALLOC_FREE( pwd );
416 DEBUG(0, ("could not create account to add new user %s\n", username));
417 return -1;
420 password1 = get_pass( "new password:", stdin_get);
421 password2 = get_pass( "retype new password:", stdin_get);
422 if ((rc_pwd_cmp = strcmp (password1, password2))) {
423 fprintf (stderr, "Passwords do not match!\n");
424 TALLOC_FREE(sam_pwent);
425 } else {
426 pdb_set_plaintext_passwd(sam_pwent, password1);
429 memset(password1, 0, strlen(password1));
430 SAFE_FREE(password1);
431 memset(password2, 0, strlen(password2));
432 SAFE_FREE(password2);
434 /* pwds do _not_ match? */
435 if (rc_pwd_cmp)
436 return -1;
438 if (fullname)
439 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
440 if (homedir)
441 pdb_set_homedir (sam_pwent, homedir, PDB_CHANGED);
442 if (drive)
443 pdb_set_dir_drive (sam_pwent, drive, PDB_CHANGED);
444 if (script)
445 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
446 if (profile)
447 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
448 if (user_sid) {
449 DOM_SID u_sid;
450 if (!string_to_sid(&u_sid, user_sid)) {
451 /* not a complete sid, may be a RID, try building a SID */
452 int u_rid;
454 if (sscanf(user_sid, "%d", &u_rid) != 1) {
455 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
456 return -1;
458 sid_copy(&u_sid, get_global_sam_sid());
459 sid_append_rid(&u_sid, u_rid);
461 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
464 pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL, PDB_CHANGED);
466 if (NT_STATUS_IS_OK(in->add_sam_account (in, sam_pwent))) {
467 print_user_info (in, username, True, False);
468 } else {
469 fprintf (stderr, "Unable to add user! (does it already exist?)\n");
470 TALLOC_FREE(sam_pwent);
471 return -1;
473 TALLOC_FREE(sam_pwent);
474 return 0;
477 /*********************************************************
478 Add New Machine
479 **********************************************************/
481 static int new_machine (struct pdb_methods *in, const char *machine_in)
483 struct samu *sam_pwent=NULL;
484 fstring machinename;
485 fstring machineaccount;
486 struct passwd *pwd = NULL;
488 get_global_sam_sid();
490 fstrcpy(machinename, machine_in);
491 machinename[15]= '\0';
493 if (machinename[strlen (machinename) -1] == '$')
494 machinename[strlen (machinename) -1] = '\0';
496 strlower_m(machinename);
498 fstrcpy(machineaccount, machinename);
499 fstrcat(machineaccount, "$");
501 if ((pwd = getpwnam_alloc(NULL, machineaccount))) {
503 if ( !(sam_pwent = samu_new( NULL )) ) {
504 fprintf(stderr, "Memory allocation error!\n");
505 TALLOC_FREE(pwd);
506 return -1;
509 if ( !NT_STATUS_IS_OK(samu_set_unix(sam_pwent, pwd )) ) {
510 fprintf(stderr, "Could not init sam from pw\n");
511 TALLOC_FREE(pwd);
512 return -1;
515 TALLOC_FREE(pwd);
516 } else {
517 if ( !(sam_pwent = samu_new( NULL )) ) {
518 fprintf(stderr, "Could not init sam from pw\n");
519 return -1;
523 pdb_set_plaintext_passwd (sam_pwent, machinename);
524 pdb_set_username (sam_pwent, machineaccount, PDB_CHANGED);
525 pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST, PDB_CHANGED);
527 if (NT_STATUS_IS_OK(in->add_sam_account (in, sam_pwent))) {
528 print_user_info (in, machineaccount, True, False);
529 } else {
530 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
531 TALLOC_FREE(sam_pwent);
532 return -1;
534 TALLOC_FREE(sam_pwent);
535 return 0;
538 /*********************************************************
539 Delete user entry
540 **********************************************************/
542 static int delete_user_entry (struct pdb_methods *in, const char *username)
544 struct samu *samaccount = NULL;
546 if ( !(samaccount = samu_new( NULL )) ) {
547 return -1;
550 if (!NT_STATUS_IS_OK(in->getsampwnam(in, samaccount, username))) {
551 fprintf (stderr, "user %s does not exist in the passdb\n", username);
552 return -1;
555 if (!NT_STATUS_IS_OK(in->delete_sam_account (in, samaccount))) {
556 fprintf (stderr, "Unable to delete user %s\n", username);
557 return -1;
559 return 0;
562 /*********************************************************
563 Delete machine entry
564 **********************************************************/
566 static int delete_machine_entry (struct pdb_methods *in, const char *machinename)
568 fstring name;
569 struct samu *samaccount = NULL;
571 fstrcpy(name, machinename);
572 name[15] = '\0';
573 if (name[strlen(name)-1] != '$')
574 fstrcat (name, "$");
576 if ( !(samaccount = samu_new( NULL )) ) {
577 return -1;
580 if (!NT_STATUS_IS_OK(in->getsampwnam(in, samaccount, name))) {
581 fprintf (stderr, "machine %s does not exist in the passdb\n", name);
582 return -1;
585 if (!NT_STATUS_IS_OK(in->delete_sam_account (in, samaccount))) {
586 fprintf (stderr, "Unable to delete machine %s\n", name);
587 return -1;
590 return 0;
593 /*********************************************************
594 Start here.
595 **********************************************************/
597 int main (int argc, char **argv)
599 static BOOL list_users = False;
600 static BOOL verbose = False;
601 static BOOL spstyle = False;
602 static BOOL machine = False;
603 static BOOL add_user = False;
604 static BOOL delete_user = False;
605 static BOOL modify_user = False;
606 uint32 setparms, checkparms;
607 int opt;
608 static char *full_name = NULL;
609 static char *acct_desc = NULL;
610 static const char *user_name = NULL;
611 static char *home_dir = NULL;
612 static char *home_drive = NULL;
613 static char *backend = NULL;
614 static char *backend_in = NULL;
615 static char *backend_out = NULL;
616 static BOOL transfer_groups = False;
617 static BOOL transfer_account_policies = False;
618 static BOOL reset_account_policies = False;
619 static BOOL force_initialised_password = False;
620 static char *logon_script = NULL;
621 static char *profile_path = NULL;
622 static char *user_domain = NULL;
623 static char *account_control = NULL;
624 static char *account_policy = NULL;
625 static char *user_sid = NULL;
626 static long int account_policy_value = 0;
627 BOOL account_policy_value_set = False;
628 static BOOL badpw_reset = False;
629 static BOOL hours_reset = False;
630 static char *pwd_can_change_time = NULL;
631 static char *pwd_must_change_time = NULL;
632 static char *pwd_time_format = NULL;
633 static BOOL pw_from_stdin = False;
635 struct pdb_methods *bdef = NULL;
636 poptContext pc;
637 struct poptOption long_options[] = {
638 POPT_AUTOHELP
639 {"list", 'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
640 {"verbose", 'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
641 {"smbpasswd-style", 'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
642 {"user", 'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
643 {"account-desc", 'N', POPT_ARG_STRING, &acct_desc, 0, "set account description", NULL},
644 {"fullname", 'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
645 {"homedir", 'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
646 {"drive", 'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
647 {"script", 'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
648 {"profile", 'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
649 {"domain", 'I', POPT_ARG_STRING, &user_domain, 0, "set a users' domain", NULL},
650 {"user SID", 'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
651 {"create", 'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
652 {"modify", 'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
653 {"machine", 'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
654 {"delete", 'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
655 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
656 {"import", 'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
657 {"export", 'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
658 {"group", 'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
659 {"policies", 'y', POPT_ARG_NONE, &transfer_account_policies, 0, "use -i and -e to move account policies between backends", NULL},
660 {"policies-reset", 0, POPT_ARG_NONE, &reset_account_policies, 0, "restore default policies", NULL},
661 {"account-policy", 'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
662 {"value", 'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
663 {"account-control", 'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
664 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
665 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
666 {"logon-hours-reset", 'Z', POPT_ARG_NONE, &hours_reset, 0, "reset logon hours", NULL},
667 {"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 },
668 {"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 },
669 {"time-format", 0, POPT_ARG_STRING, &pwd_time_format, 0, "The time format for time parameters", NULL },
670 {"password-from-stdin", 't', POPT_ARG_NONE, &pw_from_stdin, 0, "get password from standard in", NULL},
671 POPT_COMMON_SAMBA
672 POPT_TABLEEND
675 load_case_tables();
677 setup_logging("pdbedit", True);
679 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
680 POPT_CONTEXT_KEEP_FIRST);
682 while((opt = poptGetNextOpt(pc)) != -1) {
683 switch (opt) {
684 case 'C':
685 account_policy_value_set = True;
686 break;
690 poptGetArg(pc); /* Drop argv[0], the program name */
692 if (user_name == NULL)
693 user_name = poptGetArg(pc);
695 if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
696 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
697 exit(1);
700 if(!initialize_password_db(False))
701 exit(1);
703 if (!init_names())
704 exit(1);
706 setparms = (backend ? BIT_BACKEND : 0) +
707 (verbose ? BIT_VERBOSE : 0) +
708 (spstyle ? BIT_SPSTYLE : 0) +
709 (full_name ? BIT_FULLNAME : 0) +
710 (home_dir ? BIT_HOMEDIR : 0) +
711 (home_drive ? BIT_HDIRDRIVE : 0) +
712 (logon_script ? BIT_LOGSCRIPT : 0) +
713 (profile_path ? BIT_PROFILE : 0) +
714 (user_domain ? BIT_USERDOMAIN : 0) +
715 (machine ? BIT_MACHINE : 0) +
716 (user_name ? BIT_USER : 0) +
717 (list_users ? BIT_LIST : 0) +
718 (force_initialised_password ? BIT_FIX_INIT : 0) +
719 (user_sid ? BIT_USERSIDS : 0) +
720 (modify_user ? BIT_MODIFY : 0) +
721 (add_user ? BIT_CREATE : 0) +
722 (delete_user ? BIT_DELETE : 0) +
723 (account_control ? BIT_ACCTCTRL : 0) +
724 (account_policy ? BIT_ACCPOLICY : 0) +
725 (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
726 (backend_in ? BIT_IMPORT : 0) +
727 (backend_out ? BIT_EXPORT : 0) +
728 (badpw_reset ? BIT_BADPWRESET : 0) +
729 (hours_reset ? BIT_LOGONHOURS : 0) +
730 (pwd_can_change_time ? BIT_CAN_CHANGE: 0) +
731 (pwd_must_change_time ? BIT_MUST_CHANGE: 0);
733 if (setparms & BIT_BACKEND) {
734 if (!NT_STATUS_IS_OK(make_pdb_method_name( &bdef, backend ))) {
735 fprintf(stderr, "Can't initialize passdb backend.\n");
736 return 1;
738 } else {
739 if (!NT_STATUS_IS_OK(make_pdb_method_name(&bdef, lp_passdb_backend()))) {
740 fprintf(stderr, "Can't initialize passdb backend.\n");
741 return 1;
745 /* the lowest bit options are always accepted */
746 checkparms = setparms & ~MASK_ALWAYS_GOOD;
748 if (checkparms & BIT_FIX_INIT) {
749 return fix_users_list(bdef);
752 /* account policy operations */
753 if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
754 uint32 value;
755 int field = account_policy_name_to_fieldnum(account_policy);
756 if (field == 0) {
757 char *apn = account_policy_names_list();
758 fprintf(stderr, "No account policy by that name\n");
759 if (apn) {
760 fprintf(stderr, "Account policy names are :\n%s\n", apn);
762 SAFE_FREE(apn);
763 exit(1);
765 if (!pdb_get_account_policy(field, &value)) {
766 fprintf(stderr, "valid account policy, but unable to fetch value!\n");
767 if (!account_policy_value_set)
768 exit(1);
770 printf("account policy \"%s\" description: %s\n", account_policy, account_policy_get_desc(field));
771 if (account_policy_value_set) {
772 printf("account policy \"%s\" value was: %u\n", account_policy, value);
773 if (!pdb_set_account_policy(field, account_policy_value)) {
774 fprintf(stderr, "valid account policy, but unable to set value!\n");
775 exit(1);
777 printf("account policy \"%s\" value is now: %lu\n", account_policy, account_policy_value);
778 exit(0);
779 } else {
780 printf("account policy \"%s\" value is: %u\n", account_policy, value);
781 exit(0);
785 if (reset_account_policies) {
786 if (!reinit_account_policies()) {
787 exit(1);
790 exit(0);
793 /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
794 /* fake up BIT_LIST if only BIT_USER is defined */
795 if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
796 checkparms += BIT_LIST;
799 /* modify flag is optional to maintain backwards compatibility */
800 /* fake up BIT_MODIFY if BIT_USER and at least one of MASK_USER_GOOD is defined */
801 if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
802 checkparms += BIT_MODIFY;
805 /* list users operations */
806 if (checkparms & BIT_LIST) {
807 if (!(checkparms & ~BIT_LIST)) {
808 return print_users_list (bdef, verbose, spstyle);
810 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
811 return print_user_info (bdef, user_name, verbose, spstyle);
815 /* mask out users options */
816 checkparms &= ~MASK_USER_GOOD;
818 /* if bad password count is reset, we must be modifying */
819 if (checkparms & BIT_BADPWRESET) {
820 checkparms |= BIT_MODIFY;
821 checkparms &= ~BIT_BADPWRESET;
824 /* if logon hours is reset, must modify */
825 if (checkparms & BIT_LOGONHOURS) {
826 checkparms |= BIT_MODIFY;
827 checkparms &= ~BIT_LOGONHOURS;
830 /* account operation */
831 if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
832 /* check use of -u option */
833 if (!(checkparms & BIT_USER)) {
834 fprintf (stderr, "Username not specified! (use -u option)\n");
835 return -1;
838 /* account creation operations */
839 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
840 if (checkparms & BIT_MACHINE) {
841 return new_machine (bdef, user_name);
842 } else {
843 return new_user (bdef, user_name, full_name, home_dir,
844 home_drive, logon_script, profile_path, user_sid, pw_from_stdin);
848 /* account deletion operations */
849 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
850 if (checkparms & BIT_MACHINE) {
851 return delete_machine_entry (bdef, user_name);
852 } else {
853 return delete_user_entry (bdef, user_name);
857 /* account modification operations */
858 if (!(checkparms & ~(BIT_MODIFY + BIT_USER))) {
859 time_t pwd_can_change = -1;
860 time_t pwd_must_change = -1;
861 const char *errstr;
863 if (pwd_can_change_time) {
864 errstr = "can";
865 if (pwd_time_format) {
866 struct tm tm;
867 char *ret;
869 memset(&tm, 0, sizeof(struct tm));
870 ret = strptime(pwd_can_change_time, pwd_time_format, &tm);
871 if (ret == NULL || *ret != '\0') {
872 goto error;
875 pwd_can_change = mktime(&tm);
877 if (pwd_can_change == -1) {
878 goto error;
880 } else { /* assume it is unix time */
881 errno = 0;
882 pwd_can_change = strtol(pwd_can_change_time, NULL, 10);
883 if (errno) {
884 goto error;
888 if (pwd_must_change_time) {
889 errstr = "must";
890 if (pwd_time_format) {
891 struct tm tm;
892 char *ret;
894 memset(&tm, 0, sizeof(struct tm));
895 ret = strptime(pwd_must_change_time, pwd_time_format, &tm);
896 if (ret == NULL || *ret != '\0') {
897 goto error;
900 pwd_must_change = mktime(&tm);
902 if (pwd_must_change == -1) {
903 goto error;
905 } else { /* assume it is unix time */
906 errno = 0;
907 pwd_must_change = strtol(pwd_must_change_time, NULL, 10);
908 if (errno) {
909 goto error;
913 return set_user_info (bdef, user_name, full_name, home_dir,
914 acct_desc, home_drive, logon_script, profile_path, account_control,
915 user_sid, user_domain, badpw_reset, hours_reset, pwd_can_change,
916 pwd_must_change);
917 error:
918 fprintf (stderr, "Error parsing the time in pwd-%s-change-time!\n", errstr);
919 return -1;
923 if (setparms >= 0x20) {
924 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
926 poptPrintHelp(pc, stderr, 0);
928 return 1;