dns_pw.c revision 1.1.1.1.2.2 1 1.1.1.1.2.2 jym /* $NetBSD: dns_pw.c,v 1.1.1.1.2.2 2009/05/13 18:52:09 jym Exp $ */
2 1.1.1.1.2.2 jym
3 1.1.1.1.2.2 jym /*
4 1.1.1.1.2.2 jym * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 1.1.1.1.2.2 jym * Copyright (c) 1996,1999 by Internet Software Consortium.
6 1.1.1.1.2.2 jym *
7 1.1.1.1.2.2 jym * Permission to use, copy, modify, and distribute this software for any
8 1.1.1.1.2.2 jym * purpose with or without fee is hereby granted, provided that the above
9 1.1.1.1.2.2 jym * copyright notice and this permission notice appear in all copies.
10 1.1.1.1.2.2 jym *
11 1.1.1.1.2.2 jym * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 1.1.1.1.2.2 jym * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1.1.1.2.2 jym * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 1.1.1.1.2.2 jym * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1.1.1.2.2 jym * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1.1.1.2.2 jym * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 1.1.1.1.2.2 jym * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1.1.1.2.2 jym */
19 1.1.1.1.2.2 jym
20 1.1.1.1.2.2 jym #if defined(LIBC_SCCS) && !defined(lint)
21 1.1.1.1.2.2 jym static const char rcsid[] = "Id: dns_pw.c,v 1.3 2005/04/27 04:56:22 sra Exp";
22 1.1.1.1.2.2 jym #endif
23 1.1.1.1.2.2 jym
24 1.1.1.1.2.2 jym #include "port_before.h"
25 1.1.1.1.2.2 jym
26 1.1.1.1.2.2 jym #ifndef WANT_IRS_PW
27 1.1.1.1.2.2 jym static int __bind_irs_pw_unneeded;
28 1.1.1.1.2.2 jym #else
29 1.1.1.1.2.2 jym
30 1.1.1.1.2.2 jym #include <stdio.h>
31 1.1.1.1.2.2 jym #include <stdlib.h>
32 1.1.1.1.2.2 jym #include <errno.h>
33 1.1.1.1.2.2 jym #include <string.h>
34 1.1.1.1.2.2 jym
35 1.1.1.1.2.2 jym #include <sys/types.h>
36 1.1.1.1.2.2 jym #include <netinet/in.h>
37 1.1.1.1.2.2 jym #include <arpa/nameser.h>
38 1.1.1.1.2.2 jym #include <resolv.h>
39 1.1.1.1.2.2 jym
40 1.1.1.1.2.2 jym #include <isc/memcluster.h>
41 1.1.1.1.2.2 jym
42 1.1.1.1.2.2 jym #include <irs.h>
43 1.1.1.1.2.2 jym
44 1.1.1.1.2.2 jym #include "port_after.h"
45 1.1.1.1.2.2 jym
46 1.1.1.1.2.2 jym #include "irs_p.h"
47 1.1.1.1.2.2 jym #include "hesiod.h"
48 1.1.1.1.2.2 jym #include "dns_p.h"
49 1.1.1.1.2.2 jym
50 1.1.1.1.2.2 jym /* Types. */
51 1.1.1.1.2.2 jym
52 1.1.1.1.2.2 jym struct pvt {
53 1.1.1.1.2.2 jym struct dns_p * dns;
54 1.1.1.1.2.2 jym struct passwd passwd;
55 1.1.1.1.2.2 jym char * pwbuf;
56 1.1.1.1.2.2 jym };
57 1.1.1.1.2.2 jym
58 1.1.1.1.2.2 jym /* Forward. */
59 1.1.1.1.2.2 jym
60 1.1.1.1.2.2 jym static void pw_close(struct irs_pw *);
61 1.1.1.1.2.2 jym static struct passwd * pw_byname(struct irs_pw *, const char *);
62 1.1.1.1.2.2 jym static struct passwd * pw_byuid(struct irs_pw *, uid_t);
63 1.1.1.1.2.2 jym static struct passwd * pw_next(struct irs_pw *);
64 1.1.1.1.2.2 jym static void pw_rewind(struct irs_pw *);
65 1.1.1.1.2.2 jym static void pw_minimize(struct irs_pw *);
66 1.1.1.1.2.2 jym static struct __res_state * pw_res_get(struct irs_pw *);
67 1.1.1.1.2.2 jym static void pw_res_set(struct irs_pw *,
68 1.1.1.1.2.2 jym struct __res_state *,
69 1.1.1.1.2.2 jym void (*)(void *));
70 1.1.1.1.2.2 jym
71 1.1.1.1.2.2 jym static struct passwd * getpwcommon(struct irs_pw *, const char *,
72 1.1.1.1.2.2 jym const char *);
73 1.1.1.1.2.2 jym
74 1.1.1.1.2.2 jym /* Public. */
75 1.1.1.1.2.2 jym
76 1.1.1.1.2.2 jym struct irs_pw *
77 1.1.1.1.2.2 jym irs_dns_pw(struct irs_acc *this) {
78 1.1.1.1.2.2 jym struct dns_p *dns = (struct dns_p *)this->private;
79 1.1.1.1.2.2 jym struct irs_pw *pw;
80 1.1.1.1.2.2 jym struct pvt *pvt;
81 1.1.1.1.2.2 jym
82 1.1.1.1.2.2 jym if (!dns || !dns->hes_ctx) {
83 1.1.1.1.2.2 jym errno = ENODEV;
84 1.1.1.1.2.2 jym return (NULL);
85 1.1.1.1.2.2 jym }
86 1.1.1.1.2.2 jym if (!(pvt = memget(sizeof *pvt))) {
87 1.1.1.1.2.2 jym errno = ENOMEM;
88 1.1.1.1.2.2 jym return (NULL);
89 1.1.1.1.2.2 jym }
90 1.1.1.1.2.2 jym memset(pvt, 0, sizeof *pvt);
91 1.1.1.1.2.2 jym pvt->dns = dns;
92 1.1.1.1.2.2 jym if (!(pw = memget(sizeof *pw))) {
93 1.1.1.1.2.2 jym memput(pvt, sizeof *pvt);
94 1.1.1.1.2.2 jym errno = ENOMEM;
95 1.1.1.1.2.2 jym return (NULL);
96 1.1.1.1.2.2 jym }
97 1.1.1.1.2.2 jym memset(pw, 0x5e, sizeof *pw);
98 1.1.1.1.2.2 jym pw->private = pvt;
99 1.1.1.1.2.2 jym pw->close = pw_close;
100 1.1.1.1.2.2 jym pw->byname = pw_byname;
101 1.1.1.1.2.2 jym pw->byuid = pw_byuid;
102 1.1.1.1.2.2 jym pw->next = pw_next;
103 1.1.1.1.2.2 jym pw->rewind = pw_rewind;
104 1.1.1.1.2.2 jym pw->minimize = pw_minimize;
105 1.1.1.1.2.2 jym pw->res_get = pw_res_get;
106 1.1.1.1.2.2 jym pw->res_set = pw_res_set;
107 1.1.1.1.2.2 jym return (pw);
108 1.1.1.1.2.2 jym }
109 1.1.1.1.2.2 jym
110 1.1.1.1.2.2 jym /* Methods. */
111 1.1.1.1.2.2 jym
112 1.1.1.1.2.2 jym static void
113 1.1.1.1.2.2 jym pw_close(struct irs_pw *this) {
114 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
115 1.1.1.1.2.2 jym
116 1.1.1.1.2.2 jym if (pvt->pwbuf)
117 1.1.1.1.2.2 jym free(pvt->pwbuf);
118 1.1.1.1.2.2 jym
119 1.1.1.1.2.2 jym memput(pvt, sizeof *pvt);
120 1.1.1.1.2.2 jym memput(this, sizeof *this);
121 1.1.1.1.2.2 jym }
122 1.1.1.1.2.2 jym
123 1.1.1.1.2.2 jym static struct passwd *
124 1.1.1.1.2.2 jym pw_byname(struct irs_pw *this, const char *nam) {
125 1.1.1.1.2.2 jym return (getpwcommon(this, nam, "passwd"));
126 1.1.1.1.2.2 jym }
127 1.1.1.1.2.2 jym
128 1.1.1.1.2.2 jym static struct passwd *
129 1.1.1.1.2.2 jym pw_byuid(struct irs_pw *this, uid_t uid) {
130 1.1.1.1.2.2 jym char uidstr[16];
131 1.1.1.1.2.2 jym
132 1.1.1.1.2.2 jym sprintf(uidstr, "%lu", (u_long)uid);
133 1.1.1.1.2.2 jym return (getpwcommon(this, uidstr, "uid"));
134 1.1.1.1.2.2 jym }
135 1.1.1.1.2.2 jym
136 1.1.1.1.2.2 jym static struct passwd *
137 1.1.1.1.2.2 jym pw_next(struct irs_pw *this) {
138 1.1.1.1.2.2 jym UNUSED(this);
139 1.1.1.1.2.2 jym errno = ENODEV;
140 1.1.1.1.2.2 jym return (NULL);
141 1.1.1.1.2.2 jym }
142 1.1.1.1.2.2 jym
143 1.1.1.1.2.2 jym static void
144 1.1.1.1.2.2 jym pw_rewind(struct irs_pw *this) {
145 1.1.1.1.2.2 jym UNUSED(this);
146 1.1.1.1.2.2 jym /* NOOP */
147 1.1.1.1.2.2 jym }
148 1.1.1.1.2.2 jym
149 1.1.1.1.2.2 jym static void
150 1.1.1.1.2.2 jym pw_minimize(struct irs_pw *this) {
151 1.1.1.1.2.2 jym UNUSED(this);
152 1.1.1.1.2.2 jym /* NOOP */
153 1.1.1.1.2.2 jym }
154 1.1.1.1.2.2 jym
155 1.1.1.1.2.2 jym static struct __res_state *
156 1.1.1.1.2.2 jym pw_res_get(struct irs_pw *this) {
157 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
158 1.1.1.1.2.2 jym struct dns_p *dns = pvt->dns;
159 1.1.1.1.2.2 jym
160 1.1.1.1.2.2 jym return (__hesiod_res_get(dns->hes_ctx));
161 1.1.1.1.2.2 jym }
162 1.1.1.1.2.2 jym
163 1.1.1.1.2.2 jym static void
164 1.1.1.1.2.2 jym pw_res_set(struct irs_pw *this, struct __res_state * res,
165 1.1.1.1.2.2 jym void (*free_res)(void *)) {
166 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
167 1.1.1.1.2.2 jym struct dns_p *dns = pvt->dns;
168 1.1.1.1.2.2 jym
169 1.1.1.1.2.2 jym __hesiod_res_set(dns->hes_ctx, res, free_res);
170 1.1.1.1.2.2 jym }
171 1.1.1.1.2.2 jym
172 1.1.1.1.2.2 jym /* Private. */
173 1.1.1.1.2.2 jym
174 1.1.1.1.2.2 jym static struct passwd *
175 1.1.1.1.2.2 jym getpwcommon(struct irs_pw *this, const char *arg, const char *type) {
176 1.1.1.1.2.2 jym struct pvt *pvt = (struct pvt *)this->private;
177 1.1.1.1.2.2 jym char **hes_list, *cp;
178 1.1.1.1.2.2 jym
179 1.1.1.1.2.2 jym if (!(hes_list = hesiod_resolve(pvt->dns->hes_ctx, arg, type)))
180 1.1.1.1.2.2 jym return (NULL);
181 1.1.1.1.2.2 jym if (!*hes_list) {
182 1.1.1.1.2.2 jym hesiod_free_list(pvt->dns->hes_ctx, hes_list);
183 1.1.1.1.2.2 jym errno = ENOENT;
184 1.1.1.1.2.2 jym return (NULL);
185 1.1.1.1.2.2 jym }
186 1.1.1.1.2.2 jym
187 1.1.1.1.2.2 jym memset(&pvt->passwd, 0, sizeof pvt->passwd);
188 1.1.1.1.2.2 jym if (pvt->pwbuf)
189 1.1.1.1.2.2 jym free(pvt->pwbuf);
190 1.1.1.1.2.2 jym pvt->pwbuf = strdup(*hes_list);
191 1.1.1.1.2.2 jym hesiod_free_list(pvt->dns->hes_ctx, hes_list);
192 1.1.1.1.2.2 jym
193 1.1.1.1.2.2 jym cp = pvt->pwbuf;
194 1.1.1.1.2.2 jym pvt->passwd.pw_name = cp;
195 1.1.1.1.2.2 jym if (!(cp = strchr(cp, ':')))
196 1.1.1.1.2.2 jym goto cleanup;
197 1.1.1.1.2.2 jym *cp++ = '\0';
198 1.1.1.1.2.2 jym
199 1.1.1.1.2.2 jym pvt->passwd.pw_passwd = cp;
200 1.1.1.1.2.2 jym if (!(cp = strchr(cp, ':')))
201 1.1.1.1.2.2 jym goto cleanup;
202 1.1.1.1.2.2 jym *cp++ = '\0';
203 1.1.1.1.2.2 jym
204 1.1.1.1.2.2 jym pvt->passwd.pw_uid = atoi(cp);
205 1.1.1.1.2.2 jym if (!(cp = strchr(cp, ':')))
206 1.1.1.1.2.2 jym goto cleanup;
207 1.1.1.1.2.2 jym *cp++ = '\0';
208 1.1.1.1.2.2 jym
209 1.1.1.1.2.2 jym pvt->passwd.pw_gid = atoi(cp);
210 1.1.1.1.2.2 jym if (!(cp = strchr(cp, ':')))
211 1.1.1.1.2.2 jym goto cleanup;
212 1.1.1.1.2.2 jym *cp++ = '\0';
213 1.1.1.1.2.2 jym
214 1.1.1.1.2.2 jym pvt->passwd.pw_gecos = cp;
215 1.1.1.1.2.2 jym if (!(cp = strchr(cp, ':')))
216 1.1.1.1.2.2 jym goto cleanup;
217 1.1.1.1.2.2 jym *cp++ = '\0';
218 1.1.1.1.2.2 jym
219 1.1.1.1.2.2 jym pvt->passwd.pw_dir = cp;
220 1.1.1.1.2.2 jym if (!(cp = strchr(cp, ':')))
221 1.1.1.1.2.2 jym goto cleanup;
222 1.1.1.1.2.2 jym *cp++ = '\0';
223 1.1.1.1.2.2 jym
224 1.1.1.1.2.2 jym pvt->passwd.pw_shell = cp;
225 1.1.1.1.2.2 jym return (&pvt->passwd);
226 1.1.1.1.2.2 jym
227 1.1.1.1.2.2 jym cleanup:
228 1.1.1.1.2.2 jym free(pvt->pwbuf);
229 1.1.1.1.2.2 jym pvt->pwbuf = NULL;
230 1.1.1.1.2.2 jym return (NULL);
231 1.1.1.1.2.2 jym }
232 1.1.1.1.2.2 jym
233 1.1.1.1.2.2 jym #endif /* WANT_IRS_PW */
234 1.1.1.1.2.2 jym /*! \file */
235