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/>.
24 #include "popt_common.h"
25 #include "../librpc/gen_ndr/samr.h"
26 #include "../libcli/security/security.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)
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");
76 sid_compose(sid, get_global_sam_sid(), rid);
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,
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"));
102 if (!in->search_users(in, u_search, 0)) {
103 DEBUG(0, ("Could not start searching users\n"));
104 TALLOC_FREE(u_search);
108 while (u_search->next_entry(u_search, &userentry)) {
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
123 user = samu_new(talloc_tos());
125 DEBUG(0, ("talloc failed\n"));
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",
140 account = samu_new(NULL);
141 if (account == NULL) {
142 fprintf(stderr, "export_database: Memory allocation "
145 TALLOC_FREE(u_search);
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 );
155 status = out->add_sam_account(out, user);
158 if ( NT_STATUS_IS_OK(status) ) {
164 TALLOC_FREE( account );
168 TALLOC_FREE(u_search);
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;
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");
191 for (i=0; i<entries; i++) {
192 out->add_group_mapping_entry(out, maps[i]);
200 /*********************************************************
201 Reset account policies to their default values and remove marker
202 ********************************************************/
204 static int reinit_account_policies (void)
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");
214 if (!account_policy_set(i, policy_value)) {
215 fprintf(stderr, "Can't set account policy in tdb\n");
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)
232 for ( i=1; decode_account_policy_name(i) != NULL; i++ ) {
233 uint32_t policy_value;
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);
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);
255 /*********************************************************
256 Print info from sam structure
257 **********************************************************/
259 static int print_sam_info (struct samu *sam_pwent, bool verbosity, bool smbpwdstyle)
264 /* TODO: check if entry is a user or a workstation */
265 if (!sam_pwent) return -1;
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);
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) {
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),
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)));
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));
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;
363 sam_pwent = samu_new(NULL);
368 bret = pdb_getsampwnam(sam_pwent, username);
370 fprintf (stderr, "Username not found!\n");
371 TALLOC_FREE(sam_pwent);
375 ret = print_sam_info(sam_pwent, verbosity, smbpwdstyle);
377 TALLOC_FREE(sam_pwent);
381 /*********************************************************
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;
390 struct dom_sid user_sid;
394 tosctx = talloc_tos();
396 DEBUG(0, ("talloc failed\n"));
400 u_search = pdb_search_users(tosctx, 0);
402 DEBUG(0, ("User Search failed!\n"));
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"));
416 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
418 bret = pdb_getsampwsid(sam_pwent, &user_sid);
420 DEBUG(2, ("getsampwsid failed\n"));
421 TALLOC_FREE(sam_pwent);
426 printf ("---------------\n");
428 print_sam_info(sam_pwent, verbosity, smbpwdstyle);
429 TALLOC_FREE(sam_pwent);
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;
448 struct dom_sid user_sid;
453 tosctx = talloc_tos();
455 fprintf(stderr, "Out of memory!\n");
459 u_search = pdb_search_users(tosctx, 0);
461 fprintf(stderr, "User Search failed!\n");
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");
475 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
477 bret = pdb_getsampwsid(sam_pwent, &user_sid);
479 DEBUG(2, ("getsampwsid failed\n"));
480 TALLOC_FREE(sam_pwent);
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);
499 /*********************************************************
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];
516 uint32_t not_settable;
518 struct dom_sid u_sid;
521 sam_pwent = samu_new(NULL);
526 ret = pdb_getsampwnam(sam_pwent, username);
528 fprintf (stderr, "Username not found!\n");
529 TALLOC_FREE(sam_pwent);
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"));
549 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
551 pdb_set_acct_desc(sam_pwent, acct_desc, PDB_CHANGED);
553 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
555 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
557 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
559 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
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);
575 acb_flags = pdb_get_acct_ctrl(sam_pwent);
577 pdb_set_acct_ctrl(sam_pwent,
578 (acb_flags & not_settable) | new_flags,
582 if (get_sid_from_cli_string(&u_sid, user_sid)) {
583 fprintf(stderr, "Failed to parse SID\n");
586 pdb_set_user_sid(sam_pwent, &u_sid, PDB_CHANGED);
590 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
591 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
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");
606 value = convert_uint32_t_to_time_t(num);
609 pdb_set_kickoff_time(sam_pwent, value, PDB_CHANGED);
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");
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");
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");
629 if (!pdb_update_history(sam_pwent, new_nt_p16)){
630 fprintf(stderr, "Failed to update password history\n");
635 if (NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
637 print_user_info(username, True, (str_hex_pwd != NULL ));
639 fprintf (stderr, "Unable to modify entry!\n");
640 TALLOC_FREE(sam_pwent);
643 TALLOC_FREE(sam_pwent);
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;
654 uint32_t not_settable;
656 struct dom_sid m_sid;
661 len = strlen(machinename);
663 fprintf(stderr, "No machine name given\n");
667 tosctx = talloc_tos();
669 fprintf(stderr, "Out of memory!\n");
673 sam_pwent = samu_new(tosctx);
678 if (machinename[len-1] == '$') {
679 name = talloc_strdup(sam_pwent, machinename);
681 name = talloc_asprintf(sam_pwent, "%s$", machinename);
684 fprintf(stderr, "Out of memory!\n");
685 TALLOC_FREE(sam_pwent);
689 if (!strlower_m(name)) {
690 fprintf(stderr, "strlower_m %s failed\n", name);
691 TALLOC_FREE(sam_pwent);
695 ret = pdb_getsampwnam(sam_pwent, name);
697 fprintf (stderr, "Username not found!\n");
698 TALLOC_FREE(sam_pwent);
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);
713 acb_flags = pdb_get_acct_ctrl(sam_pwent);
715 pdb_set_acct_ctrl(sam_pwent,
716 (acb_flags & not_settable) | new_flags,
720 if (get_sid_from_cli_string(&m_sid, machine_sid)) {
721 fprintf(stderr, "Failed to parse SID\n");
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);
730 fprintf (stderr, "Unable to modify entry!\n");
731 TALLOC_FREE(sam_pwent);
734 TALLOC_FREE(sam_pwent);
738 /*********************************************************
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;
751 struct dom_sid u_sid;
755 tosctx = talloc_tos();
757 fprintf(stderr, "Out of memory!\n");
762 if (get_sid_from_cli_string(&u_sid, user_sid)) {
763 fprintf(stderr, "Failed to parse SID\n");
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");
774 ret = strcmp(pwd1, pwd2);
776 fprintf (stderr, "Passwords do not match!\n");
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);
789 sam_pwent = samu_new(tosctx);
791 fprintf(stderr, "Out of memory!\n");
796 if (!pdb_getsampwnam(sam_pwent, username)) {
797 fprintf(stderr, "User %s not found!\n", username);
803 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
805 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
807 pdb_set_dir_drive(sam_pwent, drive, PDB_CHANGED);
809 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
811 pdb_set_profile_path(sam_pwent, profile, PDB_CHANGED);
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)) {
818 "Failed to modify entry for user %s.!\n",
824 print_user_info(username, True, False);
828 if (pwd1) memset(pwd1, 0, strlen(pwd1));
829 if (pwd2) memset(pwd2, 0, strlen(pwd2));
834 TALLOC_FREE(sam_pwent);
838 /*********************************************************
840 **********************************************************/
842 static int new_machine(const char *machinename, char *machine_sid)
844 char *err = NULL, *msg = NULL;
845 struct samu *sam_pwent = NULL;
848 struct dom_sid m_sid;
855 len = strlen(machinename);
857 fprintf(stderr, "No machine name given\n");
861 tosctx = talloc_tos();
863 fprintf(stderr, "Out of memory!\n");
868 if (get_sid_from_cli_string(&m_sid, machine_sid)) {
869 fprintf(stderr, "Failed to parse SID\n");
874 compatpwd = talloc_strdup(tosctx, machinename);
876 fprintf(stderr, "Out of memory!\n");
880 if (machinename[len-1] == '$') {
881 name = talloc_strdup(tosctx, machinename);
882 compatpwd[len-1] = '\0';
884 name = talloc_asprintf(tosctx, "%s$", machinename);
887 fprintf(stderr, "Out of memory!\n");
891 if (!strlower_m(name)) {
892 fprintf(stderr, "strlower_m %s failed\n", name);
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);
905 sam_pwent = samu_new(tosctx);
907 fprintf(stderr, "Out of memory!\n");
912 if (!pdb_getsampwnam(sam_pwent, name)) {
913 fprintf(stderr, "Machine %s not found!\n", name);
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)) {
924 "Failed to modify entry for %s.!\n", name);
929 print_user_info(name, True, False);
935 TALLOC_FREE(sam_pwent);
939 /*********************************************************
941 **********************************************************/
943 static int delete_user_entry(const char *username)
945 struct samu *samaccount;
947 samaccount = samu_new(NULL);
949 fprintf(stderr, "Out of memory!\n");
953 if (!pdb_getsampwnam(samaccount, username)) {
955 "user %s does not exist in the passdb\n", username);
956 TALLOC_FREE(samaccount);
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);
966 TALLOC_FREE(samaccount);
970 /*********************************************************
972 **********************************************************/
974 static int delete_machine_entry(const char *machinename)
976 struct samu *samaccount = NULL;
979 if (strlen(machinename) == 0) {
980 fprintf(stderr, "No machine name given\n");
984 samaccount = samu_new(NULL);
986 fprintf(stderr, "Out of memory!\n");
990 if (machinename[strlen(machinename)-1] != '$') {
991 name = talloc_asprintf(samaccount, "%s$", machinename);
996 if (!pdb_getsampwnam(samaccount, name)) {
998 "machine %s does not exist in the passdb\n", name);
999 TALLOC_FREE(samaccount);
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);
1009 TALLOC_FREE(samaccount);
1013 /*********************************************************
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;
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();
1059 struct poptOption long_options[] = {
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},
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) {
1110 account_policy_value_set = True;
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());
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);
1164 backend = lp_passdb_backend();
1167 if (!initialize_password_db(False, NULL)) {
1168 fprintf(stderr, "Can't initialize passdb backend.\n");
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))) {
1182 enum pdb_policy_type field = account_policy_name_to_typenum(account_policy);
1187 account_policy_names_list(talloc_tos(), &names, &count);
1188 fprintf(stderr, "No account policy by that name!\n");
1190 fprintf(stderr, "Account policy names are:\n");
1191 for (i = 0; i < count ; i++) {
1192 d_fprintf(stderr, "%s\n", names[i]);
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)
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");
1210 printf("account policy \"%s\" value is now: %lu\n", account_policy, account_policy_value);
1213 printf("account policy \"%s\" value is: %u\n", account_policy, value);
1218 if (reset_account_policies) {
1219 if (reinit_account_policies()) {
1226 /* import and export operations */
1228 if (((checkparms & BIT_IMPORT) ||
1229 (checkparms & BIT_EXPORT)) &&
1230 !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
1233 status = make_pdb_method_name(&bin, backend_in);
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);
1245 status = make_pdb_method_name(&bout, backend_out);
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);
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);
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");
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);
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);
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,
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);