4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 * The Regents of the University of California
34 * University Acknowledgment- Portions of this document are derived from
35 * software developed by the University of California, Berkeley, and its
40 * rlogin - remote login
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/errno.h>
46 #include <sys/socket.h>
48 #include <sys/stropts.h>
49 #include <sys/ttold.h>
50 #include <sys/sockio.h>
52 #include <sys/ptyvar.h>
53 #include <sys/resource.h>
54 #include <sys/select.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
59 #include <priv_utils.h>
75 #include <profile/prof_int.h>
80 /* signal disposition - signal handler or SIG_IGN, SIG_ERR, etc. */
81 typedef void (*sigdisp_t
)(int);
83 extern errcode_t
profile_get_options_boolean(profile_t
, char **,
84 profile_options_boolean
*);
85 extern errcode_t
profile_get_options_string(profile_t
, char **,
86 profile_option_strings
*);
88 #define RLOGIN_BUFSIZ (1024 * 50)
89 static char des_inbuf
[2 * RLOGIN_BUFSIZ
];
90 /* needs to be > largest read size */
91 static char des_outbuf
[2 * RLOGIN_BUFSIZ
];
92 /* needs to be > largest write size */
93 static krb5_data desinbuf
, desoutbuf
;
94 static krb5_encrypt_block eblock
; /* eblock for encrypt/decrypt */
95 static krb5_keyblock
*session_key
;
96 static krb5_creds
*cred
;
97 static krb5_context bsd_context
= NULL
;
98 static krb5_auth_context auth_context
;
100 static char *krb_realm
;
102 static int krb5auth_flag
; /* Flag set, when KERBEROS is enabled */
103 static profile_options_boolean autologin_option
[] = {
104 { "autologin", &krb5auth_flag
, 0 },
108 static int fflag
, Fflag
; /* Flag set, when option -f / -F used */
109 static int encrypt_flag
; /* Flag set, when the "-x" option is used */
111 /* Flag set, if -PN / -PO is specified */
112 static boolean_t rcmdoption_done
;
114 /* Flags set, if corres. cmd line options are turned on */
115 static boolean_t encrypt_done
, fwd_done
, fwdable_done
;
117 static profile_options_boolean option
[] = {
118 { "encrypt", &encrypt_flag
, 0 },
119 { "forward", &fflag
, 0 },
120 { "forwardable", &Fflag
, 0 },
124 static char *rcmdproto
;
125 static profile_option_strings rcmdversion
[] = {
126 { "rcmd_protocol", &rcmdproto
, 0 },
130 static char rlogin
[] = "rlogin";
132 static char *realmdef
[] = { "realms", NULL
, rlogin
, NULL
};
133 static char *appdef
[] = { "appdefaults", rlogin
, NULL
};
135 #ifndef TIOCPKT_WINDOW
136 #define TIOCPKT_WINDOW 0x80
137 #endif /* TIOCPKT_WINDOW */
140 #define sigmask(m) (1 << ((m)-1))
143 #define set2mask(setp) ((setp)->__sigbits[0])
144 #define mask2set(mask, setp) \
145 ((mask) == -1 ? sigfillset(setp) : (((setp)->__sigbits[0]) = (mask)))
148 #define DEBUGOPTSTRING "D:"
150 #define DEBUGOPTSTRING ""
153 static boolean_t ttcompat
;
154 static struct termios savetty
;
157 static int port_number
;
159 static char cmdchar
= '~';
160 static boolean_t nocmdchar
;
161 static boolean_t eight
;
162 static boolean_t litout
;
163 static boolean_t null_local_username
;
165 * Note that this list of speeds is shorter than the list of speeds
166 * supported by termios. This is because we can't be sure other rlogind's
167 * in the world will correctly cope with values other than what 4.2/4.3BSD
170 static char *speeds
[] =
171 { "0", "50", "75", "110", "134", "150", "200", "300",
172 "600", "1200", "1800", "2400", "4800", "9600", "19200",
174 static char term
[256] = "network";
175 static void lostpeer(void);
176 static boolean_t dosigwinch
;
177 static struct winsize winsize
;
178 static void sigwinch(int);
179 static void oob(void);
180 static void doit(int);
181 static sigdisp_t
sigdisp(int);
186 static void catchild(int);
188 static void copytochild(int);
189 static void writeroob(int);
190 static void stop(char), echo(char);
192 static int defflags
, tabflag
;
193 static int deflflags
;
194 static char deferase
, defkill
;
195 static struct tchars deftc
;
196 static struct ltchars defltc
;
197 static struct tchars notc
= { (char)-1, (char)-1, (char)-1,
198 (char)-1, (char)-1, (char)-1 };
199 static struct ltchars noltc
= { (char)-1, (char)-1, (char)-1,
200 (char)-1, (char)-1, (char)-1 };
202 static void done(int);
203 static void mode(int);
204 static int reader(int);
205 static void writer(void);
206 static void prf(const char *, ...);
207 static void sendwindow(void);
208 static int compat_ioctl(int, int, void *);
216 (void) sigprocmask(0, NULL
, &nset
);
217 mask2set(mask
, &nset
);
218 (void) sigprocmask(SIG_SETMASK
, &nset
, &oset
);
227 (void) sigprocmask(0, NULL
, &nset
);
228 mask2set(mask
, &nset
);
229 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
230 return (set2mask(&oset
));
237 * Pop ttcompat module
239 (void) ioctl(STDIN_FILENO
, I_POP
, 0);
241 (void) tcsetattr(STDIN_FILENO
, TCSANOW
, &savetty
);
247 (void) fprintf(stderr
, "%s\n%s\n",
248 gettext("usage: rlogin [-option] [-option...] "
249 "[-k realm] [-l username] host"),
250 gettext(" where option is e, 8, E, L, A, a, K, x, "
257 die(const char *format
, ...)
261 va_start(ap
, format
);
262 (void) vfprintf(stderr
, format
, ap
);
270 die(gettext("rlogin: Only one of -f and -F allowed.\n"));
274 main(int argc
, char **argv
)
277 char *cp
, *cmd
, *name
= NULL
;
280 int options
= 0, oldmask
;
287 krb5_error_code status
;
288 enum kcmd_proto kcmd_proto
= KCMD_NEW_PROTOCOL
;
290 (void) setlocale(LC_ALL
, "");
292 #if !defined(TEXT_DOMAIN)
293 #define TEXT_DOMAIN "SYS_TEST"
295 (void) textdomain(TEXT_DOMAIN
);
297 if (__init_suid_priv(0, PRIV_NET_PRIVADDR
, NULL
) == -1) {
298 (void) fprintf(stderr
,
299 gettext("Insufficient privileges, "
300 "rlogin must be set-uid root\n"));
307 if ((getattr_ret
= tcgetattr(STDIN_FILENO
, &savetty
)) < 0)
309 it
= ioctl(STDIN_FILENO
, I_FIND
, "ttcompat");
311 perror("ioctl I_FIND ttcompat");
312 return (EXIT_FAILURE
);
315 if (ioctl(STDIN_FILENO
, I_PUSH
, "ttcompat") < 0) {
316 perror("ioctl I_PUSH ttcompat");
324 * Determine command name used to invoke to rlogin(1). Users can
325 * create links named by a host pointing to the binary and type
326 * "hostname" to log into that host afterwards.
328 cmd
= strrchr(argv
[0], '/');
329 cmd
= (cmd
!= NULL
) ? (cmd
+ 1) : argv
[0];
331 if (strcmp(cmd
, rlogin
) == 0) {
334 if (*argv
[1] != '-') {
344 while ((c
= getopt(argc
, argv
,
345 DEBUGOPTSTRING
"8AEFLP:aKde:fk:l:x")) != -1) {
355 portnumber
= htons(atoi(optarg
));
367 fwdable_done
= B_TRUE
;
380 if (strcmp(optarg
, "N") == 0)
381 kcmd_proto
= KCMD_NEW_PROTOCOL
;
382 else if (strcmp(optarg
, "O") == 0)
383 kcmd_proto
= KCMD_OLD_PROTOCOL
;
385 die(gettext("rlogin: Only -PN or -PO "
388 die(gettext("rlogin: Only one of -PN and -PO "
390 rcmdoption_done
= B_TRUE
;
396 * Force the remote host to prompt for a password by sending
397 * a NULL username. These options are mutually exclusive with
398 * the -A, -x, -f, -F, -k <realm> options.
400 null_local_username
= B_TRUE
;
410 if ((c
= *cp
) != '\\') {
414 if (c
== '\0' || c
== '\\') {
416 } else if (c
>= '0' && c
<= '7') {
419 lc
= strtol(&cp
[1], NULL
, 8);
420 if (lc
< 0 || lc
> 255)
421 die(gettext("rlogin: octal "
422 "escape character %s too "
426 die(gettext("rlogin: unrecognized "
427 "escape character option %s.\n"),
443 encrypt_done
= B_TRUE
;
463 pwd
= getpwuid(uid
= getuid());
465 (void) fprintf(stderr
, gettext("getpwuid(): can not find "
466 "password entry for user id %d."), uid
);
467 return (EXIT_FAILURE
);
473 * If the `-a or -K' options are issued on the cmd line, we reset
474 * all flags associated with other KRB5 specific options, since
475 * these options are mutually exclusive with the rest.
477 if (null_local_username
) {
479 fflag
= Fflag
= encrypt_flag
= 0;
480 (void) fprintf(stderr
,
481 gettext("Note: The -a (or -K) option nullifies "
482 "all other Kerberos-specific\noptions "
483 "you may have used.\n"));
484 } else if (!krb5auth_flag
) {
485 /* is autologin set in krb5.conf? */
486 status
= krb5_init_context(&bsd_context
);
487 /* don't sweat failure here */
490 * note that the call to profile_get_options_boolean
491 * with autologin_option can affect value of
494 (void) profile_get_options_boolean(bsd_context
->profile
,
495 appdef
, autologin_option
);
501 status
= krb5_init_context(&bsd_context
);
503 com_err(rlogin
, status
,
504 gettext("while initializing krb5"));
505 return (EXIT_FAILURE
);
509 * Set up buffers for desread and deswrite.
511 desinbuf
.data
= des_inbuf
;
512 desoutbuf
.data
= des_outbuf
;
513 desinbuf
.length
= sizeof (des_inbuf
);
514 desoutbuf
.length
= sizeof (des_outbuf
);
517 * Get our local realm to look up local realm options.
519 status
= krb5_get_default_realm(bsd_context
, &realmdef
[1]);
521 com_err(rlogin
, status
,
522 gettext("while getting default realm"));
523 return (EXIT_FAILURE
);
526 * Check the realms section in krb5.conf for encryption,
527 * forward & forwardable info
529 (void) profile_get_options_boolean(bsd_context
->profile
,
532 * Check the appdefaults section
534 (void) profile_get_options_boolean(bsd_context
->profile
,
536 (void) profile_get_options_string(bsd_context
->profile
,
537 appdef
, rcmdversion
);
540 * Set the *_flag variables, if the corresponding *_done are
541 * set to 1, because we dont want the config file values
542 * overriding the command line options.
549 } else if (fwdable_done
) {
553 if (!rcmdoption_done
&& (rcmdproto
!= NULL
)) {
554 if (strncmp(rcmdproto
, "rcmdv2", 6) == 0) {
555 kcmd_proto
= KCMD_NEW_PROTOCOL
;
556 } else if (strncmp(rcmdproto
, "rcmdv1", 6) == 0) {
557 kcmd_proto
= KCMD_OLD_PROTOCOL
;
559 (void) fprintf(stderr
, gettext("Unrecognized "
560 "KCMD protocol (%s)"), rcmdproto
);
561 return (EXIT_FAILURE
);
565 if (encrypt_flag
&& (!krb5_privacy_allowed())) {
566 (void) fprintf(stderr
, gettext("rlogin: "
567 "Encryption not supported.\n"));
568 return (EXIT_FAILURE
);
572 if (port_number
== 0) {
577 * If the krb5auth_flag is set (via -A, -f, -F, -k) &
578 * if there is an entry in /etc/services for Kerberos
579 * login, attempt to login with Kerberos. If we fail
580 * at any step, use the standard rlogin
582 sp
= getservbyname(encrypt_flag
?
583 "eklogin" : "klogin", "tcp");
585 port_number
= encrypt_flag
?
586 htons(2105) : htons(543);
588 port_number
= sp
->s_port
;
591 port_number
= htons(IPPORT_LOGINSERVER
);
597 (void) strncpy(term
, cp
, sizeof (term
));
598 term
[sizeof (term
) - 1] = '\0';
600 if (getattr_ret
== 0) {
601 speed
= cfgetospeed(&savetty
);
603 * "Be conservative in what we send" -- Only send baud rates
604 * which at least all 4.x BSD derivatives are known to handle
606 * NOTE: This code assumes new termios speed values will
607 * be "higher" speeds.
614 * Only put the terminal speed info in if we have room
615 * so we don't overflow the buffer, and only if we have
616 * a speed we recognize.
618 if (speed
> 0 && speed
< sizeof (speeds
)/sizeof (char *) &&
619 strlen(term
) + strlen("/") + strlen(speeds
[speed
]) + 1 <
621 (void) strcat(term
, "/");
622 (void) strcat(term
, speeds
[speed
]);
624 (void) sigset(SIGPIPE
, (sigdisp_t
)lostpeer
);
625 /* will use SIGUSR1 for window size hack, so hold it off */
626 oldmask
= sigblock(sigmask(SIGURG
) | sigmask(SIGUSR1
));
629 * Determine if v4 literal address and if so store it to one
630 * side. This is to correct the undesired behaviour of rcmd_af
631 * which converts a passed in v4 literal address to a v4 mapped
632 * v6 literal address. If it was a v4 literal we then re-assign
636 if (inet_addr(host
) != (in_addr_t
)-1)
640 authopts
= AP_OPTS_MUTUAL_REQUIRED
;
642 /* Piggy-back forwarding flags on top of authopts; */
643 /* they will be reset in kcmd */
645 authopts
|= OPTS_FORWARD_CREDS
;
647 authopts
|= OPTS_FORWARDABLE_CREDS
;
649 status
= kcmd(&sock
, &host
, port_number
,
650 null_local_username
? "" : pwd
->pw_name
,
652 "host", krb_realm
, bsd_context
, &auth_context
,
654 NULL
, /* No need for sequence number */
655 NULL
, /* No need for server seq # */
657 0, /* Not any port # */
662 * If new protocol requested, we dont fallback to
665 if (kcmd_proto
== KCMD_NEW_PROTOCOL
) {
666 (void) fprintf(stderr
, gettext("rlogin: kcmdv2 "
667 "to host %s failed - %s\n"
668 "Fallback to normal rlogin denied."),
669 host
, error_message(status
));
670 return (EXIT_FAILURE
);
673 (void) fprintf(stderr
, gettext("rlogin: kcmd "
674 "to host %s failed - %s,\n"
675 "trying normal rlogin...\n\n"),
676 host
, error_message(status
));
678 (void) fprintf(stderr
,
679 gettext("trying normal rlogin...\n"));
682 * kcmd() failed, so we have to
683 * fallback to normal rlogin
685 port_number
= htons(IPPORT_LOGINSERVER
);
687 fflag
= Fflag
= encrypt_flag
= 0;
688 null_local_username
= B_FALSE
;
690 (void) fprintf(stderr
,
691 gettext("connected with Kerberos V5\n"));
694 * Setup eblock for desread and deswrite.
696 session_key
= &cred
->keyblock
;
698 if (kcmd_proto
== KCMD_NEW_PROTOCOL
) {
699 status
= krb5_auth_con_getlocalsubkey(
704 com_err(rlogin
, status
,
705 "determining subkey for session");
706 return (EXIT_FAILURE
);
708 if (session_key
== NULL
) {
710 "no subkey negotiated for "
712 return (EXIT_FAILURE
);
716 eblock
.crypto_entry
= session_key
->enctype
;
717 eblock
.key
= (krb5_keyblock
*)session_key
;
719 init_encrypt(encrypt_flag
, bsd_context
, kcmd_proto
,
720 &desinbuf
, &desoutbuf
, CLIENT
, &eblock
);
729 * Don't merge this with the "if" statement above because
730 * "krb5auth_flag" might be set to false inside it.
732 if (!krb5auth_flag
) {
733 rem
= rcmd_af(&host
, port_number
,
734 null_local_username
? "" : pwd
->pw_name
,
735 name
, term
, NULL
, AF_INET6
);
740 /* Never need our privilege again */
746 if (options
& SO_DEBUG
&&
747 setsockopt(rem
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
,
749 perror("rlogin: setsockopt (SO_DEBUG)");
754 (void) setsockopt(rem
, SOL_SOCKET
, SO_RCVBUF
, (char *)&bufsize
,
768 if (ioctl(STDIN_FILENO
, TIOCGETP
, (char *)&sb
) == -1)
769 perror("ioctl TIOCGETP");
770 defflags
= sb
.sg_flags
;
771 tabflag
= defflags
& O_TBDELAY
;
772 defflags
&= ECHO
| O_CRMOD
;
773 deferase
= sb
.sg_erase
;
774 defkill
= sb
.sg_kill
;
775 if (ioctl(STDIN_FILENO
, TIOCLGET
, (char *)&deflflags
) == -1)
776 perror("ioctl TIOCLGET");
777 if (ioctl(STDIN_FILENO
, TIOCGETC
, (char *)&deftc
) == -1)
778 perror("ioctl TIOCGETC");
779 notc
.t_startc
= deftc
.t_startc
;
780 notc
.t_stopc
= deftc
.t_stopc
;
781 if (ioctl(STDIN_FILENO
, TIOCGLTC
, (char *)&defltc
) == -1)
782 perror("ioctl TIOCGLTC");
783 (void) sigset(SIGINT
, SIG_IGN
);
784 if (sigdisp(SIGHUP
) != SIG_IGN
)
785 (void) sigset(SIGHUP
, exit
);
786 if (sigdisp(SIGQUIT
) != SIG_IGN
)
787 (void) sigset(SIGQUIT
, exit
);
789 if (child
== (pid_t
)-1) {
790 perror("rlogin: fork");
795 if (reader(oldmask
) == 0) {
796 prf(gettext("Connection to %.*s closed."),
797 MAXHOSTNAMELEN
, host
);
801 prf(gettext("\aConnection to %.*s closed."),
802 MAXHOSTNAMELEN
, host
);
807 * We may still own the socket, and may have a pending SIGURG (or might
808 * receive one soon) that we really want to send to the reader. Set a
809 * trap that simply copies such signals to the child.
811 #ifdef F_SETOWN_BUG_FIXED
812 (void) sigset(SIGURG
, copytochild
);
814 (void) sigset(SIGURG
, SIG_IGN
);
815 #endif /* F_SETOWN_BUG_FIXED */
816 (void) sigset(SIGUSR1
, writeroob
);
818 * Of course, if the urgent byte already arrived, allowing SIGURG
819 * won't get us notification. So, we check to see if we've got
820 * an urgent byte. If so, force a call to writeroob() to pretend
823 if (ioctl(rem
, SIOCATMARK
, &atmark
) >= 0) {
828 (void) sigset(SIGCHLD
, catchild
);
830 prf(gettext("Closed connection to %.*s."), MAXHOSTNAMELEN
, host
);
835 * Get signal disposition (or signal handler) for a given signal
840 struct sigaction act
;
842 act
.sa_handler
= NULL
;
844 (void) sigemptyset(&act
.sa_mask
);
845 (void) sigaction(sig
, NULL
, &act
);
846 return (act
.sa_handler
);
856 /* make sure catchild does not snap it up */
857 (void) sigset(SIGCHLD
, SIG_DFL
);
858 if (kill(child
, SIGKILL
) >= 0)
859 while ((w
= wait(0)) > (pid_t
)0 && w
!= child
)
866 * Copy SIGURGs to the child process.
871 copytochild(int signum
)
874 (void) kill(child
, SIGURG
);
878 * This is called when the reader process gets the out-of-band (urgent)
879 * request to turn on the window-changing protocol.
884 writeroob(int signum
)
890 * Start tracking window size. It doesn't matter which
891 * order the next two are in, because we'll be unconditionally
892 * sending a size notification in a moment.
894 (void) sigset(SIGWINCH
, sigwinch
);
898 * It would be bad if a SIGWINCH came in between the ioctl
899 * and sending the data. It could result in the SIGWINCH
900 * handler sending a good message, and then us sending an
901 * outdated or inconsistent message.
903 * Instead, if the change is made before the
904 * ioctl, the sigwinch handler will send a size message
905 * and we'll send another, identical, one. If the change
906 * is made after the ioctl, we'll send a message with the
907 * old value, and then the sigwinch handler will send
908 * a revised, correct one.
910 mask
= sigblock(sigmask(SIGWINCH
));
911 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &winsize
) == 0)
926 options
= WNOHANG
| WEXITED
;
927 error
= waitid(P_ALL
, 0, &info
, options
);
930 if (info
.si_pid
== 0)
932 if (info
.si_code
== CLD_TRAPPED
)
934 if (info
.si_code
== CLD_STOPPED
)
936 done(info
.si_status
);
941 * writer: write to remote: 0 -> line.
943 * ~^Z suspend rlogin process.
944 * ~^Y suspend rlogin process, but leave reader alone.
951 boolean_t bol
= B_TRUE
; /* beginning of line */
952 boolean_t local
= B_FALSE
;
955 n
= read(STDIN_FILENO
, &c
, 1);
962 prf(gettext("Read error from terminal: %s"),
968 * If we're at the beginning of the line
969 * and recognize a command character, then
970 * we echo locally. Otherwise, characters
971 * are echo'd remotely. If the command
972 * character is doubled, this acts as a
973 * force and local echo is suppressed.
975 if (bol
&& !nocmdchar
) {
983 if (c
== '.' || c
== deftc
.t_eofc
) {
987 if (c
== defltc
.t_suspc
|| c
== defltc
.t_dsuspc
) {
994 if (deswrite(rem
, &cmdchar
, 1, 0) < 0) {
996 "Write error to network: %s"),
1002 if ((n
= deswrite(rem
, &c
, 1, 0)) <= 0) {
1004 prf(gettext("line gone"));
1006 prf(gettext("Write error to network: %s"),
1010 bol
= c
== defkill
|| c
== deftc
.t_eofc
||
1011 c
== deftc
.t_intrc
|| c
== defltc
.t_suspc
||
1012 c
== '\r' || c
== '\n';
1027 } else if (c
== 0177) {
1034 if (write(STDOUT_FILENO
, buf
, p
- buf
) < 0)
1035 prf(gettext("Write error to terminal: %s"), strerror(errno
));
1042 (void) sigset(SIGCHLD
, SIG_IGN
);
1043 (void) kill(cmdc
== defltc
.t_suspc
? 0 : getpid(), SIGTSTP
);
1044 (void) sigset(SIGCHLD
, catchild
);
1046 sigwinch(0); /* check for size changes */
1051 sigwinch(int signum
)
1055 if (dosigwinch
&& ioctl(STDIN_FILENO
, TIOCGWINSZ
, &ws
) == 0 &&
1056 memcmp(&winsize
, &ws
, sizeof (ws
)) != 0) {
1063 * Send the window size to the server via the magic escape.
1064 * Note: SIGWINCH should be blocked when this is called, lest
1065 * winsize change underneath us and chaos result.
1070 char obuf
[4 + sizeof (struct winsize
)];
1071 struct winsize
*wp
= (struct winsize
*)(void *)(obuf
+4);
1077 wp
->ws_row
= htons(winsize
.ws_row
);
1078 wp
->ws_col
= htons(winsize
.ws_col
);
1079 wp
->ws_xpixel
= htons(winsize
.ws_xpixel
);
1080 wp
->ws_ypixel
= htons(winsize
.ws_ypixel
);
1081 if (deswrite(rem
, obuf
, sizeof (obuf
), 0) < 0)
1082 prf(gettext("Write error to network: %s"), strerror(errno
));
1087 * reader: read from remote: remote -> stdout
1092 static char rcvbuf
[8 * 1024];
1094 static int rcvstate
;
1096 static jmp_buf rcvtop
;
1101 int out
= FWRITE
, atmark
, n
;
1103 char waste
[4*BUFSIZ
], mark
;
1109 FD_ZERO(&exceptfds
);
1110 FD_SET(rem
, &exceptfds
);
1112 ret
= select(rem
+1, NULL
, NULL
, &exceptfds
, &tv
);
1114 * We may get an extra signal at start up time since we are trying
1115 * to take all precautions not to miss the urgent byte. This
1116 * means we may get here without any urgent data to process, in which
1117 * case we do nothing and just return.
1123 if (ioctl(rem
, SIOCATMARK
, &atmark
) < 0) {
1128 * Urgent data not here yet.
1129 * It may not be possible to send it yet
1130 * if we are blocked for output
1131 * and our input buffer is full.
1133 if (rcvcnt
< sizeof (rcvbuf
)) {
1134 n
= desread(rem
, rcvbuf
+ rcvcnt
,
1135 sizeof (rcvbuf
) - rcvcnt
, 0);
1142 * We still haven't gotten to the urgent mark
1143 * and we're out of buffer space. Since we
1144 * must clear our receive window to allow it
1145 * to arrive, we will have to throw away
1148 n
= desread(rem
, waste
, sizeof (waste
), 0);
1153 } while (atmark
== 0);
1154 while (recv(rem
, &mark
, 1, MSG_OOB
) < 0) {
1159 * We've reached the urgent mark, so the next
1160 * data to arrive will be the urgent, but it must
1161 * not have arrived yet.
1170 if (mark
& TIOCPKT_WINDOW
) {
1172 * Let server know about window size changes
1174 (void) kill(ppid
, SIGUSR1
);
1176 if (!eight
&& (mark
& TIOCPKT_NOSTOP
)) {
1177 if (ioctl(STDIN_FILENO
, TIOCGETP
, (char *)&sb
) == -1)
1178 perror("ioctl TIOCGETP");
1179 sb
.sg_flags
&= ~O_CBREAK
;
1180 sb
.sg_flags
|= O_RAW
;
1181 if (compat_ioctl(STDIN_FILENO
, TIOCSETP
, &sb
) == -1)
1182 perror("ioctl TIOCSETP 1");
1185 if (compat_ioctl(STDIN_FILENO
, TIOCSETC
, ¬c
) == -1)
1186 perror("ioctl TIOCSETC");
1188 if (!eight
&& (mark
& TIOCPKT_DOSTOP
)) {
1189 if (ioctl(STDIN_FILENO
, TIOCGETP
, (char *)&sb
) == -1)
1190 perror("ioctl TIOCGETP");
1191 sb
.sg_flags
&= ~O_RAW
;
1192 sb
.sg_flags
|= O_CBREAK
;
1193 if (compat_ioctl(STDIN_FILENO
, TIOCSETP
, &sb
) == -1)
1194 perror("ioctl TIOCSETP 2");
1195 notc
.t_stopc
= deftc
.t_stopc
;
1196 notc
.t_startc
= deftc
.t_startc
;
1197 if (compat_ioctl(STDIN_FILENO
, TIOCSETC
, ¬c
) == -1)
1198 perror("ioctl TIOCSETC");
1200 if (mark
& TIOCPKT_FLUSHWRITE
) {
1201 if (ioctl(STDOUT_FILENO
, TIOCFLUSH
, (char *)&out
) == -1)
1202 perror("ioctl TIOCFLUSH");
1204 if (ioctl(rem
, SIOCATMARK
, &atmark
) < 0) {
1205 perror("ioctl SIOCATMARK");
1210 n
= desread(rem
, waste
, sizeof (waste
), 0);
1214 "Read error from network: %s"),
1220 * Don't want any pending data to be output,
1221 * so clear the recv buffer.
1222 * If we were hanging on a write when interrupted,
1223 * don't want it to restart. If we were reading,
1230 * If we filled the receive buffer while a read was pending,
1231 * longjmp to the top to restart appropriately. Don't abort
1232 * a pending write, however, or we won't know how much was written.
1234 if (rcvd
&& rcvstate
== READING
)
1239 * reader: read from remote: line -> 1
1245 * 4.3bsd or later and SunOS 4.0 or later use the posiitive
1246 * pid; otherwise use the negative.
1248 pid_t pid
= getpid();
1250 char *bufp
= rcvbuf
;
1252 (void) sigset(SIGTTOU
, SIG_IGN
);
1253 (void) sigset(SIGURG
, (void (*)())oob
);
1255 if (fcntl(rem
, F_SETOWN
, pid
) == -1)
1256 perror("fcntl F_SETOWN");
1258 * A SIGURG may have been posted before we were completely forked,
1259 * which means we may not have received it. To insure we do not miss
1260 * any urgent data, we force the signal. The signal hander will be
1261 * able to determine if in fact there is urgent data or not.
1263 (void) kill(pid
, SIGURG
);
1264 (void) setjmp(rcvtop
);
1265 sigsetmask(oldmask
);
1267 while ((remaining
= rcvcnt
- (bufp
- rcvbuf
)) > 0) {
1269 n
= write(STDOUT_FILENO
, bufp
, remaining
);
1271 if (errno
!= EINTR
) {
1273 "Write error to terminal: %s"),
1284 rcvcnt
= desread(rem
, rcvbuf
, sizeof (rcvbuf
), 0);
1290 prf(gettext("Read error from network: %s"),
1301 struct ltchars
*ltc
;
1305 if (ioctl(STDIN_FILENO
, TIOCGETP
, (char *)&sb
) == -1)
1306 perror("ioctl TIOCGETP");
1307 if (ioctl(STDIN_FILENO
, TIOCLGET
, (char *)&lflags
) == -1)
1308 perror("ioctl TIOCLGET");
1312 sb
.sg_flags
&= ~(O_CBREAK
|O_RAW
|O_TBDELAY
);
1313 sb
.sg_flags
|= defflags
|tabflag
;
1316 sb
.sg_kill
= defkill
;
1317 sb
.sg_erase
= deferase
;
1322 sb
.sg_flags
|= (eight
? O_RAW
: O_CBREAK
);
1323 sb
.sg_flags
&= ~defflags
;
1324 /* preserve tab delays, but turn off XTABS */
1325 if ((sb
.sg_flags
& O_TBDELAY
) == O_XTABS
)
1326 sb
.sg_flags
&= ~O_TBDELAY
;
1329 sb
.sg_kill
= sb
.sg_erase
= -1;
1338 if (compat_ioctl(STDIN_FILENO
, TIOCSLTC
, ltc
) == -1)
1339 perror("ioctl TIOCSLTC");
1340 if (compat_ioctl(STDIN_FILENO
, TIOCSETC
, tc
) == -1)
1341 perror("ioctl TIOCSETC");
1342 if (compat_ioctl(STDIN_FILENO
, TIOCSETP
, &sb
) == -1)
1343 perror("ioctl TIOCSETP 3");
1344 if (compat_ioctl(STDIN_FILENO
, TIOCLSET
, &lflags
) == -1)
1345 perror("ioctl TIOCLSET");
1350 prf(const char *format
, ...)
1354 va_start(ap
, format
);
1355 (void) vfprintf(stderr
, format
, ap
);
1357 (void) fputs(CRLF
, stderr
);
1363 (void) sigset(SIGPIPE
, SIG_IGN
);
1364 prf(gettext("\aConnection to %.*s closed."), MAXHOSTNAMELEN
, host
);
1369 compat_ioctl(int des
, int request
, void *arg
)
1372 boolean_t flag
= B_FALSE
;
1374 if (ioctl(des
, request
, arg
) < 0)
1377 if (tcgetattr(des
, &tb
) < 0)
1380 if (cfgetispeed(&tb
) != cfgetispeed(&savetty
)) {
1381 (void) cfsetispeed(&tb
, cfgetispeed(&savetty
));
1384 if (cfgetospeed(&tb
) != cfgetospeed(&savetty
)) {
1385 (void) cfsetospeed(&tb
, cfgetospeed(&savetty
));
1389 return (flag
? tcsetattr(des
, TCSANOW
, &tb
) : 0);