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