| 1 |
/* |
| 2 |
net123_exec: network (HTTP(S)) streaming for mpg123 via fork+exec |
| 3 |
|
| 4 |
copyright 2022-2023 by the mpg123 project |
| 5 |
free software under the terms of the LGPL 2.1 |
| 6 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 7 |
initially written by Thomas Orgis |
| 8 |
|
| 9 |
This avoids linking any network code directly into mpg123, just using external |
| 10 |
tools at runtime. |
| 11 |
|
| 12 |
This calls wget with fallback to curl by default, one of those two |
| 13 |
specifically if param.network_backend is set accordingly. |
| 14 |
*/ |
| 15 |
|
| 16 |
// kill |
| 17 |
#define _XOPEN_SOURCE 600 |
| 18 |
#define _POSIX_C_SOURCE 200112L |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
#include "version.h" |
| 22 |
#include "net123.h" |
| 23 |
// for strings |
| 24 |
#include "mpg123.h" |
| 25 |
|
| 26 |
// Just for parameter struct that we use for HTTP auth and proxy info. |
| 27 |
#include "mpg123app.h" |
| 28 |
|
| 29 |
#include "compat/compat.h" |
| 30 |
|
| 31 |
#include "common/debug.h" |
| 32 |
#include <sys/types.h> |
| 33 |
#include <sys/wait.h> |
| 34 |
#include <unistd.h> |
| 35 |
|
| 36 |
// Those are set via environment variables: |
| 37 |
// http_proxy |
| 38 |
// https_proxy |
| 39 |
// If set, the http_proxy and https_proxy variables should contain the |
| 40 |
// URLs of the proxies for HTTP and HTTPS connections respectively. |
| 41 |
// ftp_proxy |
| 42 |
// same |
| 43 |
// wget --user=... --password=... |
| 44 |
// Alternatively: Have them in .netrc. |
| 45 |
|
| 46 |
typedef struct |
| 47 |
{ |
| 48 |
int fd; |
| 49 |
pid_t worker; |
| 50 |
} exec_handle; |
| 51 |
|
| 52 |
// Combine two given strings into one newly allocated one. |
| 53 |
// Use: (--parameter=, value) -> --parameter=value |
| 54 |
static char *catstr(const char *par, const char *value) |
| 55 |
{ |
| 56 |
char *res = malloc(strlen(par)+strlen(value)+1); |
| 57 |
if(res) |
| 58 |
{ |
| 59 |
res[0] = 0; |
| 60 |
strcat(res, par); |
| 61 |
strcat(res, value); |
| 62 |
} |
| 63 |
return res; |
| 64 |
} |
| 65 |
|
| 66 |
// < 0: not checked, 0: not there, 1: present |
| 67 |
static int got_curl = -1; |
| 68 |
static int got_wget = -1; |
| 69 |
|
| 70 |
// Check if program executes, also test if given token occurs in its output. |
| 71 |
// Returns 0 if not, 1 if exec works, 2 if also token found. |
| 72 |
// Token has to be < 1024 characters in length. |
| 73 |
static int check_program(char * const *argv, const char *token) |
| 74 |
{ |
| 75 |
int fd[2]; |
| 76 |
int gottoken = 0; |
| 77 |
if(token) |
| 78 |
{ |
| 79 |
if(pipe(fd)) |
| 80 |
return 0; |
| 81 |
INT123_compat_binmode(fd[0], TRUE); |
| 82 |
INT123_compat_binmode(fd[1], TRUE); |
| 83 |
} |
| 84 |
pid_t pid = fork(); |
| 85 |
if(pid == 0) |
| 86 |
{ |
| 87 |
int outfd = fd[1]; |
| 88 |
if(token) |
| 89 |
close(fd[0]); |
| 90 |
else |
| 91 |
outfd = open("/dev/null", O_WRONLY); |
| 92 |
dup2(outfd, STDOUT_FILENO); |
| 93 |
int infd = open("/dev/null", O_RDONLY); |
| 94 |
dup2(infd, STDIN_FILENO); |
| 95 |
int errfd = open("/dev/null", O_WRONLY); |
| 96 |
dup2(errfd, STDERR_FILENO); |
| 97 |
execvp(argv[0], argv); |
| 98 |
exit(1); |
| 99 |
} |
| 100 |
else if(pid > 0) |
| 101 |
{ |
| 102 |
if(token) |
| 103 |
{ |
| 104 |
char buf[1024]; |
| 105 |
close(fd[1]); |
| 106 |
size_t toklen = strlen(token); |
| 107 |
if(toklen > 0 && toklen < sizeof(buf)) |
| 108 |
{ |
| 109 |
size_t bufoff = 0; |
| 110 |
size_t got; |
| 111 |
while( (got = INT123_unintr_read(fd[0], buf+bufoff, sizeof(buf)-1-bufoff)) ) |
| 112 |
{ |
| 113 |
bufoff += got; |
| 114 |
buf[bufoff] = 0; // Now it's a terminated string. |
| 115 |
if(!gottoken && strstr(buf, token)) |
| 116 |
gottoken = 1; |
| 117 |
if(gottoken) |
| 118 |
bufoff = 0; // just forget everything |
| 119 |
else if(bufoff > toklen) |
| 120 |
{ |
| 121 |
// Remember the last toklen-1 bytes to compare later. |
| 122 |
memmove(buf, buf+bufoff-toklen+1, toklen-1); |
| 123 |
bufoff = toklen-1; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
close(fd[0]); |
| 128 |
} |
| 129 |
int stat; |
| 130 |
if( (waitpid(pid, &stat, 0) == pid) |
| 131 |
&& WIFEXITED(stat) && WEXITSTATUS(stat)==0 ) |
| 132 |
return 1+gottoken; |
| 133 |
} else if(token) |
| 134 |
{ |
| 135 |
close(fd[0]); |
| 136 |
close(fd[1]); |
| 137 |
} |
| 138 |
return 0; // false, not there |
| 139 |
} |
| 140 |
|
| 141 |
static char **wget_argv(const char *url, const char * const * client_head) |
| 142 |
{ |
| 143 |
const char* base_args[] = |
| 144 |
{ |
| 145 |
"wget" // begins with program name |
| 146 |
, "--output-document=-" |
| 147 |
#ifndef DEBUG |
| 148 |
, "--quiet" |
| 149 |
#endif |
| 150 |
, "--save-headers" |
| 151 |
}; |
| 152 |
size_t cheads = 0; |
| 153 |
while(client_head && client_head[cheads]){ ++cheads; } |
| 154 |
// Get the count of argument strings right! |
| 155 |
// Fixed args + agent + client headers [+ auth] + URL + NULL |
| 156 |
int argc = sizeof(base_args)/sizeof(char*)+1+cheads+1+1; |
| 157 |
char *httpauth = NULL; |
| 158 |
char *user = NULL; |
| 159 |
char *password = NULL; |
| 160 |
if(param.httpauth && (httpauth = INT123_compat_strdup(param.httpauth))) |
| 161 |
{ |
| 162 |
char *sep = strchr(httpauth, ':'); |
| 163 |
if(sep) |
| 164 |
{ |
| 165 |
argc += 2; |
| 166 |
*sep = 0; |
| 167 |
user = httpauth; |
| 168 |
password = sep+1; |
| 169 |
} |
| 170 |
} |
| 171 |
char ** argv = malloc(sizeof(char*)*(argc+1)); |
| 172 |
if(!argv) |
| 173 |
{ |
| 174 |
error("failed to allocate argv"); |
| 175 |
return NULL; |
| 176 |
} |
| 177 |
int an = 0; |
| 178 |
for(;an<sizeof(base_args)/sizeof(char*); ++an) |
| 179 |
argv[an] = INT123_compat_strdup(base_args[an]); |
| 180 |
argv[an++] = INT123_compat_strdup("--user-agent=" PACKAGE_NAME "/" MPG123_VERSION); |
| 181 |
for(size_t ch=0; ch < cheads; ++ch) |
| 182 |
argv[an++] = catstr("--header=", client_head[ch]); |
| 183 |
if(user) |
| 184 |
argv[an++] = catstr("--user=", user); |
| 185 |
if(password) |
| 186 |
argv[an++] = catstr("--password=", password); |
| 187 |
argv[an++] = INT123_compat_strdup(url); |
| 188 |
argv[an++] = NULL; |
| 189 |
return argv; |
| 190 |
} |
| 191 |
|
| 192 |
static char **curl_argv(const char *url, const char * const * client_head) |
| 193 |
{ |
| 194 |
const char* base_args[] = |
| 195 |
{ |
| 196 |
"curl" // begins with program name |
| 197 |
#ifdef DEBUG |
| 198 |
, "--verbose" |
| 199 |
#else |
| 200 |
, "--silent" |
| 201 |
, "--show-error" |
| 202 |
#endif |
| 203 |
, "--dump-header" |
| 204 |
, "-" |
| 205 |
}; |
| 206 |
size_t cheads = 0; |
| 207 |
while(client_head && client_head[cheads]){ ++cheads; } |
| 208 |
// Get the count of argument strings right! |
| 209 |
// Fixed args + agent + client headers [+ auth] + URL + NULL |
| 210 |
int argc = sizeof(base_args)/sizeof(char*)+2+2*cheads+1+1; |
| 211 |
if(got_curl > 1) |
| 212 |
argc++; // add --http0.9 |
| 213 |
char *httpauth = NULL; |
| 214 |
if(param.httpauth && (httpauth = INT123_compat_strdup(param.httpauth))) |
| 215 |
argc += 2; |
| 216 |
char ** argv = malloc(sizeof(char*)*(argc+1)); |
| 217 |
if(!argv) |
| 218 |
{ |
| 219 |
error("failed to allocate argv"); |
| 220 |
return NULL; |
| 221 |
} |
| 222 |
int an = 0; |
| 223 |
for(;an<sizeof(base_args)/sizeof(char*); ++an) |
| 224 |
argv[an] = INT123_compat_strdup(base_args[an]); |
| 225 |
if(got_curl > 1) |
| 226 |
argv[an++] = INT123_compat_strdup("--http0.9"); |
| 227 |
argv[an++] = INT123_compat_strdup("--user-agent"); |
| 228 |
argv[an++] = INT123_compat_strdup(PACKAGE_NAME "/" MPG123_VERSION); |
| 229 |
for(size_t ch=0; ch < cheads; ++ch) |
| 230 |
{ |
| 231 |
argv[an++] = INT123_compat_strdup("--header"); |
| 232 |
argv[an++] = INT123_compat_strdup(client_head[ch]); |
| 233 |
} |
| 234 |
if(httpauth) |
| 235 |
{ |
| 236 |
argv[an++] = INT123_compat_strdup("--user"); |
| 237 |
argv[an++] = httpauth; |
| 238 |
} |
| 239 |
argv[an++] = INT123_compat_strdup(url); |
| 240 |
argv[an++] = NULL; |
| 241 |
return argv; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
static size_t net123_read(net123_handle *nh, void *buf, size_t bufsize) |
| 246 |
{ |
| 247 |
if(!nh || (bufsize && !buf)) |
| 248 |
return 0; |
| 249 |
return INT123_unintr_read(((exec_handle*)nh->parts)->fd, buf, bufsize); |
| 250 |
} |
| 251 |
|
| 252 |
static void net123_close(net123_handle *nh) |
| 253 |
{ |
| 254 |
if(!nh) |
| 255 |
return; |
| 256 |
exec_handle *eh = nh->parts; |
| 257 |
if(eh->worker) |
| 258 |
{ |
| 259 |
kill(eh->worker, SIGKILL); |
| 260 |
errno = 0; |
| 261 |
if(waitpid(eh->worker, NULL, 0) < 0) |
| 262 |
merror("failed to wait for worker process: %s", INT123_strerror(errno)); |
| 263 |
else if(param.verbose > 1) |
| 264 |
fprintf(stderr, "Note: network helper %"PRIiMAX" finished\n", (intmax_t)eh->worker); |
| 265 |
} |
| 266 |
if(eh->fd > -1) |
| 267 |
close(eh->fd); |
| 268 |
free(nh->parts); |
| 269 |
free(nh); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
net123_handle *net123_open_exec(const char *url, const char * const * client_head) |
| 274 |
{ |
| 275 |
int use_curl = 0; |
| 276 |
char * const curl_check_argv[] = { "curl", "--help", "all", NULL }; |
| 277 |
char * const wget_check_argv[] = { "wget", "--version", NULL }; |
| 278 |
// Semi-threadsafe: The check might take place multiple times, but writing the integer |
| 279 |
// should be safe enough. |
| 280 |
if(!strcmp("auto",param.network_backend)) |
| 281 |
{ |
| 282 |
if(got_wget < 0) |
| 283 |
got_wget = check_program(wget_check_argv, NULL); |
| 284 |
if(!got_wget && got_curl < 0) |
| 285 |
got_curl = check_program(curl_check_argv, "--http0.9"); |
| 286 |
if(got_wget < 1 && got_curl) |
| 287 |
use_curl = 1; |
| 288 |
} else if(!strcmp("curl", param.network_backend)) |
| 289 |
{ |
| 290 |
if(got_curl < 0) // Still need to know if HTTP/0.9 option is there. |
| 291 |
got_curl = check_program(curl_check_argv, "--http0.9"); |
| 292 |
use_curl = 1; |
| 293 |
} else if(!strcmp("wget", param.network_backend)) |
| 294 |
{ |
| 295 |
if(got_wget < 0) |
| 296 |
got_wget = check_program(wget_check_argv, NULL); |
| 297 |
use_curl = 0; |
| 298 |
} else |
| 299 |
{ |
| 300 |
merror("invalid network backend specified: %s", param.network_backend); |
| 301 |
return NULL; |
| 302 |
} |
| 303 |
if((!use_curl && !got_wget) || (use_curl && !got_curl)) |
| 304 |
{ |
| 305 |
error("missing working network helper program (wget or curl)"); |
| 306 |
return NULL; |
| 307 |
} |
| 308 |
|
| 309 |
int fd[2]; |
| 310 |
net123_handle *nh = malloc(sizeof(net123_handle)); |
| 311 |
exec_handle *eh = malloc(sizeof(exec_handle)); |
| 312 |
if(!nh || !eh) |
| 313 |
{ |
| 314 |
if(nh) |
| 315 |
free(nh); |
| 316 |
if(eh) |
| 317 |
free(eh); |
| 318 |
return NULL; |
| 319 |
} |
| 320 |
nh->parts = eh; |
| 321 |
nh->read = net123_read; |
| 322 |
nh->close = net123_close; |
| 323 |
eh->fd = -1; |
| 324 |
eh->worker = 0; |
| 325 |
errno = 0; |
| 326 |
if(pipe(fd)) |
| 327 |
{ |
| 328 |
merror("failed creating a pipe: %s", INT123_strerror(errno)); |
| 329 |
free(nh); |
| 330 |
return NULL; |
| 331 |
} |
| 332 |
|
| 333 |
INT123_compat_binmode(fd[0], TRUE); |
| 334 |
INT123_compat_binmode(fd[1], TRUE); |
| 335 |
|
| 336 |
eh->worker = fork(); |
| 337 |
if(eh->worker == -1) |
| 338 |
{ |
| 339 |
merror("fork failed: %s", INT123_strerror(errno)); |
| 340 |
free(nh); |
| 341 |
return NULL; |
| 342 |
} |
| 343 |
|
| 344 |
if(eh->worker == 0) |
| 345 |
{ |
| 346 |
close(fd[0]); |
| 347 |
dup2(fd[1], STDOUT_FILENO); |
| 348 |
int infd = open("/dev/null", O_RDONLY); |
| 349 |
dup2(infd, STDIN_FILENO); |
| 350 |
// child |
| 351 |
// Proxy environment variables can just be set in the user and inherited here, right? |
| 352 |
|
| 353 |
char **argv = use_curl ? curl_argv(url, client_head) : wget_argv(url, client_head); |
| 354 |
if(!argv) |
| 355 |
exit(1); |
| 356 |
errno = 0; |
| 357 |
if(!param.quiet) |
| 358 |
{ |
| 359 |
if(param.verbose > 2) |
| 360 |
{ |
| 361 |
char **a = argv; |
| 362 |
fprintf(stderr, "HTTP helper command:\n"); |
| 363 |
while(*a) |
| 364 |
{ |
| 365 |
fprintf(stderr, " %s\n", *a); |
| 366 |
++a; |
| 367 |
} |
| 368 |
} |
| 369 |
} else |
| 370 |
{ |
| 371 |
int errfd = open("/dev/null", O_WRONLY); |
| 372 |
dup2(errfd, STDERR_FILENO); |
| 373 |
} |
| 374 |
execvp(argv[0], argv); |
| 375 |
merror("cannot execute %s: %s", argv[0], INT123_strerror(errno)); |
| 376 |
exit(1); |
| 377 |
} |
| 378 |
// parent |
| 379 |
if(param.verbose > 1) |
| 380 |
fprintf(stderr, "Note: started network helper with PID %"PRIiMAX"\n", (intmax_t)eh->worker); |
| 381 |
errno = 0; |
| 382 |
close(fd[1]); |
| 383 |
eh->fd = fd[0]; |
| 384 |
return nh; |
| 385 |
} |