Import bind 9.5.2 vendor sources.
[dragonfly.git] / contrib / bind-9.5.2 / lib / bind / irs / dns_pw.c
blob4ea97adfdb0c2a3db3843580324d75f20b0fe569
1 /*
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 $";
20 #endif
22 #include "port_before.h"
24 #ifndef WANT_IRS_PW
25 static int __bind_irs_pw_unneeded;
26 #else
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <string.h>
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <arpa/nameser.h>
36 #include <resolv.h>
38 #include <isc/memcluster.h>
40 #include <irs.h>
42 #include "port_after.h"
44 #include "irs_p.h"
45 #include "hesiod.h"
46 #include "dns_p.h"
48 /* Types. */
50 struct pvt {
51 struct dns_p * dns;
52 struct passwd passwd;
53 char * pwbuf;
56 /* Forward. */
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 *,
66 struct __res_state *,
67 void (*)(void *));
69 static struct passwd * getpwcommon(struct irs_pw *, const char *,
70 const char *);
72 /* Public. */
74 struct irs_pw *
75 irs_dns_pw(struct irs_acc *this) {
76 struct dns_p *dns = (struct dns_p *)this->private;
77 struct irs_pw *pw;
78 struct pvt *pvt;
80 if (!dns || !dns->hes_ctx) {
81 errno = ENODEV;
82 return (NULL);
84 if (!(pvt = memget(sizeof *pvt))) {
85 errno = ENOMEM;
86 return (NULL);
88 memset(pvt, 0, sizeof *pvt);
89 pvt->dns = dns;
90 if (!(pw = memget(sizeof *pw))) {
91 memput(pvt, sizeof *pvt);
92 errno = ENOMEM;
93 return (NULL);
95 memset(pw, 0x5e, sizeof *pw);
96 pw->private = pvt;
97 pw->close = pw_close;
98 pw->byname = pw_byname;
99 pw->byuid = pw_byuid;
100 pw->next = pw_next;
101 pw->rewind = pw_rewind;
102 pw->minimize = pw_minimize;
103 pw->res_get = pw_res_get;
104 pw->res_set = pw_res_set;
105 return (pw);
108 /* Methods. */
110 static void
111 pw_close(struct irs_pw *this) {
112 struct pvt *pvt = (struct pvt *)this->private;
114 if (pvt->pwbuf)
115 free(pvt->pwbuf);
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) {
128 char uidstr[16];
130 sprintf(uidstr, "%lu", (u_long)uid);
131 return (getpwcommon(this, uidstr, "uid"));
134 static struct passwd *
135 pw_next(struct irs_pw *this) {
136 UNUSED(this);
137 errno = ENODEV;
138 return (NULL);
141 static void
142 pw_rewind(struct irs_pw *this) {
143 UNUSED(this);
144 /* NOOP */
147 static void
148 pw_minimize(struct irs_pw *this) {
149 UNUSED(this);
150 /* NOOP */
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));
161 static void
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);
170 /* Private. */
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)))
178 return (NULL);
179 if (!*hes_list) {
180 hesiod_free_list(pvt->dns->hes_ctx, hes_list);
181 errno = ENOENT;
182 return (NULL);
185 memset(&pvt->passwd, 0, sizeof pvt->passwd);
186 if (pvt->pwbuf)
187 free(pvt->pwbuf);
188 pvt->pwbuf = strdup(*hes_list);
189 hesiod_free_list(pvt->dns->hes_ctx, hes_list);
191 cp = pvt->pwbuf;
192 pvt->passwd.pw_name = cp;
193 if (!(cp = strchr(cp, ':')))
194 goto cleanup;
195 *cp++ = '\0';
197 pvt->passwd.pw_passwd = cp;
198 if (!(cp = strchr(cp, ':')))
199 goto cleanup;
200 *cp++ = '\0';
202 pvt->passwd.pw_uid = atoi(cp);
203 if (!(cp = strchr(cp, ':')))
204 goto cleanup;
205 *cp++ = '\0';
207 pvt->passwd.pw_gid = atoi(cp);
208 if (!(cp = strchr(cp, ':')))
209 goto cleanup;
210 *cp++ = '\0';
212 pvt->passwd.pw_gecos = cp;
213 if (!(cp = strchr(cp, ':')))
214 goto cleanup;
215 *cp++ = '\0';
217 pvt->passwd.pw_dir = cp;
218 if (!(cp = strchr(cp, ':')))
219 goto cleanup;
220 *cp++ = '\0';
222 pvt->passwd.pw_shell = cp;
223 return (&pvt->passwd);
225 cleanup:
226 free(pvt->pwbuf);
227 pvt->pwbuf = NULL;
228 return (NULL);
231 #endif /* WANT_IRS_PW */
232 /*! \file */