archive_read_disk_set_standard_lookup.c 8.0 KB

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