[PATCH] Some cleanups for samlogon_cache.c

Volker Lendecke vl at samba.org
Thu Dec 1 11:19:17 UTC 2016


Hi!

Review appreciated!

Thanks, Volker
-------------- next part --------------
>From 4856fbfc6fb6a2b3f5760454bab266b406bedbc3 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Nov 2016 14:55:47 +0000
Subject: [PATCH 1/9] lib: Delete an orphaned piece of code in samlogon_cache.c

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/libsmb/samlogon_cache.c | 19 -------------------
 1 file changed, 19 deletions(-)

diff --git a/source3/libsmb/samlogon_cache.c b/source3/libsmb/samlogon_cache.c
index 7be5479..c87a233 100644
--- a/source3/libsmb/samlogon_cache.c
+++ b/source3/libsmb/samlogon_cache.c
@@ -265,25 +265,6 @@ struct netr_SamInfo3 *netsamlogon_cache_get(TALLOC_CTX *mem_ctx, const struct do
 	SAFE_FREE(data.dptr);
 
 	return info3;
-
-#if 0	/* The netsamlogon cache needs to hang around.  Something about
-	   this feels wrong, but it is the only way we can get all of the
-	   groups.  The old universal groups cache didn't expire either.
-	   --jerry */
-	{
-		time_t		now = time(NULL);
-		uint32_t	time_diff;
-
-		/* is the entry expired? */
-		time_diff = now - t;
-
-		if ( (time_diff < 0 ) || (time_diff > lp_winbind_cache_time()) ) {
-			DEBUG(10,("netsamlogon_cache_get: cache entry expired \n"));
-			tdb_delete( netsamlogon_tdb, key );
-			TALLOC_FREE( user );
-		}
-	}
-#endif
 }
 
 bool netsamlogon_cache_have(const struct dom_sid *user_sid)
-- 
2.1.4


>From 1049475eb59ed2a4df427a2712fa4effd927050b Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Nov 2016 14:58:51 +0000
Subject: [PATCH 2/9] lib: Delete unused netsamlogon_cache_shutdown

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/include/proto.h         |  1 -
 source3/libsmb/samlogon_cache.c | 14 --------------
 2 files changed, 15 deletions(-)

diff --git a/source3/include/proto.h b/source3/include/proto.h
index 33e3f6c..c3460a9 100644
--- a/source3/include/proto.h
+++ b/source3/include/proto.h
@@ -842,7 +842,6 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
 /* The following definitions come from libsmb/samlogon_cache.c  */
 
 bool netsamlogon_cache_init(void);
-bool netsamlogon_cache_shutdown(void);
 void netsamlogon_clear_cached_user(const struct dom_sid *user_sid);
 bool netsamlogon_cache_store(const char *username, struct netr_SamInfo3 *info3);
 struct netr_SamInfo3 *netsamlogon_cache_get(TALLOC_CTX *mem_ctx, const struct dom_sid *user_sid);
diff --git a/source3/libsmb/samlogon_cache.c b/source3/libsmb/samlogon_cache.c
index c87a233..956df5c 100644
--- a/source3/libsmb/samlogon_cache.c
+++ b/source3/libsmb/samlogon_cache.c
@@ -87,20 +87,6 @@ clear:
 	goto again;
 }
 
-
-/***********************************************************************
- Shutdown samlogon_cache database
-***********************************************************************/
-
-bool netsamlogon_cache_shutdown(void)
-{
-	if (netsamlogon_tdb) {
-		return (tdb_close(netsamlogon_tdb) == 0);
-	}
-
-	return true;
-}
-
 /***********************************************************************
  Clear cache getpwnam and getgroups entries from the winbindd cache
 ***********************************************************************/
-- 
2.1.4


>From 36996f515c46beeecadf2cef0b1900c9c0d5452c Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Nov 2016 15:20:14 +0000
Subject: [PATCH 3/9] lib: Fix netsamlogon_cache_have for README.Coding

"False" should have gone long ago

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

