Coverage Report

Created: 2025-07-11 06:49

/src/avahi/avahi-common/domain.c
Line
Count
Source (jump to first uncovered line)
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
42.6k
char *avahi_unescape_label(const char **name, char *dest, size_t size) {
42
42.6k
    unsigned i = 0;
43
42.6k
    char *d;
44
45
42.6k
    assert(dest);
46
42.6k
    assert(size > 0);
47
42.6k
    assert(name);
48
49
42.6k
    d = dest;
50
51
123k
    for (;;) {
52
123k
        if (i >= size)
53
0
            return NULL;
54
55
123k
        if (**name == '.') {
56
38.9k
            (*name)++;
57
38.9k
            break;
58
38.9k
        }
59
60
84.3k
        if (**name == 0)
61
3.68k
            break;
62
63
80.6k
        if (**name == '\\') {
64
            /* Escaped character */
65
66
24.1k
            (*name) ++;
67
68
24.1k
            if (**name == 0)
69
                /* Ending NUL */
70
0
                return NULL;
71
72
24.1k
            else if (**name == '\\' || **name == '.') {
73
                /* Escaped backslash or dot */
74
1.46k
                *(d++) = *((*name) ++);
75
1.46k
                i++;
76
22.6k
            } else if (isdigit(**name)) {
77
22.6k
                int n;
78
79
                /* Escaped literal ASCII character */
80
81
22.6k
                if (!isdigit(*(*name+1)) || !isdigit(*(*name+2)))
82
0
                    return NULL;
83
84
22.6k
                n = ((uint8_t) (**name - '0') * 100) + ((uint8_t) (*(*name+1) - '0') * 10) + ((uint8_t) (*(*name +2) - '0'));
85
86
22.6k
                if (n > 255 || n == 0)
87
13
                    return NULL;
88
89
22.6k
                *(d++) = (char) n;
90
22.6k
                i++;
91
92
22.6k
                (*name) += 3;
93
22.6k
            } else
94
0
                return NULL;
95
96
56.4k
        } else {
97
98
            /* Normal character */
99
100
56.4k
            *(d++) = *((*name) ++);
101
56.4k
            i++;
102
56.4k
        }
103
80.6k
    }
104
105
42.6k
    assert(i < size);
106
107
42.6k
    *d = 0;
108
109
42.6k
    if (!avahi_utf8_valid(dest))
110
64
        return NULL;
111
112
42.5k
    return dest;
113
42.6k
}
114
115
/* Escape "\" and ".", append \0 */
116
25.4k
char *avahi_escape_label(const char* src, size_t src_length, char **ret_name, size_t *ret_size) {
117
25.4k
    char *r;
118
119
25.4k
    assert(src);
120
25.4k
    assert(ret_name);
121
25.4k
    assert(*ret_name);
122
25.4k
    assert(ret_size);
123
25.4k
    assert(*ret_size > 0);
124
125
25.4k
    r = *ret_name;
126
127
82.9k
    while (src_length > 0) {
128
57.4k
        if (*src == '.' || *src == '\\') {
129
130
            /* Dot or backslash */
131
132
1.72k
            if (*ret_size < 3)
133
5
                return NULL;
134
135
1.72k
            *((*ret_name) ++) = '\\';
136
1.72k
            *((*ret_name) ++) = *src;
137
1.72k
            (*ret_size) -= 2;
138
139
55.7k
        } else if (
140
55.7k
            *src == '_' ||
141
55.7k
            *src == '-' ||
142
55.7k
            (*src >= '0' && *src <= '9') ||
143
55.7k
            (*src >= 'a' && *src <= 'z') ||
144
55.7k
            (*src >= 'A' && *src <= 'Z')) {
145
146
            /* Proper character */
147
148
41.6k
            if (*ret_size < 2)
149
1
                return NULL;
150
151
41.6k
            *((*ret_name)++) = *src;
152
41.6k
            (*ret_size) --;
153
154
41.6k
        } else {
155
156
            /* Everything else */
157
158
14.0k
            if (*ret_size < 5)
159
20
                return NULL;
160
161
14.0k
            *((*ret_name) ++) = '\\';
162
14.0k
            *((*ret_name) ++) = '0' + (char)  ((uint8_t) *src / 100);
163
14.0k
            *((*ret_name) ++) = '0' + (char) (((uint8_t) *src / 10) % 10);
164
14.0k
            *((*ret_name) ++) = '0' + (char)  ((uint8_t) *src % 10);
165
166
14.0k
            (*ret_size) -= 4;
167
14.0k
        }
168
169
57.4k
        src_length --;
170
57.4k
        src++;
171
57.4k
    }
172
173
25.4k
    **ret_name = 0;
174
175
25.4k
    return r;
176
25.4k
}
177
178
1.08k
char *avahi_normalize_name(const char *s, char *ret_s, size_t size) {
179
1.08k
    int empty = 1;
180
1.08k
    char *r;
181
182
1.08k
    assert(s);
183
1.08k
    assert(ret_s);
184
1.08k
    assert(size > 0);
185
186
1.08k
    r = ret_s;
187
1.08k
    *ret_s = 0;
188
189
11.8k
    while (*s) {
190
10.8k
        char label[AVAHI_LABEL_MAX];
191
192
10.8k
        if (!(avahi_unescape_label(&s, label, sizeof(label))))
193
77
            return NULL;
194
195
10.7k
        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
10.7k
        if (!empty) {
204
10.0k
            if (size < 2)
205
0
                return NULL;
206
207
10.0k
            *(r++) = '.';
208
10.0k
            size--;
209
210
10.0k
        } else
211
724
            empty = 0;
212
213
10.7k
        if (!(avahi_escape_label(label, strlen(label), &r, &size)))
214
0
            return NULL;
215
10.7k
    }
216
217
1.00k
    return ret_s;
218
1.08k
}
219
220
1.08k
char *avahi_normalize_name_strdup(const char *s) {
221
1.08k
    char t[AVAHI_DOMAIN_NAME_MAX];
222
1.08k
    assert(s);
223
224
1.08k
    if (!(avahi_normalize_name(s, t, sizeof(t))))
225
77
        return NULL;
226
227
1.00k
    return avahi_strdup(t);
228
1.08k
}
229
230
322
int avahi_domain_equal(const char *a, const char *b) {
231
322
    assert(a);
232
322
    assert(b);
233
234
322
    if (a == b)
235
0
        return 1;
236
237
680
    for (;;) {
238
680
        char ca[AVAHI_LABEL_MAX], cb[AVAHI_LABEL_MAX], *r;
239
240
680
        r = avahi_unescape_label(&a, ca, sizeof(ca));
241
680
        assert(r);
242
680
        r = avahi_unescape_label(&b, cb, sizeof(cb));
243
680
        assert(r);
244
245
680
        if (strcasecmp(ca, cb))
246
202
            return 0;
247
248
478
        if (!*a && !*b)
249
120
            return 1;
250
478
    }
251
252
0
    return 1;
253
322
}
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
1.89k
int avahi_is_valid_domain_name(const char *t) {
371
1.89k
    int is_first = 1;
372
1.89k
    assert(t);
373
374
1.89k
    if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX)
