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