4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid
[] = "Id: nis_pw.c,v 1.4 2005/04/27 04:56:33 sra Exp";
22 #endif /* LIBC_SCCS and not lint */
26 #include "port_before.h"
28 #if !defined(WANT_IRS_PW) || !defined(WANT_IRS_NIS)
29 static int __bind_irs_pw_unneeded
;
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <arpa/nameser.h>
37 #include <isc/memcluster.h>
40 #include <rpcsvc/yp_prot.h>
41 #include <rpcsvc/ypclnt.h>
51 #include <isc/memcluster.h>
55 #include "port_after.h"
73 enum do_what
{ do_none
= 0x0, do_key
= 0x1, do_val
= 0x2, do_all
= 0x3 };
75 static /*const*/ char passwd_byname
[] = "passwd.byname";
76 static /*const*/ char passwd_byuid
[] = "passwd.byuid";
80 static void pw_close(struct irs_pw
*);
81 static struct passwd
* pw_next(struct irs_pw
*);
82 static struct passwd
* pw_byname(struct irs_pw
*, const char *);
83 static struct passwd
* pw_byuid(struct irs_pw
*, uid_t
);
84 static void pw_rewind(struct irs_pw
*);
85 static void pw_minimize(struct irs_pw
*);
87 static struct passwd
* makepasswdent(struct irs_pw
*);
88 static void nisfree(struct pvt
*, enum do_what
);
93 irs_nis_pw(struct irs_acc
*this) {
97 if (!(pw
= memget(sizeof *pw
))) {
101 memset(pw
, 0x5e, sizeof *pw
);
102 if (!(pvt
= memget(sizeof *pvt
))) {
103 memput(pw
, sizeof *pw
);
107 memset(pvt
, 0, sizeof *pvt
);
109 pvt
->nis_domain
= ((struct nis_p
*)this->private)->domain
;
111 pw
->close
= pw_close
;
113 pw
->byname
= pw_byname
;
114 pw
->byuid
= pw_byuid
;
115 pw
->rewind
= pw_rewind
;
116 pw
->minimize
= pw_minimize
;
125 pw_close(struct irs_pw
*this) {
126 struct pvt
*pvt
= (struct pvt
*)this->private;
130 nisfree(pvt
, do_all
);
131 memput(pvt
, sizeof *pvt
);
132 memput(this, sizeof *this);
135 static struct passwd
*
136 pw_next(struct irs_pw
*this) {
137 struct pvt
*pvt
= (struct pvt
*)this->private;
142 if (pvt
->needrewind
) {
143 nisfree(pvt
, do_all
);
144 r
= yp_first(pvt
->nis_domain
, passwd_byname
,
145 &pvt
->curkey_data
, &pvt
->curkey_len
,
146 &pvt
->curval_data
, &pvt
->curval_len
);
152 nisfree(pvt
, do_val
);
153 r
= yp_next(pvt
->nis_domain
, passwd_byname
,
154 pvt
->curkey_data
, pvt
->curkey_len
,
155 &newkey_data
, &newkey_len
,
156 &pvt
->curval_data
, &pvt
->curval_len
);
157 nisfree(pvt
, do_key
);
158 pvt
->curkey_data
= newkey_data
;
159 pvt
->curkey_len
= newkey_len
;
165 rval
= makepasswdent(this);
166 } while (rval
== NULL
);
170 static struct passwd
*
171 pw_byname(struct irs_pw
*this, const char *name
) {
172 struct pvt
*pvt
= (struct pvt
*)this->private;
176 nisfree(pvt
, do_val
);
178 r
= yp_match(pvt
->nis_domain
, passwd_byname
, tmp
, strlen(tmp
),
179 &pvt
->curval_data
, &pvt
->curval_len
);
184 return (makepasswdent(this));
187 static struct passwd
*
188 pw_byuid(struct irs_pw
*this, uid_t uid
) {
189 struct pvt
*pvt
= (struct pvt
*)this->private;
190 char tmp
[sizeof "4294967295"];
193 nisfree(pvt
, do_val
);
194 (void) sprintf(tmp
, "%u", (unsigned int)uid
);
195 r
= yp_match(pvt
->nis_domain
, passwd_byuid
, tmp
, strlen(tmp
),
196 &pvt
->curval_data
, &pvt
->curval_len
);
201 return (makepasswdent(this));
205 pw_rewind(struct irs_pw
*this) {
206 struct pvt
*pvt
= (struct pvt
*)this->private;
212 pw_minimize(struct irs_pw
*this) {
219 static struct passwd
*
220 makepasswdent(struct irs_pw
*this) {
221 struct pvt
*pvt
= (struct pvt
*)this->private;
224 memset(&pvt
->passwd
, 0, sizeof pvt
->passwd
);
227 pvt
->pwbuf
= pvt
->curval_data
;
228 pvt
->curval_data
= NULL
;
231 pvt
->passwd
.pw_name
= cp
;
232 if (!(cp
= strchr(cp
, ':')))
235 pvt
->passwd
.pw_class
= cp
; /*%< Needs to point at a \0. */
239 pvt
->passwd
.pw_passwd
= cp
;
240 if (!(cp
= strchr(cp
, ':')))
244 pvt
->passwd
.pw_uid
= atoi(cp
);
245 if (!(cp
= strchr(cp
, ':')))
249 pvt
->passwd
.pw_gid
= atoi(cp
);
250 if (!(cp
= strchr(cp
, ':')))
254 pvt
->passwd
.pw_gecos
= cp
;
255 if (!(cp
= strchr(cp
, ':')))
259 pvt
->passwd
.pw_dir
= cp
;
260 if (!(cp
= strchr(cp
, ':')))
264 pvt
->passwd
.pw_shell
= cp
;
266 if ((cp
= strchr(cp
, '\n')) != NULL
)
269 return (&pvt
->passwd
);
278 nisfree(struct pvt
*pvt
, enum do_what do_what
) {
279 if ((do_what
& do_key
) && pvt
->curkey_data
) {
280 free(pvt
->curkey_data
);
281 pvt
->curkey_data
= NULL
;
283 if ((do_what
& do_val
) && pvt
->curval_data
) {
284 free(pvt
->curval_data
);
285 pvt
->curval_data
= NULL
;
289 #endif /* WANT_IRS_PW && WANT_IRS_NIS */