2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (c) 1996 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: irp_pw.c,v 1.4 2005/04/27 04:56:29 sra Exp $";
20 #endif /* LIBC_SCCS and not lint */
24 #include "port_before.h"
27 static int __bind_irs_pw_unneeded
;
31 #include <sys/param.h>
46 #include <isc/memcluster.h>
47 #include <isc/irpmarshall.h>
49 #include "port_after.h"
58 struct irp_p
*girpdata
; /*%< global IRP data */
60 struct passwd passwd
; /*%< password structure */
65 static void pw_close(struct irs_pw
*);
66 static struct passwd
* pw_next(struct irs_pw
*);
67 static struct passwd
* pw_byname(struct irs_pw
*, const char *);
68 static struct passwd
* pw_byuid(struct irs_pw
*, uid_t
);
69 static void pw_rewind(struct irs_pw
*);
70 static void pw_minimize(struct irs_pw
*);
72 static void free_passwd(struct passwd
*pw
);
76 irs_irp_pw(struct irs_acc
*this) {
80 if (!(pw
= memget(sizeof *pw
))) {
84 memset(pw
, 0, sizeof *pw
);
86 if (!(pvt
= memget(sizeof *pvt
))) {
87 memput(pw
, sizeof *pw
);
91 memset(pvt
, 0, sizeof *pvt
);
92 pvt
->girpdata
= this->private;
97 pw
->byname
= pw_byname
;
99 pw
->rewind
= pw_rewind
;
100 pw
->minimize
= pw_minimize
;
108 * void pw_close(struct irs_pw *this)
113 pw_close(struct irs_pw
*this) {
114 struct pvt
*pvt
= (struct pvt
*)this->private;
118 free_passwd(&pvt
->passwd
);
120 memput(pvt
, sizeof *pvt
);
121 memput(this, sizeof *this);
125 * struct passwd * pw_next(struct irs_pw *this)
129 static struct passwd
*
130 pw_next(struct irs_pw
*this) {
131 struct pvt
*pvt
= (struct pvt
*)this->private;
132 struct passwd
*pw
= &pvt
->passwd
;
138 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
142 if (irs_irp_send_command(pvt
->girpdata
, "getpwent") != 0) {
146 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
148 &body
, &bodylen
) != 0) {
152 if (code
== IRPD_GETUSER_OK
) {
154 if (irp_unmarshall_pw(pw
, body
) != 0) {
162 memput(body
, bodylen
);
169 * struct passwd * pw_byname(struct irs_pw *this, const char *name)
173 static struct passwd
*
174 pw_byname(struct irs_pw
*this, const char *name
) {
175 struct pvt
*pvt
= (struct pvt
*)this->private;
176 struct passwd
*pw
= &pvt
->passwd
;
182 if (pw
->pw_name
!= NULL
&& strcmp(name
, pw
->pw_name
) == 0) {
186 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
190 if (irs_irp_send_command(pvt
->girpdata
, "getpwnam %s", name
) != 0) {
194 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
196 &body
, &bodylen
) != 0) {
200 if (code
== IRPD_GETUSER_OK
) {
202 if (irp_unmarshall_pw(pw
, body
) != 0) {
210 memput(body
, bodylen
);
217 * struct passwd * pw_byuid(struct irs_pw *this, uid_t uid)
221 static struct passwd
*
222 pw_byuid(struct irs_pw
*this, uid_t uid
) {
223 struct pvt
*pvt
= (struct pvt
*)this->private;
228 struct passwd
*pw
= &pvt
->passwd
;
230 if (pw
->pw_name
!= NULL
&& pw
->pw_uid
== uid
) {
234 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
238 if (irs_irp_send_command(pvt
->girpdata
, "getpwuid %d", uid
) != 0) {
242 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
244 &body
, &bodylen
) != 0) {
248 if (code
== IRPD_GETUSER_OK
) {
250 if (irp_unmarshall_pw(pw
, body
) != 0) {
258 memput(body
, bodylen
);
265 * void pw_rewind(struct irs_pw *this)
270 pw_rewind(struct irs_pw
*this) {
271 struct pvt
*pvt
= (struct pvt
*)this->private;
275 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
279 if (irs_irp_send_command(pvt
->girpdata
, "setpwent") != 0) {
283 code
= irs_irp_read_response(pvt
->girpdata
, text
, sizeof text
);
284 if (code
!= IRPD_GETUSER_SETOK
) {
285 if (irp_log_errors
) {
286 syslog(LOG_WARNING
, "setpwent failed: %s", text
);
294 * void pw_minimize(struct irs_pw *this)
299 pw_minimize(struct irs_pw
*this) {
300 struct pvt
*pvt
= (struct pvt
*)this->private;
302 irs_irp_disconnect(pvt
->girpdata
);
309 * Deallocate all the memory irp_unmarshall_pw allocated.
314 free_passwd(struct passwd
*pw
) {
318 if (pw
->pw_name
!= NULL
)
321 if (pw
->pw_passwd
!= NULL
)
325 if (pw
->pw_class
!= NULL
)
329 if (pw
->pw_gecos
!= NULL
)
332 if (pw
->pw_dir
!= NULL
)
335 if (pw
->pw_shell
!= NULL
)
339 #endif /* WANT_IRS_PW */