rlogind.c revision 1.9 1 1.1 cgd /*-
2 1.5 cgd * Copyright (c) 1983, 1988, 1989, 1993
3 1.5 cgd * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.5 cgd static char copyright[] =
36 1.5 cgd "@(#) Copyright (c) 1983, 1988, 1989, 1993\n\
37 1.5 cgd The Regents of the University of California. All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.5 cgd /* from: static char sccsid[] = "@(#)rlogind.c 8.1 (Berkeley) 6/4/93"; */
42 1.9 lukem static char *rcsid = "$Id: rlogind.c,v 1.9 1996/11/08 07:47:45 lukem Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd /*
46 1.1 cgd * remote login server:
47 1.1 cgd * \0
48 1.1 cgd * remuser\0
49 1.1 cgd * locuser\0
50 1.1 cgd * terminal_type/speed\0
51 1.1 cgd * data
52 1.1 cgd */
53 1.1 cgd
54 1.1 cgd #define FD_SETSIZE 16 /* don't need many bits for select */
55 1.1 cgd #include <sys/param.h>
56 1.1 cgd #include <sys/stat.h>
57 1.1 cgd #include <sys/ioctl.h>
58 1.1 cgd #include <signal.h>
59 1.1 cgd #include <termios.h>
60 1.1 cgd
61 1.1 cgd #include <sys/socket.h>
62 1.1 cgd #include <netinet/in.h>
63 1.1 cgd #include <netinet/in_systm.h>
64 1.1 cgd #include <netinet/ip.h>
65 1.1 cgd #include <arpa/inet.h>
66 1.1 cgd #include <netdb.h>
67 1.1 cgd
68 1.1 cgd #include <pwd.h>
69 1.1 cgd #include <syslog.h>
70 1.1 cgd #include <errno.h>
71 1.1 cgd #include <stdio.h>
72 1.1 cgd #include <unistd.h>
73 1.1 cgd #include <stdlib.h>
74 1.1 cgd #include <string.h>
75 1.9 lukem #include <util.h>
76 1.8 mrg #include <utmp.h>
77 1.1 cgd #include "pathnames.h"
78 1.1 cgd
79 1.1 cgd #ifndef TIOCPKT_WINDOW
80 1.1 cgd #define TIOCPKT_WINDOW 0x80
81 1.1 cgd #endif
82 1.1 cgd
83 1.9 lukem #define OPTIONS "alnL"
84 1.1 cgd
85 1.1 cgd char *env[2];
86 1.1 cgd #define NMAX 30
87 1.1 cgd char lusername[NMAX+1], rusername[NMAX+1];
88 1.1 cgd static char term[64] = "TERM=";
89 1.1 cgd #define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation */
90 1.1 cgd int keepalive = 1;
91 1.1 cgd int check_all = 0;
92 1.9 lukem int log_success = 0;
93 1.1 cgd
94 1.1 cgd struct passwd *pwd;
95 1.1 cgd
96 1.5 cgd void doit __P((int, struct sockaddr_in *));
97 1.5 cgd int control __P((int, char *, int));
98 1.5 cgd void protocol __P((int, int));
99 1.5 cgd void cleanup __P((int));
100 1.5 cgd void fatal __P((int, char *, int));
101 1.9 lukem int do_rlogin __P((struct sockaddr_in *, char *));
102 1.5 cgd void getstr __P((char *, int, char *));
103 1.5 cgd void setup_term __P((int));
104 1.5 cgd int do_krb_login __P((struct sockaddr_in *));
105 1.5 cgd void usage __P((void));
106 1.5 cgd int local_domain __P((char *));
107 1.5 cgd char *topdomain __P((char *));
108 1.5 cgd
109 1.5 cgd int
110 1.1 cgd main(argc, argv)
111 1.1 cgd int argc;
112 1.5 cgd char *argv[];
113 1.1 cgd {
114 1.3 pk extern int __check_rhosts_file;
115 1.1 cgd struct sockaddr_in from;
116 1.5 cgd int ch, fromlen, on;
117 1.1 cgd
118 1.1 cgd openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH);
119 1.1 cgd
120 1.1 cgd opterr = 0;
121 1.9 lukem while ((ch = getopt(argc, argv, OPTIONS)) != EOF)
122 1.1 cgd switch (ch) {
123 1.1 cgd case 'a':
124 1.1 cgd check_all = 1;
125 1.1 cgd break;
126 1.1 cgd case 'l':
127 1.3 pk __check_rhosts_file = 0;
128 1.1 cgd break;
129 1.1 cgd case 'n':
130 1.1 cgd keepalive = 0;
131 1.1 cgd break;
132 1.9 lukem case 'L':
133 1.9 lukem log_success = 1;
134 1.9 lukem break;
135 1.1 cgd case '?':
136 1.1 cgd default:
137 1.1 cgd usage();
138 1.1 cgd break;
139 1.1 cgd }
140 1.1 cgd argc -= optind;
141 1.1 cgd argv += optind;
142 1.1 cgd
143 1.1 cgd fromlen = sizeof (from);
144 1.1 cgd if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
145 1.1 cgd syslog(LOG_ERR,"Can't get peer name of remote host: %m");
146 1.1 cgd fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
147 1.1 cgd }
148 1.5 cgd on = 1;
149 1.1 cgd if (keepalive &&
150 1.1 cgd setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
151 1.1 cgd syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
152 1.1 cgd on = IPTOS_LOWDELAY;
153 1.1 cgd if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
154 1.1 cgd syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
155 1.1 cgd doit(0, &from);
156 1.9 lukem /* NOTREACHED */
157 1.1 cgd }
158 1.1 cgd
159 1.1 cgd int child;
160 1.1 cgd int netf;
161 1.1 cgd char line[MAXPATHLEN];
162 1.1 cgd int confirmed;
163 1.1 cgd
164 1.1 cgd struct winsize win = { 0, 0, 0, 0 };
165 1.1 cgd
166 1.1 cgd
167 1.5 cgd void
168 1.1 cgd doit(f, fromp)
169 1.1 cgd int f;
170 1.1 cgd struct sockaddr_in *fromp;
171 1.1 cgd {
172 1.5 cgd int master, pid, on = 1;
173 1.5 cgd int authenticated = 0;
174 1.1 cgd register struct hostent *hp;
175 1.9 lukem char utmphost[UT_HOSTSIZE + 1];
176 1.9 lukem char *hostname;
177 1.9 lukem char hostnamebuf[2 * MAXHOSTNAMELEN + 1];
178 1.1 cgd char c;
179 1.1 cgd
180 1.1 cgd alarm(60);
181 1.1 cgd read(f, &c, 1);
182 1.1 cgd
183 1.1 cgd if (c != 0)
184 1.1 cgd exit(1);
185 1.1 cgd
186 1.1 cgd alarm(0);
187 1.1 cgd fromp->sin_port = ntohs((u_short)fromp->sin_port);
188 1.1 cgd hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof(struct in_addr),
189 1.5 cgd fromp->sin_family);
190 1.9 lukem if (hp) {
191 1.9 lukem /*
192 1.9 lukem * If name returned by gethostbyaddr is in our domain,
193 1.9 lukem * attempt to verify that we haven't been fooled by someone
194 1.9 lukem * in a remote net; look up the name and check that this
195 1.9 lukem * address corresponds to the name.
196 1.9 lukem */
197 1.9 lukem hostname = hp->h_name;
198 1.9 lukem if (check_all || local_domain(hp->h_name)) {
199 1.9 lukem strncpy(hostnamebuf, hp->h_name,
200 1.9 lukem sizeof(hostnamebuf) - 1);
201 1.9 lukem hostnamebuf[sizeof(hostnamebuf) - 1] = 0;
202 1.9 lukem hp = gethostbyname(hostnamebuf);
203 1.9 lukem if (hp == NULL) {
204 1.9 lukem syslog(LOG_INFO,
205 1.9 lukem "Couldn't look up address for %s",
206 1.9 lukem hostnamebuf);
207 1.9 lukem hostname = inet_ntoa(fromp->sin_addr);
208 1.9 lukem } else for (; ; hp->h_addr_list++) {
209 1.9 lukem if (hp->h_addr_list[0] == NULL) {
210 1.9 lukem syslog(LOG_NOTICE,
211 1.9 lukem "Host addr %s not listed for host %s",
212 1.9 lukem inet_ntoa(fromp->sin_addr),
213 1.9 lukem hp->h_name);
214 1.9 lukem hostname = inet_ntoa(fromp->sin_addr);
215 1.9 lukem break;
216 1.9 lukem }
217 1.9 lukem if (!bcmp(hp->h_addr_list[0],
218 1.9 lukem (caddr_t)&fromp->sin_addr,
219 1.9 lukem sizeof(fromp->sin_addr))) {
220 1.9 lukem hostname = hp->h_name;
221 1.9 lukem break;
222 1.9 lukem }
223 1.9 lukem }
224 1.9 lukem }
225 1.9 lukem hostname = strncpy(hostnamebuf, hostname,
226 1.9 lukem sizeof(hostnamebuf) - 1);
227 1.9 lukem } else
228 1.9 lukem hostname = strncpy(hostnamebuf, inet_ntoa(fromp->sin_addr),
229 1.9 lukem sizeof(hostnamebuf) - 1);
230 1.9 lukem
231 1.9 lukem hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
232 1.9 lukem
233 1.9 lukem if (strlen(hostname) < sizeof(utmphost))
234 1.9 lukem (void)strcpy(utmphost, hostname);
235 1.5 cgd else
236 1.9 lukem (void)strncpy(utmphost, inet_ntoa(fromp->sin_addr),
237 1.9 lukem sizeof(utmphost));
238 1.9 lukem utmphost[sizeof(utmphost) - 1] = '\0';
239 1.9 lukem
240 1.9 lukem if (fromp->sin_family != AF_INET ||
241 1.9 lukem fromp->sin_port >= IPPORT_RESERVED ||
242 1.9 lukem fromp->sin_port < IPPORT_RESERVED/2) {
243 1.9 lukem syslog(LOG_NOTICE, "Connection from %s on illegal port",
244 1.9 lukem inet_ntoa(fromp->sin_addr));
245 1.9 lukem fatal(f, "Permission denied", 0);
246 1.9 lukem }
247 1.9 lukem #ifdef IP_OPTIONS
248 1.1 cgd {
249 1.5 cgd u_char optbuf[BUFSIZ/3], *cp;
250 1.5 cgd char lbuf[BUFSIZ], *lp;
251 1.5 cgd int optsize = sizeof(optbuf), ipproto;
252 1.5 cgd struct protoent *ip;
253 1.5 cgd
254 1.5 cgd if ((ip = getprotobyname("ip")) != NULL)
255 1.5 cgd ipproto = ip->p_proto;
256 1.5 cgd else
257 1.5 cgd ipproto = IPPROTO_IP;
258 1.5 cgd if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
259 1.5 cgd &optsize) == 0 && optsize != 0) {
260 1.5 cgd lp = lbuf;
261 1.5 cgd for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
262 1.5 cgd sprintf(lp, " %2.2x", *cp);
263 1.5 cgd syslog(LOG_NOTICE,
264 1.5 cgd "Connection received using IP options (ignored):%s",
265 1.5 cgd lbuf);
266 1.5 cgd if (setsockopt(0, ipproto, IP_OPTIONS,
267 1.5 cgd (char *)NULL, optsize) != 0) {
268 1.5 cgd syslog(LOG_ERR,
269 1.5 cgd "setsockopt IP_OPTIONS NULL: %m");
270 1.5 cgd exit(1);
271 1.5 cgd }
272 1.5 cgd }
273 1.9 lukem }
274 1.1 cgd #endif
275 1.9 lukem if (do_rlogin(fromp, hostname) == 0)
276 1.9 lukem authenticated++;
277 1.1 cgd if (confirmed == 0) {
278 1.1 cgd write(f, "", 1);
279 1.1 cgd confirmed = 1; /* we sent the null! */
280 1.1 cgd }
281 1.1 cgd netf = f;
282 1.1 cgd
283 1.1 cgd pid = forkpty(&master, line, NULL, &win);
284 1.1 cgd if (pid < 0) {
285 1.1 cgd if (errno == ENOENT)
286 1.1 cgd fatal(f, "Out of ptys", 0);
287 1.1 cgd else
288 1.1 cgd fatal(f, "Forkpty", 1);
289 1.1 cgd }
290 1.1 cgd if (pid == 0) {
291 1.5 cgd if (f > 2) /* f should always be 0, but... */
292 1.1 cgd (void) close(f);
293 1.1 cgd setup_term(0);
294 1.7 mycroft if (authenticated)
295 1.1 cgd execl(_PATH_LOGIN, "login", "-p",
296 1.9 lukem "-h", utmphost, "-f", "--", lusername, (char *)0);
297 1.7 mycroft else
298 1.1 cgd execl(_PATH_LOGIN, "login", "-p",
299 1.9 lukem "-h", utmphost, "--", lusername, (char *)0);
300 1.1 cgd fatal(STDERR_FILENO, _PATH_LOGIN, 1);
301 1.1 cgd /*NOTREACHED*/
302 1.1 cgd }
303 1.1 cgd ioctl(f, FIONBIO, &on);
304 1.1 cgd ioctl(master, FIONBIO, &on);
305 1.1 cgd ioctl(master, TIOCPKT, &on);
306 1.1 cgd signal(SIGCHLD, cleanup);
307 1.1 cgd protocol(f, master);
308 1.1 cgd signal(SIGCHLD, SIG_IGN);
309 1.5 cgd cleanup(0);
310 1.1 cgd }
311 1.1 cgd
312 1.1 cgd char magic[2] = { 0377, 0377 };
313 1.1 cgd char oobdata[] = {TIOCPKT_WINDOW};
314 1.1 cgd
315 1.1 cgd /*
316 1.1 cgd * Handle a "control" request (signaled by magic being present)
317 1.1 cgd * in the data stream. For now, we are only willing to handle
318 1.1 cgd * window size changes.
319 1.1 cgd */
320 1.5 cgd int
321 1.1 cgd control(pty, cp, n)
322 1.1 cgd int pty;
323 1.1 cgd char *cp;
324 1.1 cgd int n;
325 1.1 cgd {
326 1.1 cgd struct winsize w;
327 1.1 cgd
328 1.1 cgd if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's')
329 1.1 cgd return (0);
330 1.1 cgd oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */
331 1.1 cgd bcopy(cp+4, (char *)&w, sizeof(w));
332 1.1 cgd w.ws_row = ntohs(w.ws_row);
333 1.1 cgd w.ws_col = ntohs(w.ws_col);
334 1.1 cgd w.ws_xpixel = ntohs(w.ws_xpixel);
335 1.1 cgd w.ws_ypixel = ntohs(w.ws_ypixel);
336 1.1 cgd (void)ioctl(pty, TIOCSWINSZ, &w);
337 1.1 cgd return (4+sizeof (w));
338 1.1 cgd }
339 1.1 cgd
340 1.1 cgd /*
341 1.1 cgd * rlogin "protocol" machine.
342 1.1 cgd */
343 1.5 cgd void
344 1.1 cgd protocol(f, p)
345 1.1 cgd register int f, p;
346 1.1 cgd {
347 1.1 cgd char pibuf[1024+1], fibuf[1024], *pbp, *fbp;
348 1.1 cgd register pcc = 0, fcc = 0;
349 1.1 cgd int cc, nfd, n;
350 1.1 cgd char cntl;
351 1.1 cgd
352 1.1 cgd /*
353 1.1 cgd * Must ignore SIGTTOU, otherwise we'll stop
354 1.1 cgd * when we try and set slave pty's window shape
355 1.1 cgd * (our controlling tty is the master pty).
356 1.1 cgd */
357 1.1 cgd (void) signal(SIGTTOU, SIG_IGN);
358 1.1 cgd send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */
359 1.1 cgd if (f > p)
360 1.1 cgd nfd = f + 1;
361 1.1 cgd else
362 1.1 cgd nfd = p + 1;
363 1.1 cgd if (nfd > FD_SETSIZE) {
364 1.1 cgd syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
365 1.1 cgd fatal(f, "internal error (select mask too small)", 0);
366 1.1 cgd }
367 1.1 cgd for (;;) {
368 1.1 cgd fd_set ibits, obits, ebits, *omask;
369 1.1 cgd
370 1.1 cgd FD_ZERO(&ebits);
371 1.1 cgd FD_ZERO(&ibits);
372 1.1 cgd FD_ZERO(&obits);
373 1.1 cgd omask = (fd_set *)NULL;
374 1.1 cgd if (fcc) {
375 1.1 cgd FD_SET(p, &obits);
376 1.1 cgd omask = &obits;
377 1.1 cgd } else
378 1.1 cgd FD_SET(f, &ibits);
379 1.1 cgd if (pcc >= 0)
380 1.1 cgd if (pcc) {
381 1.1 cgd FD_SET(f, &obits);
382 1.1 cgd omask = &obits;
383 1.1 cgd } else
384 1.1 cgd FD_SET(p, &ibits);
385 1.1 cgd FD_SET(p, &ebits);
386 1.1 cgd if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
387 1.1 cgd if (errno == EINTR)
388 1.1 cgd continue;
389 1.1 cgd fatal(f, "select", 1);
390 1.1 cgd }
391 1.1 cgd if (n == 0) {
392 1.1 cgd /* shouldn't happen... */
393 1.1 cgd sleep(5);
394 1.1 cgd continue;
395 1.1 cgd }
396 1.1 cgd #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
397 1.1 cgd if (FD_ISSET(p, &ebits)) {
398 1.1 cgd cc = read(p, &cntl, 1);
399 1.1 cgd if (cc == 1 && pkcontrol(cntl)) {
400 1.1 cgd cntl |= oobdata[0];
401 1.1 cgd send(f, &cntl, 1, MSG_OOB);
402 1.1 cgd if (cntl & TIOCPKT_FLUSHWRITE) {
403 1.1 cgd pcc = 0;
404 1.1 cgd FD_CLR(p, &ibits);
405 1.1 cgd }
406 1.1 cgd }
407 1.1 cgd }
408 1.1 cgd if (FD_ISSET(f, &ibits)) {
409 1.1 cgd fcc = read(f, fibuf, sizeof(fibuf));
410 1.1 cgd if (fcc < 0 && errno == EWOULDBLOCK)
411 1.1 cgd fcc = 0;
412 1.1 cgd else {
413 1.1 cgd register char *cp;
414 1.1 cgd int left, n;
415 1.1 cgd
416 1.1 cgd if (fcc <= 0)
417 1.1 cgd break;
418 1.1 cgd fbp = fibuf;
419 1.1 cgd
420 1.1 cgd top:
421 1.1 cgd for (cp = fibuf; cp < fibuf+fcc-1; cp++)
422 1.1 cgd if (cp[0] == magic[0] &&
423 1.1 cgd cp[1] == magic[1]) {
424 1.1 cgd left = fcc - (cp-fibuf);
425 1.1 cgd n = control(p, cp, left);
426 1.1 cgd if (n) {
427 1.1 cgd left -= n;
428 1.1 cgd if (left > 0)
429 1.9 lukem bcopy(cp+n, cp,
430 1.9 lukem left);
431 1.1 cgd fcc -= n;
432 1.1 cgd goto top; /* n^2 */
433 1.1 cgd }
434 1.1 cgd }
435 1.1 cgd FD_SET(p, &obits); /* try write */
436 1.1 cgd }
437 1.1 cgd }
438 1.1 cgd
439 1.1 cgd if (FD_ISSET(p, &obits) && fcc > 0) {
440 1.1 cgd cc = write(p, fbp, fcc);
441 1.1 cgd if (cc > 0) {
442 1.1 cgd fcc -= cc;
443 1.1 cgd fbp += cc;
444 1.1 cgd }
445 1.1 cgd }
446 1.1 cgd
447 1.1 cgd if (FD_ISSET(p, &ibits)) {
448 1.1 cgd pcc = read(p, pibuf, sizeof (pibuf));
449 1.1 cgd pbp = pibuf;
450 1.1 cgd if (pcc < 0 && errno == EWOULDBLOCK)
451 1.1 cgd pcc = 0;
452 1.1 cgd else if (pcc <= 0)
453 1.1 cgd break;
454 1.1 cgd else if (pibuf[0] == 0) {
455 1.1 cgd pbp++, pcc--;
456 1.1 cgd FD_SET(f, &obits); /* try write */
457 1.1 cgd } else {
458 1.1 cgd if (pkcontrol(pibuf[0])) {
459 1.1 cgd pibuf[0] |= oobdata[0];
460 1.1 cgd send(f, &pibuf[0], 1, MSG_OOB);
461 1.1 cgd }
462 1.1 cgd pcc = 0;
463 1.1 cgd }
464 1.1 cgd }
465 1.1 cgd if ((FD_ISSET(f, &obits)) && pcc > 0) {
466 1.1 cgd cc = write(f, pbp, pcc);
467 1.1 cgd if (cc < 0 && errno == EWOULDBLOCK) {
468 1.1 cgd /*
469 1.1 cgd * This happens when we try write after read
470 1.1 cgd * from p, but some old kernels balk at large
471 1.1 cgd * writes even when select returns true.
472 1.1 cgd */
473 1.1 cgd if (!FD_ISSET(p, &ibits))
474 1.1 cgd sleep(5);
475 1.1 cgd continue;
476 1.1 cgd }
477 1.1 cgd if (cc > 0) {
478 1.1 cgd pcc -= cc;
479 1.1 cgd pbp += cc;
480 1.1 cgd }
481 1.1 cgd }
482 1.1 cgd }
483 1.1 cgd }
484 1.1 cgd
485 1.1 cgd void
486 1.5 cgd cleanup(signo)
487 1.5 cgd int signo;
488 1.1 cgd {
489 1.1 cgd char *p;
490 1.1 cgd
491 1.1 cgd p = line + sizeof(_PATH_DEV) - 1;
492 1.1 cgd if (logout(p))
493 1.1 cgd logwtmp(p, "", "");
494 1.1 cgd (void)chmod(line, 0666);
495 1.1 cgd (void)chown(line, 0, 0);
496 1.1 cgd *p = 'p';
497 1.1 cgd (void)chmod(line, 0666);
498 1.1 cgd (void)chown(line, 0, 0);
499 1.1 cgd shutdown(netf, 2);
500 1.1 cgd exit(1);
501 1.1 cgd }
502 1.1 cgd
503 1.5 cgd void
504 1.1 cgd fatal(f, msg, syserr)
505 1.5 cgd int f;
506 1.1 cgd char *msg;
507 1.5 cgd int syserr;
508 1.1 cgd {
509 1.1 cgd int len;
510 1.1 cgd char buf[BUFSIZ], *bp = buf;
511 1.1 cgd
512 1.1 cgd /*
513 1.1 cgd * Prepend binary one to message if we haven't sent
514 1.1 cgd * the magic null as confirmation.
515 1.1 cgd */
516 1.1 cgd if (!confirmed)
517 1.1 cgd *bp++ = '\01'; /* error indicator */
518 1.1 cgd if (syserr)
519 1.1 cgd len = sprintf(bp, "rlogind: %s: %s.\r\n",
520 1.1 cgd msg, strerror(errno));
521 1.1 cgd else
522 1.1 cgd len = sprintf(bp, "rlogind: %s.\r\n", msg);
523 1.1 cgd (void) write(f, buf, bp + len - buf);
524 1.1 cgd exit(1);
525 1.1 cgd }
526 1.1 cgd
527 1.5 cgd int
528 1.9 lukem do_rlogin(dest, host)
529 1.5 cgd struct sockaddr_in *dest;
530 1.9 lukem char *host;
531 1.1 cgd {
532 1.9 lukem extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c */
533 1.9 lukem int retval;
534 1.9 lukem
535 1.1 cgd getstr(rusername, sizeof(rusername), "remuser too long");
536 1.1 cgd getstr(lusername, sizeof(lusername), "locuser too long");
537 1.1 cgd getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
538 1.1 cgd
539 1.1 cgd pwd = getpwnam(lusername);
540 1.9 lukem if (pwd == NULL) {
541 1.9 lukem syslog(LOG_INFO,
542 1.9 lukem "%s@%s as %s: unknown login.", rusername, host, lusername);
543 1.5 cgd return (-1);
544 1.9 lukem }
545 1.9 lukem retval = iruserok(dest->sin_addr.s_addr, pwd->pw_uid == 0, rusername,
546 1.9 lukem lusername);
547 1.9 lukem /* XXX put inet_ntoa(dest->sin_addr.s_addr) into all messages below */
548 1.9 lukem if (retval == 0) {
549 1.9 lukem if (log_success)
550 1.9 lukem syslog(LOG_INFO, "%s@%s as %s: iruserok succeeded",
551 1.9 lukem rusername, host, lusername);
552 1.9 lukem } else {
553 1.9 lukem if (__rcmd_errstr)
554 1.9 lukem syslog(LOG_INFO, "%s@%s as %s: iruserok failed (%s)",
555 1.9 lukem rusername, host, lusername, __rcmd_errstr);
556 1.9 lukem else
557 1.9 lukem syslog(LOG_INFO, "%s@%s as %s: iruserok failed",
558 1.9 lukem rusername, host, lusername);
559 1.9 lukem }
560 1.9 lukem return(retval);
561 1.1 cgd }
562 1.1 cgd
563 1.5 cgd void
564 1.1 cgd getstr(buf, cnt, errmsg)
565 1.1 cgd char *buf;
566 1.1 cgd int cnt;
567 1.1 cgd char *errmsg;
568 1.1 cgd {
569 1.1 cgd char c;
570 1.1 cgd
571 1.1 cgd do {
572 1.1 cgd if (read(0, &c, 1) != 1)
573 1.1 cgd exit(1);
574 1.1 cgd if (--cnt < 0)
575 1.1 cgd fatal(STDOUT_FILENO, errmsg, 0);
576 1.1 cgd *buf++ = c;
577 1.1 cgd } while (c != 0);
578 1.1 cgd }
579 1.1 cgd
580 1.1 cgd extern char **environ;
581 1.1 cgd
582 1.5 cgd void
583 1.1 cgd setup_term(fd)
584 1.1 cgd int fd;
585 1.1 cgd {
586 1.1 cgd register char *cp = index(term+ENVSIZE, '/');
587 1.1 cgd char *speed;
588 1.1 cgd struct termios tt;
589 1.1 cgd
590 1.1 cgd #ifndef notyet
591 1.1 cgd tcgetattr(fd, &tt);
592 1.1 cgd if (cp) {
593 1.1 cgd *cp++ = '\0';
594 1.1 cgd speed = cp;
595 1.1 cgd cp = index(speed, '/');
596 1.1 cgd if (cp)
597 1.1 cgd *cp++ = '\0';
598 1.1 cgd cfsetspeed(&tt, atoi(speed));
599 1.1 cgd }
600 1.1 cgd
601 1.1 cgd tt.c_iflag = TTYDEF_IFLAG;
602 1.1 cgd tt.c_oflag = TTYDEF_OFLAG;
603 1.1 cgd tt.c_lflag = TTYDEF_LFLAG;
604 1.1 cgd tcsetattr(fd, TCSAFLUSH, &tt);
605 1.1 cgd #else
606 1.1 cgd if (cp) {
607 1.1 cgd *cp++ = '\0';
608 1.1 cgd speed = cp;
609 1.1 cgd cp = index(speed, '/');
610 1.1 cgd if (cp)
611 1.1 cgd *cp++ = '\0';
612 1.1 cgd tcgetattr(fd, &tt);
613 1.1 cgd cfsetspeed(&tt, atoi(speed));
614 1.1 cgd tcsetattr(fd, TCSAFLUSH, &tt);
615 1.1 cgd }
616 1.1 cgd #endif
617 1.1 cgd
618 1.1 cgd env[0] = term;
619 1.1 cgd env[1] = 0;
620 1.1 cgd environ = env;
621 1.1 cgd }
622 1.1 cgd
623 1.1 cgd
624 1.5 cgd void
625 1.1 cgd usage()
626 1.1 cgd {
627 1.9 lukem syslog(LOG_ERR, "usage: rlogind [-alnL]");
628 1.1 cgd }
629 1.1 cgd
630 1.1 cgd /*
631 1.1 cgd * Check whether host h is in our local domain,
632 1.1 cgd * defined as sharing the last two components of the domain part,
633 1.1 cgd * or the entire domain part if the local domain has only one component.
634 1.1 cgd * If either name is unqualified (contains no '.'),
635 1.1 cgd * assume that the host is local, as it will be
636 1.1 cgd * interpreted as such.
637 1.1 cgd */
638 1.5 cgd int
639 1.1 cgd local_domain(h)
640 1.1 cgd char *h;
641 1.1 cgd {
642 1.1 cgd char localhost[MAXHOSTNAMELEN];
643 1.5 cgd char *p1, *p2;
644 1.1 cgd
645 1.1 cgd localhost[0] = 0;
646 1.1 cgd (void) gethostname(localhost, sizeof(localhost));
647 1.1 cgd p1 = topdomain(localhost);
648 1.1 cgd p2 = topdomain(h);
649 1.1 cgd if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
650 1.5 cgd return (1);
651 1.5 cgd return (0);
652 1.1 cgd }
653 1.1 cgd
654 1.1 cgd char *
655 1.1 cgd topdomain(h)
656 1.1 cgd char *h;
657 1.1 cgd {
658 1.1 cgd register char *p;
659 1.1 cgd char *maybe = NULL;
660 1.1 cgd int dots = 0;
661 1.1 cgd
662 1.1 cgd for (p = h + strlen(h); p >= h; p--) {
663 1.1 cgd if (*p == '.') {
664 1.1 cgd if (++dots == 2)
665 1.1 cgd return (p);
666 1.1 cgd maybe = p;
667 1.1 cgd }
668 1.1 cgd }
669 1.1 cgd return (maybe);
670 1.1 cgd }
671