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