+ /*
+ * Get the sampass first as we need to check privilages
+ * based on what kind of user object this is.
+ * But don't reveal info too early if it didn't exist.
+ */
+
+ become_root();
+ ret=pdb_getsampwsid(sampass, &sid);
+ unbecome_root();
+
+ se_priv_copy(&se_rights, &se_priv_none);
+
+ /*
+ * We do the override access checks on *open*, not at
+ * SetUserInfo time.
+ */
+ if (ret) {
+ uint32_t acb_info = pdb_get_acct_ctrl(sampass);
+
+ if ((acb_info & ACB_WSTRUST) &&
+ user_has_any_privilege(p->server_info->ptok,
+ &se_machine_account)) {
+ /*
+ * SeMachineAccount is needed to add
+ * GENERIC_RIGHTS_USER_WRITE to a machine
+ * account.
+ */
+ se_priv_add(&se_rights, &se_machine_account);
+ DEBUG(10,("_samr_OpenUser: adding machine account "
+ "rights to handle for user %s\n",
+ pdb_get_username(sampass) ));
+ }
+ if ((acb_info & ACB_NORMAL) &&
+ user_has_any_privilege(p->server_info->ptok,
+ &se_add_users)) {
+ /*
+ * SeAddUsers is needed to add
+ * GENERIC_RIGHTS_USER_WRITE to a normal
+ * account.
+ */
+ se_priv_add(&se_rights, &se_add_users);
+ DEBUG(10,("_samr_OpenUser: adding add user "
+ "rights to handle for user %s\n",
+ pdb_get_username(sampass) ));
+ }
+ /*
+ * Cheat - allow GENERIC_RIGHTS_USER_WRITE if pipe user is
+ * in DOMAIN_GROUP_RID_ADMINS. This is almost certainly not
+ * what Windows does but is a hack for people who haven't
+ * set up privilages on groups in Samba.
+ */
+ if (acb_info & (ACB_SVRTRUST|ACB_DOMTRUST)) {
+ if (lp_enable_privileges() && nt_token_check_domain_rid(p->server_info->ptok,
+ DOMAIN_GROUP_RID_ADMINS)) {
+ des_access &= ~GENERIC_RIGHTS_USER_WRITE;
+ extra_access = GENERIC_RIGHTS_USER_WRITE;
+ DEBUG(4,("_samr_OpenUser: Allowing "
+ "GENERIC_RIGHTS_USER_WRITE for "
+ "rid admins\n"));
+ }
+ }
+ }
+
+ TALLOC_FREE(sampass);