4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid
[] = "Id: nis_pr.c,v 1.4 2005/04/27 04:56:33 sra Exp";
26 #include "port_before.h"
29 static int __bind_irs_nis_unneeded
;
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
37 #undef T_NULL /* Silence re-definition warning of T_NULL. */
41 #include <rpcsvc/yp_prot.h>
42 #include <rpcsvc/ypclnt.h>
51 #include <isc/memcluster.h>
54 #include "port_after.h"
68 struct protoent proto
;
72 enum do_what
{ do_none
= 0x0, do_key
= 0x1, do_val
= 0x2, do_all
= 0x3 };
74 static /*const*/ char protocols_byname
[] = "protocols.byname";
75 static /*const*/ char protocols_bynumber
[] = "protocols.bynumber";
79 static void pr_close(struct irs_pr
*);
80 static struct protoent
* pr_byname(struct irs_pr
*, const char *);
81 static struct protoent
* pr_bynumber(struct irs_pr
*, int);
82 static struct protoent
* pr_next(struct irs_pr
*);
83 static void pr_rewind(struct irs_pr
*);
84 static void pr_minimize(struct irs_pr
*);
86 static struct protoent
* makeprotoent(struct irs_pr
*this);
87 static void nisfree(struct pvt
*, enum do_what
);
92 irs_nis_pr(struct irs_acc
*this) {
96 if (!(pr
= memget(sizeof *pr
))) {
100 memset(pr
, 0x5e, sizeof *pr
);
101 if (!(pvt
= memget(sizeof *pvt
))) {
102 memput(pr
, sizeof *pr
);
106 memset(pvt
, 0, sizeof *pvt
);
108 pvt
->nis_domain
= ((struct nis_p
*)this->private)->domain
;
110 pr
->byname
= pr_byname
;
111 pr
->bynumber
= pr_bynumber
;
113 pr
->rewind
= pr_rewind
;
114 pr
->close
= pr_close
;
115 pr
->minimize
= pr_minimize
;
124 pr_close(struct irs_pr
*this) {
125 struct pvt
*pvt
= (struct pvt
*)this->private;
127 nisfree(pvt
, do_all
);
128 if (pvt
->proto
.p_aliases
)
129 free(pvt
->proto
.p_aliases
);
132 memput(pvt
, sizeof *pvt
);
133 memput(this, sizeof *this);
136 static struct protoent
*
137 pr_byname(struct irs_pr
*this, const char *name
) {
138 struct pvt
*pvt
= (struct pvt
*)this->private;
142 nisfree(pvt
, do_val
);
144 r
= yp_match(pvt
->nis_domain
, protocols_byname
, tmp
,
145 strlen(tmp
), &pvt
->curval_data
, &pvt
->curval_len
);
150 return (makeprotoent(this));
153 static struct protoent
*
154 pr_bynumber(struct irs_pr
*this, int num
) {
155 struct pvt
*pvt
= (struct pvt
*)this->private;
156 char tmp
[sizeof "-4294967295"];
159 nisfree(pvt
, do_val
);
160 (void) sprintf(tmp
, "%d", num
);
161 r
= yp_match(pvt
->nis_domain
, protocols_bynumber
, tmp
, strlen(tmp
),
162 &pvt
->curval_data
, &pvt
->curval_len
);
167 return (makeprotoent(this));
170 static struct protoent
*
171 pr_next(struct irs_pr
*this) {
172 struct pvt
*pvt
= (struct pvt
*)this->private;
173 struct protoent
*rval
;
177 if (pvt
->needrewind
) {
178 nisfree(pvt
, do_all
);
179 r
= yp_first(pvt
->nis_domain
, protocols_bynumber
,
180 &pvt
->curkey_data
, &pvt
->curkey_len
,
181 &pvt
->curval_data
, &pvt
->curval_len
);
187 nisfree(pvt
, do_val
);
188 r
= yp_next(pvt
->nis_domain
, protocols_bynumber
,
189 pvt
->curkey_data
, pvt
->curkey_len
,
190 &newkey_data
, &newkey_len
,
191 &pvt
->curval_data
, &pvt
->curval_len
);
192 nisfree(pvt
, do_key
);
193 pvt
->curkey_data
= newkey_data
;
194 pvt
->curkey_len
= newkey_len
;
200 rval
= makeprotoent(this);
201 } while (rval
== NULL
);
206 pr_rewind(struct irs_pr
*this) {
207 struct pvt
*pvt
= (struct pvt
*)this->private;
213 pr_minimize(struct irs_pr
*this) {
220 static struct protoent
*
221 makeprotoent(struct irs_pr
*this) {
222 struct pvt
*pvt
= (struct pvt
*)this->private;
228 pvt
->prbuf
= pvt
->curval_data
;
229 pvt
->curval_data
= NULL
;
231 for (p
= pvt
->prbuf
; *p
&& *p
!= '#';)
233 while (p
> pvt
->prbuf
&& isspace((unsigned char)(p
[-1])))
240 pvt
->proto
.p_name
= p
;
241 while (*p
&& !isspace((unsigned char)*p
))
247 while (*p
&& isspace((unsigned char)*p
))
249 pvt
->proto
.p_proto
= atoi(p
);
250 while (*p
&& !isspace((unsigned char)*p
))
255 if ((n
+ 1) >= m
|| !pvt
->proto
.p_aliases
) {
257 t
= realloc(pvt
->proto
.p_aliases
,
263 pvt
->proto
.p_aliases
= t
;
265 pvt
->proto
.p_aliases
[n
++] = p
;
266 while (*p
&& !isspace((unsigned char)*p
))
271 if (!pvt
->proto
.p_aliases
)
272 pvt
->proto
.p_aliases
= malloc(sizeof(char *));
273 if (!pvt
->proto
.p_aliases
)
275 pvt
->proto
.p_aliases
[n
] = NULL
;
276 return (&pvt
->proto
);
279 if (pvt
->proto
.p_aliases
) {
280 free(pvt
->proto
.p_aliases
);
281 pvt
->proto
.p_aliases
= NULL
;
291 nisfree(struct pvt
*pvt
, enum do_what do_what
) {
292 if ((do_what
& do_key
) && pvt
->curkey_data
) {
293 free(pvt
->curkey_data
);
294 pvt
->curkey_data
= NULL
;
296 if ((do_what
& do_val
) && pvt
->curval_data
) {
297 free(pvt
->curval_data
);
298 pvt
->curval_data
= NULL
;
302 #endif /*WANT_IRS_NIS*/