diff --git a/source3/libsmb/samlogon_cache.c b/source3/libsmb/samlogon_cache.c
index 956df5c..fdcdf13 100644
--- a/source3/libsmb/samlogon_cache.c
+++ b/source3/libsmb/samlogon_cache.c
@@ -259,8 +259,9 @@ bool netsamlogon_cache_have(const struct dom_sid *user_sid)
 	struct netr_SamInfo3 *info3 = NULL;
 	bool result;
 
-	if (!mem_ctx)
-		return False;
+	if (mem_ctx == NULL) {
+		return false;
+	}
 
 	info3 = netsamlogon_cache_get(mem_ctx, user_sid);
 
-- 
2.1.4


>From 4fffb123ebc1fe6ce345d218d28787fc7e6bcc72 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Nov 2016 15:41:27 +0000
Subject: [PATCH 4/9] lib: Add samlogon_cache.h

Move prototypes into its own header file

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/auth/auth_domain.c        |  1 +
 source3/include/proto.h           |  8 --------
 source3/libsmb/samlogon_cache.c   |  1 +
 source3/libsmb/samlogon_cache.h   | 41 +++++++++++++++++++++++++++++++++++++++
 source3/winbindd/winbindd.c       |  1 +
 source3/winbindd/winbindd_ads.c   |  1 +
 source3/winbindd/winbindd_cache.c |  1 +
 source3/winbindd/winbindd_creds.c |  1 +
 source3/winbindd/winbindd_msrpc.c |  1 +
 source3/winbindd/winbindd_pam.c   |  1 +
 source3/winbindd/winbindd_util.c  |  1 +
 11 files changed, 50 insertions(+), 8 deletions(-)
 create mode 100644 source3/libsmb/samlogon_cache.h

diff --git a/source3/auth/auth_domain.c b/source3/auth/auth_domain.c
index 1a8cd91..d663923 100644
--- a/source3/auth/auth_domain.c
+++ b/source3/auth/auth_domain.c
@@ -28,6 +28,7 @@
 #include "passdb.h"
 #include "libsmb/libsmb.h"
 #include "libcli/auth/netlogon_creds_cli.h"
+#include "libsmb/samlogon_cache.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_AUTH
diff --git a/source3/include/proto.h b/source3/include/proto.h
index c3460a9..53a2d6a 100644
--- a/source3/include/proto.h
+++ b/source3/include/proto.h
@@ -839,14 +839,6 @@ NTSTATUS remote_password_change(const char *remote_machine, const char *user_nam
 				const char *old_passwd, const char *new_passwd,
 				char **err_str);
 
-/* The following definitions come from libsmb/samlogon_cache.c  */
-
-bool netsamlogon_cache_init(void);
-void netsamlogon_clear_cached_user(const struct dom_sid *user_sid);
-bool netsamlogon_cache_store(const char *username, struct netr_SamInfo3 *info3);
-struct netr_SamInfo3 *netsamlogon_cache_get(TALLOC_CTX *mem_ctx, const struct dom_sid *user_sid);
-bool netsamlogon_cache_have(const struct dom_sid *user_sid);
-
 /* The following definitions come from libsmb/smberr.c  */
 
 const char *smb_dos_err_name(uint8_t e_class, uint16_t num);
diff --git a/source3/libsmb/samlogon_cache.c b/source3/libsmb/samlogon_cache.c
index fdcdf13..de2156d 100644
--- a/source3/libsmb/samlogon_cache.c
+++ b/source3/libsmb/samlogon_cache.c
@@ -22,6 +22,7 @@
 */
 
 #include "includes.h"
+#include "samlogon_cache.h"
 #include "system/filesys.h"
 #include "librpc/gen_ndr/ndr_krb5pac.h"
 #include "../libcli/security/security.h"
