home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / chrootuid / part01 / chrootuid.c < prev    next >
C/C++ Source or Header  |  1993-08-13  |  3KB  |  109 lines

  1. /*++
  2. /* NAME
  3. /*    chrootuid 1
  4. /* SUMMARY
  5. /*    run command in restricted environment
  6. /* SYNOPSIS
  7. /*    chrootuid newroot newuser command...
  8. /* DESCRIPTION
  9. /*    The \fIchrootuid\fR command sets up a restricted environment for
  10. /*    command execution. Access to the file system is restricted to
  11. /*    the \fInewroot\fR subtree; privileges are restricted to those of 
  12. /*    \fInewuser\fR. The initial working directory is changed to 
  13. /*    \fInewroot\fR.
  14. /*
  15. /*    \fIchrootuid\fR combines chroot(8) and su(1) into one program, so 
  16. /*    that there is no need to have dangerous commands such as /usr/bin/su 
  17. /*    in the restricted environment.
  18. /*
  19. /*    Only the superuser can use the \fIchrootuid\fR command.
  20. /* SEE ALSO
  21. /*    chroot(8), su(1)
  22. /* DIAGNOSTICS
  23. /*    Problems are reported to the syslog daemon.
  24. /* BUGS
  25. /*    The \fInewuser\fR must be known in the \fInewroot\fR universe.
  26. /* AUTHOR(S)
  27. /*    W.Z. Venema
  28. /*    Eindhoven University of Technology
  29. /*    Department of Mathematics and Computer Science
  30. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  31. /* CREATION DATE
  32. /*    Tue Oct 13 11:37:29 MET 1992
  33. /* LAST MODIFICATION
  34. /*    92/10/13 12:01:19
  35. /* VERSION/RELEASE
  36. /*    1.1
  37. /*--*/
  38.  
  39. #ifndef lint
  40. static char sccsid[] = "@(#) chrootuid.c 1.1 92/10/13 12:01:19";
  41. #endif
  42.  
  43. /* System libraries. */
  44.  
  45. #include <pwd.h>
  46. #include <syslog.h>
  47.  
  48. main(argc, argv)
  49. int     argc;
  50. char  **argv;
  51. {
  52.     struct passwd *pwd;
  53.  
  54.     /*
  55.      * Open a channel to the syslog daemon. Older versions of openlog()
  56.      * require only two arguments.
  57.      */
  58.  
  59. #ifdef LOG_DAEMON
  60.     (void) openlog(argv[0], LOG_PID, LOG_DAEMON);
  61. #else
  62.     (void) openlog(argv[0], LOG_PID);
  63. #endif
  64.  
  65.     /*
  66.      * Require proper amount of arguments. In all cases of error, exit with
  67.      * zero status because we have already reported the problem via syslogd.
  68.      * No need to make inetd complain, too.
  69.      */
  70.  
  71.     if (argc < 4) {
  72.     syslog(LOG_ERR, "usage: %s path user command", argv[0]);
  73.     return (0);
  74.     }
  75.     /* Must step into the new subtree. */
  76.  
  77.     if (chdir(argv[1])) {
  78.     syslog(LOG_ERR, "chdir(%s): %m", argv[1]);
  79.     return (0);
  80.     }
  81.     /* Do the chroot() before giving away root privileges. */
  82.  
  83.     if (chroot(argv[1])) {
  84.     syslog(LOG_ERR, "chroot(%s): %m", argv[1]);
  85.     return (0);
  86.     }
  87.     /* The user must be known in the chrooted universe... */
  88.  
  89.     if ((pwd = getpwnam(argv[2])) == 0) {
  90.     syslog(LOG_ERR, "%s: user unknown", argv[2]);
  91.     return (0);
  92.     }
  93.     /* Switch group id then user id. */
  94.  
  95.     if (setgid(pwd->pw_gid)) {
  96.     syslog(LOG_ERR, "setgid(%d): %m", pwd->pw_gid);
  97.     return (0);
  98.     }
  99.     if (setuid(pwd->pw_uid)) {
  100.     syslog(LOG_ERR, "setuid(%d): %m", pwd->pw_uid);
  101.     return (0);
  102.     }
  103.     /* Run the command and hope for the best. */
  104.  
  105.     (void) execvp(argv[3], argv + 3);
  106.     syslog(LOG_ERR, "%s: %m", argv[3]);
  107.     return (0);
  108. }
  109.