archive_read_disk_set_standard_lookup.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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$");
  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. #if defined(_WIN32) && !defined(__CYGWIN__)
  47. int
  48. archive_read_disk_set_standard_lookup(struct archive *a)
  49. {
  50. archive_set_error(a, -1, "Standard lookups not available on Windows");
  51. return (ARCHIVE_FATAL);
  52. }
  53. #else /* ! (_WIN32 && !__CYGWIN__) */
  54. #define name_cache_size 127
  55. static const char * const NO_NAME = "(noname)";
  56. struct name_cache {
  57. struct archive *archive;
  58. char *buff;
  59. size_t buff_size;
  60. int probes;
  61. int hits;
  62. size_t size;
  63. struct {
  64. id_t id;
  65. const char *name;
  66. } cache[name_cache_size];
  67. };
  68. static const char * lookup_gname(void *, gid_t);
  69. static const char * lookup_uname(void *, uid_t);
  70. static void cleanup(void *);
  71. static const char * lookup_gname_helper(struct name_cache *, id_t gid);
  72. static const char * lookup_uname_helper(struct name_cache *, id_t uid);
  73. /*
  74. * Installs functions that use getpwuid()/getgrgid()---along with
  75. * a simple cache to accelerate such lookups---into the archive_read_disk
  76. * object. This is in a separate file because getpwuid()/getgrgid()
  77. * can pull in a LOT of library code (including NIS/LDAP functions, which
  78. * pull in DNS resolvers, etc). This can easily top 500kB, which makes
  79. * it inappropriate for some space-constrained applications.
  80. *
  81. * Applications that are size-sensitive may want to just use the
  82. * real default functions (defined in archive_read_disk.c) that just
  83. * use the uid/gid without the lookup. Or define your own custom functions
  84. * if you prefer.
  85. */
  86. int
  87. archive_read_disk_set_standard_lookup(struct archive *a)
  88. {
  89. struct name_cache *ucache = malloc(sizeof(struct name_cache));
  90. struct name_cache *gcache = malloc(sizeof(struct name_cache));
  91. if (ucache == NULL || gcache == NULL) {
  92. archive_set_error(a, ENOMEM,
  93. "Can't allocate uname/gname lookup cache");
  94. free(ucache);
  95. free(gcache);
  96. return (ARCHIVE_FATAL);
  97. }
  98. memset(ucache, 0, sizeof(*ucache));
  99. ucache->archive = a;
  100. ucache->size = name_cache_size;
  101. memset(gcache, 0, sizeof(*gcache));
  102. gcache->archive = a;
  103. gcache->size = name_cache_size;
  104. archive_read_disk_set_gname_lookup(a, gcache, lookup_gname, cleanup);
  105. archive_read_disk_set_uname_lookup(a, ucache, lookup_uname, cleanup);
  106. return (ARCHIVE_OK);
  107. }
  108. static void
  109. cleanup(void *data)
  110. {
  111. struct name_cache *cache = (struct name_cache *)data;
  112. size_t i;
  113. if (cache != NULL) {
  114. for (i = 0; i < cache->size; i++) {
  115. if (cache->cache[i].name != NULL &&
  116. cache->cache[i].name != NO_NAME)
  117. free((void *)(uintptr_t)cache->cache[i].name);
  118. }
  119. free(cache->buff);
  120. free(cache);
  121. }
  122. }
  123. /*
  124. * Lookup uid/gid from uname/gname, return NULL if no match.
  125. */
  126. static const char *
  127. lookup_name(struct name_cache *cache,
  128. const char * (*lookup_fn)(struct name_cache *, id_t), id_t id)
  129. {
  130. const char *name;
  131. int slot;
  132. cache->probes++;
  133. slot = id % cache->size;
  134. if (cache->cache[slot].name != NULL) {
  135. if (cache->cache[slot].id == id) {
  136. cache->hits++;
  137. if (cache->cache[slot].name == NO_NAME)
  138. return (NULL);
  139. return (cache->cache[slot].name);
  140. }
  141. if (cache->cache[slot].name != NO_NAME)
  142. free((void *)(uintptr_t)cache->cache[slot].name);
  143. cache->cache[slot].name = NULL;
  144. }
  145. name = (lookup_fn)(cache, id);
  146. if (name == NULL) {
  147. /* Cache and return the negative response. */
  148. cache->cache[slot].name = NO_NAME;
  149. cache->cache[slot].id = id;
  150. return (NULL);
  151. }
  152. cache->cache[slot].name = name;
  153. cache->cache[slot].id = id;
  154. return (cache->cache[slot].name);
  155. }
  156. static const char *
  157. lookup_uname(void *data, uid_t uid)
  158. {
  159. struct name_cache *uname_cache = (struct name_cache *)data;
  160. return (lookup_name(uname_cache,
  161. &lookup_uname_helper, (id_t)uid));
  162. }
  163. static const char *
  164. lookup_uname_helper(struct name_cache *cache, id_t id)
  165. {
  166. struct passwd pwent, *result;
  167. char * nbuff;
  168. size_t nbuff_size;
  169. int r;
  170. if (cache->buff_size == 0) {
  171. cache->buff_size = 256;
  172. cache->buff = malloc(cache->buff_size);
  173. }
  174. if (cache->buff == NULL)
  175. return (NULL);
  176. for (;;) {
  177. r = getpwuid_r((uid_t)id, &pwent,
  178. cache->buff, cache->buff_size, &result);
  179. if (r == 0)
  180. break;
  181. if (r != ERANGE)
  182. break;
  183. /* ERANGE means our buffer was too small, but POSIX
  184. * doesn't tell us how big the buffer should be, so
  185. * we just double it and try again. Because the buffer
  186. * is kept around in the cache object, we shouldn't
  187. * have to do this very often. */
  188. nbuff_size = cache->buff_size * 2;
  189. nbuff = realloc(cache->buff, nbuff_size);
  190. if (nbuff == NULL)
  191. break;
  192. cache->buff = nbuff;
  193. cache->buff_size = nbuff_size;
  194. }
  195. if (r != 0) {
  196. archive_set_error(cache->archive, errno,
  197. "Can't lookup user for id %d", (int)id);
  198. return (NULL);
  199. }
  200. if (result == NULL)
  201. return (NULL);
  202. return strdup(result->pw_name);
  203. }
  204. static const char *
  205. lookup_gname(void *data, gid_t gid)
  206. {
  207. struct name_cache *gname_cache = (struct name_cache *)data;
  208. return (lookup_name(gname_cache,
  209. &lookup_gname_helper, (id_t)gid));
  210. }
  211. static const char *
  212. lookup_gname_helper(struct name_cache *cache, id_t id)
  213. {
  214. struct group grent, *result;
  215. char * nbuff;
  216. size_t nbuff_size;
  217. int r;
  218. if (cache->buff_size == 0) {
  219. cache->buff_size = 256;
  220. cache->buff = malloc(cache->buff_size);
  221. }
  222. if (cache->buff == NULL)
  223. return (NULL);
  224. for (;;) {
  225. r = getgrgid_r((gid_t)id, &grent,
  226. cache->buff, cache->buff_size, &result);
  227. if (r == 0)
  228. break;
  229. if (r != ERANGE)
  230. break;
  231. /* ERANGE means our buffer was too small, but POSIX
  232. * doesn't tell us how big the buffer should be, so
  233. * we just double it and try again. */
  234. nbuff_size = cache->buff_size * 2;
  235. nbuff = realloc(cache->buff, nbuff_size);
  236. if (nbuff == NULL)
  237. break;
  238. cache->buff = nbuff;
  239. cache->buff_size = nbuff_size;
  240. }
  241. if (r != 0) {
  242. archive_set_error(cache->archive, errno,
  243. "Can't lookup group for id %d", (int)id);
  244. return (NULL);
  245. }
  246. if (result == NULL)
  247. return (NULL);
  248. return strdup(result->gr_name);
  249. }
  250. #endif /* ! (_WIN32 && !__CYGWIN__) */