📄 pdbedit.c
字号:
static int set_user_info (struct pdb_context *in, const char *username, const char *fullname, const char *homedir, const char *acct_desc, const char *drive, const char *script, const char *profile, const char *account_control, const char *user_sid, const char *group_sid, const char *user_domain, const BOOL badpw, const BOOL hours, time_t pwd_can_change, time_t pwd_must_change){ BOOL updated_autolock = False, updated_badpw = False; SAM_ACCOUNT *sam_pwent=NULL; BOOL ret; pdb_init_sam(&sam_pwent); ret = NT_STATUS_IS_OK(in->pdb_getsampwnam (in, sam_pwent, username)); if (ret==False) { fprintf (stderr, "Username not found!\n"); pdb_free_sam(&sam_pwent); return -1; } if (hours) { uint8 hours_array[MAX_HOURS_LEN]; uint32 hours_len; hours_len = pdb_get_hours_len(sam_pwent); memset(hours_array, 0xff, hours_len); pdb_set_hours(sam_pwent, hours_array, PDB_CHANGED); } if (pwd_can_change != -1) { pdb_set_pass_can_change_time(sam_pwent, pwd_can_change, PDB_CHANGED); } if (pwd_must_change != -1) { pdb_set_pass_must_change_time(sam_pwent, pwd_must_change, PDB_CHANGED); } if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) { DEBUG(2,("pdb_update_autolock_flag failed.\n")); } if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) { DEBUG(2,("pdb_update_bad_password_count failed.\n")); } if (fullname) pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED); if (acct_desc) pdb_set_acct_desc(sam_pwent, acct_desc, PDB_CHANGED); if (homedir) pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED); if (drive) pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED); if (script) pdb_set_logon_script(sam_pwent, script, PDB_CHANGED); if (profile) pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED); if (user_domain) pdb_set_domain(sam_pwent, user_domain, PDB_CHANGED); if (account_control) { uint16 not_settable = ~(ACB_DISABLED|ACB_HOMDIRREQ|ACB_PWNOTREQ| ACB_PWNOEXP|ACB_AUTOLOCK); uint16 newflag = pdb_decode_acct_ctrl(account_control); if (newflag & not_settable) { fprintf(stderr, "Can only set [NDHLX] flags\n"); pdb_free_sam(&sam_pwent); return -1; } pdb_set_acct_ctrl(sam_pwent, (pdb_get_acct_ctrl(sam_pwent) & not_settable) | newflag, PDB_CHANGED); } if (user_sid) { DOM_SID u_sid; if (!string_to_sid(&u_sid, user_sid)) { /* not a complete sid, may be a RID, try building a SID */ int u_rid; if (sscanf(user_sid, "%d", &u_rid) != 1) { fprintf(stderr, "Error passed string is not a complete user SID or RID!\n"); return -1; } sid_copy(&u_sid, get_global_sam_sid()); sid_append_rid(&u_sid, u_rid); } pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED); } if (group_sid) { DOM_SID g_sid; if (!string_to_sid(&g_sid, group_sid)) { /* not a complete sid, may be a RID, try building a SID */ int g_rid; if (sscanf(group_sid, "%d", &g_rid) != 1) { fprintf(stderr, "Error passed string is not a complete group SID or RID!\n"); return -1; } sid_copy(&g_sid, get_global_sam_sid()); sid_append_rid(&g_sid, g_rid); } pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED); } if (badpw) { pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED); pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED); } if (NT_STATUS_IS_OK(in->pdb_update_sam_account (in, sam_pwent))) print_user_info (in, username, True, False); else { fprintf (stderr, "Unable to modify entry!\n"); pdb_free_sam(&sam_pwent); return -1; } pdb_free_sam(&sam_pwent); return 0;}/********************************************************* Add New User**********************************************************/static int new_user (struct pdb_context *in, const char *username, const char *fullname, const char *homedir, const char *drive, const char *script, const char *profile, char *user_sid, char *group_sid, BOOL stdin_get){ SAM_ACCOUNT *sam_pwent=NULL; char *password1, *password2; int rc_pwd_cmp; get_global_sam_sid(); if (!NT_STATUS_IS_OK(pdb_init_sam_new(&sam_pwent, username, 0))) { DEBUG(0, ("could not create account to add new user %s\n", username)); return -1; } password1 = get_pass( "new password:", stdin_get); password2 = get_pass( "retype new password:", stdin_get); if ((rc_pwd_cmp = strcmp (password1, password2))) { fprintf (stderr, "Passwords do not match!\n"); pdb_free_sam (&sam_pwent); } else { pdb_set_plaintext_passwd(sam_pwent, password1); } memset(password1, 0, strlen(password1)); SAFE_FREE(password1); memset(password2, 0, strlen(password2)); SAFE_FREE(password2); /* pwds do _not_ match? */ if (rc_pwd_cmp) return -1; if (fullname) pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED); if (homedir) pdb_set_homedir (sam_pwent, homedir, PDB_CHANGED); if (drive) pdb_set_dir_drive (sam_pwent, drive, PDB_CHANGED); if (script) pdb_set_logon_script(sam_pwent, script, PDB_CHANGED); if (profile) pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED); if (user_sid) { DOM_SID u_sid; if (!string_to_sid(&u_sid, user_sid)) { /* not a complete sid, may be a RID, try building a SID */ int u_rid; if (sscanf(user_sid, "%d", &u_rid) != 1) { fprintf(stderr, "Error passed string is not a complete user SID or RID!\n"); return -1; } sid_copy(&u_sid, get_global_sam_sid()); sid_append_rid(&u_sid, u_rid); } pdb_set_user_sid (sam_pwent, &u_sid, PDB_CHANGED); } if (group_sid) { DOM_SID g_sid; if (!string_to_sid(&g_sid, group_sid)) { /* not a complete sid, may be a RID, try building a SID */ int g_rid; if (sscanf(group_sid, "%d", &g_rid) != 1) { fprintf(stderr, "Error passed string is not a complete group SID or RID!\n"); return -1; } sid_copy(&g_sid, get_global_sam_sid()); sid_append_rid(&g_sid, g_rid); } pdb_set_group_sid (sam_pwent, &g_sid, PDB_CHANGED); } pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL, PDB_CHANGED); if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) { print_user_info (in, username, True, False); } else { fprintf (stderr, "Unable to add user! (does it already exist?)\n"); pdb_free_sam (&sam_pwent); return -1; } pdb_free_sam (&sam_pwent); return 0;}/********************************************************* Add New Machine**********************************************************/static int new_machine (struct pdb_context *in, const char *machine_in){ SAM_ACCOUNT *sam_pwent=NULL; fstring machinename; fstring machineaccount; struct passwd *pwd = NULL; get_global_sam_sid(); fstrcpy(machinename, machine_in); machinename[15]= '\0'; if (machinename[strlen (machinename) -1] == '$') machinename[strlen (machinename) -1] = '\0'; strlower_m(machinename); fstrcpy(machineaccount, machinename); fstrcat(machineaccount, "$"); if ((pwd = getpwnam_alloc(machineaccount))) { if (!NT_STATUS_IS_OK(pdb_init_sam_pw( &sam_pwent, pwd))) { fprintf(stderr, "Could not init sam from pw\n"); passwd_free(&pwd); return -1; } passwd_free(&pwd); } else { if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) { fprintf(stderr, "Could not init sam from pw\n"); return -1; } } pdb_set_plaintext_passwd (sam_pwent, machinename); pdb_set_username (sam_pwent, machineaccount, PDB_CHANGED); pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST, PDB_CHANGED); pdb_set_group_sid_from_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS, PDB_CHANGED); if (NT_STATUS_IS_OK(in->pdb_add_sam_account (in, sam_pwent))) { print_user_info (in, machineaccount, True, False); } else { fprintf (stderr, "Unable to add machine! (does it already exist?)\n"); pdb_free_sam (&sam_pwent); return -1; } pdb_free_sam (&sam_pwent); return 0;}/********************************************************* Delete user entry**********************************************************/static int delete_user_entry (struct pdb_context *in, const char *username){ SAM_ACCOUNT *samaccount = NULL; if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) { return -1; } if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, username))) { fprintf (stderr, "user %s does not exist in the passdb\n", username); return -1; } if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) { fprintf (stderr, "Unable to delete user %s\n", username); return -1; } return 0;}/********************************************************* Delete machine entry**********************************************************/static int delete_machine_entry (struct pdb_context *in, const char *machinename){ fstring name; SAM_ACCOUNT *samaccount = NULL; fstrcpy(name, machinename); name[15] = '\0'; if (name[strlen(name)-1] != '$') fstrcat (name, "$"); if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) { return -1; } if (!NT_STATUS_IS_OK(in->pdb_getsampwnam(in, samaccount, name))) { fprintf (stderr, "machine %s does not exist in the passdb\n", name); return -1; } if (!NT_STATUS_IS_OK(in->pdb_delete_sam_account (in, samaccount))) { fprintf (stderr, "Unable to delete machine %s\n", name); return -1; } return 0;}/********************************************************* Start here.**********************************************************/int main (int argc, char **argv){ static BOOL list_users = False; static BOOL verbose = False; static BOOL spstyle = False; static BOOL machine = False; static BOOL add_user = False; static BOOL delete_user = False; static BOOL modify_user = False; uint32 setparms, checkparms; int opt; static char *full_name = NULL; static char *acct_desc = NULL; static const char *user_name = NULL; static char *home_dir = NULL; static char *home_drive = NULL; static char *backend = NULL; static char *backend_in = NULL; static char *backend_out = NULL; static BOOL transfer_groups = False; static BOOL transfer_account_policies = False; static BOOL reset_account_policies = False; static BOOL force_initialised_password = False;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -