18823d24bSXin LI /* $OpenBSD: rcmdsh.c,v 1.7 2002/03/12 00:05:44 millert Exp $ */
2d4567212SWarner Losh
3*8a16b7a1SPedro F. Giffuni /*-
4*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
5*8a16b7a1SPedro F. Giffuni *
69368e99fSJacques Vidrine * Copyright (c) 2001, MagniComp
79368e99fSJacques Vidrine * All rights reserved.
89368e99fSJacques Vidrine *
99368e99fSJacques Vidrine * Redistribution and use in source and binary forms, with or without
109368e99fSJacques Vidrine * modification, are permitted provided that the following conditions
119368e99fSJacques Vidrine * are met:
129368e99fSJacques Vidrine * 1. Redistributions of source code must retain the above copyright
139368e99fSJacques Vidrine * notice, this list of conditions and the following disclaimer.
149368e99fSJacques Vidrine * 2. Redistributions in binary form must reproduce the above copyright
159368e99fSJacques Vidrine * notice, this list of conditions and the following disclaimer in
169368e99fSJacques Vidrine * the documentation and/or other materials provided with the distribution.
179368e99fSJacques Vidrine * 3. Neither the name of the MagniComp nor the names of its contributors may
189368e99fSJacques Vidrine * be used to endorse or promote products derived from this software
199368e99fSJacques Vidrine * without specific prior written permission.
209368e99fSJacques Vidrine *
219368e99fSJacques Vidrine * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
229368e99fSJacques Vidrine * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239368e99fSJacques Vidrine * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249368e99fSJacques Vidrine * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
259368e99fSJacques Vidrine * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269368e99fSJacques Vidrine * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
279368e99fSJacques Vidrine * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
289368e99fSJacques Vidrine * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
299368e99fSJacques Vidrine * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
309368e99fSJacques Vidrine * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
319368e99fSJacques Vidrine */
329368e99fSJacques Vidrine
339368e99fSJacques Vidrine /*
34d4567212SWarner Losh * This is an rcmd() replacement originally by
35d4567212SWarner Losh * Chris Siebenmann <cks@utcc.utoronto.ca>.
36d4567212SWarner Losh */
37d4567212SWarner Losh
3859797edfSJilles Tjoelker #include "namespace.h"
39d4567212SWarner Losh #include <sys/types.h>
40d4567212SWarner Losh #include <sys/socket.h>
41d4567212SWarner Losh #include <sys/wait.h>
422bbd7cf8SJacques Vidrine #include <arpa/inet.h>
43e117e7a5SRuslan Ermilov
44d4567212SWarner Losh #include <errno.h>
45d4567212SWarner Losh #include <netdb.h>
46e117e7a5SRuslan Ermilov #include <paths.h>
47e117e7a5SRuslan Ermilov #include <pwd.h>
48d4567212SWarner Losh #include <stdio.h>
49d4567212SWarner Losh #include <string.h>
50d4567212SWarner Losh #include <unistd.h>
5159797edfSJilles Tjoelker #include "un-namespace.h"
52d4567212SWarner Losh
53d4567212SWarner Losh /*
54d4567212SWarner Losh * This is a replacement rcmd() function that uses the rsh(1)
55d4567212SWarner Losh * program in place of a direct rcmd(3) function call so as to
56d4567212SWarner Losh * avoid having to be root. Note that rport is ignored.
57d4567212SWarner Losh */
58d4567212SWarner Losh int
rcmdsh(char ** ahost,int rport,const char * locuser,const char * remuser,const char * cmd,const char * rshprog)598823d24bSXin LI rcmdsh(char **ahost, int rport, const char *locuser, const char *remuser,
608823d24bSXin LI const char *cmd, const char *rshprog)
61d4567212SWarner Losh {
627b0e82a9SHajimu UMEMOTO struct addrinfo hints, *res;
638823d24bSXin LI int sp[2], error;
648823d24bSXin LI pid_t cpid;
65d4567212SWarner Losh char *p;
66d4567212SWarner Losh struct passwd *pw;
677b0e82a9SHajimu UMEMOTO char num[8];
687b0e82a9SHajimu UMEMOTO static char hbuf[NI_MAXHOST];
69d4567212SWarner Losh
70d4567212SWarner Losh /* What rsh/shell to use. */
71d4567212SWarner Losh if (rshprog == NULL)
72d4567212SWarner Losh rshprog = _PATH_RSH;
73d4567212SWarner Losh
74d4567212SWarner Losh /* locuser must exist on this host. */
75d4567212SWarner Losh if ((pw = getpwnam(locuser)) == NULL) {
76d4567212SWarner Losh (void)fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser);
77d4567212SWarner Losh return (-1);
78d4567212SWarner Losh }
79d4567212SWarner Losh
80d4567212SWarner Losh /* Validate remote hostname. */
81d4567212SWarner Losh if (strcmp(*ahost, "localhost") != 0) {
827b0e82a9SHajimu UMEMOTO memset(&hints, 0, sizeof(hints));
837b0e82a9SHajimu UMEMOTO hints.ai_flags = AI_CANONNAME;
847b0e82a9SHajimu UMEMOTO hints.ai_family = PF_UNSPEC;
857b0e82a9SHajimu UMEMOTO hints.ai_socktype = SOCK_STREAM;
862bbd7cf8SJacques Vidrine (void)snprintf(num, sizeof(num), "%u",
872bbd7cf8SJacques Vidrine (unsigned int)ntohs(rport));
887b0e82a9SHajimu UMEMOTO error = getaddrinfo(*ahost, num, &hints, &res);
897b0e82a9SHajimu UMEMOTO if (error) {
907b0e82a9SHajimu UMEMOTO fprintf(stderr, "rcmdsh: getaddrinfo: %s\n",
917b0e82a9SHajimu UMEMOTO gai_strerror(error));
92d4567212SWarner Losh return (-1);
93d4567212SWarner Losh }
947b0e82a9SHajimu UMEMOTO if (res->ai_canonname) {
957b0e82a9SHajimu UMEMOTO strncpy(hbuf, res->ai_canonname, sizeof(hbuf) - 1);
967b0e82a9SHajimu UMEMOTO hbuf[sizeof(hbuf) - 1] = '\0';
977b0e82a9SHajimu UMEMOTO *ahost = hbuf;
987b0e82a9SHajimu UMEMOTO }
997b0e82a9SHajimu UMEMOTO freeaddrinfo(res);
100d4567212SWarner Losh }
101d4567212SWarner Losh
102d4567212SWarner Losh /* Get a socketpair we'll use for stdin and stdout. */
10359797edfSJilles Tjoelker if (_socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) {
104d4567212SWarner Losh perror("rcmdsh: socketpair");
105d4567212SWarner Losh return (-1);
106d4567212SWarner Losh }
107d4567212SWarner Losh
108d4567212SWarner Losh cpid = fork();
109e117e7a5SRuslan Ermilov if (cpid == -1) {
110d4567212SWarner Losh perror("rcmdsh: fork failed");
111d4567212SWarner Losh return (-1);
112d4567212SWarner Losh } else if (cpid == 0) {
113d4567212SWarner Losh /*
114d4567212SWarner Losh * Child. We use sp[1] to be stdin/stdout, and close sp[0].
115d4567212SWarner Losh */
11659797edfSJilles Tjoelker (void)_close(sp[0]);
11759797edfSJilles Tjoelker if (_dup2(sp[1], 0) == -1 || _dup2(0, 1) == -1) {
118d4567212SWarner Losh perror("rcmdsh: dup2 failed");
119d4567212SWarner Losh _exit(255);
120d4567212SWarner Losh }
121d4567212SWarner Losh /* Fork again to lose parent. */
122d4567212SWarner Losh cpid = fork();
123e117e7a5SRuslan Ermilov if (cpid == -1) {
124d4567212SWarner Losh perror("rcmdsh: fork to lose parent failed");
125d4567212SWarner Losh _exit(255);
126d4567212SWarner Losh }
127d4567212SWarner Losh if (cpid > 0)
128d4567212SWarner Losh _exit(0);
129d4567212SWarner Losh
130d4567212SWarner Losh /* In grandchild here. Become local user for rshprog. */
131e117e7a5SRuslan Ermilov if (setuid(pw->pw_uid) == -1) {
132d4567212SWarner Losh (void)fprintf(stderr, "rcmdsh: setuid(%u): %s\n",
133d4567212SWarner Losh pw->pw_uid, strerror(errno));
134d4567212SWarner Losh _exit(255);
135d4567212SWarner Losh }
136d4567212SWarner Losh
137d4567212SWarner Losh /*
138e117e7a5SRuslan Ermilov * If remote host is "localhost" and local and remote users
139d4567212SWarner Losh * are the same, avoid running remote shell for efficiency.
140d4567212SWarner Losh */
141e117e7a5SRuslan Ermilov if (strcmp(*ahost, "localhost") == 0 &&
142e117e7a5SRuslan Ermilov strcmp(locuser, remuser) == 0) {
143d4567212SWarner Losh if (pw->pw_shell[0] == '\0')
144d4567212SWarner Losh rshprog = _PATH_BSHELL;
145d4567212SWarner Losh else
146d4567212SWarner Losh rshprog = pw->pw_shell;
147d4567212SWarner Losh p = strrchr(rshprog, '/');
148d4567212SWarner Losh execlp(rshprog, p ? p + 1 : rshprog, "-c", cmd,
149d4567212SWarner Losh (char *)NULL);
150d4567212SWarner Losh } else {
151d4567212SWarner Losh p = strrchr(rshprog, '/');
152d4567212SWarner Losh execlp(rshprog, p ? p + 1 : rshprog, *ahost, "-l",
153d4567212SWarner Losh remuser, cmd, (char *)NULL);
154d4567212SWarner Losh }
155d4567212SWarner Losh (void)fprintf(stderr, "rcmdsh: execlp %s failed: %s\n",
156d4567212SWarner Losh rshprog, strerror(errno));
157d4567212SWarner Losh _exit(255);
158d4567212SWarner Losh } else {
159d4567212SWarner Losh /* Parent. close sp[1], return sp[0]. */
16059797edfSJilles Tjoelker (void)_close(sp[1]);
161d4567212SWarner Losh /* Reap child. */
16259797edfSJilles Tjoelker (void)_waitpid(cpid, NULL, 0);
163d4567212SWarner Losh return (sp[0]);
164d4567212SWarner Losh }
165d4567212SWarner Losh /* NOTREACHED */
166d4567212SWarner Losh }
167