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