kernel: Fix buildkernel without INVARIANTS.
[dragonfly.git] / usr.bin / rlogin / rlogin.c
bloba9cde4a771042376ba02c0a4b2958bd3e3ee0533
1 /*
2 * Copyright (c) 1983, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
5 * All rights reserved.
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
40 * @(#) Copyright (c) 1983, 1990, 1993 The Regents of the University of California. All rights reserved.
41 * @(#)rlogin.c 8.1 (Berkeley) 6/6/93
42 * $FreeBSD: src/usr.bin/rlogin/rlogin.c,v 1.30 2002/04/28 11:16:43 markm Exp $
46 * rlogin - remote login
48 #include <sys/param.h>
49 #include <sys/socket.h>
50 #include <sys/time.h>
51 #include <sys/resource.h>
52 #include <sys/wait.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/tcp.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <libutil.h>
63 #include <netdb.h>
64 #include <paths.h>
65 #include <pwd.h>
66 #include <setjmp.h>
67 #include <sgtty.h>
68 #include <signal.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <unistd.h>
74 #ifdef KERBEROS
75 #include <openssl/des.h>
76 #include <krb.h>
78 #include "krb.h"
80 CREDENTIALS cred;
81 Key_schedule schedule;
82 int use_kerberos = 1, doencrypt;
83 char dst_realm_buf[REALM_SZ], *dest_realm = NULL;
84 #endif
86 #ifndef TIOCPKT_WINDOW
87 #define TIOCPKT_WINDOW 0x80
88 #endif
90 /* concession to Sun */
91 #ifndef SIGUSR1
92 #define SIGUSR1 30
93 #endif
95 int eight, litout, rem;
96 int family = PF_UNSPEC;
98 int noescape;
99 u_char escapechar = '~';
101 const char *speeds[] = {
102 "0", "50", "75", "110", "134", "150", "200", "300", "600", "1200",
103 "1800", "2400", "4800", "9600", "19200", "38400", "57600", "115200"
104 #define MAX_SPEED_LENGTH (sizeof("115200") - 1)
107 #define get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp)
108 struct winsize winsize;
110 void catch_child(int);
111 void copytochild(int);
112 void doit(long) __dead2;
113 void done(int) __dead2;
114 void echo(char);
115 u_int getescape(char *);
116 void lostpeer(int);
117 void mode(int);
118 void msg(const char *);
119 void oob(int);
120 int reader(int);
121 void sendwindow(void);
122 void setsignal(int);
123 void sigwinch(int);
124 void stop(char);
125 void usage(void) __dead2;
126 void writer(void);
127 void writeroob(int);
130 main(int argc, char *argv[])
132 struct passwd *pw;
133 struct servent *sp;
134 struct sgttyb ttyb;
135 long omask;
136 int argoff, ch, dflag, Dflag, one, uid;
137 char *host, *localname, *p, *user, term[1024];
138 #ifdef KERBEROS
139 char *k;
140 #endif
141 struct sockaddr_storage ss;
142 int sslen;
144 argoff = dflag = Dflag = 0;
145 one = 1;
146 host = localname = user = NULL;
148 if ((p = strrchr(argv[0], '/')))
149 ++p;
150 else
151 p = argv[0];
153 if (strcmp(p, "rlogin"))
154 host = p;
156 /* handle "rlogin host flags" */
157 if (!host && argc > 2 && argv[1][0] != '-') {
158 host = argv[1];
159 argoff = 1;
162 #ifdef KERBEROS
163 #define OPTIONS "468DEKLde:i:k:l:x"
164 #else
165 #define OPTIONS "468DEKLde:i:l:"
166 #endif
167 while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
168 switch(ch) {
169 case '4':
170 family = PF_INET;
171 break;
173 case '6':
174 family = PF_INET6;
175 break;
177 case '8':
178 eight = 1;
179 break;
180 case 'D':
181 Dflag = 1;
182 break;
183 case 'E':
184 noescape = 1;
185 break;
186 case 'K':
187 #ifdef KERBEROS
188 use_kerberos = 0;
189 #endif
190 break;
191 case 'L':
192 litout = 1;
193 break;
194 case 'd':
195 dflag = 1;
196 break;
197 case 'e':
198 noescape = 0;
199 escapechar = getescape(optarg);
200 break;
201 case 'i':
202 if (getuid() != 0)
203 errx(1, "-i user: permission denied");
204 localname = optarg;
205 break;
206 #ifdef KERBEROS
207 case 'k':
208 dest_realm = dst_realm_buf;
209 (void)strncpy(dest_realm, optarg, REALM_SZ);
210 break;
211 #endif
212 case 'l':
213 user = optarg;
214 break;
215 #ifdef CRYPT
216 #ifdef KERBEROS
217 case 'x':
218 doencrypt = 1;
219 break;
220 #endif
221 #endif
222 case '?':
223 default:
224 usage();
226 optind += argoff;
228 /* if haven't gotten a host yet, do so */
229 if (!host && !(host = argv[optind++]))
230 usage();
232 if (argv[optind])
233 usage();
235 if (!(pw = getpwuid(uid = getuid())))
236 errx(1, "unknown user id");
237 if (!user)
238 user = pw->pw_name;
239 if (!localname)
240 localname = pw->pw_name;
242 sp = NULL;
243 #ifdef KERBEROS
244 k = auth_getval("auth_list");
245 if (k && !strstr(k, "kerberos"))
246 use_kerberos = 0;
247 if (use_kerberos) {
248 sp = getservbyname((doencrypt ? "eklogin" : "klogin"), "tcp");
249 if (sp == NULL) {
250 use_kerberos = 0;
251 warn("can't get entry for %s/tcp service",
252 doencrypt ? "eklogin" : "klogin");
255 #endif
256 if (sp == NULL)
257 sp = getservbyname("login", "tcp");
258 if (sp == NULL)
259 errx(1, "login/tcp: unknown service");
261 #define MAX_TERM_LENGTH (sizeof(term) - 1 - MAX_SPEED_LENGTH - 1)
263 (void)strncpy(term, (p = getenv("TERM")) ? p : "network",
264 MAX_TERM_LENGTH);
265 term[MAX_TERM_LENGTH] = '\0';
266 if (ioctl(0, TIOCGETP, &ttyb) == 0) {
267 (void)strcat(term, "/");
268 (void)strcat(term, speeds[(int)ttyb.sg_ospeed]);
271 (void)get_window_size(0, &winsize);
273 (void)signal(SIGPIPE, lostpeer);
274 /* will use SIGUSR1 for window size hack, so hold it off */
275 omask = sigblock(sigmask(SIGURG) | sigmask(SIGUSR1));
277 * We set SIGURG and SIGUSR1 below so that an
278 * incoming signal will be held pending rather than being
279 * discarded. Note that these routines will be ready to get
280 * a signal by the time that they are unblocked below.
282 (void)signal(SIGURG, copytochild);
283 (void)signal(SIGUSR1, writeroob);
285 #ifdef KERBEROS
286 if (use_kerberos) {
287 setuid(getuid());
288 rem = KSUCCESS;
289 errno = 0;
290 if (dest_realm == NULL)
291 dest_realm = krb_realmofhost(host);
293 #ifdef CRYPT
294 if (doencrypt) {
295 rem = krcmd_mutual(&host, sp->s_port, user, term, 0,
296 dest_realm, &cred, schedule);
297 des_set_key(&cred.session, schedule);
298 } else
299 #endif /* CRYPT */
300 rem = krcmd(&host, sp->s_port, user, term, 0,
301 dest_realm);
302 if (rem < 0) {
303 int i;
304 char **newargv;
306 sp = getservbyname("login", "tcp");
307 if (sp == NULL)
308 errx(1, "unknown service login/tcp");
309 if (errno == ECONNREFUSED)
310 warn("remote host doesn't support Kerberos");
311 if (errno == ENOENT)
312 warn("can't provide Kerberos auth data");
313 newargv = malloc((argc + 2) * sizeof(*newargv));
314 if (newargv == NULL)
315 err(1, "malloc");
316 newargv[0] = argv[0];
317 newargv[1] = "-K";
318 for(i = 1; i < argc; ++i)
319 newargv[i + 1] = argv[i];
320 newargv[argc + 1] = NULL;
321 execv(_PATH_RLOGIN, newargv);
323 } else {
324 #ifdef CRYPT
325 if (doencrypt)
326 errx(1, "the -x flag requires Kerberos authentication");
327 #endif /* CRYPT */
328 rem = rcmd_af(&host, sp->s_port, localname, user, term, 0,
329 family);
331 #else
332 rem = rcmd_af(&host, sp->s_port, localname, user, term, 0, family);
333 #endif /* KERBEROS */
335 if (rem < 0)
336 exit(1);
338 if (dflag &&
339 setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one, sizeof(one)) < 0)
340 warn("setsockopt");
341 if (Dflag &&
342 setsockopt(rem, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) < 0)
343 warn("setsockopt NODELAY (ignored)");
345 sslen = sizeof(ss);
346 one = IPTOS_LOWDELAY;
347 if (getsockname(rem, (struct sockaddr *)&ss, &sslen) == 0 &&
348 ss.ss_family == AF_INET) {
349 if (setsockopt(rem, IPPROTO_IP, IP_TOS, (char *)&one,
350 sizeof(int)) < 0)
351 warn("setsockopt TOS (ignored)");
352 } else
353 if (ss.ss_family == AF_INET)
354 warn("setsockopt getsockname failed");
356 (void)setuid(uid);
357 doit(omask);
358 /*NOTREACHED*/
361 int child, defflags, deflflags, tabflag;
362 char deferase, defkill;
363 struct tchars deftc;
364 struct ltchars defltc;
365 struct tchars notc = { -1, -1, -1, -1, -1, -1 };
366 struct ltchars noltc = { -1, -1, -1, -1, -1, -1 };
368 void
369 doit(long omask)
371 struct sgttyb sb;
373 (void)ioctl(0, TIOCGETP, (char *)&sb);
374 defflags = sb.sg_flags;
375 tabflag = defflags & TBDELAY;
376 defflags &= ECHO | CRMOD;
377 deferase = sb.sg_erase;
378 defkill = sb.sg_kill;
379 (void)ioctl(0, TIOCLGET, &deflflags);
380 (void)ioctl(0, TIOCGETC, &deftc);
381 notc.t_startc = deftc.t_startc;
382 notc.t_stopc = deftc.t_stopc;
383 (void)ioctl(0, TIOCGLTC, &defltc);
384 (void)signal(SIGINT, SIG_IGN);
385 setsignal(SIGHUP);
386 setsignal(SIGQUIT);
387 child = fork();
388 if (child == -1) {
389 warn("fork");
390 done(1);
392 if (child == 0) {
393 mode(1);
394 if (reader(omask) == 0) {
395 msg("connection closed.");
396 exit(0);
398 sleep(1);
399 msg("\007connection closed.");
400 exit(1);
404 * We may still own the socket, and may have a pending SIGURG (or might
405 * receive one soon) that we really want to send to the reader. When
406 * one of these comes in, the trap copytochild simply copies such
407 * signals to the child. We can now unblock SIGURG and SIGUSR1
408 * that were set above.
410 (void)sigsetmask(omask);
411 (void)signal(SIGCHLD, catch_child);
412 writer();
413 msg("closed connection.");
414 done(0);
417 /* trap a signal, unless it is being ignored. */
418 void
419 setsignal(int sig)
421 int omask = sigblock(sigmask(sig));
423 if (signal(sig, exit) == SIG_IGN)
424 (void)signal(sig, SIG_IGN);
425 (void)sigsetmask(omask);
428 void
429 done(int status)
431 int w, wstatus;
433 mode(0);
434 if (child > 0) {
435 /* make sure catch_child does not snap it up */
436 (void)signal(SIGCHLD, SIG_DFL);
437 if (kill(child, SIGKILL) >= 0)
438 while ((w = wait(&wstatus)) > 0 && w != child);
440 exit(status);
443 int dosigwinch;
446 * This is called when the reader process gets the out-of-band (urgent)
447 * request to turn on the window-changing protocol.
449 void
450 writeroob(int signo __unused)
452 if (dosigwinch == 0) {
453 sendwindow();
454 (void)signal(SIGWINCH, sigwinch);
456 dosigwinch = 1;
459 void
460 catch_child(int signo __unused)
462 union wait status;
463 int pid;
465 for (;;) {
466 pid = wait3((int *)&status, WNOHANG|WUNTRACED, NULL);
467 if (pid == 0)
468 return;
469 /* if the child (reader) dies, just quit */
470 if (pid < 0 || (pid == child && !WIFSTOPPED(status)))
471 done((int)(status.w_termsig | status.w_retcode));
473 /* NOTREACHED */
477 * writer: write to remote: 0 -> line.
478 * ~. terminate
479 * ~^Z suspend rlogin process.
480 * ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
482 void
483 writer(void)
485 int bol, local, n;
486 char c;
488 bol = 1; /* beginning of line */
489 local = 0;
490 for (;;) {
491 n = read(STDIN_FILENO, &c, 1);
492 if (n <= 0) {
493 if (n < 0 && errno == EINTR)
494 continue;
495 break;
498 * If we're at the beginning of the line and recognize a
499 * command character, then we echo locally. Otherwise,
500 * characters are echo'd remotely. If the command character
501 * is doubled, this acts as a force and local echo is
502 * suppressed.
504 if (bol) {
505 bol = 0;
506 if (!noescape && c == escapechar) {
507 local = 1;
508 continue;
510 } else if (local) {
511 local = 0;
512 if (c == '.' || c == deftc.t_eofc) {
513 echo(c);
514 break;
516 if (c == defltc.t_suspc || c == defltc.t_dsuspc) {
517 bol = 1;
518 echo(c);
519 stop(c);
520 continue;
522 if (c != escapechar)
523 #ifdef CRYPT
524 #ifdef KERBEROS
525 if (doencrypt)
526 (void)des_enc_write(rem,
527 (char *)&escapechar, 1,
528 schedule, &cred.session);
529 else
530 #endif
531 #endif
532 (void)write(rem, &escapechar, 1);
535 #ifdef CRYPT
536 #ifdef KERBEROS
537 if (doencrypt) {
538 if (des_enc_write(rem, &c, 1, schedule, &cred.session) == 0) {
539 msg("line gone");
540 break;
542 } else
543 #endif
544 #endif
545 if (write(rem, &c, 1) == 0) {
546 msg("line gone");
547 break;
549 bol = c == defkill || c == deftc.t_eofc ||
550 c == deftc.t_intrc || c == defltc.t_suspc ||
551 c == '\r' || c == '\n';
555 void
556 echo(char c)
558 char *p;
559 char buf[8];
561 p = buf;
562 c &= 0177;
563 *p++ = escapechar;
564 if (c < ' ') {
565 *p++ = '^';
566 *p++ = c + '@';
567 } else if (c == 0177) {
568 *p++ = '^';
569 *p++ = '?';
570 } else
571 *p++ = c;
572 *p++ = '\r';
573 *p++ = '\n';
574 (void)write(STDOUT_FILENO, buf, p - buf);
577 void
578 stop(char cmdc)
580 mode(0);
581 (void)signal(SIGCHLD, SIG_IGN);
582 (void)kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
583 (void)signal(SIGCHLD, catch_child);
584 mode(1);
585 sigwinch(0); /* check for size changes */
588 void
589 sigwinch(int signo __unused)
591 struct winsize ws;
593 if (dosigwinch && get_window_size(0, &ws) == 0 &&
594 bcmp(&ws, &winsize, sizeof(ws))) {
595 winsize = ws;
596 sendwindow();
601 * Send the window size to the server via the magic escape
603 void
604 sendwindow(void)
606 struct winsize *wp;
607 char obuf[4 + sizeof (struct winsize)];
609 wp = (struct winsize *)(obuf+4);
610 obuf[0] = 0377;
611 obuf[1] = 0377;
612 obuf[2] = 's';
613 obuf[3] = 's';
614 wp->ws_row = htons(winsize.ws_row);
615 wp->ws_col = htons(winsize.ws_col);
616 wp->ws_xpixel = htons(winsize.ws_xpixel);
617 wp->ws_ypixel = htons(winsize.ws_ypixel);
619 #ifdef CRYPT
620 #ifdef KERBEROS
621 if(doencrypt)
622 (void)des_enc_write(rem, obuf, sizeof(obuf),
623 schedule, &cred.session);
624 else
625 #endif
626 #endif
627 (void)write(rem, obuf, sizeof(obuf));
631 * reader: read from remote: line -> 1
633 #define READING 1
634 #define WRITING 2
636 jmp_buf rcvtop;
637 int ppid, rcvcnt, rcvstate;
638 char rcvbuf[8 * 1024];
640 void
641 oob(int signo __unused)
643 struct sgttyb sb;
644 int atmark, n, out, rcvd;
645 char waste[BUFSIZ], mark;
647 out = O_RDWR;
648 rcvd = 0;
649 while (recv(rem, &mark, 1, MSG_OOB) < 0) {
650 switch (errno) {
651 case EWOULDBLOCK:
653 * Urgent data not here yet. It may not be possible
654 * to send it yet if we are blocked for output and
655 * our input buffer is full.
657 if (rcvcnt < (int)sizeof(rcvbuf)) {
658 n = read(rem, rcvbuf + rcvcnt,
659 sizeof(rcvbuf) - rcvcnt);
660 if (n <= 0)
661 return;
662 rcvd += n;
663 } else {
664 n = read(rem, waste, sizeof(waste));
665 if (n <= 0)
666 return;
668 continue;
669 default:
670 return;
673 if (mark & TIOCPKT_WINDOW) {
674 /* Let server know about window size changes */
675 (void)kill(ppid, SIGUSR1);
677 if (!eight && (mark & TIOCPKT_NOSTOP)) {
678 (void)ioctl(0, TIOCGETP, (char *)&sb);
679 sb.sg_flags &= ~CBREAK;
680 sb.sg_flags |= RAW;
681 (void)ioctl(0, TIOCSETN, (char *)&sb);
682 notc.t_stopc = -1;
683 notc.t_startc = -1;
684 (void)ioctl(0, TIOCSETC, (char *)&notc);
686 if (!eight && (mark & TIOCPKT_DOSTOP)) {
687 (void)ioctl(0, TIOCGETP, (char *)&sb);
688 sb.sg_flags &= ~RAW;
689 sb.sg_flags |= CBREAK;
690 (void)ioctl(0, TIOCSETN, (char *)&sb);
691 notc.t_stopc = deftc.t_stopc;
692 notc.t_startc = deftc.t_startc;
693 (void)ioctl(0, TIOCSETC, (char *)&notc);
695 if (mark & TIOCPKT_FLUSHWRITE) {
696 (void)ioctl(1, TIOCFLUSH, (char *)&out);
697 for (;;) {
698 if (ioctl(rem, SIOCATMARK, &atmark) < 0) {
699 warn("ioctl");
700 break;
702 if (atmark)
703 break;
704 n = read(rem, waste, sizeof (waste));
705 if (n <= 0)
706 break;
709 * Don't want any pending data to be output, so clear the recv
710 * buffer. If we were hanging on a write when interrupted,
711 * don't want it to restart. If we were reading, restart
712 * anyway.
714 rcvcnt = 0;
715 longjmp(rcvtop, 1);
718 /* oob does not do FLUSHREAD (alas!) */
721 * If we filled the receive buffer while a read was pending, longjmp
722 * to the top to restart appropriately. Don't abort a pending write,
723 * however, or we won't know how much was written.
725 if (rcvd && rcvstate == READING)
726 longjmp(rcvtop, 1);
729 /* reader: read from remote: line -> 1 */
731 reader(int omask)
733 int pid, n, remaining;
734 char *bufp;
736 #if BSD >= 43 || defined(SUNOS4)
737 pid = getpid(); /* modern systems use positives for pid */
738 #else
739 pid = -getpid(); /* old broken systems use negatives */
740 #endif
741 (void)signal(SIGTTOU, SIG_IGN);
742 (void)signal(SIGURG, oob);
743 (void)signal(SIGUSR1, oob); /* When propogating SIGURG from parent */
744 ppid = getppid();
745 (void)fcntl(rem, F_SETOWN, pid);
746 (void)setjmp(rcvtop);
747 (void)sigsetmask(omask);
748 bufp = rcvbuf;
749 for (;;) {
750 while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
751 rcvstate = WRITING;
752 n = write(STDOUT_FILENO, bufp, remaining);
753 if (n < 0) {
754 if (errno != EINTR)
755 return (-1);
756 continue;
758 bufp += n;
760 bufp = rcvbuf;
761 rcvcnt = 0;
762 rcvstate = READING;
764 #ifdef CRYPT
765 #ifdef KERBEROS
766 if (doencrypt)
767 rcvcnt = des_enc_read(rem, rcvbuf, sizeof(rcvbuf),
768 schedule, &cred.session);
769 else
770 #endif
771 #endif
772 rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
773 if (rcvcnt == 0)
774 return (0);
775 if (rcvcnt < 0) {
776 if (errno == EINTR)
777 continue;
778 warn("read");
779 return (-1);
784 void
785 mode(int f)
787 struct ltchars *ltc;
788 struct sgttyb sb;
789 struct tchars *tc;
790 int lflags;
792 (void)ioctl(0, TIOCGETP, (char *)&sb);
793 (void)ioctl(0, TIOCLGET, (char *)&lflags);
794 switch(f) {
795 case 0:
796 sb.sg_flags &= ~(CBREAK|RAW|TBDELAY);
797 sb.sg_flags |= defflags|tabflag;
798 tc = &deftc;
799 ltc = &defltc;
800 sb.sg_kill = defkill;
801 sb.sg_erase = deferase;
802 lflags = deflflags;
803 break;
804 case 1:
805 sb.sg_flags |= (eight ? RAW : CBREAK);
806 sb.sg_flags &= ~defflags;
807 /* preserve tab delays, but turn off XTABS */
808 if ((sb.sg_flags & TBDELAY) == XTABS)
809 sb.sg_flags &= ~TBDELAY;
810 tc = &notc;
811 ltc = &noltc;
812 sb.sg_kill = sb.sg_erase = -1;
813 if (litout)
814 lflags |= LLITOUT;
815 break;
816 default:
817 return;
819 (void)ioctl(0, TIOCSLTC, (char *)ltc);
820 (void)ioctl(0, TIOCSETC, (char *)tc);
821 (void)ioctl(0, TIOCSETN, (char *)&sb);
822 (void)ioctl(0, TIOCLSET, (char *)&lflags);
825 void
826 lostpeer(int signo __unused)
828 (void)signal(SIGPIPE, SIG_IGN);
829 msg("\007connection closed.");
830 done(1);
833 /* copy SIGURGs to the child process via SIGUSR1. */
834 void
835 copytochild(int signo __unused)
837 (void)kill(child, SIGUSR1);
840 void
841 msg(const char *str)
843 (void)fprintf(stderr, "rlogin: %s\r\n", str);
846 void
847 usage(void)
849 (void)fprintf(stderr,
850 "usage: rlogin [-46%s]%s[-e char] [-i localname] [-l username] host\n",
851 #ifdef KERBEROS
852 #ifdef CRYPT
853 "8DEKLdx", " [-k realm] ");
854 #else
855 "8DEKLd", " [-k realm] ");
856 #endif
857 #else
858 "8DEKLd", " ");
859 #endif
860 exit(1);
863 u_int
864 getescape(char *p)
866 long val;
867 int len;
869 if ((len = strlen(p)) == 1) /* use any single char, including '\' */
870 return ((u_int)*p);
871 /* otherwise, \nnn */
872 if (*p == '\\' && len >= 2 && len <= 4) {
873 val = strtol(++p, NULL, 8);
874 for (;;) {
875 if (!*++p)
876 return ((u_int)val);
877 if (*p < '0' || *p > '8')
878 break;
881 msg("illegal option value -- e");
882 usage();
883 /* NOTREACHED */