xref: /csrg-svn/libexec/rshd/rshd.c (revision 8380)
1 #ifndef lint
2 static char sccsid[] = "@(#)rshd.c	4.2 82/10/07";
3 #endif
4 
5 #include <stdio.h>
6 #include <sys/ioctl.h>
7 #include <sys/param.h>
8 #include <sys/socket.h>
9 #include <net/in.h>
10 #include <errno.h>
11 #include <pwd.h>
12 #include <wait.h>
13 #include <signal.h>
14 #include <netdb.h>
15 
16 int	errno;
17 struct	sockaddr_in sin = { AF_INET };
18 struct	passwd *getpwnam();
19 char	*index(), *rindex(), *sprintf();
20 int	options = SO_ACCEPTCONN|SO_KEEPALIVE;
21 /* VARARGS 1 */
22 int	error();
23 /*
24  * remote execute server:
25  *	remuser\0
26  *	locuser\0
27  *	command\0
28  *	data
29  */
30 main(argc, argv)
31 	int argc;
32 	char **argv;
33 {
34 	union wait status;
35 	int f;
36 	struct sockaddr_in from;
37 	struct servent *sp;
38 
39 	sp = getservbyname("shell", "tcp");
40 	if (sp == 0) {
41 		fprintf(stderr, "rshd: tcp/shell: unknown service\n");
42 		exit(1);
43 	}
44 #ifndef DEBUG
45 	if (fork())
46 		exit(0);
47 	for (f = 0; f < 10; f++)
48 		(void) close(f);
49 	(void) open("/", 0);
50 	(void) dup2(0, 1);
51 	(void) dup2(0, 2);
52 	{ int t = open("/dev/tty", 2);
53 	  if (t >= 0) {
54 		ioctl(t, TIOCNOTTY, (char *)0);
55 		(void) close(t);
56 	  }
57 	}
58 #endif
59 	sin.sin_port = htons(sp->s_port);
60 	argc--, argv++;
61 	if (argc > 0 && !strcmp(argv[0], "-d"))
62 		options |= SO_DEBUG, argc--, argv++;
63 	if (argc > 0) {
64 		int port = atoi(argv[0]);
65 
66 		if (port < 0) {
67 			fprintf(stderr, "%s: bad port #\n", argv[0]);
68 			exit(1);
69 		}
70 		sin.sin_port = htons(port);
71 		argc--, argv++;
72 	}
73 	for (;;) {
74 		errno = 0;
75 		f = socket(SOCK_STREAM, 0, &sin, options);
76 		if (f < 0) {
77 			perror("socket");
78 			sleep(5);
79 			continue;
80 		}
81 		if (accept(f, &from) < 0) {
82 			perror("accept");
83 			(void) close(f);
84 			sleep(1);
85 			continue;
86 		}
87 		if (fork() == 0)
88 			doit(f, &from);
89 		(void) close(f);
90 		while(wait3(status, WNOHANG, 0) > 0)
91 			continue;
92 	}
93 }
94 
95 char	username[20] = "USER=";
96 char	homedir[64] = "HOME=";
97 char	shell[64] = "SHELL=";
98 char	*envinit[] =
99 	    {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", username, 0};
100 char	**environ;
101 
102 doit(f, fromp)
103 	int f;
104 	struct sockaddr_in *fromp;
105 {
106 	char cmdbuf[NCARGS+1], *cp;
107 	char locuser[16], remuser[16];
108 	struct passwd *pwd;
109 	int s;
110 	struct hostent *hp;
111 	short port;
112 	int pv[2], pid, ready, readfrom, cc;
113 	char buf[BUFSIZ], sig;
114 	int one = 1;
115 
116 	(void) signal(SIGINT, SIG_DFL);
117 	(void) signal(SIGQUIT, SIG_DFL);
118 	(void) signal(SIGTERM, SIG_DFL);
119 #ifdef DEBUG
120 	{ int t = open("/dev/tty", 2);
121 	  if (t >= 0) {
122 		ioctl(t, TIOCNOTTY, (char *)0);
123 		(void) close(t);
124 	  }
125 	}
126 #endif
127 	dup2(f, 0);
128 	dup2(f, 1);
129 	dup2(f, 2);
130 	fromp->sin_port = ntohs((u_short)fromp->sin_port);
131 	if (fromp->sin_family != AF_INET ||
132 	    fromp->sin_port >= IPPORT_RESERVED)
133 		exit(1);
134 	(void) alarm(60);
135 	port = 0;
136 	for (;;) {
137 		char c;
138 		if (read(f, &c, 1) != 1)
139 			exit(1);
140 		if (c == 0)
141 			break;
142 		port = port * 10 + c - '0';
143 	}
144 	(void) alarm(0);
145 	if (port != 0) {
146 		s = rresvport(0);
147 		if (s < 0)
148 			exit(1);
149 		if (port >= IPPORT_RESERVED)
150 			goto protofail;
151 		(void) alarm(60);
152 		fromp->sin_port = ntohs(port);
153 		if (connect(s, fromp) < 0)
154 			exit(1);
155 		(void) alarm(0);
156 	}
157 	hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr),
158 		fromp->sin_family);
159 	if (hp == 0) {
160 		error("Host name for your address unknown\n");
161 		exit(1);
162 	}
163 	getstr(remuser, sizeof(remuser), "remuser");
164 	getstr(locuser, sizeof(locuser), "locuser");
165 	getstr(cmdbuf, sizeof(cmdbuf), "command");
166 	setpwent();
167 	pwd = getpwnam(locuser);
168 	if (pwd == NULL) {
169 		error("Login incorrect.\n");
170 		exit(1);
171 	}
172 	endpwent();
173 	if (chdir(pwd->pw_dir) < 0) {
174 		error("No remote directory.\n");
175 		exit(1);
176 	}
177 	if (ruserok(hp->h_name, remuser, locuser) < 0) {
178 		error("Permission denied.\n");
179 		exit(1);
180 	}
181 	(void) write(2, "\0", 1);
182 	if (port) {
183 		if (pipe(pv) < 0) {
184 			error("Can't make pipe.\n");
185 			exit(1);
186 		}
187 		pid = fork();
188 		if (pid == -1)  {
189 			error("Try again.\n");
190 			exit(1);
191 		}
192 		if (pid) {
193 			(void) close(0); (void) close(1); (void) close(2);
194 			(void) close(f); (void) close(pv[1]);
195 			readfrom = (1<<s) | (1<<pv[0]);
196 			ioctl(pv[1], FIONBIO, (char *)&one);
197 			/* should set s nbio! */
198 			do {
199 				ready = readfrom;
200 				(void) select(32, &ready, 0, 1000000);
201 				if (ready & (1<<s)) {
202 					if (read(s, &sig, 1) <= 0)
203 						readfrom &= ~(1<<s);
204 					else
205 						killpg(pid, sig);
206 				}
207 				if (ready & (1<<pv[0])) {
208 					cc = read(pv[0], buf, sizeof (buf));
209 					if (cc <= 0) {
210 						int done = 1+1;
211 						ioctl(s, SIOCDONE, (char *)&done);
212 						readfrom &= ~(1<<pv[0]);
213 					} else
214 						(void) write(s, buf, cc);
215 				}
216 			} while (readfrom);
217 			exit(0);
218 		}
219 		setpgrp(0, getpid());
220 		(void) close(s); (void) close(pv[0]);
221 		dup2(pv[1], 2);
222 	}
223 	if (*pwd->pw_shell == '\0')
224 		pwd->pw_shell = "/bin/sh";
225 	(void) close(f);
226 	inigrp(pwd->pw_name, pwd->pw_gid);
227 	(void) setuid(pwd->pw_uid);
228 	(void) setgid(pwd->pw_gid);
229 	environ = envinit;
230 	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
231 	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
232 	strncat(username, pwd->pw_name, sizeof(username)-6);
233 	cp = rindex(pwd->pw_shell, '/');
234 	if (cp)
235 		cp++;
236 	else
237 		cp = pwd->pw_shell;
238 	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
239 	perror(pwd->pw_shell);
240 	exit(1);
241 protofail:
242 	error("rsh: protocol failure detected by remote\n");
243 	exit(1);
244 }
245 
246 /* VARARGS 1 */
247 error(fmt)
248 	char *fmt;
249 {
250 	char buf[BUFSIZ];
251 
252 	buf[0] = 1;
253 	(void) sprintf(buf+1, fmt);
254 	(void) write(2, buf, strlen(buf));
255 }
256 
257 getstr(buf, cnt, err)
258 	char *buf;
259 	int cnt;
260 	char *err;
261 {
262 	char c;
263 
264 	do {
265 		if (read(0, &c, 1) != 1)
266 			exit(1);
267 		*buf++ = c;
268 		if (--cnt == 0) {
269 			error("%s too long\n", err);
270 			exit(1);
271 		}
272 	} while (c != 0);
273 }
274