Home | History | Annotate | Line # | Download | only in gen
      1 /*	$NetBSD: compat_getpwent.c,v 1.3 2009/06/01 06:04:37 yamt 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.3 2009/06/01 06:04:37 yamt 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 
     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 
     97 struct passwd50	*
     98 getpwuid(uid_t uid)
     99 {
    100 	return cvt(__getpwuid50(uid));
    101 
    102 }
    103 
    104 struct passwd50	*
    105 getpwnam(const char *name)
    106 {
    107 	return cvt(__getpwnam50(name));
    108 }
    109 
    110 int
    111 getpwnam_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 
    121 int
    122 getpwuid_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 
    132 struct passwd50	*
    133 getpwent(void)
    134 {
    135 	return cvt(__getpwent50());
    136 }
    137 
    138 #ifdef notdef /* for libutil */
    139 int
    140 pw_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 
    149 int
    150 getpwent_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 
    159 static struct passwd50 * (*__getpwnamf)(const char *);
    160 static struct passwd50 * (*__getpwuidf)(uid_t);
    161 static struct passwd pw;
    162 
    163 static struct passwd *
    164 internal_getpwnam(const char *name)
    165 {
    166 	struct passwd50 *p = (*__getpwnamf)(name);
    167 	passwd50_to_passwd(p, &pw);
    168 	return &pw;
    169 }
    170 
    171 static struct passwd *
    172 internal_getpwuid(uid_t uid)
    173 {
    174 	struct passwd50 *p = (*__getpwuidf)(uid);
    175 	passwd50_to_passwd(p, &pw);
    176 	return &pw;
    177 }
    178 
    179 int 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