diff --git a/source3/libsmb/samlogon_cache.h b/source3/libsmb/samlogon_cache.h
new file mode 100644
index 0000000..0a2fd14
--- /dev/null
+++ b/source3/libsmb/samlogon_cache.h
@@ -0,0 +1,41 @@
+/*
+ * Unix SMB/CIFS implementation.
+ * Net_sam_logon info3 helpers
+ * Copyright (C) Alexander Bokovoy              2002.
+ * Copyright (C) Andrew Bartlett                2002.
+ * Copyright (C) Gerald Carter			2003.
+ * Copyright (C) Tim Potter			2003.
+ * Copyright (C) Guenther Deschner		2008.
+ *
+ * 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __LIBSMB_SAMLOGON_CACHE_H__
+#define __LIBSMB_SAMLOGON_CACHE_H__
+
+#include "replace.h"
+#include <talloc.h>
+
+struct dom_sid;
+struct netr_SamInfo3;
+
+bool netsamlogon_cache_init(void);
+void netsamlogon_clear_cached_user(const struct dom_sid *user_sid);
+bool netsamlogon_cache_store(const char *username,
+			     struct netr_SamInfo3 *info3);
+struct netr_SamInfo3 *netsamlogon_cache_get(TALLOC_CTX *mem_ctx,
+					    const struct dom_sid *user_sid);
+bool netsamlogon_cache_have(const struct dom_sid *user_sid);
+
+#endif
diff --git a/source3/winbindd/winbindd.c b/source3/winbindd/winbindd.c
index f79dc47..778ee80 100644
--- a/source3/winbindd/winbindd.c
+++ b/source3/winbindd/winbindd.c
@@ -43,6 +43,7 @@
 #include "source4/lib/messaging/messaging.h"
 #include "lib/param/param.h"
 #include "lib/async_req/async_sock.h"
+#include "libsmb/samlogon_cache.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
diff --git a/source3/winbindd/winbindd_ads.c b/source3/winbindd/winbindd_ads.c
index dc92a4a..fdb6806 100644
--- a/source3/winbindd/winbindd_ads.c
+++ b/source3/winbindd/winbindd_ads.c
@@ -30,6 +30,7 @@
 #include "../libcli/ldap/ldap_ndr.h"
 #include "../libcli/security/security.h"
 #include "../libds/common/flag_mapping.h"
+#include "libsmb/samlogon_cache.h"
 #include "passdb.h"
 
 #ifdef HAVE_ADS
diff --git a/source3/winbindd/winbindd_cache.c b/source3/winbindd/winbindd_cache.c
index 4a9782a..05b356f 100644
--- a/source3/winbindd/winbindd_cache.c
+++ b/source3/winbindd/winbindd_cache.c
@@ -34,6 +34,7 @@
 #include "../libcli/security/security.h"
 #include "passdb/machine_sid.h"
 #include "util_tdb.h"
+#include "libsmb/samlogon_cache.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
diff --git a/source3/winbindd/winbindd_creds.c b/source3/winbindd/winbindd_creds.c
index 3717641..15cca55 100644
--- a/source3/winbindd/winbindd_creds.c
+++ b/source3/winbindd/winbindd_creds.c
@@ -23,6 +23,7 @@
 #include "winbindd.h"
 #include "../libcli/auth/libcli_auth.h"
 #include "../libcli/security/security.h"
+#include "libsmb/samlogon_cache.h"
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
 
diff --git a/source3/winbindd/winbindd_msrpc.c b/source3/winbindd/winbindd_msrpc.c
index 4121747..dff27b4 100644
--- a/source3/winbindd/winbindd_msrpc.c
+++ b/source3/winbindd/winbindd_msrpc.c
@@ -31,6 +31,7 @@
 #include "rpc_client/cli_samr.h"
 #include "rpc_client/cli_lsarpc.h"
 #include "../libcli/security/security.h"
+#include "libsmb/samlogon_cache.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
diff --git a/source3/winbindd/winbindd_pam.c b/source3/winbindd/winbindd_pam.c
index 9433ad1..741a42f 100644
--- a/source3/winbindd/winbindd_pam.c
+++ b/source3/winbindd/winbindd_pam.c
@@ -42,6 +42,7 @@
 #include "auth/gensec/gensec.h"
 #include "librpc/crypto/gse_krb5.h"
 #include "lib/afs/afs_funcs.h"
+#include "libsmb/samlogon_cache.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
diff --git a/source3/winbindd/winbindd_util.c b/source3/winbindd/winbindd_util.c
index 217c049..aabaa6a 100644
--- a/source3/winbindd/winbindd_util.c
+++ b/source3/winbindd/winbindd_util.c
@@ -30,6 +30,7 @@
 #include "source4/lib/messaging/messaging.h"
 #include "librpc/gen_ndr/ndr_lsa.h"
 #include "auth/credentials/credentials.h"
+#include "libsmb/samlogon_cache.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
-- 
2.1.4


>From f96c2bd0be2498256526f8a1addec94e4e3c9964 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Nov 2016 15:52:29 +0000
Subject: [PATCH 5/9] lib: Avoid fstring in samlogon_cache.c

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/libsmb/samlogon_cache.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/source3/libsmb/samlogon_cache.c b/source3/libsmb/samlogon_cache.c
index de2156d..ed70038 100644
--- a/source3/libsmb/samlogon_cache.c
+++ b/source3/libsmb/samlogon_cache.c
@@ -94,7 +94,7 @@ clear:
 
 void netsamlogon_clear_cached_user(const struct dom_sid *user_sid)
 {
-	fstring keystr;
+	char keystr[DOM_SID_STR_BUFLEN];
 
 	if (!netsamlogon_cache_init()) {
 		DEBUG(0,("netsamlogon_clear_cached_user: cannot open "
@@ -104,7 +104,7 @@ void netsamlogon_clear_cached_user(const struct dom_sid *user_sid)
 	}
 
 	/* Prepare key as DOMAIN-SID/USER-RID string */
