[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[samba.git] / source / rpc_server / srv_pipe.c
index 23419d5c555d6c1109d0467796820e4458bda19a..72ce72fb28d6987d49c100bf15b4d05b6dc4e5c2 100644 (file)
@@ -5,7 +5,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,
@@ -14,8 +14,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 module apparently provides an implementation of DCE/RPC over a
@@ -71,7 +70,7 @@ static BOOL create_next_pdu_ntlmssp(pipes_struct *p)
         */
 
        if(p->fault_state) {
-               setup_fault_pdu(p, NT_STATUS(0x1c010002));
+               setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
                return True;
        }
 
@@ -284,7 +283,7 @@ static BOOL create_next_pdu_schannel(pipes_struct *p)
         */
 
        if(p->fault_state) {
-               setup_fault_pdu(p, NT_STATUS(0x1c010002));
+               setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
                return True;
        }
 
@@ -460,7 +459,7 @@ static BOOL create_next_pdu_noauth(pipes_struct *p)
         */
 
        if(p->fault_state) {
-               setup_fault_pdu(p, NT_STATUS(0x1c010002));
+               setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
                return True;
        }
 
@@ -606,7 +605,7 @@ static BOOL pipe_ntlmssp_verify_final(pipes_struct *p, DATA_BLOB *p_resp_blob)
        NTSTATUS status;
        AUTH_NTLMSSP_STATE *a = p->auth.a_u.auth_ntlmssp_state;
 
-       DEBUG(5,("pipe_ntlmssp_verify_final: checking user details\n"));
+       DEBUG(5,("pipe_ntlmssp_verify_final: pipe %s checking user details\n", p->name));
 
        ZERO_STRUCT(reply);
 
@@ -616,11 +615,14 @@ static BOOL pipe_ntlmssp_verify_final(pipes_struct *p, DATA_BLOB *p_resp_blob)
        memset(p->wks, '\0', sizeof(p->wks));
 
        /* Set up for non-authenticated user. */
-       talloc_free(p->pipe_user.nt_user_token);
+       TALLOC_FREE(p->pipe_user.nt_user_token);
        p->pipe_user.ut.ngroups = 0;
        SAFE_FREE( p->pipe_user.ut.groups);
 
+       /* this has to be done as root in order to verify the password */
+       become_root();
        status = auth_ntlmssp_update(a, *p_resp_blob, &reply);
+       unbecome_root();
 
        /* Don't generate a reply. */
        data_blob_free(&reply);
@@ -629,6 +631,27 @@ static BOOL pipe_ntlmssp_verify_final(pipes_struct *p, DATA_BLOB *p_resp_blob)
                return False;
        }
 
