r10656: BIG merge from trunk. Features not copied over
[Samba/nascimento.git] / source3 / utils / pdbedit.c
blobdacaa1e26f26a0a47ec29959ed2830b89632e01c
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_u.username));
80 if (!username ||
81 (strcmp(username, user->private_u.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 Add all currently available account policy from tdb to one backend
123 ********************************************************/
125 static int export_account_policies (struct pdb_context *in, struct pdb_context *out)
127 int i;
129 for (i=1; decode_account_policy_name(i) != NULL; i++) {
130 uint32 policy_value;
131 if (NT_STATUS_IS_ERR(in->pdb_get_account_policy(in, i, &policy_value))) {
132 fprintf(stderr, "Can't get account policy from tdb\n");
133 return -1;
135 out->pdb_set_account_policy(out, i, policy_value);
138 return 0;
142 /*********************************************************
143 Print info from sam structure
144 **********************************************************/
146 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
148 uid_t uid;
149 time_t tmp;
151 /* TODO: chaeck if entry is a user or a workstation */
152 if (!sam_pwent) return -1;
154 if (verbosity) {
155 pstring temp;
156 const uint8 *hours;
158 printf ("Unix username: %s\n", pdb_get_username(sam_pwent));
159 printf ("NT username: %s\n", pdb_get_nt_username(sam_pwent));
160 printf ("Account Flags: %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
161 printf ("User SID: %s\n",
162 sid_string_static(pdb_get_user_sid(sam_pwent)));
163 printf ("Primary Group SID: %s\n",
164 sid_string_static(pdb_get_group_sid(sam_pwent)));
165 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent));
166 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
167 printf ("HomeDir Drive: %s\n", pdb_get_dir_drive(sam_pwent));
168 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent));
169 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent));
170 printf ("Domain: %s\n", pdb_get_domain(sam_pwent));
171 printf ("Account desc: %s\n", pdb_get_acct_desc(sam_pwent));
172 printf ("Workstations: %s\n", pdb_get_workstations(sam_pwent));
173 printf ("Munged dial: %s\n", pdb_get_munged_dial(sam_pwent));
175 tmp = pdb_get_logon_time(sam_pwent);
176 printf ("Logon time: %s\n", tmp ? http_timestring(tmp) : "0");
178 tmp = pdb_get_logoff_time(sam_pwent);
179 printf ("Logoff time: %s\n", tmp ? http_timestring(tmp) : "0");
181 tmp = pdb_get_kickoff_time(sam_pwent);
182 printf ("Kickoff time: %s\n", tmp ? http_timestring(tmp) : "0");
184 tmp = pdb_get_pass_last_set_time(sam_pwent);
185 printf ("Password last set: %s\n", tmp ? http_timestring(tmp) : "0");
187 tmp = pdb_get_pass_can_change_time(sam_pwent);
188 printf ("Password can change: %s\n", tmp ? http_timestring(tmp) : "0");
190 tmp = pdb_get_pass_must_change_time(sam_pwent);
191 printf ("Password must change: %s\n", tmp ? http_timestring(tmp) : "0");
193 tmp = pdb_get_bad_password_time(sam_pwent);
194 printf ("Last bad password : %s\n", tmp ? http_timestring(tmp) : "0");
195 printf ("Bad password count : %d\n",
196 pdb_get_bad_password_count(sam_pwent));
198 hours = pdb_get_hours(sam_pwent);
199 pdb_sethexhours(temp, hours);
200 printf ("Logon hours : %s\n", temp);
202 } else if (smbpwdstyle) {
203 char lm_passwd[33];
204 char nt_passwd[33];
206 uid = nametouid(pdb_get_username(sam_pwent));
207 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
208 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
210 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
211 pdb_get_username(sam_pwent),
212 (unsigned long)uid,
213 lm_passwd,
214 nt_passwd,
215 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
216 (uint32)pdb_get_pass_last_set_time(sam_pwent));
217 } else {
218 uid = nametouid(pdb_get_username(sam_pwent));
219 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid,
220 pdb_get_fullname(sam_pwent));
223 return 0;
226 /*********************************************************
227 Get an Print User Info
228 **********************************************************/
230 static int print_user_info (struct pdb_context *in, const char *username, BOOL verbosity, BOOL smbpwdstyle)
232 SAM_ACCOUNT *sam_pwent=NULL;
233 BOOL ret;
235 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
236 return -1;
239 ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
241 if (ret==False) {
242 fprintf (stderr, "Username not found!\n");
243 pdb_free_sam(&sam_pwent);
244 return -1;
247 ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
248 pdb_free_sam(&sam_pwent);
250 return ret;
253 /*********************************************************
254 List Users
255 **********************************************************/
256 static int print_users_list (struct pdb_context *in, BOOL verbosity, BOOL smbpwdstyle)
258 SAM_ACCOUNT *sam_pwent=NULL;
259 BOOL check;
261 check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False, 0));
262 if (!check) {
263 return 1;
266 check = True;
267 if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
269 while (check && NT_STATUS_IS_OK(in->pdb_getsampwent (in, sam_pwent))) {
270 if (verbosity)
271 printf ("---------------\n");
272 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
273 pdb_free_sam(&sam_pwent);
274 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
276 if (check) pdb_free_sam(&sam_pwent);
278 in->pdb_endsampwent(in);
279 return 0;
282 /*********************************************************
283 Fix a list of Users for uninitialised passwords
284 **********************************************************/
285 static int fix_users_list (struct pdb_context *in)
287 SAM_ACCOUNT *sam_pwent=NULL;
288 BOOL check;
290 check = NT_STATUS_IS_OK(in->pdb_setsampwent(in, False, 0));
291 if (!check) {
292 return 1;
295 check = True;
296 if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
298 while (check && NT_STATUS_IS_OK(in->pdb_getsampwent (in, sam_pwent))) {
299 printf("Updating record for user %s\n", pdb_get_username(sam_pwent));
301 if (!pdb_update_sam_account(sam_pwent)) {
302 printf("Update of user %s failed!\n", pdb_get_username(sam_pwent));
304 pdb_free_sam(&sam_pwent);
305 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
306 if (!check) {
307 fprintf(stderr, "Failed to initialise new SAM_ACCOUNT structure (out of memory?)\n");
311 if (check) pdb_free_sam(&sam_pwent);
313 in->pdb_endsampwent(in);
314 return 0;
317 /*********************************************************
318 Set User Info
319 **********************************************************/
321 static int set_user_info (struct pdb_context *in, const char *username,
322 const char *fullname, const char *homedir,
323 const char *acct_desc,
324 const char *drive, const char *script,
325 const char *profile, const char *account_control,
326 const char *user_sid, const char *group_sid,
327 const BOOL badpw, const BOOL hours,
328 time_t pwd_can_change, time_t pwd_must_change)
330 BOOL updated_autolock = False, updated_badpw = False;
331 SAM_ACCOUNT *sam_pwent=NULL;
332 BOOL ret;
334 pdb_init_sam(&sam_pwent);
336 ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username));
337 if (ret==False) {
338 fprintf (stderr, "Username not found!\n");
339 pdb_free_sam(&sam_pwent);
340 return -1;
343 if (hours) {
344 uint8 hours_array[MAX_HOURS_LEN];
345 uint32 hours_len;
347 hours_len = pdb_get_hours_len(sam_pwent);
348 memset(hours_array, 0xff, hours_len);
350 pdb_set_hours(sam_pwent, hours_array, PDB_CHANGED);
353 if (pwd_can_change != -1) {
354 pdb_set_pass_can_change_time(sam_pwent, pwd_can_change, PDB_CHANGED);
357 if (pwd_must_change != -1) {
358 pdb_set_pass_must_change_time(sam_pwent, pwd_must_change, PDB_CHANGED);
361 if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) {
362 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
365 if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) {
366 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
369 if (fullname)
370 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
371 if (acct_desc)
372 pdb_set_acct_desc(sam_pwent, acct_desc, PDB_CHANGED);
373 if (homedir)
374 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
375 if (drive)
376 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
377 if (script)
378 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
379 if (profile)
380 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
382 if (account_control) {
383 uint16 not_settable = ~(ACB_DISABLED|ACB_HOMDIRREQ|ACB_PWNOTREQ|
384 ACB_PWNOEXP|ACB_AUTOLOCK);
386 uint16 newflag = pdb_decode_acct_ctrl(account_control);
388 if (newflag & not_settable) {
389 fprintf(stderr, "Can only set [NDHLX] flags\n");
390 pdb_free_sam(&sam_pwent);
391 return -1;
394 pdb_set_acct_ctrl(sam_pwent,
395 (pdb_get_acct_ctrl(sam_pwent) & not_settable) | newflag,
396 PDB_CHANGED);
398 if (user_sid) {
399 DOM_SID u_sid;
400 if (!string_to_sid(&u_sid, user_sid)) {
401 /* not a complete sid, may be a RID, try building a SID */
402 int u_rid;
404 if (sscanf(user_sid, "%d", &u_rid) != 1) {
405 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
406 return -1;
408 sid_copy(&u_sid, get_global_sam_sid());
409 sid_append_rid(&u_sid, u_rid);
411 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
413 if (group_sid) {
414 DOM_SID g_sid;
415 if (!string_to_sid(&g_sid, group_sid)) {
416 /* not a complete sid, may be a RID, try building a SID */
417 int g_rid;
419 if (sscanf(group_sid, "%d", &g_rid) != 1) {
420 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
421 return -1;
423 sid_copy(&g_sid, get_global_sam_sid());
424 sid_append_rid(&g_sid, g_rid);
426 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
429 if (badpw) {
430 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
431 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
434 if (NT_STATUS_IS_OK(in->pdb_update_sam_account (in, sam_pwent)))
435 print_user_info (in, username, True, False);
436 else {
437 fprintf (stderr, "Unable to modify entry!\n");
438 pdb_free_sam(&sam_pwent);
439 return -1;
441 pdb_free_sam(&sam_pwent);
442 return 0;
445 /*********************************************************
446 Add New User
447 **********************************************************/
448 static int new_user (struct pdb_context *in, const char *username,
449 const char *fullname, const char *homedir,
450 const char *drive, const char *script,
451 const char *profile, char *user_sid, char *group_sid)
453 SAM_ACCOUNT *sam_pwent=NULL;
455 char *password1, *password2, *staticpass;
457 get_global_sam_sid();
459 if (!NT_STATUS_IS_OK(pdb_init_sam_new(&sam_pwent, username, 0))) {
460 DEBUG(0, ("could not create account to add new user %s\n", username));
461 return -1;
464 staticpass = getpass("new password:");
465 password1 = SMB_STRDUP(staticpass);
466 memset(staticpass, 0, strlen(staticpass));
467 staticpass = getpass("retype new password:");
468 password2 = SMB_STRDUP(staticpass);
469 memset(staticpass, 0, strlen(staticpass));
470 if (strcmp (password1, password2)) {
471 fprintf (stderr, "Passwords does not match!\n");
472 memset(password1, 0, strlen(password1));
473 SAFE_FREE(password1);
474 memset(password2, 0, strlen(password2));
475 SAFE_FREE(password2);
476 pdb_free_sam (&sam_pwent);
477 return -1;
480 pdb_set_plaintext_passwd(sam_pwent, password1);
481 memset(password1, 0, strlen(password1));
482 SAFE_FREE(password1);
483 memset(password2, 0, strlen(password2));
484 SAFE_FREE(password2);
486 if (fullname)
487 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
488 if (homedir)
489 pdb_set_homedir (sam_pwent, homedir, PDB_CHANGED);
490 if (drive)
491 pdb_set_dir_drive (sam_pwent, drive, PDB_CHANGED);
492 if (script)
493 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
494 if (profile)
495 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
496 if (user_sid) {
497 DOM_SID u_sid;
498 if (!string_to_sid(&u_sid, user_sid)) {
499 /* not a complete sid, may be a RID, try building a SID */
500 int u_rid;
502 if (sscanf(user_sid, "%d", &u_rid) != 1) {
503 fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
504 return -1;
506 sid_copy(&u_sid, get_global_sam_sid());
507 sid_append_rid(&u_sid, u_rid);
509 pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED);
511 if (group_sid) {
512 DOM_SID g_sid;
513 if (!string_to_sid(&g_sid, group_sid)) {
514 /* not a complete sid, may be a RID, try building a SID */
515 int g_rid;
517 if (sscanf(group_sid, "%d", &g_rid) != 1) {
518 fprintf(stderr, "Error passed string is not a complete group SID or RID!\n");
519 return -1;
521 sid_copy(&g_sid, get_global_sam_sid());
522 sid_append_rid(&g_sid, g_rid);
524 pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED);
527 pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL, PDB_CHANGED);
529 if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) {
530 print_user_info (in, username, True, False);
531 } else {
532 fprintf (stderr, "Unable to add user! (does it already exist?)\n");
533 pdb_free_sam (&sam_pwent);
534 return -1;
536 pdb_free_sam (&sam_pwent);
537 return 0;
540 /*********************************************************
541 Add New Machine
542 **********************************************************/
544 static int new_machine (struct pdb_context *in, const char *machine_in)
546 SAM_ACCOUNT *sam_pwent=NULL;
547 fstring machinename;
548 fstring machineaccount;
549 struct passwd *pwd = NULL;
551 get_global_sam_sid();
553 fstrcpy(machinename, machine_in);
554 machinename[15]= '\0';
556 if (machinename[strlen (machinename) -1] == '$')
557 machinename[strlen (machinename) -1] = '\0';
559 strlower_m(machinename);
561 fstrcpy(machineaccount, machinename);
562 fstrcat(machineaccount, "$");
564 if ((pwd = getpwnam_alloc(machineaccount))) {
565 if (!NT_STATUS_IS_OK(pdb_init_sam_pw( &sam_pwent, pwd))) {
566 fprintf(stderr, "Could not init sam from pw\n");
567 passwd_free(&pwd);
568 return -1;
570 passwd_free(&pwd);
571 } else {
572 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
573 fprintf(stderr, "Could not init sam from pw\n");
574 return -1;
578 pdb_set_plaintext_passwd (sam_pwent, machinename);
580 pdb_set_username (sam_pwent, machineaccount, PDB_CHANGED);
582 pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST, PDB_CHANGED);
584 pdb_set_group_sid_from_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS, PDB_CHANGED);
586 if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) {
587 print_user_info (in, machineaccount, True, False);
588 } else {
589 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
590 pdb_free_sam (&sam_pwent);
591 return -1;
593 pdb_free_sam (&sam_pwent);
594 return 0;
597 /*********************************************************
598 Delete user entry
599 **********************************************************/
601 static int delete_user_entry (struct pdb_context *in, const char *username)
603 SAM_ACCOUNT *samaccount = NULL;
605 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
606 return -1;
609 if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, username))) {
610 fprintf (stderr, "user %s does not exist in the passdb\n", username);
611 return -1;
614 if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) {
615 fprintf (stderr, "Unable to delete user %s\n", username);
616 return -1;
618 return 0;
621 /*********************************************************
622 Delete machine entry
623 **********************************************************/
625 static int delete_machine_entry (struct pdb_context *in, const char *machinename)
627 fstring name;
628 SAM_ACCOUNT *samaccount = NULL;
630 fstrcpy(name, machinename);
631 name[15] = '\0';
632 if (name[strlen(name)-1] != '$')
633 fstrcat (name, "$");
635 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
636 return -1;
639 if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, name))) {
640 fprintf (stderr, "machine %s does not exist in the passdb\n", name);
641 return -1;
644 if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) {
645 fprintf (stderr, "Unable to delete machine %s\n", name);
646 return -1;
649 return 0;
652 /*********************************************************
653 Start here.
654 **********************************************************/
656 int main (int argc, char **argv)
658 static BOOL list_users = False;
659 static BOOL verbose = False;
660 static BOOL spstyle = False;
661 static BOOL machine = False;
662 static BOOL add_user = False;
663 static BOOL delete_user = False;
664 static BOOL modify_user = False;
665 uint32 setparms, checkparms;
666 int opt;
667 static char *full_name = NULL;
668 static char *acct_desc = NULL;
669 static const char *user_name = NULL;
670 static char *home_dir = NULL;
671 static char *home_drive = NULL;
672 static char *backend = NULL;
673 static char *backend_in = NULL;
674 static char *backend_out = NULL;
675 static BOOL transfer_groups = False;
676 static BOOL transfer_account_policies = False;
677 static BOOL force_initialised_password = False;
678 static char *logon_script = NULL;
679 static char *profile_path = NULL;
680 static char *account_control = NULL;
681 static char *account_policy = NULL;
682 static char *user_sid = NULL;
683 static char *group_sid = NULL;
684 static long int account_policy_value = 0;
685 BOOL account_policy_value_set = False;
686 static BOOL badpw_reset = False;
687 static BOOL hours_reset = False;
688 static char *pwd_can_change_time = NULL;
689 static char *pwd_must_change_time = NULL;
690 static char *pwd_time_format = NULL;
692 struct pdb_context *bin;
693 struct pdb_context *bout;
694 struct pdb_context *bdef;
695 poptContext pc;
696 struct poptOption long_options[] = {
697 POPT_AUTOHELP
698 {"list", 'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
699 {"verbose", 'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
700 {"smbpasswd-style", 'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
701 {"user", 'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
702 {"account-desc", 'N', POPT_ARG_STRING, &acct_desc, 0, "set account description", NULL},
703 {"fullname", 'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
704 {"homedir", 'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
705 {"drive", 'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
706 {"script", 'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
707 {"profile", 'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
708 {"user SID", 'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
709 {"group SID", 'G', POPT_ARG_STRING, &group_sid, 0, "set group SID or RID", NULL},
710 {"create", 'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
711 {"modify", 'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
712 {"machine", 'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
713 {"delete", 'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
714 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
715 {"import", 'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
716 {"export", 'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
717 {"group", 'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
718 {"policies", 'y', POPT_ARG_NONE, &transfer_account_policies, 0, "use -i and -e to move account policies between backends", NULL},
719 {"account-policy", 'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
720 {"value", 'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
721 {"account-control", 'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
722 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
723 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
724 {"logon-hours-reset", 'Z', POPT_ARG_NONE, &hours_reset, 0, "reset logon hours", NULL},
725 {"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 },
726 {"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 },
727 {"time-format", 0, POPT_ARG_STRING, &pwd_time_format, 0, "The time format for time parameters", NULL },
728 POPT_COMMON_SAMBA
729 POPT_TABLEEND
732 setup_logging("pdbedit", True);
734 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
735 POPT_CONTEXT_KEEP_FIRST);
737 while((opt = poptGetNextOpt(pc)) != -1) {
738 switch (opt) {
739 case 'C':
740 account_policy_value_set = True;
741 break;
745 poptGetArg(pc); /* Drop argv[0], the program name */
747 if (user_name == NULL)
748 user_name = poptGetArg(pc);
750 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
751 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
752 exit(1);
755 if(!initialize_password_db(False))
756 exit(1);
758 if (!init_names())
759 exit(1);
761 setparms = (backend ? BIT_BACKEND : 0) +
762 (verbose ? BIT_VERBOSE : 0) +
763 (spstyle ? BIT_SPSTYLE : 0) +
764 (full_name ? BIT_FULLNAME : 0) +
765 (home_dir ? BIT_HOMEDIR : 0) +
766 (home_drive ? BIT_HDIRDRIVE : 0) +
767 (logon_script ? BIT_LOGSCRIPT : 0) +
768 (profile_path ? BIT_PROFILE : 0) +
769 (machine ? BIT_MACHINE : 0) +
770 (user_name ? BIT_USER : 0) +
771 (list_users ? BIT_LIST : 0) +
772 (force_initialised_password ? BIT_FIX_INIT : 0) +
773 (modify_user ? BIT_MODIFY : 0) +
774 (add_user ? BIT_CREATE : 0) +
775 (delete_user ? BIT_DELETE : 0) +
776 (account_control ? BIT_ACCTCTRL : 0) +
777 (account_policy ? BIT_ACCPOLICY : 0) +
778 (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
779 (backend_in ? BIT_IMPORT : 0) +
780 (backend_out ? BIT_EXPORT : 0) +
781 (badpw_reset ? BIT_BADPWRESET : 0) +
782 (hours_reset ? BIT_LOGONHOURS : 0) +
783 (pwd_can_change_time ? BIT_CAN_CHANGE: 0) +
784 (pwd_must_change_time ? BIT_MUST_CHANGE: 0);
786 if (setparms & BIT_BACKEND) {
787 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bdef, backend))) {
788 fprintf(stderr, "Can't initialize passdb backend.\n");
789 return 1;
791 } else {
792 if (!NT_STATUS_IS_OK(make_pdb_context_list(&bdef, lp_passdb_backend()))) {
793 fprintf(stderr, "Can't initialize passdb backend.\n");
794 return 1;
798 /* the lowest bit options are always accepted */
799 checkparms = setparms & ~MASK_ALWAYS_GOOD;
801 if (checkparms & BIT_FIX_INIT) {
802 return fix_users_list(bdef);
805 /* account policy operations */
806 if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
807 uint32 value;
808 int field = account_policy_name_to_fieldnum(account_policy);
809 if (field == 0) {
810 char *apn = account_policy_names_list();
811 fprintf(stderr, "No account policy by that name\n");
812 if (apn) {
813 fprintf(stderr, "Account policy names are :\n%s\n", apn);
815 SAFE_FREE(apn);
816 exit(1);
818 if (!pdb_get_account_policy(field, &value)) {
819 fprintf(stderr, "valid account policy, but unable to fetch value!\n");
820 if (!account_policy_value_set)
821 exit(1);
823 printf("account policy \"%s\" description: %s\n", account_policy, account_policy_get_desc(field));
824 if (account_policy_value_set) {
825 printf("account policy \"%s\" value was: %u\n", account_policy, value);
826 if (!pdb_set_account_policy(field, account_policy_value)) {
827 fprintf(stderr, "valid account policy, but unable to set value!\n");
828 exit(1);
830 printf("account policy \"%s\" value is now: %lu\n", account_policy, account_policy_value);
831 exit(0);
832 } else {
833 printf("account policy \"%s\" value is: %u\n", account_policy, value);
834 exit(0);
838 /* import and export operations */
839 if (((checkparms & BIT_IMPORT) || (checkparms & BIT_EXPORT))
840 && !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
841 if (backend_in) {
842 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bin, backend_in))) {
843 fprintf(stderr, "Can't initialize passdb backend.\n");
844 return 1;
846 } else {
847 bin = bdef;
849 if (backend_out) {
850 if (!NT_STATUS_IS_OK(make_pdb_context_string(&bout, backend_out))) {
851 fprintf(stderr, "Can't initialize %s.\n", backend_out);
852 return 1;
854 } else {
855 bout = bdef;
857 if (transfer_account_policies) {
858 if (!(checkparms & BIT_USER))
859 return export_account_policies(bin, bout);
860 } else if (transfer_groups) {
861 if (!(checkparms & BIT_USER))
862 return export_groups(bin, bout);
863 } else {
864 if (checkparms & BIT_USER)
865 return export_database(bin, bout,
866 user_name);
867 else
868 return export_database(bin, bout,
869 NULL);
873 /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
874 /* fake up BIT_LIST if only BIT_USER is defined */
875 if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
876 checkparms += BIT_LIST;
879 /* modify flag is optional to maintain backwards compatibility */
880 /* fake up BIT_MODIFY if BIT_USER and at least one of MASK_USER_GOOD is defined */
881 if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
882 checkparms += BIT_MODIFY;
885 /* list users operations */
886 if (checkparms & BIT_LIST) {
887 if (!(checkparms & ~BIT_LIST)) {
888 return print_users_list (bdef, verbose, spstyle);
890 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
891 return print_user_info (bdef, user_name, verbose, spstyle);
895 /* mask out users options */
896 checkparms &= ~MASK_USER_GOOD;
898 /* if bad password count is reset, we must be modifying */
899 if (checkparms & BIT_BADPWRESET) {
900 checkparms |= BIT_MODIFY;
901 checkparms &= ~BIT_BADPWRESET;
904 /* if logon hours is reset, must modify */
905 if (checkparms & BIT_LOGONHOURS) {
906 checkparms |= BIT_MODIFY;
907 checkparms &= ~BIT_LOGONHOURS;
910 /* account operation */
911 if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
912 /* check use of -u option */
913 if (!(checkparms & BIT_USER)) {
914 fprintf (stderr, "Username not specified! (use -u option)\n");
915 return -1;
918 /* account creation operations */
919 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
920 if (checkparms & BIT_MACHINE) {
921 return new_machine (bdef, user_name);
922 } else {
923 return new_user (bdef, user_name, full_name, home_dir,
924 home_drive, logon_script,
925 profile_path, user_sid, group_sid);
929 /* account deletion operations */
930 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
931 if (checkparms & BIT_MACHINE) {
932 return delete_machine_entry (bdef, user_name);
933 } else {
934 return delete_user_entry (bdef, user_name);
938 /* account modification operations */
939 if (!(checkparms & ~(BIT_MODIFY + BIT_USER))) {
940 time_t pwd_can_change = -1;
941 time_t pwd_must_change = -1;
942 const char *errstr;
944 if (pwd_can_change_time) {
945 errstr = "can";
946 if (pwd_time_format) {
947 struct tm tm;
948 char *ret;
950 memset(&tm, 0, sizeof(struct tm));
951 ret = strptime(pwd_can_change_time, pwd_time_format, &tm);
952 if (ret == NULL || *ret != '\0') {
953 goto error;
956 pwd_can_change = mktime(&tm);
958 if (pwd_can_change == -1) {
959 goto error;
961 } else { /* assume it is unix time */
962 errno = 0;
963 pwd_can_change = strtol(pwd_can_change_time, NULL, 10);
964 if (errno) {
965 goto error;
969 if (pwd_must_change_time) {
970 errstr = "must";
971 if (pwd_time_format) {
972 struct tm tm;
973 char *ret;
975 memset(&tm, 0, sizeof(struct tm));
976 ret = strptime(pwd_must_change_time, pwd_time_format, &tm);
977 if (ret == NULL || *ret != '\0') {
978 goto error;
981 pwd_must_change = mktime(&tm);
983 if (pwd_must_change == -1) {
984 goto error;
986 } else { /* assume it is unix time */
987 errno = 0;
988 pwd_must_change = strtol(pwd_must_change_time, NULL, 10);
989 if (errno) {
990 goto error;
994 return set_user_info (bdef, user_name, full_name,
995 home_dir,
996 acct_desc,
997 home_drive,
998 logon_script,
999 profile_path, account_control,
1000 user_sid, group_sid,
1001 badpw_reset, hours_reset,
1002 pwd_can_change, pwd_must_change);
1003 error:
1004 fprintf (stderr, "Error parsing the time in pwd-%s-change-time!\n", errstr);
1005 return -1;
1009 if (setparms >= 0x20) {
1010 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
1012 poptPrintHelp(pc, stderr, 0);
1014 return 1;