archive_write_disk_set_standard_lookup.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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: src/lib/libarchive/archive_write_disk_set_standard_lookup.c,v 1.4 2007/05/29 01:00:19 kientzle Exp $");
  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 gid_t lookup_gid(void *, const char *uname, gid_t);
  57. static uid_t lookup_uid(void *, const char *uname, uid_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 gid_t
  95. lookup_gid(void *private_data, const char *gname, gid_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. {
  116. char _buffer[128];
  117. size_t bufsize = 128;
  118. char *buffer = _buffer;
  119. struct group grent, *result;
  120. int r;
  121. for (;;) {
  122. r = getgrnam_r(gname, &grent, buffer, bufsize, &result);
  123. if (r == 0)
  124. break;
  125. if (r != ERANGE)
  126. break;
  127. bufsize *= 2;
  128. if (buffer != _buffer)
  129. free(buffer);
  130. buffer = malloc(bufsize);
  131. if (buffer == NULL)
  132. break;
  133. }
  134. if (result != NULL)
  135. gid = result->gr_gid;
  136. if (buffer != _buffer)
  137. free(buffer);
  138. }
  139. #elif defined(_WIN32) && !defined(__CYGWIN__)
  140. /* TODO: do a gname->gid lookup for Windows. */
  141. #else
  142. #error No way to perform gid lookups on this platform
  143. #endif
  144. b->id = gid;
  145. return (gid);
  146. }
  147. static uid_t
  148. lookup_uid(void *private_data, const char *uname, uid_t uid)
  149. {
  150. int h;
  151. struct bucket *b;
  152. struct bucket *ucache = (struct bucket *)private_data;
  153. /* If no uname, just use the uid provided. */
  154. if (uname == NULL || *uname == '\0')
  155. return (uid);
  156. /* Try to find uname in the cache. */
  157. h = hash(uname);
  158. b = &ucache[h % cache_size ];
  159. if (b->name != NULL && b->hash == h && strcmp(uname, b->name) == 0)
  160. return ((uid_t)b->id);
  161. /* Free the cache slot for a new entry. */
  162. if (b->name != NULL)
  163. free(b->name);
  164. b->name = strdup(uname);
  165. /* Note: If strdup fails, that's okay; we just won't cache. */
  166. b->hash = h;
  167. #if HAVE_PWD_H
  168. {
  169. char _buffer[128];
  170. size_t bufsize = 128;
  171. char *buffer = _buffer;
  172. struct passwd pwent, *result;
  173. int r;
  174. for (;;) {
  175. r = getpwnam_r(uname, &pwent, buffer, bufsize, &result);
  176. if (r == 0)
  177. break;
  178. if (r != ERANGE)
  179. break;
  180. bufsize *= 2;
  181. if (buffer != _buffer)
  182. free(buffer);
  183. buffer = malloc(bufsize);
  184. if (buffer == NULL)
  185. break;
  186. }
  187. if (result != NULL)
  188. uid = result->pw_uid;
  189. if (buffer != _buffer)
  190. free(buffer);
  191. }
  192. #elif defined(_WIN32) && !defined(__CYGWIN__)
  193. /* TODO: do a uname->uid lookup for Windows. */
  194. #else
  195. #error No way to look up uids on this platform
  196. #endif
  197. b->id = uid;
  198. return (uid);
  199. }
  200. static void
  201. cleanup(void *private)
  202. {
  203. size_t i;
  204. struct bucket *cache = (struct bucket *)private;
  205. for (i = 0; i < cache_size; i++)
  206. free(cache[i].name);
  207. free(cache);
  208. }
  209. static unsigned int
  210. hash(const char *p)
  211. {
  212. /* A 32-bit version of Peter Weinberger's (PJW) hash algorithm,
  213. as used by ELF for hashing function names. */
  214. unsigned g, h = 0;
  215. while (*p != '\0') {
  216. h = ( h << 4 ) + *p++;
  217. if (( g = h & 0xF0000000 )) {
  218. h ^= g >> 24;
  219. h &= 0x0FFFFFFF;
  220. }
  221. }
  222. return h;
  223. }