2 * Copyright (c) 1996,1999 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: nis_pw.c,v 1.16 1999/01/30 00:53:16 vixie 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;
173 nisfree(pvt, do_val);
174 r = yp_match(pvt->nis_domain, passwd_byname, name, strlen(name),
175 &pvt->curval_data, &pvt->curval_len);
180 return (makepasswdent(this));
183 static struct passwd *
184 pw_byuid(struct irs_pw *this, uid_t uid) {
185 struct pvt *pvt = (struct pvt *)this->private;
186 char tmp[sizeof "4294967295"];
189 nisfree(pvt, do_val);
190 (void) sprintf(tmp, "%u", (unsigned int)uid);
191 r = yp_match(pvt->nis_domain, passwd_byuid, tmp, strlen(tmp),
192 &pvt->curval_data, &pvt->curval_len);
197 return (makepasswdent(this));
201 pw_rewind(struct irs_pw *this) {
202 struct pvt *pvt = (struct pvt *)this->private;
208 pw_minimize(struct irs_pw *this) {
214 static struct passwd *
215 makepasswdent(struct irs_pw *this) {
216 struct pvt *pvt = (struct pvt *)this->private;
219 memset(&pvt->passwd, 0, sizeof pvt->passwd);
222 pvt->pwbuf = pvt->curval_data;
223 pvt->curval_data = NULL;
226 pvt->passwd.pw_name = cp;
227 if (!(cp = strchr(cp, ':')))
229 pvt->passwd.pw_class = cp; /* Needs to point at a \0. */
232 pvt->passwd.pw_passwd = cp;
233 if (!(cp = strchr(cp, ':')))
237 pvt->passwd.pw_uid = atoi(cp);
238 if (!(cp = strchr(cp, ':')))
242 pvt->passwd.pw_gid = atoi(cp);
243 if (!(cp = strchr(cp, ':')))
247 pvt->passwd.pw_gecos = cp;
248 if (!(cp = strchr(cp, ':')))
252 pvt->passwd.pw_dir = cp;
253 if (!(cp = strchr(cp, ':')))
257 pvt->passwd.pw_shell = cp;
259 if ((cp = strchr(cp, '\n')) != NULL)
262 return (&pvt->passwd);
271 nisfree(struct pvt *pvt, enum do_what do_what) {
272 if ((do_what & do_key) && pvt->curkey_data) {
273 free(pvt->curkey_data);
274 pvt->curkey_data = NULL;
276 if ((do_what & do_val) && pvt->curval_data) {
277 free(pvt->curval_data);
278 pvt->curval_data = NULL;
282 #endif /* WANT_IRS_PW && WANT_IRS_NIS */