r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[samba.git] / source / libsmb / smb_seal.c
index f0e79b404cf03f7d540366f21919e6d55406f7fb..33352b85ceb14b35c5ae3c61006acdc734a2b528 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,
    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/>.
 */
 
 #include "includes.h"
 
+/******************************************************************************
+ Pull out the encryption context for this packet. 0 means global context.
+******************************************************************************/
+
+NTSTATUS get_enc_ctx_num(char *buf, uint16 *p_enc_ctx_num)
+{
+       if (smb_len(buf) < 8) {
+               return NT_STATUS_INVALID_BUFFER_SIZE;
+       }
+
+       if (buf[4] == (char)0xFF) {
+               if (buf[5] == 'S' && buf [6] == 'M' && buf[7] == 'B') {
+                       /* Not an encrypted buffer. */
+                       return NT_STATUS_NOT_FOUND;
+               }
+               if (buf[5] == 'E') {
+                       *p_enc_ctx_num = SVAL(buf,6);
+                       return NT_STATUS_OK;
+               }
+       }
+       return NT_STATUS_INVALID_NETWORK_RESPONSE;
+}
+
 /******************************************************************************
  Generic code for client and server.
  Is encryption turned on ?
@@ -33,56 +55,71 @@ BOOL common_encryption_on(struct smb_trans_enc_state *es)
 /******************************************************************************
  Generic code for client and server.
  NTLM decrypt an incoming buffer.
+ Abartlett tells me that SSPI puts the signature first before the encrypted
+ output, so cope with the same for compatibility.
 ******************************************************************************/
 
 NTSTATUS common_ntlm_decrypt_buffer(NTLMSSP_STATE *ntlmssp_state, char *buf)
 {
        NTSTATUS status;
        size_t buf_len = smb_len(buf) + 4; /* Don't forget the 4 length bytes. */
+       size_t data_len;
+       char *inbuf;
        DATA_BLOB sig;
 
        if (buf_len < 8 + NTLMSSP_SIG_SIZE) {
                return NT_STATUS_BUFFER_TOO_SMALL;
        }
 
+       inbuf = (char *)smb_xmemdup(buf, buf_len);
+
        /* Adjust for the signature. */
-       buf_len -= NTLMSSP_SIG_SIZE;
+       data_len = buf_len - 8 - NTLMSSP_SIG_SIZE;
 
-       /* Save off the signature. */
-       sig = data_blob(buf+buf_len, NTLMSSP_SIG_SIZE);
+       /* Point at the signature. */
+       sig = data_blob_const(inbuf+8, NTLMSSP_SIG_SIZE);
 
        status = ntlmssp_unseal_packet(ntlmssp_state,
-               (unsigned char *)buf + 8, /* 4 byte len + 0xFF 'S' 'M' 'B' */
-               buf_len - 8,
-               (unsigned char *)buf + 8,
-               buf_len - 8,
+               (unsigned char *)inbuf + 8 + NTLMSSP_SIG_SIZE, /* 4 byte len + 0xFF 'E' <enc> <ctx> */
+               data_len,
+               (unsigned char *)inbuf + 8 + NTLMSSP_SIG_SIZE,
+               data_len,
                &sig);
 
        if (!NT_STATUS_IS_OK(status)) {
-               data_blob_free(&sig);
+               SAFE_FREE(inbuf);
                return status;
        }
 
+       memcpy(buf + 8, inbuf + 8 + NTLMSSP_SIG_SIZE, data_len);
+
        /* Reset the length. */
-       smb_setlen(buf, smb_len(buf) - NTLMSSP_SIG_SIZE);
+       smb_setlen(inbuf, buf, data_len + 4);
+
+       SAFE_FREE(inbuf);
        return NT_STATUS_OK;
 }
 
 /******************************************************************************
  Generic code for client and server.
  NTLM encrypt an outgoing buffer. Return the encrypted pointer in ppbuf_out.
+ Abartlett tells me that SSPI puts the signature first before the encrypted
+ output, so do the same for compatibility.
 ******************************************************************************/
 
