unstack, sort: cleanup and improvement
[minix.git] / commands / rlogin / rlogin.c
blobe712b486da70e6d705571888b370eadc0378ad47
1 /*
2 * Copyright (c) 1983, 1990 The Regents of the University of California.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1983, 1990 The Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
40 #ifndef lint
41 #ifdef ID
42 static char sccsid[] = "@(#)rlogin.c 5.33 (Berkeley) 3/1/91";
43 #endif
44 #endif /* not lint */
47 * $Source$
48 * $Header: mit/rlogin/RCS/rlogin.c,v 5.2 89/07/26 12:11:21 kfall
49 * Exp Locker: kfall $
53 * rlogin - remote login
55 #include <sys/ioctl.h>
56 #include <sys/types.h>
57 #include <sys/wait.h>
59 #include <net/netlib.h>
60 #include <net/hton.h>
61 #include <net/gen/in.h>
62 #include <net/gen/netdb.h>
63 #include <net/gen/tcp.h>
64 #include <net/gen/tcp_io.h>
66 #include <termios.h>
67 #include <setjmp.h>
68 #include <errno.h>
69 #include <pwd.h>
70 #include <stdio.h>
71 #include <unistd.h>
72 #include <string.h>
73 #include <assert.h>
74 #include <ctype.h>
75 #include <fcntl.h>
76 #include <signal.h>
77 #include <stdlib.h>
79 #ifdef KERBEROS
80 #include <kerberosIV/des.h>
81 #include <kerberosIV/krb.h>
83 CREDENTIALS cred;
84 Key_schedule schedule;
85 int use_kerberos = 1, doencrypt;
86 char dst_realm_buf[REALM_SZ], *dest_realm = NULL;
87 extern char *krb_realmofhost();
88 #endif
90 #ifndef TIOCPKT_WINDOW
91 #define TIOCPKT_WINDOW 0x80
92 #endif
94 /* concession to Sun */
95 #ifndef SIGUSR1
96 #define SIGUSR1 30
97 #endif
99 extern int errno;
100 int eight, litout, rem;
102 int noescape;
103 u_char escapechar = '~';
105 struct speed
107 speed_t speed;
108 char *name;
109 } speeds[] = {
110 { B0, "0" }, { B50, "50" }, { B75, "75" }, { B110, "110" },
111 { B134, "134" }, { B150, "150" }, { B200, "200" }, { B300, "300" },
112 { B600, "600" }, { B1200, "1200" }, { B1800, "1800" },
113 { B2400, "2400" }, { B4800, "4800" }, { B9600, "9600" },
114 { B19200, "19200" }, { B38400, "38400" }, { B57600, "57600" },
115 { B115200, "115200" },
116 { -1, NULL },
119 #if __minix_vmd
120 /* flow control variables */
121 int more2read_0;
122 int inprogress_0;
123 int more2write_1;
124 int inprogress_1;
125 int more2read_rem;
126 int inprogress_rd_rem;
127 int more2write_rem;
128 int inprogress_wr_rem;
130 /* write to remote */
131 size_t wr_rem_size;
132 size_t wr_rem_offset;
133 size_t extra_wr_rem_size;
134 size_t extra_wr_rem_offset;
135 char *extra_wr_rem;
136 size_t extra_wr_rem_new_size;
137 char *extra_wr_rem_new;
139 #endif /* __minix_vmd */
141 struct winsize winsize;
143 #define get_window_size(fd, wp) ioctl(fd, TIOCGWINSZ, wp)
145 extern int main( int argc, char **argv );
146 static void usage( void );
147 static u_char getescape( char *p );
148 static char *speeds2str( speed_t speed );
149 static void lostpeer( int sig );
150 static void doit( void );
151 static void setsignal( int sig, void (*act)(int sig) );
152 static void msg( char *str );
153 static void done( int status );
154 #if !__minix_vmd
155 static int reader( void );
156 #endif
157 static void mode( int f );
158 #if __minix_vmd
159 static void mark_async( int fd );
160 static void init_0( void );
161 static void init_1( void );
162 static void init_rd_rem( void );
163 static void init_wr_rem( void );
164 static void restart_0( void );
165 static void restart_1( void );
166 static void restart_rd_rem( void );
167 static void restart_wr_rem( void );
168 static void completed_0( int result, int error );
169 static void completed_1( int result, int error );
170 static void completed_rd_rem( int result, int error );
171 static void completed_wr_rem( int result, int error );
172 static void do_urg( int urg_byte );
173 #endif
174 #if !__minix_vmd
175 static void catch_child( int sig );
176 static void writer( void );
177 #endif
178 static void echo( int c );
179 #if __minix_vmd
180 static void finish( void );
181 static void sendwindow( void );
182 static void sigwinch( int sig );
183 static void subshell( void );
184 #endif
186 int main(argc, argv)
187 int argc;
188 char **argv;
190 extern char *optarg;
191 extern int optind;
192 struct passwd *pw;
193 struct servent *sp;
194 struct termios ttyb;
195 nwio_tcpopt_t tcpopt;
196 int error;
197 int argoff, ch, dflag, one, uid;
198 char *host, *p, *user, term[1024];
200 argoff = dflag = 0;
201 one = 1;
202 host = user = NULL;
204 if (p = rindex(argv[0], '/'))
205 ++p;
206 else
207 p = argv[0];
209 if (strcmp(p, "rlogin"))
210 host = p;
212 /* handle "rlogin host flags" */
213 if (!host && argc > 2 && argv[1][0] != '-') {
214 host = argv[1];
215 argoff = 1;
218 #ifdef KERBEROS
219 #define OPTIONS "8EKLde:k:l:x"
220 #else
221 #define OPTIONS "8EKLde:l:"
222 #endif
223 while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != EOF)
224 switch(ch) {
225 case '8':
226 eight = 1;
227 break;
228 case 'E':
229 noescape = 1;
230 break;
231 case 'K':
232 #ifdef KERBEROS
233 use_kerberos = 0;
234 #endif
235 break;
236 case 'L':
237 litout = 1;
238 break;
239 case 'd':
240 dflag = 1;
241 break;
242 case 'e':
243 escapechar = getescape(optarg);
244 break;
245 #ifdef KERBEROS
246 case 'k':
247 dest_realm = dst_realm_buf;
248 (void)strncpy(dest_realm, optarg, REALM_SZ);
249 break;
250 #endif
251 case 'l':
252 user = optarg;
253 break;
254 #ifdef CRYPT
255 #ifdef KERBEROS
256 case 'x':
257 doencrypt = 1;
258 des_set_key(cred.session, schedule);
259 break;
260 #endif
261 #endif
262 case '?':
263 default:
264 usage();
266 optind += argoff;
267 argc -= optind;
268 argv += optind;
270 /* if haven't gotten a host yet, do so */
271 if (!host && !(host = *argv++))
272 usage();
274 if (*argv)
275 usage();
277 if (!(pw = getpwuid(uid = getuid()))) {
278 (void)fprintf(stderr, "rlogin: unknown user id.\n");
279 exit(1);
281 if (!user)
282 user = pw->pw_name;
284 sp = NULL;
285 #ifdef KERBEROS
286 if (use_kerberos) {
287 sp = getservbyname((doencrypt ? "eklogin" : "klogin"), "tcp");
288 if (sp == NULL) {
289 use_kerberos = 0;
290 warning("can't get entry for %s/tcp service",
291 doencrypt ? "eklogin" : "klogin");
294 #endif
295 if (sp == NULL)
296 sp = getservbyname("login", "tcp");
297 if (sp == NULL) {
298 (void)fprintf(stderr, "rlogin: login/tcp: unknown service.\n");
299 exit(1);
302 (void)strncpy(term, (p = getenv("TERM")) ? p : "network", sizeof(term));
303 term[sizeof(term)-1]= 0;
305 if (tcgetattr(0, &ttyb) == 0) {
306 (void)strcat(term, "/");
307 (void)strcat(term, speeds2str(cfgetospeed(&ttyb)));
310 (void)get_window_size(0, &winsize);
312 (void)signal(SIGPIPE, lostpeer);
314 #ifdef KERBEROS
315 try_connect:
316 if (use_kerberos) {
317 rem = KSUCCESS;
318 errno = 0;
319 if (dest_realm == NULL)
320 dest_realm = krb_realmofhost(host);
322 #ifdef CRYPT
323 if (doencrypt)
324 rem = krcmd_mutual(&host, sp->s_port, user, term, 0,
325 dest_realm, &cred, schedule);
326 else
327 #endif /* CRYPT */
328 rem = krcmd(&host, sp->s_port, user, term, 0,
329 dest_realm);
330 if (rem < 0) {
331 use_kerberos = 0;
332 sp = getservbyname("login", "tcp");
333 if (sp == NULL) {
334 (void)fprintf(stderr,
335 "rlogin: unknown service login/tcp.\n");
336 exit(1);
338 if (errno == ECONNREFUSED)
339 warning("remote host doesn't support Kerberos");
340 if (errno == ENOENT)
341 warning("can't provide Kerberos auth data");
342 goto try_connect;
344 } else {
345 #ifdef CRYPT
346 if (doencrypt) {
347 (void)fprintf(stderr,
348 "rlogin: the -x flag requires Kerberos authentication.\n");
349 exit(1);
351 #endif /* CRYPT */
352 rem = rcmd(&host, sp->s_port, pw->pw_name, user, term, 0);
354 #else
355 rem = rcmd(&host, sp->s_port, pw->pw_name, user, term, 0);
356 #endif /* KERBEROS */
358 if (rem < 0)
359 exit(1);
361 /* Enable BSD compatibility for urgent data. */
362 tcpopt.nwto_flags= NWTO_BSD_URG;
363 error= ioctl(rem, NWIOSTCPOPT, &tcpopt);
364 if (error == -1)
366 fprintf(stderr, "rlogin: NWIOSTCPOPT failed: %s\n",
367 strerror(errno));
370 (void)setuid(uid);
371 doit();
372 /*NOTREACHED*/
375 struct termios defattr, rawattr;
376 #if __minix_vmd
377 int mustsendwindow;
378 #else
379 int child;
380 #endif
382 static void
383 doit()
385 struct termios sb;
386 #if !__minix_vmd
387 int r;
388 #else
389 asio_fd_set_t fd_set;
390 struct fwait fw;
391 int result;
392 #endif
394 (void)tcgetattr(0, &sb);
395 defattr = sb;
396 rawattr = sb;
398 rawattr.c_iflag &= ~(ICRNL | IGNCR | INLCR | ISTRIP | IXOFF | IXON |
399 PARMRK | IXANY);
400 rawattr.c_oflag &= ~(OPOST);
401 rawattr.c_lflag &= ~(ECHONL | ECHO | ICANON | IEXTEN | ISIG);
403 (void)signal(SIGINT, SIG_IGN);
404 setsignal(SIGHUP, exit);
405 setsignal(SIGQUIT, exit);
407 #if !__minix_vmd
408 child = fork();
409 if (child == -1) {
410 (void)fprintf(stderr, "rlogin: fork: %s.\n", strerror(errno));
411 done(1);
413 if (child == 0) {
414 mode(1);
415 r = reader();
416 if (r == 0) {
417 msg("connection closed.");
418 exit(0);
420 sleep(1);
421 msg("\007connection closed.");
422 exit(1);
425 (void)signal(SIGCHLD, catch_child);
426 writer();
428 #else /* __minix_vmd */
430 mode(1);
431 /* mark the file descriptors 0, 1, and rem as asynchronous. */
432 mark_async(0);
433 mark_async(1);
434 mark_async(rem);
435 init_0();
436 init_1();
437 init_rd_rem();
438 init_wr_rem();
440 for (;;)
442 ASIO_FD_ZERO(&fd_set);
443 fw.fw_flags= 0;
444 fw.fw_bits= fd_set.afds_bits;
445 fw.fw_maxfd= ASIO_FD_SETSIZE;
447 if (more2read_0 && !inprogress_0)
449 restart_0();
450 fw.fw_flags |= FWF_NONBLOCK;
453 if (more2write_1 && !inprogress_1)
455 restart_1();
456 fw.fw_flags |= FWF_NONBLOCK;
459 if (more2read_rem && !inprogress_rd_rem)
461 restart_rd_rem();
462 fw.fw_flags |= FWF_NONBLOCK;
465 if (more2write_rem && !inprogress_wr_rem)
467 restart_wr_rem();
468 fw.fw_flags |= FWF_NONBLOCK;
471 if (more2read_0 && inprogress_0)
472 ASIO_FD_SET(0, ASIO_READ, &fd_set);
473 if (more2write_1 && inprogress_1)
474 ASIO_FD_SET(1, ASIO_WRITE, &fd_set);
475 if (more2read_rem && inprogress_rd_rem)
476 ASIO_FD_SET(rem, ASIO_READ, &fd_set);
477 if (more2write_rem && inprogress_wr_rem)
478 ASIO_FD_SET(rem, ASIO_WRITE, &fd_set);
480 for (;;)
482 result= fwait(&fw);
483 if (result == -1 && (errno == EAGAIN ||
484 errno == EINTR))
486 break;
488 if (result == -1)
490 fprintf(stderr, "fwait failed (%s)\n",
491 strerror(errno));
492 exit(1);
494 assert(result == 0);
495 #if 0
496 printf("fwait: fw_fw= %d, fw_operation= %d, fw_result= %d, fw.fw_errno= %d\n",
497 fw.fw_fd, fw.fw_operation, fw.fw_result, fw.fw_errno);
498 #endif
499 if (fw.fw_fd == 0 && fw.fw_operation == ASIO_READ)
501 completed_0(fw.fw_result, fw.fw_errno);
503 else if (fw.fw_fd == 1 &&
504 fw.fw_operation == ASIO_WRITE)
506 completed_1(fw.fw_result, fw.fw_errno);
508 else if (fw.fw_fd == rem &&
509 fw.fw_operation == ASIO_READ)
511 completed_rd_rem(fw.fw_result, fw.fw_errno);
513 else if (fw.fw_fd == rem &&
514 fw.fw_operation == ASIO_WRITE)
516 completed_wr_rem(fw.fw_result, fw.fw_errno);
518 else
520 fprintf(stderr,
521 "strange result from fwait: fd= %d, operation= %d\n",
522 fw.fw_fd, fw.fw_operation);
523 exit(1);
525 if (!(fw.fw_flags & FWF_MORE))
526 break;
528 if (mustsendwindow)
530 mustsendwindow= 0;
531 sendwindow();
534 #endif /* __minix_vmd */
535 msg("connection closed.");
536 done(0);
539 /* trap a signal, unless it is being ignored. */
540 static void
541 setsignal(sig, act)
542 int sig;
543 void(*act) ( int sig );
545 if (signal(sig, act) == SIG_IGN)
546 (void)signal(sig, SIG_IGN);
549 static void
550 done(status)
551 int status;
553 int w, wstatus;
555 mode(0);
556 #if !__minix_vmd
557 if (child > 0) {
558 /* make sure catch_child does not snap it up */
559 (void)signal(SIGCHLD, SIG_DFL);
560 if (kill(child, SIGKILL) >= 0)
561 while ((w = wait(&wstatus)) > 0 && w != child);
563 #endif
564 exit(status);
567 int dosigwinch;
568 #if !__minix
569 void sigwinch();
570 #endif
572 #if !__minix_vmd
573 static void
574 catch_child(sig)
575 int sig;
577 int status;
578 int pid;
580 for (;;) {
581 pid = waitpid(-1, &status, WNOHANG|WUNTRACED);
582 if (pid == 0)
583 return;
584 /* if the child (reader) dies, just quit */
585 if (pid < 0 || pid == child && !WIFSTOPPED(status))
586 done(WTERMSIG(status) | WEXITSTATUS(status));
588 /* NOTREACHED */
590 #endif
592 #if !__minix_vmd
594 * writer: write to remote: 0 -> line.
595 * ~. terminate
596 * ~^Z suspend rlogin process.
597 * ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
599 static void
600 writer()
602 register int bol, local, n;
603 u_char ch;
604 int c;
606 bol = 1; /* beginning of line */
607 local = 0;
608 for (;;) {
609 n = read(STDIN_FILENO, &ch, 1);
610 if (n <= 0) {
611 if (n < 0 && errno == EINTR)
612 continue;
613 break;
615 c = ch;
617 * If we're at the beginning of the line and recognize a
618 * command character, then we echo locally. Otherwise,
619 * characters are echo'd remotely. If the command character
620 * is doubled, this acts as a force and local echo is
621 * suppressed.
623 if (bol) {
624 bol = 0;
625 if (!noescape && c == escapechar) {
626 local = 1;
627 continue;
629 } else if (local) {
630 local = 0;
631 if (c == '.' || c == defattr.c_cc[VEOF]) {
632 echo(c);
633 break;
635 #if !__minix
636 if (c == defattr.c_cc[VSUSP]) {
637 bol = 1;
638 echo(c);
639 stop(c);
640 continue;
642 #endif
643 if (c != escapechar)
644 #ifdef CRYPT
645 #ifdef KERBEROS
646 if (doencrypt)
647 (void)des_write(rem, &escapechar, 1);
648 else
649 #endif
650 #endif
651 (void)write(rem, &escapechar, 1);
654 ch = c;
655 #ifdef CRYPT
656 #ifdef KERBEROS
657 if (doencrypt) {
658 if (des_write(rem, &ch, 1) == 0) {
659 msg("line gone");
660 break;
662 } else
663 #endif
664 #endif
665 if (write(rem, &ch, 1) == 0) {
666 msg("line gone");
667 break;
669 bol = c == defattr.c_cc[VKILL] ||
670 c == defattr.c_cc[VEOF] ||
671 c == defattr.c_cc[VINTR] ||
672 c == defattr.c_cc[VSUSP] ||
673 c == '\r' || c == '\n';
676 #endif
678 #if !__minix_vmd
679 static void
680 echo(c)
681 int c;
683 register char *p;
684 char buf[8];
686 p = buf;
687 c &= 0177;
688 *p++ = escapechar;
689 if (c < ' ') {
690 *p++ = '^';
691 *p++ = c + '@';
692 } else if (c == 0177) {
693 *p++ = '^';
694 *p++ = '?';
695 } else
696 *p++ = c;
697 *p++ = '\r';
698 *p++ = '\n';
699 (void)write(STDOUT_FILENO, buf, p - buf);
701 #endif
703 #if !__minix
704 stop(cmdc)
705 char cmdc;
707 mode(0);
708 (void)signal(SIGCHLD, SIG_IGN);
709 (void)kill(cmdc == defltc.t_suspc ? 0 : getpid(), SIGTSTP);
710 (void)signal(SIGCHLD, catch_child);
711 mode(1);
712 sigwinch(); /* check for size changes */
714 #endif
716 #if __minix_vmd
717 #ifdef SIGWINCH
718 static void
719 sigwinch(sig)
720 int sig;
722 struct winsize ws;
724 #if __minix
725 signal(SIGWINCH, sigwinch);
726 #endif
728 if (dosigwinch && get_window_size(0, &ws) == 0 &&
729 memcmp(&ws, &winsize, sizeof(ws))) {
730 winsize = ws;
731 mustsendwindow= 1;
736 * Send the window size to the server via the magic escape
738 static void
739 sendwindow()
741 struct winsize *wp;
742 char *obuf, *new_buf;
744 new_buf= realloc(extra_wr_rem_new,
745 extra_wr_rem_new_size+4+sizeof(*wp));
746 if (new_buf == 0)
747 return;
748 extra_wr_rem_new= new_buf;
749 obuf= new_buf+extra_wr_rem_new_size;
750 extra_wr_rem_new_size += 4+sizeof(*wp);
752 more2read_0= 0;
753 more2write_rem= 1;
755 wp = (struct winsize *)(obuf+4);
756 obuf[0] = 0377;
757 obuf[1] = 0377;
758 obuf[2] = 's';
759 obuf[3] = 's';
760 wp->ws_row = htons(winsize.ws_row);
761 wp->ws_col = htons(winsize.ws_col);
762 wp->ws_xpixel = htons(winsize.ws_xpixel);
763 wp->ws_ypixel = htons(winsize.ws_ypixel);
765 #endif /* SIGWINCH */
766 #endif
768 #if !__minix_vmd
770 * reader: read from remote: line -> 1
772 #define READING 1
773 #define WRITING 2
775 int rcvcnt, rcvstate;
776 char rcvbuf[8 * 1024];
778 static int
779 reader()
781 int pid = -getpid();
782 int n, remaining;
783 char *bufp = rcvbuf;
785 for (;;) {
786 while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
787 rcvstate = WRITING;
788 n = write(STDOUT_FILENO, bufp, remaining);
789 if (n < 0) {
790 if (errno != EINTR)
791 return(-1);
792 continue;
794 bufp += n;
796 bufp = rcvbuf;
797 rcvcnt = 0;
798 rcvstate = READING;
800 #ifdef CRYPT
801 #ifdef KERBEROS
802 if (doencrypt)
803 rcvcnt = des_read(rem, rcvbuf, sizeof(rcvbuf));
804 else
805 #endif
806 #endif
807 rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
808 if (rcvcnt == 0)
809 return (0);
810 if (rcvcnt < 0) {
811 if (errno == EINTR)
812 continue;
813 if (errno == EURG) {
814 nwio_tcpopt_t tcpopt;
815 #if DEBUG
816 fprintf(stderr, "\n\rEURG\n\r");
817 #endif
818 tcpopt.nwto_flags= NWTO_RCV_URG;
819 if (ioctl(rem, NWIOSTCPOPT, &tcpopt) == -1) {
820 fprintf(stderr,
821 "rlogin: trouble with urgent data: %s\n",
822 strerror(errno));
823 return(-1);
825 continue;
827 if (errno == ENOURG) {
828 nwio_tcpopt_t tcpopt;
829 #if DEBUG
830 fprintf(stderr, "\n\rENOURG\n\r");
831 #endif
832 tcpopt.nwto_flags= NWTO_RCV_NOTURG;
833 if (ioctl(rem, NWIOSTCPOPT, &tcpopt) == -1) {
834 fprintf(stderr,
835 "rlogin: trouble with not-urgent data: %s\n",
836 strerror(errno));
837 return(-1);
839 continue;
841 (void)fprintf(stderr, "rlogin: read: %s\n",
842 strerror(errno));
843 return(-1);
847 #endif /* !__minix_vmd */
849 static void
850 mode(f)
851 int f;
853 struct termios *sb;
855 switch(f) {
856 case 0:
857 sb= &defattr;
858 break;
859 case 1:
860 sb= &rawattr;
861 break;
862 default:
863 return;
865 (void)tcsetattr(0, TCSAFLUSH, sb);
868 static void
869 lostpeer(sig)
870 int sig;
872 (void)signal(SIGPIPE, SIG_IGN);
873 msg("\007connection closed.");
874 done(1);
877 static void
878 msg(str)
879 char *str;
881 (void)fprintf(stderr, "rlogin: %s\r\n", str);
884 #ifdef KERBEROS
885 /* VARARGS */
886 warning(va_alist)
887 va_dcl
889 va_list ap;
890 char *fmt;
892 (void)fprintf(stderr, "rlogin: warning, using standard rlogin: ");
893 va_start(ap);
894 fmt = va_arg(ap, char *);
895 vfprintf(stderr, fmt, ap);
896 va_end(ap);
897 (void)fprintf(stderr, ".\n");
899 #endif
901 static void
902 usage()
904 (void)fprintf(stderr,
905 "Usage: rlogin [-%s]%s[-e char] [-l username] host\n",
906 #ifdef KERBEROS
907 #ifdef CRYPT
908 "8ELx", " [-k realm] ");
909 #else
910 "8EL", " [-k realm] ");
911 #endif
912 #else
913 "8EL", " ");
914 #endif
915 exit(1);
919 * The following routine provides compatibility (such as it is) between 4.2BSD
920 * Suns and others. Suns have only a `ttysize', so we convert it to a winsize.
922 #ifdef sun
923 get_window_size(fd, wp)
924 int fd;
925 struct winsize *wp;
927 struct ttysize ts;
928 int error;
930 if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
931 return(error);
932 wp->ws_row = ts.ts_lines;
933 wp->ws_col = ts.ts_cols;
934 wp->ws_xpixel = 0;
935 wp->ws_ypixel = 0;
936 return(0);
938 #endif
940 static u_char
941 getescape(p)
942 register char *p;
944 long val;
945 int len;
947 if ((len = strlen(p)) == 1) /* use any single char, including '\' */
948 return((u_char)*p);
949 /* otherwise, \nnn */
950 if (*p == '\\' && len >= 2 && len <= 4) {
951 val = strtol(++p, (char **)NULL, 8);
952 for (;;) {
953 if (!*++p)
954 return((u_char)val);
955 if (*p < '0' || *p > '8')
956 break;
959 msg("illegal option value -- e");
960 usage();
961 /* NOTREACHED */
964 static char *
965 speeds2str(speed)
966 speed_t speed;
968 int i;
969 for (i= 0; speeds[i].name != NULL && speeds[i].speed != speed; i++) {
970 if (speeds[i].speed == speed) return speeds[i].name;
972 return "unknown";
975 #if __minix_vmd
976 static void
977 mark_async(fd)
978 int fd;
980 int result;
981 int v;
983 result= fcntl(fd, F_GETFD);
984 if (result == -1)
986 fprintf(stderr,
987 "rlogin: mark_async: fcntl(%d, GETFD) failed (%s)\n",
988 fd, strerror(errno));
989 exit(1);
991 v= result | FD_ASYNCHIO;
992 result= fcntl(fd, F_SETFD, v);
993 if (result == -1)
995 fprintf(stderr,
996 "rlogin: mark_async: fcntl(%d, SETFD, %d) failed (%s)\n",
997 fd, v, strerror(errno));
998 exit(1);
1002 #define RD_0_BUFSIZE 256
1003 char rd_0_buf[RD_0_BUFSIZE];
1004 size_t rd_0_offset;
1006 static void
1007 init_0()
1009 more2read_0= 1;
1010 inprogress_0= 0;
1011 rd_0_offset= 0;
1014 size_t wr_1_size;
1015 size_t wr_1_offset;
1016 char *urg_1;
1017 size_t urg_1_size;
1018 char *extra_1;
1019 size_t extra_1_size;
1020 size_t extra_1_offset;
1021 char *extra_1_new;
1022 size_t extra_1_new_size;
1023 #define MAX_EXTRA_1_NEW_SIZE (16*1024)
1025 static void
1026 init_1()
1028 more2write_1= 0;
1029 inprogress_1= 0;
1030 wr_1_size= 0;
1031 wr_1_offset= 0;
1032 urg_1= NULL;
1033 urg_1_size= 0;
1034 extra_1= NULL;
1035 extra_1_size= 0;
1036 extra_1_offset= 0;
1037 extra_1_new= NULL;
1038 extra_1_new_size= 0;
1041 #define RD_REM_BUFSIZE (8*1024)
1042 char rd_rem_buf[RD_REM_BUFSIZE];
1043 size_t rd_rem_offset;
1044 int rd_rem_urg;
1046 static void
1047 init_rd_rem()
1049 more2read_rem= 1;
1050 inprogress_rd_rem= 0;
1051 rd_rem_offset= 0;
1052 rd_rem_urg= 0;
1055 static void
1056 init_wr_rem()
1058 more2write_rem= 0;
1059 inprogress_wr_rem= 0;
1060 wr_rem_size= 0;
1061 wr_rem_offset= 0;
1062 extra_wr_rem_size= 0;
1063 extra_wr_rem_offset= 0;
1064 extra_wr_rem= NULL;
1065 extra_wr_rem_new_size= 0;
1066 extra_wr_rem_new= NULL;
1069 static void
1070 restart_0()
1072 size_t offset;
1073 int result, error;
1075 assert(!inprogress_0);
1076 rd_0_offset= 1;
1077 offset= 0;
1078 while (offset < RD_0_BUFSIZE)
1080 result= read(0, rd_0_buf+rd_0_offset+offset,
1081 RD_0_BUFSIZE-rd_0_offset-offset);
1082 if (result > 0)
1084 offset += result;
1085 assert(rd_0_offset+offset <= RD_0_BUFSIZE);
1086 continue;
1088 error= errno;
1090 if (offset != 0)
1091 completed_0(offset, 0);
1092 rd_0_offset += offset;
1093 if (result == -1 && error == EINPROGRESS)
1095 inprogress_0= 1;
1096 return;
1098 completed_0(result, error);
1099 return;
1101 completed_0(offset, 0);
1104 static void
1105 restart_1()
1107 size_t offset;
1108 int result, error;
1110 assert(!inprogress_1);
1112 while (extra_1 != NULL || extra_1_new != NULL)
1114 if (extra_1 == NULL)
1116 extra_1= extra_1_new;
1117 extra_1_new= NULL;
1118 extra_1_size= extra_1_new_size;
1119 extra_1_new_size= 0;
1120 extra_1_offset= 0;
1122 offset= 0;
1123 #if DEBUG
1124 if (extra_1_size == 0)
1125 fprintf(stderr, "restart_1: extra_1_size= 0\n");
1126 #endif
1127 while (offset < extra_1_size)
1129 result= write(1, extra_1+extra_1_offset+offset,
1130 extra_1_size-offset);
1131 if (result > 0)
1133 assert (result <= extra_1_size-offset);
1134 offset += result;
1135 continue;
1137 error= errno;
1138 if (offset != 0)
1139 completed_1(offset, 0);
1141 if (result == -1 && errno == EINPROGRESS)
1143 inprogress_1= 1;
1144 return;
1146 completed_1(result, errno);
1147 return;
1149 completed_1(offset, 0);
1152 offset= 0;
1154 if (wr_1_size == 0)
1156 more2write_1= 0;
1157 more2read_rem= 1;
1158 return;
1161 while (offset < wr_1_size)
1163 result= write(1, rd_rem_buf+wr_1_offset+offset,
1164 wr_1_size-offset);
1165 if (result > 0)
1167 assert (result <= wr_1_size-offset);
1168 offset += result;
1169 continue;
1171 error= errno;
1172 if (offset != 0)
1173 completed_1(offset, 0);
1175 if (result == -1 && errno == EINPROGRESS)
1177 inprogress_1= 1;
1178 return;
1180 completed_1(result, errno);
1181 return;
1183 completed_1(offset, 0);
1186 static void
1187 restart_rd_rem()
1189 size_t offset;
1190 int result, error;
1192 assert(!inprogress_rd_rem);
1193 rd_rem_offset= 0;
1194 offset= 0;
1195 while (offset < RD_REM_BUFSIZE)
1197 result= read(rem, rd_rem_buf+offset, RD_REM_BUFSIZE-offset);
1198 if (result > 0)
1200 offset += result;
1201 assert(offset <= RD_REM_BUFSIZE);
1202 continue;
1204 error= errno;
1206 if (offset != 0)
1207 completed_rd_rem(offset, 0);
1208 rd_rem_offset= offset;
1209 if (result == -1 && error == EINPROGRESS)
1211 inprogress_rd_rem= 1;
1212 return;
1214 completed_rd_rem(result, error);
1215 return;
1217 completed_rd_rem(offset, 0);
1220 static void
1221 restart_wr_rem()
1223 size_t offset;
1224 int result, error;
1226 assert(!inprogress_wr_rem);
1228 if (extra_wr_rem_new != NULL && extra_wr_rem == NULL)
1230 extra_wr_rem= extra_wr_rem_new;
1231 extra_wr_rem_size= extra_wr_rem_new_size;
1232 extra_wr_rem_offset= 0;
1233 extra_wr_rem_new= NULL;
1234 extra_wr_rem_new_size= 0;
1236 if (extra_wr_rem != NULL)
1238 offset= 0;
1239 while (offset < extra_wr_rem_size)
1241 result= write(rem,
1242 extra_wr_rem+extra_wr_rem_offset+offset,
1243 extra_wr_rem_size-offset);
1244 if (result > 0)
1246 assert (result <= extra_wr_rem_size-offset);
1247 offset += result;
1248 continue;
1250 error= errno;
1251 if (offset != 0)
1252 completed_wr_rem(offset, 0);
1254 if (result == -1 && errno == EINPROGRESS)
1256 inprogress_wr_rem= 1;
1257 return;
1259 completed_wr_rem(result, errno);
1260 return;
1262 completed_wr_rem(offset, 0);
1264 if (wr_rem_size == 0)
1265 return;
1267 offset= 0;
1268 while (offset < wr_rem_size)
1270 result= write(rem, rd_0_buf+wr_rem_offset+offset,
1271 wr_rem_size-offset);
1272 if (result > 0)
1274 assert (result <= wr_rem_size-offset);
1275 offset += result;
1276 continue;
1278 error= errno;
1279 if (offset != 0)
1280 completed_wr_rem(offset, 0);
1282 if (result == -1 && errno == EINPROGRESS)
1284 inprogress_wr_rem= 1;
1285 return;
1287 completed_wr_rem(result, errno);
1288 return;
1290 completed_wr_rem(offset, 0);
1293 static void
1294 completed_0(result, error)
1295 int result;
1296 int error;
1298 static int bol= 0, local= 0;
1300 char *iptr, *optr;
1301 int i;
1302 u_char c;
1304 inprogress_0= 0;
1306 if (result > 0)
1308 assert(rd_0_offset > 0);
1309 wr_rem_offset= 1;
1311 iptr= rd_0_buf+rd_0_offset;
1312 optr= rd_0_buf+wr_rem_offset;
1313 for (i= 0; i<result; iptr++, i++)
1315 c= *iptr;
1316 if (bol)
1318 bol= 0;
1319 if (!noescape && c == escapechar)
1321 local= 1;
1322 continue;
1325 else if (local)
1327 local= 0;
1328 if (c == '.' || (c != _POSIX_VDISABLE &&
1329 c == defattr.c_cc[VEOF]))
1331 echo(c);
1332 finish();
1333 /* NOTREACHED */
1335 if (c == '!')
1337 subshell();
1338 continue;
1340 if (c != escapechar)
1342 if (optr < iptr)
1344 *(optr++)= escapechar;
1346 else
1348 assert(optr == iptr);
1349 assert(iptr == rd_0_buf+
1350 rd_0_offset);
1351 assert(rd_0_offset > 0);
1352 wr_rem_offset--;
1353 optr[-1]= escapechar;
1357 *(optr++)= c;
1358 bol= (c != _POSIX_VDISABLE) &&
1359 (c == defattr.c_cc[VKILL] ||
1360 c == defattr.c_cc[VEOF] ||
1361 c == defattr.c_cc[VINTR] ||
1362 c == defattr.c_cc[VSUSP] ||
1363 c == '\r' || c == '\n');
1365 wr_rem_size += optr-rd_0_buf-wr_rem_offset;
1366 if (wr_rem_size != 0)
1368 more2read_0= 0;
1369 more2write_rem= 1;
1371 return;
1372 } else
1373 if (result < 0) {
1374 fprintf(stderr, "rlogin: %s\n", strerror(error));
1376 done(1);
1379 static void
1380 completed_1(result, error)
1381 int result;
1382 int error;
1384 inprogress_1= 0;
1386 if (result > 0)
1388 if (extra_1 != NULL)
1390 assert (result <= extra_1_size);
1391 extra_1_size -= result;
1392 extra_1_offset += result;
1393 if (extra_1_size == 0)
1395 more2write_1= 0;
1396 more2read_rem= 1;
1397 free(extra_1);
1398 extra_1= NULL;
1400 return;
1402 assert (result <= wr_1_size);
1403 wr_1_size -= result;
1404 wr_1_offset += result;
1405 if (wr_1_size == 0)
1407 more2write_1= 0;
1408 more2read_rem= 1;
1410 return;
1411 } else
1412 if (result < 0) {
1413 fprintf(stderr, "rlogin: %s\n", strerror(error));
1415 done(1);
1418 static void
1419 completed_rd_rem(result, error)
1420 int result;
1421 int error;
1423 nwio_tcpopt_t tcpopt;
1424 char *new_buf;
1425 size_t keep_size;
1426 u_char urg_byte;
1427 int i;
1429 inprogress_rd_rem= 0;
1431 if (result > 0)
1433 if (rd_rem_urg)
1435 #if DEBUG
1436 fprintf(stderr, "\n\r%d urg bytes\n\r", result);
1437 #endif
1438 if (urg_1_size > MAX_EXTRA_1_NEW_SIZE)
1440 keep_size= MAX_EXTRA_1_NEW_SIZE/2;
1441 memmove(urg_1, urg_1+urg_1_size-keep_size,
1442 keep_size);
1443 urg_1_size= keep_size;
1445 new_buf= realloc(urg_1, urg_1_size+result);
1446 if (new_buf == NULL)
1448 fprintf(stderr,
1449 "rlogin: warning realloc %d failed\n",
1450 urg_1_size+result);
1451 return;
1453 memcpy(new_buf+urg_1_size,
1454 rd_rem_buf+rd_rem_offset, result);
1455 urg_1= new_buf;
1456 urg_1_size += result;
1457 return;
1459 more2read_rem= 0;
1460 more2write_1= 1;
1461 wr_1_size= result;
1462 wr_1_offset= rd_rem_offset;
1463 return;
1465 if (result == -1 && error == EURG)
1467 #if DEBUG
1468 fprintf(stderr, "\n\rEURG\n\r");
1469 #endif
1470 rd_rem_urg= 1;
1471 tcpopt.nwto_flags= NWTO_RCV_URG;
1472 result= ioctl(rem, NWIOSTCPOPT, &tcpopt);
1473 if (result == -1)
1475 fprintf(stderr,
1476 "rlogin: NWIOSTCPOPT on %d failed (%s)\n",
1477 rem, strerror(errno));
1478 exit(1);
1480 return;
1482 if (result == -1 && error == ENOURG)
1484 #if DEBUG
1485 fprintf(stderr, "\n\rENOURG\n\r");
1486 #endif
1487 rd_rem_urg= 0;
1488 tcpopt.nwto_flags= NWTO_RCV_NOTURG;
1489 result= ioctl(rem, NWIOSTCPOPT, &tcpopt);
1490 if (result == -1)
1492 fprintf(stderr,
1493 "rlogin: NWIOSTCPOPT on %d failed (%s)\n",
1494 rem, strerror(errno));
1495 exit(1);
1497 if (urg_1_size != 0)
1499 urg_byte= urg_1[urg_1_size-1];
1500 urg_1_size--;
1501 do_urg(urg_byte);
1502 if (urg_1_size == 0)
1503 return;
1504 if (extra_1_new_size + urg_1_size > MAX_EXTRA_1_NEW_SIZE)
1506 extra_1_new_size= 0;
1507 free(extra_1_new);
1508 extra_1_new= NULL;
1510 if (extra_1_new_size != 0)
1512 new_buf= realloc(extra_1_new,
1513 extra_1_new_size+urg_1_size);
1514 if (new_buf == 0)
1516 extra_1_new_size= 0;
1517 free(extra_1_new);
1518 extra_1_new= NULL;
1520 else
1522 extra_1_new= new_buf;
1523 memcpy(extra_1_new+extra_1_new_size,
1524 urg_1, urg_1_size);
1525 extra_1_new_size += urg_1_size;
1526 urg_1_size= 0;
1527 free(urg_1);
1528 urg_1= NULL;
1531 if (extra_1_new_size == 0)
1533 extra_1_new_size= urg_1_size;
1534 extra_1_new= urg_1;
1535 urg_1_size= 0;
1536 urg_1= NULL;
1538 more2read_rem= 0;
1539 more2write_1= 1;
1541 return;
1543 if (result == -1 && error == EINTR)
1545 /* Never mind. */
1546 return;
1548 if (result == 0)
1550 msg("connection closed.");
1551 done(0);
1553 if (result < 0) {
1554 fprintf(stderr, "rlogin: %s\n", strerror(error));
1556 done(1);
1559 static void
1560 completed_wr_rem(result, error)
1561 int result;
1562 int error;
1564 inprogress_wr_rem= 0;
1566 if (result > 0)
1568 if (extra_wr_rem != NULL)
1570 assert (result <= extra_wr_rem_size);
1571 extra_wr_rem_size -= result;
1572 extra_wr_rem_offset += result;
1573 if (extra_wr_rem_size == 0)
1575 free(extra_wr_rem);
1576 extra_wr_rem= NULL;
1577 if (wr_rem_size == 0)
1579 more2write_rem= 0;
1580 more2read_0= 1;
1583 return;
1586 assert (result <= wr_rem_size);
1587 wr_rem_size -= result;
1588 wr_rem_offset += result;
1589 if (wr_rem_size == 0)
1591 more2write_rem= 0;
1592 more2read_0= 1;
1594 return;
1596 if (result < 0) {
1597 fprintf(stderr, "rlogin: %s\n", strerror(error));
1599 done(1);
1602 static void
1603 do_urg(urg_byte)
1604 int urg_byte;
1606 #if DEBUG
1607 fprintf(stderr, "rlogin: warning got urg_byte 0x%x\r\n", urg_byte);
1608 #endif
1609 if (urg_byte & TIOCPKT_WINDOW)
1611 if (dosigwinch == 0)
1613 sendwindow();
1614 signal(SIGWINCH, sigwinch);
1616 dosigwinch= 1;
1620 static void
1621 echo(c)
1622 int c;
1624 u_char c1;
1625 char *new_buf;
1627 new_buf= realloc(extra_1_new, extra_1_new_size+6);
1628 if (new_buf == NULL)
1629 return;
1630 extra_1_new= new_buf;
1631 new_buf= extra_1_new+extra_1_new_size;
1633 c1= escapechar;
1634 if (c1 < ' ')
1636 *new_buf++= '^';
1637 *new_buf++= c1 + '@';
1639 else if (c1 == 0x7f)
1641 *new_buf++= '^';
1642 *new_buf++= '?';
1644 else
1645 *new_buf++= c1;
1647 if (c < ' ')
1649 *new_buf++= '^';
1650 *new_buf++= c + '@';
1652 else if (c == 0x7f)
1654 *new_buf++= '^';
1655 *new_buf++= '?';
1657 else
1658 *new_buf++= c;
1660 *new_buf++= '\r';
1661 *new_buf++= '\n';
1662 extra_1_new_size= new_buf-extra_1_new;
1663 more2write_1= 1;
1666 static void
1667 finish()
1669 done(0);
1672 static char cmdbuf[256];
1674 static void
1675 subshell()
1677 /* Start a subshell. Based on the first character of the command,
1678 * the tcp connection will be present at fd 3 ('+'), or at
1679 * fd 0 and fd 1 ('=')
1681 int r, pid, stat, len;
1682 char *shell, *cmd;
1684 /* cancel the reads and writes that are in progress. */
1685 if (inprogress_0)
1687 r= fcancel(0, ASIO_READ);
1688 if (r != 0) abort();
1690 if (inprogress_1)
1692 r= fcancel(1, ASIO_WRITE);
1693 if (r != 0) abort();
1695 if (inprogress_rd_rem)
1697 r= fcancel(rem, ASIO_READ);
1698 if (r != 0) abort();
1700 if (inprogress_wr_rem)
1702 r= fcancel(rem, ASIO_WRITE);
1703 if (r != 0) abort();
1706 mode(0);
1708 pid= fork();
1709 if (pid == -1) abort();
1710 if (pid != 0)
1712 r= waitpid(pid, &stat, 0);
1713 if (r != pid) abort();
1715 #if DEBUG
1716 fprintf(stderr, "stat: 0x%x\n", stat);
1717 #endif
1718 mode(1);
1719 return;
1722 (void)signal(SIGINT, SIG_DFL);
1724 shell= getenv("SHELL");
1725 if (shell == NULL)
1726 shell= "/bin/sh";
1727 printf("~!\ncommand [%s]: ", shell);
1728 cmd= fgets(cmdbuf, sizeof(cmdbuf), stdin);
1729 if (cmd == NULL)
1730 exit(0);
1731 #if DEBUG
1732 printf("got command '%s'\n", cmd);
1733 #endif
1735 /* Strip the trailing newline */
1736 len= strlen(cmd);
1737 if (len > 0 && cmd[len-1] == '\n')
1738 cmd[len-1]= '\0';
1739 else
1740 printf("\n");
1742 /* Skip leading white space */
1743 while (*cmd != '\0' && isspace(*cmd))
1744 cmd++;
1746 if (*cmd == '+')
1748 if (rem != 3)
1750 dup2(rem, 3);
1751 close(rem);
1753 cmd++;
1755 else if (*cmd == '=')
1757 dup2(rem, 0);
1758 dup2(rem, 1);
1759 close(rem);
1760 cmd++;
1762 else
1763 close(rem);
1764 if (*cmd == '\0')
1766 r= execl(shell, shell, NULL);
1768 else
1770 r= execl("/bin/sh", "sh", "-c", cmd, NULL);
1772 printf("exec failed: %d, %d\n", r, errno);
1773 exit(0);
1775 #endif /* __minix_vmd */