Patchset to make "struct smb2_lease" available for common use

Volker Lendecke Volker.Lendecke at SerNet.DE
Tue Apr 30 05:53:32 MDT 2013


Hi!

The attached patch moves struct smb2_lease to / and adds
marshalling routines.

Please review & push if ok.

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 097e634193550206cc7420a9e37d64d0c7fca4f2 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Wed, 17 Apr 2013 16:48:21 +0200
Subject: [PATCH 1/4] libsmb: Move "struct smb2_lease" to common

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 libcli/smb/smb2_lease.h         |   43 +++++++++++++++++++++++++++++++++++++++
 libcli/smb/smb_common.h         |    1 +
 libcli/smb/wscript              |    1 +
 source4/libcli/raw/interfaces.h |   17 ----------------
 4 files changed, 45 insertions(+), 17 deletions(-)
 create mode 100644 libcli/smb/smb2_lease.h

diff --git a/libcli/smb/smb2_lease.h b/libcli/smb/smb2_lease.h
new file mode 100644
index 0000000..7fc598b
--- /dev/null
+++ b/libcli/smb/smb2_lease.h
@@ -0,0 +1,43 @@
+/*
+   Unix SMB/CIFS implementation.
+
+   SMB2 Lease context handling
+
+   Copyright (C) Stefan Metzmacher 2012
+   Copyright (C) Volker Lendecke 2013
+
+   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 _LIBCLI_SMB_SMB2_LEASE_H_
+#define _LIBCLI_SMB_SMB2_LEASE_H_
+
+/*
+  SMB2 lease structure (per MS-SMB2 2.2.13)
+*/
+struct smb2_lease_key {
+	uint64_t data[2];
+};
+
+struct smb2_lease {
+	struct smb2_lease_key lease_key;
+	uint32_t lease_state;
+	uint32_t lease_flags;
+	uint64_t lease_duration; /* should be 0 */
+	/* only for v2 */
+	struct smb2_lease_key parent_lease_key;
+	uint16_t lease_epoch;
+};
+
+#endif /* _LIBCLI_SMB_SMB2_LEASE_H_ */
diff --git a/libcli/smb/smb_common.h b/libcli/smb/smb_common.h
index 47a336a..2f66b0a 100644
--- a/libcli/smb/smb_common.h
+++ b/libcli/smb/smb_common.h
@@ -25,6 +25,7 @@
 #include "libcli/smb/smb_constants.h"
 #include "libcli/smb/smb2_constants.h"
 #include "libcli/smb/smb2_create_blob.h"
+#include "libcli/smb/smb2_lease.h"
 #include "libcli/smb/smb2_signing.h"
 #include "libcli/smb/smb_util.h"
 #include "libcli/smb/smb_unix_ext.h"
diff --git a/libcli/smb/wscript b/libcli/smb/wscript
index 3616ad1..13cf894 100755
--- a/libcli/smb/wscript
+++ b/libcli/smb/wscript
@@ -41,6 +41,7 @@ def build(bld):
 		smb_common.h smb2_constants.h smb_constants.h
 		smb_signing.h smb_seal.h
 		smb2_create_blob.h smb2_signing.h
