1994-05-27 12:39:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-11-26 07:29:04 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:39:25 +00:00
|
|
|
"@(#) Copyright (c) 1983, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-11-26 07:29:04 +00:00
|
|
|
#if 0
|
1994-05-27 12:39:25 +00:00
|
|
|
static char sccsid[] = "@(#)rexecd.c 8.1 (Berkeley) 6/4/93";
|
1997-11-26 07:29:04 +00:00
|
|
|
#endif
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1994-05-27 12:39:25 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
1997-11-26 07:29:04 +00:00
|
|
|
#include <err.h>
|
2002-05-02 05:06:32 +00:00
|
|
|
#include <errno.h>
|
1999-04-06 23:06:00 +00:00
|
|
|
#include <libutil.h>
|
1994-05-27 12:39:25 +00:00
|
|
|
#include <paths.h>
|
2001-07-09 17:34:22 +00:00
|
|
|
#include <pwd.h>
|
1994-05-27 12:39:25 +00:00
|
|
|
#include <signal.h>
|
2002-02-07 23:57:01 +00:00
|
|
|
#include <stdarg.h>
|
1994-05-27 12:39:25 +00:00
|
|
|
#include <stdio.h>
|
2004-05-24 13:21:24 +00:00
|
|
|
#include <stdlib.h>
|
1994-05-27 12:39:25 +00:00
|
|
|
#include <string.h>
|
1994-09-29 09:23:58 +00:00
|
|
|
#include <syslog.h>
|
1997-11-26 07:29:04 +00:00
|
|
|
#include <unistd.h>
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2002-05-02 05:06:32 +00:00
|
|
|
#include <security/pam_appl.h>
|
|
|
|
#include <security/openpam.h>
|
|
|
|
|
|
|
|
static pam_handle_t *pamh;
|
|
|
|
static struct pam_conv pamc = {
|
|
|
|
openpam_nullconv,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
static int pam_flags = PAM_SILENT|PAM_DISALLOW_NULL_AUTHTOK;
|
|
|
|
static int pam_err;
|
|
|
|
#define pam_ok(err) ((pam_err = (err)) == PAM_SUCCESS)
|
|
|
|
|
1994-09-29 09:23:58 +00:00
|
|
|
char **environ;
|
1999-04-07 08:27:45 +00:00
|
|
|
char remote[MAXHOSTNAMELEN];
|
1994-09-29 09:23:58 +00:00
|
|
|
|
2005-02-23 17:13:28 +00:00
|
|
|
struct sockaddr_storage sa;
|
|
|
|
|
|
|
|
char default_shell[] = _PATH_BSHELL;
|
1994-09-29 09:23:58 +00:00
|
|
|
|
2005-03-01 10:55:06 +00:00
|
|
|
static void doit(struct sockaddr *);
|
2005-04-17 01:06:37 +00:00
|
|
|
static void getstr(char *, size_t, const char *);
|
2002-05-03 13:12:06 +00:00
|
|
|
static void error(const char *fmt, ...);
|
1997-11-26 07:29:04 +00:00
|
|
|
|
2000-05-13 15:58:36 +00:00
|
|
|
int no_uid_0 = 1;
|
|
|
|
|
1994-05-27 12:39:25 +00:00
|
|
|
/*
|
|
|
|
* remote execute server:
|
|
|
|
* username\0
|
|
|
|
* password\0
|
|
|
|
* command\0
|
|
|
|
* data
|
|
|
|
*/
|
|
|
|
/*ARGSUSED*/
|
1997-11-26 07:29:04 +00:00
|
|
|
int
|
2002-02-07 23:57:01 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
2002-04-16 10:15:30 +00:00
|
|
|
struct sockaddr_storage from;
|
2005-02-14 17:42:58 +00:00
|
|
|
socklen_t fromlen;
|
2000-11-28 18:15:25 +00:00
|
|
|
int ch;
|
|
|
|
|
|
|
|
openlog("rexecd", LOG_PID, LOG_AUTH);
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2000-11-28 18:15:25 +00:00
|
|
|
while ((ch = getopt(argc, argv, "i")) != -1)
|
2002-02-07 23:57:01 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'i':
|
|
|
|
no_uid_0 = 0;
|
|
|
|
break;
|
|
|
|
default:
|
2002-05-03 13:12:06 +00:00
|
|
|
syslog(LOG_ERR, "usage: rexecd [-i]");
|
|
|
|
exit(1);
|
2002-02-07 23:57:01 +00:00
|
|
|
}
|
2000-11-28 18:15:25 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2000-05-13 15:58:36 +00:00
|
|
|
|
1994-05-27 12:39:25 +00:00
|
|
|
fromlen = sizeof (from);
|
1997-11-26 07:29:04 +00:00
|
|
|
if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0)
|
|
|
|
err(1, "getpeername");
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2002-04-16 10:15:30 +00:00
|
|
|
realhostname_sa(remote, sizeof(remote) - 1,
|
|
|
|
(struct sockaddr *)&from, fromlen);
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2005-03-01 10:55:06 +00:00
|
|
|
doit((struct sockaddr *)&from);
|
1997-11-26 07:29:04 +00:00
|
|
|
return(0);
|
1994-09-29 09:23:58 +00:00
|
|
|
}
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2002-05-03 13:12:06 +00:00
|
|
|
static void
|
2005-03-01 10:55:06 +00:00
|
|
|
doit(struct sockaddr *fromp)
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
2005-03-21 08:01:14 +00:00
|
|
|
char *cmdbuf, *cp;
|
2005-04-17 01:06:37 +00:00
|
|
|
size_t maxcmdlen;
|
2005-04-08 11:19:50 +00:00
|
|
|
char user[16], pass[16];
|
|
|
|
struct passwd *pwd;
|
|
|
|
const void *item;
|
2002-05-02 05:06:32 +00:00
|
|
|
int fd, r, sd;
|
1994-05-27 12:39:25 +00:00
|
|
|
u_short port;
|
2002-05-02 05:06:32 +00:00
|
|
|
int pv[2], pid, cc, nfds;
|
|
|
|
fd_set rfds, fds;
|
1994-05-27 12:39:25 +00:00
|
|
|
char buf[BUFSIZ], sig;
|
|
|
|
int one = 1;
|
|
|
|
|
2005-04-17 01:06:37 +00:00
|
|
|
maxcmdlen = sysconf(_SC_ARG_MAX);
|
2005-03-21 08:01:14 +00:00
|
|
|
if (maxcmdlen <= 0 || (cmdbuf = malloc(maxcmdlen)) == NULL)
|
|
|
|
exit(1);
|
|
|
|
|
1994-05-27 12:39:25 +00:00
|
|
|
(void) signal(SIGINT, SIG_DFL);
|
|
|
|
(void) signal(SIGQUIT, SIG_DFL);
|
|
|
|
(void) signal(SIGTERM, SIG_DFL);
|
2002-05-02 05:06:32 +00:00
|
|
|
dup2(STDIN_FILENO, STDOUT_FILENO);
|
|
|
|
dup2(STDIN_FILENO, STDOUT_FILENO);
|
1994-05-27 12:39:25 +00:00
|
|
|
(void) alarm(60);
|
|
|
|
port = 0;
|
2005-02-23 17:13:28 +00:00
|
|
|
sd = -1;
|
1994-05-27 12:39:25 +00:00
|
|
|
for (;;) {
|
|
|
|
char c;
|
2002-05-02 05:06:32 +00:00
|
|
|
if (read(STDIN_FILENO, &c, 1) != 1)
|
1994-05-27 12:39:25 +00:00
|
|
|
exit(1);
|
|
|
|
if (c == 0)
|
|
|
|
break;
|
|
|
|
port = port * 10 + c - '0';
|
|
|
|
}
|
1996-11-22 08:59:07 +00:00
|
|
|
if (port != 0) {
|
2005-03-01 10:55:06 +00:00
|
|
|
sd = socket(fromp->sa_family, SOCK_STREAM, 0);
|
2002-05-02 05:06:32 +00:00
|
|
|
if (sd < 0)
|
1996-11-22 08:59:07 +00:00
|
|
|
exit(1);
|
2005-02-23 17:13:28 +00:00
|
|
|
bzero(&sa, sizeof(sa));
|
2005-03-01 10:55:06 +00:00
|
|
|
sa.ss_family = fromp->sa_family;
|
|
|
|
sa.ss_len = fromp->sa_len;
|
2005-02-23 17:13:28 +00:00
|
|
|
if (bind(sd, (struct sockaddr *)&sa, sa.ss_len) < 0)
|
1996-11-22 08:59:07 +00:00
|
|
|
exit(1);
|
2005-03-01 10:55:06 +00:00
|
|
|
switch (fromp->sa_family) {
|
2002-04-16 10:15:30 +00:00
|
|
|
case AF_INET:
|
2005-03-01 10:55:06 +00:00
|
|
|
((struct sockaddr_in *)(void *)fromp)->sin_port = htons(port);
|
2002-04-16 10:15:30 +00:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2005-03-01 10:55:06 +00:00
|
|
|
((struct sockaddr_in6 *)(void *)fromp)->sin6_port = htons(port);
|
2002-04-16 10:15:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exit(1);
|
|
|
|
}
|
2005-03-01 10:55:06 +00:00
|
|
|
if (connect(sd, fromp, fromp->sa_len) < 0)
|
1996-11-22 08:59:07 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2005-04-08 11:19:50 +00:00
|
|
|
getstr(user, sizeof(user), "username");
|
1994-05-27 12:39:25 +00:00
|
|
|
getstr(pass, sizeof(pass), "password");
|
2005-03-21 08:01:14 +00:00
|
|
|
getstr(cmdbuf, maxcmdlen, "command");
|
2002-05-02 05:06:32 +00:00
|
|
|
(void) alarm(0);
|
1995-05-30 05:51:47 +00:00
|
|
|
|
2005-04-05 14:55:33 +00:00
|
|
|
if (!pam_ok(pam_start("rexecd", user, &pamc, &pamh)) ||
|
2002-05-02 05:06:32 +00:00
|
|
|
!pam_ok(pam_set_item(pamh, PAM_RHOST, remote)) ||
|
|
|
|
!pam_ok(pam_set_item(pamh, PAM_AUTHTOK, pass)) ||
|
|
|
|
!pam_ok(pam_authenticate(pamh, pam_flags)) ||
|
2005-04-05 14:55:33 +00:00
|
|
|
!pam_ok(pam_acct_mgmt(pamh, pam_flags)) ||
|
2005-04-08 11:19:50 +00:00
|
|
|
!pam_ok(pam_get_item(pamh, PAM_USER, &item)) || item == NULL ||
|
|
|
|
strlen(item) >= sizeof(user) || strcpy(user, item) == NULL ||
|
|
|
|
(pwd = getpwnam(user)) == NULL ||
|
|
|
|
(pwd->pw_uid == 0 && no_uid_0)) {
|
1994-09-29 09:23:58 +00:00
|
|
|
syslog(LOG_ERR, "%s LOGIN REFUSED from %s", user, remote);
|
|
|
|
error("Login incorrect.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
syslog(LOG_INFO, "login from %s as %s", remote, user);
|
|
|
|
|
2001-07-26 11:02:39 +00:00
|
|
|
(void) write(STDERR_FILENO, "\0", 1);
|
2002-05-02 05:06:32 +00:00
|
|
|
if (port != 0) {
|
1994-05-27 12:39:25 +00:00
|
|
|
(void) pipe(pv);
|
2002-05-02 05:06:32 +00:00
|
|
|
|
1994-05-27 12:39:25 +00:00
|
|
|
pid = fork();
|
|
|
|
if (pid == -1) {
|
|
|
|
error("Try again.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (pid) {
|
2002-05-02 05:06:32 +00:00
|
|
|
/* parent */
|
|
|
|
(void) pam_end(pamh, pam_err);
|
|
|
|
(void) close(STDIN_FILENO);
|
|
|
|
(void) close(STDOUT_FILENO);
|
|
|
|
(void) close(STDERR_FILENO);
|
|
|
|
(void) close(pv[1]);
|
|
|
|
ioctl(pv[0], FIONBIO, (char *)&one);
|
|
|
|
/* should set sd nbio! */
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(sd, &fds);
|
|
|
|
nfds = sd + 1;
|
|
|
|
FD_SET(pv[0], &fds);
|
|
|
|
if (pv[0] >= nfds)
|
|
|
|
nfds = pv[0] + 1;
|
1994-05-27 12:39:25 +00:00
|
|
|
do {
|
2002-05-02 05:06:32 +00:00
|
|
|
rfds = fds;
|
|
|
|
for (;;) {
|
|
|
|
r = select(nfds, &rfds, NULL, NULL, NULL);
|
|
|
|
if (r > 0)
|
|
|
|
break;
|
|
|
|
if (r < 0 && errno != EINTR)
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (FD_ISSET(sd, &rfds)) {
|
|
|
|
if (read(sd, &sig, 1) <= 0)
|
|
|
|
FD_CLR(sd, &fds);
|
1994-05-27 12:39:25 +00:00
|
|
|
else
|
|
|
|
killpg(pid, sig);
|
|
|
|
}
|
2002-05-02 05:06:32 +00:00
|
|
|
if (FD_ISSET(pv[0], &fds)) {
|
1994-05-27 12:39:25 +00:00
|
|
|
cc = read(pv[0], buf, sizeof (buf));
|
|
|
|
if (cc <= 0) {
|
2002-05-02 05:06:32 +00:00
|
|
|
shutdown(sd, SHUT_RDWR);
|
|
|
|
FD_CLR(pv[0], &fds);
|
|
|
|
} else {
|
|
|
|
(void) write(sd, buf, cc);
|
|
|
|
}
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
2002-05-02 05:06:32 +00:00
|
|
|
} while (FD_ISSET(sd, &fds) || FD_ISSET(pv[0], &fds));
|
1994-05-27 12:39:25 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2002-05-02 05:06:32 +00:00
|
|
|
/* child */
|
|
|
|
(void) close(sd);
|
|
|
|
(void) close(pv[0]);
|
1994-05-27 12:39:25 +00:00
|
|
|
dup2(pv[1], 2);
|
|
|
|
}
|
2002-05-02 05:06:32 +00:00
|
|
|
for (fd = getdtablesize(); fd > 2; fd--)
|
|
|
|
(void) close(fd);
|
1994-05-27 12:39:25 +00:00
|
|
|
if (*pwd->pw_shell == '\0')
|
2005-02-23 17:13:28 +00:00
|
|
|
pwd->pw_shell = default_shell;
|
2002-05-02 05:06:32 +00:00
|
|
|
if (setsid() == -1)
|
|
|
|
syslog(LOG_ERR, "setsid() failed: %m");
|
1995-07-29 15:21:15 +00:00
|
|
|
if (setlogin(pwd->pw_name) < 0)
|
|
|
|
syslog(LOG_ERR, "setlogin() failed: %m");
|
1994-05-27 12:39:25 +00:00
|
|
|
(void) setgid((gid_t)pwd->pw_gid);
|
|
|
|
initgroups(pwd->pw_name, pwd->pw_gid);
|
2002-05-02 05:06:32 +00:00
|
|
|
if (!pam_ok(pam_setcred(pamh, PAM_ESTABLISH_CRED)))
|
|
|
|
syslog(LOG_ERR, "pam_setcred() failed: %s",
|
|
|
|
pam_strerror(pamh, pam_err));
|
|
|
|
(void) pam_setenv(pamh, "HOME", pwd->pw_dir, 1);
|
|
|
|
(void) pam_setenv(pamh, "SHELL", pwd->pw_shell, 1);
|
|
|
|
(void) pam_setenv(pamh, "USER", pwd->pw_name, 1);
|
|
|
|
(void) pam_setenv(pamh, "PATH", _PATH_DEFPATH, 1);
|
|
|
|
environ = pam_getenvlist(pamh);
|
|
|
|
(void) pam_end(pamh, pam_err);
|
1994-05-27 12:39:25 +00:00
|
|
|
(void) setuid((uid_t)pwd->pw_uid);
|
|
|
|
cp = strrchr(pwd->pw_shell, '/');
|
|
|
|
if (cp)
|
|
|
|
cp++;
|
|
|
|
else
|
|
|
|
cp = pwd->pw_shell;
|
1997-03-24 05:57:28 +00:00
|
|
|
if (chdir(pwd->pw_dir) < 0) {
|
|
|
|
error("No remote directory.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2001-07-09 09:24:06 +00:00
|
|
|
execl(pwd->pw_shell, cp, "-c", cmdbuf, (char *)0);
|
1997-11-26 07:29:04 +00:00
|
|
|
err(1, "%s", pwd->pw_shell);
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
|
|
|
|
2002-05-03 13:12:06 +00:00
|
|
|
static void
|
2002-02-07 23:57:01 +00:00
|
|
|
error(const char *fmt, ...)
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
2002-02-07 23:57:01 +00:00
|
|
|
va_list ap;
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2002-02-07 23:57:01 +00:00
|
|
|
va_start(ap, fmt);
|
1994-05-27 12:39:25 +00:00
|
|
|
buf[0] = 1;
|
2002-02-07 23:57:01 +00:00
|
|
|
(void)vsnprintf(buf + 1, sizeof(buf) - 1, fmt, ap);
|
|
|
|
(void)write(STDERR_FILENO, buf, strlen(buf));
|
|
|
|
va_end(ap);
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
|
|
|
|
2002-05-03 13:12:06 +00:00
|
|
|
static void
|
2005-04-17 01:06:37 +00:00
|
|
|
getstr(char *buf, size_t cnt, const char *field)
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
do {
|
2001-07-26 11:02:39 +00:00
|
|
|
if (read(STDIN_FILENO, &c, 1) != 1)
|
1994-05-27 12:39:25 +00:00
|
|
|
exit(1);
|
|
|
|
*buf++ = c;
|
|
|
|
if (--cnt == 0) {
|
2005-02-23 17:13:28 +00:00
|
|
|
error("%s too long\n", field);
|
1994-05-27 12:39:25 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} while (c != 0);
|
|
|
|
}
|