Message-Id: <199903110030.AAA114304@out4.ibm.net> From: "Mark E." To: djgpp-workers AT delorie DOT com Date: Wed, 10 Mar 1999 19:30:09 -0500 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: chroot patches v5 X-mailer: Pegasus Mail for Win32 (v3.01d) Reply-To: djgpp-workers AT delorie DOT com Below are my latest patches. I've only included files that are new or changed. The biggest changes are in chroot.c. Instead of 'setenv', I now use 'putenv' which then required some adjustments. *** include/libc/stubs.h.orig Wed Mar 10 19:14:58 1999 --- include/libc/stubs.h Wed Mar 10 19:04:16 1999 *************** *** 1,3 **** --- 1,4 ---- + /* Copyright (C) 1999 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ #ifndef __dj_include_libc_stubs_h__ *************** *** 33,38 **** --- 34,40 ---- /* DJGPP functions (for compiling POSIX or ANSI functions) */ + #define chroot __chroot #define crlf2nl __crlf2nl #define dosmemget __dosmemget #define dosmemput __dosmemput *************** *** 55,60 **** --- 57,63 ---- #define spawnve __spawnve #define spawnvpe __spawnvpe #define stricmp __stricmp + #define strnicmp __strnicmp #define sync __sync #endif /* !_POSIX_SOURCE */ *** src/libc/compat/unistd/makefile.orig Sun Jun 28 17:53:24 1998 --- src/libc/compat/unistd/makefile Wed Mar 10 18:47:28 1999 *************** *** 3,9 **** --- 3,11 ---- TOP=../.. SRC += basename.c + SRC += chroot.c SRC += dirname.c + SRC += fchroot.c SRC += fsync.c SRC += ftruncat.c SRC += getdtabl.c *** src/libc/compat/unistd/chroot.c.orig Sun Feb 28 14:25:18 1999 --- src/libc/compat/unistd/chroot.c Wed Mar 10 19:07:06 1999 *************** *** 0 **** --- 1,154 ---- + #include + #include + #include + #include + #include + #include + #include + #include + + static int chroot_bss_count = -1; + + unsigned int __chroot_flags = __CHROOT_UNIX_MODE_FLAG; + static unsigned int old_flags; + + /* To keep the space used for the root path and the + root dir. environment string to a minimum, + we alias __djgpp_root to inside root_env. */ + + static char root_env[PATH_MAX + sizeof(ROOT_ENV) + 1] = ROOT_ENV "="; + + char * __djgpp_root = root_env + sizeof(ROOT_ENV); + int __djgpp_root_len = 0; + + static char mode_env[] = CHROOT_ENV "=Y"; + + int + __chroot (const char *path) + { + char buf[PATH_MAX+1]; + char *to = __djgpp_root; + char *from = buf; + const int old_root_len = __djgpp_root_len; + int unix_emu; + int path_len; + + if ((path_len = strlen(path)) > PATH_MAX) + { + errno = ENAMETOOLONG; + return -1; + } + + if (chroot_bss_count != __bss_count) + { + chroot_bss_count = __bss_count; + old_flags = ~0; + } + + unix_emu = __chroot_flags & __CHROOT_UNIX_MODE_FLAG; + + /* When not emulating the Unix behavior of chroot, + allow a null root to delete the root path. */ + if (path == NULL || *path == '\0') + { + if (unix_emu) + { + errno = ENOENT; + return -1; + } + __djgpp_root[0] = '\0'; + __djgpp_root_len = 0; + putenv(root_env); + return 0; + } + + /* When in Unix/restrictive mode, don't allow drive letters + in a new root directory after a root directory has been set. + Even if the new directory is relative to the root. */ + if (*path && *(path+1) == ':') + { + if (unix_emu && __djgpp_root_len > 0) + { + errno = EACCES; + return -1; + } + } + + _fixpath(path, buf); + + if (unix_emu) + { + /* If a root directory already exists, the new root directory + must be relative to the existing root directory. + If not, then reject the path. */ + if (__djgpp_root_len > 0 && *buf != '/') + { + errno = EACCES; + return -1; + } + } + + /* Make sure the path is valid before + making it the new root path. */ + if (access(buf, D_OK) < 0) + return -1; + + /* Copy in the new root path. */ + if (*buf != '/') + __djgpp_root_len = 0; + else /* Append to the current root path. */ + { + to += __djgpp_root_len; + } + + while (*from) + { + *to = *from; + ++to; + ++from; + ++__djgpp_root_len; + } + *to = '\0'; + + /* Set up environmental variables to be used so child programs + created with DJGPP will inherit the root path and mode. */ + if (__djgpp_root_len != old_root_len) + { + putenv (root_env); + } + + if (old_flags != __chroot_flags) + { + mode_env[sizeof(CHROOT_ENV)] = (unix_emu ? 'Y' : 'N'); + putenv(mode_env); + old_flags = __chroot_flags; + } + + return 0; + } + + + /* Delete the root path from an input path. + The input path is assumed to have already been canonicalized. */ + + char * + __delete_root (char *path) + { + if (__djgpp_root_len > 0 && path[0] != '/') + { + if (strnicmp (__djgpp_root, path, __djgpp_root_len) == 0 + && (path[__djgpp_root_len] == '/' || path[__djgpp_root_len] == '\0')) + { + if (path[__djgpp_root_len] == '/') + strcpy (path, path + __djgpp_root_len); + else + { + /* path is equal to the root path. */ + path[0] = '/'; + path[1] = '\0'; + } + } + } + return path; + } + *** src/libc/dos/io/putpath.c.orig Thu Oct 29 05:24:44 1998 --- src/libc/dos/io/putpath.c Tue Mar 9 00:17:48 1999 *************** *** 3,12 **** --- 3,21 ---- #include #include #include + #include #include #include #include #include + #include + + static int + __inline__ + __is_dirsep (char ch) + { + return ((ch == '/') || (ch == '\\')); + } void _put_path(const char *path) *************** *** 31,37 **** if (p[0] && p[1] == ':') p += 2; ! if (strncmp(p, "/dev/", 5) == 0) { if (strcmp(p+5, "null") == 0) path = "nul"; --- 40,46 ---- if (p[0] && p[1] == ':') p += 2; ! if (__is_dirsep(*p) && (strncmp("dev", p+1, 3) == 0) && __is_dirsep(*(p+4))) { if (strcmp(p+5, "null") == 0) path = "nul"; *************** *** 49,54 **** --- 58,78 ---- } else if (p[5]) path = p + 5; + } + else if ((__is_dirsep(*path) || (__chroot_flags & __CHROOT_UNIX_MODE_FLAG)) && __djgpp_root_len > 0 ) + { + /* If the path is absolute and a root path is set, + then add the root path to the output. */ + char *root = __djgpp_root; + + while (*root) + { + _farnspokeb (o++, *root); + space -= 1; + ++root; + } + if (!__is_dirsep(*path)) + _farnspokeb (o++, '/'); } /* collapse multiple slashes to a single slash */ *** src/libc/stubs/stub0038.s.orig Wed Mar 10 18:35:28 1999 --- src/libc/stubs/stub0038.s Wed Mar 10 19:15:46 1999 *************** *** 0 **** --- 1,5 ---- + /* Copyright (C) 1999 DJ Delorie, see COPYING.DJ for details */ + .file "sync.stub" + .global _chroot + _sync: + jmp ___chroot *** src/libc/stubs/stub0039.s.orig Wed Mar 10 18:35:28 1999 --- src/libc/stubs/stub0039.s Wed Mar 10 18:35:52 1999 *************** *** 0 **** --- 1,5 ---- + /* Copyright (C) 1999 DJ Delorie, see COPYING.DJ for details */ + .file "sync.stub" + .global _strnicmp + _sync: + jmp ___strnicmp --- Mark Elbrecht snowball3 AT usa DOT net http://members.xoom.com/snowball3/