PageRenderTime 40ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/nss/nss_files/files-hosts.c

https://gitlab.com/Namal/glibc
C | 482 lines | 355 code | 63 blank | 64 comment | 90 complexity | b6ee9a9a3d859462cd874617e5672146 MD5 | raw file
  1. /* Hosts file parser in nss_files module.
  2. Copyright (C) 1996-2016 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <netinet/in.h>
  17. #include <arpa/inet.h>
  18. #include <arpa/nameser.h>
  19. #include <netdb.h>
  20. #include <resolv.h>
  21. /* Get implementation for some internal functions. */
  22. #include "../resolv/mapv4v6addr.h"
  23. #include "../resolv/res_hconf.h"
  24. #define ENTNAME hostent
  25. #define DATABASE "hosts"
  26. #define NEED_H_ERRNO
  27. #define EXTRA_ARGS , af, flags
  28. #define EXTRA_ARGS_DECL , int af, int flags
  29. #define ENTDATA hostent_data
  30. struct hostent_data
  31. {
  32. unsigned char host_addr[16]; /* IPv4 or IPv6 address. */
  33. char *h_addr_ptrs[2]; /* Points to that and null terminator. */
  34. };
  35. #define TRAILING_LIST_MEMBER h_aliases
  36. #define TRAILING_LIST_SEPARATOR_P isspace
  37. #include "files-parse.c"
  38. LINE_PARSER
  39. ("#",
  40. {
  41. char *addr;
  42. STRING_FIELD (addr, isspace, 1);
  43. /* Parse address. */
  44. if (inet_pton (af == AF_UNSPEC ? AF_INET : af, addr, entdata->host_addr)
  45. > 0)
  46. af = af == AF_UNSPEC ? AF_INET : af;
  47. else
  48. {
  49. if (af == AF_INET6 && (flags & AI_V4MAPPED) != 0
  50. && inet_pton (AF_INET, addr, entdata->host_addr) > 0)
  51. map_v4v6_address ((char *) entdata->host_addr,
  52. (char *) entdata->host_addr);
  53. else if (af == AF_INET
  54. && inet_pton (AF_INET6, addr, entdata->host_addr) > 0)
  55. {
  56. if (IN6_IS_ADDR_V4MAPPED (entdata->host_addr))
  57. memcpy (entdata->host_addr, entdata->host_addr + 12, INADDRSZ);
  58. else if (IN6_IS_ADDR_LOOPBACK (entdata->host_addr))
  59. {
  60. in_addr_t localhost = htonl (INADDR_LOOPBACK);
  61. memcpy (entdata->host_addr, &localhost, sizeof (localhost));
  62. }
  63. else
  64. /* Illegal address: ignore line. */
  65. return 0;
  66. }
  67. else if (af == AF_UNSPEC
  68. && inet_pton (AF_INET6, addr, entdata->host_addr) > 0)
  69. af = AF_INET6;
  70. else
  71. /* Illegal address: ignore line. */
  72. return 0;
  73. }
  74. /* We always return entries of the requested form. */
  75. result->h_addrtype = af;
  76. result->h_length = af == AF_INET ? INADDRSZ : IN6ADDRSZ;
  77. /* Store a pointer to the address in the expected form. */
  78. entdata->h_addr_ptrs[0] = (char *) entdata->host_addr;
  79. entdata->h_addr_ptrs[1] = NULL;
  80. result->h_addr_list = entdata->h_addr_ptrs;
  81. STRING_FIELD (result->h_name, isspace, 1);
  82. })
  83. #define EXTRA_ARGS_VALUE \
  84. , ((_res.options & RES_USE_INET6) ? AF_INET6 : AF_INET), \
  85. ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0)
  86. #include "files-XXX.c"
  87. #undef EXTRA_ARGS_VALUE
  88. /* We only need to consider IPv4 mapped addresses if the input to the
  89. gethostbyaddr() function is an IPv6 address. */
  90. #define EXTRA_ARGS_VALUE \
  91. , af, (len == IN6ADDRSZ ? AI_V4MAPPED : 0)
  92. DB_LOOKUP (hostbyaddr, ,,,
  93. {
  94. if (result->h_length == (int) len
  95. && ! memcmp (addr, result->h_addr_list[0], len))
  96. break;
  97. }, const void *addr, socklen_t len, int af)
  98. #undef EXTRA_ARGS_VALUE
  99. enum nss_status
  100. _nss_files_gethostbyname3_r (const char *name, int af, struct hostent *result,
  101. char *buffer, size_t buflen, int *errnop,
  102. int *herrnop, int32_t *ttlp, char **canonp)
  103. {
  104. FILE *stream = NULL;
  105. uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct hostent_data);
  106. buffer += pad;
  107. buflen = buflen > pad ? buflen - pad : 0;
  108. /* Open file. */
  109. enum nss_status status = internal_setent (&stream);
  110. if (status == NSS_STATUS_SUCCESS)
  111. {
  112. /* XXX Is using _res to determine whether we want to convert IPv4
  113. addresses to IPv6 addresses really the right thing to do? */
  114. int flags = ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0);
  115. while ((status = internal_getent (stream, result, buffer, buflen, errnop,
  116. herrnop, af, flags))
  117. == NSS_STATUS_SUCCESS)
  118. {
  119. LOOKUP_NAME_CASE (h_name, h_aliases)
  120. }
  121. if (status == NSS_STATUS_SUCCESS
  122. && _res_hconf.flags & HCONF_FLAG_MULTI)
  123. {
  124. /* We have to get all host entries from the file. */
  125. size_t tmp_buflen = MIN (buflen, 4096);
  126. char tmp_buffer_stack[tmp_buflen]
  127. __attribute__ ((__aligned__ (__alignof__ (struct hostent_data))));
  128. char *tmp_buffer = tmp_buffer_stack;
  129. struct hostent tmp_result_buf;
  130. int naddrs = 1;
  131. int naliases = 0;
  132. char *bufferend;
  133. bool tmp_buffer_malloced = false;
  134. while (result->h_aliases[naliases] != NULL)
  135. ++naliases;
  136. bufferend = (char *) &result->h_aliases[naliases + 1];
  137. again:
  138. while ((status = internal_getent (stream, &tmp_result_buf, tmp_buffer,
  139. tmp_buflen, errnop, herrnop, af,
  140. flags))
  141. == NSS_STATUS_SUCCESS)
  142. {
  143. int matches = 1;
  144. struct hostent *old_result = result;
  145. result = &tmp_result_buf;
  146. /* The following piece is a bit clumsy but we want to use the
  147. `LOOKUP_NAME_CASE' value. The optimizer should do its
  148. job. */
  149. do
  150. {
  151. LOOKUP_NAME_CASE (h_name, h_aliases)
  152. result = old_result;
  153. }
  154. while ((matches = 0));
  155. if (matches)
  156. {
  157. /* We could be very clever and try to recycle a few bytes
  158. in the buffer instead of generating new arrays. But
  159. we are not doing this here since it's more work than
  160. it's worth. Simply let the user provide a bit bigger
  161. buffer. */
  162. char **new_h_addr_list;
  163. char **new_h_aliases;
  164. int newaliases = 0;
  165. size_t newstrlen = 0;
  166. int cnt;
  167. /* Count the new aliases and the length of the strings. */
  168. while (tmp_result_buf.h_aliases[newaliases] != NULL)
  169. {
  170. char *cp = tmp_result_buf.h_aliases[newaliases];
  171. ++newaliases;
  172. newstrlen += strlen (cp) + 1;
  173. }
  174. /* If the real name is different add it also to the
  175. aliases. This means that there is a duplication
  176. in the alias list but this is really the user's
  177. problem. */
  178. if (strcmp (old_result->h_name,
  179. tmp_result_buf.h_name) != 0)
  180. {
  181. ++newaliases;
  182. newstrlen += strlen (tmp_result_buf.h_name) + 1;
  183. }
  184. /* Make sure bufferend is aligned. */
  185. assert ((bufferend - (char *) 0) % sizeof (char *) == 0);
  186. /* Now we can check whether the buffer is large enough.
  187. 16 is the maximal size of the IP address. */
  188. if (bufferend + 16 + (naddrs + 2) * sizeof (char *)
  189. + roundup (newstrlen, sizeof (char *))
  190. + (naliases + newaliases + 1) * sizeof (char *)
  191. >= buffer + buflen)
  192. {
  193. *errnop = ERANGE;
  194. *herrnop = NETDB_INTERNAL;
  195. status = NSS_STATUS_TRYAGAIN;
  196. goto out;
  197. }
  198. new_h_addr_list =
  199. (char **) (bufferend
  200. + roundup (newstrlen, sizeof (char *))
  201. + 16);
  202. new_h_aliases =
  203. (char **) ((char *) new_h_addr_list
  204. + (naddrs + 2) * sizeof (char *));
  205. /* Copy the old data in the new arrays. */
  206. for (cnt = 0; cnt < naddrs; ++cnt)
  207. new_h_addr_list[cnt] = old_result->h_addr_list[cnt];
  208. for (cnt = 0; cnt < naliases; ++cnt)
  209. new_h_aliases[cnt] = old_result->h_aliases[cnt];
  210. /* Store the new strings. */
  211. cnt = 0;
  212. while (tmp_result_buf.h_aliases[cnt] != NULL)
  213. {
  214. new_h_aliases[naliases++] = bufferend;
  215. bufferend = (__stpcpy (bufferend,
  216. tmp_result_buf.h_aliases[cnt])
  217. + 1);
  218. ++cnt;
  219. }
  220. if (cnt < newaliases)
  221. {
  222. new_h_aliases[naliases++] = bufferend;
  223. bufferend = __stpcpy (bufferend,
  224. tmp_result_buf.h_name) + 1;
  225. }
  226. /* Final NULL pointer. */
  227. new_h_aliases[naliases] = NULL;
  228. /* Round up the buffer end address. */
  229. bufferend += (sizeof (char *)
  230. - ((bufferend - (char *) 0)
  231. % sizeof (char *))) % sizeof (char *);
  232. /* Now the new address. */
  233. new_h_addr_list[naddrs++] =
  234. memcpy (bufferend, tmp_result_buf.h_addr,
  235. tmp_result_buf.h_length);
  236. /* Also here a final NULL pointer. */
  237. new_h_addr_list[naddrs] = NULL;
  238. /* Store the new array pointers. */
  239. old_result->h_aliases = new_h_aliases;
  240. old_result->h_addr_list = new_h_addr_list;
  241. /* Compute the new buffer end. */
  242. bufferend = (char *) &new_h_aliases[naliases + 1];
  243. assert (bufferend <= buffer + buflen);
  244. result = old_result;
  245. }
  246. }
  247. if (status == NSS_STATUS_TRYAGAIN)
  248. {
  249. size_t newsize = 2 * tmp_buflen;
  250. if (tmp_buffer_malloced)
  251. {
  252. char *newp = realloc (tmp_buffer, newsize);
  253. if (newp != NULL)
  254. {
  255. assert ((((uintptr_t) newp)
  256. & (__alignof__ (struct hostent_data) - 1))
  257. == 0);
  258. tmp_buffer = newp;
  259. tmp_buflen = newsize;
  260. goto again;
  261. }
  262. }
  263. else if (!__libc_use_alloca (buflen + newsize))
  264. {
  265. tmp_buffer = malloc (newsize);
  266. if (tmp_buffer != NULL)
  267. {
  268. assert ((((uintptr_t) tmp_buffer)
  269. & (__alignof__ (struct hostent_data) - 1))
  270. == 0);
  271. tmp_buffer_malloced = true;
  272. tmp_buflen = newsize;
  273. goto again;
  274. }
  275. }
  276. else
  277. {
  278. tmp_buffer
  279. = extend_alloca (tmp_buffer, tmp_buflen,
  280. newsize
  281. + __alignof__ (struct hostent_data));
  282. tmp_buffer = (char *) (((uintptr_t) tmp_buffer
  283. + __alignof__ (struct hostent_data)
  284. - 1)
  285. & ~(__alignof__ (struct hostent_data)
  286. - 1));
  287. goto again;
  288. }
  289. }
  290. else
  291. status = NSS_STATUS_SUCCESS;
  292. out:
  293. if (tmp_buffer_malloced)
  294. free (tmp_buffer);
  295. }
  296. internal_endent (&stream);
  297. }
  298. if (canonp && status == NSS_STATUS_SUCCESS)
  299. *canonp = result->h_name;
  300. return status;
  301. }
  302. enum nss_status
  303. _nss_files_gethostbyname_r (const char *name, struct hostent *result,
  304. char *buffer, size_t buflen, int *errnop,
  305. int *herrnop)
  306. {
  307. int af = ((_res.options & RES_USE_INET6) ? AF_INET6 : AF_INET);
  308. return _nss_files_gethostbyname3_r (name, af, result, buffer, buflen,
  309. errnop, herrnop, NULL, NULL);
  310. }
  311. enum nss_status
  312. _nss_files_gethostbyname2_r (const char *name, int af, struct hostent *result,
  313. char *buffer, size_t buflen, int *errnop,
  314. int *herrnop)
  315. {
  316. return _nss_files_gethostbyname3_r (name, af, result, buffer, buflen,
  317. errnop, herrnop, NULL, NULL);
  318. }
  319. enum nss_status
  320. _nss_files_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
  321. char *buffer, size_t buflen, int *errnop,
  322. int *herrnop, int32_t *ttlp)
  323. {
  324. FILE *stream = NULL;
  325. /* Open file. */
  326. enum nss_status status = internal_setent (&stream);
  327. if (status == NSS_STATUS_SUCCESS)
  328. {
  329. bool any = false;
  330. bool got_canon = false;
  331. while (1)
  332. {
  333. /* Align the buffer for the next record. */
  334. uintptr_t pad = (-(uintptr_t) buffer
  335. % __alignof__ (struct hostent_data));
  336. buffer += pad;
  337. buflen = buflen > pad ? buflen - pad : 0;
  338. struct hostent result;
  339. status = internal_getent (stream, &result, buffer, buflen, errnop,
  340. herrnop, AF_UNSPEC, 0);
  341. if (status != NSS_STATUS_SUCCESS)
  342. break;
  343. int naliases = 0;
  344. if (__strcasecmp (name, result.h_name) != 0)
  345. {
  346. for (; result.h_aliases[naliases] != NULL; ++naliases)
  347. if (! __strcasecmp (name, result.h_aliases[naliases]))
  348. break;
  349. if (result.h_aliases[naliases] == NULL)
  350. continue;
  351. /* We know this alias exist. Count it. */
  352. ++naliases;
  353. }
  354. /* Determine how much memory has been used so far. */
  355. // XXX It is not necessary to preserve the aliases array
  356. while (result.h_aliases[naliases] != NULL)
  357. ++naliases;
  358. char *bufferend = (char *) &result.h_aliases[naliases + 1];
  359. assert (buflen >= bufferend - buffer);
  360. buflen -= bufferend - buffer;
  361. buffer = bufferend;
  362. /* We found something. */
  363. any = true;
  364. /* Create the record the caller expects. There is only one
  365. address. */
  366. assert (result.h_addr_list[1] == NULL);
  367. if (*pat == NULL)
  368. {
  369. uintptr_t pad = (-(uintptr_t) buffer
  370. % __alignof__ (struct gaih_addrtuple));
  371. buffer += pad;
  372. buflen = buflen > pad ? buflen - pad : 0;
  373. if (__builtin_expect (buflen < sizeof (struct gaih_addrtuple),
  374. 0))
  375. {
  376. *errnop = ERANGE;
  377. *herrnop = NETDB_INTERNAL;
  378. status = NSS_STATUS_TRYAGAIN;
  379. break;
  380. }
  381. *pat = (struct gaih_addrtuple *) buffer;
  382. buffer += sizeof (struct gaih_addrtuple);
  383. buflen -= sizeof (struct gaih_addrtuple);
  384. }
  385. (*pat)->next = NULL;
  386. (*pat)->name = got_canon ? NULL : result.h_name;
  387. got_canon = true;
  388. (*pat)->family = result.h_addrtype;
  389. memcpy ((*pat)->addr, result.h_addr_list[0], result.h_length);
  390. (*pat)->scopeid = 0;
  391. pat = &((*pat)->next);
  392. /* If we only look for the first matching entry we are done. */
  393. if ((_res_hconf.flags & HCONF_FLAG_MULTI) == 0)
  394. break;
  395. }
  396. /* If we have to look for multiple records and found one, this
  397. is a success. */
  398. if (status == NSS_STATUS_NOTFOUND && any)
  399. {
  400. assert ((_res_hconf.flags & HCONF_FLAG_MULTI) != 0);
  401. status = NSS_STATUS_SUCCESS;
  402. }
  403. internal_endent (&stream);
  404. }
  405. else if (status == NSS_STATUS_TRYAGAIN)
  406. {
  407. *errnop = errno;
  408. *herrnop = TRY_AGAIN;
  409. }
  410. else
  411. {
  412. *errnop = errno;
  413. *herrnop = NO_DATA;
  414. }
  415. return status;
  416. }