smbget's -P option

Christian Ambach ambi at samba.org
Tue Feb 23 11:45:40 UTC 2016


Hi Andreas,

Am 22.02.16 um 09:06 schrieb Andreas Schneider:
>> Debian and Ubuntu ship with a version of popt that fixes the problem we
>> are hitting in smbget (see
>> https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=217602).
>> Patch 7 in the patchset applies the same change to our copy of popt.
>> While looking into this, I found another problem with the option parsing
>> of smbget that I also included a patch for.
>> So some patches in the set need review, the others are included for
>> completeness and so they can all be pushed together.
>
> What do you think about the attached patch?

It broke option parsing for me, I then got the same error that you got 
without the patch.
So I would give up on making popt work with bool, especially as we 
cannot make sure that the popt libs on the systems we compile on will 
work properly. I would need to add a configure check to find out if its 
good or not.
So let's rather spend the few more entries in the switch statement and 
set the options there.

Regards,
Christian


-------------- next part --------------
From 567afb20e5459d74cde6ec766c75401962fb09a7 Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Mon, 8 Feb 2016 23:24:36 +0100
Subject: [PATCH 01/10] s3:utils/smbget another int -> bool conversion

Signed-off-by: Christian Ambach <ambi at samba.org>
Reviewed-by: Andreas Schneider <asn at samba.org>
---
 source3/utils/smbget.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c
index 91809d1..24f26b0 100644
--- a/source3/utils/smbget.c
+++ b/source3/utils/smbget.c
@@ -163,9 +163,7 @@ static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen,
 	}
 }
 
-/* Return 1 on error, 0 on success. */
-
-static int smb_download_dir(const char *base, const char *name, int resume)
+static bool smb_download_dir(const char *base, const char *name, int resume)
 {
 	char path[SMB_MAXPATHLEN];
 	int dirhandle;
@@ -173,7 +171,7 @@ static int smb_download_dir(const char *base, const char *name, int resume)
 	const char *relname = name;
 	char *tmpname;
 	struct stat remotestat;
-	int ret = 0;
+	bool ok = false;
 
 	snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
 		 (base[0] && name[0] && name[0] != '/' &&
@@ -189,7 +187,7 @@ static int smb_download_dir(const char *base, const char *name, int resume)
 		}
 		fprintf(stderr, "Can't open directory %s: %s\n", path,
 			strerror(errno));
-		return 1;
+		return false;
 	}
 
 	while (*relname == '/') {
@@ -206,28 +204,28 @@ static int smb_download_dir(const char *base, const char *name, int resume)
 		}
 		if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
 			free(tmpname);
-			return 1;
+			return false;
 		}
 		switch (dirent->smbc_type) {
 		case SMBC_DIR:
-			ret = smb_download_dir(base, newname, resume);
+			ok = smb_download_dir(base, newname, resume);
 			break;
 
 		case SMBC_WORKGROUP:
-			ret = smb_download_dir("smb://", dirent->name, resume);
+			ok = smb_download_dir("smb://", dirent->name, resume);
 			break;
 
 		case SMBC_SERVER:
-			ret = smb_download_dir("smb://", dirent->name, resume);
+			ok = smb_download_dir("smb://", dirent->name, resume);
 			break;
 
 		case SMBC_FILE:
-			ret = smb_download_file(base, newname, true, resume,
+			ok = smb_download_file(base, newname, true, resume,
 						false, NULL);
 			break;
 
 		case SMBC_FILE_SHARE:
-			ret = smb_download_dir(base, newname, resume);
+			ok = smb_download_dir(base, newname, resume);
 			break;
 
 		case SMBC_PRINTER_SHARE:
@@ -266,7 +264,7 @@ static int smb_download_dir(const char *base, const char *name, int resume)
 				"Unable to get stats on %s on remote server\n",
 				path);
 			smbc_closedir(dirhandle);
-			return 1;
+			return false;
 		}
 
 		if (chmod(relname, remotestat.st_mode) < 0) {
@@ -274,12 +272,12 @@ static int smb_download_dir(const char *base, const char *name, int resume)
 				"Unable to change mode of local dir %s to %o\n",
 				relname, (unsigned int)remotestat.st_mode);
 			smbc_closedir(dirhandle);
-			return 1;
+			return false;
 		}
 	}
 
 	smbc_closedir(dirhandle);
-	return ret;
+	return ok;
 }
 
 static char *print_time(long t)
