-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathnet123_exec.c
385 lines (360 loc) · 9.22 KB
/
net123_exec.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
/*
net123_exec: network (HTTP(S)) streaming for mpg123 via fork+exec
copyright 2022-2023 by the mpg123 project
free software under the terms of the LGPL 2.1
see COPYING and AUTHORS files in distribution or http://mpg123.org
initially written by Thomas Orgis
This avoids linking any network code directly into mpg123, just using external
tools at runtime.
This calls wget with fallback to curl by default, one of those two
specifically if param.network_backend is set accordingly.
*/
// kill
#define _XOPEN_SOURCE 600
#define _POSIX_C_SOURCE 200112L
#include "config.h"
#include "version.h"
#include "net123.h"
// for strings
#include "mpg123.h"
// Just for parameter struct that we use for HTTP auth and proxy info.
#include "mpg123app.h"
#include "compat/compat.h"
#include "common/debug.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
// Those are set via environment variables:
// http_proxy
// https_proxy
// If set, the http_proxy and https_proxy variables should contain the
// URLs of the proxies for HTTP and HTTPS connections respectively.
// ftp_proxy
// same
// wget --user=... --password=...
// Alternatively: Have them in .netrc.
typedef struct
{
int fd;
pid_t worker;
} exec_handle;
// Combine two given strings into one newly allocated one.
// Use: (--parameter=, value) -> --parameter=value
static char *catstr(const char *par, const char *value)
{
char *res = malloc(strlen(par)+strlen(value)+1);
if(res)
{
res[0] = 0;
strcat(res, par);
strcat(res, value);
}
return res;
}
// < 0: not checked, 0: not there, 1: present
static int got_curl = -1;
static int got_wget = -1;
// Check if program executes, also test if given token occurs in its output.
// Returns 0 if not, 1 if exec works, 2 if also token found.
// Token has to be < 1024 characters in length.
static int check_program(char * const *argv, const char *token)
{
int fd[2];
int gottoken = 0;
if(token)
{
if(pipe(fd))
return 0;
INT123_compat_binmode(fd[0], TRUE);
INT123_compat_binmode(fd[1], TRUE);
}
pid_t pid = fork();
if(pid == 0)
{
int outfd = fd[1];
if(token)
close(fd[0]);
else
outfd = open("/dev/null", O_WRONLY);
dup2(outfd, STDOUT_FILENO);
int infd = open("/dev/null", O_RDONLY);
dup2(infd, STDIN_FILENO);
int errfd = open("/dev/null", O_WRONLY);
dup2(errfd, STDERR_FILENO);
execvp(argv[0], argv);
exit(1);
}
else if(pid > 0)
{
if(token)
{
char buf[1024];
close(fd[1]);
size_t toklen = strlen(token);
if(toklen > 0 && toklen < sizeof(buf))
{
size_t bufoff = 0;
size_t got;
while( (got = INT123_unintr_read(fd[0], buf+bufoff, sizeof(buf)-1-bufoff)) )
{
bufoff += got;
buf[bufoff] = 0; // Now it's a terminated string.
if(!gottoken && strstr(buf, token))
gottoken = 1;
if(gottoken)
bufoff = 0; // just forget everything
else if(bufoff > toklen)
{
// Remember the last toklen-1 bytes to compare later.
memmove(buf, buf+bufoff-toklen+1, toklen-1);
bufoff = toklen-1;
}
}
}
close(fd[0]);
}
int stat;
if( (waitpid(pid, &stat, 0) == pid)
&& WIFEXITED(stat) && WEXITSTATUS(stat)==0 )
return 1+gottoken;
} else if(token)
{
close(fd[0]);
close(fd[1]);
}
return 0; // false, not there
}
static char **wget_argv(const char *url, const char * const * client_head)
{
const char* base_args[] =
{
"wget" // begins with program name
, "--output-document=-"
#ifndef DEBUG
, "--quiet"
#endif
, "--save-headers"
};
size_t cheads = 0;
while(client_head && client_head[cheads]){ ++cheads; }
// Get the count of argument strings right!
// Fixed args + agent + client headers [+ auth] + URL + NULL
int argc = sizeof(base_args)/sizeof(char*)+1+cheads+1+1;
char *httpauth = NULL;
char *user = NULL;
char *password = NULL;
if(param.httpauth && (httpauth = INT123_compat_strdup(param.httpauth)))
{
char *sep = strchr(httpauth, ':');
if(sep)
{
argc += 2;
*sep = 0;
user = httpauth;
password = sep+1;
}
}
char ** argv = malloc(sizeof(char*)*(argc+1));
if(!argv)
{
error("failed to allocate argv");
return NULL;
}
int an = 0;
for(;an<sizeof(base_args)/sizeof(char*); ++an)
argv[an] = INT123_compat_strdup(base_args[an]);
argv[an++] = INT123_compat_strdup("--user-agent=" PACKAGE_NAME "/" MPG123_VERSION);
for(size_t ch=0; ch < cheads; ++ch)
argv[an++] = catstr("--header=", client_head[ch]);
if(user)
argv[an++] = catstr("--user=", user);
if(password)
argv[an++] = catstr("--password=", password);
argv[an++] = INT123_compat_strdup(url);
argv[an++] = NULL;
return argv;
}
static char **curl_argv(const char *url, const char * const * client_head)
{
const char* base_args[] =
{
"curl" // begins with program name
#ifdef DEBUG
, "--verbose"
#else
, "--silent"
, "--show-error"
#endif
, "--dump-header"
, "-"
};
size_t cheads = 0;
while(client_head && client_head[cheads]){ ++cheads; }
// Get the count of argument strings right!
// Fixed args + agent + client headers [+ auth] + URL + NULL
int argc = sizeof(base_args)/sizeof(char*)+2+2*cheads+1+1;
if(got_curl > 1)
argc++; // add --http0.9
char *httpauth = NULL;
if(param.httpauth && (httpauth = INT123_compat_strdup(param.httpauth)))
argc += 2;
char ** argv = malloc(sizeof(char*)*(argc+1));
if(!argv)
{
error("failed to allocate argv");
return NULL;
}
int an = 0;
for(;an<sizeof(base_args)/sizeof(char*); ++an)
argv[an] = INT123_compat_strdup(base_args[an]);
if(got_curl > 1)
argv[an++] = INT123_compat_strdup("--http0.9");
argv[an++] = INT123_compat_strdup("--user-agent");
argv[an++] = INT123_compat_strdup(PACKAGE_NAME "/" MPG123_VERSION);
for(size_t ch=0; ch < cheads; ++ch)
{
argv[an++] = INT123_compat_strdup("--header");
argv[an++] = INT123_compat_strdup(client_head[ch]);
}
if(httpauth)
{
argv[an++] = INT123_compat_strdup("--user");
argv[an++] = httpauth;
}
argv[an++] = INT123_compat_strdup(url);
argv[an++] = NULL;
return argv;
}
static size_t net123_read(net123_handle *nh, void *buf, size_t bufsize)
{
if(!nh || (bufsize && !buf))
return 0;
return INT123_unintr_read(((exec_handle*)nh->parts)->fd, buf, bufsize);
}
static void net123_close(net123_handle *nh)
{
if(!nh)
return;
exec_handle *eh = nh->parts;
if(eh->worker)
{
kill(eh->worker, SIGKILL);
errno = 0;
if(waitpid(eh->worker, NULL, 0) < 0)
merror("failed to wait for worker process: %s", INT123_strerror(errno));
else if(param.verbose > 1)
fprintf(stderr, "Note: network helper %"PRIiMAX" finished\n", (intmax_t)eh->worker);
}
if(eh->fd > -1)
close(eh->fd);
free(nh->parts);
free(nh);
}
net123_handle *net123_open_exec(const char *url, const char * const * client_head)
{
int use_curl = 0;
char * const curl_check_argv[] = { "curl", "--help", "all", NULL };
char * const wget_check_argv[] = { "wget", "--version", NULL };
// Semi-threadsafe: The check might take place multiple times, but writing the integer
// should be safe enough.
if(!strcmp("auto",param.network_backend))
{
if(got_wget < 0)
got_wget = check_program(wget_check_argv, NULL);
if(!got_wget && got_curl < 0)
got_curl = check_program(curl_check_argv, "--http0.9");
if(got_wget < 1 && got_curl)
use_curl = 1;
} else if(!strcmp("curl", param.network_backend))
{
if(got_curl < 0) // Still need to know if HTTP/0.9 option is there.
got_curl = check_program(curl_check_argv, "--http0.9");
use_curl = 1;
} else if(!strcmp("wget", param.network_backend))
{
if(got_wget < 0)
got_wget = check_program(wget_check_argv, NULL);
use_curl = 0;
} else
{
merror("invalid network backend specified: %s", param.network_backend);
return NULL;
}
if((!use_curl && !got_wget) || (use_curl && !got_curl))
{
error("missing working network helper program (wget or curl)");
return NULL;
}
int fd[2];
net123_handle *nh = malloc(sizeof(net123_handle));
exec_handle *eh = malloc(sizeof(exec_handle));
if(!nh || !eh)
{
if(nh)
free(nh);
if(eh)
free(eh);
return NULL;
}
nh->parts = eh;
nh->read = net123_read;
nh->close = net123_close;
eh->fd = -1;
eh->worker = 0;
errno = 0;
if(pipe(fd))
{
merror("failed creating a pipe: %s", INT123_strerror(errno));
free(nh);
return NULL;
}
INT123_compat_binmode(fd[0], TRUE);
INT123_compat_binmode(fd[1], TRUE);
eh->worker = fork();
if(eh->worker == -1)
{
merror("fork failed: %s", INT123_strerror(errno));
free(nh);
return NULL;
}
if(eh->worker == 0)
{
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
int infd = open("/dev/null", O_RDONLY);
dup2(infd, STDIN_FILENO);
// child
// Proxy environment variables can just be set in the user and inherited here, right?
char **argv = use_curl ? curl_argv(url, client_head) : wget_argv(url, client_head);
if(!argv)
exit(1);
errno = 0;
if(!param.quiet)
{
if(param.verbose > 2)
{
char **a = argv;
fprintf(stderr, "HTTP helper command:\n");
while(*a)
{
fprintf(stderr, " %s\n", *a);
++a;
}
}
} else
{
int errfd = open("/dev/null", O_WRONLY);
dup2(errfd, STDERR_FILENO);
}
execvp(argv[0], argv);
merror("cannot execute %s: %s", argv[0], INT123_strerror(errno));
exit(1);
}
// parent
if(param.verbose > 1)
fprintf(stderr, "Note: started network helper with PID %"PRIiMAX"\n", (intmax_t)eh->worker);
errno = 0;
close(fd[1]);
eh->fd = fd[0];
return nh;
}