added LIBC_SCCS condition for sccs ids
[unix-history] / usr / src / lib / libcompat / 4.3 / rexec.c
CommitLineData
8ea4199d
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
2ce81398
DS
7#if defined(LIBC_SCCS) && !defined(lint)
8static char sccsid[] = "@(#)rexec.c 5.2 (Berkeley) %G%";
9#endif LIBC_SCCS and not lint
08081c0d 10
08081c0d
KM
11#include <sys/types.h>
12#include <sys/socket.h>
40d6b022
SL
13
14#include <netinet/in.h>
15
16#include <stdio.h>
17#include <netdb.h>
95091ecc 18#include <errno.h>
08081c0d 19
95091ecc
BJ
20extern errno;
21char *index(), *sprintf();
22int rexecoptions;
23char *getpass(), *getlogin();
08081c0d 24
95091ecc
BJ
25rexec(ahost, rport, name, pass, cmd, fd2p)
26 char **ahost;
27 int rport;
28 char *name, *pass, *cmd;
29 int *fd2p;
08081c0d 30{
40d6b022 31 int s, timo = 1, s3;
95091ecc
BJ
32 struct sockaddr_in sin, sin2, from;
33 char c;
34 short port;
40d6b022 35 struct hostent *hp;
08081c0d 36
40d6b022
SL
37 hp = gethostbyname(*ahost);
38 if (hp == 0) {
95091ecc 39 fprintf(stderr, "%s: unknown host\n", *ahost);
08081c0d
KM
40 return (-1);
41 }
40d6b022
SL
42 *ahost = hp->h_name;
43 ruserpass(hp->h_name, &name, &pass);
95091ecc 44retry:
2431680b 45 s = socket(AF_INET, SOCK_STREAM, 0);
40d6b022
SL
46 if (s < 0) {
47 perror("rexec: socket");
08081c0d 48 return (-1);
40d6b022
SL
49 }
50 sin.sin_family = hp->h_addrtype;
95091ecc 51 sin.sin_port = rport;
40d6b022 52 bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
11718d57 53 if (connect(s, &sin, sizeof(sin)) < 0) {
95091ecc
BJ
54 if (errno == ECONNREFUSED && timo <= 16) {
55 (void) close(s);
56 sleep(timo);
57 timo *= 2;
58 goto retry;
59 }
40d6b022 60 perror(hp->h_name);
08081c0d
KM
61 return (-1);
62 }
95091ecc
BJ
63 if (fd2p == 0) {
64 (void) write(s, "", 1);
65 port = 0;
66 } else {
67 char num[8];
6a3a3fba 68 int s2, sin2len;
95091ecc 69
2431680b 70 s2 = socket(AF_INET, SOCK_STREAM, 0);
95091ecc
BJ
71 if (s2 < 0) {
72 (void) close(s);
73 return (-1);
74 }
40d6b022 75 listen(s2, 1);
6a3a3fba
SL
76 sin2len = sizeof (sin2);
77 if (getsockname(s2, (char *)&sin2, &sin2len) < 0 ||
78 sin2len != sizeof (sin2)) {
79 perror("getsockname");
80 (void) close(s2);
81 goto bad;
82 }
40d6b022 83 port = ntohs((u_short)sin2.sin_port);
95091ecc
BJ
84 (void) sprintf(num, "%d", port);
85 (void) write(s, num, strlen(num)+1);
40d6b022
SL
86 { int len = sizeof (from);
87 s3 = accept(s2, &from, &len, 0);
88 close(s2);
89 if (s3 < 0) {
95091ecc 90 perror("accept");
40d6b022 91 port = 0;
95091ecc 92 goto bad;
40d6b022 93 }
95091ecc 94 }
40d6b022 95 *fd2p = s3;
95091ecc
BJ
96 }
97 (void) write(s, name, strlen(name) + 1);
08081c0d 98 /* should public key encypt the password here */
95091ecc
BJ
99 (void) write(s, pass, strlen(pass) + 1);
100 (void) write(s, cmd, strlen(cmd) + 1);
101 if (read(s, &c, 1) != 1) {
102 perror(*ahost);
103 goto bad;
104 }
105 if (c != 0) {
106 while (read(s, &c, 1) == 1) {
107 (void) write(2, &c, 1);
108 if (c == '\n')
109 break;
110 }
111 goto bad;
112 }
113 return (s);
114bad:
115 if (port)
116 (void) close(*fd2p);
117 (void) close(s);
118 return (-1);
08081c0d 119}