375
0
        return 0;
376
377
21.3k
    do {
378
21.3k
        char label[AVAHI_LABEL_MAX];
379
380
21.3k
        if (!(avahi_unescape_label(&t, label, sizeof(label))))
381
0
            return 0;
382
383
        /* Explicitly allow the root domain name */
384
21.3k
        if (is_first && label[0] == 0 && *t == 0)
385
564
            return 1;
386
387
20.7k
        is_first = 0;
388
389
20.7k
        if (label[0] == 0)
390
0
            return 0;
391
392
20.7k
    } while (*t);
393
394
1.33k
    return 1;
395
1.89k
}
396
397
0
int avahi_is_valid_service_name(const char *t) {
398
0
    assert(t);
399
400
0
    if (strlen(t) >= AVAHI_LABEL_MAX || !*t || !avahi_utf8_valid(t))
401
0
        return 0;
402
403
0
    return 1;
404
0
}
405
406
0
int avahi_is_valid_host_name(const char *t) {
407
0
    char label[AVAHI_LABEL_MAX];
408
0
    assert(t);
409
410
0
    if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX || !*t)
411
0
        return 0;
412
413
0
    if (!(avahi_unescape_label(&t, label, sizeof(label))))
414
0
        return 0;
415
416
0
    if (strlen(label) < 1)
