xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/rsh.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate /*
9*0Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
10*0Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
11*0Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  */
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate #define	_FILE_OFFSET_BITS 64
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate #include <sys/time.h>
18*0Sstevel@tonic-gate #include <sys/types.h>
19*0Sstevel@tonic-gate #include <sys/socket.h>
20*0Sstevel@tonic-gate #include <sys/ioctl.h>
21*0Sstevel@tonic-gate /* just for FIONBIO ... */
22*0Sstevel@tonic-gate #include <sys/filio.h>
23*0Sstevel@tonic-gate #include <sys/stat.h>
24*0Sstevel@tonic-gate #include <sys/select.h>
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #include <netinet/in.h>
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include <assert.h>
29*0Sstevel@tonic-gate #include <fcntl.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <string.h>
32*0Sstevel@tonic-gate #include <unistd.h>
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <errno.h>
35*0Sstevel@tonic-gate #include <signal.h>
36*0Sstevel@tonic-gate #include <pwd.h>
37*0Sstevel@tonic-gate #include <netdb.h>
38*0Sstevel@tonic-gate #include <locale.h>
39*0Sstevel@tonic-gate #include <priv_utils.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include <k5-int.h>
42*0Sstevel@tonic-gate #include <profile/prof_int.h>
43*0Sstevel@tonic-gate #include <com_err.h>
44*0Sstevel@tonic-gate #include <kcmd.h>
45*0Sstevel@tonic-gate #include <krb5.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /* signal disposition - signal handler or SIG_IGN, SIG_ERR, etc. */
48*0Sstevel@tonic-gate typedef void (*sigdisp_t)(int);
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate extern errcode_t	profile_get_options_boolean(profile_t, char **,
51*0Sstevel@tonic-gate     profile_options_boolean *);
52*0Sstevel@tonic-gate extern errcode_t	profile_get_options_string(profile_t, char **,
53*0Sstevel@tonic-gate     profile_option_strings *);
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate #define	RSH_BUFSIZ (1024 * 50)
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate static char des_inbuf[2 * RSH_BUFSIZ];	/* needs to be > largest read size */
58*0Sstevel@tonic-gate static char des_outbuf[2 * RSH_BUFSIZ];	/* needs to be > largest write size */
59*0Sstevel@tonic-gate static krb5_data desinbuf, desoutbuf;
60*0Sstevel@tonic-gate static krb5_encrypt_block eblock;	/* eblock for encrypt/decrypt */
61*0Sstevel@tonic-gate static krb5_context bsd_context;
62*0Sstevel@tonic-gate static krb5_auth_context auth_context;
63*0Sstevel@tonic-gate static krb5_creds *cred;
64*0Sstevel@tonic-gate static krb5_keyblock *session_key;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate static int encrypt_flag;	/* Flag set, when encryption is used */
67*0Sstevel@tonic-gate static boolean_t krb5auth_flag;	/* Flag set, when KERBEROS is enabled */
68*0Sstevel@tonic-gate static int fflag;	/* Flag set, if creds to be fwd'ed via -f */
69*0Sstevel@tonic-gate static int Fflag;	/* Flag set, if fwd'able creds to be fwd'ed via -F */
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /* Flag set, if -PN / -PO is specified */
72*0Sstevel@tonic-gate static boolean_t rcmdoption_done;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate /* Flags set, if corres. cmd line options are turned on */
75*0Sstevel@tonic-gate static boolean_t encrypt_done, fwd_done, fwdable_done;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate static profile_options_boolean option[] = {
78*0Sstevel@tonic-gate 	{ "encrypt", &encrypt_flag, 0 },
79*0Sstevel@tonic-gate 	{ "forward", &fflag, 0 },
80*0Sstevel@tonic-gate 	{ "forwardable", &Fflag, 0 },
81*0Sstevel@tonic-gate 	{ NULL, NULL, 0 }
82*0Sstevel@tonic-gate };
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate static char *rcmdproto;
85*0Sstevel@tonic-gate static profile_option_strings rcmdversion[] = {
86*0Sstevel@tonic-gate 	{ "rcmd_protocol", &rcmdproto, 0 },
87*0Sstevel@tonic-gate 	{ NULL, NULL, 0 }
88*0Sstevel@tonic-gate };
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate static char *realmdef[] = { "realms", NULL, "rsh", NULL };
91*0Sstevel@tonic-gate static char *appdef[] = { "appdefaults", "rsh", NULL };
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate static void sendsig(int);
94*0Sstevel@tonic-gate static sigdisp_t sigdisp(int);
95*0Sstevel@tonic-gate static boolean_t init_service(boolean_t);
96*0Sstevel@tonic-gate static int desrshread(int, char *, int);
97*0Sstevel@tonic-gate static int desrshwrite(int, char *, int);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate static int		options;
100*0Sstevel@tonic-gate static int		rfd2;
101*0Sstevel@tonic-gate static int		portnumber;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate static const char	rlogin_path[] = "/usr/bin/rlogin";
104*0Sstevel@tonic-gate static const char	dash_x[] = "-x ";	/* Note the blank after -x */
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate static boolean_t readiv, writeiv;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate #define	set2mask(setp)	((setp)->__sigbits[0])
109*0Sstevel@tonic-gate #define	mask2set(mask, setp) \
110*0Sstevel@tonic-gate 	((mask) == -1 ? sigfillset(setp) : (set2mask(setp) = (mask)))
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate #ifdef DEBUG
113*0Sstevel@tonic-gate #define	DEBUGOPTSTRING	"D:"
114*0Sstevel@tonic-gate #else
115*0Sstevel@tonic-gate #define	DEBUGOPTSTRING	""
116*0Sstevel@tonic-gate #endif	/* DEBUG */
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate static void
119*0Sstevel@tonic-gate sigsetmask(int mask)
120*0Sstevel@tonic-gate {
121*0Sstevel@tonic-gate 	sigset_t	nset;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	(void) sigprocmask(0, NULL, &nset);
124*0Sstevel@tonic-gate 	mask2set(mask, &nset);
125*0Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &nset, NULL);
126*0Sstevel@tonic-gate }
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate static int
129*0Sstevel@tonic-gate sigblock(int mask)
130*0Sstevel@tonic-gate {
131*0Sstevel@tonic-gate 	sigset_t oset;
132*0Sstevel@tonic-gate 	sigset_t nset;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	(void) sigprocmask(0, NULL, &nset);
135*0Sstevel@tonic-gate 	mask2set(mask, &nset);
136*0Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
137*0Sstevel@tonic-gate 	return (set2mask(&oset));
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate /*
141*0Sstevel@tonic-gate  * Get signal disposition (or signal handler) for a given signal
142*0Sstevel@tonic-gate  */
143*0Sstevel@tonic-gate static sigdisp_t
144*0Sstevel@tonic-gate sigdisp(int sig)
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate 	struct sigaction act;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	act.sa_handler = NULL;
149*0Sstevel@tonic-gate 	act.sa_flags = 0;
150*0Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
151*0Sstevel@tonic-gate 	(void) sigaction(sig, NULL, &act);
152*0Sstevel@tonic-gate 	return (act.sa_handler);
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate static pid_t child_pid = -1;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate  * If you do a command like "rsh host output | wc"
159*0Sstevel@tonic-gate  * and wc terminates, then the parent will receive SIGPIPE
160*0Sstevel@tonic-gate  * and the child needs to be terminated.
161*0Sstevel@tonic-gate  */
162*0Sstevel@tonic-gate /* ARGSUSED */
163*0Sstevel@tonic-gate static void
164*0Sstevel@tonic-gate sigpipehandler(int signal)
165*0Sstevel@tonic-gate {
166*0Sstevel@tonic-gate 	if (child_pid != -1)
167*0Sstevel@tonic-gate 		(void) kill(child_pid, SIGKILL);
168*0Sstevel@tonic-gate 	exit(EXIT_SUCCESS);
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate #define	mask(s)	(1 << ((s) - 1))
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate static void
174*0Sstevel@tonic-gate usage(void) {
175*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s\n%s\n",
176*0Sstevel@tonic-gate 	    gettext("usage: rsh [ -PN / -PO ] [ -l login ] [ -n ] "
177*0Sstevel@tonic-gate 		"[ -k realm ] [ -a ] [ -x ] [ -f / -F ] host command"),
178*0Sstevel@tonic-gate 	    gettext("       rsh [ -PN / -PO ] [ -l login ] [ -k realm ] "
179*0Sstevel@tonic-gate 		"[ -a ] [ -x ] [ -f / -F ] host"));
180*0Sstevel@tonic-gate 	exit(EXIT_FAILURE);
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate static void
184*0Sstevel@tonic-gate die(const char *message)
185*0Sstevel@tonic-gate {
186*0Sstevel@tonic-gate 	(void) fputs(message, stderr);
187*0Sstevel@tonic-gate 	usage();
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate static void
191*0Sstevel@tonic-gate usage_forward(void)
192*0Sstevel@tonic-gate {
193*0Sstevel@tonic-gate 	die(gettext("rsh: Only one of -f and -F allowed.\n"));
194*0Sstevel@tonic-gate }
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate /*
197*0Sstevel@tonic-gate  * rsh - remote shell
198*0Sstevel@tonic-gate  */
199*0Sstevel@tonic-gate /* VARARGS */
200*0Sstevel@tonic-gate int
201*0Sstevel@tonic-gate main(int argc, char **argv)
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate 	int c, rem;
204*0Sstevel@tonic-gate 	char *cmd, *cp, **ap, buf[RSH_BUFSIZ], **argv0, *args, *args_no_x;
205*0Sstevel@tonic-gate 	char *host = NULL, *user = NULL;
206*0Sstevel@tonic-gate 	int cc;
207*0Sstevel@tonic-gate 	boolean_t asrsh = B_FALSE;
208*0Sstevel@tonic-gate 	struct passwd *pwd;
209*0Sstevel@tonic-gate 	boolean_t readfrom_rem;
210*0Sstevel@tonic-gate 	boolean_t readfrom_rfd2;
211*0Sstevel@tonic-gate 	int one = 1;
212*0Sstevel@tonic-gate 	int omask;
213*0Sstevel@tonic-gate 	boolean_t nflag = B_FALSE;
214*0Sstevel@tonic-gate 	char *krb_realm = NULL;
215*0Sstevel@tonic-gate 	krb5_flags authopts;
216*0Sstevel@tonic-gate 	krb5_error_code status;
217*0Sstevel@tonic-gate 	enum kcmd_proto kcmd_proto = KCMD_NEW_PROTOCOL;
218*0Sstevel@tonic-gate 	uid_t uid = getuid();
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	c = (argc + 1) * sizeof (char *);
221*0Sstevel@tonic-gate 	if ((argv0 = malloc(c)) == NULL) {
222*0Sstevel@tonic-gate 		perror("malloc");
223*0Sstevel@tonic-gate 		return (EXIT_FAILURE);
224*0Sstevel@tonic-gate 	}
225*0Sstevel@tonic-gate 	(void) memcpy(argv0, argv, c);
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	/*
232*0Sstevel@tonic-gate 	 * Determine command name used to invoke to rlogin(1). Users can
233*0Sstevel@tonic-gate 	 * create links named by a host pointing to the binary and type
234*0Sstevel@tonic-gate 	 * "hostname" to log into that host afterwards.
235*0Sstevel@tonic-gate 	 */
236*0Sstevel@tonic-gate 	cmd = strrchr(argv[0], '/');
237*0Sstevel@tonic-gate 	cmd = (cmd != NULL) ? (cmd + 1) : argv[0];
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	/*
240*0Sstevel@tonic-gate 	 *	Add "remsh" as an alias for "rsh" (System III, V networking
241*0Sstevel@tonic-gate 	 *	add-ons often used this name for the remote shell since rsh
242*0Sstevel@tonic-gate 	 *	was already taken for the restricted shell).  Note that this
243*0Sstevel@tonic-gate 	 *	usurps the ability to use "remsh" as the name of a host (by
244*0Sstevel@tonic-gate 	 *	symlinking it to rsh), so we go one step farther:  if the
245*0Sstevel@tonic-gate 	 *	file "/usr/bin/remsh" does not exist, we behave as if "remsh"
246*0Sstevel@tonic-gate 	 *	is a host name.  If it does exist, we accept "remsh" as an
247*0Sstevel@tonic-gate 	 *	"rsh" alias.
248*0Sstevel@tonic-gate 	 */
249*0Sstevel@tonic-gate 	if (strcmp(cmd, "remsh") == 0) {
250*0Sstevel@tonic-gate 		struct stat sb;
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 		if (stat("/usr/bin/remsh", &sb) < 0)
253*0Sstevel@tonic-gate 			host = cmd;
254*0Sstevel@tonic-gate 	} else if (strcmp(cmd, "rsh") != 0) {
255*0Sstevel@tonic-gate 		host = cmd;
256*0Sstevel@tonic-gate 	}
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	/* Handle legacy synopsis "rsh hostname options [command]". */
259*0Sstevel@tonic-gate 	if (host == NULL) {
260*0Sstevel@tonic-gate 		if (argc < 2)
261*0Sstevel@tonic-gate 			usage();
262*0Sstevel@tonic-gate 		if (*argv[1] != '-') {
263*0Sstevel@tonic-gate 			host = argv[1];
264*0Sstevel@tonic-gate 			argc--;
265*0Sstevel@tonic-gate 			argv[1] = argv[0];
266*0Sstevel@tonic-gate 			argv++;
267*0Sstevel@tonic-gate 			asrsh = B_TRUE;
268*0Sstevel@tonic-gate 		}
269*0Sstevel@tonic-gate 	}
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv,
272*0Sstevel@tonic-gate 	    DEBUGOPTSTRING "8AFLP:ade:fk:l:nwx")) != -1) {
273*0Sstevel@tonic-gate 		switch (c) {
274*0Sstevel@tonic-gate #ifdef DEBUG
275*0Sstevel@tonic-gate 		case 'D':
276*0Sstevel@tonic-gate 			portnumber = htons(atoi(optarg));
277*0Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
278*0Sstevel@tonic-gate 			break;
279*0Sstevel@tonic-gate #endif /* DEBUG */
280*0Sstevel@tonic-gate 		case 'F':
281*0Sstevel@tonic-gate 			if (fflag)
282*0Sstevel@tonic-gate 				usage_forward();
283*0Sstevel@tonic-gate 			Fflag = 1;
284*0Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
285*0Sstevel@tonic-gate 			fwdable_done = B_TRUE;
286*0Sstevel@tonic-gate 			break;
287*0Sstevel@tonic-gate 		case 'f':
288*0Sstevel@tonic-gate 			if (Fflag)
289*0Sstevel@tonic-gate 				usage_forward();
290*0Sstevel@tonic-gate 			fflag = 1;
291*0Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
292*0Sstevel@tonic-gate 			fwd_done = B_TRUE;
293*0Sstevel@tonic-gate 			break;
294*0Sstevel@tonic-gate 		case 'P':
295*0Sstevel@tonic-gate 			if (strcmp(optarg, "N") == 0)
296*0Sstevel@tonic-gate 				kcmd_proto = KCMD_NEW_PROTOCOL;
297*0Sstevel@tonic-gate 			else if (strcmp(optarg, "O") == 0)
298*0Sstevel@tonic-gate 				kcmd_proto = KCMD_OLD_PROTOCOL;
299*0Sstevel@tonic-gate 			else
300*0Sstevel@tonic-gate 				die(gettext("rsh: Only -PN or -PO "
301*0Sstevel@tonic-gate 				    "allowed.\n"));
302*0Sstevel@tonic-gate 			if (rcmdoption_done)
303*0Sstevel@tonic-gate 				die(gettext("rsh: Only one of -PN and -PO "
304*0Sstevel@tonic-gate 				    "allowed.\n"));
305*0Sstevel@tonic-gate 			rcmdoption_done = B_TRUE;
306*0Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
307*0Sstevel@tonic-gate 			break;
308*0Sstevel@tonic-gate 		case 'a':
309*0Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
310*0Sstevel@tonic-gate 			break;
311*0Sstevel@tonic-gate 		case 'd':
312*0Sstevel@tonic-gate 			options |= SO_DEBUG;
313*0Sstevel@tonic-gate 			break;
314*0Sstevel@tonic-gate 		case 'k':
315*0Sstevel@tonic-gate 			krb_realm = optarg;
316*0Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
317*0Sstevel@tonic-gate 			break;
318*0Sstevel@tonic-gate 		case 'l':
319*0Sstevel@tonic-gate 			user = optarg;
320*0Sstevel@tonic-gate 			break;
321*0Sstevel@tonic-gate 		case 'n':
322*0Sstevel@tonic-gate 			if (!nflag) {
323*0Sstevel@tonic-gate 				if (close(STDIN_FILENO) < 0) {
324*0Sstevel@tonic-gate 					perror("close");
325*0Sstevel@tonic-gate 					return (EXIT_FAILURE);
326*0Sstevel@tonic-gate 				}
327*0Sstevel@tonic-gate 				/*
328*0Sstevel@tonic-gate 				 * "STDION_FILENO" defined to 0 by POSIX
329*0Sstevel@tonic-gate 				 * and hence the lowest file descriptor.
330*0Sstevel@tonic-gate 				 * So the open(2) below is guaranteed to
331*0Sstevel@tonic-gate 				 * reopen it because we closed it above.
332*0Sstevel@tonic-gate 				 */
333*0Sstevel@tonic-gate 				if (open("/dev/null", O_RDONLY) < 0) {
334*0Sstevel@tonic-gate 					perror("open");
335*0Sstevel@tonic-gate 					return (EXIT_FAILURE);
336*0Sstevel@tonic-gate 				}
337*0Sstevel@tonic-gate 				nflag = B_TRUE;
338*0Sstevel@tonic-gate 			}
339*0Sstevel@tonic-gate 			break;
340*0Sstevel@tonic-gate 		case 'x':
341*0Sstevel@tonic-gate 			encrypt_flag = 1;
342*0Sstevel@tonic-gate 			krb5auth_flag = B_TRUE;
343*0Sstevel@tonic-gate 			encrypt_done = B_TRUE;
344*0Sstevel@tonic-gate 			break;
345*0Sstevel@tonic-gate 		/*
346*0Sstevel@tonic-gate 		 * Ignore the -L, -w, -e and -8 flags to allow aliases with
347*0Sstevel@tonic-gate 		 * rlogin to work. Actually rlogin(1) doesn't understand
348*0Sstevel@tonic-gate 		 * -w either but because "rsh -w hostname command" used
349*0Sstevel@tonic-gate 		 * to work we still accept it.
350*0Sstevel@tonic-gate 		 */
351*0Sstevel@tonic-gate 		case '8':
352*0Sstevel@tonic-gate 		case 'L':
353*0Sstevel@tonic-gate 		case 'e':
354*0Sstevel@tonic-gate 		case 'w':
355*0Sstevel@tonic-gate 		/*
356*0Sstevel@tonic-gate 		 * On the lines of the -L, -w, -e and -8 options above, we
357*0Sstevel@tonic-gate 		 * ignore the -A option too, in order to allow aliases with
358*0Sstevel@tonic-gate 		 * rlogin to work.
359*0Sstevel@tonic-gate 		 *
360*0Sstevel@tonic-gate 		 * Mind you !, the -a option to trigger Kerberos authentication
361*0Sstevel@tonic-gate 		 * in rsh, has a totally different usage in rlogin, its the
362*0Sstevel@tonic-gate 		 * -A option (in rlogin) which needs to be used to talk
363*0Sstevel@tonic-gate 		 * Kerberos.
364*0Sstevel@tonic-gate 		 */
365*0Sstevel@tonic-gate 		case 'A':
366*0Sstevel@tonic-gate 			break;
367*0Sstevel@tonic-gate 		default:
368*0Sstevel@tonic-gate 			usage();
369*0Sstevel@tonic-gate 		}
370*0Sstevel@tonic-gate 	}
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	argc -= optind;
373*0Sstevel@tonic-gate 	argv += optind;
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	if (host == NULL) {
376*0Sstevel@tonic-gate 		if (argc == 0)
377*0Sstevel@tonic-gate 			usage();
378*0Sstevel@tonic-gate 		argc--;
379*0Sstevel@tonic-gate 		host = *argv++;
380*0Sstevel@tonic-gate 		asrsh = B_TRUE;
381*0Sstevel@tonic-gate 	}
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 	if (argc == 0) {
384*0Sstevel@tonic-gate 		(void) setreuid(uid, uid);
385*0Sstevel@tonic-gate 		if (nflag)
386*0Sstevel@tonic-gate 			usage();
387*0Sstevel@tonic-gate 		if (asrsh)
388*0Sstevel@tonic-gate 			*argv0 = "rlogin";
389*0Sstevel@tonic-gate 		(void) execv(rlogin_path, argv0);
390*0Sstevel@tonic-gate 		perror(rlogin_path);
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("No local rlogin "
393*0Sstevel@tonic-gate 				"program found.\n"));
394*0Sstevel@tonic-gate 		return (EXIT_FAILURE);
395*0Sstevel@tonic-gate 	}
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate 	if (__init_suid_priv(0, PRIV_NET_PRIVADDR, NULL) == -1) {
398*0Sstevel@tonic-gate 		(void) fprintf(stderr,
399*0Sstevel@tonic-gate 		    gettext("Insufficient privileges, "
400*0Sstevel@tonic-gate 			"rsh must be set-uid root\n"));
401*0Sstevel@tonic-gate 		return (EXIT_FAILURE);
402*0Sstevel@tonic-gate 	}
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 	pwd = getpwuid(uid);
405*0Sstevel@tonic-gate 	if (pwd == NULL) {
406*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("who are you?\n"));
407*0Sstevel@tonic-gate 		return (EXIT_FAILURE);
408*0Sstevel@tonic-gate 	}
409*0Sstevel@tonic-gate 	if (user == NULL)
410*0Sstevel@tonic-gate 		user = pwd->pw_name;
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 	if (krb5auth_flag) {
413*0Sstevel@tonic-gate 		status = krb5_init_context(&bsd_context);
414*0Sstevel@tonic-gate 		if (status) {
415*0Sstevel@tonic-gate 			com_err("rsh", status, "while initializing krb5");
416*0Sstevel@tonic-gate 			return (EXIT_FAILURE);
417*0Sstevel@tonic-gate 		}
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 		/*
420*0Sstevel@tonic-gate 		 * Get our local realm to look up local realm options.
421*0Sstevel@tonic-gate 		 */
422*0Sstevel@tonic-gate 		status = krb5_get_default_realm(bsd_context, &realmdef[1]);
423*0Sstevel@tonic-gate 		if (status) {
424*0Sstevel@tonic-gate 			com_err("rsh", status,
425*0Sstevel@tonic-gate 				gettext("while getting default realm"));
426*0Sstevel@tonic-gate 			return (EXIT_FAILURE);
427*0Sstevel@tonic-gate 		}
428*0Sstevel@tonic-gate 		/*
429*0Sstevel@tonic-gate 		 * Check the realms section in krb5.conf for encryption,
430*0Sstevel@tonic-gate 		 * forward & forwardable info
431*0Sstevel@tonic-gate 		 */
432*0Sstevel@tonic-gate 		profile_get_options_boolean(bsd_context->profile, realmdef,
433*0Sstevel@tonic-gate 						option);
434*0Sstevel@tonic-gate 		/*
435*0Sstevel@tonic-gate 		 * Check the appdefaults section
436*0Sstevel@tonic-gate 		 */
437*0Sstevel@tonic-gate 		profile_get_options_boolean(bsd_context->profile, appdef,
438*0Sstevel@tonic-gate 						option);
439*0Sstevel@tonic-gate 		profile_get_options_string(bsd_context->profile, appdef,
440*0Sstevel@tonic-gate 						rcmdversion);
441*0Sstevel@tonic-gate 		/*
442*0Sstevel@tonic-gate 		 * Set the *_flag variables, if the corresponding *_done are
443*0Sstevel@tonic-gate 		 * set to 1, because we dont want the config file values
444*0Sstevel@tonic-gate 		 * overriding the command line options.
445*0Sstevel@tonic-gate 		 */
446*0Sstevel@tonic-gate 		if (encrypt_done)
447*0Sstevel@tonic-gate 			encrypt_flag = 1;
448*0Sstevel@tonic-gate 		if (fwd_done) {
449*0Sstevel@tonic-gate 			fflag = 1;
450*0Sstevel@tonic-gate 			Fflag = 0;
451*0Sstevel@tonic-gate 		} else if (fwdable_done) {
452*0Sstevel@tonic-gate 			Fflag = 1;
453*0Sstevel@tonic-gate 			fflag = 0;
454*0Sstevel@tonic-gate 		}
455*0Sstevel@tonic-gate 		if (!rcmdoption_done && (rcmdproto != NULL)) {
456*0Sstevel@tonic-gate 			if (strncmp(rcmdproto, "rcmdv2", 6) == 0) {
457*0Sstevel@tonic-gate 				kcmd_proto = KCMD_NEW_PROTOCOL;
458*0Sstevel@tonic-gate 			} else if (strncmp(rcmdproto, "rcmdv1", 6) == 0) {
459*0Sstevel@tonic-gate 				kcmd_proto = KCMD_OLD_PROTOCOL;
460*0Sstevel@tonic-gate 			} else {
461*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("Unrecognized "
462*0Sstevel@tonic-gate 					"KCMD protocol (%s)"), rcmdproto);
463*0Sstevel@tonic-gate 				return (EXIT_FAILURE);
464*0Sstevel@tonic-gate 			}
465*0Sstevel@tonic-gate 		}
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 		if (encrypt_flag && (!krb5_privacy_allowed())) {
469*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("rsh: Encryption not "
470*0Sstevel@tonic-gate 				"supported.\n"));
471*0Sstevel@tonic-gate 			return (EXIT_FAILURE);
472*0Sstevel@tonic-gate 		}
473*0Sstevel@tonic-gate 	}
474*0Sstevel@tonic-gate 
475*0Sstevel@tonic-gate 	/*
476*0Sstevel@tonic-gate 	 * Connect with the service (shell/kshell) on the daemon side
477*0Sstevel@tonic-gate 	 */
478*0Sstevel@tonic-gate 	if (portnumber == 0) {
479*0Sstevel@tonic-gate 		while (!init_service(krb5auth_flag)) {
480*0Sstevel@tonic-gate 			/*
481*0Sstevel@tonic-gate 			 * Connecting to the 'kshell' service failed,
482*0Sstevel@tonic-gate 			 * fallback to normal rsh; Reset all KRB5 flags
483*0Sstevel@tonic-gate 			 * and connect to 'shell' service on the server
484*0Sstevel@tonic-gate 			 */
485*0Sstevel@tonic-gate 			krb5auth_flag = B_FALSE;
486*0Sstevel@tonic-gate 			encrypt_flag = fflag = Fflag = 0;
487*0Sstevel@tonic-gate 		}
488*0Sstevel@tonic-gate 	}
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 	cc = encrypt_flag ? strlen(dash_x) : 0;
491*0Sstevel@tonic-gate 	for (ap = argv; *ap != NULL; ap++)
492*0Sstevel@tonic-gate 		cc += strlen(*ap) + 1;
493*0Sstevel@tonic-gate 	cp = args = malloc(cc);
494*0Sstevel@tonic-gate 	if (cp == NULL)
495*0Sstevel@tonic-gate 		perror("malloc");
496*0Sstevel@tonic-gate 	if (encrypt_flag) {
497*0Sstevel@tonic-gate 		int length;
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 		length = strlcpy(args, dash_x, cc);
500*0Sstevel@tonic-gate 		cp += length;
501*0Sstevel@tonic-gate 		cc -= length;
502*0Sstevel@tonic-gate 	}
503*0Sstevel@tonic-gate 	args_no_x = args;
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 	for (ap = argv; *ap != NULL; ap++) {
506*0Sstevel@tonic-gate 		int length;
507*0Sstevel@tonic-gate 
508*0Sstevel@tonic-gate 		length = strlcpy(cp, *ap, cc);
509*0Sstevel@tonic-gate 		assert(length < cc);
510*0Sstevel@tonic-gate 		cp += length;
511*0Sstevel@tonic-gate 		cc -= length;
512*0Sstevel@tonic-gate 		if (ap[1] != NULL) {
513*0Sstevel@tonic-gate 			*cp++ = ' ';
514*0Sstevel@tonic-gate 			cc--;
515*0Sstevel@tonic-gate 		}
516*0Sstevel@tonic-gate 	}
517*0Sstevel@tonic-gate 
518*0Sstevel@tonic-gate 	if (krb5auth_flag) {
519*0Sstevel@tonic-gate 		authopts = AP_OPTS_MUTUAL_REQUIRED;
520*0Sstevel@tonic-gate 		/*
521*0Sstevel@tonic-gate 		 * Piggy-back forwarding flags on top of authopts;
522*0Sstevel@tonic-gate 		 * they will be reset in kcmd
523*0Sstevel@tonic-gate 		 */
524*0Sstevel@tonic-gate 		if (fflag || Fflag)
525*0Sstevel@tonic-gate 			authopts |= OPTS_FORWARD_CREDS;
526*0Sstevel@tonic-gate 		if (Fflag)
527*0Sstevel@tonic-gate 			authopts |= OPTS_FORWARDABLE_CREDS;
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 		status = kcmd(&rem, &host, portnumber,
530*0Sstevel@tonic-gate 				pwd->pw_name, user,
531*0Sstevel@tonic-gate 				args, &rfd2, "host", krb_realm,
532*0Sstevel@tonic-gate 				bsd_context, &auth_context, &cred,
533*0Sstevel@tonic-gate 				NULL,	/* No need for sequence number */
534*0Sstevel@tonic-gate 				NULL,	/* No need for server seq # */
535*0Sstevel@tonic-gate 				authopts,
536*0Sstevel@tonic-gate 				1,	/* Always set anyport */
537*0Sstevel@tonic-gate 				&kcmd_proto);
538*0Sstevel@tonic-gate 		if (status != 0) {
539*0Sstevel@tonic-gate 			/*
540*0Sstevel@tonic-gate 			 * If new protocol requested, we dont fallback to
541*0Sstevel@tonic-gate 			 * less secure ones.
542*0Sstevel@tonic-gate 			 */
543*0Sstevel@tonic-gate 			if (kcmd_proto == KCMD_NEW_PROTOCOL) {
544*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("rsh: kcmdv2 "
545*0Sstevel@tonic-gate 					"to host %s failed - %s\n"
546*0Sstevel@tonic-gate 					"Fallback to normal rsh denied."),
547*0Sstevel@tonic-gate 					host, error_message(status));
548*0Sstevel@tonic-gate 				return (EXIT_FAILURE);
549*0Sstevel@tonic-gate 			}
550*0Sstevel@tonic-gate 			/* check NO_TKT_FILE or equivalent... */
551*0Sstevel@tonic-gate 			if (status != -1) {
552*0Sstevel@tonic-gate 				(void) fprintf(stderr,
553*0Sstevel@tonic-gate 				gettext("rsh: kcmd to host %s failed - %s\n"
554*0Sstevel@tonic-gate 				"trying normal rsh...\n\n"),
555*0Sstevel@tonic-gate 				host, error_message(status));
556*0Sstevel@tonic-gate 			} else {
557*0Sstevel@tonic-gate 				(void) fprintf(stderr,
558*0Sstevel@tonic-gate 					gettext("trying normal rsh...\n"));
559*0Sstevel@tonic-gate 			}
560*0Sstevel@tonic-gate 			/*
561*0Sstevel@tonic-gate 			 * kcmd() failed, so we now fallback to normal rsh,
562*0Sstevel@tonic-gate 			 * after resetting the KRB5 flags and the 'args' array
563*0Sstevel@tonic-gate 			 */
564*0Sstevel@tonic-gate 			krb5auth_flag = B_FALSE;
565*0Sstevel@tonic-gate 			encrypt_flag = fflag = Fflag = 0;
566*0Sstevel@tonic-gate 			args = args_no_x;
567*0Sstevel@tonic-gate 			(void) init_service(B_FALSE);
568*0Sstevel@tonic-gate 		} else {
569*0Sstevel@tonic-gate 			/*
570*0Sstevel@tonic-gate 			 * Set up buffers for desread and deswrite.
571*0Sstevel@tonic-gate 			 */
572*0Sstevel@tonic-gate 			desinbuf.data = des_inbuf;
573*0Sstevel@tonic-gate 			desoutbuf.data = des_outbuf;
574*0Sstevel@tonic-gate 			desinbuf.length = sizeof (des_inbuf);
575*0Sstevel@tonic-gate 			desoutbuf.length = sizeof (des_outbuf);
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 			session_key = &cred->keyblock;
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate 			if (kcmd_proto == KCMD_NEW_PROTOCOL) {
580*0Sstevel@tonic-gate 				status = krb5_auth_con_getlocalsubkey(
581*0Sstevel@tonic-gate 				    bsd_context,
582*0Sstevel@tonic-gate 				    auth_context,
583*0Sstevel@tonic-gate 				    &session_key);
584*0Sstevel@tonic-gate 				if (status) {
585*0Sstevel@tonic-gate 					com_err("rsh", status,
586*0Sstevel@tonic-gate 					    "determining subkey for session");
587*0Sstevel@tonic-gate 					return (EXIT_FAILURE);
588*0Sstevel@tonic-gate 				}
589*0Sstevel@tonic-gate 				if (session_key == NULL) {
590*0Sstevel@tonic-gate 					com_err("rsh", 0, "no subkey "
591*0Sstevel@tonic-gate 					    "negotiated for connection");
592*0Sstevel@tonic-gate 					return (EXIT_FAILURE);
593*0Sstevel@tonic-gate 				}
594*0Sstevel@tonic-gate 			}
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 			eblock.crypto_entry = session_key->enctype;
597*0Sstevel@tonic-gate 			eblock.key = (krb5_keyblock *)session_key;
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 			init_encrypt(encrypt_flag, bsd_context, kcmd_proto,
600*0Sstevel@tonic-gate 			    &desinbuf, &desoutbuf, CLIENT, &eblock);
601*0Sstevel@tonic-gate 			if (encrypt_flag) {
602*0Sstevel@tonic-gate 				char *s = gettext("This rsh session is using "
603*0Sstevel@tonic-gate 				    "encryption for all data transmissions.");
604*0Sstevel@tonic-gate 				(void) write(STDERR_FILENO, s, strlen(s));
605*0Sstevel@tonic-gate 				(void) write(STDERR_FILENO, "\r\n", 2);
606*0Sstevel@tonic-gate 			}
607*0Sstevel@tonic-gate 		}
608*0Sstevel@tonic-gate 	}
609*0Sstevel@tonic-gate 
610*0Sstevel@tonic-gate 	/*
611*0Sstevel@tonic-gate 	 * Don't merge this with the "if" statement above because
612*0Sstevel@tonic-gate 	 * "krb5auth_flag" might be set to false inside it.
613*0Sstevel@tonic-gate 	 */
614*0Sstevel@tonic-gate 	if (!krb5auth_flag) {
615*0Sstevel@tonic-gate 		rem = rcmd_af(&host, portnumber, pwd->pw_name, user, args,
616*0Sstevel@tonic-gate 		    &rfd2, AF_INET6);
617*0Sstevel@tonic-gate 		if (rem < 0)
618*0Sstevel@tonic-gate 			return (EXIT_FAILURE);
619*0Sstevel@tonic-gate 	}
620*0Sstevel@tonic-gate 	__priv_relinquish();
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate 	if (rfd2 < 0) {
623*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rsh: can't establish "
624*0Sstevel@tonic-gate 				"stderr\n"));
625*0Sstevel@tonic-gate 		return (EXIT_FAILURE);
626*0Sstevel@tonic-gate 	}
627*0Sstevel@tonic-gate 	if (options & SO_DEBUG) {
628*0Sstevel@tonic-gate 		if (setsockopt(rem, SOL_SOCKET, SO_DEBUG, (char *)&one,
629*0Sstevel@tonic-gate 		    sizeof (one)) < 0)
630*0Sstevel@tonic-gate 			perror("rsh: setsockopt (stdin)");
631*0Sstevel@tonic-gate 		if (setsockopt(rfd2, SOL_SOCKET, SO_DEBUG, (char *)&one,
632*0Sstevel@tonic-gate 		    sizeof (one)) < 0)
633*0Sstevel@tonic-gate 			perror("rsh: setsockopt (stderr)");
634*0Sstevel@tonic-gate 	}
635*0Sstevel@tonic-gate 	omask = sigblock(mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate 	if (sigdisp(SIGINT) != SIG_IGN)
638*0Sstevel@tonic-gate 		(void) sigset(SIGINT, sendsig);
639*0Sstevel@tonic-gate 	if (sigdisp(SIGQUIT) != SIG_IGN)
640*0Sstevel@tonic-gate 		(void) sigset(SIGQUIT, sendsig);
641*0Sstevel@tonic-gate 	if (sigdisp(SIGTERM) != SIG_IGN)
642*0Sstevel@tonic-gate 		(void) sigset(SIGTERM, sendsig);
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate 	if (nflag) {
645*0Sstevel@tonic-gate 		(void) shutdown(rem, SHUT_WR);
646*0Sstevel@tonic-gate 	} else {
647*0Sstevel@tonic-gate 		child_pid = fork();
648*0Sstevel@tonic-gate 		if (child_pid < 0) {
649*0Sstevel@tonic-gate 			perror("rsh: fork");
650*0Sstevel@tonic-gate 			return (EXIT_FAILURE);
651*0Sstevel@tonic-gate 		}
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate 		if (!encrypt_flag) {
654*0Sstevel@tonic-gate 			(void) ioctl(rfd2, FIONBIO, &one);
655*0Sstevel@tonic-gate 			(void) ioctl(rem, FIONBIO, &one);
656*0Sstevel@tonic-gate 		}
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate 		if (child_pid == 0) {
659*0Sstevel@tonic-gate 			/* Child */
660*0Sstevel@tonic-gate 			fd_set remset;
661*0Sstevel@tonic-gate 			char *bp;
662*0Sstevel@tonic-gate 			int  wc;
663*0Sstevel@tonic-gate 			(void) close(rfd2);
664*0Sstevel@tonic-gate 		reread:
665*0Sstevel@tonic-gate 			errno = 0;
666*0Sstevel@tonic-gate 			cc = read(0, buf, sizeof (buf));
667*0Sstevel@tonic-gate 			if (cc <= 0)
668*0Sstevel@tonic-gate 				goto done;
669*0Sstevel@tonic-gate 			bp = buf;
670*0Sstevel@tonic-gate 		rewrite:
671*0Sstevel@tonic-gate 			FD_ZERO(&remset);
672*0Sstevel@tonic-gate 			FD_SET(rem, &remset);
673*0Sstevel@tonic-gate 			if (select(rem + 1, NULL, &remset, NULL, NULL) < 0) {
674*0Sstevel@tonic-gate 				if (errno != EINTR) {
675*0Sstevel@tonic-gate 					perror("rsh: select");
676*0Sstevel@tonic-gate 					return (EXIT_FAILURE);
677*0Sstevel@tonic-gate 				}
678*0Sstevel@tonic-gate 				goto rewrite;
679*0Sstevel@tonic-gate 			}
680*0Sstevel@tonic-gate 			if (!FD_ISSET(rem, &remset))
681*0Sstevel@tonic-gate 				goto rewrite;
682*0Sstevel@tonic-gate 			writeiv = B_FALSE;
683*0Sstevel@tonic-gate 			wc = desrshwrite(rem, bp, cc);
684*0Sstevel@tonic-gate 			if (wc < 0) {
685*0Sstevel@tonic-gate 				if (errno == EWOULDBLOCK)
686*0Sstevel@tonic-gate 					goto rewrite;
687*0Sstevel@tonic-gate 				goto done;
688*0Sstevel@tonic-gate 			}
689*0Sstevel@tonic-gate 			cc -= wc; bp += wc;
690*0Sstevel@tonic-gate 			if (cc == 0)
691*0Sstevel@tonic-gate 				goto reread;
692*0Sstevel@tonic-gate 			goto rewrite;
693*0Sstevel@tonic-gate 		done:
694*0Sstevel@tonic-gate 			(void) shutdown(rem, SHUT_WR);
695*0Sstevel@tonic-gate 			return (EXIT_SUCCESS);
696*0Sstevel@tonic-gate 		}
697*0Sstevel@tonic-gate 	}
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate #define	MAX(a, b)	(((a) > (b)) ? (a) : (b))
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate 	sigsetmask(omask);
702*0Sstevel@tonic-gate 	readfrom_rem = B_TRUE;
703*0Sstevel@tonic-gate 	readfrom_rfd2 = B_TRUE;
704*0Sstevel@tonic-gate 	(void) sigset(SIGPIPE, sigpipehandler);
705*0Sstevel@tonic-gate 	do {
706*0Sstevel@tonic-gate 		fd_set readyset;
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate 		FD_ZERO(&readyset);
709*0Sstevel@tonic-gate 		if (readfrom_rem)
710*0Sstevel@tonic-gate 			FD_SET(rem, &readyset);
711*0Sstevel@tonic-gate 		if (readfrom_rfd2)
712*0Sstevel@tonic-gate 			FD_SET(rfd2, &readyset);
713*0Sstevel@tonic-gate 		if (select(MAX(rem, rfd2) + 1, &readyset, NULL, NULL,
714*0Sstevel@tonic-gate 		    NULL) < 0) {
715*0Sstevel@tonic-gate 			if (errno != EINTR) {
716*0Sstevel@tonic-gate 				perror("rsh: select");
717*0Sstevel@tonic-gate 				return (EXIT_FAILURE);
718*0Sstevel@tonic-gate 			}
719*0Sstevel@tonic-gate 			continue;
720*0Sstevel@tonic-gate 		}
721*0Sstevel@tonic-gate 		if (FD_ISSET(rfd2, &readyset)) {
722*0Sstevel@tonic-gate 			errno = 0;
723*0Sstevel@tonic-gate 			readiv = B_TRUE;
724*0Sstevel@tonic-gate 			cc = desrshread(rfd2, buf, sizeof (buf));
725*0Sstevel@tonic-gate 			if (cc <= 0) {
726*0Sstevel@tonic-gate 				if (errno != EWOULDBLOCK)
727*0Sstevel@tonic-gate 					readfrom_rfd2 = B_FALSE;
728*0Sstevel@tonic-gate 			} else {
729*0Sstevel@tonic-gate 				(void) write(STDERR_FILENO, buf, cc);
730*0Sstevel@tonic-gate 			}
731*0Sstevel@tonic-gate 		}
732*0Sstevel@tonic-gate 		if (FD_ISSET(rem, &readyset)) {
733*0Sstevel@tonic-gate 			errno = 0;
734*0Sstevel@tonic-gate 			readiv = B_FALSE;
735*0Sstevel@tonic-gate 			cc = desrshread(rem, buf, sizeof (buf));
736*0Sstevel@tonic-gate 			if (cc <= 0) {
737*0Sstevel@tonic-gate 				if (errno != EWOULDBLOCK)
738*0Sstevel@tonic-gate 					readfrom_rem = B_FALSE;
739*0Sstevel@tonic-gate 			} else
740*0Sstevel@tonic-gate 				(void) write(STDOUT_FILENO, buf, cc);
741*0Sstevel@tonic-gate 		}
742*0Sstevel@tonic-gate 	} while (readfrom_rem || readfrom_rfd2);
743*0Sstevel@tonic-gate 
744*0Sstevel@tonic-gate 	if (!nflag)
745*0Sstevel@tonic-gate 		(void) kill(child_pid, SIGKILL);
746*0Sstevel@tonic-gate 	return (EXIT_SUCCESS);
747*0Sstevel@tonic-gate }
748*0Sstevel@tonic-gate 
749*0Sstevel@tonic-gate static void
750*0Sstevel@tonic-gate sendsig(int signum)
751*0Sstevel@tonic-gate {
752*0Sstevel@tonic-gate 	char	buffer;
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 	writeiv = B_TRUE;
755*0Sstevel@tonic-gate 	buffer = (char)signum;
756*0Sstevel@tonic-gate 	(void) desrshwrite(rfd2, &buffer, 1);
757*0Sstevel@tonic-gate }
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate static boolean_t
760*0Sstevel@tonic-gate init_service(boolean_t krb5flag)
761*0Sstevel@tonic-gate {
762*0Sstevel@tonic-gate 	struct servent *sp;
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate 	if (krb5flag) {
765*0Sstevel@tonic-gate 		sp = getservbyname("kshell", "tcp");
766*0Sstevel@tonic-gate 		if (sp == NULL) {
767*0Sstevel@tonic-gate 			(void) fprintf(stderr,
768*0Sstevel@tonic-gate 				gettext("rsh: kshell/tcp: unknown service.\n"
769*0Sstevel@tonic-gate 				"trying normal shell/tcp service\n"));
770*0Sstevel@tonic-gate 			return (B_FALSE);
771*0Sstevel@tonic-gate 		}
772*0Sstevel@tonic-gate 	} else {
773*0Sstevel@tonic-gate 		sp = getservbyname("shell", "tcp");
774*0Sstevel@tonic-gate 		if (sp == NULL) {
775*0Sstevel@tonic-gate 			portnumber = htons(IPPORT_CMDSERVER);
776*0Sstevel@tonic-gate 			return (B_TRUE);
777*0Sstevel@tonic-gate 		}
778*0Sstevel@tonic-gate 	}
779*0Sstevel@tonic-gate 
780*0Sstevel@tonic-gate 	portnumber = sp->s_port;
781*0Sstevel@tonic-gate 	return (B_TRUE);
782*0Sstevel@tonic-gate }
783*0Sstevel@tonic-gate 
784*0Sstevel@tonic-gate static int
785*0Sstevel@tonic-gate desrshread(int fd, char *buf, int len)
786*0Sstevel@tonic-gate {
787*0Sstevel@tonic-gate 	return (desread(fd, buf, len, readiv ? 1 : 0));
788*0Sstevel@tonic-gate }
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate static int
791*0Sstevel@tonic-gate desrshwrite(int fd, char *buf, int len)
792*0Sstevel@tonic-gate {
793*0Sstevel@tonic-gate 	return (deswrite(fd, buf, len, writeiv ? 1 : 0));
794*0Sstevel@tonic-gate }
795