summaryrefslogtreecommitdiffstats
path: root/src/nss.c
blob: 13c81090f21c341429a98433141dcc68eb5313e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/* $Id$ */

/***
    This file is part of nss-mdns.
 
    nss-mdns is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published
    by the Free Software Foundation; either version 2 of the License,
    or (at your option) any later version.
 
    nss-mdns is distributed in the hope that it will be useful, but1
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public License
    along with nss-mdns; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
    USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <netdb.h>
#include <sys/socket.h>
#include <nss.h>
#include <stdio.h>
#include <stdlib.h>

#include "query.h"

#ifdef ENABLE_AVAHI
#include "avahi.h"
#endif

#if defined(NSS_IPV4_ONLY) && ! defined(MDNS_MINIMAL)
#define _nss_mdns_gethostbyname2_r _nss_mdns4_gethostbyname2_r
#define _nss_mdns_gethostbyname_r  _nss_mdns4_gethostbyname_r
#define _nss_mdns_gethostbyaddr_r  _nss_mdns4_gethostbyaddr_r
#elif defined(NSS_IPV4_ONLY) && defined(MDNS_MINIMAL)
#define _nss_mdns_gethostbyname2_r _nss_mdns4_minimal_gethostbyname2_r
#define _nss_mdns_gethostbyname_r  _nss_mdns4_minimal_gethostbyname_r
#define _nss_mdns_gethostbyaddr_r  _nss_mdns4_minimal_gethostbyaddr_r
#elif defined(NSS_IPV6_ONLY) && ! defined(MDNS_MINIMAL)
#define _nss_mdns_gethostbyname2_r _nss_mdns6_gethostbyname2_r
#define _nss_mdns_gethostbyname_r  _nss_mdns6_gethostbyname_r
#define _nss_mdns_gethostbyaddr_r  _nss_mdns6_gethostbyaddr_r
#elif defined(NSS_IPV6_ONLY) && defined(MDNS_MINIMAL)
#define _nss_mdns_gethostbyname2_r _nss_mdns6_minimal_gethostbyname2_r
#define _nss_mdns_gethostbyname_r  _nss_mdns6_minimal_gethostbyname_r
#define _nss_mdns_gethostbyaddr_r  _nss_mdns6_minimal_gethostbyaddr_r
#elif defined(MDNS_MINIMAL)
#define _nss_mdns_gethostbyname2_r _nss_mdns_minimal_gethostbyname2_r
#define _nss_mdns_gethostbyname_r  _nss_mdns_minimal_gethostbyname_r
#define _nss_mdns_gethostbyaddr_r  _nss_mdns_minimal_gethostbyaddr_r
#endif

/* Maximum number of entries to return */
#define MAX_ENTRIES 16

/* The resolv.conf page states that they only support 6 domains */
#define MAX_SEARCH_DOMAINS 6

#define ALIGN(idx) do { \
  if (idx % sizeof(void*)) \
    idx += (sizeof(void*) - idx % sizeof(void*)); /* Align on 32 bit boundary */ \
} while(0)

struct userdata {
    int count;
    int data_len; /* only valid when doing reverse lookup */
    union  {
        ipv4_address_t ipv4[MAX_ENTRIES];
        ipv6_address_t ipv6[MAX_ENTRIES];
        char *name[MAX_ENTRIES];
    } data;
};

#ifndef NSS_IPV6_ONLY
static void ipv4_callback(const ipv4_address_t *ipv4, void *userdata) {
    struct userdata *u = userdata;
    assert(ipv4 && userdata);

    if (u->count >= MAX_ENTRIES)
        return;

    u->data.ipv4[u->count++] = *ipv4;
    u->data_len += sizeof(ipv4_address_t);
}
#endif

#ifndef NSS_IPV4_ONLY
static void ipv6_callback(const ipv6_address_t *ipv6, void *userdata) {
    struct userdata *u = userdata;
    assert(ipv6 && userdata);

    if (u->count >= MAX_ENTRIES)
        return;

    u->data.ipv6[u->count++] = *ipv6;
    u->data_len += sizeof(ipv6_address_t);
}
#endif

static void name_callback(const char*name, void *userdata) {
    struct userdata *u = userdata;
    assert(name && userdata);

    if (u->count >= MAX_ENTRIES)
        return;

    u->data.name[u->count++] = strdup(name);
    u->data_len += strlen(name)+1;
}

