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