xref: /illumos-gate/usr/src/lib/libresolv2/common/irs/nis.c (revision 8b80e8cb6855118d46f605e91b5ed4ce83417395)
1 /*
2  * Copyright 1997-2002 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 1996-1999 by Internet Software Consortium.
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20  * SOFTWARE.
21  */
22 
23 #pragma ident	"%Z%%M%	%I%	%E% SMI"
24 
25 #if defined(LIBC_SCCS) && !defined(lint)
26 static const char rcsid[] = "$Id: nis.c,v 1.15 2001/05/29 05:49:11 marka Exp $";
27 #endif
28 
29 /* Imports */
30 
31 #include "port_before.h"
32 
33 #ifdef WANT_IRS_NIS
34 
35 #include <rpc/rpc.h>
36 #include <rpc/xdr.h>
37 #include <rpcsvc/yp_prot.h>
38 #include <rpcsvc/ypclnt.h>
39 
40 #include <stdlib.h>
41 #include <string.h>
42 #include <errno.h>
43 
44 #include <sys/types.h>
45 #include <netinet/in.h>
46 #include <arpa/nameser.h>
47 #include <resolv.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 "hesiod.h"
56 #include "nis_p.h"
57 
58 /* Forward */
59 
60 static void		nis_close(struct irs_acc *);
61 static struct __res_state * nis_res_get(struct irs_acc *);
62 static void		nis_res_set(struct irs_acc *, struct __res_state *,
63 				void (*)(void *));
64 
65 /* Public */
66 
67 struct irs_acc *
68 irs_nis_acc(const char *options) {
69 	struct nis_p *nis;
70 	struct irs_acc *acc;
71 	char *domain;
72 
73 	UNUSED(options);
74 
75 	if (yp_get_default_domain(&domain) != 0)
76 		return (NULL);
77 	if (!(nis = memget(sizeof *nis))) {
78 		errno = ENOMEM;
79 		return (NULL);
80 	}
81 	memset(nis, 0, sizeof *nis);
82 	if (!(acc = memget(sizeof *acc))) {
83 		memput(nis, sizeof *nis);
84 		errno = ENOMEM;
85 		return (NULL);
86 	}
87 	memset(acc, 0x5e, sizeof *acc);
88 	acc->private = nis;
89 	nis->domain = strdup(domain);
90 #ifdef WANT_IRS_GR
91 	acc->gr_map = irs_nis_gr;
92 #else
93 	acc->gr_map = NULL;
94 #endif
95 #ifdef WANT_IRS_PW
96 	acc->pw_map = irs_nis_pw;
97 #else
98 	acc->pw_map = NULL;
99 #endif
100 	acc->sv_map = irs_nis_sv;
101 	acc->pr_map = irs_nis_pr;
102 	acc->ho_map = irs_nis_ho;
103 	acc->nw_map = irs_nis_nw;
104 	acc->ng_map = irs_nis_ng;
105 	acc->res_get = nis_res_get;
106 	acc->res_set = nis_res_set;
107 	acc->close = nis_close;
108 	return (acc);
109 }
110 
111 /* Methods */
112 
113 static struct __res_state *
114 nis_res_get(struct irs_acc *this) {
115 	struct nis_p *nis = (struct nis_p *)this->private;
116 
117 	if (nis->res == NULL) {
118 		struct __res_state *res;
119 		res = (struct __res_state *)malloc(sizeof *res);
120 		if (res == NULL)
121 			return (NULL);
122 		memset(res, 0, sizeof *res);
123 		nis_res_set(this, res, free);
124 	}
125 
126 	if ((nis->res->options & RES_INIT) == 0 &&
127 	    res_ninit(nis->res) < 0)
128 		return (NULL);
129 
130 	return (nis->res);
131 }
132 
133 static void
134 nis_res_set(struct irs_acc *this, struct __res_state *res,
135 		void (*free_res)(void *)) {
136 	struct nis_p *nis = (struct nis_p *)this->private;
137 
138 	if (nis->res && nis->free_res) {
139 		res_nclose(nis->res);
140 		(*nis->free_res)(nis->res);
141 	}
142 
143 	nis->res = res;
144 	nis->free_res = free_res;
145 }
146 
147 static void
148 nis_close(struct irs_acc *this) {
149 	struct nis_p *nis = (struct nis_p *)this->private;
150 
151 	if (nis->res && nis->free_res)
152 		(*nis->free_res)(nis->res);
153 	free(nis->domain);
154 	memput(nis, sizeof *nis);
155 	memput(this, sizeof *this);
156 }
157 
158 #endif /*WANT_IRS_NIS*/
159