+       /* Finally - if the pipe negotiated integrity (sign) or privacy (seal)
+          ensure the underlying NTLMSSP flags are also set. If not we should
+          refuse the bind. */
+
+       if (p->auth.auth_level == PIPE_AUTH_LEVEL_INTEGRITY) {
+               if (!(a->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
+                       DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet integrity requested "
+                               "but client declined signing.\n",
+                                       p->name ));
+                       return False;
+               }
+       }
+       if (p->auth.auth_level == PIPE_AUTH_LEVEL_PRIVACY) {
+               if (!(a->ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
+                       DEBUG(0,("pipe_ntlmssp_verify_final: pipe %s : packet privacy requested "
+                               "but client declined sealing.\n",
+                                       p->name ));
+                       return False;
+               }
+       }
+       
        fstrcpy(p->user_name, a->ntlmssp_state->user);
        fstrcpy(p->pipe_user_name, a->server_info->unix_name);
        fstrcpy(p->domain, a->ntlmssp_state->domain);
@@ -656,7 +679,7 @@ static BOOL pipe_ntlmssp_verify_final(pipes_struct *p, DATA_BLOB *p_resp_blob)
 
        p->pipe_user.ut.ngroups = a->server_info->n_groups;
        if (p->pipe_user.ut.ngroups) {
-               if (!(p->pipe_user.ut.groups = memdup(a->server_info->groups,
+               if (!(p->pipe_user.ut.groups = (gid_t *)memdup(a->server_info->groups,
                                                sizeof(gid_t) * p->pipe_user.ut.ngroups))) {
                        DEBUG(0,("failed to memdup group list to p->pipe_user.groups\n"));
                        return False;
@@ -966,9 +989,9 @@ BOOL check_bind_req(struct pipes_struct *p, RPC_IFACE* abstract,
                DEBUG(10,("checking %s\n", pipe_names[i].client_pipe));
                if ( strequal(pipe_names[i].client_pipe, pname)
                        && (abstract->version == pipe_names[i].abstr_syntax.version) 
-                       && (memcmp(&abstract->uuid, &pipe_names[i].abstr_syntax.uuid, sizeof(struct uuid)) == 0)
+                       && (memcmp(&abstract->uuid, &pipe_names[i].abstr_syntax.uuid, sizeof(struct GUID)) == 0)
                        && (transfer->version == pipe_names[i].trans_syntax.version)
-                       && (memcmp(&transfer->uuid, &pipe_names[i].trans_syntax.uuid, sizeof(struct uuid)) == 0) ) {
+                       && (memcmp(&transfer->uuid, &pipe_names[i].trans_syntax.uuid, sizeof(struct GUID)) == 0) ) {
                        struct api_struct       *fns = NULL;
                        int                     n_fns = 0;
                        PIPE_RPC_FNS            *context_fns;
@@ -1032,7 +1055,7 @@ NTSTATUS rpc_pipe_register_commands(int version, const char *clnt, const char *s
            rpc_lookup will still be valid afterwards.  It could then succeed if
            called again later */
        rpc_lookup_size++;
-        rpc_entry = SMB_REALLOC_ARRAY(rpc_lookup, struct rpc_table, rpc_lookup_size);
+        rpc_entry = SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(rpc_lookup, struct rpc_table, rpc_lookup_size);
         if (NULL == rpc_entry) {
                 rpc_lookup_size--;
                 DEBUG(0, ("rpc_pipe_register_commands: memory allocation failed\n"));
@@ -1046,6 +1069,9 @@ NTSTATUS rpc_pipe_register_commands(int version, const char *clnt, const char *s
         rpc_entry->pipe.clnt = SMB_STRDUP(clnt);
         rpc_entry->pipe.srv = SMB_STRDUP(srv);
         rpc_entry->cmds = SMB_REALLOC_ARRAY(rpc_entry->cmds, struct api_struct, rpc_entry->n_cmds + size);
+       if (!rpc_entry->cmds) {
+               return NT_STATUS_NO_MEMORY;
+       }
         memcpy(rpc_entry->cmds + rpc_entry->n_cmds, cmds, size * sizeof(struct api_struct));
         rpc_entry->n_cmds += size;
         
@@ -1292,8 +1318,14 @@ static BOOL pipe_schannel_auth_bind(pipes_struct *p, prs_struct *rpc_in_p,
                return False;
        }
 
+       /*
+        * The neg.myname key here must match the remote computer name
+        * given in the DOM_CLNT_SRV.uni_comp_name used on all netlogon pipe
+        * operations that use credentials.
+        */
+
        become_root();
-       ret = secrets_restore_schannel_session_info(p->mem_ctx, get_remote_machine_name(), &pdcinfo);
+       ret = secrets_restore_schannel_session_info(p->mem_ctx, neg.myname, &pdcinfo);
        unbecome_root();
 
        if (!ret) {
@@ -1303,7 +1335,7 @@ static BOOL pipe_schannel_auth_bind(pipes_struct *p, prs_struct *rpc_in_p,
 
        p->auth.a_u.schannel_auth = TALLOC_P(p->pipe_state_mem_ctx, struct schannel_auth_struct);
        if (!p->auth.a_u.schannel_auth) {
-               talloc_free(pdcinfo);
+               TALLOC_FREE(pdcinfo);
                return False;
        }
 
@@ -1311,7 +1343,7 @@ static BOOL pipe_schannel_auth_bind(pipes_struct *p, prs_struct *rpc_in_p,
        memcpy(p->auth.a_u.schannel_auth->sess_key, pdcinfo->sess_key,
                        sizeof(pdcinfo->sess_key));
 
-       talloc_free(pdcinfo);
+       TALLOC_FREE(pdcinfo);
 
        p->auth.a_u.schannel_auth->seq_num = 0;
 
@@ -1599,11 +1631,18 @@ BOOL api_pipe_bind_req(pipes_struct *p, prs_struct *rpc_in_p)
 
                case RPC_ANONYMOUS_AUTH_TYPE:
                        /* Unauthenticated bind request. */
+                       /* Get the authenticated pipe user from current_user */
+                       if (!copy_current_user(&p->pipe_user, &current_user)) {
+                               DEBUG(10, ("Could not copy current user\n"));
+                               goto err_exit;
+                       }
                        /* We're finished - no more packets. */
                        p->auth.auth_type = PIPE_AUTH_TYPE_NONE;
                        /* We must set the pipe auth_level here also. */
                        p->auth.auth_level = PIPE_AUTH_LEVEL_NONE;
                        p->pipe_bound = True;
+                       /* The session key was initialized from the SMB
+                        * session in make_internal_rpc_pipe_p */
                        break;
 
                default:
@@ -2194,9 +2233,9 @@ BOOL api_pipe_request(pipes_struct *p)
        pipe_fns = find_pipe_fns_by_context(p->contexts, p->hdr_req.context_id);
        
        if ( pipe_fns ) {
-               set_current_rpc_talloc(p->mem_ctx);
+               TALLOC_CTX *frame = talloc_stackframe();
                ret = api_rpcTNP(p, p->name, pipe_fns->cmds, pipe_fns->n_cmds);
-               set_current_rpc_talloc(NULL);   
+               TALLOC_FREE(frame);
        }
        else {
                DEBUG(0,("api_pipe_request: No rpc function table associated with context [%d] on pipe [%s]\n",
@@ -2241,7 +2280,7 @@ BOOL api_rpcTNP(pipes_struct *p, const char *rpc_name,
                 * and not put the pipe into fault state. JRA.
                 */
                DEBUG(4, ("unknown\n"));
-               setup_fault_pdu(p, NT_STATUS(0x1c010002));
+               setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
                return True;
        }
 
@@ -2259,7 +2298,14 @@ BOOL api_rpcTNP(pipes_struct *p, const char *rpc_name,
        if (p->bad_handle_fault_state) {
                DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
                p->bad_handle_fault_state = False;
-               setup_fault_pdu(p, NT_STATUS(0x1C00001A));
+               setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_CONTEXT_MISMATCH));
+               return True;
+       }
+
+       if (p->rng_fault_state) {
+               DEBUG(4, ("api_rpcTNP: rng fault return\n"));
+               p->rng_fault_state = False;
+               setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
                return True;
        }
 
@@ -2276,7 +2322,7 @@ BOOL api_rpcTNP(pipes_struct *p, const char *rpc_name,
        if ((DEBUGLEVEL >= 10) && 
            (prs_offset(&p->in_data.data) != prs_data_size(&p->in_data.data))) {
                size_t data_len = prs_data_size(&p->in_data.data) - prs_offset(&p->in_data.data);
-               char *data = SMB_MALLOC(data_len);
+               char *data = (char *)SMB_MALLOC(data_len);
 
                DEBUG(10, ("api_rpcTNP: rpc input buffer underflow (parse error?)\n"));
                if (data) {
@@ -2311,13 +2357,13 @@ void get_pipe_fns( int idx, struct api_struct **fns, int *n_fns )
                        netlog_get_pipe_fns( &cmds, &n_cmds );
                        break;
                case PI_SRVSVC:
-                       srvsvc_get_pipe_fns( &cmds, &n_cmds );
+                       srvsvc2_get_pipe_fns( &cmds, &n_cmds );
                        break;
                case PI_WKSSVC:
                        wkssvc_get_pipe_fns( &cmds, &n_cmds );
                        break;
                case PI_WINREG:
-                       reg_get_pipe_fns( &cmds, &n_cmds );
+                       winreg_get_pipe_fns( &cmds, &n_cmds );
                        break;
                case PI_SPOOLSS:
                        spoolss_get_pipe_fns( &cmds, &n_cmds );
@@ -2326,17 +2372,17 @@ void get_pipe_fns( int idx, struct api_struct **fns, int *n_fns )
                        netdfs_get_pipe_fns( &cmds, &n_cmds );
                        break;
                case PI_SVCCTL:
-                       svcctl_get_pipe_fns( &cmds, &n_cmds );
+                       svcctl2_get_pipe_fns( &cmds, &n_cmds );
                        break;
                case PI_EVENTLOG:
-                       eventlog_get_pipe_fns( &cmds, &n_cmds );
+                       eventlog2_get_pipe_fns( &cmds, &n_cmds );
                        break;
                case PI_NTSVCS:
                        ntsvcs_get_pipe_fns( &cmds, &n_cmds );
                        break;
 #ifdef DEVELOPER
-               case PI_ECHO:
-                       echo_get_pipe_fns( &cmds, &n_cmds );
+               case PI_RPCECHO:
+                       rpcecho_get_pipe_fns( &cmds, &n_cmds );
                        break;
 #endif
                default: