]>
Commit | Line | Data |
---|---|---|
6501dd31 DR |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2017 Dmitry Rozhkov | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
20 | #include "conf-files.h" | |
21 | #include "conf-parser.h" | |
22 | #include "resolved-dnssd.h" | |
23 | #include "resolved-dns-rr.h" | |
24 | #include "resolved-manager.h" | |
25 | #include "specifier.h" | |
26 | #include "strv.h" | |
27 | ||
28 | const char* const dnssd_service_dirs[] = { | |
29 | "/etc/systemd/dnssd", | |
30 | "/run/systemd/dnssd", | |
31 | "/usr/lib/systemd/dnssd", | |
32 | #ifdef HAVE_SPLIT_USR | |
33 | "/lib/systemd/dnssd", | |
34 | #endif | |
35 | NULL | |
36 | }; | |
37 | ||
38 | DnssdService *dnssd_service_free(DnssdService *service) { | |
39 | if (!service) | |
40 | return NULL; | |
41 | ||
42 | if (service->manager) | |
43 | hashmap_remove(service->manager->dnssd_services, service->name); | |
44 | ||
45 | dns_resource_record_unref(service->ptr_rr); | |
46 | dns_resource_record_unref(service->srv_rr); | |
47 | dns_resource_record_unref(service->txt_rr); | |
48 | ||
49 | free(service->filename); | |
50 | free(service->name); | |
51 | free(service->type); | |
52 | free(service->name_template); | |
53 | dns_txt_item_free_all(service->txt); | |
54 | ||
55 | return mfree(service); | |
56 | } | |
57 | ||
58 | static int dnssd_service_load(Manager *manager, const char *filename) { | |
59 | _cleanup_(dnssd_service_freep) DnssdService *service = NULL; | |
60 | char *d; | |
61 | const char *dropin_dirname; | |
62 | int r; | |
63 | ||
64 | assert(manager); | |
65 | assert(filename); | |
66 | ||
67 | service = new0(DnssdService, 1); | |
68 | if (!service) | |
69 | return log_oom(); | |
70 | ||
71 | service->filename = strdup(filename); | |
72 | if (!service->filename) | |
73 | return log_oom(); | |
74 | ||
75 | service->name = strdup(basename(filename)); | |
76 | if (!service->name) | |
77 | return log_oom(); | |
78 | ||
79 | d = endswith(service->name, ".dnssd"); | |
80 | if (!d) | |
81 | return -EINVAL; | |
82 | ||
83 | assert(streq(d, ".dnssd")); | |
84 | ||
85 | *d = '\0'; | |
86 | ||
87 | dropin_dirname = strjoina(service->name, ".dnssd.d"); | |
88 | ||
89 | r = config_parse_many(filename, dnssd_service_dirs, dropin_dirname, | |
90 | "Service\0", | |
91 | config_item_perf_lookup, resolved_dnssd_gperf_lookup, | |
92 | false, service); | |
93 | if (r < 0) | |
94 | return r; | |
95 | ||
96 | if (!service->name_template) { | |
97 | log_error("%s doesn't define service instance name", service->name); | |
98 | return -EINVAL; | |
99 | } | |
100 | ||
101 | if (!service->type) { | |
102 | log_error("%s doesn't define service type", service->name); | |
103 | return -EINVAL; | |
104 | } | |
105 | ||
106 | if (!service->txt) { | |
107 | r = dns_txt_item_new_empty(&service->txt); | |
108 | if (r < 0) | |
109 | return r; | |
110 | } | |
111 | ||
112 | r = hashmap_ensure_allocated(&manager->dnssd_services, &string_hash_ops); | |
113 | if (r < 0) | |
114 | return r; | |
115 | ||
116 | r = hashmap_put(manager->dnssd_services, service->name, service); | |
117 | if (r < 0) | |
118 | return r; | |
119 | ||
120 | service->manager = manager; | |
121 | ||
122 | service = NULL; | |
123 | ||
124 | return 0; | |
125 | } | |
126 | ||
127 | static int specifier_dnssd_host_name(char specifier, void *data, void *userdata, char **ret) { | |
128 | DnssdService *s = (DnssdService *) userdata; | |
129 | char *n; | |
130 | ||
131 | assert(s); | |
132 | assert(s->manager); | |
133 | assert(s->manager->llmnr_hostname); | |
134 | ||
135 | n = strdup(s->manager->llmnr_hostname); | |
136 | if (!n) | |
137 | return -ENOMEM; | |
138 | ||
139 | *ret = n; | |
140 | return 0; | |
141 | } | |
142 | ||
143 | int dnssd_render_instance_name(DnssdService *s, char **ret_name) { | |
144 | static const Specifier specifier_table[] = { | |
145 | { 'b', specifier_boot_id, NULL }, | |
146 | { 'H', specifier_dnssd_host_name, NULL }, | |
147 | { 'm', specifier_machine_id, NULL }, | |
148 | { 'v', specifier_kernel_release, NULL }, | |
149 | {} | |
150 | }; | |
151 | _cleanup_free_ char *name = NULL; | |
152 | int r; | |
153 | ||
154 | assert(s); | |
155 | assert(s->name_template); | |
156 | ||
157 | r = specifier_printf(s->name_template, specifier_table, s, &name); | |
158 | if (r < 0) | |
159 | return log_debug_errno(r, "Failed to replace specifiers: %m"); | |
160 | ||
161 | if (!dns_service_name_is_valid(name)) { | |
162 | log_debug("Service instance name '%s' is invalid.", name); | |
163 | return -EINVAL; | |
164 | } | |
165 | ||
166 | *ret_name = name; | |
167 | name = NULL; | |
168 | ||
169 | return 0; | |
170 | } | |
171 | ||
172 | int dnssd_load(Manager *manager) { | |
173 | _cleanup_strv_free_ char **files = NULL; | |
174 | char **f; | |
175 | int r; | |
176 | ||
177 | assert(manager); | |
178 | ||
179 | if (manager->mdns_support != RESOLVE_SUPPORT_YES) | |
180 | return 0; | |
181 | ||
182 | r = conf_files_list_strv(&files, ".dnssd", NULL, 0, dnssd_service_dirs); | |
183 | if (r < 0) | |
184 | return log_error_errno(r, "Failed to enumerate .dnssd files: %m"); | |
185 | ||
186 | STRV_FOREACH_BACKWARDS(f, files) { | |
187 | r = dnssd_service_load(manager, *f); | |
188 | if (r < 0) | |
189 | log_warning_errno(r, "Failed to load '%s': %m", *f);; | |
190 | } | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
195 | int dnssd_txt_item_new_from_string(const char *key, const char *value, DnsTxtItem **ret_item) { | |
196 | size_t length; | |
197 | DnsTxtItem *i; | |
198 | ||
199 | length = strlen(key); | |
200 | ||
201 | if (!isempty(value)) | |
202 | length += strlen(value) + 1; /* length of value plus '=' */ | |
203 | ||
204 | i = malloc0(offsetof(DnsTxtItem, data) + length + 1); /* for safety reasons we add an extra NUL byte */ | |
205 | if (!i) | |
206 | return -ENOMEM; | |
207 | ||
208 | memcpy(i->data, key, strlen(key)); | |
209 | if (!isempty(value)) { | |
210 | memcpy(i->data + strlen(key), "=", 1); | |
211 | memcpy(i->data + strlen(key) + 1, value, strlen(value)); | |
212 | } | |
213 | i->length = length; | |
214 | ||
215 | *ret_item = i; | |
216 | i = NULL; | |
217 | ||
218 | return 0; | |
219 | } | |
220 | ||
221 | int dnssd_txt_item_new_from_data(const char *key, const void *data, const size_t size, DnsTxtItem **ret_item) { | |
222 | size_t length; | |
223 | DnsTxtItem *i; | |
224 | ||
225 | length = strlen(key); | |
226 | ||
227 | if (size > 0) | |
228 | length += size + 1; /* size of date plus '=' */ | |
229 | ||
230 | i = malloc0(offsetof(DnsTxtItem, data) + length + 1); /* for safety reasons we add an extra NUL byte */ | |
231 | if (!i) | |
232 | return -ENOMEM; | |
233 | ||
234 | memcpy(i->data, key, strlen(key)); | |
235 | if (size > 0) { | |
236 | memcpy(i->data + strlen(key), "=", 1); | |
237 | memcpy(i->data + strlen(key) + 1, data, size); | |
238 | } | |
239 | i->length = length; | |
240 | ||
241 | *ret_item = i; | |
242 | i = NULL; | |
243 | ||
244 | return 0; | |
245 | } |