static int ends_with(const char *name, const char* suffix) {
    size_t ln, ls;
    assert(name);
    assert(suffix);

    if ((ls = strlen(suffix)) > (ln = strlen(name)))
        return 0;

    return strcasecmp(name+ln-ls, suffix) == 0;
}

static int verify_name_allowed(const char *name) {
#ifndef MDNS_MINIMAL
    FILE *f;
#endif
    
    assert(name);

#ifndef MDNS_MINIMAL
    if ((f = fopen(MDNS_ALLOW_FILE, "r"))) {
        int valid = 0;
        
        
        while (!feof(f)) {
            char ln[128], ln2[128], *t;
            
            if (!fgets(ln, sizeof(ln), f))
                break;
            
            ln[strcspn(ln, "#\t\n\r ")] = 0;
            
            if (ln[0] == 0)
                continue;
            
            if (strcmp(ln, "*") == 0) {
                valid = 1;
                break;
            }
            
            if (ln[0] != '.')
                snprintf(t = ln2, sizeof(ln2), ".%s", ln);
            else
                t = ln;
            
            if (ends_with(name, t)) {
                valid = 1;
                break;
            }
        }
        
        fclose(f);
        return valid;
    }
#endif

    return ends_with(name, ".local") || ends_with(name, ".local."); 
}

#ifdef HONOUR_SEARCH_DOMAINS

static char **alloc_domains(unsigned ndomains) {
    char **domains;

    if (!(domains = malloc(sizeof(char*) * ndomains)))
        return NULL;

    /* initialize them all to 0 */
    memset(domains, 0, sizeof(char*) * ndomains);
    return domains;
}

static void free_domains(char **domains) {
    char **p;

    if (!domains)
        return;

    for(p = domains; *p; p++) 
        free(*p);

    free(domains);
}

static char** parse_domains(const char *domains_in) {
    /* leave room for the NULL terminator */
    char **domains_out;
    const char *start = domains_in;
    unsigned domain = 0;

    if (!(domains_out = alloc_domains(MAX_SEARCH_DOMAINS+1)))
        return NULL;

    while (domain < MAX_SEARCH_DOMAINS) {
        const char *end;
        char *tmp;
        size_t domain_len;
        
        end = start + strcspn(start, " \t\r\n");
        domain_len = (end - start);

        if (!(tmp = malloc(domain_len + 1)))
            break;
        
        memcpy(tmp, start, domain_len);
        tmp[domain_len] = '\0';

        domains_out[domain++] = tmp;

        end += strspn(end," \t\r\n");

        if (!*end)
            break;
        
        start = end;
    }

    return domains_out;
}

static char** get_search_domains(void) {
    FILE *f = 0;
    char **domains = NULL;

    /* according to the resolv.conf man page (in Linux) the LOCALDOMAIN
       environment variable should override the settings in the resolv.conf file */
    char *line = getenv("LOCALDOMAIN");
    if (line && *line != 0)
        return parse_domains(line);
    
    if (!(f = fopen(RESOLV_CONF_FILE, "r")))
        return NULL;

    while (!feof(f)) {
        char *start = NULL;
        char ln[512];
	  
        if (!fgets(ln, sizeof(ln), f))
            break;

        start = ln + strspn(ln, " \t\r\n");
    
        if (strncmp(start, "search", 6) && strncmp(start, "domain", 6))
            continue;
        
        if (start[6] != ' ' && start[6] != '\t')
            continue;

        /* scan to the end of the keyword ('search' or 'domain' currently) */
        start += strcspn(start, " \t\r\n");

        /* find the begining of the first domain in the list */
        start += strspn(start, " \t\r\n");

        /* the resolv.conf manpage also states that 'search' and 'domain' are mutually exclusive
           and that the last one wins. */
        free_domains(domains);
        domains = parse_domains(start);
    }

    fclose(f);

    return domains;
}

#endif

