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