~ubuntu-branches/ubuntu/precise/nss-mdns/precise

« back to all changes in this revision

Viewing changes to src/bsdnss.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2007-05-15 16:27:20 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070515162720-iugp5xi9dhkwxbk0
Tags: 0.10-0.1ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/postinst: Automatically transition the dapper default hosts: line
    'files dns mdns' to the recommended value, since dns before mdns is
    inefficient, a DNS server DoS, and an information leak. This needs to be
    kept until the next LTS release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: bsdnss.c 114 2007-02-08 16:42:22Z lennart $ */
 
2
 
 
3
/***
 
4
    This file is part of nss-mdns.
 
5
 
6
    nss-mdns is free software; you can redistribute it and/or modify
 
7
    it under the terms of the GNU Lesser General Public License as published
 
8
    by the Free Software Foundation; either version 2 of the License,
 
9
    or (at your option) any later version.
 
10
 
11
    nss-mdns is distributed in the hope that it will be useful, but1
 
12
    WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
14
    General Public License for more details.
 
15
 
16
    You should have received a copy of the GNU Lesser General Public License
 
17
    along with nss-mdns; if not, write to the Free Software
 
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 
19
    USA.
 
20
***/
 
21
 
 
22
/* Original author: Bruce M. Simpson <bms@FreeBSD.org> */
 
23
 
 
24
#ifdef HAVE_CONFIG_H
 
25
#include <config.h>
 
26
#endif
 
27
 
 
28
#include <sys/param.h>
 
29
#include <sys/types.h>
 
30
#include <sys/time.h>
 
31
#include <sys/uio.h>
 
32
#include <sys/socket.h>
 
33
#include <sys/ktrace.h>
 
34
 
 
35
#include <stdarg.h>
 
36
#include <stdlib.h>
 
37
#include <stdio.h>
 
38
#include <string.h>
 
39
 
 
40
#include <errno.h>
 
41
#include <pwd.h>
 
42
#include <grp.h>
 
43
#include <nss.h>
 
44
 
 
45
#include <netinet/in.h>
 
46
#include <netdb.h>
 
47
 
 
48
#include "config.h"
 
49
 
 
50
#ifdef MDNS_MINIMAL
 
51
/*
 
52
 * FreeBSD support prefers Avahi.
 
53
 */
 
54
#endif
 
55
 
 
56
#if defined(NSS_IPV4_ONLY) || defined(NSS_IPV6_ONLY)
 
57
/*
 
58
 * FreeBSD's libc is always built with IPv4 support.
 
59
 * There is no way of telling at compile time with a define if libc
 
60
 * was built with -DINET6 or not; a configure test would be required.
 
61
 * Therefore, distinguishing between the two makes no sense.
 
62
 */
 
63
#define NO_BUILD_BSD_NSS
 
64
#endif
 
65
 
 
66
#ifndef NO_BUILD_BSD_NSS
 
67
/*
 
68
 * To turn on utrace() records, compile with -DDEBUG_UTRACE.
 
69
 */
 
70
#ifdef DEBUG_UTRACE
 
71
#define _NSS_UTRACE(msg)                                                \
 
72
        do {                                                            \
 
73
                static const char __msg[] = msg ;                       \
 
74
                (void)utrace(__msg, sizeof(__msg));                     \
 
75
        } while (0)
 
76
#else
 
77
#define _NSS_UTRACE(msg)
 
78
#endif
 
79
 
 
80
ns_mtab *nss_module_register(const char *source, unsigned int *mtabsize,
 
81
                             nss_module_unregister_fn *unreg);
 
82
 
 
83
extern enum nss_status _nss_mdns_gethostbyname_r (const char *name, struct hostent * result,
 
84
                           char *buffer, size_t buflen, int *errnop,
 
85
                           int *h_errnop);
 
86
 
 
87
extern enum nss_status _nss_mdns_gethostbyname2_r (const char *name, int af, struct hostent * result,
 
88
                            char *buffer, size_t buflen, int *errnop,
 
89
                            int *h_errnop);
 
90
extern enum nss_status _nss_mdns_gethostbyaddr_r (struct in_addr * addr, int len, int type,
 
91
                           struct hostent * result, char *buffer,
 
92
                           size_t buflen, int *errnop, int *h_errnop);
 
