summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/rcmdsh.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/rcmdsh.c')
-rw-r--r--src/lib/libc/net/rcmdsh.c193
1 files changed, 193 insertions, 0 deletions
diff --git a/src/lib/libc/net/rcmdsh.c b/src/lib/libc/net/rcmdsh.c
new file mode 100644
index 0000000000..3ec97e4763
--- /dev/null
+++ b/src/lib/libc/net/rcmdsh.c
@@ -0,0 +1,193 @@
1/* $OpenBSD: rcmdsh.c,v 1.9 2004/04/01 04:14:29 marc Exp $ */
2
3/*
4 * Copyright (c) 2001, MagniComp
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the MagniComp nor the names of its contributors may
16 * be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/*
32 * This is an rcmd() replacement originally by
33 * Chris Siebenmann <cks@utcc.utoronto.ca>.
34 */
35
36#if defined(LIBC_SCCS) && !defined(lint)
37static char *rcsid = "$OpenBSD: rcmdsh.c,v 1.9 2004/04/01 04:14:29 marc Exp $";
38#endif /* LIBC_SCCS and not lint */
39
40#include <sys/types.h>
41#include <sys/socket.h>
42#include <sys/wait.h>
43#include <signal.h>
44#include <errno.h>
45#include <netdb.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <pwd.h>
50#include <paths.h>
51#include <unistd.h>
52
53/*
54 * This is a replacement rcmd() function that uses the rsh(1)
55 * program in place of a direct rcmd(3) function call so as to
56 * avoid having to be root. Note that rport is ignored.
57 */
58/* ARGSUSED */
59int
60rcmdsh(ahost, rport, locuser, remuser, cmd, rshprog)
61 char **ahost;
62 int rport;
63 const char *locuser, *remuser, *cmd;
64 char *rshprog;
65{
66 struct hostent *hp;
67 int sp[2];
68 pid_t cpid;
69 char *p;
70 struct passwd *pw;
71
72 /* What rsh/shell to use. */
73 if (rshprog == NULL)
74 rshprog = _PATH_RSH;
75
76 /* locuser must exist on this host. */
77 if ((pw = getpwnam(locuser)) == NULL) {
78 (void) fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser);
79 return(-1);
80 }
81
82 /* Validate remote hostname. */
83 if (strcmp(*ahost, "localhost") != 0) {
84 if (((hp = gethostbyname2(*ahost, AF_INET)) == NULL) &&
85 ((hp = gethostbyname2(*ahost, AF_INET6)) == NULL)) {
86 herror(*ahost);
87 return(-1);
88 }
89 *ahost = hp->h_name;
90 }
91
92 /* Get a socketpair we'll use for stdin and stdout. */
93 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) < 0) {
94 perror("rcmdsh: socketpair");
95 return(-1);
96 }
97
98 cpid = fork();
99 if (cpid < 0) {
100 perror("rcmdsh: fork failed");
101 return(-1);
102 } else if (cpid == 0) {
103 /*
104 * Child. We use sp[1] to be stdin/stdout, and close sp[0].
105 */
106 (void) close(sp[0]);
107 if (dup2(sp[1], 0) < 0 || dup2(0, 1) < 0) {
108 perror("rcmdsh: dup2 failed");
109 _exit(255);
110 }
111 /* Fork again to lose parent. */
112 cpid = fork();
113 if (cpid < 0) {
114 perror("rcmdsh: fork to lose parent failed");
115 _exit(255);
116 }
117 if (cpid > 0)
118 _exit(0);
119
120 /* In grandchild here. Become local user for rshprog. */
121 if (setuid(pw->pw_uid)) {
122 (void) fprintf(stderr, "rcmdsh: setuid(%u): %s\n",
123 pw->pw_uid, strerror(errno));
124 _exit(255);
125 }
126
127 /*
128 * If remote host is "localhost" and local and remote user
129 * are the same, avoid running remote shell for efficiency.
130 */
131 if (!strcmp(*ahost, "localhost") && !strcmp(locuser, remuser)) {
132 char *argv[4];
133 if (pw->pw_shell[0] == '\0')
134 rshprog = _PATH_BSHELL;
135 else
136 rshprog = pw->pw_shell;
137 p = strrchr(rshprog, '/');
138 argv[0] = p ? p + 1 : rshprog;
139 argv[1] = "-c";
140 argv[2] = (char *)cmd;
141 argv[3] = NULL;
142 execvp(rshprog, argv);
143 } else if ((p = strchr(rshprog, ' ')) == NULL) {
144 /* simple case */
145 char *argv[6];
146 p = strrchr(rshprog, '/');
147 argv[0] = p ? p + 1 : rshprog;
148 argv[1] = "-l";
149 argv[2] = (char *)remuser;
150 argv[3] = *ahost;
151 argv[4] = (char *)cmd;
152 argv[5] = NULL;
153 execvp(rshprog, argv);
154 } else {
155 /* must pull args out of rshprog and dyn alloc argv */
156 char **argv, **ap;
157 int n;
158 for (n = 7; (p = strchr(++p, ' ')) != NULL; n++)
159 continue;
160 rshprog = strdup(rshprog);
161 ap = argv = malloc(sizeof(char *) * n);
162 if (rshprog == NULL || argv == NULL) {
163 perror("rcmdsh");
164 _exit(255);
165 }
166 while ((p = strsep(&rshprog, " ")) != NULL) {
167 if (*p == '\0')
168 continue;
169 *ap++ = p;
170 }
171 if (ap != argv) /* all spaces?!? */
172 rshprog = argv[0];
173 if ((p = strrchr(argv[0], '/')) != NULL)
174 argv[0] = p + 1;
175 *ap++ = "-l";
176 *ap++ = (char *)remuser;
177 *ap++ = *ahost;
178 *ap++ = (char *)cmd;
179 *ap++ = NULL;
180 execvp(rshprog, argv);
181 }
182 (void) fprintf(stderr, "rcmdsh: execvp %s failed: %s\n",
183 rshprog, strerror(errno));
184 _exit(255);
185 } else {
186 /* Parent. close sp[1], return sp[0]. */
187 (void) close(sp[1]);
188 /* Reap child. */
189 (void) wait(NULL);
190 return(sp[0]);
191 }
192 /* NOTREACHED */
193}