1*df69c215Sderaadt /* $OpenBSD: rcmdsh.c,v 1.20 2019/06/28 13:32:42 deraadt Exp $ */
2062b2a16Smillert
3062b2a16Smillert /*
4062b2a16Smillert * Copyright (c) 2001, MagniComp
5062b2a16Smillert * All rights reserved.
6062b2a16Smillert *
7062b2a16Smillert * Redistribution and use in source and binary forms, with or without
8062b2a16Smillert * modification, are permitted provided that the following conditions
9062b2a16Smillert * are met:
10062b2a16Smillert * 1. Redistributions of source code must retain the above copyright
11062b2a16Smillert * notice, this list of conditions and the following disclaimer.
12062b2a16Smillert * 2. Redistributions in binary form must reproduce the above copyright
13062b2a16Smillert * notice, this list of conditions and the following disclaimer in
14062b2a16Smillert * the documentation and/or other materials provided with the distribution.
15062b2a16Smillert * 3. Neither the name of the MagniComp nor the names of its contributors may
16062b2a16Smillert * be used to endorse or promote products derived from this software
17062b2a16Smillert * without specific prior written permission.
18062b2a16Smillert *
19062b2a16Smillert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20062b2a16Smillert * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21062b2a16Smillert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22062b2a16Smillert * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
23062b2a16Smillert * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24062b2a16Smillert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25062b2a16Smillert * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26062b2a16Smillert * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27062b2a16Smillert * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28062b2a16Smillert * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29062b2a16Smillert */
30dcbdbe15Smillert
31dcbdbe15Smillert /*
32dcbdbe15Smillert * This is an rcmd() replacement originally by
33dcbdbe15Smillert * Chris Siebenmann <cks@utcc.utoronto.ca>.
34dcbdbe15Smillert */
35dcbdbe15Smillert
36dcbdbe15Smillert #include <sys/types.h>
37dcbdbe15Smillert #include <sys/socket.h>
38dcbdbe15Smillert #include <sys/wait.h>
39dcbdbe15Smillert #include <signal.h>
40dcbdbe15Smillert #include <errno.h>
41eeba2ff2Smillert #include <limits.h>
42dcbdbe15Smillert #include <netdb.h>
43dcbdbe15Smillert #include <stdio.h>
448dd0ec21Smillert #include <stdlib.h>
45dcbdbe15Smillert #include <string.h>
46dcbdbe15Smillert #include <pwd.h>
47dcbdbe15Smillert #include <paths.h>
48c8f91e0dStholo #include <unistd.h>
49dcbdbe15Smillert
50dcbdbe15Smillert /*
51dc85dc40Sjmc * This is a replacement rcmd() function that uses the ssh(1)
52dcbdbe15Smillert * program in place of a direct rcmd(3) function call so as to
53dcbdbe15Smillert * avoid having to be root. Note that rport is ignored.
54dcbdbe15Smillert */
55dcbdbe15Smillert int
rcmdsh(char ** ahost,int rport,const char * locuser,const char * remuser,const char * cmd,char * rshprog)56db5b349cSotto rcmdsh(char **ahost, int rport, const char *locuser, const char *remuser,
57db5b349cSotto const char *cmd, char *rshprog)
58dcbdbe15Smillert {
59eeba2ff2Smillert static char hbuf[HOST_NAME_MAX+1];
60eeba2ff2Smillert struct addrinfo hint, *res;
61e4490880Sderaadt int sp[2];
62e4490880Sderaadt pid_t cpid;
63cd245bcaSmillert char *p, pwbuf[_PW_BUF_LEN];
64cd245bcaSmillert struct passwd pwstore, *pw = NULL;
65dcbdbe15Smillert
66dcbdbe15Smillert /* What rsh/shell to use. */
67dcbdbe15Smillert if (rshprog == NULL)
68dcbdbe15Smillert rshprog = _PATH_RSH;
69dcbdbe15Smillert
70dcbdbe15Smillert /* locuser must exist on this host. */
71cd245bcaSmillert getpwnam_r(locuser, &pwstore, pwbuf, sizeof(pwbuf), &pw);
72cd245bcaSmillert if (pw == NULL) {
73dcbdbe15Smillert (void) fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser);
74dcbdbe15Smillert return(-1);
75dcbdbe15Smillert }
76dcbdbe15Smillert
77dcbdbe15Smillert /* Validate remote hostname. */
783b65d27bSmillert if (strcmp(*ahost, "localhost") != 0) {
79eeba2ff2Smillert memset(&hint, 0, sizeof(hint));
80eeba2ff2Smillert hint.ai_family = PF_UNSPEC;
81eeba2ff2Smillert hint.ai_flags = AI_CANONNAME;
82eeba2ff2Smillert if (getaddrinfo(*ahost, NULL, &hint, &res) == 0) {
83eeba2ff2Smillert if (res->ai_canonname) {
84eeba2ff2Smillert strlcpy(hbuf, res->ai_canonname, sizeof(hbuf));
85eeba2ff2Smillert *ahost = hbuf;
86eeba2ff2Smillert }
87eeba2ff2Smillert freeaddrinfo(res);
88eeba2ff2Smillert }
893b65d27bSmillert }
90dcbdbe15Smillert
91dcbdbe15Smillert /* Get a socketpair we'll use for stdin and stdout. */
92*df69c215Sderaadt if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) {
93dcbdbe15Smillert perror("rcmdsh: socketpair");
94dcbdbe15Smillert return(-1);
95dcbdbe15Smillert }
96dcbdbe15Smillert
97dcbdbe15Smillert cpid = fork();
98*df69c215Sderaadt if (cpid == -1) {
99dcbdbe15Smillert perror("rcmdsh: fork failed");
100dcbdbe15Smillert return(-1);
101dcbdbe15Smillert } else if (cpid == 0) {
102dcbdbe15Smillert /*
103dcbdbe15Smillert * Child. We use sp[1] to be stdin/stdout, and close sp[0].
104dcbdbe15Smillert */
105dcbdbe15Smillert (void) close(sp[0]);
106*df69c215Sderaadt if (dup2(sp[1], 0) == -1 || dup2(0, 1) == -1) {
107dcbdbe15Smillert perror("rcmdsh: dup2 failed");
108dcbdbe15Smillert _exit(255);
109dcbdbe15Smillert }
110dcbdbe15Smillert /* Fork again to lose parent. */
111dcbdbe15Smillert cpid = fork();
112*df69c215Sderaadt if (cpid == -1) {
113dcbdbe15Smillert perror("rcmdsh: fork to lose parent failed");
114dcbdbe15Smillert _exit(255);
115dcbdbe15Smillert }
116dcbdbe15Smillert if (cpid > 0)
117dcbdbe15Smillert _exit(0);
118dcbdbe15Smillert
119dcbdbe15Smillert /* In grandchild here. Become local user for rshprog. */
120dcbdbe15Smillert if (setuid(pw->pw_uid)) {
121dcbdbe15Smillert (void) fprintf(stderr, "rcmdsh: setuid(%u): %s\n",
122dcbdbe15Smillert pw->pw_uid, strerror(errno));
123dcbdbe15Smillert _exit(255);
124dcbdbe15Smillert }
125dcbdbe15Smillert
126dcbdbe15Smillert /*
127dcbdbe15Smillert * If remote host is "localhost" and local and remote user
128dcbdbe15Smillert * are the same, avoid running remote shell for efficiency.
129dcbdbe15Smillert */
130dcbdbe15Smillert if (!strcmp(*ahost, "localhost") && !strcmp(locuser, remuser)) {
1318dd0ec21Smillert char *argv[4];
132dcbdbe15Smillert if (pw->pw_shell[0] == '\0')
133dcbdbe15Smillert rshprog = _PATH_BSHELL;
134dcbdbe15Smillert else
135dcbdbe15Smillert rshprog = pw->pw_shell;
136dcbdbe15Smillert p = strrchr(rshprog, '/');
1378dd0ec21Smillert argv[0] = p ? p + 1 : rshprog;
1388dd0ec21Smillert argv[1] = "-c";
1398dd0ec21Smillert argv[2] = (char *)cmd;
1408dd0ec21Smillert argv[3] = NULL;
1418dd0ec21Smillert execvp(rshprog, argv);
1428dd0ec21Smillert } else if ((p = strchr(rshprog, ' ')) == NULL) {
1438dd0ec21Smillert /* simple case */
1448dd0ec21Smillert char *argv[6];
145dcbdbe15Smillert p = strrchr(rshprog, '/');
1468dd0ec21Smillert argv[0] = p ? p + 1 : rshprog;
1478dd0ec21Smillert argv[1] = "-l";
1488dd0ec21Smillert argv[2] = (char *)remuser;
1498dd0ec21Smillert argv[3] = *ahost;
1508dd0ec21Smillert argv[4] = (char *)cmd;
1518dd0ec21Smillert argv[5] = NULL;
1528dd0ec21Smillert execvp(rshprog, argv);
1538dd0ec21Smillert } else {
1548dd0ec21Smillert /* must pull args out of rshprog and dyn alloc argv */
1558dd0ec21Smillert char **argv, **ap;
1568dd0ec21Smillert int n;
1578dd0ec21Smillert for (n = 7; (p = strchr(++p, ' ')) != NULL; n++)
1588dd0ec21Smillert continue;
1598dd0ec21Smillert rshprog = strdup(rshprog);
1601ed98fdfSderaadt ap = argv = calloc(sizeof(char *), n);
1618dd0ec21Smillert if (rshprog == NULL || argv == NULL) {
1628dd0ec21Smillert perror("rcmdsh");
1638dd0ec21Smillert _exit(255);
164dcbdbe15Smillert }
1658dd0ec21Smillert while ((p = strsep(&rshprog, " ")) != NULL) {
1668dd0ec21Smillert if (*p == '\0')
1678dd0ec21Smillert continue;
1688dd0ec21Smillert *ap++ = p;
1698dd0ec21Smillert }
1708dd0ec21Smillert if (ap != argv) /* all spaces?!? */
1718dd0ec21Smillert rshprog = argv[0];
1728dd0ec21Smillert if ((p = strrchr(argv[0], '/')) != NULL)
1738dd0ec21Smillert argv[0] = p + 1;
1748dd0ec21Smillert *ap++ = "-l";
1758dd0ec21Smillert *ap++ = (char *)remuser;
1768dd0ec21Smillert *ap++ = *ahost;
1778dd0ec21Smillert *ap++ = (char *)cmd;
1788dd0ec21Smillert *ap++ = NULL;
1798dd0ec21Smillert execvp(rshprog, argv);
1808dd0ec21Smillert }
1818dd0ec21Smillert (void) fprintf(stderr, "rcmdsh: execvp %s failed: %s\n",
182dcbdbe15Smillert rshprog, strerror(errno));
183dcbdbe15Smillert _exit(255);
184c8f91e0dStholo } else {
185dcbdbe15Smillert /* Parent. close sp[1], return sp[0]. */
186dcbdbe15Smillert (void) close(sp[1]);
187dcbdbe15Smillert /* Reap child. */
1885b6c92c3Sguenther while (waitpid(cpid, NULL, 0) == -1 && errno == EINTR)
1895b6c92c3Sguenther ;
190dcbdbe15Smillert return(sp[0]);
191dcbdbe15Smillert }
192dcbdbe15Smillert /* NOTREACHED */
193dcbdbe15Smillert }
1940e3c6306Sguenther DEF_WEAK(rcmdsh);
195