r9198: Convert hex_encode and strhex_to_data_blob to take a talloc context.
authorVolker Lendecke <vlendec@samba.org>
Sun, 7 Aug 2005 20:59:28 +0000 (20:59 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 16:00:27 +0000 (11:00 -0500)
Volker

source/lib/util_str.c
source/rpcclient/rpcclient.c
source/utils/ntlm_auth.c

index 1401d6d85349de0f8e17f34451c89e1bfa30f28b..1252c6756bf4ca0a8d500df0e12e051ddb9763c3 100644 (file)
@@ -795,9 +795,14 @@ size_t strhex_to_str(char *p, size_t len, const char *strhex)
        return num_chars;
 }
 
-DATA_BLOB strhex_to_data_blob(const char *strhex) 
+DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *strhex) 
 {
-       DATA_BLOB ret_blob = data_blob(NULL, strlen(strhex)/2+1);
+       DATA_BLOB ret_blob;
+
+       if (mem_ctx != NULL)
+               ret_blob = data_blob_talloc(mem_ctx, NULL, strlen(strhex)/2+1);
+       else
+               data_blob(NULL, strlen(strhex)/2+1);
 
        ret_blob.length = strhex_to_str((char*)ret_blob.data,   
                                        strlen(strhex), 
@@ -810,16 +815,17 @@ DATA_BLOB strhex_to_data_blob(const char *strhex)
  * Routine to print a buffer as HEX digits, into an allocated string.
  */
 
-void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer)
+char *hex_encode(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len)
 {
        int i;
        char *hex_buffer;
 
-       *out_hex_buffer = SMB_XMALLOC_ARRAY(char, (len*2)+1);
-       hex_buffer = *out_hex_buffer;
+       hex_buffer = TALLOC_ARRAY(mem_ctx, char, (len*2)+1);
 
        for (i = 0; i < len; i++)
                slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
+
+       return hex_buffer;
 }
 
 /**
index 3112db19ad990d7c1a064a4eb5ab3e265d5ba443..34e81cafe68d4e44d945cd9bdf8af3cd2184d4dd 100644 (file)
@@ -384,11 +384,10 @@ static NTSTATUS setup_schannel(struct cli_state *cli, int pipe_auth_flags,
        ret = cli_nt_setup_netsec(cli, sec_channel_type, pipe_auth_flags, trust_password);
        if (NT_STATUS_IS_OK(ret)) {
                char *hex_session_key;
-               hex_encode(cli->pipes[cli->pipe_idx].auth_info.sess_key,
-                          sizeof(cli->pipes[cli->pipe_idx].auth_info.sess_key),
-                          &hex_session_key);
+               hex_session_key = hex_encode(NULL, cli->pipes[cli->pipe_idx].auth_info.sess_key,
+                                            sizeof(cli->pipes[cli->pipe_idx].auth_info.sess_key));
                printf("Got Session key: %s\n", hex_session_key);
-               SAFE_FREE(hex_session_key);
+               talloc_free(hex_session_key);
        }
        return ret;
 }
index 6d5737aad4a867d105ad4ba9b30b921145201f39..3fdd657a2da0f919842f4f42557d74b68272f4df 100644 (file)
@@ -1470,21 +1470,21 @@ static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mod
                                if (ntlm_server_1_lm_session_key 
                                    && (memcmp(zeros, lm_key, 
                                               sizeof(lm_key)) != 0)) {
-                                       hex_encode((const unsigned char *)lm_key,
-                                                  sizeof(lm_key),
-                                                  &hex_lm_key);
+                                       hex_lm_key = hex_encode(NULL,
+                                                               (const unsigned char *)lm_key,
+                                                               sizeof(lm_key));
                                        x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
-                                       SAFE_FREE(hex_lm_key);
+                                       talloc_free(hex_lm_key);
                                }
 
                                if (ntlm_server_1_user_session_key 
                                    && (memcmp(zeros, user_session_key, 
                                               sizeof(user_session_key)) != 0)) {
-                                       hex_encode((const unsigned char *)user_session_key, 
-                                                  sizeof(user_session_key)
-                                                  &hex_user_session_key);
+                                       hex_user_session_key = hex_encode(NULL,
+                                                                         (const unsigned char *)user_session_key
+                                                                         sizeof(user_session_key));
                                        x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
-                                       SAFE_FREE(hex_user_session_key);
+                                       talloc_free(hex_user_session_key);
                                }
                        }
                }
@@ -1533,7 +1533,7 @@ static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mod
        }
 
        if (strequal(request, "LANMAN-Challenge")) {
-               challenge = strhex_to_data_blob(parameter);
+               challenge = strhex_to_data_blob(NULL, parameter);
                if (challenge.length != 8) {
                        x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n", 
                                  parameter,
@@ -1541,7 +1541,7 @@ static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mod
                        challenge = data_blob(NULL, 0);
                }
        } else if (strequal(request, "NT-Response")) {
-               nt_response = strhex_to_data_blob(parameter);
+               nt_response = strhex_to_data_blob(NULL, parameter);
                if (nt_response.length < 24) {
                        x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n", 
                                  parameter,
@@ -1549,7 +1549,7 @@ static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mod
                        nt_response = data_blob(NULL, 0);
                }
        } else if (strequal(request, "LANMAN-Response")) {
-               lm_response = strhex_to_data_blob(parameter);
+               lm_response = strhex_to_data_blob(NULL, parameter);
                if (lm_response.length != 24) {
                        x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n", 
                                  parameter,
@@ -1672,20 +1672,18 @@ static BOOL check_auth_crap(void)
        if (request_lm_key 
            && (memcmp(zeros, lm_key, 
                       sizeof(lm_key)) != 0)) {
-               hex_encode((const unsigned char *)lm_key,
-                          sizeof(lm_key),
-                          &hex_lm_key);
+               hex_lm_key = hex_encode(NULL, (const unsigned char *)lm_key,
+                                       sizeof(lm_key));
                x_fprintf(x_stdout, "LM_KEY: %s\n", hex_lm_key);
-               SAFE_FREE(hex_lm_key);
+               talloc_free(hex_lm_key);
        }
        if (request_user_session_key 
            && (memcmp(zeros, user_session_key, 
                       sizeof(user_session_key)) != 0)) {
-               hex_encode((const unsigned char *)user_session_key, 
-                          sizeof(user_session_key), 
-                          &hex_user_session_key);
+               hex_user_session_key = hex_encode(NULL, (const unsigned char *)user_session_key, 
+                                                 sizeof(user_session_key));
                x_fprintf(x_stdout, "NT_KEY: %s\n", hex_user_session_key);
-               SAFE_FREE(hex_user_session_key);
+               talloc_free(hex_user_session_key);
        }
 
         return True;
@@ -1775,7 +1773,7 @@ enum {
        while((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
                case OPT_CHALLENGE:
-                       opt_challenge = strhex_to_data_blob(hex_challenge);
+                       opt_challenge = strhex_to_data_blob(NULL, hex_challenge);
                        if (opt_challenge.length != 8) {
                                x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n", 
                                          hex_challenge,
@@ -1784,7 +1782,7 @@ enum {
                        }
                        break;
                case OPT_LM: 
-                       opt_lm_response = strhex_to_data_blob(hex_lm_response);
+                       opt_lm_response = strhex_to_data_blob(NULL, hex_lm_response);
                        if (opt_lm_response.length != 24) {
                                x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n", 
                                          hex_lm_response,
@@ -1794,7 +1792,7 @@ enum {
                        break;
 
                case OPT_NT: 
-                       opt_nt_response = strhex_to_data_blob(hex_nt_response);
+                       opt_nt_response = strhex_to_data_blob(NULL, hex_nt_response);
                        if (opt_nt_response.length < 24) {
                                x_fprintf(x_stderr, "hex decode of %s failed! (only got %d bytes)\n", 
                                          hex_nt_response,