1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/* @(#)names.c 1.17 06/09/13 Copyright 1993, 1995-2005 J. Schilling */
#ifndef lint
static char sccsid[] =
"@(#)names.c 1.17 06/09/13 Copyright 1993, 1995-2005 J. Schilling";
#endif
/*
* Handle user/group names for archive header
*
* Copyright (c) 1993, 1995-2005 J. Schilling
*/
/*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* See the file CDDL.Schily.txt in this distribution for details.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file CDDL.Schily.txt from this distribution.
*/
#include <schily/mconfig.h>
#include <stdio.h>
#include <schily/standard.h>
#include <schily/utypes.h>
#include <pwd.h>
#include <grp.h>
#include <schily/string.h>
#ifdef __STAR__
#include "star.h"
#include "starsubs.h"
#else
#define TUNMLEN 32
#define TGNMLEN 32
#endif
#include <schily/schily.h>
#define C_SIZE 16
typedef struct u_id {
uid_t uid;
char name[TUNMLEN+1];
char valid;
} uidc_t;
typedef struct g_id {
gid_t gid;
char name[TGNMLEN+1];
char valid;
} gidc_t;
LOCAL uidc_t uidcache[C_SIZE];
LOCAL int lastuidx; /* Last index for new entry */
EXPORT uid_t uid_nobody; /* Uid for user "nobody" */
LOCAL gidc_t gidcache[C_SIZE];
LOCAL int lastgidx; /* Last index for new entry */
EXPORT gid_t gid_nobody; /* Gid for user "nobody" */
EXPORT BOOL nameuid __PR((char *name, int namelen, uid_t uid));
EXPORT BOOL uidname __PR((char *name, int namelen, uid_t *uidp));
EXPORT BOOL namegid __PR((char *name, int namelen, gid_t gid));
EXPORT BOOL gidname __PR((char *name, int namelen, gid_t *gidp));
EXPORT void nameinit __PR((void));
/*
* Get name from uid
*/
EXPORT BOOL
nameuid(name, namelen, uid)
char *name;
int namelen;
uid_t uid;
{
struct passwd *pw;
register int i;
register uidc_t *idp;
for (i = 0, idp = uidcache; i < C_SIZE; i++, idp++) {
if (idp->valid == 0) /* Entry not yet filled */
break;
if (idp->uid == uid)
goto out;
}
idp = &uidcache[lastuidx++]; /* Round robin fill next ent */
if (lastuidx >= C_SIZE)
lastuidx = 0;
idp->uid = uid;
idp->name[0] = '\0';
idp->valid = 1;
if ((pw = getpwuid(uid)) != NULL) {
strncpy(idp->name, pw->pw_name, TUNMLEN);
idp->name[TUNMLEN] = '\0';
/*
* XXX We should find a better method than shortening the cache
*/
if (namelen <= (TUNMLEN+1))
idp->name[namelen-1] = 0;
}
out:
strcpy(name, idp->name);
return (name[0] != '\0');
}
/*
* Get uid from name
*/
EXPORT BOOL
uidname(name, namelen, uidp)
char *name;
int namelen;
uid_t *uidp;
{
struct passwd *pw;
register int len = namelen > TUNMLEN?TUNMLEN:namelen;
register int i;
register uidc_t *idp;
if (name[0] == '\0') {
*uidp = uid_nobody; /* Return UID_NOBODY */
return (FALSE);
}
for (i = 0, idp = uidcache; i < C_SIZE; i++, idp++) {
if (idp->valid == 0) /* Entry not yet filled */
break;
if (name[0] == idp->name[0] &&
strncmp(name, idp->name, len) == 0) {
*uidp = idp->uid;
if (idp->valid == 2) { /* Name not found */
*uidp = uid_nobody; /* Return UID_NOBODY */
return (FALSE);
}
return (TRUE);
}
}
idp = &uidcache[lastuidx++]; /* Round robin fill next ent */
if (lastuidx >= C_SIZE)
lastuidx = 0;
idp->uid = 0;
idp->name[0] = '\0';
strncpy(idp->name, name, len);
idp->name[len] = '\0';
idp->valid = 1;
if ((pw = getpwnam(idp->name)) != NULL) {
idp->uid = pw->pw_uid;
*uidp = idp->uid;
return (TRUE);
} else {
idp->valid = 2; /* Mark name as not found */
*uidp = uid_nobody; /* Return UID_NOBODY */
return (FALSE);
}
}
/*
* Get name from gid
*/
EXPORT BOOL
namegid(name, namelen, gid)
char *name;
int namelen;
gid_t gid;
{
struct group *gr;
register int i;
register gidc_t *idp;
for (i = 0, idp = gidcache; i < C_SIZE; i++, idp++) {
if (idp->valid == 0) /* Entry not yet filled */
break;
if (idp->gid == gid)
goto out;
}
idp = &gidcache[lastgidx++]; /* Round robin fill next ent */
if (lastgidx >= C_SIZE)
lastgidx = 0;
idp->gid = gid;
idp->name[0] = '\0';
idp->valid = 1;
if ((gr = getgrgid(gid)) != NULL) {
strncpy(idp->name, gr->gr_name, TGNMLEN);
idp->name[TGNMLEN] = '\0';
/*
* XXX We should find a better method than shortening the cache
*/
if (namelen <= (TGNMLEN+1))
idp->name[namelen-1] = 0;
}
out:
strcpy(name, idp->name);
return (name[0] != '\0');
}
/*
* Get gid from name
*/
EXPORT BOOL
gidname(name, namelen, gidp)
char *name;
int namelen;
gid_t *gidp;
{
struct group *gr;
register int len = namelen > TGNMLEN?TGNMLEN:namelen;
register int i;
register gidc_t *idp;
if (name[0] == '\0') {
*gidp = gid_nobody; /* Return GID_NOBODY */
return (FALSE);
}
for (i = 0, idp = gidcache; i < C_SIZE; i++, idp++) {
if (idp->valid == 0) /* Entry not yet filled */
break;
if (name[0] == idp->name[0] &&
strncmp(name, idp->name, len) == 0) {
*gidp = idp->gid;
if (idp->valid == 2) { /* Name not found */
*gidp = gid_nobody; /* Return GID_NOBODY */
return (FALSE);
}
return (TRUE);
}
}
idp = &gidcache[lastgidx++]; /* Round robin fill next ent */
if (lastgidx >= C_SIZE)
lastgidx = 0;
idp->gid = 0;
idp->name[0] = '\0';
strncpy(idp->name, name, len);
idp->name[len] = '\0';
idp->valid = 1;
if ((gr = getgrnam(idp->name)) != NULL) {
idp->gid = gr->gr_gid;
*gidp = idp->gid;
return (TRUE);
} else {
idp->valid = 2; /* Mark name as not found */
*gidp = gid_nobody; /* Return GID_NOBODY */
return (FALSE);
}
}
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifndef UID_NOBODY
#define UID_NOBODY 65534 /* The old SunOS-4.x and *BSD value */
#endif
#ifndef GID_NOBODY
#define GID_NOBODY 65534 /* The old SunOS-4.x and *BSD value */
#endif
EXPORT void
nameinit()
{
char *name;
int namelen;
uid_t uid;
gid_t gid;
name = "nobody";
namelen = strlen(name);
if (!uidname(name, namelen, &uid))
uid = UID_NOBODY;
uid_nobody = uid;
if (!gidname(name, namelen, &gid))
gid = GID_NOBODY;
gid_nobody = gid;
}
|