xref: /csrg-svn/libexec/rlogind/rlogind.c (revision 25423)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 char copyright[] =
9 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
10  All rights reserved.\n";
11 #endif not lint
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)rlogind.c	5.9 (Berkeley) 11/08/85";
15 #endif not lint
16 
17 /*
18  * remote login server:
19  *	remuser\0
20  *	locuser\0
21  *	terminal info\0
22  *	data
23  */
24 
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/socket.h>
29 #include <sys/wait.h>
30 #include <sys/file.h>
31 
32 #include <netinet/in.h>
33 
34 #include <errno.h>
35 #include <pwd.h>
36 #include <signal.h>
37 #include <sgtty.h>
38 #include <stdio.h>
39 #include <netdb.h>
40 #include <syslog.h>
41 #include <strings.h>
42 
43 # ifndef TIOCPKT_WINDOW
44 # define TIOCPKT_WINDOW 0x80
45 # endif TIOCPKT_WINDOW
46 
47 extern	errno;
48 int	reapchild();
49 struct	passwd *getpwnam();
50 char	*malloc();
51 
52 main(argc, argv)
53 	int argc;
54 	char **argv;
55 {
56 	int on = 1, options = 0, fromlen;
57 	struct sockaddr_in from;
58 
59 	openlog("rlogind", LOG_PID | LOG_AUTH, LOG_AUTH);
60 	fromlen = sizeof (from);
61 	if (getpeername(0, &from, &fromlen) < 0) {
62 		fprintf(stderr, "%s: ", argv[0]);
63 		perror("getpeername");
64 		_exit(1);
65 	}
66 	if (setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) {
67 		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
68 	}
69 	doit(0, &from);
70 }
71 
72 int	child;
73 int	cleanup();
74 int	netf;
75 extern	errno;
76 char	*line;
77 extern	char	*inet_ntoa();
78 
79 struct winsize win = { 0, 0, 0, 0 };
80 
81 
82 doit(f, fromp)
83 	int f;
84 	struct sockaddr_in *fromp;
85 {
86 	int i, p, t, pid, on = 1;
87 	register struct hostent *hp;
88 	struct hostent hostent;
89 	char c;
90 
91 	alarm(60);
92 	read(f, &c, 1);
93 	if (c != 0)
94 		exit(1);
95 	alarm(0);
96 	fromp->sin_port = ntohs((u_short)fromp->sin_port);
97 	hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr),
98 		fromp->sin_family);
99 	if (hp == 0) {
100 		/*
101 		 * Only the name is used below.
102 		 */
103 		hp = &hostent;
104 		hp->h_name = inet_ntoa(fromp->sin_addr);
105 	}
106 	if (fromp->sin_family != AF_INET ||
107 	    fromp->sin_port >= IPPORT_RESERVED)
108 		fatal(f, "Permission denied");
109 	write(f, "", 1);
110 	for (c = 'p'; c <= 's'; c++) {
111 		struct stat stb;
112 		line = "/dev/ptyXX";
113 		line[strlen("/dev/pty")] = c;
114 		line[strlen("/dev/ptyp")] = '0';
115 		if (stat(line, &stb) < 0)
116 			break;
117 		for (i = 0; i < 16; i++) {
118 			line[strlen("/dev/ptyp")] = "0123456789abcdef"[i];
119 			p = open(line, 2);
120 			if (p > 0)
121 				goto gotpty;
122 		}
123 	}
124 	fatal(f, "Out of ptys");
125 	/*NOTREACHED*/
126 gotpty:
127 	(void) ioctl(p, TIOCSWINSZ, &win);
128 	netf = f;
129 	line[strlen("/dev/")] = 't';
130 #ifdef DEBUG
131 	{ int tt = open("/dev/tty", 2);
132 	  if (tt > 0) {
133 		ioctl(tt, TIOCNOTTY, 0);
134 		close(tt);
135 	  }
136 	}
137 #endif
138 	t = open(line, 2);
139 	if (t < 0)
140 		fatalperror(f, line, errno);
141 	{ struct sgttyb b;
142 	  gtty(t, &b); b.sg_flags = RAW|ANYP; stty(t, &b);
143 	}
144 	pid = fork();
145 	if (pid < 0)
146 		fatalperror(f, "", errno);
147 	if (pid == 0) {
148 		close(f), close(p);
149 		dup2(t, 0), dup2(t, 1), dup2(t, 2);
150 		close(t);
151 		execl("/bin/login", "login", "-r", hp->h_name, 0);
152 		fatalperror(2, "/bin/login", errno);
153 		/*NOTREACHED*/
154 	}
155 	close(t);
156 	ioctl(f, FIONBIO, &on);
157 	ioctl(p, FIONBIO, &on);
158 	ioctl(p, TIOCPKT, &on);
159 	signal(SIGTSTP, SIG_IGN);
160 	signal(SIGCHLD, cleanup);
161 	setpgrp(0, 0);
162 	protocol(f, p);
163 	cleanup();
164 }
165 
166 char	magic[2] = { 0377, 0377 };
167 char	oobdata[] = {TIOCPKT_WINDOW};
168 
169 /*
170  * Handle a "control" request (signaled by magic being present)
171  * in the data stream.  For now, we are only willing to handle
172  * window size changes.
173  */
174 control(pty, cp, n)
175 	int pty;
176 	char *cp;
177 	int n;
178 {
179 	struct winsize *wp;
180 
181 	if (n < 4+sizeof (*wp) || cp[2] != 's' || cp[3] != 's')
182 		return (0);
183 	oobdata[0] &= ~TIOCPKT_WINDOW;	/* we know he heard */
184 	wp = (struct winsize *)(cp+4);
185 	wp->ws_row = ntohs(wp->ws_row);
186 	wp->ws_col = ntohs(wp->ws_col);
187 	wp->ws_xpixel = ntohs(wp->ws_xpixel);
188 	wp->ws_ypixel = ntohs(wp->ws_ypixel);
189 	(void)ioctl(pty, TIOCSWINSZ, wp);
190 	return (4+sizeof (*wp));
191 }
192 
193 /*
194  * rlogin "protocol" machine.
195  */
196 protocol(f, p)
197 	int f, p;
198 {
199 	char pibuf[1024], fibuf[1024], *pbp, *fbp;
200 	register pcc = 0, fcc = 0;
201 	int cc;
202 
203 	/*
204 	 * Must ignore SIGTTOU, otherwise we'll stop
205 	 * when we try and set slave pty's window shape
206 	 * (our controlling tty is the master pty).
207 	 */
208 	(void) signal(SIGTTOU, SIG_IGN);
209 	send(f, oobdata, 1, MSG_OOB);	/* indicate new rlogin */
210 	for (;;) {
211 		int ibits = 0, obits = 0;
212 
213 		if (fcc)
214 			obits |= (1<<p);
215 		else
216 			ibits |= (1<<f);
217 		if (pcc >= 0)
218 			if (pcc)
219 				obits |= (1<<f);
220 			else
221 				ibits |= (1<<p);
222 		if (select(16, &ibits, &obits, 0, 0) < 0) {
223 			if (errno == EINTR)
224 				continue;
225 			fatalperror(f, "select", errno);
226 		}
227 		if (ibits == 0 && obits == 0) {
228 			/* shouldn't happen... */
229 			sleep(5);
230 			continue;
231 		}
232 		if (ibits & (1<<f)) {
233 			fcc = read(f, fibuf, sizeof (fibuf));
234 			if (fcc < 0 && errno == EWOULDBLOCK)
235 				fcc = 0;
236 			else {
237 				register char *cp;
238 				int left, n;
239 
240 				if (fcc <= 0)
241 					break;
242 				fbp = fibuf;
243 
244 			top:
245 				for (cp = fibuf; cp < fibuf+fcc-1; cp++)
246 					if (cp[0] == magic[0] &&
247 					    cp[1] == magic[1]) {
248 						left = fcc - (cp-fibuf);
249 						n = control(p, cp, left);
250 						if (n) {
251 							left -= n;
252 							if (left > 0)
253 								bcopy(cp+n, cp, left);
254 							fcc -= n;
255 							goto top; /* n^2 */
256 						}
257 					}
258 			}
259 		}
260 
261 		if ((obits & (1<<p)) && fcc > 0) {
262 			cc = write(p, fbp, fcc);
263 			if (cc > 0) {
264 				fcc -= cc;
265 				fbp += cc;
266 			}
267 		}
268 
269 		if (ibits & (1<<p)) {
270 			pcc = read(p, pibuf, sizeof (pibuf));
271 			pbp = pibuf;
272 			if (pcc < 0 && errno == EWOULDBLOCK)
273 				pcc = 0;
274 			else if (pcc <= 0)
275 				break;
276 			else if (pibuf[0] == 0)
277 				pbp++, pcc--;
278 			else {
279 #define	pkcontrol(c)	((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
280 				if (pkcontrol(pibuf[0])) {
281 					pibuf[0] |= oobdata[0];
282 					send(f, &pibuf[0], 1, MSG_OOB);
283 				}
284 				pcc = 0;
285 			}
286 		}
287 		if ((obits & (1<<f)) && pcc > 0) {
288 			cc = write(f, pbp, pcc);
289 			if (cc < 0 && errno == EWOULDBLOCK) {
290 				/* also shouldn't happen */
291 				sleep(5);
292 				continue;
293 			}
294 			if (cc > 0) {
295 				pcc -= cc;
296 				pbp += cc;
297 			}
298 		}
299 	}
300 }
301 
302 cleanup()
303 {
304 
305 	rmut();
306 	vhangup();		/* XXX */
307 	shutdown(netf, 2);
308 	exit(1);
309 }
310 
311 fatal(f, msg)
312 	int f;
313 	char *msg;
314 {
315 	char buf[BUFSIZ];
316 
317 	buf[0] = '\01';		/* error indicator */
318 	(void) sprintf(buf + 1, "rlogind: %s.\r\n", msg);
319 	(void) write(f, buf, strlen(buf));
320 	exit(1);
321 }
322 
323 fatalperror(f, msg, errno)
324 	int f;
325 	char *msg;
326 	int errno;
327 {
328 	char buf[BUFSIZ];
329 	extern int sys_nerr;
330 	extern char *sys_errlist[];
331 
332 	if ((unsigned)errno < sys_nerr)
333 		(void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]);
334 	else
335 		(void) sprintf(buf, "%s: Error %d", msg, errno);
336 	fatal(f, buf);
337 }
338 
339 #include <utmp.h>
340 
341 struct	utmp wtmp;
342 char	wtmpf[]	= "/usr/adm/wtmp";
343 char	utmpf[] = "/etc/utmp";
344 #define SCPYN(a, b)	strncpy(a, b, sizeof(a))
345 #define SCMPN(a, b)	strncmp(a, b, sizeof(a))
346 
347 rmut()
348 {
349 	register f;
350 	int found = 0;
351 	struct utmp *u, *utmp;
352 	int nutmp;
353 	struct stat statbf;
354 
355 	f = open(utmpf, O_RDWR);
356 	if (f >= 0) {
357 		fstat(f, &statbf);
358 		utmp = (struct utmp *)malloc(statbf.st_size);
359 		if (!utmp)
360 			syslog(LOG_ERR, "utmp malloc failed");
361 		if (statbf.st_size && utmp) {
362 			nutmp = read(f, utmp, statbf.st_size);
363 			nutmp /= sizeof(struct utmp);
364 
365 			for (u = utmp ; u < &utmp[nutmp] ; u++) {
366 				if (SCMPN(u->ut_line, line+5) ||
367 				    u->ut_name[0]==0)
368 					continue;
369 				lseek(f, ((long)u)-((long)utmp), L_SET);
370 				SCPYN(u->ut_name, "");
371 				SCPYN(u->ut_host, "");
372 				time(&u->ut_time);
373 				write(f, (char *)u, sizeof(wtmp));
374 				found++;
375 			}
376 		}
377 		close(f);
378 	}
379 	if (found) {
380 		f = open(wtmpf, O_WRONLY|O_APPEND);
381 		if (f >= 0) {
382 			SCPYN(wtmp.ut_line, line+5);
383 			SCPYN(wtmp.ut_name, "");
384 			SCPYN(wtmp.ut_host, "");
385 			time(&wtmp.ut_time);
386 			write(f, (char *)&wtmp, sizeof(wtmp));
387 			close(f);
388 		}
389 	}
390 	chmod(line, 0666);
391 	chown(line, 0, 0);
392 	line[strlen("/dev/")] = 'p';
393 	chmod(line, 0666);
394 	chown(line, 0, 0);
395 }
396