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