param: Add default for rndc command to s3 loadparm
[Samba/wip.git] / source3 / utils / pdbedit.c
blob8e3a5a514bd80d5a04ff28706d45fbf6960e10a5
1 /*
2 Unix SMB/CIFS implementation.
3 passdb editing frontend
5 Copyright (C) Simo Sorce 2000-2009
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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "popt_common.h"
25 #include "../librpc/gen_ndr/samr.h"
26 #include "../libcli/security/security.h"
27 #include "passdb.h"
29 #define BIT_BACKEND 0x00000004
30 #define BIT_VERBOSE 0x00000008
31 #define BIT_SPSTYLE 0x00000010
32 #define BIT_CAN_CHANGE 0x00000020
33 #define BIT_MUST_CHANGE 0x00000040
34 #define BIT_USERSIDS 0x00000080
35 #define BIT_FULLNAME 0x00000100
36 #define BIT_HOMEDIR 0x00000200
37 #define BIT_HDIRDRIVE 0x00000400
38 #define BIT_LOGSCRIPT 0x00000800
39 #define BIT_PROFILE 0x00001000
40 #define BIT_MACHINE 0x00002000
41 #define BIT_USERDOMAIN 0x00004000
42 #define BIT_USER 0x00008000
43 #define BIT_LIST 0x00010000
44 #define BIT_MODIFY 0x00020000
45 #define BIT_CREATE 0x00040000
46 #define BIT_DELETE 0x00080000
47 #define BIT_ACCPOLICY 0x00100000
48 #define BIT_ACCPOLVAL 0x00200000
49 #define BIT_ACCTCTRL 0x00400000
50 #define BIT_RESERV_7 0x00800000
51 #define BIT_IMPORT 0x01000000
52 #define BIT_EXPORT 0x02000000
53 #define BIT_FIX_INIT 0x04000000
54 #define BIT_BADPWRESET 0x08000000
55 #define BIT_LOGONHOURS 0x10000000
56 #define BIT_KICKOFFTIME 0x20000000
57 #define BIT_DESCRIPTION 0x40000000
59 #define MASK_ALWAYS_GOOD 0x0000001F
60 #define MASK_USER_GOOD 0x60405FE0
62 static int get_sid_from_cli_string(struct dom_sid *sid, const char *str_sid)
64 uint32_t rid;
66 if (!string_to_sid(sid, str_sid)) {
67 /* not a complete sid, may be a RID,
68 * try building a SID */
70 if (sscanf(str_sid, "%u", &rid) != 1) {
71 fprintf(stderr, "Error passed string is not "
72 "a complete SID or RID!\n");
73 return -1;
75 sid_compose(sid, get_global_sam_sid(), rid);
78 return 0;
81 /*********************************************************
82 Add all currently available users to another db
83 ********************************************************/
85 static int export_database (struct pdb_methods *in,
86 struct pdb_methods *out,
87 const char *username)
89 NTSTATUS status;
90 struct pdb_search *u_search;
91 struct samr_displayentry userentry;
93 DEBUG(3, ("export_database: username=\"%s\"\n", username ? username : "(NULL)"));
95 u_search = pdb_search_init(talloc_tos(), PDB_USER_SEARCH);
96 if (u_search == NULL) {
97 DEBUG(0, ("pdb_search_init failed\n"));
98 return 1;
101 if (!in->search_users(in, u_search, 0)) {
102 DEBUG(0, ("Could not start searching users\n"));
103 TALLOC_FREE(u_search);
104 return 1;
107 while (u_search->next_entry(u_search, &userentry)) {
108 struct samu *user;
109 struct samu *account;
110 struct dom_sid user_sid;
112 DEBUG(4, ("Processing account %s\n", userentry.account_name));
114 if ((username != NULL)
115 && (strcmp(username, userentry.account_name) != 0)) {
117 * ignore unwanted users
119 continue;
122 user = samu_new(talloc_tos());
123 if (user == NULL) {
124 DEBUG(0, ("talloc failed\n"));
125 break;
128 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
130 status = in->getsampwsid(in, user, &user_sid);
132 if (!NT_STATUS_IS_OK(status)) {
133 DEBUG(2, ("getsampwsid failed: %s\n",
134 nt_errstr(status)));
135 TALLOC_FREE(user);
136 continue;
139 account = samu_new(NULL);
140 if (account == NULL) {
141 fprintf(stderr, "export_database: Memory allocation "
142 "failure!\n");
143 TALLOC_FREE( user );
144 TALLOC_FREE(u_search);
145 return 1;
148 printf("Importing account for %s...", user->username);
149 status = out->getsampwnam(out, account, user->username);
151 if (NT_STATUS_IS_OK(status)) {
152 status = out->update_sam_account( out, user );
153 } else {
154 status = out->add_sam_account(out, user);
157 if ( NT_STATUS_IS_OK(status) ) {
158 printf( "ok\n");
159 } else {
160 printf( "failed\n");
163 TALLOC_FREE( account );
164 TALLOC_FREE( user );
167 TALLOC_FREE(u_search);
169 return 0;
172 /*********************************************************
173 Add all currently available group mappings to another db
174 ********************************************************/
176 static int export_groups (struct pdb_methods *in, struct pdb_methods *out)
178 GROUP_MAP **maps = NULL;
179 size_t i, entries = 0;
180 NTSTATUS status;
182 status = in->enum_group_mapping(in, get_global_sam_sid(),
183 SID_NAME_DOM_GRP, &maps, &entries, False);
185 if ( NT_STATUS_IS_ERR(status) ) {
186 fprintf(stderr, "Unable to enumerate group map entries.\n");
187 return 1;
190 for (i=0; i<entries; i++) {
191 out->add_group_mapping_entry(out, maps[i]);
194 TALLOC_FREE(maps);
196 return 0;
199 /*********************************************************
200 Reset account policies to their default values and remove marker
201 ********************************************************/
203 static int reinit_account_policies (void)
205 int i;
207 for (i=1; decode_account_policy_name(i) != NULL; i++) {
208 uint32_t policy_value;
209 if (!account_policy_get_default(i, &policy_value)) {
210 fprintf(stderr, "Can't get default account policy\n");
211 return -1;
213 if (!account_policy_set(i, policy_value)) {
214 fprintf(stderr, "Can't set account policy in tdb\n");
215 return -1;
219 return 0;
223 /*********************************************************
224 Add all currently available account policy from tdb to one backend
225 ********************************************************/
227 static int export_account_policies (struct pdb_methods *in, struct pdb_methods *out)
229 int i;
231 for ( i=1; decode_account_policy_name(i) != NULL; i++ ) {
232 uint32_t policy_value;
233 NTSTATUS status;
235 status = in->get_account_policy(in, i, &policy_value);
237 if ( NT_STATUS_IS_ERR(status) ) {
238 fprintf(stderr, "Unable to get account policy from %s\n", in->name);
239 return -1;
242 status = out->set_account_policy(out, i, policy_value);
244 if ( NT_STATUS_IS_ERR(status) ) {
245 fprintf(stderr, "Unable to migrate account policy to %s\n", out->name);
246 return -1;
250 return 0;
254 /*********************************************************
255 Print info from sam structure
256 **********************************************************/
258 static int print_sam_info (struct samu *sam_pwent, bool verbosity, bool smbpwdstyle)
260 uid_t uid;
261 time_t tmp;
263 /* TODO: check if entry is a user or a workstation */
264 if (!sam_pwent) return -1;
266 if (verbosity) {
267 char temp[44];
268 const uint8_t *hours;
270 printf ("Unix username: %s\n", pdb_get_username(sam_pwent));
271 printf ("NT username: %s\n", pdb_get_nt_username(sam_pwent));
272 printf ("Account Flags: %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
273 printf ("User SID: %s\n",
274 sid_string_tos(pdb_get_user_sid(sam_pwent)));
275 printf ("Primary Group SID: %s\n",
276 sid_string_tos(pdb_get_group_sid(sam_pwent)));
277 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent));
278 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
279 printf ("HomeDir Drive: %s\n", pdb_get_dir_drive(sam_pwent));
280 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent));
281 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent));
282 printf ("Domain: %s\n", pdb_get_domain(sam_pwent));
283 printf ("Account desc: %s\n", pdb_get_acct_desc(sam_pwent));
284 printf ("Workstations: %s\n", pdb_get_workstations(sam_pwent));
285 printf ("Munged dial: %s\n", pdb_get_munged_dial(sam_pwent));
287 tmp = pdb_get_logon_time(sam_pwent);
288 printf ("Logon time: %s\n",
289 tmp ? http_timestring(talloc_tos(), tmp) : "0");
291 tmp = pdb_get_logoff_time(sam_pwent);
292 printf ("Logoff time: %s\n",
293 tmp ? http_timestring(talloc_tos(), tmp) : "0");
295 tmp = pdb_get_kickoff_time(sam_pwent);
296 printf ("Kickoff time: %s\n",
297 tmp ? http_timestring(talloc_tos(), tmp) : "0");
299 tmp = pdb_get_pass_last_set_time(sam_pwent);
300 printf ("Password last set: %s\n",
301 tmp ? http_timestring(talloc_tos(), tmp) : "0");
303 tmp = pdb_get_pass_can_change_time(sam_pwent);
304 printf ("Password can change: %s\n",
305 tmp ? http_timestring(talloc_tos(), tmp) : "0");
307 tmp = pdb_get_pass_must_change_time(sam_pwent);
308 printf ("Password must change: %s\n",
309 tmp ? http_timestring(talloc_tos(), tmp) : "0");
311 tmp = pdb_get_bad_password_time(sam_pwent);
312 printf ("Last bad password : %s\n",
313 tmp ? http_timestring(talloc_tos(), tmp) : "0");
314 printf ("Bad password count : %d\n",
315 pdb_get_bad_password_count(sam_pwent));
317 hours = pdb_get_hours(sam_pwent);
318 pdb_sethexhours(temp, hours);
319 printf ("Logon hours : %s\n", temp);
321 } else if (smbpwdstyle) {
322 char lm_passwd[33];
323 char nt_passwd[33];
325 uid = nametouid(pdb_get_username(sam_pwent));
326 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
327 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
329 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
330 pdb_get_username(sam_pwent),
331 (unsigned long)uid,
332 lm_passwd,
333 nt_passwd,
334 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
335 (uint32)convert_time_t_to_uint32_t(pdb_get_pass_last_set_time(sam_pwent)));
336 } else {
337 uid = nametouid(pdb_get_username(sam_pwent));
338 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid,
339 pdb_get_fullname(sam_pwent));
342 return 0;
345 /*********************************************************
346 Get an Print User Info
347 **********************************************************/
349 static int print_user_info(const char *username,
350 bool verbosity, bool smbpwdstyle)
352 struct samu *sam_pwent = NULL;
353 bool bret;
354 int ret;
356 sam_pwent = samu_new(NULL);
357 if (!sam_pwent) {
358 return -1;
361 bret = pdb_getsampwnam(sam_pwent, username);
362 if (!bret) {
363 fprintf (stderr, "Username not found!\n");
364 TALLOC_FREE(sam_pwent);
365 return -1;
368 ret = print_sam_info(sam_pwent, verbosity, smbpwdstyle);
370 TALLOC_FREE(sam_pwent);
371 return ret;
374 /*********************************************************
375 List Users
376 **********************************************************/
377 static int print_users_list(bool verbosity, bool smbpwdstyle)
379 struct pdb_search *u_search;
380 struct samr_displayentry userentry;
381 struct samu *sam_pwent;
382 TALLOC_CTX *tosctx;
383 struct dom_sid user_sid;
384 bool bret;
385 int ret;
387 tosctx = talloc_tos();
388 if (!tosctx) {
389 DEBUG(0, ("talloc failed\n"));
390 return 1;
393 u_search = pdb_search_users(tosctx, 0);
394 if (!u_search) {
395 DEBUG(0, ("User Search failed!\n"));
396 ret = 1;
397 goto done;
400 while (u_search->next_entry(u_search, &userentry)) {
402 sam_pwent = samu_new(tosctx);
403 if (sam_pwent == NULL) {
404 DEBUG(0, ("talloc failed\n"));
405 ret = 1;
406 goto done;
409 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
411 bret = pdb_getsampwsid(sam_pwent, &user_sid);
412 if (!bret) {
413 DEBUG(2, ("getsampwsid failed\n"));
414 TALLOC_FREE(sam_pwent);
415 continue;
418 if (verbosity) {
419 printf ("---------------\n");
421 print_sam_info(sam_pwent, verbosity, smbpwdstyle);
422 TALLOC_FREE(sam_pwent);
425 ret = 0;
427 done:
428 TALLOC_FREE(tosctx);
429 return ret;
432 /*********************************************************
433 Fix a list of Users for uninitialised passwords
434 **********************************************************/
435 static int fix_users_list(void)
437 struct pdb_search *u_search;
438 struct samr_displayentry userentry;
439 struct samu *sam_pwent;
440 TALLOC_CTX *tosctx;
441 struct dom_sid user_sid;
442 NTSTATUS status;
443 bool bret;
444 int ret;
446 tosctx = talloc_tos();
447 if (!tosctx) {
448 fprintf(stderr, "Out of memory!\n");
449 return 1;
452 u_search = pdb_search_users(tosctx, 0);
453 if (!u_search) {
454 fprintf(stderr, "User Search failed!\n");
455 ret = 1;
456 goto done;
459 while (u_search->next_entry(u_search, &userentry)) {
461 sam_pwent = samu_new(tosctx);
462 if (sam_pwent == NULL) {
463 fprintf(stderr, "Out of memory!\n");
464 ret = 1;
465 goto done;
468 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
470 bret = pdb_getsampwsid(sam_pwent, &user_sid);
471 if (!bret) {
472 DEBUG(2, ("getsampwsid failed\n"));
473 TALLOC_FREE(sam_pwent);
474 continue;
477 status = pdb_update_sam_account(sam_pwent);
478 if (!NT_STATUS_IS_OK(status)) {
479 printf("Update of user %s failed!\n",
480 pdb_get_username(sam_pwent));
482 TALLOC_FREE(sam_pwent);
485 ret = 0;
487 done:
488 TALLOC_FREE(tosctx);
489 return ret;
492 /*********************************************************
493 Set User Info
494 **********************************************************/
496 static int set_user_info(const char *username, const char *fullname,
497 const char *homedir, const char *acct_desc,
498 const char *drive, const char *script,
499 const char *profile, const char *account_control,
500 const char *user_sid, const char *user_domain,
501 const bool badpw, const bool hours,
502 const char *kickoff_time)
504 bool updated_autolock = False, updated_badpw = False;
505 struct samu *sam_pwent;
506 uint8_t hours_array[MAX_HOURS_LEN];
507 uint32_t hours_len;
508 uint32_t acb_flags;
509 uint32_t not_settable;
510 uint32_t new_flags;
511 struct dom_sid u_sid;
512 bool ret;
514 sam_pwent = samu_new(NULL);
515 if (!sam_pwent) {
516 return 1;
519 ret = pdb_getsampwnam(sam_pwent, username);
520 if (!ret) {
521 fprintf (stderr, "Username not found!\n");
522 TALLOC_FREE(sam_pwent);
523 return -1;
526 if (hours) {
527 hours_len = pdb_get_hours_len(sam_pwent);
528 memset(hours_array, 0xff, hours_len);
530 pdb_set_hours(sam_pwent, hours_array, hours_len, PDB_CHANGED);
533 if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) {
534 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
537 if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) {
538 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
541 if (fullname)
542 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
543 if (acct_desc)
544 pdb_set_acct_desc(sam_pwent, acct_desc, PDB_CHANGED);
545 if (homedir)
546 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
547 if (drive)
548 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
549 if (script)
550 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
551 if (profile)
552 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
553 if (user_domain)
554 pdb_set_domain(sam_pwent, user_domain, PDB_CHANGED);
556 if (account_control) {
557 not_settable = ~(ACB_DISABLED | ACB_HOMDIRREQ |
558 ACB_PWNOTREQ | ACB_PWNOEXP | ACB_AUTOLOCK);
560 new_flags = pdb_decode_acct_ctrl(account_control);
562 if (new_flags & not_settable) {
563 fprintf(stderr, "Can only set [NDHLX] flags\n");
564 TALLOC_FREE(sam_pwent);
565 return -1;
568 acb_flags = pdb_get_acct_ctrl(sam_pwent);
570 pdb_set_acct_ctrl(sam_pwent,
571 (acb_flags & not_settable) | new_flags,
572 PDB_CHANGED);
574 if (user_sid) {
575 if (get_sid_from_cli_string(&u_sid, user_sid)) {
576 fprintf(stderr, "Failed to parse SID\n");
577 return -1;
579 pdb_set_user_sid(sam_pwent, &u_sid, PDB_CHANGED);
582 if (badpw) {
583 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
584 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
587 if (kickoff_time) {
588 char *endptr;
589 time_t value = get_time_t_max();
591 if (strcmp(kickoff_time, "never") != 0) {
592 uint32_t num = strtoul(kickoff_time, &endptr, 10);
594 if ((endptr == kickoff_time) || (endptr[0] != '\0')) {
595 fprintf(stderr, "Failed to parse kickoff time\n");
596 return -1;
599 value = convert_uint32_t_to_time_t(num);
602 pdb_set_kickoff_time(sam_pwent, value, PDB_CHANGED);
605 if (NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
606 print_user_info(username, True, False);
607 } else {
608 fprintf (stderr, "Unable to modify entry!\n");
609 TALLOC_FREE(sam_pwent);
610 return -1;
612 TALLOC_FREE(sam_pwent);
613 return 0;
616 static int set_machine_info(const char *machinename,
617 const char *account_control,
618 const char *machine_sid)
620 struct samu *sam_pwent = NULL;
621 TALLOC_CTX *tosctx;
622 uint32_t acb_flags;
623 uint32_t not_settable;
624 uint32_t new_flags;
625 struct dom_sid m_sid;
626 char *name;
627 int len;
628 bool ret;
630 len = strlen(machinename);
631 if (len == 0) {
632 fprintf(stderr, "No machine name given\n");
633 return -1;
636 tosctx = talloc_tos();
637 if (!tosctx) {
638 fprintf(stderr, "Out of memory!\n");
639 return -1;
642 sam_pwent = samu_new(tosctx);
643 if (!sam_pwent) {
644 return 1;
647 if (machinename[len-1] == '$') {
648 name = talloc_strdup(sam_pwent, machinename);
649 } else {
650 name = talloc_asprintf(sam_pwent, "%s$", machinename);
652 if (!name) {
653 fprintf(stderr, "Out of memory!\n");
654 TALLOC_FREE(sam_pwent);
655 return -1;
658 if (!strlower_m(name)) {
659 fprintf(stderr, "strlower_m %s failed\n", name);
660 TALLOC_FREE(sam_pwent);
661 return -1;
664 ret = pdb_getsampwnam(sam_pwent, name);
665 if (!ret) {
666 fprintf (stderr, "Username not found!\n");
667 TALLOC_FREE(sam_pwent);
668 return -1;
671 if (account_control) {
672 not_settable = ~(ACB_DISABLED);
674 new_flags = pdb_decode_acct_ctrl(account_control);
676 if (new_flags & not_settable) {
677 fprintf(stderr, "Can only set [D] flags\n");
678 TALLOC_FREE(sam_pwent);
679 return -1;
682 acb_flags = pdb_get_acct_ctrl(sam_pwent);
684 pdb_set_acct_ctrl(sam_pwent,
685 (acb_flags & not_settable) | new_flags,
686 PDB_CHANGED);
688 if (machine_sid) {
689 if (get_sid_from_cli_string(&m_sid, machine_sid)) {
690 fprintf(stderr, "Failed to parse SID\n");
691 return -1;
693 pdb_set_user_sid(sam_pwent, &m_sid, PDB_CHANGED);
696 if (NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
697 print_user_info(name, True, False);
698 } else {
699 fprintf (stderr, "Unable to modify entry!\n");
700 TALLOC_FREE(sam_pwent);
701 return -1;
703 TALLOC_FREE(sam_pwent);
704 return 0;
707 /*********************************************************
708 Add New User
709 **********************************************************/
710 static int new_user(const char *username, const char *fullname,
711 const char *homedir, const char *drive,
712 const char *script, const char *profile,
713 char *user_sid, bool stdin_get)
715 char *pwd1 = NULL, *pwd2 = NULL;
716 char *err = NULL, *msg = NULL;
717 struct samu *sam_pwent = NULL;
718 TALLOC_CTX *tosctx;
719 NTSTATUS status;
720 struct dom_sid u_sid;
721 int flags;
722 int ret;
724 tosctx = talloc_tos();
725 if (!tosctx) {
726 fprintf(stderr, "Out of memory!\n");
727 return -1;
730 if (user_sid) {
731 if (get_sid_from_cli_string(&u_sid, user_sid)) {
732 fprintf(stderr, "Failed to parse SID\n");
733 return -1;
737 pwd1 = get_pass( "new password:", stdin_get);
738 pwd2 = get_pass( "retype new password:", stdin_get);
739 if (!pwd1 || !pwd2) {
740 fprintf(stderr, "Failed to read passwords.\n");
741 return -1;
743 ret = strcmp(pwd1, pwd2);
744 if (ret != 0) {
745 fprintf (stderr, "Passwords do not match!\n");
746 goto done;
749 flags = LOCAL_ADD_USER | LOCAL_SET_PASSWORD;
751 status = local_password_change(username, flags, pwd1, &err, &msg);
752 if (!NT_STATUS_IS_OK(status)) {
753 if (err) fprintf(stderr, "%s", err);
754 ret = -1;
755 goto done;
758 sam_pwent = samu_new(tosctx);
759 if (!sam_pwent) {
760 fprintf(stderr, "Out of memory!\n");
761 ret = -1;
762 goto done;
765 if (!pdb_getsampwnam(sam_pwent, username)) {
766 fprintf(stderr, "User %s not found!\n", username);
767 ret = -1;
768 goto done;
771 if (fullname)
772 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
773 if (homedir)
774 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
775 if (drive)
776 pdb_set_dir_drive(sam_pwent, drive, PDB_CHANGED);
777 if (script)
778 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
779 if (profile)
780 pdb_set_profile_path(sam_pwent, profile, PDB_CHANGED);
781 if (user_sid)
782 pdb_set_user_sid(sam_pwent, &u_sid, PDB_CHANGED);
784 status = pdb_update_sam_account(sam_pwent);
785 if (!NT_STATUS_IS_OK(status)) {
786 fprintf(stderr,
787 "Failed to modify entry for user %s.!\n",
788 username);
789 ret = -1;
790 goto done;
793 print_user_info(username, True, False);
794 ret = 0;
796 done:
797 if (pwd1) memset(pwd1, 0, strlen(pwd1));
798 if (pwd2) memset(pwd2, 0, strlen(pwd2));
799 SAFE_FREE(pwd1);
800 SAFE_FREE(pwd2);
801 SAFE_FREE(err);
802 SAFE_FREE(msg);
803 TALLOC_FREE(sam_pwent);
804 return ret;
807 /*********************************************************
808 Add New Machine
809 **********************************************************/
811 static int new_machine(const char *machinename, char *machine_sid)
813 char *err = NULL, *msg = NULL;
814 struct samu *sam_pwent = NULL;
815 TALLOC_CTX *tosctx;
816 NTSTATUS status;
817 struct dom_sid m_sid;
818 char *compatpwd;
819 char *name;
820 int flags;
821 int len;
822 int ret;
824 len = strlen(machinename);
825 if (len == 0) {
826 fprintf(stderr, "No machine name given\n");
827 return -1;
830 tosctx = talloc_tos();
831 if (!tosctx) {
832 fprintf(stderr, "Out of memory!\n");
833 return -1;
836 if (machine_sid) {
837 if (get_sid_from_cli_string(&m_sid, machine_sid)) {
838 fprintf(stderr, "Failed to parse SID\n");
839 return -1;
843 compatpwd = talloc_strdup(tosctx, machinename);
844 if (!compatpwd) {
845 fprintf(stderr, "Out of memory!\n");
846 return -1;
849 if (machinename[len-1] == '$') {
850 name = talloc_strdup(tosctx, machinename);
851 compatpwd[len-1] = '\0';
852 } else {
853 name = talloc_asprintf(tosctx, "%s$", machinename);
855 if (!name) {
856 fprintf(stderr, "Out of memory!\n");
857 return -1;
860 if (!strlower_m(name)) {
861 fprintf(stderr, "strlower_m %s failed\n", name);
862 return -1;
865 flags = LOCAL_ADD_USER | LOCAL_TRUST_ACCOUNT | LOCAL_SET_PASSWORD;
867 status = local_password_change(name, flags, compatpwd, &err, &msg);
869 if (!NT_STATUS_IS_OK(status)) {
870 if (err) fprintf(stderr, "%s", err);
871 ret = -1;
874 sam_pwent = samu_new(tosctx);
875 if (!sam_pwent) {
876 fprintf(stderr, "Out of memory!\n");
877 ret = -1;
878 goto done;
881 if (!pdb_getsampwnam(sam_pwent, name)) {
882 fprintf(stderr, "Machine %s not found!\n", name);
883 ret = -1;
884 goto done;
887 if (machine_sid)
888 pdb_set_user_sid(sam_pwent, &m_sid, PDB_CHANGED);
890 status = pdb_update_sam_account(sam_pwent);
891 if (!NT_STATUS_IS_OK(status)) {
892 fprintf(stderr,
893 "Failed to modify entry for %s.!\n", name);
894 ret = -1;
895 goto done;
898 print_user_info(name, True, False);
899 ret = 0;
901 done:
902 SAFE_FREE(err);
903 SAFE_FREE(msg);
904 TALLOC_FREE(sam_pwent);
905 return ret;
908 /*********************************************************
909 Delete user entry
910 **********************************************************/
912 static int delete_user_entry(const char *username)
914 struct samu *samaccount;
916 samaccount = samu_new(NULL);
917 if (!samaccount) {
918 fprintf(stderr, "Out of memory!\n");
919 return -1;
922 if (!pdb_getsampwnam(samaccount, username)) {
923 fprintf (stderr,
924 "user %s does not exist in the passdb\n", username);
925 TALLOC_FREE(samaccount);
926 return -1;
929 if (!NT_STATUS_IS_OK(pdb_delete_sam_account(samaccount))) {
930 fprintf (stderr, "Unable to delete user %s\n", username);
931 TALLOC_FREE(samaccount);
932 return -1;
935 TALLOC_FREE(samaccount);
936 return 0;
939 /*********************************************************
940 Delete machine entry
941 **********************************************************/
943 static int delete_machine_entry(const char *machinename)
945 struct samu *samaccount = NULL;
946 const char *name;
948 if (strlen(machinename) == 0) {
949 fprintf(stderr, "No machine name given\n");
950 return -1;
953 samaccount = samu_new(NULL);
954 if (!samaccount) {
955 fprintf(stderr, "Out of memory!\n");
956 return -1;
959 if (machinename[strlen(machinename)-1] != '$') {
960 name = talloc_asprintf(samaccount, "%s$", machinename);
961 } else {
962 name = machinename;
965 if (!pdb_getsampwnam(samaccount, name)) {
966 fprintf (stderr,
967 "machine %s does not exist in the passdb\n", name);
968 TALLOC_FREE(samaccount);
969 return -1;
972 if (!NT_STATUS_IS_OK(pdb_delete_sam_account(samaccount))) {
973 fprintf (stderr, "Unable to delete machine %s\n", name);
974 TALLOC_FREE(samaccount);
975 return -1;
978 TALLOC_FREE(samaccount);
979 return 0;
982 /*********************************************************
983 Start here.
984 **********************************************************/
986 int main (int argc, char **argv)
988 static int list_users = False;
989 static int verbose = False;
990 static int spstyle = False;
991 static int machine = False;
992 static int add_user = False;
993 static int delete_user = False;
994 static int modify_user = False;
995 uint32 setparms, checkparms;
996 int opt;
997 static char *full_name = NULL;
998 static char *acct_desc = NULL;
999 static const char *user_name = NULL;
1000 static char *home_dir = NULL;
1001 static char *home_drive = NULL;
1002 static const char *backend = NULL;
1003 static char *backend_in = NULL;
1004 static char *backend_out = NULL;
1005 static int transfer_groups = False;
1006 static int transfer_account_policies = False;
1007 static int reset_account_policies = False;
1008 static int force_initialised_password = False;
1009 static char *logon_script = NULL;
1010 static char *profile_path = NULL;
1011 static char *user_domain = NULL;
1012 static char *account_control = NULL;
1013 static char *account_policy = NULL;
1014 static char *user_sid = NULL;
1015 static char *machine_sid = NULL;
1016 static long int account_policy_value = 0;
1017 bool account_policy_value_set = False;
1018 static int badpw_reset = False;
1019 static int hours_reset = False;
1020 static char *pwd_time_format = NULL;
1021 static int pw_from_stdin = False;
1022 struct pdb_methods *bin, *bout;
1023 static char *kickoff_time = NULL;
1024 TALLOC_CTX *frame = talloc_stackframe();
1025 NTSTATUS status;
1026 poptContext pc;
1027 struct poptOption long_options[] = {
1028 POPT_AUTOHELP
1029 {"list", 'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
1030 {"verbose", 'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
1031 {"smbpasswd-style", 'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
1032 {"user", 'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
1033 {"account-desc", 'N', POPT_ARG_STRING, &acct_desc, 0, "set account description", NULL},
1034 {"fullname", 'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
1035 {"homedir", 'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
1036 {"drive", 'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
1037 {"script", 'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
1038 {"profile", 'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
1039 {"domain", 'I', POPT_ARG_STRING, &user_domain, 0, "set a users' domain", NULL},
1040 {"user SID", 'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
1041 {"machine SID", 'M', POPT_ARG_STRING, &machine_sid, 0, "set machine SID or RID", NULL},
1042 {"create", 'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
1043 {"modify", 'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
1044 {"machine", 'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
1045 {"delete", 'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
1046 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
1047 {"import", 'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
1048 {"export", 'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
1049 {"group", 'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
1050 {"policies", 'y', POPT_ARG_NONE, &transfer_account_policies, 0, "use -i and -e to move account policies between backends", NULL},
1051 {"policies-reset", 0, POPT_ARG_NONE, &reset_account_policies, 0, "restore default policies", NULL},
1052 {"account-policy", 'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
1053 {"value", 'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
1054 {"account-control", 'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
1055 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
1056 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
1057 {"logon-hours-reset", 'Z', POPT_ARG_NONE, &hours_reset, 0, "reset logon hours", NULL},
1058 {"time-format", 0, POPT_ARG_STRING, &pwd_time_format, 0, "The time format for time parameters", NULL },
1059 {"password-from-stdin", 't', POPT_ARG_NONE, &pw_from_stdin, 0, "get password from standard in", NULL},
1060 {"kickoff-time", 'K', POPT_ARG_STRING, &kickoff_time, 0, "set the kickoff time", NULL},
1061 POPT_COMMON_SAMBA
1062 POPT_TABLEEND
1065 bin = bout = NULL;
1067 load_case_tables();
1069 setup_logging("pdbedit", DEBUG_STDOUT);
1071 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
1072 POPT_CONTEXT_KEEP_FIRST);
1074 while((opt = poptGetNextOpt(pc)) != -1) {
1075 switch (opt) {
1076 case 'C':
1077 account_policy_value_set = True;
1078 break;
1082 poptGetArg(pc); /* Drop argv[0], the program name */
1084 if (user_name == NULL)
1085 user_name = poptGetArg(pc);
1087 if (!lp_load_global(get_dyn_CONFIGFILE())) {
1088 fprintf(stderr, "Can't load %s - run testparm to debug it\n", get_dyn_CONFIGFILE());
1089 exit(1);
1092 if (!init_names())
1093 exit(1);
1095 setparms = (backend ? BIT_BACKEND : 0) +
1096 (verbose ? BIT_VERBOSE : 0) +
1097 (spstyle ? BIT_SPSTYLE : 0) +
1098 (full_name ? BIT_FULLNAME : 0) +
1099 (home_dir ? BIT_HOMEDIR : 0) +
1100 (home_drive ? BIT_HDIRDRIVE : 0) +
1101 (logon_script ? BIT_LOGSCRIPT : 0) +
1102 (profile_path ? BIT_PROFILE : 0) +
1103 (user_domain ? BIT_USERDOMAIN : 0) +
1104 (machine ? BIT_MACHINE : 0) +
1105 (user_name ? BIT_USER : 0) +
1106 (list_users ? BIT_LIST : 0) +
1107 (force_initialised_password ? BIT_FIX_INIT : 0) +
1108 (user_sid ? BIT_USERSIDS : 0) +
1109 (machine_sid ? BIT_USERSIDS : 0) +
1110 (modify_user ? BIT_MODIFY : 0) +
1111 (add_user ? BIT_CREATE : 0) +
1112 (delete_user ? BIT_DELETE : 0) +
1113 (account_control ? BIT_ACCTCTRL : 0) +
1114 (account_policy ? BIT_ACCPOLICY : 0) +
1115 (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
1116 (backend_in ? BIT_IMPORT : 0) +
1117 (backend_out ? BIT_EXPORT : 0) +
1118 (badpw_reset ? BIT_BADPWRESET : 0) +
1119 (hours_reset ? BIT_LOGONHOURS : 0) +
1120 (kickoff_time ? BIT_KICKOFFTIME : 0) +
1121 (acct_desc ? BIT_DESCRIPTION : 0);
1123 if (setparms & BIT_BACKEND) {
1124 /* HACK: set the global passdb backend by overwriting globals.
1125 * This way we can use regular pdb functions for default
1126 * operations that do not involve passdb migrations */
1127 lp_set_cmdline("passdb backend", backend);
1128 } else {
1129 backend = lp_passdb_backend();
1132 if (!initialize_password_db(False, NULL)) {
1133 fprintf(stderr, "Can't initialize passdb backend.\n");
1134 exit(1);
1137 /* the lowest bit options are always accepted */
1138 checkparms = setparms & ~MASK_ALWAYS_GOOD;
1140 if (checkparms & BIT_FIX_INIT) {
1141 return fix_users_list();
1144 /* account policy operations */
1145 if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
1146 uint32_t value;
1147 enum pdb_policy_type field = account_policy_name_to_typenum(account_policy);
1148 if (field == 0) {
1149 const char **names;
1150 int count;
1151 int i;
1152 account_policy_names_list(talloc_tos(), &names, &count);
1153 fprintf(stderr, "No account policy by that name!\n");
1154 if (count !=0) {
1155 fprintf(stderr, "Account policy names are:\n");
1156 for (i = 0; i < count ; i++) {
1157 d_fprintf(stderr, "%s\n", names[i]);
1160 TALLOC_FREE(names);
1161 exit(1);
1163 if (!pdb_get_account_policy(field, &value)) {
1164 fprintf(stderr, "valid account policy, but unable to fetch value!\n");
1165 if (!account_policy_value_set)
1166 exit(1);
1168 printf("account policy \"%s\" description: %s\n", account_policy, account_policy_get_desc(field));
1169 if (account_policy_value_set) {
1170 printf("account policy \"%s\" value was: %u\n", account_policy, value);
1171 if (!pdb_set_account_policy(field, account_policy_value)) {
1172 fprintf(stderr, "valid account policy, but unable to set value!\n");
1173 exit(1);
1175 printf("account policy \"%s\" value is now: %lu\n", account_policy, account_policy_value);
1176 exit(0);
1177 } else {
1178 printf("account policy \"%s\" value is: %u\n", account_policy, value);
1179 exit(0);
1183 if (reset_account_policies) {
1184 if (reinit_account_policies()) {
1185 exit(1);
1188 exit(0);
1191 /* import and export operations */
1193 if (((checkparms & BIT_IMPORT) ||
1194 (checkparms & BIT_EXPORT)) &&
1195 !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
1197 if (backend_in) {
1198 status = make_pdb_method_name(&bin, backend_in);
1199 } else {
1200 status = make_pdb_method_name(&bin, backend);
1203 if (!NT_STATUS_IS_OK(status)) {
1204 fprintf(stderr, "Unable to initialize %s.\n",
1205 backend_in ? backend_in : backend);
1206 return 1;
1209 if (backend_out) {
1210 status = make_pdb_method_name(&bout, backend_out);
1211 } else {
1212 status = make_pdb_method_name(&bout, backend);
1215 if (!NT_STATUS_IS_OK(status)) {
1216 fprintf(stderr, "Unable to initialize %s.\n",
1217 backend_out ? backend_out : backend);
1218 return 1;
1221 if (transfer_account_policies) {
1223 if (!(checkparms & BIT_USER)) {
1224 return export_account_policies(bin, bout);
1227 } else if (transfer_groups) {
1229 if (!(checkparms & BIT_USER)) {
1230 return export_groups(bin, bout);
1233 } else {
1234 return export_database(bin, bout,
1235 (checkparms & BIT_USER) ? user_name : NULL);
1239 /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
1240 /* fake up BIT_LIST if only BIT_USER is defined */
1241 if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
1242 checkparms += BIT_LIST;
1245 /* modify flag is optional to maintain backwards compatibility */
1246 /* fake up BIT_MODIFY if BIT_USER and at least one of MASK_USER_GOOD is defined */
1247 if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
1248 checkparms += BIT_MODIFY;
1251 /* list users operations */
1252 if (checkparms & BIT_LIST) {
1253 if (!(checkparms & ~BIT_LIST)) {
1254 return print_users_list(verbose, spstyle);
1256 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
1257 return print_user_info(user_name, verbose, spstyle);
1261 /* mask out users options */
1262 checkparms &= ~MASK_USER_GOOD;
1264 /* if bad password count is reset, we must be modifying */
1265 if (checkparms & BIT_BADPWRESET) {
1266 checkparms |= BIT_MODIFY;
1267 checkparms &= ~BIT_BADPWRESET;
1270 /* if logon hours is reset, must modify */
1271 if (checkparms & BIT_LOGONHOURS) {
1272 checkparms |= BIT_MODIFY;
1273 checkparms &= ~BIT_LOGONHOURS;
1276 /* account operation */
1277 if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
1278 /* check use of -u option */
1279 if (!(checkparms & BIT_USER)) {
1280 fprintf (stderr, "Username not specified! (use -u option)\n");
1281 return -1;
1284 /* account creation operations */
1285 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
1286 if (checkparms & BIT_MACHINE) {
1287 return new_machine(user_name, machine_sid);
1288 } else {
1289 return new_user(user_name, full_name,
1290 home_dir, home_drive,
1291 logon_script, profile_path,
1292 user_sid, pw_from_stdin);
1296 /* account deletion operations */
1297 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
1298 if (checkparms & BIT_MACHINE) {
1299 return delete_machine_entry(user_name);
1300 } else {
1301 return delete_user_entry(user_name);
1305 /* account modification operations */
1306 if (!(checkparms & ~(BIT_MODIFY + BIT_USER + BIT_MACHINE))) {
1307 if (checkparms & BIT_MACHINE) {
1308 return set_machine_info(user_name,
1309 account_control,
1310 machine_sid);
1311 } else {
1312 return set_user_info(user_name, full_name,
1313 home_dir, acct_desc,
1314 home_drive, logon_script,
1315 profile_path, account_control,
1316 user_sid, user_domain,
1317 badpw_reset, hours_reset,
1318 kickoff_time);
1323 if (setparms >= 0x20) {
1324 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
1326 poptPrintHelp(pc, stderr, 0);
1328 TALLOC_FREE(frame);
1329 return 1;