1/*	$NetBSD: compat_getpwent.c,v 1.2 2009/01/11 02:46:25 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *        This product includes software developed by the NetBSD
21 *        Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38#include <sys/cdefs.h>
39#if defined(LIBC_SCCS) && !defined(lint)
40__RCSID("$NetBSD: compat_getpwent.c,v 1.2 2009/01/11 02:46:25 christos Exp $");
41#endif /* LIBC_SCCS and not lint */
42
43#define __LIBC12_SOURCE__
44
45#include "namespace.h"
46#include <stdint.h>
47#include <stdlib.h>
48#include <pwd.h>
49#include <compat/include/pwd.h>
50
51__warn_references(getpwuid,
52    "warning: reference to compatibility getpwuid(); include <pwd.h> to generate correct reference")
53__warn_references(getpwnam,
54    "warning: reference to compatibility getpwnam(); include <pwd.h> to generate correct reference")
55__warn_references(getpwnam_r,
56    "warning: reference to compatibility getpwnam_r(); include <pwd.h> to generate correct reference")
57__warn_references(getpwuid_r,
58    "warning: reference to compatibility getpwuid_r(); include <pwd.h> to generate correct reference")
59__warn_references(getpwent,
60    "warning: reference to compatibility getpwent(); include <pwd.h> to generate correct reference")
61#ifdef notdef /* for libutil */
62__warn_references(pw_scan,
63    "warning: reference to compatibility pw_scan(); include <pwd.h> to generate correct reference")
64#endif
65__warn_references(getpwent_r,
66    "warning: reference to compatibility getpwent_r(); include <pwd.h> to generate correct reference")
67__warn_references(pwcache_userdb,
68    "warning: reference to compatibility pwcache_userdb(); include <pwd.h> to generate correct reference")
69
70#ifdef __weak_alias
71__weak_alias(getpwent, _getpwent)
72__weak_alias(getpwent_r, _getpwent_r)
73__weak_alias(getpwuid, _getpwuid)
74__weak_alias(getpwuid_r, _getpwuid_r)
75__weak_alias(getpwnam, _getpwnam)
76__weak_alias(getpwnam_r, _getpwnam_r)
77__weak_alias(pwcache_userdb, _pwcache_userdb)
78#endif
79
80static struct passwd50 *
81cvt(struct passwd *p)
82{
83	struct passwd50 *q = (void *)p;
84
85	if (q == NULL) {
86		return NULL;
87	}
88	q->pw_change = (int32_t)p->pw_change;
89	q->pw_class = p->pw_class;
90	q->pw_gecos = p->pw_gecos;
91	q->pw_dir = p->pw_dir;
92	q->pw_shell = p->pw_shell;
93	q->pw_expire = (int32_t)p->pw_expire;
94	return q;
95}
96
97struct passwd50	*
98getpwuid(uid_t uid)
99{
100	return cvt(__getpwuid50(uid));
101
102}
103
104struct passwd50	*
105getpwnam(const char *name)
106{
107	return cvt(__getpwnam50(name));
108}
109
110int
111getpwnam_r(const char *name , struct passwd50 *p, char *buf, size_t len,
112    struct passwd50 **q)
113{
114	struct passwd px, *qx;
115	int rv = __getpwnam_r50(name, &px, buf, len, &qx);
116	*q = p;
117	passwd_to_passwd50(&px, p);
118	return rv;
119}
120
121int
122getpwuid_r(uid_t uid, struct passwd50 *p, char *buf, size_t len,
123    struct passwd50 **q)
124{
125	struct passwd px, *qx;
126	int rv = __getpwuid_r50(uid, &px, buf, len, &qx);
127	*q = p;
128	passwd_to_passwd50(&px, p);
129	return rv;
130}
131
132struct passwd50	*
133getpwent(void)
134{
135	return cvt(__getpwent50());
136}
137
138#ifdef notdef /* for libutil */
139int
140pw_scan(char *buf, struct passwd50 *p, int *flags)
141{
142	struct passwd px;
143	int rv = __pw_scan50(buf, &px, flags);
144	passwd_to_passwd50(&px, p);
145	return rv;
146}
147#endif
148
149int
150getpwent_r(struct passwd50 *p, char *buf, size_t len, struct passwd50 **q)
151{
152	struct passwd px, *qx;
153	int rv = __getpwent_r50(&px, buf, len, &qx);
154	*q = p;
155	passwd_to_passwd50(&px, p);
156	return rv;
157}
158
159static struct passwd50 * (*__getpwnamf)(const char *);
160static struct passwd50 * (*__getpwuidf)(uid_t);
161static struct passwd pw;
162
163static struct passwd *
164internal_getpwnam(const char *name)
165{
166	struct passwd50 *p = (*__getpwnamf)(name);
167	passwd50_to_passwd(p, &pw);
168	return &pw;
169}
170
171static struct passwd *
172internal_getpwuid(uid_t uid)
173{
174	struct passwd50 *p = (*__getpwuidf)(uid);
175	passwd50_to_passwd(p, &pw);
176	return &pw;
177}
178
179int pwcache_userdb(int (*setpassentf)(int), void (*endpwentf)(void),
180    struct passwd50 * (*getpwnamf)(const char *),
181    struct passwd50 * (*getpwuidf)(uid_t))
182{
183	__getpwnamf = getpwnamf;
184	__getpwuidf = getpwuidf;
185	return __pwcache_userdb50(setpassentf, endpwentf, internal_getpwnam,
186	    internal_getpwuid);
187}
188