@matheusbsbs / matheus-bagliano / commits / 5a688fe470
Fork 0

"core.sharedrepository = 0mode" should set, not loosen

This fixes the behaviour of octal notation to how it is defined in the documentation, while keeping the traditional "loosen only" semantics intact for "group" and "everybody". Three main points of this patch are: - For an explicit octal notation, the internal shared_repository variable is set to a negative value, so that we can tell "group" (which is to "OR" in 0660) and 0660 (which is to "SET" to 0660); - git-init did not set shared_repository variable early enough to affect the initial creation of many files, notably copied templates and the configuration. We set it very early when a command-line option specifies a custom value. - Many codepaths create files inside $GIT_DIR by various ways that all involve mkstemp(), and then call move_temp_to_file() to rename it to its final destination. We can add adjust_shared_perm() call here; for the traditional "loosen-only", this would be a no-op for many codepaths because the mode is already loose enough, but with the new behaviour it makes a difference. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Mar 25, 2009 at 16:19 UTC 5a688fe4706462dfa0a7932ef0c82c335c47e9ab
5 files changed +77 -20
builtin-init-db.c
+10 -2
@@ -195,6 +195,8 @@ static int create_default_files(const char *template_path)
195
196 git_config(git_default_config, NULL);
197 is_bare_repository_cfg = init_is_bare_repository;
198 +
199 + /* reading existing config may have overwrote it */
200 if (init_shared_repository != -1)
201 shared_repository = init_shared_repository;
202
@@ -313,12 +315,15 @@ int init_db(const char *template_dir, unsigned int flags)
315 * and compatibility values for PERM_GROUP and
316 * PERM_EVERYBODY.
317 */
316 - if (shared_repository == PERM_GROUP)
318 + if (shared_repository < 0)
319 + /* force to the mode value */
320 + sprintf(buf, "0%o", -shared_repository);
321 + else if (shared_repository == PERM_GROUP)
322 sprintf(buf, "%d", OLD_PERM_GROUP);
323 else if (shared_repository == PERM_EVERYBODY)
324 sprintf(buf, "%d", OLD_PERM_EVERYBODY);
325 else
321 - sprintf(buf, "0%o", shared_repository);
326 + die("oops");
327 git_config_set("core.sharedrepository", buf);
328 git_config_set("receive.denyNonFastforwards", "true");
329 }
@@ -398,6 +403,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
403 usage(init_db_usage);
404 }
405
406 + if (init_shared_repository != -1)
407 + shared_repository = init_shared_repository;
408 +
409 /*
410 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
411 * without --bare. Catch the error early.
path.c
+21 -15
@@ -314,33 +314,39 @@ char *enter_repo(char *path, int strict)
314 int adjust_shared_perm(const char *path)
315 {
316 struct stat st;
317 - int mode;
317 + int mode, tweak, shared;
318
319 if (!shared_repository)
320 return 0;
321 if (lstat(path, &st) < 0)
322 return -1;
323 mode = st.st_mode;
324 -
325 - if (shared_repository) {
326 - int tweak = shared_repository;
327 - if (!(mode & S_IWUSR))
328 - tweak &= ~0222;
324 + if (shared_repository < 0)
325 + shared = -shared_repository;
326 + else
327 + shared = shared_repository;
328 + tweak = shared;
329 +
330 + if (!(mode & S_IWUSR))
331 + tweak &= ~0222;
332 + if (mode & S_IXUSR)
333 + /* Copy read bits to execute bits */
334 + tweak |= (tweak & 0444) >> 2;
335 + if (shared_repository < 0)
336 + mode = (mode & ~0777) | tweak;
337 + else
338 mode |= tweak;
330 - } else {
331 - /* Preserve old PERM_UMASK behaviour */
332 - if (mode & S_IWUSR)
333 - mode |= S_IWGRP;
334 - }
339
340 if (S_ISDIR(mode)) {
337 - mode |= FORCE_DIR_SET_GID;
338 -
341 /* Copy read bits to execute bits */
340 - mode |= (shared_repository & 0444) >> 2;
342 + mode |= (shared & 0444) >> 2;
343 + mode |= FORCE_DIR_SET_GID;
344 }
345
343 - if ((mode & st.st_mode) != mode && chmod(path, mode) < 0)
346 + if (((shared_repository < 0
347 + ? (st.st_mode & (FORCE_DIR_SET_GID | 0777))
348 + : (st.st_mode & mode)) != mode) &&
349 + chmod(path, mode) < 0)
350 return -2;
351 return 0;
352 }
setup.c
+2 -2
@@ -434,7 +434,7 @@ int git_config_perm(const char *var, const char *value)
434
435 /*
436 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
437 - * a chmod value.
437 + * a chmod value to restrict to.
438 */
439 switch (i) {
440 case PERM_UMASK: /* 0 */
@@ -456,7 +456,7 @@ int git_config_perm(const char *var, const char *value)
456 * Mask filemode value. Others can not get write permission.
457 * x flags for directories are handled separately.
458 */
459 - return i & 0666;
459 + return -(i & 0666);
460 }
461
462 int check_repository_format_version(const char *var, const char *value, void *cb)
sha1_file.c
+7 -1
@@ -2243,11 +2243,15 @@ static void write_sha1_file_prepare(const void *buf, unsigned long len,
2243 }
2244
2245 /*
2246 - * Move the just written object into its final resting place
2246 + * Move the just written object into its final resting place.
2247 + * NEEDSWORK: this should be renamed to finalize_temp_file() as
2248 + * "moving" is only a part of what it does, when no patch between
2249 + * master to pu changes the call sites of this function.
2250 */
2251 int move_temp_to_file(const char *tmpfile, const char *filename)
2252 {
2253 int ret = 0;
2254 +
2255 if (link(tmpfile, filename))
2256 ret = errno;
2257
@@ -2275,6 +2279,8 @@ int move_temp_to_file(const char *tmpfile, const char *filename)
2279 /* FIXME!!! Collision check here ? */
2280 }
2281
2282 + if (adjust_shared_perm(filename))
2283 + return error("unable to set permission to '%s'", filename);
2284 return 0;
2285 }
2286
t/t1301-shared-repo.sh
+37
@@ -126,4 +126,41 @@ test_expect_success 'git reflog expire honors core.sharedRepository' '
126 esac
127 '
128
129 +test_expect_success 'forced modes' '
130 + mkdir -p templates/hooks &&
131 + echo update-server-info >templates/hooks/post-update &&
132 + chmod +x templates/hooks/post-update &&
133 + echo : >random-file &&
134 + mkdir new &&
135 + (
136 + cd new &&
137 + umask 002 &&
138 + git init --shared=0660 --template=../templates &&
139 + >frotz &&
140 + git add frotz &&
141 + git commit -a -m initial &&
142 + git repack
143 + ) &&
144 + find new/.git -print |
145 + xargs ls -ld >actual &&
146 +
147 + # Everything must be unaccessible to others
148 + test -z "$(sed -n -e "/^.......---/d" actual)" &&
149 +
150 + # All directories must have 2770
151 + test -z "$(sed -n -e "/^drwxrws---/d" -e "/^d/p" actual)" &&
152 +
153 + # post-update hook must be 0770
154 + test -z "$(sed -n -e "/post-update/{
155 + /^-rwxrwx---/d
156 + p
157 + }" actual)" &&
158 +
159 + # All files inside objects must be 0440
160 + test -z "$(sed -n -e "/objects\//{
161 + /^d/d
162 + /^-r--r-----/d
163 + }" actual)"
164 +'
165 +
166 test_done