| 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 |
#include <sys/types.h> |
| 39 |
#include <sys/stat.h> |
| 40 |
#if HAVE_GRP_H |
| 41 |
#include <grp.h> |
| 42 |
#endif |
| 43 |
#include <pwd.h> |
| 44 |
#include <stdio.h> |
| 45 |
#include <stdlib.h> |
| 46 |
#include <string.h> |
| 47 |
#if HAVE_STRINGS_H |
| 48 |
#include <strings.h> |
| 49 |
#endif |
| 50 |
|
| 51 |
#include "pax.h" |
| 52 |
#include "extern.h" |
| 53 |
|
| 54 |
#if HAVE_UGID_FROM_UG |
| 55 |
# error do not include this file if you do not need it |
| 56 |
#endif |
| 57 |
|
| 58 |
__RCSID("$MirOS: src/bin/pax/cache.c,v 1.11 2018/12/13 07:09:09 tg Exp $"); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
#define UNMLEN 32 |
| 72 |
#define GNMLEN 32 |
| 73 |
#define UID_SZ 317 |
| 74 |
#define UNM_SZ 317 |
| 75 |
#define GID_SZ 251 |
| 76 |
#define GNM_SZ 317 |
| 77 |
#define VALID 1 |
| 78 |
#define INVALID 2 |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
typedef struct uidc { |
| 85 |
int valid; |
| 86 |
char name[UNMLEN]; |
| 87 |
uid_t uid; |
| 88 |
} UIDC; |
| 89 |
|
| 90 |
typedef struct gidc { |
| 91 |
int valid; |
| 92 |
char name[GNMLEN]; |
| 93 |
gid_t gid; |
| 94 |
} GIDC; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
static int pwopn = 0; |
| 104 |
static int gropn = 0; |
| 105 |
#if !HAVE_UG_FROM_UGID |
| 106 |
static UIDC **uidtb = NULL; |
| 107 |
static GIDC **gidtb = NULL; |
| 108 |
#endif |
| 109 |
static UIDC **usrtb = NULL; |
| 110 |
static GIDC **grptb = NULL; |
| 111 |
|
| 112 |
#if !HAVE_UG_FROM_UGID |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
int |
| 121 |
uidtb_start(void) |
| 122 |
{ |
| 123 |
static int fail = 0; |
| 124 |
|
| 125 |
if (uidtb != NULL) |
| 126 |
return(0); |
| 127 |
if (fail) |
| 128 |
return(-1); |
| 129 |
if ((uidtb = calloc(UID_SZ, sizeof(UIDC *))) == NULL) { |
| 130 |
++fail; |
| 131 |
paxwarn(1, "%s for %s", "Out of memory", |
| 132 |
"user id cache table"); |
| 133 |
return (-1); |
| 134 |
} |
| 135 |
return(0); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
int |
| 146 |
gidtb_start(void) |
| 147 |
{ |
| 148 |
static int fail = 0; |
| 149 |
|
| 150 |
if (gidtb != NULL) |
| 151 |
return(0); |
| 152 |
if (fail) |
| 153 |
return(-1); |
| 154 |
if ((gidtb = calloc(GID_SZ, sizeof(GIDC *))) == NULL) { |
| 155 |
++fail; |
| 156 |
paxwarn(1, "%s for %s", "Out of memory", |
| 157 |
"group id cache table"); |
| 158 |
return (-1); |
| 159 |
} |
| 160 |
return(0); |
| 161 |
} |
| 162 |
#endif |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
int |
| 172 |
usrtb_start(void) |
| 173 |
{ |
| 174 |
static int fail = 0; |
| 175 |
|
| 176 |
if (usrtb != NULL) |
| 177 |
return(0); |
| 178 |
if (fail) |
| 179 |
return(-1); |
| 180 |
if ((usrtb = calloc(UNM_SZ, sizeof(UIDC *))) == NULL) { |
| 181 |
++fail; |
| 182 |
paxwarn(1, "%s for %s", "Out of memory", |
| 183 |
"user name cache table"); |
| 184 |
return (-1); |
| 185 |
} |
| 186 |
return(0); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
int |
| 197 |
grptb_start(void) |
| 198 |
{ |
| 199 |
static int fail = 0; |
| 200 |
|
| 201 |
if (grptb != NULL) |
| 202 |
return(0); |
| 203 |
if (fail) |
| 204 |
return(-1); |
| 205 |
if ((grptb = calloc(GNM_SZ, sizeof(GIDC *))) == NULL) { |
| 206 |
++fail; |
| 207 |
paxwarn(1,"%s for %s", "Out of memory", |
| 208 |
"group name cache table"); |
| 209 |
return (-1); |
| 210 |
} |
| 211 |
return(0); |
| 212 |
} |
| 213 |
|
| 214 |
#if !HAVE_UG_FROM_UGID |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
const char * |
| 224 |
name_uid(uid_t uid, int frc) |
| 225 |
{ |
| 226 |
struct passwd *pw; |
| 227 |
UIDC *ptr; |
| 228 |
|
| 229 |
if ((uidtb == NULL) && (uidtb_start() < 0)) |
| 230 |
return(""); |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
ptr = uidtb[uid % UID_SZ]; |
| 236 |
if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) { |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
if (frc || (ptr->valid == VALID)) |
| 241 |
return(ptr->name); |
| 242 |
return(""); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
if (!pwopn) { |
| 249 |
#if HAVE_SETPGENT |
| 250 |
setpassent(1); |
| 251 |
#else |
| 252 |
setpwent(); |
| 253 |
#endif |
| 254 |
++pwopn; |
| 255 |
} |
| 256 |
if (ptr == NULL) |
| 257 |
ptr = uidtb[uid % UID_SZ] = malloc(sizeof(UIDC)); |
| 258 |
|
| 259 |
if ((pw = getpwuid(uid)) == NULL) { |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
if (ptr == NULL) |
| 265 |
return(""); |
| 266 |
ptr->uid = uid; |
| 267 |
ptr->valid = INVALID; |
| 268 |
(void)snprintf(ptr->name, sizeof(ptr->name), "%lu", |
| 269 |
(unsigned long)uid); |
| 270 |
if (frc == 0) |
| 271 |
return(""); |
| 272 |
} else { |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
if (ptr == NULL) |
| 277 |
return(pw->pw_name); |
| 278 |
ptr->uid = uid; |
| 279 |
(void)strlcpy(ptr->name, pw->pw_name, sizeof(ptr->name)); |
| 280 |
ptr->valid = VALID; |
| 281 |
} |
| 282 |
return(ptr->name); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
const char * |
| 294 |
name_gid(gid_t gid, int frc) |
| 295 |
{ |
| 296 |
struct group *gr; |
| 297 |
GIDC *ptr; |
| 298 |
|
| 299 |
if ((gidtb == NULL) && (gidtb_start() < 0)) |
| 300 |
return(""); |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
ptr = gidtb[gid % GID_SZ]; |
| 306 |
if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) { |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
if (frc || (ptr->valid == VALID)) |
| 311 |
return(ptr->name); |
| 312 |
return(""); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
if (!gropn) { |
| 319 |
#if HAVE_SETPGENT |
| 320 |
setgroupent(1); |
| 321 |
#else |
| 322 |
setgrent(); |
| 323 |
#endif |
| 324 |
++gropn; |
| 325 |
} |
| 326 |
if (ptr == NULL) |
| 327 |
ptr = gidtb[gid % GID_SZ] = malloc(sizeof(GIDC)); |
| 328 |
|
| 329 |
if ((gr = getgrgid(gid)) == NULL) { |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
if (ptr == NULL) |
| 335 |
return(""); |
| 336 |
ptr->gid = gid; |
| 337 |
ptr->valid = INVALID; |
| 338 |
(void)snprintf(ptr->name, sizeof(ptr->name), "%lu", |
| 339 |
(unsigned long)gid); |
| 340 |
if (frc == 0) |
| 341 |
return(""); |
| 342 |
} else { |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
if (ptr == NULL) |
| 347 |
return(gr->gr_name); |
| 348 |
ptr->gid = gid; |
| 349 |
(void)strlcpy(ptr->name, gr->gr_name, sizeof(ptr->name)); |
| 350 |
ptr->valid = VALID; |
| 351 |
} |
| 352 |
return(ptr->name); |
| 353 |
} |
| 354 |
#endif |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
int |
| 364 |
uid_name(const char *name, uid_t *uid) |
| 365 |
{ |
| 366 |
struct passwd *pw; |
| 367 |
UIDC *ptr; |
| 368 |
int namelen; |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
if (((namelen = strlen(name)) == 0) || (name[0] == '\0')) |
| 374 |
return(-1); |
| 375 |
if ((usrtb == NULL) && (usrtb_start() < 0)) |
| 376 |
return(-1); |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
ptr = usrtb[st_hash(name, namelen, UNM_SZ)]; |
| 383 |
if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { |
| 384 |
if (ptr->valid == INVALID) |
| 385 |
return(-1); |
| 386 |
*uid = ptr->uid; |
| 387 |
return(0); |
| 388 |
} |
| 389 |
|
| 390 |
if (!pwopn) { |
| 391 |
#if HAVE_SETPGENT |
| 392 |
setpassent(1); |
| 393 |
#else |
| 394 |
setpwent(); |
| 395 |
#endif |
| 396 |
++pwopn; |
| 397 |
} |
| 398 |
|
| 399 |
if (ptr == NULL) |
| 400 |
ptr = usrtb[st_hash(name, namelen, UNM_SZ)] = |
| 401 |
malloc(sizeof(UIDC)); |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
if (ptr == NULL) { |
| 408 |
if ((pw = getpwnam(name)) == NULL) |
| 409 |
return(-1); |
| 410 |
*uid = pw->pw_uid; |
| 411 |
return(0); |
| 412 |
} |
| 413 |
(void)strlcpy(ptr->name, name, sizeof(ptr->name)); |
| 414 |
if ((pw = getpwnam(name)) == NULL) { |
| 415 |
ptr->valid = INVALID; |
| 416 |
return(-1); |
| 417 |
} |
| 418 |
ptr->valid = VALID; |
| 419 |
*uid = ptr->uid = pw->pw_uid; |
| 420 |
return(0); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
int |
| 431 |
gid_name(const char *name, gid_t *gid) |
| 432 |
{ |
| 433 |
struct group *gr; |
| 434 |
GIDC *ptr; |
| 435 |
int namelen; |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
if (((namelen = strlen(name)) == 0) || (name[0] == '\0')) |
| 441 |
return(-1); |
| 442 |
if ((grptb == NULL) && (grptb_start() < 0)) |
| 443 |
return(-1); |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
ptr = grptb[st_hash(name, namelen, GID_SZ)]; |
| 450 |
if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { |
| 451 |
if (ptr->valid == INVALID) |
| 452 |
return(-1); |
| 453 |
*gid = ptr->gid; |
| 454 |
return(0); |
| 455 |
} |
| 456 |
|
| 457 |
if (!gropn) { |
| 458 |
#if HAVE_SETPGENT |
| 459 |
setgroupent(1); |
| 460 |
#else |
| 461 |
setgrent(); |
| 462 |
#endif |
| 463 |
++gropn; |
| 464 |
} |
| 465 |
if (ptr == NULL) |
| 466 |
ptr = grptb[st_hash(name, namelen, GID_SZ)] = |
| 467 |
malloc(sizeof(GIDC)); |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
if (ptr == NULL) { |
| 474 |
if ((gr = getgrnam(name)) == NULL) |
| 475 |
return(-1); |
| 476 |
*gid = gr->gr_gid; |
| 477 |
return(0); |
| 478 |
} |
| 479 |
|
| 480 |
(void)strlcpy(ptr->name, name, sizeof(ptr->name)); |
| 481 |
if ((gr = getgrnam(name)) == NULL) { |
| 482 |
ptr->valid = INVALID; |
| 483 |
return(-1); |
| 484 |
} |
| 485 |
ptr->valid = VALID; |
| 486 |
*gid = ptr->gid = gr->gr_gid; |
| 487 |
return(0); |
| 488 |
} |
| 489 |
|
| 490 |
int |
| 491 |
uid_uncached(const char *name, uid_t *uid) |
| 492 |
{ |
| 493 |
struct passwd *pw; |
| 494 |
|
| 495 |
if ((pw = getpwnam(name)) == NULL) |
| 496 |
return (-1); |
| 497 |
*uid = pw->pw_uid; |
| 498 |
return (0); |
| 499 |
} |
| 500 |
|
| 501 |
int |
| 502 |
gid_uncached(const char *name, gid_t *gid) |
| 503 |
{ |
| 504 |
struct group *gr; |
| 505 |
|
| 506 |
if ((gr = getgrnam(name)) == NULL) |
| 507 |
return (-1); |
| 508 |
*gid = gr->gr_gid; |
| 509 |
return (0); |
| 510 |
} |