| 1 | /* Copyright (C) 2004-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2004. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <netdb.h> |
| 21 | #include <resolv.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdint.h> |
| 24 | #include <arpa/nameser.h> |
| 25 | #include <nsswitch.h> |
| 26 | |
| 27 | |
| 28 | #if PACKETSZ > 65536 |
| 29 | # define MAXPACKET PACKETSZ |
| 30 | #else |
| 31 | # define MAXPACKET 65536 |
| 32 | #endif |
| 33 | |
| 34 | |
| 35 | /* We need this time later. */ |
| 36 | typedef union querybuf |
| 37 | { |
| 38 | HEADER hdr; |
| 39 | unsigned char buf[MAXPACKET]; |
| 40 | } querybuf; |
| 41 | |
| 42 | |
| 43 | static const short int qtypes[] = { ns_t_a, ns_t_aaaa }; |
| 44 | #define nqtypes (sizeof (qtypes) / sizeof (qtypes[0])) |
| 45 | |
| 46 | |
| 47 | enum nss_status |
| 48 | _nss_dns_getcanonname_r (const char *name, char *buffer, size_t buflen, |
| 49 | char **result,int *errnop, int *h_errnop) |
| 50 | { |
| 51 | /* Just an alibi buffer, res_nquery will allocate a real buffer for |
| 52 | us. */ |
| 53 | unsigned char buf[20]; |
| 54 | union |
| 55 | { |
| 56 | querybuf *buf; |
| 57 | unsigned char *ptr; |
| 58 | } ansp = { .ptr = buf }; |
| 59 | enum nss_status status = NSS_STATUS_UNAVAIL; |
| 60 | |
| 61 | for (int i = 0; i < nqtypes; ++i) |
| 62 | { |
| 63 | int r = __libc_res_nquery (&_res, name, ns_c_in, qtypes[i], |
| 64 | buf, sizeof (buf), &ansp.ptr, NULL, NULL, |
| 65 | NULL, NULL); |
| 66 | if (r > 0) |
| 67 | { |
| 68 | /* We need to decode the response. Just one question record. |
| 69 | And if we got no answers we bail out, too. */ |
| 70 | if (ansp.buf->hdr.qdcount != htons (1)) |
| 71 | continue; |
| 72 | |
| 73 | /* Number of answers. */ |
| 74 | unsigned int ancount = ntohs (ansp.buf->hdr.ancount); |
| 75 | |
| 76 | /* Beginning and end of the buffer with query, answer, and the |
| 77 | rest. */ |
| 78 | unsigned char *ptr = &ansp.buf->buf[sizeof (HEADER)]; |
| 79 | unsigned char *endptr = ansp.ptr + r; |
| 80 | |
| 81 | /* Skip over the query. This is the name, type, and class. */ |
| 82 | int s = __dn_skipname (ptr, endptr); |
| 83 | if (s < 0) |
| 84 | { |
| 85 | unavail: |
| 86 | status = NSS_STATUS_UNAVAIL; |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | /* Skip over the name and the two 16-bit values containing type |
| 91 | and class. */ |
| 92 | ptr += s + 2 * sizeof (uint16_t); |
| 93 | |
| 94 | while (ancount-- > 0) |
| 95 | { |
| 96 | /* Now the reply. First again the name from the query, |
| 97 | then type, class, TTL, and the length of the RDATA. |
| 98 | We remember the name start. */ |
| 99 | unsigned char *namestart = ptr; |
| 100 | s = __dn_skipname (ptr, endptr); |
| 101 | if (s < 0) |
| 102 | goto unavail; |
| 103 | |
| 104 | ptr += s; |
| 105 | |
| 106 | /* Check whether type and class match. */ |
| 107 | uint_fast16_t type; |
| 108 | NS_GET16 (type, ptr); |
| 109 | if (type == qtypes[i]) |
| 110 | { |
| 111 | /* We found the record. */ |
| 112 | s = __dn_expand (ansp.buf->buf, endptr, namestart, |
| 113 | buffer, buflen); |
| 114 | if (s < 0) |
| 115 | { |
| 116 | if (errno != EMSGSIZE) |
| 117 | goto unavail; |
| 118 | |
| 119 | /* The buffer is too small. */ |
| 120 | *errnop = ERANGE; |
| 121 | status = NSS_STATUS_TRYAGAIN; |
| 122 | h_errno = NETDB_INTERNAL; |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | /* Success. */ |
| 127 | *result = buffer; |
| 128 | status = NSS_STATUS_SUCCESS; |
| 129 | } |
| 130 | |
| 131 | goto out; |
| 132 | } |
| 133 | |
| 134 | if (type != ns_t_cname) |
| 135 | goto unavail; |
| 136 | |
| 137 | if (__ns_get16 (ptr) != ns_c_in) |
| 138 | goto unavail; |
| 139 | |
| 140 | /* Also skip over the TTL. */ |
| 141 | ptr += sizeof (uint16_t) + sizeof (uint32_t); |
| 142 | |
| 143 | /* Skip over the data length and data. */ |
| 144 | ptr += sizeof (uint16_t) + __ns_get16 (ptr); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | out: |
| 150 | *h_errnop = h_errno; |
| 151 | |
| 152 | if (ansp.ptr != buf) |
| 153 | free (ansp.ptr); |
| 154 | |
| 155 | return status; |
| 156 | } |
| 157 | |