summaryrefslogtreecommitdiffstats
path: root/src/bsdnss.c
blob: cdda366e513ffa046e07b8ba8120efee5ddeda94 (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
/* $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.
***/

/* Original author: Bruce M. Simpson <bms@FreeBSD.org> */

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

#include <sys/param.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <sys/ktrace.h>

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <nss.h>

#include <netinet/in.h>
#include <netdb.h>

#include "config.h"

#ifdef MDNS_MINIMAL
/*
 * FreeBSD support prefers Avahi.
 */
#endif

#if defined(NSS_IPV4_ONLY) || defined(NSS_IPV6_ONLY)
/*
 * FreeBSD's libc is always built with IPv4 support.
 * There is no way of telling at compile time with a define if libc
 * was built with -DINET6 or not; a configure test would be required.
 * Therefore, distinguishing between the two makes no sense.
 */
#define NO_BUILD_BSD_NSS
#endif

#ifndef NO_BUILD_BSD_NSS
/*
 * To turn on utrace() records, compile with -DDEBUG_UTRACE.
 */
#ifdef DEBUG_UTRACE
#define _NSS_UTRACE(msg)						\
	do {								\
		static const char __msg[] = msg ;			\
		(void)utrace(__msg, sizeof(__msg));			\
	} while (0)
#else
#define _NSS_UTRACE(msg)
#endif

ns_mtab *nss_module_register(const char *source, unsigned int *mtabsize,
			     nss_module_unregister_fn *unreg);

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

extern 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);
extern enum nss_status _nss_mdns_gethostbyaddr_r (struct in_addr * addr, int len, int type,
			   struct hostent * result, char *buffer,
			   size_t buflen, int *errnop, int *h_errnop);
extern enum nss_status _nss_mdns4_gethostbyname_r (const char *name, struct hostent * result,
			   char *buffer, size_t buflen, int *errnop,
			   int *h_errnop);

extern enum nss_status _nss_mdns4_gethostbyname2_r (const char *name, int af, struct hostent * result,
			    char *buffer, size_t buflen, int *errnop,
			    int *h_errnop);
extern enum nss_status _nss_mdns4_gethostbyaddr_r (struct in_addr * addr, int len, int type,
			   struct hostent * result, char *buffer,
			   size_t buflen, int *errnop, int *h_errnop);
extern enum nss_status _nss_mdns6_gethostbyname_r (const char *name, struct hostent * result,
			   char *buffer, size_t buflen, int *errnop,
			   int *h_errnop);

extern enum nss_status _nss_mdns6_gethostbyname2_r (const char *name, int af, struct hostent * result,
			    char *buffer, size_t buflen, int *errnop,
			    int *h_errnop);
extern enum nss_status _nss_mdns6_gethostbyaddr_r (struct in_addr * addr, int len, int type,
			   struct hostent * result, char *buffer,
			   size_t buflen, int *errnop, int *h_errnop);

typedef enum nss_status 	(*_bsd_nsstub_fn_t)(const char *, struct hostent *, char *, size_t, int *, int *);

/* XXX: FreeBSD 5.x is not supported. */
static NSS_METHOD_PROTOTYPE(__nss_bsdcompat_getaddrinfo);
static NSS_METHOD_PROTOTYPE(__nss_bsdcompat_gethostbyaddr_r);
static NSS_METHOD_PROTOTYPE(__nss_bsdcompat_gethostbyname2_r);
static NSS_METHOD_PROTOTYPE(__nss_bsdcompat_ghbyaddr);
static NSS_METHOD_PROTOTYPE(__nss_bsdcompat_ghbyname);

static ns_mtab methods[] = {
    /* database, name, method, mdata */
    { NSDB_HOSTS, "getaddrinfo", __nss_bsdcompat_getaddrinfo, NULL },
    { NSDB_HOSTS, "gethostbyaddr_r", __nss_bsdcompat_gethostbyaddr_r, NULL },
    { NSDB_HOSTS, "gethostbyname2_r", __nss_bsdcompat_gethostbyname2_r, NULL },
    { NSDB_HOSTS, "ghbyaddr", __nss_bsdcompat_ghbyaddr, NULL },
    { NSDB_HOSTS, "ghbyname", __nss_bsdcompat_ghbyname, NULL },
};

ns_mtab *
nss_module_register(const char *source, unsigned int *mtabsize,
    nss_module_unregister_fn *unreg)
{

	*mtabsize = sizeof(methods)/sizeof(methods[0]);
	*unreg = NULL;
	return (methods);
}

/*
 * Calling convention:
 * ap: const char *name (optional), struct addrinfo *pai (hints, optional)
 * retval: struct addrinfo **
 *
 * TODO: Map all returned hostents, not just the first match.
 *
 * name must always be specified by libc; pai is allocated
 * by libc and must always be specified.
 *
 * We can malloc() addrinfo instances and hang them off ai->next;
 * canonnames may also be malloc()'d.
 * libc is responsible for mapping our ns error return to gai_strerror().
 *
 * libc calls us only to look up qualified hostnames. We don't need to
 * worry about port numbers; libc will call getservbyname() and explore
 * the appropriate maps configured in nsswitch.conf(5).
 *
 * _errno and _h_errno are unused by getaddrinfo(), as it is
 * [mostly] OS independent interface implemented by Win32.
 */
