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