| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/ipv6.h> |
| 4 |
#include <stdio.h> |
| 5 |
|
| 6 |
#include "sd-id128.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "calendarspec.h" |
| 10 |
#include "chase.h" |
| 11 |
#include "conf-files.h" |
| 12 |
#include "conf-parser.h" |
| 13 |
#include "constants.h" |
| 14 |
#include "dns-domain.h" |
| 15 |
#include "escape.h" |
| 16 |
#include "ether-addr-util.h" |
| 17 |
#include "extract-word.h" |
| 18 |
#include "fd-util.h" |
| 19 |
#include "fileio.h" |
| 20 |
#include "fs-util.h" |
| 21 |
#include "hash-funcs.h" |
| 22 |
#include "hostname-util.h" |
| 23 |
#include "id128-util.h" |
| 24 |
#include "in-addr-prefix-util.h" |
| 25 |
#include "ip-protocol-list.h" |
| 26 |
#include "log.h" |
| 27 |
#include "missing-network.h" |
| 28 |
#include "nulstr-util.h" |
| 29 |
#include "parse-helpers.h" |
| 30 |
#include "parse-util.h" |
| 31 |
#include "path-util.h" |
| 32 |
#include "percent-util.h" |
| 33 |
#include "process-util.h" |
| 34 |
#include "rlimit-util.h" |
| 35 |
#include "set.h" |
| 36 |
#include "signal-util.h" |
| 37 |
#include "siphash24.h" |
| 38 |
#include "socket-util.h" |
| 39 |
#include "stat-util.h" |
| 40 |
#include "string-util.h" |
| 41 |
#include "strv.h" |
| 42 |
#include "syslog-util.h" |
| 43 |
#include "time-util.h" |
| 44 |
#include "utf8.h" |
| 45 |
|
| 46 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(config_file_hash_ops_fclose, |
| 47 |
char, path_hash_func, path_compare, |
| 48 |
FILE, safe_fclose); |
| 49 |
|
| 50 |
int config_item_table_lookup( |
| 51 |
const void *table, |
| 52 |
const char *section, |
| 53 |
const char *lvalue, |
| 54 |
ConfigParserCallback *ret_func, |
| 55 |
int *ret_ltype, |
| 56 |
void **ret_data, |
| 57 |
void *userdata) { |
| 58 |
|
| 59 |
assert(table); |
| 60 |
assert(lvalue); |
| 61 |
assert(ret_func); |
| 62 |
assert(ret_ltype); |
| 63 |
assert(ret_data); |
| 64 |
|
| 65 |
for (const ConfigTableItem *t = table; t->lvalue; t++) { |
| 66 |
|
| 67 |
if (!streq(lvalue, t->lvalue)) |
| 68 |
continue; |
| 69 |
|
| 70 |
if (!streq_ptr(section, t->section)) |
| 71 |
continue; |
| 72 |
|
| 73 |
*ret_func = t->parse; |
| 74 |
*ret_ltype = t->ltype; |
| 75 |
*ret_data = t->data; |
| 76 |
return 1; |
| 77 |
} |
| 78 |
|
| 79 |
*ret_func = NULL; |
| 80 |
*ret_ltype = 0; |
| 81 |
*ret_data = NULL; |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
int config_item_perf_lookup( |
| 86 |
const void *table, |
| 87 |
const char *section, |
| 88 |
const char *lvalue, |
| 89 |
ConfigParserCallback *ret_func, |
| 90 |
int *ret_ltype, |
| 91 |
void **ret_data, |
| 92 |
void *userdata) { |
| 93 |
|
| 94 |
ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table; |
| 95 |
const ConfigPerfItem *p; |
| 96 |
|
| 97 |
assert(table); |
| 98 |
assert(lvalue); |
| 99 |
assert(ret_func); |
| 100 |
assert(ret_ltype); |
| 101 |
assert(ret_data); |
| 102 |
|
| 103 |
if (section) { |
| 104 |
const char *key; |
| 105 |
|
| 106 |
key = strjoina(section, ".", lvalue); |
| 107 |
p = lookup(key, strlen(key)); |
| 108 |
} else |
| 109 |
p = lookup(lvalue, strlen(lvalue)); |
| 110 |
if (!p) { |
| 111 |
*ret_func = NULL; |
| 112 |
*ret_ltype = 0; |
| 113 |
*ret_data = NULL; |
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
*ret_func = p->parse; |
| 118 |
*ret_ltype = p->ltype; |
| 119 |
*ret_data = (uint8_t*) userdata + p->offset; |
| 120 |
return 1; |
| 121 |
} |
| 122 |
|
| 123 |
/* Run the user supplied parser for an assignment */ |
| 124 |
static int next_assignment( |
| 125 |
const char *unit, |
| 126 |
const char *filename, |
| 127 |
unsigned line, |
| 128 |
ConfigItemLookup lookup, |
| 129 |
const void *table, |
| 130 |
const char *section, |
| 131 |
unsigned section_line, |
| 132 |
const char *lvalue, |
| 133 |
const char *rvalue, |
| 134 |
ConfigParseFlags flags, |
| 135 |
void *userdata) { |
| 136 |
|
| 137 |
ConfigParserCallback func = NULL; |
| 138 |
int ltype = 0; |
| 139 |
void *data = NULL; |
| 140 |
int r; |
| 141 |
|
| 142 |
assert(filename); |
| 143 |
assert(line > 0); |
| 144 |
assert(lookup); |
| 145 |
assert(lvalue); |
| 146 |
assert(rvalue); |
| 147 |
|
| 148 |
r = lookup(table, section, lvalue, &func, <ype, &data, userdata); |
| 149 |
if (r < 0) |
| 150 |
return r; |
| 151 |
if (r > 0) { |
| 152 |
if (!func) |
| 153 |
return 0; |
| 154 |
|
| 155 |
return func(unit, filename, line, section, section_line, |
| 156 |
lvalue, ltype, rvalue, data, userdata); |
| 157 |
} |
| 158 |
|
| 159 |
/* Warn about unknown non-extension fields. */ |
| 160 |
if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-")) |
| 161 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 162 |
"Unknown key '%s'%s%s%s, ignoring.", |
| 163 |
lvalue, |
| 164 |
section ? " in section [" : "", |
| 165 |
strempty(section), |
| 166 |
section ? "]" : ""); |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
/* Parse a single logical line */ |
| 172 |
static int parse_line( |
| 173 |
const char *unit, |
| 174 |
const char *filename, |
| 175 |
unsigned line, |
| 176 |
const char *sections, |
| 177 |
ConfigItemLookup lookup, |
| 178 |
const void *table, |
| 179 |
ConfigParseFlags flags, |
| 180 |
char **section, |
| 181 |
unsigned *section_line, |
| 182 |
bool *section_ignored, |
| 183 |
char *l, /* is modified */ |
| 184 |
void *userdata) { |
| 185 |
|
| 186 |
char *e; |
| 187 |
|
| 188 |
assert(filename); |
| 189 |
assert(line > 0); |
| 190 |
assert(lookup); |
| 191 |
assert(l); |
| 192 |
assert(section); |
| 193 |
assert(section_line); |
| 194 |
assert(section_ignored); |
| 195 |
|
| 196 |
l = strstrip(l); |
| 197 |
if (isempty(l)) |
| 198 |
return 0; |
| 199 |
|
| 200 |
if (l[0] == '\n') |
| 201 |
return 0; |
| 202 |
|
| 203 |
if (!utf8_is_valid(l)) |
| 204 |
return log_syntax_invalid_utf8(unit, LOG_WARNING, filename, line, l); |
| 205 |
|
| 206 |
if (l[0] == '[') { |
| 207 |
_cleanup_free_ char *n = NULL; |
| 208 |
size_t k; |
| 209 |
|
| 210 |
k = strlen(l); |
| 211 |
assert(k > 0); |
| 212 |
|
| 213 |
if (l[k-1] != ']') |
| 214 |
return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EBADMSG), "Invalid section header '%s'", l); |
| 215 |
|
| 216 |
n = strndup(l+1, k-2); |
| 217 |
if (!n) |
| 218 |
return log_oom(); |
| 219 |
|
| 220 |
if (!string_is_safe(n, /* flags= */ 0)) |
| 221 |
return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EBADMSG), "Section header invalid '%s'", l); |
| 222 |
|
| 223 |
if (sections && !nulstr_contains(sections, n)) { |
| 224 |
bool ignore; |
| 225 |
|
| 226 |
ignore = (flags & CONFIG_PARSE_RELAXED) || startswith(n, "X-"); |
| 227 |
|
| 228 |
if (!ignore) |
| 229 |
NULSTR_FOREACH(t, sections) |
| 230 |
if (streq_ptr(n, startswith(t, "-"))) { /* Ignore sections prefixed with "-" in valid section list */ |
| 231 |
ignore = true; |
| 232 |
break; |
| 233 |
} |
| 234 |
|
| 235 |
if (!ignore) |
| 236 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n); |
| 237 |
|
| 238 |
*section = mfree(*section); |
| 239 |
*section_line = 0; |
| 240 |
*section_ignored = true; |
| 241 |
} else { |
| 242 |
free_and_replace(*section, n); |
| 243 |
*section_line = line; |
| 244 |
*section_ignored = false; |
| 245 |
} |
| 246 |
|
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
if (sections && !*section) { |
| 251 |
if (!(flags & CONFIG_PARSE_RELAXED) && !*section_ignored) |
| 252 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring."); |
| 253 |
|
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
e = strchr(l, '='); |
| 258 |
if (!e) |
| 259 |
return log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 260 |
"Missing '=', ignoring line."); |
| 261 |
if (e == l) |
| 262 |
return log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 263 |
"Missing key name before '=', ignoring line."); |
| 264 |
|
| 265 |
*e = 0; |
| 266 |
e++; |
| 267 |
|
| 268 |
return next_assignment(unit, |
| 269 |
filename, |
| 270 |
line, |
| 271 |
lookup, |
| 272 |
table, |
| 273 |
*section, |
| 274 |
*section_line, |
| 275 |
strstrip(l), |
| 276 |
strstrip(e), |
| 277 |
flags, |
| 278 |
userdata); |
| 279 |
} |
| 280 |
|
| 281 |
/* Go through the file and parse each line */ |
| 282 |
int config_parse( |
| 283 |
const char *unit, |
| 284 |
const char *filename, |
| 285 |
FILE *f, |
| 286 |
const char *sections, |
| 287 |
ConfigItemLookup lookup, |
| 288 |
const void *table, |
| 289 |
ConfigParseFlags flags, |
| 290 |
void *userdata, |
| 291 |
struct stat *ret_stat) { |
| 292 |
|
| 293 |
_cleanup_free_ char *section = NULL, *continuation = NULL; |
| 294 |
_cleanup_fclose_ FILE *ours = NULL; |
| 295 |
unsigned line = 0, section_line = 0; |
| 296 |
bool section_ignored = false, bom_seen = false; |
| 297 |
struct stat st; |
| 298 |
int r, fd; |
| 299 |
|
| 300 |
assert(filename); |
| 301 |
assert(lookup); |
| 302 |
|
| 303 |
if (!f) { |
| 304 |
f = ours = fopen(filename, "re"); |
| 305 |
if (!f) { |
| 306 |
/* Only log on request, except for ENOENT, |
| 307 |
* since we return 0 to the caller. */ |
| 308 |
if ((flags & CONFIG_PARSE_WARN) || errno == ENOENT) |
| 309 |
log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno, |
| 310 |
"Failed to open configuration file '%s': %m", filename); |
| 311 |
|
| 312 |
if (errno == ENOENT) { |
| 313 |
if (ret_stat) |
| 314 |
*ret_stat = (struct stat) {}; |
| 315 |
|
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
return -errno; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
fd = fileno(f); |
| 324 |
if (fd >= 0) { /* stream might not have an fd, let's be careful hence */ |
| 325 |
|
| 326 |
if (fstat(fd, &st) < 0) |
| 327 |
return log_full_errno(FLAGS_SET(flags, CONFIG_PARSE_WARN) ? LOG_ERR : LOG_DEBUG, errno, |
| 328 |
"Failed to fstat(%s): %m", filename); |
| 329 |
|
| 330 |
(void) stat_warn_permissions(filename, &st); |
| 331 |
} else |
| 332 |
st = (struct stat) {}; |
| 333 |
|
| 334 |
for (;;) { |
| 335 |
_cleanup_free_ char *buf = NULL; |
| 336 |
bool escaped = false; |
| 337 |
char *l, *p, *e; |
| 338 |
|
| 339 |
r = read_line(f, LONG_LINE_MAX, &buf); |
| 340 |
if (r == 0) |
| 341 |
break; |
| 342 |
if (r == -ENOBUFS) { |
| 343 |
if (flags & CONFIG_PARSE_WARN) |
| 344 |
log_error_errno(r, "%s:%u: Line too long", filename, line); |
| 345 |
|
| 346 |
return r; |
| 347 |
} |
| 348 |
if (r < 0) { |
| 349 |
if (FLAGS_SET(flags, CONFIG_PARSE_WARN)) |
| 350 |
log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line); |
| 351 |
|
| 352 |
return r; |
| 353 |
} |
| 354 |
|
| 355 |
line++; |
| 356 |
|
| 357 |
l = skip_leading_chars(buf, WHITESPACE); |
| 358 |
if (*l != '\0' && strchr(COMMENTS, *l)) |
| 359 |
continue; |
| 360 |
|
| 361 |
l = buf; |
| 362 |
if (!bom_seen) { |
| 363 |
char *q; |
| 364 |
|
| 365 |
q = startswith(buf, UTF8_BYTE_ORDER_MARK); |
| 366 |
if (q) { |
| 367 |
l = q; |
| 368 |
bom_seen = true; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
if (continuation) { |
| 373 |
if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) { |
| 374 |
if (flags & CONFIG_PARSE_WARN) |
| 375 |
log_error("%s:%u: Continuation line too long", filename, line); |
| 376 |
return -ENOBUFS; |
| 377 |
} |
| 378 |
|
| 379 |
if (!strextend(&continuation, l)) { |
| 380 |
if (flags & CONFIG_PARSE_WARN) |
| 381 |
log_oom(); |
| 382 |
return -ENOMEM; |
| 383 |
} |
| 384 |
|
| 385 |
p = continuation; |
| 386 |
} else |
| 387 |
p = l; |
| 388 |
|
| 389 |
for (e = p; *e; e++) { |
| 390 |
if (escaped) |
| 391 |
escaped = false; |
| 392 |
else if (*e == '\\') |
| 393 |
escaped = true; |
| 394 |
} |
| 395 |
|
| 396 |
if (escaped) { |
| 397 |
*(e-1) = ' '; |
| 398 |
|
| 399 |
if (!continuation) { |
| 400 |
continuation = strdup(l); |
| 401 |
if (!continuation) { |
| 402 |
if (flags & CONFIG_PARSE_WARN) |
| 403 |
log_oom(); |
| 404 |
return -ENOMEM; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
continue; |
| 409 |
} |
| 410 |
|
| 411 |
r = parse_line(unit, |
| 412 |
filename, |
| 413 |
line, |
| 414 |
sections, |
| 415 |
lookup, |
| 416 |
table, |
| 417 |
flags, |
| 418 |
§ion, |
| 419 |
§ion_line, |
| 420 |
§ion_ignored, |
| 421 |
p, |
| 422 |
userdata); |
| 423 |
if (r < 0) { |
| 424 |
if (flags & CONFIG_PARSE_WARN) |
| 425 |
log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line); |
| 426 |
return r; |
| 427 |
} |
| 428 |
|
| 429 |
continuation = mfree(continuation); |
| 430 |
} |
| 431 |
|
| 432 |
if (continuation) { |
| 433 |
r = parse_line(unit, |
| 434 |
filename, |
| 435 |
++line, |
| 436 |
sections, |
| 437 |
lookup, |
| 438 |
table, |
| 439 |
flags, |
| 440 |
§ion, |
| 441 |
§ion_line, |
| 442 |
§ion_ignored, |
| 443 |
continuation, |
| 444 |
userdata); |
| 445 |
if (r < 0) { |
| 446 |
if (flags & CONFIG_PARSE_WARN) |
| 447 |
log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line); |
| 448 |
return r; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
if (ret_stat) |
| 453 |
*ret_stat = st; |
| 454 |
|
| 455 |
return 1; |
| 456 |
} |
| 457 |
|
| 458 |
int hashmap_put_stats_by_path(Hashmap **stats_by_path, const char *path, const struct stat *st) { |
| 459 |
_cleanup_free_ struct stat *st_copy = NULL; |
| 460 |
_cleanup_free_ char *path_copy = NULL; |
| 461 |
int r; |
| 462 |
|
| 463 |
assert(stats_by_path); |
| 464 |
assert(path); |
| 465 |
assert(st); |
| 466 |
|
| 467 |
st_copy = newdup(struct stat, st, 1); |
| 468 |
if (!st_copy) |
| 469 |
return -ENOMEM; |
| 470 |
|
| 471 |
path_copy = strdup(path); |
| 472 |
if (!path_copy) |
| 473 |
return -ENOMEM; |
| 474 |
|
| 475 |
r = hashmap_ensure_put(stats_by_path, &path_hash_ops_free_free, path_copy, st_copy); |
| 476 |
if (r < 0) |
| 477 |
return r; |
| 478 |
|
| 479 |
assert(r > 0); |
| 480 |
TAKE_PTR(path_copy); |
| 481 |
TAKE_PTR(st_copy); |
| 482 |
return 0; |
| 483 |
} |
| 484 |
|
| 485 |
static int config_parse_many_files( |
| 486 |
const char *root, |
| 487 |
int root_fd, |
| 488 |
const char* const* conf_files, |
| 489 |
char **files, |
| 490 |
const char *sections, |
| 491 |
ConfigItemLookup lookup, |
| 492 |
const void *table, |
| 493 |
ConfigParseFlags flags, |
| 494 |
void *userdata, |
| 495 |
Hashmap **ret_stats_by_path) { |
| 496 |
|
| 497 |
_cleanup_hashmap_free_ Hashmap *stats_by_path = NULL; |
| 498 |
_cleanup_ordered_hashmap_free_ OrderedHashmap *dropins = NULL; |
| 499 |
_cleanup_set_free_ Set *inodes = NULL; |
| 500 |
struct stat st; |
| 501 |
int r, level = FLAGS_SET(flags, CONFIG_PARSE_WARN) ? LOG_WARNING : LOG_DEBUG; |
| 502 |
|
| 503 |
if (ret_stats_by_path) { |
| 504 |
stats_by_path = hashmap_new(&path_hash_ops_free_free); |
| 505 |
if (!stats_by_path) |
| 506 |
return log_oom_full(level); |
| 507 |
} |
| 508 |
|
| 509 |
/* Pin and stat() all dropins */ |
| 510 |
STRV_FOREACH(fn, files) { |
| 511 |
_cleanup_fclose_ FILE *f = NULL; |
| 512 |
r = chase_and_fopenat_unlocked(root_fd, root_fd, *fn, CHASE_MUST_BE_REGULAR, "re", /* ret_path= */ NULL, &f); |
| 513 |
if (r == -ENOENT) |
| 514 |
continue; |
| 515 |
if (r < 0) |
| 516 |
return log_full_errno(level, r, "Failed to open %s: %m", *fn); |
| 517 |
|
| 518 |
int fd = fileno(f); |
| 519 |
assert(fd >= 0); |
| 520 |
|
| 521 |
r = ordered_hashmap_ensure_put(&dropins, &config_file_hash_ops_fclose, *fn, f); |
| 522 |
if (r < 0) { |
| 523 |
assert(r == -ENOMEM); |
| 524 |
return log_oom_full(level); |
| 525 |
} |
| 526 |
assert(r > 0); |
| 527 |
TAKE_PTR(f); |
| 528 |
|
| 529 |
/* Get inodes for all drop-ins. Later we'll verify if main config is a symlink to or is |
| 530 |
* symlinked as one of them. If so, we skip reading main config file directly. */ |
| 531 |
|
| 532 |
_cleanup_free_ struct stat *st_dropin = new(struct stat, 1); |
| 533 |
if (!st_dropin) |
| 534 |
return log_oom_full(level); |
| 535 |
|
| 536 |
if (fstat(fd, st_dropin) < 0) |
| 537 |
return log_full_errno(level, errno, "Failed to stat %s: %m", *fn); |
| 538 |
|
| 539 |
r = set_ensure_consume(&inodes, &inode_hash_ops, TAKE_PTR(st_dropin)); |
| 540 |
if (r < 0) |
| 541 |
return log_oom_full(level); |
| 542 |
} |
| 543 |
|
| 544 |
/* First process the first found main config file. */ |
| 545 |
STRV_FOREACH(fn, conf_files) { |
| 546 |
_cleanup_fclose_ FILE *f = NULL; |
| 547 |
r = chase_and_fopenat_unlocked(root_fd, root_fd, *fn, CHASE_MUST_BE_REGULAR, "re", /* ret_path= */ NULL, &f); |
| 548 |
if (r == -ENOENT) |
| 549 |
continue; |
| 550 |
if (r < 0) |
| 551 |
return log_full_errno(level, r, "Failed to open %s: %m", *fn); |
| 552 |
|
| 553 |
if (inodes) { |
| 554 |
if (fstat(fileno(f), &st) < 0) |
| 555 |
return log_full_errno(level, errno, "Failed to stat %s: %m", *fn); |
| 556 |
|
| 557 |
if (set_contains(inodes, &st)) { |
| 558 |
log_debug("%s: symlink to/symlinked as drop-in, will be read later.", *fn); |
| 559 |
break; |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
r = config_parse(/* unit= */ NULL, *fn, f, sections, lookup, table, flags, userdata, &st); |
| 564 |
if (r < 0) |
| 565 |
return r; /* config_parse() logs internally. */ |
| 566 |
assert(r > 0); |
| 567 |
|
| 568 |
if (ret_stats_by_path) { |
| 569 |
r = hashmap_put_stats_by_path(&stats_by_path, *fn, &st); |
| 570 |
if (r < 0) |
| 571 |
return log_full_errno(level, r, "Failed to save stats of %s: %m", *fn); |
| 572 |
} |
| 573 |
|
| 574 |
break; |
| 575 |
} |
| 576 |
|
| 577 |
/* Then read all the drop-ins. */ |
| 578 |
const char *path_dropin; |
| 579 |
FILE *f_dropin; |
| 580 |
ORDERED_HASHMAP_FOREACH_KEY(f_dropin, path_dropin, dropins) { |
| 581 |
r = config_parse(/* unit= */ NULL, path_dropin, f_dropin, sections, lookup, table, flags, userdata, &st); |
| 582 |
if (r < 0) |
| 583 |
return r; /* config_parse() logs internally. */ |
| 584 |
assert(r > 0); |
| 585 |
|
| 586 |
if (ret_stats_by_path) { |
| 587 |
r = hashmap_put_stats_by_path(&stats_by_path, path_dropin, &st); |
| 588 |
if (r < 0) |
| 589 |
return log_full_errno(level, r, "Failed to save stats of %s: %m", path_dropin); |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
if (ret_stats_by_path) |
| 594 |
*ret_stats_by_path = TAKE_PTR(stats_by_path); |
| 595 |
|
| 596 |
return 0; |
| 597 |
} |
| 598 |
|
| 599 |
static int normalize_root_fd(const char *root, int *root_fd, int *ret_opened_fd) { |
| 600 |
assert(root_fd); |
| 601 |
assert(ret_opened_fd); |
| 602 |
|
| 603 |
/* Normalizes a root dir specification: if root_fd is already valid, keep it. Otherwise, we open the |
| 604 |
* specified dir */ |
| 605 |
|
| 606 |
if (wildcard_fd_is_valid(*root_fd)) { |
| 607 |
*ret_opened_fd = -EBADF; |
| 608 |
return 0; |
| 609 |
} |
| 610 |
|
| 611 |
if (empty_or_root(root)) { |
| 612 |
*root_fd = XAT_FDROOT; |
| 613 |
*ret_opened_fd = -EBADF; |
| 614 |
return 0; |
| 615 |
} |
| 616 |
|
| 617 |
int fd = open(root, O_CLOEXEC|O_PATH|O_DIRECTORY); |
| 618 |
if (fd < 0) |
| 619 |
return log_error_errno(errno, "Failed to open root directory '%s': %m", root); |
| 620 |
|
| 621 |
*ret_opened_fd = *root_fd = fd; |
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
/* Parse each config file in the directories specified as strv. */ |
| 626 |
int config_parse_many_full( |
| 627 |
const char* const* conf_files, |
| 628 |
const char* const* conf_file_dirs, |
| 629 |
const char *dropin_dirname, |
| 630 |
const char *root, |
| 631 |
int root_fd, |
| 632 |
const char *sections, |
| 633 |
ConfigItemLookup lookup, |
| 634 |
const void *table, |
| 635 |
ConfigParseFlags flags, |
| 636 |
void *userdata, |
| 637 |
Hashmap **ret_stats_by_path, |
| 638 |
char ***ret_dropin_files) { |
| 639 |
|
| 640 |
_cleanup_strv_free_ char **files = NULL; |
| 641 |
int r; |
| 642 |
|
| 643 |
assert(conf_file_dirs); |
| 644 |
assert(dropin_dirname); |
| 645 |
assert(table); |
| 646 |
|
| 647 |
_cleanup_close_ int opened_root_fd = -EBADF; |
| 648 |
r = normalize_root_fd(root, &root_fd, &opened_root_fd); |
| 649 |
if (r < 0) |
| 650 |
return r; |
| 651 |
|
| 652 |
r = conf_files_list_dropins( |
| 653 |
&files, |
| 654 |
dropin_dirname, |
| 655 |
root, |
| 656 |
root_fd, |
| 657 |
FLAGS_SET(flags, CONFIG_PARSE_WARN) ? CONF_FILES_WARN : 0, |
| 658 |
conf_file_dirs); |
| 659 |
if (r < 0) |
| 660 |
return log_full_errno(FLAGS_SET(flags, CONFIG_PARSE_WARN) ? LOG_WARNING : LOG_DEBUG, r, |
| 661 |
"Failed to list drop-ins in %s: %m", dropin_dirname); |
| 662 |
|
| 663 |
r = config_parse_many_files( |
| 664 |
root, |
| 665 |
root_fd, |
| 666 |
conf_files, |
| 667 |
files, |
| 668 |
sections, |
| 669 |
lookup, |
| 670 |
table, |
| 671 |
flags, |
| 672 |
userdata, |
| 673 |
ret_stats_by_path); |
| 674 |
if (r < 0) |
| 675 |
return r; /* config_parse_many_files() logs internally. */ |
| 676 |
|
| 677 |
if (ret_dropin_files) |
| 678 |
*ret_dropin_files = TAKE_PTR(files); |
| 679 |
|
| 680 |
return 0; |
| 681 |
} |
| 682 |
|
| 683 |
int config_parse_standard_file_with_dropins_full( |
| 684 |
const char *root, |
| 685 |
int root_fd, |
| 686 |
const char *main_file, /* A path like "systemd/frobnicator.conf" */ |
| 687 |
const char *sections, |
| 688 |
ConfigItemLookup lookup, |
| 689 |
const void *table, |
| 690 |
ConfigParseFlags flags, |
| 691 |
void *userdata, |
| 692 |
Hashmap **ret_stats_by_path, |
| 693 |
char ***ret_dropin_files) { |
| 694 |
|
| 695 |
const char* const *conf_file_dirs = (const char* const*) CONF_PATHS_STRV(""); |
| 696 |
_cleanup_strv_free_ char **conf_files = NULL; |
| 697 |
int r, level = FLAGS_SET(flags, CONFIG_PARSE_WARN) ? LOG_WARNING : LOG_DEBUG; |
| 698 |
|
| 699 |
/* Build the list of main config files */ |
| 700 |
r = strv_extend_strv_concat( |
| 701 |
&conf_files, |
| 702 |
conf_file_dirs, |
| 703 |
main_file); |
| 704 |
if (r < 0) |
| 705 |
return log_oom_full(level); |
| 706 |
|
| 707 |
_cleanup_free_ char *dropin_dirname = strjoin(main_file, ".d"); |
| 708 |
if (!dropin_dirname) |
| 709 |
return log_oom_full(level); |
| 710 |
|
| 711 |
return config_parse_many_full( |
| 712 |
(const char* const*) conf_files, |
| 713 |
conf_file_dirs, |
| 714 |
dropin_dirname, |
| 715 |
root, |
| 716 |
root_fd, |
| 717 |
sections, |
| 718 |
lookup, |
| 719 |
table, |
| 720 |
flags, |
| 721 |
userdata, |
| 722 |
ret_stats_by_path, |
| 723 |
ret_dropin_files); |
| 724 |
} |
| 725 |
|
| 726 |
static int dropins_get_stats_by_path( |
| 727 |
const char* conf_file, |
| 728 |
const char* const* conf_file_dirs, |
| 729 |
Hashmap **stats_by_path) { |
| 730 |
|
| 731 |
_cleanup_strv_free_ char **files = NULL; |
| 732 |
_cleanup_free_ char *dropin_dirname = NULL; |
| 733 |
int r; |
| 734 |
|
| 735 |
assert(conf_file); |
| 736 |
assert(conf_file_dirs); |
| 737 |
assert(stats_by_path); |
| 738 |
|
| 739 |
r = path_extract_filename(conf_file, &dropin_dirname); |
| 740 |
if (r < 0) |
| 741 |
return r; |
| 742 |
if (r == O_DIRECTORY) |
| 743 |
return -EINVAL; |
| 744 |
|
| 745 |
if (!strextend(&dropin_dirname, ".d")) |
| 746 |
return -ENOMEM; |
| 747 |
|
| 748 |
r = conf_files_list_dropins( |
| 749 |
&files, |
| 750 |
dropin_dirname, |
| 751 |
/* root= */ NULL, |
| 752 |
/* root_fd= */ XAT_FDROOT, |
| 753 |
/* flags= */ 0, |
| 754 |
conf_file_dirs); |
| 755 |
if (r < 0) |
| 756 |
return r; |
| 757 |
|
| 758 |
STRV_FOREACH(fn, files) { |
| 759 |
struct stat st; |
| 760 |
|
| 761 |
if (stat(*fn, &st) < 0) { |
| 762 |
if (errno == ENOENT) |
| 763 |
continue; |
| 764 |
|
| 765 |
return -errno; |
| 766 |
} |
| 767 |
|
| 768 |
r = hashmap_put_stats_by_path(stats_by_path, *fn, &st); |
| 769 |
if (r < 0) |
| 770 |
return r; |
| 771 |
} |
| 772 |
|
| 773 |
return 0; |
| 774 |
} |
| 775 |
|
| 776 |
int config_get_stats_by_path( |
| 777 |
const char *suffix, |
| 778 |
const char *root, |
| 779 |
ConfFilesFlags flags, |
| 780 |
const char* const* dirs, |
| 781 |
bool check_dropins, |
| 782 |
Hashmap **ret) { |
| 783 |
|
| 784 |
_cleanup_hashmap_free_ Hashmap *stats_by_path = NULL; |
| 785 |
_cleanup_strv_free_ char **files = NULL; |
| 786 |
int r; |
| 787 |
|
| 788 |
assert(suffix); |
| 789 |
assert(dirs); |
| 790 |
assert(ret); |
| 791 |
|
| 792 |
/* Unlike config_parse(), this does not support stream. */ |
| 793 |
|
| 794 |
r = conf_files_list_strv(&files, suffix, root, flags, dirs); |
| 795 |
if (r < 0) |
| 796 |
return r; |
| 797 |
|
| 798 |
STRV_FOREACH(f, files) { |
| 799 |
struct stat st; |
| 800 |
|
| 801 |
/* First read the main config file. */ |
| 802 |
if (stat(*f, &st) < 0) { |
| 803 |
if (errno == ENOENT) |
| 804 |
continue; |
| 805 |
|
| 806 |
return -errno; |
| 807 |
} |
| 808 |
|
| 809 |
/* Skipping an empty file. */ |
| 810 |
if (null_or_empty(&st)) |
| 811 |
continue; |
| 812 |
|
| 813 |
r = hashmap_put_stats_by_path(&stats_by_path, *f, &st); |
| 814 |
if (r < 0) |
| 815 |
return r; |
| 816 |
|
| 817 |
if (!check_dropins) |
| 818 |
continue; |
| 819 |
|
| 820 |
/* Then read all the drop-ins if requested. */ |
| 821 |
r = dropins_get_stats_by_path(*f, dirs, &stats_by_path); |
| 822 |
if (r < 0) |
| 823 |
return r; |
| 824 |
} |
| 825 |
|
| 826 |
*ret = TAKE_PTR(stats_by_path); |
| 827 |
return 0; |
| 828 |
} |
| 829 |
|
| 830 |
bool stats_by_path_equal(Hashmap *a, Hashmap *b) { |
| 831 |
struct stat *st_a, *st_b; |
| 832 |
const char *path; |
| 833 |
|
| 834 |
if (hashmap_size(a) != hashmap_size(b)) |
| 835 |
return false; |
| 836 |
|
| 837 |
HASHMAP_FOREACH_KEY(st_a, path, a) { |
| 838 |
st_b = hashmap_get(b, path); |
| 839 |
if (!st_b) |
| 840 |
return false; |
| 841 |
|
| 842 |
if (!stat_inode_unmodified(st_a, st_b)) |
| 843 |
return false; |
| 844 |
} |
| 845 |
|
| 846 |
return true; |
| 847 |
} |
| 848 |
|
| 849 |
int config_section_parse( |
| 850 |
const ConfigSectionParser *parsers, |
| 851 |
size_t n_parsers, |
| 852 |
const char *unit, |
| 853 |
const char *filename, |
| 854 |
unsigned line, |
| 855 |
const char *section, |
| 856 |
unsigned section_line, |
| 857 |
const char *lvalue, |
| 858 |
int ltype, |
| 859 |
const char *rvalue, |
| 860 |
void *userdata) { |
| 861 |
|
| 862 |
assert(parsers); |
| 863 |
assert(n_parsers > 0); |
| 864 |
assert(ltype >= 0); |
| 865 |
assert((size_t) ltype < n_parsers); |
| 866 |
assert(userdata); |
| 867 |
|
| 868 |
const ConfigSectionParser *e = parsers + ltype; |
| 869 |
assert(e->parser); |
| 870 |
|
| 871 |
/* This is used when a object is dynamically allocated per [SECTION] in a config parser, e.g. |
| 872 |
* [Address] for systemd.network. Takes the allocated object as 'userdata', then it is passed to |
| 873 |
* config parsers in the table. The 'data' field points to an element of the passed object, where |
| 874 |
* its offset is given by the table. */ |
| 875 |
|
| 876 |
return e->parser(unit, filename, line, section, section_line, lvalue, e->ltype, rvalue, |
| 877 |
(uint8_t*) userdata + e->offset, userdata); |
| 878 |
} |
| 879 |
|
| 880 |
void config_section_hash_func(const ConfigSection *c, struct siphash *state) { |
| 881 |
siphash24_compress_string(c->filename, state); |
| 882 |
siphash24_compress_typesafe(c->line, state); |
| 883 |
} |
| 884 |
|
| 885 |
int config_section_compare_func(const ConfigSection *x, const ConfigSection *y) { |
| 886 |
int r; |
| 887 |
|
| 888 |
r = strcmp(x->filename, y->filename); |
| 889 |
if (r != 0) |
| 890 |
return r; |
| 891 |
|
| 892 |
return CMP(x->line, y->line); |
| 893 |
} |
| 894 |
|
| 895 |
DEFINE_HASH_OPS(config_section_hash_ops, ConfigSection, config_section_hash_func, config_section_compare_func); |
| 896 |
|
| 897 |
int config_section_new(const char *filename, unsigned line, ConfigSection **ret) { |
| 898 |
ConfigSection *cs; |
| 899 |
|
| 900 |
assert(filename); |
| 901 |
assert(line > 0); |
| 902 |
assert(ret); |
| 903 |
|
| 904 |
cs = malloc0(offsetof(ConfigSection, filename) + strlen(filename) + 1); |
| 905 |
if (!cs) |
| 906 |
return -ENOMEM; |
| 907 |
|
| 908 |
strcpy(cs->filename, filename); |
| 909 |
cs->line = line; |
| 910 |
|
| 911 |
*ret = TAKE_PTR(cs); |
| 912 |
return 0; |
| 913 |
} |
| 914 |
|
| 915 |
static int _hashmap_by_section_find_unused_line( |
| 916 |
HashmapBase *entries_by_section, |
| 917 |
const char *filename, |
| 918 |
unsigned *ret) { |
| 919 |
|
| 920 |
ConfigSection *cs; |
| 921 |
unsigned n = 0; |
| 922 |
void *entry; |
| 923 |
|
| 924 |
assert(ret); |
| 925 |
|
| 926 |
HASHMAP_BASE_FOREACH_KEY(entry, cs, entries_by_section) { |
| 927 |
if (filename && !streq(cs->filename, filename)) |
| 928 |
continue; |
| 929 |
n = MAX(n, cs->line); |
| 930 |
} |
| 931 |
|
| 932 |
/* overflow? */ |
| 933 |
if (n >= UINT_MAX) |
| 934 |
return -EFBIG; |
| 935 |
|
| 936 |
*ret = n + 1; |
| 937 |
return 0; |
| 938 |
} |
| 939 |
|
| 940 |
int hashmap_by_section_find_unused_line( |
| 941 |
Hashmap *entries_by_section, |
| 942 |
const char *filename, |
| 943 |
unsigned *ret) { |
| 944 |
|
| 945 |
return _hashmap_by_section_find_unused_line(HASHMAP_BASE(entries_by_section), filename, ret); |
| 946 |
} |
| 947 |
|
| 948 |
int ordered_hashmap_by_section_find_unused_line( |
| 949 |
OrderedHashmap *entries_by_section, |
| 950 |
const char *filename, |
| 951 |
unsigned *ret) { |
| 952 |
|
| 953 |
return _hashmap_by_section_find_unused_line(HASHMAP_BASE(entries_by_section), filename, ret); |
| 954 |
} |
| 955 |
|
| 956 |
#define DEFINE_PARSER(type, vartype, conv_func) \ |
| 957 |
DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype) |
| 958 |
|
| 959 |
DEFINE_PARSER(int, int, safe_atoi); |
| 960 |
DEFINE_PARSER(long, long, safe_atoli); |
| 961 |
DEFINE_PARSER(uint8, uint8_t, safe_atou8); |
| 962 |
DEFINE_PARSER(uint16, uint16_t, safe_atou16); |
| 963 |
DEFINE_PARSER(uint32, uint32_t, safe_atou32); |
| 964 |
DEFINE_PARSER(int32, int32_t, safe_atoi32); |
| 965 |
DEFINE_PARSER(uint64, uint64_t, safe_atou64); |
| 966 |
DEFINE_PARSER(unsigned, unsigned, safe_atou); |
| 967 |
DEFINE_PARSER(double, double, safe_atod); |
| 968 |
DEFINE_PARSER(nsec, nsec_t, parse_nsec); |
| 969 |
DEFINE_PARSER(sec, usec_t, parse_sec); |
| 970 |
DEFINE_PARSER(sec_def_infinity, usec_t, parse_sec_def_infinity); |
| 971 |
DEFINE_PARSER(mode, mode_t, parse_mode); |
| 972 |
DEFINE_PARSER(pid, pid_t, parse_pid); |
| 973 |
|
| 974 |
int config_parse_iec_size( |
| 975 |
const char *unit, |
| 976 |
const char *filename, |
| 977 |
unsigned line, |
| 978 |
const char *section, |
| 979 |
unsigned section_line, |
| 980 |
const char *lvalue, |
| 981 |
int ltype, |
| 982 |
const char *rvalue, |
| 983 |
void *data, |
| 984 |
void *userdata) { |
| 985 |
|
| 986 |
size_t *sz = ASSERT_PTR(data); |
| 987 |
uint64_t v; |
| 988 |
int r; |
| 989 |
|
| 990 |
assert(filename); |
| 991 |
assert(lvalue); |
| 992 |
assert(rvalue); |
| 993 |
|
| 994 |
r = parse_size(rvalue, 1024, &v); |
| 995 |
if (r >= 0 && (uint64_t) (size_t) v != v) |
| 996 |
r = -ERANGE; |
| 997 |
if (r < 0) |
| 998 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 999 |
|
| 1000 |
*sz = (size_t) v; |
| 1001 |
return 1; |
| 1002 |
} |
| 1003 |
|
| 1004 |
int config_parse_si_uint64( |
| 1005 |
const char *unit, |
| 1006 |
const char *filename, |
| 1007 |
unsigned line, |
| 1008 |
const char *section, |
| 1009 |
unsigned section_line, |
| 1010 |
const char *lvalue, |
| 1011 |
int ltype, |
| 1012 |
const char *rvalue, |
| 1013 |
void *data, |
| 1014 |
void *userdata) { |
| 1015 |
|
| 1016 |
uint64_t *sz = ASSERT_PTR(data); |
| 1017 |
int r; |
| 1018 |
|
| 1019 |
assert(filename); |
| 1020 |
assert(lvalue); |
| 1021 |
assert(rvalue); |
| 1022 |
|
| 1023 |
r = parse_size(rvalue, 1000, sz); |
| 1024 |
if (r < 0) |
| 1025 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1026 |
|
| 1027 |
return 1; |
| 1028 |
} |
| 1029 |
|
| 1030 |
int config_parse_iec_uint64( |
| 1031 |
const char *unit, |
| 1032 |
const char *filename, |
| 1033 |
unsigned line, |
| 1034 |
const char *section, |
| 1035 |
unsigned section_line, |
| 1036 |
const char *lvalue, |
| 1037 |
int ltype, |
| 1038 |
const char *rvalue, |
| 1039 |
void *data, |
| 1040 |
void *userdata) { |
| 1041 |
|
| 1042 |
uint64_t *bytes = ASSERT_PTR(data); |
| 1043 |
int r; |
| 1044 |
|
| 1045 |
assert(filename); |
| 1046 |
assert(lvalue); |
| 1047 |
assert(rvalue); |
| 1048 |
|
| 1049 |
r = parse_size(rvalue, 1024, bytes); |
| 1050 |
if (r < 0) |
| 1051 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1052 |
|
| 1053 |
return 1; |
| 1054 |
} |
| 1055 |
|
| 1056 |
int config_parse_iec_uint64_infinity( |
| 1057 |
const char *unit, |
| 1058 |
const char *filename, |
| 1059 |
unsigned line, |
| 1060 |
const char *section, |
| 1061 |
unsigned section_line, |
| 1062 |
const char *lvalue, |
| 1063 |
int ltype, |
| 1064 |
const char *rvalue, |
| 1065 |
void *data, |
| 1066 |
void *userdata) { |
| 1067 |
|
| 1068 |
uint64_t *bytes = ASSERT_PTR(data); |
| 1069 |
|
| 1070 |
assert(rvalue); |
| 1071 |
|
| 1072 |
if (streq(rvalue, "infinity")) { |
| 1073 |
*bytes = UINT64_MAX; |
| 1074 |
return 1; |
| 1075 |
} |
| 1076 |
|
| 1077 |
return config_parse_iec_uint64(unit, filename, line, section, section_line, lvalue, ltype, rvalue, data, userdata); |
| 1078 |
} |
| 1079 |
|
| 1080 |
int config_parse_bool( |
| 1081 |
const char *unit, |
| 1082 |
const char *filename, |
| 1083 |
unsigned line, |
| 1084 |
const char *section, |
| 1085 |
unsigned section_line, |
| 1086 |
const char *lvalue, |
| 1087 |
int ltype, |
| 1088 |
const char *rvalue, |
| 1089 |
void *data, |
| 1090 |
void *userdata) { |
| 1091 |
|
| 1092 |
bool *b = ASSERT_PTR(data); |
| 1093 |
bool fatal = ltype; |
| 1094 |
int r; |
| 1095 |
|
| 1096 |
assert(filename); |
| 1097 |
assert(lvalue); |
| 1098 |
assert(rvalue); |
| 1099 |
|
| 1100 |
r = parse_boolean(rvalue); |
| 1101 |
if (r < 0) { |
| 1102 |
log_syntax_parse_error_full(unit, filename, line, r, fatal, lvalue, rvalue); |
| 1103 |
return fatal ? -ENOEXEC : 0; |
| 1104 |
} |
| 1105 |
|
| 1106 |
*b = r; |
| 1107 |
return 1; /* set */ |
| 1108 |
} |
| 1109 |
|
| 1110 |
int config_parse_uint32_flag( |
| 1111 |
const char *unit, |
| 1112 |
const char *filename, |
| 1113 |
unsigned line, |
| 1114 |
const char *section, |
| 1115 |
unsigned section_line, |
| 1116 |
const char *lvalue, |
| 1117 |
int ltype, |
| 1118 |
const char *rvalue, |
| 1119 |
void *data, |
| 1120 |
void *userdata) { |
| 1121 |
|
| 1122 |
uint32_t *flags = ASSERT_PTR(data); |
| 1123 |
int r; |
| 1124 |
|
| 1125 |
assert(ltype != 0); |
| 1126 |
|
| 1127 |
r = isempty(rvalue) ? 0 : parse_boolean(rvalue); |
| 1128 |
if (r < 0) |
| 1129 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1130 |
|
| 1131 |
SET_FLAG(*flags, ltype, r); |
| 1132 |
return 1; |
| 1133 |
} |
| 1134 |
|
| 1135 |
int config_parse_uint32_invert_flag( |
| 1136 |
const char *unit, |
| 1137 |
const char *filename, |
| 1138 |
unsigned line, |
| 1139 |
const char *section, |
| 1140 |
unsigned section_line, |
| 1141 |
const char *lvalue, |
| 1142 |
int ltype, |
| 1143 |
const char *rvalue, |
| 1144 |
void *data, |
| 1145 |
void *userdata) { |
| 1146 |
|
| 1147 |
uint32_t *flags = ASSERT_PTR(data); |
| 1148 |
int r; |
| 1149 |
|
| 1150 |
assert(ltype != 0); |
| 1151 |
|
| 1152 |
r = isempty(rvalue) ? 0 : parse_boolean(rvalue); |
| 1153 |
if (r < 0) |
| 1154 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1155 |
|
| 1156 |
SET_FLAG(*flags, ltype, !r); |
| 1157 |
return 1; |
| 1158 |
} |
| 1159 |
|
| 1160 |
int config_parse_id128( |
| 1161 |
const char *unit, |
| 1162 |
const char *filename, |
| 1163 |
unsigned line, |
| 1164 |
const char *section, |
| 1165 |
unsigned section_line, |
| 1166 |
const char *lvalue, |
| 1167 |
int ltype, |
| 1168 |
const char *rvalue, |
| 1169 |
void *data, |
| 1170 |
void *userdata) { |
| 1171 |
|
| 1172 |
sd_id128_t *result = data; |
| 1173 |
int r; |
| 1174 |
|
| 1175 |
assert(filename); |
| 1176 |
assert(lvalue); |
| 1177 |
assert(rvalue); |
| 1178 |
|
| 1179 |
r = id128_from_string_nonzero(rvalue, result); |
| 1180 |
if (r == -ENXIO) { |
| 1181 |
log_syntax(unit, LOG_WARNING, filename, line, r, "128-bit ID/UUID is all 0, ignoring: %s", rvalue); |
| 1182 |
return 0; |
| 1183 |
} |
| 1184 |
if (r < 0) |
| 1185 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1186 |
|
| 1187 |
return 1; |
| 1188 |
} |
| 1189 |
|
| 1190 |
int config_parse_tristate( |
| 1191 |
const char *unit, |
| 1192 |
const char *filename, |
| 1193 |
unsigned line, |
| 1194 |
const char *section, |
| 1195 |
unsigned section_line, |
| 1196 |
const char *lvalue, |
| 1197 |
int ltype, |
| 1198 |
const char *rvalue, |
| 1199 |
void *data, |
| 1200 |
void *userdata) { |
| 1201 |
|
| 1202 |
int r, *t = ASSERT_PTR(data); |
| 1203 |
|
| 1204 |
assert(filename); |
| 1205 |
assert(lvalue); |
| 1206 |
assert(rvalue); |
| 1207 |
|
| 1208 |
/* A tristate is pretty much a boolean, except that it can also take an empty string, |
| 1209 |
* indicating "uninitialized", much like NULL is for a pointer type. */ |
| 1210 |
|
| 1211 |
if (isempty(rvalue)) { |
| 1212 |
*t = -1; |
| 1213 |
return 1; |
| 1214 |
} |
| 1215 |
|
| 1216 |
r = parse_tristate(rvalue, t); |
| 1217 |
if (r < 0) |
| 1218 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1219 |
|
| 1220 |
return 1; |
| 1221 |
} |
| 1222 |
|
| 1223 |
int config_parse_string( |
| 1224 |
const char *unit, |
| 1225 |
const char *filename, |
| 1226 |
unsigned line, |
| 1227 |
const char *section, |
| 1228 |
unsigned section_line, |
| 1229 |
const char *lvalue, |
| 1230 |
int ltype, |
| 1231 |
const char *rvalue, |
| 1232 |
void *data, |
| 1233 |
void *userdata) { |
| 1234 |
|
| 1235 |
char **s = ASSERT_PTR(data); |
| 1236 |
int r; |
| 1237 |
|
| 1238 |
assert(filename); |
| 1239 |
assert(lvalue); |
| 1240 |
assert(rvalue); |
| 1241 |
|
| 1242 |
if (isempty(rvalue)) { |
| 1243 |
*s = mfree(*s); |
| 1244 |
return 1; |
| 1245 |
} |
| 1246 |
|
| 1247 |
if (FLAGS_SET(ltype, CONFIG_PARSE_STRING_SAFE) && !string_is_safe(rvalue, STRING_ALLOW_GLOBS)) { |
| 1248 |
_cleanup_free_ char *escaped = NULL; |
| 1249 |
|
| 1250 |
escaped = cescape(rvalue); |
| 1251 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1252 |
"Specified string contains unsafe characters, ignoring: %s", strna(escaped)); |
| 1253 |
return 0; |
| 1254 |
} |
| 1255 |
|
| 1256 |
if (FLAGS_SET(ltype, CONFIG_PARSE_STRING_ASCII) && !ascii_is_valid(rvalue)) { |
| 1257 |
_cleanup_free_ char *escaped = NULL; |
| 1258 |
|
| 1259 |
escaped = cescape(rvalue); |
| 1260 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1261 |
"Specified string contains invalid ASCII characters, ignoring: %s", strna(escaped)); |
| 1262 |
return 0; |
| 1263 |
} |
| 1264 |
|
| 1265 |
r = free_and_strdup_warn(s, rvalue); |
| 1266 |
if (r < 0) |
| 1267 |
return r; |
| 1268 |
|
| 1269 |
return 1; |
| 1270 |
} |
| 1271 |
|
| 1272 |
int config_parse_dns_name( |
| 1273 |
const char *unit, |
| 1274 |
const char *filename, |
| 1275 |
unsigned line, |
| 1276 |
const char *section, |
| 1277 |
unsigned section_line, |
| 1278 |
const char *lvalue, |
| 1279 |
int ltype, |
| 1280 |
const char *rvalue, |
| 1281 |
void *data, |
| 1282 |
void *userdata) { |
| 1283 |
|
| 1284 |
char **hostname = ASSERT_PTR(data); |
| 1285 |
int r; |
| 1286 |
|
| 1287 |
assert(filename); |
| 1288 |
assert(lvalue); |
| 1289 |
assert(rvalue); |
| 1290 |
|
| 1291 |
if (isempty(rvalue)) { |
| 1292 |
*hostname = mfree(*hostname); |
| 1293 |
return 1; |
| 1294 |
} |
| 1295 |
|
| 1296 |
r = dns_name_is_valid(rvalue); |
| 1297 |
if (r < 0) { |
| 1298 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1299 |
"Failed to check validity of DNS domain name '%s', ignoring assignment: %m", rvalue); |
| 1300 |
return 0; |
| 1301 |
} |
| 1302 |
if (r == 0) { |
| 1303 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1304 |
"Specified invalid DNS domain name, ignoring assignment: %s", rvalue); |
| 1305 |
return 0; |
| 1306 |
} |
| 1307 |
|
| 1308 |
r = free_and_strdup_warn(hostname, rvalue); |
| 1309 |
if (r < 0) |
| 1310 |
return r; |
| 1311 |
|
| 1312 |
return 1; |
| 1313 |
} |
| 1314 |
|
| 1315 |
int config_parse_hostname( |
| 1316 |
const char *unit, |
| 1317 |
const char *filename, |
| 1318 |
unsigned line, |
| 1319 |
const char *section, |
| 1320 |
unsigned section_line, |
| 1321 |
const char *lvalue, |
| 1322 |
int ltype, |
| 1323 |
const char *rvalue, |
| 1324 |
void *data, |
| 1325 |
void *userdata) { |
| 1326 |
|
| 1327 |
char **hostname = ASSERT_PTR(data); |
| 1328 |
|
| 1329 |
assert(filename); |
| 1330 |
assert(lvalue); |
| 1331 |
assert(rvalue); |
| 1332 |
|
| 1333 |
if (isempty(rvalue)) { |
| 1334 |
*hostname = mfree(*hostname); |
| 1335 |
return 1; |
| 1336 |
} |
| 1337 |
|
| 1338 |
if (!hostname_is_valid(rvalue, 0)) { |
| 1339 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1340 |
"Specified invalid hostname, ignoring assignment: %s", rvalue); |
| 1341 |
return 0; |
| 1342 |
} |
| 1343 |
|
| 1344 |
return config_parse_dns_name(unit, filename, line, section, section_line, |
| 1345 |
lvalue, ltype, rvalue, data, userdata); |
| 1346 |
} |
| 1347 |
|
| 1348 |
int config_parse_path( |
| 1349 |
const char *unit, |
| 1350 |
const char *filename, |
| 1351 |
unsigned line, |
| 1352 |
const char *section, |
| 1353 |
unsigned section_line, |
| 1354 |
const char *lvalue, |
| 1355 |
int ltype, |
| 1356 |
const char *rvalue, |
| 1357 |
void *data, |
| 1358 |
void *userdata) { |
| 1359 |
|
| 1360 |
_cleanup_free_ char *n = NULL; |
| 1361 |
bool fatal = ltype; |
| 1362 |
char **s = ASSERT_PTR(data); |
| 1363 |
int r; |
| 1364 |
|
| 1365 |
assert(filename); |
| 1366 |
assert(lvalue); |
| 1367 |
assert(rvalue); |
| 1368 |
|
| 1369 |
if (isempty(rvalue)) { |
| 1370 |
*s = mfree(*s); |
| 1371 |
return 1; |
| 1372 |
} |
| 1373 |
|
| 1374 |
n = strdup(rvalue); |
| 1375 |
if (!n) |
| 1376 |
return log_oom(); |
| 1377 |
|
| 1378 |
r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE | (fatal ? PATH_CHECK_FATAL : 0), unit, filename, line, lvalue); |
| 1379 |
if (r < 0) |
| 1380 |
return fatal ? -ENOEXEC : 0; |
| 1381 |
|
| 1382 |
free_and_replace(*s, n); |
| 1383 |
return 1; |
| 1384 |
} |
| 1385 |
|
| 1386 |
int config_parse_strv( |
| 1387 |
const char *unit, |
| 1388 |
const char *filename, |
| 1389 |
unsigned line, |
| 1390 |
const char *section, |
| 1391 |
unsigned section_line, |
| 1392 |
const char *lvalue, |
| 1393 |
int ltype, /* When true, duplicated entries will be filtered. */ |
| 1394 |
const char *rvalue, |
| 1395 |
void *data, |
| 1396 |
void *userdata) { |
| 1397 |
|
| 1398 |
char ***sv = ASSERT_PTR(data); |
| 1399 |
int r; |
| 1400 |
|
| 1401 |
assert(filename); |
| 1402 |
assert(lvalue); |
| 1403 |
assert(rvalue); |
| 1404 |
|
| 1405 |
if (isempty(rvalue)) { |
| 1406 |
*sv = strv_free(*sv); |
| 1407 |
return 1; |
| 1408 |
} |
| 1409 |
|
| 1410 |
_cleanup_strv_free_ char **strv = NULL; |
| 1411 |
for (const char *p = rvalue;;) { |
| 1412 |
char *word; |
| 1413 |
|
| 1414 |
r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RETAIN_ESCAPE); |
| 1415 |
if (r < 0) |
| 1416 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1417 |
if (r == 0) |
| 1418 |
break; |
| 1419 |
|
| 1420 |
r = strv_consume(&strv, word); |
| 1421 |
if (r < 0) |
| 1422 |
return log_oom(); |
| 1423 |
} |
| 1424 |
|
| 1425 |
r = strv_extend_strv_consume(sv, TAKE_PTR(strv), /* filter_duplicates= */ ltype); |
| 1426 |
if (r < 0) |
| 1427 |
return log_oom(); |
| 1428 |
|
| 1429 |
return 1; |
| 1430 |
} |
| 1431 |
|
| 1432 |
int config_parse_warn_compat( |
| 1433 |
const char *unit, |
| 1434 |
const char *filename, |
| 1435 |
unsigned line, |
| 1436 |
const char *section, |
| 1437 |
unsigned section_line, |
| 1438 |
const char *lvalue, |
| 1439 |
int ltype, |
| 1440 |
const char *rvalue, |
| 1441 |
void *data, |
| 1442 |
void *userdata) { |
| 1443 |
|
| 1444 |
Disabled reason = ltype; |
| 1445 |
|
| 1446 |
switch (reason) { |
| 1447 |
|
| 1448 |
case DISABLED_CONFIGURATION: |
| 1449 |
log_syntax(unit, LOG_DEBUG, filename, line, 0, |
| 1450 |
"Support for option %s= has been disabled at compile time and it is ignored", lvalue); |
| 1451 |
break; |
| 1452 |
|
| 1453 |
case DISABLED_LEGACY: |
| 1454 |
log_syntax(unit, LOG_INFO, filename, line, 0, |
| 1455 |
"Support for option %s= has been removed and it is ignored", lvalue); |
| 1456 |
break; |
| 1457 |
|
| 1458 |
case DISABLED_EXPERIMENTAL: |
| 1459 |
log_syntax(unit, LOG_INFO, filename, line, 0, |
| 1460 |
"Support for option %s= has not yet been enabled and it is ignored", lvalue); |
| 1461 |
break; |
| 1462 |
} |
| 1463 |
|
| 1464 |
return 0; |
| 1465 |
} |
| 1466 |
|
| 1467 |
int config_parse_log_facility( |
| 1468 |
const char *unit, |
| 1469 |
const char *filename, |
| 1470 |
unsigned line, |
| 1471 |
const char *section, |
| 1472 |
unsigned section_line, |
| 1473 |
const char *lvalue, |
| 1474 |
int ltype, |
| 1475 |
const char *rvalue, |
| 1476 |
void *data, |
| 1477 |
void *userdata) { |
| 1478 |
|
| 1479 |
int *o = data, x; |
| 1480 |
|
| 1481 |
assert(filename); |
| 1482 |
assert(lvalue); |
| 1483 |
assert(rvalue); |
| 1484 |
assert(data); |
| 1485 |
|
| 1486 |
x = log_facility_unshifted_from_string(rvalue); |
| 1487 |
if (x < 0) |
| 1488 |
return log_syntax_parse_error(unit, filename, line, x, lvalue, rvalue); |
| 1489 |
|
| 1490 |
*o = (x << 3) | LOG_PRI(*o); |
| 1491 |
|
| 1492 |
return 1; |
| 1493 |
} |
| 1494 |
|
| 1495 |
int config_parse_log_level( |
| 1496 |
const char *unit, |
| 1497 |
const char *filename, |
| 1498 |
unsigned line, |
| 1499 |
const char *section, |
| 1500 |
unsigned section_line, |
| 1501 |
const char *lvalue, |
| 1502 |
int ltype, |
| 1503 |
const char *rvalue, |
| 1504 |
void *data, |
| 1505 |
void *userdata) { |
| 1506 |
|
| 1507 |
int *o = data, x; |
| 1508 |
|
| 1509 |
assert(filename); |
| 1510 |
assert(lvalue); |
| 1511 |
assert(rvalue); |
| 1512 |
assert(data); |
| 1513 |
|
| 1514 |
x = log_level_from_string(rvalue); |
| 1515 |
if (x < 0) |
| 1516 |
return log_syntax_parse_error(unit, filename, line, x, lvalue, rvalue); |
| 1517 |
|
| 1518 |
if (*o < 0) /* if it wasn't initialized so far, assume zero facility */ |
| 1519 |
*o = x; |
| 1520 |
else |
| 1521 |
*o = (*o & LOG_FACMASK) | x; |
| 1522 |
|
| 1523 |
return 1; |
| 1524 |
} |
| 1525 |
|
| 1526 |
int config_parse_signal( |
| 1527 |
const char *unit, |
| 1528 |
const char *filename, |
| 1529 |
unsigned line, |
| 1530 |
const char *section, |
| 1531 |
unsigned section_line, |
| 1532 |
const char *lvalue, |
| 1533 |
int ltype, |
| 1534 |
const char *rvalue, |
| 1535 |
void *data, |
| 1536 |
void *userdata) { |
| 1537 |
|
| 1538 |
int *sig = data, r; |
| 1539 |
|
| 1540 |
assert(filename); |
| 1541 |
assert(lvalue); |
| 1542 |
assert(rvalue); |
| 1543 |
assert(sig); |
| 1544 |
|
| 1545 |
r = signal_from_string(rvalue); |
| 1546 |
if (r <= 0) |
| 1547 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1548 |
|
| 1549 |
*sig = r; |
| 1550 |
return 1; |
| 1551 |
} |
| 1552 |
|
| 1553 |
int config_parse_personality( |
| 1554 |
const char *unit, |
| 1555 |
const char *filename, |
| 1556 |
unsigned line, |
| 1557 |
const char *section, |
| 1558 |
unsigned section_line, |
| 1559 |
const char *lvalue, |
| 1560 |
int ltype, |
| 1561 |
const char *rvalue, |
| 1562 |
void *data, |
| 1563 |
void *userdata) { |
| 1564 |
|
| 1565 |
unsigned long *personality = data, p; |
| 1566 |
|
| 1567 |
assert(filename); |
| 1568 |
assert(lvalue); |
| 1569 |
assert(rvalue); |
| 1570 |
assert(personality); |
| 1571 |
|
| 1572 |
if (isempty(rvalue)) { |
| 1573 |
*personality = PERSONALITY_INVALID; |
| 1574 |
return 1; |
| 1575 |
} |
| 1576 |
|
| 1577 |
p = personality_from_string(rvalue); |
| 1578 |
if (p == PERSONALITY_INVALID) |
| 1579 |
return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue); |
| 1580 |
|
| 1581 |
*personality = p; |
| 1582 |
return 1; |
| 1583 |
} |
| 1584 |
|
| 1585 |
int config_parse_ifname( |
| 1586 |
const char *unit, |
| 1587 |
const char *filename, |
| 1588 |
unsigned line, |
| 1589 |
const char *section, |
| 1590 |
unsigned section_line, |
| 1591 |
const char *lvalue, |
| 1592 |
int ltype, |
| 1593 |
const char *rvalue, |
| 1594 |
void *data, |
| 1595 |
void *userdata) { |
| 1596 |
|
| 1597 |
char **s = ASSERT_PTR(data); |
| 1598 |
int r; |
| 1599 |
|
| 1600 |
assert(filename); |
| 1601 |
assert(lvalue); |
| 1602 |
assert(rvalue); |
| 1603 |
|
| 1604 |
if (isempty(rvalue)) { |
| 1605 |
*s = mfree(*s); |
| 1606 |
return 1; |
| 1607 |
} |
| 1608 |
|
| 1609 |
if (!ifname_valid(rvalue)) { |
| 1610 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue); |
| 1611 |
return 0; |
| 1612 |
} |
| 1613 |
|
| 1614 |
r = free_and_strdup_warn(s, rvalue); |
| 1615 |
if (r < 0) |
| 1616 |
return r; |
| 1617 |
|
| 1618 |
return 1; |
| 1619 |
} |
| 1620 |
|
| 1621 |
int config_parse_ifnames( |
| 1622 |
const char *unit, |
| 1623 |
const char *filename, |
| 1624 |
unsigned line, |
| 1625 |
const char *section, |
| 1626 |
unsigned section_line, |
| 1627 |
const char *lvalue, |
| 1628 |
int ltype, |
| 1629 |
const char *rvalue, |
| 1630 |
void *data, |
| 1631 |
void *userdata) { |
| 1632 |
|
| 1633 |
_cleanup_strv_free_ char **names = NULL; |
| 1634 |
char ***s = ASSERT_PTR(data); |
| 1635 |
int r; |
| 1636 |
|
| 1637 |
assert(filename); |
| 1638 |
assert(lvalue); |
| 1639 |
assert(rvalue); |
| 1640 |
|
| 1641 |
if (isempty(rvalue)) { |
| 1642 |
*s = strv_free(*s); |
| 1643 |
return 1; |
| 1644 |
} |
| 1645 |
|
| 1646 |
for (const char *p = rvalue;;) { |
| 1647 |
_cleanup_free_ char *word = NULL; |
| 1648 |
|
| 1649 |
r = extract_first_word(&p, &word, NULL, 0); |
| 1650 |
if (r < 0) |
| 1651 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1652 |
if (r == 0) |
| 1653 |
break; |
| 1654 |
|
| 1655 |
if (!ifname_valid_full(word, ltype)) { |
| 1656 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 1657 |
"Interface name is not valid or too long, ignoring assignment: %s", |
| 1658 |
word); |
| 1659 |
continue; |
| 1660 |
} |
| 1661 |
|
| 1662 |
r = strv_consume(&names, TAKE_PTR(word)); |
| 1663 |
if (r < 0) |
| 1664 |
return log_oom(); |
| 1665 |
} |
| 1666 |
|
| 1667 |
r = strv_extend_strv(s, names, true); |
| 1668 |
if (r < 0) |
| 1669 |
return log_oom(); |
| 1670 |
|
| 1671 |
return 1; |
| 1672 |
} |
| 1673 |
|
| 1674 |
int config_parse_ip_port( |
| 1675 |
const char *unit, |
| 1676 |
const char *filename, |
| 1677 |
unsigned line, |
| 1678 |
const char *section, |
| 1679 |
unsigned section_line, |
| 1680 |
const char *lvalue, |
| 1681 |
int ltype, |
| 1682 |
const char *rvalue, |
| 1683 |
void *data, |
| 1684 |
void *userdata) { |
| 1685 |
|
| 1686 |
uint16_t *s = ASSERT_PTR(data); |
| 1687 |
uint16_t port; |
| 1688 |
int r; |
| 1689 |
|
| 1690 |
assert(filename); |
| 1691 |
assert(lvalue); |
| 1692 |
assert(rvalue); |
| 1693 |
|
| 1694 |
if (isempty(rvalue)) { |
| 1695 |
*s = 0; |
| 1696 |
return 1; |
| 1697 |
} |
| 1698 |
|
| 1699 |
r = parse_ip_port(rvalue, &port); |
| 1700 |
if (r < 0) |
| 1701 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1702 |
|
| 1703 |
*s = port; |
| 1704 |
return 1; |
| 1705 |
} |
| 1706 |
|
| 1707 |
int config_parse_mtu( |
| 1708 |
const char *unit, |
| 1709 |
const char *filename, |
| 1710 |
unsigned line, |
| 1711 |
const char *section, |
| 1712 |
unsigned section_line, |
| 1713 |
const char *lvalue, |
| 1714 |
int ltype, |
| 1715 |
const char *rvalue, |
| 1716 |
void *data, |
| 1717 |
void *userdata) { |
| 1718 |
|
| 1719 |
uint32_t *mtu = ASSERT_PTR(data); |
| 1720 |
int r; |
| 1721 |
|
| 1722 |
assert(rvalue); |
| 1723 |
|
| 1724 |
r = parse_mtu(ltype, rvalue, mtu); |
| 1725 |
if (r == -ERANGE) { |
| 1726 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1727 |
"Maximum transfer unit (MTU) value out of range. Permitted range is %" PRIu32 "…%" PRIu32 ", ignoring: %s", |
| 1728 |
(uint32_t) (ltype == AF_INET6 ? IPV6_MIN_MTU : IPV4_MIN_MTU), (uint32_t) UINT32_MAX, |
| 1729 |
rvalue); |
| 1730 |
return 0; |
| 1731 |
} |
| 1732 |
if (r < 0) |
| 1733 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1734 |
|
| 1735 |
return 1; |
| 1736 |
} |
| 1737 |
|
| 1738 |
int config_parse_rlimit( |
| 1739 |
const char *unit, |
| 1740 |
const char *filename, |
| 1741 |
unsigned line, |
| 1742 |
const char *section, |
| 1743 |
unsigned section_line, |
| 1744 |
const char *lvalue, |
| 1745 |
int ltype, |
| 1746 |
const char *rvalue, |
| 1747 |
void *data, |
| 1748 |
void *userdata) { |
| 1749 |
|
| 1750 |
struct rlimit **rl = data, d = {}; |
| 1751 |
int r; |
| 1752 |
|
| 1753 |
assert(rvalue); |
| 1754 |
assert(rl); |
| 1755 |
|
| 1756 |
r = rlimit_parse(ltype, rvalue, &d); |
| 1757 |
if (r == -EILSEQ) { |
| 1758 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Soft resource limit chosen higher than hard limit, ignoring: %s", rvalue); |
| 1759 |
return 0; |
| 1760 |
} |
| 1761 |
if (r < 0) |
| 1762 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1763 |
|
| 1764 |
if (rl[ltype]) |
| 1765 |
*rl[ltype] = d; |
| 1766 |
else { |
| 1767 |
rl[ltype] = newdup(struct rlimit, &d, 1); |
| 1768 |
if (!rl[ltype]) |
| 1769 |
return log_oom(); |
| 1770 |
} |
| 1771 |
|
| 1772 |
return 1; |
| 1773 |
} |
| 1774 |
|
| 1775 |
int config_parse_permille( |
| 1776 |
const char *unit, |
| 1777 |
const char *filename, |
| 1778 |
unsigned line, |
| 1779 |
const char *section, |
| 1780 |
unsigned section_line, |
| 1781 |
const char *lvalue, |
| 1782 |
int ltype, |
| 1783 |
const char *rvalue, |
| 1784 |
void *data, |
| 1785 |
void *userdata) { |
| 1786 |
|
| 1787 |
unsigned *permille = ASSERT_PTR(data); |
| 1788 |
int r; |
| 1789 |
|
| 1790 |
assert(filename); |
| 1791 |
assert(lvalue); |
| 1792 |
assert(rvalue); |
| 1793 |
|
| 1794 |
r = parse_permille(rvalue); |
| 1795 |
if (r < 0) |
| 1796 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1797 |
|
| 1798 |
*permille = (unsigned) r; |
| 1799 |
return 1; |
| 1800 |
} |
| 1801 |
|
| 1802 |
int config_parse_vlanprotocol( |
| 1803 |
const char *unit, |
| 1804 |
const char *filename, |
| 1805 |
unsigned line, |
| 1806 |
const char *section, |
| 1807 |
unsigned section_line, |
| 1808 |
const char *lvalue, |
| 1809 |
int ltype, |
| 1810 |
const char *rvalue, |
| 1811 |
void *data, |
| 1812 |
void *userdata) { |
| 1813 |
|
| 1814 |
int *vlan_protocol = data; |
| 1815 |
|
| 1816 |
assert(filename); |
| 1817 |
assert(lvalue); |
| 1818 |
|
| 1819 |
if (isempty(rvalue)) { |
| 1820 |
*vlan_protocol = -1; |
| 1821 |
return 1; |
| 1822 |
} |
| 1823 |
|
| 1824 |
if (STR_IN_SET(rvalue, "802.1ad", "802.1AD")) |
| 1825 |
*vlan_protocol = ETH_P_8021AD; |
| 1826 |
else if (STR_IN_SET(rvalue, "802.1q", "802.1Q")) |
| 1827 |
*vlan_protocol = ETH_P_8021Q; |
| 1828 |
else |
| 1829 |
return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue); |
| 1830 |
|
| 1831 |
return 1; |
| 1832 |
} |
| 1833 |
|
| 1834 |
int config_parse_hw_addr( |
| 1835 |
const char *unit, |
| 1836 |
const char *filename, |
| 1837 |
unsigned line, |
| 1838 |
const char *section, |
| 1839 |
unsigned section_line, |
| 1840 |
const char *lvalue, |
| 1841 |
int ltype, |
| 1842 |
const char *rvalue, |
| 1843 |
void *data, |
| 1844 |
void *userdata) { |
| 1845 |
|
| 1846 |
struct hw_addr_data *hwaddr = ASSERT_PTR(data); |
| 1847 |
int r; |
| 1848 |
|
| 1849 |
assert(filename); |
| 1850 |
assert(lvalue); |
| 1851 |
assert(rvalue); |
| 1852 |
|
| 1853 |
if (isempty(rvalue)) { |
| 1854 |
*hwaddr = HW_ADDR_NULL; |
| 1855 |
return 1; |
| 1856 |
} |
| 1857 |
|
| 1858 |
r = parse_hw_addr_full(rvalue, ltype, hwaddr); |
| 1859 |
if (r < 0) |
| 1860 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1861 |
|
| 1862 |
return 1; |
| 1863 |
} |
| 1864 |
|
| 1865 |
int config_parse_hw_addrs( |
| 1866 |
const char *unit, |
| 1867 |
const char *filename, |
| 1868 |
unsigned line, |
| 1869 |
const char *section, |
| 1870 |
unsigned section_line, |
| 1871 |
const char *lvalue, |
| 1872 |
int ltype, |
| 1873 |
const char *rvalue, |
| 1874 |
void *data, |
| 1875 |
void *userdata) { |
| 1876 |
|
| 1877 |
Set **hwaddrs = ASSERT_PTR(data); |
| 1878 |
int r; |
| 1879 |
|
| 1880 |
assert(filename); |
| 1881 |
assert(lvalue); |
| 1882 |
assert(rvalue); |
| 1883 |
|
| 1884 |
if (isempty(rvalue)) { |
| 1885 |
/* Empty assignment resets the list */ |
| 1886 |
*hwaddrs = set_free(*hwaddrs); |
| 1887 |
return 1; |
| 1888 |
} |
| 1889 |
|
| 1890 |
for (const char *p = rvalue;;) { |
| 1891 |
_cleanup_free_ char *word = NULL; |
| 1892 |
_cleanup_free_ struct hw_addr_data *n = NULL; |
| 1893 |
|
| 1894 |
r = extract_first_word(&p, &word, NULL, 0); |
| 1895 |
if (r < 0) |
| 1896 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1897 |
if (r == 0) |
| 1898 |
return 1; |
| 1899 |
|
| 1900 |
n = new(struct hw_addr_data, 1); |
| 1901 |
if (!n) |
| 1902 |
return log_oom(); |
| 1903 |
|
| 1904 |
r = parse_hw_addr_full(word, ltype, n); |
| 1905 |
if (r < 0) { |
| 1906 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1907 |
"Not a valid hardware address, ignoring: %s", word); |
| 1908 |
continue; |
| 1909 |
} |
| 1910 |
|
| 1911 |
r = set_ensure_consume(hwaddrs, &hw_addr_hash_ops_free, TAKE_PTR(n)); |
| 1912 |
if (r < 0) |
| 1913 |
return log_oom(); |
| 1914 |
} |
| 1915 |
} |
| 1916 |
|
| 1917 |
int config_parse_ether_addr( |
| 1918 |
const char *unit, |
| 1919 |
const char *filename, |
| 1920 |
unsigned line, |
| 1921 |
const char *section, |
| 1922 |
unsigned section_line, |
| 1923 |
const char *lvalue, |
| 1924 |
int ltype, |
| 1925 |
const char *rvalue, |
| 1926 |
void *data, |
| 1927 |
void *userdata) { |
| 1928 |
|
| 1929 |
_cleanup_free_ struct ether_addr *n = NULL; |
| 1930 |
struct ether_addr **hwaddr = ASSERT_PTR(data); |
| 1931 |
int r; |
| 1932 |
|
| 1933 |
assert(filename); |
| 1934 |
assert(lvalue); |
| 1935 |
assert(rvalue); |
| 1936 |
|
| 1937 |
if (isempty(rvalue)) { |
| 1938 |
*hwaddr = mfree(*hwaddr); |
| 1939 |
return 1; |
| 1940 |
} |
| 1941 |
|
| 1942 |
n = new0(struct ether_addr, 1); |
| 1943 |
if (!n) |
| 1944 |
return log_oom(); |
| 1945 |
|
| 1946 |
r = parse_ether_addr(rvalue, n); |
| 1947 |
if (r < 0) |
| 1948 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1949 |
|
| 1950 |
free_and_replace(*hwaddr, n); |
| 1951 |
return 1; |
| 1952 |
} |
| 1953 |
|
| 1954 |
int config_parse_ether_addrs( |
| 1955 |
const char *unit, |
| 1956 |
const char *filename, |
| 1957 |
unsigned line, |
| 1958 |
const char *section, |
| 1959 |
unsigned section_line, |
| 1960 |
const char *lvalue, |
| 1961 |
int ltype, |
| 1962 |
const char *rvalue, |
| 1963 |
void *data, |
| 1964 |
void *userdata) { |
| 1965 |
|
| 1966 |
Set **hwaddrs = ASSERT_PTR(data); |
| 1967 |
int r; |
| 1968 |
|
| 1969 |
assert(filename); |
| 1970 |
assert(lvalue); |
| 1971 |
assert(rvalue); |
| 1972 |
|
| 1973 |
if (isempty(rvalue)) { |
| 1974 |
/* Empty assignment resets the list */ |
| 1975 |
*hwaddrs = set_free(*hwaddrs); |
| 1976 |
return 1; |
| 1977 |
} |
| 1978 |
|
| 1979 |
for (const char *p = rvalue;;) { |
| 1980 |
_cleanup_free_ char *word = NULL; |
| 1981 |
_cleanup_free_ struct ether_addr *n = NULL; |
| 1982 |
|
| 1983 |
r = extract_first_word(&p, &word, NULL, 0); |
| 1984 |
if (r < 0) |
| 1985 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 1986 |
if (r == 0) |
| 1987 |
return 1; |
| 1988 |
|
| 1989 |
n = new(struct ether_addr, 1); |
| 1990 |
if (!n) |
| 1991 |
return log_oom(); |
| 1992 |
|
| 1993 |
r = parse_ether_addr(word, n); |
| 1994 |
if (r < 0) { |
| 1995 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 1996 |
"Not a valid MAC address, ignoring: %s", word); |
| 1997 |
continue; |
| 1998 |
} |
| 1999 |
|
| 2000 |
r = set_ensure_consume(hwaddrs, ðer_addr_hash_ops_free, TAKE_PTR(n)); |
| 2001 |
if (r < 0) |
| 2002 |
return log_oom(); |
| 2003 |
} |
| 2004 |
} |
| 2005 |
|
| 2006 |
int config_parse_in_addr_non_null( |
| 2007 |
const char *unit, |
| 2008 |
const char *filename, |
| 2009 |
unsigned line, |
| 2010 |
const char *section, |
| 2011 |
unsigned section_line, |
| 2012 |
const char *lvalue, |
| 2013 |
int ltype, |
| 2014 |
const char *rvalue, |
| 2015 |
void *data, |
| 2016 |
void *userdata) { |
| 2017 |
|
| 2018 |
/* data must be a pointer to struct in_addr or in6_addr, and the type is determined by ltype. */ |
| 2019 |
struct in_addr *ipv4 = ASSERT_PTR(data); |
| 2020 |
struct in6_addr *ipv6 = ASSERT_PTR(data); |
| 2021 |
union in_addr_union a; |
| 2022 |
int r; |
| 2023 |
|
| 2024 |
assert(filename); |
| 2025 |
assert(lvalue); |
| 2026 |
assert(rvalue); |
| 2027 |
assert(IN_SET(ltype, AF_INET, AF_INET6)); |
| 2028 |
|
| 2029 |
if (isempty(rvalue)) { |
| 2030 |
if (ltype == AF_INET) |
| 2031 |
*ipv4 = (struct in_addr) {}; |
| 2032 |
else |
| 2033 |
*ipv6 = (struct in6_addr) {}; |
| 2034 |
return 1; |
| 2035 |
} |
| 2036 |
|
| 2037 |
r = in_addr_from_string(ltype, rvalue, &a); |
| 2038 |
if (r < 0) |
| 2039 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 2040 |
|
| 2041 |
if (!in_addr_is_set(ltype, &a)) { |
| 2042 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 2043 |
"%s= cannot be the ANY address, ignoring: %s", lvalue, rvalue); |
| 2044 |
return 0; |
| 2045 |
} |
| 2046 |
|
| 2047 |
if (ltype == AF_INET) |
| 2048 |
*ipv4 = a.in; |
| 2049 |
else |
| 2050 |
*ipv6 = a.in6; |
| 2051 |
return 1; |
| 2052 |
} |
| 2053 |
|
| 2054 |
int config_parse_in_addr_data( |
| 2055 |
const char *unit, |
| 2056 |
const char *filename, |
| 2057 |
unsigned line, |
| 2058 |
const char *section, |
| 2059 |
unsigned section_line, |
| 2060 |
const char *lvalue, |
| 2061 |
int ltype, |
| 2062 |
const char *rvalue, |
| 2063 |
void *data, |
| 2064 |
void *userdata) { |
| 2065 |
|
| 2066 |
struct in_addr_data *p = ASSERT_PTR(data); |
| 2067 |
int r; |
| 2068 |
|
| 2069 |
assert(filename); |
| 2070 |
assert(lvalue); |
| 2071 |
|
| 2072 |
if (isempty(rvalue)) { |
| 2073 |
*p = (struct in_addr_data) {}; |
| 2074 |
return 1; |
| 2075 |
} |
| 2076 |
|
| 2077 |
r = in_addr_from_string_auto(rvalue, &p->family, &p->address); |
| 2078 |
if (r < 0) |
| 2079 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 2080 |
|
| 2081 |
return 1; |
| 2082 |
} |
| 2083 |
|
| 2084 |
int config_parse_in_addr_prefix( |
| 2085 |
const char *unit, |
| 2086 |
const char *filename, |
| 2087 |
unsigned line, |
| 2088 |
const char *section, |
| 2089 |
unsigned section_line, |
| 2090 |
const char *lvalue, |
| 2091 |
int ltype, /* takes boolean, whether we warn about missing prefixlen */ |
| 2092 |
const char *rvalue, |
| 2093 |
void *data, |
| 2094 |
void *userdata) { |
| 2095 |
|
| 2096 |
struct in_addr_prefix *p = ASSERT_PTR(data); |
| 2097 |
int r; |
| 2098 |
|
| 2099 |
assert(filename); |
| 2100 |
assert(lvalue); |
| 2101 |
|
| 2102 |
if (isempty(rvalue)) { |
| 2103 |
*p = (struct in_addr_prefix) {}; |
| 2104 |
return 1; |
| 2105 |
} |
| 2106 |
|
| 2107 |
r = in_addr_prefix_from_string_auto_full(rvalue, ltype ? PREFIXLEN_REFUSE : PREFIXLEN_FULL, &p->family, &p->address, &p->prefixlen); |
| 2108 |
if (r == -ENOANO) { |
| 2109 |
r = in_addr_prefix_from_string_auto(rvalue, &p->family, &p->address, &p->prefixlen); |
| 2110 |
if (r >= 0) |
| 2111 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2112 |
"%s=%s is specified without prefix length. Assuming the prefix length is %u. " |
| 2113 |
"Please specify the prefix length explicitly.", lvalue, rvalue, p->prefixlen); |
| 2114 |
} |
| 2115 |
if (r < 0) |
| 2116 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 2117 |
|
| 2118 |
return 1; |
| 2119 |
} |
| 2120 |
|
| 2121 |
int config_parse_unsigned_bounded( |
| 2122 |
const char *unit, |
| 2123 |
const char *filename, |
| 2124 |
unsigned line, |
| 2125 |
const char *section, |
| 2126 |
unsigned section_line, |
| 2127 |
const char *lvalue, |
| 2128 |
const char *rvalue, |
| 2129 |
unsigned min, |
| 2130 |
unsigned max, |
| 2131 |
bool ignoring, |
| 2132 |
unsigned *ret) { |
| 2133 |
|
| 2134 |
int r; |
| 2135 |
|
| 2136 |
assert(filename); |
| 2137 |
assert(lvalue); |
| 2138 |
assert(rvalue); |
| 2139 |
assert(ret); |
| 2140 |
|
| 2141 |
r = safe_atou_bounded(rvalue, min, max, ret); |
| 2142 |
if (r == -ERANGE) { |
| 2143 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2144 |
"Invalid '%s=%s', allowed range is %u..%u%s.", |
| 2145 |
lvalue, rvalue, min, max, ignoring ? ", ignoring" : ""); |
| 2146 |
return ignoring ? 0 : r; |
| 2147 |
} |
| 2148 |
if (r < 0) |
| 2149 |
return log_syntax_parse_error_full(unit, filename, line, r, /* critical= */ !ignoring, lvalue, rvalue); |
| 2150 |
|
| 2151 |
return 1; /* Return 1 if something was set */ |
| 2152 |
} |
| 2153 |
|
| 2154 |
int config_parse_calendar( |
| 2155 |
const char *unit, |
| 2156 |
const char *filename, |
| 2157 |
unsigned line, |
| 2158 |
const char *section, |
| 2159 |
unsigned section_line, |
| 2160 |
const char *lvalue, |
| 2161 |
int ltype, |
| 2162 |
const char *rvalue, |
| 2163 |
void *data, |
| 2164 |
void *userdata) { |
| 2165 |
|
| 2166 |
CalendarSpec **cr = data; |
| 2167 |
_cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; |
| 2168 |
int r; |
| 2169 |
|
| 2170 |
assert(filename); |
| 2171 |
assert(lvalue); |
| 2172 |
assert(rvalue); |
| 2173 |
assert(data); |
| 2174 |
|
| 2175 |
if (isempty(rvalue)) { |
| 2176 |
*cr = calendar_spec_free(*cr); |
| 2177 |
return 1; |
| 2178 |
} |
| 2179 |
|
| 2180 |
r = calendar_spec_from_string(rvalue, &c); |
| 2181 |
if (r < 0) |
| 2182 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 2183 |
|
| 2184 |
free_and_replace_full(*cr, c, calendar_spec_free); |
| 2185 |
return 1; |
| 2186 |
} |
| 2187 |
|
| 2188 |
DEFINE_CONFIG_PARSE(config_parse_percent, parse_percent); |
| 2189 |
DEFINE_CONFIG_PARSE(config_parse_permyriad, parse_permyriad); |
| 2190 |
DEFINE_CONFIG_PARSE_PTR(config_parse_sec_fix_0, parse_sec_fix_0, usec_t); |
| 2191 |
|
| 2192 |
int config_parse_timezone( |
| 2193 |
const char *unit, |
| 2194 |
const char *filename, |
| 2195 |
unsigned line, |
| 2196 |
const char *section, |
| 2197 |
unsigned section_line, |
| 2198 |
const char *lvalue, |
| 2199 |
int ltype, |
| 2200 |
const char *rvalue, |
| 2201 |
void *data, |
| 2202 |
void *userdata) { |
| 2203 |
|
| 2204 |
char **tz = ASSERT_PTR(data); |
| 2205 |
int r; |
| 2206 |
|
| 2207 |
assert(filename); |
| 2208 |
assert(lvalue); |
| 2209 |
assert(rvalue); |
| 2210 |
|
| 2211 |
if (isempty(rvalue)) { |
| 2212 |
*tz = mfree(*tz); |
| 2213 |
return 1; |
| 2214 |
} |
| 2215 |
|
| 2216 |
r = verify_timezone(rvalue, LOG_WARNING); |
| 2217 |
if (r < 0) |
| 2218 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 2219 |
|
| 2220 |
r = free_and_strdup_warn(tz, rvalue); |
| 2221 |
if (r < 0) |
| 2222 |
return r; |
| 2223 |
|
| 2224 |
return 1; |
| 2225 |
} |
| 2226 |
|
| 2227 |
int config_parse_ip_protocol( |
| 2228 |
const char *unit, |
| 2229 |
const char *filename, |
| 2230 |
unsigned line, |
| 2231 |
const char *section, |
| 2232 |
unsigned section_line, |
| 2233 |
const char *lvalue, |
| 2234 |
int ltype, |
| 2235 |
const char *rvalue, |
| 2236 |
void *data, |
| 2237 |
void *userdata) { |
| 2238 |
|
| 2239 |
uint8_t *proto = ASSERT_PTR(data); |
| 2240 |
int r; |
| 2241 |
|
| 2242 |
r = isempty(rvalue) ? 0 : parse_ip_protocol_full(rvalue, /* relaxed= */ ltype); |
| 2243 |
if (r < 0) |
| 2244 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 2245 |
|
| 2246 |
if (r > UINT8_MAX) { |
| 2247 |
/* linux/fib_rules.h and linux/fou.h define the netlink field as one byte, so we need to |
| 2248 |
* reject protocols numbers that don't fit in one byte. */ |
| 2249 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 2250 |
"Invalid '%s=%s', allowed range is 0..255, ignoring.", |
| 2251 |
lvalue, rvalue); |
| 2252 |
return 0; |
| 2253 |
} |
| 2254 |
|
| 2255 |
*proto = r; |
| 2256 |
return 1; /* done. */ |
| 2257 |
} |
| 2258 |
|
| 2259 |
int config_parse_loadavg( |
| 2260 |
const char *unit, |
| 2261 |
const char *filename, |
| 2262 |
unsigned line, |
| 2263 |
const char *section, |
| 2264 |
unsigned section_line, |
| 2265 |
const char *lvalue, |
| 2266 |
int ltype, |
| 2267 |
const char *rvalue, |
| 2268 |
void *data, |
| 2269 |
void *userdata) { |
| 2270 |
|
| 2271 |
loadavg_t *i = ASSERT_PTR(data); |
| 2272 |
int r; |
| 2273 |
|
| 2274 |
assert(rvalue); |
| 2275 |
|
| 2276 |
r = parse_permyriad(rvalue); |
| 2277 |
if (r < 0) |
| 2278 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 2279 |
|
| 2280 |
r = store_loadavg_fixed_point(r / 100, r % 100, i); |
| 2281 |
if (r < 0) |
| 2282 |
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); |
| 2283 |
|
| 2284 |
return 1; /* done. */ |
| 2285 |
} |