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