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.c153
1 files changed, 153 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..a1c7e7d6b8
--- /dev/null
+++ b/src/lib/libc/net/rcmdsh.c
@@ -0,0 +1,153 @@
1/* $OpenBSD: rcmdsh.c,v 1.7 2002/03/12 00:05:44 millert 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.7 2002/03/12 00:05:44 millert 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 <string.h>
48#include <pwd.h>
49#include <paths.h>
50#include <unistd.h>
51
52/*
53 * This is a replacement rcmd() function that uses the rsh(1)
54 * program in place of a direct rcmd(3) function call so as to
55 * avoid having to be root. Note that rport is ignored.
56 */
57/* ARGSUSED */
58int
59rcmdsh(ahost, rport, locuser, remuser, cmd, rshprog)
60 char **ahost;
61 int rport;
62 const char *locuser, *remuser, *cmd;
63 char *rshprog;
64{
65 struct hostent *hp;
66 int sp[2];
67 pid_t cpid;
68 char *p;
69 struct passwd *pw;
70
71 /* What rsh/shell to use. */
72 if (rshprog == NULL)
73 rshprog = _PATH_RSH;
74
75 /* locuser must exist on this host. */
76 if ((pw = getpwnam(locuser)) == NULL) {
77 (void) fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser);
78 return(-1);
79 }
80
81 /* Validate remote hostname. */
82 if (strcmp(*ahost, "localhost") != 0) {
83 if ((hp = gethostbyname(*ahost)) == NULL) {
84 herror(*ahost);
85 return(-1);
86 }
87 *ahost = hp->h_name;
88 }
89
90 /* Get a socketpair we'll use for stdin and stdout. */
91 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) < 0) {
92 perror("rcmdsh: socketpair");
93 return(-1);
94 }
95
96 cpid = fork();
97 if (cpid < 0) {
98 perror("rcmdsh: fork failed");
99 return(-1);
100 } else if (cpid == 0) {
101 /*
102 * Child. We use sp[1] to be stdin/stdout, and close sp[0].
103 */
104 (void) close(sp[0]);
105 if (dup2(sp[1], 0) < 0 || dup2(0, 1) < 0) {
106 perror("rcmdsh: dup2 failed");
107 _exit(255);
108 }
109 /* Fork again to lose parent. */
110 cpid = fork();
111 if (cpid < 0) {
112 perror("rcmdsh: fork to lose parent failed");
113 _exit(255);
114 }
115 if (cpid > 0)
116 _exit(0);
117
118 /* In grandchild here. Become local user for rshprog. */
119 if (setuid(pw->pw_uid)) {
120 (void) fprintf(stderr, "rcmdsh: setuid(%u): %s\n",
121 pw->pw_uid, strerror(errno));
122 _exit(255);
123 }
124
125 /*
126 * If remote host is "localhost" and local and remote user
127 * are the same, avoid running remote shell for efficiency.
128 */
129 if (!strcmp(*ahost, "localhost") && !strcmp(locuser, remuser)) {
130 if (pw->pw_shell[0] == '\0')
131 rshprog = _PATH_BSHELL;
132 else
133 rshprog = pw->pw_shell;
134 p = strrchr(rshprog, '/');
135 execlp(rshprog, p ? p+1 : rshprog, "-c", cmd,
136 (char *) NULL);
137 } else {
138 p = strrchr(rshprog, '/');
139 execlp(rshprog, p ? p+1 : rshprog, *ahost, "-l",
140 remuser, cmd, (char *) NULL);
141 }
142 (void) fprintf(stderr, "rcmdsh: execlp %s failed: %s\n",
143 rshprog, strerror(errno));
144 _exit(255);
145 } else {
146 /* Parent. close sp[1], return sp[0]. */
147 (void) close(sp[1]);
148 /* Reap child. */
149 (void) wait(NULL);
150 return(sp[0]);
151 }
152 /* NOTREACHED */
153}