static NTSTATUS pdb_samba_dsdb_init_sam_from_priv(struct pdb_methods *m,
					   struct samu *sam,
					   struct ldb_message *msg)
{
	struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
		m->private_data, struct pdb_samba_dsdb_state);
	TALLOC_CTX *frame = talloc_stackframe();
	NTSTATUS status = NT_STATUS_INTERNAL_DB_CORRUPTION;
	const char *str;
	time_t tmp_time;
	struct dom_sid *sid, group_sid;
	uint64_t n;
	const DATA_BLOB *blob;

	str = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
	if (str == NULL) {
		DEBUG(10, ("no samAccountName\n"));
		goto fail;
	}
	pdb_set_username(sam, str, PDB_SET);

	if (pdb_samba_dsdb_pull_time(msg, "lastLogon", &tmp_time)) {
		pdb_set_logon_time(sam, tmp_time, PDB_SET);
	}
	if (pdb_samba_dsdb_pull_time(msg, "lastLogoff", &tmp_time)) {
		pdb_set_logoff_time(sam, tmp_time, PDB_SET);
	}
	if (pdb_samba_dsdb_pull_time(msg, "pwdLastSet", &tmp_time)) {
		pdb_set_pass_last_set_time(sam, tmp_time, PDB_SET);
	}
	if (pdb_samba_dsdb_pull_time(msg, "accountExpires", &tmp_time)) {
		pdb_set_kickoff_time(sam, tmp_time, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "displayName",
					    NULL);
	if (str != NULL) {
		pdb_set_fullname(sam, str, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "homeDirectory",
					    NULL);
	if (str != NULL) {
		pdb_set_homedir(sam, str, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "homeDrive", NULL);
	if (str != NULL) {
		pdb_set_dir_drive(sam, str, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "scriptPath", NULL);
	if (str != NULL) {
		pdb_set_logon_script(sam, str, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "profilePath",
					    NULL);
	if (str != NULL) {
		pdb_set_profile_path(sam, str, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "comment",
					    NULL);
	if (str != NULL) {
		pdb_set_comment(sam, str, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "description",
					    NULL);
	if (str != NULL) {
		pdb_set_acct_desc(sam, str, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "userWorkstations",
					    NULL);
	if (str != NULL) {
		pdb_set_workstations(sam, str, PDB_SET);
	}

	blob = ldb_msg_find_ldb_val(msg, "userParameters");
	if (blob != NULL) {
		str = base64_encode_data_blob(frame, *blob);
		if (str == NULL) {
			DEBUG(0, ("base64_encode_data_blob() failed\n"));
			goto fail;
		}
		pdb_set_munged_dial(sam, str, PDB_SET);
	}

	sid = samdb_result_dom_sid(talloc_tos(), msg, "objectSid");
	if (!sid) {
		DEBUG(10, ("Could not pull SID\n"));
		goto fail;
	}
	pdb_set_user_sid(sam, sid, PDB_SET);

	n = samdb_result_acct_flags(msg, "msDS-User-Account-Control-Computed");
	if (n == 0) {
		DEBUG(10, ("Could not pull userAccountControl\n"));
		goto fail;
	}
	pdb_set_acct_ctrl(sam, n, PDB_SET);

	blob = ldb_msg_find_ldb_val(msg, "unicodePwd");
	if (blob) {
		if (blob->length != NT_HASH_LEN) {
			DEBUG(0, ("Got NT hash of length %d, expected %d\n",
				  (int)blob->length, NT_HASH_LEN));
			goto fail;
		}
		pdb_set_nt_passwd(sam, blob->data, PDB_SET);
	}

	blob = ldb_msg_find_ldb_val(msg, "dBCSPwd");
	if (blob) {
		if (blob->length != LM_HASH_LEN) {
			DEBUG(0, ("Got LM hash of length %d, expected %d\n",
				  (int)blob->length, LM_HASH_LEN));
			goto fail;
		}
		pdb_set_lanman_passwd(sam, blob->data, PDB_SET);
	}

	n = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", 0);
	if (n == 0) {
		DEBUG(10, ("Could not pull primaryGroupID\n"));
		goto fail;
	}
	sid_compose(&group_sid, samdb_domain_sid(state->ldb), n);
	pdb_set_group_sid(sam, &group_sid, PDB_SET);

	status = NT_STATUS_OK;
fail:
	TALLOC_FREE(frame);
	return status;
}
static NTSTATUS pdb_samba_dsdb_init_sam_from_priv(struct pdb_methods *m,
					   struct samu *sam,
					   struct ldb_message *msg)
{
	struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
		m->private_data, struct pdb_samba_dsdb_state);
	TALLOC_CTX *frame = talloc_stackframe();
	NTSTATUS status = NT_STATUS_INTERNAL_DB_CORRUPTION;
	const char *str;
	time_t tmp_time;
	struct dom_sid *sid, group_sid;
	uint64_t n;
	const DATA_BLOB *blob;

	str = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
	if (str == NULL) {
		DEBUG(10, ("no samAccountName\n"));
		goto fail;
	}
	pdb_set_username(sam, str, PDB_SET);

	if (pdb_samba_dsdb_pull_time(msg, "lastLogon", &tmp_time)) {
		pdb_set_logon_time(sam, tmp_time, PDB_SET);
	}
	if (pdb_samba_dsdb_pull_time(msg, "lastLogoff", &tmp_time)) {
		pdb_set_logoff_time(sam, tmp_time, PDB_SET);
	}
	if (pdb_samba_dsdb_pull_time(msg, "pwdLastSet", &tmp_time)) {
		pdb_set_pass_last_set_time(sam, tmp_time, PDB_SET);
	}
	if (pdb_samba_dsdb_pull_time(msg, "accountExpires", &tmp_time)) {
		pdb_set_kickoff_time(sam, tmp_time, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "displayName",
					    NULL);
	if (str != NULL) {
		pdb_set_fullname(sam, str, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "homeDirectory",
					    NULL);
	if (str != NULL) {
		pdb_set_homedir(sam, str, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "homeDrive", NULL);
	if (str != NULL) {
		pdb_set_dir_drive(sam, str, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "scriptPath", NULL);
	if (str != NULL) {
		pdb_set_logon_script(sam, str, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "profilePath",
					    NULL);
	if (str != NULL) {
		pdb_set_profile_path(sam, str, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "comment",
					    NULL);
	if (str != NULL) {
		pdb_set_comment(sam, str, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "description",
					    NULL);
	if (str != NULL) {
		pdb_set_acct_desc(sam, str, PDB_SET);
	}

	str = ldb_msg_find_attr_as_string(msg, "userWorkstations",
					    NULL);
	if (str != NULL) {
		pdb_set_workstations(sam, str, PDB_SET);
	}

	blob = ldb_msg_find_ldb_val(msg, "userParameters");
	if (blob != NULL) {
		str = base64_encode_data_blob(frame, *blob);
		if (str == NULL) {
			DEBUG(0, ("base64_encode_data_blob() failed\n"));
			goto fail;
		}
		pdb_set_munged_dial(sam, str, PDB_SET);
	}

	sid = samdb_result_dom_sid(talloc_tos(), msg, "objectSid");
	if (!sid) {
		DEBUG(10, ("Could not pull SID\n"));
		goto fail;
	}
	pdb_set_user_sid(sam, sid, PDB_SET);

	n = samdb_result_acct_flags(msg, "msDS-User-Account-Control-Computed");
	if (n == 0) {
		DEBUG(10, ("Could not pull userAccountControl\n"));
		goto fail;
	}
	pdb_set_acct_ctrl(sam, n, PDB_SET);

	blob = ldb_msg_find_ldb_val(msg, "unicodePwd");
	if (blob) {
		if (blob->length != NT_HASH_LEN) {
			DEBUG(0, ("Got NT hash of length %d, expected %d\n",
				  (int)blob->length, NT_HASH_LEN));
			goto fail;
		}
		pdb_set_nt_passwd(sam, blob->data, PDB_SET);
	}

	blob = ldb_msg_find_ldb_val(msg, "dBCSPwd");
	if (blob) {
		if (blob->length != LM_HASH_LEN) {
			DEBUG(0, ("Got LM hash of length %d, expected %d\n",
				  (int)blob->length, LM_HASH_LEN));
			goto fail;
		}
		pdb_set_lanman_passwd(sam, blob->data, PDB_SET);
	}

	n = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", 0);
	if (n == 0) {
		DEBUG(10, ("Could not pull primaryGroupID\n"));
		goto fail;
	}
	sid_compose(&group_sid, samdb_domain_sid(state->ldb), n);
	pdb_set_group_sid(sam, &group_sid, PDB_SET);

	status = NT_STATUS_OK;
fail:
	TALLOC_FREE(frame);
	return status;
}