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