2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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.
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: nis_pw.c,v 1.2.206.1 2004/03/09 08:33:38 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
24 #include "port_before.h"
26 #if !defined(WANT_IRS_PW) || !defined(WANT_IRS_NIS)
27 static int __bind_irs_pw_unneeded;
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <arpa/nameser.h>
35 #include <isc/memcluster.h>
38 #include <rpcsvc/yp_prot.h>
39 #include <rpcsvc/ypclnt.h>
49 #include <isc/memcluster.h>
53 #include "port_after.h"
71 enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 };
73 static /*const*/ char passwd_byname[] = "passwd.byname";
74 static /*const*/ char passwd_byuid[] = "passwd.byuid";
78 static void pw_close(struct irs_pw *);
79 static struct passwd * pw_next(struct irs_pw *);
80 static struct passwd * pw_byname(struct irs_pw *, const char *);
81 static struct passwd * pw_byuid(struct irs_pw *, uid_t);
82 static void pw_rewind(struct irs_pw *);
83 static void pw_minimize(struct irs_pw *);
85 static struct passwd * makepasswdent(struct irs_pw *);
86 static void nisfree(struct pvt *, enum do_what);
91 irs_nis_pw(struct irs_acc *this) {
95 if (!(pw = memget(sizeof *pw))) {
99 memset(pw, 0x5e, sizeof *pw);
100 if (!(pvt = memget(sizeof *pvt))) {
101 memput(pw, sizeof *pw);
105 memset(pvt, 0, sizeof *pvt);
107 pvt->nis_domain = ((struct nis_p *)this->private)->domain;
109 pw->close = pw_close;
111 pw->byname = pw_byname;
112 pw->byuid = pw_byuid;
113 pw->rewind = pw_rewind;
114 pw->minimize = pw_minimize;
123 pw_close(struct irs_pw *this) {
124 struct pvt *pvt = (struct pvt *)this->private;
128 nisfree(pvt, do_all);
129 memput(pvt, sizeof *pvt);
130 memput(this, sizeof *this);
133 static struct passwd *
134 pw_next(struct irs_pw *this) {
135 struct pvt *pvt = (struct pvt *)this->private;
140 if (pvt->needrewind) {
141 nisfree(pvt, do_all);
142 r = yp_first(pvt->nis_domain, passwd_byname,
143 &pvt->curkey_data, &pvt->curkey_len,
144 &pvt->curval_data, &pvt->curval_len);
150 nisfree(pvt, do_val);
151 r = yp_next(pvt->nis_domain, passwd_byname,
152 pvt->curkey_data, pvt->curkey_len,
153 &newkey_data, &newkey_len,
154 &pvt->curval_data, &pvt->curval_len);
155 nisfree(pvt, do_key);
156 pvt->curkey_data = newkey_data;
157 pvt->curkey_len = newkey_len;
163 rval = makepasswdent(this);
164 } while (rval == NULL);
168 static struct passwd *
169 pw_byname(struct irs_pw *this, const char *name) {
170 struct pvt *pvt = (struct pvt *)this->private;
174 nisfree(pvt, do_val);
176 r = yp_match(pvt->nis_domain, passwd_byname, tmp, strlen(tmp),
177 &pvt->curval_data, &pvt->curval_len);
182 return (makepasswdent(this));
185 static struct passwd *
186 pw_byuid(struct irs_pw *this, uid_t uid) {
187 struct pvt *pvt = (struct pvt *)this->private;
188 char tmp[sizeof "4294967295"];
191 nisfree(pvt, do_val);
192 (void) sprintf(tmp, "%u", (unsigned int)uid);
193 r = yp_match(pvt->nis_domain, passwd_byuid, tmp, strlen(tmp),
194 &pvt->curval_data, &pvt->curval_len);
199 return (makepasswdent(this));
203 pw_rewind(struct irs_pw *this) {
204 struct pvt *pvt = (struct pvt *)this->private;
210 pw_minimize(struct irs_pw *this) {
217 static struct passwd *
218 makepasswdent(struct irs_pw *this) {
219 struct pvt *pvt = (struct pvt *)this->private;
222 memset(&pvt->passwd, 0, sizeof pvt->passwd);
225 pvt->pwbuf = pvt->curval_data;
226 pvt->curval_data = NULL;
229 pvt->passwd.pw_name = cp;
230 if (!(cp = strchr(cp, ':')))
233 pvt->passwd.pw_class = cp; /* Needs to point at a \0. */
237 pvt->passwd.pw_passwd = cp;
238 if (!(cp = strchr(cp, ':')))
242 pvt->passwd.pw_uid = atoi(cp);
243 if (!(cp = strchr(cp, ':')))
247 pvt->passwd.pw_gid = atoi(cp);
248 if (!(cp = strchr(cp, ':')))
252 pvt->passwd.pw_gecos = cp;
253 if (!(cp = strchr(cp, ':')))
257 pvt->passwd.pw_dir = cp;
258 if (!(cp = strchr(cp, ':')))
262 pvt->passwd.pw_shell = cp;
264 if ((cp = strchr(cp, '\n')) != NULL)
267 return (&pvt->passwd);
276 nisfree(struct pvt *pvt, enum do_what do_what) {
277 if ((do_what & do_key) && pvt->curkey_data) {
278 free(pvt->curkey_data);
279 pvt->curkey_data = NULL;
281 if ((do_what & do_val) && pvt->curval_data) {
282 free(pvt->curval_data);
283 pvt->curval_data = NULL;
287 #endif /* WANT_IRS_PW && WANT_IRS_NIS */