static int
__nss_bsdcompat_getaddrinfo(void *retval, void *mdata __unused, va_list ap)
{
	struct addrinfo		 sentinel;
	struct addrinfo		*ai;
	char			*buffer;
	void 			*cbufp;	/* buffer handed to libc */
	char			*hap;
	struct hostent		*hp;
	void 			*mbufp;	/* buffer handed to mdns */
	const char 		*name;
	const struct addrinfo	*pai;
	struct sockaddr		*psa;	/* actually *sockaddr_storage */
	struct addrinfo		**resultp;
	int			 _errno;
	int			 _h_errno;
	size_t			 mbuflen = 1024;
	enum nss_status		 status;

	_NSS_UTRACE("__nss_bsdcompat_getaddrinfo: called");

	_h_errno = _errno = 0;
	status = NSS_STATUS_UNAVAIL;

	name = va_arg(ap, const char *);
	pai = va_arg(ap, struct addrinfo *);
	resultp = (struct addrinfo **)retval;

	/* XXX: Will be used to hang off multiple matches later. */
	memset(&sentinel, 0, sizeof(sentinel));

	if (name == NULL || pai == NULL) {
		*resultp = sentinel.ai_next;
		return (NS_UNAVAIL);
	}

	mbufp = malloc((sizeof(struct hostent) + mbuflen));
	if (mbufp == NULL) {
		*resultp = sentinel.ai_next;
		return (NS_UNAVAIL);
	}
	hp = (struct hostent *)mbufp;
	buffer = (char *)(hp + 1);

	cbufp = malloc(sizeof(struct addrinfo) +
	    sizeof(struct sockaddr_storage));
	if (cbufp == NULL) {
		free(mbufp);
		*resultp = sentinel.ai_next;
		return (NS_UNAVAIL);
	}
	ai = (struct addrinfo *)cbufp;
	psa = (struct sockaddr *)(ai + 1);

	/*
	 * 1. Select which function to call based on the address family.
	 * 2. Map hostent to addrinfo.
	 * 3. Hand-off buffer to libc.
	 */
	switch (pai->ai_family) {
	case AF_UNSPEC:
		status = _nss_mdns_gethostbyname_r(name, hp, buffer, mbuflen,
						   &_errno, &_h_errno);
		break;
	case AF_INET:
		status = _nss_mdns4_gethostbyname_r(name, hp, buffer, mbuflen,
						    &_errno, &_h_errno);
		break;
	case AF_INET6:
		status = _nss_mdns6_gethostbyname_r(name, hp, buffer, mbuflen,
						    &_errno, &_h_errno);
		break;
	default:
		break;
	}
	status = __nss_compat_result(status, _errno);

	if (status == NS_SUCCESS) {
		memset(ai, 0, sizeof(struct addrinfo));
		ai->ai_flags = pai->ai_flags;
		ai->ai_socktype = pai->ai_socktype;
		ai->ai_protocol = pai->ai_protocol;
		ai->ai_family = hp->h_addrtype;
		memset(psa, 0, sizeof(struct sockaddr_storage));
		psa->sa_len = ai->ai_addrlen;
		psa->sa_family = ai->ai_family;
		ai->ai_addr = psa;
		hap = hp->h_addr_list[0];
		switch (ai->ai_family) {
		case AF_INET:
			ai->ai_addrlen = sizeof(struct sockaddr_in);
			memcpy(&((struct sockaddr_in *)psa)->sin_addr, hap,
			    ai->ai_addrlen);
			break;
		case AF_INET6:
			ai->ai_addrlen = sizeof(struct sockaddr_in6);
			memcpy(&((struct sockaddr_in6 *)psa)->sin6_addr, hap,
			    ai->ai_addrlen);
			break;
		default:
			ai->ai_addrlen = sizeof(struct sockaddr_storage);
			memcpy(psa->sa_data, hap, ai->ai_addrlen);
		}
		sentinel.ai_next = ai;
		free(mbufp);
	}

	if (sentinel.ai_next == NULL) {
		free(cbufp);
		free(mbufp);
	}

	*resultp = sentinel.ai_next;
	return (status);
}

/*
 * Calling convention:
 * ap: const u_char *uaddr, socklen_t len, int af, struct hostent *hp,
 *     char *buf, size_t buflen, int ret_errno, int *h_errnop
 * retval: should be set to NULL or hp passed in
 */