enum nss_status _nss_mdns_gethostbyname2_r(
    const char *name,
    int af,
    struct hostent * result,
    char *buffer,
    size_t buflen,
    int *errnop,
    int *h_errnop) {

    struct userdata u;
    enum nss_status status = NSS_STATUS_UNAVAIL;
    int i;
    size_t address_length, l, idx, astart;
    void (*ipv4_func)(const ipv4_address_t *ipv4, void *userdata);
    void (*ipv6_func)(const ipv6_address_t *ipv6, void *userdata);
    int name_allowed;

#ifdef ENABLE_AVAHI
    int avahi_works = 1;
    void * data[32];
#endif

#ifdef ENABLE_LEGACY
    int fd = -1;
#endif

/*     DEBUG_TRAP; */

    if (af == AF_UNSPEC)
#ifdef NSS_IPV6_ONLY
        af = AF_INET6;
#else
        af = AF_INET;
#endif

#ifdef NSS_IPV4_ONLY
    if (af != AF_INET) 
#elif NSS_IPV6_ONLY
    if (af != AF_INET6)
#else        
    if (af != AF_INET && af != AF_INET6)
#endif        
    {    
        *errnop = EINVAL;
        *h_errnop = NO_RECOVERY;

        goto finish;
    }

    address_length = af == AF_INET ? sizeof(ipv4_address_t) : sizeof(ipv6_address_t);
    if (buflen <
        sizeof(char*)+    /* alias names */
        strlen(name)+1)  {   /* official name */
        
        *errnop = ERANGE;
        *h_errnop = NO_RECOVERY;
        status = NSS_STATUS_TRYAGAIN;
        
        goto finish;
    }
    
    u.count = 0;
    u.data_len = 0;

#ifdef NSS_IPV6_ONLY
    ipv4_func = NULL;
#else
    ipv4_func = af == AF_INET ? ipv4_callback : NULL;
#endif    

#ifdef NSS_IPV4_ONLY
    ipv6_func = NULL;
#else
    ipv6_func = af == AF_INET6 ? ipv6_callback : NULL;
#endif

    name_allowed = verify_name_allowed(name);
    
#ifdef ENABLE_AVAHI

    if (avahi_works && name_allowed) {
        int r;

        if ((r = avahi_resolve_name(af, name, data)) < 0)
            avahi_works = 0;
        else if (r == 0) {
            if (af == AF_INET && ipv4_func)
                ipv4_func((ipv4_address_t*) data, &u);
            if (af == AF_INET6 && ipv6_func)
                ipv6_func((ipv6_address_t*)data, &u);
        } else
            status = NSS_STATUS_NOTFOUND;
    }

#ifdef HONOUR_SEARCH_DOMAINS
    if (u.count == 0 && avahi_works && !ends_with(name, ".")) {
        char **domains;

        if ((domains = get_search_domains())) {
            char **p;
            
            /* Try to concatenate host names */
	    for (p = domains; *p; p++) {
                int fullnamesize;
                char *fullname;
                
	        fullnamesize = strlen(name) + strlen(*p) + 2;

                if (!(fullname = malloc(fullnamesize)))
                    break;
                
		snprintf(fullname, fullnamesize, "%s.%s", name, *p);

                if (verify_name_allowed(fullname)) {
                    int r;

                    r = avahi_resolve_name(af, fullname, data);
                    free(fullname);
                    
                    if (r < 0) {
                        /* Lookup failed */
                        avahi_works = 0;
                        break;
                    } else if (r == 0) {
                        /* Lookup succeeded */
                        if (af == AF_INET && ipv4_func)
                            ipv4_func((ipv4_address_t*) data, &u);
                        if (af == AF_INET6 && ipv6_func)
                            ipv6_func((ipv6_address_t*)data, &u);
                        break;
                    } else
                        /* Lookup suceeded, but nothing found */
                        status = NSS_STATUS_NOTFOUND;
                    
		} else
                    free(fullname);
	    }
            
	    free_domains(domains);
	}
    }
#endif /* HONOUR_SEARCH_DOMAINS */
#endif /* ENABLE_AVAHI */

#if defined(ENABLE_LEGACY) && defined(ENABLE_AVAHI)
    if (u.count == 0 && !avahi_works) 
#endif

#if defined(ENABLE_LEGACY)
    {
        if ((fd = mdns_open_socket()) < 0) {
            *errnop = errno;
            *h_errnop = NO_RECOVERY;
            goto finish;
        }

        if (name_allowed) {
            /* Ignore return value */
            mdns_query_name(fd, name, ipv4_func, ipv6_func, &u);

            if (!u.count)
                status = NSS_STATUS_NOTFOUND;
        }

#ifdef HONOUR_SEARCH_DOMAINS
        if (u.count == 0 && !ends_with(name, ".")) {
            char **domains;
            
            /* Try the search domains if the user did not use a traling '.' */
            
            if ((domains = get_search_domains())) {
                char **p;
                
                for (p = domains; *p; p++) {
                    int fullnamesize = 0;
                    char *fullname = NULL;
                    
                    fullnamesize = strlen(name) + strlen(*p) + 2;
                    if (!(fullname = malloc(fullnamesize)))
                        break;
                    
                    snprintf(fullname, fullnamesize, "%s.%s", name, *p);
                    
                    if (verify_name_allowed(fullname)) {
                        
                        /* Ignore return value */
                        mdns_query_name(fd, fullname, ipv4_func, ipv6_func, &u);
                        
                        if (u.count > 0) {
                            /* We found something, so let's quit */
                            free(fullname);
                            break;
                        } else
                            status = NSS_STATUS_NOTFOUND;

                    }
                    
                    free(fullname);
                }
                
                free_domains(domains);
	    }
        }
#endif /* HONOUR_SEARCH_DOMAINS */
    }
#endif /* ENABLE_LEGACY */

    if (u.count == 0) {
        *errnop = ETIMEDOUT;
        *h_errnop = HOST_NOT_FOUND;
        goto finish;
    }
    
    /* Alias names */
    *((char**) buffer) = NULL;
    result->h_aliases = (char**) buffer;
    idx = sizeof(char*);
    
    /* Official name */
    strcpy(buffer+idx, name); 
    result->h_name = buffer+idx;
    idx += strlen(name)+1;

    ALIGN(idx);
    
    result->h_addrtype = af;
    result->h_length = address_length;
    
    /* Check if there's enough space for the addresses */
    if (buflen < idx+u.data_len+sizeof(char*)*(u.count+1)) {
        *errnop = ERANGE;
        *h_errnop = NO_RECOVERY;
        status = NSS_STATUS_TRYAGAIN;
        goto finish;
    }

    /* Addresses */
    astart = idx;
    l = u.count*address_length;
    memcpy(buffer+astart, &u.data, l);
    /* address_length is a multiple of 32bits, so idx is still aligned
     * correctly */
    idx += l;

    /* Address array address_lenght is always a multiple of 32bits */
    for (i = 0; i < u.count; i++)
        ((char**) (buffer+idx))[i] = buffer+astart+address_length*i;
    ((char**) (buffer+idx))[i] = NULL;
    result->h_addr_list = (char**) (buffer+idx);

    status = NSS_STATUS_SUCCESS;
    
finish:
#ifdef ENABLE_LEGACY
    if (fd >= 0)
        close(fd);
#endif

    return status;
}