+		smb2_lease.h
 		smb_util.h
 		smb_unix_ext.h
 	''',
diff --git a/source4/libcli/raw/interfaces.h b/source4/libcli/raw/interfaces.h
index fb73f26..7bc79ca 100644
--- a/source4/libcli/raw/interfaces.h
+++ b/source4/libcli/raw/interfaces.h
@@ -54,23 +54,6 @@ struct smb2_handle {
 	uint64_t data[2];
 };
 
-/*
-  SMB2 lease structure (per MS-SMB2 2.2.13)
-*/
-struct smb2_lease_key {
-	uint64_t data[2];
-};
-
-struct smb2_lease {
-	struct smb2_lease_key lease_key;
-	uint32_t lease_state;
-	uint32_t lease_flags;
-	uint64_t lease_duration; /* should be 0 */
-	/* only for v2 */
-	struct smb2_lease_key parent_lease_key;
-	uint16_t lease_epoch;
-};
-
 struct smb2_lease_break {
 	struct smb2_lease current_lease;
 	uint32_t break_flags;
-- 
1.7.9.5


From e38ff4d4b7492d5ce908ab6176d914d8319af0c5 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Wed, 17 Apr 2013 17:04:38 +0200
Subject: [PATCH 2/4] libcli: Add smb2_lease marshalling

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 libcli/smb/smb2_lease.c |   86 +++++++++++++++++++++++++++++++++++++++++++++++
 libcli/smb/smb2_lease.h |    7 ++++
 libcli/smb/wscript      |    1 +
 3 files changed, 94 insertions(+)
 create mode 100644 libcli/smb/smb2_lease.c

diff --git a/libcli/smb/smb2_lease.c b/libcli/smb/smb2_lease.c
new file mode 100644
index 0000000..10beaca
--- /dev/null
+++ b/libcli/smb/smb2_lease.c
@@ -0,0 +1,86 @@
+/*
+   Unix SMB/CIFS implementation.
+
+   SMB2 Lease context handling
+
+   Copyright (C) Stefan Metzmacher 2012
+   Copyright (C) Volker Lendecke 2013
+
+   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/>.
+*/
+
+#include "includes.h"
+#include "../libcli/smb/smb_common.h"
+
+ssize_t smb2_lease_pull(uint8_t *buf, size_t len, struct smb2_lease *lease)
+{
+	int version;
+
+	switch (len) {
+	case 32:
+		version = 1;
+		break;
+	case 52:
+		version = 2;
+		break;
+	default:
+		return -1;
+	}
+
+	memcpy(&lease->lease_key, buf, 16);
+	lease->lease_state = IVAL(buf, 16);
+	lease->lease_flags = IVAL(buf, 20);
+	lease->lease_duration = BVAL(buf, 24);
+
+	switch (version) {
+	case 1:
+		memcpy(&lease->parent_lease_key, buf+32, 16);
+		lease->lease_epoch = SVAL(buf, 48);
+		break;
+	case 2:
+		ZERO_STRUCT(lease->parent_lease_key);
+		lease->lease_epoch = 0;
+		break;
+	}
+
+	return len;
+}
+
+bool smb2_lease_push(const struct smb2_lease *lease, uint8_t *buf, size_t len)
+{
+	int version;
+
+	switch (len) {
+	case 32:
+		version = 1;
+		break;
+	case 52:
+		version = 2;
+		break;
+	default:
+		return false;
+	}
+
+	memcpy(&buf[0], &lease->lease_key, 16);
+	SIVAL(buf, 16, lease->lease_state);
+	SIVAL(buf, 20, lease->lease_flags);
+	SBVAL(buf, 24, lease->lease_duration);
+
+	if (version == 2) {
+		memcpy(&buf[32], &lease->parent_lease_key, 16);
+		SIVAL(buf, 48, lease->lease_epoch);
+	}
+
+	return true;
+}
diff --git a/libcli/smb/smb2_lease.h b/libcli/smb/smb2_lease.h
index 7fc598b..fa8e7af 100644
--- a/libcli/smb/smb2_lease.h
+++ b/libcli/smb/smb2_lease.h
@@ -40,4 +40,11 @@ struct smb2_lease {
 	uint16_t lease_epoch;
 };
 
+/*
+ * Parse a smb2 lease create context. Return -1 on error, buffer.length on
+ * success. V1 and V2 differ only by length of buffer.length
+ */
+ssize_t smb2_lease_pull(uint8_t *buf, size_t len, struct smb2_lease *lease);
+bool smb2_lease_push(const struct smb2_lease *lease, uint8_t *buf, size_t len);
+
 #endif /* _LIBCLI_SMB_SMB2_LEASE_H_ */
diff --git a/libcli/smb/wscript b/libcli/smb/wscript
index 13cf894..544c43e 100755
--- a/libcli/smb/wscript
+++ b/libcli/smb/wscript
@@ -18,6 +18,7 @@ def build(bld):
 	source='''
 		smb_signing.c smb_seal.c
 		smb2_create_blob.c smb2_signing.c
