1/*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25
26#include "archive_platform.h"
27__FBSDID("$FreeBSD: head/lib/libarchive/archive_write_disk_set_standard_lookup.c 201083 2009-12-28 02:09:57Z kientzle $");
28
29#ifdef HAVE_SYS_TYPES_H
30#include <sys/types.h>
31#endif
32#ifdef HAVE_ERRNO_H
33#include <errno.h>
34#endif
35#ifdef HAVE_GRP_H
36#include <grp.h>
37#endif
38#ifdef HAVE_PWD_H
39#include <pwd.h>
40#endif
41#ifdef HAVE_STDLIB_H
42#include <stdlib.h>
43#endif
44#ifdef HAVE_STRING_H
45#include <string.h>
46#endif
47
48#include "archive.h"
49#include "archive_private.h"
50#include "archive_read_private.h"
51#include "archive_write_disk_private.h"
52
53struct bucket {
54	char	*name;
55	int	 hash;
56	id_t	 id;
57};
58
59static const size_t cache_size = 127;
60static unsigned int	hash(const char *);
61static gid_t	lookup_gid(void *, const char *uname, gid_t);
62static uid_t	lookup_uid(void *, const char *uname, uid_t);
63static void	cleanup(void *);
64
65/*
66 * Installs functions that use getpwnam()/getgrnam()---along with
67 * a simple cache to accelerate such lookups---into the archive_write_disk
68 * object.  This is in a separate file because getpwnam()/getgrnam()
69 * can pull in a LOT of library code (including NIS/LDAP functions, which
70 * pull in DNS resolveers, etc).  This can easily top 500kB, which makes
71 * it inappropriate for some space-constrained applications.
72 *
73 * Applications that are size-sensitive may want to just use the
74 * real default functions (defined in archive_write_disk.c) that just
75 * use the uid/gid without the lookup.  Or define your own custom functions
76 * if you prefer.
77 *
78 * TODO: Replace these hash tables with simpler move-to-front LRU
79 * lists with a bounded size (128 items?).  The hash is a bit faster,
80 * but has a bad pathology in which it thrashes a single bucket.  Even
81 * walking a list of 128 items is a lot faster than calling
82 * getpwnam()!
83 */
84int
85archive_write_disk_set_standard_lookup(struct archive *a)
86{
87	struct bucket *ucache = malloc(cache_size * sizeof(struct bucket));
88	struct bucket *gcache = malloc(cache_size * sizeof(struct bucket));
89	memset(ucache, 0, cache_size * sizeof(struct bucket));
90	memset(gcache, 0, cache_size * sizeof(struct bucket));
91	archive_write_disk_set_group_lookup(a, gcache, lookup_gid, cleanup);
92	archive_write_disk_set_user_lookup(a, ucache, lookup_uid, cleanup);
93	return (ARCHIVE_OK);
94}
95
96static gid_t
97lookup_gid(void *private_data, const char *gname, gid_t gid)
98{
99	int h;
100	struct bucket *b;
101	struct bucket *gcache = (struct bucket *)private_data;
102
103	/* If no gname, just use the gid provided. */
104	if (gname == NULL || *gname == '\0')
105		return (gid);
106
107	/* Try to find gname in the cache. */
108	h = hash(gname);
109	b = &gcache[h % cache_size ];
110	if (b->name != NULL && b->hash == h && strcmp(gname, b->name) == 0)
111		return ((gid_t)b->id);
112
113	/* Free the cache slot for a new entry. */
114	if (b->name != NULL)
115		free(b->name);
116	b->name = strdup(gname);
117	/* Note: If strdup fails, that's okay; we just won't cache. */
118	b->hash = h;
119#if HAVE_GRP_H
120	{
121		char _buffer[128];
122		size_t bufsize = 128;
123		char *buffer = _buffer;
124		struct group	grent, *result;
125		int r;
126
127		for (;;) {
128			result = &grent; /* Old getgrnam_r ignores last arg. */
129			r = getgrnam_r(gname, &grent, buffer, bufsize, &result);
130			if (r == 0)
131				break;
132			if (r != ERANGE)
133				break;
134			bufsize *= 2;
135			if (buffer != _buffer)
136				free(buffer);
137			buffer = malloc(bufsize);
138			if (buffer == NULL)
139				break;
140		}
141		if (result != NULL)
142			gid = result->gr_gid;
143		if (buffer != _buffer)
144			free(buffer);
145	}
146#elif defined(_WIN32) && !defined(__CYGWIN__)
147	/* TODO: do a gname->gid lookup for Windows. */
148#else
149	#error No way to perform gid lookups on this platform
150#endif
151	b->id = gid;
152
153	return (gid);
154}
155
156static uid_t
157lookup_uid(void *private_data, const char *uname, uid_t uid)
158{
159	int h;
160	struct bucket *b;
161	struct bucket *ucache = (struct bucket *)private_data;
162
163	/* If no uname, just use the uid provided. */
164	if (uname == NULL || *uname == '\0')
165		return (uid);
166
167	/* Try to find uname in the cache. */
168	h = hash(uname);
169	b = &ucache[h % cache_size ];
170	if (b->name != NULL && b->hash == h && strcmp(uname, b->name) == 0)
171		return ((uid_t)b->id);
172
173	/* Free the cache slot for a new entry. */
174	if (b->name != NULL)
175		free(b->name);
176	b->name = strdup(uname);
177	/* Note: If strdup fails, that's okay; we just won't cache. */
178	b->hash = h;
179#if HAVE_PWD_H
180	{
181		char _buffer[128];
182		size_t bufsize = 128;
183		char *buffer = _buffer;
184		struct passwd	pwent, *result;
185		int r;
186
187		for (;;) {
188			result = &pwent; /* Old getpwnam_r ignores last arg. */
189			r = getpwnam_r(uname, &pwent, buffer, bufsize, &result);
190			if (r == 0)
191				break;
192			if (r != ERANGE)
193				break;
194			bufsize *= 2;
195			if (buffer != _buffer)
196				free(buffer);
197			buffer = malloc(bufsize);
198			if (buffer == NULL)
199				break;
200		}
201		if (result != NULL)
202			uid = result->pw_uid;
203		if (buffer != _buffer)
204			free(buffer);
205	}
206#elif defined(_WIN32) && !defined(__CYGWIN__)
207	/* TODO: do a uname->uid lookup for Windows. */
208#else
209	#error No way to look up uids on this platform
210#endif
211	b->id = uid;
212
213	return (uid);
214}
215
216static void
217cleanup(void *private)
218{
219	size_t i;
220	struct bucket *cache = (struct bucket *)private;
221
222	for (i = 0; i < cache_size; i++)
223		free(cache[i].name);
224	free(cache);
225}
226
227
228static unsigned int
229hash(const char *p)
230{
231	/* A 32-bit version of Peter Weinberger's (PJW) hash algorithm,
232	   as used by ELF for hashing function names. */
233	unsigned g, h = 0;
234	while (*p != '\0') {
235		h = (h << 4) + *p++;
236		if ((g = h & 0xF0000000) != 0) {
237			h ^= g >> 24;
238			h &= 0x0FFFFFFF;
239		}
240	}
241	return h;
242}
243