1*8823d24bSXin LI /* $OpenBSD: rcmdsh.c,v 1.7 2002/03/12 00:05:44 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> 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> 51d4567212SWarner Losh 52d4567212SWarner Losh /* 53d4567212SWarner Losh * This is a replacement rcmd() function that uses the rsh(1) 54d4567212SWarner Losh * program in place of a direct rcmd(3) function call so as to 55d4567212SWarner Losh * avoid having to be root. Note that rport is ignored. 56d4567212SWarner Losh */ 57d4567212SWarner Losh int 58*8823d24bSXin LI rcmdsh(char **ahost, int rport, const char *locuser, const char *remuser, 59*8823d24bSXin LI const char *cmd, const char *rshprog) 60d4567212SWarner Losh { 617b0e82a9SHajimu UMEMOTO struct addrinfo hints, *res; 62*8823d24bSXin LI int sp[2], error; 63*8823d24bSXin LI pid_t cpid; 64d4567212SWarner Losh char *p; 65d4567212SWarner Losh struct passwd *pw; 667b0e82a9SHajimu UMEMOTO char num[8]; 677b0e82a9SHajimu UMEMOTO static char hbuf[NI_MAXHOST]; 68d4567212SWarner Losh 69d4567212SWarner Losh /* What rsh/shell to use. */ 70d4567212SWarner Losh if (rshprog == NULL) 71d4567212SWarner Losh rshprog = _PATH_RSH; 72d4567212SWarner Losh 73d4567212SWarner Losh /* locuser must exist on this host. */ 74d4567212SWarner Losh if ((pw = getpwnam(locuser)) == NULL) { 75d4567212SWarner Losh (void)fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser); 76d4567212SWarner Losh return (-1); 77d4567212SWarner Losh } 78d4567212SWarner Losh 79d4567212SWarner Losh /* Validate remote hostname. */ 80d4567212SWarner Losh if (strcmp(*ahost, "localhost") != 0) { 817b0e82a9SHajimu UMEMOTO memset(&hints, 0, sizeof(hints)); 827b0e82a9SHajimu UMEMOTO hints.ai_flags = AI_CANONNAME; 837b0e82a9SHajimu UMEMOTO hints.ai_family = PF_UNSPEC; 847b0e82a9SHajimu UMEMOTO hints.ai_socktype = SOCK_STREAM; 852bbd7cf8SJacques Vidrine (void)snprintf(num, sizeof(num), "%u", 862bbd7cf8SJacques Vidrine (unsigned int)ntohs(rport)); 877b0e82a9SHajimu UMEMOTO error = getaddrinfo(*ahost, num, &hints, &res); 887b0e82a9SHajimu UMEMOTO if (error) { 897b0e82a9SHajimu UMEMOTO fprintf(stderr, "rcmdsh: getaddrinfo: %s\n", 907b0e82a9SHajimu UMEMOTO gai_strerror(error)); 91d4567212SWarner Losh return (-1); 92d4567212SWarner Losh } 937b0e82a9SHajimu UMEMOTO if (res->ai_canonname) { 947b0e82a9SHajimu UMEMOTO strncpy(hbuf, res->ai_canonname, sizeof(hbuf) - 1); 957b0e82a9SHajimu UMEMOTO hbuf[sizeof(hbuf) - 1] = '\0'; 967b0e82a9SHajimu UMEMOTO *ahost = hbuf; 977b0e82a9SHajimu UMEMOTO } 987b0e82a9SHajimu UMEMOTO freeaddrinfo(res); 99d4567212SWarner Losh } 100d4567212SWarner Losh 101d4567212SWarner Losh /* Get a socketpair we'll use for stdin and stdout. */ 102e117e7a5SRuslan Ermilov if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) { 103d4567212SWarner Losh perror("rcmdsh: socketpair"); 104d4567212SWarner Losh return (-1); 105d4567212SWarner Losh } 106d4567212SWarner Losh 107d4567212SWarner Losh cpid = fork(); 108e117e7a5SRuslan Ermilov if (cpid == -1) { 109d4567212SWarner Losh perror("rcmdsh: fork failed"); 110d4567212SWarner Losh return (-1); 111d4567212SWarner Losh } else if (cpid == 0) { 112d4567212SWarner Losh /* 113d4567212SWarner Losh * Child. We use sp[1] to be stdin/stdout, and close sp[0]. 114d4567212SWarner Losh */ 115d4567212SWarner Losh (void)close(sp[0]); 116e117e7a5SRuslan Ermilov if (dup2(sp[1], 0) == -1 || dup2(0, 1) == -1) { 117d4567212SWarner Losh perror("rcmdsh: dup2 failed"); 118d4567212SWarner Losh _exit(255); 119d4567212SWarner Losh } 120d4567212SWarner Losh /* Fork again to lose parent. */ 121d4567212SWarner Losh cpid = fork(); 122e117e7a5SRuslan Ermilov if (cpid == -1) { 123d4567212SWarner Losh perror("rcmdsh: fork to lose parent failed"); 124d4567212SWarner Losh _exit(255); 125d4567212SWarner Losh } 126d4567212SWarner Losh if (cpid > 0) 127d4567212SWarner Losh _exit(0); 128d4567212SWarner Losh 129d4567212SWarner Losh /* In grandchild here. Become local user for rshprog. */ 130e117e7a5SRuslan Ermilov if (setuid(pw->pw_uid) == -1) { 131d4567212SWarner Losh (void)fprintf(stderr, "rcmdsh: setuid(%u): %s\n", 132d4567212SWarner Losh pw->pw_uid, strerror(errno)); 133d4567212SWarner Losh _exit(255); 134d4567212SWarner Losh } 135d4567212SWarner Losh 136d4567212SWarner Losh /* 137e117e7a5SRuslan Ermilov * If remote host is "localhost" and local and remote users 138d4567212SWarner Losh * are the same, avoid running remote shell for efficiency. 139d4567212SWarner Losh */ 140e117e7a5SRuslan Ermilov if (strcmp(*ahost, "localhost") == 0 && 141e117e7a5SRuslan Ermilov strcmp(locuser, remuser) == 0) { 142d4567212SWarner Losh if (pw->pw_shell[0] == '\0') 143d4567212SWarner Losh rshprog = _PATH_BSHELL; 144d4567212SWarner Losh else 145d4567212SWarner Losh rshprog = pw->pw_shell; 146d4567212SWarner Losh p = strrchr(rshprog, '/'); 147d4567212SWarner Losh execlp(rshprog, p ? p + 1 : rshprog, "-c", cmd, 148d4567212SWarner Losh (char *)NULL); 149d4567212SWarner Losh } else { 150d4567212SWarner Losh p = strrchr(rshprog, '/'); 151d4567212SWarner Losh execlp(rshprog, p ? p + 1 : rshprog, *ahost, "-l", 152d4567212SWarner Losh remuser, cmd, (char *)NULL); 153d4567212SWarner Losh } 154d4567212SWarner Losh (void)fprintf(stderr, "rcmdsh: execlp %s failed: %s\n", 155d4567212SWarner Losh rshprog, strerror(errno)); 156d4567212SWarner Losh _exit(255); 157d4567212SWarner Losh } else { 158d4567212SWarner Losh /* Parent. close sp[1], return sp[0]. */ 159d4567212SWarner Losh (void)close(sp[1]); 160d4567212SWarner Losh /* Reap child. */ 161d4567212SWarner Losh (void)wait(NULL); 162d4567212SWarner Losh return (sp[0]); 163d4567212SWarner Losh } 164d4567212SWarner Losh /* NOTREACHED */ 165d4567212SWarner Losh } 166