93
extern enum nss_status _nss_mdns4_gethostbyname_r (const char *name, struct hostent * result,
 
94
                           char *buffer, size_t buflen, int *errnop,
 
95
                           int *h_errnop);
 
96
 
 
97
extern enum nss_status _nss_mdns4_gethostbyname2_r (const char *name, int af, struct hostent * result,
 
98
                            char *buffer, size_t buflen, int *errnop,
 
99
                            int *h_errnop);
 
100
extern enum nss_status _nss_mdns4_gethostbyaddr_r (struct in_addr * addr, int len, int type,
 
101
                           struct hostent * result, char *buffer,
 
102
                           size_t buflen, int *errnop, int *h_errnop);
 
103
extern enum nss_status _nss_mdns6_gethostbyname_r (const char *name, struct hostent * result,
 
104
                           char *buffer, size_t buflen, int *errnop,
 
105
                           int *h_errnop);
 
106
 
 
107
extern enum nss_status _nss_mdns6_gethostbyname2_r (const char *name, int af, struct hostent * result,
 
108
                            char *buffer, size_t buflen, int *errnop,
 
109
                            int *h_errnop);
 
110
extern enum nss_status _nss_mdns6_gethostbyaddr_r (struct in_addr * addr, int len, int type,
 
111
                           struct hostent * result, char *buffer,
 
112
                           size_t buflen, int *errnop, int *h_errnop);
 
113
 
 
114
typedef enum nss_status         (*_bsd_nsstub_fn_t)(const char *, struct hostent *, char *, size_t, int *, int *);
 
115
 
 
116
/* XXX: FreeBSD 5.x is not supported. */
 
117
static NSS_METHOD_PROTOTYPE(__nss_bsdcompat_getaddrinfo);
 
118
static NSS_METHOD_PROTOTYPE(__nss_bsdcompat_gethostbyaddr_r);
 
119
static NSS_METHOD_PROTOTYPE(__nss_bsdcompat_gethostbyname2_r);
 
120
static NSS_METHOD_PROTOTYPE(__nss_bsdcompat_ghbyaddr);
 
121
static NSS_METHOD_PROTOTYPE(__nss_bsdcompat_ghbyname);
 
122
 
 
123
static ns_mtab methods[] = {
 
124
    /* database, name, method, mdata */
 
125
    { NSDB_HOSTS, "getaddrinfo", __nss_bsdcompat_getaddrinfo, NULL },
 
126
    { NSDB_HOSTS, "gethostbyaddr_r", __nss_bsdcompat_gethostbyaddr_r, NULL },
 
127
    { NSDB_HOSTS, "gethostbyname2_r", __nss_bsdcompat_gethostbyname2_r, NULL },
 
128
    { NSDB_HOSTS, "ghbyaddr", __nss_bsdcompat_ghbyaddr, NULL },
 
129
    { NSDB_HOSTS, "ghbyname", __nss_bsdcompat_ghbyname, NULL },
 
130
};
 
131
 
 
132
ns_mtab *
 
133
nss_module_register(const char *source, unsigned int *mtabsize,
 
134
    nss_module_unregister_fn *unreg)
 
135
{
 
136
 
 
137
        *mtabsize = sizeof(methods)/sizeof(methods[0]);
 
138
        *unreg = NULL;
 
139
        return (methods);
 
140
}
 
141
 
 
142
/*
 
143
 * Calling convention:
 
144
 * ap: const char *name (optional), struct addrinfo *pai (hints, optional)
 
145
 * retval: struct addrinfo **
 
146
 *
 
147
 * TODO: Map all returned hostents, not just the first match.
 
148
 *
 
149
 * name must always be specified by libc; pai is allocated
 
150
 * by libc and must always be specified.
 
151
 *
 
152
 * We can malloc() addrinfo instances and hang them off ai->next;
 
153
 * canonnames may also be malloc()'d.
 
154
 * libc is responsible for mapping our ns error return to gai_strerror().
 
155
 *
 
156
 * libc calls us only to look up qualified hostnames. We don't need to
 
157
 * worry about port numbers; libc will call getservbyname() and explore
 
158
 * the appropriate maps configured in nsswitch.conf(5).
 
159
 *
 
160
 * _errno and _h_errno are unused by getaddrinfo(), as it is
 
161
 * [mostly] OS independent interface implemented by Win32.
 
162
 */
 
163
static int
 
