tools/llvm: Do not build with symbols
[minix3.git] / minix / commands / rlogin / rlogin.c
blob90839c49cdef10b327999f3e149a27b977190453
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 <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 ) __attribute__((noreturn));
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 n, remaining;
782 char *bufp = rcvbuf;
784 for (;;) {
785 while ((remaining = rcvcnt - (bufp - rcvbuf)) > 0) {
786 rcvstate = WRITING;
787 n = write(STDOUT_FILENO, bufp, remaining);
788 if (n < 0) {
789 if (errno != EINTR)
790 return(-1);
791 continue;
793 bufp += n;
795 bufp = rcvbuf;
796 rcvcnt = 0;
797 rcvstate = READING;
799 #ifdef CRYPT
800 #ifdef KERBEROS
801 if (doencrypt)
802 rcvcnt = des_read(rem, rcvbuf, sizeof(rcvbuf));
803 else
804 #endif
805 #endif
806 rcvcnt = read(rem, rcvbuf, sizeof (rcvbuf));
807 if (rcvcnt == 0)
808 return (0);
809 if (rcvcnt < 0) {
810 if (errno == EINTR)
811 continue;
812 if (errno == EURG) {
813 nwio_tcpopt_t tcpopt;
814 #if DEBUG
815 fprintf(stderr, "\n\rEURG\n\r");
816 #endif
817 tcpopt.nwto_flags= NWTO_RCV_URG;
818 if (ioctl(rem, NWIOSTCPOPT, &tcpopt) == -1) {
819 fprintf(stderr,
820 "rlogin: trouble with urgent data: %s\n",
821 strerror(errno));
822 return(-1);
824 continue;
826 if (errno == ENOURG) {
827 nwio_tcpopt_t tcpopt;
828 #if DEBUG
829 fprintf(stderr, "\n\rENOURG\n\r");
830 #endif
831 tcpopt.nwto_flags= NWTO_RCV_NOTURG;
832 if (ioctl(rem, NWIOSTCPOPT, &tcpopt) == -1) {
833 fprintf(stderr,
834 "rlogin: trouble with not-urgent data: %s\n",
835 strerror(errno));
836 return(-1);
838 continue;
840 (void)fprintf(stderr, "rlogin: read: %s\n",
841 strerror(errno));
842 return(-1);
846 #endif /* !__minix_vmd */
848 static void
849 mode(f)
850 int f;
852 struct termios *sb;
854 switch(f) {
855 case 0:
856 sb= &defattr;
857 break;
858 case 1:
859 sb= &rawattr;
860 break;
861 default:
862 return;
864 (void)tcsetattr(0, TCSAFLUSH, sb);
867 static void
868 lostpeer(sig)
869 int sig;
871 (void)signal(SIGPIPE, SIG_IGN);
872 msg("\007connection closed.");
873 done(1);
876 static void
877 msg(str)
878 char *str;
880 (void)fprintf(stderr, "rlogin: %s\r\n", str);
883 #ifdef KERBEROS
884 /* VARARGS */
885 warning(va_alist)
886 va_dcl
888 va_list ap;
889 char *fmt;
891 (void)fprintf(stderr, "rlogin: warning, using standard rlogin: ");
892 va_start(ap);
893 fmt = va_arg(ap, char *);
894 vfprintf(stderr, fmt, ap);
895 va_end(ap);
896 (void)fprintf(stderr, ".\n");
898 #endif
900 static void
901 usage()
903 (void)fprintf(stderr,
904 "Usage: rlogin [-%s]%s[-e char] [-l username] host\n",
905 #ifdef KERBEROS
906 #ifdef CRYPT
907 "8ELx", " [-k realm] ");
908 #else
909 "8EL", " [-k realm] ");
910 #endif
911 #else
912 "8EL", " ");
913 #endif
914 exit(1);
918 * The following routine provides compatibility (such as it is) between 4.2BSD
919 * Suns and others. Suns have only a `ttysize', so we convert it to a winsize.
921 #ifdef sun
922 get_window_size(fd, wp)
923 int fd;
924 struct winsize *wp;
926 struct ttysize ts;
927 int error;
929 if ((error = ioctl(0, TIOCGSIZE, &ts)) != 0)
930 return(error);
931 wp->ws_row = ts.ts_lines;
932 wp->ws_col = ts.ts_cols;
933 wp->ws_xpixel = 0;
934 wp->ws_ypixel = 0;
935 return(0);
937 #endif
939 static u_char
940 getescape(p)
941 register char *p;
943 long val;
944 int len;
946 if ((len = strlen(p)) == 1) /* use any single char, including '\' */
947 return((u_char)*p);
948 /* otherwise, \nnn */
949 if (*p == '\\' && len >= 2 && len <= 4) {
950 val = strtol(++p, (char **)NULL, 8);
951 for (;;) {
952 if (!*++p)
953 return((u_char)val);
954 if (*p < '0' || *p > '8')
955 break;
958 msg("illegal option value -- e");
959 usage();
960 /* NOTREACHED */
963 static char *
964 speeds2str(speed)
965 speed_t speed;
967 int i;
968 for (i= 0; speeds[i].name != NULL && speeds[i].speed != speed; i++) {
969 if (speeds[i].speed == speed) return speeds[i].name;
971 return "unknown";
974 #if __minix_vmd
975 static void
976 mark_async(fd)
977 int fd;
979 int result;
980 int v;
982 result= fcntl(fd, F_GETFD);
983 if (result == -1)
985 fprintf(stderr,
986 "rlogin: mark_async: fcntl(%d, GETFD) failed (%s)\n",
987 fd, strerror(errno));
988 exit(1);
990 v= result | FD_ASYNCHIO;
991 result= fcntl(fd, F_SETFD, v);
992 if (result == -1)
994 fprintf(stderr,
995 "rlogin: mark_async: fcntl(%d, SETFD, %d) failed (%s)\n",
996 fd, v, strerror(errno));
997 exit(1);
1001 #define RD_0_BUFSIZE 256
1002 char rd_0_buf[RD_0_BUFSIZE];
1003 size_t rd_0_offset;
1005 static void
1006 init_0()
1008 more2read_0= 1;
1009 inprogress_0= 0;
1010 rd_0_offset= 0;
1013 size_t wr_1_size;
1014 size_t wr_1_offset;
1015 char *urg_1;
1016 size_t urg_1_size;
1017 char *extra_1;
1018 size_t extra_1_size;
1019 size_t extra_1_offset;
1020 char *extra_1_new;
1021 size_t extra_1_new_size;
1022 #define MAX_EXTRA_1_NEW_SIZE (16*1024)
1024 static void
1025 init_1()
1027 more2write_1= 0;
1028 inprogress_1= 0;
1029 wr_1_size= 0;
1030 wr_1_offset= 0;
1031 urg_1= NULL;
1032 urg_1_size= 0;
1033 extra_1= NULL;
1034 extra_1_size= 0;
1035 extra_1_offset= 0;
1036 extra_1_new= NULL;
1037 extra_1_new_size= 0;
1040 #define RD_REM_BUFSIZE (8*1024)
1041 char rd_rem_buf[RD_REM_BUFSIZE];
1042 size_t rd_rem_offset;
1043 int rd_rem_urg;
1045 static void
1046 init_rd_rem()
1048 more2read_rem= 1;
1049 inprogress_rd_rem= 0;
1050 rd_rem_offset= 0;
1051 rd_rem_urg= 0;
1054 static void
1055 init_wr_rem()
1057 more2write_rem= 0;
1058 inprogress_wr_rem= 0;
1059 wr_rem_size= 0;
1060 wr_rem_offset= 0;
1061 extra_wr_rem_size= 0;
1062 extra_wr_rem_offset= 0;
1063 extra_wr_rem= NULL;
1064 extra_wr_rem_new_size= 0;
1065 extra_wr_rem_new= NULL;
1068 static void
1069 restart_0()
1071 size_t offset;
1072 int result, error;
1074 assert(!inprogress_0);
1075 rd_0_offset= 1;
1076 offset= 0;
1077 while (offset < RD_0_BUFSIZE)
1079 result= read(0, rd_0_buf+rd_0_offset+offset,
1080 RD_0_BUFSIZE-rd_0_offset-offset);
1081 if (result > 0)
1083 offset += result;
1084 assert(rd_0_offset+offset <= RD_0_BUFSIZE);
1085 continue;
1087 error= errno;
1089 if (offset != 0)
1090 completed_0(offset, 0);
1091 rd_0_offset += offset;
1092 if (result == -1 && error == EINPROGRESS)
1094 inprogress_0= 1;
1095 return;
1097 completed_0(result, error);
1098 return;
1100 completed_0(offset, 0);
1103 static void
1104 restart_1()
1106 size_t offset;
1107 int result, error;
1109 assert(!inprogress_1);
1111 while (extra_1 != NULL || extra_1_new != NULL)
1113 if (extra_1 == NULL)
1115 extra_1= extra_1_new;
1116 extra_1_new= NULL;
1117 extra_1_size= extra_1_new_size;
1118 extra_1_new_size= 0;
1119 extra_1_offset= 0;
1121 offset= 0;
1122 #if DEBUG
1123 if (extra_1_size == 0)
1124 fprintf(stderr, "restart_1: extra_1_size= 0\n");
1125 #endif
1126 while (offset < extra_1_size)
1128 result= write(1, extra_1+extra_1_offset+offset,
1129 extra_1_size-offset);
1130 if (result > 0)
1132 assert (result <= extra_1_size-offset);
1133 offset += result;
1134 continue;
1136 error= errno;
1137 if (offset != 0)
1138 completed_1(offset, 0);
1140 if (result == -1 && errno == EINPROGRESS)
1142 inprogress_1= 1;
1143 return;
1145 completed_1(result, errno);
1146 return;
1148 completed_1(offset, 0);
1151 offset= 0;
1153 if (wr_1_size == 0)
1155 more2write_1= 0;
1156 more2read_rem= 1;
1157 return;
1160 while (offset < wr_1_size)
1162 result= write(1, rd_rem_buf+wr_1_offset+offset,
1163 wr_1_size-offset);
1164 if (result > 0)
1166 assert (result <= wr_1_size-offset);
1167 offset += result;
1168 continue;
1170 error= errno;
1171 if (offset != 0)
1172 completed_1(offset, 0);
1174 if (result == -1 && errno == EINPROGRESS)
1176 inprogress_1= 1;
1177 return;
1179 completed_1(result, errno);
1180 return;
1182 completed_1(offset, 0);
1185 static void
1186 restart_rd_rem()
1188 size_t offset;
1189 int result, error;
1191 assert(!inprogress_rd_rem);
1192 rd_rem_offset= 0;
1193 offset= 0;
1194 while (offset < RD_REM_BUFSIZE)
1196 result= read(rem, rd_rem_buf+offset, RD_REM_BUFSIZE-offset);
1197 if (result > 0)
1199 offset += result;
1200 assert(offset <= RD_REM_BUFSIZE);
1201 continue;
1203 error= errno;
1205 if (offset != 0)
1206 completed_rd_rem(offset, 0);
1207 rd_rem_offset= offset;
1208 if (result == -1 && error == EINPROGRESS)
1210 inprogress_rd_rem= 1;
1211 return;
1213 completed_rd_rem(result, error);
1214 return;
1216 completed_rd_rem(offset, 0);
1219 static void
1220 restart_wr_rem()
1222 size_t offset;
1223 int result, error;
1225 assert(!inprogress_wr_rem);
1227 if (extra_wr_rem_new != NULL && extra_wr_rem == NULL)
1229 extra_wr_rem= extra_wr_rem_new;
1230 extra_wr_rem_size= extra_wr_rem_new_size;
1231 extra_wr_rem_offset= 0;
1232 extra_wr_rem_new= NULL;
1233 extra_wr_rem_new_size= 0;
1235 if (extra_wr_rem != NULL)
1237 offset= 0;
1238 while (offset < extra_wr_rem_size)
1240 result= write(rem,
1241 extra_wr_rem+extra_wr_rem_offset+offset,
1242 extra_wr_rem_size-offset);
1243 if (result > 0)
1245 assert (result <= extra_wr_rem_size-offset);
1246 offset += result;
1247 continue;
1249 error= errno;
1250 if (offset != 0)
1251 completed_wr_rem(offset, 0);
1253 if (result == -1 && errno == EINPROGRESS)
1255 inprogress_wr_rem= 1;
1256 return;
1258 completed_wr_rem(result, errno);
1259 return;
1261 completed_wr_rem(offset, 0);
1263 if (wr_rem_size == 0)
1264 return;
1266 offset= 0;
1267 while (offset < wr_rem_size)
1269 result= write(rem, rd_0_buf+wr_rem_offset+offset,
1270 wr_rem_size-offset);
1271 if (result > 0)
1273 assert (result <= wr_rem_size-offset);
1274 offset += result;
1275 continue;
1277 error= errno;
1278 if (offset != 0)
1279 completed_wr_rem(offset, 0);
1281 if (result == -1 && errno == EINPROGRESS)
1283 inprogress_wr_rem= 1;
1284 return;
1286 completed_wr_rem(result, errno);
1287 return;
1289 completed_wr_rem(offset, 0);
1292 static void
1293 completed_0(result, error)
1294 int result;
1295 int error;
1297 static int bol= 0, local= 0;
1299 char *iptr, *optr;
1300 int i;
1301 u_char c;
1303 inprogress_0= 0;
1305 if (result > 0)
1307 assert(rd_0_offset > 0);
1308 wr_rem_offset= 1;
1310 iptr= rd_0_buf+rd_0_offset;
1311 optr= rd_0_buf+wr_rem_offset;
1312 for (i= 0; i<result; iptr++, i++)
1314 c= *iptr;
1315 if (bol)
1317 bol= 0;
1318 if (!noescape && c == escapechar)
1320 local= 1;
1321 continue;
1324 else if (local)
1326 local= 0;
1327 if (c == '.' || (c != _POSIX_VDISABLE &&
1328 c == defattr.c_cc[VEOF]))
1330 echo(c);
1331 finish();
1332 /* NOTREACHED */
1334 if (c == '!')
1336 subshell();
1337 continue;
1339 if (c != escapechar)
1341 if (optr < iptr)
1343 *(optr++)= escapechar;
1345 else
1347 assert(optr == iptr);
1348 assert(iptr == rd_0_buf+
1349 rd_0_offset);
1350 assert(rd_0_offset > 0);
1351 wr_rem_offset--;
1352 optr[-1]= escapechar;
1356 *(optr++)= c;
1357 bol= (c != _POSIX_VDISABLE) &&
1358 (c == defattr.c_cc[VKILL] ||
1359 c == defattr.c_cc[VEOF] ||
1360 c == defattr.c_cc[VINTR] ||
1361 c == defattr.c_cc[VSUSP] ||
1362 c == '\r' || c == '\n');
1364 wr_rem_size += optr-rd_0_buf-wr_rem_offset;
1365 if (wr_rem_size != 0)
1367 more2read_0= 0;
1368 more2write_rem= 1;
1370 return;
1371 } else
1372 if (result < 0) {
1373 fprintf(stderr, "rlogin: %s\n", strerror(error));
1375 done(1);
1378 static void
1379 completed_1(result, error)
1380 int result;
1381 int error;
1383 inprogress_1= 0;
1385 if (result > 0)
1387 if (extra_1 != NULL)
1389 assert (result <= extra_1_size);
1390 extra_1_size -= result;
1391 extra_1_offset += result;
1392 if (extra_1_size == 0)
1394 more2write_1= 0;
1395 more2read_rem= 1;
1396 free(extra_1);
1397 extra_1= NULL;
1399 return;
1401 assert (result <= wr_1_size);
1402 wr_1_size -= result;
1403 wr_1_offset += result;
1404 if (wr_1_size == 0)
1406 more2write_1= 0;
1407 more2read_rem= 1;
1409 return;
1410 } else
1411 if (result < 0) {
1412 fprintf(stderr, "rlogin: %s\n", strerror(error));
1414 done(1);
1417 static void
1418 completed_rd_rem(result, error)
1419 int result;
1420 int error;
1422 nwio_tcpopt_t tcpopt;
1423 char *new_buf;
1424 size_t keep_size;
1425 u_char urg_byte;
1426 int i;
1428 inprogress_rd_rem= 0;
1430 if (result > 0)
1432 if (rd_rem_urg)
1434 #if DEBUG
1435 fprintf(stderr, "\n\r%d urg bytes\n\r", result);
1436 #endif
1437 if (urg_1_size > MAX_EXTRA_1_NEW_SIZE)
1439 keep_size= MAX_EXTRA_1_NEW_SIZE/2;
1440 memmove(urg_1, urg_1+urg_1_size-keep_size,
1441 keep_size);
1442 urg_1_size= keep_size;
1444 new_buf= realloc(urg_1, urg_1_size+result);
1445 if (new_buf == NULL)
1447 fprintf(stderr,
1448 "rlogin: warning realloc %d failed\n",
1449 urg_1_size+result);
1450 return;
1452 memcpy(new_buf+urg_1_size,
1453 rd_rem_buf+rd_rem_offset, result);
1454 urg_1= new_buf;
1455 urg_1_size += result;
1456 return;
1458 more2read_rem= 0;
1459 more2write_1= 1;
1460 wr_1_size= result;
1461 wr_1_offset= rd_rem_offset;
1462 return;
1464 if (result == -1 && error == EURG)
1466 #if DEBUG
1467 fprintf(stderr, "\n\rEURG\n\r");
1468 #endif
1469 rd_rem_urg= 1;
1470 tcpopt.nwto_flags= NWTO_RCV_URG;
1471 result= ioctl(rem, NWIOSTCPOPT, &tcpopt);
1472 if (result == -1)
1474 fprintf(stderr,
1475 "rlogin: NWIOSTCPOPT on %d failed (%s)\n",
1476 rem, strerror(errno));
1477 exit(1);
1479 return;
1481 if (result == -1 && error == ENOURG)
1483 #if DEBUG
1484 fprintf(stderr, "\n\rENOURG\n\r");
1485 #endif
1486 rd_rem_urg= 0;
1487 tcpopt.nwto_flags= NWTO_RCV_NOTURG;
1488 result= ioctl(rem, NWIOSTCPOPT, &tcpopt);
1489 if (result == -1)
1491 fprintf(stderr,
1492 "rlogin: NWIOSTCPOPT on %d failed (%s)\n",
1493 rem, strerror(errno));
1494 exit(1);
1496 if (urg_1_size != 0)
1498 urg_byte= urg_1[urg_1_size-1];
1499 urg_1_size--;
1500 do_urg(urg_byte);
1501 if (urg_1_size == 0)
1502 return;
1503 if (extra_1_new_size + urg_1_size > MAX_EXTRA_1_NEW_SIZE)
1505 extra_1_new_size= 0;
1506 free(extra_1_new);
1507 extra_1_new= NULL;
1509 if (extra_1_new_size != 0)
1511 new_buf= realloc(extra_1_new,
1512 extra_1_new_size+urg_1_size);
1513 if (new_buf == 0)
1515 extra_1_new_size= 0;
1516 free(extra_1_new);
1517 extra_1_new= NULL;
1519 else
1521 extra_1_new= new_buf;
1522 memcpy(extra_1_new+extra_1_new_size,
1523 urg_1, urg_1_size);
1524 extra_1_new_size += urg_1_size;
1525 urg_1_size= 0;
1526 free(urg_1);
1527 urg_1= NULL;
1530 if (extra_1_new_size == 0)
1532 extra_1_new_size= urg_1_size;
1533 extra_1_new= urg_1;
1534 urg_1_size= 0;
1535 urg_1= NULL;
1537 more2read_rem= 0;
1538 more2write_1= 1;
1540 return;
1542 if (result == -1 && error == EINTR)
1544 /* Never mind. */
1545 return;
1547 if (result == 0)
1549 msg("connection closed.");
1550 done(0);
1552 if (result < 0) {
1553 fprintf(stderr, "rlogin: %s\n", strerror(error));
1555 done(1);
1558 static void
1559 completed_wr_rem(result, error)
1560 int result;
1561 int error;
1563 inprogress_wr_rem= 0;
1565 if (result > 0)
1567 if (extra_wr_rem != NULL)
1569 assert (result <= extra_wr_rem_size);
1570 extra_wr_rem_size -= result;
1571 extra_wr_rem_offset += result;
1572 if (extra_wr_rem_size == 0)
1574 free(extra_wr_rem);
1575 extra_wr_rem= NULL;
1576 if (wr_rem_size == 0)
1578 more2write_rem= 0;
1579 more2read_0= 1;
1582 return;
1585 assert (result <= wr_rem_size);
1586 wr_rem_size -= result;
1587 wr_rem_offset += result;
1588 if (wr_rem_size == 0)
1590 more2write_rem= 0;
1591 more2read_0= 1;
1593 return;
1595 if (result < 0) {
1596 fprintf(stderr, "rlogin: %s\n", strerror(error));
1598 done(1);
1601 static void
1602 do_urg(urg_byte)
1603 int urg_byte;
1605 #if DEBUG
1606 fprintf(stderr, "rlogin: warning got urg_byte 0x%x\r\n", urg_byte);
1607 #endif
1608 if (urg_byte & TIOCPKT_WINDOW)
1610 if (dosigwinch == 0)
1612 sendwindow();
1613 signal(SIGWINCH, sigwinch);
1615 dosigwinch= 1;
1619 static void
1620 echo(c)
1621 int c;
1623 u_char c1;
1624 char *new_buf;
1626 new_buf= realloc(extra_1_new, extra_1_new_size+6);
1627 if (new_buf == NULL)
1628 return;
1629 extra_1_new= new_buf;
1630 new_buf= extra_1_new+extra_1_new_size;
1632 c1= escapechar;
1633 if (c1 < ' ')
1635 *new_buf++= '^';
1636 *new_buf++= c1 + '@';
1638 else if (c1 == 0x7f)
1640 *new_buf++= '^';
1641 *new_buf++= '?';
1643 else
1644 *new_buf++= c1;
1646 if (c < ' ')
1648 *new_buf++= '^';
1649 *new_buf++= c + '@';
1651 else if (c == 0x7f)
1653 *new_buf++= '^';
1654 *new_buf++= '?';
1656 else
1657 *new_buf++= c;
1659 *new_buf++= '\r';
1660 *new_buf++= '\n';
1661 extra_1_new_size= new_buf-extra_1_new;
1662 more2write_1= 1;
1665 static void
1666 finish()
1668 done(0);
1671 static char cmdbuf[256];
1673 static void
1674 subshell()
1676 /* Start a subshell. Based on the first character of the command,
1677 * the tcp connection will be present at fd 3 ('+'), or at
1678 * fd 0 and fd 1 ('=')
1680 int r, pid, stat, len;
1681 char *shell, *cmd;
1683 /* cancel the reads and writes that are in progress. */
1684 if (inprogress_0)
1686 r= fcancel(0, ASIO_READ);
1687 if (r != 0) abort();
1689 if (inprogress_1)
1691 r= fcancel(1, ASIO_WRITE);
1692 if (r != 0) abort();
1694 if (inprogress_rd_rem)
1696 r= fcancel(rem, ASIO_READ);
1697 if (r != 0) abort();
1699 if (inprogress_wr_rem)
1701 r= fcancel(rem, ASIO_WRITE);
1702 if (r != 0) abort();
1705 mode(0);
1707 pid= fork();
1708 if (pid == -1) abort();
1709 if (pid != 0)
1711 r= waitpid(pid, &stat, 0);
1712 if (r != pid) abort();
1714 #if DEBUG
1715 fprintf(stderr, "stat: 0x%x\n", stat);
1716 #endif
1717 mode(1);
1718 return;
1721 (void)signal(SIGINT, SIG_DFL);
1723 shell= getenv("SHELL");
1724 if (shell == NULL)
1725 shell= "/bin/sh";
1726 printf("~!\ncommand [%s]: ", shell);
1727 cmd= fgets(cmdbuf, sizeof(cmdbuf), stdin);
1728 if (cmd == NULL)
1729 exit(0);
1730 #if DEBUG
1731 printf("got command '%s'\n", cmd);
1732 #endif
1734 /* Strip the trailing newline */
1735 len= strlen(cmd);
1736 if (len > 0 && cmd[len-1] == '\n')
1737 cmd[len-1]= '\0';
1738 else
1739 printf("\n");
1741 /* Skip leading white space */
1742 while (*cmd != '\0' && isspace(*cmd))
1743 cmd++;
1745 if (*cmd == '+')
1747 if (rem != 3)
1749 dup2(rem, 3);
1750 close(rem);
1752 cmd++;
1754 else if (*cmd == '=')
1756 dup2(rem, 0);
1757 dup2(rem, 1);
1758 close(rem);
1759 cmd++;
1761 else
1762 close(rem);
1763 if (*cmd == '\0')
1765 r= execl(shell, shell, NULL);
1767 else
1769 r= execl("/bin/sh", "sh", "-c", cmd, NULL);
1771 printf("exec failed: %d, %d\n", r, errno);
1772 exit(0);
1774 #endif /* __minix_vmd */