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