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