s3:auth: add function to convert samu to netr_SamInfo3
[Samba/ekacnet.git] / source3 / utils / pdbedit.c
blobc6ccbc86a294b936b9b054ebf61c029d93bc720d
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"
25 #define BIT_BACKEND 0x00000004
26 #define BIT_VERBOSE 0x00000008
27 #define BIT_SPSTYLE 0x00000010
28 #define BIT_CAN_CHANGE 0x00000020
29 #define BIT_MUST_CHANGE 0x00000040
30 #define BIT_USERSIDS 0x00000080
31 #define BIT_FULLNAME 0x00000100
32 #define BIT_HOMEDIR 0x00000200
33 #define BIT_HDIRDRIVE 0x00000400
34 #define BIT_LOGSCRIPT 0x00000800
35 #define BIT_PROFILE 0x00001000
36 #define BIT_MACHINE 0x00002000
37 #define BIT_USERDOMAIN 0x00004000
38 #define BIT_USER 0x00008000
39 #define BIT_LIST 0x00010000
40 #define BIT_MODIFY 0x00020000
41 #define BIT_CREATE 0x00040000
42 #define BIT_DELETE 0x00080000
43 #define BIT_ACCPOLICY 0x00100000
44 #define BIT_ACCPOLVAL 0x00200000
45 #define BIT_ACCTCTRL 0x00400000
46 #define BIT_RESERV_7 0x00800000
47 #define BIT_IMPORT 0x01000000
48 #define BIT_EXPORT 0x02000000
49 #define BIT_FIX_INIT 0x04000000
50 #define BIT_BADPWRESET 0x08000000
51 #define BIT_LOGONHOURS 0x10000000
52 #define BIT_KICKOFFTIME 0x20000000
53 #define BIT_DESCRIPTION 0x40000000
55 #define MASK_ALWAYS_GOOD 0x0000001F
56 #define MASK_USER_GOOD 0x60405FE0
58 static int get_sid_from_cli_string(struct dom_sid *sid, const char *str_sid)
60 uint32_t rid;
62 if (!string_to_sid(sid, str_sid)) {
63 /* not a complete sid, may be a RID,
64 * try building a SID */
66 if (sscanf(str_sid, "%u", &rid) != 1) {
67 fprintf(stderr, "Error passed string is not "
68 "a complete SID or RID!\n");
69 return -1;
71 sid_compose(sid, get_global_sam_sid(), rid);
74 return 0;
77 /*********************************************************
78 Add all currently available users to another db
79 ********************************************************/
81 static int export_database (struct pdb_methods *in,
82 struct pdb_methods *out,
83 const char *username)
85 NTSTATUS status;
86 struct pdb_search *u_search;
87 struct samr_displayentry userentry;
89 DEBUG(3, ("export_database: username=\"%s\"\n", username ? username : "(NULL)"));
91 u_search = pdb_search_init(talloc_tos(), PDB_USER_SEARCH);
92 if (u_search == NULL) {
93 DEBUG(0, ("pdb_search_init failed\n"));
94 return 1;
97 if (!in->search_users(in, u_search, 0)) {
98 DEBUG(0, ("Could not start searching users\n"));
99 TALLOC_FREE(u_search);
100 return 1;
103 while (u_search->next_entry(u_search, &userentry)) {
104 struct samu *user;
105 struct samu *account;
106 struct dom_sid user_sid;
108 DEBUG(4, ("Processing account %s\n", userentry.account_name));
110 if ((username != NULL)
111 && (strcmp(username, userentry.account_name) != 0)) {
113 * ignore unwanted users
115 continue;
118 user = samu_new(talloc_tos());
119 if (user == NULL) {
120 DEBUG(0, ("talloc failed\n"));
121 break;
124 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
126 status = in->getsampwsid(in, user, &user_sid);
128 if (!NT_STATUS_IS_OK(status)) {
129 DEBUG(2, ("getsampwsid failed: %s\n",
130 nt_errstr(status)));
131 TALLOC_FREE(user);
132 continue;
135 account = samu_new(NULL);
136 if (account == NULL) {
137 fprintf(stderr, "export_database: Memory allocation "
138 "failure!\n");
139 TALLOC_FREE( user );
140 TALLOC_FREE(u_search);
141 return 1;
144 printf("Importing account for %s...", user->username);
145 status = out->getsampwnam(out, account, user->username);
147 if (NT_STATUS_IS_OK(status)) {
148 status = out->update_sam_account( out, user );
149 } else {
150 status = out->add_sam_account(out, user);
153 if ( NT_STATUS_IS_OK(status) ) {
154 printf( "ok\n");
155 } else {
156 printf( "failed\n");
159 TALLOC_FREE( account );
160 TALLOC_FREE( user );
163 TALLOC_FREE(u_search);
165 return 0;
168 /*********************************************************
169 Add all currently available group mappings to another db
170 ********************************************************/
172 static int export_groups (struct pdb_methods *in, struct pdb_methods *out)
174 GROUP_MAP *maps = NULL;
175 size_t i, entries = 0;
176 NTSTATUS status;
178 status = in->enum_group_mapping(in, get_global_sam_sid(),
179 SID_NAME_DOM_GRP, &maps, &entries, False);
181 if ( NT_STATUS_IS_ERR(status) ) {
182 fprintf(stderr, "Unable to enumerate group map entries.\n");
183 return 1;
186 for (i=0; i<entries; i++) {
187 out->add_group_mapping_entry(out, &(maps[i]));
190 SAFE_FREE( maps );
192 return 0;
195 /*********************************************************
196 Reset account policies to their default values and remove marker
197 ********************************************************/
199 static int reinit_account_policies (void)
201 int i;
203 for (i=1; decode_account_policy_name(i) != NULL; i++) {
204 uint32_t policy_value;
205 if (!account_policy_get_default(i, &policy_value)) {
206 fprintf(stderr, "Can't get default account policy\n");
207 return -1;
209 if (!account_policy_set(i, policy_value)) {
210 fprintf(stderr, "Can't set account policy in tdb\n");
211 return -1;
215 return 0;
219 /*********************************************************
220 Add all currently available account policy from tdb to one backend
221 ********************************************************/
223 static int export_account_policies (struct pdb_methods *in, struct pdb_methods *out)
225 int i;
227 for ( i=1; decode_account_policy_name(i) != NULL; i++ ) {
228 uint32_t policy_value;
229 NTSTATUS status;
231 status = in->get_account_policy(in, i, &policy_value);
233 if ( NT_STATUS_IS_ERR(status) ) {
234 fprintf(stderr, "Unable to get account policy from %s\n", in->name);
235 return -1;
238 status = out->set_account_policy(out, i, policy_value);
240 if ( NT_STATUS_IS_ERR(status) ) {
241 fprintf(stderr, "Unable to migrate account policy to %s\n", out->name);
242 return -1;
246 return 0;
250 /*********************************************************
251 Print info from sam structure
252 **********************************************************/
254 static int print_sam_info (struct samu *sam_pwent, bool verbosity, bool smbpwdstyle)
256 uid_t uid;
257 time_t tmp;
259 /* TODO: check if entry is a user or a workstation */
260 if (!sam_pwent) return -1;
262 if (verbosity) {
263 char temp[44];
264 const uint8_t *hours;
266 printf ("Unix username: %s\n", pdb_get_username(sam_pwent));
267 printf ("NT username: %s\n", pdb_get_nt_username(sam_pwent));
268 printf ("Account Flags: %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
269 printf ("User SID: %s\n",
270 sid_string_tos(pdb_get_user_sid(sam_pwent)));
271 printf ("Primary Group SID: %s\n",
272 sid_string_tos(pdb_get_group_sid(sam_pwent)));
273 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent));
274 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
275 printf ("HomeDir Drive: %s\n", pdb_get_dir_drive(sam_pwent));
276 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent));
277 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent));
278 printf ("Domain: %s\n", pdb_get_domain(sam_pwent));
279 printf ("Account desc: %s\n", pdb_get_acct_desc(sam_pwent));
280 printf ("Workstations: %s\n", pdb_get_workstations(sam_pwent));
281 printf ("Munged dial: %s\n", pdb_get_munged_dial(sam_pwent));
283 tmp = pdb_get_logon_time(sam_pwent);
284 printf ("Logon time: %s\n",
285 tmp ? http_timestring(talloc_tos(), tmp) : "0");
287 tmp = pdb_get_logoff_time(sam_pwent);
288 printf ("Logoff time: %s\n",
289 tmp ? http_timestring(talloc_tos(), tmp) : "0");
291 tmp = pdb_get_kickoff_time(sam_pwent);
292 printf ("Kickoff time: %s\n",
293 tmp ? http_timestring(talloc_tos(), tmp) : "0");
295 tmp = pdb_get_pass_last_set_time(sam_pwent);
296 printf ("Password last set: %s\n",
297 tmp ? http_timestring(talloc_tos(), tmp) : "0");
299 tmp = pdb_get_pass_can_change_time(sam_pwent);
300 printf ("Password can change: %s\n",
301 tmp ? http_timestring(talloc_tos(), tmp) : "0");
303 tmp = pdb_get_pass_must_change_time(sam_pwent);
304 printf ("Password must change: %s\n",
305 tmp ? http_timestring(talloc_tos(), tmp) : "0");
307 tmp = pdb_get_bad_password_time(sam_pwent);
308 printf ("Last bad password : %s\n",
309 tmp ? http_timestring(talloc_tos(), tmp) : "0");
310 printf ("Bad password count : %d\n",
311 pdb_get_bad_password_count(sam_pwent));
313 hours = pdb_get_hours(sam_pwent);
314 pdb_sethexhours(temp, hours);
315 printf ("Logon hours : %s\n", temp);
317 } else if (smbpwdstyle) {
318 char lm_passwd[33];
319 char nt_passwd[33];
321 uid = nametouid(pdb_get_username(sam_pwent));
322 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
323 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
325 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
326 pdb_get_username(sam_pwent),
327 (unsigned long)uid,
328 lm_passwd,
329 nt_passwd,
330 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
331 (uint32)convert_time_t_to_uint32_t(pdb_get_pass_last_set_time(sam_pwent)));
332 } else {
333 uid = nametouid(pdb_get_username(sam_pwent));
334 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid,
335 pdb_get_fullname(sam_pwent));
338 return 0;
341 /*********************************************************
342 Get an Print User Info
343 **********************************************************/
345 static int print_user_info(const char *username,
346 bool verbosity, bool smbpwdstyle)
348 struct samu *sam_pwent = NULL;
349 bool bret;
350 int ret;
352 sam_pwent = samu_new(NULL);
353 if (!sam_pwent) {
354 return -1;
357 bret = pdb_getsampwnam(sam_pwent, username);
358 if (!bret) {
359 fprintf (stderr, "Username not found!\n");
360 TALLOC_FREE(sam_pwent);
361 return -1;
364 ret = print_sam_info(sam_pwent, verbosity, smbpwdstyle);
366 TALLOC_FREE(sam_pwent);
367 return ret;
370 /*********************************************************
371 List Users
372 **********************************************************/
373 static int print_users_list(bool verbosity, bool smbpwdstyle)
375 struct pdb_search *u_search;
376 struct samr_displayentry userentry;
377 struct samu *sam_pwent;
378 TALLOC_CTX *tosctx;
379 struct dom_sid user_sid;
380 bool bret;
381 int ret;
383 tosctx = talloc_tos();
384 if (!tosctx) {
385 DEBUG(0, ("talloc failed\n"));
386 return 1;
389 u_search = pdb_search_users(tosctx, 0);
390 if (!u_search) {
391 DEBUG(0, ("User Search failed!\n"));
392 ret = 1;
393 goto done;
396 while (u_search->next_entry(u_search, &userentry)) {
398 sam_pwent = samu_new(tosctx);
399 if (sam_pwent == NULL) {
400 DEBUG(0, ("talloc failed\n"));
401 ret = 1;
402 goto done;
405 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
407 bret = pdb_getsampwsid(sam_pwent, &user_sid);
408 if (!bret) {
409 DEBUG(2, ("getsampwsid failed\n"));
410 TALLOC_FREE(sam_pwent);
411 continue;
414 if (verbosity) {
415 printf ("---------------\n");
417 print_sam_info(sam_pwent, verbosity, smbpwdstyle);
418 TALLOC_FREE(sam_pwent);
421 ret = 0;
423 done:
424 TALLOC_FREE(tosctx);
425 return ret;
428 /*********************************************************
429 Fix a list of Users for uninitialised passwords
430 **********************************************************/
431 static int fix_users_list(void)
433 struct pdb_search *u_search;
434 struct samr_displayentry userentry;
435 struct samu *sam_pwent;
436 TALLOC_CTX *tosctx;
437 struct dom_sid user_sid;
438 NTSTATUS status;
439 bool bret;
440 int ret;
442 tosctx = talloc_tos();
443 if (!tosctx) {
444 fprintf(stderr, "Out of memory!\n");
445 return 1;
448 u_search = pdb_search_users(tosctx, 0);
449 if (!u_search) {
450 fprintf(stderr, "User Search failed!\n");
451 ret = 1;
452 goto done;
455 while (u_search->next_entry(u_search, &userentry)) {
457 sam_pwent = samu_new(tosctx);
458 if (sam_pwent == NULL) {
459 fprintf(stderr, "Out of memory!\n");
460 ret = 1;
461 goto done;
464 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
466 bret = pdb_getsampwsid(sam_pwent, &user_sid);
467 if (!bret) {
468 DEBUG(2, ("getsampwsid failed\n"));
469 TALLOC_FREE(sam_pwent);
470 continue;
473 status = pdb_update_sam_account(sam_pwent);
474 if (!NT_STATUS_IS_OK(status)) {
475 printf("Update of user %s failed!\n",
476 pdb_get_username(sam_pwent));
478 TALLOC_FREE(sam_pwent);
481 ret = 0;
483 done:
484 TALLOC_FREE(tosctx);
485 return ret;
488 /*********************************************************
489 Set User Info
490 **********************************************************/
492 static int set_user_info(const char *username, const char *fullname,
493 const char *homedir, const char *acct_desc,
494 const char *drive, const char *script,
495 const char *profile, const char *account_control,
496 const char *user_sid, const char *user_domain,
497 const bool badpw, const bool hours,
498 const char *kickoff_time)
500 bool updated_autolock = False, updated_badpw = False;
501 struct samu *sam_pwent;
502 uint8_t hours_array[MAX_HOURS_LEN];
503 uint32_t hours_len;
504 uint32_t acb_flags;
505 uint32_t not_settable;
506 uint32_t new_flags;
507 struct dom_sid u_sid;
508 bool ret;
510 sam_pwent = samu_new(NULL);
511 if (!sam_pwent) {
512 return 1;
515 ret = pdb_getsampwnam(sam_pwent, username);
516 if (!ret) {
517 fprintf (stderr, "Username not found!\n");
518 TALLOC_FREE(sam_pwent);
519 return -1;
522 if (hours) {
523 hours_len = pdb_get_hours_len(sam_pwent);
524 memset(hours_array, 0xff, hours_len);
526 pdb_set_hours(sam_pwent, hours_array, PDB_CHANGED);
529 if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) {
530 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
533 if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) {
534 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
537 if (fullname)
538 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
539 if (acct_desc)
540 pdb_set_acct_desc(sam_pwent, acct_desc, PDB_CHANGED);
541 if (homedir)
542 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
543 if (drive)
544 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
545 if (script)
546 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
547 if (profile)
548 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
549 if (user_domain)
550 pdb_set_domain(sam_pwent, user_domain, PDB_CHANGED);
552 if (account_control) {
553 not_settable = ~(ACB_DISABLED | ACB_HOMDIRREQ |
554 ACB_PWNOTREQ | ACB_PWNOEXP | ACB_AUTOLOCK);
556 new_flags = pdb_decode_acct_ctrl(account_control);
558 if (new_flags & not_settable) {
559 fprintf(stderr, "Can only set [NDHLX] flags\n");
560 TALLOC_FREE(sam_pwent);
561 return -1;
564 acb_flags = pdb_get_acct_ctrl(sam_pwent);
566 pdb_set_acct_ctrl(sam_pwent,
567 (acb_flags & not_settable) | new_flags,
568 PDB_CHANGED);
570 if (user_sid) {
571 if (get_sid_from_cli_string(&u_sid, user_sid)) {
572 fprintf(stderr, "Failed to parse SID\n");
573 return -1;
575 pdb_set_user_sid(sam_pwent, &u_sid, PDB_CHANGED);
578 if (badpw) {
579 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
580 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
583 if (kickoff_time) {
584 char *endptr;
585 time_t value = get_time_t_max();
587 if (strcmp(kickoff_time, "never") != 0) {
588 uint32_t num = strtoul(kickoff_time, &endptr, 10);
590 if ((endptr == kickoff_time) || (endptr[0] != '\0')) {
591 fprintf(stderr, "Failed to parse kickoff time\n");
592 return -1;
595 value = convert_uint32_t_to_time_t(num);
598 pdb_set_kickoff_time(sam_pwent, value, PDB_CHANGED);
601 if (NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
602 print_user_info(username, True, False);
603 } else {
604 fprintf (stderr, "Unable to modify entry!\n");
605 TALLOC_FREE(sam_pwent);
606 return -1;
608 TALLOC_FREE(sam_pwent);
609 return 0;
612 static int set_machine_info(const char *machinename,
613 const char *account_control,
614 const char *machine_sid)
616 struct samu *sam_pwent = NULL;
617 TALLOC_CTX *tosctx;
618 uint32_t acb_flags;
619 uint32_t not_settable;
620 uint32_t new_flags;
621 struct dom_sid m_sid;
622 char *name;
623 int len;
624 bool ret;
626 len = strlen(machinename);
627 if (len == 0) {
628 fprintf(stderr, "No machine name given\n");
629 return -1;
632 tosctx = talloc_tos();
633 if (!tosctx) {
634 fprintf(stderr, "Out of memory!\n");
635 return -1;
638 sam_pwent = samu_new(tosctx);
639 if (!sam_pwent) {
640 return 1;
643 if (machinename[len-1] == '$') {
644 name = talloc_strdup(sam_pwent, machinename);
645 } else {
646 name = talloc_asprintf(sam_pwent, "%s$", machinename);
648 if (!name) {
649 fprintf(stderr, "Out of memory!\n");
650 TALLOC_FREE(sam_pwent);
651 return -1;
654 strlower_m(name);
656 ret = pdb_getsampwnam(sam_pwent, name);
657 if (!ret) {
658 fprintf (stderr, "Username not found!\n");
659 TALLOC_FREE(sam_pwent);
660 return -1;
663 if (account_control) {
664 not_settable = ~(ACB_DISABLED);
666 new_flags = pdb_decode_acct_ctrl(account_control);
668 if (new_flags & not_settable) {
669 fprintf(stderr, "Can only set [D] flags\n");
670 TALLOC_FREE(sam_pwent);
671 return -1;
674 acb_flags = pdb_get_acct_ctrl(sam_pwent);
676 pdb_set_acct_ctrl(sam_pwent,
677 (acb_flags & not_settable) | new_flags,
678 PDB_CHANGED);
680 if (machine_sid) {
681 if (get_sid_from_cli_string(&m_sid, machine_sid)) {
682 fprintf(stderr, "Failed to parse SID\n");
683 return -1;
685 pdb_set_user_sid(sam_pwent, &m_sid, PDB_CHANGED);
688 if (NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
689 print_user_info(name, True, False);
690 } else {
691 fprintf (stderr, "Unable to modify entry!\n");
692 TALLOC_FREE(sam_pwent);
693 return -1;
695 TALLOC_FREE(sam_pwent);
696 return 0;
699 /*********************************************************
700 Add New User
701 **********************************************************/
702 static int new_user(const char *username, const char *fullname,
703 const char *homedir, const char *drive,
704 const char *script, const char *profile,
705 char *user_sid, bool stdin_get)
707 char *pwd1 = NULL, *pwd2 = NULL;
708 char *err = NULL, *msg = NULL;
709 struct samu *sam_pwent = NULL;
710 TALLOC_CTX *tosctx;
711 NTSTATUS status;
712 struct dom_sid u_sid;
713 int flags;
714 int ret;
716 tosctx = talloc_tos();
717 if (!tosctx) {
718 fprintf(stderr, "Out of memory!\n");
719 return -1;
722 if (user_sid) {
723 if (get_sid_from_cli_string(&u_sid, user_sid)) {
724 fprintf(stderr, "Failed to parse SID\n");
725 return -1;
729 pwd1 = get_pass( "new password:", stdin_get);
730 pwd2 = get_pass( "retype new password:", stdin_get);
731 if (!pwd1 || !pwd2) {
732 fprintf(stderr, "Failed to read passwords.\n");
733 return -1;
735 ret = strcmp(pwd1, pwd2);
736 if (ret != 0) {
737 fprintf (stderr, "Passwords do not match!\n");
738 goto done;
741 flags = LOCAL_ADD_USER | LOCAL_SET_PASSWORD;
743 status = local_password_change(username, flags, pwd1, &err, &msg);
744 if (!NT_STATUS_IS_OK(status)) {
745 if (err) fprintf(stderr, "%s", err);
746 ret = -1;
747 goto done;
750 sam_pwent = samu_new(tosctx);
751 if (!sam_pwent) {
752 fprintf(stderr, "Out of memory!\n");
753 ret = -1;
754 goto done;
757 if (!pdb_getsampwnam(sam_pwent, username)) {
758 fprintf(stderr, "User %s not found!\n", username);
759 ret = -1;
760 goto done;
763 if (fullname)
764 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
765 if (homedir)
766 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
767 if (drive)
768 pdb_set_dir_drive(sam_pwent, drive, PDB_CHANGED);
769 if (script)
770 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
771 if (profile)
772 pdb_set_profile_path(sam_pwent, profile, PDB_CHANGED);
773 if (user_sid)
774 pdb_set_user_sid(sam_pwent, &u_sid, PDB_CHANGED);
776 status = pdb_update_sam_account(sam_pwent);
777 if (!NT_STATUS_IS_OK(status)) {
778 fprintf(stderr,
779 "Failed to modify entry for user %s.!\n",
780 username);
781 ret = -1;
782 goto done;
785 print_user_info(username, True, False);
786 ret = 0;
788 done:
789 if (pwd1) memset(pwd1, 0, strlen(pwd1));
790 if (pwd2) memset(pwd2, 0, strlen(pwd2));
791 SAFE_FREE(pwd1);
792 SAFE_FREE(pwd2);
793 SAFE_FREE(err);
794 SAFE_FREE(msg);
795 TALLOC_FREE(sam_pwent);
796 return ret;
799 /*********************************************************
800 Add New Machine
801 **********************************************************/
803 static int new_machine(const char *machinename, char *machine_sid)
805 char *err = NULL, *msg = NULL;
806 struct samu *sam_pwent = NULL;
807 TALLOC_CTX *tosctx;
808 NTSTATUS status;
809 struct dom_sid m_sid;
810 char *compatpwd;
811 char *name;
812 int flags;
813 int len;
814 int ret;
816 len = strlen(machinename);
817 if (len == 0) {
818 fprintf(stderr, "No machine name given\n");
819 return -1;
822 tosctx = talloc_tos();
823 if (!tosctx) {
824 fprintf(stderr, "Out of memory!\n");
825 return -1;
828 if (machine_sid) {
829 if (get_sid_from_cli_string(&m_sid, machine_sid)) {
830 fprintf(stderr, "Failed to parse SID\n");
831 return -1;
835 compatpwd = talloc_strdup(tosctx, machinename);
836 if (!compatpwd) {
837 fprintf(stderr, "Out of memory!\n");
838 return -1;
841 if (machinename[len-1] == '$') {
842 name = talloc_strdup(tosctx, machinename);
843 compatpwd[len-1] = '\0';
844 } else {
845 name = talloc_asprintf(tosctx, "%s$", machinename);
847 if (!name) {
848 fprintf(stderr, "Out of memory!\n");
849 return -1;
852 strlower_m(name);
854 flags = LOCAL_ADD_USER | LOCAL_TRUST_ACCOUNT | LOCAL_SET_PASSWORD;
856 status = local_password_change(name, flags, compatpwd, &err, &msg);
858 if (!NT_STATUS_IS_OK(status)) {
859 if (err) fprintf(stderr, "%s", err);
860 ret = -1;
863 sam_pwent = samu_new(tosctx);
864 if (!sam_pwent) {
865 fprintf(stderr, "Out of memory!\n");
866 ret = -1;
867 goto done;
870 if (!pdb_getsampwnam(sam_pwent, name)) {
871 fprintf(stderr, "Machine %s not found!\n", name);
872 ret = -1;
873 goto done;
876 if (machine_sid)
877 pdb_set_user_sid(sam_pwent, &m_sid, PDB_CHANGED);
879 status = pdb_update_sam_account(sam_pwent);
880 if (!NT_STATUS_IS_OK(status)) {
881 fprintf(stderr,
882 "Failed to modify entry for %s.!\n", name);
883 ret = -1;
884 goto done;
887 print_user_info(name, True, False);
888 ret = 0;
890 done:
891 SAFE_FREE(err);
892 SAFE_FREE(msg);
893 TALLOC_FREE(sam_pwent);
894 return ret;
897 /*********************************************************
898 Delete user entry
899 **********************************************************/
901 static int delete_user_entry(const char *username)
903 struct samu *samaccount;
905 samaccount = samu_new(NULL);
906 if (!samaccount) {
907 fprintf(stderr, "Out of memory!\n");
908 return -1;
911 if (!pdb_getsampwnam(samaccount, username)) {
912 fprintf (stderr,
913 "user %s does not exist in the passdb\n", username);
914 TALLOC_FREE(samaccount);
915 return -1;
918 if (!NT_STATUS_IS_OK(pdb_delete_sam_account(samaccount))) {
919 fprintf (stderr, "Unable to delete user %s\n", username);
920 TALLOC_FREE(samaccount);
921 return -1;
924 TALLOC_FREE(samaccount);
925 return 0;
928 /*********************************************************
929 Delete machine entry
930 **********************************************************/
932 static int delete_machine_entry(const char *machinename)
934 struct samu *samaccount = NULL;
935 const char *name;
937 if (strlen(machinename) == 0) {
938 fprintf(stderr, "No machine name given\n");
939 return -1;
942 samaccount = samu_new(NULL);
943 if (!samaccount) {
944 fprintf(stderr, "Out of memory!\n");
945 return -1;
948 if (machinename[strlen(machinename)-1] != '$') {
949 name = talloc_asprintf(samaccount, "%s$", machinename);
950 } else {
951 name = machinename;
954 if (!pdb_getsampwnam(samaccount, name)) {
955 fprintf (stderr,
956 "machine %s does not exist in the passdb\n", name);
957 return -1;
958 TALLOC_FREE(samaccount);
961 if (!NT_STATUS_IS_OK(pdb_delete_sam_account(samaccount))) {
962 fprintf (stderr, "Unable to delete machine %s\n", name);
963 TALLOC_FREE(samaccount);
964 return -1;
967 TALLOC_FREE(samaccount);
968 return 0;
971 /*********************************************************
972 Start here.
973 **********************************************************/
975 int main (int argc, char **argv)
977 static int list_users = False;
978 static int verbose = False;
979 static int spstyle = False;
980 static int machine = False;
981 static int add_user = False;
982 static int delete_user = False;
983 static int modify_user = False;
984 uint32 setparms, checkparms;
985 int opt;
986 static char *full_name = NULL;
987 static char *acct_desc = NULL;
988 static const char *user_name = NULL;
989 static char *home_dir = NULL;
990 static char *home_drive = NULL;
991 static const char *backend = NULL;
992 static char *backend_in = NULL;
993 static char *backend_out = NULL;
994 static int transfer_groups = False;
995 static int transfer_account_policies = False;
996 static int reset_account_policies = False;
997 static int force_initialised_password = False;
998 static char *logon_script = NULL;
999 static char *profile_path = NULL;
1000 static char *user_domain = NULL;
1001 static char *account_control = NULL;
1002 static char *account_policy = NULL;
1003 static char *user_sid = NULL;
1004 static char *machine_sid = NULL;
1005 static long int account_policy_value = 0;
1006 bool account_policy_value_set = False;
1007 static int badpw_reset = False;
1008 static int hours_reset = False;
1009 static char *pwd_time_format = NULL;
1010 static int pw_from_stdin = False;
1011 struct pdb_methods *bin, *bout;
1012 static char *kickoff_time = NULL;
1013 TALLOC_CTX *frame = talloc_stackframe();
1014 NTSTATUS status;
1015 poptContext pc;
1016 struct poptOption long_options[] = {
1017 POPT_AUTOHELP
1018 {"list", 'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
1019 {"verbose", 'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
1020 {"smbpasswd-style", 'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
1021 {"user", 'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
1022 {"account-desc", 'N', POPT_ARG_STRING, &acct_desc, 0, "set account description", NULL},
1023 {"fullname", 'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
1024 {"homedir", 'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
1025 {"drive", 'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
1026 {"script", 'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
1027 {"profile", 'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
1028 {"domain", 'I', POPT_ARG_STRING, &user_domain, 0, "set a users' domain", NULL},
1029 {"user SID", 'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
1030 {"machine SID", 'M', POPT_ARG_STRING, &machine_sid, 0, "set machine SID or RID", NULL},
1031 {"create", 'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
1032 {"modify", 'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
1033 {"machine", 'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
1034 {"delete", 'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
1035 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
1036 {"import", 'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
1037 {"export", 'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
1038 {"group", 'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
1039 {"policies", 'y', POPT_ARG_NONE, &transfer_account_policies, 0, "use -i and -e to move account policies between backends", NULL},
1040 {"policies-reset", 0, POPT_ARG_NONE, &reset_account_policies, 0, "restore default policies", NULL},
1041 {"account-policy", 'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
1042 {"value", 'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
1043 {"account-control", 'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
1044 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
1045 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
1046 {"logon-hours-reset", 'Z', POPT_ARG_NONE, &hours_reset, 0, "reset logon hours", NULL},
1047 {"time-format", 0, POPT_ARG_STRING, &pwd_time_format, 0, "The time format for time parameters", NULL },
1048 {"password-from-stdin", 't', POPT_ARG_NONE, &pw_from_stdin, 0, "get password from standard in", NULL},
1049 {"kickoff-time", 'K', POPT_ARG_STRING, &kickoff_time, 0, "set the kickoff time", NULL},
1050 POPT_COMMON_SAMBA
1051 POPT_TABLEEND
1054 bin = bout = NULL;
1056 load_case_tables();
1058 setup_logging("pdbedit", True);
1060 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
1061 POPT_CONTEXT_KEEP_FIRST);
1063 while((opt = poptGetNextOpt(pc)) != -1) {
1064 switch (opt) {
1065 case 'C':
1066 account_policy_value_set = True;
1067 break;
1071 poptGetArg(pc); /* Drop argv[0], the program name */
1073 if (user_name == NULL)
1074 user_name = poptGetArg(pc);
1076 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True)) {
1077 fprintf(stderr, "Can't load %s - run testparm to debug it\n", get_dyn_CONFIGFILE());
1078 exit(1);
1081 if (!init_names())
1082 exit(1);
1084 setparms = (backend ? BIT_BACKEND : 0) +
1085 (verbose ? BIT_VERBOSE : 0) +
1086 (spstyle ? BIT_SPSTYLE : 0) +
1087 (full_name ? BIT_FULLNAME : 0) +
1088 (home_dir ? BIT_HOMEDIR : 0) +
1089 (home_drive ? BIT_HDIRDRIVE : 0) +
1090 (logon_script ? BIT_LOGSCRIPT : 0) +
1091 (profile_path ? BIT_PROFILE : 0) +
1092 (user_domain ? BIT_USERDOMAIN : 0) +
1093 (machine ? BIT_MACHINE : 0) +
1094 (user_name ? BIT_USER : 0) +
1095 (list_users ? BIT_LIST : 0) +
1096 (force_initialised_password ? BIT_FIX_INIT : 0) +
1097 (user_sid ? BIT_USERSIDS : 0) +
1098 (machine_sid ? BIT_USERSIDS : 0) +
1099 (modify_user ? BIT_MODIFY : 0) +
1100 (add_user ? BIT_CREATE : 0) +
1101 (delete_user ? BIT_DELETE : 0) +
1102 (account_control ? BIT_ACCTCTRL : 0) +
1103 (account_policy ? BIT_ACCPOLICY : 0) +
1104 (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
1105 (backend_in ? BIT_IMPORT : 0) +
1106 (backend_out ? BIT_EXPORT : 0) +
1107 (badpw_reset ? BIT_BADPWRESET : 0) +
1108 (hours_reset ? BIT_LOGONHOURS : 0) +
1109 (kickoff_time ? BIT_KICKOFFTIME : 0) +
1110 (acct_desc ? BIT_DESCRIPTION : 0);
1112 if (setparms & BIT_BACKEND) {
1113 /* HACK: set the global passdb backend by overwriting globals.
1114 * This way we can use regular pdb functions for default
1115 * operations that do not involve passdb migrations */
1116 lp_set_passdb_backend(backend);
1117 } else {
1118 backend = lp_passdb_backend();
1121 if (!initialize_password_db(False, NULL)) {
1122 fprintf(stderr, "Can't initialize passdb backend.\n");
1123 exit(1);
1126 /* the lowest bit options are always accepted */
1127 checkparms = setparms & ~MASK_ALWAYS_GOOD;
1129 if (checkparms & BIT_FIX_INIT) {
1130 return fix_users_list();
1133 /* account policy operations */
1134 if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
1135 uint32_t value;
1136 enum pdb_policy_type field = account_policy_name_to_typenum(account_policy);
1137 if (field == 0) {
1138 const char **names;
1139 int count;
1140 int i;
1141 account_policy_names_list(&names, &count);
1142 fprintf(stderr, "No account policy by that name!\n");
1143 if (count !=0) {
1144 fprintf(stderr, "Account policy names are:\n");
1145 for (i = 0; i < count ; i++) {
1146 d_fprintf(stderr, "%s\n", names[i]);
1149 SAFE_FREE(names);
1150 exit(1);
1152 if (!pdb_get_account_policy(field, &value)) {
1153 fprintf(stderr, "valid account policy, but unable to fetch value!\n");
1154 if (!account_policy_value_set)
1155 exit(1);
1157 printf("account policy \"%s\" description: %s\n", account_policy, account_policy_get_desc(field));
1158 if (account_policy_value_set) {
1159 printf("account policy \"%s\" value was: %u\n", account_policy, value);
1160 if (!pdb_set_account_policy(field, account_policy_value)) {
1161 fprintf(stderr, "valid account policy, but unable to set value!\n");
1162 exit(1);
1164 printf("account policy \"%s\" value is now: %lu\n", account_policy, account_policy_value);
1165 exit(0);
1166 } else {
1167 printf("account policy \"%s\" value is: %u\n", account_policy, value);
1168 exit(0);
1172 if (reset_account_policies) {
1173 if (!reinit_account_policies()) {
1174 exit(1);
1177 exit(0);
1180 /* import and export operations */
1182 if (((checkparms & BIT_IMPORT) ||
1183 (checkparms & BIT_EXPORT)) &&
1184 !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
1186 if (backend_in) {
1187 status = make_pdb_method_name(&bin, backend_in);
1188 } else {
1189 status = make_pdb_method_name(&bin, backend);
1192 if (!NT_STATUS_IS_OK(status)) {
1193 fprintf(stderr, "Unable to initialize %s.\n",
1194 backend_in ? backend_in : backend);
1195 return 1;
1198 if (backend_out) {
1199 status = make_pdb_method_name(&bout, backend_out);
1200 } else {
1201 status = make_pdb_method_name(&bout, backend);
1204 if (!NT_STATUS_IS_OK(status)) {
1205 fprintf(stderr, "Unable to initialize %s.\n",
1206 backend_out ? backend_out : backend);
1207 return 1;
1210 if (transfer_account_policies) {
1212 if (!(checkparms & BIT_USER)) {
1213 return export_account_policies(bin, bout);
1216 } else if (transfer_groups) {
1218 if (!(checkparms & BIT_USER)) {
1219 return export_groups(bin, bout);
1222 } else {
1223 return export_database(bin, bout,
1224 (checkparms & BIT_USER) ? user_name : NULL);
1228 /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
1229 /* fake up BIT_LIST if only BIT_USER is defined */
1230 if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
1231 checkparms += BIT_LIST;
1234 /* modify flag is optional to maintain backwards compatibility */
1235 /* fake up BIT_MODIFY if BIT_USER and at least one of MASK_USER_GOOD is defined */
1236 if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
1237 checkparms += BIT_MODIFY;
1240 /* list users operations */
1241 if (checkparms & BIT_LIST) {
1242 if (!(checkparms & ~BIT_LIST)) {
1243 return print_users_list(verbose, spstyle);
1245 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
1246 return print_user_info(user_name, verbose, spstyle);
1250 /* mask out users options */
1251 checkparms &= ~MASK_USER_GOOD;
1253 /* if bad password count is reset, we must be modifying */
1254 if (checkparms & BIT_BADPWRESET) {
1255 checkparms |= BIT_MODIFY;
1256 checkparms &= ~BIT_BADPWRESET;
1259 /* if logon hours is reset, must modify */
1260 if (checkparms & BIT_LOGONHOURS) {
1261 checkparms |= BIT_MODIFY;
1262 checkparms &= ~BIT_LOGONHOURS;
1265 /* account operation */
1266 if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
1267 /* check use of -u option */
1268 if (!(checkparms & BIT_USER)) {
1269 fprintf (stderr, "Username not specified! (use -u option)\n");
1270 return -1;
1273 /* account creation operations */
1274 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
1275 if (checkparms & BIT_MACHINE) {
1276 return new_machine(user_name, machine_sid);
1277 } else {
1278 return new_user(user_name, full_name,
1279 home_dir, home_drive,
1280 logon_script, profile_path,
1281 user_sid, pw_from_stdin);
1285 /* account deletion operations */
1286 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
1287 if (checkparms & BIT_MACHINE) {
1288 return delete_machine_entry(user_name);
1289 } else {
1290 return delete_user_entry(user_name);
1294 /* account modification operations */
1295 if (!(checkparms & ~(BIT_MODIFY + BIT_USER + BIT_MACHINE))) {
1296 if (checkparms & BIT_MACHINE) {
1297 return set_machine_info(user_name,
1298 account_control,
1299 machine_sid);
1300 } else {
1301 return set_user_info(user_name, full_name,
1302 home_dir, acct_desc,
1303 home_drive, logon_script,
1304 profile_path, account_control,
1305 user_sid, user_domain,
1306 badpw_reset, hours_reset,
1307 kickoff_time);
1312 if (setparms >= 0x20) {
1313 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
1315 poptPrintHelp(pc, stderr, 0);
1317 TALLOC_FREE(frame);
1318 return 1;