[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[samba.git] / source / rpc_server / srv_lsa_nt.c
index bde1ef81da4e6e57692dbe872f53a5964b527634..c513d8489cb2632d2a68ad1e23bae7d13494c4e6 100644 (file)
@@ -13,7 +13,7 @@
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation; either version 3 of the License, or
  *  (at your option) any later version.
  *  
  *  This program is distributed in the hope that it will be useful,
@@ -22,8 +22,7 @@
  *  GNU General Public License for more details.
  *  
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /* This is the implementation of the lsa server code. */
@@ -40,7 +39,7 @@ struct lsa_info {
        uint32 access;
 };
 
-struct generic_mapping lsa_generic_mapping = {
+const struct generic_mapping lsa_generic_mapping = {
        POLICY_READ,
        POLICY_WRITE,
        POLICY_EXECUTE,
@@ -380,12 +379,10 @@ static void init_reply_lookup_names4(LSA_R_LOOKUP_NAMES4 *r_l,
 
 static void init_reply_lookup_sids2(LSA_R_LOOKUP_SIDS2 *r_l,
                                DOM_R_REF *ref,
-                               LSA_TRANS_NAME_ENUM2 *names,
                                uint32 mapped_count)
 {
        r_l->ptr_dom_ref  = ref ? 1 : 0;
        r_l->dom_ref      = ref;
-       r_l->names        = names;
        r_l->mapped_count = mapped_count;
 }
 
@@ -395,12 +392,10 @@ static void init_reply_lookup_sids2(LSA_R_LOOKUP_SIDS2 *r_l,
 
 static void init_reply_lookup_sids3(LSA_R_LOOKUP_SIDS3 *r_l,
                                DOM_R_REF *ref,
-                               LSA_TRANS_NAME_ENUM2 *names,
                                uint32 mapped_count)
 {
        r_l->ptr_dom_ref  = ref ? 1 : 0;
        r_l->dom_ref      = ref;
-       r_l->names        = names;
        r_l->mapped_count = mapped_count;
 }
 
@@ -414,11 +409,7 @@ static NTSTATUS init_reply_lookup_sids(TALLOC_CTX *mem_ctx,
                                LSA_TRANS_NAME_ENUM2 *names,
                                uint32 mapped_count)
 {
-       LSA_TRANS_NAME_ENUM *oldnames = TALLOC_ZERO_P(mem_ctx, LSA_TRANS_NAME_ENUM);
-
-       if (!oldnames) {
-               return NT_STATUS_NO_MEMORY;
-       }
+       LSA_TRANS_NAME_ENUM *oldnames = &r_l->names;
 
        oldnames->num_entries = names->num_entries;
        oldnames->ptr_trans_names = names->ptr_trans_names;
@@ -428,7 +419,7 @@ static NTSTATUS init_reply_lookup_sids(TALLOC_CTX *mem_ctx,
        if (names->num_entries) {
                int i;
 
-               oldnames->name = TALLOC_ARRAY(oldnames, LSA_TRANS_NAME, names->num_entries);
+               oldnames->name = TALLOC_ARRAY(mem_ctx, LSA_TRANS_NAME, names->num_entries);
 
                if (!oldnames->name) {
                        return NT_STATUS_NO_MEMORY;
@@ -442,7 +433,6 @@ static NTSTATUS init_reply_lookup_sids(TALLOC_CTX *mem_ctx,
 
        r_l->ptr_dom_ref  = ref ? 1 : 0;
        r_l->dom_ref      = ref;
-       r_l->names        = oldnames;
        r_l->mapped_count = mapped_count;
        return NT_STATUS_OK;
 }
@@ -810,13 +800,12 @@ static NTSTATUS _lsa_lookup_sids_internal(pipes_struct *p,
                                int num_sids,                           /* input */
                                const DOM_SID2 *sid,                    /* input */
                                DOM_R_REF **pp_ref,                     /* output */
-                               LSA_TRANS_NAME_ENUM2 **pp_names,        /* output */
+                               LSA_TRANS_NAME_ENUM2 *names,            /* input/output */
                                uint32 *pp_mapped_count)
 {
        NTSTATUS status;
        int i;
        const DOM_SID **sids = NULL;
-       LSA_TRANS_NAME_ENUM2 *names = NULL;
        DOM_R_REF *ref = NULL;
        uint32 mapped_count = 0;
        struct lsa_dom_info *dom_infos = NULL;
@@ -824,17 +813,16 @@ static NTSTATUS _lsa_lookup_sids_internal(pipes_struct *p,
 
        *pp_mapped_count = 0;
        *pp_ref = NULL;
-       *pp_names = NULL;
+       ZERO_STRUCTP(names);
 
        if (num_sids == 0) {
                return NT_STATUS_OK;
        }
 
-       names = TALLOC_ZERO_P(p->mem_ctx, LSA_TRANS_NAME_ENUM2);
        sids = TALLOC_ARRAY(p->mem_ctx, const DOM_SID *, num_sids);
        ref = TALLOC_ZERO_P(p->mem_ctx, DOM_R_REF);
 
-       if (sids == NULL || names == NULL || ref == NULL) {
+       if (sids == NULL || ref == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -849,8 +837,8 @@ static NTSTATUS _lsa_lookup_sids_internal(pipes_struct *p,
                return status;
        }
 
-       names->name = TALLOC_ARRAY(names, LSA_TRANS_NAME2, num_sids);
-       names->uni_name = TALLOC_ARRAY(names, UNISTR2, num_sids);
+       names->name = TALLOC_ARRAY(p->mem_ctx, LSA_TRANS_NAME2, num_sids);
+       names->uni_name = TALLOC_ARRAY(p->mem_ctx, UNISTR2, num_sids);
        if ((names->name == NULL) || (names->uni_name == NULL)) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -907,7 +895,6 @@ static NTSTATUS _lsa_lookup_sids_internal(pipes_struct *p,
 
        *pp_mapped_count = mapped_count;
        *pp_ref = ref;
-       *pp_names = names;
 
        return status;
 }
@@ -924,7 +911,7 @@ NTSTATUS _lsa_lookup_sids(pipes_struct *p,
        int num_sids = q_u->sids.num_entries;
        uint32 mapped_count = 0;
        DOM_R_REF *ref = NULL;
-       LSA_TRANS_NAME_ENUM2 *names = NULL;
+       LSA_TRANS_NAME_ENUM2 names;
        NTSTATUS status;
 
        if ((q_u->level < 1) || (q_u->level > 6)) {
@@ -956,7 +943,7 @@ NTSTATUS _lsa_lookup_sids(pipes_struct *p,
 
        /* Convert from LSA_TRANS_NAME_ENUM2 to LSA_TRANS_NAME_ENUM */
 
-       status = init_reply_lookup_sids(p->mem_ctx, r_u, ref, names, mapped_count);
+       status = init_reply_lookup_sids(p->mem_ctx, r_u, ref, &names, mapped_count);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -975,7 +962,6 @@ NTSTATUS _lsa_lookup_sids2(pipes_struct *p,
        int num_sids = q_u->sids.num_entries;
        uint32 mapped_count = 0;
        DOM_R_REF *ref = NULL;
-       LSA_TRANS_NAME_ENUM2 *names = NULL;
 
        if ((q_u->level < 1) || (q_u->level > 6)) {
                return NT_STATUS_INVALID_PARAMETER;
@@ -1001,45 +987,51 @@ NTSTATUS _lsa_lookup_sids2(pipes_struct *p,
                                                num_sids, 
                                                q_u->sids.sid,
                                                &ref,
-                                               &names,
+                                               &r_u->names,
                                                &mapped_count);
 
-       init_reply_lookup_sids2(r_u, ref, names, mapped_count);
+       init_reply_lookup_sids2(r_u, ref, mapped_count);
        return r_u->status;
 }
 
 /***************************************************************************
  _lsa_lookup_sida3
-
- Before someone actually re-activates this, please present a sniff showing
- this call against some Windows server. I (vl) could not make it work against
- w2k3 at all.
  ***************************************************************************/
 
 NTSTATUS _lsa_lookup_sids3(pipes_struct *p,
                          LSA_Q_LOOKUP_SIDS3 *q_u,
                          LSA_R_LOOKUP_SIDS3 *r_u)
 {
+       int num_sids = q_u->sids.num_entries;
        uint32 mapped_count = 0;
-       DOM_R_REF *ref;
-       LSA_TRANS_NAME_ENUM2 *names;
+       DOM_R_REF *ref = NULL;
 
        if ((q_u->level < 1) || (q_u->level > 6)) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       r_u->status = NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED;
-
-       ref = TALLOC_ZERO_P(p->mem_ctx, DOM_R_REF);
-       names = TALLOC_ZERO_P(p->mem_ctx, LSA_TRANS_NAME_ENUM2);
+       /* No policy handle on this call. Restrict to crypto connections. */
+       if (p->auth.auth_type != PIPE_AUTH_TYPE_SCHANNEL) {
+               DEBUG(0,("_lsa_lookup_sids3: client %s not using schannel for netlogon\n",
+                       get_remote_machine_name() ));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
-       if ((ref == NULL) || (names == NULL)) {
-               /* We would segfault later on in lsa_io_r_lookup_sids3 anyway,
-                * so do a planned exit here. We NEEEED pidl! */
-               smb_panic("talloc failed");
+       if (num_sids >  MAX_LOOKUP_SIDS) {
+               DEBUG(5,("_lsa_lookup_sids3: limit of %d exceeded, requested %d\n",
+                        MAX_LOOKUP_SIDS, num_sids));
+               return NT_STATUS_NONE_MAPPED;
        }
 
-       init_reply_lookup_sids3(r_u, ref, names, mapped_count);
+       r_u->status = _lsa_lookup_sids_internal(p,
+                                               q_u->level,
+                                               num_sids, 
+                                               q_u->sids.sid,
+                                               &ref,
+                                               &r_u->names,
+                                               &mapped_count);
+
+       init_reply_lookup_sids3(r_u, ref, mapped_count);
        return r_u->status;
 }
 
@@ -1533,16 +1525,16 @@ NTSTATUS _lsa_enum_accounts(pipes_struct *p, LSA_Q_ENUM_ACCOUNTS *q_u, LSA_R_ENU
                        SAFE_FREE(sid_list);
                        return NT_STATUS_NO_MEMORY;
                }
+
+               for (i = q_u->enum_context, j = 0; i < num_entries; i++, j++) {
+                       init_dom_sid2(&(*sids).sid[j], &sid_list[i]);
+                       (*sids).ptr_sid[j] = 1;
+               }
        } else {
                sids->ptr_sid = NULL;
                sids->sid = NULL;
        }
 
-       for (i = q_u->enum_context, j = 0; i < num_entries; i++, j++) {
-               init_dom_sid2(&(*sids).sid[j], &sid_list[i]);
-               (*sids).ptr_sid[j] = 1;
-       }
-
        talloc_free(sid_list);
 
        init_lsa_r_enum_accounts(r_u, num_entries);
@@ -1553,26 +1545,14 @@ NTSTATUS _lsa_enum_accounts(pipes_struct *p, LSA_Q_ENUM_ACCOUNTS *q_u, LSA_R_ENU
 
 NTSTATUS _lsa_unk_get_connuser(pipes_struct *p, LSA_Q_UNK_GET_CONNUSER *q_u, LSA_R_UNK_GET_CONNUSER *r_u)
 {
-       const char *username, *domname;
+       fstring username, domname;
        user_struct *vuser = get_valid_user_struct(p->vuid);
   
        if (vuser == NULL)
                return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
-
-       if (vuser->guest) {
-               /*
-                * I'm 99% sure this is not the right place to do this,
-                * global_sid_Anonymous should probably be put into the token
-                * instead of the guest id -- vl
-                */
-               if (!lookup_sid(p->mem_ctx, &global_sid_Anonymous,
-                               &domname, &username, NULL)) {
-                       return NT_STATUS_NO_MEMORY;
-               }
-       } else {
-               username = vuser->user.smb_name;
-               domname = vuser->user.domain;
-       }
+  
+       fstrcpy(username, vuser->user.smb_name);
+       fstrcpy(domname, vuser->user.domain);
   
        r_u->ptr_user_name = 1;
        init_unistr2(&r_u->uni2_user_name, username, UNI_STR_TERMINATE);
@@ -1608,23 +1588,17 @@ NTSTATUS _lsa_create_account(pipes_struct *p, LSA_Q_CREATEACCOUNT *q_u, LSA_R_CR
         * I don't know if it's the right one. not documented.
         * but guessed with rpcclient.
         */
-       if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION)) {
-               DEBUG(10, ("_lsa_create_account: No POLICY_GET_PRIVATE_INFORMATION access right!\n"));
+       if (!(handle->access & POLICY_GET_PRIVATE_INFORMATION))
                return NT_STATUS_ACCESS_DENIED;
-       }
 
        /* check to see if the pipe_user is a Domain Admin since 
           account_pol.tdb was already opened as root, this is all we have */
           
-       if ( !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) ) {
-               DEBUG(10, ("_lsa_create_account: The use is not a Domain Admin, deny access!\n"));
+       if ( !nt_token_check_domain_rid( p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS ) )
                return NT_STATUS_ACCESS_DENIED;
-       }
                
-       if ( is_privileged_sid( &q_u->sid.sid ) ) {
-               DEBUG(10, ("_lsa_create_account: Policy account already exists!\n"));
+       if ( is_privileged_sid( &q_u->sid.sid ) )
                return NT_STATUS_OBJECT_NAME_COLLISION;
-       }
 
        /* associate the user/group SID with the (unique) handle. */
        
@@ -1639,7 +1613,6 @@ NTSTATUS _lsa_create_account(pipes_struct *p, LSA_Q_CREATEACCOUNT *q_u, LSA_R_CR
        if (!create_policy_hnd(p, &r_u->pol, free_lsa_info, (void *)info))
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
 
-       DEBUG(10, ("_lsa_create_account: call privileges code to create an account\n"));
        return privilege_create_account( &info->sid );
 }
 
@@ -1734,7 +1707,7 @@ NTSTATUS _lsa_getsystemaccount(pipes_struct *p, LSA_Q_GETSYSTEMACCOUNT *q_u, LSA
                return NT_STATUS_INVALID_HANDLE;
 
        if (!lookup_sid(p->mem_ctx, &info->sid, NULL, NULL, NULL))
-               return NT_STATUS_OK;
+               return NT_STATUS_ACCESS_DENIED;
 
        /*
          0x01 -> Log on locally