]>
Commit | Line | Data |
---|---|---|
355885d5 | 1 | #include "cache.h" |
cbd53a21 | 2 | #include "object-store.h" |
109cd76d | 3 | #include "repository.h" |
355885d5 MK |
4 | #include "object.h" |
5 | #include "blob.h" | |
6 | #include "tree.h" | |
7 | #include "tree-walk.h" | |
8 | #include "commit.h" | |
9 | #include "tag.h" | |
10 | #include "fsck.h" | |
cec097be | 11 | #include "refs.h" |
a2b26ffb | 12 | #include "url.h" |
a18fcc9f | 13 | #include "utf8.h" |
7b35efd7 | 14 | #include "decorate.h" |
159e7b08 | 15 | #include "oidset.h" |
27387444 | 16 | #include "packfile.h" |
ed8b10f6 JK |
17 | #include "submodule-config.h" |
18 | #include "config.h" | |
07259e74 | 19 | #include "credential.h" |
3ac68a93 | 20 | #include "help.h" |
159e7b08 JK |
21 | |
22 | static struct oidset gitmodules_found = OIDSET_INIT; | |
23 | static struct oidset gitmodules_done = OIDSET_INIT; | |
355885d5 | 24 | |
f50c4407 | 25 | #define FSCK_FATAL -1 |
f27d05b1 | 26 | #define FSCK_INFO -2 |
f50c4407 | 27 | |
c99ba492 | 28 | #define FOREACH_MSG_ID(FUNC) \ |
f50c4407 JS |
29 | /* fatal errors */ \ |
30 | FUNC(NUL_IN_HEADER, FATAL) \ | |
31 | FUNC(UNTERMINATED_HEADER, FATAL) \ | |
c99ba492 JS |
32 | /* errors */ \ |
33 | FUNC(BAD_DATE, ERROR) \ | |
34 | FUNC(BAD_DATE_OVERFLOW, ERROR) \ | |
35 | FUNC(BAD_EMAIL, ERROR) \ | |
36 | FUNC(BAD_NAME, ERROR) \ | |
37 | FUNC(BAD_OBJECT_SHA1, ERROR) \ | |
38 | FUNC(BAD_PARENT_SHA1, ERROR) \ | |
39 | FUNC(BAD_TAG_OBJECT, ERROR) \ | |
40 | FUNC(BAD_TIMEZONE, ERROR) \ | |
41 | FUNC(BAD_TREE, ERROR) \ | |
42 | FUNC(BAD_TREE_SHA1, ERROR) \ | |
43 | FUNC(BAD_TYPE, ERROR) \ | |
44 | FUNC(DUPLICATE_ENTRIES, ERROR) \ | |
45 | FUNC(MISSING_AUTHOR, ERROR) \ | |
46 | FUNC(MISSING_COMMITTER, ERROR) \ | |
47 | FUNC(MISSING_EMAIL, ERROR) \ | |
c99ba492 JS |
48 | FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \ |
49 | FUNC(MISSING_OBJECT, ERROR) \ | |
c99ba492 JS |
50 | FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \ |
51 | FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \ | |
52 | FUNC(MISSING_TAG, ERROR) \ | |
53 | FUNC(MISSING_TAG_ENTRY, ERROR) \ | |
c99ba492 | 54 | FUNC(MISSING_TREE, ERROR) \ |
159e7b08 | 55 | FUNC(MISSING_TREE_OBJECT, ERROR) \ |
c99ba492 JS |
56 | FUNC(MISSING_TYPE, ERROR) \ |
57 | FUNC(MISSING_TYPE_ENTRY, ERROR) \ | |
c9ad147f | 58 | FUNC(MULTIPLE_AUTHORS, ERROR) \ |
c99ba492 JS |
59 | FUNC(TREE_NOT_SORTED, ERROR) \ |
60 | FUNC(UNKNOWN_TYPE, ERROR) \ | |
c99ba492 | 61 | FUNC(ZERO_PADDED_DATE, ERROR) \ |
159e7b08 JK |
62 | FUNC(GITMODULES_MISSING, ERROR) \ |
63 | FUNC(GITMODULES_BLOB, ERROR) \ | |
0d68764d | 64 | FUNC(GITMODULES_LARGE, ERROR) \ |
ed8b10f6 | 65 | FUNC(GITMODULES_NAME, ERROR) \ |
b7b1fca1 | 66 | FUNC(GITMODULES_SYMLINK, ERROR) \ |
a124133e | 67 | FUNC(GITMODULES_URL, ERROR) \ |
1a7fd1fb | 68 | FUNC(GITMODULES_PATH, ERROR) \ |
bb92255e | 69 | FUNC(GITMODULES_UPDATE, ERROR) \ |
c99ba492 JS |
70 | /* warnings */ \ |
71 | FUNC(BAD_FILEMODE, WARN) \ | |
c99ba492 JS |
72 | FUNC(EMPTY_NAME, WARN) \ |
73 | FUNC(FULL_PATHNAME, WARN) \ | |
74 | FUNC(HAS_DOT, WARN) \ | |
75 | FUNC(HAS_DOTDOT, WARN) \ | |
76 | FUNC(HAS_DOTGIT, WARN) \ | |
c99ba492 | 77 | FUNC(NULL_SHA1, WARN) \ |
f27d05b1 | 78 | FUNC(ZERO_PADDED_FILEMODE, WARN) \ |
6d2d780f | 79 | FUNC(NUL_IN_COMMIT, WARN) \ |
f27d05b1 | 80 | /* infos (reported as warnings, but ignored by default) */ \ |
64eb14d3 | 81 | FUNC(GITMODULES_PARSE, INFO) \ |
f27d05b1 JS |
82 | FUNC(BAD_TAG_NAME, INFO) \ |
83 | FUNC(MISSING_TAGGER_ENTRY, INFO) | |
c99ba492 JS |
84 | |
85 | #define MSG_ID(id, msg_type) FSCK_MSG_##id, | |
86 | enum fsck_msg_id { | |
87 | FOREACH_MSG_ID(MSG_ID) | |
88 | FSCK_MSG_MAX | |
89 | }; | |
90 | #undef MSG_ID | |
91 | ||
f417eed8 | 92 | #define STR(x) #x |
a4a9cc19 | 93 | #define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type }, |
c99ba492 | 94 | static struct { |
f417eed8 JS |
95 | const char *id_string; |
96 | const char *downcased; | |
a4a9cc19 | 97 | const char *camelcased; |
c99ba492 JS |
98 | int msg_type; |
99 | } msg_id_info[FSCK_MSG_MAX + 1] = { | |
100 | FOREACH_MSG_ID(MSG_ID) | |
a4a9cc19 | 101 | { NULL, NULL, NULL, -1 } |
c99ba492 JS |
102 | }; |
103 | #undef MSG_ID | |
104 | ||
a46baac6 | 105 | static void prepare_msg_ids(void) |
f417eed8 JS |
106 | { |
107 | int i; | |
108 | ||
a46baac6 NTND |
109 | if (msg_id_info[0].downcased) |
110 | return; | |
111 | ||
112 | /* convert id_string to lower case, without underscores. */ | |
113 | for (i = 0; i < FSCK_MSG_MAX; i++) { | |
114 | const char *p = msg_id_info[i].id_string; | |
115 | int len = strlen(p); | |
116 | char *q = xmalloc(len); | |
117 | ||
118 | msg_id_info[i].downcased = q; | |
119 | while (*p) | |
120 | if (*p == '_') | |
121 | p++; | |
122 | else | |
123 | *(q)++ = tolower(*(p)++); | |
124 | *q = '\0'; | |
a4a9cc19 NTND |
125 | |
126 | p = msg_id_info[i].id_string; | |
127 | q = xmalloc(len); | |
128 | msg_id_info[i].camelcased = q; | |
129 | while (*p) { | |
130 | if (*p == '_') { | |
131 | p++; | |
132 | if (*p) | |
133 | *q++ = *p++; | |
134 | } else { | |
135 | *q++ = tolower(*p++); | |
136 | } | |
f417eed8 | 137 | } |
a4a9cc19 | 138 | *q = '\0'; |
f417eed8 | 139 | } |
a46baac6 NTND |
140 | } |
141 | ||
142 | static int parse_msg_id(const char *text) | |
143 | { | |
144 | int i; | |
145 | ||
146 | prepare_msg_ids(); | |
f417eed8 JS |
147 | |
148 | for (i = 0; i < FSCK_MSG_MAX; i++) | |
149 | if (!strcmp(text, msg_id_info[i].downcased)) | |
150 | return i; | |
151 | ||
152 | return -1; | |
153 | } | |
154 | ||
3ac68a93 NTND |
155 | void list_config_fsck_msg_ids(struct string_list *list, const char *prefix) |
156 | { | |
157 | int i; | |
158 | ||
159 | prepare_msg_ids(); | |
160 | ||
3ac68a93 | 161 | for (i = 0; i < FSCK_MSG_MAX; i++) |
a4a9cc19 | 162 | list_config_item(list, prefix, msg_id_info[i].camelcased); |
3ac68a93 NTND |
163 | } |
164 | ||
c99ba492 JS |
165 | static int fsck_msg_type(enum fsck_msg_id msg_id, |
166 | struct fsck_options *options) | |
167 | { | |
168 | int msg_type; | |
169 | ||
0282f4dc JS |
170 | assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX); |
171 | ||
172 | if (options->msg_type) | |
173 | msg_type = options->msg_type[msg_id]; | |
174 | else { | |
175 | msg_type = msg_id_info[msg_id].msg_type; | |
176 | if (options->strict && msg_type == FSCK_WARN) | |
177 | msg_type = FSCK_ERROR; | |
178 | } | |
c99ba492 JS |
179 | |
180 | return msg_type; | |
181 | } | |
182 | ||
0282f4dc JS |
183 | static int parse_msg_type(const char *str) |
184 | { | |
185 | if (!strcmp(str, "error")) | |
186 | return FSCK_ERROR; | |
187 | else if (!strcmp(str, "warn")) | |
188 | return FSCK_WARN; | |
efaba7cc JS |
189 | else if (!strcmp(str, "ignore")) |
190 | return FSCK_IGNORE; | |
0282f4dc JS |
191 | else |
192 | die("Unknown fsck message type: '%s'", str); | |
193 | } | |
194 | ||
5d477a33 JS |
195 | int is_valid_msg_type(const char *msg_id, const char *msg_type) |
196 | { | |
197 | if (parse_msg_id(msg_id) < 0) | |
198 | return 0; | |
199 | parse_msg_type(msg_type); | |
200 | return 1; | |
201 | } | |
202 | ||
0282f4dc JS |
203 | void fsck_set_msg_type(struct fsck_options *options, |
204 | const char *msg_id, const char *msg_type) | |
205 | { | |
206 | int id = parse_msg_id(msg_id), type; | |
207 | ||
208 | if (id < 0) | |
209 | die("Unhandled message id: %s", msg_id); | |
210 | type = parse_msg_type(msg_type); | |
211 | ||
f50c4407 JS |
212 | if (type != FSCK_ERROR && msg_id_info[id].msg_type == FSCK_FATAL) |
213 | die("Cannot demote %s to %s", msg_id, msg_type); | |
214 | ||
0282f4dc JS |
215 | if (!options->msg_type) { |
216 | int i; | |
b32fa95f JK |
217 | int *msg_type; |
218 | ALLOC_ARRAY(msg_type, FSCK_MSG_MAX); | |
0282f4dc JS |
219 | for (i = 0; i < FSCK_MSG_MAX; i++) |
220 | msg_type[i] = fsck_msg_type(i, options); | |
221 | options->msg_type = msg_type; | |
222 | } | |
223 | ||
224 | options->msg_type[id] = type; | |
225 | } | |
226 | ||
227 | void fsck_set_msg_types(struct fsck_options *options, const char *values) | |
228 | { | |
229 | char *buf = xstrdup(values), *to_free = buf; | |
230 | int done = 0; | |
231 | ||
232 | while (!done) { | |
233 | int len = strcspn(buf, " ,|"), equal; | |
234 | ||
235 | done = !buf[len]; | |
236 | if (!len) { | |
237 | buf++; | |
238 | continue; | |
239 | } | |
240 | buf[len] = '\0'; | |
241 | ||
242 | for (equal = 0; | |
243 | equal < len && buf[equal] != '=' && buf[equal] != ':'; | |
244 | equal++) | |
245 | buf[equal] = tolower(buf[equal]); | |
246 | buf[equal] = '\0'; | |
247 | ||
cd94c6f9 JS |
248 | if (!strcmp(buf, "skiplist")) { |
249 | if (equal == len) | |
250 | die("skiplist requires a path"); | |
24eb33eb | 251 | oidset_parse_file(&options->skiplist, buf + equal + 1); |
cd94c6f9 JS |
252 | buf += len + 1; |
253 | continue; | |
254 | } | |
255 | ||
0282f4dc JS |
256 | if (equal == len) |
257 | die("Missing '=': '%s'", buf); | |
355885d5 | 258 | |
0282f4dc JS |
259 | fsck_set_msg_type(options, buf, buf + equal + 1); |
260 | buf += len + 1; | |
261 | } | |
262 | free(to_free); | |
263 | } | |
264 | ||
71ab8fa8 JS |
265 | static void append_msg_id(struct strbuf *sb, const char *msg_id) |
266 | { | |
267 | for (;;) { | |
268 | char c = *(msg_id)++; | |
269 | ||
270 | if (!c) | |
271 | break; | |
272 | if (c != '_') | |
273 | strbuf_addch(sb, tolower(c)); | |
274 | else { | |
275 | assert(*msg_id); | |
276 | strbuf_addch(sb, *(msg_id)++); | |
277 | } | |
278 | } | |
279 | ||
280 | strbuf_addstr(sb, ": "); | |
281 | } | |
282 | ||
f5979376 JK |
283 | static int object_on_skiplist(struct fsck_options *opts, |
284 | const struct object_id *oid) | |
fb162877 | 285 | { |
f5979376 | 286 | return opts && oid && oidset_contains(&opts->skiplist, oid); |
fb162877 RJ |
287 | } |
288 | ||
38370253 JK |
289 | __attribute__((format (printf, 5, 6))) |
290 | static int report(struct fsck_options *options, | |
291 | const struct object_id *oid, enum object_type object_type, | |
292 | enum fsck_msg_id id, const char *fmt, ...) | |
c99ba492 JS |
293 | { |
294 | va_list ap; | |
295 | struct strbuf sb = STRBUF_INIT; | |
296 | int msg_type = fsck_msg_type(id, options), result; | |
297 | ||
efaba7cc JS |
298 | if (msg_type == FSCK_IGNORE) |
299 | return 0; | |
300 | ||
38370253 | 301 | if (object_on_skiplist(options, oid)) |
cd94c6f9 JS |
302 | return 0; |
303 | ||
f50c4407 JS |
304 | if (msg_type == FSCK_FATAL) |
305 | msg_type = FSCK_ERROR; | |
f27d05b1 JS |
306 | else if (msg_type == FSCK_INFO) |
307 | msg_type = FSCK_WARN; | |
f50c4407 | 308 | |
71ab8fa8 JS |
309 | append_msg_id(&sb, msg_id_info[id].id_string); |
310 | ||
c99ba492 JS |
311 | va_start(ap, fmt); |
312 | strbuf_vaddf(&sb, fmt, ap); | |
38370253 | 313 | result = options->error_func(options, oid, object_type, |
5afc4b1d | 314 | msg_type, sb.buf); |
c99ba492 JS |
315 | strbuf_release(&sb); |
316 | va_end(ap); | |
317 | ||
318 | return result; | |
319 | } | |
320 | ||
a59cfb32 JK |
321 | void fsck_enable_object_names(struct fsck_options *options) |
322 | { | |
323 | if (!options->object_names) | |
73390290 | 324 | options->object_names = kh_init_oid_map(); |
a59cfb32 JK |
325 | } |
326 | ||
73390290 JK |
327 | const char *fsck_get_object_name(struct fsck_options *options, |
328 | const struct object_id *oid) | |
7b35efd7 | 329 | { |
73390290 | 330 | khiter_t pos; |
7b35efd7 JS |
331 | if (!options->object_names) |
332 | return NULL; | |
73390290 JK |
333 | pos = kh_get_oid_map(options->object_names, *oid); |
334 | if (pos >= kh_end(options->object_names)) | |
335 | return NULL; | |
336 | return kh_value(options->object_names, pos); | |
7b35efd7 JS |
337 | } |
338 | ||
73390290 JK |
339 | void fsck_put_object_name(struct fsck_options *options, |
340 | const struct object_id *oid, | |
a59cfb32 | 341 | const char *fmt, ...) |
7b35efd7 JS |
342 | { |
343 | va_list ap; | |
344 | struct strbuf buf = STRBUF_INIT; | |
73390290 JK |
345 | khiter_t pos; |
346 | int hashret; | |
7b35efd7 JS |
347 | |
348 | if (!options->object_names) | |
349 | return; | |
73390290 JK |
350 | |
351 | pos = kh_put_oid_map(options->object_names, *oid, &hashret); | |
352 | if (!hashret) | |
7b35efd7 JS |
353 | return; |
354 | va_start(ap, fmt); | |
355 | strbuf_vaddf(&buf, fmt, ap); | |
73390290 | 356 | kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL); |
7b35efd7 JS |
357 | va_end(ap); |
358 | } | |
359 | ||
a59cfb32 | 360 | const char *fsck_describe_object(struct fsck_options *options, |
73390290 | 361 | const struct object_id *oid) |
90cf590f | 362 | { |
a59cfb32 JK |
363 | static struct strbuf bufs[] = { |
364 | STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT | |
365 | }; | |
366 | static int b = 0; | |
367 | struct strbuf *buf; | |
73390290 | 368 | const char *name = fsck_get_object_name(options, oid); |
a59cfb32 JK |
369 | |
370 | buf = bufs + b; | |
371 | b = (b + 1) % ARRAY_SIZE(bufs); | |
372 | strbuf_reset(buf); | |
73390290 | 373 | strbuf_addstr(buf, oid_to_hex(oid)); |
a59cfb32 JK |
374 | if (name) |
375 | strbuf_addf(buf, " (%s)", name); | |
90cf590f | 376 | |
a59cfb32 | 377 | return buf->buf; |
90cf590f JS |
378 | } |
379 | ||
22410549 | 380 | static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options) |
355885d5 MK |
381 | { |
382 | struct tree_desc desc; | |
383 | struct name_entry entry; | |
384 | int res = 0; | |
7b35efd7 | 385 | const char *name; |
355885d5 MK |
386 | |
387 | if (parse_tree(tree)) | |
388 | return -1; | |
389 | ||
73390290 | 390 | name = fsck_get_object_name(options, &tree->object.oid); |
8354fa3d DT |
391 | if (init_tree_desc_gently(&desc, tree->buffer, tree->size)) |
392 | return -1; | |
393 | while (tree_entry_gently(&desc, &entry)) { | |
7b35efd7 | 394 | struct object *obj; |
355885d5 MK |
395 | int result; |
396 | ||
397 | if (S_ISGITLINK(entry.mode)) | |
398 | continue; | |
7b35efd7 JS |
399 | |
400 | if (S_ISDIR(entry.mode)) { | |
ea82b2a0 | 401 | obj = (struct object *)lookup_tree(the_repository, &entry.oid); |
2720f6db | 402 | if (name && obj) |
73390290 | 403 | fsck_put_object_name(options, &entry.oid, "%s%s/", |
a59cfb32 | 404 | name, entry.path); |
7b35efd7 JS |
405 | result = options->walk(obj, OBJ_TREE, data, options); |
406 | } | |
407 | else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) { | |
ea82b2a0 | 408 | obj = (struct object *)lookup_blob(the_repository, &entry.oid); |
2720f6db | 409 | if (name && obj) |
73390290 | 410 | fsck_put_object_name(options, &entry.oid, "%s%s", |
a59cfb32 | 411 | name, entry.path); |
7b35efd7 JS |
412 | result = options->walk(obj, OBJ_BLOB, data, options); |
413 | } | |
355885d5 | 414 | else { |
82247e9b | 415 | result = error("in tree %s: entry %s has bad mode %.6o", |
73390290 | 416 | fsck_describe_object(options, &tree->object.oid), |
a59cfb32 | 417 | entry.path, entry.mode); |
355885d5 MK |
418 | } |
419 | if (result < 0) | |
420 | return result; | |
421 | if (!res) | |
422 | res = result; | |
423 | } | |
424 | return res; | |
425 | } | |
426 | ||
22410549 | 427 | static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options) |
355885d5 | 428 | { |
7b35efd7 | 429 | int counter = 0, generation = 0, name_prefix_len = 0; |
355885d5 MK |
430 | struct commit_list *parents; |
431 | int res; | |
432 | int result; | |
7b35efd7 | 433 | const char *name; |
355885d5 MK |
434 | |
435 | if (parse_commit(commit)) | |
436 | return -1; | |
437 | ||
73390290 | 438 | name = fsck_get_object_name(options, &commit->object.oid); |
7b35efd7 | 439 | if (name) |
73390290 | 440 | fsck_put_object_name(options, get_commit_tree_oid(commit), |
a59cfb32 | 441 | "%s:", name); |
7b35efd7 | 442 | |
2e27bd77 DS |
443 | result = options->walk((struct object *)get_commit_tree(commit), |
444 | OBJ_TREE, data, options); | |
355885d5 MK |
445 | if (result < 0) |
446 | return result; | |
447 | res = result; | |
448 | ||
449 | parents = commit->parents; | |
7b35efd7 JS |
450 | if (name && parents) { |
451 | int len = strlen(name), power; | |
452 | ||
453 | if (len && name[len - 1] == '^') { | |
454 | generation = 1; | |
455 | name_prefix_len = len - 1; | |
456 | } | |
457 | else { /* parse ~<generation> suffix */ | |
458 | for (generation = 0, power = 1; | |
459 | len && isdigit(name[len - 1]); | |
460 | power *= 10) | |
461 | generation += power * (name[--len] - '0'); | |
462 | if (power > 1 && len && name[len - 1] == '~') | |
463 | name_prefix_len = len - 1; | |
464 | } | |
465 | } | |
466 | ||
355885d5 | 467 | while (parents) { |
7b35efd7 | 468 | if (name) { |
73390290 | 469 | struct object_id *oid = &parents->item->object.oid; |
7b35efd7 | 470 | |
b84c7838 | 471 | if (counter++) |
73390290 | 472 | fsck_put_object_name(options, oid, "%s^%d", |
a59cfb32 | 473 | name, counter); |
7b35efd7 | 474 | else if (generation > 0) |
73390290 | 475 | fsck_put_object_name(options, oid, "%.*s~%d", |
a59cfb32 JK |
476 | name_prefix_len, name, |
477 | generation + 1); | |
7b35efd7 | 478 | else |
73390290 | 479 | fsck_put_object_name(options, oid, "%s^", name); |
7b35efd7 | 480 | } |
22410549 | 481 | result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options); |
355885d5 MK |
482 | if (result < 0) |
483 | return result; | |
484 | if (!res) | |
485 | res = result; | |
486 | parents = parents->next; | |
487 | } | |
488 | return res; | |
489 | } | |
490 | ||
22410549 | 491 | static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options) |
355885d5 | 492 | { |
73390290 | 493 | const char *name = fsck_get_object_name(options, &tag->object.oid); |
7b35efd7 | 494 | |
355885d5 MK |
495 | if (parse_tag(tag)) |
496 | return -1; | |
7b35efd7 | 497 | if (name) |
73390290 | 498 | fsck_put_object_name(options, &tag->tagged->oid, "%s", name); |
22410549 | 499 | return options->walk(tag->tagged, OBJ_ANY, data, options); |
355885d5 MK |
500 | } |
501 | ||
22410549 | 502 | int fsck_walk(struct object *obj, void *data, struct fsck_options *options) |
355885d5 MK |
503 | { |
504 | if (!obj) | |
505 | return -1; | |
a2b22854 JK |
506 | |
507 | if (obj->type == OBJ_NONE) | |
109cd76d | 508 | parse_object(the_repository, &obj->oid); |
a2b22854 | 509 | |
355885d5 MK |
510 | switch (obj->type) { |
511 | case OBJ_BLOB: | |
512 | return 0; | |
513 | case OBJ_TREE: | |
22410549 | 514 | return fsck_walk_tree((struct tree *)obj, data, options); |
355885d5 | 515 | case OBJ_COMMIT: |
22410549 | 516 | return fsck_walk_commit((struct commit *)obj, data, options); |
355885d5 | 517 | case OBJ_TAG: |
22410549 | 518 | return fsck_walk_tag((struct tag *)obj, data, options); |
355885d5 | 519 | default: |
a59cfb32 | 520 | error("Unknown object type for %s", |
73390290 | 521 | fsck_describe_object(options, &obj->oid)); |
355885d5 MK |
522 | return -1; |
523 | } | |
524 | } | |
ba002f3b MK |
525 | |
526 | /* | |
527 | * The entries in a tree are ordered in the _path_ order, | |
528 | * which means that a directory entry is ordered by adding | |
529 | * a slash to the end of it. | |
530 | * | |
531 | * So a directory called "a" is ordered _after_ a file | |
532 | * called "a.c", because "a/" sorts after "a.c". | |
533 | */ | |
534 | #define TREE_UNORDERED (-1) | |
535 | #define TREE_HAS_DUPS (-2) | |
536 | ||
537 | static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2) | |
538 | { | |
539 | int len1 = strlen(name1); | |
540 | int len2 = strlen(name2); | |
541 | int len = len1 < len2 ? len1 : len2; | |
542 | unsigned char c1, c2; | |
543 | int cmp; | |
544 | ||
545 | cmp = memcmp(name1, name2, len); | |
546 | if (cmp < 0) | |
547 | return 0; | |
548 | if (cmp > 0) | |
549 | return TREE_UNORDERED; | |
550 | ||
551 | /* | |
552 | * Ok, the first <len> characters are the same. | |
553 | * Now we need to order the next one, but turn | |
554 | * a '\0' into a '/' for a directory entry. | |
555 | */ | |
556 | c1 = name1[len]; | |
557 | c2 = name2[len]; | |
558 | if (!c1 && !c2) | |
559 | /* | |
560 | * git-write-tree used to write out a nonsense tree that has | |
561 | * entries with the same name, one blob and one tree. Make | |
562 | * sure we do not have duplicate entries. | |
563 | */ | |
564 | return TREE_HAS_DUPS; | |
565 | if (!c1 && S_ISDIR(mode1)) | |
566 | c1 = '/'; | |
567 | if (!c2 && S_ISDIR(mode2)) | |
568 | c2 = '/'; | |
569 | return c1 < c2 ? 0 : TREE_UNORDERED; | |
570 | } | |
571 | ||
b2f2039c | 572 | static int fsck_tree(const struct object_id *oid, |
23a173a7 JK |
573 | const char *buffer, unsigned long size, |
574 | struct fsck_options *options) | |
ba002f3b | 575 | { |
8354fa3d | 576 | int retval = 0; |
c479d14a | 577 | int has_null_sha1 = 0; |
ba002f3b MK |
578 | int has_full_path = 0; |
579 | int has_empty_name = 0; | |
5d34a435 JK |
580 | int has_dot = 0; |
581 | int has_dotdot = 0; | |
5c17f512 | 582 | int has_dotgit = 0; |
ba002f3b MK |
583 | int has_zero_pad = 0; |
584 | int has_bad_modes = 0; | |
585 | int has_dup_entries = 0; | |
586 | int not_properly_sorted = 0; | |
587 | struct tree_desc desc; | |
588 | unsigned o_mode; | |
589 | const char *o_name; | |
ba002f3b | 590 | |
23a173a7 | 591 | if (init_tree_desc_gently(&desc, buffer, size)) { |
b2f2039c | 592 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree"); |
8354fa3d DT |
593 | return retval; |
594 | } | |
ba002f3b MK |
595 | |
596 | o_mode = 0; | |
597 | o_name = NULL; | |
ba002f3b MK |
598 | |
599 | while (desc.size) { | |
5ec1e728 | 600 | unsigned short mode; |
288a74bc | 601 | const char *name, *backslash; |
ce6663a9 | 602 | const struct object_id *oid; |
ba002f3b | 603 | |
ce6663a9 | 604 | oid = tree_entry_extract(&desc, &name, &mode); |
ba002f3b | 605 | |
ce6663a9 | 606 | has_null_sha1 |= is_null_oid(oid); |
effd12ec HS |
607 | has_full_path |= !!strchr(name, '/'); |
608 | has_empty_name |= !*name; | |
609 | has_dot |= !strcmp(name, "."); | |
610 | has_dotdot |= !strcmp(name, ".."); | |
ed9c3220 | 611 | has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name); |
ba002f3b | 612 | has_zero_pad |= *(char *)desc.buffer == '0'; |
159e7b08 | 613 | |
b7b1fca1 JK |
614 | if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) { |
615 | if (!S_ISLNK(mode)) | |
616 | oidset_insert(&gitmodules_found, oid); | |
617 | else | |
38370253 | 618 | retval += report(options, |
b2f2039c | 619 | oid, OBJ_TREE, |
b7b1fca1 JK |
620 | FSCK_MSG_GITMODULES_SYMLINK, |
621 | ".gitmodules is a symbolic link"); | |
622 | } | |
159e7b08 | 623 | |
288a74bc JS |
624 | if ((backslash = strchr(name, '\\'))) { |
625 | while (backslash) { | |
626 | backslash++; | |
627 | has_dotgit |= is_ntfs_dotgit(backslash); | |
bdfef049 JS |
628 | if (is_ntfs_dotgitmodules(backslash)) { |
629 | if (!S_ISLNK(mode)) | |
630 | oidset_insert(&gitmodules_found, oid); | |
631 | else | |
7034cd09 | 632 | retval += report(options, oid, OBJ_TREE, |
bdfef049 JS |
633 | FSCK_MSG_GITMODULES_SYMLINK, |
634 | ".gitmodules is a symbolic link"); | |
635 | } | |
288a74bc JS |
636 | backslash = strchr(backslash, '\\'); |
637 | } | |
638 | } | |
639 | ||
8354fa3d | 640 | if (update_tree_entry_gently(&desc)) { |
b2f2039c | 641 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree"); |
8354fa3d DT |
642 | break; |
643 | } | |
ba002f3b MK |
644 | |
645 | switch (mode) { | |
646 | /* | |
647 | * Standard modes.. | |
648 | */ | |
649 | case S_IFREG | 0755: | |
650 | case S_IFREG | 0644: | |
651 | case S_IFLNK: | |
652 | case S_IFDIR: | |
653 | case S_IFGITLINK: | |
654 | break; | |
655 | /* | |
656 | * This is nonstandard, but we had a few of these | |
657 | * early on when we honored the full set of mode | |
658 | * bits.. | |
659 | */ | |
660 | case S_IFREG | 0664: | |
22410549 | 661 | if (!options->strict) |
ba002f3b | 662 | break; |
1cf01a34 | 663 | /* fallthrough */ |
ba002f3b MK |
664 | default: |
665 | has_bad_modes = 1; | |
666 | } | |
667 | ||
668 | if (o_name) { | |
669 | switch (verify_ordered(o_mode, o_name, mode, name)) { | |
670 | case TREE_UNORDERED: | |
671 | not_properly_sorted = 1; | |
672 | break; | |
673 | case TREE_HAS_DUPS: | |
674 | has_dup_entries = 1; | |
675 | break; | |
676 | default: | |
677 | break; | |
678 | } | |
679 | } | |
680 | ||
681 | o_mode = mode; | |
682 | o_name = name; | |
ba002f3b MK |
683 | } |
684 | ||
c479d14a | 685 | if (has_null_sha1) |
b2f2039c | 686 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1"); |
ba002f3b | 687 | if (has_full_path) |
b2f2039c | 688 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_FULL_PATHNAME, "contains full pathnames"); |
ba002f3b | 689 | if (has_empty_name) |
b2f2039c | 690 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_EMPTY_NAME, "contains empty pathname"); |
5d34a435 | 691 | if (has_dot) |
b2f2039c | 692 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOT, "contains '.'"); |
5d34a435 | 693 | if (has_dotdot) |
b2f2039c | 694 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTDOT, "contains '..'"); |
5c17f512 | 695 | if (has_dotgit) |
b2f2039c | 696 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTGIT, "contains '.git'"); |
ba002f3b | 697 | if (has_zero_pad) |
b2f2039c | 698 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes"); |
ba002f3b | 699 | if (has_bad_modes) |
b2f2039c | 700 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_FILEMODE, "contains bad file modes"); |
ba002f3b | 701 | if (has_dup_entries) |
b2f2039c | 702 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries"); |
ba002f3b | 703 | if (not_properly_sorted) |
b2f2039c | 704 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted"); |
ba002f3b MK |
705 | return retval; |
706 | } | |
707 | ||
84d18c0b | 708 | static int verify_headers(const void *data, unsigned long size, |
cc579000 JK |
709 | const struct object_id *oid, enum object_type type, |
710 | struct fsck_options *options) | |
4d0d8975 JS |
711 | { |
712 | const char *buffer = (const char *)data; | |
713 | unsigned long i; | |
714 | ||
715 | for (i = 0; i < size; i++) { | |
716 | switch (buffer[i]) { | |
717 | case '\0': | |
cc579000 | 718 | return report(options, oid, type, |
c99ba492 JS |
719 | FSCK_MSG_NUL_IN_HEADER, |
720 | "unterminated header: NUL at offset %ld", i); | |
4d0d8975 JS |
721 | case '\n': |
722 | if (i + 1 < size && buffer[i + 1] == '\n') | |
723 | return 0; | |
724 | } | |
725 | } | |
726 | ||
84d18c0b JH |
727 | /* |
728 | * We did not find double-LF that separates the header | |
729 | * and the body. Not having a body is not a crime but | |
730 | * we do want to see the terminating LF for the last header | |
731 | * line. | |
732 | */ | |
733 | if (size && buffer[size - 1] == '\n') | |
734 | return 0; | |
735 | ||
cc579000 | 736 | return report(options, oid, type, |
c99ba492 | 737 | FSCK_MSG_UNTERMINATED_HEADER, "unterminated header"); |
4d0d8975 JS |
738 | } |
739 | ||
78543993 JK |
740 | static int fsck_ident(const char **ident, |
741 | const struct object_id *oid, enum object_type type, | |
742 | struct fsck_options *options) | |
daae1922 | 743 | { |
e6826e33 | 744 | const char *p = *ident; |
d4b8de04 JK |
745 | char *end; |
746 | ||
e6826e33 JS |
747 | *ident = strchrnul(*ident, '\n'); |
748 | if (**ident == '\n') | |
749 | (*ident)++; | |
750 | ||
751 | if (*p == '<') | |
78543993 | 752 | return report(options, oid, type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email"); |
e6826e33 JS |
753 | p += strcspn(p, "<>\n"); |
754 | if (*p == '>') | |
78543993 | 755 | return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name"); |
e6826e33 | 756 | if (*p != '<') |
78543993 | 757 | return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email"); |
e6826e33 | 758 | if (p[-1] != ' ') |
78543993 | 759 | return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email"); |
e6826e33 JS |
760 | p++; |
761 | p += strcspn(p, "<>\n"); | |
762 | if (*p != '>') | |
78543993 | 763 | return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email"); |
e6826e33 JS |
764 | p++; |
765 | if (*p != ' ') | |
78543993 | 766 | return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date"); |
e6826e33 JS |
767 | p++; |
768 | if (*p == '0' && p[1] != ' ') | |
78543993 | 769 | return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date"); |
1aeb7e75 | 770 | if (date_overflows(parse_timestamp(p, &end, 10))) |
78543993 | 771 | return report(options, oid, type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow"); |
e6826e33 | 772 | if ((end == p || *end != ' ')) |
78543993 | 773 | return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date"); |
e6826e33 JS |
774 | p = end + 1; |
775 | if ((*p != '+' && *p != '-') || | |
776 | !isdigit(p[1]) || | |
777 | !isdigit(p[2]) || | |
778 | !isdigit(p[3]) || | |
779 | !isdigit(p[4]) || | |
780 | (p[5] != '\n')) | |
78543993 | 781 | return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone"); |
e6826e33 | 782 | p += 6; |
daae1922 JN |
783 | return 0; |
784 | } | |
785 | ||
c5b4269b JK |
786 | static int fsck_commit(const struct object_id *oid, |
787 | const char *buffer, unsigned long size, | |
788 | struct fsck_options *options) | |
ba002f3b | 789 | { |
f648ee70 | 790 | struct object_id tree_oid, parent_oid; |
ec652315 | 791 | unsigned author_count; |
daae1922 | 792 | int err; |
6d2d780f | 793 | const char *buffer_begin = buffer; |
c54f5ca9 | 794 | const char *p; |
ba002f3b | 795 | |
c5b4269b | 796 | if (verify_headers(buffer, size, oid, OBJ_COMMIT, options)) |
4d0d8975 JS |
797 | return -1; |
798 | ||
cf4fff57 | 799 | if (!skip_prefix(buffer, "tree ", &buffer)) |
c5b4269b | 800 | return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line"); |
c54f5ca9 | 801 | if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') { |
c5b4269b | 802 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1"); |
b3584761 JS |
803 | if (err) |
804 | return err; | |
805 | } | |
c54f5ca9 | 806 | buffer = p + 1; |
cf4fff57 | 807 | while (skip_prefix(buffer, "parent ", &buffer)) { |
f648ee70 | 808 | if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') { |
c5b4269b | 809 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1"); |
b3584761 JS |
810 | if (err) |
811 | return err; | |
812 | } | |
c54f5ca9 | 813 | buffer = p + 1; |
ba002f3b | 814 | } |
c9ad147f JS |
815 | author_count = 0; |
816 | while (skip_prefix(buffer, "author ", &buffer)) { | |
817 | author_count++; | |
c5b4269b | 818 | err = fsck_ident(&buffer, oid, OBJ_COMMIT, options); |
c9ad147f JS |
819 | if (err) |
820 | return err; | |
ba002f3b | 821 | } |
c9ad147f | 822 | if (author_count < 1) |
c5b4269b | 823 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line"); |
c9ad147f | 824 | else if (author_count > 1) |
c5b4269b | 825 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines"); |
daae1922 JN |
826 | if (err) |
827 | return err; | |
cf4fff57 | 828 | if (!skip_prefix(buffer, "committer ", &buffer)) |
c5b4269b JK |
829 | return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line"); |
830 | err = fsck_ident(&buffer, oid, OBJ_COMMIT, options); | |
daae1922 JN |
831 | if (err) |
832 | return err; | |
6d2d780f | 833 | if (memchr(buffer_begin, '\0', size)) { |
c5b4269b | 834 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT, |
6d2d780f JH |
835 | "NUL byte in the commit object body"); |
836 | if (err) | |
837 | return err; | |
838 | } | |
ba002f3b MK |
839 | return 0; |
840 | } | |
841 | ||
103fb6d4 | 842 | static int fsck_tag(const struct object_id *oid, const char *buffer, |
2175a0c6 | 843 | unsigned long size, struct fsck_options *options) |
cec097be | 844 | { |
f648ee70 | 845 | struct object_id tagged_oid; |
cec097be | 846 | int ret = 0; |
23a173a7 | 847 | char *eol; |
cec097be | 848 | struct strbuf sb = STRBUF_INIT; |
c54f5ca9 | 849 | const char *p; |
cec097be | 850 | |
103fb6d4 | 851 | ret = verify_headers(buffer, size, oid, OBJ_TAG, options); |
8a272f29 | 852 | if (ret) |
cec097be JS |
853 | goto done; |
854 | ||
855 | if (!skip_prefix(buffer, "object ", &buffer)) { | |
103fb6d4 | 856 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line"); |
cec097be JS |
857 | goto done; |
858 | } | |
f648ee70 | 859 | if (parse_oid_hex(buffer, &tagged_oid, &p) || *p != '\n') { |
103fb6d4 | 860 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1"); |
7d7d5b05 JS |
861 | if (ret) |
862 | goto done; | |
cec097be | 863 | } |
c54f5ca9 | 864 | buffer = p + 1; |
cec097be JS |
865 | |
866 | if (!skip_prefix(buffer, "type ", &buffer)) { | |
103fb6d4 | 867 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line"); |
cec097be JS |
868 | goto done; |
869 | } | |
870 | eol = strchr(buffer, '\n'); | |
871 | if (!eol) { | |
103fb6d4 | 872 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line"); |
cec097be JS |
873 | goto done; |
874 | } | |
875 | if (type_from_string_gently(buffer, eol - buffer, 1) < 0) | |
103fb6d4 | 876 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value"); |
cec097be JS |
877 | if (ret) |
878 | goto done; | |
879 | buffer = eol + 1; | |
880 | ||
881 | if (!skip_prefix(buffer, "tag ", &buffer)) { | |
103fb6d4 | 882 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line"); |
cec097be JS |
883 | goto done; |
884 | } | |
885 | eol = strchr(buffer, '\n'); | |
886 | if (!eol) { | |
103fb6d4 | 887 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line"); |
cec097be JS |
888 | goto done; |
889 | } | |
890 | strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer); | |
f27d05b1 | 891 | if (check_refname_format(sb.buf, 0)) { |
103fb6d4 | 892 | ret = report(options, oid, OBJ_TAG, |
38370253 JK |
893 | FSCK_MSG_BAD_TAG_NAME, |
894 | "invalid 'tag' name: %.*s", | |
895 | (int)(eol - buffer), buffer); | |
f27d05b1 JS |
896 | if (ret) |
897 | goto done; | |
898 | } | |
cec097be JS |
899 | buffer = eol + 1; |
900 | ||
f27d05b1 | 901 | if (!skip_prefix(buffer, "tagger ", &buffer)) { |
cec097be | 902 | /* early tags do not contain 'tagger' lines; warn only */ |
103fb6d4 | 903 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line"); |
f27d05b1 JS |
904 | if (ret) |
905 | goto done; | |
906 | } | |
cec097be | 907 | else |
103fb6d4 | 908 | ret = fsck_ident(&buffer, oid, OBJ_TAG, options); |
cec097be JS |
909 | |
910 | done: | |
911 | strbuf_release(&sb); | |
cec097be JS |
912 | return ret; |
913 | } | |
914 | ||
a2b26ffb JN |
915 | /* |
916 | * Like builtin/submodule--helper.c's starts_with_dot_slash, but without | |
917 | * relying on the platform-dependent is_dir_sep helper. | |
918 | * | |
919 | * This is for use in checking whether a submodule URL is interpreted as | |
920 | * relative to the current directory on any platform, since \ is a | |
921 | * directory separator on Windows but not on other platforms. | |
922 | */ | |
923 | static int starts_with_dot_slash(const char *str) | |
924 | { | |
925 | return str[0] == '.' && (str[1] == '/' || str[1] == '\\'); | |
926 | } | |
927 | ||
928 | /* | |
929 | * Like starts_with_dot_slash, this is a variant of submodule--helper's | |
930 | * helper of the same name with the twist that it accepts backslash as a | |
931 | * directory separator even on non-Windows platforms. | |
932 | */ | |
933 | static int starts_with_dot_dot_slash(const char *str) | |
934 | { | |
935 | return str[0] == '.' && starts_with_dot_slash(str + 1); | |
936 | } | |
937 | ||
938 | static int submodule_url_is_relative(const char *url) | |
939 | { | |
940 | return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url); | |
941 | } | |
942 | ||
c44088ec JN |
943 | /* |
944 | * Count directory components that a relative submodule URL should chop | |
945 | * from the remote_url it is to be resolved against. | |
946 | * | |
947 | * In other words, this counts "../" components at the start of a | |
948 | * submodule URL. | |
949 | * | |
950 | * Returns the number of directory components to chop and writes a | |
951 | * pointer to the next character of url after all leading "./" and | |
952 | * "../" components to out. | |
953 | */ | |
954 | static int count_leading_dotdots(const char *url, const char **out) | |
955 | { | |
956 | int result = 0; | |
957 | while (1) { | |
958 | if (starts_with_dot_dot_slash(url)) { | |
959 | result++; | |
960 | url += strlen("../"); | |
961 | continue; | |
962 | } | |
963 | if (starts_with_dot_slash(url)) { | |
964 | url += strlen("./"); | |
965 | continue; | |
966 | } | |
967 | *out = url; | |
968 | return result; | |
969 | } | |
970 | } | |
a2b26ffb JN |
971 | /* |
972 | * Check whether a transport is implemented by git-remote-curl. | |
973 | * | |
974 | * If it is, returns 1 and writes the URL that would be passed to | |
975 | * git-remote-curl to the "out" parameter. | |
976 | * | |
977 | * Otherwise, returns 0 and leaves "out" untouched. | |
978 | * | |
979 | * Examples: | |
980 | * http::https://example.com/repo.git -> 1, https://example.com/repo.git | |
981 | * https://example.com/repo.git -> 1, https://example.com/repo.git | |
982 | * git://example.com/repo.git -> 0 | |
983 | * | |
984 | * This is for use in checking for previously exploitable bugs that | |
985 | * required a submodule URL to be passed to git-remote-curl. | |
986 | */ | |
987 | static int url_to_curl_url(const char *url, const char **out) | |
988 | { | |
989 | /* | |
990 | * We don't need to check for case-aliases, "http.exe", and so | |
991 | * on because in the default configuration, is_transport_allowed | |
992 | * prevents URLs with those schemes from being cloned | |
993 | * automatically. | |
994 | */ | |
995 | if (skip_prefix(url, "http::", out) || | |
996 | skip_prefix(url, "https::", out) || | |
997 | skip_prefix(url, "ftp::", out) || | |
998 | skip_prefix(url, "ftps::", out)) | |
999 | return 1; | |
1000 | if (starts_with(url, "http://") || | |
1001 | starts_with(url, "https://") || | |
1002 | starts_with(url, "ftp://") || | |
1003 | starts_with(url, "ftps://")) { | |
1004 | *out = url; | |
1005 | return 1; | |
1006 | } | |
1007 | return 0; | |
1008 | } | |
1009 | ||
07259e74 JK |
1010 | static int check_submodule_url(const char *url) |
1011 | { | |
a2b26ffb | 1012 | const char *curl_url; |
07259e74 JK |
1013 | |
1014 | if (looks_like_command_line_option(url)) | |
1015 | return -1; | |
1016 | ||
a2b26ffb | 1017 | if (submodule_url_is_relative(url)) { |
c44088ec JN |
1018 | char *decoded; |
1019 | const char *next; | |
1020 | int has_nl; | |
1021 | ||
a2b26ffb JN |
1022 | /* |
1023 | * This could be appended to an http URL and url-decoded; | |
1024 | * check for malicious characters. | |
1025 | */ | |
c44088ec JN |
1026 | decoded = url_decode(url); |
1027 | has_nl = !!strchr(decoded, '\n'); | |
1028 | ||
a2b26ffb JN |
1029 | free(decoded); |
1030 | if (has_nl) | |
1031 | return -1; | |
c44088ec JN |
1032 | |
1033 | /* | |
1034 | * URLs which escape their root via "../" can overwrite | |
1035 | * the host field and previous components, resolving to | |
1a3609e4 JN |
1036 | * URLs like https::example.com/submodule.git and |
1037 | * https:///example.com/submodule.git that were | |
c44088ec JN |
1038 | * susceptible to CVE-2020-11008. |
1039 | */ | |
1040 | if (count_leading_dotdots(url, &next) > 0 && | |
1a3609e4 | 1041 | (*next == ':' || *next == '/')) |
c44088ec | 1042 | return -1; |
a2b26ffb JN |
1043 | } |
1044 | ||
1045 | else if (url_to_curl_url(url, &curl_url)) { | |
1046 | struct credential c = CREDENTIAL_INIT; | |
1a3609e4 JN |
1047 | int ret = 0; |
1048 | if (credential_from_url_gently(&c, curl_url, 1) || | |
1049 | !*c.host) | |
1050 | ret = -1; | |
a2b26ffb JN |
1051 | credential_clear(&c); |
1052 | return ret; | |
1053 | } | |
1054 | ||
1055 | return 0; | |
07259e74 JK |
1056 | } |
1057 | ||
ed8b10f6 | 1058 | struct fsck_gitmodules_data { |
6da40b22 | 1059 | const struct object_id *oid; |
ed8b10f6 JK |
1060 | struct fsck_options *options; |
1061 | int ret; | |
1062 | }; | |
1063 | ||
1064 | static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata) | |
1065 | { | |
1066 | struct fsck_gitmodules_data *data = vdata; | |
1067 | const char *subsection, *key; | |
1068 | int subsection_len; | |
1069 | char *name; | |
1070 | ||
1071 | if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 || | |
1072 | !subsection) | |
1073 | return 0; | |
1074 | ||
1075 | name = xmemdupz(subsection, subsection_len); | |
1076 | if (check_submodule_name(name) < 0) | |
38370253 | 1077 | data->ret |= report(data->options, |
6da40b22 | 1078 | data->oid, OBJ_BLOB, |
ed8b10f6 JK |
1079 | FSCK_MSG_GITMODULES_NAME, |
1080 | "disallowed submodule name: %s", | |
1081 | name); | |
a124133e | 1082 | if (!strcmp(key, "url") && value && |
07259e74 | 1083 | check_submodule_url(value) < 0) |
38370253 | 1084 | data->ret |= report(data->options, |
6da40b22 | 1085 | data->oid, OBJ_BLOB, |
a124133e JK |
1086 | FSCK_MSG_GITMODULES_URL, |
1087 | "disallowed submodule url: %s", | |
1088 | value); | |
1a7fd1fb JK |
1089 | if (!strcmp(key, "path") && value && |
1090 | looks_like_command_line_option(value)) | |
38370253 | 1091 | data->ret |= report(data->options, |
6da40b22 | 1092 | data->oid, OBJ_BLOB, |
1a7fd1fb JK |
1093 | FSCK_MSG_GITMODULES_PATH, |
1094 | "disallowed submodule path: %s", | |
1095 | value); | |
bb92255e JN |
1096 | if (!strcmp(key, "update") && value && |
1097 | parse_submodule_update_type(value) == SM_UPDATE_COMMAND) | |
7034cd09 | 1098 | data->ret |= report(data->options, data->oid, OBJ_BLOB, |
bb92255e JN |
1099 | FSCK_MSG_GITMODULES_UPDATE, |
1100 | "disallowed submodule update setting: %s", | |
1101 | value); | |
ed8b10f6 JK |
1102 | free(name); |
1103 | ||
1104 | return 0; | |
1105 | } | |
1106 | ||
6da40b22 | 1107 | static int fsck_blob(const struct object_id *oid, const char *buf, |
7ac4f3a0 JK |
1108 | unsigned long size, struct fsck_options *options) |
1109 | { | |
ed8b10f6 | 1110 | struct fsck_gitmodules_data data; |
de6bd9e3 | 1111 | struct config_options config_opts = { 0 }; |
ed8b10f6 | 1112 | |
6da40b22 | 1113 | if (!oidset_contains(&gitmodules_found, oid)) |
ed8b10f6 | 1114 | return 0; |
6da40b22 | 1115 | oidset_insert(&gitmodules_done, oid); |
ed8b10f6 | 1116 | |
6da40b22 | 1117 | if (object_on_skiplist(options, oid)) |
fb162877 RJ |
1118 | return 0; |
1119 | ||
ed8b10f6 JK |
1120 | if (!buf) { |
1121 | /* | |
1122 | * A missing buffer here is a sign that the caller found the | |
1123 | * blob too gigantic to load into memory. Let's just consider | |
1124 | * that an error. | |
1125 | */ | |
6da40b22 | 1126 | return report(options, oid, OBJ_BLOB, |
0d68764d | 1127 | FSCK_MSG_GITMODULES_LARGE, |
ed8b10f6 JK |
1128 | ".gitmodules too large to parse"); |
1129 | } | |
1130 | ||
6da40b22 | 1131 | data.oid = oid; |
ed8b10f6 JK |
1132 | data.options = options; |
1133 | data.ret = 0; | |
de6bd9e3 | 1134 | config_opts.error_action = CONFIG_ERROR_SILENT; |
ed8b10f6 | 1135 | if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB, |
de6bd9e3 | 1136 | ".gitmodules", buf, size, &data, &config_opts)) |
6da40b22 | 1137 | data.ret |= report(options, oid, OBJ_BLOB, |
ed8b10f6 JK |
1138 | FSCK_MSG_GITMODULES_PARSE, |
1139 | "could not parse gitmodules blob"); | |
1140 | ||
1141 | return data.ret; | |
7ac4f3a0 JK |
1142 | } |
1143 | ||
90a398bb | 1144 | int fsck_object(struct object *obj, void *data, unsigned long size, |
22410549 | 1145 | struct fsck_options *options) |
ba002f3b MK |
1146 | { |
1147 | if (!obj) | |
38370253 | 1148 | return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck"); |
ba002f3b MK |
1149 | |
1150 | if (obj->type == OBJ_BLOB) | |
6da40b22 | 1151 | return fsck_blob(&obj->oid, data, size, options); |
ba002f3b | 1152 | if (obj->type == OBJ_TREE) |
b2f2039c | 1153 | return fsck_tree(&obj->oid, data, size, options); |
ba002f3b | 1154 | if (obj->type == OBJ_COMMIT) |
c5b4269b | 1155 | return fsck_commit(&obj->oid, data, size, options); |
ba002f3b | 1156 | if (obj->type == OBJ_TAG) |
103fb6d4 | 1157 | return fsck_tag(&obj->oid, data, size, options); |
ba002f3b | 1158 | |
38370253 JK |
1159 | return report(options, &obj->oid, obj->type, |
1160 | FSCK_MSG_UNKNOWN_TYPE, | |
1161 | "unknown type '%d' (internal fsck error)", | |
1162 | obj->type); | |
ba002f3b | 1163 | } |
d6ffc8d7 | 1164 | |
1cd772cc | 1165 | int fsck_error_function(struct fsck_options *o, |
5afc4b1d JK |
1166 | const struct object_id *oid, |
1167 | enum object_type object_type, | |
1168 | int msg_type, const char *message) | |
d6ffc8d7 | 1169 | { |
0282f4dc | 1170 | if (msg_type == FSCK_WARN) { |
5afc4b1d | 1171 | warning("object %s: %s", fsck_describe_object(o, oid), message); |
0282f4dc JS |
1172 | return 0; |
1173 | } | |
5afc4b1d | 1174 | error("object %s: %s", fsck_describe_object(o, oid), message); |
d6ffc8d7 MK |
1175 | return 1; |
1176 | } | |
159e7b08 JK |
1177 | |
1178 | int fsck_finish(struct fsck_options *options) | |
1179 | { | |
1180 | int ret = 0; | |
1181 | struct oidset_iter iter; | |
1182 | const struct object_id *oid; | |
1183 | ||
1184 | oidset_iter_init(&gitmodules_found, &iter); | |
1185 | while ((oid = oidset_iter_next(&iter))) { | |
159e7b08 JK |
1186 | enum object_type type; |
1187 | unsigned long size; | |
1188 | char *buf; | |
1189 | ||
1190 | if (oidset_contains(&gitmodules_done, oid)) | |
1191 | continue; | |
1192 | ||
7913f53b | 1193 | buf = read_object_file(oid, &type, &size); |
159e7b08 | 1194 | if (!buf) { |
b8b00f16 | 1195 | if (is_promisor_object(oid)) |
27387444 | 1196 | continue; |
38370253 | 1197 | ret |= report(options, |
b8b00f16 | 1198 | oid, OBJ_BLOB, |
159e7b08 JK |
1199 | FSCK_MSG_GITMODULES_MISSING, |
1200 | "unable to read .gitmodules blob"); | |
1201 | continue; | |
1202 | } | |
1203 | ||
1204 | if (type == OBJ_BLOB) | |
b8b00f16 | 1205 | ret |= fsck_blob(oid, buf, size, options); |
159e7b08 | 1206 | else |
38370253 | 1207 | ret |= report(options, |
b8b00f16 | 1208 | oid, type, |
159e7b08 JK |
1209 | FSCK_MSG_GITMODULES_BLOB, |
1210 | "non-blob found at .gitmodules"); | |
1211 | free(buf); | |
1212 | } | |
1213 | ||
1214 | ||
1215 | oidset_clear(&gitmodules_found); | |
1216 | oidset_clear(&gitmodules_done); | |
1217 | return ret; | |
1218 | } |