No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / dns_pw.c
blobc7c1d10dcdc976678453d4a1b2837b17a4da661b
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: dns_pw.c,v 1.3 2005/04/27 04:56:22 sra Exp";
22 #endif
24 #include "port_before.h"
26 #ifndef WANT_IRS_PW
27 static int __bind_irs_pw_unneeded;
28 #else
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <string.h>
35 #include <sys/types.h>
36 #include <netinet/in.h>
37 #include <arpa/nameser.h>
38 #include <resolv.h>
40 #include <isc/memcluster.h>
42 #include <irs.h>
44 #include "port_after.h"
46 #include "irs_p.h"
47 #include "hesiod.h"
48 #include "dns_p.h"
50 /* Types. */
52 struct pvt {
53 struct dns_p * dns;
54 struct passwd passwd;
55 char * pwbuf;
58 /* Forward. */
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 *,
68 struct __res_state *,
69 void (*)(void *));
71 static struct passwd * getpwcommon(struct irs_pw *, const char *,
72 const char *);
74 /* Public. */
76 struct irs_pw *
77 irs_dns_pw(struct irs_acc *this) {
78 struct dns_p *dns = (struct dns_p *)this->private;
79 struct irs_pw *pw;
80 struct pvt *pvt;
82 if (!dns || !dns->hes_ctx) {
83 errno = ENODEV;
84 return (NULL);
86 if (!(pvt = memget(sizeof *pvt))) {
87 errno = ENOMEM;
88 return (NULL);
90 memset(pvt, 0, sizeof *pvt);
91 pvt->dns = dns;
92 if (!(pw = memget(sizeof *pw))) {
93 memput(pvt, sizeof *pvt);
94 errno = ENOMEM;
95 return (NULL);
97 memset(pw, 0x5e, sizeof *pw);
98 pw->private = pvt;
99 pw->close = pw_close;
100 pw->byname = pw_byname;
101 pw->byuid = pw_byuid;
102 pw->next = pw_next;
103 pw->rewind = pw_rewind;
104 pw->minimize = pw_minimize;
105 pw->res_get = pw_res_get;
106 pw->res_set = pw_res_set;
107 return (pw);
110 /* Methods. */
112 static void
113 pw_close(struct irs_pw *this) {
114 struct pvt *pvt = (struct pvt *)this->private;
116 if (pvt->pwbuf)
117 free(pvt->pwbuf);
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) {
130 char uidstr[16];
132 sprintf(uidstr, "%lu", (u_long)uid);
133 return (getpwcommon(this, uidstr, "uid"));
136 static struct passwd *
137 pw_next(struct irs_pw *this) {
138 UNUSED(this);
139 errno = ENODEV;
140 return (NULL);
143 static void
144 pw_rewind(struct irs_pw *this) {
145 UNUSED(this);
146 /* NOOP */
149 static void
150 pw_minimize(struct irs_pw *this) {
151 UNUSED(this);
152 /* NOOP */
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));
163 static void
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);
172 /* Private. */
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)))
180 return (NULL);
181 if (!*hes_list) {
182 hesiod_free_list(pvt->dns->hes_ctx, hes_list);
183 errno = ENOENT;
184 return (NULL);
187 memset(&pvt->passwd, 0, sizeof pvt->passwd);
188 if (pvt->pwbuf)
189 free(pvt->pwbuf);
190 pvt->pwbuf = strdup(*hes_list);
191 hesiod_free_list(pvt->dns->hes_ctx, hes_list);
193 cp = pvt->pwbuf;
194 pvt->passwd.pw_name = cp;
195 if (!(cp = strchr(cp, ':')))
196 goto cleanup;
197 *cp++ = '\0';
199 pvt->passwd.pw_passwd = cp;
200 if (!(cp = strchr(cp, ':')))
201 goto cleanup;
202 *cp++ = '\0';
204 pvt->passwd.pw_uid = atoi(cp);
205 if (!(cp = strchr(cp, ':')))
206 goto cleanup;
207 *cp++ = '\0';
209 pvt->passwd.pw_gid = atoi(cp);
210 if (!(cp = strchr(cp, ':')))
211 goto cleanup;
212 *cp++ = '\0';
214 pvt->passwd.pw_gecos = cp;
215 if (!(cp = strchr(cp, ':')))
216 goto cleanup;
217 *cp++ = '\0';
219 pvt->passwd.pw_dir = cp;
220 if (!(cp = strchr(cp, ':')))
221 goto cleanup;
222 *cp++ = '\0';
224 pvt->passwd.pw_shell = cp;
225 return (&pvt->passwd);
227 cleanup:
228 free(pvt->pwbuf);
229 pvt->pwbuf = NULL;
230 return (NULL);
233 #endif /* WANT_IRS_PW */
234 /*! \file */