chroot_realpath.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * chroot_realpath.c -- resolve pathname as if inside chroot
  3. * Based on realpath.c Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; see the file COPYING.LIB. If not,
  17. * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. * Boston, MA 02111-1307, USA.
  19. *
  20. * 2005/09/12: Dan Howell (modified from realpath.c to emulate chroot)
  21. */
  22. #include "porting.h"
  23. #define MAX_READLINKS 32
  24. char *chroot_realpath(const char *chroot, const char *path,
  25. char resolved_path[])
  26. {
  27. char copy_path[PATH_MAX];
  28. char link_path[PATH_MAX];
  29. char got_path[PATH_MAX];
  30. char *got_path_root = got_path;
  31. char *new_path = got_path;
  32. char *max_path;
  33. int readlinks = 0;
  34. int n;
  35. int chroot_len;
  36. /* Trivial case. */
  37. if (chroot == NULL || *chroot == '\0' ||
  38. (*chroot == '/' && chroot[1] == '\0')) {
  39. strcpy(resolved_path, path);
  40. return resolved_path;
  41. }
  42. chroot_len = strlen(chroot);
  43. if (chroot_len + strlen(path) >= PATH_MAX - 3) {
  44. errno = ENAMETOOLONG;
  45. return NULL;
  46. }
  47. /* Make a copy of the source path since we may need to modify it. */
  48. strcpy(copy_path, path);
  49. path = copy_path;
  50. max_path = copy_path + PATH_MAX - chroot_len - 3;
  51. /* Start with the chroot path. */
  52. strcpy(new_path, chroot);
  53. new_path += chroot_len;
  54. while (*new_path == '/' && new_path > got_path)
  55. new_path--;
  56. got_path_root = new_path;
  57. *new_path++ = '/';
  58. /* Expand each slash-separated pathname component. */
  59. while (*path != '\0') {
  60. /* Ignore stray "/". */
  61. if (*path == '/') {
  62. path++;
  63. continue;
  64. }
  65. if (*path == '.') {
  66. /* Ignore ".". */
  67. if (path[1] == '\0' || path[1] == '/') {
  68. path++;
  69. continue;
  70. }
  71. if (path[1] == '.') {
  72. if (path[2] == '\0' || path[2] == '/') {
  73. path += 2;
  74. /* Ignore ".." at root. */
  75. if (new_path == got_path_root + 1)
  76. continue;
  77. /* Handle ".." by backing up. */
  78. while ((--new_path)[-1] != '/') ;
  79. continue;
  80. }
  81. }
  82. }
  83. /* Safely copy the next pathname component. */
  84. while (*path != '\0' && *path != '/') {
  85. if (path > max_path) {
  86. errno = ENAMETOOLONG;
  87. return NULL;
  88. }
  89. *new_path++ = *path++;
  90. }
  91. if (*path == '\0')
  92. /* Don't follow symlink for last pathname component. */
  93. break;
  94. #ifdef S_IFLNK
  95. /* Protect against infinite loops. */
  96. if (readlinks++ > MAX_READLINKS) {
  97. errno = ELOOP;
  98. return NULL;
  99. }
  100. /* See if latest pathname component is a symlink. */
  101. *new_path = '\0';
  102. n = readlink(got_path, link_path, PATH_MAX - 1);
  103. if (n < 0) {
  104. /* EINVAL means the file exists but isn't a symlink. */
  105. if (errno != EINVAL) {
  106. /* Make sure it's null terminated. */
  107. *new_path = '\0';
  108. strcpy(resolved_path, got_path);
  109. return NULL;
  110. }
  111. } else {
  112. /* Note: readlink doesn't add the null byte. */
  113. link_path[n] = '\0';
  114. if (*link_path == '/')
  115. /* Start over for an absolute symlink. */
  116. new_path = got_path_root;
  117. else
  118. /* Otherwise back up over this component. */
  119. while (*(--new_path) != '/') ;
  120. /* Safe sex check. */
  121. if (strlen(path) + n >= PATH_MAX - 2) {
  122. errno = ENAMETOOLONG;
  123. return NULL;
  124. }
  125. /* Insert symlink contents into path. */
  126. strcat(link_path, path);
  127. strcpy(copy_path, link_path);
  128. path = copy_path;
  129. }
  130. #endif /* S_IFLNK */
  131. *new_path++ = '/';
  132. }
  133. /* Delete trailing slash but don't whomp a lone slash. */
  134. if (new_path != got_path + 1 && new_path[-1] == '/')
  135. new_path--;
  136. /* Make sure it's null terminated. */
  137. *new_path = '\0';
  138. strcpy(resolved_path, got_path);
  139. return resolved_path;
  140. }