// $Id: chroot-sh.c 2536 2007-04-27 09:01:20Z hollow $ --*- c -*-- // Copyright (C) 2005 Enrico Scholz // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 of the License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include #include #include #define ENSC_WRAPPERS_PREFIX "chroot-sh: " #define ENSC_WRAPPERS_UNISTD 1 #define ENSC_WRAPPERS_IO 1 #define ENSC_WRAPPERS_FCNTL 1 #include int wrapper_exit_code = EXIT_FAILURE; static void showFD(int fd_in, int fd_out) { for (;;) { char buf[4096]; char const * ptr=buf; ssize_t len; len = Eread(fd_in, buf, sizeof(buf)); if (len<=0) break; EwriteAll(fd_out, ptr, len); } } static int redirectFileInternal(int argc, char *argv[], int mode, bool is_input, char const *operation) { int fd; if (argc<2) { WRITE_MSG(2, "Not enough parameters for '"); WRITE_STR(2, operation); WRITE_MSG(2, "' operation; use '--help' for more information\n"); return wrapper_exit_code; } fd = EopenD(argv[1], mode, 0644); if (is_input) showFD(fd, 1); else showFD( 0, fd); Eclose(fd); return EXIT_SUCCESS; } static mode_t testInternal(int argc, char *argv[], char const *operation) { struct stat st; if (argc<2) { WRITE_MSG(2, "Not enough parameters for '"); WRITE_STR(2, operation); WRITE_MSG(2, "' operation; use '--help' for more information\n"); return wrapper_exit_code; } if (stat(argv[1], &st)==-1) return -1; else return st.st_mode; } static int execCat(int argc, char *argv[]) { return redirectFileInternal(argc, argv, O_RDONLY|O_NOCTTY, true, "cat"); } static int execAppend(int argc, char *argv[]) { return redirectFileInternal(argc, argv, O_WRONLY|O_CREAT|O_APPEND, false, "append"); } static int execTruncate(int argc, char *argv[]) { return redirectFileInternal(argc, argv, O_WRONLY|O_CREAT|O_TRUNC, false, "truncate"); } static int execRm(int argc, char *argv[]) { int i = 1; int res = EXIT_SUCCESS; if (argc<2) { WRITE_MSG(2, "No files specified for 'rm' operation; try '--help' for more information\n"); return wrapper_exit_code; } for (;i *\n\n" "This program chroots into the current directory and executes the specified\n" "commands there. This means that all used paths are relative to the current\n" "directory, and symlinks can point to files under the current path only.\n" "\n" "The supported commands are:\n" " cat ... gives out to stdout\n" " append ... appends stdin to which is created when needed\n" " truncate ... clear and fill it with stdin; the is\n" " created when needed\n" " rm + ... unlink the given files\n" " mkdir + ... create the given directories\n" " chmod +\n" " ... change access permissions of files\n" " link dst> ... create a symbolic link from to \n\n" "Please report bugs to " PACKAGE_BUGREPORT "\n"); exit(0); } static void showVersion() { WRITE_MSG(1, "chroot-sh " VERSION " -- execute commands within a chroot\n" "This program is part of " PACKAGE_STRING "\n\n" "Copyright (C) 2005 Enrico Scholz\n" VERSION_COPYRIGHT_DISCLAIMER); exit(0); } int main(int argc, char *argv[]) { struct Command const *cmd; int idx = 1; if (argc>=2) { if (strcmp(argv[idx], "--help") ==0) showHelp(); if (strcmp(argv[idx], "--version")==0) showVersion(); if (strcmp(argv[idx], "--")==0) ++idx; } if (argccmd!=0; ++cmd) { if (strcmp(cmd->cmd, argv[idx])==0) return cmd->handler(argc-idx, argv+idx); } WRITE_MSG(2, "Invalid command '"); WRITE_STR(2, argv[idx]); WRITE_MSG(2, "'; try '--help' for more information\n"); return wrapper_exit_code; }