10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 1989 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/socket.h>
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <netinet/in.h>
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <netdb.h>
360Sstevel@tonic-gate #include <errno.h>
37*722Smuffin #include <strings.h>
38*722Smuffin #include <unistd.h>
390Sstevel@tonic-gate
40*722Smuffin
41*722Smuffin char *getpass();
420Sstevel@tonic-gate
430Sstevel@tonic-gate int
rexec(char ** ahost,unsigned short rport,char * name,char * pass,char * cmd,int * fd2p)440Sstevel@tonic-gate rexec(
450Sstevel@tonic-gate char **ahost,
460Sstevel@tonic-gate unsigned short rport,
47*722Smuffin char *name,
48*722Smuffin char *pass,
49*722Smuffin char *cmd,
500Sstevel@tonic-gate int *fd2p)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate int s, timo = 1, s3;
530Sstevel@tonic-gate struct sockaddr_in sin, sin2, from;
540Sstevel@tonic-gate char c;
550Sstevel@tonic-gate u_short port;
560Sstevel@tonic-gate struct hostent *hp;
570Sstevel@tonic-gate
580Sstevel@tonic-gate hp = gethostbyname(*ahost);
590Sstevel@tonic-gate if (hp == 0) {
600Sstevel@tonic-gate fprintf(stderr, "%s: unknown host\n", *ahost);
610Sstevel@tonic-gate return (-1);
620Sstevel@tonic-gate }
630Sstevel@tonic-gate *ahost = hp->h_name;
640Sstevel@tonic-gate _ruserpass(hp->h_name, &name, &pass);
650Sstevel@tonic-gate retry:
660Sstevel@tonic-gate s = socket(AF_INET, SOCK_STREAM, 0);
670Sstevel@tonic-gate if (s < 0) {
680Sstevel@tonic-gate perror("rexec: socket");
690Sstevel@tonic-gate return (-1);
700Sstevel@tonic-gate }
710Sstevel@tonic-gate sin.sin_family = hp->h_addrtype;
720Sstevel@tonic-gate sin.sin_port = rport;
730Sstevel@tonic-gate bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
740Sstevel@tonic-gate if (connect(s, &sin, sizeof(sin)) < 0) {
750Sstevel@tonic-gate if (errno == ECONNREFUSED && timo <= 16) {
760Sstevel@tonic-gate (void) close(s);
770Sstevel@tonic-gate sleep(timo);
780Sstevel@tonic-gate timo *= 2;
790Sstevel@tonic-gate goto retry;
800Sstevel@tonic-gate }
810Sstevel@tonic-gate perror(hp->h_name);
820Sstevel@tonic-gate (void) close(s);
830Sstevel@tonic-gate return (-1);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate if (fd2p == 0) {
860Sstevel@tonic-gate (void) write(s, "", 1);
870Sstevel@tonic-gate port = 0;
880Sstevel@tonic-gate } else {
890Sstevel@tonic-gate char num[8];
900Sstevel@tonic-gate int s2, sin2len;
910Sstevel@tonic-gate
920Sstevel@tonic-gate s2 = socket(AF_INET, SOCK_STREAM, 0);
930Sstevel@tonic-gate if (s2 < 0) {
940Sstevel@tonic-gate (void) close(s);
950Sstevel@tonic-gate return (-1);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate listen(s2, 1);
980Sstevel@tonic-gate sin2len = sizeof (sin2);
990Sstevel@tonic-gate if (getsockname(s2, (char *)&sin2, &sin2len) < 0 ||
1000Sstevel@tonic-gate sin2len != sizeof (sin2)) {
1010Sstevel@tonic-gate perror("getsockname");
1020Sstevel@tonic-gate (void) close(s2);
1030Sstevel@tonic-gate goto bad;
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate port = ntohs((u_short)sin2.sin_port);
1060Sstevel@tonic-gate (void) sprintf(num, "%u", port);
1070Sstevel@tonic-gate (void) write(s, num, strlen(num)+1);
1080Sstevel@tonic-gate { int len = sizeof (from);
1090Sstevel@tonic-gate s3 = accept(s2, &from, &len);
1100Sstevel@tonic-gate close(s2);
1110Sstevel@tonic-gate if (s3 < 0) {
1120Sstevel@tonic-gate perror("accept");
1130Sstevel@tonic-gate port = 0;
1140Sstevel@tonic-gate goto bad;
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate *fd2p = s3;
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate (void) write(s, name, strlen(name) + 1);
1200Sstevel@tonic-gate /* should public key encypt the password here */
1210Sstevel@tonic-gate (void) write(s, pass, strlen(pass) + 1);
1220Sstevel@tonic-gate (void) write(s, cmd, strlen(cmd) + 1);
1230Sstevel@tonic-gate if (read(s, &c, 1) != 1) {
1240Sstevel@tonic-gate perror(*ahost);
1250Sstevel@tonic-gate goto bad;
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate if (c != 0) {
1280Sstevel@tonic-gate while (read(s, &c, 1) == 1) {
1290Sstevel@tonic-gate (void) write(2, &c, 1);
1300Sstevel@tonic-gate if (c == '\n')
1310Sstevel@tonic-gate break;
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate goto bad;
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate return (s);
1360Sstevel@tonic-gate bad:
1370Sstevel@tonic-gate if (port)
1380Sstevel@tonic-gate (void) close(*fd2p);
1390Sstevel@tonic-gate (void) close(s);
1400Sstevel@tonic-gate return (-1);
1410Sstevel@tonic-gate }
142