Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* $NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 christos Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1985, 1993 |
| 5 | * The Regents of the University of California. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. All advertising materials mentioning features or use of this software |
| 16 | * must display the following acknowledgement: |
| 17 | * This product includes software developed by the University of |
| 18 | * California, Berkeley and its contributors. |
| 19 | * 4. Neither the name of the University nor the names of its contributors |
| 20 | * may be used to endorse or promote products derived from this software |
| 21 | * without specific prior written permission. |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 33 | * SUCH DAMAGE. |
| 34 | */ |
| 35 | |
| 36 | /* |
| 37 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
| 38 | * |
| 39 | * Permission to use, copy, modify, and distribute this software for any |
| 40 | * purpose with or without fee is hereby granted, provided that the above |
| 41 | * copyright notice and this permission notice appear in all copies, and that |
| 42 | * the name of Digital Equipment Corporation not be used in advertising or |
| 43 | * publicity pertaining to distribution of the document or software without |
| 44 | * specific, written prior permission. |
| 45 | * |
| 46 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
| 47 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES |
| 48 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT |
| 49 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 50 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 51 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 52 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 53 | * SOFTWARE. |
| 54 | */ |
| 55 | |
| 56 | /* |
| 57 | * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") |
| 58 | * Portions Copyright (c) 1996-1999 by Internet Software Consortium. |
| 59 | * |
| 60 | * Permission to use, copy, modify, and distribute this software for any |
| 61 | * purpose with or without fee is hereby granted, provided that the above |
| 62 | * copyright notice and this permission notice appear in all copies. |
| 63 | * |
| 64 | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES |
| 65 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 66 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR |
| 67 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 68 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 69 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 70 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 71 | */ |
| 72 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 73 | #include <arpa/nameser.h> |
| 74 | #include <netdb.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 75 | #include <netinet/in.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 76 | #include <stdio.h> |
| 77 | #include <string.h> |
Bernie Innocenti | 2fd418e | 2018-08-30 12:04:03 +0900 | [diff] [blame] | 78 | #include <sys/param.h> |
| 79 | #include <sys/types.h> |
| 80 | |
| 81 | #include "resolv_private.h" |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 82 | |
| 83 | /* Options. Leave them on. */ |
| 84 | #ifndef DEBUG |
| 85 | #define DEBUG |
| 86 | #endif |
| 87 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 88 | extern const char* _res_opcodes[]; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * Form all types of queries. |
| 92 | * Returns the size of the result or -1. |
| 93 | */ |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 94 | int res_nmkquery(res_state statp, int op, /* opcode of query */ |
| 95 | const char* dname, /* domain name */ |
| 96 | int cl, int type, /* class and type of query */ |
| 97 | const u_char* data, /* resource record data */ |
| 98 | int datalen, /* length of data */ |
| 99 | const u_char* /*newrr_in*/, /* new rr for modify or append */ |
| 100 | u_char* buf, /* buffer to put query */ |
| 101 | int buflen) /* size of buffer */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 102 | { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 103 | HEADER* hp; |
| 104 | u_char *cp, *ep; |
| 105 | int n; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 106 | u_char *dnptrs[20], **dpp, **lastdnptr; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 107 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 108 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 109 | if (statp->options & RES_DEBUG) |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 110 | printf(";; res_nmkquery(%s, %s, %s, %s)\n", _res_opcodes[op], dname, p_class(cl), |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 111 | p_type(type)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 112 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 113 | /* |
| 114 | * Initialize header fields. |
| 115 | */ |
| 116 | if ((buf == NULL) || (buflen < HFIXEDSZ)) return (-1); |
| 117 | memset(buf, 0, HFIXEDSZ); |
| 118 | hp = (HEADER*) (void*) buf; |
| 119 | hp->id = htons(res_randomid()); |
| 120 | hp->opcode = op; |
| 121 | hp->rd = (statp->options & RES_RECURSE) != 0U; |
| 122 | hp->ad = (statp->options & RES_USE_DNSSEC) != 0U; |
| 123 | hp->rcode = NOERROR; |
| 124 | cp = buf + HFIXEDSZ; |
| 125 | ep = buf + buflen; |
| 126 | dpp = dnptrs; |
| 127 | *dpp++ = buf; |
| 128 | *dpp++ = NULL; |
| 129 | lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0]; |
| 130 | /* |
| 131 | * perform opcode specific processing |
| 132 | */ |
| 133 | switch (op) { |
Bernie Innocenti | 8bb94ba | 2018-10-10 22:30:12 +0900 | [diff] [blame^] | 134 | case QUERY: |
| 135 | [[fallthrough]]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 136 | case NS_NOTIFY_OP: |
| 137 | if (ep - cp < QFIXEDSZ) return (-1); |
| 138 | if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, lastdnptr)) < 0) return (-1); |
| 139 | cp += n; |
| 140 | ns_put16(type, cp); |
| 141 | cp += INT16SZ; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 142 | ns_put16(cl, cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 143 | cp += INT16SZ; |
| 144 | hp->qdcount = htons(1); |
| 145 | if (op == QUERY || data == NULL) break; |
| 146 | /* |
| 147 | * Make an additional record for completion domain. |
| 148 | */ |
| 149 | if ((ep - cp) < RRFIXEDSZ) return (-1); |
| 150 | n = dn_comp((const char*) data, cp, ep - cp - RRFIXEDSZ, dnptrs, lastdnptr); |
| 151 | if (n < 0) return (-1); |
| 152 | cp += n; |
| 153 | ns_put16(T_NULL, cp); |
| 154 | cp += INT16SZ; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 155 | ns_put16(cl, cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 156 | cp += INT16SZ; |
| 157 | ns_put32(0, cp); |
| 158 | cp += INT32SZ; |
| 159 | ns_put16(0, cp); |
| 160 | cp += INT16SZ; |
| 161 | hp->arcount = htons(1); |
| 162 | break; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 163 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 164 | case IQUERY: |
| 165 | /* |
| 166 | * Initialize answer section |
| 167 | */ |
| 168 | if (ep - cp < 1 + RRFIXEDSZ + datalen) return (-1); |
| 169 | *cp++ = '\0'; /* no domain name */ |
| 170 | ns_put16(type, cp); |
| 171 | cp += INT16SZ; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 172 | ns_put16(cl, cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 173 | cp += INT16SZ; |
| 174 | ns_put32(0, cp); |
| 175 | cp += INT32SZ; |
| 176 | ns_put16(datalen, cp); |
| 177 | cp += INT16SZ; |
| 178 | if (datalen) { |
| 179 | memcpy(cp, data, (size_t) datalen); |
| 180 | cp += datalen; |
| 181 | } |
| 182 | hp->ancount = htons(1); |
| 183 | break; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 184 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 185 | default: |
| 186 | return (-1); |
| 187 | } |
| 188 | return (cp - buf); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | #ifdef RES_USE_EDNS0 |
| 192 | /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */ |
| 193 | #ifndef T_OPT |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 194 | #define T_OPT 41 |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 195 | #endif |
| 196 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 197 | int res_nopt(res_state statp, int n0, /* current offset in buffer */ |
| 198 | u_char* buf, /* buffer to put query */ |
| 199 | int buflen, /* size of buffer */ |
| 200 | int anslen) /* UDP answer buffer size */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 201 | { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 202 | HEADER* hp; |
| 203 | u_char *cp, *ep; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 204 | u_int16_t flags = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 205 | |
| 206 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 207 | if ((statp->options & RES_DEBUG) != 0U) printf(";; res_nopt()\n"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 208 | #endif |
| 209 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 210 | hp = (HEADER*) (void*) buf; |
| 211 | cp = buf + n0; |
| 212 | ep = buf + buflen; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 213 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 214 | if ((ep - cp) < 1 + RRFIXEDSZ) return (-1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 215 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 216 | *cp++ = 0; /* "." */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 217 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 218 | ns_put16(T_OPT, cp); /* TYPE */ |
| 219 | cp += INT16SZ; |
| 220 | if (anslen > 0xffff) anslen = 0xffff; |
| 221 | ns_put16(anslen, cp); /* CLASS = UDP payload size */ |
| 222 | cp += INT16SZ; |
| 223 | *cp++ = NOERROR; /* extended RCODE */ |
| 224 | *cp++ = 0; /* EDNS version */ |
| 225 | if (statp->options & RES_USE_DNSSEC) { |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 226 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 227 | if (statp->options & RES_DEBUG) printf(";; res_opt()... ENDS0 DNSSEC\n"); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 228 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 229 | flags |= NS_OPT_DNSSEC_OK; |
| 230 | } |
| 231 | ns_put16(flags, cp); |
| 232 | cp += INT16SZ; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 233 | #ifdef EDNS0_PADDING |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 234 | { |
| 235 | u_int16_t minlen = (cp - buf) + 3 * INT16SZ; |
| 236 | u_int16_t extra = minlen % EDNS0_PADDING; |
| 237 | u_int16_t padlen = (EDNS0_PADDING - extra) % EDNS0_PADDING; |
| 238 | if (minlen > buflen) { |
| 239 | return (-1); |
| 240 | } |
| 241 | padlen = MIN(padlen, buflen - minlen); |
| 242 | ns_put16(padlen + 2 * INT16SZ, cp); /* RDLEN */ |
| 243 | cp += INT16SZ; |
| 244 | ns_put16(NS_OPT_PADDING, cp); /* OPTION-CODE */ |
| 245 | cp += INT16SZ; |
| 246 | ns_put16(padlen, cp); /* OPTION-LENGTH */ |
| 247 | cp += INT16SZ; |
| 248 | memset(cp, 0, padlen); |
| 249 | cp += padlen; |
| 250 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 251 | #else |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 252 | ns_put16(0, cp); /* RDLEN */ |
| 253 | cp += INT16SZ; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 254 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 255 | hp->arcount = htons(ntohs(hp->arcount) + 1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 256 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 257 | return (cp - buf); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 258 | } |
| 259 | #endif |