xref: /titanic_51/usr/src/lib/libbc/libc/net/rexec.c (revision 5d54f3d8999eac1762fe0a8c7177d20f1f201fae)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/socket.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <netinet/in.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <netdb.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
37*5d54f3d8Smuffin #include <strings.h>
38*5d54f3d8Smuffin #include <unistd.h>
397c478bd9Sstevel@tonic-gate 
40*5d54f3d8Smuffin 
41*5d54f3d8Smuffin char	*getpass();
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate int
447c478bd9Sstevel@tonic-gate rexec(
457c478bd9Sstevel@tonic-gate 	char **ahost,
467c478bd9Sstevel@tonic-gate 	unsigned short rport,
47*5d54f3d8Smuffin 	char *name,
48*5d54f3d8Smuffin 	char *pass,
49*5d54f3d8Smuffin 	char *cmd,
507c478bd9Sstevel@tonic-gate 	int *fd2p)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	int s, timo = 1, s3;
537c478bd9Sstevel@tonic-gate 	struct sockaddr_in sin, sin2, from;
547c478bd9Sstevel@tonic-gate 	char c;
557c478bd9Sstevel@tonic-gate 	u_short port;
567c478bd9Sstevel@tonic-gate 	struct hostent *hp;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	hp = gethostbyname(*ahost);
597c478bd9Sstevel@tonic-gate 	if (hp == 0) {
607c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: unknown host\n", *ahost);
617c478bd9Sstevel@tonic-gate 		return (-1);
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 	*ahost = hp->h_name;
647c478bd9Sstevel@tonic-gate 	_ruserpass(hp->h_name, &name, &pass);
657c478bd9Sstevel@tonic-gate retry:
667c478bd9Sstevel@tonic-gate 	s = socket(AF_INET, SOCK_STREAM, 0);
677c478bd9Sstevel@tonic-gate 	if (s < 0) {
687c478bd9Sstevel@tonic-gate 		perror("rexec: socket");
697c478bd9Sstevel@tonic-gate 		return (-1);
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 	sin.sin_family = hp->h_addrtype;
727c478bd9Sstevel@tonic-gate 	sin.sin_port = rport;
737c478bd9Sstevel@tonic-gate 	bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
747c478bd9Sstevel@tonic-gate 	if (connect(s, &sin, sizeof(sin)) < 0) {
757c478bd9Sstevel@tonic-gate 		if (errno == ECONNREFUSED && timo <= 16) {
767c478bd9Sstevel@tonic-gate 			(void) close(s);
777c478bd9Sstevel@tonic-gate 			sleep(timo);
787c478bd9Sstevel@tonic-gate 			timo *= 2;
797c478bd9Sstevel@tonic-gate 			goto retry;
807c478bd9Sstevel@tonic-gate 		}
817c478bd9Sstevel@tonic-gate 		perror(hp->h_name);
827c478bd9Sstevel@tonic-gate 		(void) close(s);
837c478bd9Sstevel@tonic-gate 		return (-1);
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 	if (fd2p == 0) {
867c478bd9Sstevel@tonic-gate 		(void) write(s, "", 1);
877c478bd9Sstevel@tonic-gate 		port = 0;
887c478bd9Sstevel@tonic-gate 	} else {
897c478bd9Sstevel@tonic-gate 		char num[8];
907c478bd9Sstevel@tonic-gate 		int s2, sin2len;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 		s2 = socket(AF_INET, SOCK_STREAM, 0);
937c478bd9Sstevel@tonic-gate 		if (s2 < 0) {
947c478bd9Sstevel@tonic-gate 			(void) close(s);
957c478bd9Sstevel@tonic-gate 			return (-1);
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 		listen(s2, 1);
987c478bd9Sstevel@tonic-gate 		sin2len = sizeof (sin2);
997c478bd9Sstevel@tonic-gate 		if (getsockname(s2, (char *)&sin2, &sin2len) < 0 ||
1007c478bd9Sstevel@tonic-gate 		  sin2len != sizeof (sin2)) {
1017c478bd9Sstevel@tonic-gate 			perror("getsockname");
1027c478bd9Sstevel@tonic-gate 			(void) close(s2);
1037c478bd9Sstevel@tonic-gate 			goto bad;
1047c478bd9Sstevel@tonic-gate 		}
1057c478bd9Sstevel@tonic-gate 		port = ntohs((u_short)sin2.sin_port);
1067c478bd9Sstevel@tonic-gate 		(void) sprintf(num, "%u", port);
1077c478bd9Sstevel@tonic-gate 		(void) write(s, num, strlen(num)+1);
1087c478bd9Sstevel@tonic-gate 		{ int len = sizeof (from);
1097c478bd9Sstevel@tonic-gate 		  s3 = accept(s2, &from, &len);
1107c478bd9Sstevel@tonic-gate 		  close(s2);
1117c478bd9Sstevel@tonic-gate 		  if (s3 < 0) {
1127c478bd9Sstevel@tonic-gate 			perror("accept");
1137c478bd9Sstevel@tonic-gate 			port = 0;
1147c478bd9Sstevel@tonic-gate 			goto bad;
1157c478bd9Sstevel@tonic-gate 		  }
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 		*fd2p = s3;
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	(void) write(s, name, strlen(name) + 1);
1207c478bd9Sstevel@tonic-gate 	/* should public key encypt the password here */
1217c478bd9Sstevel@tonic-gate 	(void) write(s, pass, strlen(pass) + 1);
1227c478bd9Sstevel@tonic-gate 	(void) write(s, cmd, strlen(cmd) + 1);
1237c478bd9Sstevel@tonic-gate 	if (read(s, &c, 1) != 1) {
1247c478bd9Sstevel@tonic-gate 		perror(*ahost);
1257c478bd9Sstevel@tonic-gate 		goto bad;
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	if (c != 0) {
1287c478bd9Sstevel@tonic-gate 		while (read(s, &c, 1) == 1) {
1297c478bd9Sstevel@tonic-gate 			(void) write(2, &c, 1);
1307c478bd9Sstevel@tonic-gate 			if (c == '\n')
1317c478bd9Sstevel@tonic-gate 				break;
1327c478bd9Sstevel@tonic-gate 		}
1337c478bd9Sstevel@tonic-gate 		goto bad;
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	return (s);
1367c478bd9Sstevel@tonic-gate bad:
1377c478bd9Sstevel@tonic-gate 	if (port)
1387c478bd9Sstevel@tonic-gate 		(void) close(*fd2p);
1397c478bd9Sstevel@tonic-gate 	(void) close(s);
1407c478bd9Sstevel@tonic-gate 	return (-1);
1417c478bd9Sstevel@tonic-gate }
142