213 lines
5.7 KiB
C
213 lines
5.7 KiB
C
|
/*-
|
||
|
* Copyright (c) 2003-2007 Tim Kientzle
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* Redistribution and use in source and binary forms, with or without
|
||
|
* modification, are permitted provided that the following conditions
|
||
|
* are met:
|
||
|
* 1. Redistributions of source code must retain the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer.
|
||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer in the
|
||
|
* documentation and/or other materials provided with the distribution.
|
||
|
*
|
||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
*/
|
||
|
|
||
|
#include "archive_platform.h"
|
||
|
__FBSDID("$FreeBSD$");
|
||
|
|
||
|
#ifdef HAVE_SYS_TYPES_H
|
||
|
#include <sys/types.h>
|
||
|
#endif
|
||
|
#ifdef HAVE_SYS_ACL_H
|
||
|
#include <sys/acl.h>
|
||
|
#endif
|
||
|
#ifdef HAVE_ATTR_XATTR_H
|
||
|
#include <attr/xattr.h>
|
||
|
#endif
|
||
|
#ifdef HAVE_SYS_IOCTL_H
|
||
|
#include <sys/ioctl.h>
|
||
|
#endif
|
||
|
#ifdef HAVE_SYS_STAT_H
|
||
|
#include <sys/stat.h>
|
||
|
#endif
|
||
|
#ifdef HAVE_SYS_TIME_H
|
||
|
#include <sys/time.h>
|
||
|
#endif
|
||
|
|
||
|
#ifdef HAVE_ERRNO_H
|
||
|
#include <errno.h>
|
||
|
#endif
|
||
|
#ifdef HAVE_GRP_H
|
||
|
#include <grp.h>
|
||
|
#endif
|
||
|
#ifdef HAVE_PWD_H
|
||
|
#include <pwd.h>
|
||
|
#endif
|
||
|
#ifdef HAVE_STDLIB_H
|
||
|
#include <stdlib.h>
|
||
|
#endif
|
||
|
#ifdef HAVE_STRING_H
|
||
|
#include <string.h>
|
||
|
#endif
|
||
|
|
||
|
#include "archive.h"
|
||
|
#include "archive_private.h"
|
||
|
#include "archive_read_private.h"
|
||
|
#include "archive_write_disk_private.h"
|
||
|
|
||
|
struct bucket {
|
||
|
char *name;
|
||
|
int hash;
|
||
|
id_t id;
|
||
|
};
|
||
|
|
||
|
static unsigned int hash(const char *);
|
||
|
static gid_t lookup_gid(void *, const char *uname, gid_t);
|
||
|
static uid_t lookup_uid(void *, const char *uname, uid_t);
|
||
|
static void cleanup(void *);
|
||
|
|
||
|
/*
|
||
|
* Installs functions that use getpwnam()/getgrnam()---along with
|
||
|
* a simple cache to accelerate such lookups---into the archive_write_disk
|
||
|
* object. This is in a separate file because getpwnam()/getgrnam()
|
||
|
* can pull in a LOT of library code (including NIS/LDAP functions, which
|
||
|
* pull in DNS resolveers, etc). This can easily top 500kB, which makes
|
||
|
* it inappropriate for some space-constrained applications.
|
||
|
*
|
||
|
* Applications that are size-sensitive may want to just use the
|
||
|
* real default functions (defined in archive_write_disk.c) that just
|
||
|
* use the uid/gid without the lookup. Or define your own custom functions
|
||
|
* if you prefer.
|
||
|
*
|
||
|
* TODO: Replace these hash tables with simpler move-to-front LRU
|
||
|
* lists with a bounded size (128 items?). The hash is a bit faster,
|
||
|
* but has a bad pathology in which it thrashes a single bucket. Even
|
||
|
* walking a list of 128 items is a lot faster than calling
|
||
|
* getpwnam()!
|
||
|
*/
|
||
|
int
|
||
|
archive_write_disk_set_standard_lookup(struct archive *a)
|
||
|
{
|
||
|
struct bucket *ucache = malloc(sizeof(struct bucket[127]));
|
||
|
struct bucket *gcache = malloc(sizeof(struct bucket[127]));
|
||
|
memset(ucache, 0, sizeof(struct bucket[127]));
|
||
|
memset(gcache, 0, sizeof(struct bucket[127]));
|
||
|
archive_write_disk_set_group_lookup(a, gcache, lookup_gid, cleanup);
|
||
|
archive_write_disk_set_user_lookup(a, ucache, lookup_uid, cleanup);
|
||
|
return (ARCHIVE_OK);
|
||
|
}
|
||
|
|
||
|
static gid_t
|
||
|
lookup_gid(void *private_data, const char *gname, gid_t gid)
|
||
|
{
|
||
|
int h;
|
||
|
struct bucket *b;
|
||
|
int cache_size;
|
||
|
struct bucket *gcache = (struct bucket *)private_data;
|
||
|
|
||
|
cache_size = 127;
|
||
|
|
||
|
/* If no gname, just use the gid provided. */
|
||
|
if (gname == NULL || *gname == '\0')
|
||
|
return (gid);
|
||
|
|
||
|
/* Try to find gname in the cache. */
|
||
|
h = hash(gname);
|
||
|
b = &gcache[h % cache_size ];
|
||
|
if (b->name != NULL && b->hash == h && strcmp(gname, b->name) == 0)
|
||
|
return ((gid_t)b->id);
|
||
|
|
||
|
/* Free the cache slot for a new entry. */
|
||
|
if (b->name != NULL)
|
||
|
free(b->name);
|
||
|
b->name = strdup(gname);
|
||
|
/* Note: If strdup fails, that's okay; we just won't cache. */
|
||
|
b->hash = h;
|
||
|
#if HAVE_GRP_H
|
||
|
{
|
||
|
struct group *grent = getgrnam(gname);
|
||
|
if (grent != NULL)
|
||
|
gid = grent->gr_gid;
|
||
|
}
|
||
|
#elif _WIN32
|
||
|
/* TODO: do a gname->gid lookup for Windows. */
|
||
|
#endif
|
||
|
b->id = gid;
|
||
|
|
||
|
return (gid);
|
||
|
}
|
||
|
|
||
|
static uid_t
|
||
|
lookup_uid(void *private_data, const char *uname, uid_t uid)
|
||
|
{
|
||
|
int h;
|
||
|
struct bucket *b;
|
||
|
int cache_size;
|
||
|
struct bucket *ucache = (struct bucket *)private_data;
|
||
|
|
||
|
cache_size = 127;
|
||
|
|
||
|
/* If no uname, just use the uid provided. */
|
||
|
if (uname == NULL || *uname == '\0')
|
||
|
return (uid);
|
||
|
|
||
|
/* Try to find uname in the cache. */
|
||
|
h = hash(uname);
|
||
|
b = &ucache[h % cache_size ];
|
||
|
if (b->name != NULL && b->hash == h && strcmp(uname, b->name) == 0)
|
||
|
return ((uid_t)b->id);
|
||
|
|
||
|
/* Free the cache slot for a new entry. */
|
||
|
if (b->name != NULL)
|
||
|
free(b->name);
|
||
|
b->name = strdup(uname);
|
||
|
/* Note: If strdup fails, that's okay; we just won't cache. */
|
||
|
b->hash = h;
|
||
|
#if HAVE_PWD_H
|
||
|
{
|
||
|
struct passwd *pwent = getpwnam(uname);
|
||
|
if (pwent != NULL)
|
||
|
uid = pwent->pw_uid;
|
||
|
}
|
||
|
#elif _WIN32
|
||
|
/* TODO: do a uname->uid lookup for Windows. */
|
||
|
#endif
|
||
|
b->id = uid;
|
||
|
|
||
|
return (uid);
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
cleanup(void *private)
|
||
|
{
|
||
|
free(private);
|
||
|
}
|
||
|
|
||
|
|
||
|
static unsigned int
|
||
|
hash(const char *p)
|
||
|
{
|
||
|
/* A 32-bit version of Peter Weinberger's (PJW) hash algorithm,
|
||
|
as used by ELF for hashing function names. */
|
||
|
unsigned g, h = 0;
|
||
|
while (*p != '\0') {
|
||
|
h = ( h << 4 ) + *p++;
|
||
|
if (( g = h & 0xF0000000 )) {
|
||
|
h ^= g >> 24;
|
||
|
h &= 0x0FFFFFFF;
|
||
|
}
|
||
|
}
|
||
|
return h;
|
||
|
}
|