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