2015-08-18 22:37:25 +00:00
|
|
|
/* $OpenBSD: rcmdsh.c,v 1.7 2002/03/12 00:05:44 millert Exp $ */
|
2001-10-23 06:22:15 +00:00
|
|
|
|
2017-11-20 19:49:47 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2002-04-15 12:46:07 +00:00
|
|
|
* Copyright (c) 2001, MagniComp
|
|
|
|
* 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. Neither the name of the MagniComp 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 COPYRIGHT HOLDERS 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.
|
|
|
|
*/
|
|
|
|
|
2001-10-23 06:22:15 +00:00
|
|
|
/*
|
2001-11-09 15:19:25 +00:00
|
|
|
* This is an rcmd() replacement originally by
|
2001-10-23 06:22:15 +00:00
|
|
|
* Chris Siebenmann <cks@utcc.utoronto.ca>.
|
|
|
|
*/
|
|
|
|
|
2001-11-09 15:19:25 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
2001-10-23 06:22:15 +00:00
|
|
|
|
2015-12-20 23:05:20 +00:00
|
|
|
#include "namespace.h"
|
2001-11-09 15:19:25 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/wait.h>
|
2003-02-27 13:40:01 +00:00
|
|
|
#include <arpa/inet.h>
|
2001-11-09 15:19:25 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2015-12-20 23:05:20 +00:00
|
|
|
#include "un-namespace.h"
|
2001-10-23 06:22:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a replacement rcmd() function that uses the rsh(1)
|
|
|
|
* program in place of a direct rcmd(3) function call so as to
|
|
|
|
* avoid having to be root. Note that rport is ignored.
|
|
|
|
*/
|
|
|
|
int
|
2015-08-18 22:37:25 +00:00
|
|
|
rcmdsh(char **ahost, int rport, const char *locuser, const char *remuser,
|
|
|
|
const char *cmd, const char *rshprog)
|
2001-10-23 06:22:15 +00:00
|
|
|
{
|
2002-04-15 18:45:20 +00:00
|
|
|
struct addrinfo hints, *res;
|
2015-08-18 22:37:25 +00:00
|
|
|
int sp[2], error;
|
|
|
|
pid_t cpid;
|
2001-10-23 06:22:15 +00:00
|
|
|
char *p;
|
|
|
|
struct passwd *pw;
|
2002-04-15 18:45:20 +00:00
|
|
|
char num[8];
|
|
|
|
static char hbuf[NI_MAXHOST];
|
2001-10-23 06:22:15 +00:00
|
|
|
|
|
|
|
/* What rsh/shell to use. */
|
|
|
|
if (rshprog == NULL)
|
|
|
|
rshprog = _PATH_RSH;
|
|
|
|
|
|
|
|
/* locuser must exist on this host. */
|
|
|
|
if ((pw = getpwnam(locuser)) == NULL) {
|
2001-11-09 15:19:25 +00:00
|
|
|
(void)fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser);
|
|
|
|
return (-1);
|
2001-10-23 06:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate remote hostname. */
|
|
|
|
if (strcmp(*ahost, "localhost") != 0) {
|
2002-04-15 18:45:20 +00:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_flags = AI_CANONNAME;
|
|
|
|
hints.ai_family = PF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2003-02-27 13:40:01 +00:00
|
|
|
(void)snprintf(num, sizeof(num), "%u",
|
|
|
|
(unsigned int)ntohs(rport));
|
2002-04-15 18:45:20 +00:00
|
|
|
error = getaddrinfo(*ahost, num, &hints, &res);
|
|
|
|
if (error) {
|
|
|
|
fprintf(stderr, "rcmdsh: getaddrinfo: %s\n",
|
|
|
|
gai_strerror(error));
|
2001-11-09 15:19:25 +00:00
|
|
|
return (-1);
|
2001-10-23 06:22:15 +00:00
|
|
|
}
|
2002-04-15 18:45:20 +00:00
|
|
|
if (res->ai_canonname) {
|
|
|
|
strncpy(hbuf, res->ai_canonname, sizeof(hbuf) - 1);
|
|
|
|
hbuf[sizeof(hbuf) - 1] = '\0';
|
|
|
|
*ahost = hbuf;
|
|
|
|
}
|
|
|
|
freeaddrinfo(res);
|
2001-10-23 06:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a socketpair we'll use for stdin and stdout. */
|
2015-12-20 23:05:20 +00:00
|
|
|
if (_socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) {
|
2001-10-23 06:22:15 +00:00
|
|
|
perror("rcmdsh: socketpair");
|
2001-11-09 15:19:25 +00:00
|
|
|
return (-1);
|
2001-10-23 06:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cpid = fork();
|
2001-11-09 15:19:25 +00:00
|
|
|
if (cpid == -1) {
|
2001-10-23 06:22:15 +00:00
|
|
|
perror("rcmdsh: fork failed");
|
2001-11-09 15:19:25 +00:00
|
|
|
return (-1);
|
2001-10-23 06:22:15 +00:00
|
|
|
} else if (cpid == 0) {
|
|
|
|
/*
|
|
|
|
* Child. We use sp[1] to be stdin/stdout, and close sp[0].
|
|
|
|
*/
|
2015-12-20 23:05:20 +00:00
|
|
|
(void)_close(sp[0]);
|
|
|
|
if (_dup2(sp[1], 0) == -1 || _dup2(0, 1) == -1) {
|
2001-10-23 06:22:15 +00:00
|
|
|
perror("rcmdsh: dup2 failed");
|
|
|
|
_exit(255);
|
|
|
|
}
|
|
|
|
/* Fork again to lose parent. */
|
|
|
|
cpid = fork();
|
2001-11-09 15:19:25 +00:00
|
|
|
if (cpid == -1) {
|
2001-10-23 06:22:15 +00:00
|
|
|
perror("rcmdsh: fork to lose parent failed");
|
|
|
|
_exit(255);
|
|
|
|
}
|
|
|
|
if (cpid > 0)
|
|
|
|
_exit(0);
|
|
|
|
|
|
|
|
/* In grandchild here. Become local user for rshprog. */
|
2001-11-09 15:19:25 +00:00
|
|
|
if (setuid(pw->pw_uid) == -1) {
|
|
|
|
(void)fprintf(stderr, "rcmdsh: setuid(%u): %s\n",
|
|
|
|
pw->pw_uid, strerror(errno));
|
2001-10-23 06:22:15 +00:00
|
|
|
_exit(255);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-11-09 15:19:25 +00:00
|
|
|
* If remote host is "localhost" and local and remote users
|
2001-10-23 06:22:15 +00:00
|
|
|
* are the same, avoid running remote shell for efficiency.
|
|
|
|
*/
|
2001-11-09 15:19:25 +00:00
|
|
|
if (strcmp(*ahost, "localhost") == 0 &&
|
|
|
|
strcmp(locuser, remuser) == 0) {
|
2001-10-23 06:22:15 +00:00
|
|
|
if (pw->pw_shell[0] == '\0')
|
|
|
|
rshprog = _PATH_BSHELL;
|
|
|
|
else
|
|
|
|
rshprog = pw->pw_shell;
|
|
|
|
p = strrchr(rshprog, '/');
|
2001-11-09 15:19:25 +00:00
|
|
|
execlp(rshprog, p ? p + 1 : rshprog, "-c", cmd,
|
|
|
|
(char *)NULL);
|
2001-10-23 06:22:15 +00:00
|
|
|
} else {
|
|
|
|
p = strrchr(rshprog, '/');
|
2001-11-09 15:19:25 +00:00
|
|
|
execlp(rshprog, p ? p + 1 : rshprog, *ahost, "-l",
|
|
|
|
remuser, cmd, (char *)NULL);
|
2001-10-23 06:22:15 +00:00
|
|
|
}
|
2001-11-09 15:19:25 +00:00
|
|
|
(void)fprintf(stderr, "rcmdsh: execlp %s failed: %s\n",
|
|
|
|
rshprog, strerror(errno));
|
2001-10-23 06:22:15 +00:00
|
|
|
_exit(255);
|
|
|
|
} else {
|
|
|
|
/* Parent. close sp[1], return sp[0]. */
|
2015-12-20 23:05:20 +00:00
|
|
|
(void)_close(sp[1]);
|
2001-10-23 06:22:15 +00:00
|
|
|
/* Reap child. */
|
2015-12-20 23:05:20 +00:00
|
|
|
(void)_waitpid(cpid, NULL, 0);
|
2001-11-09 15:19:25 +00:00
|
|
|
return (sp[0]);
|
2001-10-23 06:22:15 +00:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|