Coverage Report

Created: 2026-06-30 06:08

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