home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume38
/
chrootuid
/
part01
/
chrootuid.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-13
|
3KB
|
109 lines
/*++
/* NAME
/* chrootuid 1
/* SUMMARY
/* run command in restricted environment
/* SYNOPSIS
/* chrootuid newroot newuser command...
/* DESCRIPTION
/* The \fIchrootuid\fR command sets up a restricted environment for
/* command execution. Access to the file system is restricted to
/* the \fInewroot\fR subtree; privileges are restricted to those of
/* \fInewuser\fR. The initial working directory is changed to
/* \fInewroot\fR.
/*
/* \fIchrootuid\fR combines chroot(8) and su(1) into one program, so
/* that there is no need to have dangerous commands such as /usr/bin/su
/* in the restricted environment.
/*
/* Only the superuser can use the \fIchrootuid\fR command.
/* SEE ALSO
/* chroot(8), su(1)
/* DIAGNOSTICS
/* Problems are reported to the syslog daemon.
/* BUGS
/* The \fInewuser\fR must be known in the \fInewroot\fR universe.
/* AUTHOR(S)
/* W.Z. Venema
/* Eindhoven University of Technology
/* Department of Mathematics and Computer Science
/* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
/* CREATION DATE
/* Tue Oct 13 11:37:29 MET 1992
/* LAST MODIFICATION
/* 92/10/13 12:01:19
/* VERSION/RELEASE
/* 1.1
/*--*/
#ifndef lint
static char sccsid[] = "@(#) chrootuid.c 1.1 92/10/13 12:01:19";
#endif
/* System libraries. */
#include <pwd.h>
#include <syslog.h>
main(argc, argv)
int argc;
char **argv;
{
struct passwd *pwd;
/*
* Open a channel to the syslog daemon. Older versions of openlog()
* require only two arguments.
*/
#ifdef LOG_DAEMON
(void) openlog(argv[0], LOG_PID, LOG_DAEMON);
#else
(void) openlog(argv[0], LOG_PID);
#endif
/*
* Require proper amount of arguments. In all cases of error, exit with
* zero status because we have already reported the problem via syslogd.
* No need to make inetd complain, too.
*/
if (argc < 4) {
syslog(LOG_ERR, "usage: %s path user command", argv[0]);
return (0);
}
/* Must step into the new subtree. */
if (chdir(argv[1])) {
syslog(LOG_ERR, "chdir(%s): %m", argv[1]);
return (0);
}
/* Do the chroot() before giving away root privileges. */
if (chroot(argv[1])) {
syslog(LOG_ERR, "chroot(%s): %m", argv[1]);
return (0);
}
/* The user must be known in the chrooted universe... */
if ((pwd = getpwnam(argv[2])) == 0) {
syslog(LOG_ERR, "%s: user unknown", argv[2]);
return (0);
}
/* Switch group id then user id. */
if (setgid(pwd->pw_gid)) {
syslog(LOG_ERR, "setgid(%d): %m", pwd->pw_gid);
return (0);
}
if (setuid(pwd->pw_uid)) {
syslog(LOG_ERR, "setuid(%d): %m", pwd->pw_uid);
return (0);
}
/* Run the command and hope for the best. */
(void) execvp(argv[3], argv + 3);
syslog(LOG_ERR, "%s: %m", argv[3]);
return (0);
}