xref: /onnv-gate/usr/src/lib/libsocket/inet/getnetent_r.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1986-2002 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <ctype.h>
30*0Sstevel@tonic-gate #include <sys/types.h>
31*0Sstevel@tonic-gate #include <sys/socket.h>
32*0Sstevel@tonic-gate #include <netinet/in.h>
33*0Sstevel@tonic-gate #include <arpa/inet.h>
34*0Sstevel@tonic-gate #include <netdb.h>
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <nss_dbdefs.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate static int str2netent(const char *, int, void *, char *, int);
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate static int net_stayopen;
43*0Sstevel@tonic-gate /*
44*0Sstevel@tonic-gate  * Unsynchronized, but it affects only
45*0Sstevel@tonic-gate  * efficiency, not correctness
46*0Sstevel@tonic-gate  */
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
49*0Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate static void
52*0Sstevel@tonic-gate _nss_initf_net(nss_db_params_t *p)
53*0Sstevel@tonic-gate {
54*0Sstevel@tonic-gate 	p->name	= NSS_DBNAM_NETWORKS;
55*0Sstevel@tonic-gate 	p->default_config = NSS_DEFCONF_NETWORKS;
56*0Sstevel@tonic-gate }
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate struct netent *
59*0Sstevel@tonic-gate getnetbyname_r(const char *name, struct netent *result,
60*0Sstevel@tonic-gate 	char *buffer, int buflen)
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 	nss_XbyY_args_t arg;
63*0Sstevel@tonic-gate 	nss_status_t	res;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2netent);
66*0Sstevel@tonic-gate 	arg.key.name	= name;
67*0Sstevel@tonic-gate 	arg.stayopen	= net_stayopen;
68*0Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_net,
69*0Sstevel@tonic-gate 		NSS_DBOP_NETWORKS_BYNAME, &arg);
70*0Sstevel@tonic-gate 	arg.status = res;
71*0Sstevel@tonic-gate 	(void) NSS_XbyY_FINI(&arg);
72*0Sstevel@tonic-gate 	return ((struct netent *)arg.returnval);
73*0Sstevel@tonic-gate }
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate struct netent *
76*0Sstevel@tonic-gate getnetbyaddr_r(long net, int type, struct netent *result,
77*0Sstevel@tonic-gate 	char *buffer, int buflen)
78*0Sstevel@tonic-gate {
79*0Sstevel@tonic-gate 	nss_XbyY_args_t arg;
80*0Sstevel@tonic-gate 	nss_status_t	res;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2netent);
83*0Sstevel@tonic-gate 	arg.key.netaddr.net = (uint32_t)net;
84*0Sstevel@tonic-gate 	arg.key.netaddr.type = type;
85*0Sstevel@tonic-gate 	arg.stayopen	= net_stayopen;
86*0Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_net,
87*0Sstevel@tonic-gate 		NSS_DBOP_NETWORKS_BYADDR, &arg);
88*0Sstevel@tonic-gate 	arg.status = res;
89*0Sstevel@tonic-gate 	(void) NSS_XbyY_FINI(&arg);
90*0Sstevel@tonic-gate 	return ((struct netent *)arg.returnval);
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate int
94*0Sstevel@tonic-gate setnetent(int stay)
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	net_stayopen |= stay;	/* === Or maybe just "=" ? */
97*0Sstevel@tonic-gate 	nss_setent(&db_root, _nss_initf_net, &context);
98*0Sstevel@tonic-gate 	return (0);
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate int
102*0Sstevel@tonic-gate endnetent()
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	net_stayopen = 0;
105*0Sstevel@tonic-gate 	nss_endent(&db_root, _nss_initf_net, &context);
106*0Sstevel@tonic-gate 	nss_delete(&db_root);
107*0Sstevel@tonic-gate 	return (0);
108*0Sstevel@tonic-gate }
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate struct netent *
111*0Sstevel@tonic-gate getnetent_r(struct netent *result, char *buffer, int buflen)
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate 	nss_XbyY_args_t arg;
114*0Sstevel@tonic-gate 	nss_status_t	res;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2netent);
117*0Sstevel@tonic-gate 	/* No stayopen flag;  of course you stay open for iteration */
118*0Sstevel@tonic-gate 	res = nss_getent(&db_root, _nss_initf_net, &context, &arg);
119*0Sstevel@tonic-gate 	arg.status = res;
120*0Sstevel@tonic-gate 	(void) NSS_XbyY_FINI(&arg);
121*0Sstevel@tonic-gate 	return ((struct netent *)arg.returnval);
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate /*
125*0Sstevel@tonic-gate  * Return values: 0 = success, 1 = parse error, 2 = erange ...
126*0Sstevel@tonic-gate  * The structure pointer passed in is a structure in the caller's space
127*0Sstevel@tonic-gate  * wherein the field pointers would be set to areas in the buffer if
128*0Sstevel@tonic-gate  * need be. instring and buffer should be separate areas.
129*0Sstevel@tonic-gate  */
130*0Sstevel@tonic-gate static int
131*0Sstevel@tonic-gate str2netent(const char *instr, int lenstr,
132*0Sstevel@tonic-gate 	void *ent /* really (struct netnet *) */, char *buffer, int buflen)
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate 	struct netent	*net	= (struct netent *)ent;
135*0Sstevel@tonic-gate 	const char	*p, *numstart, *limit, *namestart;
136*0Sstevel@tonic-gate 	int		namelen = 0;
137*0Sstevel@tonic-gate 	ptrdiff_t	numlen;
138*0Sstevel@tonic-gate 	char		numbuf[16];
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	if ((instr >= buffer && (buffer + buflen) > instr) ||
141*0Sstevel@tonic-gate 	    (buffer >= instr && (instr + lenstr) > buffer)) {
142*0Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	p = instr;
146*0Sstevel@tonic-gate 	limit = p + lenstr;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
149*0Sstevel@tonic-gate 		p++;
150*0Sstevel@tonic-gate 	}
151*0Sstevel@tonic-gate 	namestart = p;
152*0Sstevel@tonic-gate 	while (p < limit && !isspace(*p)) {
153*0Sstevel@tonic-gate 		p++;		/* Skip over the canonical name */
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 	namelen = (int)(p - namestart);
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	if (buflen <= namelen) { /* not enough buffer */
158*0Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 	(void) memcpy(buffer, namestart, namelen);
161*0Sstevel@tonic-gate 	buffer[namelen] = '\0';
162*0Sstevel@tonic-gate 	net->n_name = buffer;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
165*0Sstevel@tonic-gate 		p++;
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate 	if (p >= limit) {
168*0Sstevel@tonic-gate 		/* Syntax error -- no net number */
169*0Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
170*0Sstevel@tonic-gate 	}
171*0Sstevel@tonic-gate 	numstart = p;
172*0Sstevel@tonic-gate 	do {
173*0Sstevel@tonic-gate 		p++;		/* Find the end of the net number */
174*0Sstevel@tonic-gate 	} while (p < limit && !isspace(*p));
175*0Sstevel@tonic-gate 	numlen = p - numstart;
176*0Sstevel@tonic-gate 	if (numlen >= (ptrdiff_t)sizeof (numbuf)) {
177*0Sstevel@tonic-gate 		/* Syntax error -- supposed number is too long */
178*0Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 	(void) memcpy(numbuf, numstart, numlen);
181*0Sstevel@tonic-gate 	numbuf[numlen] = '\0';
182*0Sstevel@tonic-gate 	net->n_net = inet_network(numbuf);
183*0Sstevel@tonic-gate 	if (net->n_net == (in_addr_t)-1) {
184*0Sstevel@tonic-gate 		/* inet_network failed to parse the string */
185*0Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	net->n_addrtype = AF_INET;
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
191*0Sstevel@tonic-gate 		p++;
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate 	/*
194*0Sstevel@tonic-gate 	 * Although nss_files_XY_all calls us with # stripped,
195*0Sstevel@tonic-gate 	 * we should be able to deal with it here in order to
196*0Sstevel@tonic-gate 	 * be more useful.
197*0Sstevel@tonic-gate 	 */
198*0Sstevel@tonic-gate 	if (p >= limit || *p == '#') { /* no aliases, no problem */
199*0Sstevel@tonic-gate 		char **ptr;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 		ptr = (char **)ROUND_UP(buffer + namelen + 1,
202*0Sstevel@tonic-gate 							sizeof (char *));
203*0Sstevel@tonic-gate 		if ((char *)ptr >= buffer + buflen) {
204*0Sstevel@tonic-gate 			net->n_aliases = 0; /* hope they don't try to peek in */
205*0Sstevel@tonic-gate 			return (NSS_STR_PARSE_ERANGE);
206*0Sstevel@tonic-gate 		} else {
207*0Sstevel@tonic-gate 			*ptr = 0;
208*0Sstevel@tonic-gate 			net->n_aliases = ptr;
209*0Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
210*0Sstevel@tonic-gate 		}
211*0Sstevel@tonic-gate 	}
212*0Sstevel@tonic-gate 	net->n_aliases = _nss_netdb_aliases(p, lenstr - (int)(p - instr),
213*0Sstevel@tonic-gate 				buffer + namelen + 1, buflen - namelen - 1);
214*0Sstevel@tonic-gate 	return (NSS_STR_PARSE_SUCCESS);
215*0Sstevel@tonic-gate }
216