-- 
1.9.1


From 82a45cc6d956068ff799dda78ae792e2c54f6d53 Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Mon, 8 Feb 2016 23:25:04 +0100
Subject: [PATCH 02/10] s3:utils/smbget abort recursive download on error

Signed-off-by: Christian Ambach <ambi at samba.org>
Reviewed-by: Andreas Schneider <asn at samba.org>
---
 source3/utils/smbget.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c
index 24f26b0..7957417 100644
--- a/source3/utils/smbget.c
+++ b/source3/utils/smbget.c
@@ -254,6 +254,12 @@ static bool smb_download_dir(const char *base, const char *name, int resume)
 				newname, dirent->smbc_type);
 			break;
 		}
+
+		if (!ok) {
+			fprintf(stderr, "Failed to download %s: %s\n",
+				newname, strerror(errno));
+			return false;
+		}
 		free(newname);
 	}
 	free(tmpname);
-- 
1.9.1


From ff2a3cfef9c4a8d8c56917a5675f0336c86a7d62 Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Mon, 8 Feb 2016 23:27:09 +0100
Subject: [PATCH 03/10] s3:utils/smbget improve check of write() result

check that all bytes in the buffer have been written

Signed-off-by: Christian Ambach <ambi at samba.org>
Reviewed-by: Andreas Schneider <asn at samba.org>
---
 source3/utils/smbget.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c
