r23792: convert Samba4 to GPLv3
[bbaumbach/samba-autobuild/.git] / source4 / libcli / auth / session.c
index fda0aab055c7b7207bae19fdba118e11add20d6a..4a9d79c4256c5791b5169de46bf9fbafa0c21a25 100644 (file)
@@ -7,7 +7,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"
+#include "libcli/auth/libcli_auth.h"
 
 /*
   encrypt or decrypt a blob of data using the user session key
@@ -96,7 +96,8 @@ DATA_BLOB sess_encrypt_string(const char *str, const DATA_BLOB *session_key)
 
   caller should free the returned string
 */
-char *sess_decrypt_string(DATA_BLOB *blob, const DATA_BLOB *session_key)
+char *sess_decrypt_string(TALLOC_CTX *mem_ctx, 
+                         DATA_BLOB *blob, const DATA_BLOB *session_key)
 {
        DATA_BLOB out;
        int slen;
@@ -106,7 +107,7 @@ char *sess_decrypt_string(DATA_BLOB *blob, const DATA_BLOB *session_key)
                return NULL;
        }
        
-       out = data_blob(NULL, blob->length);
+       out = data_blob_talloc(mem_ctx, NULL, blob->length);
        if (!out.data) {
                return NULL;
        }
@@ -116,19 +117,23 @@ char *sess_decrypt_string(DATA_BLOB *blob, const DATA_BLOB *session_key)
        if (IVAL(out.data, 4) != 1) {
                DEBUG(0,("Unexpected revision number %d in session crypted string\n",
                         IVAL(out.data, 4)));
+               data_blob_free(&out);
                return NULL;
        }
 
        slen = IVAL(out.data, 0);
        if (slen > blob->length - 8) {
                DEBUG(0,("Invalid crypt length %d\n", slen));
+               data_blob_free(&out);
                return NULL;
        }
 
-       ret = strndup((const char *)(out.data+8), slen);
+       ret = talloc_strndup(mem_ctx, (const char *)(out.data+8), slen);
 
        data_blob_free(&out);
 
+       DEBUG(0,("decrypted string '%s' of length %d\n", ret, slen));
+
        return ret;
 }
 
@@ -150,7 +155,7 @@ DATA_BLOB sess_encrypt_blob(TALLOC_CTX *mem_ctx, DATA_BLOB *blob_in, const DATA_
                return data_blob(NULL, 0);
        }
 
-       ret = data_blob(NULL, 8+dlen);
+       ret = data_blob_talloc(mem_ctx, NULL, 8+dlen);
        if (!ret.data) {
                data_blob_free(&src);
                return data_blob(NULL, 0);
@@ -179,7 +184,7 @@ NTSTATUS sess_decrypt_blob(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, const DAT
 
        if (blob->length < 8) {
                DEBUG(0, ("Unexpected length %d in session crypted secret (BLOB)\n",
-                         blob->length));
+                         (int)blob->length));
                return NT_STATUS_INVALID_PARAMETER;
        }
        
@@ -191,7 +196,7 @@ NTSTATUS sess_decrypt_blob(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, const DAT
        sess_crypt_blob(&out, blob, session_key, False);
 
        if (IVAL(out.data, 4) != 1) {
-               DEBUG(0,("Unexpected revision number %d in session crypted secret (BLOB)\n",
+               DEBUG(2,("Unexpected revision number %d in session crypted secret (BLOB)\n",
                         IVAL(out.data, 4)));
                return NT_STATUS_UNKNOWN_REVISION;
        }