| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "conf-files.h" |
| 7 |
#include "conf-parser.h" |
| 8 |
#include "constants.h" |
| 9 |
#include "dns-domain.h" |
| 10 |
#include "dns-rr.h" |
| 11 |
#include "extract-word.h" |
| 12 |
#include "hashmap.h" |
| 13 |
#include "hexdecoct.h" |
| 14 |
#include "path-util.h" |
| 15 |
#include "resolved-conf.h" |
| 16 |
#include "resolved-dns-zone.h" |
| 17 |
#include "resolved-dnssd.h" |
| 18 |
#include "resolved-manager.h" |
| 19 |
#include "specifier.h" |
| 20 |
#include "string-util.h" |
| 21 |
#include "strv.h" |
| 22 |
#include "utf8.h" |
| 23 |
|
| 24 |
#define DNSSD_SERVICE_DIRS ((const char* const*) CONF_PATHS_STRV("systemd/dnssd")) |
| 25 |
|
| 26 |
DnssdTxtData *dnssd_txtdata_free(DnssdTxtData *txt_data) { |
| 27 |
if (!txt_data) |
| 28 |
return NULL; |
| 29 |
|
| 30 |
dns_resource_record_unref(txt_data->rr); |
| 31 |
dns_txt_item_free_all(txt_data->txts); |
| 32 |
|
| 33 |
return mfree(txt_data); |
| 34 |
} |
| 35 |
|
| 36 |
DnssdTxtData *dnssd_txtdata_free_all(DnssdTxtData *txt_data) { |
| 37 |
DnssdTxtData *next; |
| 38 |
|
| 39 |
if (!txt_data) |
| 40 |
return NULL; |
| 41 |
|
| 42 |
next = txt_data->items_next; |
| 43 |
|
| 44 |
dnssd_txtdata_free(txt_data); |
| 45 |
|
| 46 |
return dnssd_txtdata_free_all(next); |
| 47 |
} |
| 48 |
|
| 49 |
DnssdRegisteredService *dnssd_registered_service_free(DnssdRegisteredService *service) { |
| 50 |
if (!service) |
| 51 |
return NULL; |
| 52 |
|
| 53 |
if (service->manager) |
| 54 |
hashmap_remove(service->manager->dnssd_registered_services, service->id); |
| 55 |
|
| 56 |
dns_resource_record_unref(service->ptr_rr); |
| 57 |
dns_resource_record_unref(service->sub_ptr_rr); |
| 58 |
dns_resource_record_unref(service->srv_rr); |
| 59 |
|
| 60 |
dnssd_txtdata_free_all(service->txt_data_items); |
| 61 |
|
| 62 |
free(service->path); |
| 63 |
free(service->id); |
| 64 |
free(service->type); |
| 65 |
free(service->subtype); |
| 66 |
free(service->name_template); |
| 67 |
|
| 68 |
return mfree(service); |
| 69 |
} |
| 70 |
|
| 71 |
void dnssd_registered_service_clear_on_reload(Hashmap *services) { |
| 72 |
DnssdRegisteredService *service; |
| 73 |
|
| 74 |
HASHMAP_FOREACH(service, services) |
| 75 |
if (service->config_source == RESOLVE_CONFIG_SOURCE_FILE) { |
| 76 |
hashmap_remove(services, service->id); |
| 77 |
dnssd_registered_service_free(service); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
static int dnssd_id_from_path(const char *path, char **ret_id) { |
| 82 |
int r; |
| 83 |
|
| 84 |
assert(path); |
| 85 |
assert(ret_id); |
| 86 |
|
| 87 |
_cleanup_free_ char *fn = NULL; |
| 88 |
r = path_extract_filename(path, &fn); |
| 89 |
if (r < 0) |
| 90 |
return r; |
| 91 |
|
| 92 |
char *d = endswith(fn, ".dnssd"); |
| 93 |
if (!d) |
| 94 |
return -EINVAL; |
| 95 |
|
| 96 |
*d = '\0'; |
| 97 |
|
| 98 |
*ret_id = TAKE_PTR(fn); |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
static int dnssd_registered_service_load(Manager *manager, const char *path) { |
| 103 |
_cleanup_(dnssd_registered_service_freep) DnssdRegisteredService *service = NULL; |
| 104 |
_cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL; |
| 105 |
_cleanup_free_ char *dropin_dirname = NULL; |
| 106 |
int r; |
| 107 |
|
| 108 |
assert(manager); |
| 109 |
assert(path); |
| 110 |
|
| 111 |
service = new0(DnssdRegisteredService, 1); |
| 112 |
if (!service) |
| 113 |
return log_oom(); |
| 114 |
|
| 115 |
service->path = strdup(path); |
| 116 |
if (!service->path) |
| 117 |
return log_oom(); |
| 118 |
|
| 119 |
r = dnssd_id_from_path(path, &service->id); |
| 120 |
if (r < 0) |
| 121 |
return log_error_errno(r, "Failed to extract DNS-SD service id from filename: %m"); |
| 122 |
|
| 123 |
dropin_dirname = strjoin(service->id, ".dnssd.d"); |
| 124 |
if (!dropin_dirname) |
| 125 |
return log_oom(); |
| 126 |
|
| 127 |
r = config_parse_many( |
| 128 |
STRV_MAKE_CONST(path), |
| 129 |
DNSSD_SERVICE_DIRS, |
| 130 |
dropin_dirname, |
| 131 |
"Service\0", |
| 132 |
config_item_perf_lookup, resolved_dnssd_gperf_lookup, |
| 133 |
CONFIG_PARSE_WARN, |
| 134 |
service); |
| 135 |
if (r < 0) |
| 136 |
return r; |
| 137 |
|
| 138 |
if (!service->name_template) |
| 139 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 140 |
"%s doesn't define service instance name", |
| 141 |
service->id); |
| 142 |
|
| 143 |
if (!service->type) |
| 144 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 145 |
"%s doesn't define service type", |
| 146 |
service->id); |
| 147 |
|
| 148 |
if (!service->txt_data_items) { |
| 149 |
txt_data = new0(DnssdTxtData, 1); |
| 150 |
if (!txt_data) |
| 151 |
return log_oom(); |
| 152 |
|
| 153 |
r = dns_txt_item_new_empty(&txt_data->txts); |
| 154 |
if (r < 0) |
| 155 |
return r; |
| 156 |
|
| 157 |
LIST_PREPEND(items, service->txt_data_items, txt_data); |
| 158 |
TAKE_PTR(txt_data); |
| 159 |
} |
| 160 |
|
| 161 |
r = hashmap_ensure_put(&manager->dnssd_registered_services, &string_hash_ops, service->id, service); |
| 162 |
if (r < 0) |
| 163 |
return r; |
| 164 |
|
| 165 |
service->manager = manager; |
| 166 |
|
| 167 |
r = dnssd_update_rrs(service); |
| 168 |
if (r < 0) |
| 169 |
return r; |
| 170 |
|
| 171 |
TAKE_PTR(service); |
| 172 |
|
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
static int specifier_dnssd_hostname(char specifier, const void *data, const char *root, const void *userdata, char **ret) { |
| 177 |
const Manager *m = ASSERT_PTR(userdata); |
| 178 |
|
| 179 |
assert(m->llmnr_hostname); |
| 180 |
|
| 181 |
return strdup_to(ret, m->llmnr_hostname); |
| 182 |
} |
| 183 |
|
| 184 |
int dnssd_render_instance_name(Manager *m, DnssdRegisteredService *s, char **ret) { |
| 185 |
static const Specifier specifier_table[] = { |
| 186 |
{ 'a', specifier_architecture, NULL }, |
| 187 |
{ 'b', specifier_boot_id, NULL }, |
| 188 |
{ 'B', specifier_os_build_id, NULL }, |
| 189 |
{ 'H', specifier_dnssd_hostname, NULL }, |
| 190 |
{ 'm', specifier_machine_id, NULL }, |
| 191 |
{ 'o', specifier_os_id, NULL }, |
| 192 |
{ 'v', specifier_kernel_release, NULL }, |
| 193 |
{ 'w', specifier_os_version_id, NULL }, |
| 194 |
{ 'W', specifier_os_variant_id, NULL }, |
| 195 |
{} |
| 196 |
}; |
| 197 |
_cleanup_free_ char *name = NULL; |
| 198 |
int r; |
| 199 |
|
| 200 |
assert(m); |
| 201 |
assert(s); |
| 202 |
assert(s->name_template); |
| 203 |
|
| 204 |
r = specifier_printf(s->name_template, DNS_LABEL_MAX, specifier_table, NULL, m, &name); |
| 205 |
if (r < 0) |
| 206 |
return log_debug_errno(r, "Failed to replace specifiers: %m"); |
| 207 |
|
| 208 |
if (!dns_service_name_is_valid(name)) |
| 209 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 210 |
"Service instance name '%s' is invalid.", |
| 211 |
name); |
| 212 |
|
| 213 |
if (ret) |
| 214 |
*ret = TAKE_PTR(name); |
| 215 |
|
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
int dnssd_load(Manager *manager) { |
| 220 |
_cleanup_strv_free_ char **files = NULL; |
| 221 |
int r; |
| 222 |
|
| 223 |
assert(manager); |
| 224 |
|
| 225 |
if (manager->mdns_support != RESOLVE_SUPPORT_YES) |
| 226 |
return 0; |
| 227 |
|
| 228 |
r = conf_files_list_strv(&files, ".dnssd", /* root= */ NULL, CONF_FILES_WARN, DNSSD_SERVICE_DIRS); |
| 229 |
if (r < 0) |
| 230 |
return log_error_errno(r, "Failed to enumerate .dnssd files: %m"); |
| 231 |
|
| 232 |
STRV_FOREACH_BACKWARDS(f, files) { |
| 233 |
r = dnssd_registered_service_load(manager, *f); |
| 234 |
if (r < 0) |
| 235 |
log_warning_errno(r, "Failed to load '%s': %m", *f); |
| 236 |
} |
| 237 |
|
| 238 |
return 0; |
| 239 |
} |
| 240 |
|
| 241 |
int dnssd_update_rrs(DnssdRegisteredService *s) { |
| 242 |
_cleanup_free_ char *n = NULL, *service_name = NULL, *full_name = NULL, *sub_name = NULL, *selective_name = NULL; |
| 243 |
int r; |
| 244 |
|
| 245 |
assert(s); |
| 246 |
assert(s->txt_data_items); |
| 247 |
assert(s->manager); |
| 248 |
|
| 249 |
s->ptr_rr = dns_resource_record_unref(s->ptr_rr); |
| 250 |
s->sub_ptr_rr = dns_resource_record_unref(s->sub_ptr_rr); |
| 251 |
s->srv_rr = dns_resource_record_unref(s->srv_rr); |
| 252 |
LIST_FOREACH(items, txt_data, s->txt_data_items) |
| 253 |
txt_data->rr = dns_resource_record_unref(txt_data->rr); |
| 254 |
|
| 255 |
r = dnssd_render_instance_name(s->manager, s, &n); |
| 256 |
if (r < 0) |
| 257 |
return r; |
| 258 |
|
| 259 |
r = dns_name_concat(s->type, "local", 0, &service_name); |
| 260 |
if (r < 0) |
| 261 |
return r; |
| 262 |
r = dns_name_concat(n, service_name, 0, &full_name); |
| 263 |
if (r < 0) |
| 264 |
return r; |
| 265 |
if (s->subtype) { |
| 266 |
r = dns_name_concat("_sub", service_name, 0, &sub_name); |
| 267 |
if (r < 0) |
| 268 |
return r; |
| 269 |
r = dns_name_concat(s->subtype, sub_name, 0, &selective_name); |
| 270 |
if (r < 0) |
| 271 |
return r; |
| 272 |
} |
| 273 |
|
| 274 |
LIST_FOREACH(items, txt_data, s->txt_data_items) { |
| 275 |
txt_data->rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_TXT, |
| 276 |
full_name); |
| 277 |
if (!txt_data->rr) |
| 278 |
goto oom; |
| 279 |
|
| 280 |
txt_data->rr->ttl = MDNS_DEFAULT_TTL; |
| 281 |
txt_data->rr->txt.items = dns_txt_item_copy(txt_data->txts); |
| 282 |
if (!txt_data->rr->txt.items) |
| 283 |
goto oom; |
| 284 |
} |
| 285 |
|
| 286 |
s->ptr_rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, |
| 287 |
service_name); |
| 288 |
if (!s->ptr_rr) |
| 289 |
goto oom; |
| 290 |
|
| 291 |
s->ptr_rr->ttl = MDNS_DEFAULT_TTL; |
| 292 |
s->ptr_rr->ptr.name = strdup(full_name); |
| 293 |
if (!s->ptr_rr->ptr.name) |
| 294 |
goto oom; |
| 295 |
|
| 296 |
if (selective_name) { |
| 297 |
s->sub_ptr_rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, selective_name); |
| 298 |
if (!s->sub_ptr_rr) |
| 299 |
goto oom; |
| 300 |
|
| 301 |
s->sub_ptr_rr->ttl = MDNS_DEFAULT_TTL; |
| 302 |
s->sub_ptr_rr->ptr.name = strdup(full_name); |
| 303 |
if (!s->sub_ptr_rr->ptr.name) |
| 304 |
goto oom; |
| 305 |
} |
| 306 |
|
| 307 |
s->srv_rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SRV, |
| 308 |
full_name); |
| 309 |
if (!s->srv_rr) |
| 310 |
goto oom; |
| 311 |
|
| 312 |
s->srv_rr->ttl = MDNS_DEFAULT_TTL; |
| 313 |
s->srv_rr->srv.priority = s->priority; |
| 314 |
s->srv_rr->srv.weight = s->weight; |
| 315 |
s->srv_rr->srv.port = s->port; |
| 316 |
s->srv_rr->srv.name = strdup(s->manager->mdns_hostname); |
| 317 |
if (!s->srv_rr->srv.name) |
| 318 |
goto oom; |
| 319 |
|
| 320 |
return 0; |
| 321 |
|
| 322 |
oom: |
| 323 |
LIST_FOREACH(items, txt_data, s->txt_data_items) |
| 324 |
txt_data->rr = dns_resource_record_unref(txt_data->rr); |
| 325 |
s->ptr_rr = dns_resource_record_unref(s->ptr_rr); |
| 326 |
s->sub_ptr_rr = dns_resource_record_unref(s->sub_ptr_rr); |
| 327 |
s->srv_rr = dns_resource_record_unref(s->srv_rr); |
| 328 |
return -ENOMEM; |
| 329 |
} |
| 330 |
|
| 331 |
int dnssd_txt_item_new_from_string(const char *key, const char *value, DnsTxtItem **ret_item) { |
| 332 |
size_t length; |
| 333 |
DnsTxtItem *i; |
| 334 |
|
| 335 |
assert(ret_item); |
| 336 |
|
| 337 |
length = strlen(key); |
| 338 |
|
| 339 |
if (!isempty(value)) |
| 340 |
length += strlen(value) + 1; /* length of value plus '=' */ |
| 341 |
|
| 342 |
i = malloc0(offsetof(DnsTxtItem, data) + length + 1); /* for safety reasons we add an extra NUL byte */ |
| 343 |
if (!i) |
| 344 |
return -ENOMEM; |
| 345 |
|
| 346 |
memcpy(i->data, key, strlen(key)); |
| 347 |
if (!isempty(value)) { |
| 348 |
memcpy(i->data + strlen(key), "=", 1); |
| 349 |
memcpy(i->data + strlen(key) + 1, value, strlen(value)); |
| 350 |
} |
| 351 |
i->length = length; |
| 352 |
|
| 353 |
*ret_item = TAKE_PTR(i); |
| 354 |
|
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
int dnssd_txt_item_new_from_data(const char *key, const void *data, const size_t size, DnsTxtItem **ret_item) { |
| 359 |
size_t length; |
| 360 |
DnsTxtItem *i; |
| 361 |
|
| 362 |
assert(ret_item); |
| 363 |
|
| 364 |
length = strlen(key); |
| 365 |
|
| 366 |
if (size > 0) |
| 367 |
length += size + 1; /* size of date plus '=' */ |
| 368 |
|
| 369 |
i = malloc0(offsetof(DnsTxtItem, data) + length + 1); /* for safety reasons we add an extra NUL byte */ |
| 370 |
if (!i) |
| 371 |
return -ENOMEM; |
| 372 |
|
| 373 |
memcpy(i->data, key, strlen(key)); |
| 374 |
if (size > 0) { |
| 375 |
memcpy(i->data + strlen(key), "=", 1); |
| 376 |
memcpy(i->data + strlen(key) + 1, data, size); |
| 377 |
} |
| 378 |
i->length = length; |
| 379 |
|
| 380 |
*ret_item = TAKE_PTR(i); |
| 381 |
|
| 382 |
return 0; |
| 383 |
} |
| 384 |
|
| 385 |
int dnssd_signal_conflict(Manager *manager, const char *name) { |
| 386 |
DnssdRegisteredService *s; |
| 387 |
int r; |
| 388 |
|
| 389 |
if (sd_bus_is_ready(manager->bus) <= 0) |
| 390 |
return 0; |
| 391 |
|
| 392 |
HASHMAP_FOREACH(s, manager->dnssd_registered_services) { |
| 393 |
if (s->withdrawn) |
| 394 |
continue; |
| 395 |
|
| 396 |
if (dns_name_equal(dns_resource_key_name(s->srv_rr->key), name) > 0) { |
| 397 |
_cleanup_free_ char *path = NULL; |
| 398 |
|
| 399 |
s->withdrawn = true; |
| 400 |
|
| 401 |
r = sd_bus_path_encode("/org/freedesktop/resolve1/dnssd", s->id, &path); |
| 402 |
if (r < 0) |
| 403 |
return log_error_errno(r, "Can't get D-BUS object path: %m"); |
| 404 |
|
| 405 |
r = sd_bus_emit_signal(manager->bus, |
| 406 |
path, |
| 407 |
"org.freedesktop.resolve1.DnssdService", |
| 408 |
"Conflicted", |
| 409 |
NULL); |
| 410 |
if (r < 0) |
| 411 |
return log_error_errno(r, "Cannot emit signal: %m"); |
| 412 |
|
| 413 |
break; |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
return 0; |
| 418 |
} |
| 419 |
|
| 420 |
int config_parse_dnssd_name( |
| 421 |
const char *unit, |
| 422 |
const char *filename, |
| 423 |
unsigned line, |
| 424 |
const char *section, |
| 425 |
unsigned section_line, |
| 426 |
const char *lvalue, |
| 427 |
int ltype, |
| 428 |
const char *rvalue, |
| 429 |
void *data, |
| 430 |
void *userdata) { |
| 431 |
|
| 432 |
static const Specifier specifier_table[] = { |
| 433 |
{ 'a', specifier_architecture, NULL }, |
| 434 |
{ 'b', specifier_boot_id, NULL }, |
| 435 |
{ 'B', specifier_os_build_id, NULL }, |
| 436 |
{ 'H', specifier_hostname, NULL }, /* We will use specifier_dnssd_hostname(). */ |
| 437 |
{ 'm', specifier_machine_id, NULL }, |
| 438 |
{ 'o', specifier_os_id, NULL }, |
| 439 |
{ 'v', specifier_kernel_release, NULL }, |
| 440 |
{ 'w', specifier_os_version_id, NULL }, |
| 441 |
{ 'W', specifier_os_variant_id, NULL }, |
| 442 |
{} |
| 443 |
}; |
| 444 |
DnssdRegisteredService *s = ASSERT_PTR(userdata); |
| 445 |
_cleanup_free_ char *name = NULL; |
| 446 |
int r; |
| 447 |
|
| 448 |
assert(filename); |
| 449 |
assert(lvalue); |
| 450 |
assert(rvalue); |
| 451 |
|
| 452 |
if (isempty(rvalue)) { |
| 453 |
s->name_template = mfree(s->name_template); |
| 454 |
return 0; |
| 455 |
} |
| 456 |
|
| 457 |
r = specifier_printf(rvalue, DNS_LABEL_MAX, specifier_table, NULL, NULL, &name); |
| 458 |
if (r < 0) { |
| 459 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 460 |
"Invalid service instance name template '%s', ignoring assignment: %m", rvalue); |
| 461 |
return 0; |
| 462 |
} |
| 463 |
|
| 464 |
if (!dns_service_name_is_valid(name)) { |
| 465 |
log_syntax(unit, LOG_WARNING, filename, line, 0, |
| 466 |
"Service instance name template '%s' renders to invalid name '%s'. Ignoring assignment.", |
| 467 |
rvalue, name); |
| 468 |
return 0; |
| 469 |
} |
| 470 |
|
| 471 |
return free_and_strdup_warn(&s->name_template, rvalue); |
| 472 |
} |
| 473 |
|
| 474 |
int config_parse_dnssd_type( |
| 475 |
const char *unit, |
| 476 |
const char *filename, |
| 477 |
unsigned line, |
| 478 |
const char *section, |
| 479 |
unsigned section_line, |
| 480 |
const char *lvalue, |
| 481 |
int ltype, |
| 482 |
const char *rvalue, |
| 483 |
void *data, |
| 484 |
void *userdata) { |
| 485 |
|
| 486 |
DnssdRegisteredService *s = ASSERT_PTR(userdata); |
| 487 |
int r; |
| 488 |
|
| 489 |
assert(filename); |
| 490 |
assert(lvalue); |
| 491 |
assert(rvalue); |
| 492 |
|
| 493 |
if (isempty(rvalue)) { |
| 494 |
s->type = mfree(s->type); |
| 495 |
return 0; |
| 496 |
} |
| 497 |
|
| 498 |
if (!dnssd_srv_type_is_valid(rvalue)) { |
| 499 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Service type is invalid. Ignoring."); |
| 500 |
return 0; |
| 501 |
} |
| 502 |
|
| 503 |
r = free_and_strdup(&s->type, rvalue); |
| 504 |
if (r < 0) |
| 505 |
return log_oom(); |
| 506 |
|
| 507 |
return 0; |
| 508 |
} |
| 509 |
|
| 510 |
int config_parse_dnssd_subtype( |
| 511 |
const char *unit, |
| 512 |
const char *filename, |
| 513 |
unsigned line, |
| 514 |
const char *section, |
| 515 |
unsigned section_line, |
| 516 |
const char *lvalue, |
| 517 |
int ltype, |
| 518 |
const char *rvalue, |
| 519 |
void *data, |
| 520 |
void *userdata) { |
| 521 |
|
| 522 |
DnssdRegisteredService *s = ASSERT_PTR(userdata); |
| 523 |
|
| 524 |
assert(filename); |
| 525 |
assert(lvalue); |
| 526 |
assert(rvalue); |
| 527 |
|
| 528 |
if (isempty(rvalue)) { |
| 529 |
s->subtype = mfree(s->subtype); |
| 530 |
return 0; |
| 531 |
} |
| 532 |
|
| 533 |
if (!dns_subtype_name_is_valid(rvalue)) { |
| 534 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Service subtype is invalid. Ignoring."); |
| 535 |
return 0; |
| 536 |
} |
| 537 |
|
| 538 |
return free_and_strdup_warn(&s->subtype, rvalue); |
| 539 |
} |
| 540 |
|
| 541 |
int config_parse_dnssd_txt( |
| 542 |
const char *unit, |
| 543 |
const char *filename, |
| 544 |
unsigned line, |
| 545 |
const char *section, |
| 546 |
unsigned section_line, |
| 547 |
const char *lvalue, |
| 548 |
int ltype, |
| 549 |
const char *rvalue, |
| 550 |
void *data, |
| 551 |
void *userdata) { |
| 552 |
|
| 553 |
_cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL; |
| 554 |
DnssdRegisteredService *s = ASSERT_PTR(userdata); |
| 555 |
DnsTxtItem *last = NULL; |
| 556 |
|
| 557 |
assert(filename); |
| 558 |
assert(lvalue); |
| 559 |
assert(rvalue); |
| 560 |
|
| 561 |
if (isempty(rvalue)) { |
| 562 |
/* Flush out collected items */ |
| 563 |
s->txt_data_items = dnssd_txtdata_free_all(s->txt_data_items); |
| 564 |
return 0; |
| 565 |
} |
| 566 |
|
| 567 |
txt_data = new0(DnssdTxtData, 1); |
| 568 |
if (!txt_data) |
| 569 |
return log_oom(); |
| 570 |
|
| 571 |
for (;;) { |
| 572 |
_cleanup_free_ char *word = NULL, *key = NULL, *value = NULL; |
| 573 |
_cleanup_free_ void *decoded = NULL; |
| 574 |
size_t length = 0; |
| 575 |
DnsTxtItem *i; |
| 576 |
int r; |
| 577 |
|
| 578 |
r = extract_first_word(&rvalue, &word, NULL, |
| 579 |
EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_RELAX); |
| 580 |
if (r == 0) |
| 581 |
break; |
| 582 |
if (r == -ENOMEM) |
| 583 |
return log_oom(); |
| 584 |
if (r < 0) { |
| 585 |
log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); |
| 586 |
return 0; |
| 587 |
} |
| 588 |
|
| 589 |
r = split_pair(word, "=", &key, &value); |
| 590 |
if (r == -ENOMEM) |
| 591 |
return log_oom(); |
| 592 |
if (r == -EINVAL) |
| 593 |
key = TAKE_PTR(word); |
| 594 |
|
| 595 |
if (!ascii_is_valid(key)) { |
| 596 |
log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid key, ignoring: %s", key); |
| 597 |
continue; |
| 598 |
} |
| 599 |
|
| 600 |
switch (ltype) { |
| 601 |
|
| 602 |
case DNS_TXT_ITEM_DATA: |
| 603 |
if (value) { |
| 604 |
r = unbase64mem(value, &decoded, &length); |
| 605 |
if (r == -ENOMEM) |
| 606 |
return log_oom(); |
| 607 |
if (r < 0) { |
| 608 |
log_syntax(unit, LOG_WARNING, filename, line, r, |
| 609 |
"Invalid base64 encoding, ignoring: %s", value); |
| 610 |
continue; |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
r = dnssd_txt_item_new_from_data(key, decoded, length, &i); |
| 615 |
if (r < 0) |
| 616 |
return log_oom(); |
| 617 |
break; |
| 618 |
|
| 619 |
case DNS_TXT_ITEM_TEXT: |
| 620 |
r = dnssd_txt_item_new_from_string(key, value, &i); |
| 621 |
if (r < 0) |
| 622 |
return log_oom(); |
| 623 |
break; |
| 624 |
|
| 625 |
default: |
| 626 |
assert_not_reached(); |
| 627 |
} |
| 628 |
|
| 629 |
LIST_INSERT_AFTER(items, txt_data->txts, last, i); |
| 630 |
last = i; |
| 631 |
} |
| 632 |
|
| 633 |
if (txt_data->txts) { |
| 634 |
LIST_PREPEND(items, s->txt_data_items, txt_data); |
| 635 |
TAKE_PTR(txt_data); |
| 636 |
} |
| 637 |
|
| 638 |
return 0; |
| 639 |
} |