+                smb2_lease.c
 		util.c
 		smbXcli_base.c
 		smb1cli_trans.c
-- 
1.7.9.5


From 29b29ee01bd24e91d18610228154fca58aa99259 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 25 Apr 2013 14:19:36 +0200
Subject: [PATCH 3/4] libsmb: Use smb2_lease_push in smb2_create_send

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source4/libcli/smb2/create.c |   22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/source4/libcli/smb2/create.c b/source4/libcli/smb2/create.c
index 7267f92..552ac93 100644
--- a/source4/libcli/smb2/create.c
+++ b/source4/libcli/smb2/create.c
@@ -212,10 +212,11 @@ struct smb2_request *smb2_create_send(struct smb2_tree *tree, struct smb2_create
 	if (io->in.lease_request) {
 		uint8_t data[32];
 
-		memcpy(&data[0], &io->in.lease_request->lease_key, 16);
-		SIVAL(data, 16, io->in.lease_request->lease_state);
-		SIVAL(data, 20, io->in.lease_request->lease_flags);
-		SBVAL(data, 24, io->in.lease_request->lease_duration);
+		if (!smb2_lease_push(io->in.lease_request, data,
+				     sizeof(data))) {
+			TALLOC_FREE(req);
+			return NULL;
+		}
 
 		status = smb2_create_blob_add(req, &blobs,
 					      SMB2_CREATE_TAG_RQLS,
@@ -227,16 +228,13 @@ struct smb2_request *smb2_create_send(struct smb2_tree *tree, struct smb2_create
 	}
 
 	if (io->in.lease_request_v2) {
-		struct smb2_lease *ls = io->in.lease_request_v2;
 		uint8_t data[52];
 
-		memcpy(&data[0], &ls->lease_key, 16);
-		SIVAL(data, 16, ls->lease_state);
-		SIVAL(data, 20, ls->lease_flags);
-		SBVAL(data, 24, ls->lease_duration);
-		memcpy(&data[32], &ls->parent_lease_key, 16);
-		SSVAL(data, 48, ls->lease_epoch);
-		SSVAL(data, 50, 0); /* reserved */
+		if (!smb2_lease_push(io->in.lease_request_v2, data,
+				     sizeof(data))) {
+			TALLOC_FREE(req);
+			return NULL;
+		}
 
 		status = smb2_create_blob_add(req, &blobs,
 					      SMB2_CREATE_TAG_RQLS,
-- 
1.7.9.5


From 18462c8d309ac9d810982fa856e748214fb79ffb Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Thu, 25 Apr 2013 14:24:08 +0200
Subject: [PATCH 4/4] libsmb: Use sizeof instead of explicit numbers

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

diff --git a/source4/libcli/smb2/create.c b/source4/libcli/smb2/create.c
index 552ac93..1a7f02b 100644
--- a/source4/libcli/smb2/create.c
+++ b/source4/libcli/smb2/create.c
@@ -218,9 +218,9 @@ struct smb2_request *smb2_create_send(struct smb2_tree *tree, struct smb2_create
 			return NULL;
 		}
 
-		status = smb2_create_blob_add(req, &blobs,
-					      SMB2_CREATE_TAG_RQLS,
-					      data_blob_const(data, 32));
+		status = smb2_create_blob_add(
+			req, &blobs, SMB2_CREATE_TAG_RQLS,
+			data_blob_const(data, sizeof(data)));
 		if (!NT_STATUS_IS_OK(status)) {
 			talloc_free(req);
 			return NULL;
@@ -236,9 +236,9 @@ struct smb2_request *smb2_create_send(struct smb2_tree *tree, struct smb2_create
 			return NULL;
 		}
 
-		status = smb2_create_blob_add(req, &blobs,
-					      SMB2_CREATE_TAG_RQLS,
-					      data_blob_const(data, 52));
+		status = smb2_create_blob_add(
+			req, &blobs, SMB2_CREATE_TAG_RQLS,
+			data_blob_const(data, sizeof(data)));
 		if (!NT_STATUS_IS_OK(status)) {
 			talloc_free(req);
 			return NULL;
-- 
1.7.9.5



More information about the samba-technical mailing list