1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996,1999 by Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: nis_pr.c,v 1.4 2005/04/27 04:56:33 sra Exp $";
20 #endif
21 
22 /* Imports */
23 
24 #include "port_before.h"
25 
26 #ifndef WANT_IRS_NIS
27 static int __bind_irs_nis_unneeded;
28 #else
29 
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
33 #include <resolv.h>
34 #ifdef T_NULL
35 #undef T_NULL			/* Silence re-definition warning of T_NULL. */
36 #endif
37 #include <rpc/rpc.h>
38 #include <rpc/xdr.h>
39 #include <rpcsvc/yp_prot.h>
40 #include <rpcsvc/ypclnt.h>
41 
42 #include <stdio.h>
43 #include <string.h>
44 #include <netdb.h>
45 #include <ctype.h>
46 #include <stdlib.h>
47 #include <errno.h>
48 
49 #include <isc/memcluster.h>
50 #include <irs.h>
51 
52 #include "port_after.h"
53 
54 #include "irs_p.h"
55 #include "nis_p.h"
56 
57 /* Definitions */
58 
59 struct pvt {
60 	int		needrewind;
61 	char *		nis_domain;
62 	char *		curkey_data;
63 	int		curkey_len;
64 	char *		curval_data;
65 	int		curval_len;
66 	struct protoent	proto;
67 	char *		prbuf;
68 };
69 
70 enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 };
71 
72 static /*const*/ char protocols_byname[] =	"protocols.byname";
73 static /*const*/ char protocols_bynumber[] =	"protocols.bynumber";
74 
75 /* Forward */
76 
77 static void 			pr_close(struct irs_pr *);
78 static struct protoent *	pr_byname(struct irs_pr *, const char *);
79 static struct protoent *	pr_bynumber(struct irs_pr *, int);
80 static struct protoent *	pr_next(struct irs_pr *);
81 static void			pr_rewind(struct irs_pr *);
82 static void			pr_minimize(struct irs_pr *);
83 
84 static struct protoent *	makeprotoent(struct irs_pr *this);
85 static void			nisfree(struct pvt *, enum do_what);
86 
87 /* Public */
88 
89 struct irs_pr *
irs_nis_pr(struct irs_acc * this)90 irs_nis_pr(struct irs_acc *this) {
91 	struct irs_pr *pr;
92 	struct pvt *pvt;
93 
94 	if (!(pr = memget(sizeof *pr))) {
95 		errno = ENOMEM;
96 		return (NULL);
97 	}
98 	memset(pr, 0x5e, sizeof *pr);
99 	if (!(pvt = memget(sizeof *pvt))) {
100 		memput(pr, sizeof *pr);
101 		errno = ENOMEM;
102 		return (NULL);
103 	}
104 	memset(pvt, 0, sizeof *pvt);
105 	pvt->needrewind = 1;
106 	pvt->nis_domain = ((struct nis_p *)this->private)->domain;
107 	pr->private = pvt;
108 	pr->byname = pr_byname;
109 	pr->bynumber = pr_bynumber;
110 	pr->next = pr_next;
111 	pr->rewind = pr_rewind;
112 	pr->close = pr_close;
113 	pr->minimize = pr_minimize;
114 	pr->res_get = NULL;
115 	pr->res_set = NULL;
116 	return (pr);
117 }
118 
119 /* Methods. */
120 
121 static void
pr_close(struct irs_pr * this)122 pr_close(struct irs_pr *this) {
123 	struct pvt *pvt = (struct pvt *)this->private;
124 
125 	nisfree(pvt, do_all);
126 	if (pvt->proto.p_aliases)
127 		free(pvt->proto.p_aliases);
128 	if (pvt->prbuf)
129 		free(pvt->prbuf);
130 	memput(pvt, sizeof *pvt);
131 	memput(this, sizeof *this);
132 }
133 
134 static struct protoent *
pr_byname(struct irs_pr * this,const char * name)135 pr_byname(struct irs_pr *this, const char *name) {
136 	struct pvt *pvt = (struct pvt *)this->private;
137 	int r;
138 	char *tmp;
139 
140 	nisfree(pvt, do_val);
141 	DE_CONST(name, tmp);
142 	r = yp_match(pvt->nis_domain, protocols_byname, tmp,
143 		     strlen(tmp), &pvt->curval_data, &pvt->curval_len);
144 	if (r != 0) {
145 		errno = ENOENT;
146 		return (NULL);
147 	}
148 	return (makeprotoent(this));
149 }
150 
151 static struct protoent *
pr_bynumber(struct irs_pr * this,int num)152 pr_bynumber(struct irs_pr *this, int num) {
153 	struct pvt *pvt = (struct pvt *)this->private;
154 	char tmp[sizeof "-4294967295"];
155 	int r;
156 
157 	nisfree(pvt, do_val);
158 	(void) sprintf(tmp, "%d", num);
159 	r = yp_match(pvt->nis_domain, protocols_bynumber, tmp, strlen(tmp),
160 		     &pvt->curval_data, &pvt->curval_len);
161 	if (r != 0) {
162 		errno = ENOENT;
163 		return (NULL);
164 	}
165 	return (makeprotoent(this));
166 }
167 
168 static struct protoent *
pr_next(struct irs_pr * this)169 pr_next(struct irs_pr *this) {
170 	struct pvt *pvt = (struct pvt *)this->private;
171 	struct protoent *rval;
172 	int r;
173 
174 	do {
175 		if (pvt->needrewind) {
176 			nisfree(pvt, do_all);
177 			r = yp_first(pvt->nis_domain, protocols_bynumber,
178 				     &pvt->curkey_data, &pvt->curkey_len,
179 				     &pvt->curval_data, &pvt->curval_len);
180 			pvt->needrewind = 0;
181 		} else {
182 			char *newkey_data;
183 			int newkey_len;
184 
185 			nisfree(pvt, do_val);
186 			r = yp_next(pvt->nis_domain, protocols_bynumber,
187 				    pvt->curkey_data, pvt->curkey_len,
188 				    &newkey_data, &newkey_len,
189 				    &pvt->curval_data, &pvt->curval_len);
190 			nisfree(pvt, do_key);
191 			pvt->curkey_data = newkey_data;
192 			pvt->curkey_len = newkey_len;
193 		}
194 		if (r != 0) {
195 			errno = ENOENT;
196 			return (NULL);
197 		}
198 		rval = makeprotoent(this);
199 	} while (rval == NULL);
200 	return (rval);
201 }
202 
203 static void
pr_rewind(struct irs_pr * this)204 pr_rewind(struct irs_pr *this) {
205 	struct pvt *pvt = (struct pvt *)this->private;
206 
207 	pvt->needrewind = 1;
208 }
209 
210 static void
pr_minimize(struct irs_pr * this)211 pr_minimize(struct irs_pr *this) {
212 	UNUSED(this);
213 	/* NOOP */
214 }
215 
216 /* Private */
217 
218 static struct protoent *
makeprotoent(struct irs_pr * this)219 makeprotoent(struct irs_pr *this) {
220 	struct pvt *pvt = (struct pvt *)this->private;
221 	char *p, **t;
222 	int n, m;
223 
224 	if (pvt->prbuf)
225 		free(pvt->prbuf);
226 	pvt->prbuf = pvt->curval_data;
227 	pvt->curval_data = NULL;
228 
229 	for (p = pvt->prbuf; *p && *p != '#';)
230 		p++;
231 	while (p > pvt->prbuf && isspace((unsigned char)(p[-1])))
232 		p--;
233 	*p = '\0';
234 
235 	p = pvt->prbuf;
236 	n = m = 0;
237 
238 	pvt->proto.p_name = p;
239 	while (*p && !isspace((unsigned char)*p))
240 		p++;
241 	if (!*p)
242 		return (NULL);
243 	*p++ = '\0';
244 
245 	while (*p && isspace((unsigned char)*p))
246 		p++;
247 	pvt->proto.p_proto = atoi(p);
248 	while (*p && !isspace((unsigned char)*p))
249 		p++;
250 	*p++ = '\0';
251 
252 	while (*p) {
253 		if ((n + 1) >= m || !pvt->proto.p_aliases) {
254 			m += 10;
255 			t = realloc(pvt->proto.p_aliases,
256 				      m * sizeof(char *));
257 			if (!t) {
258 				errno = ENOMEM;
259 				goto cleanup;
260 			}
261 			pvt->proto.p_aliases = t;
262 		}
263 		pvt->proto.p_aliases[n++] = p;
264 		while (*p && !isspace((unsigned char)*p))
265 			p++;
266 		if (*p)
267 			*p++ = '\0';
268 	}
269 	if (!pvt->proto.p_aliases)
270 		pvt->proto.p_aliases = malloc(sizeof(char *));
271 	if (!pvt->proto.p_aliases)
272 		goto cleanup;
273 	pvt->proto.p_aliases[n] = NULL;
274 	return (&pvt->proto);
275 
276  cleanup:
277 	if (pvt->proto.p_aliases) {
278 		free(pvt->proto.p_aliases);
279 		pvt->proto.p_aliases = NULL;
280 	}
281 	if (pvt->prbuf) {
282 		free(pvt->prbuf);
283 		pvt->prbuf = NULL;
284 	}
285 	return (NULL);
286 }
287 
288 static void
nisfree(struct pvt * pvt,enum do_what do_what)289 nisfree(struct pvt *pvt, enum do_what do_what) {
290 	if ((do_what & do_key) && pvt->curkey_data) {
291 		free(pvt->curkey_data);
292 		pvt->curkey_data = NULL;
293 	}
294 	if ((do_what & do_val) && pvt->curval_data) {
295 		free(pvt->curval_data);
296 		pvt->curval_data = NULL;
297 	}
298 }
299 
300 #endif /*WANT_IRS_NIS*/
301 
302 /*! \file */
303