| 1329 | } |
| 1330 | |
| 1331 | int daemon_avoid_alias(const char *p) |
| 1332 | { |
| 1333 | int sl, ndot; |
| 1334 | |
| 1335 | /* |
| 1336 | * This resurrects the belts and suspenders paranoia check by HPA |
| 1337 | * done in <435560F7.4080006@zytor.com> thread, now enter_repo() |
| 1338 | * does not do getcwd() based path canonicalization. |
| 1339 | * |
| 1340 | * sl becomes true immediately after seeing '/' and continues to |
| 1341 | * be true as long as dots continue after that without intervening |
| 1342 | * non-dot character. |
| 1343 | */ |
| 1344 | if (!p || (*p != '/' && *p != '~')) |
| 1345 | return -1; |
| 1346 | sl = 1; ndot = 0; |
| 1347 | p++; |
| 1348 | |
| 1349 | while (1) { |
| 1350 | char ch = *p++; |
| 1351 | if (sl) { |
| 1352 | if (ch == '.') |
| 1353 | ndot++; |
| 1354 | else if (ch == '/') { |
| 1355 | if (ndot < 3) |
| 1356 | /* reject //, /./ and /../ */ |
| 1357 | return -1; |
| 1358 | ndot = 0; |
| 1359 | } |
| 1360 | else if (ch == 0) { |
| 1361 | if (0 < ndot && ndot < 3) |
| 1362 | /* reject /.$ and /..$ */ |
| 1363 | return -1; |
| 1364 | return 0; |
| 1365 | } |
| 1366 | else |
| 1367 | sl = ndot = 0; |
| 1368 | } |
| 1369 | else if (ch == 0) |
| 1370 | return 0; |
| 1371 | else if (ch == '/') { |
| 1372 | sl = 1; |
| 1373 | ndot = 0; |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | /* |
| 1379 | * On NTFS, we need to be careful to disallow certain synonyms of the `.git/` |