]> git.meshlink.io Git - catta/blob - avahi-common/domain.c
fix size of escaped_name in avahi_service_name_join()
[catta] / avahi-common / domain.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 <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <stdlib.h>
34 #include <assert.h>
35
36 #include "domain.h"
37 #include "malloc.h"
38 #include "error.h"
39
40 char *avahi_get_host_name(char *ret_s, size_t size) {
41 #ifdef HOST_NAME_MAX
42     char t[HOST_NAME_MAX];
43 #else
44     char t[256];
45 #endif
46     
47     assert(ret_s);
48     assert(size > 0);
49     
50     gethostname(t, sizeof(t));
51     t[sizeof(t)-1] = 0;
52     
53     return avahi_normalize_name(t, ret_s, size);
54 }
55
56 char *avahi_get_host_name_strdup(void) {
57     char t[AVAHI_DOMAIN_NAME_MAX];
58
59     if (!(avahi_get_host_name(t, sizeof(t))))
60         return NULL;
61
62     return avahi_strdup(t);
63 }
64
65 /* Read the first label from string *name, unescape "\" and write it to dest */
66 char *avahi_unescape_label(const char **name, char *dest, size_t size) {
67     unsigned i = 0;
68     char *d;
69     
70     assert(dest);
71     assert(size > 0);
72     assert(name);
73
74     d = dest;
75     
76     for (;;) {
77         if (i >= size)
78             return NULL;
79
80         if (**name == '.') {
81             (*name)++;
82             break;
83         }
84         
85         if (**name == 0)
86             break;
87         
88         if (**name == '\\') {
89             /* Escaped character */
90
91             (*name) ++;
92
93             if (**name == 0)
94                 /* Ending NUL */
95                 return NULL;
96             
97             else if (**name == '\\' || **name == '.') {
98                 /* Escaped backslash or dot */
99                 *(d++) = *((*name) ++);
100                 i++;
101             } else if (isdigit(**name)) {
102                 int n;
103
104                 /* Escaped literal ASCII character */
105                 
106                 if (!isdigit(*(*name+1)) || !isdigit(*(*name+2)))
107                     return NULL;
108
109                 n = ((uint8_t) (**name - '0') * 100) + ((uint8_t) (*(*name+1) - '0') * 10) + ((uint8_t) (*(*name +2) - '0'));
110
111                 if (n > 255 || n == 0)
112                     return NULL;
113                 
114                 *(d++) = (char) n;
115                 i++;
116
117                 (*name) += 3;
118             } else
119                 return NULL;
120             
121         } else {
122
123             /* Normal character */
124             
125             *(d++) = *((*name) ++);
126             i++;
127         }
128     }
129
130     assert(i < size);
131
132     *d = 0;
133
134     return dest;
135 }
136
137 /* Escape "\" and ".", append \0 */
138 char *avahi_escape_label(const char* src, size_t src_length, char **ret_name, size_t *ret_size) {
139     char *r;
140
141     assert(src);
142     assert(ret_name);
143     assert(*ret_name);
144     assert(ret_size);
145     assert(*ret_size > 0);
146
147     r = *ret_name;
148
149     while (src_length > 0) {
150         if (*src == '.' || *src == '\\') {
151
152             /* Dot or backslash */
153             
154             if (*ret_size < 3)
155                 return NULL;
156             
157             *((*ret_name) ++) = '\\';
158             *((*ret_name) ++) = *src;
159             (*ret_size) -= 2;
160             
161         } else if (
162             *src == '_' ||
163             *src == '-' ||
164             (*src >= '0' && *src <= '9') ||
165             (*src >= 'a' && *src <= 'z') ||
166             (*src >= 'A' && *src <= 'Z')) {
167
168             /* Proper character */
169             
170             if (*ret_size < 2)
171                 return NULL;
172         
173             *((*ret_name)++) = *src;
174             (*ret_size) --;
175             
176         } else {
177
178             /* Everything else */
179
180             if (*ret_size < 5)
181                 return NULL;
182
183             *((*ret_name) ++) = '\\';
184             *((*ret_name) ++) = '0' + (char)  ((uint8_t) *src / 100);
185             *((*ret_name) ++) = '0' + (char) (((uint8_t) *src / 10) % 10);
186             *((*ret_name) ++) = '0' + (char)  ((uint8_t) *src % 10);
187             
188             (*ret_size) -= 4;
189         }
190
191         src_length --;
192         src++;
193     }
194
195     **ret_name = 0;
196
197     return r;
198 }
199
200 char *avahi_normalize_name(const char *s, char *ret_s, size_t size) {
201     int empty = 1;
202     char *r;
203     
204     assert(s);
205     assert(ret_s);
206     assert(size > 0);
207
208     r = ret_s;
209     while (*s) {
210         char label[AVAHI_LABEL_MAX];
211
212         if (!(avahi_unescape_label(&s, label, sizeof(label))))
213             return NULL;
214
215         if (strlen(label) > 0) {
216
217             if (!empty) {
218                 if (size < 1)
219                     return NULL;
220
221                 *(r++) = '.';
222                 size--;
223
224             } else
225                 empty = 0;
226             
227             avahi_escape_label(label, strlen(label), &r, &size);
228         }
229     }
230
231     if (empty)
232         return NULL;
233     
234     return ret_s;
235 }
236
237 char *avahi_normalize_name_strdup(const char *s) {
238     char t[AVAHI_DOMAIN_NAME_MAX];
239     assert(s);
240
241     if (!(avahi_normalize_name(s, t, sizeof(t))))
242         return NULL;
243
244     return avahi_strdup(t);
245 }
246
247 int avahi_domain_equal(const char *a, const char *b) {
248     assert(a);
249     assert(b);
250
251     if (a == b)
252         return 1;
253     
254     for (;;) {
255         char ca[AVAHI_LABEL_MAX], cb[AVAHI_LABEL_MAX], *r;
256
257         r = avahi_unescape_label(&a, ca, sizeof(ca));
258         assert(r);
259         r = avahi_unescape_label(&b, cb, sizeof(cb));
260         assert(r);
261
262         if (strcasecmp(ca, cb))
263             return 0;
264         
265         if (!*a && !*b)
266             return 1;
267     }
268
269     return 1;
270 }
271
272 int avahi_binary_domain_cmp(const char *a, const char *b) {
273     assert(a);
274     assert(b);
275
276     if (a == b)
277         return 0;
278
279     for (;;) {
280         char ca[AVAHI_LABEL_MAX], cb[AVAHI_LABEL_MAX], *p;
281         int r;
282
283         p = avahi_unescape_label(&a, ca, sizeof(ca));
284         assert(p);
285         p = avahi_unescape_label(&b, cb, sizeof(cb));
286         assert(p);
287
288         if ((r = strcmp(ca, cb)))
289             return r;
290         
291         if (!*a && !*b)
292             return 0;
293     }
294 }
295
296 int avahi_is_valid_service_type_generic(const char *t) {
297     assert(t);
298
299     if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
300         return 0;
301
302     do {
303         char label[AVAHI_LABEL_MAX];
304
305         if (!(avahi_unescape_label(&t, label, sizeof(label))))
306             return 0;
307
308         if (strlen(label) <= 2 || label[0] != '_')
309             return 0;
310         
311     } while (*t);
312
313     return 1;
314 }
315
316 int avahi_is_valid_service_type_strict(const char *t) {
317     char label[AVAHI_LABEL_MAX];
318     assert(t);
319
320     if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
321         return 0;
322
323     /* Application name */
324     
325     if (!(avahi_unescape_label(&t, label, sizeof(label))))
326         return 0;
327
328     if (strlen(label) <= 2 || label[0] != '_')
329         return 0;
330
331     if (!*t)
332         return 0;
333
334     /* _tcp or _udp boilerplate */
335     
336     if (!(avahi_unescape_label(&t, label, sizeof(label))))
337         return 0;
338
339     if (strcasecmp(label, "_tcp") && strcasecmp(label, "_udp"))
340         return 0;
341
342     if (*t)
343         return 0;
344     
345     return 1;
346 }
347
348 const char *avahi_get_type_from_subtype(const char *t) {
349     char label[AVAHI_LABEL_MAX];
350     const char *ret;
351     assert(t);
352
353     if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
354         return NULL;
355
356     /* Subtype name */
357     
358     if (!(avahi_unescape_label(&t, label, sizeof(label))))
359         return NULL;
360
361     if (strlen(label) <= 2 || label[0] != '_')
362         return NULL;
363
364     if (!*t)
365         return NULL;
366
367     /* String "_sub" */
368     
369     if (!(avahi_unescape_label(&t, label, sizeof(label))))
370         return NULL;
371
372     if (strcasecmp(label, "_sub"))
373         return NULL;
374
375     if (!*t)
376         return NULL;
377
378     ret = t;
379     
380     /* Application name */
381
382     if (!(avahi_unescape_label(&t, label, sizeof(label))))
383         return NULL;
384
385     if (strlen(label) <= 2 || label[0] != '_')
386         return NULL;
387
388     if (!*t)
389         return NULL;
390     
391     /* _tcp or _udp boilerplate */
392     
393     if (!(avahi_unescape_label(&t, label, sizeof(label))))
394         return NULL;
395
396     if (strcasecmp(label, "_tcp") && strcasecmp(label, "_udp"))
397         return NULL;
398
399     if (*t)
400         return NULL;
401
402     return ret;
403 }
404
405 int avahi_is_valid_service_subtype(const char *t) {
406     assert(t);
407
408     return !!avahi_get_type_from_subtype(t);
409 }
410
411 int avahi_is_valid_domain_name(const char *t) {
412     assert(t);
413
414     if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
415         return 0;
416
417     do {
418         char label[AVAHI_LABEL_MAX];
419
420         if (!(avahi_unescape_label(&t, label, sizeof(label))))
421             return 0;
422
423         if (strlen(label) < 1)
424             return 0;
425         
426     } while (*t);
427
428     return 1;
429 }
430
431 int avahi_is_valid_service_name(const char *t) {
432     assert(t);
433
434     if (strlen(t) >= AVAHI_LABEL_MAX || !*t)
435         return 0;
436         
437     return 1;
438 }
439
440 int avahi_is_valid_host_name(const char *t) {
441     char label[AVAHI_LABEL_MAX];
442     assert(t);
443
444     if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
445         return 0;
446
447     if (!(avahi_unescape_label(&t, label, sizeof(label))))
448         return 0;
449
450     if (strlen(label) < 1)
451         return 0;
452
453     if (*t)
454         return 0;
455
456     return 1;
457 }
458
459 unsigned avahi_domain_hash(const char *s) {
460     unsigned hash = 0;
461     
462     while (*s) {
463         char c[AVAHI_LABEL_MAX], *p, *r;
464
465         r = avahi_unescape_label(&s, c, sizeof(c));
466         assert(r);
467
468         for (p = c; *p; p++)
469             hash = 31 * hash + tolower(*p);
470     }
471
472     return hash;
473 }
474
475 int avahi_domain_ends_with(const char *domain, const char *suffix) {
476     assert(domain);
477     assert(suffix);
478
479     for (;;) {
480         char dummy[AVAHI_LABEL_MAX], *r;
481
482         if (*domain == 0)
483             return 0;
484         
485         if (avahi_domain_equal(domain, suffix))
486             return 1;
487
488         r = avahi_unescape_label(&domain, dummy, sizeof(dummy));
489         assert(r);
490     } 
491 }
492
493 int avahi_service_name_join(char *p, size_t size, const char *name, const char *type, const char *domain) {
494     char escaped_name[AVAHI_LABEL_MAX*4];
495     char normalized_type[AVAHI_DOMAIN_NAME_MAX];
496     char normalized_domain[AVAHI_DOMAIN_NAME_MAX];
497     
498     assert(p);
499
500     /* Validity checks */
501     
502     if ((name && !avahi_is_valid_service_name(name)))
503         return AVAHI_ERR_INVALID_SERVICE_NAME;
504
505     if (!avahi_is_valid_service_type_generic(type))
506         return AVAHI_ERR_INVALID_SERVICE_TYPE;
507         
508     if (!avahi_is_valid_domain_name(domain))
509         return AVAHI_ERR_INVALID_DOMAIN_NAME;
510
511     /* Preparation */
512     
513     if (name) {
514         size_t l = sizeof(escaped_name);
515         char *e = escaped_name, *r;
516         r = avahi_escape_label(name, strlen(name), &e, &l);
517         assert(r);
518     }
519
520     if (!(avahi_normalize_name(type, normalized_type, sizeof(normalized_type))))
521         return AVAHI_ERR_INVALID_SERVICE_TYPE;
522
523     if (!(avahi_normalize_name(domain, normalized_domain, sizeof(normalized_domain))))
524         return AVAHI_ERR_INVALID_DOMAIN_NAME;
525
526     /* Concatenation */
527     
528     snprintf(p, size, "%s%s%s.%s", name ? escaped_name : "", name ? "." : "", normalized_type, normalized_domain);
529
530     return AVAHI_OK;
531 }
532
533 #ifndef HAVE_STRLCPY
534
535 static size_t strlcpy(char *dest, const char *src, size_t n) {
536     assert(dest);
537     assert(src);
538     
539     if (n > 0) {
540         strncpy(dest, src, n-1);
541         dest[n-1] = 0;
542     }
543     
544     return strlen(src);
545 }
546
547 #endif
548
549 int avahi_service_name_split(const char *p, char *name, size_t name_size, char *type, size_t type_size, char *domain, size_t domain_size) {
550     enum {
551         NAME,
552         TYPE,
553         DOMAIN
554     } state;
555     int type_empty = 1, domain_empty = 1;
556     
557     assert(p);
558     assert(type);
559     assert(type_size > 0);
560     assert(domain);
561     assert(domain_size > 0);
562
563     if (name) {
564         assert(name_size > 0);
565         *name = 0;
566         state = NAME;
567     } else
568         state = TYPE;
569     
570     *type = *domain = 0;
571     
572     while (*p) {
573         char buf[64];
574         
575         if (!(avahi_unescape_label(&p, buf, sizeof(buf))))
576             return -1;
577
578         switch (state) {
579             case NAME:
580                 strlcpy(name, buf, name_size);
581                 state = TYPE;
582                 break;
583
584             case TYPE:
585
586                 if (buf[0] == '_') {
587
588                     if (!type_empty) {
589                         if (!type_size)
590                             return AVAHI_ERR_NO_MEMORY;
591                         
592                         *(type++) = '.';
593                         type_size --;
594
595                     } else
596                         type_empty = 0;
597                     
598                     if (!(avahi_escape_label(buf, strlen(buf), &type, &type_size)))
599                         return AVAHI_ERR_NO_MEMORY;
600
601                     break;
602                 } 
603
604                 state = DOMAIN;
605                 /* fall through */
606
607             case DOMAIN:
608
609                 if (!domain_empty) {
610                     if (!domain_size)
611                         return AVAHI_ERR_NO_MEMORY;
612                     
613                     *(domain++) = '.';
614                     domain_size --;
615                 } else
616                     domain_empty = 0;
617
618                 if (!(avahi_escape_label(buf, strlen(buf), &domain, &domain_size)))
619                     return AVAHI_ERR_NO_MEMORY;
620
621                 break;
622         }
623     }
624
625     return 0;
626 }
627