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: dns_pw.c,v 1.3 2005/04/27 04:56:22 sra Exp";
24 #include "port_before.h"
27 static int __bind_irs_pw_unneeded
;
35 #include <sys/types.h>
36 #include <netinet/in.h>
37 #include <arpa/nameser.h>
40 #include <isc/memcluster.h>
44 #include "port_after.h"
60 static void pw_close(struct irs_pw
*);
61 static struct passwd
* pw_byname(struct irs_pw
*, const char *);
62 static struct passwd
* pw_byuid(struct irs_pw
*, uid_t
);
63 static struct passwd
* pw_next(struct irs_pw
*);
64 static void pw_rewind(struct irs_pw
*);
65 static void pw_minimize(struct irs_pw
*);
66 static struct __res_state
* pw_res_get(struct irs_pw
*);
67 static void pw_res_set(struct irs_pw
*,
71 static struct passwd
* getpwcommon(struct irs_pw
*, const char *,
77 irs_dns_pw(struct irs_acc
*this) {
78 struct dns_p
*dns
= (struct dns_p
*)this->private;
82 if (!dns
|| !dns
->hes_ctx
) {
86 if (!(pvt
= memget(sizeof *pvt
))) {
90 memset(pvt
, 0, sizeof *pvt
);
92 if (!(pw
= memget(sizeof *pw
))) {
93 memput(pvt
, sizeof *pvt
);
97 memset(pw
, 0x5e, sizeof *pw
);
100 pw
->byname
= pw_byname
;
101 pw
->byuid
= pw_byuid
;
103 pw
->rewind
= pw_rewind
;
104 pw
->minimize
= pw_minimize
;
105 pw
->res_get
= pw_res_get
;
106 pw
->res_set
= pw_res_set
;
113 pw_close(struct irs_pw
*this) {
114 struct pvt
*pvt
= (struct pvt
*)this->private;
119 memput(pvt
, sizeof *pvt
);
120 memput(this, sizeof *this);
123 static struct passwd
*
124 pw_byname(struct irs_pw
*this, const char *nam
) {
125 return (getpwcommon(this, nam
, "passwd"));
128 static struct passwd
*
129 pw_byuid(struct irs_pw
*this, uid_t uid
) {
132 sprintf(uidstr
, "%lu", (u_long
)uid
);
133 return (getpwcommon(this, uidstr
, "uid"));
136 static struct passwd
*
137 pw_next(struct irs_pw
*this) {
144 pw_rewind(struct irs_pw
*this) {
150 pw_minimize(struct irs_pw
*this) {
155 static struct __res_state
*
156 pw_res_get(struct irs_pw
*this) {
157 struct pvt
*pvt
= (struct pvt
*)this->private;
158 struct dns_p
*dns
= pvt
->dns
;
160 return (__hesiod_res_get(dns
->hes_ctx
));
164 pw_res_set(struct irs_pw
*this, struct __res_state
* res
,
165 void (*free_res
)(void *)) {
166 struct pvt
*pvt
= (struct pvt
*)this->private;
167 struct dns_p
*dns
= pvt
->dns
;
169 __hesiod_res_set(dns
->hes_ctx
, res
, free_res
);
174 static struct passwd
*
175 getpwcommon(struct irs_pw
*this, const char *arg
, const char *type
) {
176 struct pvt
*pvt
= (struct pvt
*)this->private;
177 char **hes_list
, *cp
;
179 if (!(hes_list
= hesiod_resolve(pvt
->dns
->hes_ctx
, arg
, type
)))
182 hesiod_free_list(pvt
->dns
->hes_ctx
, hes_list
);
187 memset(&pvt
->passwd
, 0, sizeof pvt
->passwd
);
190 pvt
->pwbuf
= strdup(*hes_list
);
191 hesiod_free_list(pvt
->dns
->hes_ctx
, hes_list
);
194 pvt
->passwd
.pw_name
= cp
;
195 if (!(cp
= strchr(cp
, ':')))
199 pvt
->passwd
.pw_passwd
= cp
;
200 if (!(cp
= strchr(cp
, ':')))
204 pvt
->passwd
.pw_uid
= atoi(cp
);
205 if (!(cp
= strchr(cp
, ':')))
209 pvt
->passwd
.pw_gid
= atoi(cp
);
210 if (!(cp
= strchr(cp
, ':')))
214 pvt
->passwd
.pw_gecos
= cp
;
215 if (!(cp
= strchr(cp
, ':')))
219 pvt
->passwd
.pw_dir
= cp
;
220 if (!(cp
= strchr(cp
, ':')))
224 pvt
->passwd
.pw_shell
= cp
;
225 return (&pvt
->passwd
);
233 #endif /* WANT_IRS_PW */