164
__nss_bsdcompat_getaddrinfo(void *retval, void *mdata __unused, va_list ap)
 
165
{
 
166
        struct addrinfo          sentinel;
 
167
        struct addrinfo         *ai;
 
168
        char                    *buffer;
 
169
        void                    *cbufp; /* buffer handed to libc */
 
170
        char                    *hap;
 
171
        struct hostent          *hp;
 
172
        void                    *mbufp; /* buffer handed to mdns */
 
173
        const char              *name;
 
174
        const struct addrinfo   *pai;
 
175
        struct sockaddr         *psa;   /* actually *sockaddr_storage */
 
176
        struct addrinfo         **resultp;
 
177
        int                      _errno;
 
178
        int                      _h_errno;
 
179
        size_t                   mbuflen = 1024;
 
180
        enum nss_status          status;
 
181
 
 
182
        _NSS_UTRACE("__nss_bsdcompat_getaddrinfo: called");
 
183
 
 
184
        _h_errno = _errno = 0;
 
185
        status = NSS_STATUS_UNAVAIL;
 
186
 
 
187
        name = va_arg(ap, const char *);
 
188
        pai = va_arg(ap, struct addrinfo *);
 
189
        resultp = (struct addrinfo **)retval;
 
190
 
 
191
        /* XXX: Will be used to hang off multiple matches later. */
 
192
        memset(&sentinel, 0, sizeof(sentinel));
 
193
 
 
194
        if (name == NULL || pai == NULL) {
 
195
                *resultp = sentinel.ai_next;
 
196
                return (NS_UNAVAIL);
 
197
        }
 
198
 
 
199
        mbufp = malloc((sizeof(struct hostent) + mbuflen));
 
200
        if (mbufp == NULL) {
 
201
                *resultp = sentinel.ai_next;
 
202
                return (NS_UNAVAIL);
 
203
        }
 
204
        hp = (struct hostent *)mbufp;
 
205
        buffer = (char *)(hp + 1);
 
206
 
 
207
        cbufp = malloc(sizeof(struct addrinfo) +
 
208
            sizeof(struct sockaddr_storage));
 
209
        if (cbufp == NULL) {
 
210
                free(mbufp);
 
211
                *resultp = sentinel.ai_next;
 
212
                return (NS_UNAVAIL);
 
213
        }
 
214
        ai = (struct addrinfo *)cbufp;
 
215
        psa = (struct sockaddr *)(ai + 1);
 
216
 
 
217
        /*
 
218
         * 1. Select which function to call based on the address family.
 
219
         * 2. Map hostent to addrinfo.
 
220
         * 3. Hand-off buffer to libc.
 
221
         */
 
222
        switch (pai->ai_family) {
 
223
        case AF_UNSPEC:
 
224
                status = _nss_mdns_gethostbyname_r(name, hp, buffer, mbuflen,
 
225
                                                   &_errno, &_h_errno);
 
226
                break;
 
227
        case AF_INET:
 
228
                status = _nss_mdns4_gethostbyname_r(name, hp, buffer, mbuflen,
 
229
                                                    &_errno, &_h_errno);
 
230
                break;
 
231
        case AF_INET6:
 
232
                status = _nss_mdns6_gethostbyname_r(name, hp, buffer, mbuflen,
 
233
                                                    &_errno, &_h_errno);
 
234
                break;
 
235
        default:
 
236
                break;
 
237
        }
 
238
        status = __nss_compat_result(status, _errno);
 
239
 
 
240
        if (status == NS_SUCCESS) {
 
241
                memset(ai, 0, sizeof(struct addrinfo));
 
242
                ai->ai_flags = pai->ai_flags;
 
243
                ai->ai_socktype = pai->ai_socktype;
 
244
                ai->ai_protocol = pai->ai_protocol;
 
245
                ai->ai_family = hp->h_addrtype;
 
246
                memset(psa, 0, sizeof(struct sockaddr_storage));
 
247
                psa->sa_len = ai->ai_addrlen;
 
248
                psa->sa_family = ai->ai_family;
 
249
                ai->ai_addr = psa;
 
250
                hap = hp->h_addr_list[0];
 
251
                switch (ai->ai_family) {
 
252
                case AF_INET:
 
253
                        ai->ai_addrlen = sizeof(struct sockaddr_in);
 
254
                        memcpy(&((struct sockaddr_in *)psa)->sin_addr, hap,
 
255
                            ai->ai_addrlen);
 
256
                        break;
 
257
                case AF_INET6:
 
258
                        ai->ai_addrlen = sizeof(struct sockaddr_in6);
 
259
                        memcpy(&((struct sockaddr_in6 *)psa)->sin6_addr, hap,
 
260
                            ai->ai_addrlen);
 
261
                        break;
 
262
                default:
 
263
                        ai->ai_addrlen = sizeof(struct sockaddr_storage);
 
264
                        memcpy(psa->sa_data, hap, ai->ai_addrlen);
 
265
                }
 
266
                sentinel.ai_next = ai;
 
267
                free(mbufp);
 
268
        }
 
269
 
 
270
        if (sentinel.ai_next == NULL) {
 
271
                free(cbufp);
 
272
                free(mbufp);
 
273
        }
 
274
 
 
275
        *resultp = sentinel.ai_next;
 
276
        return (status);
 
277
}
 
