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