chroot_realpath.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 *root, const char *path,
  25. char resolved_path[]);
  26. char *chroot_realpath(const char *root, const char *path,
  27. char resolved_path[])
  28. {
  29. char copy_path[PATH_MAX];
  30. char link_path[PATH_MAX];
  31. char got_path[PATH_MAX];
  32. char *got_path_root = got_path;
  33. char *new_path = got_path;
  34. char *max_path;
  35. int readlinks = 0;
  36. int n;
  37. int chroot_len;
  38. /* Trivial case. */
  39. if (root == NULL || *root == '\0' ||
  40. (*root == '/' && root[1] == '\0')) {
  41. strcpy(resolved_path, path);
  42. return resolved_path;
  43. }
  44. chroot_len = strlen(root);
  45. if (chroot_len + strlen(path) >= PATH_MAX - 3) {
  46. errno = ENAMETOOLONG;
  47. return NULL;
  48. }
  49. /* Make a copy of the source path since we may need to modify it. */
  50. strcpy(copy_path, path);
  51. path = copy_path;
  52. max_path = copy_path + PATH_MAX - chroot_len - 3;
  53. /* Start with the chroot path. */
  54. strcpy(new_path, root);
  55. new_path += chroot_len;
  56. while (*new_path == '/' && new_path > got_path)
  57. new_path--;
  58. got_path_root = new_path;
  59. *new_path++ = '/';
  60. /* Expand each slash-separated pathname component. */
  61. while (*path != '\0') {
  62. /* Ignore stray "/". */
  63. if (*path == '/') {
  64. path++;
  65. continue;
  66. }
  67. if (*path == '.') {
  68. /* Ignore ".". */
  69. if (path[1] == '\0' || path[1] == '/') {
  70. path++;
  71. continue;
  72. }
  73. if (path[1] == '.') {
  74. if (path[2] == '\0' || path[2] == '/') {
  75. path += 2;
  76. /* Ignore ".." at root. */
  77. if (new_path == got_path_root + 1)
  78. continue;
  79. /* Handle ".." by backing up. */
  80. while ((--new_path)[-1] != '/') ;
  81. continue;
  82. }
  83. }
  84. }
  85. /* Safely copy the next pathname component. */
  86. while (*path != '\0' && *path != '/') {
  87. if (path > max_path) {
  88. errno = ENAMETOOLONG;
  89. return NULL;
  90. }
  91. *new_path++ = *path++;
  92. }
  93. if (*path == '\0')
  94. /* Don't follow symlink for last pathname component. */
  95. break;
  96. #ifdef S_IFLNK
  97. /* Protect against infinite loops. */
  98. if (readlinks++ > MAX_READLINKS) {
  99. errno = ELOOP;
  100. return NULL;
  101. }
  102. /* See if latest pathname component is a symlink. */
  103. *new_path = '\0';
  104. n = readlink(got_path, link_path, PATH_MAX - 1);
  105. if (n < 0) {
  106. /* EINVAL means the file exists but isn't a symlink. */
  107. if (errno != EINVAL) {
  108. /* Make sure it's null terminated. */
  109. *new_path = '\0';
  110. strcpy(resolved_path, got_path);
  111. return NULL;
  112. }
  113. } else {
  114. /* Note: readlink doesn't add the null byte. */
  115. link_path[n] = '\0';
  116. if (*link_path == '/')
  117. /* Start over for an absolute symlink. */
  118. new_path = got_path_root;
  119. else
  120. /* Otherwise back up over this component. */
  121. while (*(--new_path) != '/') ;
  122. /* Safe sex check. */
  123. if (strlen(path) + n >= PATH_MAX - 2) {
  124. errno = ENAMETOOLONG;
  125. return NULL;
  126. }
  127. /* Insert symlink contents into path. */
  128. strcat(link_path, path);
  129. strcpy(copy_path, link_path);
  130. path = copy_path;
  131. }
  132. #endif /* S_IFLNK */
  133. *new_path++ = '/';
  134. }
  135. /* Delete trailing slash but don't whomp a lone slash. */
  136. if (new_path != got_path + 1 && new_path[-1] == '/')
  137. new_path--;
  138. /* Make sure it's null terminated. */
  139. *new_path = '\0';
  140. strcpy(resolved_path, got_path);
  141. return resolved_path;
  142. }