Coverage Report

Created: 2026-12-01 06:04

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