git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Sören Krecker" <soekkle@freenet.de>
To: git@vger.kernel.org
Cc: sunshine@sunshineco.com, "Sören Krecker" <soekkle@freenet.de>
Subject: [PATCH v5 1/1] Adds domain/username to error message
Date: Thu,  4 Jan 2024 20:22:02 +0100	[thread overview]
Message-ID: <20240104192202.2124-2-soekkle@freenet.de> (raw)
In-Reply-To: <20240104192202.2124-1-soekkle@freenet.de>

Adds domain/username in error message, if owner sid of repository and
user sid are not equal on windows systems.

Old Prompted error message:
'''
fatal: detected dubious ownership in repository at 'C:/Users/test/source/repos/git'
'C:/Users/test/source/repos/git' is owned by:
	'S-1-5-21-571067702-4104414259-3379520149-500'
but the current user is:
	'S-1-5-21-571067702-4104414259-3379520149-1001'
To add an exception for this directory, call:

	git config --global --add safe.directory C:/Users/test/source/repos/git
'''

New prompted error massage:
'''
fatal: detected dubious ownership in repository at 'C:/Users/test/source/repos/git'
'C:/Users/test/source/repos/git' is owned by:
        'DESKTOP-L78JVA6/Administrator' (S-1-5-21-571067702-4104414259-3379520149-500)
but the current user is:
        'DESKTOP-L78JVA6/test' (S-1-5-21-571067702-4104414259-3379520149-1001)
To add an exception for this directory, call:

        git config --global --add safe.directory C:/Users/test/source/repos/git
'''

Signed-off-by: Sören Krecker <soekkle@freenet.de>
---
 compat/mingw.c | 64 ++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 51 insertions(+), 13 deletions(-)

diff --git a/compat/mingw.c b/compat/mingw.c
index 42053c1f65..6240387205 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -2684,6 +2684,26 @@ static PSID get_current_user_sid(void)
 	return result;
 }
 
+static BOOL user_sid_to_user_name(PSID sid, LPSTR *str)
+{
+	SID_NAME_USE pe_use;
+	DWORD len_user = 0, len_domain = 0;
+	BOOL translate_sid_to_user;
+
+	/* returns only FALSE, because the string pointers are NULL*/
+	LookupAccountSidA(NULL, sid, NULL, &len_user, NULL, &len_domain,
+			  &pe_use); 
+	/*Alloc needed space of the strings*/
+	ALLOC_ARRAY((*str), (size_t)len_domain + (size_t)len_user); 
+	translate_sid_to_user = LookupAccountSidA(NULL, sid, (*str) + len_domain, &len_user,
+				   *str, &len_domain, &pe_use);
+	if (translate_sid_to_user == FALSE)
+		FREE_AND_NULL(*str);
+	else
+		(*str)[len_domain] = '/';
+	return translate_sid_to_user;
+}
+
 static int acls_supported(const char *path)
 {
 	size_t offset = offset_1st_component(path);
@@ -2765,27 +2785,45 @@ int is_path_owned_by_current_sid(const char *path, struct strbuf *report)
 			strbuf_addf(report, "'%s' is on a file system that does "
 				    "not record ownership\n", path);
 		} else if (report) {
-			LPSTR str1, str2, to_free1 = NULL, to_free2 = NULL;
+			LPSTR str1, str2, str3, str4, to_free1 = NULL, to_free3 = NULL, to_local_free2=NULL, to_local_free4=NULL;
 
-			if (ConvertSidToStringSidA(sid, &str1))
+			if (user_sid_to_user_name(sid, &str1))
 				to_free1 = str1;
 			else
 				str1 = "(inconvertible)";
-
-			if (!current_user_sid)
-				str2 = "(none)";
-			else if (!IsValidSid(current_user_sid))
-				str2 = "(invalid)";
-			else if (ConvertSidToStringSidA(current_user_sid, &str2))
-				to_free2 = str2;
+			if (ConvertSidToStringSidA(sid, &str2))
+				to_local_free2 = str2;
 			else
 				str2 = "(inconvertible)";
+
+			if (!current_user_sid) {
+				str3 = "(none)";
+				str4 = "(none)";
+			}
+			else if (!IsValidSid(current_user_sid)) {
+				str3 = "(invalid)";
+				str4 = "(invalid)";
+			} else {
+				if (user_sid_to_user_name(current_user_sid,
+							  &str3))
+					to_free3 = str3;
+				else
+					str3 = "(inconvertible)";
+				if (ConvertSidToStringSidA(current_user_sid,
+							   &str4))
+					to_local_free4 = str4;
+				else
+					str4 = "(inconvertible)";
+			}
 			strbuf_addf(report,
 				    "'%s' is owned by:\n"
-				    "\t'%s'\nbut the current user is:\n"
-				    "\t'%s'\n", path, str1, str2);
-			LocalFree(to_free1);
-			LocalFree(to_free2);
+				    "\t'%s' (%s)\nbut the current user is:\n"
+				    "\t'%s' (%s)\n",
+				    path, str1, str2, str3, str4);
+			free(to_free1);
+			LocalFree(to_local_free2);
+			free(to_free3);
+			LocalFree(to_local_free4);
 		}
 	}
 
