[PATCH] some cleanups

Volker Lendecke Volker.Lendecke at SerNet.DE
Fri May 8 08:46:41 MDT 2015


Hi!

Review&push appreciated!

Thanks,

Volker

-- 
SerNet GmbH, Bahnhofsallee 1b, 37081 Göttingen
phone: +49-551-370000-0, fax: +49-551-370000-9
AG Göttingen, HRB 2816, GF: Dr. Johannes Loxen
http://www.sernet.de, mailto:kontakt at sernet.de
-------------- next part --------------
From fdf3ae079e95455a99b6a2380b5b587ab08142ee Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Fri, 8 May 2015 07:06:53 +0000
Subject: [PATCH 1/5] lib: Simplify sid_binstring_hex()

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/lib/util_sid.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/source3/lib/util_sid.c b/source3/lib/util_sid.c
index f051b7a..55904d7 100644
--- a/source3/lib/util_sid.c
+++ b/source3/lib/util_sid.c
@@ -115,14 +115,11 @@ bool non_mappable_sid(struct dom_sid *sid)
 
 char *sid_binstring_hex(const struct dom_sid *sid)
 {
-	char *buf, *s;
+	char *s;
 	int len = ndr_size_dom_sid(sid, 0);
-	buf = (char *)SMB_MALLOC(len);
-	if (!buf)
-		return NULL;
+	char buf[len];
 	sid_linearize(buf, len, sid);
 	hex_encode((const unsigned char *)buf, len, &s);
-	free(buf);
 	return s;
 }
 
-- 
1.9.1


From ed8d721bd184e6186ea6d67001dc1b14093e19c3 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Fri, 8 May 2015 10:06:23 +0000
Subject: [PATCH 2/5] lib: Make sid_binstring_hex use TALLOC

talloc_tos() is better than plain malloc...

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/include/proto.h     | 2 +-
 source3/lib/util_sid.c      | 6 ++----
 source3/libads/ldap_utils.c | 6 +++---
 3 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/source3/include/proto.h b/source3/include/proto.h
index 815c864..dad4dd9 100644
--- a/source3/include/proto.h
+++ b/source3/include/proto.h
@@ -524,7 +524,7 @@ char *sid_string_dbg(const struct dom_sid *sid);
 char *sid_string_tos(const struct dom_sid *sid);
 bool sid_linearize(char *outbuf, size_t len, const struct dom_sid *sid);
 bool non_mappable_sid(struct dom_sid *sid);
-char *sid_binstring_hex(const struct dom_sid *sid);
+char *sid_binstring_hex_talloc(TALLOC_CTX *mem_ctx, const struct dom_sid *sid);
 struct netr_SamInfo3;
 NTSTATUS sid_array_from_info3(TALLOC_CTX *mem_ctx,
 			      const struct netr_SamInfo3 *info3,
diff --git a/source3/lib/util_sid.c b/source3/lib/util_sid.c
index 55904d7..e336510 100644
--- a/source3/lib/util_sid.c
+++ b/source3/lib/util_sid.c
@@ -113,14 +113,12 @@ bool non_mappable_sid(struct dom_sid *sid)
  Caller must free.
 *****************************************************************/
 
-char *sid_binstring_hex(const struct dom_sid *sid)
+char *sid_binstring_hex_talloc(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
 {
-	char *s;
 	int len = ndr_size_dom_sid(sid, 0);
 	char buf[len];
 	sid_linearize(buf, len, sid);
-	hex_encode((const unsigned char *)buf, len, &s);
-	return s;
+	return hex_encode_talloc(mem_ctx, (const unsigned char *)buf, len);
 }
 
 NTSTATUS sid_array_from_info3(TALLOC_CTX *mem_ctx,
diff --git a/source3/libads/ldap_utils.c b/source3/libads/ldap_utils.c
index 117dc55..157f694 100644
--- a/source3/libads/ldap_utils.c
+++ b/source3/libads/ldap_utils.c
@@ -214,20 +214,20 @@ static ADS_STATUS ads_do_search_retry_args(ADS_STRUCT *ads, const char *bind_pat
 	char *dn, *sid_string;
 	ADS_STATUS status;
 
-	sid_string = sid_binstring_hex(sid);
+	sid_string = sid_binstring_hex_talloc(talloc_tos(), sid);
 	if (sid_string == NULL) {
 		return ADS_ERROR(LDAP_NO_MEMORY);
 	}
 
 	if (!asprintf(&dn, "<SID=%s>", sid_string)) {
-		SAFE_FREE(sid_string);
+		TALLOC_FREE(sid_string);
 		return ADS_ERROR(LDAP_NO_MEMORY);
 	}
 
 	status = ads_do_search_retry(ads, dn, LDAP_SCOPE_BASE,
 				   "(objectclass=*)", attrs, res);
 	SAFE_FREE(dn);
-	SAFE_FREE(sid_string);
+	TALLOC_FREE(sid_string);
 	return status;
 }
 
-- 
1.9.1


From 897293e7905ac584f4c32f1f45ee5f54ac309419 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Fri, 8 May 2015 10:12:21 +0000
Subject: [PATCH 3/5] ntlm_auth: Remove two uses of hex_encode()

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source4/utils/ntlm_auth.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/source4/utils/ntlm_auth.c b/source4/utils/ntlm_auth.c
index f44e782..f1683f5 100644
--- a/source4/utils/ntlm_auth.c
+++ b/source4/utils/ntlm_auth.c
@@ -796,8 +796,6 @@ static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mod
 				SAFE_FREE(error_string);
 			} else {
 				static char zeros[16];
-				char *hex_lm_key;
-				char *hex_user_session_key;
 
 				mux_printf(mux_id, "Authenticated: Yes\n");
 
@@ -805,22 +803,22 @@ static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mod
 				    && lm_key.length 
 				    && (memcmp(zeros, lm_key.data, 
 								lm_key.length) != 0)) {
-					hex_encode(lm_key.data,
-						   lm_key.length,
-						   &hex_lm_key);
+					char hex_lm_key[lm_key.length*2+1];
+					hex_encode_buf(buf, lm_key.data,
+						       lm_key.length);
 					mux_printf(mux_id, "LANMAN-Session-Key: %s\n", hex_lm_key);
-					SAFE_FREE(hex_lm_key);
 				}
 
 				if (ntlm_server_1_user_session_key 
 				    && user_session_key.length 
 				    && (memcmp(zeros, user_session_key.data, 
 					       user_session_key.length) != 0)) {
-					hex_encode(user_session_key.data, 
-						   user_session_key.length, 
-						   &hex_user_session_key);
+					char hex_user_session_key[
+						user_session_key.length*2+1];
+					hex_encode_buf(hex_user_session_key,
+						       user_session_key.data,
+						       user_session_key.length);
 					mux_printf(mux_id, "User-Session-Key: %s\n", hex_user_session_key);
-					SAFE_FREE(hex_user_session_key);
 				}
 			}
 		}
