Samba VFS_WORM module

Volker Lendecke Volker.Lendecke at SerNet.DE
Thu Dec 12 05:30:56 MST 2013


On Fri, Dec 06, 2013 at 09:11:05PM +0100, Marc Muehlfeld wrote:
> Hello,
> 
> the subsidiary firm of my employer ordered a Samba enhancement, that
> will be part of the official sources soon. And I'm sure, that it
> could be interesting for other Samba users, too: A _VFS module_,
> that provides basic _WORM functionality_ (write once read many) on
> top of a Samba share.
> 
> The new module is currently still only in samba-master, but will be
> likely part of the next 4.1 and 4.0 release.
> 
> I've added a HowTo to the Wiki, describing the module and how to use
> it: https://wiki.samba.org/index.php/VFS/vfs_worm

... and here is the patch.

If nobody objects, we would push this to master in a few
days.

With best regards,

Volker Lendecke

-- 
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 b2429e9217d9b3c8dacab18ff596b8e6bc888200 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Wed, 20 Nov 2013 12:00:17 +0100
Subject: [PATCH 1/5] lib-util: add functions to get elapsed from given
 timespec structs

Signed-off-by: Volker Lendecke <vl at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 lib/util/time.c |   18 ++++++++++++++++++
 lib/util/time.h |   10 ++++++++++
 2 files changed, 28 insertions(+)

diff --git a/lib/util/time.c b/lib/util/time.c
index 05251dd..a09490a 100644
--- a/lib/util/time.c
+++ b/lib/util/time.c
@@ -652,6 +652,24 @@ _PUBLIC_ double timeval_elapsed(const struct timeval *tv)
 	struct timeval tv2 = timeval_current();
 	return timeval_elapsed2(tv, &tv2);
 }
+/**
+ *   return the number of seconds elapsed between two times
+ **/
+_PUBLIC_ double timespec_elapsed2(const struct timespec *ts1,
+				const struct timespec *ts2)
+{
+	return (ts2->tv_sec - ts1->tv_sec) +
+	       (ts2->tv_nsec - ts1->tv_nsec)*1.0e-9;
+}
+
+/**
+ *   return the number of seconds elapsed since a given time
+ */
+_PUBLIC_ double timespec_elapsed(const struct timespec *ts)
+{
+	struct timespec ts2 = timespec_current();
+	return timespec_elapsed2(ts, &ts2);
+}
 
 /**
   return the lesser of two timevals
diff --git a/lib/util/time.h b/lib/util/time.h
index 69ba783..b5302f8 100644
--- a/lib/util/time.h
+++ b/lib/util/time.h
@@ -247,6 +247,16 @@ double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2);
 double timeval_elapsed(const struct timeval *tv);
 
 /**
+  return the number of seconds elapsed between two times
+*/
+double timespec_elapsed2(const struct timespec *ts1,
+			 const struct timespec *ts2);
+/**
+  return the number of seconds elapsed since a given time
+*/
+double timespec_elapsed(const struct timespec *ts);
+
+/**
   return the lesser of two timevals
 */
 struct timeval timeval_min(const struct timeval *tv1,
-- 
1.7.9.5


From 06ce0ca032be0cf62e58b93457fd88bf2e3207db Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Wed, 20 Nov 2013 12:09:47 +0100
Subject: [PATCH 2/5] s3-modules: add new vfs_worm module

VFS module to disallow writes for older files.

Signed-off-by: Volker Lendecke <vl at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 source3/modules/vfs_worm.c |   97 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)
 create mode 100644 source3/modules/vfs_worm.c

