1*5bbd2a12Schristos /* $NetBSD: irp_pw.c,v 1.1.1.2 2012/09/09 16:07:51 christos Exp $ */
2b5677b36Schristos
3b5677b36Schristos /*
4b5677b36Schristos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5b5677b36Schristos * Portions Copyright (c) 1996 by Internet Software Consortium.
6b5677b36Schristos *
7b5677b36Schristos * Permission to use, copy, modify, and distribute this software for any
8b5677b36Schristos * purpose with or without fee is hereby granted, provided that the above
9b5677b36Schristos * copyright notice and this permission notice appear in all copies.
10b5677b36Schristos *
11b5677b36Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12b5677b36Schristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13b5677b36Schristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14b5677b36Schristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15b5677b36Schristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16b5677b36Schristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17b5677b36Schristos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18b5677b36Schristos */
19b5677b36Schristos
20b5677b36Schristos #if defined(LIBC_SCCS) && !defined(lint)
21b5677b36Schristos static const char rcsid[] = "Id: irp_pw.c,v 1.4 2005/04/27 04:56:29 sra Exp ";
22b5677b36Schristos #endif /* LIBC_SCCS and not lint */
23b5677b36Schristos
24b5677b36Schristos /* Extern */
25b5677b36Schristos
26b5677b36Schristos #include "port_before.h"
27b5677b36Schristos
28b5677b36Schristos #ifndef WANT_IRS_PW
29b5677b36Schristos static int __bind_irs_pw_unneeded;
30b5677b36Schristos #else
31b5677b36Schristos
32b5677b36Schristos #include <syslog.h>
33b5677b36Schristos #include <sys/param.h>
34b5677b36Schristos
35b5677b36Schristos #include <db.h>
36b5677b36Schristos #include <errno.h>
37b5677b36Schristos #include <fcntl.h>
38b5677b36Schristos #include <limits.h>
39b5677b36Schristos #include <pwd.h>
40b5677b36Schristos #include <stdlib.h>
41b5677b36Schristos #include <string.h>
42b5677b36Schristos #include <syslog.h>
43b5677b36Schristos #include <utmp.h>
44b5677b36Schristos #include <unistd.h>
45b5677b36Schristos
46b5677b36Schristos #include <irs.h>
47b5677b36Schristos #include <irp.h>
48b5677b36Schristos #include <isc/memcluster.h>
49b5677b36Schristos #include <isc/irpmarshall.h>
50b5677b36Schristos
51b5677b36Schristos #include "port_after.h"
52b5677b36Schristos
53b5677b36Schristos #include "irs_p.h"
54b5677b36Schristos #include "irp_p.h"
55b5677b36Schristos
56b5677b36Schristos
57b5677b36Schristos /* Types */
58b5677b36Schristos
59b5677b36Schristos struct pvt {
60b5677b36Schristos struct irp_p *girpdata; /*%< global IRP data */
61b5677b36Schristos int warned;
62b5677b36Schristos struct passwd passwd; /*%< password structure */
63b5677b36Schristos };
64b5677b36Schristos
65b5677b36Schristos /* Forward */
66b5677b36Schristos
67b5677b36Schristos static void pw_close(struct irs_pw *);
68b5677b36Schristos static struct passwd * pw_next(struct irs_pw *);
69b5677b36Schristos static struct passwd * pw_byname(struct irs_pw *, const char *);
70b5677b36Schristos static struct passwd * pw_byuid(struct irs_pw *, uid_t);
71b5677b36Schristos static void pw_rewind(struct irs_pw *);
72b5677b36Schristos static void pw_minimize(struct irs_pw *);
73b5677b36Schristos
74b5677b36Schristos static void free_passwd(struct passwd *pw);
75b5677b36Schristos
76b5677b36Schristos /* Public */
77b5677b36Schristos struct irs_pw *
irs_irp_pw(struct irs_acc * this)78b5677b36Schristos irs_irp_pw(struct irs_acc *this) {
79b5677b36Schristos struct irs_pw *pw;
80b5677b36Schristos struct pvt *pvt;
81b5677b36Schristos
82b5677b36Schristos if (!(pw = memget(sizeof *pw))) {
83b5677b36Schristos errno = ENOMEM;
84b5677b36Schristos return (NULL);
85b5677b36Schristos }
86b5677b36Schristos memset(pw, 0, sizeof *pw);
87b5677b36Schristos
88b5677b36Schristos if (!(pvt = memget(sizeof *pvt))) {
89b5677b36Schristos memput(pw, sizeof *pw);
90b5677b36Schristos errno = ENOMEM;
91b5677b36Schristos return (NULL);
92b5677b36Schristos }
93b5677b36Schristos memset(pvt, 0, sizeof *pvt);
94b5677b36Schristos pvt->girpdata = this->private;
95b5677b36Schristos
96b5677b36Schristos pw->private = pvt;
97b5677b36Schristos pw->close = pw_close;
98b5677b36Schristos pw->next = pw_next;
99b5677b36Schristos pw->byname = pw_byname;
100b5677b36Schristos pw->byuid = pw_byuid;
101b5677b36Schristos pw->rewind = pw_rewind;
102b5677b36Schristos pw->minimize = pw_minimize;
103b5677b36Schristos
104b5677b36Schristos return (pw);
105b5677b36Schristos }
106b5677b36Schristos
107b5677b36Schristos /* Methods */
108b5677b36Schristos
109b5677b36Schristos /*%
110b5677b36Schristos * void pw_close(struct irs_pw *this)
111b5677b36Schristos *
112b5677b36Schristos */
113b5677b36Schristos
114b5677b36Schristos static void
pw_close(struct irs_pw * this)115b5677b36Schristos pw_close(struct irs_pw *this) {
116b5677b36Schristos struct pvt *pvt = (struct pvt *)this->private;
117b5677b36Schristos
118b5677b36Schristos pw_minimize(this);
119b5677b36Schristos
120b5677b36Schristos free_passwd(&pvt->passwd);
121b5677b36Schristos
122b5677b36Schristos memput(pvt, sizeof *pvt);
123b5677b36Schristos memput(this, sizeof *this);
124b5677b36Schristos }
125b5677b36Schristos
126b5677b36Schristos /*%
127b5677b36Schristos * struct passwd * pw_next(struct irs_pw *this)
128b5677b36Schristos *
129b5677b36Schristos */
130b5677b36Schristos
131b5677b36Schristos static struct passwd *
pw_next(struct irs_pw * this)132b5677b36Schristos pw_next(struct irs_pw *this) {
133b5677b36Schristos struct pvt *pvt = (struct pvt *)this->private;
134b5677b36Schristos struct passwd *pw = &pvt->passwd;
135b5677b36Schristos char *body;
136b5677b36Schristos size_t bodylen;
137b5677b36Schristos int code;
138b5677b36Schristos char text[256];
139b5677b36Schristos
140b5677b36Schristos if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
141b5677b36Schristos return (NULL);
142b5677b36Schristos }
143b5677b36Schristos
144b5677b36Schristos if (irs_irp_send_command(pvt->girpdata, "getpwent") != 0) {
145b5677b36Schristos return (NULL);
146b5677b36Schristos }
147b5677b36Schristos
148b5677b36Schristos if (irs_irp_get_full_response(pvt->girpdata, &code,
149b5677b36Schristos text, sizeof text,
150b5677b36Schristos &body, &bodylen) != 0) {
151b5677b36Schristos return (NULL);
152b5677b36Schristos }
153b5677b36Schristos
154b5677b36Schristos if (code == IRPD_GETUSER_OK) {
155b5677b36Schristos free_passwd(pw);
156b5677b36Schristos if (irp_unmarshall_pw(pw, body) != 0) {
157b5677b36Schristos pw = NULL;
158b5677b36Schristos }
159b5677b36Schristos } else {
160b5677b36Schristos pw = NULL;
161b5677b36Schristos }
162b5677b36Schristos
163b5677b36Schristos if (body != NULL) {
164b5677b36Schristos memput(body, bodylen);
165b5677b36Schristos }
166b5677b36Schristos
167b5677b36Schristos return (pw);
168b5677b36Schristos }
169b5677b36Schristos
170b5677b36Schristos /*%
171b5677b36Schristos * struct passwd * pw_byname(struct irs_pw *this, const char *name)
172b5677b36Schristos *
173b5677b36Schristos */
174b5677b36Schristos
175b5677b36Schristos static struct passwd *
pw_byname(struct irs_pw * this,const char * name)176b5677b36Schristos pw_byname(struct irs_pw *this, const char *name) {
177b5677b36Schristos struct pvt *pvt = (struct pvt *)this->private;
178b5677b36Schristos struct passwd *pw = &pvt->passwd;
179b5677b36Schristos char *body = NULL;
180b5677b36Schristos char text[256];
181b5677b36Schristos size_t bodylen;
182b5677b36Schristos int code;
183b5677b36Schristos
184b5677b36Schristos if (pw->pw_name != NULL && strcmp(name, pw->pw_name) == 0) {
185b5677b36Schristos return (pw);
186b5677b36Schristos }
187b5677b36Schristos
188b5677b36Schristos if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
189b5677b36Schristos return (NULL);
190b5677b36Schristos }
191b5677b36Schristos
192b5677b36Schristos if (irs_irp_send_command(pvt->girpdata, "getpwnam %s", name) != 0) {
193b5677b36Schristos return (NULL);
194b5677b36Schristos }
195b5677b36Schristos
196b5677b36Schristos if (irs_irp_get_full_response(pvt->girpdata, &code,
197b5677b36Schristos text, sizeof text,
198b5677b36Schristos &body, &bodylen) != 0) {
199b5677b36Schristos return (NULL);
200b5677b36Schristos }
201b5677b36Schristos
202b5677b36Schristos if (code == IRPD_GETUSER_OK) {
203b5677b36Schristos free_passwd(pw);
204b5677b36Schristos if (irp_unmarshall_pw(pw, body) != 0) {
205b5677b36Schristos pw = NULL;
206b5677b36Schristos }
207b5677b36Schristos } else {
208b5677b36Schristos pw = NULL;
209b5677b36Schristos }
210b5677b36Schristos
211b5677b36Schristos if (body != NULL) {
212b5677b36Schristos memput(body, bodylen);
213b5677b36Schristos }
214b5677b36Schristos
215b5677b36Schristos return (pw);
216b5677b36Schristos }
217b5677b36Schristos
218b5677b36Schristos /*%
219b5677b36Schristos * struct passwd * pw_byuid(struct irs_pw *this, uid_t uid)
220b5677b36Schristos *
221b5677b36Schristos */
222b5677b36Schristos
223b5677b36Schristos static struct passwd *
pw_byuid(struct irs_pw * this,uid_t uid)224b5677b36Schristos pw_byuid(struct irs_pw *this, uid_t uid) {
225b5677b36Schristos struct pvt *pvt = (struct pvt *)this->private;
226b5677b36Schristos char *body;
227b5677b36Schristos char text[256];
228b5677b36Schristos size_t bodylen;
229b5677b36Schristos int code;
230b5677b36Schristos struct passwd *pw = &pvt->passwd;
231b5677b36Schristos
232b5677b36Schristos if (pw->pw_name != NULL && pw->pw_uid == uid) {
233b5677b36Schristos return (pw);
234b5677b36Schristos }
235b5677b36Schristos
236b5677b36Schristos if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
237b5677b36Schristos return (NULL);
238b5677b36Schristos }
239b5677b36Schristos
240b5677b36Schristos if (irs_irp_send_command(pvt->girpdata, "getpwuid %d", uid) != 0) {
241b5677b36Schristos return (NULL);
242b5677b36Schristos }
243b5677b36Schristos
244b5677b36Schristos if (irs_irp_get_full_response(pvt->girpdata, &code,
245b5677b36Schristos text, sizeof text,
246b5677b36Schristos &body, &bodylen) != 0) {
247b5677b36Schristos return (NULL);
248b5677b36Schristos }
249b5677b36Schristos
250b5677b36Schristos if (code == IRPD_GETUSER_OK) {
251b5677b36Schristos free_passwd(pw);
252b5677b36Schristos if (irp_unmarshall_pw(pw, body) != 0) {
253b5677b36Schristos pw = NULL;
254b5677b36Schristos }
255b5677b36Schristos } else {
256b5677b36Schristos pw = NULL;
257b5677b36Schristos }
258b5677b36Schristos
259b5677b36Schristos if (body != NULL) {
260b5677b36Schristos memput(body, bodylen);
261b5677b36Schristos }
262b5677b36Schristos
263b5677b36Schristos return (pw);
264b5677b36Schristos }
265b5677b36Schristos
266b5677b36Schristos /*%
267b5677b36Schristos * void pw_rewind(struct irs_pw *this)
268b5677b36Schristos *
269b5677b36Schristos */
270b5677b36Schristos
271b5677b36Schristos static void
pw_rewind(struct irs_pw * this)272b5677b36Schristos pw_rewind(struct irs_pw *this) {
273b5677b36Schristos struct pvt *pvt = (struct pvt *)this->private;
274b5677b36Schristos char text[256];
275b5677b36Schristos int code;
276b5677b36Schristos
277b5677b36Schristos if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
278b5677b36Schristos return;
279b5677b36Schristos }
280b5677b36Schristos
281b5677b36Schristos if (irs_irp_send_command(pvt->girpdata, "setpwent") != 0) {
282b5677b36Schristos return;
283b5677b36Schristos }
284b5677b36Schristos
285b5677b36Schristos code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
286b5677b36Schristos if (code != IRPD_GETUSER_SETOK) {
287b5677b36Schristos if (irp_log_errors) {
288b5677b36Schristos syslog(LOG_WARNING, "setpwent failed: %s", text);
289b5677b36Schristos }
290b5677b36Schristos }
291b5677b36Schristos
292b5677b36Schristos return;
293b5677b36Schristos }
294b5677b36Schristos
295b5677b36Schristos /*%
296b5677b36Schristos * void pw_minimize(struct irs_pw *this)
297b5677b36Schristos *
298b5677b36Schristos */
299b5677b36Schristos
300b5677b36Schristos static void
pw_minimize(struct irs_pw * this)301b5677b36Schristos pw_minimize(struct irs_pw *this) {
302b5677b36Schristos struct pvt *pvt = (struct pvt *)this->private;
303b5677b36Schristos
304b5677b36Schristos irs_irp_disconnect(pvt->girpdata);
305b5677b36Schristos }
306b5677b36Schristos
307b5677b36Schristos
308b5677b36Schristos /* Private. */
309b5677b36Schristos
310b5677b36Schristos /*%
311b5677b36Schristos * Deallocate all the memory irp_unmarshall_pw allocated.
312b5677b36Schristos *
313b5677b36Schristos */
314b5677b36Schristos
315b5677b36Schristos static void
free_passwd(struct passwd * pw)316b5677b36Schristos free_passwd(struct passwd *pw) {
317b5677b36Schristos if (pw == NULL)
318b5677b36Schristos return;
319b5677b36Schristos
320b5677b36Schristos if (pw->pw_name != NULL)
321b5677b36Schristos free(pw->pw_name);
322b5677b36Schristos
323b5677b36Schristos if (pw->pw_passwd != NULL)
324b5677b36Schristos free(pw->pw_passwd);
325b5677b36Schristos
326b5677b36Schristos #ifdef HAVE_PW_CLASS
327b5677b36Schristos if (pw->pw_class != NULL)
328b5677b36Schristos free(pw->pw_class);
329b5677b36Schristos #endif
330b5677b36Schristos
331b5677b36Schristos if (pw->pw_gecos != NULL)
332b5677b36Schristos free(pw->pw_gecos);
333b5677b36Schristos
334b5677b36Schristos if (pw->pw_dir != NULL)
335b5677b36Schristos free(pw->pw_dir);
336b5677b36Schristos
337b5677b36Schristos if (pw->pw_shell != NULL)
338b5677b36Schristos free(pw->pw_shell);
339b5677b36Schristos }
340b5677b36Schristos
341b5677b36Schristos #endif /* WANT_IRS_PW */
342b5677b36Schristos /*! \file */
343