2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD$");
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
52 archive_read_disk_set_standard_lookup(struct archive *a)
54 archive_set_error(a, -1, "Standard lookups not available on Windows");
55 return (ARCHIVE_FATAL);
58 #define name_cache_size 127
60 static const char * const NO_NAME = "(noname)";
63 struct archive *archive;
70 } cache[name_cache_size];
73 static const char * lookup_gname(void *, gid_t);
74 static const char * lookup_uname(void *, uid_t);
75 static void cleanup(void *);
76 static const char * lookup_gname_helper(struct archive *, id_t gid);
77 static const char * lookup_uname_helper(struct archive *, id_t uid);
80 * Installs functions that use getpwuid()/getgrgid()---along with
81 * a simple cache to accelerate such lookups---into the archive_read_disk
82 * object. This is in a separate file because getpwuid()/getgrgid()
83 * can pull in a LOT of library code (including NIS/LDAP functions, which
84 * pull in DNS resolveers, etc). This can easily top 500kB, which makes
85 * it inappropriate for some space-constrained applications.
87 * Applications that are size-sensitive may want to just use the
88 * real default functions (defined in archive_read_disk.c) that just
89 * use the uid/gid without the lookup. Or define your own custom functions
93 archive_read_disk_set_standard_lookup(struct archive *a)
95 struct name_cache *ucache = malloc(sizeof(struct name_cache));
96 struct name_cache *gcache = malloc(sizeof(struct name_cache));
98 if (ucache == NULL || gcache == NULL) {
99 archive_set_error(a, ENOMEM,
100 "Can't allocate uname/gname lookup cache");
103 return (ARCHIVE_FATAL);
106 memset(ucache, 0, sizeof(*ucache));
108 ucache->size = name_cache_size;
109 memset(gcache, 0, sizeof(*gcache));
111 gcache->size = name_cache_size;
113 archive_read_disk_set_gname_lookup(a, gcache, lookup_gname, cleanup);
114 archive_read_disk_set_uname_lookup(a, ucache, lookup_uname, cleanup);
122 struct name_cache *cache = (struct name_cache *)data;
126 for (i = 0; i < cache->size; i++) {
127 if (cache->cache[i].name != NULL &&
128 cache->cache[i].name != NO_NAME)
129 free((void *)(uintptr_t)cache->cache[i].name);
136 * Lookup uid/gid from uname/gname, return NULL if no match.
139 lookup_name(struct name_cache *cache,
140 const char * (*lookup_fn)(struct archive *, id_t), id_t id)
148 slot = id % cache->size;
149 if (cache->cache[slot].name != NULL) {
150 if (cache->cache[slot].id == id) {
152 if (cache->cache[slot].name == NO_NAME)
154 return (cache->cache[slot].name);
156 if (cache->cache[slot].name != NO_NAME)
157 free((void *)(uintptr_t)cache->cache[slot].name);
158 cache->cache[slot].name = NULL;
161 name = (lookup_fn)(cache->archive, id);
163 /* Cache and return the negative response. */
164 cache->cache[slot].name = NO_NAME;
165 cache->cache[slot].id = id;
169 cache->cache[slot].name = name;
170 cache->cache[slot].id = id;
171 return (cache->cache[slot].name);
175 lookup_uname(void *data, uid_t uid)
177 struct name_cache *uname_cache = (struct name_cache *)data;
178 return (lookup_name(uname_cache,
179 &lookup_uname_helper, (id_t)uid));
183 lookup_uname_helper(struct archive *a, id_t id)
186 struct passwd pwent, *result;
190 r = getpwuid_r((uid_t)id, &pwent, buffer, sizeof(buffer), &result);
192 archive_set_error(a, errno,
193 "Can't lookup user for id %d", (int)id);
199 return strdup(pwent.pw_name);
203 lookup_gname(void *data, gid_t gid)
205 struct name_cache *gname_cache = (struct name_cache *)data;
206 return (lookup_name(gname_cache,
207 &lookup_gname_helper, (id_t)gid));
211 lookup_gname_helper(struct archive *a, id_t id)
214 struct group grent, *result;
218 r = getgrgid_r((gid_t)id, &grent, buffer, sizeof(buffer), &result);
220 archive_set_error(a, errno,
221 "Can't lookup group for id %d", (int)id);
227 return strdup(grent.gr_name);