Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / nis.c
blob3ed48fd8a0443af26d15b21b669c1a9d3a96f518
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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.
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
24 /* Imports */
26 #include "port_before.h"
28 #ifdef WANT_IRS_NIS
30 #include <rpc/rpc.h>
31 #include <rpc/xdr.h>
32 #include <rpcsvc/yp_prot.h>
33 #include <rpcsvc/ypclnt.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
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>
47 #include <isc/memcluster.h>
48 #include <irs.h>
50 #include "port_after.h"
52 #include "irs_p.h"
53 #include "hesiod.h"
54 #include "nis_p.h"
56 /* Forward */
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 *));
63 /* Public */
65 struct irs_acc *
66 irs_nis_acc(const char *options) {
67 struct nis_p *nis;
68 struct irs_acc *acc;
69 char *domain;
71 UNUSED(options);
73 if (yp_get_default_domain(&domain) != 0)
74 return (NULL);
75 if (!(nis = memget(sizeof *nis))) {
76 errno = ENOMEM;
77 return (NULL);
79 memset(nis, 0, sizeof *nis);
80 if (!(acc = memget(sizeof *acc))) {
81 memput(nis, sizeof *nis);
82 errno = ENOMEM;
83 return (NULL);
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);
109 /* Methods */
111 static struct __res_state *
112 nis_res_get(struct irs_acc *this) {
113 struct nis_p *nis = (struct nis_p *)this->private;
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);
124 if ((nis->res->options & RES_INIT) == 0 &&
125 res_ninit(nis->res) < 0)
126 return (NULL);
128 return (nis->res);
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;
136 if (nis->res && nis->free_res) {
137 res_nclose(nis->res);
138 (*nis->free_res)(nis->res);
141 nis->res = res;
142 nis->free_res = free_res;
145 static void
146 nis_close(struct irs_acc *this) {
147 struct nis_p *nis = (struct nis_p *)this->private;
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);
156 #endif /*WANT_IRS_NIS*/
158 /*! \file */