6501dd31a7
Introduce network services loaded from .dnssd files that can be used for server-side DNS-SD implementation in systemd-resolved.
246 lines
6.8 KiB
C
246 lines
6.8 KiB
C
/***
|
|
This file is part of systemd.
|
|
|
|
Copyright 2017 Dmitry Rozhkov
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
(at your option) any later version.
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
***/
|
|
|
|
#include "conf-files.h"
|
|
#include "conf-parser.h"
|
|
#include "resolved-dnssd.h"
|
|
#include "resolved-dns-rr.h"
|
|
#include "resolved-manager.h"
|
|
#include "specifier.h"
|
|
#include "strv.h"
|
|
|
|
const char* const dnssd_service_dirs[] = {
|
|
"/etc/systemd/dnssd",
|
|
"/run/systemd/dnssd",
|
|
"/usr/lib/systemd/dnssd",
|
|
#ifdef HAVE_SPLIT_USR
|
|
"/lib/systemd/dnssd",
|
|
#endif
|
|
NULL
|
|
};
|
|
|
|
DnssdService *dnssd_service_free(DnssdService *service) {
|
|
if (!service)
|
|
return NULL;
|
|
|
|
if (service->manager)
|
|
hashmap_remove(service->manager->dnssd_services, service->name);
|
|
|
|
dns_resource_record_unref(service->ptr_rr);
|
|
dns_resource_record_unref(service->srv_rr);
|
|
dns_resource_record_unref(service->txt_rr);
|
|
|
|
free(service->filename);
|
|
free(service->name);
|
|
free(service->type);
|
|
free(service->name_template);
|
|
dns_txt_item_free_all(service->txt);
|
|
|
|
return mfree(service);
|
|
}
|
|
|
|
static int dnssd_service_load(Manager *manager, const char *filename) {
|
|
_cleanup_(dnssd_service_freep) DnssdService *service = NULL;
|
|
char *d;
|
|
const char *dropin_dirname;
|
|
int r;
|
|
|
|
assert(manager);
|
|
assert(filename);
|
|
|
|
service = new0(DnssdService, 1);
|
|
if (!service)
|
|
return log_oom();
|
|
|
|
service->filename = strdup(filename);
|
|
if (!service->filename)
|
|
return log_oom();
|
|
|
|
service->name = strdup(basename(filename));
|
|
if (!service->name)
|
|
return log_oom();
|
|
|
|
d = endswith(service->name, ".dnssd");
|
|
if (!d)
|
|
return -EINVAL;
|
|
|
|
assert(streq(d, ".dnssd"));
|
|
|
|
*d = '\0';
|
|
|
|
dropin_dirname = strjoina(service->name, ".dnssd.d");
|
|
|
|
r = config_parse_many(filename, dnssd_service_dirs, dropin_dirname,
|
|
"Service\0",
|
|
config_item_perf_lookup, resolved_dnssd_gperf_lookup,
|
|
false, service);
|
|
if (r < 0)
|
|
return r;
|
|
|
|
if (!service->name_template) {
|
|
log_error("%s doesn't define service instance name", service->name);
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (!service->type) {
|
|
log_error("%s doesn't define service type", service->name);
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (!service->txt) {
|
|
r = dns_txt_item_new_empty(&service->txt);
|
|
if (r < 0)
|
|
return r;
|
|
}
|
|
|
|
r = hashmap_ensure_allocated(&manager->dnssd_services, &string_hash_ops);
|
|
if (r < 0)
|
|
return r;
|
|
|
|
r = hashmap_put(manager->dnssd_services, service->name, service);
|
|
if (r < 0)
|
|
return r;
|
|
|
|
service->manager = manager;
|
|
|
|
service = NULL;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int specifier_dnssd_host_name(char specifier, void *data, void *userdata, char **ret) {
|
|
DnssdService *s = (DnssdService *) userdata;
|
|
char *n;
|
|
|
|
assert(s);
|
|
assert(s->manager);
|
|
assert(s->manager->llmnr_hostname);
|
|
|
|
n = strdup(s->manager->llmnr_hostname);
|
|
if (!n)
|
|
return -ENOMEM;
|
|
|
|
*ret = n;
|
|
return 0;
|
|
}
|
|
|
|
int dnssd_render_instance_name(DnssdService *s, char **ret_name) {
|
|
static const Specifier specifier_table[] = {
|
|
{ 'b', specifier_boot_id, NULL },
|
|
{ 'H', specifier_dnssd_host_name, NULL },
|
|
{ 'm', specifier_machine_id, NULL },
|
|
{ 'v', specifier_kernel_release, NULL },
|
|
{}
|
|
};
|
|
_cleanup_free_ char *name = NULL;
|
|
int r;
|
|
|
|
assert(s);
|
|
assert(s->name_template);
|
|
|
|
r = specifier_printf(s->name_template, specifier_table, s, &name);
|
|
if (r < 0)
|
|
return log_debug_errno(r, "Failed to replace specifiers: %m");
|
|
|
|
if (!dns_service_name_is_valid(name)) {
|
|
log_debug("Service instance name '%s' is invalid.", name);
|
|
return -EINVAL;
|
|
}
|
|
|
|
*ret_name = name;
|
|
name = NULL;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int dnssd_load(Manager *manager) {
|
|
_cleanup_strv_free_ char **files = NULL;
|
|
char **f;
|
|
int r;
|
|
|
|
assert(manager);
|
|
|
|
if (manager->mdns_support != RESOLVE_SUPPORT_YES)
|
|
return 0;
|
|
|
|
r = conf_files_list_strv(&files, ".dnssd", NULL, 0, dnssd_service_dirs);
|
|
if (r < 0)
|
|
return log_error_errno(r, "Failed to enumerate .dnssd files: %m");
|
|
|
|
STRV_FOREACH_BACKWARDS(f, files) {
|
|
r = dnssd_service_load(manager, *f);
|
|
if (r < 0)
|
|
log_warning_errno(r, "Failed to load '%s': %m", *f);;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int dnssd_txt_item_new_from_string(const char *key, const char *value, DnsTxtItem **ret_item) {
|
|
size_t length;
|
|
DnsTxtItem *i;
|
|
|
|
length = strlen(key);
|
|
|
|
if (!isempty(value))
|
|
length += strlen(value) + 1; /* length of value plus '=' */
|
|
|
|
i = malloc0(offsetof(DnsTxtItem, data) + length + 1); /* for safety reasons we add an extra NUL byte */
|
|
if (!i)
|
|
return -ENOMEM;
|
|
|
|
memcpy(i->data, key, strlen(key));
|
|
if (!isempty(value)) {
|
|
memcpy(i->data + strlen(key), "=", 1);
|
|
memcpy(i->data + strlen(key) + 1, value, strlen(value));
|
|
}
|
|
i->length = length;
|
|
|
|
*ret_item = i;
|
|
i = NULL;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int dnssd_txt_item_new_from_data(const char *key, const void *data, const size_t size, DnsTxtItem **ret_item) {
|
|
size_t length;
|
|
DnsTxtItem *i;
|
|
|
|
length = strlen(key);
|
|
|
|
if (size > 0)
|
|
length += size + 1; /* size of date plus '=' */
|
|
|
|
i = malloc0(offsetof(DnsTxtItem, data) + length + 1); /* for safety reasons we add an extra NUL byte */
|
|
if (!i)
|
|
return -ENOMEM;
|
|
|
|
memcpy(i->data, key, strlen(key));
|
|
if (size > 0) {
|
|
memcpy(i->data + strlen(key), "=", 1);
|
|
memcpy(i->data + strlen(key) + 1, data, size);
|
|
}
|
|
i->length = length;
|
|
|
|
*ret_item = i;
|
|
i = NULL;
|
|
|
|
return 0;
|
|
}
|