archive_write_disk_set_standard_lookup.c 7.3 KB

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