Merge branch 'vendor/GCC44'
[games.git] / contrib / bind-9.3 / lib / bind / irs / dns_pw.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996,1999 by Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: dns_pw.c,v 1.1.206.1 2004/03/09 08:33:34 marka Exp $";
20 #endif
21
22 #include "port_before.h"
23
24 #ifndef WANT_IRS_PW
25 static int __bind_irs_pw_unneeded;
26 #else
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <string.h>
32
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <arpa/nameser.h>
36 #include <resolv.h>
37
38 #include <isc/memcluster.h>
39
40 #include <irs.h>
41
42 #include "port_after.h"
43
44 #include "irs_p.h"
45 #include "hesiod.h"
46 #include "dns_p.h"
47
48 /* Types. */
49
50 struct pvt {
51         struct dns_p *  dns;
52         struct passwd   passwd;
53         char *          pwbuf;
54 };
55
56 /* Forward. */
57
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 *));
68
69 static struct passwd *          getpwcommon(struct irs_pw *, const char *,
70                                             const char *);
71
72 /* Public. */
73
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;
79
80         if (!dns || !dns->hes_ctx) {
81                 errno = ENODEV;
82                 return (NULL);
83         }
84         if (!(pvt = memget(sizeof *pvt))) {
85                 errno = ENOMEM;
86                 return (NULL);
87         }
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);
94         }
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);
106 }
107
108 /* Methods. */
109
110 static void
111 pw_close(struct irs_pw *this) {
112         struct pvt *pvt = (struct pvt *)this->private;
113
114         if (pvt->pwbuf)
115                 free(pvt->pwbuf);
116
117         memput(pvt, sizeof *pvt);
118         memput(this, sizeof *this);
119 }
120
121 static struct passwd *
122 pw_byname(struct irs_pw *this, const char *nam) {
123         return (getpwcommon(this, nam, "passwd"));
124 }
125
126 static struct passwd *
127 pw_byuid(struct irs_pw *this, uid_t uid) {
128         char uidstr[16];
129
130         sprintf(uidstr, "%lu", (u_long)uid);
131         return (getpwcommon(this, uidstr, "uid"));
132 }
133
134 static struct passwd *
135 pw_next(struct irs_pw *this) {
136         UNUSED(this);
137         errno = ENODEV;
138         return (NULL);
139 }
140
141 static void
142 pw_rewind(struct irs_pw *this) {
143         UNUSED(this);
144         /* NOOP */
145 }
146
147 static void
148 pw_minimize(struct irs_pw *this) {
149         UNUSED(this);
150         /* NOOP */
151 }
152
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;
157
158         return (__hesiod_res_get(dns->hes_ctx));
159 }
160
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;
166
167         __hesiod_res_set(dns->hes_ctx, res, free_res);
168 }
169
170 /* Private. */
171
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;
176
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);
183         }
184
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);
190
191         cp = pvt->pwbuf;
192         pvt->passwd.pw_name = cp;
193         if (!(cp = strchr(cp, ':')))
194                 goto cleanup;
195         *cp++ = '\0';
196
197         pvt->passwd.pw_passwd = cp;
198         if (!(cp = strchr(cp, ':')))
199                 goto cleanup;
200         *cp++ = '\0';
201         
202         pvt->passwd.pw_uid = atoi(cp);
203         if (!(cp = strchr(cp, ':')))
204                 goto cleanup;
205         *cp++ = '\0';
206
207         pvt->passwd.pw_gid = atoi(cp);
208         if (!(cp = strchr(cp, ':')))
209                 goto cleanup;
210         *cp++ = '\0';
211
212         pvt->passwd.pw_gecos = cp;
213         if (!(cp = strchr(cp, ':')))
214                 goto cleanup;
215         *cp++ = '\0';
216
217         pvt->passwd.pw_dir = cp;
218         if (!(cp = strchr(cp, ':')))
219                 goto cleanup;
220         *cp++ = '\0';
221
222         pvt->passwd.pw_shell = cp;
223         return (&pvt->passwd);
224         
225  cleanup:
226         free(pvt->pwbuf);
227         pvt->pwbuf = NULL;
228         return (NULL);
229 }
230
231 #endif /* WANT_IRS_PW */