]> git.meshlink.io Git - catta/blob - avahi-daemon/static-services.c
* Improve error message when /etc/avahi/services is not available
[catta] / avahi-daemon / static-services.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/stat.h>
27 #include <glob.h>
28 #include <limits.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33
34 #include <glib.h>
35 #include <expat.h>
36
37 #include <avahi-core/llist.h>
38 #include <avahi-core/log.h>
39
40 #include "main.h"
41
42 typedef struct StaticService StaticService;
43 typedef struct StaticServiceGroup StaticServiceGroup;
44
45 struct StaticService {
46     StaticServiceGroup *group;
47     
48     gchar *type;
49     gchar *domain_name;
50     gchar *host_name;
51     guint16 port;
52
53     AvahiStringList *txt_records;
54     
55     AVAHI_LLIST_FIELDS(StaticService, services);
56 };
57
58 struct StaticServiceGroup {
59     gchar *filename;
60     time_t mtime;
61
62     gchar *name, *chosen_name;
63     gboolean replace_wildcards;
64
65     AvahiEntryGroup *entry_group;
66     AVAHI_LLIST_HEAD(StaticService, services);
67     AVAHI_LLIST_FIELDS(StaticServiceGroup, groups);
68 };
69
70 static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
71
72 static gchar *replacestr(const gchar *pattern, const gchar *a, const gchar *b) {
73     gchar *r = NULL, *e, *n;
74
75     while ((e = strstr(pattern, a))) {
76         gchar *k;
77
78         k = g_strndup(pattern, e - pattern);
79         if (r)
80             n = g_strconcat(r, k, b, NULL);
81         else
82             n = g_strconcat(k, b, NULL);
83
84         g_free(k);
85         g_free(r);
86         r = n;
87
88         pattern = e + strlen(a);
89     }
90
91     if (!r)
92         return g_strdup(pattern);
93
94     n = g_strconcat(r, pattern, NULL);
95     g_free(r);
96
97     return n;
98 }
99
100 static void add_static_service_group_to_server(StaticServiceGroup *g);
101 static void remove_static_service_group_from_server(StaticServiceGroup *g);
102
103 static StaticService *static_service_new(StaticServiceGroup *group) {
104     StaticService *s;
105     
106     g_assert(group);
107     s = g_new(StaticService, 1);
108     s->group = group;
109
110     s->type = s->host_name = s->domain_name = NULL;
111     s->port = 0;
112
113     s->txt_records = NULL;
114
115     AVAHI_LLIST_PREPEND(StaticService, services, group->services, s);
116
117     return s;
118 }
119
120 static StaticServiceGroup *static_service_group_new(gchar *filename) {
121     StaticServiceGroup *g;
122     g_assert(filename);
123
124     g = g_new(StaticServiceGroup, 1);
125     g->filename = g_strdup(filename);
126     g->mtime = 0;
127     g->name = g->chosen_name = NULL;
128     g->replace_wildcards = FALSE;
129     g->entry_group = NULL;
130
131     AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
132     AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);
133
134     return g;
135 }
136
137 static void static_service_free(StaticService *s) {
138     g_assert(s);
139     
140     AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
141
142     g_free(s->type);
143     g_free(s->host_name);
144     g_free(s->domain_name);
145
146     avahi_string_list_free(s->txt_records);
147     
148     g_free(s);
149 }
150
151 static void static_service_group_free(StaticServiceGroup *g) {
152     g_assert(g);
153
154     remove_static_service_group_from_server(g);
155
156     while (g->services)
157         static_service_free(g->services);
158
159     AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
160
161     g_free(g->filename);
162     g_free(g->name);
163     g_free(g->chosen_name);
164     g_free(g);
165 }
166
167 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *eg, AvahiEntryGroupState state, gpointer userdata) {
168     StaticServiceGroup *g = userdata;
169     
170     g_assert(s);
171     g_assert(g);
172     
173     if (state == AVAHI_ENTRY_GROUP_COLLISION) {
174         gchar *n;
175
176         remove_static_service_group_from_server(g);
177
178         n = avahi_alternative_service_name(g->chosen_name);
179         g_free(g->chosen_name);
180         g->chosen_name = n;
181
182         avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
183
184         add_static_service_group_to_server(g);
185     } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
186         avahi_log_info("Service \"%s\" (%s) successfully established.", g->chosen_name, g->filename);
187 }
188
189 static void add_static_service_group_to_server(StaticServiceGroup *g) {
190     StaticService *s;
191
192     g_assert(g);
193     
194     if (g->entry_group)
195         return;
196     
197     if (g->chosen_name)
198         g_free(g->chosen_name);
199     
200     if (g->replace_wildcards)
201         g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server));
202     else
203         g->chosen_name = g_strdup(g->name);
204     
205     g->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, g);
206     
207     for (s = g->services; s; s = s->services_next) {
208
209         if (avahi_server_add_service_strlst(
210                 avahi_server,
211                 g->entry_group,
212                 -1, AF_UNSPEC,
213                 s->type, g->chosen_name,
214                 s->domain_name, s->host_name, s->port,
215                 avahi_string_list_copy(s->txt_records)) < 0) {
216             avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s)", g->chosen_name, s->type, g->filename);
217             remove_static_service_group_from_server(g);
218             return;
219         }
220     }
221
222     avahi_entry_group_commit(g->entry_group);
223 }
224
225 static void remove_static_service_group_from_server(StaticServiceGroup *g) {
226     g_assert(g);
227
228     if (g->entry_group) {
229         avahi_entry_group_free(g->entry_group);
230         g->entry_group = NULL;
231     }
232 }
233
234 typedef enum {
235     XML_TAG_INVALID,
236     XML_TAG_SERVICE_GROUP,
237     XML_TAG_NAME,
238     XML_TAG_SERVICE,
239     XML_TAG_TYPE,
240     XML_TAG_DOMAIN_NAME,
241     XML_TAG_HOST_NAME,
242     XML_TAG_PORT,
243     XML_TAG_TXT_RECORD
244 } xml_tag_name;
245
246 struct xml_userdata {
247     StaticServiceGroup *group;
248     StaticService *service;
249     xml_tag_name current_tag;
250     gboolean failed;
251     gchar *buf;
252 };
253
254 static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
255     struct xml_userdata *u = data;
256     
257     g_assert(u);
258
259     if (u->failed)
260         return;
261
262     if (u->current_tag == XML_TAG_INVALID && strcmp(el, "service-group") == 0) {
263
264         if (attr[0])
265             goto invalid_attr;
266
267         u->current_tag = XML_TAG_SERVICE_GROUP;
268     } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "name") == 0) {
269         u->current_tag = XML_TAG_NAME;
270
271         if (attr[0]) {
272             if (strcmp(attr[0], "replace-wildcards") == 0) 
273                 u->group->replace_wildcards = strcmp(attr[1], "yes") == 0;
274             else
275                 goto invalid_attr;
276         }
277
278         if (attr[2])
279             goto invalid_attr;
280         
281             
282         
283     } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) {
284         if (attr[0])
285             goto invalid_attr;
286
287         g_assert(!u->service);
288         u->service = static_service_new(u->group);
289
290         u->current_tag = XML_TAG_SERVICE;
291     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) {
292         if (attr[0])
293             goto invalid_attr;
294
295         u->current_tag = XML_TAG_TYPE;
296     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "domain-name") == 0) {
297         if (attr[0])
298             goto invalid_attr;
299         
300         u->current_tag = XML_TAG_DOMAIN_NAME;
301     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "host-name") == 0) {
302         if (attr[0])
303             goto invalid_attr;
304         
305         u->current_tag = XML_TAG_HOST_NAME;
306     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "port") == 0) {
307         if (attr[0])
308             goto invalid_attr;
309         
310         u->current_tag = XML_TAG_PORT;
311     } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "txt-record") == 0) {
312         if (attr[0])
313             goto invalid_attr;
314         
315         u->current_tag = XML_TAG_TXT_RECORD;
316     } else {
317         avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
318         u->failed = TRUE;
319     }
320
321     return;
322
323 invalid_attr:
324     avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
325     u->failed = TRUE;
326     return;
327 }
328     
329 static void XMLCALL xml_end(void *data, const char *el) {
330     struct xml_userdata *u = data;
331     g_assert(u);
332
333     if (u->failed)
334         return;
335     
336     switch (u->current_tag) {
337         case XML_TAG_SERVICE_GROUP:
338
339             if (!u->group->name || !u->group->services) {
340                 avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
341                 u->failed = TRUE;
342                 return;
343             }
344             
345             u->current_tag = XML_TAG_INVALID;
346             break;
347
348         case XML_TAG_SERVICE:
349
350             if (u->service->port == 0 || !u->service->type) {
351                 avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
352                 u->failed = TRUE;
353                 return;
354             }
355             
356             u->service = NULL;
357             u->current_tag = XML_TAG_SERVICE_GROUP;
358             break;
359
360         case XML_TAG_NAME:
361             u->current_tag = XML_TAG_SERVICE_GROUP;
362             break;
363
364         case XML_TAG_PORT: {
365             int p;
366             g_assert(u->service);
367             
368             p = u->buf ? atoi(u->buf) : 0;
369
370             if (p <= 0 || p > 0xFFFF) {
371                 avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
372                 u->failed = TRUE;
373                 return;
374             }
375
376             u->service->port = (guint16) p;
377
378             u->current_tag = XML_TAG_SERVICE;
379             break;
380         }
381
382         case XML_TAG_TXT_RECORD: {
383             g_assert(u->service);
384             
385             u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
386             u->current_tag = XML_TAG_SERVICE;
387             break;
388         }
389             
390         case XML_TAG_TYPE:
391         case XML_TAG_DOMAIN_NAME:
392         case XML_TAG_HOST_NAME:
393             u->current_tag = XML_TAG_SERVICE;
394             break;
395
396         case XML_TAG_INVALID:
397             ;
398     }
399
400     g_free(u->buf);
401     u->buf = NULL;
402 }
403
404 static gchar *append_cdata(gchar *t, const gchar *n, int length) {
405     gchar *r, *k;
406     
407     if (!length)
408         return t;
409
410     k = g_strndup(n, length);
411
412     if (t) {
413         r = g_strconcat(t, k, NULL);
414         g_free(k);
415         g_free(t);
416     } else
417         r = k;
418     
419     return r;
420 }
421
422 static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
423     struct xml_userdata *u = data;
424     g_assert(u);
425
426     if (u->failed)
427         return;
428
429     switch (u->current_tag) {
430         case XML_TAG_NAME:
431             u->group->name = append_cdata(u->group->name, s, len);
432             break;
433             
434         case XML_TAG_TYPE:
435             g_assert(u->service);
436             u->service->type = append_cdata(u->service->type, s, len);
437             break;
438
439         case XML_TAG_DOMAIN_NAME:
440             g_assert(u->service);
441             u->service->domain_name = append_cdata(u->service->domain_name, s, len);
442             break;
443
444         case XML_TAG_HOST_NAME:
445             g_assert(u->service);
446             u->service->host_name = append_cdata(u->service->host_name, s, len);
447             break;
448
449         case XML_TAG_PORT:
450         case XML_TAG_TXT_RECORD:
451             u->buf = append_cdata(u->buf, s, len);
452             break;
453
454         case XML_TAG_SERVICE_GROUP:
455         case XML_TAG_SERVICE:
456         case XML_TAG_INVALID:
457             ;
458     }
459 }
460
461 static gint static_service_group_load(StaticServiceGroup *g) {
462     XML_Parser parser = NULL;
463     gint fd = -1;
464     struct xml_userdata u;
465     gint r = -1;
466     struct stat st;
467     ssize_t n;
468
469     g_assert(g);
470
471     u.buf = NULL;
472     u.group = g;
473     u.service = NULL;
474     u.current_tag = XML_TAG_INVALID;
475     u.failed = FALSE;
476
477     /* Cleanup old data in this service group, if available */
478     remove_static_service_group_from_server(g);
479     while (g->services)
480         static_service_free(g->services);
481
482     g_free(g->name);
483     g_free(g->chosen_name);
484     g->name = g->chosen_name = NULL;
485     g->replace_wildcards = FALSE;
486     
487     if (!(parser = XML_ParserCreate(NULL))) {
488         avahi_log_error("XML_ParserCreate() failed.");
489         goto finish;
490     }
491
492     if ((fd = open(g->filename, O_RDONLY)) < 0) {
493         avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
494         goto finish;
495     }
496
497     if (fstat(fd, &st) < 0) {
498         avahi_log_error("fstat(): %s", strerror(errno));
499         goto finish;
500     }
501
502     g->mtime = st.st_mtime;
503     
504     XML_SetUserData(parser, &u);
505
506     XML_SetElementHandler(parser, xml_start, xml_end);
507     XML_SetCharacterDataHandler(parser, xml_cdata);
508
509     do {
510         void *buffer;
511
512 #define BUFSIZE (10*1024)
513
514         if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
515             avahi_log_error("XML_GetBuffer() failed.");
516             goto finish;
517         }
518
519         if ((n = read(fd, buffer, BUFSIZE)) < 0) {
520             avahi_log_error("read(): %s\n", strerror(errno));
521             goto finish;
522         }
523
524         if (!XML_ParseBuffer(parser, n, n == 0)) {
525             avahi_log_error("XML_ParseBuffer() failed at line %d: %s.\n", XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
526             goto finish;
527         }
528
529     } while (n != 0);
530
531     if (!u.failed)
532         r = 0;
533
534 finish:
535
536     if (fd >= 0)
537         close(fd);
538
539     if (parser)
540         XML_ParserFree(parser);
541
542     g_free(u.buf);
543
544     return r;
545 }
546
547 static void load_file(gchar *n) {
548     StaticServiceGroup *g;
549     g_assert(n);
550
551     for (g = groups; g; g = g->groups_next)
552         if (strcmp(g->filename, n) == 0)
553             return;
554
555     avahi_log_info("Loading service file %s", n);
556     
557     g = static_service_group_new(n);
558     if (static_service_group_load(g) < 0) {
559         avahi_log_error("Failed to load service group file %s, ignoring.", g->filename);
560         static_service_group_free(g);
561     }
562 }
563
564 void static_service_load(void) {
565     StaticServiceGroup *g, *n;
566     glob_t globbuf;
567     gchar **p;
568
569     for (g = groups; g; g = n) {
570         struct stat st;
571
572         n = g->groups_next;
573
574         if (stat(g->filename, &st) < 0) {
575
576             if (errno == ENOENT)
577                 avahi_log_info("Service group file %s vanished, removing services.", g->filename);
578             else
579                 avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
580             
581             static_service_group_free(g);
582         } else if (st.st_mtime != g->mtime) {
583             avahi_log_info("Service group file %s changed, reloading.", g->filename);
584             
585             if (static_service_group_load(g) < 0) {
586                 avahi_log_warn("Failed to load service group file %s, removing service.", g->filename);
587                 static_service_group_free(g);
588             }
589         }
590     }
591
592     memset(&globbuf, 0, sizeof(globbuf));
593     if (glob(AVAHI_SERVICE_DIRECTORY "/*.service", GLOB_ERR, NULL, &globbuf) != 0)
594         avahi_log_error("Failed to read service directory.");
595     else {
596         for (p = globbuf.gl_pathv; *p; p++)
597             load_file(*p);
598         
599         globfree(&globbuf);
600     }
601 }
602
603 void static_service_free_all(void) {
604     
605     while (groups)
606         static_service_group_free(groups);
607 }
608
609 void static_service_add_to_server(void) {
610     StaticServiceGroup *g;
611
612     for (g = groups; g; g = g->groups_next)
613         add_static_service_group_to_server(g);
614 }
615
616 void static_service_remove_from_server(void) {
617     StaticServiceGroup *g;
618
619     for (g = groups; g; g = g->groups_next)
620         remove_static_service_group_from_server(g);
621 }