archive_write_disk_set_standard_lookup.c 7.2 KB

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