-- 
1.9.1


From 0671ac5efa4176707b1fd8d71ceaf2e51119f03c Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Fri, 8 May 2015 10:24:48 +0000
Subject: [PATCH 4/5] lib: Remove unused hex_encode()

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/samba_util.h |  7 +------
 lib/util/util.c       | 14 +-------------
 2 files changed, 2 insertions(+), 19 deletions(-)

diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h
index 176930b..1c974cd 100644
--- a/lib/util/samba_util.h
+++ b/lib/util/samba_util.h
@@ -205,12 +205,7 @@ _PUBLIC_ _PURE_ DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *s
 _PUBLIC_ void hex_encode_buf(char *dst, const uint8_t *src, size_t srclen);
 
 /**
- * Routine to print a buffer as HEX digits, into an allocated string.
- */
-_PUBLIC_ void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer);
-
-/**
- * talloc version of hex_encode()
+ * talloc version of hex_encode_buf()
  */
 _PUBLIC_ char *hex_encode_talloc(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len);
 
diff --git a/lib/util/util.c b/lib/util/util.c
index 562f7df..9ef7124 100644
--- a/lib/util/util.c
+++ b/lib/util/util.c
@@ -932,19 +932,7 @@ _PUBLIC_ void hex_encode_buf(char *dst, const uint8_t *src, size_t srclen)
 }
 
 /**
- * Routine to print a buffer as HEX digits, into an allocated string.
- */
-_PUBLIC_ void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer)
-{
-	char *hex_buffer;
-
-	*out_hex_buffer = malloc_array_p(char, (len*2)+1);
-	hex_buffer = *out_hex_buffer;
-	hex_encode_buf(hex_buffer, buff_in, len);
-}
-
-/**
- * talloc version of hex_encode()
+ * talloc version of hex_encode_buf()
  */
 _PUBLIC_ char *hex_encode_talloc(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len)
 {
-- 
1.9.1


From 784f8bd38990ac168552a4ebb72f839d0b570372 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Fri, 8 May 2015 13:15:37 +0000
Subject: [PATCH 5/5] dbwrap: Remove an unused variable

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/dbwrap/dbwrap_local_open.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/lib/dbwrap/dbwrap_local_open.c b/lib/dbwrap/dbwrap_local_open.c
index c850e3c..6509ff9 100644
--- a/lib/dbwrap/dbwrap_local_open.c
+++ b/lib/dbwrap/dbwrap_local_open.c
@@ -32,13 +32,11 @@ struct db_context *dbwrap_local_open(TALLOC_CTX *mem_ctx,
 				     enum dbwrap_lock_order lock_order,
 				     uint64_t dbwrap_flags)
 {
-	TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
 	struct db_context *db = NULL;
 
 	db = db_open_tdb(mem_ctx, lp_ctx, name, hash_size,
 			 tdb_flags, open_flags, mode,
 			 lock_order, dbwrap_flags);
 
-	talloc_free(tmp_ctx);
 	return db;
 }
-- 
1.9.1



More information about the samba-technical mailing list