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