Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / irp_pw.c
blobd9256d3bf0417c8b024855f4844ebcfe45c94a42
1 /* $NetBSD$ */
3 /*
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 */
24 /* Extern */
26 #include "port_before.h"
28 #ifndef WANT_IRS_PW
29 static int __bind_irs_pw_unneeded;
30 #else
32 #include <syslog.h>
33 #include <sys/param.h>
35 #include <db.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <limits.h>
39 #include <pwd.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <syslog.h>
43 #include <utmp.h>
44 #include <unistd.h>
46 #include <irs.h>
47 #include <irp.h>
48 #include <isc/memcluster.h>
49 #include <isc/irpmarshall.h>
51 #include "port_after.h"
53 #include "irs_p.h"
54 #include "irp_p.h"
57 /* Types */
59 struct pvt {
60 struct irp_p *girpdata; /*%< global IRP data */
61 int warned;
62 struct passwd passwd; /*%< password structure */
65 /* Forward */
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);
76 /* Public */
77 struct irs_pw *
78 irs_irp_pw(struct irs_acc *this) {
79 struct irs_pw *pw;
80 struct pvt *pvt;
82 if (!(pw = memget(sizeof *pw))) {
83 errno = ENOMEM;
84 return (NULL);
86 memset(pw, 0, sizeof *pw);
88 if (!(pvt = memget(sizeof *pvt))) {
89 memput(pw, sizeof *pw);
90 errno = ENOMEM;
91 return (NULL);
93 memset(pvt, 0, sizeof *pvt);
94 pvt->girpdata = this->private;
96 pw->private = pvt;
97 pw->close = pw_close;
98 pw->next = pw_next;
99 pw->byname = pw_byname;
100 pw->byuid = pw_byuid;
101 pw->rewind = pw_rewind;
102 pw->minimize = pw_minimize;
104 return (pw);
107 /* Methods */
110 * void pw_close(struct irs_pw *this)
114 static void
115 pw_close(struct irs_pw *this) {
116 struct pvt *pvt = (struct pvt *)this->private;
118 pw_minimize(this);
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;
135 char *body;
136 size_t bodylen;
137 int code;
138 char text[256];
140 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
141 return (NULL);
144 if (irs_irp_send_command(pvt->girpdata, "getpwent") != 0) {
145 return (NULL);
148 if (irs_irp_get_full_response(pvt->girpdata, &code,
149 text, sizeof text,
150 &body, &bodylen) != 0) {
151 return (NULL);
154 if (code == IRPD_GETUSER_OK) {
155 free_passwd(pw);
156 if (irp_unmarshall_pw(pw, body) != 0) {
157 pw = NULL;
159 } else {
160 pw = NULL;
163 if (body != NULL) {
164 memput(body, bodylen);
167 return (pw);
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;
179 char *body = NULL;
180 char text[256];
181 size_t bodylen;
182 int code;
184 if (pw->pw_name != NULL && strcmp(name, pw->pw_name) == 0) {
185 return (pw);
188 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
189 return (NULL);
192 if (irs_irp_send_command(pvt->girpdata, "getpwnam %s", name) != 0) {
193 return (NULL);
196 if (irs_irp_get_full_response(pvt->girpdata, &code,
197 text, sizeof text,
198 &body, &bodylen) != 0) {
199 return (NULL);
202 if (code == IRPD_GETUSER_OK) {
203 free_passwd(pw);
204 if (irp_unmarshall_pw(pw, body) != 0) {
205 pw = NULL;
207 } else {
208 pw = NULL;
211 if (body != NULL) {
212 memput(body, bodylen);
215 return (pw);
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;
226 char *body;
227 char text[256];
228 size_t bodylen;
229 int code;
230 struct passwd *pw = &pvt->passwd;
232 if (pw->pw_name != NULL && pw->pw_uid == uid) {
233 return (pw);
236 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
237 return (NULL);
240 if (irs_irp_send_command(pvt->girpdata, "getpwuid %d", uid) != 0) {
241 return (NULL);
244 if (irs_irp_get_full_response(pvt->girpdata, &code,
245 text, sizeof text,
246 &body, &bodylen) != 0) {
247 return (NULL);
250 if (code == IRPD_GETUSER_OK) {
251 free_passwd(pw);
252 if (irp_unmarshall_pw(pw, body) != 0) {
253 pw = NULL;
255 } else {
256 pw = NULL;
259 if (body != NULL) {
260 memput(body, bodylen);
263 return (pw);
267 * void pw_rewind(struct irs_pw *this)
271 static void
272 pw_rewind(struct irs_pw *this) {
273 struct pvt *pvt = (struct pvt *)this->private;
274 char text[256];
275 int code;
277 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
278 return;
281 if (irs_irp_send_command(pvt->girpdata, "setpwent") != 0) {
282 return;
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);
292 return;
296 * void pw_minimize(struct irs_pw *this)
300 static void
301 pw_minimize(struct irs_pw *this) {
302 struct pvt *pvt = (struct pvt *)this->private;
304 irs_irp_disconnect(pvt->girpdata);
308 /* Private. */
311 * Deallocate all the memory irp_unmarshall_pw allocated.
315 static void
316 free_passwd(struct passwd *pw) {
317 if (pw == NULL)
318 return;
320 if (pw->pw_name != NULL)
321 free(pw->pw_name);
323 if (pw->pw_passwd != NULL)
324 free(pw->pw_passwd);
326 #ifdef HAVE_PW_CLASS
327 if (pw->pw_class != NULL)
328 free(pw->pw_class);
329 #endif
331 if (pw->pw_gecos != NULL)
332 free(pw->pw_gecos);
334 if (pw->pw_dir != NULL)
335 free(pw->pw_dir);
337 if (pw->pw_shell != NULL)
338 free(pw->pw_shell);
341 #endif /* WANT_IRS_PW */
342 /*! \file */