chroot_realpath.c 4.4 KB

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