278
 
 
279
/*
 
280
 * Calling convention:
 
281
 * ap: const u_char *uaddr, socklen_t len, int af, struct hostent *hp,
 
282
 *     char *buf, size_t buflen, int ret_errno, int *h_errnop
 
283
 * retval: should be set to NULL or hp passed in
 
284
 */
 
285
static int
 
286
__nss_bsdcompat_gethostbyaddr_r(void *retval, void *mdata __unused, va_list ap)
 
287
{
 
288
        void            *addr;
 
289
        char            *buf;
 
290
        int             *h_errnop;
 
291
        struct hostent  *hp;
 
292
        struct hostent  **resultp;
 
293
        int              af;
 
294
        size_t           buflen;
 
295
        int              len;
 
296
        int              ret_errno;
 
297
        enum nss_status  status;
 
298
 
 
299
        addr = va_arg(ap, void *);
 
300
        len = va_arg(ap, socklen_t);
 
301
        af = va_arg(ap, int);
 
302
        hp = va_arg(ap, struct hostent *);
 
303
        buf = va_arg(ap, char *);
 
304
        buflen = va_arg(ap, size_t);
 
305
        ret_errno = va_arg(ap, int);
 
306
        h_errnop = va_arg(ap, int *);
 
307
        resultp = (struct hostent **)retval;
 
308
 
 
309
        *resultp = NULL;
 
310
        status = _nss_mdns_gethostbyaddr_r(addr, len, af, hp, buf, buflen,
 
311
            &ret_errno, h_errnop);
 
312
 
 
313
        status = __nss_compat_result(status, *h_errnop);
 
314
        if (status == NS_SUCCESS)
 
315
                *resultp = hp;
 
316
        return (status);
 
317
}
 
318
 
 
319
/*
 
320
 * Calling convention:
 
321
 * ap: const char *name, int af, struct hostent *hp, char *buf,
 
322
 *     size_t buflen, int ret_errno, int *h_errnop
 
323
 * retval is a struct hostent **result passed in by the libc client,
 
324
 * which is responsible for allocating storage.
 
325
 */
 
326
static int
 
327
__nss_bsdcompat_gethostbyname2_r(void *retval, void *mdata __unused,
 
328
    va_list ap)
 
329
{
 
330
        char            *buf;
 
331
        const char      *name;
 
332
        int             *h_errnop;
 
333
        struct hostent  *hp;
 
334
        struct hostent  **resultp;
 
335
        int              af;
 
336
        size_t           buflen;
 
337
        int              ret_errno;
 
338
        enum nss_status  status;
 
339
 
 
340
        name = va_arg(ap, char *);
 
341
        af = va_arg(ap, int);
 
342
        hp = va_arg(ap, struct hostent *);
 
343
        buf = va_arg(ap, char *);
 
344
        buflen = va_arg(ap, size_t);
 
345
        ret_errno = va_arg(ap, int);
 
346
        h_errnop = va_arg(ap, int *);
 
347
        resultp = (struct hostent **)retval;
 
348
 
 
349
        *resultp = NULL;
 
350
        if (hp == NULL)
 
351
                return (NS_UNAVAIL);
 
352
 
 
353
        status = _nss_mdns_gethostbyname2_r(name, af, hp, buf, buflen,
 
354
            &ret_errno, h_errnop);
 
355
 
 
356
        status = __nss_compat_result(status, *h_errnop);
 
357
        if (status == NS_SUCCESS)
 
358
                *resultp = hp;
 
359
        return (status);
 
360
}
 
