-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrexec.c
240 lines (207 loc) · 4.89 KB
/
rexec.c
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
/* rexec.c --- rexec for winsock
*
* Time-stamp: <04/01/01 09:42:24 keuchel@lightning>
*
*/
#include <windows.h>
#include <winsock.h>
#include <stdarg.h>
#include "celib.h"
#include "cesocket.h"
#define bcopy(from, to, len) memcpy(to, from, len)
#define SENDFLAG 0
#define RECVFLAG 0
#define IPPORT_RESERVED 1024
#define WSAEAGAIN 23
static int wserrno;
SOCKET rresvport (int *alport);
SOCKET
xcerexec(char **ahost, unsigned short rport, char *user,
char *pass, char *cmd, int *fd2p)
{
int wserrno;
SOCKET s, timo = 1;
struct sockaddr_in sin, from;
char c;
int lport = IPPORT_RESERVED - 1;
struct hostent *hp = NULL;
char username[16+1] = "";
char password[16+1] = "";
struct in_addr ina;
if(user == NULL || pass == NULL)
{
return SOCKET_ERROR;
}
// first check numeric address...
if(ascii2addr(AF_INET, *ahost, &ina) < 0)
{
hp = gethostbyname (*ahost);
if (hp == NULL)
{
wserror(("rexec:gethostbyname failed"));
return (INVALID_SOCKET);
}
*ahost = hp->h_name;
}
else
{
TRACE(("Numeric address OK\n"));
}
for (;;)
{
s = rresvport(&lport);
if (s == INVALID_SOCKET)
{
if (WSAGetLastError() == WSAEAGAIN)
wserror (("rexec:socket:all ports in use"));
else
wserror (("rexec:socket failed"));
return (INVALID_SOCKET);
}
if(hp)
{
sin.sin_family = hp->h_addrtype;
bcopy (hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
}
else
{
sin.sin_family = AF_INET;
bcopy(&ina, &sin.sin_addr, 4);
}
sin.sin_port = rport;
/* when connected, exit loop */
if (connect(s, (struct sockaddr *) &sin, sizeof (sin)) >= 0)
break;
wserrno = WSAGetLastError();
closesocket (s);
if (wserrno == WSAEADDRINUSE)
{
lport--;
continue;
}
if (wserrno == WSAECONNREFUSED && timo <= 16)
{
Sleep(timo * 1000);
timo *= 2;
continue;
}
if (hp && hp->h_addr_list[1] != NULL)
{
wserror(("connect to address %s: "), inet_ntoa (sin.sin_addr));
hp->h_addr_list++;
bcopy (hp->h_addr_list[0], &sin.sin_addr, hp->h_length);
wserror(("trying %s..."), inet_ntoa (sin.sin_addr));
continue;
}
return (INVALID_SOCKET);
}
/* we are connected */
lport--;
if (fd2p == 0)
{
send (s, "", 1, SENDFLAG);
lport = 0;
}
else
{
char num[8];
wchar_t wnum[8];
SOCKET s2 = rresvport (&lport), s3;
int len = sizeof (from);
if (s2 == INVALID_SOCKET)
goto bad;
listen (s2, 1);
sprintf(num, ("%d"), lport);
if (send (s, num, strlen (num) + 1, SENDFLAG) != (int) strlen (num) + 1)
{
wserror (("rexec:write: setting up stderr"));
closesocket (s2);
goto bad;
}
s3 = accept (s2, (struct sockaddr *) &from, &len);
closesocket (s2);
if (s3 == INVALID_SOCKET)
{
wserror (("rexec:accept failed"));
lport = 0;
goto bad;
}
*fd2p = s3;
from.sin_port = ntohs ((u_short) from.sin_port);
if (from.sin_family != AF_INET
#if 0
// SCO does not obeye this...
|| from.sin_port >= IPPORT_RESERVED
#endif
)
{
wserror(("socket: protocol failure in circuit setup."));
goto bad2;
}
}
/* these are null terminated */
send (s, user, strlen (user) + 1, SENDFLAG);
send (s, pass, strlen (pass) + 1, SENDFLAG);
send (s, cmd, strlen (cmd) + 1, SENDFLAG);
if (recv (s, &c, 1, RECVFLAG) != 1)
{
wserror (("rexec:cannot read from socket"));
goto bad2;
}
/* get one line from server: "Permission denied" */
if (c != 0)
{
wchar_t buf[126];
int i = 0;
while (recv (s, &c, 1, RECVFLAG) == 1)
{
buf[i++] = c;
if (c == '\n')
break;
}
buf[i] = 0;
AfxMessageBox(buf);
goto bad2;
}
/* return socket to caller */
return (s);
bad2:
if (lport)
closesocket (*fd2p);
bad:
closesocket (s);
return (INVALID_SOCKET);
}
static SOCKET
rresvport (int *alport)
{
struct sockaddr_in sin;
SOCKET s;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
s = socket (AF_INET, SOCK_STREAM, 0);
if (s == INVALID_SOCKET)
{
wserrno = WSAGetLastError ();
return (INVALID_SOCKET);
}
for (;;)
{
sin.sin_port = htons ((u_short) * alport);
if (bind (s, (struct sockaddr *) &sin, sizeof (sin)) == 0)
return (s);
wserrno = WSAGetLastError ();
if (wserrno != WSAEADDRINUSE)
{
closesocket (s);
return (INVALID_SOCKET);
}
(*alport)--;
if (*alport == IPPORT_RESERVED / 2)
{
closesocket (s);
wserrno = WSAEAGAIN;
return (INVALID_SOCKET);
}
}
}