s3-dcerpc: Pull packet in the caller, before validation
[Samba.git] / source3 / utils / pdbedit.c
blob9dff45b8f375eb7646efd5d80669fe5addb347f2
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"
27 #define BIT_BACKEND 0x00000004
28 #define BIT_VERBOSE 0x00000008
29 #define BIT_SPSTYLE 0x00000010
30 #define BIT_CAN_CHANGE 0x00000020
31 #define BIT_MUST_CHANGE 0x00000040
32 #define BIT_USERSIDS 0x00000080
33 #define BIT_FULLNAME 0x00000100
34 #define BIT_HOMEDIR 0x00000200
35 #define BIT_HDIRDRIVE 0x00000400
36 #define BIT_LOGSCRIPT 0x00000800
37 #define BIT_PROFILE 0x00001000
38 #define BIT_MACHINE 0x00002000
39 #define BIT_USERDOMAIN 0x00004000
40 #define BIT_USER 0x00008000
41 #define BIT_LIST 0x00010000
42 #define BIT_MODIFY 0x00020000
43 #define BIT_CREATE 0x00040000
44 #define BIT_DELETE 0x00080000
45 #define BIT_ACCPOLICY 0x00100000
46 #define BIT_ACCPOLVAL 0x00200000
47 #define BIT_ACCTCTRL 0x00400000
48 #define BIT_RESERV_7 0x00800000
49 #define BIT_IMPORT 0x01000000
50 #define BIT_EXPORT 0x02000000
51 #define BIT_FIX_INIT 0x04000000
52 #define BIT_BADPWRESET 0x08000000
53 #define BIT_LOGONHOURS 0x10000000
54 #define BIT_KICKOFFTIME 0x20000000
55 #define BIT_DESCRIPTION 0x40000000
57 #define MASK_ALWAYS_GOOD 0x0000001F
58 #define MASK_USER_GOOD 0x60405FE0
60 static int get_sid_from_cli_string(struct dom_sid *sid, const char *str_sid)
62 uint32_t rid;
64 if (!string_to_sid(sid, str_sid)) {
65 /* not a complete sid, may be a RID,
66 * try building a SID */
68 if (sscanf(str_sid, "%u", &rid) != 1) {
69 fprintf(stderr, "Error passed string is not "
70 "a complete SID or RID!\n");
71 return -1;
73 sid_compose(sid, get_global_sam_sid(), rid);
76 return 0;
79 /*********************************************************
80 Add all currently available users to another db
81 ********************************************************/
83 static int export_database (struct pdb_methods *in,
84 struct pdb_methods *out,
85 const char *username)
87 NTSTATUS status;
88 struct pdb_search *u_search;
89 struct samr_displayentry userentry;
91 DEBUG(3, ("export_database: username=\"%s\"\n", username ? username : "(NULL)"));
93 u_search = pdb_search_init(talloc_tos(), PDB_USER_SEARCH);
94 if (u_search == NULL) {
95 DEBUG(0, ("pdb_search_init failed\n"));
96 return 1;
99 if (!in->search_users(in, u_search, 0)) {
100 DEBUG(0, ("Could not start searching users\n"));
101 TALLOC_FREE(u_search);
102 return 1;
105 while (u_search->next_entry(u_search, &userentry)) {
106 struct samu *user;
107 struct samu *account;
108 struct dom_sid user_sid;
110 DEBUG(4, ("Processing account %s\n", userentry.account_name));
112 if ((username != NULL)
113 && (strcmp(username, userentry.account_name) != 0)) {
115 * ignore unwanted users
117 continue;
120 user = samu_new(talloc_tos());
121 if (user == NULL) {
122 DEBUG(0, ("talloc failed\n"));
123 break;
126 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
128 status = in->getsampwsid(in, user, &user_sid);
130 if (!NT_STATUS_IS_OK(status)) {
131 DEBUG(2, ("getsampwsid failed: %s\n",
132 nt_errstr(status)));
133 TALLOC_FREE(user);
134 continue;
137 account = samu_new(NULL);
138 if (account == NULL) {
139 fprintf(stderr, "export_database: Memory allocation "
140 "failure!\n");
141 TALLOC_FREE( user );
142 TALLOC_FREE(u_search);
143 return 1;
146 printf("Importing account for %s...", user->username);
147 status = out->getsampwnam(out, account, user->username);
149 if (NT_STATUS_IS_OK(status)) {
150 status = out->update_sam_account( out, user );
151 } else {
152 status = out->add_sam_account(out, user);
155 if ( NT_STATUS_IS_OK(status) ) {
156 printf( "ok\n");
157 } else {
158 printf( "failed\n");
161 TALLOC_FREE( account );
162 TALLOC_FREE( user );
165 TALLOC_FREE(u_search);
167 return 0;
170 /*********************************************************
171 Add all currently available group mappings to another db
172 ********************************************************/
174 static int export_groups (struct pdb_methods *in, struct pdb_methods *out)
176 GROUP_MAP *maps = NULL;
177 size_t i, entries = 0;
178 NTSTATUS status;
180 status = in->enum_group_mapping(in, get_global_sam_sid(),
181 SID_NAME_DOM_GRP, &maps, &entries, False);
183 if ( NT_STATUS_IS_ERR(status) ) {
184 fprintf(stderr, "Unable to enumerate group map entries.\n");
185 return 1;
188 for (i=0; i<entries; i++) {
189 out->add_group_mapping_entry(out, &(maps[i]));
192 SAFE_FREE( maps );
194 return 0;
197 /*********************************************************
198 Reset account policies to their default values and remove marker
199 ********************************************************/
201 static int reinit_account_policies (void)
203 int i;
205 for (i=1; decode_account_policy_name(i) != NULL; i++) {
206 uint32_t policy_value;
207 if (!account_policy_get_default(i, &policy_value)) {
208 fprintf(stderr, "Can't get default account policy\n");
209 return -1;
211 if (!account_policy_set(i, policy_value)) {
212 fprintf(stderr, "Can't set account policy in tdb\n");
213 return -1;
217 return 0;
221 /*********************************************************
222 Add all currently available account policy from tdb to one backend
223 ********************************************************/
225 static int export_account_policies (struct pdb_methods *in, struct pdb_methods *out)
227 int i;
229 for ( i=1; decode_account_policy_name(i) != NULL; i++ ) {
230 uint32_t policy_value;
231 NTSTATUS status;
233 status = in->get_account_policy(in, i, &policy_value);
235 if ( NT_STATUS_IS_ERR(status) ) {
236 fprintf(stderr, "Unable to get account policy from %s\n", in->name);
237 return -1;
240 status = out->set_account_policy(out, i, policy_value);
242 if ( NT_STATUS_IS_ERR(status) ) {
243 fprintf(stderr, "Unable to migrate account policy to %s\n", out->name);
244 return -1;
248 return 0;
252 /*********************************************************
253 Print info from sam structure
254 **********************************************************/
256 static int print_sam_info (struct samu *sam_pwent, bool verbosity, bool smbpwdstyle)
258 uid_t uid;
259 time_t tmp;
261 /* TODO: check if entry is a user or a workstation */
262 if (!sam_pwent) return -1;
264 if (verbosity) {
265 char temp[44];
266 const uint8_t *hours;
268 printf ("Unix username: %s\n", pdb_get_username(sam_pwent));
269 printf ("NT username: %s\n", pdb_get_nt_username(sam_pwent));
270 printf ("Account Flags: %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
271 printf ("User SID: %s\n",
272 sid_string_tos(pdb_get_user_sid(sam_pwent)));
273 printf ("Primary Group SID: %s\n",
274 sid_string_tos(pdb_get_group_sid(sam_pwent)));
275 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent));
276 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
277 printf ("HomeDir Drive: %s\n", pdb_get_dir_drive(sam_pwent));
278 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent));
279 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent));
280 printf ("Domain: %s\n", pdb_get_domain(sam_pwent));
281 printf ("Account desc: %s\n", pdb_get_acct_desc(sam_pwent));
282 printf ("Workstations: %s\n", pdb_get_workstations(sam_pwent));
283 printf ("Munged dial: %s\n", pdb_get_munged_dial(sam_pwent));
285 tmp = pdb_get_logon_time(sam_pwent);
286 printf ("Logon time: %s\n",
287 tmp ? http_timestring(talloc_tos(), tmp) : "0");
289 tmp = pdb_get_logoff_time(sam_pwent);
290 printf ("Logoff time: %s\n",
291 tmp ? http_timestring(talloc_tos(), tmp) : "0");
293 tmp = pdb_get_kickoff_time(sam_pwent);
294 printf ("Kickoff time: %s\n",
295 tmp ? http_timestring(talloc_tos(), tmp) : "0");
297 tmp = pdb_get_pass_last_set_time(sam_pwent);
298 printf ("Password last set: %s\n",
299 tmp ? http_timestring(talloc_tos(), tmp) : "0");
301 tmp = pdb_get_pass_can_change_time(sam_pwent);
302 printf ("Password can change: %s\n",
303 tmp ? http_timestring(talloc_tos(), tmp) : "0");
305 tmp = pdb_get_pass_must_change_time(sam_pwent);
306 printf ("Password must change: %s\n",
307 tmp ? http_timestring(talloc_tos(), tmp) : "0");
309 tmp = pdb_get_bad_password_time(sam_pwent);
310 printf ("Last bad password : %s\n",
311 tmp ? http_timestring(talloc_tos(), tmp) : "0");
312 printf ("Bad password count : %d\n",
313 pdb_get_bad_password_count(sam_pwent));
315 hours = pdb_get_hours(sam_pwent);
316 pdb_sethexhours(temp, hours);
317 printf ("Logon hours : %s\n", temp);
319 } else if (smbpwdstyle) {
320 char lm_passwd[33];
321 char nt_passwd[33];
323 uid = nametouid(pdb_get_username(sam_pwent));
324 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
325 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
327 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
328 pdb_get_username(sam_pwent),
329 (unsigned long)uid,
330 lm_passwd,
331 nt_passwd,
332 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
333 (uint32)convert_time_t_to_uint32_t(pdb_get_pass_last_set_time(sam_pwent)));
334 } else {
335 uid = nametouid(pdb_get_username(sam_pwent));
336 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid,
337 pdb_get_fullname(sam_pwent));
340 return 0;
343 /*********************************************************
344 Get an Print User Info
345 **********************************************************/
347 static int print_user_info(const char *username,
348 bool verbosity, bool smbpwdstyle)
350 struct samu *sam_pwent = NULL;
351 bool bret;
352 int ret;
354 sam_pwent = samu_new(NULL);
355 if (!sam_pwent) {
356 return -1;
359 bret = pdb_getsampwnam(sam_pwent, username);
360 if (!bret) {
361 fprintf (stderr, "Username not found!\n");
362 TALLOC_FREE(sam_pwent);
363 return -1;
366 ret = print_sam_info(sam_pwent, verbosity, smbpwdstyle);
368 TALLOC_FREE(sam_pwent);
369 return ret;
372 /*********************************************************
373 List Users
374 **********************************************************/
375 static int print_users_list(bool verbosity, bool smbpwdstyle)
377 struct pdb_search *u_search;
378 struct samr_displayentry userentry;
379 struct samu *sam_pwent;
380 TALLOC_CTX *tosctx;
381 struct dom_sid user_sid;
382 bool bret;
383 int ret;
385 tosctx = talloc_tos();
386 if (!tosctx) {
387 DEBUG(0, ("talloc failed\n"));
388 return 1;
391 u_search = pdb_search_users(tosctx, 0);
392 if (!u_search) {
393 DEBUG(0, ("User Search failed!\n"));
394 ret = 1;
395 goto done;
398 while (u_search->next_entry(u_search, &userentry)) {
400 sam_pwent = samu_new(tosctx);
401 if (sam_pwent == NULL) {
402 DEBUG(0, ("talloc failed\n"));
403 ret = 1;
404 goto done;
407 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
409 bret = pdb_getsampwsid(sam_pwent, &user_sid);
410 if (!bret) {
411 DEBUG(2, ("getsampwsid failed\n"));
412 TALLOC_FREE(sam_pwent);
413 continue;
416 if (verbosity) {
417 printf ("---------------\n");
419 print_sam_info(sam_pwent, verbosity, smbpwdstyle);
420 TALLOC_FREE(sam_pwent);
423 ret = 0;
425 done:
426 TALLOC_FREE(tosctx);
427 return ret;
430 /*********************************************************
431 Fix a list of Users for uninitialised passwords
432 **********************************************************/
433 static int fix_users_list(void)
435 struct pdb_search *u_search;
436 struct samr_displayentry userentry;
437 struct samu *sam_pwent;
438 TALLOC_CTX *tosctx;
439 struct dom_sid user_sid;
440 NTSTATUS status;
441 bool bret;
442 int ret;
444 tosctx = talloc_tos();
445 if (!tosctx) {
446 fprintf(stderr, "Out of memory!\n");
447 return 1;
450 u_search = pdb_search_users(tosctx, 0);
451 if (!u_search) {
452 fprintf(stderr, "User Search failed!\n");
453 ret = 1;
454 goto done;
457 while (u_search->next_entry(u_search, &userentry)) {
459 sam_pwent = samu_new(tosctx);
460 if (sam_pwent == NULL) {
461 fprintf(stderr, "Out of memory!\n");
462 ret = 1;
463 goto done;
466 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
468 bret = pdb_getsampwsid(sam_pwent, &user_sid);
469 if (!bret) {
470 DEBUG(2, ("getsampwsid failed\n"));
471 TALLOC_FREE(sam_pwent);
472 continue;
475 status = pdb_update_sam_account(sam_pwent);
476 if (!NT_STATUS_IS_OK(status)) {
477 printf("Update of user %s failed!\n",
478 pdb_get_username(sam_pwent));
480 TALLOC_FREE(sam_pwent);
483 ret = 0;
485 done:
486 TALLOC_FREE(tosctx);
487 return ret;
490 /*********************************************************
491 Set User Info
492 **********************************************************/
494 static int set_user_info(const char *username, const char *fullname,
495 const char *homedir, const char *acct_desc,
496 const char *drive, const char *script,
497 const char *profile, const char *account_control,
498 const char *user_sid, const char *user_domain,
499 const bool badpw, const bool hours,
500 const char *kickoff_time)
502 bool updated_autolock = False, updated_badpw = False;
503 struct samu *sam_pwent;
504 uint8_t hours_array[MAX_HOURS_LEN];
505 uint32_t hours_len;
506 uint32_t acb_flags;
507 uint32_t not_settable;
508 uint32_t new_flags;
509 struct dom_sid u_sid;
510 bool ret;
512 sam_pwent = samu_new(NULL);
513 if (!sam_pwent) {
514 return 1;
517 ret = pdb_getsampwnam(sam_pwent, username);
518 if (!ret) {
519 fprintf (stderr, "Username not found!\n");
520 TALLOC_FREE(sam_pwent);
521 return -1;
524 if (hours) {
525 hours_len = pdb_get_hours_len(sam_pwent);
526 memset(hours_array, 0xff, hours_len);
528 pdb_set_hours(sam_pwent, hours_array, PDB_CHANGED);
531 if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) {
532 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
535 if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) {
536 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
539 if (fullname)
540 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
541 if (acct_desc)
542 pdb_set_acct_desc(sam_pwent, acct_desc, PDB_CHANGED);
543 if (homedir)
544 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
545 if (drive)
546 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
547 if (script)
548 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
549 if (profile)
550 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
551 if (user_domain)
552 pdb_set_domain(sam_pwent, user_domain, PDB_CHANGED);
554 if (account_control) {
555 not_settable = ~(ACB_DISABLED | ACB_HOMDIRREQ |
556 ACB_PWNOTREQ | ACB_PWNOEXP | ACB_AUTOLOCK);
558 new_flags = pdb_decode_acct_ctrl(account_control);
560 if (new_flags & not_settable) {
561 fprintf(stderr, "Can only set [NDHLX] flags\n");
562 TALLOC_FREE(sam_pwent);
563 return -1;
566 acb_flags = pdb_get_acct_ctrl(sam_pwent);
568 pdb_set_acct_ctrl(sam_pwent,
569 (acb_flags & not_settable) | new_flags,
570 PDB_CHANGED);
572 if (user_sid) {
573 if (get_sid_from_cli_string(&u_sid, user_sid)) {
574 fprintf(stderr, "Failed to parse SID\n");
575 return -1;
577 pdb_set_user_sid(sam_pwent, &u_sid, PDB_CHANGED);
580 if (badpw) {
581 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
582 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
585 if (kickoff_time) {
586 char *endptr;
587 time_t value = get_time_t_max();
589 if (strcmp(kickoff_time, "never") != 0) {
590 uint32_t num = strtoul(kickoff_time, &endptr, 10);
592 if ((endptr == kickoff_time) || (endptr[0] != '\0')) {
593 fprintf(stderr, "Failed to parse kickoff time\n");
594 return -1;
597 value = convert_uint32_t_to_time_t(num);
600 pdb_set_kickoff_time(sam_pwent, value, PDB_CHANGED);
603 if (NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
604 print_user_info(username, True, False);
605 } else {
606 fprintf (stderr, "Unable to modify entry!\n");
607 TALLOC_FREE(sam_pwent);
608 return -1;
610 TALLOC_FREE(sam_pwent);
611 return 0;
614 static int set_machine_info(const char *machinename,
615 const char *account_control,
616 const char *machine_sid)
618 struct samu *sam_pwent = NULL;
619 TALLOC_CTX *tosctx;
620 uint32_t acb_flags;
621 uint32_t not_settable;
622 uint32_t new_flags;
623 struct dom_sid m_sid;
624 char *name;
625 int len;
626 bool ret;
628 len = strlen(machinename);
629 if (len == 0) {
630 fprintf(stderr, "No machine name given\n");
631 return -1;
634 tosctx = talloc_tos();
635 if (!tosctx) {
636 fprintf(stderr, "Out of memory!\n");
637 return -1;
640 sam_pwent = samu_new(tosctx);
641 if (!sam_pwent) {
642 return 1;
645 if (machinename[len-1] == '$') {
646 name = talloc_strdup(sam_pwent, machinename);
647 } else {
648 name = talloc_asprintf(sam_pwent, "%s$", machinename);
650 if (!name) {
651 fprintf(stderr, "Out of memory!\n");
652 TALLOC_FREE(sam_pwent);
653 return -1;
656 strlower_m(name);
658 ret = pdb_getsampwnam(sam_pwent, name);
659 if (!ret) {
660 fprintf (stderr, "Username not found!\n");
661 TALLOC_FREE(sam_pwent);
662 return -1;
665 if (account_control) {
666 not_settable = ~(ACB_DISABLED);
668 new_flags = pdb_decode_acct_ctrl(account_control);
670 if (new_flags & not_settable) {
671 fprintf(stderr, "Can only set [D] flags\n");
672 TALLOC_FREE(sam_pwent);
673 return -1;
676 acb_flags = pdb_get_acct_ctrl(sam_pwent);
678 pdb_set_acct_ctrl(sam_pwent,
679 (acb_flags & not_settable) | new_flags,
680 PDB_CHANGED);
682 if (machine_sid) {
683 if (get_sid_from_cli_string(&m_sid, machine_sid)) {
684 fprintf(stderr, "Failed to parse SID\n");
685 return -1;
687 pdb_set_user_sid(sam_pwent, &m_sid, PDB_CHANGED);
690 if (NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
691 print_user_info(name, True, False);
692 } else {
693 fprintf (stderr, "Unable to modify entry!\n");
694 TALLOC_FREE(sam_pwent);
695 return -1;
697 TALLOC_FREE(sam_pwent);
698 return 0;
701 /*********************************************************
702 Add New User
703 **********************************************************/
704 static int new_user(const char *username, const char *fullname,
705 const char *homedir, const char *drive,
706 const char *script, const char *profile,
707 char *user_sid, bool stdin_get)
709 char *pwd1 = NULL, *pwd2 = NULL;
710 char *err = NULL, *msg = NULL;
711 struct samu *sam_pwent = NULL;
712 TALLOC_CTX *tosctx;
713 NTSTATUS status;
714 struct dom_sid u_sid;
715 int flags;
716 int ret;
718 tosctx = talloc_tos();
719 if (!tosctx) {
720 fprintf(stderr, "Out of memory!\n");
721 return -1;
724 if (user_sid) {
725 if (get_sid_from_cli_string(&u_sid, user_sid)) {
726 fprintf(stderr, "Failed to parse SID\n");
727 return -1;
731 pwd1 = get_pass( "new password:", stdin_get);
732 pwd2 = get_pass( "retype new password:", stdin_get);
733 if (!pwd1 || !pwd2) {
734 fprintf(stderr, "Failed to read passwords.\n");
735 return -1;
737 ret = strcmp(pwd1, pwd2);
738 if (ret != 0) {
739 fprintf (stderr, "Passwords do not match!\n");
740 goto done;
743 flags = LOCAL_ADD_USER | LOCAL_SET_PASSWORD;
745 status = local_password_change(username, flags, pwd1, &err, &msg);
746 if (!NT_STATUS_IS_OK(status)) {
747 if (err) fprintf(stderr, "%s", err);
748 ret = -1;
749 goto done;
752 sam_pwent = samu_new(tosctx);
753 if (!sam_pwent) {
754 fprintf(stderr, "Out of memory!\n");
755 ret = -1;
756 goto done;
759 if (!pdb_getsampwnam(sam_pwent, username)) {
760 fprintf(stderr, "User %s not found!\n", username);
761 ret = -1;
762 goto done;
765 if (fullname)
766 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
767 if (homedir)
768 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
769 if (drive)
770 pdb_set_dir_drive(sam_pwent, drive, PDB_CHANGED);
771 if (script)
772 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
773 if (profile)
774 pdb_set_profile_path(sam_pwent, profile, PDB_CHANGED);
775 if (user_sid)
776 pdb_set_user_sid(sam_pwent, &u_sid, PDB_CHANGED);
778 status = pdb_update_sam_account(sam_pwent);
779 if (!NT_STATUS_IS_OK(status)) {
780 fprintf(stderr,
781 "Failed to modify entry for user %s.!\n",
782 username);
783 ret = -1;
784 goto done;
787 print_user_info(username, True, False);
788 ret = 0;
790 done:
791 if (pwd1) memset(pwd1, 0, strlen(pwd1));
792 if (pwd2) memset(pwd2, 0, strlen(pwd2));
793 SAFE_FREE(pwd1);
794 SAFE_FREE(pwd2);
795 SAFE_FREE(err);
796 SAFE_FREE(msg);
797 TALLOC_FREE(sam_pwent);
798 return ret;
801 /*********************************************************
802 Add New Machine
803 **********************************************************/
805 static int new_machine(const char *machinename, char *machine_sid)
807 char *err = NULL, *msg = NULL;
808 struct samu *sam_pwent = NULL;
809 TALLOC_CTX *tosctx;
810 NTSTATUS status;
811 struct dom_sid m_sid;
812 char *compatpwd;
813 char *name;
814 int flags;
815 int len;
816 int ret;
818 len = strlen(machinename);
819 if (len == 0) {
820 fprintf(stderr, "No machine name given\n");
821 return -1;
824 tosctx = talloc_tos();
825 if (!tosctx) {
826 fprintf(stderr, "Out of memory!\n");
827 return -1;
830 if (machine_sid) {
831 if (get_sid_from_cli_string(&m_sid, machine_sid)) {
832 fprintf(stderr, "Failed to parse SID\n");
833 return -1;
837 compatpwd = talloc_strdup(tosctx, machinename);
838 if (!compatpwd) {
839 fprintf(stderr, "Out of memory!\n");
840 return -1;
843 if (machinename[len-1] == '$') {
844 name = talloc_strdup(tosctx, machinename);
845 compatpwd[len-1] = '\0';
846 } else {
847 name = talloc_asprintf(tosctx, "%s$", machinename);
849 if (!name) {
850 fprintf(stderr, "Out of memory!\n");
851 return -1;
854 strlower_m(name);
856 flags = LOCAL_ADD_USER | LOCAL_TRUST_ACCOUNT | LOCAL_SET_PASSWORD;
858 status = local_password_change(name, flags, compatpwd, &err, &msg);
860 if (!NT_STATUS_IS_OK(status)) {
861 if (err) fprintf(stderr, "%s", err);
862 ret = -1;
865 sam_pwent = samu_new(tosctx);
866 if (!sam_pwent) {
867 fprintf(stderr, "Out of memory!\n");
868 ret = -1;
869 goto done;
872 if (!pdb_getsampwnam(sam_pwent, name)) {
873 fprintf(stderr, "Machine %s not found!\n", name);
874 ret = -1;
875 goto done;
878 if (machine_sid)
879 pdb_set_user_sid(sam_pwent, &m_sid, PDB_CHANGED);
881 status = pdb_update_sam_account(sam_pwent);
882 if (!NT_STATUS_IS_OK(status)) {
883 fprintf(stderr,
884 "Failed to modify entry for %s.!\n", name);
885 ret = -1;
886 goto done;
889 print_user_info(name, True, False);
890 ret = 0;
892 done:
893 SAFE_FREE(err);
894 SAFE_FREE(msg);
895 TALLOC_FREE(sam_pwent);
896 return ret;
899 /*********************************************************
900 Delete user entry
901 **********************************************************/
903 static int delete_user_entry(const char *username)
905 struct samu *samaccount;
907 samaccount = samu_new(NULL);
908 if (!samaccount) {
909 fprintf(stderr, "Out of memory!\n");
910 return -1;
913 if (!pdb_getsampwnam(samaccount, username)) {
914 fprintf (stderr,
915 "user %s does not exist in the passdb\n", username);
916 TALLOC_FREE(samaccount);
917 return -1;
920 if (!NT_STATUS_IS_OK(pdb_delete_sam_account(samaccount))) {
921 fprintf (stderr, "Unable to delete user %s\n", username);
922 TALLOC_FREE(samaccount);
923 return -1;
926 TALLOC_FREE(samaccount);
927 return 0;
930 /*********************************************************
931 Delete machine entry
932 **********************************************************/
934 static int delete_machine_entry(const char *machinename)
936 struct samu *samaccount = NULL;
937 const char *name;
939 if (strlen(machinename) == 0) {
940 fprintf(stderr, "No machine name given\n");
941 return -1;
944 samaccount = samu_new(NULL);
945 if (!samaccount) {
946 fprintf(stderr, "Out of memory!\n");
947 return -1;
950 if (machinename[strlen(machinename)-1] != '$') {
951 name = talloc_asprintf(samaccount, "%s$", machinename);
952 } else {
953 name = machinename;
956 if (!pdb_getsampwnam(samaccount, name)) {
957 fprintf (stderr,
958 "machine %s does not exist in the passdb\n", name);
959 return -1;
960 TALLOC_FREE(samaccount);
963 if (!NT_STATUS_IS_OK(pdb_delete_sam_account(samaccount))) {
964 fprintf (stderr, "Unable to delete machine %s\n", name);
965 TALLOC_FREE(samaccount);
966 return -1;
969 TALLOC_FREE(samaccount);
970 return 0;
973 /*********************************************************
974 Start here.
975 **********************************************************/
977 int main (int argc, char **argv)
979 static int list_users = False;
980 static int verbose = False;
981 static int spstyle = False;
982 static int machine = False;
983 static int add_user = False;
984 static int delete_user = False;
985 static int modify_user = False;
986 uint32 setparms, checkparms;
987 int opt;
988 static char *full_name = NULL;
989 static char *acct_desc = NULL;
990 static const char *user_name = NULL;
991 static char *home_dir = NULL;
992 static char *home_drive = NULL;
993 static const char *backend = NULL;
994 static char *backend_in = NULL;
995 static char *backend_out = NULL;
996 static int transfer_groups = False;
997 static int transfer_account_policies = False;
998 static int reset_account_policies = False;
999 static int force_initialised_password = False;
1000 static char *logon_script = NULL;
1001 static char *profile_path = NULL;
1002 static char *user_domain = NULL;
1003 static char *account_control = NULL;
1004 static char *account_policy = NULL;
1005 static char *user_sid = NULL;
1006 static char *machine_sid = NULL;
1007 static long int account_policy_value = 0;
1008 bool account_policy_value_set = False;
1009 static int badpw_reset = False;
1010 static int hours_reset = False;
1011 static char *pwd_time_format = NULL;
1012 static int pw_from_stdin = False;
1013 struct pdb_methods *bin, *bout;
1014 static char *kickoff_time = NULL;
1015 TALLOC_CTX *frame = talloc_stackframe();
1016 NTSTATUS status;
1017 poptContext pc;
1018 struct poptOption long_options[] = {
1019 POPT_AUTOHELP
1020 {"list", 'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
1021 {"verbose", 'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
1022 {"smbpasswd-style", 'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
1023 {"user", 'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
1024 {"account-desc", 'N', POPT_ARG_STRING, &acct_desc, 0, "set account description", NULL},
1025 {"fullname", 'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
1026 {"homedir", 'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
1027 {"drive", 'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
1028 {"script", 'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
1029 {"profile", 'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
1030 {"domain", 'I', POPT_ARG_STRING, &user_domain, 0, "set a users' domain", NULL},
1031 {"user SID", 'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
1032 {"machine SID", 'M', POPT_ARG_STRING, &machine_sid, 0, "set machine SID or RID", NULL},
1033 {"create", 'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
1034 {"modify", 'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
1035 {"machine", 'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
1036 {"delete", 'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
1037 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
1038 {"import", 'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
1039 {"export", 'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
1040 {"group", 'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
1041 {"policies", 'y', POPT_ARG_NONE, &transfer_account_policies, 0, "use -i and -e to move account policies between backends", NULL},
1042 {"policies-reset", 0, POPT_ARG_NONE, &reset_account_policies, 0, "restore default policies", NULL},
1043 {"account-policy", 'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
1044 {"value", 'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
1045 {"account-control", 'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
1046 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
1047 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
1048 {"logon-hours-reset", 'Z', POPT_ARG_NONE, &hours_reset, 0, "reset logon hours", NULL},
1049 {"time-format", 0, POPT_ARG_STRING, &pwd_time_format, 0, "The time format for time parameters", NULL },
1050 {"password-from-stdin", 't', POPT_ARG_NONE, &pw_from_stdin, 0, "get password from standard in", NULL},
1051 {"kickoff-time", 'K', POPT_ARG_STRING, &kickoff_time, 0, "set the kickoff time", NULL},
1052 POPT_COMMON_SAMBA
1053 POPT_TABLEEND
1056 bin = bout = NULL;
1058 load_case_tables();
1060 setup_logging("pdbedit", True);
1062 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
1063 POPT_CONTEXT_KEEP_FIRST);
1065 while((opt = poptGetNextOpt(pc)) != -1) {
1066 switch (opt) {
1067 case 'C':
1068 account_policy_value_set = True;
1069 break;
1073 poptGetArg(pc); /* Drop argv[0], the program name */
1075 if (user_name == NULL)
1076 user_name = poptGetArg(pc);
1078 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True)) {
1079 fprintf(stderr, "Can't load %s - run testparm to debug it\n", get_dyn_CONFIGFILE());
1080 exit(1);
1083 if (!init_names())
1084 exit(1);
1086 setparms = (backend ? BIT_BACKEND : 0) +
1087 (verbose ? BIT_VERBOSE : 0) +
1088 (spstyle ? BIT_SPSTYLE : 0) +
1089 (full_name ? BIT_FULLNAME : 0) +
1090 (home_dir ? BIT_HOMEDIR : 0) +
1091 (home_drive ? BIT_HDIRDRIVE : 0) +
1092 (logon_script ? BIT_LOGSCRIPT : 0) +
1093 (profile_path ? BIT_PROFILE : 0) +
1094 (user_domain ? BIT_USERDOMAIN : 0) +
1095 (machine ? BIT_MACHINE : 0) +
1096 (user_name ? BIT_USER : 0) +
1097 (list_users ? BIT_LIST : 0) +
1098 (force_initialised_password ? BIT_FIX_INIT : 0) +
1099 (user_sid ? BIT_USERSIDS : 0) +
1100 (machine_sid ? BIT_USERSIDS : 0) +
1101 (modify_user ? BIT_MODIFY : 0) +
1102 (add_user ? BIT_CREATE : 0) +
1103 (delete_user ? BIT_DELETE : 0) +
1104 (account_control ? BIT_ACCTCTRL : 0) +
1105 (account_policy ? BIT_ACCPOLICY : 0) +
1106 (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
1107 (backend_in ? BIT_IMPORT : 0) +
1108 (backend_out ? BIT_EXPORT : 0) +
1109 (badpw_reset ? BIT_BADPWRESET : 0) +
1110 (hours_reset ? BIT_LOGONHOURS : 0) +
1111 (kickoff_time ? BIT_KICKOFFTIME : 0) +
1112 (acct_desc ? BIT_DESCRIPTION : 0);
1114 if (setparms & BIT_BACKEND) {
1115 /* HACK: set the global passdb backend by overwriting globals.
1116 * This way we can use regular pdb functions for default
1117 * operations that do not involve passdb migrations */
1118 lp_set_passdb_backend(backend);
1119 } else {
1120 backend = lp_passdb_backend();
1123 if (!initialize_password_db(False, NULL)) {
1124 fprintf(stderr, "Can't initialize passdb backend.\n");
1125 exit(1);
1128 /* the lowest bit options are always accepted */
1129 checkparms = setparms & ~MASK_ALWAYS_GOOD;
1131 if (checkparms & BIT_FIX_INIT) {
1132 return fix_users_list();
1135 /* account policy operations */
1136 if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
1137 uint32_t value;
1138 enum pdb_policy_type field = account_policy_name_to_typenum(account_policy);
1139 if (field == 0) {
1140 const char **names;
1141 int count;
1142 int i;
1143 account_policy_names_list(&names, &count);
1144 fprintf(stderr, "No account policy by that name!\n");
1145 if (count !=0) {
1146 fprintf(stderr, "Account policy names are:\n");
1147 for (i = 0; i < count ; i++) {
1148 d_fprintf(stderr, "%s\n", names[i]);
1151 SAFE_FREE(names);
1152 exit(1);
1154 if (!pdb_get_account_policy(field, &value)) {
1155 fprintf(stderr, "valid account policy, but unable to fetch value!\n");
1156 if (!account_policy_value_set)
1157 exit(1);
1159 printf("account policy \"%s\" description: %s\n", account_policy, account_policy_get_desc(field));
1160 if (account_policy_value_set) {
1161 printf("account policy \"%s\" value was: %u\n", account_policy, value);
1162 if (!pdb_set_account_policy(field, account_policy_value)) {
1163 fprintf(stderr, "valid account policy, but unable to set value!\n");
1164 exit(1);
1166 printf("account policy \"%s\" value is now: %lu\n", account_policy, account_policy_value);
1167 exit(0);
1168 } else {
1169 printf("account policy \"%s\" value is: %u\n", account_policy, value);
1170 exit(0);
1174 if (reset_account_policies) {
1175 if (!reinit_account_policies()) {
1176 exit(1);
1179 exit(0);
1182 /* import and export operations */
1184 if (((checkparms & BIT_IMPORT) ||
1185 (checkparms & BIT_EXPORT)) &&
1186 !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
1188 if (backend_in) {
1189 status = make_pdb_method_name(&bin, backend_in);
1190 } else {
1191 status = make_pdb_method_name(&bin, backend);
1194 if (!NT_STATUS_IS_OK(status)) {
1195 fprintf(stderr, "Unable to initialize %s.\n",
1196 backend_in ? backend_in : backend);
1197 return 1;
1200 if (backend_out) {
1201 status = make_pdb_method_name(&bout, backend_out);
1202 } else {
1203 status = make_pdb_method_name(&bout, backend);
1206 if (!NT_STATUS_IS_OK(status)) {
1207 fprintf(stderr, "Unable to initialize %s.\n",
1208 backend_out ? backend_out : backend);
1209 return 1;
1212 if (transfer_account_policies) {
1214 if (!(checkparms & BIT_USER)) {
1215 return export_account_policies(bin, bout);
1218 } else if (transfer_groups) {
1220 if (!(checkparms & BIT_USER)) {
1221 return export_groups(bin, bout);
1224 } else {
1225 return export_database(bin, bout,
1226 (checkparms & BIT_USER) ? user_name : NULL);
1230 /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
1231 /* fake up BIT_LIST if only BIT_USER is defined */
1232 if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
1233 checkparms += BIT_LIST;
1236 /* modify flag is optional to maintain backwards compatibility */
1237 /* fake up BIT_MODIFY if BIT_USER and at least one of MASK_USER_GOOD is defined */
1238 if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
1239 checkparms += BIT_MODIFY;
1242 /* list users operations */
1243 if (checkparms & BIT_LIST) {
1244 if (!(checkparms & ~BIT_LIST)) {
1245 return print_users_list(verbose, spstyle);
1247 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
1248 return print_user_info(user_name, verbose, spstyle);
1252 /* mask out users options */
1253 checkparms &= ~MASK_USER_GOOD;
1255 /* if bad password count is reset, we must be modifying */
1256 if (checkparms & BIT_BADPWRESET) {
1257 checkparms |= BIT_MODIFY;
1258 checkparms &= ~BIT_BADPWRESET;
1261 /* if logon hours is reset, must modify */
1262 if (checkparms & BIT_LOGONHOURS) {
1263 checkparms |= BIT_MODIFY;
1264 checkparms &= ~BIT_LOGONHOURS;
1267 /* account operation */
1268 if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
1269 /* check use of -u option */
1270 if (!(checkparms & BIT_USER)) {
1271 fprintf (stderr, "Username not specified! (use -u option)\n");
1272 return -1;
1275 /* account creation operations */
1276 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
1277 if (checkparms & BIT_MACHINE) {
1278 return new_machine(user_name, machine_sid);
1279 } else {
1280 return new_user(user_name, full_name,
1281 home_dir, home_drive,
1282 logon_script, profile_path,
1283 user_sid, pw_from_stdin);
1287 /* account deletion operations */
1288 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
1289 if (checkparms & BIT_MACHINE) {
1290 return delete_machine_entry(user_name);
1291 } else {
1292 return delete_user_entry(user_name);
1296 /* account modification operations */
1297 if (!(checkparms & ~(BIT_MODIFY + BIT_USER + BIT_MACHINE))) {
1298 if (checkparms & BIT_MACHINE) {
1299 return set_machine_info(user_name,
1300 account_control,
1301 machine_sid);
1302 } else {
1303 return set_user_info(user_name, full_name,
1304 home_dir, acct_desc,
1305 home_drive, logon_script,
1306 profile_path, account_control,
1307 user_sid, user_domain,
1308 badpw_reset, hours_reset,
1309 kickoff_time);
1314 if (setparms >= 0x20) {
1315 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
1317 poptPrintHelp(pc, stderr, 0);
1319 TALLOC_FREE(frame);
1320 return 1;