index 7957417..998761f 100644
--- a/source3/utils/smbget.c
+++ b/source3/utils/smbget.c
@@ -600,9 +600,10 @@ static bool smb_download_file(const char *base, const char *name,
 	/* Now, download all bytes from offset_download to the end */
 	for (curpos = offset_download; curpos < remotestat.st_size;
 	     curpos += opt.blocksize) {
-		ssize_t bytesread = smbc_read(remotehandle,
-					      readbuf,
-					      opt.blocksize);
+		ssize_t bytesread;
+		ssize_t byteswritten;
+
+		bytesread = smbc_read(remotehandle, readbuf, opt.blocksize);
 		if(bytesread < 0) {
 			fprintf(stderr,
 				"Can't read %zu bytes at offset %jd, file %s\n",
@@ -617,7 +618,8 @@ static bool smb_download_file(const char *base, const char *name,
 
 		total_bytes += bytesread;
 
-		if(write(localhandle, readbuf, bytesread) < 0) {
+		byteswritten = write(localhandle, readbuf, bytesread);
+		if (byteswritten != bytesread) {
 			fprintf(stderr,
 				"Can't write %zd bytes to local file %s at "
 				"offset %jd\n", bytesread, path,
-- 
1.9.1


From 272ab8006d78ac64ce0e093396d8f7fb67e14b97 Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Mon, 15 Feb 2016 22:25:11 +0100
Subject: [PATCH 04/10] s3:utils/smbget remove -P option

as agreed on samba-technical list.
It does not really provide a useful function but can cause confusion

Signed-off-by: Christian Ambach <ambi at samba.org>
Reviewed-by: Andreas Schneider <asn at samba.org>
---
 source3/utils/smbget.c | 32 --------------------------------
 1 file changed, 32 deletions(-)

diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c
index 998761f..5ee73db 100644
--- a/source3/utils/smbget.c
+++ b/source3/utils/smbget.c
@@ -51,7 +51,6 @@ struct opt {
 	bool nonprompt;
 	bool quiet;
 	bool dots;
-	bool keep_permissions;
 	bool verbose;
 	bool send_stdout;
 	bool update;
@@ -170,7 +169,6 @@ static bool smb_download_dir(const char *base, const char *name, int resume)
 	struct smbc_dirent *dirent;
 	const char *relname = name;
 	char *tmpname;
-	struct stat remotestat;
 	bool ok = false;
 
 	snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
@@ -264,24 +262,6 @@ static bool smb_download_dir(const char *base, const char *name, int resume)
 	}
 	free(tmpname);
 
-	if (opt.keep_permissions) {
-		if (smbc_fstat(dirhandle, &remotestat) < 0) {
-			fprintf(stderr,
-				"Unable to get stats on %s on remote server\n",
-				path);
-			smbc_closedir(dirhandle);
-			return false;
-		}
-
-		if (chmod(relname, remotestat.st_mode) < 0) {
-			fprintf(stderr,
-				"Unable to change mode of local dir %s to %o\n",
-				relname, (unsigned int)remotestat.st_mode);
-			smbc_closedir(dirhandle);
-			return false;
-		}
-	}
-
 	smbc_closedir(dirhandle);
 	return ok;
 }
@@ -657,17 +637,6 @@ static bool smb_download_file(const char *base, const char *name,
 		fputc('\n', stderr);
 	}
 
-	if (opt.keep_permissions && !opt.send_stdout) {
-		if (fchmod(localhandle, remotestat.st_mode) < 0) {
-			fprintf(stderr, "Unable to change mode of local "
-				"file %s to %o\n",
-				path, (unsigned int)remotestat.st_mode);
-			smbc_close(remotehandle);
-			close(localhandle);
-			return false;
-		}
-	}
-
 	smbc_close(remotehandle);
 	if (localhandle != STDOUT_FILENO) {
 		close(localhandle);
@@ -802,7 +771,6 @@ int main(int argc, char **argv)
 		{"resume",     'r', POPT_ARG_NONE,   &resume,           0,  "Automatically resume aborted files" },
 		{"update",     'u', POPT_ARG_NONE,   &opt.update,       0,  "Download only when remote file is newer than local file or local file is missing"},
 		{"recursive",  'R', POPT_ARG_NONE,   &recursive,        0,  "Recursively download files" },
-		{"keep-permissions", 'P', POPT_ARG_NONE, &opt.keep_permissions, 'P', "Keep permissions" },
 		{"blocksize",  'b', POPT_ARG_INT,    &opt.blocksize,   'b', "Change number of bytes in a block"},
 
 		{"outputfile", 'o', POPT_ARG_STRING, &opt.outputfile,  'o', "Write downloaded data to specified file" },
-- 
1.9.1


From 5f60c3a571f0ce7d03dc5239476ad766154e1619 Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Wed, 17 Feb 2016 19:25:59 +0100
Subject: [PATCH 05/10] s3:utils/smbget update manpage with -P option removal

Signed-off-by: Christian Ambach <ambi at samba.org>
Reviewed-by: Andreas Schneider <asn at samba.org>
---
 docs-xml/manpages/smbget.1.xml | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/docs-xml/manpages/smbget.1.xml b/docs-xml/manpages/smbget.1.xml
index 59e2ffe..d77cb8e 100644
--- a/docs-xml/manpages/smbget.1.xml
+++ b/docs-xml/manpages/smbget.1.xml
@@ -27,7 +27,6 @@
 		<arg choice="opt">-n, --nonprompt</arg>
 		<arg choice="opt">-d, --debuglevel=INT</arg>
 		<arg choice="opt">-D, --dots</arg>
-		<arg choice="opt">-P, --keep-permissions</arg>
 		<arg choice="opt">-o, --outputfile</arg>
 		<arg choice="opt">-f, --rcfile</arg>
 		<arg choice="opt">-q, --quiet</arg>
@@ -102,11 +101,6 @@
 	</varlistentry>
 
 	<varlistentry>
-		<term>-P, --keep-permissions</term>
-		<listitem><para>Set same permissions on local file as are set on remote file.</para></listitem>
-	</varlistentry>
-
-	<varlistentry>
 		<term>-o, --outputfile</term>
 		<listitem><para>Write the file that is being downloaded to the specified file. Can not be used together with -R.</para></listitem>
 	</varlistentry>
-- 
1.9.1


From d0399ab43106211d9eecc8e0c2a1e0acbae02c0b Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Wed, 17 Feb 2016 22:57:59 +0100
Subject: [PATCH 06/10] WHATSNEW: document removal of -P in smbget

Signed-off-by: Christian Ambach <ambi at samba.org>
Reviewed-by: Andreas Schneider <asn at samba.org>
---
 WHATSNEW.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/WHATSNEW.txt b/WHATSNEW.txt
index 7c748c2..d81e1ed6 100644
--- a/WHATSNEW.txt
+++ b/WHATSNEW.txt
@@ -71,6 +71,7 @@ The -u and -p options for user and password were replaced by the -U option that
 accepts username[%password] as in many other tools of the Samba suite.
 Similary, smbgetrc files do not accept username and password options any more,
 only a single "user" option which also accepts user%password combinations.
+The -P option was removed.
 
 s4-rpc_server
 -------------
-- 
1.9.1


From 1a6cb65aa0996db3ecb17cc566ef5f995db6552e Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Sat, 20 Feb 2016 21:11:51 +0100
Subject: [PATCH 07/10] s3:utils/smbget fix option parsing

* use proper values for val in poptOption
* popt does not support bool, so set them via the switch statement
* abort when option parsing reported errors

Signed-off-by: Christian Ambach <ambi at samba.org>
---
 source3/utils/smbget.c | 49 ++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 40 insertions(+), 9 deletions(-)

diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c
index 5ee73db..4909fa2 100644
--- a/source3/utils/smbget.c
+++ b/source3/utils/smbget.c
@@ -764,20 +764,20 @@ int main(int argc, char **argv)
 		{"user",       'U', POPT_ARG_STRING, &opt.username,    'U', "Username to use" },
 		{"guest",      'a', POPT_ARG_NONE,   NULL,             'a', "Work as user guest" },
 
-		{"nonprompt",  'n', POPT_ARG_NONE,   &opt.nonprompt,   'n', "Don't ask anything (non-interactive)" },
+		{"nonprompt",  'n', POPT_ARG_NONE,   NULL,             'n',  "Don't ask anything (non-interactive)" },
 		{"debuglevel", 'd', POPT_ARG_INT,    &opt.debuglevel,  'd', "Debuglevel to use" },
 
 		{"encrypt",    'e', POPT_ARG_NONE,   NULL,             'e', "Encrypt SMB transport" },
-		{"resume",     'r', POPT_ARG_NONE,   &resume,           0,  "Automatically resume aborted files" },
-		{"update",     'u', POPT_ARG_NONE,   &opt.update,       0,  "Download only when remote file is newer than local file or local file is missing"},
-		{"recursive",  'R', POPT_ARG_NONE,   &recursive,        0,  "Recursively download files" },
+		{"resume",     'r', POPT_ARG_NONE,   NULL,             'r',  "Automatically resume aborted files" },
+		{"update",     'u', POPT_ARG_NONE,   NULL,             'u',  "Download only when remote file is newer than local file or local file is missing"},
+		{"recursive",  'R', POPT_ARG_NONE,   NULL,             'R',  "Recursively download files" },
 		{"blocksize",  'b', POPT_ARG_INT,    &opt.blocksize,   'b', "Change number of bytes in a block"},
 
 		{"outputfile", 'o', POPT_ARG_STRING, &opt.outputfile,  'o', "Write downloaded data to specified file" },
-		{"stdout",     'O', POPT_ARG_NONE,   &opt.send_stdout, 'O', "Write data to stdout" },
-		{"dots",       'D', POPT_ARG_NONE,   &opt.dots,        'D', "Show dots as progress indication" },
-		{"quiet",      'q', POPT_ARG_NONE,   &opt.quiet,       'q', "Be quiet" },
-		{"verbose",    'v', POPT_ARG_NONE,   &opt.verbose,     'v', "Be verbose" },
+		{"stdout",     'O', POPT_ARG_NONE,   NULL,             'O',  "Write data to stdout" },
+		{"dots",       'D', POPT_ARG_NONE,   NULL,             'D',  "Show dots as progress indication" },
+		{"quiet",      'q', POPT_ARG_NONE,   NULL,             'q',  "Be quiet" },
+		{"verbose",    'v', POPT_ARG_NONE,   NULL,             'v',  "Be verbose" },
 		{"rcfile",     'f', POPT_ARG_STRING, NULL,             'f', "Use specified rc file"},
 
 		POPT_TABLEEND
@@ -803,7 +803,7 @@ int main(int argc, char **argv)
 
 	pc = poptGetContext(argv[0], argc, argv_const, long_options, 0);
 
-	while ((c = poptGetNextOpt(pc)) >= 0) {
+	while ((c = poptGetNextOpt(pc)) > 0) {
 		switch (c) {
 		case 'f':
 			readrcfile(poptGetOptArg(pc), long_options);
@@ -827,9 +827,40 @@ int main(int argc, char **argv)
 				opt.password_specified = true;
 			}
 			break;
+		case 'n':
+			opt.nonprompt = true;
+			break;
+		case 'r':
+			resume = true;
+			break;
+		case 'u':
+			opt.update = true;
+			break;
+		case 'R':
+			recursive = true;
+			break;
+		case 'O':
+			opt.send_stdout = true;
+			break;
+		case 'D':
+			opt.dots = true;
+			break;
+		case 'q':
+			opt.quiet = true;
+			break;
+		case 'v':
+			opt.verbose = true;
+			break;
 		}
 	}
 
+	if (c < -1) {
+		fprintf(stderr, "%s: %s\n",
+			poptBadOption(pc, POPT_BADOPTION_NOALIAS),
+			poptStrerror(c));
+		return 1;
+	}
+
 	if ((opt.send_stdout || resume || opt.outputfile) && opt.update) {
 		fprintf(stderr, "The -o, -R or -O and -U options can not be "
 			"used together.\n");
-- 
1.9.1


From 3ab1ac625425d974469bafeff837c32fbbd8445a Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Thu, 18 Feb 2016 00:48:04 +0100
Subject: [PATCH 08/10] selftest: reduce code duplication

factor out a createuser sub

Signed-off-by: Christian Ambach <ambi at samba.org>
Reviewed-by: Andreas Schneider <asn at samba.org>
---
 selftest/target/Samba3.pm | 41 ++++++++++++++++++-----------------------
 1 file changed, 18 insertions(+), 23 deletions(-)

diff --git a/selftest/target/Samba3.pm b/selftest/target/Samba3.pm
index cb9ee08..f3d4454 100755
--- a/selftest/target/Samba3.pm
+++ b/selftest/target/Samba3.pm
@@ -1061,6 +1061,22 @@ sub check_or_start($$$$$) {
 	return $self->wait_for_start($env_vars, $nmbd, $winbindd, $smbd);
 }
 
+sub createuser($$$$)
+{
+	my ($self, $username, $password, $conffile) = @_;
+	my $cmd = "UID_WRAPPER_ROOT=1 " . Samba::bindir_path($self, "smbpasswd")." -c $conffile -L -s -a $username > /dev/null";
+	unless (open(PWD, "|$cmd")) {
+	    warn("Unable to set password for $username account\n$cmd");
+	    return undef;
+	}
+	print PWD "$password\n$password\n";
+	unless (close(PWD)) {
+	    warn("Unable to set password for $username account\n$cmd");
+	    return undef;
+	}
+	print "DONE\n";
+}
+
 sub provision($$$$$$$$)
 {
 	my ($self, $prefix, $server, $password, $extra_options, $dc_server_ip, $dc_server_ipv6, $no_delete_prefix) = @_;
@@ -1752,29 +1768,8 @@ force_user:x:$gid_force_user:
 		$ENV{RESOLV_WRAPPER_HOSTS} = $dns_host_file;
 	}
 
-        my $cmd = "UID_WRAPPER_ROOT=1 " . Samba::bindir_path($self, "smbpasswd")." -c $conffile -L -s -a $unix_name > /dev/null";
-	unless (open(PWD, "|$cmd")) {
-             warn("Unable to set password for test account\n$cmd");
-             return undef;
-        }
-	print PWD "$password\n$password\n";
-	unless (close(PWD)) {
-             warn("Unable to set password for test account\n$cmd");
-             return undef; 
-        }
-
-	# Add another user named: force_user
-        my $cmd = "UID_WRAPPER_ROOT=1 " . Samba::bindir_path($self, "smbpasswd")." -c $conffile -L -s -a force_user > /dev/null";
-	unless (open(PWD, "|$cmd")) {
-             warn("Unable to set password for test account force_user\n$cmd");
-             return undef;
-        }
-	print PWD "$password\n$password\n";
-	unless (close(PWD)) {
-             warn("Unable to set password for test account force_user\n$cmd");
-             return undef;
-        }
-	print "DONE\n";
+	createuser($self, $unix_name, $password, $conffile) || die("Unable to create user");
+	createuser($self, "force_user", $password, $conffile) || die("Unable to create force_user");
 
 	open(DNS_UPDATE_LIST, ">$prefix/dns_update_list") or die("Unable to open $$prefix/dns_update_list");
 	print DNS_UPDATE_LIST "A $server. $server_ip\n";
-- 
1.9.1


From a92828a44fcd8af5af2a698dc56b469778f4caf2 Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Thu, 4 Feb 2016 21:41:08 +0100
Subject: [PATCH 09/10] selftest: add a helper for the smbget binary

Signed-off-by: Christian Ambach <ambi at samba.org>
Reviewed-by: Andreas Schneider <asn at samba.org>
---
 selftest/selftesthelpers.py | 1 +
 1 file changed, 1 insertion(+)

diff --git a/selftest/selftesthelpers.py b/selftest/selftesthelpers.py
index a5ab3f9..42499b0 100644
--- a/selftest/selftesthelpers.py
+++ b/selftest/selftesthelpers.py
@@ -184,3 +184,4 @@ wbinfo = binpath('wbinfo')
 dbwrap_tool = binpath('dbwrap_tool')
 vfstest = binpath('vfstest')
 smbcquotas = binpath('smbcquotas')
+smbget = binpath('smbget')
-- 
1.9.1


From 7abef29dbfa6f799ff8c897d964cdbfe4623a499 Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Sat, 6 Feb 2016 10:30:29 +0100
Subject: [PATCH 10/10] selftest: Add a blackbox test for smbget

Signed-off-by: Christian Ambach <ambi at samba.org>
Reviewed-by: Andreas Schneider <asn at samba.org>
---
 selftest/target/Samba3.pm           |  14 ++-
 source3/script/tests/test_smbget.sh | 236 ++++++++++++++++++++++++++++++++++++
 source3/selftest/tests.py           |   1 +
 3 files changed, 250 insertions(+), 1 deletion(-)
 create mode 100755 source3/script/tests/test_smbget.sh

diff --git a/selftest/target/Samba3.pm b/selftest/target/Samba3.pm
index f3d4454..2dde4ca 100755
--- a/selftest/target/Samba3.pm
+++ b/selftest/target/Samba3.pm
@@ -606,6 +606,9 @@ sub setup_fileserver($$)
 	my $force_user_valid_users_dir = "$share_dir/force_user_valid_users";
 	push(@dirs, $force_user_valid_users_dir);
 
+	my $smbget_sharedir="$share_dir/smbget";
+	push(@dirs,$smbget_sharedir);
+
 	my $fileserver_options = "
 [lowercase]
 	path = $lower_case_share_dir
@@ -641,7 +644,12 @@ sub setup_fileserver($$)
 	force user = force_user
 	force group = everyone
 	write list = force_user
-	";
+
+[smbget]
+	path = $smbget_sharedir
+	comment = smb username is [%U]
+	guest ok = yes
+";
 
 	my $vars = $self->provision($path,
 				    "FILESERVER",
@@ -1299,6 +1307,7 @@ sub provision($$$$$$$$)
 	my ($max_uid, $max_gid);
 	my ($uid_nobody, $uid_root, $uid_pdbtest, $uid_pdbtest2, $uid_userdup);
 	my ($uid_pdbtest_wkn);
+	my ($uid_smbget);
 	my ($uid_force_user);
 	my ($gid_nobody, $gid_nogroup, $gid_root, $gid_domusers, $gid_domadmins);
 	my ($gid_userdup, $gid_everyone);
@@ -1317,6 +1326,7 @@ sub provision($$$$$$$$)
 	$uid_userdup = $max_uid - 5;
 	$uid_pdbtest_wkn = $max_uid - 6;
 	$uid_force_user = $max_uid - 7;
+	$uid_smbget = $max_uid - 8;
 
 	if ($unix_gids[0] < 0xffff - 8) {
 		$max_gid = 0xffff;
@@ -1697,6 +1707,7 @@ pdbtest2:x:$uid_pdbtest2:$gid_nogroup:pdbtest gecos:$prefix_abs:/bin/false
 userdup:x:$uid_userdup:$gid_userdup:userdup gecos:$prefix_abs:/bin/false
 pdbtest_wkn:x:$uid_pdbtest_wkn:$gid_everyone:pdbtest_wkn gecos:$prefix_abs:/bin/false
 force_user:x:$uid_force_user:$gid_force_user:force user gecos:$prefix_abs:/bin/false
+smbget_user:x:$uid_smbget:$gid_domusers:smbget_user gecos:$prefix_abs:/bin/false
 ";
 	if ($unix_uid != 0) {
 		print PASSWD "root:x:$uid_root:$gid_root:root gecos:$prefix_abs:/bin/false
@@ -1770,6 +1781,7 @@ force_user:x:$gid_force_user:
 
 	createuser($self, $unix_name, $password, $conffile) || die("Unable to create user");
 	createuser($self, "force_user", $password, $conffile) || die("Unable to create force_user");
+	createuser($self, "smbget_user", $password, $conffile) || die("Unable to create smbget_user");
 
 	open(DNS_UPDATE_LIST, ">$prefix/dns_update_list") or die("Unable to open $$prefix/dns_update_list");
 	print DNS_UPDATE_LIST "A $server. $server_ip\n";
diff --git a/source3/script/tests/test_smbget.sh b/source3/script/tests/test_smbget.sh
new file mode 100755
index 0000000..f21a131
--- /dev/null
+++ b/source3/script/tests/test_smbget.sh
@@ -0,0 +1,236 @@
+#!/bin/bash
+#
+# Blackbox test for smbget.
+#
+
+if [ $# -lt 7 ]; then
+cat <<EOF
+Usage: test_smbget SERVER SERVER_IP DOMAIN USERNAME PASSWORD WORKDIR SMBGET
+EOF
+exit 1;
+fi
+
+SERVER=${1}
+SERVER_IP=${2}
+DOMAIN=${3}
+USERNAME=${4}
+PASSWORD=${5}
+WORKDIR=${6}
+SMBGET="$VALGRIND ${7}"
+
+TMPDIR="$SRCDIR_ABS/st/tmp"
+
+incdir=`dirname $0`/../../../testprogs/blackbox
+. $incdir/subunit.sh
+
+create_test_data()
+{
+	pushd $WORKDIR
+	dd if=/dev/urandom bs=1024 count=128 of=testfile
+	chmod 644 testfile
+	mkdir dir1
+	dd if=/dev/urandom bs=1024 count=128 of=dir1/testfile1
+	mkdir dir2
+	dd if=/dev/urandom bs=1024 count=128 of=dir2/testfile2
+	popd
+}
+
+remove_test_data()
+{
+	rm -rf dir1 dir2 testfile
+	pushd $WORKDIR
+	rm -rf dir1 dir2 testfile
+	popd
+}
+
+test_singlefile_guest()
+{
+	[ -e testfile ] && rm testfile
+	echo "$SMBGET -v -a smb://$SERVER_IP/smbget/testfile"
+	$SMBGET -v -a smb://$SERVER_IP/smbget/testfile
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: RC does not match, expected: 0'
+		return 1
+	fi
+	cmp --silent $WORKDIR/testfile ./testfile
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: file content does not match'
+		return 1
+	fi
+	return 0
+}
+
+test_singlefile_U()
+{
+	[ -e testfile ] && rm testfile
+	$SMBGET -v -U$USERNAME%$PASSWORD smb://$SERVER_IP/smbget/testfile
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: RC does not match, expected: 0'
+		return 1
+	fi
+	cmp --silent $WORKDIR/testfile ./testfile
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: file content does not match'
+		return 1
+	fi
+	return 0
+}
+
+test_singlefile_smburl()
+{
+	[ -e testfile ] && rm testfile
+	$SMBGET -w $DOMAIN smb://$USERNAME:$PASSWORD@$SERVER_IP/smbget/testfile
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: RC does not match, expected: 0'
+		return 1
+	fi
+	cmp --silent $WORKDIR/testfile ./testfile
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: file content does not match'
+		return 1
+	fi
+	return 0
+}
+
+test_singlefile_rcfile()
+{
+	[ -e testfile ] && rm testfile
+	echo "user $USERNAME%$PASSWORD" > $TMPDIR/rcfile
+	$SMBGET -vn -f $TMPDIR/rcfile smb://$SERVER_IP/smbget/testfile
+	rc=$?
+	rm -f $TMPDIR/rcfile
+	if [ $rc -ne 0 ]; then
+		echo 'ERROR: RC does not match, expected: 0'
+		return 1
+	fi
+	cmp --silent $WORKDIR/testfile ./testfile
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: file content does not match'
+		return 1
+	fi
+	return 0
+}
+
+test_recursive_U()
+{
+	[ -e testfile ] && rm testfile
+	[ -d dir1 ] && rm -rf dir1
+	[ -d dir2 ] && rm -rf dir2
+	$SMBGET -v -R -U$USERNAME%$PASSWORD smb://$SERVER_IP/smbget/
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: RC does not match, expected: 0'
+		return 1
+	fi
+
+	cmp --silent $WORKDIR/testfile ./testfile && \
+	cmp --silent $WORKDIR/dir1/testfile1 ./dir1/testfile1 && \
+	cmp --silent $WORKDIR/dir2/testfile2 ./dir2/testfile2
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: file content does not match'
+		return 1
+	fi
+
+	return 0
+}
+
+test_resume()
+{
+	[ -e testfile ] && rm testfile
+	cp $WORKDIR/testfile .
+	truncate -s 1024 testfile
+	$SMBGET -v -r -U$USERNAME%$PASSWORD smb://$SERVER_IP/smbget/testfile
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: RC does not match, expected: 0'
+		return 1
+	fi
+
+	cmp --silent $WORKDIR/testfile ./testfile
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: file content does not match'
+		return 1
+	fi
+
+	return 0
+}
+
+test_resume_modified()
+{
+	dd if=/dev/urandom bs=1024 count=2 of=testfile
+	$SMBGET -v -r -U$USERNAME%$PASSWORD smb://$SERVER_IP/smbget/testfile
+	if [ $? -ne 1 ]; then
+		echo 'ERROR: RC does not match, expected: 1'
+		return 1
+	fi
+
+	return 0
+}
+
+test_update()
+{
+	[ -e testfile ] && rm testfile
+	$SMBGET -v -U$USERNAME%$PASSWORD smb://$SERVER_IP/smbget/testfile
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: RC does not match, expected: 0'
+		return 1
+	fi
+
+	# secondary download should pass
+	$SMBGET -v -u -U$USERNAME%$PASSWORD smb://$SERVER_IP/smbget/testfile
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: RC does not match, expected: 0'
+		return 1
+	fi
+
+	echo "modified" >> testfile
+	# touch source to trigger new download
+	sleep 2
+	touch -m $WORKDIR/testfile
+	$SMBGET -v -u -U$USERNAME%$PASSWORD smb://$SERVER_IP/smbget/testfile
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: RC does not match, expected: 0'
+		return 1
+	fi
+
+	cmp --silent $WORKDIR/testfile ./testfile
+	if [ $? -ne 0 ]; then
+		echo 'ERROR: file content does not match'
+		return 1
+	fi
+
+	return 0
+}
+
+create_test_data
+
+pushd $TMPDIR
+
+failed=0
+testit "download single file as guest" test_singlefile_guest \
+	|| failed=`expr $failed + 1`
+
+testit "download single file with -U" test_singlefile_U \
+	|| failed=`expr $failed + 1`
+
+testit "download single file with smb URL" test_singlefile_smburl \
+	|| failed=`expr $failed + 1`
+
+testit "download single file with rcfile" test_singlefile_rcfile \
+	|| failed=`expr $failed + 1`
+
+testit "recursive download" test_recursive_U \
+	|| failed=`expr $failed + 1`
+
+testit "resume download" test_resume \
+	|| failed=`expr $failed + 1`
+
+testit "resume download (modified file)" test_resume_modified \
+	|| failed=`expr $failed + 1`
+
+testit "update" test_update \
+	|| failed=`expr $failed + 1`
+
+popd
+
+remove_test_data
+
+exit $failed
diff --git a/source3/selftest/tests.py b/source3/selftest/tests.py
index b2bae75..48e082f 100755
--- a/source3/selftest/tests.py
+++ b/source3/selftest/tests.py
@@ -182,6 +182,7 @@ for env in ["fileserver"]:
     plantestsuite("samba3.blackbox.offline (%s)" % env, env, [os.path.join(samba3srcdir, "script/tests/test_offline.sh"), '$SERVER', '$SERVER_IP', '$DOMAIN', '$USERNAME', '$PASSWORD', '$LOCAL_PATH/offline', smbclient3])
     plantestsuite("samba3.blackbox.shadow_copy2 (%s)" % env, env, [os.path.join(samba3srcdir, "script/tests/test_shadow_copy.sh"), '$SERVER', '$SERVER_IP', '$DOMAIN', '$USERNAME', '$PASSWORD', '$LOCAL_PATH/shadow', smbclient3])
     plantestsuite("samba3.blackbox.smbclient.forceuser_validusers (%s)" % env, env, [os.path.join(samba3srcdir, "script/tests/test_forceuser_validusers.sh"), '$SERVER', '$DOMAIN', '$USERNAME', '$PASSWORD', '$LOCAL_PATH', smbclient3])
+    plantestsuite("samba3.blackbox.smbget (%s)" % env, env, [os.path.join(samba3srcdir, "script/tests/test_smbget.sh"), '$SERVER', '$SERVER_IP', '$DOMAIN', 'smbget_user', '$PASSWORD', '$LOCAL_PATH/smbget', smbget])
 
     #
     # tar command tests
-- 
1.9.1



More information about the samba-technical mailing list