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