fc9f39
From ab9266a2907fe523937d8576f6de7313d577c2e8 Mon Sep 17 00:00:00 2001
fc9f39
From: Andreas Schneider <asn@samba.org>
fc9f39
Date: Mon, 13 May 2019 16:55:49 +0200
fc9f39
Subject: [PATCH 1/9] s3:smbspool: Add the 'lp' group to the users groups
fc9f39
fc9f39
This is required to access files in /var/spool/cups which have been
fc9f39
temporarily created in there by CUPS.
fc9f39
fc9f39
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13939
fc9f39
fc9f39
Signed-off-by: Andreas Schneider <asn@samba.org>
fc9f39
Reviewed-by: Guenther Deschner <gd@samba.org>
fc9f39
(cherry picked from commit 6086efb6808089c431e7307fa239924bfda1185b)
fc9f39
---
fc9f39
 source3/client/smbspool_krb5_wrapper.c | 22 ++++++++++++++++++++++
fc9f39
 1 file changed, 22 insertions(+)
fc9f39
fc9f39
diff --git a/source3/client/smbspool_krb5_wrapper.c b/source3/client/smbspool_krb5_wrapper.c
fc9f39
index 5c4da33238b..e6684fc0d0c 100644
fc9f39
--- a/source3/client/smbspool_krb5_wrapper.c
fc9f39
+++ b/source3/client/smbspool_krb5_wrapper.c
fc9f39
@@ -82,6 +82,7 @@ int main(int argc, char *argv[])
fc9f39
 {
fc9f39
 	char smbspool_cmd[PATH_MAX] = {0};
fc9f39
 	struct passwd *pwd;
fc9f39
+	struct group *g = NULL;
fc9f39
 	char gen_cc[PATH_MAX] = {0};
fc9f39
 	struct stat sb;
fc9f39
 	char *env = NULL;
fc9f39
@@ -89,6 +90,7 @@ int main(int argc, char *argv[])
fc9f39
 	char device_uri[4096] = {0};
fc9f39
 	uid_t uid = (uid_t)-1;
fc9f39
 	gid_t gid = (gid_t)-1;
fc9f39
+	gid_t groups[1] = { (gid_t)-1 };
fc9f39
 	unsigned long tmp;
fc9f39
 	int cmp;
fc9f39
 	int rc;
fc9f39
@@ -176,6 +178,26 @@ int main(int argc, char *argv[])
fc9f39
 		return CUPS_BACKEND_FAILED;
fc9f39
 	}
fc9f39
 
fc9f39
+	/*
fc9f39
+	 * We need the primary group of the 'lp' user. This is needed to access
fc9f39
+	 * temporary files in /var/spool/cups/.
fc9f39
+	 */
fc9f39
+	g = getgrnam("lp");
fc9f39
+	if (g == NULL) {
fc9f39
+		CUPS_SMB_ERROR("Failed to find user 'lp' - %s",
fc9f39
+			       strerror(errno));
fc9f39
+		return CUPS_BACKEND_FAILED;
fc9f39
+	}
fc9f39
+
fc9f39
+	CUPS_SMB_DEBUG("Adding group 'lp' (%u)", g->gr_gid);
fc9f39
+	groups[0] = g->gr_gid;
fc9f39
+	rc = setgroups(sizeof(groups), groups);
fc9f39
+	if (rc != 0) {
fc9f39
+		CUPS_SMB_ERROR("Failed to set groups for 'lp' - %s",
fc9f39
+			       strerror(errno));
fc9f39
+		return CUPS_BACKEND_FAILED;
fc9f39
+	}
fc9f39
+
fc9f39
 	CUPS_SMB_DEBUG("Switching to gid=%d", gid);
fc9f39
 	rc = setgid(gid);