static int
__nss_bsdcompat_gethostbyaddr_r(void *retval, void *mdata __unused, va_list ap)
{
	void		*addr;
	char		*buf;
	int		*h_errnop;
	struct hostent	*hp;
	struct hostent	**resultp;
	int		 af;
	size_t		 buflen;
	int		 len;
	int		 ret_errno;
	enum nss_status	 status;

	addr = va_arg(ap, void *);
	len = va_arg(ap, socklen_t);
	af = va_arg(ap, int);
	hp = va_arg(ap, struct hostent *);
	buf = va_arg(ap, char *);
	buflen = va_arg(ap, size_t);
	ret_errno = va_arg(ap, int);
	h_errnop = va_arg(ap, int *);
	resultp = (struct hostent **)retval;

	*resultp = NULL;
	status = _nss_mdns_gethostbyaddr_r(addr, len, af, hp, buf, buflen,
	    &ret_errno, h_errnop);

	status = __nss_compat_result(status, *h_errnop);
	if (status == NS_SUCCESS)
		*resultp = hp;
	return (status);
}

/*
 * Calling convention:
 * ap: const char *name, int af, struct hostent *hp, char *buf,
 *     size_t buflen, int ret_errno, int *h_errnop
 * retval is a struct hostent **result passed in by the libc client,
 * which is responsible for allocating storage.
 */
static int
__nss_bsdcompat_gethostbyname2_r(void *retval, void *mdata __unused,
    va_list ap)
{
	char		*buf;
	const char 	*name;
	int		*h_errnop;
	struct hostent	*hp;
	struct hostent	**resultp;
	int		 af;
	size_t		 buflen;
	int		 ret_errno;
	enum nss_status	 status;

	name = va_arg(ap, char *);
	af = va_arg(ap, int);
	hp = va_arg(ap, struct hostent *);
	buf = va_arg(ap, char *);
	buflen = va_arg(ap, size_t);
	ret_errno = va_arg(ap, int);
	h_errnop = va_arg(ap, int *);
	resultp = (struct hostent **)retval;

	*resultp = NULL;
	if (hp == NULL)
		return (NS_UNAVAIL);

	status = _nss_mdns_gethostbyname2_r(name, af, hp, buf, buflen,
	    &ret_errno, h_errnop);

	status = __nss_compat_result(status, *h_errnop);
	if (status == NS_SUCCESS)
		*resultp = hp;
	return (status);
}

/*
 * Used by getipnodebyaddr(3).
 *
 * Calling convention:
 * ap: struct in[6]_addr *src, size_t len, int af, int *errp
 * retval: pointer to a pointer to an uninitialized struct hostent,
 * in which should be returned a single pointer to on-heap storage.
 *
 * This function is responsible for allocating on-heap storage.
 * The caller is responsible for calling freehostent() on the returned
 * storage.
 */
static int
__nss_bsdcompat_ghbyaddr(void *retval, void *mdata __unused, va_list ap)
{
	char		*buffer;
	void 		*bufp;
	int		*errp;
	struct hostent	*hp;
	struct hostent	**resultp;
	void		*src;
	int		 af;
	size_t		 buflen = 1024;
	size_t		 len;
	int		 h_errnop;
	enum nss_status	 status;

	src = va_arg(ap, void *);
	len = va_arg(ap, size_t);
	af = va_arg(ap, int);
	errp = va_arg(ap, int *);
	resultp = (struct hostent **)retval;

	_NSS_UTRACE("__nss_bsdcompat_ghbyaddr: called");

	bufp = malloc((sizeof(struct hostent) + buflen));
	if (bufp == NULL) {
		*resultp = NULL;
		return (NS_UNAVAIL);
	}
	hp = (struct hostent *)bufp;
	buffer = (char *)(hp + 1);

	status = _nss_mdns_gethostbyaddr_r(src, len, af, hp, buffer,
	    buflen, errp, &h_errnop);

	status = __nss_compat_result(status, *errp);
	if (status != NS_SUCCESS) {
		free(bufp);
		hp = NULL;
	}
	*resultp = hp;
	return (status);
}

/*
 * Used by getipnodebyname(3).
 *
 * Calling convention:
 * ap: const char *name, int af, int *errp
 * retval: pointer to a pointer to an uninitialized struct hostent.
 *
 * This function is responsible for allocating on-heap storage.
 * The caller is responsible for calling freehostent() on the returned
 * storage.
 */
static int
__nss_bsdcompat_ghbyname(void *retval, void *mdata __unused, va_list ap)
{
	char		*buffer;
	void 		*bufp;
	int		*errp;
	struct hostent	*hp;
	struct hostent	**resultp;
	char		*name;
	int		 af;
	size_t		 buflen = 1024;
	int		 h_errnop;
	enum nss_status	 status;

	name = va_arg(ap, char *);
	af = va_arg(ap, int);
	errp = va_arg(ap, int *);
	resultp = (struct hostent **)retval;

	bufp = malloc((sizeof(struct hostent) + buflen));
	if (bufp == NULL) {
		*resultp = NULL;
		return (NS_UNAVAIL);
	}
	hp = (struct hostent *)bufp;
	buffer = (char *)(hp + 1);

	status = _nss_mdns_gethostbyname_r(name, hp, buffer, buflen, errp,
	    &h_errnop);

	status = __nss_compat_result(status, *errp);
	if (status != NS_SUCCESS) {
		free(bufp);
		hp = NULL;
	}
	*resultp = hp;
	return (status);
}

#endif /* !NO_BUILD_BSD_NSS */