r4868: Add "net rpc user RENAME"-command.
[Samba/nascimento.git] / source3 / utils / pdbedit.c
blobea2faebdff42d5aac690ea1780baaf0cedc20d6c
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_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");
69 return 1;
72 if (!NT_STATUS_IS_OK(pdb_init_sam(&user))) {
73 fprintf(stderr, "Can't initialize new SAM_ACCOUNT!\n");
74 return 1;
77 while (NT_STATUS_IS_OK(in->pdb_getsampwent(in, user))) {
78 DEBUG(4, ("Processing account %s\n",
79 user->private.username));
80 if (!username ||
81 (strcmp(username, user->private.username)
82 == 0)) {
83 out->pdb_add_sam_account(out, user);
84 if (!NT_STATUS_IS_OK(pdb_reset_sam(user))) {
85 fprintf(stderr,
86 "Can't reset SAM_ACCOUNT!\n");
87 return 1;
92 in->pdb_endsampwent(in);
94 return 0;
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;
103 int i, entries = 0;
105 if (NT_STATUS_IS_ERR(in->pdb_enum_group_mapping(in, SID_NAME_UNKNOWN,
106 &maps, &entries,
107 False))) {
108 fprintf(stderr, "Can't get group mappings!\n");
109 return 1;
112 for (i=0; i<entries; i++) {
113 out->pdb_add_group_mapping_entry(out, &(maps[i]));
116 SAFE_FREE(maps);
118 return 0;
121 /*********************************************************
122 Print info from sam structure
123 **********************************************************/
125 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
127 uid_t uid;
128 time_t tmp;
130 /* TODO: chaeck if entry is a user or a workstation */
131 if (!sam_pwent) return -1;
133 if (verbosity) {
134 pstring temp;
135 const uint8 *hours;
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) {
182 char lm_passwd[33];
183 char nt_passwd[33];
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),
191 (unsigned long)uid,
192 lm_passwd,
193 nt_passwd,
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));
196 } else {
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));
202 return 0;
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;
212 BOOL ret;
214 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
215 return -1;
218 ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
220 if (ret==False) {
221 fprintf (stderr, "Username not found!\n");
222 pdb_free_sam(&sam_pwent);
223 return -1;
226 ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
227 pdb_free_sam(&sam_pwent);
229 return ret;
232 /*********************************************************
233 List Users
234 **********************************************************/
235 static int print_users_list (struct pdb_context *in, BOOL verbosity, BOOL smbpwdstyle)
237 SAM_ACCOUNT *sam_pwent=NULL;
238 BOOL check, ret;
240 check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False, 0));
241 if (!check) {
242 return 1;
245 check = True;
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)))) {
249 if (verbosity)
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);
258 return 0;
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;
267 BOOL check, ret;
269 check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False, 0));
270 if (!check) {
271 return 1;
274 check = True;
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));
285 if (!check) {
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);
293 return 0;
296 /*********************************************************
297 Set User Info
298 **********************************************************/
300 static int set_user_info (struct pdb_context *in, const char *username,
301 const char *fullname, const char *homedir,
302 const char *drive, const char *script,
303 const char *profile, const char *account_control,
304 const char *user_sid, const char *group_sid,
305 const BOOL badpw, const BOOL hours,
306 time_t pwd_can_change, time_t pwd_must_change)
308 BOOL updated_autolock = False, updated_badpw = False;
309 SAM_ACCOUNT *sam_pwent=NULL;
310 BOOL ret;
312 pdb_init_sam(&sam_pwent);
314 ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
315 if (ret==False) {
316 fprintf (stderr, "Username not found!\n");
317 pdb_free_sam(&sam_pwent);
318 return -1;
321 if (hours) {
322 uint8 hours_array[MAX_HOURS_LEN];
323 uint32 hours_len;
325 hours_len = pdb_get_hours_len(sam_pwent);
326 memset(hours_array, 0xff, hours_len);
328 pdb_set_hours(sam_pwent, hours_array, PDB_CHANGED);
331 if (pwd_can_change != -1) {
332 pdb_set_pass_can_change_time(sam_pwent, pwd_can_change, PDB_CHANGED);
335 if (pwd_must_change != -1) {
336 pdb_set_pass_must_change_time(sam_pwent, pwd_must_change, PDB_CHANGED);
339 if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) {
340 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
343 if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) {
344 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
347 if (fullname)
348 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
349 if (homedir)
350 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
351 if (drive)
352 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
353 if (script)
354 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
355 if (profile)
356 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
358 if (account_control) {
359 uint16 not_settable = ~(ACB_DISABLED|ACB_HOMDIRREQ|ACB_PWNOTREQ|
360 ACB_PWNOEXP|ACB_AUTOLOCK);
362 uint16 newflag = pdb_decode_acct_ctrl(account_control);
364 if (newflag & not_settable) {
365 fprintf(stderr, "Can only set [NDHLX] flags\n");
366 pdb_free_sam(&sam_pwent);
367 return -1;
370 pdb_set_acct_ctrl(sam_pwent,
371 (pdb_get_acct_ctrl(sam_pwent) & not_settable) | newflag,
372 PDB_CHANGED);
374 if (user_sid) {
375 DOM_SID u_sid;
376 if (!string_to_sid(&u_sid, user_sid)) {
377 /* not a complete sid, may be a RID, try building a SID */
378 int u_rid;
380 if (sscanf(user_sid, "%d", &u_rid) != 1) {
381 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
382 return -1;
384 sid_copy(&u_sid, get_global_sam_sid());
385 sid_append_rid(&u_sid, u_rid);
387 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
389 if (group_sid) {
390 DOM_SID g_sid;
391 if (!string_to_sid(&g_sid, group_sid)) {
392 /* not a complete sid, may be a RID, try building a SID */
393 int g_rid;
395 if (sscanf(group_sid, "%d", &g_rid) != 1) {
396 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
397 return -1;
399 sid_copy(&g_sid, get_global_sam_sid());
400 sid_append_rid(&g_sid, g_rid);
402 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
405 if (badpw) {
406 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
407 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
410 if (NT_STATUS_IS_OK(in->pdb_update_sam_account (in, sam_pwent)))
411 print_user_info (in, username, True, False);
412 else {
413 fprintf (stderr, "Unable to modify entry!\n");
414 pdb_free_sam(&sam_pwent);
415 return -1;
417 pdb_free_sam(&sam_pwent);
418 return 0;
421 /*********************************************************
422 Add New User
423 **********************************************************/
424 static int new_user (struct pdb_context *in, const char *username,
425 const char *fullname, const char *homedir,
426 const char *drive, const char *script,
427 const char *profile, char *user_sid, char *group_sid)
429 SAM_ACCOUNT *sam_pwent=NULL;
430 NTSTATUS nt_status;
431 char *password1, *password2, *staticpass;
433 get_global_sam_sid();
435 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam_new(&sam_pwent, username, 0))) {
436 DEBUG(0, ("could not create account to add new user %s\n", username));
437 return -1;
440 staticpass = getpass("new password:");
441 password1 = SMB_STRDUP(staticpass);
442 memset(staticpass, 0, strlen(staticpass));
443 staticpass = getpass("retype new password:");
444 password2 = SMB_STRDUP(staticpass);
445 memset(staticpass, 0, strlen(staticpass));
446 if (strcmp (password1, password2)) {
447 fprintf (stderr, "Passwords does not match!\n");
448 memset(password1, 0, strlen(password1));
449 SAFE_FREE(password1);
450 memset(password2, 0, strlen(password2));
451 SAFE_FREE(password2);
452 pdb_free_sam (&sam_pwent);
453 return -1;
456 pdb_set_plaintext_passwd(sam_pwent, password1);
457 memset(password1, 0, strlen(password1));
458 SAFE_FREE(password1);
459 memset(password2, 0, strlen(password2));
460 SAFE_FREE(password2);
462 if (fullname)
463 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
464 if (homedir)
465 pdb_set_homedir (sam_pwent, homedir, PDB_CHANGED);
466 if (drive)
467 pdb_set_dir_drive (sam_pwent, drive, PDB_CHANGED);
468 if (script)
469 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
470 if (profile)
471 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
472 if (user_sid) {
473 DOM_SID u_sid;
474 if (!string_to_sid(&u_sid, user_sid)) {
475 /* not a complete sid, may be a RID, try building a SID */
476 int u_rid;
478 if (sscanf(user_sid, "%d", &u_rid) != 1) {
479 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
480 return -1;
482 sid_copy(&u_sid, get_global_sam_sid());
483 sid_append_rid(&u_sid, u_rid);
485 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
487 if (group_sid) {
488 DOM_SID g_sid;
489 if (!string_to_sid(&g_sid, group_sid)) {
490 /* not a complete sid, may be a RID, try building a SID */
491 int g_rid;
493 if (sscanf(group_sid, "%d", &g_rid) != 1) {
494 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
495 return -1;
497 sid_copy(&g_sid, get_global_sam_sid());
498 sid_append_rid(&g_sid, g_rid);
500 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
503 pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL, PDB_CHANGED);
505 if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) {
506 print_user_info (in, username, True, False);
507 } else {
508 fprintf (stderr, "Unable to add user! (does it already exist?)\n");
509 pdb_free_sam (&sam_pwent);
510 return -1;
512 pdb_free_sam (&sam_pwent);
513 return 0;
516 /*********************************************************
517 Add New Machine
518 **********************************************************/
520 static int new_machine (struct pdb_context *in, const char *machine_in)
522 SAM_ACCOUNT *sam_pwent=NULL;
523 fstring machinename;
524 fstring machineaccount;
525 struct passwd *pwd = NULL;
527 get_global_sam_sid();
529 fstrcpy(machinename, machine_in);
530 machinename[15]= '\0';
532 if (machinename[strlen (machinename) -1] == '$')
533 machinename[strlen (machinename) -1] = '\0';
535 strlower_m(machinename);
537 fstrcpy(machineaccount, machinename);
538 fstrcat(machineaccount, "$");
540 if ((pwd = getpwnam_alloc(machineaccount))) {
541 if (!NT_STATUS_IS_OK(pdb_init_sam_pw( &sam_pwent, pwd))) {
542 fprintf(stderr, "Could not init sam from pw\n");
543 passwd_free(&pwd);
544 return -1;
546 passwd_free(&pwd);
547 } else {
548 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
549 fprintf(stderr, "Could not init sam from pw\n");
550 return -1;
554 pdb_set_plaintext_passwd (sam_pwent, machinename);
556 pdb_set_username (sam_pwent, machineaccount, PDB_CHANGED);
558 pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST, PDB_CHANGED);
560 pdb_set_group_sid_from_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS, PDB_CHANGED);
562 if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) {
563 print_user_info (in, machineaccount, True, False);
564 } else {
565 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
566 pdb_free_sam (&sam_pwent);
567 return -1;
569 pdb_free_sam (&sam_pwent);
570 return 0;
573 /*********************************************************
574 Delete user entry
575 **********************************************************/
577 static int delete_user_entry (struct pdb_context *in, const char *username)
579 SAM_ACCOUNT *samaccount = NULL;
581 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
582 return -1;
585 if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, username))) {
586 fprintf (stderr, "user %s does not exist in the passdb\n", username);
587 return -1;
590 if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) {
591 fprintf (stderr, "Unable to delete user %s\n", username);
592 return -1;
594 return 0;
597 /*********************************************************
598 Delete machine entry
599 **********************************************************/
601 static int delete_machine_entry (struct pdb_context *in, const char *machinename)
603 fstring name;
604 SAM_ACCOUNT *samaccount = NULL;
606 fstrcpy(name, machinename);
607 name[15] = '\0';
608 if (name[strlen(name)-1] != '$')
609 fstrcat (name, "$");
611 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
612 return -1;
615 if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, name))) {
616 fprintf (stderr, "machine %s does not exist in the passdb\n", name);
617 return -1;
620 if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) {
621 fprintf (stderr, "Unable to delete machine %s\n", name);
622 return -1;
625 return 0;
628 /*********************************************************
629 Start here.
630 **********************************************************/
632 int main (int argc, char **argv)
634 static BOOL list_users = False;
635 static BOOL verbose = False;
636 static BOOL spstyle = False;
637 static BOOL machine = False;
638 static BOOL add_user = False;
639 static BOOL delete_user = False;
640 static BOOL modify_user = False;
641 uint32 setparms, checkparms;
642 int opt;
643 static char *full_name = NULL;
644 static const char *user_name = NULL;
645 static char *home_dir = NULL;
646 static char *home_drive = NULL;
647 static char *backend = NULL;
648 static char *backend_in = NULL;
649 static char *backend_out = NULL;
650 static BOOL transfer_groups = False;
651 static BOOL force_initialised_password = False;
652 static char *logon_script = NULL;
653 static char *profile_path = NULL;
654 static char *account_control = NULL;
655 static char *account_policy = NULL;
656 static char *user_sid = NULL;
657 static char *group_sid = NULL;
658 static long int account_policy_value = 0;
659 BOOL account_policy_value_set = False;
660 static BOOL badpw_reset = False;
661 static BOOL hours_reset = False;
662 static char *pwd_can_change_time = NULL;
663 static char *pwd_must_change_time = NULL;
664 static char *pwd_time_format = NULL;
666 struct pdb_context *bin;
667 struct pdb_context *bout;
668 struct pdb_context *bdef;
669 poptContext pc;
670 struct poptOption long_options[] = {
671 POPT_AUTOHELP
672 {"list", 'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
673 {"verbose", 'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
674 {"smbpasswd-style", 'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
675 {"user", 'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
676 {"fullname", 'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
677 {"homedir", 'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
678 {"drive", 'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
679 {"script", 'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
680 {"profile", 'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
681 {"user SID", 'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
682 {"group SID", 'G', POPT_ARG_STRING, &group_sid, 0, "set group SID or RID", NULL},
683 {"create", 'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
684 {"modify", 'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
685 {"machine", 'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
686 {"delete", 'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
687 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
688 {"import", 'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
689 {"export", 'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
690 {"group", 'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
691 {"account-policy", 'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
692 {"value", 'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
693 {"account-control", 'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
694 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
695 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
696 {"logon-hours-reset", 'Z', POPT_ARG_NONE, &hours_reset, 0, "reset logon hours", NULL},
697 {"pwd-can-change-time", 0, POPT_ARG_STRING, &pwd_can_change_time, 0, "Set password can change time (unix time if time format no provided)", NULL },
698 {"pwd-must-change-time", 0, POPT_ARG_STRING, &pwd_must_change_time, 0, "Set password can change time (unix time if time format no provided)", NULL },
699 {"time-format", 0, POPT_ARG_STRING, &pwd_time_format, 0, "The time format for time parameters", NULL },
700 POPT_COMMON_SAMBA
701 POPT_TABLEEND
704 setup_logging("pdbedit", True);
706 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
707 POPT_CONTEXT_KEEP_FIRST);
709 while((opt = poptGetNextOpt(pc)) != -1) {
710 switch (opt) {
711 case 'C':
712 account_policy_value_set = True;
713 break;
717 poptGetArg(pc); /* Drop argv[0], the program name */
719 if (user_name == NULL)
720 user_name = poptGetArg(pc);
722 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
723 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
724 exit(1);
727 if(!initialize_password_db(False))
728 exit(1);
730 if (!init_names())
731 exit(1);
733 setparms = (backend ? BIT_BACKEND : 0) +
734 (verbose ? BIT_VERBOSE : 0) +
735 (spstyle ? BIT_SPSTYLE : 0) +
736 (full_name ? BIT_FULLNAME : 0) +
737 (home_dir ? BIT_HOMEDIR : 0) +
738 (home_drive ? BIT_HDIRDRIVE : 0) +
739 (logon_script ? BIT_LOGSCRIPT : 0) +
740 (profile_path ? BIT_PROFILE : 0) +
741 (machine ? BIT_MACHINE : 0) +
742 (user_name ? BIT_USER : 0) +
743 (list_users ? BIT_LIST : 0) +
744 (force_initialised_password ? BIT_FIX_INIT : 0) +
745 (modify_user ? BIT_MODIFY : 0) +
746 (add_user ? BIT_CREATE : 0) +
747 (delete_user ? BIT_DELETE : 0) +
748 (account_control ? BIT_ACCTCTRL : 0) +
749 (account_policy ? BIT_ACCPOLICY : 0) +
750 (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
751 (backend_in ? BIT_IMPORT : 0) +
752 (backend_out ? BIT_EXPORT : 0) +
753 (badpw_reset ? BIT_BADPWRESET : 0) +
754 (hours_reset ? BIT_LOGONHOURS : 0) +
755 (pwd_can_change_time ? BIT_CAN_CHANGE: 0) +
756 (pwd_must_change_time ? BIT_MUST_CHANGE: 0);
758 if (setparms & BIT_BACKEND) {
759 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bdef, backend))) {
760 fprintf(stderr, "Can't initialize passdb backend.\n");
761 return 1;
763 } else {
764 if (!NT_STATUS_IS_OK(make_pdb_context_list(&bdef, lp_passdb_backend()))) {
765 fprintf(stderr, "Can't initialize passdb backend.\n");
766 return 1;
770 /* the lowest bit options are always accepted */
771 checkparms = setparms & ~MASK_ALWAYS_GOOD;
773 if (checkparms & BIT_FIX_INIT) {
774 return fix_users_list(bdef);
777 /* account policy operations */
778 if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
779 uint32 value;
780 int field = account_policy_name_to_fieldnum(account_policy);
781 if (field == 0) {
782 char *apn = account_policy_names_list();
783 fprintf(stderr, "No account policy by that name\n");
784 if (apn) {
785 fprintf(stderr, "Account policy names are :\n%s\n", apn);
787 SAFE_FREE(apn);
788 exit(1);
790 if (!account_policy_get(field, &value)) {
791 fprintf(stderr, "valid account policy, but unable to fetch value!\n");
792 exit(1);
794 if (account_policy_value_set) {
795 printf("account policy value for %s was %u\n", account_policy, value);
796 if (!account_policy_set(field, account_policy_value)) {
797 fprintf(stderr, "valid account policy, but unable to set value!\n");
798 exit(1);
800 printf("account policy value for %s is now %lu\n", account_policy, account_policy_value);
801 exit(0);
802 } else {
803 printf("account policy value for %s is %u\n", account_policy, value);
804 exit(0);
808 /* import and export operations */
809 if (((checkparms & BIT_IMPORT) || (checkparms & BIT_EXPORT))
810 && !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
811 if (backend_in) {
812 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bin, backend_in))) {
813 fprintf(stderr, "Can't initialize passdb backend.\n");
814 return 1;
816 } else {
817 bin = bdef;
819 if (backend_out) {
820 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bout, backend_out))) {
821 fprintf(stderr, "Can't initialize %s.\n", backend_out);
822 return 1;
824 } else {
825 bout = bdef;
827 if (transfer_groups) {
828 if (!(checkparms & BIT_USER))
829 return export_groups(bin, bout);
830 } else {
831 if (checkparms & BIT_USER)
832 return export_database(bin, bout,
833 user_name);
834 else
835 return export_database(bin, bout,
836 NULL);
840 /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
841 /* fake up BIT_LIST if only BIT_USER is defined */
842 if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
843 checkparms += BIT_LIST;
846 /* modify flag is optional to maintain backwards compatibility */
847 /* fake up BIT_MODIFY if BIT_USER and at least one of MASK_USER_GOOD is defined */
848 if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
849 checkparms += BIT_MODIFY;
852 /* list users operations */
853 if (checkparms & BIT_LIST) {
854 if (!(checkparms & ~BIT_LIST)) {
855 return print_users_list (bdef, verbose, spstyle);
857 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
858 return print_user_info (bdef, user_name, verbose, spstyle);
862 /* mask out users options */
863 checkparms &= ~MASK_USER_GOOD;
865 /* if bad password count is reset, we must be modifying */
866 if (checkparms & BIT_BADPWRESET) {
867 checkparms |= BIT_MODIFY;
868 checkparms &= ~BIT_BADPWRESET;
871 /* if logon hours is reset, must modify */
872 if (checkparms & BIT_LOGONHOURS) {
873 checkparms |= BIT_MODIFY;
874 checkparms &= ~BIT_LOGONHOURS;
877 /* account operation */
878 if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
879 /* check use of -u option */
880 if (!(checkparms & BIT_USER)) {
881 fprintf (stderr, "Username not specified! (use -u option)\n");
882 return -1;
885 /* account creation operations */
886 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
887 if (checkparms & BIT_MACHINE) {
888 return new_machine (bdef, user_name);
889 } else {
890 return new_user (bdef, user_name, full_name, home_dir,
891 home_drive, logon_script,
892 profile_path, user_sid, group_sid);
896 /* account deletion operations */
897 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
898 if (checkparms & BIT_MACHINE) {
899 return delete_machine_entry (bdef, user_name);
900 } else {
901 return delete_user_entry (bdef, user_name);
905 /* account modification operations */
906 if (!(checkparms & ~(BIT_MODIFY + BIT_USER))) {
907 time_t pwd_can_change = -1;
908 time_t pwd_must_change = -1;
909 char *errstr;
911 if (pwd_can_change_time) {
912 errstr = "can";
913 if (pwd_time_format) {
914 struct tm tm;
915 char *ret;
917 memset(&tm, 0, sizeof(struct tm));
918 ret = strptime(pwd_can_change_time, pwd_time_format, &tm);
919 if (ret == NULL || *ret != '\0') {
920 goto error;
923 pwd_can_change = mktime(&tm);
925 if (pwd_can_change == -1) {
926 goto error;
928 } else { /* assume it is unix time */
929 errno = 0;
930 pwd_can_change = strtol(pwd_can_change_time, NULL, 10);
931 if (errno) {
932 goto error;
936 if (pwd_must_change_time) {
937 errstr = "must";
938 if (pwd_time_format) {
939 struct tm tm;
940 char *ret;
942 memset(&tm, 0, sizeof(struct tm));
943 ret = strptime(pwd_must_change_time, pwd_time_format, &tm);
944 if (ret == NULL || *ret != '\0') {
945 goto error;
948 pwd_must_change = mktime(&tm);
950 if (pwd_must_change == -1) {
951 goto error;
953 } else { /* assume it is unix time */
954 errno = 0;
955 pwd_must_change = strtol(pwd_must_change_time, NULL, 10);
956 if (errno) {
957 goto error;
961 return set_user_info (bdef, user_name, full_name,
962 home_dir,
963 home_drive,
964 logon_script,
965 profile_path, account_control,
966 user_sid, group_sid,
967 badpw_reset, hours_reset,
968 pwd_can_change, pwd_must_change);
969 error:
970 fprintf (stderr, "Error parsing the time in pwd-%s-change-time!\n", errstr);
971 return -1;
975 if (setparms >= 0x20) {
976 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
978 poptPrintHelp(pc, stderr, 0);
980 return 1;