mingw: give more details about unsafe directory's ownership

Add domain/username in error message, if owner sid of repository and user sid are not equal on windows systems. Old 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 error message: ''' 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> Acked-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Sören Krecker committed Jan 8, 2024 at 18:38 UTC f755e092e8bf22e7bf3599d59a6a7fa562f6b61b
1 file changed +57 -13
compat/mingw.c
+57 -13
@@ -2671,6 +2671,30 @@ static PSID get_current_user_sid(void)
2671 return result;
2672 }
2673
2674 +static BOOL user_sid_to_user_name(PSID sid, LPSTR *str)
2675 +{
2676 + SID_NAME_USE pe_use;
2677 + DWORD len_user = 0, len_domain = 0;
2678 + BOOL translate_sid_to_user;
2679 +
2680 + /*
2681 + * returns only FALSE, because the string pointers are NULL
2682 + */
2683 + LookupAccountSidA(NULL, sid, NULL, &len_user, NULL, &len_domain,
2684 + &pe_use);
2685 + /*
2686 + * Alloc needed space of the strings
2687 + */
2688 + ALLOC_ARRAY((*str), (size_t)len_domain + (size_t)len_user);
2689 + translate_sid_to_user = LookupAccountSidA(NULL, sid,
2690 + (*str) + len_domain, &len_user, *str, &len_domain, &pe_use);
2691 + if (!translate_sid_to_user)
2692 + FREE_AND_NULL(*str);
2693 + else
2694 + (*str)[len_domain] = '/';
2695 + return translate_sid_to_user;
2696 +}
2697 +
2698 static int acls_supported(const char *path)
2699 {
2700 size_t offset = offset_1st_component(path);
@@ -2752,27 +2776,47 @@ int is_path_owned_by_current_sid(const char *path, struct strbuf *report)
2776 strbuf_addf(report, "'%s' is on a file system that does"
2777 "not record ownership\n", path);
2778 } else if (report) {
2755 - LPSTR str1, str2, to_free1 = NULL, to_free2 = NULL;
2779 + LPSTR str1, str2, str3, str4, to_free1 = NULL,
2780 + to_free3 = NULL, to_local_free2 = NULL,
2781 + to_local_free4 = NULL;
2782
2757 - if (ConvertSidToStringSidA(sid, &str1))
2783 + if (user_sid_to_user_name(sid, &str1))
2784 to_free1 = str1;
2785 else
2786 str1 = "(inconvertible)";
2761 -
2762 - if (!current_user_sid)
2763 - str2 = "(none)";
2764 - else if (!IsValidSid(current_user_sid))
2765 - str2 = "(invalid)";
2766 - else if (ConvertSidToStringSidA(current_user_sid, &str2))
2767 - to_free2 = str2;
2787 + if (ConvertSidToStringSidA(sid, &str2))
2788 + to_local_free2 = str2;
2789 else
2790 str2 = "(inconvertible)";
2791 +
2792 + if (!current_user_sid) {
2793 + str3 = "(none)";
2794 + str4 = "(none)";
2795 + }
2796 + else if (!IsValidSid(current_user_sid)) {
2797 + str3 = "(invalid)";
2798 + str4 = "(invalid)";
2799 + } else {
2800 + if (user_sid_to_user_name(current_user_sid,
2801 + &str3))
2802 + to_free3 = str3;
2803 + else
2804 + str3 = "(inconvertible)";
2805 + if (ConvertSidToStringSidA(current_user_sid,
2806 + &str4))
2807 + to_local_free4 = str4;
2808 + else
2809 + str4 = "(inconvertible)";
2810 + }
2811 strbuf_addf(report,
2812 "'%s' is owned by:\n"
2772 - "\t'%s'\nbut the current user is:\n"
2773 - "\t'%s'\n", path, str1, str2);
2774 - LocalFree(to_free1);
2775 - LocalFree(to_free2);
2813 + "\t%s (%s)\nbut the current user is:\n"
2814 + "\t%s (%s)\n",
2815 + path, str1, str2, str3, str4);
2816 + free(to_free1);
2817 + LocalFree(to_local_free2);
2818 + free(to_free3);
2819 + LocalFree(to_local_free4);
2820 }
2821 }
2822