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