-- 
2.39.2


  reply	other threads:[~2024-01-04 19:22 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-29 12:03 [PATCH 0/1 v2] Replace SID with domain/username on Windows Sören Krecker
2023-12-29 12:03 ` [PATCH v2 1/1] Replace SID with domain/username Sören Krecker
2024-01-02 16:20   ` Junio C Hamano
2024-01-02 17:33     ` Junio C Hamano
2023-12-31  4:08 ` [PATCH 0/1 v2] Replace SID with domain/username on Windows Eric Sunshine
2023-12-31  9:12   ` [PATCH V3 0/1] " Sören Krecker
2023-12-31  9:12     ` [PATCH v3 1/1] Replace SID with domain/username Sören Krecker
2024-01-02 17:43       ` Junio C Hamano
2024-01-02 19:15         ` [PATCH V4 0/1] Replace SID with domain/username on Windows Sören Krecker
2024-01-02 19:15           ` [PATCH V4 1/1] Replace SID with domain/username Sören Krecker
2024-01-03  0:43             ` Junio C Hamano
2024-01-03  8:21               ` Matthias Aßhauer
2024-01-03 22:22                 ` Junio C Hamano
2024-01-04 19:22                 ` [PATCH v5 0/1] Replace SID with domain/username on Windows Sören Krecker
2024-01-04 19:22                   ` Sören Krecker [this message]
2024-01-04 20:09                     ` [PATCH v5 1/1] Adds domain/username to error message Junio C Hamano
2024-01-06 11:29                       ` [PATCH v6 0/1] mingw: give more details about unsafe directory's ownership Sören Krecker
2024-01-06 11:29                         ` [PATCH v6 1/1] " Sören Krecker
2024-01-07 20:02                           ` Johannes Sixt
2024-01-08 17:38                             ` [PATCH v6 0/1] mingw: give more details about unsafe directory's Sören Krecker
2024-01-08 17:38                               ` [PATCH v7 1/1] mingw: give more details about unsafe directory's ownership Sören Krecker
2024-01-08 19:18                                 ` Junio C Hamano
2024-01-09 19:27                                 ` Johannes Sixt
2024-01-09 20:06                                   ` Junio C Hamano
2024-01-09 21:05                                     ` Johannes Sixt
2024-01-09 22:34                                       ` Junio C Hamano
2024-01-08 17:51                               ` [PATCH v6 0/1] mingw: give more details about unsafe directory's Dragan Simic
2023-12-31  9:18     ` [PATCH V3 0/1] Replace SID with domain/username on Windows Eric Sunshine

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240104192202.2124-2-soekkle@freenet.de \
    --to=soekkle@freenet.de \
    --cc=git@vger.kernel.org \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).