diff --git a/source3/modules/vfs_worm.c b/source3/modules/vfs_worm.c
new file mode 100644
index 0000000..77a18ca
--- /dev/null
+++ b/source3/modules/vfs_worm.c
@@ -0,0 +1,97 @@
+/*
+ * VFS module to disallow writes for older files
+ *
+ * Copyright (C) 2013, Volker Lendecke
+ *
+ * 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 "smbd/smbd.h"
+#include "system/filesys.h"
+#include "libcli/security/security.h"
+
+static NTSTATUS vfs_worm_create_file(vfs_handle_struct *handle,
+				     struct smb_request *req,
+				     uint16_t root_dir_fid,
+				     struct smb_filename *smb_fname,
+				     uint32_t access_mask,
+				     uint32_t share_access,
+				     uint32_t create_disposition,
+				     uint32_t create_options,
+				     uint32_t file_attributes,
+				     uint32_t oplock_request,
+				     uint64_t allocation_size,
+				     uint32_t private_flags,
+				     struct security_descriptor *sd,
+				     struct ea_list *ea_list,
+				     files_struct **result,
+				     int *pinfo)
+{
+	bool readonly = false;
+	const uint32_t write_access_flags =
+		FILE_WRITE_DATA | FILE_APPEND_DATA |
+		FILE_WRITE_ATTRIBUTES | DELETE_ACCESS |
+		WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS;
+	NTSTATUS status;
+
+	if (VALID_STAT(smb_fname->st)) {
+		double age;
+		age = timespec_elapsed(&smb_fname->st.st_ex_ctime);
+		if (age > lp_parm_int(SNUM(handle->conn), "worm",
+				      "grace_period", 3600)) {
+			readonly = true;
+		}
+	}
+
+	if (readonly && (access_mask & write_access_flags)) {
+		return NT_STATUS_ACCESS_DENIED;
+	}
+
+	status = SMB_VFS_NEXT_CREATE_FILE(
+		handle, req, root_dir_fid, smb_fname, access_mask,
+		share_access, create_disposition, create_options,
+		file_attributes, oplock_request, allocation_size,
+		private_flags, sd, ea_list, result, pinfo);
+	if (!NT_STATUS_IS_OK(status)) {
+		return status;
+	}
+
+	/*
+	 * Access via MAXIMUM_ALLOWED_ACCESS?
+	 */
+	if (readonly && ((*result)->access_mask & write_access_flags)) {
+		close_file(req, *result, NORMAL_CLOSE);
+		return NT_STATUS_ACCESS_DENIED;
+	}
+	return NT_STATUS_OK;
+}
+
+static struct vfs_fn_pointers vfs_worm_fns = {
+	.create_file_fn = vfs_worm_create_file,
+};
+
+NTSTATUS vfs_worm_init(void);
+NTSTATUS vfs_worm_init(void)
+{
+	NTSTATUS ret;
+
+	ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "worm",
+			       &vfs_worm_fns);
+	if (!NT_STATUS_IS_OK(ret)) {
+		return ret;
+	}
+
+	return ret;
+}
-- 
1.7.9.5


From cf444ded1f4de10c280bcf7a6f6c5692cfb10281 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Wed, 20 Nov 2013 12:11:41 +0100
Subject: [PATCH 3/5] s3-waf: build new vfs_worm module

Signed-off-by: Volker Lendecke <vl at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 source3/modules/wscript_build |    8 ++++++++
 source3/wscript               |    1 +
 2 files changed, 9 insertions(+)

diff --git a/source3/modules/wscript_build b/source3/modules/wscript_build
index 32e541f..25c9d5b 100644
--- a/source3/modules/wscript_build
+++ b/source3/modules/wscript_build
@@ -474,3 +474,11 @@ bld.SAMBA3_MODULE('vfs_glusterfs',
                   internal_module=bld.SAMBA3_IS_STATIC_MODULE('vfs_glusterfs'),
                   enabled=bld.SAMBA3_IS_ENABLED_MODULE('vfs_glusterfs'),
                   allow_undefined_symbols=False)
