1*8a29be6bSyamt /* $NetBSD: compat_getpwent.c,v 1.3 2009/06/01 06:04:37 yamt Exp $ */
2461a86f9Schristos
3461a86f9Schristos /*-
4461a86f9Schristos * Copyright (c) 2008 The NetBSD Foundation, Inc.
5461a86f9Schristos * All rights reserved.
6461a86f9Schristos *
7461a86f9Schristos * This code is derived from software contributed to The NetBSD Foundation
8461a86f9Schristos * by Christos Zoulas.
9461a86f9Schristos *
10461a86f9Schristos * Redistribution and use in source and binary forms, with or without
11461a86f9Schristos * modification, are permitted provided that the following conditions
12461a86f9Schristos * are met:
13461a86f9Schristos * 1. Redistributions of source code must retain the above copyright
14461a86f9Schristos * notice, this list of conditions and the following disclaimer.
15461a86f9Schristos * 2. Redistributions in binary form must reproduce the above copyright
16461a86f9Schristos * notice, this list of conditions and the following disclaimer in the
17461a86f9Schristos * documentation and/or other materials provided with the distribution.
18461a86f9Schristos * 3. All advertising materials mentioning features or use of this software
19461a86f9Schristos * must display the following acknowledgement:
20461a86f9Schristos * This product includes software developed by the NetBSD
21461a86f9Schristos * Foundation, Inc. and its contributors.
22461a86f9Schristos * 4. Neither the name of The NetBSD Foundation nor the names of its
23461a86f9Schristos * contributors may be used to endorse or promote products derived
24461a86f9Schristos * from this software without specific prior written permission.
25461a86f9Schristos *
26461a86f9Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27461a86f9Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28461a86f9Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29461a86f9Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30461a86f9Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31461a86f9Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32461a86f9Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33461a86f9Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34461a86f9Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35461a86f9Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36461a86f9Schristos * POSSIBILITY OF SUCH DAMAGE.
37461a86f9Schristos */
38461a86f9Schristos #include <sys/cdefs.h>
39461a86f9Schristos #if defined(LIBC_SCCS) && !defined(lint)
40*8a29be6bSyamt __RCSID("$NetBSD: compat_getpwent.c,v 1.3 2009/06/01 06:04:37 yamt Exp $");
41461a86f9Schristos #endif /* LIBC_SCCS and not lint */
42461a86f9Schristos
43461a86f9Schristos #define __LIBC12_SOURCE__
44461a86f9Schristos
45461a86f9Schristos #include "namespace.h"
46461a86f9Schristos #include <stdint.h>
47461a86f9Schristos #include <stdlib.h>
48461a86f9Schristos #include <pwd.h>
49461a86f9Schristos #include <compat/include/pwd.h>
50461a86f9Schristos
51461a86f9Schristos __warn_references(getpwuid,
52461a86f9Schristos "warning: reference to compatibility getpwuid(); include <pwd.h> to generate correct reference")
53461a86f9Schristos __warn_references(getpwnam,
54461a86f9Schristos "warning: reference to compatibility getpwnam(); include <pwd.h> to generate correct reference")
55461a86f9Schristos __warn_references(getpwnam_r,
56461a86f9Schristos "warning: reference to compatibility getpwnam_r(); include <pwd.h> to generate correct reference")
57461a86f9Schristos __warn_references(getpwuid_r,
58461a86f9Schristos "warning: reference to compatibility getpwuid_r(); include <pwd.h> to generate correct reference")
59461a86f9Schristos __warn_references(getpwent,
60461a86f9Schristos "warning: reference to compatibility getpwent(); include <pwd.h> to generate correct reference")
61461a86f9Schristos #ifdef notdef /* for libutil */
62461a86f9Schristos __warn_references(pw_scan,
63461a86f9Schristos "warning: reference to compatibility pw_scan(); include <pwd.h> to generate correct reference")
64461a86f9Schristos #endif
65461a86f9Schristos __warn_references(getpwent_r,
66461a86f9Schristos "warning: reference to compatibility getpwent_r(); include <pwd.h> to generate correct reference")
67461a86f9Schristos __warn_references(pwcache_userdb,
68461a86f9Schristos "warning: reference to compatibility pwcache_userdb(); include <pwd.h> to generate correct reference")
69461a86f9Schristos
70461a86f9Schristos #ifdef __weak_alias
__weak_alias(getpwent,_getpwent)71461a86f9Schristos __weak_alias(getpwent, _getpwent)
72461a86f9Schristos __weak_alias(getpwent_r, _getpwent_r)
73461a86f9Schristos __weak_alias(getpwuid, _getpwuid)
74461a86f9Schristos __weak_alias(getpwuid_r, _getpwuid_r)
75461a86f9Schristos __weak_alias(getpwnam, _getpwnam)
76461a86f9Schristos __weak_alias(getpwnam_r, _getpwnam_r)
77461a86f9Schristos __weak_alias(pwcache_userdb, _pwcache_userdb)
78461a86f9Schristos #endif
79461a86f9Schristos
80461a86f9Schristos static struct passwd50 *
81461a86f9Schristos cvt(struct passwd *p)
82461a86f9Schristos {
83461a86f9Schristos struct passwd50 *q = (void *)p;
84*8a29be6bSyamt
85*8a29be6bSyamt if (q == NULL) {
86*8a29be6bSyamt return NULL;
87*8a29be6bSyamt }
88461a86f9Schristos q->pw_change = (int32_t)p->pw_change;
89461a86f9Schristos q->pw_class = p->pw_class;
90461a86f9Schristos q->pw_gecos = p->pw_gecos;
91461a86f9Schristos q->pw_dir = p->pw_dir;
92461a86f9Schristos q->pw_shell = p->pw_shell;
93461a86f9Schristos q->pw_expire = (int32_t)p->pw_expire;
94461a86f9Schristos return q;
95461a86f9Schristos }
96461a86f9Schristos
97461a86f9Schristos struct passwd50 *
getpwuid(uid_t uid)98461a86f9Schristos getpwuid(uid_t uid)
99461a86f9Schristos {
100461a86f9Schristos return cvt(__getpwuid50(uid));
101461a86f9Schristos
102461a86f9Schristos }
103461a86f9Schristos
104461a86f9Schristos struct passwd50 *
getpwnam(const char * name)105461a86f9Schristos getpwnam(const char *name)
106461a86f9Schristos {
107461a86f9Schristos return cvt(__getpwnam50(name));
108461a86f9Schristos }
109461a86f9Schristos
110461a86f9Schristos int
getpwnam_r(const char * name,struct passwd50 * p,char * buf,size_t len,struct passwd50 ** q)111461a86f9Schristos getpwnam_r(const char *name , struct passwd50 *p, char *buf, size_t len,
112461a86f9Schristos struct passwd50 **q)
113461a86f9Schristos {
114461a86f9Schristos struct passwd px, *qx;
115461a86f9Schristos int rv = __getpwnam_r50(name, &px, buf, len, &qx);
116461a86f9Schristos *q = p;
117461a86f9Schristos passwd_to_passwd50(&px, p);
118461a86f9Schristos return rv;
119461a86f9Schristos }
120461a86f9Schristos
121461a86f9Schristos int
getpwuid_r(uid_t uid,struct passwd50 * p,char * buf,size_t len,struct passwd50 ** q)122461a86f9Schristos getpwuid_r(uid_t uid, struct passwd50 *p, char *buf, size_t len,
123461a86f9Schristos struct passwd50 **q)
124461a86f9Schristos {
125461a86f9Schristos struct passwd px, *qx;
126461a86f9Schristos int rv = __getpwuid_r50(uid, &px, buf, len, &qx);
127461a86f9Schristos *q = p;
128461a86f9Schristos passwd_to_passwd50(&px, p);
129461a86f9Schristos return rv;
130461a86f9Schristos }
131461a86f9Schristos
132461a86f9Schristos struct passwd50 *
getpwent(void)133461a86f9Schristos getpwent(void)
134461a86f9Schristos {
135461a86f9Schristos return cvt(__getpwent50());
136461a86f9Schristos }
137461a86f9Schristos
138461a86f9Schristos #ifdef notdef /* for libutil */
139461a86f9Schristos int
pw_scan(char * buf,struct passwd50 * p,int * flags)140461a86f9Schristos pw_scan(char *buf, struct passwd50 *p, int *flags)
141461a86f9Schristos {
142461a86f9Schristos struct passwd px;
143461a86f9Schristos int rv = __pw_scan50(buf, &px, flags);
144461a86f9Schristos passwd_to_passwd50(&px, p);
145461a86f9Schristos return rv;
146461a86f9Schristos }
147461a86f9Schristos #endif
148461a86f9Schristos
149461a86f9Schristos int
getpwent_r(struct passwd50 * p,char * buf,size_t len,struct passwd50 ** q)150461a86f9Schristos getpwent_r(struct passwd50 *p, char *buf, size_t len, struct passwd50 **q)
151461a86f9Schristos {
152461a86f9Schristos struct passwd px, *qx;
153461a86f9Schristos int rv = __getpwent_r50(&px, buf, len, &qx);
154461a86f9Schristos *q = p;
155461a86f9Schristos passwd_to_passwd50(&px, p);
156461a86f9Schristos return rv;
157461a86f9Schristos }
158461a86f9Schristos
159461a86f9Schristos static struct passwd50 * (*__getpwnamf)(const char *);
160461a86f9Schristos static struct passwd50 * (*__getpwuidf)(uid_t);
161461a86f9Schristos static struct passwd pw;
162461a86f9Schristos
163461a86f9Schristos static struct passwd *
internal_getpwnam(const char * name)164461a86f9Schristos internal_getpwnam(const char *name)
165461a86f9Schristos {
166461a86f9Schristos struct passwd50 *p = (*__getpwnamf)(name);
167461a86f9Schristos passwd50_to_passwd(p, &pw);
168461a86f9Schristos return &pw;
169461a86f9Schristos }
170461a86f9Schristos
171461a86f9Schristos static struct passwd *
internal_getpwuid(uid_t uid)172461a86f9Schristos internal_getpwuid(uid_t uid)
173461a86f9Schristos {
174461a86f9Schristos struct passwd50 *p = (*__getpwuidf)(uid);
175461a86f9Schristos passwd50_to_passwd(p, &pw);
176461a86f9Schristos return &pw;
177461a86f9Schristos }
178461a86f9Schristos
pwcache_userdb(int (* setpassentf)(int),void (* endpwentf)(void),struct passwd50 * (* getpwnamf)(const char *),struct passwd50 * (* getpwuidf)(uid_t))179461a86f9Schristos int pwcache_userdb(int (*setpassentf)(int), void (*endpwentf)(void),
180461a86f9Schristos struct passwd50 * (*getpwnamf)(const char *),
181461a86f9Schristos struct passwd50 * (*getpwuidf)(uid_t))
182461a86f9Schristos {
183461a86f9Schristos __getpwnamf = getpwnamf;
184461a86f9Schristos __getpwuidf = getpwuidf;
185461a86f9Schristos return __pwcache_userdb50(setpassentf, endpwentf, internal_getpwnam,
186461a86f9Schristos internal_getpwuid);
187461a86f9Schristos }
188