xref: /netbsd-src/lib/libc/compat/gen/compat_getpwent.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
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 
80 static struct passwd50 *
81 cvt(struct passwd *p)
82 {
83 	struct passwd50 *q = (void *)p;
84 	q->pw_change = (int32_t)p->pw_change;
85 	q->pw_class = p->pw_class;
86 	q->pw_gecos = p->pw_gecos;
87 	q->pw_dir = p->pw_dir;
88 	q->pw_shell = p->pw_shell;
89 	q->pw_expire = (int32_t)p->pw_expire;
90 	return q;
91 }
92 
93 struct passwd50	*
94 getpwuid(uid_t uid)
95 {
96 	return cvt(__getpwuid50(uid));
97 
98 }
99 
100 struct passwd50	*
101 getpwnam(const char *name)
102 {
103 	return cvt(__getpwnam50(name));
104 }
105 
106 int
107 getpwnam_r(const char *name , struct passwd50 *p, char *buf, size_t len,
108     struct passwd50 **q)
109 {
110 	struct passwd px, *qx;
111 	int rv = __getpwnam_r50(name, &px, buf, len, &qx);
112 	*q = p;
113 	passwd_to_passwd50(&px, p);
114 	return rv;
115 }
116 
117 int
118 getpwuid_r(uid_t uid, struct passwd50 *p, char *buf, size_t len,
119     struct passwd50 **q)
120 {
121 	struct passwd px, *qx;
122 	int rv = __getpwuid_r50(uid, &px, buf, len, &qx);
123 	*q = p;
124 	passwd_to_passwd50(&px, p);
125 	return rv;
126 }
127 
128 struct passwd50	*
129 getpwent(void)
130 {
131 	return cvt(__getpwent50());
132 }
133 
134 #ifdef notdef /* for libutil */
135 int
136 pw_scan(char *buf, struct passwd50 *p, int *flags)
137 {
138 	struct passwd px;
139 	int rv = __pw_scan50(buf, &px, flags);
140 	passwd_to_passwd50(&px, p);
141 	return rv;
142 }
143 #endif
144 
145 int
146 getpwent_r(struct passwd50 *p, char *buf, size_t len, struct passwd50 **q)
147 {
148 	struct passwd px, *qx;
149 	int rv = __getpwent_r50(&px, buf, len, &qx);
150 	*q = p;
151 	passwd_to_passwd50(&px, p);
152 	return rv;
153 }
154 
155 static struct passwd50 * (*__getpwnamf)(const char *);
156 static struct passwd50 * (*__getpwuidf)(uid_t);
157 static struct passwd pw;
158 
159 static struct passwd *
160 internal_getpwnam(const char *name)
161 {
162 	struct passwd50 *p = (*__getpwnamf)(name);
163 	passwd50_to_passwd(p, &pw);
164 	return &pw;
165 }
166 
167 static struct passwd *
168 internal_getpwuid(uid_t uid)
169 {
170 	struct passwd50 *p = (*__getpwuidf)(uid);
171 	passwd50_to_passwd(p, &pw);
172 	return &pw;
173 }
174 
175 int pwcache_userdb(int (*setpassentf)(int), void (*endpwentf)(void),
176     struct passwd50 * (*getpwnamf)(const char *),
177     struct passwd50 * (*getpwuidf)(uid_t))
178 {
179 	__getpwnamf = getpwnamf;
180 	__getpwuidf = getpwuidf;
181 	return __pwcache_userdb50(setpassentf, endpwentf, internal_getpwnam,
182 	    internal_getpwuid);
183 }
184