| 1 | /* |
| 2 | * ezstream - source client for Icecast with external en-/decoder support |
| 3 | * Copyright (C) 2003, 2004, 2005, 2006 Ed Zaleski <[email protected]> |
| 4 | * Copyright (C) 2007, 2009, 2017, 2020 Moritz Grimm <[email protected]> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #ifdef HAVE_CONFIG_H |
| 17 | # include "config.h" |
| 18 | #endif |
| 19 | |
| 20 | #include "compat.h" |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/file.h> |
| 24 | |
| 25 | #include <ctype.h> |
| 26 | #include <errno.h> |
| 27 | #include <langinfo.h> |
| 28 | #include <limits.h> |
| 29 | #include <locale.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | #ifdef HAVE_ICONV |
| 35 | # include <iconv.h> |
| 36 | #endif |
| 37 | |
| 38 | #include "log.h" |
| 39 | #include "util.h" |
| 40 | #include "xalloc.h" |
| 41 | |
| 42 | #ifndef BUFSIZ |
| 43 | # define BUFSIZ 1024 |
| 44 | #endif |
| 45 | |
| 46 | static char *pidfile_path; |
| 47 | static FILE *pidfile_file; |
| 48 | static pid_t pidfile_pid; |
| 49 | static unsigned int pidfile_numlocks; |
| 50 | |
| 51 | static char * _util_iconvert(const char *, const char *, const char *); |
| 52 | static void _util_cleanup_pidfile(void); |
| 53 | |
| 54 | static char * |
| 55 | _util_iconvert(const char *in_str, const char *from, const char *to) |
| 56 | { |
| 57 | #ifdef HAVE_ICONV |
| 58 | iconv_t cd; |
| 59 | ICONV_CONST char *input, *ip; |
| 60 | size_t input_len; |
| 61 | char *output; |
| 62 | size_t output_size; |
| 63 | char buf[BUFSIZ], *bp; |
| 64 | size_t bufavail; |
| 65 | size_t out_pos; |
| 66 | char *tocode; |
| 67 | |
| 68 | if (NULL == in_str) |
| 69 | return (xstrdup("")); |
| 70 | |
| 71 | tocode = xstrdup(to); |
| 72 | if ((cd = iconv_open(tocode, from)) == (iconv_t)-1 && |
| 73 | (cd = iconv_open("", from)) == (iconv_t)-1 && |
| 74 | (cd = iconv_open(tocode, "")) == (iconv_t)-1) { |
| 75 | xfree(tocode); |
| 76 | log_syserr(ERROR, errno, "iconv_open"); |
| 77 | return (xstrdup(in_str)); |
| 78 | } |
| 79 | |
| 80 | ip = input = (ICONV_CONST char *)in_str; |
| 81 | input_len = strlen(input); |
| 82 | output_size = 1; |
| 83 | output = xcalloc(output_size, sizeof(char)); |
| 84 | out_pos = 0; |
| 85 | output[out_pos] = '\0'; |
| 86 | while (input_len > 0) { |
| 87 | char *op; |
| 88 | size_t count; |
| 89 | |
| 90 | buf[0] = '\0'; |
| 91 | bp = buf; |
| 92 | bufavail = sizeof(buf) - 1; |
| 93 | |
| 94 | if (iconv(cd, &ip, &input_len, &bp, &bufavail) == (size_t)-1 && |
| 95 | errno != E2BIG) { |
| 96 | *bp++ = '?'; |
| 97 | ip++; |
| 98 | input_len--; |
| 99 | bufavail--; |
| 100 | } |
| 101 | *bp = '\0'; |
| 102 | |
| 103 | count = sizeof(buf) - bufavail - 1; |
| 104 | |
| 105 | output_size += count; |
| 106 | op = output = xreallocarray(output, output_size, sizeof(char)); |
| 107 | op += out_pos; |
| 108 | memcpy(op, buf, count); |
| 109 | out_pos += count; |
| 110 | op += count; |
| 111 | *op = '\0'; |
| 112 | } |
| 113 | |
| 114 | if (iconv_close(cd) == -1) { |
| 115 | log_syserr(ERROR, errno, "iconv_close"); |
| 116 | xfree(output); |
| 117 | xfree(tocode); |
| 118 | return (xstrdup(in_str)); |
| 119 | } |
| 120 | |
| 121 | xfree(tocode); |
| 122 | return (output); |
| 123 | #else |
| 124 | (void)from; |
| 125 | (void)to; |
| 126 | |
| 127 | if (NULL == in_str) |
| 128 | return (xstrdup("")); |
| 129 | |
| 130 | return (xstrdup(in_str)); |
| 131 | #endif /* HAVE_ICONV */ |
| 132 | } |
| 133 | |
| 134 | static void |
| 135 | _util_cleanup_pidfile(void) |
| 136 | { |
| 137 | if (NULL != pidfile_path && getpid() == pidfile_pid) { |
| 138 | (void)unlink(pidfile_path); |
| 139 | (void)fclose(pidfile_file); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | const char * |
| 144 | util_get_progname(const char *argv0) |
| 145 | { |
| 146 | #ifdef HAVE___PROGNAME |
| 147 | extern char *__progname; |
| 148 | |
| 149 | (void)argv0; |
| 150 | |
| 151 | return (__progname); |
| 152 | #else |
| 153 | if (argv0 == NULL) { |
| 154 | return (UTIL_DEFAULT_PROGNAME); |
| 155 | } else { |
| 156 | const char *p = strrchr(argv0, '/'); |
| 157 | |
| 158 | if (p == NULL) |
| 159 | p = argv0; |
| 160 | else |
| 161 | p++; |
| 162 | |
| 163 | return (p); |
| 164 | } |
| 165 | #endif /* HAVE___PROGNAME */ |
| 166 | } |
| 167 | |
| 168 | int |
| 169 | util_write_pid_file(const char *path) |
| 170 | { |
| 171 | int save_errno = 0; |
| 172 | pid_t pid; |
| 173 | |
| 174 | if (NULL == path) |
| 175 | return (0); |
| 176 | |
| 177 | xfree(pidfile_path); |
| 178 | pidfile_path = xstrdup(path); |
| 179 | |
| 180 | if (NULL != pidfile_file) |
| 181 | fclose(pidfile_file); |
| 182 | if (NULL == (pidfile_file = fopen(pidfile_path, "w"))) { |
| 183 | xfree(pidfile_path); |
| 184 | pidfile_path = NULL; |
| 185 | return (-1); |
| 186 | } |
| 187 | |
| 188 | pid = getpid(); |
| 189 | if (0 >= fprintf(pidfile_file, "%ld\n", (long)pid) || |
| 190 | 0 > fflush(pidfile_file) || |
| 191 | 0 > flock(fileno(pidfile_file), LOCK_EX | LOCK_NB)) |
| 192 | goto error; |
| 193 | |
| 194 | if (0 == pidfile_numlocks) { |
| 195 | pidfile_pid = pid; |
| 196 | if (0 != atexit(_util_cleanup_pidfile)) |
| 197 | goto error; |
| 198 | pidfile_numlocks++; |
| 199 | } |
| 200 | |
| 201 | return (0); |
| 202 | |
| 203 | error: |
| 204 | save_errno = errno; |
| 205 | (void)unlink(pidfile_path); |
| 206 | xfree(pidfile_path); |
| 207 | pidfile_path = NULL; |
| 208 | (void)fclose(pidfile_file); |
| 209 | pidfile_file = NULL; |
| 210 | pidfile_pid = 0; |
| 211 | errno = save_errno; |
| 212 | |
| 213 | return (-1); |
| 214 | } |
| 215 | |
| 216 | int |
| 217 | util_strrcmp(const char *s, const char *sub) |
| 218 | { |
| 219 | size_t slen = strlen(s); |
| 220 | size_t sublen = strlen(sub); |
| 221 | |
| 222 | if (sublen > slen) |
| 223 | return (1); |
| 224 | |
| 225 | return (!!memcmp(s + slen - sublen, sub, sublen)); |
| 226 | } |
| 227 | |
| 228 | int |
| 229 | util_strrcasecmp(const char *s, const char *sub) |
| 230 | { |
| 231 | char *s_cpy = xstrdup(s); |
| 232 | char *sub_cpy = xstrdup(sub); |
| 233 | char *p; |
| 234 | int ret; |
| 235 | |
| 236 | for (p = s_cpy; *p != '\0'; p++) |
| 237 | *p = (char)tolower((int)*p); |
| 238 | |
| 239 | for (p = sub_cpy; *p != '\0'; p++) |
| 240 | *p = (char)tolower((int)*p); |
| 241 | |
| 242 | ret = util_strrcmp(s_cpy, sub_cpy); |
| 243 | |
| 244 | xfree(s_cpy); |
| 245 | xfree(sub_cpy); |
| 246 | |
| 247 | return (ret); |
| 248 | } |
| 249 | |
| 250 | char * |
| 251 | util_char2utf8(const char *in_str) |
| 252 | { |
| 253 | char *codeset; |
| 254 | |
| 255 | setlocale(LC_CTYPE, ""); |
| 256 | codeset = nl_langinfo((nl_item)CODESET); |
| 257 | setlocale(LC_CTYPE, "C"); |
| 258 | |
| 259 | return (_util_iconvert(in_str, codeset, "UTF-8")); |
| 260 | } |
| 261 | |
| 262 | char * |
| 263 | util_utf82char(const char *in_str) |
| 264 | { |
| 265 | char *codeset; |
| 266 | |
| 267 | setlocale(LC_CTYPE, ""); |
| 268 | codeset = nl_langinfo((nl_item)CODESET); |
| 269 | setlocale(LC_CTYPE, "C"); |
| 270 | |
| 271 | return (_util_iconvert(in_str, "UTF-8", codeset)); |
| 272 | } |
| 273 | |
| 274 | char * |
| 275 | util_expand_words(const char *in, struct util_dict dicts[]) |
| 276 | { |
| 277 | size_t i; |
| 278 | char *out; |
| 279 | size_t out_size = strlen(in) + 1; |
| 280 | |
| 281 | /* empty input string? */ |
| 282 | if (1 == out_size) |
| 283 | return (NULL); |
| 284 | |
| 285 | out = xstrdup(in); |
| 286 | i = out_size - 1; |
| 287 | while (i--) { |
| 288 | struct util_dict *d = dicts; |
| 289 | |
| 290 | while (d && d->from) { |
| 291 | if (0 == strncmp(&out[i], d->from, strlen(d->from))) { |
| 292 | char *buf, *tmp; |
| 293 | size_t buf_len; |
| 294 | |
| 295 | buf_len = strlen(&out[i]) + strlen(d->to) |
| 296 | - strlen(d->from); |
| 297 | buf = xcalloc(buf_len + 1, sizeof(*buf)); |
| 298 | snprintf(buf, buf_len + 1, "%s%s", |
| 299 | d->to, &out[i + strlen(d->from)]); |
| 300 | |
| 301 | out_size += buf_len; |
| 302 | tmp = xcalloc(out_size, sizeof(*tmp)); |
| 303 | snprintf(tmp, i + 1, "%s", out); |
| 304 | snprintf(tmp + i, out_size - i, "%s", buf); |
| 305 | free(buf); |
| 306 | free(out); |
| 307 | out = tmp; |
| 308 | |
| 309 | break; |
| 310 | } |
| 311 | d++; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return (out); |
| 316 | } |
| 317 | |
| 318 | #define SHELLQUOTE_OUTLEN_MAX 8191UL |
| 319 | |
| 320 | char * |
| 321 | util_shellquote(const char *in, size_t outlen_max) |
| 322 | { |
| 323 | char *out, *out_p; |
| 324 | size_t out_len; |
| 325 | const char *in_p; |
| 326 | |
| 327 | if (!outlen_max || outlen_max > SHELLQUOTE_OUTLEN_MAX) |
| 328 | outlen_max = SHELLQUOTE_OUTLEN_MAX; |
| 329 | out_len = outlen_max; |
| 330 | |
| 331 | out = xcalloc(out_len + 1, sizeof(char)); |
| 332 | |
| 333 | out_p = out; |
| 334 | in_p = in; |
| 335 | |
| 336 | *out_p++ = '\''; |
| 337 | out_len--; |
| 338 | while (*in_p && 1 < out_len) { |
| 339 | int stop = 0; |
| 340 | |
| 341 | switch (*in_p) { |
| 342 | case '\'': |
| 343 | if (4 < out_len) { |
| 344 | *out_p++ = '\''; |
| 345 | *out_p++ = '\\'; |
| 346 | *out_p++ = '\''; |
| 347 | out_len -= 3; |
| 348 | } else |
| 349 | stop = 1; |
| 350 | break; |
| 351 | default: |
| 352 | break; |
| 353 | } |
| 354 | if (stop) |
| 355 | break; |
| 356 | *out_p++ = *in_p++; |
| 357 | out_len--; |
| 358 | } |
| 359 | *out_p++ = '\''; |
| 360 | |
| 361 | return (out); |
| 362 | } |