4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (c) 1996 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: irp_pw.c,v 1.4 2005/04/27 04:56:29 sra Exp";
22 #endif /* LIBC_SCCS and not lint */
26 #include "port_before.h"
29 static int __bind_irs_pw_unneeded
;
33 #include <sys/param.h>
48 #include <isc/memcluster.h>
49 #include <isc/irpmarshall.h>
51 #include "port_after.h"
60 struct irp_p
*girpdata
; /*%< global IRP data */
62 struct passwd passwd
; /*%< password structure */
67 static void pw_close(struct irs_pw
*);
68 static struct passwd
* pw_next(struct irs_pw
*);
69 static struct passwd
* pw_byname(struct irs_pw
*, const char *);
70 static struct passwd
* pw_byuid(struct irs_pw
*, uid_t
);
71 static void pw_rewind(struct irs_pw
*);
72 static void pw_minimize(struct irs_pw
*);
74 static void free_passwd(struct passwd
*pw
);
78 irs_irp_pw(struct irs_acc
*this) {
82 if (!(pw
= memget(sizeof *pw
))) {
86 memset(pw
, 0, sizeof *pw
);
88 if (!(pvt
= memget(sizeof *pvt
))) {
89 memput(pw
, sizeof *pw
);
93 memset(pvt
, 0, sizeof *pvt
);
94 pvt
->girpdata
= this->private;
99 pw
->byname
= pw_byname
;
100 pw
->byuid
= pw_byuid
;
101 pw
->rewind
= pw_rewind
;
102 pw
->minimize
= pw_minimize
;
110 * void pw_close(struct irs_pw *this)
115 pw_close(struct irs_pw
*this) {
116 struct pvt
*pvt
= (struct pvt
*)this->private;
120 free_passwd(&pvt
->passwd
);
122 memput(pvt
, sizeof *pvt
);
123 memput(this, sizeof *this);
127 * struct passwd * pw_next(struct irs_pw *this)
131 static struct passwd
*
132 pw_next(struct irs_pw
*this) {
133 struct pvt
*pvt
= (struct pvt
*)this->private;
134 struct passwd
*pw
= &pvt
->passwd
;
140 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
144 if (irs_irp_send_command(pvt
->girpdata
, "getpwent") != 0) {
148 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
150 &body
, &bodylen
) != 0) {
154 if (code
== IRPD_GETUSER_OK
) {
156 if (irp_unmarshall_pw(pw
, body
) != 0) {
164 memput(body
, bodylen
);
171 * struct passwd * pw_byname(struct irs_pw *this, const char *name)
175 static struct passwd
*
176 pw_byname(struct irs_pw
*this, const char *name
) {
177 struct pvt
*pvt
= (struct pvt
*)this->private;
178 struct passwd
*pw
= &pvt
->passwd
;
184 if (pw
->pw_name
!= NULL
&& strcmp(name
, pw
->pw_name
) == 0) {
188 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
192 if (irs_irp_send_command(pvt
->girpdata
, "getpwnam %s", name
) != 0) {
196 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
198 &body
, &bodylen
) != 0) {
202 if (code
== IRPD_GETUSER_OK
) {
204 if (irp_unmarshall_pw(pw
, body
) != 0) {
212 memput(body
, bodylen
);
219 * struct passwd * pw_byuid(struct irs_pw *this, uid_t uid)
223 static struct passwd
*
224 pw_byuid(struct irs_pw
*this, uid_t uid
) {
225 struct pvt
*pvt
= (struct pvt
*)this->private;
230 struct passwd
*pw
= &pvt
->passwd
;
232 if (pw
->pw_name
!= NULL
&& pw
->pw_uid
== uid
) {
236 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
240 if (irs_irp_send_command(pvt
->girpdata
, "getpwuid %d", uid
) != 0) {
244 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
246 &body
, &bodylen
) != 0) {
250 if (code
== IRPD_GETUSER_OK
) {
252 if (irp_unmarshall_pw(pw
, body
) != 0) {
260 memput(body
, bodylen
);
267 * void pw_rewind(struct irs_pw *this)
272 pw_rewind(struct irs_pw
*this) {
273 struct pvt
*pvt
= (struct pvt
*)this->private;
277 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
281 if (irs_irp_send_command(pvt
->girpdata
, "setpwent") != 0) {
285 code
= irs_irp_read_response(pvt
->girpdata
, text
, sizeof text
);
286 if (code
!= IRPD_GETUSER_SETOK
) {
287 if (irp_log_errors
) {
288 syslog(LOG_WARNING
, "setpwent failed: %s", text
);
296 * void pw_minimize(struct irs_pw *this)
301 pw_minimize(struct irs_pw
*this) {
302 struct pvt
*pvt
= (struct pvt
*)this->private;
304 irs_irp_disconnect(pvt
->girpdata
);
311 * Deallocate all the memory irp_unmarshall_pw allocated.
316 free_passwd(struct passwd
*pw
) {
320 if (pw
->pw_name
!= NULL
)
323 if (pw
->pw_passwd
!= NULL
)
327 if (pw
->pw_class
!= NULL
)
331 if (pw
->pw_gecos
!= NULL
)
334 if (pw
->pw_dir
!= NULL
)
337 if (pw
->pw_shell
!= NULL
)
341 #endif /* WANT_IRS_PW */