[PATCH] Fix cloud storage authentication attempt counting

Linus Torvalds torvalds at linux-foundation.org
Sun Aug 14 09:47:04 PDT 2016


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Sun, 14 Aug 2016 09:39:40 -0700
Subject: [PATCH] Fix cloud storage authentication attempt counting

The authentication count was a static counter in the authentication
callback, which gets incremented until we consider the authentication to
have failed.

The problem with that is that it doesn't get incremented for just _one_
authentication operation, it gets incremented each time you try to load
or save, so eventually the code considers authentication to have failed
even if nothing ever went wrong.

This fixes it by making it static to the whole git-access file, and have
each operation clear it before starting a new remote access.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

So I mainly use the subsurface cloud storage while traveling, and then the 
network tends to be so bad that I've dismissed the cloud storage failures 
as "real". But people continue to complain, and I just tested at home, and 
if you do multiple "Save to cloud storage", it *will* fail after a couple 
of attempts without this patch.

There may be other things triggering problems too, but this was at least 
one obvious bug in the code.

 core/git-access.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/core/git-access.c b/core/git-access.c
index 6fe90b0a97fb..458a7dd9a2f3 100644
--- a/core/git-access.c
+++ b/core/git-access.c
@@ -185,6 +185,8 @@ static int reset_to_remote(git_repository *repo, git_reference *local, const git
 	return 0;
 }
 
+static int auth_attempt = 0;
+
 int credential_ssh_cb(git_cred **out,
 		  const char *url,
 		  const char *username_from_url,
@@ -194,7 +196,6 @@ int credential_ssh_cb(git_cred **out,
 	(void) url;
 	(void) allowed_types;
 	(void) payload;
-	static int attempt = 0;
 
 	const char *priv_key = format_string("%s/%s", system_default_directory(), "ssrf_remote.key");
 	const char *passphrase = prefs.cloud_storage_password ? strdup(prefs.cloud_storage_password) : strdup("");
@@ -202,9 +203,8 @@ int credential_ssh_cb(git_cred **out,
 	/* Bail out from libgit authentication loop when credentials are
 	 * incorrect */
 
-	if (attempt++ > 2) {
+	if (auth_attempt++ > 2) {
 		report_error("Authentication to cloud storage failed.");
-		attempt = 0;
 		return GIT_EUSER;
 	}
 
@@ -221,16 +221,14 @@ int credential_https_cb(git_cred **out,
 	(void) username_from_url;
 	(void) payload;
 	(void) allowed_types;
-	static int attempt = 0;
 	const char *username = prefs.cloud_storage_email_encoded;
 	const char *password = prefs.cloud_storage_password ? strdup(prefs.cloud_storage_password) : strdup("");
 
 	/* Bail out from libgit authentication loop when credentials are
 	 * incorrect */
 
-	if (attempt++ > 2) {
+	if (auth_attempt++ > 2) {
 		report_error("Authentication to cloud storage failed.");
-		attempt = 0;
 		return GIT_EUSER;
 	}
 	return git_cred_userpass_plaintext_new(out, username, password);
@@ -273,6 +271,7 @@ static int update_remote(git_repository *repo, git_remote *origin, git_reference
 	refspec.count = 1;
 	refspec.strings = (char **)&name;
 
+	auth_attempt = 0;
 	opts.callbacks.push_transfer_progress = &push_transfer_progress_cb;
 	if (rt == RT_SSH)
 		opts.callbacks.credentials = credential_ssh_cb;
@@ -529,6 +528,7 @@ static int check_remote_status(git_repository *repo, git_remote *origin, const c
 		git_reference_list(&refspec, repo);
 		git_push_options opts = GIT_PUSH_OPTIONS_INIT;
 		opts.callbacks.transfer_progress = &transfer_progress_cb;
+		auth_attempt = 0;
 		if (rt == RT_SSH)
 			opts.callbacks.credentials = credential_ssh_cb;
 		else if (rt == RT_HTTPS)
@@ -592,6 +592,7 @@ int sync_with_remote(git_repository *repo, const char *remote, const char *branc
 		fprintf(stderr, "git storage: fetch remote\n");
 	git_fetch_options opts = GIT_FETCH_OPTIONS_INIT;
 	opts.callbacks.transfer_progress = &transfer_progress_cb;
+	auth_attempt = 0;
 	if (rt == RT_SSH)
 		opts.callbacks.credentials = credential_ssh_cb;
 	else if (rt == RT_HTTPS)
@@ -710,6 +711,7 @@ static git_repository *create_local_repo(const char *localdir, const char *remot
 	if (verbose)
 		fprintf(stderr, "git storage: create_local_repo\n");
 
+	auth_attempt = 0;
 	opts.fetch_opts.callbacks.transfer_progress = &transfer_progress_cb;
 	if (rt == RT_SSH)
 		opts.fetch_opts.callbacks.credentials = credential_ssh_cb;
-- 
2.9.2.729.ga42d7b6



More information about the subsurface mailing list