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