Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:19

0001 /*
0002  * Copyright (c) 1989, 1993
0003  *  The Regents of the University of California.  All rights reserved.
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions
0007  * are met:
0008  * 1. Redistributions of source code must retain the above copyright
0009  *    notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *    notice, this list of conditions and the following disclaimer in the
0012  *    documentation and/or other materials provided with the distribution.
0013  * 4. Neither the name of the University nor the names of its contributors
0014  *    may be used to endorse or promote products derived from this software
0015  *    without specific prior written permission.
0016  *
0017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
0018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0027  * SUCH DAMAGE.
0028  */
0029 
0030 #ifdef HAVE_CONFIG_H
0031 #include "config.h"
0032 #endif
0033 
0034 #if defined(LIBC_SCCS) && !defined(lint)
0035 static char sccsid[] = "@(#)pwcache.c   8.1 (Berkeley) 6/4/93";
0036 #endif /* LIBC_SCCS and not lint */
0037 #include <sys/cdefs.h>
0038 __FBSDID("$FreeBSD: src/lib/libc/gen/pwcache.c,v 1.11 2007/01/09 00:27:55 imp Exp $");
0039 
0040 #include <sys/types.h>
0041 
0042 #include <grp.h>
0043 #include <pwd.h>
0044 #include <stdio.h>
0045 #include <string.h>
0046 
0047 #include "internal.h"
0048 
0049 #define UT_NAMESIZE 64
0050 
0051 #define NCACHE  64          /* power of 2 */
0052 #define MASK    (NCACHE - 1)        /* bits to store with */
0053 
0054 const char *
0055 user_from_uid(uid_t uid, int nouser)
0056 {
0057     static struct ncache {
0058         uid_t   uid;
0059         int found;
0060         char    name[UT_NAMESIZE + 1];
0061     } c_uid[NCACHE];
0062     static int pwopen;
0063     struct passwd *pw;
0064     struct ncache *cp;
0065 
0066     cp = c_uid + (uid & MASK);
0067     if (cp->uid != uid || !*cp->name) {
0068         if (pwopen == 0) {
0069             //setpassent(1);
0070             pwopen = 1;
0071         }
0072         pw = getpwuid(uid);
0073         cp->uid = uid;
0074         if (pw != NULL) {
0075             cp->found = 1;
0076             (void)strncpy(cp->name, pw->pw_name, UT_NAMESIZE);
0077             cp->name[UT_NAMESIZE] = '\0';
0078         } else {
0079             cp->found = 0;
0080             (void)snprintf(cp->name, UT_NAMESIZE, "%u", uid);
0081             if (nouser)
0082                 return (NULL);
0083         }
0084     }
0085     return ((nouser && !cp->found) ? NULL : cp->name);
0086 }
0087 
0088 char *
0089 group_from_gid(gid_t gid, int nogroup)
0090 {
0091     static struct ncache {
0092         gid_t   gid;
0093         int found;
0094         char    name[UT_NAMESIZE + 1];
0095     } c_gid[NCACHE];
0096     static int gropen;
0097     struct group *gr;
0098     struct ncache *cp;
0099 
0100     cp = c_gid + (gid & MASK);
0101     if (cp->gid != gid || !*cp->name) {
0102         if (gropen == 0) {
0103             //setgroupent(1);
0104             gropen = 1;
0105         }
0106         gr = getgrgid(gid);
0107         cp->gid = gid;
0108         if (gr != NULL) {
0109             cp->found = 1;
0110             (void)strncpy(cp->name, gr->gr_name, UT_NAMESIZE);
0111             cp->name[UT_NAMESIZE] = '\0';
0112         } else {
0113             cp->found = 0;
0114             (void)snprintf(cp->name, UT_NAMESIZE, "%u", gid);
0115             if (nogroup)
0116                 return (NULL);
0117         }
0118     }
0119     return ((nogroup && !cp->found) ? NULL : cp->name);
0120 }
0121