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