2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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.
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 $";
24 #include "port_before.h"
27 static int __bind_irs_nis_unneeded;
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
35 #undef T_NULL /* Silence re-definition warning of T_NULL. */
39 #include <rpcsvc/yp_prot.h>
40 #include <rpcsvc/ypclnt.h>
49 #include <isc/memcluster.h>
52 #include "port_after.h"
66 struct protoent proto;
70 enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 };
72 static /*const*/ char protocols_byname[] = "protocols.byname";
73 static /*const*/ char protocols_bynumber[] = "protocols.bynumber";
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 *);
84 static struct protoent * makeprotoent(struct irs_pr *this);
85 static void nisfree(struct pvt *, enum do_what);
90 irs_nis_pr(struct irs_acc *this) {
94 if (!(pr = memget(sizeof *pr))) {
98 memset(pr, 0x5e, sizeof *pr);
99 if (!(pvt = memget(sizeof *pvt))) {
100 memput(pr, sizeof *pr);
104 memset(pvt, 0, sizeof *pvt);
106 pvt->nis_domain = ((struct nis_p *)this->private)->domain;
108 pr->byname = pr_byname;
109 pr->bynumber = pr_bynumber;
111 pr->rewind = pr_rewind;
112 pr->close = pr_close;
113 pr->minimize = pr_minimize;
122 pr_close(struct irs_pr *this) {
123 struct pvt *pvt = (struct pvt *)this->private;
125 nisfree(pvt, do_all);
126 if (pvt->proto.p_aliases)
127 free(pvt->proto.p_aliases);
130 memput(pvt, sizeof *pvt);
131 memput(this, sizeof *this);
134 static struct protoent *
135 pr_byname(struct irs_pr *this, const char *name) {
136 struct pvt *pvt = (struct pvt *)this->private;
140 nisfree(pvt, do_val);
142 r = yp_match(pvt->nis_domain, protocols_byname, tmp,
143 strlen(tmp), &pvt->curval_data, &pvt->curval_len);
148 return (makeprotoent(this));
151 static struct protoent *
152 pr_bynumber(struct irs_pr *this, int num) {
153 struct pvt *pvt = (struct pvt *)this->private;
154 char tmp[sizeof "-4294967295"];
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);
165 return (makeprotoent(this));
168 static struct protoent *
169 pr_next(struct irs_pr *this) {
170 struct pvt *pvt = (struct pvt *)this->private;
171 struct protoent *rval;
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);
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;
198 rval = makeprotoent(this);
199 } while (rval == NULL);
204 pr_rewind(struct irs_pr *this) {
205 struct pvt *pvt = (struct pvt *)this->private;
211 pr_minimize(struct irs_pr *this) {
218 static struct protoent *
219 makeprotoent(struct irs_pr *this) {
220 struct pvt *pvt = (struct pvt *)this->private;
226 pvt->prbuf = pvt->curval_data;
227 pvt->curval_data = NULL;
229 for (p = pvt->prbuf; *p && *p != '#';)
231 while (p > pvt->prbuf && isspace((unsigned char)(p[-1])))
238 pvt->proto.p_name = p;
239 while (*p && !isspace((unsigned char)*p))
245 while (*p && isspace((unsigned char)*p))
247 pvt->proto.p_proto = atoi(p);
248 while (*p && !isspace((unsigned char)*p))
253 if ((n + 1) >= m || !pvt->proto.p_aliases) {
255 t = realloc(pvt->proto.p_aliases,
261 pvt->proto.p_aliases = t;
263 pvt->proto.p_aliases[n++] = p;
264 while (*p && !isspace((unsigned char)*p))
269 if (!pvt->proto.p_aliases)
270 pvt->proto.p_aliases = malloc(sizeof(char *));
271 if (!pvt->proto.p_aliases)
273 pvt->proto.p_aliases[n] = NULL;
274 return (&pvt->proto);
277 if (pvt->proto.p_aliases) {
278 free(pvt->proto.p_aliases);
279 pvt->proto.p_aliases = NULL;
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;
294 if ((do_what & do_val) && pvt->curval_data) {
295 free(pvt->curval_data);
296 pvt->curval_data = NULL;
300 #endif /*WANT_IRS_NIS*/