Home | History | Annotate | Line # | Download | only in gen
compat_getpwent.c revision 1.1.2.1
      1 /*	$NetBSD: compat_getpwent.c,v 1.1.2.1 2008/11/08 21:49:34 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.1.2.1 2008/11/08 21:49:34 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 static struct passwd50 *
     71 cvt(struct passwd *p)
     72 {
     73 	struct passwd50 *q = (void *)p;
     74 	q->pw_change = (int32_t)p->pw_change;
     75 	q->pw_class = p->pw_class;
     76 	q->pw_gecos = p->pw_gecos;
     77 	q->pw_dir = p->pw_dir;
     78 	q->pw_shell = p->pw_shell;
     79 	q->pw_expire = (int32_t)p->pw_expire;
     80 	return q;
     81 }
     82 
     83 static void
     84 passwd_to_passwd50(const struct passwd *p, struct passwd50 *q)
     85 {
     86 	q->pw_name = p->pw_name;
     87 	q->pw_passwd = p->pw_passwd;
     88 	q->pw_uid = p->pw_uid;
     89 	q->pw_gid = p->pw_gid;
     90 	q->pw_change = (int32_t)p->pw_change;
     91 	q->pw_class = p->pw_class;
     92 	q->pw_gecos = p->pw_gecos;
     93 	q->pw_dir = p->pw_dir;
     94 	q->pw_shell = p->pw_shell;
     95 	q->pw_expire = (int32_t)p->pw_expire;
     96 }
     97 
     98 static void
     99 passwd50_to_passwd(const struct passwd50 *p, struct passwd *q)
    100 {
    101 	q->pw_name = p->pw_name;
    102 	q->pw_passwd = p->pw_passwd;
    103 	q->pw_uid = p->pw_uid;
    104 	q->pw_gid = p->pw_gid;
    105 	q->pw_change = p->pw_change;
    106 	q->pw_class = p->pw_class;
    107 	q->pw_gecos = p->pw_gecos;
    108 	q->pw_dir = p->pw_dir;
    109 	q->pw_shell = p->pw_shell;
    110 	q->pw_expire = p->pw_expire;
    111 }
    112 
    113 struct passwd50	*
    114 getpwuid(uid_t uid)
    115 {
    116 	return cvt(__getpwuid50(uid));
    117 
    118 }
    119 
    120 struct passwd50	*
    121 getpwnam(const char *name)
    122 {
    123 	return cvt(__getpwnam50(name));
    124 }
    125 
    126 int
    127 getpwnam_r(const char *name , struct passwd50 *p, char *buf, size_t len,
    128     struct passwd50 **q)
    129 {
    130 	struct passwd px, *qx;
    131 	int rv = __getpwnam_r50(name, &px, buf, len, &qx);
    132 	*q = p;
    133 	passwd_to_passwd50(&px, p);
    134 	return rv;
    135 }
    136 
    137 int
    138 getpwuid_r(uid_t uid, struct passwd50 *p, char *buf, size_t len,
    139     struct passwd50 **q)
    140 {
    141 	struct passwd px, *qx;
    142 	int rv = __getpwuid_r50(uid, &px, buf, len, &qx);
    143 	*q = p;
    144 	passwd_to_passwd50(&px, p);
    145 	return rv;
    146 }
    147 
    148 struct passwd50	*
    149 getpwent(void)
    150 {
    151 	return cvt(__getpwent50());
    152 }
    153 
    154 #ifdef notdef /* for libutil */
    155 int
    156 pw_scan(char *buf, struct passwd50 *p, int *flags)
    157 {
    158 	struct passwd px;
    159 	int rv = __pw_scan50(buf, &px, flags);
    160 	passwd_to_passwd50(&px, p);
    161 	return rv;
    162 }
    163 #endif
    164 
    165 int
    166 getpwent_r(struct passwd50 *p, char *buf, size_t len, struct passwd50 **q)
    167 {
    168 	struct passwd px, *qx;
    169 	int rv = __getpwent_r50(&px, buf, len, &qx);
    170 	*q = p;
    171 	passwd_to_passwd50(&px, p);
    172 	return rv;
    173 }
    174 
    175 static struct passwd50 * (*__getpwnamf)(const char *);
    176 static struct passwd50 * (*__getpwuidf)(uid_t);
    177 static struct passwd pw;
    178 
    179 static struct passwd *
    180 internal_getpwnam(const char *name)
    181 {
    182 	struct passwd50 *p = (*__getpwnamf)(name);
    183 	passwd50_to_passwd(p, &pw);
    184 	return &pw;
    185 }
    186 
    187 static struct passwd *
    188 internal_getpwuid(uid_t uid)
    189 {
    190 	struct passwd50 *p = (*__getpwuidf)(uid);
    191 	passwd50_to_passwd(p, &pw);
    192 	return &pw;
    193 }
    194 
    195 int pwcache_userdb(int (*setpassentf)(int), void (*endpwentf)(void),
    196     struct passwd50 * (*getpwnamf)(const char *),
    197     struct passwd50 * (*getpwuidf)(uid_t))
    198 {
    199 	__getpwnamf = getpwnamf;
    200 	__getpwuidf = getpwuidf;
    201 	return __pwcache_userdb50(setpassentf, endpwentf, internal_getpwnam,
    202 	    internal_getpwuid);
    203 }
    204