-NTSTATUS common_ntlm_encrypt_buffer(NTLMSSP_STATE *ntlmssp_state, char *buf, char **ppbuf_out)
+NTSTATUS common_ntlm_encrypt_buffer(NTLMSSP_STATE *ntlmssp_state,
+                               uint16 enc_ctx_num,
+                               char *buf,
+                               char **ppbuf_out)
 {
        NTSTATUS status;
        char *buf_out;
-       size_t buf_len = smb_len(buf) + 4; /* Don't forget the 4 length bytes. */
+       size_t data_len = smb_len(buf) - 4; /* Ignore the 0xFF SMB bytes. */
        DATA_BLOB sig;
 
        *ppbuf_out = NULL;
 
-       if (buf_len < 8) {
+       if (data_len == 0) {
                return NT_STATUS_BUFFER_TOO_SMALL;
        }
 
@@ -91,21 +128,21 @@ NTSTATUS common_ntlm_encrypt_buffer(NTLMSSP_STATE *ntlmssp_state, char *buf, cha
         * check needed.
         */
 
-       /* Copy the original buffer. */
+       buf_out = SMB_XMALLOC_ARRAY(char, 8 + NTLMSSP_SIG_SIZE + data_len);
+
+       /* Copy the data from the original buffer. */
 
-       buf_out = SMB_XMALLOC_ARRAY(char, buf_len + NTLMSSP_SIG_SIZE);
-       memcpy(buf_out, buf, buf_len);
-       /* Last 16 bytes undefined here... */
+       memcpy(buf_out + 8 + NTLMSSP_SIG_SIZE, buf + 8, data_len);
 
-       smb_setlen(buf_out, smb_len(buf) + NTLMSSP_SIG_SIZE);
+       smb_set_enclen(buf_out, smb_len(buf) + NTLMSSP_SIG_SIZE, enc_ctx_num);
 
        sig = data_blob(NULL, NTLMSSP_SIG_SIZE);
 
        status = ntlmssp_seal_packet(ntlmssp_state,
-               (unsigned char *)buf_out + 8, /* 4 byte len + 0xFF 'S' 'M' 'B' */
-               buf_len - 8,
-               (unsigned char *)buf_out + 8,
-               buf_len - 8,
+               (unsigned char *)buf_out + 8 + NTLMSSP_SIG_SIZE, /* 4 byte len + 0xFF 'S' <enc> <ctx> */
+               data_len,
+               (unsigned char *)buf_out + 8 + NTLMSSP_SIG_SIZE,
+               data_len,
                &sig);
 
        if (!NT_STATUS_IS_OK(status)) {
@@ -114,7 +151,8 @@ NTSTATUS common_ntlm_encrypt_buffer(NTLMSSP_STATE *ntlmssp_state, char *buf, cha
                return status;
        }
 
-       memcpy(buf_out+buf_len, sig.data, NTLMSSP_SIG_SIZE);
+       /* First 16 data bytes are signature for SSPI compatibility. */
+       memcpy(buf_out + 8, sig.data, NTLMSSP_SIG_SIZE);
        *ppbuf_out = buf_out;
        return NT_STATUS_OK;
 }
@@ -126,7 +164,7 @@ NTSTATUS common_ntlm_encrypt_buffer(NTLMSSP_STATE *ntlmssp_state, char *buf, cha
 ******************************************************************************/
 
 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
- NTSTATUS common_gss_decrypt_buffer(struct smb_tran_enc_state_gss *gss_state, char *buf)
+static NTSTATUS common_gss_decrypt_buffer(struct smb_tran_enc_state_gss *gss_state, char *buf)
 {
        gss_ctx_id_t gss_ctx = gss_state->gss_ctx;
        OM_uint32 ret = 0;
@@ -153,8 +191,7 @@ NTSTATUS common_ntlm_encrypt_buffer(NTLMSSP_STATE *ntlmssp_state, char *buf, cha
                ADS_STATUS adss = ADS_ERROR_GSS(ret, minor);
                DEBUG(0,("common_gss_encrypt_buffer: gss_unwrap failed. Error %s\n",
                        ads_errstr(adss) ));
-               /* Um - no mapping for gss-errs to NTSTATUS yet. */
-               return ads_ntstatus(adss);
+               return map_nt_error_from_gss(ret, minor);
        }
 
        if (out_buf.length > in_buf.length) {
@@ -166,20 +203,21 @@ NTSTATUS common_ntlm_encrypt_buffer(NTLMSSP_STATE *ntlmssp_state, char *buf, cha
        }
 
        memcpy(buf + 8, out_buf.value, out_buf.length);
-       smb_setlen(buf, out_buf.length + 4);
+       smb_setlen((char *)out_buf.value, buf, out_buf.length + 4);
 
        gss_release_buffer(&minor, &out_buf);
        return NT_STATUS_OK;
 }
-#endif
 
 /******************************************************************************
  Generic code for client and server.
  gss-api encrypt an outgoing buffer. Return the alloced encrypted pointer in buf_out.
 ******************************************************************************/
 
-#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
- NTSTATUS common_gss_encrypt_buffer(struct smb_tran_enc_state_gss *gss_state, char *buf, char **ppbuf_out)
+static NTSTATUS common_gss_encrypt_buffer(struct smb_tran_enc_state_gss *gss_state,
+                                       uint16 enc_ctx_num,
+                                       char *buf,
+                                       char **ppbuf_out)
 {
        gss_ctx_id_t gss_ctx = gss_state->gss_ctx;
        OM_uint32 ret = 0;
@@ -209,8 +247,7 @@ NTSTATUS common_ntlm_encrypt_buffer(NTLMSSP_STATE *ntlmssp_state, char *buf, cha
                ADS_STATUS adss = ADS_ERROR_GSS(ret, minor);
                DEBUG(0,("common_gss_encrypt_buffer: gss_wrap failed. Error %s\n",
                        ads_errstr(adss) ));
-               /* Um - no mapping for gss-errs to NTSTATUS yet. */
-               return ads_ntstatus(adss);
+               return map_nt_error_from_gss(ret, minor);
        }
 
        if (!flags_got) {
@@ -231,14 +268,14 @@ NTSTATUS common_ntlm_encrypt_buffer(NTLMSSP_STATE *ntlmssp_state, char *buf, cha
         * bother :-*(. JRA.
         */
 
-       *ppbuf_out = SMB_MALLOC(out_buf.length + 8); /* We know this can't wrap. */
+       *ppbuf_out = (char *)SMB_MALLOC(out_buf.length + 8); /* We know this can't wrap. */
        if (!*ppbuf_out) {
                gss_release_buffer(&minor, &out_buf);
                return NT_STATUS_NO_MEMORY;
        }
 
        memcpy(*ppbuf_out+8, out_buf.value, out_buf.length);
-       smb_setlen(*ppbuf_out, out_buf.length + 4);
+       smb_set_enclen(*ppbuf_out, out_buf.length + 4, enc_ctx_num);
 
        gss_release_buffer(&minor, &out_buf);
        return NT_STATUS_OK;
@@ -258,18 +295,12 @@ NTSTATUS common_encrypt_buffer(struct smb_trans_enc_state *es, char *buffer, cha
                return NT_STATUS_OK;
        }
 
-       /* Ignore session keepalives. */
-       if(CVAL(buffer,0) == SMBkeepalive) {
-               *buf_out = buffer;
-               return NT_STATUS_OK;
-       }
-
        switch (es->smb_enc_type) {
                case SMB_TRANS_ENC_NTLM:
-                       return common_ntlm_encrypt_buffer(es->s.ntlmssp_state, buffer, buf_out);
+                       return common_ntlm_encrypt_buffer(es->s.ntlmssp_state, es->enc_ctx_num, buffer, buf_out);
 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
                case SMB_TRANS_ENC_GSS:
-                       return common_gss_encrypt_buffer(es->s.gss_state, buffer, buf_out);
+                       return common_gss_encrypt_buffer(es->s.gss_state, es->enc_ctx_num, buffer, buf_out);
 #endif
                default:
                        return NT_STATUS_NOT_SUPPORTED;
@@ -289,11 +320,6 @@ NTSTATUS common_decrypt_buffer(struct smb_trans_enc_state *es, char *buf)
                return NT_STATUS_OK;
        }
 
-       /* Ignore session keepalives. */
-       if(CVAL(buf,0) == SMBkeepalive) {
-               return NT_STATUS_OK;
-       }
-
        switch (es->smb_enc_type) {
                case SMB_TRANS_ENC_NTLM:
                        return common_ntlm_decrypt_buffer(es->s.ntlmssp_state, buf);
@@ -316,8 +342,12 @@ static void common_free_gss_state(struct smb_tran_enc_state_gss **pp_gss_state)
        OM_uint32 minor = 0;
        struct smb_tran_enc_state_gss *gss_state = *pp_gss_state;
 
-       gss_release_cred(&minor, &gss_state->creds);
-       gss_delete_sec_context(&minor, &gss_state->gss_ctx, NULL);
+       if (gss_state->creds != GSS_C_NO_CREDENTIAL) {
+               gss_release_cred(&minor, &gss_state->creds);
+       }
+       if (gss_state->gss_ctx != GSS_C_NO_CONTEXT) {
+               gss_delete_sec_context(&minor, &gss_state->gss_ctx, NULL);
+       }
        SAFE_FREE(*pp_gss_state);
 }
 #endif
@@ -361,21 +391,19 @@ void common_free_enc_buffer(struct smb_trans_enc_state *es, char *buf)
                return;
        }
 
-       /* We know this is an smb buffer, and we
-        * didn't malloc, only copy, for a keepalive,
-        * so ignore session keepalives. */
-
-       if(CVAL(buf,0) == SMBkeepalive) {
-               return;
-       }
-
        if (es->smb_enc_type == SMB_TRANS_ENC_NTLM) {
                SAFE_FREE(buf);
                return;
        }
 
 #if defined(HAVE_GSSAPI) && defined(HAVE_KRB5)
-       /* gss-api free buffer.... */
+       if (es->smb_enc_type == SMB_TRANS_ENC_GSS) {
+               OM_uint32 min;
+               gss_buffer_desc rel_buf;
+               rel_buf.value = buf;
+               rel_buf.length = smb_len(buf) + 4;
+               gss_release_buffer(&min, &rel_buf);
+       }
 #endif
 }
 
@@ -389,6 +417,9 @@ void common_free_enc_buffer(struct smb_trans_enc_state *es, char *buf)
 
 BOOL cli_encryption_on(struct cli_state *cli)
 {
+       /* If we supported multiple encrytion contexts
+        * here we'd look up based on tid.
+        */
        return common_encryption_on(cli->trans_enc_state);
 }
 
@@ -407,6 +438,17 @@ void cli_free_encryption_context(struct cli_state *cli)
 
 void cli_free_enc_buffer(struct cli_state *cli, char *buf)
 {
+       /* We know this is an smb buffer, and we
+        * didn't malloc, only copy, for a keepalive,
+        * so ignore session keepalives. */
+
+       if(CVAL(buf,0) == SMBkeepalive) {
+               return;
+       }
+
+       /* If we supported multiple encrytion contexts
+        * here we'd look up based on tid.
+        */
        common_free_enc_buffer(cli->trans_enc_state, buf);
 }
 
@@ -416,6 +458,23 @@ void cli_free_enc_buffer(struct cli_state *cli, char *buf)
 
 NTSTATUS cli_decrypt_message(struct cli_state *cli)
 {
+       NTSTATUS status;
+       uint16 enc_ctx_num;
+
+       /* Ignore session keepalives. */
+       if(CVAL(cli->inbuf,0) == SMBkeepalive) {
+               return NT_STATUS_OK;
+       }
+
+       status = get_enc_ctx_num(cli->inbuf, &enc_ctx_num);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       if (enc_ctx_num != cli->trans_enc_state->enc_ctx_num) {
+               return NT_STATUS_INVALID_HANDLE;
+       }
+
        return common_decrypt_buffer(cli->trans_enc_state, cli->inbuf);
 }
 
@@ -425,5 +484,13 @@ NTSTATUS cli_decrypt_message(struct cli_state *cli)
 
 NTSTATUS cli_encrypt_message(struct cli_state *cli, char **buf_out)
 {
+       /* Ignore session keepalives. */
+       if(CVAL(cli->outbuf,0) == SMBkeepalive) {
+               return NT_STATUS_OK;
+       }
+
+       /* If we supported multiple encrytion contexts
+        * here we'd look up based on tid.
+        */
        return common_encrypt_buffer(cli->trans_enc_state, cli->outbuf, buf_out);
 }