Skip to content

Commit bcbbb4f

Browse files
committed
Windows: add support for a Windows-wide configuration
Between the libgit2 and the Git for Windows project, there has been a discussion how we could share Git configuration to avoid duplication (or worse: skew). Earlier, libgit2 was nice enough to just re-use Git for Windows' C:\Program Files (x86)\Git\etc\gitconfig but with the upcoming Git for Windows 2.x, there would be more paths to search, as we will have 64-bit and 32-bit versions, and the corresponding config files will be in %PROGRAMFILES%\Git\mingw64\etc and ...\mingw32\etc, respectively. Worse: there are portable Git for Windows versions out there which live in totally unrelated directories, still. Therefore we came to a consensus to use `%PROGRAMDATA%\Git\config` as the location for shared Git settings that are of wider interest than just Git for Windows. Of course, the configuration in `%PROGRAMDATA%\Git\config` has the widest reach, therefore it must take the lowest precedence, i.e. Git for Windows can still override settings in its `etc/gitconfig` file. Helped-by: Andreas Heiduk <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent d44acf7 commit bcbbb4f

File tree

7 files changed

+148
-7
lines changed

7 files changed

+148
-7
lines changed

Documentation/config.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ the Git commands' behavior. The files `.git/config` and optionally
77
repository are used to store the configuration for that repository, and
88
`$HOME/.gitconfig` is used to store a per-user configuration as
99
fallback values for the `.git/config` file. The file `/etc/gitconfig`
10-
can be used to store a system-wide default configuration.
10+
can be used to store a system-wide default configuration. On Windows,
11+
configuration can also be stored in `C:\ProgramData\Git\config`; This
12+
file will be used also by libgit2-based software.
1113

1214
The configuration variables are used by both the Git plumbing
1315
and the porcelains. The variables are divided into sections, wherein

Documentation/git-config.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,16 @@ FILES
272272
If not set explicitly with `--file`, there are four files where
273273
'git config' will search for configuration options:
274274

275+
$PROGRAMDATA/Git/config::
276+
(Windows-only) System-wide configuration file shared with other Git
277+
implementations. Typically `$PROGRAMDATA` points to `C:\ProgramData`.
278+
275279
$(prefix)/etc/gitconfig::
276280
System-wide configuration file.
281+
(Windows-only) This file contains only the settings which are
282+
specific for this installation of Git for Windows and which should
283+
not be shared with other Git implementations like JGit, libgit2.
284+
`--system` will select this file.
277285

278286
$XDG_CONFIG_HOME/git/config::
279287
Second user-specific configuration file. If $XDG_CONFIG_HOME is not set

Documentation/git.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,8 @@ for further details.
588588

589589
`GIT_CONFIG_NOSYSTEM`::
590590
Whether to skip reading settings from the system-wide
591-
`$(prefix)/etc/gitconfig` file. This environment variable can
591+
`$(prefix)/etc/gitconfig` file (and on Windows, also from the
592+
`%PROGRAMDATA%\Git\config` file). This environment variable can
592593
be used along with `$HOME` and `$XDG_CONFIG_HOME` to create a
593594
predictable environment for a picky script, or you can set it
594595
temporarily to avoid using a buggy `/etc/gitconfig` file while

