libcli/smb: don't overwrite status code
[Samba.git] / source3 / utils / pdbedit.c
bloba2394880c65021965226657dd803bcdab489c27f
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"
28 #include "cmdline_contexts.h"
30 #define BIT_BACKEND 0x00000004
31 #define BIT_VERBOSE 0x00000008
32 #define BIT_SPSTYLE 0x00000010
33 #define BIT_CAN_CHANGE 0x00000020
34 #define BIT_MUST_CHANGE 0x00000040
35 #define BIT_USERSIDS 0x00000080
36 #define BIT_FULLNAME 0x00000100
37 #define BIT_HOMEDIR 0x00000200
38 #define BIT_HDIRDRIVE 0x00000400
39 #define BIT_LOGSCRIPT 0x00000800
40 #define BIT_PROFILE 0x00001000
41 #define BIT_MACHINE 0x00002000
42 #define BIT_USERDOMAIN 0x00004000
43 #define BIT_USER 0x00008000
44 #define BIT_LIST 0x00010000
45 #define BIT_MODIFY 0x00020000
46 #define BIT_CREATE 0x00040000
47 #define BIT_DELETE 0x00080000
48 #define BIT_ACCPOLICY 0x00100000
49 #define BIT_ACCPOLVAL 0x00200000
50 #define BIT_ACCTCTRL 0x00400000
51 #define BIT_RESERV_7 0x00800000
52 #define BIT_IMPORT 0x01000000
53 #define BIT_EXPORT 0x02000000
54 #define BIT_FIX_INIT 0x04000000
55 #define BIT_BADPWRESET 0x08000000
56 #define BIT_LOGONHOURS 0x10000000
57 #define BIT_KICKOFFTIME 0x20000000
58 #define BIT_DESCRIPTION 0x40000000
59 #define BIT_PWSETNTHASH 0x80000000
61 #define MASK_ALWAYS_GOOD 0x0000001F
62 #define MASK_USER_GOOD 0xE0405FE0
64 static int get_sid_from_cli_string(struct dom_sid *sid, const char *str_sid)
66 uint32_t rid;
68 if (!string_to_sid(sid, str_sid)) {
69 /* not a complete sid, may be a RID,
70 * try building a SID */
72 if (sscanf(str_sid, "%u", &rid) != 1) {
73 fprintf(stderr, "Error passed string is not "
74 "a complete SID or RID!\n");
75 return -1;
77 sid_compose(sid, get_global_sam_sid(), rid);
80 return 0;
83 /*********************************************************
84 Add all currently available users to another db
85 ********************************************************/
87 static int export_database (struct pdb_methods *in,
88 struct pdb_methods *out,
89 const char *username)
91 NTSTATUS status;
92 struct pdb_search *u_search;
93 struct samr_displayentry userentry;
95 DEBUG(3, ("export_database: username=\"%s\"\n", username ? username : "(NULL)"));
97 u_search = pdb_search_init(talloc_tos(), PDB_USER_SEARCH);
98 if (u_search == NULL) {
99 DEBUG(0, ("pdb_search_init failed\n"));
100 return 1;
103 if (!in->search_users(in, u_search, 0)) {
104 DEBUG(0, ("Could not start searching users\n"));
105 TALLOC_FREE(u_search);
106 return 1;
109 while (u_search->next_entry(u_search, &userentry)) {
110 struct samu *user;
111 struct samu *account;
112 struct dom_sid user_sid;
114 DEBUG(4, ("Processing account %s\n", userentry.account_name));
116 if ((username != NULL)
117 && (strcmp(username, userentry.account_name) != 0)) {
119 * ignore unwanted users
121 continue;
124 user = samu_new(talloc_tos());
125 if (user == NULL) {
126 DEBUG(0, ("talloc failed\n"));
127 break;
130 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
132 status = in->getsampwsid(in, user, &user_sid);
134 if (!NT_STATUS_IS_OK(status)) {
135 DEBUG(2, ("getsampwsid failed: %s\n",
136 nt_errstr(status)));
137 TALLOC_FREE(user);
138 continue;
141 account = samu_new(NULL);
142 if (account == NULL) {
143 fprintf(stderr, "export_database: Memory allocation "
144 "failure!\n");
145 TALLOC_FREE( user );
146 TALLOC_FREE(u_search);
147 return 1;
150 printf("Importing account for %s...", user->username);
151 status = out->getsampwnam(out, account, user->username);
153 if (NT_STATUS_IS_OK(status)) {
154 status = out->update_sam_account( out, user );
155 } else {
156 status = out->add_sam_account(out, user);
159 if ( NT_STATUS_IS_OK(status) ) {
160 printf( "ok\n");
161 } else {
162 printf( "failed\n");
165 TALLOC_FREE( account );
166 TALLOC_FREE( user );
169 TALLOC_FREE(u_search);
171 return 0;
174 /*********************************************************
175 Add all currently available group mappings to another db
176 ********************************************************/
178 static int export_groups (struct pdb_methods *in, struct pdb_methods *out)
180 GROUP_MAP **maps = NULL;
181 size_t i, entries = 0;
182 NTSTATUS status;
184 status = in->enum_group_mapping(in, get_global_sam_sid(),
185 SID_NAME_DOM_GRP, &maps, &entries, False);
187 if ( NT_STATUS_IS_ERR(status) ) {
188 fprintf(stderr, "Unable to enumerate group map entries.\n");
189 return 1;
192 for (i=0; i<entries; i++) {
193 out->add_group_mapping_entry(out, maps[i]);
196 TALLOC_FREE(maps);
198 return 0;
201 /*********************************************************
202 Reset account policies to their default values and remove marker
203 ********************************************************/
205 static int reinit_account_policies (void)
207 int i;
209 for (i=1; decode_account_policy_name(i) != NULL; i++) {
210 uint32_t policy_value;
211 if (!account_policy_get_default(i, &policy_value)) {
212 fprintf(stderr, "Can't get default account policy\n");
213 return -1;
215 if (!account_policy_set(i, policy_value)) {
216 fprintf(stderr, "Can't set account policy in tdb\n");
217 return -1;
221 return 0;
225 /*********************************************************
226 Add all currently available account policy from tdb to one backend
227 ********************************************************/
229 static int export_account_policies (struct pdb_methods *in, struct pdb_methods *out)
231 int i;
233 for ( i=1; decode_account_policy_name(i) != NULL; i++ ) {
234 uint32_t policy_value;
235 NTSTATUS status;
237 status = in->get_account_policy(in, i, &policy_value);
239 if ( NT_STATUS_IS_ERR(status) ) {
240 fprintf(stderr, "Unable to get account policy from %s\n", in->name);
241 return -1;
244 status = out->set_account_policy(out, i, policy_value);
246 if ( NT_STATUS_IS_ERR(status) ) {
247 fprintf(stderr, "Unable to migrate account policy to %s\n", out->name);
248 return -1;
252 return 0;
256 /*********************************************************
257 Print info from sam structure
258 **********************************************************/
260 static int print_sam_info (struct samu *sam_pwent, bool verbosity, bool smbpwdstyle)
262 uid_t uid;
263 time_t tmp;
265 /* TODO: check if entry is a user or a workstation */
266 if (!sam_pwent) return -1;
268 if (verbosity) {
269 char temp[44];
270 const uint8_t *hours;
272 printf ("Unix username: %s\n", pdb_get_username(sam_pwent));
273 printf ("NT username: %s\n", pdb_get_nt_username(sam_pwent));
274 printf ("Account Flags: %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
275 printf ("User SID: %s\n",
276 sid_string_tos(pdb_get_user_sid(sam_pwent)));
277 printf ("Primary Group SID: %s\n",
278 sid_string_tos(pdb_get_group_sid(sam_pwent)));
279 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent));
280 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
281 printf ("HomeDir Drive: %s\n", pdb_get_dir_drive(sam_pwent));
282 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent));
283 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent));
284 printf ("Domain: %s\n", pdb_get_domain(sam_pwent));
285 printf ("Account desc: %s\n", pdb_get_acct_desc(sam_pwent));
286 printf ("Workstations: %s\n", pdb_get_workstations(sam_pwent));
287 printf ("Munged dial: %s\n", pdb_get_munged_dial(sam_pwent));
289 tmp = pdb_get_logon_time(sam_pwent);
290 printf ("Logon time: %s\n",
291 tmp ? http_timestring(talloc_tos(), tmp) : "0");
293 tmp = pdb_get_logoff_time(sam_pwent);
294 printf ("Logoff time: %s\n",
295 tmp ? http_timestring(talloc_tos(), tmp) : "0");
297 tmp = pdb_get_kickoff_time(sam_pwent);
298 printf ("Kickoff time: %s\n",
299 tmp ? http_timestring(talloc_tos(), tmp) : "0");
301 tmp = pdb_get_pass_last_set_time(sam_pwent);
302 printf ("Password last set: %s\n",
303 tmp ? http_timestring(talloc_tos(), tmp) : "0");
305 tmp = pdb_get_pass_can_change_time(sam_pwent);
306 printf ("Password can change: %s\n",
307 tmp ? http_timestring(talloc_tos(), tmp) : "0");
309 tmp = pdb_get_pass_must_change_time(sam_pwent);
310 printf ("Password must change: %s\n",
311 tmp ? http_timestring(talloc_tos(), tmp) : "0");
313 tmp = pdb_get_bad_password_time(sam_pwent);
314 printf ("Last bad password : %s\n",
315 tmp ? http_timestring(talloc_tos(), tmp) : "0");
316 printf ("Bad password count : %d\n",
317 pdb_get_bad_password_count(sam_pwent));
319 hours = pdb_get_hours(sam_pwent);
320 pdb_sethexhours(temp, hours);
321 printf ("Logon hours : %s\n", temp);
322 if (smbpwdstyle){
323 pdb_sethexpwd(temp, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
324 printf ("LM hash : %s\n", temp);
325 pdb_sethexpwd(temp, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
326 printf ("NT hash : %s\n", temp);
329 } else if (smbpwdstyle) {
330 char lm_passwd[33];
331 char nt_passwd[33];
333 uid = nametouid(pdb_get_username(sam_pwent));
334 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
335 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
337 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
338 pdb_get_username(sam_pwent),
339 (unsigned long)uid,
340 lm_passwd,
341 nt_passwd,
342 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
343 (uint32_t)convert_time_t_to_uint32_t(pdb_get_pass_last_set_time(sam_pwent)));
344 } else {
345 uid = nametouid(pdb_get_username(sam_pwent));
346 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid,
347 pdb_get_fullname(sam_pwent));
350 return 0;
353 /*********************************************************
354 Get an Print User Info
355 **********************************************************/
357 static int print_user_info(const char *username,
358 bool verbosity, bool smbpwdstyle)
360 struct samu *sam_pwent = NULL;
361 bool bret;
362 int ret;
364 sam_pwent = samu_new(NULL);
365 if (!sam_pwent) {
366 return -1;
369 bret = pdb_getsampwnam(sam_pwent, username);
370 if (!bret) {
371 fprintf (stderr, "Username not found!\n");
372 TALLOC_FREE(sam_pwent);
373 return -1;
376 ret = print_sam_info(sam_pwent, verbosity, smbpwdstyle);
378 TALLOC_FREE(sam_pwent);
379 return ret;
382 /*********************************************************
383 List Users
384 **********************************************************/
385 static int print_users_list(bool verbosity, bool smbpwdstyle)
387 struct pdb_search *u_search;
388 struct samr_displayentry userentry;
389 struct samu *sam_pwent;
390 TALLOC_CTX *tosctx;
391 struct dom_sid user_sid;
392 bool bret;
393 int ret;
395 tosctx = talloc_tos();
396 if (!tosctx) {
397 DEBUG(0, ("talloc failed\n"));
398 return 1;
401 u_search = pdb_search_users(tosctx, 0);
402 if (!u_search) {
403 DEBUG(0, ("User Search failed!\n"));
404 ret = 1;
405 goto done;
408 while (u_search->next_entry(u_search, &userentry)) {
410 sam_pwent = samu_new(tosctx);
411 if (sam_pwent == NULL) {
412 DEBUG(0, ("talloc failed\n"));
413 ret = 1;
414 goto done;
417 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
419 bret = pdb_getsampwsid(sam_pwent, &user_sid);
420 if (!bret) {
421 DEBUG(2, ("getsampwsid failed\n"));
422 TALLOC_FREE(sam_pwent);
423 continue;
426 if (verbosity) {
427 printf ("---------------\n");
429 print_sam_info(sam_pwent, verbosity, smbpwdstyle);
430 TALLOC_FREE(sam_pwent);
433 ret = 0;
435 done:
436 TALLOC_FREE(tosctx);
437 return ret;
440 /*********************************************************
441 Fix a list of Users for uninitialised passwords
442 **********************************************************/
443 static int fix_users_list(void)
445 struct pdb_search *u_search;
446 struct samr_displayentry userentry;
447 struct samu *sam_pwent;
448 TALLOC_CTX *tosctx;
449 struct dom_sid user_sid;
450 NTSTATUS status;
451 bool bret;
452 int ret;
454 tosctx = talloc_tos();
455 if (!tosctx) {
456 fprintf(stderr, "Out of memory!\n");
457 return 1;
460 u_search = pdb_search_users(tosctx, 0);
461 if (!u_search) {
462 fprintf(stderr, "User Search failed!\n");
463 ret = 1;
464 goto done;
467 while (u_search->next_entry(u_search, &userentry)) {
469 sam_pwent = samu_new(tosctx);
470 if (sam_pwent == NULL) {
471 fprintf(stderr, "Out of memory!\n");
472 ret = 1;
473 goto done;
476 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
478 bret = pdb_getsampwsid(sam_pwent, &user_sid);
479 if (!bret) {
480 DEBUG(2, ("getsampwsid failed\n"));
481 TALLOC_FREE(sam_pwent);
482 continue;
485 status = pdb_update_sam_account(sam_pwent);
486 if (!NT_STATUS_IS_OK(status)) {
487 printf("Update of user %s failed!\n",
488 pdb_get_username(sam_pwent));
490 TALLOC_FREE(sam_pwent);
493 ret = 0;
495 done:
496 TALLOC_FREE(tosctx);
497 return ret;
500 /*********************************************************
501 Set User Info
502 **********************************************************/
504 static int set_user_info(const char *username, const char *fullname,
505 const char *homedir, const char *acct_desc,
506 const char *drive, const char *script,
507 const char *profile, const char *account_control,
508 const char *user_sid, const char *user_domain,
509 const bool badpw, const bool hours,
510 const char *kickoff_time, const char *str_hex_pwd)
512 bool updated_autolock = False, updated_badpw = False;
513 struct samu *sam_pwent;
514 uint8_t hours_array[MAX_HOURS_LEN];
515 uint32_t hours_len;
516 uint32_t acb_flags;
517 uint32_t not_settable;
518 uint32_t new_flags;
519 struct dom_sid u_sid;
520 bool ret;
522 sam_pwent = samu_new(NULL);
523 if (!sam_pwent) {
524 return 1;
527 ret = pdb_getsampwnam(sam_pwent, username);
528 if (!ret) {
529 fprintf (stderr, "Username not found!\n");
530 TALLOC_FREE(sam_pwent);
531 return -1;
534 if (hours) {
535 hours_len = pdb_get_hours_len(sam_pwent);
536 memset(hours_array, 0xff, hours_len);
538 pdb_set_hours(sam_pwent, hours_array, hours_len, PDB_CHANGED);
541 if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) {
542 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
545 if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) {
546 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
549 if (fullname)
550 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
551 if (acct_desc)
552 pdb_set_acct_desc(sam_pwent, acct_desc, PDB_CHANGED);
553 if (homedir)
554 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
555 if (drive)
556 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
557 if (script)
558 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
559 if (profile)
560 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
561 if (user_domain)
562 pdb_set_domain(sam_pwent, user_domain, PDB_CHANGED);
564 if (account_control) {
565 not_settable = ~(ACB_DISABLED | ACB_HOMDIRREQ |
566 ACB_PWNOTREQ | ACB_PWNOEXP | ACB_AUTOLOCK);
568 new_flags = pdb_decode_acct_ctrl(account_control);
570 if (new_flags & not_settable) {
571 fprintf(stderr, "Can only set [NDHLX] flags\n");
572 TALLOC_FREE(sam_pwent);
573 return -1;
576 acb_flags = pdb_get_acct_ctrl(sam_pwent);
578 pdb_set_acct_ctrl(sam_pwent,
579 (acb_flags & not_settable) | new_flags,
580 PDB_CHANGED);
582 if (user_sid) {
583 if (get_sid_from_cli_string(&u_sid, user_sid)) {
584 fprintf(stderr, "Failed to parse SID\n");
585 return -1;
587 pdb_set_user_sid(sam_pwent, &u_sid, PDB_CHANGED);
590 if (badpw) {
591 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
592 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
595 if (kickoff_time) {
596 char *endptr;
597 time_t value = get_time_t_max();
599 if (strcmp(kickoff_time, "never") != 0) {
600 uint32_t num = strtoul(kickoff_time, &endptr, 10);
602 if ((endptr == kickoff_time) || (endptr[0] != '\0')) {
603 fprintf(stderr, "Failed to parse kickoff time\n");
604 return -1;
607 value = convert_uint32_t_to_time_t(num);
610 pdb_set_kickoff_time(sam_pwent, value, PDB_CHANGED);
612 if (str_hex_pwd) {
613 unsigned char new_nt_p16[NT_HASH_LEN];
614 if(strlen(str_hex_pwd) != (NT_HASH_LEN *2)){
615 fprintf(stderr, "Invalid hash\n");
616 return -1;
619 pdb_gethexpwd(str_hex_pwd, new_nt_p16);
621 if (!pdb_set_nt_passwd (sam_pwent, new_nt_p16 , PDB_CHANGED)) {
622 fprintf(stderr, "Failed to set password from nt-hash\n");
623 return -1;
626 if (!pdb_set_pass_last_set_time (sam_pwent, time(NULL), PDB_CHANGED)){
627 fprintf(stderr, "Failed to set last password set time\n");
628 return -1;
630 if (!pdb_update_history(sam_pwent, new_nt_p16)){
631 fprintf(stderr, "Failed to update password history\n");
632 return -1;
636 if (NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
638 print_user_info(username, True, (str_hex_pwd != NULL ));
639 } else {
640 fprintf (stderr, "Unable to modify entry!\n");
641 TALLOC_FREE(sam_pwent);
642 return -1;
644 TALLOC_FREE(sam_pwent);
645 return 0;
648 static int set_machine_info(const char *machinename,
649 const char *account_control,
650 const char *machine_sid)
652 struct samu *sam_pwent = NULL;
653 TALLOC_CTX *tosctx;
654 uint32_t acb_flags;
655 uint32_t not_settable;
656 uint32_t new_flags;
657 struct dom_sid m_sid;
658 char *name;
659 int len;
660 bool ret;
662 len = strlen(machinename);
663 if (len == 0) {
664 fprintf(stderr, "No machine name given\n");
665 return -1;
668 tosctx = talloc_tos();
669 if (!tosctx) {
670 fprintf(stderr, "Out of memory!\n");
671 return -1;
674 sam_pwent = samu_new(tosctx);
675 if (!sam_pwent) {
676 return 1;
679 if (machinename[len-1] == '$') {
680 name = talloc_strdup(sam_pwent, machinename);
681 } else {
682 name = talloc_asprintf(sam_pwent, "%s$", machinename);
684 if (!name) {
685 fprintf(stderr, "Out of memory!\n");
686 TALLOC_FREE(sam_pwent);
687 return -1;
690 if (!strlower_m(name)) {
691 fprintf(stderr, "strlower_m %s failed\n", name);
692 TALLOC_FREE(sam_pwent);
693 return -1;
696 ret = pdb_getsampwnam(sam_pwent, name);
697 if (!ret) {
698 fprintf (stderr, "Username not found!\n");
699 TALLOC_FREE(sam_pwent);
700 return -1;
703 if (account_control) {
704 not_settable = ~(ACB_DISABLED);
706 new_flags = pdb_decode_acct_ctrl(account_control);
708 if (new_flags & not_settable) {
709 fprintf(stderr, "Can only set [D] flags\n");
710 TALLOC_FREE(sam_pwent);
711 return -1;
714 acb_flags = pdb_get_acct_ctrl(sam_pwent);
716 pdb_set_acct_ctrl(sam_pwent,
717 (acb_flags & not_settable) | new_flags,
718 PDB_CHANGED);
720 if (machine_sid) {
721 if (get_sid_from_cli_string(&m_sid, machine_sid)) {
722 fprintf(stderr, "Failed to parse SID\n");
723 return -1;
725 pdb_set_user_sid(sam_pwent, &m_sid, PDB_CHANGED);
728 if (NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
729 print_user_info(name, True, False);
730 } else {
731 fprintf (stderr, "Unable to modify entry!\n");
732 TALLOC_FREE(sam_pwent);
733 return -1;
735 TALLOC_FREE(sam_pwent);
736 return 0;
739 /*********************************************************
740 Add New User
741 **********************************************************/
742 static int new_user(const char *username, const char *fullname,
743 const char *homedir, const char *drive,
744 const char *script, const char *profile,
745 char *user_sid, bool stdin_get)
747 char *pwd1 = NULL, *pwd2 = NULL;
748 char *err = NULL, *msg = NULL;
749 struct samu *sam_pwent = NULL;
750 TALLOC_CTX *tosctx;
751 NTSTATUS status;
752 struct dom_sid u_sid;
753 int flags;
754 int ret = -1;
756 tosctx = talloc_tos();
757 if (!tosctx) {
758 fprintf(stderr, "Out of memory!\n");
759 return -1;
762 if (user_sid) {
763 if (get_sid_from_cli_string(&u_sid, user_sid)) {
764 fprintf(stderr, "Failed to parse SID\n");
765 return -1;
769 pwd1 = get_pass( "new password:", stdin_get);
770 if (pwd1 == NULL) {
771 fprintf(stderr, "Failed to read passwords.\n");
772 goto done;
774 pwd2 = get_pass( "retype new password:", stdin_get);
775 if (pwd2 == NULL) {
776 fprintf(stderr, "Failed to read passwords.\n");
777 goto done;
779 ret = strcmp(pwd1, pwd2);
780 if (ret != 0) {
781 fprintf (stderr, "Passwords do not match!\n");
782 goto done;
785 flags = LOCAL_ADD_USER | LOCAL_SET_PASSWORD;
787 status = local_password_change(username, flags, pwd1, &err, &msg);
788 if (!NT_STATUS_IS_OK(status)) {
789 if (err) fprintf(stderr, "%s", err);
790 ret = -1;
791 goto done;
794 sam_pwent = samu_new(tosctx);
795 if (!sam_pwent) {
796 fprintf(stderr, "Out of memory!\n");
797 ret = -1;
798 goto done;
801 if (!pdb_getsampwnam(sam_pwent, username)) {
802 fprintf(stderr, "User %s not found!\n", username);
803 ret = -1;
804 goto done;
807 if (fullname)
808 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
809 if (homedir)
810 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
811 if (drive)
812 pdb_set_dir_drive(sam_pwent, drive, PDB_CHANGED);
813 if (script)
814 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
815 if (profile)
816 pdb_set_profile_path(sam_pwent, profile, PDB_CHANGED);
817 if (user_sid)
818 pdb_set_user_sid(sam_pwent, &u_sid, PDB_CHANGED);
820 status = pdb_update_sam_account(sam_pwent);
821 if (!NT_STATUS_IS_OK(status)) {
822 fprintf(stderr,
823 "Failed to modify entry for user %s.!\n",
824 username);
825 ret = -1;
826 goto done;
829 print_user_info(username, True, False);
830 ret = 0;
832 done:
833 if (pwd1) memset(pwd1, 0, strlen(pwd1));
834 if (pwd2) memset(pwd2, 0, strlen(pwd2));
835 SAFE_FREE(pwd1);
836 SAFE_FREE(pwd2);
837 SAFE_FREE(err);
838 SAFE_FREE(msg);
839 TALLOC_FREE(sam_pwent);
840 return ret;
843 /*********************************************************
844 Add New Machine
845 **********************************************************/
847 static int new_machine(const char *machinename, char *machine_sid)
849 char *err = NULL, *msg = NULL;
850 struct samu *sam_pwent = NULL;
851 TALLOC_CTX *tosctx;
852 NTSTATUS status;
853 struct dom_sid m_sid;
854 char *compatpwd;
855 char *name;
856 int flags;
857 int len;
858 int ret;
860 len = strlen(machinename);
861 if (len == 0) {
862 fprintf(stderr, "No machine name given\n");
863 return -1;
866 tosctx = talloc_tos();
867 if (!tosctx) {
868 fprintf(stderr, "Out of memory!\n");
869 return -1;
872 if (machine_sid) {
873 if (get_sid_from_cli_string(&m_sid, machine_sid)) {
874 fprintf(stderr, "Failed to parse SID\n");
875 return -1;
879 compatpwd = talloc_strdup(tosctx, machinename);
880 if (!compatpwd) {
881 fprintf(stderr, "Out of memory!\n");
882 return -1;
885 if (machinename[len-1] == '$') {
886 name = talloc_strdup(tosctx, machinename);
887 compatpwd[len-1] = '\0';
888 } else {
889 name = talloc_asprintf(tosctx, "%s$", machinename);
891 if (!name) {
892 fprintf(stderr, "Out of memory!\n");
893 return -1;
896 if (!strlower_m(name)) {
897 fprintf(stderr, "strlower_m %s failed\n", name);
898 return -1;
901 flags = LOCAL_ADD_USER | LOCAL_TRUST_ACCOUNT | LOCAL_SET_PASSWORD;
903 status = local_password_change(name, flags, compatpwd, &err, &msg);
905 if (!NT_STATUS_IS_OK(status)) {
906 if (err) fprintf(stderr, "%s", err);
907 ret = -1;
910 sam_pwent = samu_new(tosctx);
911 if (!sam_pwent) {
912 fprintf(stderr, "Out of memory!\n");
913 ret = -1;
914 goto done;
917 if (!pdb_getsampwnam(sam_pwent, name)) {
918 fprintf(stderr, "Machine %s not found!\n", name);
919 ret = -1;
920 goto done;
923 if (machine_sid)
924 pdb_set_user_sid(sam_pwent, &m_sid, PDB_CHANGED);
926 status = pdb_update_sam_account(sam_pwent);
927 if (!NT_STATUS_IS_OK(status)) {
928 fprintf(stderr,
929 "Failed to modify entry for %s.!\n", name);
930 ret = -1;
931 goto done;
934 print_user_info(name, True, False);
935 ret = 0;
937 done:
938 SAFE_FREE(err);
939 SAFE_FREE(msg);
940 TALLOC_FREE(sam_pwent);
941 return ret;
944 /*********************************************************
945 Delete user entry
946 **********************************************************/
948 static int delete_user_entry(const char *username)
950 struct samu *samaccount;
952 samaccount = samu_new(NULL);
953 if (!samaccount) {
954 fprintf(stderr, "Out of memory!\n");
955 return -1;
958 if (!pdb_getsampwnam(samaccount, username)) {
959 fprintf (stderr,
960 "user %s does not exist in the passdb\n", username);
961 TALLOC_FREE(samaccount);
962 return -1;
965 if (!NT_STATUS_IS_OK(pdb_delete_sam_account(samaccount))) {
966 fprintf (stderr, "Unable to delete user %s\n", username);
967 TALLOC_FREE(samaccount);
968 return -1;
971 TALLOC_FREE(samaccount);
972 return 0;
975 /*********************************************************
976 Delete machine entry
977 **********************************************************/
979 static int delete_machine_entry(const char *machinename)
981 struct samu *samaccount = NULL;
982 const char *name;
984 if (strlen(machinename) == 0) {
985 fprintf(stderr, "No machine name given\n");
986 return -1;
989 samaccount = samu_new(NULL);
990 if (!samaccount) {
991 fprintf(stderr, "Out of memory!\n");
992 return -1;
995 if (machinename[strlen(machinename)-1] != '$') {
996 name = talloc_asprintf(samaccount, "%s$", machinename);
997 } else {
998 name = machinename;
1001 if (!pdb_getsampwnam(samaccount, name)) {
1002 fprintf (stderr,
1003 "machine %s does not exist in the passdb\n", name);
1004 TALLOC_FREE(samaccount);
1005 return -1;
1008 if (!NT_STATUS_IS_OK(pdb_delete_sam_account(samaccount))) {
1009 fprintf (stderr, "Unable to delete machine %s\n", name);
1010 TALLOC_FREE(samaccount);
1011 return -1;
1014 TALLOC_FREE(samaccount);
1015 return 0;
1018 /*********************************************************
1019 Start here.
1020 **********************************************************/
1022 int main(int argc, const char **argv)
1024 static int list_users = False;
1025 static int verbose = False;
1026 static int spstyle = False;
1027 static int machine = False;
1028 static int add_user = False;
1029 static int delete_user = False;
1030 static int modify_user = False;
1031 uint32_t setparms, checkparms;
1032 int opt;
1033 static char *full_name = NULL;
1034 static char *acct_desc = NULL;
1035 static const char *user_name = NULL;
1036 static char *home_dir = NULL;
1037 static char *home_drive = NULL;
1038 static const char *backend = NULL;
1039 static char *backend_in = NULL;
1040 static char *backend_out = NULL;
1041 static int transfer_groups = False;
1042 static int transfer_account_policies = False;
1043 static int reset_account_policies = False;
1044 static int force_initialised_password = False;
1045 static char *logon_script = NULL;
1046 static char *profile_path = NULL;
1047 static char *user_domain = NULL;
1048 static char *account_control = NULL;
1049 static char *account_policy = NULL;
1050 static char *user_sid = NULL;
1051 static char *machine_sid = NULL;
1052 static long int account_policy_value = 0;
1053 bool account_policy_value_set = False;
1054 static int badpw_reset = False;
1055 static int hours_reset = False;
1056 static char *pwd_time_format = NULL;
1057 static int pw_from_stdin = False;
1058 struct pdb_methods *bin, *bout;
1059 static char *kickoff_time = NULL;
1060 static char *str_hex_pwd = NULL;
1061 TALLOC_CTX *frame = talloc_stackframe();
1062 NTSTATUS status;
1063 poptContext pc;
1064 struct poptOption long_options[] = {
1065 POPT_AUTOHELP
1066 {"list", 'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
1067 {"verbose", 'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
1068 {"smbpasswd-style", 'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
1069 {"user", 'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
1070 {"account-desc", 'N', POPT_ARG_STRING, &acct_desc, 0, "set account description", NULL},
1071 {"fullname", 'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
1072 {"homedir", 'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
1073 {"drive", 'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
1074 {"script", 'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
1075 {"profile", 'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
1076 {"domain", 'I', POPT_ARG_STRING, &user_domain, 0, "set a users' domain", NULL},
1077 {"user SID", 'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
1078 {"machine SID", 'M', POPT_ARG_STRING, &machine_sid, 0, "set machine SID or RID", NULL},
1079 {"create", 'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
1080 {"modify", 'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
1081 {"machine", 'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
1082 {"delete", 'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
1083 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
1084 {"import", 'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
1085 {"export", 'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
1086 {"group", 'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
1087 {"policies", 'y', POPT_ARG_NONE, &transfer_account_policies, 0, "use -i and -e to move account policies between backends", NULL},
1088 {"policies-reset", 0, POPT_ARG_NONE, &reset_account_policies, 0, "restore default policies", NULL},
1089 {"account-policy", 'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
1090 {"value", 'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
1091 {"account-control", 'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
1092 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
1093 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
1094 {"logon-hours-reset", 'Z', POPT_ARG_NONE, &hours_reset, 0, "reset logon hours", NULL},
1095 {"time-format", 0, POPT_ARG_STRING, &pwd_time_format, 0, "The time format for time parameters", NULL },
1096 {"password-from-stdin", 't', POPT_ARG_NONE, &pw_from_stdin, 0, "get password from standard in", NULL},
1097 {"kickoff-time", 'K', POPT_ARG_STRING, &kickoff_time, 0, "set the kickoff time", NULL},
1098 {"set-nt-hash", 0, POPT_ARG_STRING, &str_hex_pwd, 0, "set password from nt-hash", NULL},
1099 POPT_COMMON_SAMBA
1100 POPT_TABLEEND
1103 bin = bout = NULL;
1105 smb_init_locale();
1107 setup_logging("pdbedit", DEBUG_STDOUT);
1109 pc = poptGetContext(NULL, argc, argv, long_options,
1110 POPT_CONTEXT_KEEP_FIRST);
1112 while((opt = poptGetNextOpt(pc)) != -1) {
1113 switch (opt) {
1114 case 'C':
1115 account_policy_value_set = True;
1116 break;
1120 poptGetArg(pc); /* Drop argv[0], the program name */
1122 if (user_name == NULL)
1123 user_name = poptGetArg(pc);
1125 cmdline_messaging_context(get_dyn_CONFIGFILE());
1127 if (!lp_load_global(get_dyn_CONFIGFILE())) {
1128 fprintf(stderr, "Can't load %s - run testparm to debug it\n", get_dyn_CONFIGFILE());
1129 exit(1);
1132 if (!init_names())
1133 exit(1);
1135 setparms = (backend ? BIT_BACKEND : 0) +
1136 (verbose ? BIT_VERBOSE : 0) +
1137 (spstyle ? BIT_SPSTYLE : 0) +
1138 (full_name ? BIT_FULLNAME : 0) +
1139 (home_dir ? BIT_HOMEDIR : 0) +
1140 (home_drive ? BIT_HDIRDRIVE : 0) +
1141 (logon_script ? BIT_LOGSCRIPT : 0) +
1142 (profile_path ? BIT_PROFILE : 0) +
1143 (user_domain ? BIT_USERDOMAIN : 0) +
1144 (machine ? BIT_MACHINE : 0) +
1145 (user_name ? BIT_USER : 0) +
1146 (list_users ? BIT_LIST : 0) +
1147 (force_initialised_password ? BIT_FIX_INIT : 0) +
1148 (user_sid ? BIT_USERSIDS : 0) +
1149 (machine_sid ? BIT_USERSIDS : 0) +
1150 (modify_user ? BIT_MODIFY : 0) +
1151 (add_user ? BIT_CREATE : 0) +
1152 (delete_user ? BIT_DELETE : 0) +
1153 (account_control ? BIT_ACCTCTRL : 0) +
1154 (account_policy ? BIT_ACCPOLICY : 0) +
1155 (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
1156 (backend_in ? BIT_IMPORT : 0) +
1157 (backend_out ? BIT_EXPORT : 0) +
1158 (badpw_reset ? BIT_BADPWRESET : 0) +
1159 (hours_reset ? BIT_LOGONHOURS : 0) +
1160 (kickoff_time ? BIT_KICKOFFTIME : 0) +
1161 (str_hex_pwd ? BIT_PWSETNTHASH : 0 ) +
1162 (acct_desc ? BIT_DESCRIPTION : 0);
1165 if (setparms & BIT_BACKEND) {
1166 /* HACK: set the global passdb backend by overwriting globals.
1167 * This way we can use regular pdb functions for default
1168 * operations that do not involve passdb migrations */
1169 lp_set_cmdline("passdb backend", backend);
1170 } else {
1171 backend = lp_passdb_backend();
1174 if (!initialize_password_db(False, NULL)) {
1175 fprintf(stderr, "Can't initialize passdb backend.\n");
1176 exit(1);
1179 /* the lowest bit options are always accepted */
1180 checkparms = setparms & ~MASK_ALWAYS_GOOD;
1182 if (checkparms & BIT_FIX_INIT) {
1183 return fix_users_list();
1186 /* account policy operations */
1187 if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
1188 uint32_t value;
1189 enum pdb_policy_type field = account_policy_name_to_typenum(account_policy);
1190 if (field == 0) {
1191 const char **names;
1192 int count;
1193 int i;
1194 account_policy_names_list(talloc_tos(), &names, &count);
1195 fprintf(stderr, "No account policy by that name!\n");
1196 if (count !=0) {
1197 fprintf(stderr, "Account policy names are:\n");
1198 for (i = 0; i < count ; i++) {
1199 d_fprintf(stderr, "%s\n", names[i]);
1202 TALLOC_FREE(names);
1203 exit(1);
1205 if (!pdb_get_account_policy(field, &value)) {
1206 fprintf(stderr, "valid account policy, but unable to fetch value!\n");
1207 if (!account_policy_value_set)
1208 exit(1);
1210 printf("account policy \"%s\" description: %s\n", account_policy, account_policy_get_desc(field));
1211 if (account_policy_value_set) {
1212 printf("account policy \"%s\" value was: %u\n", account_policy, value);
1213 if (!pdb_set_account_policy(field, account_policy_value)) {
1214 fprintf(stderr, "valid account policy, but unable to set value!\n");
1215 exit(1);
1217 printf("account policy \"%s\" value is now: %lu\n", account_policy, account_policy_value);
1218 exit(0);
1219 } else {
1220 printf("account policy \"%s\" value is: %u\n", account_policy, value);
1221 exit(0);
1225 if (reset_account_policies) {
1226 if (reinit_account_policies()) {
1227 exit(1);
1230 exit(0);
1233 /* import and export operations */
1235 if (((checkparms & BIT_IMPORT) ||
1236 (checkparms & BIT_EXPORT)) &&
1237 !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
1239 if (backend_in) {
1240 status = make_pdb_method_name(&bin, backend_in);
1241 } else {
1242 status = make_pdb_method_name(&bin, backend);
1245 if (!NT_STATUS_IS_OK(status)) {
1246 fprintf(stderr, "Unable to initialize %s.\n",
1247 backend_in ? backend_in : backend);
1248 return 1;
1251 if (backend_out) {
1252 status = make_pdb_method_name(&bout, backend_out);
1253 } else {
1254 status = make_pdb_method_name(&bout, backend);
1257 if (!NT_STATUS_IS_OK(status)) {
1258 fprintf(stderr, "Unable to initialize %s.\n",
1259 backend_out ? backend_out : backend);
1260 return 1;
1263 if (transfer_account_policies) {
1265 if (!(checkparms & BIT_USER)) {
1266 return export_account_policies(bin, bout);
1269 } else if (transfer_groups) {
1271 if (!(checkparms & BIT_USER)) {
1272 return export_groups(bin, bout);
1275 } else {
1276 return export_database(bin, bout,
1277 (checkparms & BIT_USER) ? user_name : NULL);
1281 /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
1282 /* fake up BIT_LIST if only BIT_USER is defined */
1283 if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
1284 checkparms += BIT_LIST;
1287 /* modify flag is optional to maintain backwards compatibility */
1288 /* fake up BIT_MODIFY if BIT_USER and at least one of MASK_USER_GOOD is defined */
1289 if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
1290 checkparms += BIT_MODIFY;
1293 /* list users operations */
1294 if (checkparms & BIT_LIST) {
1295 if (!(checkparms & ~BIT_LIST)) {
1296 return print_users_list(verbose, spstyle);
1298 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
1299 return print_user_info(user_name, verbose, spstyle);
1303 /* mask out users options */
1304 checkparms &= ~MASK_USER_GOOD;
1306 /* if bad password count is reset, we must be modifying */
1307 if (checkparms & BIT_BADPWRESET) {
1308 checkparms |= BIT_MODIFY;
1309 checkparms &= ~BIT_BADPWRESET;
1312 /* if logon hours is reset, must modify */
1313 if (checkparms & BIT_LOGONHOURS) {
1314 checkparms |= BIT_MODIFY;
1315 checkparms &= ~BIT_LOGONHOURS;
1318 /* account operation */
1319 if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
1320 /* check use of -u option */
1321 if (!(checkparms & BIT_USER)) {
1322 fprintf (stderr, "Username not specified! (use -u option)\n");
1323 return -1;
1326 /* account creation operations */
1327 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
1328 if (checkparms & BIT_MACHINE) {
1329 return new_machine(user_name, machine_sid);
1330 } else {
1331 return new_user(user_name, full_name,
1332 home_dir, home_drive,
1333 logon_script, profile_path,
1334 user_sid, pw_from_stdin);
1338 /* account deletion operations */
1339 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
1340 if (checkparms & BIT_MACHINE) {
1341 return delete_machine_entry(user_name);
1342 } else {
1343 return delete_user_entry(user_name);
1347 /* account modification operations */
1348 if (!(checkparms & ~(BIT_MODIFY + BIT_USER + BIT_MACHINE))) {
1349 if (checkparms & BIT_MACHINE) {
1350 return set_machine_info(user_name,
1351 account_control,
1352 machine_sid);
1353 } else {
1354 return set_user_info(user_name, full_name,
1355 home_dir, acct_desc,
1356 home_drive, logon_script,
1357 profile_path, account_control,
1358 user_sid, user_domain,
1359 badpw_reset, hours_reset,
1360 kickoff_time, str_hex_pwd);
1365 if (setparms >= 0x20) {
1366 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
1368 poptPrintHelp(pc, stderr, 0);
1370 TALLOC_FREE(frame);
1371 return 1;