361
 
 
362
/*
 
363
 * Used by getipnodebyaddr(3).
 
364
 *
 
365
 * Calling convention:
 
366
 * ap: struct in[6]_addr *src, size_t len, int af, int *errp
 
367
 * retval: pointer to a pointer to an uninitialized struct hostent,
 
368
 * in which should be returned a single pointer to on-heap storage.
 
369
 *
 
370
 * This function is responsible for allocating on-heap storage.
 
371
 * The caller is responsible for calling freehostent() on the returned
 
372
 * storage.
 
373
 */
 
374
static int
 
375
__nss_bsdcompat_ghbyaddr(void *retval, void *mdata __unused, va_list ap)
 
376
{
 
377
        char            *buffer;
 
378
        void            *bufp;
 
379
        int             *errp;
 
380
        struct hostent  *hp;
 
381
        struct hostent  **resultp;
 
382
        void            *src;
 
383
        int              af;
 
384
        size_t           buflen = 1024;
 
385
        size_t           len;
 
386
        int              h_errnop;
 
387
        enum nss_status  status;
 
388
 
 
389
        src = va_arg(ap, void *);
 
390
        len = va_arg(ap, size_t);
 
391
        af = va_arg(ap, int);
 
392
        errp = va_arg(ap, int *);
 
393
        resultp = (struct hostent **)retval;
 
394
 
 
395
        _NSS_UTRACE("__nss_bsdcompat_ghbyaddr: called");
 
396
 
 
397
        bufp = malloc((sizeof(struct hostent) + buflen));
 
398
        if (bufp == NULL) {
 
399
                *resultp = NULL;
 
400
                return (NS_UNAVAIL);
 
401
        }
 
402
        hp = (struct hostent *)bufp;
 
403
        buffer = (char *)(hp + 1);
 
404
 
 
405
        status = _nss_mdns_gethostbyaddr_r(src, len, af, hp, buffer,
 
406
            buflen, errp, &h_errnop);
 
407
 
 
408
        status = __nss_compat_result(status, *errp);
 
409
        if (status != NS_SUCCESS) {
 
410
                free(bufp);
 
411
                hp = NULL;
 
412
        }
 
413
        *resultp = hp;
 
414
        return (status);
 
415
}
 
416
 
 
417
/*
 
418
 * Used by getipnodebyname(3).
 
419
 *
 
420
 * Calling convention:
 
421
 * ap: const char *name, int af, int *errp
 
422
 * retval: pointer to a pointer to an uninitialized struct hostent.
 
423
 *
 
424
 * This function is responsible for allocating on-heap storage.
 
425
 * The caller is responsible for calling freehostent() on the returned
 
426
 * storage.
 
427
 */
 
428
static int
 
429
__nss_bsdcompat_ghbyname(void *retval, void *mdata __unused, va_list ap)
 
430
{
 
431
        char            *buffer;
 
432
        void            *bufp;
 
433
        int             *errp;
 
434
        struct hostent  *hp;
 
435
        struct hostent  **resultp;
 
436
        char            *name;
 
437
        int              af;
 
438
        size_t           buflen = 1024;
 
439
        int              h_errnop;
 
440
        enum nss_status  status;
 
441
 
 
442
        name = va_arg(ap, char *);
 
443
        af = va_arg(ap, int);
 
444
        errp = va_arg(ap, int *);
 
445
        resultp = (struct hostent **)retval;
 
446
 
 
447
        bufp = malloc((sizeof(struct hostent) + buflen));
 
448
        if (bufp == NULL) {
 
449
                *resultp = NULL;
 
450
                return (NS_UNAVAIL);
 
451
        }
 
452
        hp = (struct hostent *)bufp;
 
453
        buffer = (char *)(hp + 1);
 
454
 
 
455
        status = _nss_mdns_gethostbyname_r(name, hp, buffer, buflen, errp,
 
456
            &h_errnop);
 
457
 
 
458
        status = __nss_compat_result(status, *errp);
 
459
        if (status != NS_SUCCESS) {
 
460
                free(bufp);
 
461
                hp = NULL;
 
462
        }
 
463
        *resultp = hp;
 
464
        return (status);
 
465
}
 
466
 
 
467
#endif /* !NO_BUILD_BSD_NSS */