compat/mingw.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "win32.h"
33
#include <conio.h>
44
#include <wchar.h>
5+
#include <aclapi.h>
6+
#include <sddl.h>
57
#include "../strbuf.h"
68
#include "../run-command.h"
79
#include "../cache.h"
@@ -2541,3 +2543,120 @@ int uname(struct utsname *buf)
25412543
"%u", (v >> 16) & 0x7fff);
25422544
return 0;
25432545
}
2546+
2547+
/*
2548+
* Verify that the file in question is owned by an administrator or system
2549+
* account, or at least by the current user.
2550+
*
2551+
* This function returns 1 if successful, 0 if the file is not owned by any of
2552+
* these, or -1 on error.
2553+
*/
2554+
static int validate_system_file_ownership(const char *path)
2555+
{
2556+
WCHAR wpath[MAX_PATH];
2557+
PSID owner_sid = NULL;
2558+
PSECURITY_DESCRIPTOR descriptor = NULL;
2559+
HANDLE token;
2560+
TOKEN_USER* info = NULL;
2561+
DWORD err, len;
2562+
int ret;
2563+
2564+
if (xutftowcs_path(wpath, path) < 0)
2565+
return -1;
2566+
2567+
err = GetNamedSecurityInfoW(wpath, SE_FILE_OBJECT,
2568+
OWNER_SECURITY_INFORMATION |
2569+
DACL_SECURITY_INFORMATION,
2570+
&owner_sid, NULL, NULL, NULL, &descriptor);
2571+
2572+
/* if the file does not exist, it does not have a valid owner */
2573+
if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) {
2574+
ret = 0;
2575+
owner_sid = NULL;
2576+
goto finish_validation;
2577+
}
2578+
2579+
if (err != ERROR_SUCCESS) {
2580+
ret = error(_("failed to validate '%s' (%ld)"), path, err);
2581+
owner_sid = NULL;
2582+
goto finish_validation;
2583+
}
2584+
2585+
if (!IsValidSid(owner_sid)) {
2586+
ret = error(_("invalid owner: '%s'"), path);
2587+
goto finish_validation;
2588+
}
2589+
2590+
if (IsWellKnownSid(owner_sid, WinBuiltinAdministratorsSid) ||
2591+
IsWellKnownSid(owner_sid, WinLocalSystemSid)) {
2592+
ret = 1;
2593+
goto finish_validation;
2594+
}
2595+
2596+
/* Obtain current user's SID */
2597+
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token) &&
2598+
!GetTokenInformation(token, TokenUser, NULL, 0, &len)) {
2599+
info = xmalloc((size_t)len);
2600+
if (!GetTokenInformation(token, TokenUser, info, len, &len))
2601+
FREE_AND_NULL(info);
2602+
}
2603+
2604+
if (!info)
2605+
ret = 0;
2606+
else {
2607+
ret = EqualSid(owner_sid, info->User.Sid) ? 1 : 0;
2608+
free(info);
2609+
}
2610+
2611+
finish_validation:
2612+
if (!ret && owner_sid) {
2613+
#define MAX_NAME_OR_DOMAIN 256
2614+
wchar_t owner_name[MAX_NAME_OR_DOMAIN];
2615+
wchar_t owner_domain[MAX_NAME_OR_DOMAIN];
2616+
wchar_t *p = NULL;
2617+
DWORD size = MAX_NAME_OR_DOMAIN;
2618+
SID_NAME_USE type;
2619+
char name[3 * MAX_NAME_OR_DOMAIN + 1];
2620+
2621+
if (!LookupAccountSidW(NULL, owner_sid, owner_name, &size,
2622+
owner_domain, &size, &type) ||
2623+
xwcstoutf(name, owner_name, ARRAY_SIZE(name)) < 0) {
2624+
if (!ConvertSidToStringSidW(owner_sid, &p))
2625+
strlcpy(name, "(unknown)", ARRAY_SIZE(name));
2626+
else {
2627+
if (xwcstoutf(name, p, ARRAY_SIZE(name)) < 0)
2628+
strlcpy(name, "(some user)",
2629+
ARRAY_SIZE(name));
2630+
LocalFree(p);
2631+
}
2632+
}
2633+
2634+
warning(_("'%s' has a dubious owner: '%s'.\n"
2635+
"For security reasons, it is therefore ignored.\n"
2636+
"To fix this, please transfer ownership to an "
2637+
"admininstrator."),
2638+
path, name);
2639+
}
2640+
2641+
if (descriptor)
2642+
LocalFree(descriptor);
2643+
2644+
return ret;
2645+
}
2646+
2647+
const char *program_data_config(void)
2648+
{
2649+
static struct strbuf path = STRBUF_INIT;
2650+
static unsigned initialized;
2651+
2652+
if (!initialized) {
2653+
const char *env = mingw_getenv("PROGRAMDATA");
2654+
if (env) {
2655+
strbuf_addf(&path, "%s/Git/config", env);
2656+
if (validate_system_file_ownership(path.buf) != 1)
2657+
strbuf_setlen(&path, 0);
2658+
}
2659+
initialized = 1;
2660+
}
2661+
return *path.buf ? path.buf : NULL;
2662+
}

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ static inline void convert_slashes(char *path)
445445
#define PATH_SEP ';'
446446
extern char *mingw_query_user_email(void);
447447
#define query_user_email mingw_query_user_email
448+
extern const char *program_data_config(void);
449+
#define git_program_data_config program_data_config
448450
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
449451
#define PRIuMAX "I64u"
450452
#define PRId64 "I64d"

config.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,11 +1711,16 @@ static int do_git_config_sequence(const struct config_options *opts,
17111711
repo_config = NULL;
17121712

17131713
current_parsing_scope = CONFIG_SCOPE_SYSTEM;
1714-
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK,
1715-
opts->system_gently ?
1716-
ACCESS_EACCES_OK : 0))
1717-
ret += git_config_from_file(fn, git_etc_gitconfig(),
1718-
data);
1714+
if (git_config_system()) {
1715+
int flags = opts->system_gently ? ACCESS_EACCES_OK : 0;
1716+
const char *program_data = git_program_data_config();
1717+
const char *etc = git_etc_gitconfig();
1718+
1719+
if (program_data && !access_or_die(program_data, R_OK, flags))
1720+
ret += git_config_from_file(fn, program_data, data);
1721+
if (!access_or_die(etc, R_OK, flags))
1722+
ret += git_config_from_file(fn, etc, data);
1723+
}
17191724

17201725
current_parsing_scope = CONFIG_SCOPE_GLOBAL;
17211726
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,10 @@ static inline char *git_find_last_dir_sep(const char *path)
421421
#endif
422422
#endif
423423

424+
#ifndef git_program_data_config
425+
#define git_program_data_config() NULL
426+
#endif
427+
424428
#if defined(__HP_cc) && (__HP_cc >= 61000)
425429
#define NORETURN __attribute__((noreturn))
426430
#define NORETURN_PTR

0 commit comments

Comments
 (0)