fc9f39
 	if (rc != 0) {
fc9f39
-- 
fc9f39
2.21.0
fc9f39
fc9f39
fc9f39
From d3ab97ba608b0c3000e733e3e56dd7da7bae617a Mon Sep 17 00:00:00 2001
fc9f39
From: Andreas Schneider <asn@samba.org>
fc9f39
Date: Thu, 16 May 2019 13:41:02 +0200
fc9f39
Subject: [PATCH 2/9] s3:smbspool: Print the principal we use to authenticate
fc9f39
 with
fc9f39
fc9f39
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13939
fc9f39
fc9f39
Signed-off-by: Andreas Schneider <asn@samba.org>
fc9f39
Reviewed-by: Guenther Deschner <gd@samba.org>
fc9f39
(cherry picked from commit 42492d547661cb7a98c237b32d42ee93de35aba5)
fc9f39
---
fc9f39
 source3/client/smbspool.c | 11 +++++++++++
fc9f39
 1 file changed, 11 insertions(+)
fc9f39
fc9f39
diff --git a/source3/client/smbspool.c b/source3/client/smbspool.c
fc9f39
index ecaaf3c3f22..98959bb677b 100644
fc9f39
--- a/source3/client/smbspool.c
fc9f39
+++ b/source3/client/smbspool.c
fc9f39
@@ -612,6 +612,7 @@ static bool kerberos_ccache_is_valid(void) {
fc9f39
 		return false;
fc9f39
 	} else {
fc9f39
 		krb5_principal default_princ = NULL;
fc9f39
+		char *princ_name = NULL;
fc9f39
 
fc9f39
 		code = krb5_cc_get_principal(ctx,
fc9f39
 					     ccache,
fc9f39
@@ -621,6 +622,16 @@ static bool kerberos_ccache_is_valid(void) {
fc9f39
 			krb5_free_context(ctx);
fc9f39
 			return false;
fc9f39
 		}
fc9f39
+
fc9f39
+		code = krb5_unparse_name(ctx,
fc9f39
+					 default_princ,
fc9f39
+					 &princ_name);
fc9f39
+		if (code == 0) {
fc9f39
+			fprintf(stderr,
fc9f39
+				"DEBUG: Try to authenticate as %s\n",
fc9f39
+				princ_name);
fc9f39
+			krb5_free_unparsed_name(ctx, princ_name);
fc9f39
+		}
fc9f39
 		krb5_free_principal(ctx, default_princ);
fc9f39
 	}
fc9f39
 	krb5_cc_close(ctx, ccache);
fc9f39
-- 
fc9f39
2.21.0
fc9f39
fc9f39
fc9f39
From b8588870940e282aa2d5f9d553771fcba91681c7 Mon Sep 17 00:00:00 2001
fc9f39
From: Andreas Schneider <asn@samba.org>
fc9f39
Date: Thu, 16 May 2019 14:25:00 +0200
fc9f39
Subject: [PATCH 3/9] s3:smbspool: Add debug for finding KRB5CCNAME
fc9f39
fc9f39
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13939
fc9f39
fc9f39
Signed-off-by: Andreas Schneider <asn@samba.org>
fc9f39
Reviewed-by: Guenther Deschner <gd@samba.org>
fc9f39
(cherry picked from commit 3632bfef25e471075886eb7aecddd4cc260db8ba)
fc9f39
---
fc9f39
 source3/client/smbspool_krb5_wrapper.c | 4 ++++
fc9f39
 1 file changed, 4 insertions(+)
fc9f39
fc9f39
diff --git a/source3/client/smbspool_krb5_wrapper.c b/source3/client/smbspool_krb5_wrapper.c
fc9f39
index e6684fc0d0c..2cdcd372ec6 100644
fc9f39
--- a/source3/client/smbspool_krb5_wrapper.c
fc9f39
+++ b/source3/client/smbspool_krb5_wrapper.c
fc9f39
@@ -219,10 +219,14 @@ int main(int argc, char *argv[])
fc9f39
 	env = getenv("KRB5CCNAME");
fc9f39
 	if (env != NULL && env[0] != 0) {
fc9f39
 		snprintf(gen_cc, sizeof(gen_cc), "%s", env);
fc9f39
+		CUPS_SMB_DEBUG("User already set KRB5CCNAME [%s] as ccache",
fc9f39
+			       gen_cc);
fc9f39
 
fc9f39
 		goto create_env;
fc9f39
 	}
fc9f39
 
fc9f39
+	CUPS_SMB_DEBUG("Trying to guess KRB5CCNAME (FILE, DIR, KEYRING)");
fc9f39
+
fc9f39
 	snprintf(gen_cc, sizeof(gen_cc), "/tmp/krb5cc_%d", uid);
fc9f39
 
fc9f39
 	rc = lstat(gen_cc, &sb);
fc9f39
-- 
fc9f39
2.21.0
fc9f39
fc9f39
fc9f39
From 30feae8f20fb60999727cc4a6777b2823db46a64 Mon Sep 17 00:00:00 2001
fc9f39
From: Andreas Schneider <asn@samba.org>
fc9f39
Date: Thu, 16 May 2019 17:10:57 +0200
fc9f39
Subject: [PATCH 4/9] s3:smbspool: Use %u format specifier to print uid
fc9f39
fc9f39
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13939
fc9f39
fc9f39
Signed-off-by: Andreas Schneider <asn@samba.org>
fc9f39
Reviewed-by: Guenther Deschner <gd@samba.org>
fc9f39
(cherry picked from commit be596ce3d2455bd49a8ebd311d8c764c37852858)
fc9f39
---
fc9f39
 source3/client/smbspool_krb5_wrapper.c | 6 +++---
fc9f39
 1 file changed, 3 insertions(+), 3 deletions(-)
fc9f39
fc9f39
diff --git a/source3/client/smbspool_krb5_wrapper.c b/source3/client/smbspool_krb5_wrapper.c
fc9f39
index 2cdcd372ec6..3266b90ec1a 100644
fc9f39
--- a/source3/client/smbspool_krb5_wrapper.c
fc9f39
+++ b/source3/client/smbspool_krb5_wrapper.c
fc9f39
@@ -227,13 +227,13 @@ int main(int argc, char *argv[])
fc9f39
 
fc9f39
 	CUPS_SMB_DEBUG("Trying to guess KRB5CCNAME (FILE, DIR, KEYRING)");
fc9f39
 
fc9f39
-	snprintf(gen_cc, sizeof(gen_cc), "/tmp/krb5cc_%d", uid);
fc9f39
+	snprintf(gen_cc, sizeof(gen_cc), "/tmp/krb5cc_%u", uid);
fc9f39
 
fc9f39
 	rc = lstat(gen_cc, &sb);
fc9f39
 	if (rc == 0) {
fc9f39
-		snprintf(gen_cc, sizeof(gen_cc), "FILE:/tmp/krb5cc_%d", uid);
fc9f39
+		snprintf(gen_cc, sizeof(gen_cc), "FILE:/tmp/krb5cc_%u", uid);
fc9f39
 	} else {
fc9f39
-		snprintf(gen_cc, sizeof(gen_cc), "/run/user/%d/krb5cc", uid);
fc9f39
+		snprintf(gen_cc, sizeof(gen_cc), "/run/user/%u/krb5cc", uid);
fc9f39
 
fc9f39
 		rc = lstat(gen_cc, &sb);
fc9f39
 		if (rc == 0 && S_ISDIR(sb.st_mode)) {
fc9f39
-- 
fc9f39
2.21.0
fc9f39
fc9f39
fc9f39
From 98b782f300a899ad39fe17fa62ccbe4932e8cd29 Mon Sep 17 00:00:00 2001
fc9f39
From: Andreas Schneider <asn@samba.org>
fc9f39
Date: Thu, 16 May 2019 17:40:43 +0200
fc9f39
Subject: [PATCH 5/9] s3:smbspool: Fallback to default ccache if KRB5CCNAME is
fc9f39
 not set
fc9f39
fc9f39
This could also support the new KCM credential cache storage.
fc9f39
fc9f39
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13939
fc9f39
fc9f39
Signed-off-by: Andreas Schneider <asn@samba.org>
fc9f39
Reviewed-by: Guenther Deschner <gd@samba.org>
fc9f39
(cherry picked from commit 6bbdf69e406916107400e2cabdbc831e2a2bbee3)
fc9f39
---
fc9f39
 source3/client/smbspool_krb5_wrapper.c | 79 ++++++++++++++++++--------
fc9f39
 source3/wscript_build                  |  1 +
fc9f39
 2 files changed, 55 insertions(+), 25 deletions(-)
fc9f39
fc9f39
diff --git a/source3/client/smbspool_krb5_wrapper.c b/source3/client/smbspool_krb5_wrapper.c
fc9f39
index 3266b90ec1a..bff1df417e8 100644
fc9f39
--- a/source3/client/smbspool_krb5_wrapper.c
fc9f39
+++ b/source3/client/smbspool_krb5_wrapper.c
fc9f39
@@ -21,6 +21,7 @@
fc9f39
 
fc9f39
 #include "includes.h"
fc9f39
 #include "system/filesys.h"
fc9f39
+#include "system/kerberos.h"
fc9f39
 #include "system/passwd.h"
fc9f39
 
fc9f39
 #include <errno.h>
fc9f39
@@ -68,6 +69,50 @@ static void cups_smb_debug(enum cups_smb_dbglvl_e lvl, const char *format, ...)
fc9f39
 		buffer);
fc9f39
 }
fc9f39
 
fc9f39
+static bool kerberos_get_default_ccache(char *ccache_buf, size_t len)
fc9f39
+{
fc9f39
+	krb5_context ctx;
fc9f39
+	const char *ccache_name = NULL;
fc9f39
+	char *full_ccache_name = NULL;
fc9f39
+	krb5_ccache ccache = NULL;
fc9f39
+	krb5_error_code code;
fc9f39
+
fc9f39
+	code = krb5_init_context(&ctx;;
fc9f39
+	if (code != 0) {
fc9f39
+		return false;
fc9f39
+	}
fc9f39
+
fc9f39
+	ccache_name = krb5_cc_default_name(ctx);
fc9f39
+	if (ccache_name == NULL) {
fc9f39
+		krb5_free_context(ctx);
fc9f39
+		return false;
fc9f39
+	}
fc9f39
+
fc9f39
+	code = krb5_cc_resolve(ctx, ccache_name, &ccache);
fc9f39
+	if (code != 0) {
fc9f39
+		krb5_free_context(ctx);
fc9f39
+		return false;
fc9f39
+	}
fc9f39
+
fc9f39
+	code = krb5_cc_get_full_name(ctx, ccache, &full_ccache_name);
fc9f39
+	krb5_cc_close(ctx, ccache);
fc9f39
+	if (code != 0) {
fc9f39
+		krb5_free_context(ctx);
fc9f39
+		return false;
fc9f39
+	}
fc9f39
+
fc9f39
+	snprintf(ccache_buf, len, "%s", full_ccache_name);
fc9f39
+
fc9f39
+#ifdef SAMBA4_USES_HEIMDAL
fc9f39
+	free(full_ccache_name);
fc9f39
+#else
fc9f39
+	krb5_free_string(ctx, full_ccache_name);
fc9f39
+#endif
fc9f39
+	krb5_free_context(ctx);
fc9f39
+
fc9f39
+	return true;
fc9f39
+}
fc9f39
+
fc9f39
 /*
fc9f39
  * This is a helper binary to execute smbspool.
fc9f39
  *
fc9f39
@@ -84,7 +129,6 @@ int main(int argc, char *argv[])
fc9f39
 	struct passwd *pwd;
fc9f39
 	struct group *g = NULL;
fc9f39
 	char gen_cc[PATH_MAX] = {0};
fc9f39
-	struct stat sb;
fc9f39
 	char *env = NULL;
fc9f39
 	char auth_info_required[256] = {0};
fc9f39
 	char device_uri[4096] = {0};
fc9f39
@@ -92,6 +136,7 @@ int main(int argc, char *argv[])
fc9f39
 	gid_t gid = (gid_t)-1;
fc9f39
 	gid_t groups[1] = { (gid_t)-1 };
fc9f39
 	unsigned long tmp;
fc9f39
+	bool ok;
fc9f39
 	int cmp;
fc9f39
 	int rc;
fc9f39
 
fc9f39
@@ -225,32 +270,16 @@ int main(int argc, char *argv[])
fc9f39
 		goto create_env;
fc9f39
 	}
fc9f39
 
fc9f39
-	CUPS_SMB_DEBUG("Trying to guess KRB5CCNAME (FILE, DIR, KEYRING)");
fc9f39
-
fc9f39
-	snprintf(gen_cc, sizeof(gen_cc), "/tmp/krb5cc_%u", uid);
fc9f39
-
fc9f39
-	rc = lstat(gen_cc, &sb);
fc9f39
-	if (rc == 0) {
fc9f39
-		snprintf(gen_cc, sizeof(gen_cc), "FILE:/tmp/krb5cc_%u", uid);
fc9f39
-	} else {
fc9f39
-		snprintf(gen_cc, sizeof(gen_cc), "/run/user/%u/krb5cc", uid);
fc9f39
-
fc9f39
-		rc = lstat(gen_cc, &sb);
fc9f39
-		if (rc == 0 && S_ISDIR(sb.st_mode)) {
fc9f39
-			snprintf(gen_cc,
fc9f39
-				 sizeof(gen_cc),
fc9f39
-				 "DIR:/run/user/%d/krb5cc",
fc9f39
-				 uid);
fc9f39
-		} else {
fc9f39
-#if defined(__linux__)
fc9f39
-			snprintf(gen_cc,
fc9f39
-				 sizeof(gen_cc),
fc9f39
-				 "KEYRING:persistent:%d",
fc9f39
-				 uid);
fc9f39
-#endif
fc9f39
-		}
fc9f39
+	ok = kerberos_get_default_ccache(gen_cc, sizeof(gen_cc));
fc9f39
+	if (ok) {
fc9f39
+		CUPS_SMB_DEBUG("Use default KRB5CCNAME [%s]",
fc9f39
+			       gen_cc);
fc9f39
+		goto create_env;
fc9f39
 	}
fc9f39
 
fc9f39
+	/* Fallback to a FILE ccache */
fc9f39
+	snprintf(gen_cc, sizeof(gen_cc), "FILE:/tmp/krb5cc_%u", uid);
fc9f39
+
fc9f39
 create_env:
fc9f39
 	/*
fc9f39
 	 * Make sure we do not have LD_PRELOAD or other security relevant
fc9f39
diff --git a/source3/wscript_build b/source3/wscript_build
fc9f39
index 15c93e46bc3..694acbfa754 100644
fc9f39
--- a/source3/wscript_build
fc9f39
+++ b/source3/wscript_build
fc9f39
@@ -1120,6 +1120,7 @@ bld.SAMBA3_BINARY('smbspool_krb5_wrapper',
fc9f39
                  deps='''
fc9f39
                       DYNCONFIG
fc9f39
                       cups
fc9f39
+                      krb5
fc9f39
                       ''',
fc9f39
                  install_path='${LIBEXECDIR}/samba',
fc9f39
                  enabled=bld.CONFIG_SET('HAVE_CUPS'))
fc9f39
-- 
fc9f39
2.21.0
fc9f39
fc9f39
fc9f39
From 0ffe2ecb356780264b157a03157875758431102f Mon Sep 17 00:00:00 2001
fc9f39
From: Andreas Schneider <asn@samba.org>
fc9f39
Date: Mon, 13 May 2019 16:48:31 +0200
fc9f39
Subject: [PATCH 6/9] s3:smbspool: Print the filename we failed to open
fc9f39
fc9f39
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13939
fc9f39
fc9f39
Signed-off-by: Andreas Schneider <asn@samba.org>
fc9f39
Reviewed-by: Guenther Deschner <gd@samba.org>
fc9f39
(cherry picked from commit 281274572bcc3125fe6026a01ef7bf7ef584a0dd)
fc9f39
---
fc9f39
 source3/client/smbspool.c | 4 +++-
fc9f39
 1 file changed, 3 insertions(+), 1 deletion(-)
fc9f39
fc9f39
diff --git a/source3/client/smbspool.c b/source3/client/smbspool.c
fc9f39
index 98959bb677b..43f0cbc04e1 100644
fc9f39
--- a/source3/client/smbspool.c
fc9f39
+++ b/source3/client/smbspool.c
fc9f39
@@ -223,7 +223,9 @@ main(int argc,			/* I - Number of command-line arguments */
fc9f39
 
fc9f39
 		fp = fopen(print_file, "rb");
fc9f39
 		if (fp == NULL) {
fc9f39
-			perror("ERROR: Unable to open print file");
fc9f39
+			fprintf(stderr,
fc9f39
+				"ERROR: Unable to open print file: %s",
fc9f39
+				print_file);
fc9f39
 			goto done;
fc9f39
 		}
fc9f39
 
fc9f39
-- 
fc9f39
2.21.0
fc9f39
fc9f39
fc9f39
From 9d662cda9def334de3a27cab7d77ab6c9deb3f16 Mon Sep 17 00:00:00 2001
fc9f39
From: Andreas Schneider <asn@samba.org>
fc9f39
Date: Mon, 13 May 2019 18:54:02 +0200
fc9f39
Subject: [PATCH 7/9] s3:smbspool: Always try to authenticate using Kerberos
fc9f39
fc9f39
If username and password is given, then fallback to NTLM. However try
fc9f39
kinit first. Also we correctly handle NULL passwords in the meantime and
fc9f39
this makes it easier to deal with issues.
fc9f39
fc9f39
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13939
fc9f39
fc9f39
Signed-off-by: Andreas Schneider <asn@samba.org>
fc9f39
Reviewed-by: Guenther Deschner <gd@samba.org>
fc9f39
(cherry picked from commit 3d719a1f85db8e423dc3a4116a2228961d5ac48d)
fc9f39
---
fc9f39
 source3/client/smbspool.c | 90 ++++++++++++++++++++++-----------------
fc9f39
 1 file changed, 51 insertions(+), 39 deletions(-)
fc9f39
fc9f39
diff --git a/source3/client/smbspool.c b/source3/client/smbspool.c
fc9f39
index 43f0cbc04e1..f8e6a76ba11 100644
fc9f39
--- a/source3/client/smbspool.c
fc9f39
+++ b/source3/client/smbspool.c
fc9f39
@@ -87,8 +87,8 @@ main(int argc,			/* I - Number of command-line arguments */
fc9f39
 	int             port;	/* Port number */
fc9f39
 	char            uri[1024],	/* URI */
fc9f39
 	               *sep,	/* Pointer to separator */
fc9f39
-	               *tmp, *tmp2,	/* Temp pointers to do escaping */
fc9f39
-	               *password;	/* Password */
fc9f39
+	               *tmp, *tmp2;	/* Temp pointers to do escaping */
fc9f39
+	const char     *password = NULL;	/* Password */
fc9f39
 	char           *username,	/* Username */
fc9f39
 	               *server,	/* Server name */
fc9f39
 	               *printer;/* Printer name */
fc9f39
@@ -292,8 +292,6 @@ main(int argc,			/* I - Number of command-line arguments */
fc9f39
 		if ((tmp2 = strchr_m(tmp, ':')) != NULL) {
fc9f39
 			*tmp2++ = '\0';
fc9f39
 			password = uri_unescape_alloc(tmp2);
fc9f39
-		} else {
fc9f39
-			password = empty_str;
fc9f39
 		}
fc9f39
 		username = uri_unescape_alloc(tmp);
fc9f39
 	} else {
fc9f39
@@ -301,14 +299,15 @@ main(int argc,			/* I - Number of command-line arguments */
fc9f39
 			username = empty_str;
fc9f39
 		}
fc9f39
 
fc9f39
-		if ((password = getenv("AUTH_PASSWORD")) == NULL) {
fc9f39
-			password = empty_str;
fc9f39
+		env = getenv("AUTH_PASSWORD");
fc9f39
+		if (env != NULL && strlen(env) > 0) {
fc9f39
+			password = env;
fc9f39
 		}
fc9f39
 
fc9f39
 		server = uri + 6;
fc9f39
 	}
fc9f39
 
fc9f39
-	if (password != empty_str) {
fc9f39
+	if (password != NULL) {
fc9f39
 		auth_info_required = "username,password";
fc9f39
 	}
fc9f39
 
fc9f39
@@ -513,6 +512,7 @@ smb_complete_connection(const char *myname,
fc9f39
 	NTSTATUS        nt_status;
fc9f39
 	struct cli_credentials *creds = NULL;
fc9f39
 	bool use_kerberos = false;
fc9f39
+	bool fallback_after_kerberos = false;
fc9f39
 
fc9f39
 	/* Start the SMB connection */
fc9f39
 	*need_auth = false;
fc9f39
@@ -523,27 +523,21 @@ smb_complete_connection(const char *myname,
fc9f39
 		return NULL;
fc9f39
 	}
fc9f39
 
fc9f39
-	/*
fc9f39
-	 * We pretty much guarantee password must be valid or a pointer to a
fc9f39
-	 * 0 char.
fc9f39
-	 */
fc9f39
-	if (!password) {
fc9f39
-		*need_auth = true;
fc9f39
-		return NULL;
fc9f39
-	}
fc9f39
-
fc9f39
 	if (flags & CLI_FULL_CONNECTION_USE_KERBEROS) {
fc9f39
-		auth_info_required = "negotiate";
fc9f39
 		use_kerberos = true;
fc9f39
 	}
fc9f39
 
fc9f39
+	if (flags & CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS) {
fc9f39
+		fallback_after_kerberos = true;
fc9f39
+	}
fc9f39
+
fc9f39
 	creds = cli_session_creds_init(cli,
fc9f39
 				       username,
fc9f39
 				       workgroup,
fc9f39
 				       NULL, /* realm */
fc9f39
 				       password,
fc9f39
 				       use_kerberos,
fc9f39
-				       false, /* fallback_after_kerberos */
fc9f39
+				       fallback_after_kerberos,
fc9f39
 				       false, /* use_ccache */
fc9f39
 				       false); /* password_is_nt_hash */
fc9f39
 	if (creds == NULL) {
fc9f39
@@ -659,6 +653,10 @@ smb_connect(const char *workgroup,	/* I - Workgroup */
fc9f39
 	struct cli_state *cli;	/* New connection */
fc9f39
 	char           *myname = NULL;	/* Client name */
fc9f39
 	struct passwd  *pwd;
fc9f39
+	int flags = CLI_FULL_CONNECTION_USE_KERBEROS;
fc9f39
+	bool use_kerberos = false;
fc9f39
+	const char *user = username;
fc9f39
+	int cmp;
fc9f39
 
fc9f39
 	/*
fc9f39
          * Get the names and addresses of the client and server...
fc9f39
@@ -668,42 +666,56 @@ smb_connect(const char *workgroup,	/* I - Workgroup */
fc9f39
 		return NULL;
fc9f39
 	}
fc9f39
 
fc9f39
-	/*
fc9f39
-	 * See if we have a username first.  This is for backwards compatible
fc9f39
-	 * behavior with 3.0.14a
fc9f39
-	 */
fc9f39
 
fc9f39
-	if (username == NULL || username[0] == '\0') {
fc9f39
-		if (kerberos_ccache_is_valid()) {
fc9f39
-			goto kerberos_auth;
fc9f39
+	cmp = strcmp(auth_info_required, "negotiate");
fc9f39
+	if (cmp == 0) {
fc9f39
+		if (!kerberos_ccache_is_valid()) {
fc9f39
+			return NULL;
fc9f39
 		}
fc9f39
+		user = jobusername;
fc9f39
+
fc9f39
+		use_kerberos = true;
fc9f39
+		fprintf(stderr,
fc9f39
+			"DEBUG: Try to connect using Kerberos ...\n");
fc9f39
+	}
fc9f39
+
fc9f39
+	cmp = strcmp(auth_info_required, "username,password");
fc9f39
+	if (cmp == 0) {
fc9f39
+		if (username == NULL || username[0] == '\0') {
fc9f39
+			return NULL;
fc9f39
+		}
fc9f39
+
fc9f39
+		/* Fallback to NTLM */
fc9f39
+		flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
fc9f39
+
fc9f39
+		fprintf(stderr,
fc9f39
+			"DEBUG: Try to connect using username/password ...\n");
fc9f39
+	}
fc9f39
+
fc9f39
+	cmp = strcmp(auth_info_required, "none");
fc9f39
+	if (cmp == 0) {
fc9f39
+		fprintf(stderr,
fc9f39
+			"DEBUG: This backend doesn't support none auth ...\n");
fc9f39
+		return NULL;
fc9f39
 	}
fc9f39
 
fc9f39
 	cli = smb_complete_connection(myname,
fc9f39
 				      server,
fc9f39
 				      port,
fc9f39
-				      username,
fc9f39
+				      user,
fc9f39
 				      password,
fc9f39
 				      workgroup,
fc9f39
 				      share,
fc9f39
-				      0,
fc9f39
+				      flags,
fc9f39
 				      need_auth);
fc9f39
 	if (cli != NULL) {
fc9f39
-		fputs("DEBUG: Connected with username/password...\n", stderr);
fc9f39
+		fprintf(stderr, "DEBUG: SMB connection established.\n");
fc9f39
 		return (cli);
fc9f39
 	}
fc9f39
 
fc9f39
-kerberos_auth:
fc9f39
-	/*
fc9f39
-	 * Try to use the user kerberos credentials (if any) to authenticate
fc9f39
-	 */
fc9f39
-	cli = smb_complete_connection(myname, server, port, jobusername, "",
fc9f39
-				      workgroup, share,
fc9f39
-				 CLI_FULL_CONNECTION_USE_KERBEROS, need_auth);
fc9f39
-
fc9f39
-	if (cli) {
fc9f39
-		fputs("DEBUG: Connected using Kerberos...\n", stderr);
fc9f39
-		return (cli);
fc9f39
+	if (!use_kerberos) {
fc9f39
+		fprintf(stderr, "ERROR: SMB connection failed!\n");
fc9f39
+		return NULL;
fc9f39
 	}
fc9f39
 
fc9f39
 	/* give a chance for a passwordless NTLMSSP session setup */
fc9f39
-- 
fc9f39
2.21.0
fc9f39
fc9f39
fc9f39
From 56f58726a1f3b98e64e9f6b27c275cc0044e2a9f Mon Sep 17 00:00:00 2001
fc9f39
From: Andreas Schneider <asn@samba.org>
fc9f39
Date: Thu, 16 May 2019 18:24:32 +0200
fc9f39
Subject: [PATCH 8/9] s3:smbspool: Add debug messages to
fc9f39
 kerberos_ccache_is_valid()
fc9f39
fc9f39
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13939
fc9f39
fc9f39
Signed-off-by: Andreas Schneider <asn@samba.org>
fc9f39
Reviewed-by: Guenther Deschner <gd@samba.org>
fc9f39
(cherry picked from commit 93acd880801524c5e621df7b5bf5ad650f93cec3)
fc9f39
---
fc9f39
 source3/client/smbspool.c | 7 +++++++
fc9f39
 1 file changed, 7 insertions(+)
fc9f39
fc9f39
diff --git a/source3/client/smbspool.c b/source3/client/smbspool.c
fc9f39
index f8e6a76ba11..ed5837daa0d 100644
fc9f39
--- a/source3/client/smbspool.c
fc9f39
+++ b/source3/client/smbspool.c
fc9f39
@@ -599,11 +599,15 @@ static bool kerberos_ccache_is_valid(void) {
fc9f39
 
fc9f39
 	ccache_name = krb5_cc_default_name(ctx);
fc9f39
 	if (ccache_name == NULL) {
fc9f39
+		DBG_ERR("Failed to get default ccache name\n");
fc9f39
+		krb5_free_context(ctx);
fc9f39
 		return false;
fc9f39
 	}
fc9f39
 
fc9f39
 	code = krb5_cc_resolve(ctx, ccache_name, &ccache);
fc9f39
 	if (code != 0) {
fc9f39
+		DBG_ERR("Failed to resolve ccache name: %s\n",
fc9f39
+			ccache_name);
fc9f39
 		krb5_free_context(ctx);
fc9f39
 		return false;
fc9f39
 	} else {
fc9f39
@@ -614,6 +618,9 @@ static bool kerberos_ccache_is_valid(void) {
fc9f39
 					     ccache,
fc9f39
 					     &default_princ);
fc9f39
 		if (code != 0) {
fc9f39
+			DBG_ERR("Failed to get default principal from "
fc9f39
+				"ccache: %s\n",
fc9f39
+				ccache_name);
fc9f39
 			krb5_cc_close(ctx, ccache);
fc9f39
 			krb5_free_context(ctx);
fc9f39
 			return false;
fc9f39
-- 
fc9f39
2.21.0
fc9f39
fc9f39
fc9f39
From cec536a0437b28e207cb69c318cb5769575d1761 Mon Sep 17 00:00:00 2001
fc9f39
From: Andreas Schneider <asn@samba.org>
fc9f39
Date: Tue, 14 May 2019 11:35:46 +0200
fc9f39
Subject: [PATCH 9/9] s3:smbspool: Use NTSTATUS return codes
fc9f39
fc9f39
This allows us to simplify some code and return better errors.
fc9f39
fc9f39
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13939
fc9f39
fc9f39
Signed-off-by: Andreas Schneider <asn@samba.org>
fc9f39
Reviewed-by: Guenther Deschner <gd@samba.org>
fc9f39
(cherry picked from commit d9af3dc02e98a3eb22441dfbdeddbaca0af078ea)
fc9f39
---
fc9f39
 source3/client/smbspool.c | 250 ++++++++++++++++++++++----------------
fc9f39
 1 file changed, 145 insertions(+), 105 deletions(-)
fc9f39
fc9f39
diff --git a/source3/client/smbspool.c b/source3/client/smbspool.c
fc9f39
index ed5837daa0d..1c09ca0826d 100644
fc9f39
--- a/source3/client/smbspool.c
fc9f39
+++ b/source3/client/smbspool.c
fc9f39
@@ -60,12 +60,27 @@
fc9f39
  * Local functions...
fc9f39
  */
fc9f39
 
fc9f39
-static int      get_exit_code(struct cli_state * cli, NTSTATUS nt_status);
fc9f39
+static int      get_exit_code(NTSTATUS nt_status);
fc9f39
 static void     list_devices(void);
fc9f39
-static struct cli_state *smb_complete_connection(const char *, const char *,
fc9f39
-	int, const char *, const char *, const char *, const char *, int, bool *need_auth);
fc9f39
-static struct cli_state *smb_connect(const char *, const char *, int, const
fc9f39
-	char *, const char *, const char *, const char *, bool *need_auth);
fc9f39
+static NTSTATUS
fc9f39
+smb_complete_connection(struct cli_state **output_cli,
fc9f39
+			const char *myname,
fc9f39
+			const char *server,
fc9f39
+			int port,
fc9f39
+			const char *username,
fc9f39
+			const char *password,
fc9f39
+			const char *workgroup,
fc9f39
+			const char *share,
fc9f39
+			int flags);
fc9f39
+static NTSTATUS
fc9f39
+smb_connect(struct cli_state **output_cli,
fc9f39
+	    const char *workgroup,
fc9f39
+	    const char *server,
fc9f39
+	    const int port,
fc9f39
+	    const char *share,
fc9f39
+	    const char *username,
fc9f39
+	    const char *password,
fc9f39
+	    const char *jobusername);
fc9f39
 static int      smb_print(struct cli_state *, const char *, FILE *);
fc9f39
 static char    *uri_unescape_alloc(const char *);
fc9f39
 #if 0
fc9f39
@@ -89,16 +104,15 @@ main(int argc,			/* I - Number of command-line arguments */
fc9f39
 	               *sep,	/* Pointer to separator */
fc9f39
 	               *tmp, *tmp2;	/* Temp pointers to do escaping */
fc9f39
 	const char     *password = NULL;	/* Password */
fc9f39
-	char           *username,	/* Username */
fc9f39
-	               *server,	/* Server name */
fc9f39
+	const char     *username = NULL;	/* Username */
fc9f39
+	char           *server,	/* Server name */
fc9f39
 	               *printer;/* Printer name */
fc9f39
 	const char     *workgroup;	/* Workgroup */
fc9f39
 	FILE           *fp;	/* File to print */
fc9f39
 	int             status = 1;	/* Status of LPD job */
fc9f39
-	struct cli_state *cli;	/* SMB interface */
fc9f39
-	char            empty_str[] = "";
fc9f39
+	NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
fc9f39
+	struct cli_state *cli = NULL;	/* SMB interface */
fc9f39
 	int             tries = 0;
fc9f39
-	bool		need_auth = true;
fc9f39
 	const char     *dev_uri = NULL;
fc9f39
 	const char     *env = NULL;
fc9f39
 	const char     *config_file = NULL;
fc9f39
@@ -295,8 +309,9 @@ main(int argc,			/* I - Number of command-line arguments */
fc9f39
 		}
fc9f39
 		username = uri_unescape_alloc(tmp);
fc9f39
 	} else {
fc9f39
-		if ((username = getenv("AUTH_USERNAME")) == NULL) {
fc9f39
-			username = empty_str;
fc9f39
+		env = getenv("AUTH_USERNAME");
fc9f39
+		if (env != NULL && strlen(env) > 0) {
fc9f39
+			username = env;
fc9f39
 		}
fc9f39
 
fc9f39
 		env = getenv("AUTH_PASSWORD");
fc9f39
@@ -368,27 +383,39 @@ main(int argc,			/* I - Number of command-line arguments */
fc9f39
 	load_interfaces();
fc9f39
 
fc9f39
 	do {
fc9f39
-		cli = smb_connect(workgroup,
fc9f39
-				  server,
fc9f39
-				  port,
fc9f39
-				  printer,
fc9f39
-				  username,
fc9f39
-				  password,
fc9f39
-				  print_user,
fc9f39
-				  &need_auth);
fc9f39
-		if (cli == NULL) {
fc9f39
-			if (need_auth) {
fc9f39
-				exit(2);
fc9f39
+		nt_status = smb_connect(&cli,
fc9f39
+					workgroup,
fc9f39
+					server,
fc9f39
+					port,
fc9f39
+					printer,
fc9f39
+					username,
fc9f39
+					password,
fc9f39
+					print_user);
fc9f39
+		if (!NT_STATUS_IS_OK(nt_status)) {
fc9f39
+			status = get_exit_code(nt_status);
fc9f39
+			if (status == 2) {
fc9f39
+				fprintf(stderr,
fc9f39
+					"DEBUG: Unable to connect to CIFS "
fc9f39
+					"host: %s",
fc9f39
+					nt_errstr(nt_status));
fc9f39
+				goto done;
fc9f39
 			} else if (getenv("CLASS") == NULL) {
fc9f39
-				fprintf(stderr, "ERROR: Unable to connect to CIFS host, will retry in 60 seconds...\n");
fc9f39
+				fprintf(stderr,
fc9f39
+					"ERROR: Unable to connect to CIFS "
fc9f39
+					"host: %s. Will retry in 60 "
fc9f39
+					"seconds...\n",
fc9f39
+					nt_errstr(nt_status));
fc9f39
 				sleep(60);
fc9f39
 				tries++;
fc9f39
 			} else {
fc9f39
-				fprintf(stderr, "ERROR: Unable to connect to CIFS host, trying next printer...\n");
fc9f39
+				fprintf(stderr,
fc9f39
+					"ERROR: Unable to connect to CIFS "
fc9f39
+					"host: %s. Trying next printer...\n",
fc9f39
+					nt_errstr(nt_status));
fc9f39
 				goto done;
fc9f39
 			}
fc9f39
 		}
fc9f39
-	} while ((cli == NULL) && (tries < MAX_RETRY_CONNECT));
fc9f39
+	} while (!NT_STATUS_IS_OK(nt_status) && (tries < MAX_RETRY_CONNECT));
fc9f39
 
fc9f39
 	if (cli == NULL) {
fc9f39
 		fprintf(stderr, "ERROR: Unable to connect to CIFS host after (tried %d times)\n", tries);
fc9f39
@@ -435,10 +462,9 @@ done:
fc9f39
  */
fc9f39
 
fc9f39
 static int
fc9f39
-get_exit_code(struct cli_state * cli,
fc9f39
-	      NTSTATUS nt_status)
fc9f39
+get_exit_code(NTSTATUS nt_status)
fc9f39
 {
fc9f39
-	int i;
fc9f39
+	size_t i;
fc9f39
 
fc9f39
 	/* List of NTSTATUS errors that are considered
fc9f39
 	 * authentication errors
fc9f39
@@ -454,17 +480,16 @@ get_exit_code(struct cli_state * cli,
fc9f39
 	};
fc9f39
 
fc9f39
 
fc9f39
-	fprintf(stderr, "DEBUG: get_exit_code(cli=%p, nt_status=%s [%x])\n",
fc9f39
-		cli, nt_errstr(nt_status), NT_STATUS_V(nt_status));
fc9f39
+	fprintf(stderr,
fc9f39
+		"DEBUG: get_exit_code(nt_status=%s [%x])\n",
fc9f39
+		nt_errstr(nt_status), NT_STATUS_V(nt_status));
fc9f39
 
fc9f39
 	for (i = 0; i < ARRAY_SIZE(auth_errors); i++) {
fc9f39
 		if (!NT_STATUS_EQUAL(nt_status, auth_errors[i])) {
fc9f39
 			continue;
fc9f39
 		}
fc9f39
 
fc9f39
-		if (cli) {
fc9f39
-			fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
fc9f39
-		}
fc9f39
+		fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
fc9f39
 
fc9f39
 		/*
fc9f39
 		 * 2 = authentication required...
fc9f39
@@ -497,16 +522,16 @@ list_devices(void)
fc9f39
 }
fc9f39
 
fc9f39
 
fc9f39
-static struct cli_state *
fc9f39
-smb_complete_connection(const char *myname,
fc9f39
+static NTSTATUS
fc9f39
+smb_complete_connection(struct cli_state **output_cli,
fc9f39
+			const char *myname,
fc9f39
 			const char *server,
fc9f39
 			int port,
fc9f39
 			const char *username,
fc9f39
 			const char *password,
fc9f39
 			const char *workgroup,
fc9f39
 			const char *share,
fc9f39
-			int flags,
fc9f39
-			bool *need_auth)
fc9f39
+			int flags)
fc9f39
 {
fc9f39
 	struct cli_state *cli;	/* New connection */
fc9f39
 	NTSTATUS        nt_status;
fc9f39
@@ -515,12 +540,11 @@ smb_complete_connection(const char *myname,
fc9f39
 	bool fallback_after_kerberos = false;
fc9f39
 
fc9f39
 	/* Start the SMB connection */
fc9f39
-	*need_auth = false;
fc9f39
 	nt_status = cli_start_connection(&cli, myname, server, NULL, port,
fc9f39
 					 SMB_SIGNING_DEFAULT, flags);
fc9f39
 	if (!NT_STATUS_IS_OK(nt_status)) {
fc9f39
 		fprintf(stderr, "ERROR: Connection failed: %s\n", nt_errstr(nt_status));
fc9f39
-		return NULL;
fc9f39
+		return nt_status;
fc9f39
 	}
fc9f39
 
fc9f39
 	if (flags & CLI_FULL_CONNECTION_USE_KERBEROS) {
fc9f39
@@ -543,20 +567,16 @@ smb_complete_connection(const char *myname,
fc9f39
 	if (creds == NULL) {
fc9f39
 		fprintf(stderr, "ERROR: cli_session_creds_init failed\n");
fc9f39
 		cli_shutdown(cli);
fc9f39
-		return NULL;
fc9f39
+		return NT_STATUS_NO_MEMORY;
fc9f39
 	}
fc9f39
 
fc9f39
 	nt_status = cli_session_setup_creds(cli, creds);
fc9f39
 	if (!NT_STATUS_IS_OK(nt_status)) {
fc9f39
 		fprintf(stderr, "ERROR: Session setup failed: %s\n", nt_errstr(nt_status));
fc9f39
 
fc9f39
-		if (get_exit_code(cli, nt_status) == 2) {
fc9f39
-			*need_auth = true;
fc9f39
-		}
fc9f39
-
fc9f39
 		cli_shutdown(cli);
fc9f39
 
fc9f39
-		return NULL;
fc9f39
+		return nt_status;
fc9f39
 	}
fc9f39
 
fc9f39
 	nt_status = cli_tree_connect_creds(cli, share, "?????", creds);
fc9f39
@@ -564,13 +584,9 @@ smb_complete_connection(const char *myname,
fc9f39
 		fprintf(stderr, "ERROR: Tree connect failed (%s)\n",
fc9f39
 			nt_errstr(nt_status));
fc9f39
 
fc9f39
-		if (get_exit_code(cli, nt_status) == 2) {
fc9f39
-			*need_auth = true;
fc9f39
-		}
fc9f39
-
fc9f39
 		cli_shutdown(cli);
fc9f39
 
fc9f39
-		return NULL;
fc9f39
+		return nt_status;
fc9f39
 	}
fc9f39
 #if 0
fc9f39
 	/* Need to work out how to specify this on the URL. */
fc9f39
@@ -583,7 +599,8 @@ smb_complete_connection(const char *myname,
fc9f39
 	}
fc9f39
 #endif
fc9f39
 
fc9f39
-	return cli;
fc9f39
+	*output_cli = cli;
fc9f39
+	return NT_STATUS_OK;
fc9f39
 }
fc9f39
 
fc9f39
 static bool kerberos_ccache_is_valid(void) {
fc9f39
@@ -647,49 +664,48 @@ static bool kerberos_ccache_is_valid(void) {
fc9f39
  * 'smb_connect()' - Return a connection to a server.
fc9f39
  */
fc9f39
 
fc9f39
-static struct cli_state *	/* O - SMB connection */
fc9f39
-smb_connect(const char *workgroup,	/* I - Workgroup */
fc9f39
+static NTSTATUS
fc9f39
+smb_connect(struct cli_state **output_cli,
fc9f39
+	    const char *workgroup,	/* I - Workgroup */
fc9f39
 	    const char *server,	/* I - Server */
fc9f39
 	    const int port,	/* I - Port */
fc9f39
 	    const char *share,	/* I - Printer */
fc9f39
 	    const char *username,	/* I - Username */
fc9f39
 	    const char *password,	/* I - Password */
fc9f39
-	    const char *jobusername,	/* I - User who issued the print job */
fc9f39
-	    bool *need_auth)
fc9f39
-{				/* O - Need authentication? */
fc9f39
-	struct cli_state *cli;	/* New connection */
fc9f39
+	    const char *jobusername)	/* I - User who issued the print job */
fc9f39
+{
fc9f39
+	struct cli_state *cli = NULL;	/* New connection */
fc9f39
 	char           *myname = NULL;	/* Client name */
fc9f39
 	struct passwd  *pwd;
fc9f39
 	int flags = CLI_FULL_CONNECTION_USE_KERBEROS;
fc9f39
 	bool use_kerberos = false;
fc9f39
 	const char *user = username;
fc9f39
-	int cmp;
fc9f39
+	NTSTATUS nt_status;
fc9f39
 
fc9f39
 	/*
fc9f39
          * Get the names and addresses of the client and server...
fc9f39
          */
fc9f39
 	myname = get_myname(talloc_tos());
fc9f39
 	if (!myname) {
fc9f39
-		return NULL;
fc9f39
+		return NT_STATUS_NO_MEMORY;
fc9f39
 	}
fc9f39
 
fc9f39
 
fc9f39
-	cmp = strcmp(auth_info_required, "negotiate");
fc9f39
-	if (cmp == 0) {
fc9f39
+	if (strcmp(auth_info_required, "negotiate") == 0) {
fc9f39
 		if (!kerberos_ccache_is_valid()) {
fc9f39
-			return NULL;
fc9f39
+			fprintf(stderr,
fc9f39
+				"ERROR: No valid Kerberos credential cache "
fc9f39
+				"found!\n");
fc9f39
+			return NT_STATUS_LOGON_FAILURE;
fc9f39
 		}
fc9f39
 		user = jobusername;
fc9f39
 
fc9f39
 		use_kerberos = true;
fc9f39
 		fprintf(stderr,
fc9f39
 			"DEBUG: Try to connect using Kerberos ...\n");
fc9f39
-	}
fc9f39
-
fc9f39
-	cmp = strcmp(auth_info_required, "username,password");
fc9f39
-	if (cmp == 0) {
fc9f39
-		if (username == NULL || username[0] == '\0') {
fc9f39
-			return NULL;
fc9f39
+	} else if (strcmp(auth_info_required, "username,password") == 0) {
fc9f39
+		if (username == NULL) {
fc9f39
+			return NT_STATUS_INVALID_ACCOUNT_NAME;
fc9f39
 		}
fc9f39
 
fc9f39
 		/* Fallback to NTLM */
fc9f39
@@ -697,59 +713,83 @@ smb_connect(const char *workgroup,	/* I - Workgroup */
fc9f39
 
fc9f39
 		fprintf(stderr,
fc9f39
 			"DEBUG: Try to connect using username/password ...\n");
fc9f39
-	}
fc9f39
+	} else {
fc9f39
+		if (username != NULL) {
fc9f39
+			flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
fc9f39
+		} else if (kerberos_ccache_is_valid()) {
fc9f39
+			auth_info_required = "negotiate";
fc9f39
 
fc9f39
-	cmp = strcmp(auth_info_required, "none");
fc9f39
-	if (cmp == 0) {
fc9f39
-		fprintf(stderr,
fc9f39
-			"DEBUG: This backend doesn't support none auth ...\n");
fc9f39
-		return NULL;
fc9f39
+			user = jobusername;
fc9f39
+			use_kerberos = true;
fc9f39
+		} else {
fc9f39
+			fprintf(stderr,
fc9f39
+				"DEBUG: This backend requires credentials!\n");
fc9f39
+			return NT_STATUS_ACCESS_DENIED;
fc9f39
+		}
fc9f39
 	}
fc9f39
 
fc9f39
-	cli = smb_complete_connection(myname,
fc9f39
-				      server,
fc9f39
-				      port,
fc9f39
-				      user,
fc9f39
-				      password,
fc9f39
-				      workgroup,
fc9f39
-				      share,
fc9f39
-				      flags,
fc9f39
-				      need_auth);
fc9f39
-	if (cli != NULL) {
fc9f39
+	nt_status = smb_complete_connection(&cli,
fc9f39
+					    myname,
fc9f39
+					    server,
fc9f39
+					    port,
fc9f39
+					    user,
fc9f39
+					    password,
fc9f39
+					    workgroup,
fc9f39
+					    share,
fc9f39
+					    flags);
fc9f39
+	if (NT_STATUS_IS_OK(nt_status)) {
fc9f39
 		fprintf(stderr, "DEBUG: SMB connection established.\n");
fc9f39
-		return (cli);
fc9f39
+
fc9f39
+		*output_cli = cli;
fc9f39
+		return NT_STATUS_OK;
fc9f39
 	}
fc9f39
 
fc9f39
 	if (!use_kerberos) {
fc9f39
 		fprintf(stderr, "ERROR: SMB connection failed!\n");
fc9f39
-		return NULL;
fc9f39
+		return nt_status;
fc9f39
 	}
fc9f39
 
fc9f39
 	/* give a chance for a passwordless NTLMSSP session setup */
fc9f39
 	pwd = getpwuid(geteuid());
fc9f39
 	if (pwd == NULL) {
fc9f39
-		return NULL;
fc9f39
-	}
fc9f39
-
fc9f39
-	cli = smb_complete_connection(myname, server, port, pwd->pw_name, "",
fc9f39
-				      workgroup, share, 0, need_auth);
fc9f39
-
fc9f39
-	if (cli) {
fc9f39
+		return NT_STATUS_ACCESS_DENIED;
fc9f39
+	}
fc9f39
+
fc9f39
+	nt_status = smb_complete_connection(&cli,
fc9f39
+					    myname,
fc9f39
+					    server,
fc9f39
+					    port,
fc9f39
+					    pwd->pw_name,
fc9f39
+					    "",
fc9f39
+					    workgroup,
fc9f39
+					    share,
fc9f39
+					    0);
fc9f39
+	if (NT_STATUS_IS_OK(nt_status)) {
fc9f39
 		fputs("DEBUG: Connected with NTLMSSP...\n", stderr);
fc9f39
-		return (cli);
fc9f39
+
fc9f39
+		*output_cli = cli;
fc9f39
+		return NT_STATUS_OK;
fc9f39
 	}
fc9f39
 
fc9f39
 	/*
fc9f39
          * last try. Use anonymous authentication
fc9f39
          */
fc9f39
 
fc9f39
-	cli = smb_complete_connection(myname, server, port, "", "",
fc9f39
-				      workgroup, share, 0, need_auth);
fc9f39
-	/*
fc9f39
-         * Return the new connection...
fc9f39
-         */
fc9f39
-
fc9f39
-	return (cli);
fc9f39
+	nt_status = smb_complete_connection(&cli,
fc9f39
+					    myname,
fc9f39
+					    server,
fc9f39
+					    port,
fc9f39
+					    "",
fc9f39
+					    "",
fc9f39
+					    workgroup,
fc9f39
+					    share,
fc9f39
+					    0);
fc9f39
+	if (NT_STATUS_IS_OK(nt_status)) {
fc9f39
+		*output_cli = cli;
fc9f39
+		return NT_STATUS_OK;
fc9f39
+	}
fc9f39
+
fc9f39
+	return nt_status;
fc9f39
 }
fc9f39
 
fc9f39
 
fc9f39
@@ -795,7 +835,7 @@ smb_print(struct cli_state * cli,	/* I - SMB connection */
fc9f39
 	if (!NT_STATUS_IS_OK(nt_status)) {
fc9f39
 		fprintf(stderr, "ERROR: %s opening remote spool %s\n",
fc9f39
 			nt_errstr(nt_status), title);
fc9f39
-		return get_exit_code(cli, nt_status);
fc9f39
+		return get_exit_code(nt_status);
fc9f39
 	}
fc9f39
 
fc9f39
 	/*
fc9f39
@@ -813,7 +853,7 @@ smb_print(struct cli_state * cli,	/* I - SMB connection */
fc9f39
 		status = cli_writeall(cli, fnum, 0, (uint8_t *)buffer,
fc9f39
 				      tbytes, nbytes, NULL);
fc9f39
 		if (!NT_STATUS_IS_OK(status)) {
fc9f39
-			int ret = get_exit_code(cli, status);
fc9f39
+			int ret = get_exit_code(status);
fc9f39
 			fprintf(stderr, "ERROR: Error writing spool: %s\n",
fc9f39
 				nt_errstr(status));
fc9f39
 			fprintf(stderr, "DEBUG: Returning status %d...\n",
fc9f39
@@ -829,7 +869,7 @@ smb_print(struct cli_state * cli,	/* I - SMB connection */
fc9f39
 	if (!NT_STATUS_IS_OK(nt_status)) {
fc9f39
 		fprintf(stderr, "ERROR: %s closing remote spool %s\n",
fc9f39
 			nt_errstr(nt_status), title);
fc9f39
-		return get_exit_code(cli, nt_status);
fc9f39
+		return get_exit_code(nt_status);
fc9f39
 	} else {
fc9f39
 		return (0);
fc9f39
 	}
fc9f39
-- 
fc9f39
2.21.0
fc9f39