417
0
        return 0;
418
419
0
    if (*t)
420
0
        return 0;
421
422
0
    return 1;
423
0
}
424
425
619
unsigned avahi_domain_hash(const char *s) {
426
619
    unsigned hash = 0;
427
428
9.71k
    while (*s) {
429
9.09k
        char c[AVAHI_LABEL_MAX], *p, *r;
430
431
9.09k
        r = avahi_unescape_label(&s, c, sizeof(c));
432
9.09k
        assert(r);
433
434
21.4k
        for (p = c; *p; p++)
435
12.3k
            hash = 31 * hash + tolower(*p);
436
9.09k
    }
437
438
619
    return hash;
439
619
}
440
441
0
int avahi_service_name_join(char *p, size_t size, const char *name, const char *type, const char *domain) {
442
0
    char escaped_name[AVAHI_LABEL_MAX*4];
443
0
    char normalized_type[AVAHI_DOMAIN_NAME_MAX];
444
0
    char normalized_domain[AVAHI_DOMAIN_NAME_MAX];
445
446
0
    assert(p);
447
448
    /* Validity checks */
449
450
0
    if ((name && !avahi_is_valid_service_name(name)))
451
0
        return AVAHI_ERR_INVALID_SERVICE_NAME;
452
453
0
    if (!avahi_is_valid_service_type_generic(type))
454
0
        return AVAHI_ERR_INVALID_SERVICE_TYPE;
455
456
0
    if (!avahi_is_valid_domain_name(domain))
457
0
        return AVAHI_ERR_INVALID_DOMAIN_NAME;
458
459
    /* Preparation */
460
461
0
    if (name) {
462
0
        size_t l = sizeof(escaped_name);
463
0
        char *e = escaped_name, *r;
464
0
        r = avahi_escape_label(name, strlen(name), &e, &l);
465
0
        assert(r);
466
0
    }
467
468
0
    if (!(avahi_normalize_name(type, normalized_type, sizeof(normalized_type))))
469
0
        return AVAHI_ERR_INVALID_SERVICE_TYPE;
470
471
0
    if (!(avahi_normalize_name(domain, normalized_domain, sizeof(normalized_domain))))
472
0
        return AVAHI_ERR_INVALID_DOMAIN_NAME;
473
474
    /* Concatenation */
475
476
0
    snprintf(p, size, "%s%s%s.%s", name ? escaped_name : "", name ? "." : "", normalized_type, normalized_domain);
477
478
0
    return AVAHI_OK;
479
0
}
480
481
#ifndef HAVE_STRLCPY
482
483
0
static size_t strlcpy(char *dest, const char *src, size_t n) {
484
0
    assert(dest);
485
0
    assert(src);
486
487
0
    if (n > 0) {
488
0
        strncpy(dest, src, n-1);
489
0
        dest[n-1] = 0;
490
0
    }
491
492
0
    return strlen(src);
493
0
}
494
495
#endif
496
497
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) {
498
0
    enum {
499
0
        NAME,
500
0
        TYPE,
501
0
        DOMAIN
502
0
    } state;
503
0
    int type_empty = 1, domain_empty = 1;
504
0
    char *oname, *otype, *odomain;
505
506
0
    assert(p);
507
0
    assert(type);
508
0
    assert(type_size > 0);
509
0
    assert(domain);
510
0
    assert(domain_size > 0);
511
512
0
    oname = name;
513
0
    otype = type;
514
0
    odomain = domain;
515
516
0
    if (name) {
517
0
        assert(name_size > 0);
518
0
        *name = 0;
519
0
        state = NAME;
520
0
    } else
521
0
        state = TYPE;
522
523
0
    *type = *domain = 0;
524
525
0
    while (*p) {
526
0
        char buf[64];
527
528
0
        if (!(avahi_unescape_label(&p, buf, sizeof(buf))))
529
0
            return -1;
530
531
0
        switch (state) {
532
0
            case NAME:
533
0
                strlcpy(name, buf, name_size);
534
0
                state = TYPE;
535
0
                break;
536
537
0
            case TYPE:
538
539
0
                if (buf[0] == '_') {
540
541
0
                    if (!type_empty) {
542
0
                        if (!type_size)
543
0
                            return AVAHI_ERR_NO_MEMORY;
544
545
0
                        *(type++) = '.';
546
0
                        type_size --;
547
548
0
                    } else
549
0
                        type_empty = 0;
550
551
0
                    if (!(avahi_escape_label(buf, strlen(buf), &type, &type_size)))
552
0
                        return AVAHI_ERR_NO_MEMORY;
553
554
0
                    break;
555
0
                }
556
557
0
                state = DOMAIN;
558
                /* fall through */
559
560
0
            case DOMAIN:
561
562
0
                if (!domain_empty) {
563
0
                    if (!domain_size)
564
0
                        return AVAHI_ERR_NO_MEMORY;
565
566
0
                    *(domain++) = '.';
567
0
                    domain_size --;
568
0
                } else
569
0
                    domain_empty = 0;
570
571
0
                if (!(avahi_escape_label(buf, strlen(buf), &domain, &domain_size)))
572
0
                    return AVAHI_ERR_NO_MEMORY;
573
574
0
                break;
575
0
        }
576
0
    }
577
578
0
    if ((oname && !avahi_is_valid_service_name(oname)))
579
0
        return AVAHI_ERR_INVALID_SERVICE_NAME;
580
581
0
    if (!avahi_is_valid_service_type_strict(otype) && !avahi_is_valid_service_subtype(otype))
582
0
        return AVAHI_ERR_INVALID_SERVICE_TYPE;
583
584
0
    if (!avahi_is_valid_domain_name(odomain))
585
0
        return AVAHI_ERR_INVALID_DOMAIN_NAME;
586
587
0
    return 0;
588
0
}
589
590
0
int avahi_is_valid_fqdn(const char *t) {
591
0
    char label[AVAHI_LABEL_MAX];
592
0
    char normalized[AVAHI_DOMAIN_NAME_MAX];
593
0
    const char *k = t;
594
0
    AvahiAddress a;
595
0
    assert(t);
596
597
0
    if (strlen(t) >= AVAHI_DOMAIN_NAME_MAX)
598
0
        return 0;
599
600
0
    if (!avahi_is_valid_domain_name(t))
601
0
        return 0;
602
603
    /* Check if there are at least two labels*/
604
0
    if (!(avahi_unescape_label(&k, label, sizeof(label))))
605
0
        return 0;
606
607
0
    if (label[0] == 0 || !k)
608
0
        return 0;
609
610
0
    if (!(avahi_unescape_label(&k, label, sizeof(label))))
611
0
        return 0;
612
613
0
    if (label[0] == 0 || !k)
614
0
        return 0;
615
616
    /* Make sure that the name is not an IP address */
617
0
    if (!(avahi_normalize_name(t, normalized, sizeof(normalized))))
618
0
        return 0;
619
620
0
    if (avahi_address_parse(normalized, AVAHI_PROTO_UNSPEC, &a))
621
0
        return 0;
622
623
0
    return 1;
624
0
}