util_str: Don't return memory from talloc_tos(), use mem_ctx instead.
authorKai Blin <kai@samba.org>
Sat, 19 Jan 2008 01:12:35 +0000 (02:12 +0100)
committerKai Blin <kai@samba.org>
Sat, 19 Jan 2008 01:20:16 +0000 (02:20 +0100)
(This used to be commit ab0ee6e9a6a9eee317228f0c2bde254ad9a59b85)

source3/lib/afs.c
source3/lib/util_str.c
source3/rpc_server/srv_samr_util.c
source3/utils/net_rpc_samsync.c
source3/utils/ntlm_auth.c

index a7d6f6c9f7fecd6df62aa8be1a4e3099a5120a82..b3d590bf245d0890cd83ec2ce067f698219e0ec1 100644 (file)
@@ -42,20 +42,23 @@ static char *afs_encode_token(const char *cell, const DATA_BLOB ticket,
                              const struct ClearToken *ct)
 {
        char *base64_ticket;
                              const struct ClearToken *ct)
 {
        char *base64_ticket;
-       char *result;
+       char *result = NULL;
 
        DATA_BLOB key = data_blob(ct->HandShakeKey, 8);
        char *base64_key;
 
        DATA_BLOB key = data_blob(ct->HandShakeKey, 8);
        char *base64_key;
+       TALLOC_CTX *mem_ctx;
+
+       mem_ctx = talloc_init("afs_encode_token");
+       if (mem_ctx == NULL)
+               goto done;
 
 
-       base64_ticket = base64_encode_data_blob(ticket);
+       base64_ticket = base64_encode_data_blob(mem_ctx, ticket);
        if (base64_ticket == NULL)
        if (base64_ticket == NULL)
-               return NULL;
+               goto done;
 
 
-       base64_key = base64_encode_data_blob(key);
-       if (base64_key == NULL) {
-               TALLOC_FREE(base64_ticket);
-               return NULL;
-       }
+       base64_key = base64_encode_data_blob(mem_ctx, key);
+       if (base64_key == NULL)
+               goto done;
 
        asprintf(&result, "%s\n%u\n%s\n%u\n%u\n%u\n%s\n", cell,
                 ct->AuthHandle, base64_key, ct->ViceId, ct->BeginTimestamp,
 
        asprintf(&result, "%s\n%u\n%s\n%u\n%u\n%u\n%s\n", cell,
                 ct->AuthHandle, base64_key, ct->ViceId, ct->BeginTimestamp,
@@ -63,8 +66,8 @@ static char *afs_encode_token(const char *cell, const DATA_BLOB ticket,
 
        DEBUG(10, ("Got ticket string:\n%s\n", result));
 
 
        DEBUG(10, ("Got ticket string:\n%s\n", result));
 
-       TALLOC_FREE(base64_ticket);
-       TALLOC_FREE(base64_key);
+done:
+       TALLOC_FREE(mem_ctx);
 
        return result;
 }
 
        return result;
 }
index 3e3268104c0458822bef39d546203f628565018b..bcb9197141c71ea0bbf5ba9e374f722873c1c7d7 100644 (file)
@@ -2415,13 +2415,13 @@ void base64_decode_inplace(char *s)
 }
 
 /**
 }
 
 /**
- * Encode a base64 string into a malloc()ed string caller to free.
+ * Encode a base64 string into a talloc()ed string caller to free.
  *
  * From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c
  * with adjustments
  **/
 
  *
  * From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c
  * with adjustments
  **/
 
-char *base64_encode_data_blob(DATA_BLOB data)
+char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
 {
        int bits = 0;
        int char_count = 0;
 {
        int bits = 0;
        int char_count = 0;
@@ -2434,7 +2434,7 @@ char *base64_encode_data_blob(DATA_BLOB data)
        out_cnt = 0;
        len = data.length;
        output_len = data.length * 2;
        out_cnt = 0;
        len = data.length;
        output_len = data.length * 2;
-       result = TALLOC_ARRAY(talloc_tos(), char, output_len); /* get us plenty of space */
+       result = TALLOC_ARRAY(mem_ctx, char, output_len); /* get us plenty of space */
        SMB_ASSERT(result != NULL);
 
        while (len-- && out_cnt < (data.length * 2) - 5) {
        SMB_ASSERT(result != NULL);
 
        while (len-- && out_cnt < (data.length * 2) - 5) {
index bde7936343360c5717a0ec63fcc7ba9bc1da4465..c8f732153c1231f44df221668eff339f2545b93b 100644 (file)
@@ -44,16 +44,16 @@ void copy_id20_to_sam_passwd(struct samu *to, SAM_USER_INFO_20 *from)
        char *new_string;
        DATA_BLOB mung;
 
        char *new_string;
        DATA_BLOB mung;
 
-       if (from == NULL || to == NULL) 
+       if (from == NULL || to == NULL)
                return;
                return;
-       
+
        if (from->hdr_munged_dial.buffer) {
                old_string = pdb_get_munged_dial(to);
                mung.length = from->hdr_munged_dial.uni_str_len;
                mung.data = (uint8 *) from->uni_munged_dial.buffer;
                mung.free = NULL;
                new_string = (mung.length == 0) ?
        if (from->hdr_munged_dial.buffer) {
                old_string = pdb_get_munged_dial(to);
                mung.length = from->hdr_munged_dial.uni_str_len;
                mung.data = (uint8 *) from->uni_munged_dial.buffer;
                mung.free = NULL;
                new_string = (mung.length == 0) ?
-                       NULL : base64_encode_data_blob(mung);
+                       NULL : base64_encode_data_blob(talloc_tos(), mung);
                DEBUG(10,("INFO_20 UNI_MUNGED_DIAL: %s -> %s\n",old_string, new_string));
                if (STRING_CHANGED_NC(old_string,new_string))
                        pdb_set_munged_dial(to   , new_string, PDB_CHANGED);
                DEBUG(10,("INFO_20 UNI_MUNGED_DIAL: %s -> %s\n",old_string, new_string));
                if (STRING_CHANGED_NC(old_string,new_string))
                        pdb_set_munged_dial(to   , new_string, PDB_CHANGED);
@@ -196,7 +196,7 @@ void copy_id21_to_sam_passwd(struct samu *to, SAM_USER_INFO_21 *from)
                mung.data = (uint8 *) from->uni_munged_dial.buffer;
                mung.free = NULL;
                newstr = (mung.length == 0) ?
                mung.data = (uint8 *) from->uni_munged_dial.buffer;
                mung.free = NULL;
                newstr = (mung.length == 0) ?
-                       NULL : base64_encode_data_blob(mung);
+                       NULL : base64_encode_data_blob(talloc_tos(), mung);
                DEBUG(10,("INFO_21 UNI_MUNGED_DIAL: %s -> %s\n",old_string, newstr));
                if (STRING_CHANGED_NC(old_string,newstr))
                        pdb_set_munged_dial(to   , newstr, PDB_CHANGED);
                DEBUG(10,("INFO_21 UNI_MUNGED_DIAL: %s -> %s\n",old_string, newstr));
                if (STRING_CHANGED_NC(old_string,newstr))
                        pdb_set_munged_dial(to   , newstr, PDB_CHANGED);
@@ -421,7 +421,7 @@ void copy_id23_to_sam_passwd(struct samu *to, SAM_USER_INFO_23 *from)
                mung.data = (uint8 *) from->uni_munged_dial.buffer;
                mung.free = NULL;
                newstr = (mung.length == 0) ?
                mung.data = (uint8 *) from->uni_munged_dial.buffer;
                mung.free = NULL;
                newstr = (mung.length == 0) ?
-                       NULL : base64_encode_data_blob(mung);
+                       NULL : base64_encode_data_blob(talloc_tos(), mung);
                DEBUG(10,("INFO_23 UNI_MUNGED_DIAL: %s -> %s\n",old_string, newstr));
                if (STRING_CHANGED_NC(old_string, newstr))
                        pdb_set_munged_dial(to   , newstr, PDB_CHANGED);
                DEBUG(10,("INFO_23 UNI_MUNGED_DIAL: %s -> %s\n",old_string, newstr));
                if (STRING_CHANGED_NC(old_string, newstr))
                        pdb_set_munged_dial(to   , newstr, PDB_CHANGED);
@@ -633,7 +633,7 @@ void copy_id25_to_sam_passwd(struct samu *to, SAM_USER_INFO_25 *from)
                mung.data = (uint8 *) from->uni_munged_dial.buffer;
                mung.free = NULL;
                newstr = (mung.length == 0) ?
                mung.data = (uint8 *) from->uni_munged_dial.buffer;
                mung.free = NULL;
                newstr = (mung.length == 0) ?
-                       NULL : base64_encode_data_blob(mung);
+                       NULL : base64_encode_data_blob(talloc_tos(), mung);
                DEBUG(10,("INFO_25 UNI_MUNGED_DIAL: %s -> %s\n",old_string, newstr));
                if (STRING_CHANGED_NC(old_string,newstr))
                        pdb_set_munged_dial(to   , newstr, PDB_CHANGED);
                DEBUG(10,("INFO_25 UNI_MUNGED_DIAL: %s -> %s\n",old_string, newstr));
                if (STRING_CHANGED_NC(old_string,newstr))
                        pdb_set_munged_dial(to   , newstr, PDB_CHANGED);
index 779006884d0e1d968519b491dd1a0e0cbebb9547..d0fcfe3aeb3c6e3e517eb03d3c2268f70df1ec21 100644 (file)
@@ -365,7 +365,8 @@ static NTSTATUS sam_account_from_delta(struct samu *account, SAM_ACCOUNT_INFO *d
                old_string = pdb_get_munged_dial(account);
                mung.length = delta->hdr_parameters.uni_str_len;
                mung.data = (uint8 *) delta->uni_parameters.buffer;
                old_string = pdb_get_munged_dial(account);
                mung.length = delta->hdr_parameters.uni_str_len;
                mung.data = (uint8 *) delta->uni_parameters.buffer;
-               newstr = (mung.length == 0) ? NULL : base64_encode_data_blob(mung);
+               newstr = (mung.length == 0) ? NULL :
+                       base64_encode_data_blob(talloc_tos(), mung);
 
                if (STRING_CHANGED_NC(old_string, newstr))
                        pdb_set_munged_dial(account, newstr, PDB_CHANGED);
 
                if (STRING_CHANGED_NC(old_string, newstr))
                        pdb_set_munged_dial(account, newstr, PDB_CHANGED);
@@ -1422,12 +1423,11 @@ static int fprintf_attr(FILE *add_fd, const char *attr_name,
        base64_blob.data = (unsigned char *)value;
        base64_blob.length = strlen(value);
 
        base64_blob.data = (unsigned char *)value;
        base64_blob.length = strlen(value);
 
-       base64 = base64_encode_data_blob(base64_blob);
+       base64 = base64_encode_data_blob(value, base64_blob);
        SMB_ASSERT(base64 != NULL);
 
        res = fprintf(add_fd, "%s:: %s\n", attr_name, base64);
        TALLOC_FREE(value);
        SMB_ASSERT(base64 != NULL);
 
        res = fprintf(add_fd, "%s:: %s\n", attr_name, base64);
        TALLOC_FREE(value);
-       TALLOC_FREE(base64);
        return res;
 }
 
        return res;
 }
 
index 6a702fc0cfad85eea9fe566b3b6c21984da75118..68bf24fec76b83c894c041b0ee2adffdac16937d 100644 (file)
@@ -739,7 +739,8 @@ static void manage_squid_ntlmssp_request(enum stdio_helper_mode stdio_helper_mod
        } else if (strncmp(buf, "GK", 2) == 0) {
                DEBUG(10, ("Requested NTLMSSP session key\n"));
                if(have_session_key) {
        } else if (strncmp(buf, "GK", 2) == 0) {
                DEBUG(10, ("Requested NTLMSSP session key\n"));
                if(have_session_key) {
-                       char *key64 = base64_encode_data_blob(session_key);
+                       char *key64 = base64_encode_data_blob(talloc_tos(),
+                                       session_key);
                        x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
                        TALLOC_FREE(key64);
                } else {
                        x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
                        TALLOC_FREE(key64);
                } else {
@@ -768,7 +769,8 @@ static void manage_squid_ntlmssp_request(enum stdio_helper_mode stdio_helper_mod
        nt_status = ntlmssp_update(ntlmssp_state, request, &reply);
        
        if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
        nt_status = ntlmssp_update(ntlmssp_state, request, &reply);
        
        if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
-               char *reply_base64 = base64_encode_data_blob(reply);
+               char *reply_base64 = base64_encode_data_blob(talloc_tos(),
+                               reply);
                x_fprintf(x_stdout, "TT %s\n", reply_base64);
                TALLOC_FREE(reply_base64);
                data_blob_free(&reply);
                x_fprintf(x_stdout, "TT %s\n", reply_base64);
                TALLOC_FREE(reply_base64);
                data_blob_free(&reply);
@@ -889,7 +891,8 @@ static void manage_client_ntlmssp_request(enum stdio_helper_mode stdio_helper_mo
                DEBUG(10, ("Requested session key\n"));
 
                if(have_session_key) {
                DEBUG(10, ("Requested session key\n"));
 
                if(have_session_key) {
-                       char *key64 = base64_encode_data_blob(session_key);
+                       char *key64 = base64_encode_data_blob(talloc_tos(),
+                                       session_key);
                        x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
                        TALLOC_FREE(key64);
                }
                        x_fprintf(x_stdout, "GK %s\n", key64?key64:"<NULL>");
                        TALLOC_FREE(key64);
                }
@@ -925,7 +928,8 @@ static void manage_client_ntlmssp_request(enum stdio_helper_mode stdio_helper_mo
        }
        
        if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
        }
        
        if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
-               char *reply_base64 = base64_encode_data_blob(reply);
+               char *reply_base64 = base64_encode_data_blob(talloc_tos(),
+                               reply);
                if (first) {
                        x_fprintf(x_stdout, "YR %s\n", reply_base64);
                } else { 
                if (first) {
                        x_fprintf(x_stdout, "YR %s\n", reply_base64);
                } else { 
@@ -939,7 +943,8 @@ static void manage_client_ntlmssp_request(enum stdio_helper_mode stdio_helper_mo
                }
                DEBUG(10, ("NTLMSSP challenge\n"));
        } else if (NT_STATUS_IS_OK(nt_status)) {
                }
                DEBUG(10, ("NTLMSSP challenge\n"));
        } else if (NT_STATUS_IS_OK(nt_status)) {
-               char *reply_base64 = base64_encode_data_blob(reply);
+               char *reply_base64 = base64_encode_data_blob(talloc_tos(),
+                               reply);
                x_fprintf(x_stdout, "AF %s\n", reply_base64);
                TALLOC_FREE(reply_base64);
 
                x_fprintf(x_stdout, "AF %s\n", reply_base64);
                TALLOC_FREE(reply_base64);
 
@@ -1039,7 +1044,7 @@ static void offer_gss_spnego_mechs(void) {
                return;
        }
 
                return;
        }
 
-       reply_base64 = base64_encode_data_blob(token);
+       reply_base64 = base64_encode_data_blob(talloc_tos(), token);
        x_fprintf(x_stdout, "TT %s *\n", reply_base64);
 
        TALLOC_FREE(reply_base64);
        x_fprintf(x_stdout, "TT %s *\n", reply_base64);
 
        TALLOC_FREE(reply_base64);
@@ -1276,7 +1281,7 @@ static void manage_gss_spnego_request(enum stdio_helper_mode stdio_helper_mode,
                return;
        }
 
                return;
        }
 
-       reply_base64 = base64_encode_data_blob(token);
+       reply_base64 = base64_encode_data_blob(talloc_tos(), token);
 
        x_fprintf(x_stdout, "%s %s %s\n",
                  reply_code, reply_base64, reply_argument);
 
        x_fprintf(x_stdout, "%s %s %s\n",
                  reply_code, reply_base64, reply_argument);
@@ -1343,7 +1348,7 @@ static bool manage_client_ntlmssp_init(SPNEGO_DATA spnego)
        write_spnego_data(&to_server, &spnego);
        data_blob_free(&spnego.negTokenInit.mechToken);
 
        write_spnego_data(&to_server, &spnego);
        data_blob_free(&spnego.negTokenInit.mechToken);
 
-       to_server_base64 = base64_encode_data_blob(to_server);
+       to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
        data_blob_free(&to_server);
        x_fprintf(x_stdout, "KK %s\n", to_server_base64);
        TALLOC_FREE(to_server_base64);
        data_blob_free(&to_server);
        x_fprintf(x_stdout, "KK %s\n", to_server_base64);
        TALLOC_FREE(to_server_base64);
@@ -1401,7 +1406,7 @@ static void manage_client_ntlmssp_targ(SPNEGO_DATA spnego)
        write_spnego_data(&to_server, &spnego);
        data_blob_free(&request);
 
        write_spnego_data(&to_server, &spnego);
        data_blob_free(&request);
 
-       to_server_base64 = base64_encode_data_blob(to_server);
+       to_server_base64 = base64_encode_data_blob(talloc_tos(), to_server);
        data_blob_free(&to_server);
        x_fprintf(x_stdout, "KK %s\n", to_server_base64);
        TALLOC_FREE(to_server_base64);
        data_blob_free(&to_server);
        x_fprintf(x_stdout, "KK %s\n", to_server_base64);
        TALLOC_FREE(to_server_base64);
@@ -1490,7 +1495,7 @@ static bool manage_client_krb5_init(SPNEGO_DATA spnego)
                return False;
        }
 
                return False;
        }
 
-       reply_base64 = base64_encode_data_blob(to_server);
+       reply_base64 = base64_encode_data_blob(talloc_tos(), to_server);
        x_fprintf(x_stdout, "KK %s *\n", reply_base64);
 
        TALLOC_FREE(reply_base64);
        x_fprintf(x_stdout, "KK %s *\n", reply_base64);
 
        TALLOC_FREE(reply_base64);