enum nss_status _nss_mdns_gethostbyname_r (
    const char *name,
    struct hostent *result,
    char *buffer,
    size_t buflen,
    int *errnop,
    int *h_errnop) {

    return _nss_mdns_gethostbyname2_r(
        name,
        AF_UNSPEC,
        result,
        buffer,
        buflen,
        errnop,
        h_errnop);
}

enum nss_status _nss_mdns_gethostbyaddr_r(
    const void* addr,
    int len,
    int af,
    struct hostent *result,
    char *buffer,
    size_t buflen,
    int *errnop,
    int *h_errnop) {
    
    struct userdata u;
    enum nss_status status = NSS_STATUS_UNAVAIL;
    int r;
    size_t address_length, idx, astart;
    
#ifdef ENABLE_AVAHI
    char t[256];
#endif
#ifdef ENABLE_LEGACY
    int fd = -1;
#endif

    *errnop = EINVAL;
    *h_errnop = NO_RECOVERY;

    u.count = 0;
    u.data_len = 0;

    /* Check for address types */
    address_length = af == AF_INET ? sizeof(ipv4_address_t) : sizeof(ipv6_address_t);

    if (len < (int) address_length ||
#ifdef NSS_IPV4_ONLY
        af != AF_INET
#elif NSS_IPV6_ONLY
        af != AF_INET6
#else        
        (af != AF_INET && af != AF_INET6)
#endif
        ) {
        *errnop = EINVAL;
        *h_errnop = NO_RECOVERY;

        goto finish;
    }

    /* Check for buffer space */
    if (buflen <
        sizeof(char*)+      /* alias names */
        address_length) {   /* address */
        
        *errnop = ERANGE;
        *h_errnop = NO_RECOVERY;
        status = NSS_STATUS_TRYAGAIN;

        goto finish;
    }

#ifdef MDNS_MINIMAL

    /* Only query for 169.254.0.0/16 IPv4 in minimal mode */
    if ((af == AF_INET && ((ntohl(*(const uint32_t*)  addr) & 0xFFFF0000UL) != 0xA9FE0000UL)) ||
        (af == AF_INET6 && !(((const uint8_t*) addr)[0] == 0xFE && (((const uint8_t*) addr)[1] >> 6) == 2))) {

        *errnop = EINVAL;
        *h_errnop = NO_RECOVERY;

        goto finish;
    }
#endif
    
#ifdef ENABLE_AVAHI
    /* Lookup using Avahi */
    if ((r = avahi_resolve_address(af, addr, t, sizeof(t))) == 0) {
        name_callback(t, &u);
    } else if (r > 0) {
        *errnop = ETIMEDOUT;
        *h_errnop = HOST_NOT_FOUND;
        status = NSS_STATUS_NOTFOUND;
        goto finish;
    } 
#endif

#if defined(ENABLE_AVAHI) && defined(ENABLE_LEGACY)
    else
#endif
    
#ifdef ENABLE_LEGACY
     /* Lookup using legacy mDNS queries */   
     {
        if ((fd = mdns_open_socket()) < 0) {
            *errnop = errno;
            *h_errnop = NO_RECOVERY;
            goto finish;
        }

	r = -1;

#if ! defined(NSS_IPV6_ONLY) && ! defined(NSS_IPV4_ONLY)
        if (af == AF_INET)
#endif
#ifndef NSS_IPV6_ONLY
            r = mdns_query_ipv4(fd, (const ipv4_address_t*) addr, name_callback, &u);
#endif
#if ! defined(NSS_IPV6_ONLY) && ! defined(NSS_IPV4_ONLY)
        else
#endif
#ifndef NSS_IPV4_ONLY
            r = mdns_query_ipv6(fd, (const ipv6_address_t*) addr, name_callback, &u);
#endif
        if (r < 0) {
            *errnop = ETIMEDOUT;
            *h_errnop = HOST_NOT_FOUND;
            status = NSS_STATUS_NOTFOUND;
            goto finish;
        }
    }
#endif /* ENABLE_LEGACY */

    if (u.count == 0) {
        *errnop = ETIMEDOUT;
        *h_errnop = NO_RECOVERY;
        goto finish;
    }

    /* Alias names, assuming buffer starts a nicely aligned offset */
    *((char**) buffer) = NULL;
    result->h_aliases = (char**) buffer;
    idx = sizeof(char*);

    assert(u.count > 0);
    assert(u.data.name[0]);
    
    if (buflen <
        strlen(u.data.name[0])+1+ /* official names */
        sizeof(char*)+ /* alias names */
        address_length+  /* address */
        sizeof(void*)*2 + /* address list */
        sizeof(void*)) {  /* padding to get the alignment right */

        *errnop = ERANGE;
        *h_errnop = NO_RECOVERY;
        status = NSS_STATUS_TRYAGAIN;
        goto finish;
    }
    
    /* Official name */
    strcpy(buffer+idx, u.data.name[0]); 
    result->h_name = buffer+idx;
    idx += strlen(u.data.name[0])+1;
    
    result->h_addrtype = af;
    result->h_length = address_length;

    /* Address */
    astart = idx;
    memcpy(buffer+astart, addr, address_length);
    idx += address_length;

    /* Address array, idx might not be at pointer alignment anymore, so we need
     * to ensure it is*/
    ALIGN(idx);

    ((char**) (buffer+idx))[0] = buffer+astart;
    ((char**) (buffer+idx))[1] = NULL;
    result->h_addr_list = (char**) (buffer+idx);

    status = NSS_STATUS_SUCCESS;
    
finish:
#ifdef ENABLE_LEGACY
    if (fd >= 0)
        close(fd);
#endif

    return status;
}