xref: /illumos-gate/usr/src/lib/libresolv2/common/irs/nis.c (revision ed093b41a93e8563e6e1e5dae0768dda2a7bcc27)
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 #include "port_before.h"
19 
20 #ifdef WANT_IRS_NIS
21 
22 #include <rpc/rpc.h>
23 #include <rpc/xdr.h>
24 #include <rpcsvc/yp_prot.h>
25 #include <rpcsvc/ypclnt.h>
26 
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 
31 #include <sys/types.h>
32 #include <netinet/in.h>
33 #ifdef T_NULL
34 #undef T_NULL			/* Silence re-definition warning of T_NULL. */
35 #endif
36 #include <arpa/nameser.h>
37 #include <resolv.h>
38 
39 #include <isc/memcluster.h>
40 #include <irs.h>
41 
42 #include "port_after.h"
43 
44 #include "irs_p.h"
45 #include "hesiod.h"
46 #include "nis_p.h"
47 
48 /* Forward */
49 
50 static void		nis_close(struct irs_acc *);
51 static struct __res_state * nis_res_get(struct irs_acc *);
52 static void		nis_res_set(struct irs_acc *, struct __res_state *,
53 				void (*)(void *));
54 
55 /* Public */
56 
57 struct irs_acc *
58 irs_nis_acc(const char *options) {
59 	struct nis_p *nis;
60 	struct irs_acc *acc;
61 	char *domain;
62 
63 	UNUSED(options);
64 
65 	if (yp_get_default_domain(&domain) != 0)
66 		return (NULL);
67 	if (!(nis = memget(sizeof *nis))) {
68 		errno = ENOMEM;
69 		return (NULL);
70 	}
71 	memset(nis, 0, sizeof *nis);
72 	if (!(acc = memget(sizeof *acc))) {
73 		memput(nis, sizeof *nis);
74 		errno = ENOMEM;
75 		return (NULL);
76 	}
77 	memset(acc, 0x5e, sizeof *acc);
78 	acc->private = nis;
79 	nis->domain = strdup(domain);
80 #ifdef WANT_IRS_GR
81 	acc->gr_map = irs_nis_gr;
82 #else
83 	acc->gr_map = NULL;
84 #endif
85 #ifdef WANT_IRS_PW
86 	acc->pw_map = irs_nis_pw;
87 #else
88 	acc->pw_map = NULL;
89 #endif
90 	acc->sv_map = irs_nis_sv;
91 	acc->pr_map = irs_nis_pr;
92 	acc->ho_map = irs_nis_ho;
93 	acc->nw_map = irs_nis_nw;
94 	acc->ng_map = irs_nis_ng;
95 	acc->res_get = nis_res_get;
96 	acc->res_set = nis_res_set;
97 	acc->close = nis_close;
98 	return (acc);
99 }
100 
101 /* Methods */
102 
103 static struct __res_state *
104 nis_res_get(struct irs_acc *this) {
105 	struct nis_p *nis = (struct nis_p *)this->private;
106 
107 	if (nis->res == NULL) {
108 		struct __res_state *res;
109 		res = (struct __res_state *)malloc(sizeof *res);
110 		if (res == NULL)
111 			return (NULL);
112 		memset(res, 0, sizeof *res);
113 		nis_res_set(this, res, free);
114 	}
115 
116 	if ((nis->res->options & RES_INIT) == 0 &&
117 	    res_ninit(nis->res) < 0)
118 		return (NULL);
119 
120 	return (nis->res);
121 }
122 
123 static void
124 nis_res_set(struct irs_acc *this, struct __res_state *res,
125 		void (*free_res)(void *)) {
126 	struct nis_p *nis = (struct nis_p *)this->private;
127 
128 	if (nis->res && nis->free_res) {
129 		res_nclose(nis->res);
130 		(*nis->free_res)(nis->res);
131 	}
132 
133 	nis->res = res;
134 	nis->free_res = free_res;
135 }
136 
137 static void
138 nis_close(struct irs_acc *this) {
139 	struct nis_p *nis = (struct nis_p *)this->private;
140 
141 	if (nis->res && nis->free_res)
142 		(*nis->free_res)(nis->res);
143 	free(nis->domain);
144 	memput(nis, sizeof *nis);
145 	memput(this, sizeof *this);
146 }
147 
148 #endif /*WANT_IRS_NIS*/
149 
150 /*! \file */
151