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