2 * Copyright (c) 1983, 1990 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
36 "@(#) Copyright (c) 1983, 1990 The Regents of the University of California.\n\
37 All rights reserved.\n";
42 static char sccsid
[] = "@(#)rlogin.c 5.33 (Berkeley) 3/1/91";
48 * $Header: mit/rlogin/RCS/rlogin.c,v 5.2 89/07/26 12:11:21 kfall
53 * rlogin - remote login
55 #include <sys/ioctl.h>
56 #include <sys/types.h>
59 #include <net/netlib.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>
80 #include <kerberosIV/des.h>
81 #include <kerberosIV/krb.h>
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();
90 #ifndef TIOCPKT_WINDOW
91 #define TIOCPKT_WINDOW 0x80
94 /* concession to Sun */
100 int eight
, litout
, rem
;
103 u_char escapechar
= '~';
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" },
120 /* flow control variables */
126 int inprogress_rd_rem
;
128 int inprogress_wr_rem
;
130 /* write to remote */
132 size_t wr_rem_offset
;
133 size_t extra_wr_rem_size
;
134 size_t extra_wr_rem_offset
;
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
);
155 static int reader( void );
157 static void mode( int f
);
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
);
175 static void catch_child( int sig
);
176 static void writer( void );
178 static void echo( int c
);
180 static void finish( void );
181 static void sendwindow( void );
182 static void sigwinch( int sig
);
183 static void subshell( void );
195 nwio_tcpopt_t tcpopt
;
197 int argoff
, ch
, dflag
, one
, uid
;
198 char *host
, *p
, *user
, term
[1024];
204 if (p
= rindex(argv
[0], '/'))
209 if (strcmp(p
, "rlogin"))
212 /* handle "rlogin host flags" */
213 if (!host
&& argc
> 2 && argv
[1][0] != '-') {
219 #define OPTIONS "8EKLde:k:l:x"
221 #define OPTIONS "8EKLde:l:"
223 while ((ch
= getopt(argc
- argoff
, argv
+ argoff
, OPTIONS
)) != EOF
)
243 escapechar
= getescape(optarg
);
247 dest_realm
= dst_realm_buf
;
248 (void)strncpy(dest_realm
, optarg
, REALM_SZ
);
258 des_set_key(cred
.session
, schedule
);
270 /* if haven't gotten a host yet, do so */
271 if (!host
&& !(host
= *argv
++))
277 if (!(pw
= getpwuid(uid
= getuid()))) {
278 (void)fprintf(stderr
, "rlogin: unknown user id.\n");
287 sp
= getservbyname((doencrypt
? "eklogin" : "klogin"), "tcp");
290 warning("can't get entry for %s/tcp service",
291 doencrypt
? "eklogin" : "klogin");
296 sp
= getservbyname("login", "tcp");
298 (void)fprintf(stderr
, "rlogin: login/tcp: unknown service.\n");
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
);
319 if (dest_realm
== NULL
)
320 dest_realm
= krb_realmofhost(host
);
324 rem
= krcmd_mutual(&host
, sp
->s_port
, user
, term
, 0,
325 dest_realm
, &cred
, schedule
);
328 rem
= krcmd(&host
, sp
->s_port
, user
, term
, 0,
332 sp
= getservbyname("login", "tcp");
334 (void)fprintf(stderr
,
335 "rlogin: unknown service login/tcp.\n");
338 if (errno
== ECONNREFUSED
)
339 warning("remote host doesn't support Kerberos");
341 warning("can't provide Kerberos auth data");
347 (void)fprintf(stderr
,
348 "rlogin: the -x flag requires Kerberos authentication.\n");
352 rem
= rcmd(&host
, sp
->s_port
, pw
->pw_name
, user
, term
, 0);
355 rem
= rcmd(&host
, sp
->s_port
, pw
->pw_name
, user
, term
, 0);
356 #endif /* KERBEROS */
361 /* Enable BSD compatibility for urgent data. */
362 tcpopt
.nwto_flags
= NWTO_BSD_URG
;
363 error
= ioctl(rem
, NWIOSTCPOPT
, &tcpopt
);
366 fprintf(stderr
, "rlogin: NWIOSTCPOPT failed: %s\n",
375 struct termios defattr
, rawattr
;
389 asio_fd_set_t fd_set
;
394 (void)tcgetattr(0, &sb
);
398 rawattr
.c_iflag
&= ~(ICRNL
| IGNCR
| INLCR
| ISTRIP
| IXOFF
| IXON
|
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
);
410 (void)fprintf(stderr
, "rlogin: fork: %s.\n", strerror(errno
));
417 msg("connection closed.");
421 msg("\007connection closed.");
425 (void)signal(SIGCHLD
, catch_child
);
428 #else /* __minix_vmd */
431 /* mark the file descriptors 0, 1, and rem as asynchronous. */
442 ASIO_FD_ZERO(&fd_set
);
444 fw
.fw_bits
= fd_set
.afds_bits
;
445 fw
.fw_maxfd
= ASIO_FD_SETSIZE
;
447 if (more2read_0
&& !inprogress_0
)
450 fw
.fw_flags
|= FWF_NONBLOCK
;
453 if (more2write_1
&& !inprogress_1
)
456 fw
.fw_flags
|= FWF_NONBLOCK
;
459 if (more2read_rem
&& !inprogress_rd_rem
)
462 fw
.fw_flags
|= FWF_NONBLOCK
;
465 if (more2write_rem
&& !inprogress_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
);
483 if (result
== -1 && (errno
== EAGAIN
||
490 fprintf(stderr
, "fwait failed (%s)\n",
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
);
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
);
521 "strange result from fwait: fd= %d, operation= %d\n",
522 fw
.fw_fd
, fw
.fw_operation
);
525 if (!(fw
.fw_flags
& FWF_MORE
))
534 #endif /* __minix_vmd */
535 msg("connection closed.");
539 /* trap a signal, unless it is being ignored. */
543 void(*act
) ( int sig
);
545 if (signal(sig
, act
) == SIG_IGN
)
546 (void)signal(sig
, SIG_IGN
);
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
);
581 pid
= waitpid(-1, &status
, WNOHANG
|WUNTRACED
);
584 /* if the child (reader) dies, just quit */
585 if (pid
< 0 || pid
== child
&& !WIFSTOPPED(status
))
586 done(WTERMSIG(status
) | WEXITSTATUS(status
));
594 * writer: write to remote: 0 -> line.
596 * ~^Z suspend rlogin process.
597 * ~<delayed-suspend char> suspend rlogin process, but leave reader alone.
602 register int bol
, local
, n
;
606 bol
= 1; /* beginning of line */
609 n
= read(STDIN_FILENO
, &ch
, 1);
611 if (n
< 0 && errno
== EINTR
)
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
625 if (!noescape
&& c
== escapechar
) {
631 if (c
== '.' || c
== defattr
.c_cc
[VEOF
]) {
636 if (c
== defattr
.c_cc
[VSUSP
]) {
647 (void)des_write(rem
, &escapechar
, 1);
651 (void)write(rem
, &escapechar
, 1);
658 if (des_write(rem
, &ch
, 1) == 0) {
665 if (write(rem
, &ch
, 1) == 0) {
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';
692 } else if (c
== 0177) {
699 (void)write(STDOUT_FILENO
, buf
, p
- buf
);
708 (void)signal(SIGCHLD
, SIG_IGN
);
709 (void)kill(cmdc
== defltc
.t_suspc
? 0 : getpid(), SIGTSTP
);
710 (void)signal(SIGCHLD
, catch_child
);
712 sigwinch(); /* check for size changes */
725 signal(SIGWINCH
, sigwinch
);
728 if (dosigwinch
&& get_window_size(0, &ws
) == 0 &&
729 memcmp(&ws
, &winsize
, sizeof(ws
))) {
736 * Send the window size to the server via the magic escape
742 char *obuf
, *new_buf
;
744 new_buf
= realloc(extra_wr_rem_new
,
745 extra_wr_rem_new_size
+4+sizeof(*wp
));
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
);
755 wp
= (struct winsize
*)(obuf
+4);
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 */
770 * reader: read from remote: line -> 1
775 int rcvcnt
, rcvstate
;
776 char rcvbuf
[8 * 1024];
786 while ((remaining
= rcvcnt
- (bufp
- rcvbuf
)) > 0) {
788 n
= write(STDOUT_FILENO
, bufp
, remaining
);
803 rcvcnt
= des_read(rem
, rcvbuf
, sizeof(rcvbuf
));
807 rcvcnt
= read(rem
, rcvbuf
, sizeof (rcvbuf
));
814 nwio_tcpopt_t tcpopt
;
816 fprintf(stderr
, "\n\rEURG\n\r");
818 tcpopt
.nwto_flags
= NWTO_RCV_URG
;
819 if (ioctl(rem
, NWIOSTCPOPT
, &tcpopt
) == -1) {
821 "rlogin: trouble with urgent data: %s\n",
827 if (errno
== ENOURG
) {
828 nwio_tcpopt_t tcpopt
;
830 fprintf(stderr
, "\n\rENOURG\n\r");
832 tcpopt
.nwto_flags
= NWTO_RCV_NOTURG
;
833 if (ioctl(rem
, NWIOSTCPOPT
, &tcpopt
) == -1) {
835 "rlogin: trouble with not-urgent data: %s\n",
841 (void)fprintf(stderr
, "rlogin: read: %s\n",
847 #endif /* !__minix_vmd */
865 (void)tcsetattr(0, TCSAFLUSH
, sb
);
872 (void)signal(SIGPIPE
, SIG_IGN
);
873 msg("\007connection closed.");
881 (void)fprintf(stderr
, "rlogin: %s\r\n", str
);
892 (void)fprintf(stderr
, "rlogin: warning, using standard rlogin: ");
894 fmt
= va_arg(ap
, char *);
895 vfprintf(stderr
, fmt
, ap
);
897 (void)fprintf(stderr
, ".\n");
904 (void)fprintf(stderr
,
905 "Usage: rlogin [-%s]%s[-e char] [-l username] host\n",
908 "8ELx", " [-k realm] ");
910 "8EL", " [-k realm] ");
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.
923 get_window_size(fd
, wp
)
930 if ((error
= ioctl(0, TIOCGSIZE
, &ts
)) != 0)
932 wp
->ws_row
= ts
.ts_lines
;
933 wp
->ws_col
= ts
.ts_cols
;
947 if ((len
= strlen(p
)) == 1) /* use any single char, including '\' */
949 /* otherwise, \nnn */
950 if (*p
== '\\' && len
>= 2 && len
<= 4) {
951 val
= strtol(++p
, (char **)NULL
, 8);
955 if (*p
< '0' || *p
> '8')
959 msg("illegal option value -- e");
969 for (i
= 0; speeds
[i
].name
!= NULL
&& speeds
[i
].speed
!= speed
; i
++) {
970 if (speeds
[i
].speed
== speed
) return speeds
[i
].name
;
983 result
= fcntl(fd
, F_GETFD
);
987 "rlogin: mark_async: fcntl(%d, GETFD) failed (%s)\n",
988 fd
, strerror(errno
));
991 v
= result
| FD_ASYNCHIO
;
992 result
= fcntl(fd
, F_SETFD
, v
);
996 "rlogin: mark_async: fcntl(%d, SETFD, %d) failed (%s)\n",
997 fd
, v
, strerror(errno
));
1002 #define RD_0_BUFSIZE 256
1003 char rd_0_buf
[RD_0_BUFSIZE
];
1019 size_t extra_1_size
;
1020 size_t extra_1_offset
;
1022 size_t extra_1_new_size
;
1023 #define MAX_EXTRA_1_NEW_SIZE (16*1024)
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
;
1050 inprogress_rd_rem
= 0;
1059 inprogress_wr_rem
= 0;
1062 extra_wr_rem_size
= 0;
1063 extra_wr_rem_offset
= 0;
1065 extra_wr_rem_new_size
= 0;
1066 extra_wr_rem_new
= NULL
;
1075 assert(!inprogress_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
);
1085 assert(rd_0_offset
+offset
<= RD_0_BUFSIZE
);
1091 completed_0(offset
, 0);
1092 rd_0_offset
+= offset
;
1093 if (result
== -1 && error
== EINPROGRESS
)
1098 completed_0(result
, error
);
1101 completed_0(offset
, 0);
1110 assert(!inprogress_1
);
1112 while (extra_1
!= NULL
|| extra_1_new
!= NULL
)
1114 if (extra_1
== NULL
)
1116 extra_1
= extra_1_new
;
1118 extra_1_size
= extra_1_new_size
;
1119 extra_1_new_size
= 0;
1124 if (extra_1_size
== 0)
1125 fprintf(stderr
, "restart_1: extra_1_size= 0\n");
1127 while (offset
< extra_1_size
)
1129 result
= write(1, extra_1
+extra_1_offset
+offset
,
1130 extra_1_size
-offset
);
1133 assert (result
<= extra_1_size
-offset
);
1139 completed_1(offset
, 0);
1141 if (result
== -1 && errno
== EINPROGRESS
)
1146 completed_1(result
, errno
);
1149 completed_1(offset
, 0);
1161 while (offset
< wr_1_size
)
1163 result
= write(1, rd_rem_buf
+wr_1_offset
+offset
,
1167 assert (result
<= wr_1_size
-offset
);
1173 completed_1(offset
, 0);
1175 if (result
== -1 && errno
== EINPROGRESS
)
1180 completed_1(result
, errno
);
1183 completed_1(offset
, 0);
1192 assert(!inprogress_rd_rem
);
1195 while (offset
< RD_REM_BUFSIZE
)
1197 result
= read(rem
, rd_rem_buf
+offset
, RD_REM_BUFSIZE
-offset
);
1201 assert(offset
<= RD_REM_BUFSIZE
);
1207 completed_rd_rem(offset
, 0);
1208 rd_rem_offset
= offset
;
1209 if (result
== -1 && error
== EINPROGRESS
)
1211 inprogress_rd_rem
= 1;
1214 completed_rd_rem(result
, error
);
1217 completed_rd_rem(offset
, 0);
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
)
1239 while (offset
< extra_wr_rem_size
)
1242 extra_wr_rem
+extra_wr_rem_offset
+offset
,
1243 extra_wr_rem_size
-offset
);
1246 assert (result
<= extra_wr_rem_size
-offset
);
1252 completed_wr_rem(offset
, 0);
1254 if (result
== -1 && errno
== EINPROGRESS
)
1256 inprogress_wr_rem
= 1;
1259 completed_wr_rem(result
, errno
);
1262 completed_wr_rem(offset
, 0);
1264 if (wr_rem_size
== 0)
1268 while (offset
< wr_rem_size
)
1270 result
= write(rem
, rd_0_buf
+wr_rem_offset
+offset
,
1271 wr_rem_size
-offset
);
1274 assert (result
<= wr_rem_size
-offset
);
1280 completed_wr_rem(offset
, 0);
1282 if (result
== -1 && errno
== EINPROGRESS
)
1284 inprogress_wr_rem
= 1;
1287 completed_wr_rem(result
, errno
);
1290 completed_wr_rem(offset
, 0);
1294 completed_0(result
, error
)
1298 static int bol
= 0, local
= 0;
1308 assert(rd_0_offset
> 0);
1311 iptr
= rd_0_buf
+rd_0_offset
;
1312 optr
= rd_0_buf
+wr_rem_offset
;
1313 for (i
= 0; i
<result
; iptr
++, i
++)
1319 if (!noescape
&& c
== escapechar
)
1328 if (c
== '.' || (c
!= _POSIX_VDISABLE
&&
1329 c
== defattr
.c_cc
[VEOF
]))
1340 if (c
!= escapechar
)
1344 *(optr
++)= escapechar
;
1348 assert(optr
== iptr
);
1349 assert(iptr
== rd_0_buf
+
1351 assert(rd_0_offset
> 0);
1353 optr
[-1]= escapechar
;
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)
1374 fprintf(stderr
, "rlogin: %s\n", strerror(error
));
1380 completed_1(result
, error
)
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)
1402 assert (result
<= wr_1_size
);
1403 wr_1_size
-= result
;
1404 wr_1_offset
+= result
;
1413 fprintf(stderr
, "rlogin: %s\n", strerror(error
));
1419 completed_rd_rem(result
, error
)
1423 nwio_tcpopt_t tcpopt
;
1429 inprogress_rd_rem
= 0;
1436 fprintf(stderr
, "\n\r%d urg bytes\n\r", result
);
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
,
1443 urg_1_size
= keep_size
;
1445 new_buf
= realloc(urg_1
, urg_1_size
+result
);
1446 if (new_buf
== NULL
)
1449 "rlogin: warning realloc %d failed\n",
1453 memcpy(new_buf
+urg_1_size
,
1454 rd_rem_buf
+rd_rem_offset
, result
);
1456 urg_1_size
+= result
;
1462 wr_1_offset
= rd_rem_offset
;
1465 if (result
== -1 && error
== EURG
)
1468 fprintf(stderr
, "\n\rEURG\n\r");
1471 tcpopt
.nwto_flags
= NWTO_RCV_URG
;
1472 result
= ioctl(rem
, NWIOSTCPOPT
, &tcpopt
);
1476 "rlogin: NWIOSTCPOPT on %d failed (%s)\n",
1477 rem
, strerror(errno
));
1482 if (result
== -1 && error
== ENOURG
)
1485 fprintf(stderr
, "\n\rENOURG\n\r");
1488 tcpopt
.nwto_flags
= NWTO_RCV_NOTURG
;
1489 result
= ioctl(rem
, NWIOSTCPOPT
, &tcpopt
);
1493 "rlogin: NWIOSTCPOPT on %d failed (%s)\n",
1494 rem
, strerror(errno
));
1497 if (urg_1_size
!= 0)
1499 urg_byte
= urg_1
[urg_1_size
-1];
1502 if (urg_1_size
== 0)
1504 if (extra_1_new_size
+ urg_1_size
> MAX_EXTRA_1_NEW_SIZE
)
1506 extra_1_new_size
= 0;
1510 if (extra_1_new_size
!= 0)
1512 new_buf
= realloc(extra_1_new
,
1513 extra_1_new_size
+urg_1_size
);
1516 extra_1_new_size
= 0;
1522 extra_1_new
= new_buf
;
1523 memcpy(extra_1_new
+extra_1_new_size
,
1525 extra_1_new_size
+= urg_1_size
;
1531 if (extra_1_new_size
== 0)
1533 extra_1_new_size
= urg_1_size
;
1543 if (result
== -1 && error
== EINTR
)
1550 msg("connection closed.");
1554 fprintf(stderr
, "rlogin: %s\n", strerror(error
));
1560 completed_wr_rem(result
, error
)
1564 inprogress_wr_rem
= 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)
1577 if (wr_rem_size
== 0)
1586 assert (result
<= wr_rem_size
);
1587 wr_rem_size
-= result
;
1588 wr_rem_offset
+= result
;
1589 if (wr_rem_size
== 0)
1597 fprintf(stderr
, "rlogin: %s\n", strerror(error
));
1607 fprintf(stderr
, "rlogin: warning got urg_byte 0x%x\r\n", urg_byte
);
1609 if (urg_byte
& TIOCPKT_WINDOW
)
1611 if (dosigwinch
== 0)
1614 signal(SIGWINCH
, sigwinch
);
1627 new_buf
= realloc(extra_1_new
, extra_1_new_size
+6);
1628 if (new_buf
== NULL
)
1630 extra_1_new
= new_buf
;
1631 new_buf
= extra_1_new
+extra_1_new_size
;
1637 *new_buf
++= c1
+ '@';
1639 else if (c1
== 0x7f)
1650 *new_buf
++= c
+ '@';
1662 extra_1_new_size
= new_buf
-extra_1_new
;
1672 static char cmdbuf
[256];
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
;
1684 /* cancel the reads and writes that are in progress. */
1687 r
= fcancel(0, ASIO_READ
);
1688 if (r
!= 0) abort();
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();
1709 if (pid
== -1) abort();
1712 r
= waitpid(pid
, &stat
, 0);
1713 if (r
!= pid
) abort();
1716 fprintf(stderr
, "stat: 0x%x\n", stat
);
1722 (void)signal(SIGINT
, SIG_DFL
);
1724 shell
= getenv("SHELL");
1727 printf("~!\ncommand [%s]: ", shell
);
1728 cmd
= fgets(cmdbuf
, sizeof(cmdbuf
), stdin
);
1732 printf("got command '%s'\n", cmd
);
1735 /* Strip the trailing newline */
1737 if (len
> 0 && cmd
[len
-1] == '\n')
1742 /* Skip leading white space */
1743 while (*cmd
!= '\0' && isspace(*cmd
))
1755 else if (*cmd
== '=')
1766 r
= execl(shell
, shell
, NULL
);
1770 r
= execl("/bin/sh", "sh", "-c", cmd
, NULL
);
1772 printf("exec failed: %d, %d\n", r
, errno
);
1775 #endif /* __minix_vmd */