| 1 | /* |
| 2 | * Copyright (c) 2017, 2020 Moritz Grimm <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #ifdef HAVE_CONFIG_H |
| 18 | # include "config.h" |
| 19 | #endif /* HAVE_CONFIG_H */ |
| 20 | |
| 21 | #include "compat.h" |
| 22 | |
| 23 | #include <sys/queue.h> |
| 24 | |
| 25 | #include <stdint.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | #include "cfg_private.h" |
| 29 | #include "cfg_server.h" |
| 30 | #include "xalloc.h" |
| 31 | |
| 32 | struct cfg_server { |
| 33 | TAILQ_ENTRY(cfg_server) entry; |
| 34 | char *name; |
| 35 | enum cfg_server_protocol protocol; |
| 36 | char hostname[NI_MAXHOST]; |
| 37 | unsigned int port; |
| 38 | char user[UCREDS_SIZE]; |
| 39 | char password[UCREDS_SIZE]; |
| 40 | enum cfg_server_tls tls; |
| 41 | char tls_cipher_suite[CSUITE_SIZE]; |
| 42 | char ca_dir[PATH_MAX]; |
| 43 | char ca_file[PATH_MAX]; |
| 44 | char client_cert[PATH_MAX]; |
| 45 | unsigned int reconnect_attempts; |
| 46 | }; |
| 47 | |
| 48 | TAILQ_HEAD(cfg_server_list, cfg_server); |
| 49 | |
| 50 | struct cfg_server_list * |
| 51 | cfg_server_list_create(void) |
| 52 | { |
| 53 | struct cfg_server_list *sl; |
| 54 | |
| 55 | sl = xcalloc(1UL, sizeof(*sl)); |
| 56 | TAILQ_INIT(sl); |
| 57 | |
| 58 | return (sl); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | cfg_server_list_destroy(cfg_server_list_t *sl_p) |
| 63 | { |
| 64 | struct cfg_server_list *sl = *sl_p; |
| 65 | struct cfg_server *s; |
| 66 | |
| 67 | if (!sl) |
| 68 | return; |
| 69 | |
| 70 | while (NULL != (s = TAILQ_FIRST(sl))) { |
| 71 | TAILQ_REMOVE(sl, s, entry); |
| 72 | cfg_server_destroy(&s); |
| 73 | } |
| 74 | |
| 75 | xfree(sl); |
| 76 | *sl_p = NULL; |
| 77 | } |
| 78 | |
| 79 | unsigned int |
| 80 | cfg_server_list_nentries(struct cfg_server_list *sl) |
| 81 | { |
| 82 | struct cfg_server *s; |
| 83 | unsigned int n = 0; |
| 84 | |
| 85 | TAILQ_FOREACH(s, sl, entry) { |
| 86 | n++; |
| 87 | } |
| 88 | |
| 89 | return (n); |
| 90 | } |
| 91 | |
| 92 | struct cfg_server * |
| 93 | cfg_server_list_find(struct cfg_server_list *sl, const char *name) |
| 94 | { |
| 95 | struct cfg_server *s; |
| 96 | |
| 97 | TAILQ_FOREACH(s, sl, entry) { |
| 98 | if (0 == strcasecmp(s->name, name)) |
| 99 | return (s); |
| 100 | } |
| 101 | |
| 102 | return (NULL); |
| 103 | } |
| 104 | |
| 105 | struct cfg_server * |
| 106 | cfg_server_list_get(struct cfg_server_list *sl, const char *name) |
| 107 | { |
| 108 | struct cfg_server *s; |
| 109 | |
| 110 | s = cfg_server_list_find(sl, name); |
| 111 | if (s) |
| 112 | return (s); |
| 113 | s = cfg_server_create(name); |
| 114 | if (!s) |
| 115 | return (NULL); |
| 116 | |
| 117 | TAILQ_INSERT_TAIL(sl, s, entry); |
| 118 | |
| 119 | return (s); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | cfg_server_list_foreach(struct cfg_server_list *sl, |
| 124 | void (*cb)(cfg_server_t, void *), void *cb_arg) |
| 125 | { |
| 126 | struct cfg_server *s; |
| 127 | |
| 128 | TAILQ_FOREACH(s, sl, entry) { |
| 129 | cb(s, cb_arg); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | struct cfg_server * |
| 134 | cfg_server_create(const char *name) |
| 135 | { |
| 136 | struct cfg_server *s; |
| 137 | |
| 138 | if (!name || !name[0]) |
| 139 | return (NULL); |
| 140 | |
| 141 | s = xcalloc(1UL, sizeof(*s)); |
| 142 | s->name = xstrdup(name); |
| 143 | |
| 144 | return (s); |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | cfg_server_destroy(struct cfg_server **s_p) |
| 149 | { |
| 150 | struct cfg_server *s = *s_p; |
| 151 | |
| 152 | xfree(s->name); |
| 153 | xfree(s); |
| 154 | *s_p = NULL; |
| 155 | } |
| 156 | |
| 157 | int |
| 158 | cfg_server_set_name(struct cfg_server *s, struct cfg_server_list *sl, |
| 159 | const char *name, const char **errstrp) |
| 160 | { |
| 161 | struct cfg_server *s2; |
| 162 | |
| 163 | if (!name || !name[0]) { |
| 164 | if (errstrp) |
| 165 | *errstrp = "empty"; |
| 166 | return (-1); |
| 167 | } |
| 168 | |
| 169 | s2 = cfg_server_list_find(sl, name); |
| 170 | if (s2 && s2 != s) { |
| 171 | if (errstrp) |
| 172 | *errstrp = "already exists"; |
| 173 | return (-1); |
| 174 | } |
| 175 | |
| 176 | SET_XSTRDUP(s->name, name, errstrp); |
| 177 | |
| 178 | return (0); |
| 179 | } |
| 180 | |
| 181 | int |
| 182 | cfg_server_set_protocol(struct cfg_server *s, struct cfg_server_list *not_used, |
| 183 | const char *protocol, const char **errstrp) |
| 184 | { |
| 185 | (void)not_used; |
| 186 | |
| 187 | if (!protocol || !protocol[0]) { |
| 188 | if (errstrp) |
| 189 | *errstrp = "empty"; |
| 190 | return (-1); |
| 191 | } |
| 192 | |
| 193 | if (0 == strcasecmp("http", protocol)) |
| 194 | s->protocol = CFG_PROTO_HTTP; |
| 195 | else if (0 == strcasecmp("https", protocol)) |
| 196 | s->protocol = CFG_PROTO_HTTPS; |
| 197 | else if (0 == strcasecmp("icy", protocol)) |
| 198 | s->protocol = CFG_PROTO_ICY; |
| 199 | else if (0 == strcasecmp("roaraudio", protocol)) |
| 200 | s->protocol = CFG_PROTO_ROARAUDIO; |
| 201 | else { |
| 202 | if (NULL != errstrp) |
| 203 | *errstrp = "unsupported"; |
| 204 | return (-1); |
| 205 | } |
| 206 | return (0); |
| 207 | } |
| 208 | |
| 209 | int |
| 210 | cfg_server_set_hostname(struct cfg_server *s, struct cfg_server_list *not_used, |
| 211 | const char *hostname, const char **errstrp) |
| 212 | { |
| 213 | (void)not_used; |
| 214 | SET_STRLCPY(s->hostname, hostname, errstrp); |
| 215 | return (0); |
| 216 | } |
| 217 | |
| 218 | int |
| 219 | cfg_server_set_port(struct cfg_server *s, struct cfg_server_list *not_used, |
| 220 | const char *port_str, const char **errstrp) |
| 221 | { |
| 222 | const char *errstr; |
| 223 | unsigned int port; |
| 224 | |
| 225 | (void)not_used; |
| 226 | |
| 227 | if (!port_str || !port_str[0]) { |
| 228 | if (errstrp) |
| 229 | *errstrp = "empty"; |
| 230 | return (-1); |
| 231 | } |
| 232 | |
| 233 | port = (unsigned int)strtonum(port_str, 1, UINT16_MAX, &errstr); |
| 234 | if (errstr) { |
| 235 | if (errstrp) |
| 236 | *errstrp = errstr; |
| 237 | return (-1); |
| 238 | } |
| 239 | s->port = port; |
| 240 | |
| 241 | return (0); |
| 242 | } |
| 243 | |
| 244 | int |
| 245 | cfg_server_set_user(struct cfg_server *s, struct cfg_server_list *not_used, |
| 246 | const char *user, const char **errstrp) |
| 247 | { |
| 248 | (void)not_used; |
| 249 | SET_STRLCPY(s->user, user, errstrp); |
| 250 | return (0); |
| 251 | } |
| 252 | |
| 253 | int |
| 254 | cfg_server_set_password(struct cfg_server *s, struct cfg_server_list *not_used, |
| 255 | const char *password, const char **errstrp) |
| 256 | { |
| 257 | (void)not_used; |
| 258 | SET_STRLCPY(s->password, password, errstrp); |
| 259 | return (0); |
| 260 | } |
| 261 | |
| 262 | int |
| 263 | cfg_server_set_tls(struct cfg_server *s, struct cfg_server_list *not_used, |
| 264 | const char *tls, const char **errstrp) |
| 265 | { |
| 266 | (void)not_used; |
| 267 | |
| 268 | if (!tls || !tls[0]) { |
| 269 | if (errstrp) |
| 270 | *errstrp = "empty"; |
| 271 | return (-1); |
| 272 | } |
| 273 | |
| 274 | if (0 == strcasecmp("may", tls)) |
| 275 | s->tls = CFG_TLS_MAY; |
| 276 | else if (0 == strcasecmp("none", tls)) |
| 277 | s->tls = CFG_TLS_NONE; |
| 278 | else if (0 == strcasecmp("required", tls)) |
| 279 | s->tls = CFG_TLS_REQUIRED; |
| 280 | else { |
| 281 | if (NULL != errstrp) |
| 282 | *errstrp = "invalid"; |
| 283 | return (-1); |
| 284 | } |
| 285 | return (0); |
| 286 | } |
| 287 | |
| 288 | int |
| 289 | cfg_server_set_tls_cipher_suite(struct cfg_server *s, |
| 290 | struct cfg_server_list *not_used,const char *suite, const char **errstrp) |
| 291 | { |
| 292 | (void)not_used; |
| 293 | SET_STRLCPY(s->tls_cipher_suite, suite, errstrp); |
| 294 | return (0); |
| 295 | } |
| 296 | |
| 297 | int |
| 298 | cfg_server_set_ca_dir(struct cfg_server *s, struct cfg_server_list *not_used, |
| 299 | const char *ca_dir, const char **errstrp) |
| 300 | { |
| 301 | (void)not_used; |
| 302 | SET_STRLCPY(s->ca_dir, ca_dir, errstrp); |
| 303 | return (0); |
| 304 | } |
| 305 | |
| 306 | int |
| 307 | cfg_server_set_ca_file(struct cfg_server *s, struct cfg_server_list *not_used, |
| 308 | const char *ca_file, const char **errstrp) |
| 309 | { |
| 310 | (void)not_used; |
| 311 | SET_STRLCPY(s->ca_file, ca_file, errstrp); |
| 312 | return (0); |
| 313 | } |
| 314 | |
| 315 | int |
| 316 | cfg_server_set_client_cert(struct cfg_server *s, |
| 317 | struct cfg_server_list *not_used, const char *client_cert, |
| 318 | const char **errstrp) |
| 319 | { |
| 320 | (void)not_used; |
| 321 | SET_STRLCPY(s->client_cert, client_cert, errstrp); |
| 322 | return (0); |
| 323 | } |
| 324 | |
| 325 | int |
| 326 | cfg_server_set_reconnect_attempts(struct cfg_server *s, |
| 327 | struct cfg_server_list *not_used,const char *num_str, |
| 328 | const char **errstrp) |
| 329 | { |
| 330 | (void)not_used; |
| 331 | SET_UINTNUM(s->reconnect_attempts, num_str, errstrp); |
| 332 | return (0); |
| 333 | } |
| 334 | |
| 335 | int |
| 336 | cfg_server_validate(struct cfg_server *s, const char **errstrp) |
| 337 | { |
| 338 | if (!cfg_server_get_hostname(s)) { |
| 339 | if (NULL != errstrp) |
| 340 | *errstrp = "hostname missing"; |
| 341 | return (-1); |
| 342 | } |
| 343 | |
| 344 | if (!cfg_server_get_password(s)) { |
| 345 | if (NULL != errstrp) |
| 346 | *errstrp = "password missing"; |
| 347 | return (-1); |
| 348 | } |
| 349 | |
| 350 | return (0); |
| 351 | } |
| 352 | |
| 353 | const char * |
| 354 | cfg_server_get_name(struct cfg_server *s) |
| 355 | { |
| 356 | return (s->name); |
| 357 | } |
| 358 | |
| 359 | enum cfg_server_protocol |
| 360 | cfg_server_get_protocol(struct cfg_server *s) |
| 361 | { |
| 362 | return (s->protocol); |
| 363 | } |
| 364 | |
| 365 | const char * |
| 366 | cfg_server_get_protocol_str(struct cfg_server *s) |
| 367 | { |
| 368 | switch (s->protocol) { |
| 369 | case CFG_PROTO_HTTPS: |
| 370 | return ("https"); |
| 371 | case CFG_PROTO_ICY: |
| 372 | return ("icy"); |
| 373 | case CFG_PROTO_ROARAUDIO: |
| 374 | return ("roaraudio"); |
| 375 | case CFG_PROTO_HTTP: |
| 376 | default: |
| 377 | return ("http"); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | const char * |
| 382 | cfg_server_get_hostname(struct cfg_server *s) |
| 383 | { |
| 384 | return (s->hostname[0] ? s->hostname : NULL); |
| 385 | } |
| 386 | |
| 387 | unsigned int |
| 388 | cfg_server_get_port(struct cfg_server *s) |
| 389 | { |
| 390 | return (s->port ? s->port : CFG_SERVER_DEFAULT_PORT); |
| 391 | } |
| 392 | |
| 393 | const char * |
| 394 | cfg_server_get_user(struct cfg_server *s) |
| 395 | { |
| 396 | return (s->user[0] ? s->user : CFG_SERVER_DEFAULT_USER); |
| 397 | } |
| 398 | |
| 399 | const char * |
| 400 | cfg_server_get_password(struct cfg_server *s) |
| 401 | { |
| 402 | return (s->password[0] ? s->password : NULL); |
| 403 | } |
| 404 | |
| 405 | enum cfg_server_tls |
| 406 | cfg_server_get_tls(struct cfg_server *s) |
| 407 | { |
| 408 | if (CFG_PROTO_HTTPS == s->protocol) |
| 409 | return (CFG_TLS_REQUIRED); |
| 410 | return (s->tls); |
| 411 | } |
| 412 | |
| 413 | const char * |
| 414 | cfg_server_get_tls_str(struct cfg_server *s) |
| 415 | { |
| 416 | if (CFG_PROTO_HTTPS == s->protocol) |
| 417 | return ("required"); |
| 418 | switch (s->tls) { |
| 419 | case CFG_TLS_NONE: |
| 420 | return ("none"); |
| 421 | case CFG_TLS_REQUIRED: |
| 422 | return ("required"); |
| 423 | case CFG_TLS_MAY: |
| 424 | default: |
| 425 | return ("may"); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | const char * |
| 430 | cfg_server_get_tls_cipher_suite(struct cfg_server *s) |
| 431 | { |
| 432 | return (s->tls_cipher_suite[0] |
| 433 | ? s->tls_cipher_suite |
| 434 | : NULL); |
| 435 | } |
| 436 | |
| 437 | const char * |
| 438 | cfg_server_get_ca_dir(struct cfg_server *s) |
| 439 | { |
| 440 | return (s->ca_dir[0] ? s->ca_dir : NULL); |
| 441 | } |
| 442 | |
| 443 | const char * |
| 444 | cfg_server_get_ca_file(struct cfg_server *s) |
| 445 | { |
| 446 | return (s->ca_file[0] ? s->ca_file : NULL); |
| 447 | } |
| 448 | |
| 449 | const char * |
| 450 | cfg_server_get_client_cert(struct cfg_server *s) |
| 451 | { |
| 452 | return (s->client_cert[0] ? s->client_cert : NULL); |
| 453 | } |
| 454 | |
| 455 | unsigned int |
| 456 | cfg_server_get_reconnect_attempts(struct cfg_server *s) |
| 457 | { |
| 458 | return (s->reconnect_attempts); |
| 459 | } |