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 
53 struct bucket {
54 	char	*name;
55 	int	 hash;
56 	id_t	 id;
57 };
58 
59 static const size_t cache_size = 127;
60 static unsigned int	hash(const char *);
61 static int64_t	lookup_gid(void *, const char *uname, int64_t);
62 static int64_t	lookup_uid(void *, const char *uname, int64_t);
63 static 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 resolvers, 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  */
84 int
85 archive_write_disk_set_standard_lookup(struct archive *a)
86 {
87 	struct bucket *ucache = calloc(cache_size, sizeof(struct bucket));
88 	struct bucket *gcache = calloc(cache_size, sizeof(struct bucket));
89 	if (ucache == NULL || gcache == NULL) {
90 		free(ucache);
91 		free(gcache);
92 		return (ARCHIVE_FATAL);
93 	}
94 	archive_write_disk_set_group_lookup(a, gcache, lookup_gid, cleanup);
95 	archive_write_disk_set_user_lookup(a, ucache, lookup_uid, cleanup);
96 	return (ARCHIVE_OK);
97 }
98 
99 static int64_t
100 lookup_gid(void *private_data, const char *gname, int64_t gid)
101 {
102 	int h;
103 	struct bucket *b;
104 	struct bucket *gcache = (struct bucket *)private_data;
105 
106 	/* If no gname, just use the gid provided. */
107 	if (gname == NULL || *gname == '\0')
108 		return (gid);
109 
110 	/* Try to find gname in the cache. */
111 	h = hash(gname);
112 	b = &gcache[h % cache_size ];
113 	if (b->name != NULL && b->hash == h && strcmp(gname, b->name) == 0)
114 		return ((gid_t)b->id);
115 
116 	/* Free the cache slot for a new entry. */
117 	if (b->name != NULL)
118 		free(b->name);
119 	b->name = strdup(gname);
120 	/* Note: If strdup fails, that's okay; we just won't cache. */
121 	b->hash = h;
122 #if HAVE_GRP_H
123 #  if HAVE_GETGRNAM_R
124 	{
125 		char _buffer[128];
126 		size_t bufsize = 128;
127 		char *buffer = _buffer;
128 		char *allocated = NULL;
129 		struct group	grent, *result;
130 		int r;
131 
132 		for (;;) {
133 			result = &grent; /* Old getgrnam_r ignores last arg. */
134 			r = getgrnam_r(gname, &grent, buffer, bufsize, &result);
135 			if (r == 0)
136 				break;
137 			if (r != ERANGE)
138 				break;
139 			bufsize *= 2;
140 			free(allocated);
141 			allocated = malloc(bufsize);
142 			if (allocated == NULL)
143 				break;
144 			buffer = allocated;
145 		}
146 		if (result != NULL)
147 			gid = result->gr_gid;
148 		free(allocated);
149 	}
150 #  else /* HAVE_GETGRNAM_R */
151 	{
152 		struct group *result;
153 
154 		result = getgrnam(gname);
155 		if (result != NULL)
156 			gid = result->gr_gid;
157 	}
158 #  endif /* HAVE_GETGRNAM_R */
159 #elif defined(_WIN32) && !defined(__CYGWIN__)
160 	/* TODO: do a gname->gid lookup for Windows. */
161 #else
162 	#error No way to perform gid lookups on this platform
163 #endif
164 	b->id = (gid_t)gid;
165 
166 	return (gid);
167 }
168 
169 static int64_t
170 lookup_uid(void *private_data, const char *uname, int64_t uid)
171 {
172 	int h;
173 	struct bucket *b;
174 	struct bucket *ucache = (struct bucket *)private_data;
175 
176 	/* If no uname, just use the uid provided. */
177 	if (uname == NULL || *uname == '\0')
178 		return (uid);
179 
180 	/* Try to find uname in the cache. */
181 	h = hash(uname);
182 	b = &ucache[h % cache_size ];
183 	if (b->name != NULL && b->hash == h && strcmp(uname, b->name) == 0)
184 		return ((uid_t)b->id);
185 
186 	/* Free the cache slot for a new entry. */
187 	if (b->name != NULL)
188 		free(b->name);
189 	b->name = strdup(uname);
190 	/* Note: If strdup fails, that's okay; we just won't cache. */
191 	b->hash = h;
192 #if HAVE_PWD_H
193 #  if HAVE_GETPWNAM_R
194 	{
195 		char _buffer[128];
196 		size_t bufsize = 128;
197 		char *buffer = _buffer;
198 		char *allocated = NULL;
199 		struct passwd	pwent, *result;
200 		int r;
201 
202 		for (;;) {
203 			result = &pwent; /* Old getpwnam_r ignores last arg. */
204 			r = getpwnam_r(uname, &pwent, buffer, bufsize, &result);
205 			if (r == 0)
206 				break;
207 			if (r != ERANGE)
208 				break;
209 			bufsize *= 2;
210 			free(allocated);
211 			allocated = malloc(bufsize);
212 			if (allocated == NULL)
213 				break;
214 			buffer = allocated;
215 		}
216 		if (result != NULL)
217 			uid = result->pw_uid;
218 		free(allocated);
219 	}
220 #  else /* HAVE_GETPWNAM_R */
221 	{
222 		struct passwd *result;
223 
224 		result = getpwnam(uname);
225 		if (result != NULL)
226 			uid = result->pw_uid;
227 	}
228 #endif	/* HAVE_GETPWNAM_R */
229 #elif defined(_WIN32) && !defined(__CYGWIN__)
230 	/* TODO: do a uname->uid lookup for Windows. */
231 #else
232 	#error No way to look up uids on this platform
233 #endif
234 	b->id = (uid_t)uid;
235 
236 	return (uid);
237 }
238 
239 static void
240 cleanup(void *private)
241 {
242 	size_t i;
243 	struct bucket *cache = (struct bucket *)private;
244 
245 	for (i = 0; i < cache_size; i++)
246 		free(cache[i].name);
247 	free(cache);
248 }
249 
250 
251 static unsigned int
252 hash(const char *p)
253 {
254 	/* A 32-bit version of Peter Weinberger's (PJW) hash algorithm,
255 	   as used by ELF for hashing function names. */
256 	unsigned g, h = 0;
257 	while (*p != '\0') {
258 		h = (h << 4) + *p++;
259 		if ((g = h & 0xF0000000) != 0) {
260 			h ^= g >> 24;
261 			h &= 0x0FFFFFFF;
262 		}
263 	}
264 	return h;
265 }
266