+
+bld.SAMBA3_MODULE('vfs_worm',
+                  subsystem='vfs',
+                  source='vfs_worm.c',
+                  deps='samba-util',
+                  init_function='',
+                  internal_module=bld.SAMBA3_IS_STATIC_MODULE('vfs_worm'),
+                  enabled=bld.SAMBA3_IS_ENABLED_MODULE('vfs_worm'))
diff --git a/source3/wscript b/source3/wscript
index b09c2db..f87a0a8 100644
--- a/source3/wscript
+++ b/source3/wscript
@@ -1786,6 +1786,7 @@ main() {
                                       vfs_smb_traffic_analyzer vfs_preopen vfs_catia vfs_scannedonly
 				      vfs_media_harmony
 				      vfs_commit
+				      vfs_worm
                                       vfs_crossrename vfs_linux_xfs_sgid
                                       vfs_time_audit idmap_autorid idmap_tdb2
                                       idmap_rid idmap_hash idmap_rfc2307'''))
-- 
1.7.9.5


From f49c38463c7ba398302bcd88a2cd862273fae093 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Baumbach?= <bb at sernet.de>
Date: Wed, 20 Nov 2013 13:00:04 +0100
Subject: [PATCH 4/5] docs-man: add manual page for the new worm vfs module
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Bj?Baumbach <bb at sernet.de>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
Reviewed-by: Volker Lendecke <vl at samba.org>
---
 docs-xml/manpages/vfs_worm.8.xml |   93 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)
 create mode 100644 docs-xml/manpages/vfs_worm.8.xml

diff --git a/docs-xml/manpages/vfs_worm.8.xml b/docs-xml/manpages/vfs_worm.8.xml
new file mode 100644
index 0000000..e041d0b
--- /dev/null
+++ b/docs-xml/manpages/vfs_worm.8.xml
@@ -0,0 +1,93 @@
+<?xml version="1.0" encoding="iso-8859-1"?>
+<!DOCTYPE refentry PUBLIC "-//Samba-Team//DTD DocBook V4.2-Based Variant V1.0//EN" "http://www.samba.org/samba/DTD/samba-doc">
+<refentry id="vfs_worm.8">
+
+<refmeta>
+	<refentrytitle>vfs_worm</refentrytitle>
+	<manvolnum>8</manvolnum>
+	<refmiscinfo class="source">Samba</refmiscinfo>
+	<refmiscinfo class="manual">System Administration tools</refmiscinfo>
+	<refmiscinfo class="version">4.1</refmiscinfo>
+</refmeta>
+
+
+<refnamediv>
+	<refname>vfs_worm</refname>
+	<refpurpose>disallows writes for older file</refpurpose>
+</refnamediv>
+
+<refsynopsisdiv>
+	<cmdsynopsis>
+		<command>vfs objects = worm</command>
+	</cmdsynopsis>
+</refsynopsisdiv>
+
+<refsect1>
+	<title>DESCRIPTION</title>
+
+	<para>This VFS module is part of the
+	<citerefentry><refentrytitle>samba</refentrytitle>
+	<manvolnum>7</manvolnum></citerefentry> suite.</para>
+
+	<para>The <command>vfs_worm</command> module controls the writability
+	of files and folders depending on their change time and a
+	adjustable grace period.</para>
+
+	<para>If the change time of a file or directory is older than
+	the specified grace period, the write access will be denied,
+	independent of further access controls (e.g. by the filesystem).</para>
+
+	<para>In the case that the grace period is not exceed, the worm
+	module will not impact any access controls.</para>
+</refsect1>
+
+<refsect1>
+	<title>OPTIONS</title>
+
+	<variablelist>
+
+		<varlistentry>
+		<term>worm:grace_period = SECONDS</term>
+		<listitem>
+		<para>Period in seconds which defines the time how long the
+		write access should be handled by the normal access controls.
+		After this grace period the file or directory become read
+		only.</para>
+		</listitem>
+		</varlistentry>
+
+	</variablelist>
+</refsect1>
+
+<refsect1>
+	<title>EXAMPLES</title>
+
+	<para>Deny the write access to files and folders, which are older
+	than five minutes (300 seconds):</para>
+
+<programlisting>
+	<smbconfsection name="[wormshare]"/>
+	<smbconfoption name="vfs objects">worm</smbconfoption>
+	<smbconfoption name="worm:grace_period">300</smbconfoption>
+</programlisting>
+
+</refsect1>
+
+<refsect1>
+	<title>VERSION</title>
+
+	<para>This man page is correct for version 4.2 of the Samba suite.
+	</para>
+</refsect1>
+
+<refsect1>
+	<title>AUTHOR</title>
+
+	<para>The original Samba software and related utilities
+	were created by Andrew Tridgell. Samba is now developed
+	by the Samba Team as an Open Source project similar
+	to the way the Linux kernel is developed.</para>
+
+</refsect1>
+
+</refentry>
-- 
1.7.9.5


From 000c861cdfe192620a5b188e971401becc916234 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Baumbach?= <bb at sernet.de>
Date: Wed, 20 Nov 2013 14:24:21 +0100
Subject: [PATCH 5/5] waf docs: build the new vfs worm man page
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Bj?Baumbach <bb at sernet.de>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
Reviewed-by: Volker Lendecke <vl at samba.org>
---
 docs-xml/wscript_build |    1 +
 1 file changed, 1 insertion(+)

diff --git a/docs-xml/wscript_build b/docs-xml/wscript_build
index 9c6042f..a752758 100644
--- a/docs-xml/wscript_build
+++ b/docs-xml/wscript_build
@@ -80,6 +80,7 @@ manpages='''
          manpages/vfs_syncops.8
          manpages/vfs_time_audit.8
          manpages/vfs_tsmsm.8
+         manpages/vfs_worm.8
          manpages/vfs_xattr_tdb.8
          manpages/vfstest.1
          manpages/wbinfo.1
-- 
1.7.9.5



More information about the samba-technical mailing list