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