-	sid_to_fstring(keystr, user_sid);
+	dom_sid_string_buf(user_sid, keystr, sizeof(keystr));
 
 	DEBUG(10,("netsamlogon_clear_cached_user: SID [%s]\n", keystr));
 
@@ -119,7 +119,7 @@ void netsamlogon_clear_cached_user(const struct dom_sid *user_sid)
 bool netsamlogon_cache_store(const char *username, struct netr_SamInfo3 *info3)
 {
 	TDB_DATA data;
-	fstring keystr;
+	char keystr[DOM_SID_STR_BUFLEN];
 	bool result = false;
 	struct dom_sid	user_sid;
 	time_t t = time(NULL);
@@ -141,7 +141,7 @@ bool netsamlogon_cache_store(const char *username, struct netr_SamInfo3 *info3)
 	sid_compose(&user_sid, info3->base.domain_sid, info3->base.rid);
 
 	/* Prepare key as DOMAIN-SID/USER-RID string */
-	sid_to_fstring(keystr, &user_sid);
+	dom_sid_string_buf(&user_sid, keystr, sizeof(keystr));
 
 	DEBUG(10,("netsamlogon_cache_store: SID [%s]\n", keystr));
 
@@ -204,7 +204,7 @@ struct netr_SamInfo3 *netsamlogon_cache_get(TALLOC_CTX *mem_ctx, const struct do
 {
 	struct netr_SamInfo3 *info3 = NULL;
 	TDB_DATA data;
-	fstring keystr;
+	char keystr[DOM_SID_STR_BUFLEN];
 	enum ndr_err_code ndr_err;
 	DATA_BLOB blob;
 	struct netsamlogoncache_entry r;
@@ -216,7 +216,7 @@ struct netr_SamInfo3 *netsamlogon_cache_get(TALLOC_CTX *mem_ctx, const struct do
 	}
 
 	/* Prepare key as DOMAIN-SID/USER-RID string */
-	sid_to_fstring(keystr, user_sid);
+	dom_sid_string_buf(user_sid, keystr, sizeof(keystr));
 	DEBUG(10,("netsamlogon_cache_get: SID [%s]\n", keystr));
 	data = tdb_fetch_bystring( netsamlogon_tdb, keystr );
 
-- 
2.1.4


>From 04f152d270a30dcdc8ce28950800d04d5ce0e7f3 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Nov 2016 15:56:19 +0000
Subject: [PATCH 6/9] lib: Replace use of deprecated talloc_destroy

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/libsmb/samlogon_cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/source3/libsmb/samlogon_cache.c b/source3/libsmb/samlogon_cache.c
index ed70038..242fa32 100644
--- a/source3/libsmb/samlogon_cache.c
+++ b/source3/libsmb/samlogon_cache.c
@@ -268,7 +268,7 @@ bool netsamlogon_cache_have(const struct dom_sid *user_sid)
 
 	result = (info3 != NULL);
 
-	talloc_destroy(mem_ctx);
+	TALLOC_FREE(mem_ctx);
 
 	return result;
 }
-- 
2.1.4


>From 59e2cca0b350f054335508869dd77c23e8b29acc Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Nov 2016 15:59:21 +0000
Subject: [PATCH 7/9] lib: Avoid includes.h in samlogon_cache.c

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/libsmb/samlogon_cache.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/source3/libsmb/samlogon_cache.c b/source3/libsmb/samlogon_cache.c
index 242fa32..bc1a8f0 100644
--- a/source3/libsmb/samlogon_cache.c
+++ b/source3/libsmb/samlogon_cache.c
@@ -21,9 +21,13 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "includes.h"
+#include "replace.h"
 #include "samlogon_cache.h"
 #include "system/filesys.h"
+#include "system/time.h"
+#include "lib/util/debug.h"
+#include "lib/util/talloc_stack.h"
+#include "source3/lib/util_path.h"
 #include "librpc/gen_ndr/ndr_krb5pac.h"
 #include "../libcli/security/security.h"
 #include "util_tdb.h"
-- 
2.1.4


>From 9ffde5079b1ace209581870d5c3e07d3a672c8f0 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Nov 2016 16:01:58 +0000
Subject: [PATCH 8/9] lib: Fix a comment

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/libsmb/samlogon_cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/source3/libsmb/samlogon_cache.c b/source3/libsmb/samlogon_cache.c
index bc1a8f0..59ae9f4 100644
--- a/source3/libsmb/samlogon_cache.c
+++ b/source3/libsmb/samlogon_cache.c
@@ -201,7 +201,7 @@ bool netsamlogon_cache_store(const char *username, struct netr_SamInfo3 *info3)
 
 /***********************************************************************
  Retrieves a netr_SamInfo3 structure from a tdb.  Caller must
- free the user_info struct (malloc()'d memory)
+ free the user_info struct (talloced memory)
 ***********************************************************************/
 
 struct netr_SamInfo3 *netsamlogon_cache_get(TALLOC_CTX *mem_ctx, const struct dom_sid *user_sid)
-- 
2.1.4


>From 8542c780651277a5c9d53cb8abb28f93769e7cd9 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Nov 2016 16:11:59 +0000
Subject: [PATCH 9/9] lib: Remove a used-once variable

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/libsmb/samlogon_cache.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/source3/libsmb/samlogon_cache.c b/source3/libsmb/samlogon_cache.c
index 59ae9f4..8131b2b 100644
--- a/source3/libsmb/samlogon_cache.c
+++ b/source3/libsmb/samlogon_cache.c
@@ -126,7 +126,6 @@ bool netsamlogon_cache_store(const char *username, struct netr_SamInfo3 *info3)
 	char keystr[DOM_SID_STR_BUFLEN];
 	bool result = false;
 	struct dom_sid	user_sid;
-	time_t t = time(NULL);
 	TALLOC_CTX *tmp_ctx = talloc_stackframe();
 	DATA_BLOB blob;
 	enum ndr_err_code ndr_err;
@@ -172,7 +171,7 @@ bool netsamlogon_cache_store(const char *username, struct netr_SamInfo3 *info3)
 		info3->base.account_name.string = talloc_strdup(info3, username);
 	}
 
-	r.timestamp = t;
+	r.timestamp = time(NULL);
 	r.info3 = *info3;
 
 	if (DEBUGLEVEL >= 10) {
-- 
2.1.4



More information about the samba-technical mailing list