archive_write_disk_set_standard_lookup.c 7.0 KB

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