2004-05-11 12:07:37 +04:00
|
|
|
/* $NetBSD: rexecd.c,v 1.18 2004/05/11 08:07:37 kleink Exp $ */
|
1997-10-07 14:11:29 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1997-10-07 14:11:29 +04:00
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 13:46:37 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-07 14:11:29 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-10-07 14:11:29 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "from: @(#)rexecd.c 8.1 (Berkeley) 6/4/93";
|
|
|
|
#else
|
2004-05-11 12:07:37 +04:00
|
|
|
__RCSID("$NetBSD: rexecd.c,v 1.18 2004/05/11 08:07:37 kleink Exp $");
|
1997-10-07 14:11:29 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
1998-07-04 23:03:20 +04:00
|
|
|
#include <sys/syslog.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/time.h>
|
1997-10-07 14:11:29 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <netinet/in.h>
|
1997-10-07 14:11:29 +04:00
|
|
|
|
1998-07-04 23:03:20 +04:00
|
|
|
#include <err.h>
|
1997-10-07 14:11:29 +04:00
|
|
|
#include <errno.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <netdb.h>
|
1997-10-07 14:11:29 +04:00
|
|
|
#include <paths.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <pwd.h>
|
1997-10-07 14:11:29 +04:00
|
|
|
#include <signal.h>
|
2002-05-26 04:02:06 +04:00
|
|
|
#include <stdarg.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-10-07 14:11:29 +04:00
|
|
|
#include <unistd.h>
|
2002-09-23 07:32:34 +04:00
|
|
|
#include <poll.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2000-10-10 23:54:37 +04:00
|
|
|
void error __P((const char *, ...))
|
|
|
|
__attribute__((__format__(__printf__, 1, 2)));
|
1997-10-07 14:11:29 +04:00
|
|
|
int main __P((int, char **));
|
2002-06-06 03:05:34 +04:00
|
|
|
void doit __P((int, struct sockaddr *));
|
1997-10-07 14:11:29 +04:00
|
|
|
void getstr __P((char *, int, char *));
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-07-04 23:03:20 +04:00
|
|
|
char username[32 + 1] = "USER=";
|
2004-05-11 12:07:37 +04:00
|
|
|
char logname[32 + 3 + 1] = "LOGNAME=";
|
1998-07-04 23:03:20 +04:00
|
|
|
char homedir[PATH_MAX + 1] = "HOME=";
|
|
|
|
char shell[PATH_MAX + 1] = "SHELL=";
|
|
|
|
char path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH=";
|
2004-05-11 12:07:37 +04:00
|
|
|
char *envinit[] = { homedir, shell, path, username, logname, 0 };
|
1998-07-04 23:03:20 +04:00
|
|
|
char **environ;
|
2002-12-06 04:59:22 +03:00
|
|
|
int dolog;
|
1998-07-04 23:03:20 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* remote execute server:
|
|
|
|
* username\0
|
|
|
|
* password\0
|
|
|
|
* command\0
|
|
|
|
* data
|
|
|
|
*/
|
1997-10-07 14:11:29 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
2002-06-06 03:05:34 +04:00
|
|
|
struct sockaddr_storage from;
|
1998-07-04 23:03:20 +04:00
|
|
|
int fromlen, ch;
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "l")) != -1)
|
|
|
|
switch (ch) {
|
|
|
|
case 'l':
|
2002-12-06 04:59:22 +03:00
|
|
|
dolog = 1;
|
1998-07-04 23:03:20 +04:00
|
|
|
openlog("rexecd", LOG_PID, LOG_DAEMON);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exit(1);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
fromlen = sizeof (from);
|
1998-07-04 23:03:20 +04:00
|
|
|
if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0)
|
|
|
|
err(1, "getpeername");
|
|
|
|
|
2002-06-06 03:05:34 +04:00
|
|
|
doit(0, (struct sockaddr *)&from);
|
1997-10-07 14:11:29 +04:00
|
|
|
exit(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-10-07 14:11:29 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
doit(f, fromp)
|
|
|
|
int f;
|
2002-06-06 03:05:34 +04:00
|
|
|
struct sockaddr *fromp;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-07-04 23:03:20 +04:00
|
|
|
struct pollfd fds[2];
|
1998-07-26 23:44:12 +04:00
|
|
|
char cmdbuf[NCARGS+1], *namep;
|
|
|
|
const char *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
char user[16], pass[16];
|
1998-07-04 23:03:20 +04:00
|
|
|
char buf[BUFSIZ], sig;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct passwd *pwd;
|
1997-10-07 14:11:29 +04:00
|
|
|
int s = -1; /* XXX gcc */
|
1998-07-04 23:03:20 +04:00
|
|
|
int pv[2], pid, cc;
|
1993-03-21 12:45:37 +03:00
|
|
|
int one = 1;
|
1998-07-04 23:03:20 +04:00
|
|
|
in_port_t port;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-07-04 23:03:20 +04:00
|
|
|
(void)signal(SIGINT, SIG_DFL);
|
|
|
|
(void)signal(SIGQUIT, SIG_DFL);
|
|
|
|
(void)signal(SIGTERM, SIG_DFL);
|
1993-03-21 12:45:37 +03:00
|
|
|
dup2(f, 0);
|
|
|
|
dup2(f, 1);
|
|
|
|
dup2(f, 2);
|
1998-07-04 23:03:20 +04:00
|
|
|
(void)alarm(60);
|
1993-03-21 12:45:37 +03:00
|
|
|
port = 0;
|
|
|
|
for (;;) {
|
|
|
|
char c;
|
1998-07-04 23:03:20 +04:00
|
|
|
if (read(f, &c, 1) != 1) {
|
2002-12-06 04:59:22 +03:00
|
|
|
if (dolog)
|
1998-07-04 23:03:20 +04:00
|
|
|
syslog(LOG_ERR,
|
|
|
|
"initial read failed");
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
1998-07-04 23:03:20 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
if (c == 0)
|
|
|
|
break;
|
|
|
|
port = port * 10 + c - '0';
|
|
|
|
}
|
1998-07-04 23:03:20 +04:00
|
|
|
(void)alarm(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (port != 0) {
|
2002-06-06 03:05:34 +04:00
|
|
|
s = socket(fromp->sa_family, SOCK_STREAM, 0);
|
1998-07-04 23:03:20 +04:00
|
|
|
if (s < 0) {
|
2002-12-06 04:59:22 +03:00
|
|
|
if (dolog)
|
1998-07-04 23:03:20 +04:00
|
|
|
syslog(LOG_ERR, "socket: %m");
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
1998-07-04 23:03:20 +04:00
|
|
|
}
|
2002-06-06 03:05:34 +04:00
|
|
|
(void)alarm(60);
|
|
|
|
switch (fromp->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
((struct sockaddr_in *)fromp)->sin_port = htons(port);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
((struct sockaddr_in6 *)fromp)->sin6_port = htons(port);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
syslog(LOG_ERR, "unsupported address family");
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
1998-07-04 23:03:20 +04:00
|
|
|
}
|
2002-06-06 03:05:34 +04:00
|
|
|
if (connect(s, (struct sockaddr *)fromp, fromp->sa_len) < 0) {
|
2002-12-06 04:59:22 +03:00
|
|
|
if (dolog)
|
1998-07-04 23:03:20 +04:00
|
|
|
syslog(LOG_ERR, "connect: %m");
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
1998-07-04 23:03:20 +04:00
|
|
|
}
|
|
|
|
(void)alarm(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
getstr(user, sizeof(user), "username");
|
|
|
|
getstr(pass, sizeof(pass), "password");
|
|
|
|
getstr(cmdbuf, sizeof(cmdbuf), "command");
|
|
|
|
setpwent();
|
|
|
|
pwd = getpwnam(user);
|
|
|
|
if (pwd == NULL) {
|
|
|
|
error("Login incorrect.\n");
|
2002-12-06 04:59:22 +03:00
|
|
|
if (dolog)
|
1998-07-04 23:03:20 +04:00
|
|
|
syslog(LOG_ERR, "no such user %s", user);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
endpwent();
|
|
|
|
if (*pwd->pw_passwd != '\0') {
|
|
|
|
namep = crypt(pass, pwd->pw_passwd);
|
|
|
|
if (strcmp(namep, pwd->pw_passwd)) {
|
1998-07-04 23:03:20 +04:00
|
|
|
error("Password incorrect.\n"); /* XXX: wrong! */
|
2002-12-06 04:59:22 +03:00
|
|
|
if (dolog)
|
1998-07-04 23:03:20 +04:00
|
|
|
syslog(LOG_ERR, "incorrect password for %s",
|
|
|
|
user);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
1998-07-04 23:03:20 +04:00
|
|
|
} else
|
|
|
|
(void)crypt("dummy password", "PA"); /* must always crypt */
|
1993-03-21 12:45:37 +03:00
|
|
|
if (chdir(pwd->pw_dir) < 0) {
|
|
|
|
error("No remote directory.\n");
|
2002-12-06 04:59:22 +03:00
|
|
|
if (dolog)
|
1998-07-04 23:03:20 +04:00
|
|
|
syslog(LOG_ERR, "%s does not exist for %s", pwd->pw_dir,
|
|
|
|
user);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
1998-07-04 23:03:20 +04:00
|
|
|
(void)write(2, "\0", 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (port) {
|
1998-07-04 23:03:20 +04:00
|
|
|
if (pipe(pv) < 0 || (pid = fork()) == -1) {
|
1993-03-21 12:45:37 +03:00
|
|
|
error("Try again.\n");
|
2002-12-06 04:59:22 +03:00
|
|
|
if (dolog)
|
1998-07-04 23:03:20 +04:00
|
|
|
syslog(LOG_ERR,"pipe or fork failed for %s: %m",
|
|
|
|
user);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (pid) {
|
1998-07-04 23:03:20 +04:00
|
|
|
(void)close(0);
|
|
|
|
(void)close(1);
|
|
|
|
(void)close(2);
|
|
|
|
(void)close(f);
|
|
|
|
(void)close(pv[1]);
|
|
|
|
fds[0].fd = s;
|
|
|
|
fds[1].fd = pv[0];
|
|
|
|
fds[0].events = fds[1].events = POLLIN;
|
|
|
|
if (ioctl(pv[1], FIONBIO, (char *)&one) < 0)
|
2001-04-06 15:13:45 +04:00
|
|
|
_exit(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
/* should set s nbio! */
|
|
|
|
do {
|
1998-07-04 23:03:20 +04:00
|
|
|
if (poll(fds, 2, 0) < 0) {
|
|
|
|
close(s);
|
|
|
|
close(pv[0]);
|
2001-04-06 15:13:45 +04:00
|
|
|
_exit(1);
|
1998-07-04 23:03:20 +04:00
|
|
|
}
|
|
|
|
if (fds[0].revents & POLLIN) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (read(s, &sig, 1) <= 0)
|
1998-07-04 23:03:20 +04:00
|
|
|
fds[0].events = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
|
|
|
killpg(pid, sig);
|
|
|
|
}
|
1998-07-04 23:03:20 +04:00
|
|
|
if (fds[1].revents & POLLIN) {
|
1993-03-21 12:45:37 +03:00
|
|
|
cc = read(pv[0], buf, sizeof (buf));
|
|
|
|
if (cc <= 0) {
|
|
|
|
shutdown(s, 1+1);
|
1998-07-04 23:03:20 +04:00
|
|
|
fds[1].events = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
} else
|
1998-07-04 23:03:20 +04:00
|
|
|
(void)write(s, buf, cc);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1998-07-04 23:03:20 +04:00
|
|
|
} while ((fds[0].events | fds[1].events) & POLLIN);
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
(void)close(s);
|
|
|
|
(void)close(pv[0]);
|
|
|
|
if (dup2(pv[1], 2) < 0) {
|
|
|
|
error("Try again.\n");
|
2002-12-06 04:59:22 +03:00
|
|
|
if (dolog)
|
1998-07-04 23:03:20 +04:00
|
|
|
syslog(LOG_ERR, "dup2 failed for %s", user);
|
|
|
|
exit(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*pwd->pw_shell == '\0')
|
|
|
|
pwd->pw_shell = _PATH_BSHELL;
|
|
|
|
if (f > 2)
|
1998-07-04 23:03:20 +04:00
|
|
|
(void)close(f);
|
2003-03-03 21:57:53 +03:00
|
|
|
if (setsid() < 0 ||
|
|
|
|
setlogin(pwd->pw_name) < 0 ||
|
1998-07-04 23:03:20 +04:00
|
|
|
initgroups(pwd->pw_name, pwd->pw_gid) < 0 ||
|
|
|
|
setgid((gid_t)pwd->pw_gid) < 0 ||
|
|
|
|
setuid((uid_t)pwd->pw_uid) < 0) {
|
|
|
|
error("Try again.\n");
|
2002-12-06 04:59:22 +03:00
|
|
|
if (dolog)
|
1998-07-04 23:03:20 +04:00
|
|
|
syslog(LOG_ERR, "could not set permissions for %s: %m",
|
|
|
|
user);
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-05-18 02:56:31 +04:00
|
|
|
(void)strlcat(path, _PATH_DEFPATH, sizeof(path));
|
1993-03-21 12:45:37 +03:00
|
|
|
environ = envinit;
|
2003-05-18 02:56:31 +04:00
|
|
|
strlcat(homedir, pwd->pw_dir, sizeof(homedir));
|
|
|
|
strlcat(shell, pwd->pw_shell, sizeof(shell));
|
|
|
|
strlcat(username, pwd->pw_name, sizeof(username));
|
2004-05-11 12:07:37 +04:00
|
|
|
strlcat(logname, pwd->pw_name, sizeof(logname));
|
1997-10-07 14:11:29 +04:00
|
|
|
cp = strrchr(pwd->pw_shell, '/');
|
1993-03-21 12:45:37 +03:00
|
|
|
if (cp)
|
|
|
|
cp++;
|
|
|
|
else
|
|
|
|
cp = pwd->pw_shell;
|
2002-12-06 04:59:22 +03:00
|
|
|
if (dolog)
|
1998-07-04 23:03:20 +04:00
|
|
|
syslog(LOG_INFO, "running command for %s: %s", user, cmdbuf);
|
1993-03-21 12:45:37 +03:00
|
|
|
execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
|
|
|
|
perror(pwd->pw_shell);
|
2002-12-06 04:59:22 +03:00
|
|
|
if (dolog)
|
1998-07-04 23:03:20 +04:00
|
|
|
syslog(LOG_ERR, "execl failed for %s: %m", user);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1997-10-07 14:11:29 +04:00
|
|
|
void
|
|
|
|
error(const char *fmt, ...)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
1997-10-07 14:11:29 +04:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
1993-03-21 12:45:37 +03:00
|
|
|
buf[0] = 1;
|
1998-07-04 23:03:20 +04:00
|
|
|
(void)vsnprintf(buf+1, sizeof(buf) - 1, fmt, ap);
|
1997-10-07 14:11:29 +04:00
|
|
|
(void)write(2, buf, strlen(buf));
|
2001-09-24 17:22:25 +04:00
|
|
|
va_end(ap);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-10-07 14:11:29 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
getstr(buf, cnt, err)
|
|
|
|
char *buf;
|
|
|
|
int cnt;
|
|
|
|
char *err;
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (read(0, &c, 1) != 1)
|
|
|
|
exit(1);
|
|
|
|
*buf++ = c;
|
|
|
|
if (--cnt == 0) {
|
|
|
|
error("%s too long\n", err);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} while (c != 0);
|
|
|
|
}
|