chroot_realpath.c 4.4 KB

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