chroot_realpath.c 4.4 KB

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