2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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.
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid
[] = "$Id: dns_pw.c,v 1.3 2005/04/27 04:56:22 sra Exp $";
22 #include "port_before.h"
25 static int __bind_irs_pw_unneeded
;
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <arpa/nameser.h>
38 #include <isc/memcluster.h>
42 #include "port_after.h"
58 static void pw_close(struct irs_pw
*);
59 static struct passwd
* pw_byname(struct irs_pw
*, const char *);
60 static struct passwd
* pw_byuid(struct irs_pw
*, uid_t
);
61 static struct passwd
* pw_next(struct irs_pw
*);
62 static void pw_rewind(struct irs_pw
*);
63 static void pw_minimize(struct irs_pw
*);
64 static struct __res_state
* pw_res_get(struct irs_pw
*);
65 static void pw_res_set(struct irs_pw
*,
69 static struct passwd
* getpwcommon(struct irs_pw
*, const char *,
75 irs_dns_pw(struct irs_acc
*this) {
76 struct dns_p
*dns
= (struct dns_p
*)this->private;
80 if (!dns
|| !dns
->hes_ctx
) {
84 if (!(pvt
= memget(sizeof *pvt
))) {
88 memset(pvt
, 0, sizeof *pvt
);
90 if (!(pw
= memget(sizeof *pw
))) {
91 memput(pvt
, sizeof *pvt
);
95 memset(pw
, 0x5e, sizeof *pw
);
98 pw
->byname
= pw_byname
;
101 pw
->rewind
= pw_rewind
;
102 pw
->minimize
= pw_minimize
;
103 pw
->res_get
= pw_res_get
;
104 pw
->res_set
= pw_res_set
;
111 pw_close(struct irs_pw
*this) {
112 struct pvt
*pvt
= (struct pvt
*)this->private;
117 memput(pvt
, sizeof *pvt
);
118 memput(this, sizeof *this);
121 static struct passwd
*
122 pw_byname(struct irs_pw
*this, const char *nam
) {
123 return (getpwcommon(this, nam
, "passwd"));
126 static struct passwd
*
127 pw_byuid(struct irs_pw
*this, uid_t uid
) {
130 sprintf(uidstr
, "%lu", (u_long
)uid
);
131 return (getpwcommon(this, uidstr
, "uid"));
134 static struct passwd
*
135 pw_next(struct irs_pw
*this) {
142 pw_rewind(struct irs_pw
*this) {
148 pw_minimize(struct irs_pw
*this) {
153 static struct __res_state
*
154 pw_res_get(struct irs_pw
*this) {
155 struct pvt
*pvt
= (struct pvt
*)this->private;
156 struct dns_p
*dns
= pvt
->dns
;
158 return (__hesiod_res_get(dns
->hes_ctx
));
162 pw_res_set(struct irs_pw
*this, struct __res_state
* res
,
163 void (*free_res
)(void *)) {
164 struct pvt
*pvt
= (struct pvt
*)this->private;
165 struct dns_p
*dns
= pvt
->dns
;
167 __hesiod_res_set(dns
->hes_ctx
, res
, free_res
);
172 static struct passwd
*
173 getpwcommon(struct irs_pw
*this, const char *arg
, const char *type
) {
174 struct pvt
*pvt
= (struct pvt
*)this->private;
175 char **hes_list
, *cp
;
177 if (!(hes_list
= hesiod_resolve(pvt
->dns
->hes_ctx
, arg
, type
)))
180 hesiod_free_list(pvt
->dns
->hes_ctx
, hes_list
);
185 memset(&pvt
->passwd
, 0, sizeof pvt
->passwd
);
188 pvt
->pwbuf
= strdup(*hes_list
);
189 hesiod_free_list(pvt
->dns
->hes_ctx
, hes_list
);
192 pvt
->passwd
.pw_name
= cp
;
193 if (!(cp
= strchr(cp
, ':')))
197 pvt
->passwd
.pw_passwd
= cp
;
198 if (!(cp
= strchr(cp
, ':')))
202 pvt
->passwd
.pw_uid
= atoi(cp
);
203 if (!(cp
= strchr(cp
, ':')))
207 pvt
->passwd
.pw_gid
= atoi(cp
);
208 if (!(cp
= strchr(cp
, ':')))
212 pvt
->passwd
.pw_gecos
= cp
;
213 if (!(cp
= strchr(cp
, ':')))
217 pvt
->passwd
.pw_dir
= cp
;
218 if (!(cp
= strchr(cp
, ':')))
222 pvt
->passwd
.pw_shell
= cp
;
223 return (&pvt
->passwd
);
231 #endif /* WANT_IRS_PW */