vfs_aio_fork: Use a shorter random delay
[Samba.git] / source3 / utils / pdbedit.c
bloba353bae7c4eb31b108a323b07fed2e6e57dd63d4
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
58 #define BIT_PWSETNTHASH 0x80000000
60 #define MASK_ALWAYS_GOOD 0x0000001F
61 #define MASK_USER_GOOD 0xE0405FE0
63 static int get_sid_from_cli_string(struct dom_sid *sid, const char *str_sid)
65 uint32_t rid;
67 if (!string_to_sid(sid, str_sid)) {
68 /* not a complete sid, may be a RID,
69 * try building a SID */
71 if (sscanf(str_sid, "%u", &rid) != 1) {
72 fprintf(stderr, "Error passed string is not "
73 "a complete SID or RID!\n");
74 return -1;
76 sid_compose(sid, get_global_sam_sid(), rid);
79 return 0;
82 /*********************************************************
83 Add all currently available users to another db
84 ********************************************************/
86 static int export_database (struct pdb_methods *in,
87 struct pdb_methods *out,
88 const char *username)
90 NTSTATUS status;
91 struct pdb_search *u_search;
92 struct samr_displayentry userentry;
94 DEBUG(3, ("export_database: username=\"%s\"\n", username ? username : "(NULL)"));
96 u_search = pdb_search_init(talloc_tos(), PDB_USER_SEARCH);
97 if (u_search == NULL) {
98 DEBUG(0, ("pdb_search_init failed\n"));
99 return 1;
102 if (!in->search_users(in, u_search, 0)) {
103 DEBUG(0, ("Could not start searching users\n"));
104 TALLOC_FREE(u_search);
105 return 1;
108 while (u_search->next_entry(u_search, &userentry)) {
109 struct samu *user;
110 struct samu *account;
111 struct dom_sid user_sid;
113 DEBUG(4, ("Processing account %s\n", userentry.account_name));
115 if ((username != NULL)
116 && (strcmp(username, userentry.account_name) != 0)) {
118 * ignore unwanted users
120 continue;
123 user = samu_new(talloc_tos());
124 if (user == NULL) {
125 DEBUG(0, ("talloc failed\n"));
126 break;
129 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
131 status = in->getsampwsid(in, user, &user_sid);
133 if (!NT_STATUS_IS_OK(status)) {
134 DEBUG(2, ("getsampwsid failed: %s\n",
135 nt_errstr(status)));
136 TALLOC_FREE(user);
137 continue;
140 account = samu_new(NULL);
141 if (account == NULL) {
142 fprintf(stderr, "export_database: Memory allocation "
143 "failure!\n");
144 TALLOC_FREE( user );
145 TALLOC_FREE(u_search);
146 return 1;
149 printf("Importing account for %s...", user->username);
150 status = out->getsampwnam(out, account, user->username);
152 if (NT_STATUS_IS_OK(status)) {
153 status = out->update_sam_account( out, user );
154 } else {
155 status = out->add_sam_account(out, user);
158 if ( NT_STATUS_IS_OK(status) ) {
159 printf( "ok\n");
160 } else {
161 printf( "failed\n");
164 TALLOC_FREE( account );
165 TALLOC_FREE( user );
168 TALLOC_FREE(u_search);
170 return 0;
173 /*********************************************************
174 Add all currently available group mappings to another db
175 ********************************************************/
177 static int export_groups (struct pdb_methods *in, struct pdb_methods *out)
179 GROUP_MAP **maps = NULL;
180 size_t i, entries = 0;
181 NTSTATUS status;
183 status = in->enum_group_mapping(in, get_global_sam_sid(),
184 SID_NAME_DOM_GRP, &maps, &entries, False);
186 if ( NT_STATUS_IS_ERR(status) ) {
187 fprintf(stderr, "Unable to enumerate group map entries.\n");
188 return 1;
191 for (i=0; i<entries; i++) {
192 out->add_group_mapping_entry(out, maps[i]);
195 TALLOC_FREE(maps);
197 return 0;
200 /*********************************************************
201 Reset account policies to their default values and remove marker
202 ********************************************************/
204 static int reinit_account_policies (void)
206 int i;
208 for (i=1; decode_account_policy_name(i) != NULL; i++) {
209 uint32_t policy_value;
210 if (!account_policy_get_default(i, &policy_value)) {
211 fprintf(stderr, "Can't get default account policy\n");
212 return -1;
214 if (!account_policy_set(i, policy_value)) {
215 fprintf(stderr, "Can't set account policy in tdb\n");
216 return -1;
220 return 0;
224 /*********************************************************
225 Add all currently available account policy from tdb to one backend
226 ********************************************************/
228 static int export_account_policies (struct pdb_methods *in, struct pdb_methods *out)
230 int i;
232 for ( i=1; decode_account_policy_name(i) != NULL; i++ ) {
233 uint32_t policy_value;
234 NTSTATUS status;
236 status = in->get_account_policy(in, i, &policy_value);
238 if ( NT_STATUS_IS_ERR(status) ) {
239 fprintf(stderr, "Unable to get account policy from %s\n", in->name);
240 return -1;
243 status = out->set_account_policy(out, i, policy_value);
245 if ( NT_STATUS_IS_ERR(status) ) {
246 fprintf(stderr, "Unable to migrate account policy to %s\n", out->name);
247 return -1;
251 return 0;
255 /*********************************************************
256 Print info from sam structure
257 **********************************************************/
259 static int print_sam_info (struct samu *sam_pwent, bool verbosity, bool smbpwdstyle)
261 uid_t uid;
262 time_t tmp;
264 /* TODO: check if entry is a user or a workstation */
265 if (!sam_pwent) return -1;
267 if (verbosity) {
268 char temp[44];
269 const uint8_t *hours;
271 printf ("Unix username: %s\n", pdb_get_username(sam_pwent));
272 printf ("NT username: %s\n", pdb_get_nt_username(sam_pwent));
273 printf ("Account Flags: %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
274 printf ("User SID: %s\n",
275 sid_string_tos(pdb_get_user_sid(sam_pwent)));
276 printf ("Primary Group SID: %s\n",
277 sid_string_tos(pdb_get_group_sid(sam_pwent)));
278 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent));
279 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
280 printf ("HomeDir Drive: %s\n", pdb_get_dir_drive(sam_pwent));
281 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent));
282 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent));
283 printf ("Domain: %s\n", pdb_get_domain(sam_pwent));
284 printf ("Account desc: %s\n", pdb_get_acct_desc(sam_pwent));
285 printf ("Workstations: %s\n", pdb_get_workstations(sam_pwent));
286 printf ("Munged dial: %s\n", pdb_get_munged_dial(sam_pwent));
288 tmp = pdb_get_logon_time(sam_pwent);
289 printf ("Logon time: %s\n",
290 tmp ? http_timestring(talloc_tos(), tmp) : "0");
292 tmp = pdb_get_logoff_time(sam_pwent);
293 printf ("Logoff time: %s\n",
294 tmp ? http_timestring(talloc_tos(), tmp) : "0");
296 tmp = pdb_get_kickoff_time(sam_pwent);
297 printf ("Kickoff time: %s\n",
298 tmp ? http_timestring(talloc_tos(), tmp) : "0");
300 tmp = pdb_get_pass_last_set_time(sam_pwent);
301 printf ("Password last set: %s\n",
302 tmp ? http_timestring(talloc_tos(), tmp) : "0");
304 tmp = pdb_get_pass_can_change_time(sam_pwent);
305 printf ("Password can change: %s\n",
306 tmp ? http_timestring(talloc_tos(), tmp) : "0");
308 tmp = pdb_get_pass_must_change_time(sam_pwent);
309 printf ("Password must change: %s\n",
310 tmp ? http_timestring(talloc_tos(), tmp) : "0");
312 tmp = pdb_get_bad_password_time(sam_pwent);
313 printf ("Last bad password : %s\n",
314 tmp ? http_timestring(talloc_tos(), tmp) : "0");
315 printf ("Bad password count : %d\n",
316 pdb_get_bad_password_count(sam_pwent));
318 hours = pdb_get_hours(sam_pwent);
319 pdb_sethexhours(temp, hours);
320 printf ("Logon hours : %s\n", temp);
321 if (smbpwdstyle){
322 pdb_sethexpwd(temp, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
323 printf ("LM hash : %s\n", temp);
324 pdb_sethexpwd(temp, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
325 printf ("NT hash : %s\n", temp);
328 } else if (smbpwdstyle) {
329 char lm_passwd[33];
330 char nt_passwd[33];
332 uid = nametouid(pdb_get_username(sam_pwent));
333 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
334 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
336 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
337 pdb_get_username(sam_pwent),
338 (unsigned long)uid,
339 lm_passwd,
340 nt_passwd,
341 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
342 (uint32_t)convert_time_t_to_uint32_t(pdb_get_pass_last_set_time(sam_pwent)));
343 } else {
344 uid = nametouid(pdb_get_username(sam_pwent));
345 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid,
346 pdb_get_fullname(sam_pwent));
349 return 0;
352 /*********************************************************
353 Get an Print User Info
354 **********************************************************/
356 static int print_user_info(const char *username,
357 bool verbosity, bool smbpwdstyle)
359 struct samu *sam_pwent = NULL;
360 bool bret;
361 int ret;
363 sam_pwent = samu_new(NULL);
364 if (!sam_pwent) {
365 return -1;
368 bret = pdb_getsampwnam(sam_pwent, username);
369 if (!bret) {
370 fprintf (stderr, "Username not found!\n");
371 TALLOC_FREE(sam_pwent);
372 return -1;
375 ret = print_sam_info(sam_pwent, verbosity, smbpwdstyle);
377 TALLOC_FREE(sam_pwent);
378 return ret;
381 /*********************************************************
382 List Users
383 **********************************************************/
384 static int print_users_list(bool verbosity, bool smbpwdstyle)
386 struct pdb_search *u_search;
387 struct samr_displayentry userentry;
388 struct samu *sam_pwent;
389 TALLOC_CTX *tosctx;
390 struct dom_sid user_sid;
391 bool bret;
392 int ret;
394 tosctx = talloc_tos();
395 if (!tosctx) {
396 DEBUG(0, ("talloc failed\n"));
397 return 1;
400 u_search = pdb_search_users(tosctx, 0);
401 if (!u_search) {
402 DEBUG(0, ("User Search failed!\n"));
403 ret = 1;
404 goto done;
407 while (u_search->next_entry(u_search, &userentry)) {
409 sam_pwent = samu_new(tosctx);
410 if (sam_pwent == NULL) {
411 DEBUG(0, ("talloc failed\n"));
412 ret = 1;
413 goto done;
416 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
418 bret = pdb_getsampwsid(sam_pwent, &user_sid);
419 if (!bret) {
420 DEBUG(2, ("getsampwsid failed\n"));
421 TALLOC_FREE(sam_pwent);
422 continue;
425 if (verbosity) {
426 printf ("---------------\n");
428 print_sam_info(sam_pwent, verbosity, smbpwdstyle);
429 TALLOC_FREE(sam_pwent);
432 ret = 0;
434 done:
435 TALLOC_FREE(tosctx);
436 return ret;
439 /*********************************************************
440 Fix a list of Users for uninitialised passwords
441 **********************************************************/
442 static int fix_users_list(void)
444 struct pdb_search *u_search;
445 struct samr_displayentry userentry;
446 struct samu *sam_pwent;
447 TALLOC_CTX *tosctx;
448 struct dom_sid user_sid;
449 NTSTATUS status;
450 bool bret;
451 int ret;
453 tosctx = talloc_tos();
454 if (!tosctx) {
455 fprintf(stderr, "Out of memory!\n");
456 return 1;
459 u_search = pdb_search_users(tosctx, 0);
460 if (!u_search) {
461 fprintf(stderr, "User Search failed!\n");
462 ret = 1;
463 goto done;
466 while (u_search->next_entry(u_search, &userentry)) {
468 sam_pwent = samu_new(tosctx);
469 if (sam_pwent == NULL) {
470 fprintf(stderr, "Out of memory!\n");
471 ret = 1;
472 goto done;
475 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
477 bret = pdb_getsampwsid(sam_pwent, &user_sid);
478 if (!bret) {
479 DEBUG(2, ("getsampwsid failed\n"));
480 TALLOC_FREE(sam_pwent);
481 continue;
484 status = pdb_update_sam_account(sam_pwent);
485 if (!NT_STATUS_IS_OK(status)) {
486 printf("Update of user %s failed!\n",
487 pdb_get_username(sam_pwent));
489 TALLOC_FREE(sam_pwent);
492 ret = 0;
494 done:
495 TALLOC_FREE(tosctx);
496 return ret;
499 /*********************************************************
500 Set User Info
501 **********************************************************/
503 static int set_user_info(const char *username, const char *fullname,
504 const char *homedir, const char *acct_desc,
505 const char *drive, const char *script,
506 const char *profile, const char *account_control,
507 const char *user_sid, const char *user_domain,
508 const bool badpw, const bool hours,
509 const char *kickoff_time, const char *str_hex_pwd)
511 bool updated_autolock = False, updated_badpw = False;
512 struct samu *sam_pwent;
513 uint8_t hours_array[MAX_HOURS_LEN];
514 uint32_t hours_len;
515 uint32_t acb_flags;
516 uint32_t not_settable;
517 uint32_t new_flags;
518 struct dom_sid u_sid;
519 bool ret;
521 sam_pwent = samu_new(NULL);
522 if (!sam_pwent) {
523 return 1;
526 ret = pdb_getsampwnam(sam_pwent, username);
527 if (!ret) {
528 fprintf (stderr, "Username not found!\n");
529 TALLOC_FREE(sam_pwent);
530 return -1;
533 if (hours) {
534 hours_len = pdb_get_hours_len(sam_pwent);
535 memset(hours_array, 0xff, hours_len);
537 pdb_set_hours(sam_pwent, hours_array, hours_len, PDB_CHANGED);
540 if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) {
541 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
544 if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) {
545 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
548 if (fullname)
549 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
550 if (acct_desc)
551 pdb_set_acct_desc(sam_pwent, acct_desc, PDB_CHANGED);
552 if (homedir)
553 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
554 if (drive)
555 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
556 if (script)
557 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
558 if (profile)
559 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
560 if (user_domain)
561 pdb_set_domain(sam_pwent, user_domain, PDB_CHANGED);
563 if (account_control) {
564 not_settable = ~(ACB_DISABLED | ACB_HOMDIRREQ |
565 ACB_PWNOTREQ | ACB_PWNOEXP | ACB_AUTOLOCK);
567 new_flags = pdb_decode_acct_ctrl(account_control);
569 if (new_flags & not_settable) {
570 fprintf(stderr, "Can only set [NDHLX] flags\n");
571 TALLOC_FREE(sam_pwent);
572 return -1;
575 acb_flags = pdb_get_acct_ctrl(sam_pwent);
577 pdb_set_acct_ctrl(sam_pwent,
578 (acb_flags & not_settable) | new_flags,
579 PDB_CHANGED);
581 if (user_sid) {
582 if (get_sid_from_cli_string(&u_sid, user_sid)) {
583 fprintf(stderr, "Failed to parse SID\n");
584 return -1;
586 pdb_set_user_sid(sam_pwent, &u_sid, PDB_CHANGED);
589 if (badpw) {
590 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
591 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
594 if (kickoff_time) {
595 char *endptr;
596 time_t value = get_time_t_max();
598 if (strcmp(kickoff_time, "never") != 0) {
599 uint32_t num = strtoul(kickoff_time, &endptr, 10);
601 if ((endptr == kickoff_time) || (endptr[0] != '\0')) {
602 fprintf(stderr, "Failed to parse kickoff time\n");
603 return -1;
606 value = convert_uint32_t_to_time_t(num);
609 pdb_set_kickoff_time(sam_pwent, value, PDB_CHANGED);
611 if (str_hex_pwd) {
612 unsigned char new_nt_p16[NT_HASH_LEN];
613 if(strlen(str_hex_pwd) != (NT_HASH_LEN *2)){
614 fprintf(stderr, "Invalid hash\n");
615 return -1;
618 pdb_gethexpwd(str_hex_pwd, new_nt_p16);
620 if (!pdb_set_nt_passwd (sam_pwent, new_nt_p16 , PDB_CHANGED)) {
621 fprintf(stderr, "Failed to set password from nt-hash\n");
622 return -1;
625 if (!pdb_set_pass_last_set_time (sam_pwent, time(NULL), PDB_CHANGED)){
626 fprintf(stderr, "Failed to set last password set time\n");
627 return -1;
629 if (!pdb_update_history(sam_pwent, new_nt_p16)){
630 fprintf(stderr, "Failed to update password history\n");
631 return -1;
635 if (NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
637 print_user_info(username, True, (str_hex_pwd != NULL ));
638 } else {
639 fprintf (stderr, "Unable to modify entry!\n");
640 TALLOC_FREE(sam_pwent);
641 return -1;
643 TALLOC_FREE(sam_pwent);
644 return 0;
647 static int set_machine_info(const char *machinename,
648 const char *account_control,
649 const char *machine_sid)
651 struct samu *sam_pwent = NULL;
652 TALLOC_CTX *tosctx;
653 uint32_t acb_flags;
654 uint32_t not_settable;
655 uint32_t new_flags;
656 struct dom_sid m_sid;
657 char *name;
658 int len;
659 bool ret;
661 len = strlen(machinename);
662 if (len == 0) {
663 fprintf(stderr, "No machine name given\n");
664 return -1;
667 tosctx = talloc_tos();
668 if (!tosctx) {
669 fprintf(stderr, "Out of memory!\n");
670 return -1;
673 sam_pwent = samu_new(tosctx);
674 if (!sam_pwent) {
675 return 1;
678 if (machinename[len-1] == '$') {
679 name = talloc_strdup(sam_pwent, machinename);
680 } else {
681 name = talloc_asprintf(sam_pwent, "%s$", machinename);
683 if (!name) {
684 fprintf(stderr, "Out of memory!\n");
685 TALLOC_FREE(sam_pwent);
686 return -1;
689 if (!strlower_m(name)) {
690 fprintf(stderr, "strlower_m %s failed\n", name);
691 TALLOC_FREE(sam_pwent);
692 return -1;
695 ret = pdb_getsampwnam(sam_pwent, name);
696 if (!ret) {
697 fprintf (stderr, "Username not found!\n");
698 TALLOC_FREE(sam_pwent);
699 return -1;
702 if (account_control) {
703 not_settable = ~(ACB_DISABLED);
705 new_flags = pdb_decode_acct_ctrl(account_control);
707 if (new_flags & not_settable) {
708 fprintf(stderr, "Can only set [D] flags\n");
709 TALLOC_FREE(sam_pwent);
710 return -1;
713 acb_flags = pdb_get_acct_ctrl(sam_pwent);
715 pdb_set_acct_ctrl(sam_pwent,
716 (acb_flags & not_settable) | new_flags,
717 PDB_CHANGED);
719 if (machine_sid) {
720 if (get_sid_from_cli_string(&m_sid, machine_sid)) {
721 fprintf(stderr, "Failed to parse SID\n");
722 return -1;
724 pdb_set_user_sid(sam_pwent, &m_sid, PDB_CHANGED);
727 if (NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
728 print_user_info(name, True, False);
729 } else {
730 fprintf (stderr, "Unable to modify entry!\n");
731 TALLOC_FREE(sam_pwent);
732 return -1;
734 TALLOC_FREE(sam_pwent);
735 return 0;
738 /*********************************************************
739 Add New User
740 **********************************************************/
741 static int new_user(const char *username, const char *fullname,
742 const char *homedir, const char *drive,
743 const char *script, const char *profile,
744 char *user_sid, bool stdin_get)
746 char *pwd1 = NULL, *pwd2 = NULL;
747 char *err = NULL, *msg = NULL;
748 struct samu *sam_pwent = NULL;
749 TALLOC_CTX *tosctx;
750 NTSTATUS status;
751 struct dom_sid u_sid;
752 int flags;
753 int ret;
755 tosctx = talloc_tos();
756 if (!tosctx) {
757 fprintf(stderr, "Out of memory!\n");
758 return -1;
761 if (user_sid) {
762 if (get_sid_from_cli_string(&u_sid, user_sid)) {
763 fprintf(stderr, "Failed to parse SID\n");
764 return -1;
768 pwd1 = get_pass( "new password:", stdin_get);
769 pwd2 = get_pass( "retype new password:", stdin_get);
770 if (!pwd1 || !pwd2) {
771 fprintf(stderr, "Failed to read passwords.\n");
772 return -1;
774 ret = strcmp(pwd1, pwd2);
775 if (ret != 0) {
776 fprintf (stderr, "Passwords do not match!\n");
777 goto done;
780 flags = LOCAL_ADD_USER | LOCAL_SET_PASSWORD;
782 status = local_password_change(username, flags, pwd1, &err, &msg);
783 if (!NT_STATUS_IS_OK(status)) {
784 if (err) fprintf(stderr, "%s", err);
785 ret = -1;
786 goto done;
789 sam_pwent = samu_new(tosctx);
790 if (!sam_pwent) {
791 fprintf(stderr, "Out of memory!\n");
792 ret = -1;
793 goto done;
796 if (!pdb_getsampwnam(sam_pwent, username)) {
797 fprintf(stderr, "User %s not found!\n", username);
798 ret = -1;
799 goto done;
802 if (fullname)
803 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
804 if (homedir)
805 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
806 if (drive)
807 pdb_set_dir_drive(sam_pwent, drive, PDB_CHANGED);
808 if (script)
809 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
810 if (profile)
811 pdb_set_profile_path(sam_pwent, profile, PDB_CHANGED);
812 if (user_sid)
813 pdb_set_user_sid(sam_pwent, &u_sid, PDB_CHANGED);
815 status = pdb_update_sam_account(sam_pwent);
816 if (!NT_STATUS_IS_OK(status)) {
817 fprintf(stderr,
818 "Failed to modify entry for user %s.!\n",
819 username);
820 ret = -1;
821 goto done;
824 print_user_info(username, True, False);
825 ret = 0;
827 done:
828 if (pwd1) memset(pwd1, 0, strlen(pwd1));
829 if (pwd2) memset(pwd2, 0, strlen(pwd2));
830 SAFE_FREE(pwd1);
831 SAFE_FREE(pwd2);
832 SAFE_FREE(err);
833 SAFE_FREE(msg);
834 TALLOC_FREE(sam_pwent);
835 return ret;
838 /*********************************************************
839 Add New Machine
840 **********************************************************/
842 static int new_machine(const char *machinename, char *machine_sid)
844 char *err = NULL, *msg = NULL;
845 struct samu *sam_pwent = NULL;
846 TALLOC_CTX *tosctx;
847 NTSTATUS status;
848 struct dom_sid m_sid;
849 char *compatpwd;
850 char *name;
851 int flags;
852 int len;
853 int ret;
855 len = strlen(machinename);
856 if (len == 0) {
857 fprintf(stderr, "No machine name given\n");
858 return -1;
861 tosctx = talloc_tos();
862 if (!tosctx) {
863 fprintf(stderr, "Out of memory!\n");
864 return -1;
867 if (machine_sid) {
868 if (get_sid_from_cli_string(&m_sid, machine_sid)) {
869 fprintf(stderr, "Failed to parse SID\n");
870 return -1;
874 compatpwd = talloc_strdup(tosctx, machinename);
875 if (!compatpwd) {
876 fprintf(stderr, "Out of memory!\n");
877 return -1;
880 if (machinename[len-1] == '$') {
881 name = talloc_strdup(tosctx, machinename);
882 compatpwd[len-1] = '\0';
883 } else {
884 name = talloc_asprintf(tosctx, "%s$", machinename);
886 if (!name) {
887 fprintf(stderr, "Out of memory!\n");
888 return -1;
891 if (!strlower_m(name)) {
892 fprintf(stderr, "strlower_m %s failed\n", name);
893 return -1;
896 flags = LOCAL_ADD_USER | LOCAL_TRUST_ACCOUNT | LOCAL_SET_PASSWORD;
898 status = local_password_change(name, flags, compatpwd, &err, &msg);
900 if (!NT_STATUS_IS_OK(status)) {
901 if (err) fprintf(stderr, "%s", err);
902 ret = -1;
905 sam_pwent = samu_new(tosctx);
906 if (!sam_pwent) {
907 fprintf(stderr, "Out of memory!\n");
908 ret = -1;
909 goto done;
912 if (!pdb_getsampwnam(sam_pwent, name)) {
913 fprintf(stderr, "Machine %s not found!\n", name);
914 ret = -1;
915 goto done;
918 if (machine_sid)
919 pdb_set_user_sid(sam_pwent, &m_sid, PDB_CHANGED);
921 status = pdb_update_sam_account(sam_pwent);
922 if (!NT_STATUS_IS_OK(status)) {
923 fprintf(stderr,
924 "Failed to modify entry for %s.!\n", name);
925 ret = -1;
926 goto done;
929 print_user_info(name, True, False);
930 ret = 0;
932 done:
933 SAFE_FREE(err);
934 SAFE_FREE(msg);
935 TALLOC_FREE(sam_pwent);
936 return ret;
939 /*********************************************************
940 Delete user entry
941 **********************************************************/
943 static int delete_user_entry(const char *username)
945 struct samu *samaccount;
947 samaccount = samu_new(NULL);
948 if (!samaccount) {
949 fprintf(stderr, "Out of memory!\n");
950 return -1;
953 if (!pdb_getsampwnam(samaccount, username)) {
954 fprintf (stderr,
955 "user %s does not exist in the passdb\n", username);
956 TALLOC_FREE(samaccount);
957 return -1;
960 if (!NT_STATUS_IS_OK(pdb_delete_sam_account(samaccount))) {
961 fprintf (stderr, "Unable to delete user %s\n", username);
962 TALLOC_FREE(samaccount);
963 return -1;
966 TALLOC_FREE(samaccount);
967 return 0;
970 /*********************************************************
971 Delete machine entry
972 **********************************************************/
974 static int delete_machine_entry(const char *machinename)
976 struct samu *samaccount = NULL;
977 const char *name;
979 if (strlen(machinename) == 0) {
980 fprintf(stderr, "No machine name given\n");
981 return -1;
984 samaccount = samu_new(NULL);
985 if (!samaccount) {
986 fprintf(stderr, "Out of memory!\n");
987 return -1;
990 if (machinename[strlen(machinename)-1] != '$') {
991 name = talloc_asprintf(samaccount, "%s$", machinename);
992 } else {
993 name = machinename;
996 if (!pdb_getsampwnam(samaccount, name)) {
997 fprintf (stderr,
998 "machine %s does not exist in the passdb\n", name);
999 TALLOC_FREE(samaccount);
1000 return -1;
1003 if (!NT_STATUS_IS_OK(pdb_delete_sam_account(samaccount))) {
1004 fprintf (stderr, "Unable to delete machine %s\n", name);
1005 TALLOC_FREE(samaccount);
1006 return -1;
1009 TALLOC_FREE(samaccount);
1010 return 0;
1013 /*********************************************************
1014 Start here.
1015 **********************************************************/
1017 int main(int argc, const char **argv)
1019 static int list_users = False;
1020 static int verbose = False;
1021 static int spstyle = False;
1022 static int machine = False;
1023 static int add_user = False;
1024 static int delete_user = False;
1025 static int modify_user = False;
1026 uint32_t setparms, checkparms;
1027 int opt;
1028 static char *full_name = NULL;
1029 static char *acct_desc = NULL;
1030 static const char *user_name = NULL;
1031 static char *home_dir = NULL;
1032 static char *home_drive = NULL;
1033 static const char *backend = NULL;
1034 static char *backend_in = NULL;
1035 static char *backend_out = NULL;
1036 static int transfer_groups = False;
1037 static int transfer_account_policies = False;
1038 static int reset_account_policies = False;
1039 static int force_initialised_password = False;
1040 static char *logon_script = NULL;
1041 static char *profile_path = NULL;
1042 static char *user_domain = NULL;
1043 static char *account_control = NULL;
1044 static char *account_policy = NULL;
1045 static char *user_sid = NULL;
1046 static char *machine_sid = NULL;
1047 static long int account_policy_value = 0;
1048 bool account_policy_value_set = False;
1049 static int badpw_reset = False;
1050 static int hours_reset = False;
1051 static char *pwd_time_format = NULL;
1052 static int pw_from_stdin = False;
1053 struct pdb_methods *bin, *bout;
1054 static char *kickoff_time = NULL;
1055 static char *str_hex_pwd = NULL;
1056 TALLOC_CTX *frame = talloc_stackframe();
1057 NTSTATUS status;
1058 poptContext pc;
1059 struct poptOption long_options[] = {
1060 POPT_AUTOHELP
1061 {"list", 'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
1062 {"verbose", 'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
1063 {"smbpasswd-style", 'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
1064 {"user", 'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
1065 {"account-desc", 'N', POPT_ARG_STRING, &acct_desc, 0, "set account description", NULL},
1066 {"fullname", 'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
1067 {"homedir", 'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
1068 {"drive", 'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
1069 {"script", 'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
1070 {"profile", 'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
1071 {"domain", 'I', POPT_ARG_STRING, &user_domain, 0, "set a users' domain", NULL},
1072 {"user SID", 'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
1073 {"machine SID", 'M', POPT_ARG_STRING, &machine_sid, 0, "set machine SID or RID", NULL},
1074 {"create", 'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
1075 {"modify", 'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
1076 {"machine", 'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
1077 {"delete", 'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
1078 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
1079 {"import", 'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
1080 {"export", 'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
1081 {"group", 'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
1082 {"policies", 'y', POPT_ARG_NONE, &transfer_account_policies, 0, "use -i and -e to move account policies between backends", NULL},
1083 {"policies-reset", 0, POPT_ARG_NONE, &reset_account_policies, 0, "restore default policies", NULL},
1084 {"account-policy", 'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
1085 {"value", 'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
1086 {"account-control", 'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
1087 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
1088 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
1089 {"logon-hours-reset", 'Z', POPT_ARG_NONE, &hours_reset, 0, "reset logon hours", NULL},
1090 {"time-format", 0, POPT_ARG_STRING, &pwd_time_format, 0, "The time format for time parameters", NULL },
1091 {"password-from-stdin", 't', POPT_ARG_NONE, &pw_from_stdin, 0, "get password from standard in", NULL},
1092 {"kickoff-time", 'K', POPT_ARG_STRING, &kickoff_time, 0, "set the kickoff time", NULL},
1093 {"set-nt-hash", 0, POPT_ARG_STRING, &str_hex_pwd, 0, "set password from nt-hash", NULL},
1094 POPT_COMMON_SAMBA
1095 POPT_TABLEEND
1098 bin = bout = NULL;
1100 smb_init_locale();
1102 setup_logging("pdbedit", DEBUG_STDOUT);
1104 pc = poptGetContext(NULL, argc, argv, long_options,
1105 POPT_CONTEXT_KEEP_FIRST);
1107 while((opt = poptGetNextOpt(pc)) != -1) {
1108 switch (opt) {
1109 case 'C':
1110 account_policy_value_set = True;
1111 break;
1115 poptGetArg(pc); /* Drop argv[0], the program name */
1117 if (user_name == NULL)
1118 user_name = poptGetArg(pc);
1120 if (!lp_load_global(get_dyn_CONFIGFILE())) {
1121 fprintf(stderr, "Can't load %s - run testparm to debug it\n", get_dyn_CONFIGFILE());
1122 exit(1);
1125 if (!init_names())
1126 exit(1);
1128 setparms = (backend ? BIT_BACKEND : 0) +
1129 (verbose ? BIT_VERBOSE : 0) +
1130 (spstyle ? BIT_SPSTYLE : 0) +
1131 (full_name ? BIT_FULLNAME : 0) +
1132 (home_dir ? BIT_HOMEDIR : 0) +
1133 (home_drive ? BIT_HDIRDRIVE : 0) +
1134 (logon_script ? BIT_LOGSCRIPT : 0) +
1135 (profile_path ? BIT_PROFILE : 0) +
1136 (user_domain ? BIT_USERDOMAIN : 0) +
1137 (machine ? BIT_MACHINE : 0) +
1138 (user_name ? BIT_USER : 0) +
1139 (list_users ? BIT_LIST : 0) +
1140 (force_initialised_password ? BIT_FIX_INIT : 0) +
1141 (user_sid ? BIT_USERSIDS : 0) +
1142 (machine_sid ? BIT_USERSIDS : 0) +
1143 (modify_user ? BIT_MODIFY : 0) +
1144 (add_user ? BIT_CREATE : 0) +
1145 (delete_user ? BIT_DELETE : 0) +
1146 (account_control ? BIT_ACCTCTRL : 0) +
1147 (account_policy ? BIT_ACCPOLICY : 0) +
1148 (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
1149 (backend_in ? BIT_IMPORT : 0) +
1150 (backend_out ? BIT_EXPORT : 0) +
1151 (badpw_reset ? BIT_BADPWRESET : 0) +
1152 (hours_reset ? BIT_LOGONHOURS : 0) +
1153 (kickoff_time ? BIT_KICKOFFTIME : 0) +
1154 (str_hex_pwd ? BIT_PWSETNTHASH : 0 ) +
1155 (acct_desc ? BIT_DESCRIPTION : 0);
1158 if (setparms & BIT_BACKEND) {
1159 /* HACK: set the global passdb backend by overwriting globals.
1160 * This way we can use regular pdb functions for default
1161 * operations that do not involve passdb migrations */
1162 lp_set_cmdline("passdb backend", backend);
1163 } else {
1164 backend = lp_passdb_backend();
1167 if (!initialize_password_db(False, NULL)) {
1168 fprintf(stderr, "Can't initialize passdb backend.\n");
1169 exit(1);
1172 /* the lowest bit options are always accepted */
1173 checkparms = setparms & ~MASK_ALWAYS_GOOD;
1175 if (checkparms & BIT_FIX_INIT) {
1176 return fix_users_list();
1179 /* account policy operations */
1180 if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
1181 uint32_t value;
1182 enum pdb_policy_type field = account_policy_name_to_typenum(account_policy);
1183 if (field == 0) {
1184 const char **names;
1185 int count;
1186 int i;
1187 account_policy_names_list(talloc_tos(), &names, &count);
1188 fprintf(stderr, "No account policy by that name!\n");
1189 if (count !=0) {
1190 fprintf(stderr, "Account policy names are:\n");
1191 for (i = 0; i < count ; i++) {
1192 d_fprintf(stderr, "%s\n", names[i]);
1195 TALLOC_FREE(names);
1196 exit(1);
1198 if (!pdb_get_account_policy(field, &value)) {
1199 fprintf(stderr, "valid account policy, but unable to fetch value!\n");
1200 if (!account_policy_value_set)
1201 exit(1);
1203 printf("account policy \"%s\" description: %s\n", account_policy, account_policy_get_desc(field));
1204 if (account_policy_value_set) {
1205 printf("account policy \"%s\" value was: %u\n", account_policy, value);
1206 if (!pdb_set_account_policy(field, account_policy_value)) {
1207 fprintf(stderr, "valid account policy, but unable to set value!\n");
1208 exit(1);
1210 printf("account policy \"%s\" value is now: %lu\n", account_policy, account_policy_value);
1211 exit(0);
1212 } else {
1213 printf("account policy \"%s\" value is: %u\n", account_policy, value);
1214 exit(0);
1218 if (reset_account_policies) {
1219 if (reinit_account_policies()) {
1220 exit(1);
1223 exit(0);
1226 /* import and export operations */
1228 if (((checkparms & BIT_IMPORT) ||
1229 (checkparms & BIT_EXPORT)) &&
1230 !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
1232 if (backend_in) {
1233 status = make_pdb_method_name(&bin, backend_in);
1234 } else {
1235 status = make_pdb_method_name(&bin, backend);
1238 if (!NT_STATUS_IS_OK(status)) {
1239 fprintf(stderr, "Unable to initialize %s.\n",
1240 backend_in ? backend_in : backend);
1241 return 1;
1244 if (backend_out) {
1245 status = make_pdb_method_name(&bout, backend_out);
1246 } else {
1247 status = make_pdb_method_name(&bout, backend);
1250 if (!NT_STATUS_IS_OK(status)) {
1251 fprintf(stderr, "Unable to initialize %s.\n",
1252 backend_out ? backend_out : backend);
1253 return 1;
1256 if (transfer_account_policies) {
1258 if (!(checkparms & BIT_USER)) {
1259 return export_account_policies(bin, bout);
1262 } else if (transfer_groups) {
1264 if (!(checkparms & BIT_USER)) {
1265 return export_groups(bin, bout);
1268 } else {
1269 return export_database(bin, bout,
1270 (checkparms & BIT_USER) ? user_name : NULL);
1274 /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
1275 /* fake up BIT_LIST if only BIT_USER is defined */
1276 if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
1277 checkparms += BIT_LIST;
1280 /* modify flag is optional to maintain backwards compatibility */
1281 /* fake up BIT_MODIFY if BIT_USER and at least one of MASK_USER_GOOD is defined */
1282 if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
1283 checkparms += BIT_MODIFY;
1286 /* list users operations */
1287 if (checkparms & BIT_LIST) {
1288 if (!(checkparms & ~BIT_LIST)) {
1289 return print_users_list(verbose, spstyle);
1291 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
1292 return print_user_info(user_name, verbose, spstyle);
1296 /* mask out users options */
1297 checkparms &= ~MASK_USER_GOOD;
1299 /* if bad password count is reset, we must be modifying */
1300 if (checkparms & BIT_BADPWRESET) {
1301 checkparms |= BIT_MODIFY;
1302 checkparms &= ~BIT_BADPWRESET;
1305 /* if logon hours is reset, must modify */
1306 if (checkparms & BIT_LOGONHOURS) {
1307 checkparms |= BIT_MODIFY;
1308 checkparms &= ~BIT_LOGONHOURS;
1311 /* account operation */
1312 if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
1313 /* check use of -u option */
1314 if (!(checkparms & BIT_USER)) {
1315 fprintf (stderr, "Username not specified! (use -u option)\n");
1316 return -1;
1319 /* account creation operations */
1320 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
1321 if (checkparms & BIT_MACHINE) {
1322 return new_machine(user_name, machine_sid);
1323 } else {
1324 return new_user(user_name, full_name,
1325 home_dir, home_drive,
1326 logon_script, profile_path,
1327 user_sid, pw_from_stdin);
1331 /* account deletion operations */
1332 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
1333 if (checkparms & BIT_MACHINE) {
1334 return delete_machine_entry(user_name);
1335 } else {
1336 return delete_user_entry(user_name);
1340 /* account modification operations */
1341 if (!(checkparms & ~(BIT_MODIFY + BIT_USER + BIT_MACHINE))) {
1342 if (checkparms & BIT_MACHINE) {
1343 return set_machine_info(user_name,
1344 account_control,
1345 machine_sid);
1346 } else {
1347 return set_user_info(user_name, full_name,
1348 home_dir, acct_desc,
1349 home_drive, logon_script,
1350 profile_path, account_control,
1351 user_sid, user_domain,
1352 badpw_reset, hours_reset,
1353 kickoff_time, str_hex_pwd);
1358 if (setparms >= 0x20) {
1359 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
1361 poptPrintHelp(pc, stderr, 0);
1363 TALLOC_FREE(frame);
1364 return 1;