rlogin.c revision 1.23 1 /* $NetBSD: rlogin.c,v 1.23 1999/07/11 18:21:18 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1983, 1990, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)rlogin.c 8.4 (Berkeley) 4/29/95";
45 #else
46 __RCSID("$NetBSD: rlogin.c,v 1.23 1999/07/11 18:21:18 thorpej Exp $");
47 #endif
48 #endif /* not lint */
49
50 /*
51 * rlogin - remote login
52 */
53 #include <sys/param.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <sys/time.h>
57 #include <sys/resource.h>
58 #include <sys/wait.h>
59 #include <sys/ioctl.h>
60
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64
65 #include <err.h>
66 #include <errno.h>
67 #include <fcntl.h>
68 #include <netdb.h>
69 #include <pwd.h>
70 #include <setjmp.h>
71 #include <termios.h>
72 #include <signal.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <unistd.h>
77
78 #ifdef __STDC__
79 #include <stdarg.h>
80 #else
81 #include <varargs.h>
82 #endif
83
84 #ifdef KERBEROS
85 #include <kerberosIV/des.h>
86 #include <kerberosIV/krb.h>
87 #include <kerberosIV/kstream.h>
88
89 #include "krb.h"
90
91 CREDENTIALS cred;
92 Key_schedule schedule;
93 MSG_DAT msg_data;
94 struct sockaddr_in local, foreign;
95 int use_kerberos = 1, doencrypt;
96 kstream krem;
97 #endif
98
99 #ifndef TIOCPKT_WINDOW
100 #define TIOCPKT_WINDOW 0x80
101 #endif
102
103 /* concession to Sun */
104 #ifndef SIGUSR1
105 #define SIGUSR1 30
106 #endif
107
108 #ifndef CCEQ
109 #define CCEQ(val, c) (c == val ? val != _POSIX_VDISABLE : 0)
110 #endif
111
112 int eight, rem;
113 struct termios deftty;
114
115 int noescape;
116 u_char escapechar = '~';
117
118 #ifdef OLDSUN
119 struct winsize {
120 unsigned short ws_row, ws_col;
121 unsigned short ws_xpixel, ws_ypixel;
122 };
123 #else
124 #define get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp)
125 #endif
126 struct winsize winsize;
127
128 void catch_child __P((int));
129 void copytochild __P((int));
130 void doit __P((sigset_t *));
131 void done __P((int));
132 void echo __P((int));
133 u_int getescape __P((char *));
134 void lostpeer __P((int));
135 int main __P((int, char **));
136 void mode __P((int));
137 void msg __P((char *));
138 void oob __P((int));
139 int reader __P((sigset_t *));
140 void sendwindow __P((void));
141 void setsignal __P((int));
142 int speed __P((int));
143 void sigwinch __P((int));
144 void stop __P((int));
145 void usage __P((void));
146 void writer __P((void));
147 void writeroob __P((int));
148
149 #ifdef KERBEROS
150 void warning __P((const char *, ...));
151 #endif
152 #ifdef OLDSUN
153 int get_window_size __P((int, struct winsize *));
154 #endif
155
156 extern char *__progname;
157
158 int
159 main(argc, argv)
160 int argc;
161 char *argv[];
162 {
163 struct passwd *pw;
164 struct servent *sp;
165 struct termios tty;
166 sigset_t smask;
167 int argoff, ch, dflag, one, uid;
168 int i, len, len2;
169 char *host, *p, *user, *name, term[1024] = "network";
170 speed_t ospeed;
171 struct sigaction sa;
172 struct rlimit rlim;
173 #ifdef KERBEROS
174 KTEXT_ST ticket;
175 int sock;
176 long authopts;
177 int through_once = 0;
178 extern int _kstream_des_debug_OOB;
179 char *dest_realm = NULL;
180 #endif
181
182 argoff = dflag = 0;
183 one = 1;
184 host = user = NULL;
185
186 if (strcmp(__progname, "rlogin") != 0)
187 host = __progname;
188
189 /* handle "rlogin host flags" */
190 if (!host && argc > 2 && argv[1][0] != '-') {
191 host = argv[1];
192 argoff = 1;
193 }
194
195 #ifdef KERBEROS
196 #define OPTIONS "8EKLde:k:l:x"
197 #else
198 #define OPTIONS "8EKLde:l:"
199 #endif
200 while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
201 switch(ch) {
202 case '8':
203 eight = 1;
204 break;
205 case 'E':
206 noescape = 1;
207 break;
208 #ifdef KERBEROS
209 case 'K':
210 use_kerberos = 0;
211 break;
212 #endif
213 case 'd':
214 #ifdef KERBEROS
215 _kstream_des_debug_OOB = 1;
216 #endif
217 dflag = 1;
218 break;
219 case 'e':
220 noescape = 0;
221 escapechar = getescape(optarg);
222 break;
223 #ifdef KERBEROS
224 case 'k':
225 dest_realm = optarg;
226 break;
227 #endif
228 case 'l':
229 user = optarg;
230 break;
231 #ifdef CRYPT
232 #ifdef KERBEROS
233 case 'x':
234 doencrypt = 1;
235 break;
236 #endif
237 #endif
238 case '?':
239 default:
240 usage();
241 }
242 optind += argoff;
243 argc -= optind;
244 argv += optind;
245
246 /* if haven't gotten a host yet, do so */
247 if (!host && !(host = *argv++))
248 usage();
249
250 if (*argv)
251 usage();
252
253 if (!(pw = getpwuid(uid = getuid())))
254 errx(1, "unknown user id.");
255 /* Accept user1@host format, though "-l user2" overrides user1 */
256 p = strchr(host, '@');
257 if (p) {
258 *p = '\0';
259 if (!user && p > host)
260 user = host;
261 host = p + 1;
262 if (*host == '\0')
263 usage();
264 }
265 if ((name = strdup(pw->pw_name)) == NULL)
266 err(1, "malloc");
267 if (!user)
268 user = name;
269
270 #ifdef KERBEROS
271 sp = NULL;
272 if (use_kerberos) {
273 sp = getservbyname((doencrypt ? "eklogin" : "klogin"), "tcp");
274 if (sp == NULL) {
275 use_kerberos = 0;
276 warning("can't get entry for %s/tcp service",
277 doencrypt ? "eklogin" : "klogin");
278 }
279 }
280 if (sp == NULL)
281 #endif
282 sp = getservbyname("login", "tcp");
283 if (sp == NULL)
284 errx(1, "login/tcp: unknown service.");
285
286 if ((p = getenv("TERM")) != NULL) {
287 (void)strncpy(term, p, sizeof(term) - 1);
288 term[sizeof(term) - 1] = '\0';
289 }
290 len = strlen(term);
291 if (len < (sizeof(term) - 1) && tcgetattr(0, &tty) == 0) {
292 /* start at 2 to include the / */
293 for (ospeed = i = cfgetospeed(&tty), len2 = 2; i > 9; len2++)
294 i /= 10;
295
296 if (len + len2 < sizeof(term))
297 (void)snprintf(term + len, len2 + 1, "/%d", ospeed);
298 }
299
300 (void)get_window_size(0, &winsize);
301
302 sigemptyset(&sa.sa_mask);
303 sa.sa_flags = SA_RESTART;
304 sa.sa_handler = lostpeer;
305 (void)sigaction(SIGPIPE, &sa, (struct sigaction *)0);
306 /* will use SIGUSR1 for window size hack, so hold it off */
307 sigemptyset(&smask);
308 sigaddset(&smask, SIGURG);
309 sigaddset(&smask, SIGUSR1);
310 (void)sigprocmask(SIG_SETMASK, &smask, &smask);
311 /*
312 * We set SIGURG and SIGUSR1 below so that an
313 * incoming signal will be held pending rather than being
314 * discarded. Note that these routines will be ready to get
315 * a signal by the time that they are unblocked below.;
316 */
317 sa.sa_handler = copytochild;
318 (void)sigaction(SIGURG, &sa, (struct sigaction *) 0);
319 sa.sa_handler = writeroob;
320 (void)sigaction(SIGUSR1, &sa, (struct sigaction *) 0);
321
322 /* don't dump core */
323 rlim.rlim_cur = rlim.rlim_max = 0;
324 if (setrlimit(RLIMIT_CORE, &rlim) < 0)
325 warn("setrlimit");
326
327 #ifdef KERBEROS
328 try_connect:
329 if (use_kerberos) {
330 struct hostent *hp;
331
332 /* Fully qualify hostname (needed for krb_realmofhost). */
333 hp = gethostbyname(host);
334 if (hp != NULL && !(host = strdup(hp->h_name)))
335 errx(1, "%s", strerror(ENOMEM));
336
337 rem = KSUCCESS;
338 errno = 0;
339 #ifdef CRYPT
340 if (doencrypt)
341 authopts = KOPT_DO_MUTUAL;
342 else
343 #endif /* CRYPT */
344 authopts = 0L;
345
346 if (dest_realm == NULL) {
347 /* default this now, once. */
348 if (!(dest_realm = krb_realmofhost (host))) {
349 warnx("Unknown realm for host %s.", host);
350 use_kerberos = 0;
351 sp = getservbyname("login", "tcp");
352 goto try_connect;
353 }
354 }
355
356 rem = kcmd(&sock, &host, sp->s_port, name, user,
357 term, 0, &ticket, "rcmd", dest_realm,
358 &cred, schedule, &msg_data, &local, &foreign,
359 authopts);
360
361 if (rem != KSUCCESS) {
362 switch(rem) {
363
364 case KDC_PR_UNKNOWN:
365 warnx("Host %s not registered for %s",
366 host, "Kerberos rlogin service");
367 use_kerberos = 0;
368 sp = getservbyname("login", "tcp");
369 goto try_connect;
370 case NO_TKT_FIL:
371 if (through_once++) {
372 use_kerberos = 0;
373 sp = getservbyname("login", "tcp");
374 goto try_connect;
375 }
376 #ifdef notyet
377 krb_get_pw_in_tkt(user, krb_realm, "krbtgt",
378 krb_realm,
379 DEFAULT_TKT_LIFE/5, 0);
380 goto try_connect;
381 #endif
382 default:
383 warnx("Kerberos rcmd failed: %s",
384 (rem == -1) ? "rcmd protocol failure" :
385 krb_err_txt[rem]);
386 use_kerberos = 0;
387 sp = getservbyname("login", "tcp");
388 goto try_connect;
389 }
390 }
391 rem = sock;
392 if (doencrypt)
393 krem = kstream_create_rlogin_from_fd(rem, &schedule,
394 &cred.session);
395 else
396 krem = kstream_create_from_fd(rem, 0, 0);
397 kstream_set_buffer_mode(krem, 0);
398 } else {
399 #ifdef CRYPT
400 if (doencrypt)
401 errx(1, "the -x flag requires Kerberos authentication.");
402 #endif /* CRYPT */
403 rem = rcmd(&host, sp->s_port, name, user, term, 0);
404 if (rem < 0)
405 exit(1);
406 }
407 #else
408 rem = rcmd(&host, sp->s_port, name, user, term, 0);
409
410 #endif /* KERBEROS */
411
412 if (rem < 0)
413 exit(1);
414
415 if (dflag &&
416 setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
417 warn("setsockopt DEBUG (ignored)");
418 one = IPTOS_LOWDELAY;
419 if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one, sizeof(int)) < 0)
420 warn("setsockopt TOS (ignored)");
421
422 (void)setuid(uid);
423 doit(&smask);
424 /*NOTREACHED*/
425 return (0);
426 }
427
428 int
429 speed(fd)
430 int fd;
431 {
432 struct termios tt;
433
434 (void)tcgetattr(fd, &tt);
435
436 return ((int)cfgetispeed(&tt));
437 }
438
439 pid_t child;
440 struct termios deftt;
441 struct termios nott;
442
443 void
444 doit(smask)
445 sigset_t *smask;
446 {
447 int i;
448 struct sigaction sa;
449
450 for (i = 0; i < NCCS; i++)
451 nott.c_cc[i] = _POSIX_VDISABLE;
452 tcgetattr(0, &deftt);
453 nott.c_cc[VSTART] = deftt.c_cc[VSTART];
454 nott.c_cc[VSTOP] = deftt.c_cc[VSTOP];
455 sigemptyset(&sa.sa_mask);
456 sa.sa_flags = SA_RESTART;
457 sa.sa_handler = SIG_IGN;
458 (void)sigaction(SIGINT, &sa, (struct sigaction *) 0);
459 setsignal(SIGHUP);
460 setsignal(SIGQUIT);
461 mode(1);
462 child = fork();
463 if (child == -1) {
464 warn("fork");
465 done(1);
466 }
467 if (child == 0) {
468 mode(1);
469 if (reader(smask) == 0) {
470 msg("connection closed.");
471 exit(0);
472 }
473 sleep(1);
474 msg("\aconnection closed.");
475 exit(1);
476 }
477
478 /*
479 * We may still own the socket, and may have a pending SIGURG (or might
480 * receive one soon) that we really want to send to the reader. When
481 * one of these comes in, the trap copytochild simply copies such
482 * signals to the child. We can now unblock SIGURG and SIGUSR1
483 * that were set above.
484 */
485 (void)sigprocmask(SIG_SETMASK, smask, (sigset_t *) 0);
486 sa.sa_handler = catch_child;
487 (void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
488 writer();
489 msg("closed connection.");
490 done(0);
491 }
492
493 /* trap a signal, unless it is being ignored. */
494 void
495 setsignal(sig)
496 int sig;
497 {
498 struct sigaction sa;
499 sigset_t sigs;
500
501 sigemptyset(&sigs);
502 sigaddset(&sigs, sig);
503 sigprocmask(SIG_BLOCK, &sigs, &sigs);
504
505 sigemptyset(&sa.sa_mask);
506 sa.sa_handler = exit;
507 sa.sa_flags = SA_RESTART;
508 (void)sigaction(sig, &sa, &sa);
509 if (sa.sa_handler == SIG_IGN)
510 (void)sigaction(sig, &sa, (struct sigaction *) 0);
511
512 (void)sigprocmask(SIG_SETMASK, &sigs, (sigset_t *) 0);
513 }
514
515 void
516 done(status)
517 int status;
518 {
519 pid_t w;
520 int wstatus;
521 struct sigaction sa;
522
523 mode(0);
524 if (child > 0) {
525 /* make sure catch_child does not snap it up */
526 sigemptyset(&sa.sa_mask);
527 sa.sa_handler = SIG_DFL;
528 sa.sa_flags = 0;
529 (void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
530 if (kill(child, SIGKILL) >= 0)
531 while ((w = wait(&wstatus)) > 0 && w != child)
532 continue;
533 }
534 exit(status);
535 }
536
537 int dosigwinch;
538
539 /*
540 * This is called when the reader process gets the out-of-band (urgent)
541 * request to turn on the window-changing protocol.
542 */
543 void
544 writeroob(signo)
545 int signo;
546 {
547 struct sigaction sa;
548
549 if (dosigwinch == 0) {
550 sendwindow();
551 sigemptyset(&sa.sa_mask);
552 sa.sa_handler = sigwinch;
553 sa.sa_flags = SA_RESTART;
554 (void)sigaction(SIGWINCH, &sa, (struct sigaction *) 0);
555 }
556 dosigwinch = 1;
557 }
558
559 void
560 catch_child(signo)
561 int signo;
562 {
563 int status;
564 pid_t pid;
565
566 for (;;) {
567 pid = waitpid(-1, &status, WNOHANG|WUNTRACED);
568 if (pid == 0)
569 return;
570 /* if the child (reader) dies, just quit */
571 if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
572 done(WEXITSTATUS(status) | WTERMSIG(status));
573 }
574 /* NOTREACHED */
575 }
576
577 /*
578 * writer: write to remote: 0 -> line.
579 * ~. terminate
580 * ~^Z suspend rlogin process.
581 * ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
582 */
583 void
584 writer()
585 {
586 int bol, local, n;
587 char c;
588
589 bol = 1; /* beginning of line */
590 local = 0;
591 for (;;) {
592 n = read(STDIN_FILENO, &c, 1);
593 if (n <= 0) {
594 if (n < 0 && errno == EINTR)
595 continue;
596 break;
597 }
598 /*
599 * If we're at the beginning of the line and recognize a
600 * command character, then we echo locally. Otherwise,
601 * characters are echo'd remotely. If the command character
602 * is doubled, this acts as a force and local echo is
603 * suppressed.
604 */
605 if (bol) {
606 bol = 0;
607 if (!noescape && c == escapechar) {
608 local = 1;
609 continue;
610 }
611 } else if (local) {
612 local = 0;
613 if (c == '.' || CCEQ(deftty.c_cc[VEOF], c)) {
614 echo((int)c);
615 break;
616 }
617 if (CCEQ(deftty.c_cc[VSUSP], c)) {
618 bol = 1;
619 echo((int)c);
620 stop(1);
621 continue;
622 }
623 if (CCEQ(deftty.c_cc[VDSUSP], c)) {
624 bol = 1;
625 echo((int)c);
626 stop(0);
627 continue;
628 }
629 if (c != escapechar) {
630 #ifdef KERBEROS
631 if (use_kerberos)
632 (void)kstream_write(krem,
633 (char *)&escapechar, 1);
634 else
635 #endif
636 (void)write(rem, &escapechar, 1);
637 }
638 }
639
640 #ifdef KERBEROS
641 if (use_kerberos) {
642 if (kstream_write(krem, &c, 1) == 0) {
643 msg("line gone");
644 break;
645 }
646 }
647 else
648 #endif
649 if (write(rem, &c, 1) == 0) {
650 msg("line gone");
651 break;
652 }
653
654 bol = CCEQ(deftty.c_cc[VKILL], c) ||
655 CCEQ(deftty.c_cc[VEOF], c) ||
656 CCEQ(deftty.c_cc[VINTR], c) ||
657 CCEQ(deftty.c_cc[VSUSP], c) ||
658 c == '\r' || c == '\n';
659 }
660 }
661
662 void
663 echo(i)
664 int i;
665 {
666 char c = (char)i;
667 char *p;
668 char buf[8];
669
670 p = buf;
671 c &= 0177;
672 *p++ = escapechar;
673 if (c < ' ') {
674 *p++ = '^';
675 *p++ = c + '@';
676 } else if (c == 0177) {
677 *p++ = '^';
678 *p++ = '?';
679 } else
680 *p++ = c;
681 *p++ = '\r';
682 *p++ = '\n';
683 (void)write(STDOUT_FILENO, buf, p - buf);
684 }
685
686 void
687 stop(all)
688 int all;
689 {
690 struct sigaction sa;
691
692 mode(0);
693 sigemptyset(&sa.sa_mask);
694 sa.sa_handler = SIG_IGN;
695 sa.sa_flags = SA_RESTART;
696 (void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
697 (void)kill(all ? 0 : getpid(), SIGTSTP);
698 sa.sa_handler = catch_child;
699 (void)sigaction(SIGCHLD, &sa, (struct sigaction *) 0);
700 mode(1);
701 sigwinch(0); /* check for size changes */
702 }
703
704 void
705 sigwinch(signo)
706 int signo;
707 {
708 struct winsize ws;
709
710 if (dosigwinch && get_window_size(0, &ws) == 0 &&
711 memcmp(&ws, &winsize, sizeof(ws))) {
712 winsize = ws;
713 sendwindow();
714 }
715 }
716
717 /*
718 * Send the window size to the server via the magic escape
719 */
720 void
721 sendwindow()
722 {
723 struct winsize *wp;
724 char obuf[4 + sizeof (struct winsize)];
725
726 wp = (struct winsize *)(obuf+4);
727 obuf[0] = 0377;
728 obuf[1] = 0377;
729 obuf[2] = 's';
730 obuf[3] = 's';
731 wp->ws_row = htons(winsize.ws_row);
732 wp->ws_col = htons(winsize.ws_col);
733 wp->ws_xpixel = htons(winsize.ws_xpixel);
734 wp->ws_ypixel = htons(winsize.ws_ypixel);
735
736 #ifdef KERBEROS
737 if (use_kerberos)
738 (void)kstream_write(krem, obuf, sizeof(obuf));
739 else
740 #endif
741 (void)write(rem, obuf, sizeof(obuf));
742 }
743
744 /*
745 * reader: read from remote: line -> 1
746 */
747 #define READING 1
748 #define WRITING 2
749
750 jmp_buf rcvtop;
751 pid_t ppid;
752 int rcvcnt, rcvstate;
753 char rcvbuf[8 * 1024];
754
755 void
756 oob(signo)
757 int signo;
758 {
759 struct termios tty;
760 int atmark, n, rcvd;
761 char waste[BUFSIZ], mark;
762
763 rcvd = 0;
764 while (recv(rem, &mark, 1, MSG_OOB) < 0) {
765 switch (errno) {
766 case EWOULDBLOCK:
767 /*
768 * Urgent data not here yet. It may not be possible
769 * to send it yet if we are blocked for output and
770 * our input buffer is full.
771 */
772 if (rcvcnt < sizeof(rcvbuf)) {
773 n = read(rem, rcvbuf + rcvcnt,
774 sizeof(rcvbuf) - rcvcnt);
775 if (n <= 0)
776 return;
777 rcvd += n;
778 } else {
779 n = read(rem, waste, sizeof(waste));
780 if (n <= 0)
781 return;
782 }
783 continue;
784 default:
785 return;
786 }
787 }
788 if (mark & TIOCPKT_WINDOW) {
789 /* Let server know about window size changes */
790 (void)kill(ppid, SIGUSR1);
791 }
792 if (!eight && (mark & TIOCPKT_NOSTOP)) {
793 (void)tcgetattr(0, &tty);
794 tty.c_iflag &= ~IXON;
795 (void)tcsetattr(0, TCSANOW, &tty);
796 }
797 if (!eight && (mark & TIOCPKT_DOSTOP)) {
798 (void)tcgetattr(0, &tty);
799 tty.c_iflag |= (deftty.c_iflag & IXON);
800 (void)tcsetattr(0, TCSANOW, &tty);
801 }
802 if (mark & TIOCPKT_FLUSHWRITE) {
803 (void)tcflush(1, TCIOFLUSH);
804 for (;;) {
805 if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
806 warn("ioctl SIOCATMARK (ignored)");
807 break;
808 }
809 if (atmark)
810 break;
811 n = read(rem, waste, sizeof (waste));
812 if (n <= 0)
813 break;
814 }
815 /*
816 * Don't want any pending data to be output, so clear the recv
817 * buffer. If we were hanging on a write when interrupted,
818 * don't want it to restart. If we were reading, restart
819 * anyway.
820 */
821 rcvcnt = 0;
822 longjmp(rcvtop, 1);
823 }
824
825 /* oob does not do FLUSHREAD (alas!) */
826
827 /*
828 * If we filled the receive buffer while a read was pending, longjmp
829 * to the top to restart appropriately. Don't abort a pending write,
830 * however, or we won't know how much was written.
831 */
832 if (rcvd && rcvstate == READING)
833 longjmp(rcvtop, 1);
834 }
835
836 /* reader: read from remote: line -> 1 */
837 int
838 reader(smask)
839 sigset_t *smask;
840 {
841 pid_t pid;
842 int n, remaining;
843 char *bufp;
844 struct sigaction sa;
845
846 pid = getpid(); /* modern systems use positives for pid */
847 sigemptyset(&sa.sa_mask);
848 sa.sa_flags = SA_RESTART;
849 sa.sa_handler = SIG_IGN;
850 (void)sigaction(SIGTTOU, &sa, (struct sigaction *) 0);
851 sa.sa_handler = oob;
852 (void)sigaction(SIGURG, &sa, (struct sigaction *) 0);
853 ppid = getppid();
854 (void)fcntl(rem, F_SETOWN, pid);
855 (void)setjmp(rcvtop);
856 (void)sigprocmask(SIG_SETMASK, smask, (sigset_t *) 0);
857 bufp = rcvbuf;
858 for (;;) {
859 while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
860 rcvstate = WRITING;
861 n = write(STDOUT_FILENO, bufp, remaining);
862 if (n < 0) {
863 if (errno != EINTR)
864 return (-1);
865 continue;
866 }
867 bufp += n;
868 }
869 bufp = rcvbuf;
870 rcvcnt = 0;
871 rcvstate = READING;
872
873 #ifdef KERBEROS
874 if (use_kerberos)
875 rcvcnt = kstream_read(krem, rcvbuf, sizeof(rcvbuf));
876 else
877 #endif
878 rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
879
880 if (rcvcnt == 0)
881 return (0);
882 if (rcvcnt < 0) {
883 if (errno == EINTR)
884 continue;
885 warn("read");
886 return (-1);
887 }
888 }
889 }
890
891 void
892 mode(f)
893 int f;
894 {
895 struct termios tty;
896
897 switch (f) {
898 case 0:
899 (void)tcsetattr(0, TCSANOW, &deftty);
900 break;
901 case 1:
902 (void)tcgetattr(0, &deftty);
903 tty = deftty;
904 /* This is loosely derived from sys/compat/tty_compat.c. */
905 tty.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);
906 tty.c_iflag &= ~ICRNL;
907 tty.c_oflag &= ~OPOST;
908 tty.c_cc[VMIN] = 1;
909 tty.c_cc[VTIME] = 0;
910 if (eight) {
911 tty.c_iflag &= IXOFF;
912 tty.c_cflag &= ~(CSIZE|PARENB);
913 tty.c_cflag |= CS8;
914 }
915 (void)tcsetattr(0, TCSANOW, &tty);
916 break;
917
918 default:
919 return;
920 }
921 }
922
923 void
924 lostpeer(signo)
925 int signo;
926 {
927 struct sigaction sa;
928 sa.sa_flags = SA_RESTART;
929 sa.sa_handler = SIG_IGN;
930 (void)sigaction(SIGPIPE, &sa, (struct sigaction *)0);
931 msg("\aconnection closed.");
932 done(1);
933 }
934
935 /* copy SIGURGs to the child process. */
936 void
937 copytochild(signo)
938 int signo;
939 {
940
941 (void)kill(child, SIGURG);
942 }
943
944 void
945 msg(str)
946 char *str;
947 {
948
949 (void)fprintf(stderr, "rlogin: %s\r\n", str);
950 }
951
952 #ifdef KERBEROS
953 /* VARARGS */
954 void
955 #if __STDC__
956 warning(const char *fmt, ...)
957 #else
958 warning(fmt, va_alist)
959 char *fmt;
960 va_dcl
961 #endif
962 {
963 va_list ap;
964
965 (void)fprintf(stderr, "rlogin: warning, using standard rlogin: ");
966 #ifdef __STDC__
967 va_start(ap, fmt);
968 #else
969 va_start(ap);
970 #endif
971 vfprintf(stderr, fmt, ap);
972 va_end(ap);
973 (void)fprintf(stderr, ".\n");
974 }
975 #endif
976
977 void
978 usage()
979 {
980 (void)fprintf(stderr,
981 "usage: rlogin [ -%s]%s[-e char] [ -l username ] [username@]host\n",
982 #ifdef KERBEROS
983 #ifdef CRYPT
984 "8EKLdx", " [-k realm] ");
985 #else
986 "8EKLd", " [-k realm] ");
987 #endif
988 #else
989 "8ELd", " ");
990 #endif
991 exit(1);
992 }
993
994 /*
995 * The following routine provides compatibility (such as it is) between older
996 * Suns and others. Suns have only a `ttysize', so we convert it to a winsize.
997 */
998 #ifdef OLDSUN
999 int
1000 get_window_size(fd, wp)
1001 int fd;
1002 struct winsize *wp;
1003 {
1004 struct ttysize ts;
1005 int error;
1006
1007 if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
1008 return (error);
1009 wp->ws_row = ts.ts_lines;
1010 wp->ws_col = ts.ts_cols;
1011 wp->ws_xpixel = 0;
1012 wp->ws_ypixel = 0;
1013 return (0);
1014 }
1015 #endif
1016
1017 u_int
1018 getescape(p)
1019 char *p;
1020 {
1021 long val;
1022 int len;
1023
1024 if ((len = strlen(p)) == 1) /* use any single char, including '\' */
1025 return ((u_int)*p);
1026 /* otherwise, \nnn */
1027 if (*p == '\\' && len >= 2 && len <= 4) {
1028 val = strtol(++p, NULL, 8);
1029 for (;;) {
1030 if (!*++p)
1031 return ((u_int)val);
1032 if (*p < '0' || *p > '8')
1033 break;
1034 }
1035 }
1036 msg("illegal option value -- e");
1037 usage();
1038 /* NOTREACHED */
1039 return (0);
1040 }
1041