res_sendsigned.c revision 1.1.10.2 1 1.1.10.2 msaitoh /* $NetBSD: res_sendsigned.c,v 1.1.10.2 2013/06/13 04:20:30 msaitoh Exp $ */
2 1.1.10.2 msaitoh #include <sys/cdefs.h>
3 1.1.10.2 msaitoh __RCSID("$NetBSD: res_sendsigned.c,v 1.1.10.2 2013/06/13 04:20:30 msaitoh Exp $");
4 1.1.10.2 msaitoh
5 1.1.10.2 msaitoh #include "port_before.h"
6 1.1.10.2 msaitoh #include "fd_setsize.h"
7 1.1.10.2 msaitoh
8 1.1.10.2 msaitoh #include <sys/types.h>
9 1.1.10.2 msaitoh #include <sys/param.h>
10 1.1.10.2 msaitoh
11 1.1.10.2 msaitoh #include <netinet/in.h>
12 1.1.10.2 msaitoh #include <arpa/nameser.h>
13 1.1.10.2 msaitoh #include <arpa/inet.h>
14 1.1.10.2 msaitoh
15 1.1.10.2 msaitoh #include <isc/dst.h>
16 1.1.10.2 msaitoh
17 1.1.10.2 msaitoh #include <errno.h>
18 1.1.10.2 msaitoh #include <netdb.h>
19 1.1.10.2 msaitoh #include <resolv.h>
20 1.1.10.2 msaitoh #include <stdio.h>
21 1.1.10.2 msaitoh #include <stdlib.h>
22 1.1.10.2 msaitoh #include <string.h>
23 1.1.10.2 msaitoh #include <unistd.h>
24 1.1.10.2 msaitoh
25 1.1.10.2 msaitoh #include "port_after.h"
26 1.1.10.2 msaitoh
27 1.1.10.2 msaitoh #include "res_debug.h"
28 1.1.10.2 msaitoh
29 1.1.10.2 msaitoh
30 1.1.10.2 msaitoh /*% res_nsendsigned */
31 1.1.10.2 msaitoh int
32 1.1.10.2 msaitoh res_nsendsigned(res_state statp, const u_char *msg, int msglen,
33 1.1.10.2 msaitoh ns_tsig_key *key, u_char *answer, int anslen)
34 1.1.10.2 msaitoh {
35 1.1.10.2 msaitoh res_state nstatp;
36 1.1.10.2 msaitoh DST_KEY *dstkey;
37 1.1.10.2 msaitoh int usingTCP = 0;
38 1.1.10.2 msaitoh u_char *newmsg;
39 1.1.10.2 msaitoh int newmsglen, bufsize, siglen;
40 1.1.10.2 msaitoh u_char sig[64];
41 1.1.10.2 msaitoh HEADER *hp;
42 1.1.10.2 msaitoh time_t tsig_time;
43 1.1.10.2 msaitoh int ret;
44 1.1.10.2 msaitoh int len;
45 1.1.10.2 msaitoh
46 1.1.10.2 msaitoh dst_init();
47 1.1.10.2 msaitoh
48 1.1.10.2 msaitoh nstatp = (res_state) malloc(sizeof(*statp));
49 1.1.10.2 msaitoh if (nstatp == NULL) {
50 1.1.10.2 msaitoh errno = ENOMEM;
51 1.1.10.2 msaitoh return (-1);
52 1.1.10.2 msaitoh }
53 1.1.10.2 msaitoh memcpy(nstatp, statp, sizeof(*statp));
54 1.1.10.2 msaitoh
55 1.1.10.2 msaitoh bufsize = msglen + 1024;
56 1.1.10.2 msaitoh newmsg = (u_char *) malloc(bufsize);
57 1.1.10.2 msaitoh if (newmsg == NULL) {
58 1.1.10.2 msaitoh free(nstatp);
59 1.1.10.2 msaitoh errno = ENOMEM;
60 1.1.10.2 msaitoh return (-1);
61 1.1.10.2 msaitoh }
62 1.1.10.2 msaitoh memcpy(newmsg, msg, msglen);
63 1.1.10.2 msaitoh newmsglen = msglen;
64 1.1.10.2 msaitoh
65 1.1.10.2 msaitoh if (ns_samename(key->alg, NS_TSIG_ALG_HMAC_MD5) != 1)
66 1.1.10.2 msaitoh dstkey = NULL;
67 1.1.10.2 msaitoh else
68 1.1.10.2 msaitoh dstkey = dst_buffer_to_key(key->name, KEY_HMAC_MD5,
69 1.1.10.2 msaitoh NS_KEY_TYPE_AUTH_ONLY,
70 1.1.10.2 msaitoh NS_KEY_PROT_ANY,
71 1.1.10.2 msaitoh key->data, key->len);
72 1.1.10.2 msaitoh if (dstkey == NULL) {
73 1.1.10.2 msaitoh errno = EINVAL;
74 1.1.10.2 msaitoh free(nstatp);
75 1.1.10.2 msaitoh free(newmsg);
76 1.1.10.2 msaitoh return (-1);
77 1.1.10.2 msaitoh }
78 1.1.10.2 msaitoh
79 1.1.10.2 msaitoh nstatp->nscount = 1;
80 1.1.10.2 msaitoh siglen = sizeof(sig);
81 1.1.10.2 msaitoh ret = ns_sign(newmsg, &newmsglen, bufsize, NOERROR, dstkey, NULL, 0,
82 1.1.10.2 msaitoh sig, &siglen, 0);
83 1.1.10.2 msaitoh if (ret < 0) {
84 1.1.10.2 msaitoh free (nstatp);
85 1.1.10.2 msaitoh free (newmsg);
86 1.1.10.2 msaitoh dst_free_key(dstkey);
87 1.1.10.2 msaitoh if (ret == NS_TSIG_ERROR_NO_SPACE)
88 1.1.10.2 msaitoh errno = EMSGSIZE;
89 1.1.10.2 msaitoh else if (ret == -1)
90 1.1.10.2 msaitoh errno = EINVAL;
91 1.1.10.2 msaitoh return (ret);
92 1.1.10.2 msaitoh }
93 1.1.10.2 msaitoh
94 1.1.10.2 msaitoh if (newmsglen > PACKETSZ || nstatp->options & RES_USEVC)
95 1.1.10.2 msaitoh usingTCP = 1;
96 1.1.10.2 msaitoh if (usingTCP == 0)
97 1.1.10.2 msaitoh nstatp->options |= RES_IGNTC;
98 1.1.10.2 msaitoh else
99 1.1.10.2 msaitoh nstatp->options |= RES_USEVC;
100 1.1.10.2 msaitoh /*
101 1.1.10.2 msaitoh * Stop res_send printing the answer.
102 1.1.10.2 msaitoh */
103 1.1.10.2 msaitoh nstatp->options &= ~RES_DEBUG;
104 1.1.10.2 msaitoh nstatp->pfcode &= ~RES_PRF_REPLY;
105 1.1.10.2 msaitoh
106 1.1.10.2 msaitoh retry:
107 1.1.10.2 msaitoh
108 1.1.10.2 msaitoh len = res_nsend(nstatp, newmsg, newmsglen, answer, anslen);
109 1.1.10.2 msaitoh if (len < 0) {
110 1.1.10.2 msaitoh free (nstatp);
111 1.1.10.2 msaitoh free (newmsg);
112 1.1.10.2 msaitoh dst_free_key(dstkey);
113 1.1.10.2 msaitoh return (len);
114 1.1.10.2 msaitoh }
115 1.1.10.2 msaitoh
116 1.1.10.2 msaitoh ret = ns_verify(answer, &len, dstkey, sig, siglen,
117 1.1.10.2 msaitoh NULL, NULL, &tsig_time, (nstatp->options & RES_KEEPTSIG) != 0);
118 1.1.10.2 msaitoh if (ret != 0) {
119 1.1.10.2 msaitoh Dprint((statp->options & RES_DEBUG) ||
120 1.1.10.2 msaitoh ((statp->pfcode & RES_PRF_REPLY) &&
121 1.1.10.2 msaitoh (statp->pfcode & RES_PRF_HEAD1)),
122 1.1.10.2 msaitoh (stdout, ";; got answer:\n"));
123 1.1.10.2 msaitoh
124 1.1.10.2 msaitoh DprintQ((statp->options & RES_DEBUG) ||
125 1.1.10.2 msaitoh (statp->pfcode & RES_PRF_REPLY),
126 1.1.10.2 msaitoh (stdout, "%s", ""),
127 1.1.10.2 msaitoh answer, (anslen > len) ? len : anslen);
128 1.1.10.2 msaitoh
129 1.1.10.2 msaitoh if (ret > 0) {
130 1.1.10.2 msaitoh Dprint(statp->pfcode & RES_PRF_REPLY,
131 1.1.10.2 msaitoh (stdout, ";; server rejected TSIG (%s)\n",
132 1.1.10.2 msaitoh p_rcode(ret)));
133 1.1.10.2 msaitoh } else {
134 1.1.10.2 msaitoh Dprint(statp->pfcode & RES_PRF_REPLY,
135 1.1.10.2 msaitoh (stdout, ";; TSIG invalid (%s)\n",
136 1.1.10.2 msaitoh p_rcode(-ret)));
137 1.1.10.2 msaitoh }
138 1.1.10.2 msaitoh
139 1.1.10.2 msaitoh free (nstatp);
140 1.1.10.2 msaitoh free (newmsg);
141 1.1.10.2 msaitoh dst_free_key(dstkey);
142 1.1.10.2 msaitoh if (ret == -1)
143 1.1.10.2 msaitoh errno = EINVAL;
144 1.1.10.2 msaitoh else
145 1.1.10.2 msaitoh errno = ENOTTY;
146 1.1.10.2 msaitoh return (-1);
147 1.1.10.2 msaitoh }
148 1.1.10.2 msaitoh
149 1.1.10.2 msaitoh hp = (HEADER *)(void *)answer;
150 1.1.10.2 msaitoh if (hp->tc && !usingTCP && (statp->options & RES_IGNTC) == 0U) {
151 1.1.10.2 msaitoh nstatp->options &= ~RES_IGNTC;
152 1.1.10.2 msaitoh usingTCP = 1;
153 1.1.10.2 msaitoh goto retry;
154 1.1.10.2 msaitoh }
155 1.1.10.2 msaitoh Dprint((statp->options & RES_DEBUG) ||
156 1.1.10.2 msaitoh ((statp->pfcode & RES_PRF_REPLY) &&
157 1.1.10.2 msaitoh (statp->pfcode & RES_PRF_HEAD1)),
158 1.1.10.2 msaitoh (stdout, ";; got answer:\n"));
159 1.1.10.2 msaitoh
160 1.1.10.2 msaitoh DprintQ((statp->options & RES_DEBUG) ||
161 1.1.10.2 msaitoh (statp->pfcode & RES_PRF_REPLY),
162 1.1.10.2 msaitoh (stdout, "%s", ""),
163 1.1.10.2 msaitoh answer, (anslen > len) ? len : anslen);
164 1.1.10.2 msaitoh
165 1.1.10.2 msaitoh Dprint(statp->pfcode & RES_PRF_REPLY, (stdout, ";; TSIG ok\n"));
166 1.1.10.2 msaitoh
167 1.1.10.2 msaitoh free (nstatp);
168 1.1.10.2 msaitoh free (newmsg);
169 1.1.10.2 msaitoh dst_free_key(dstkey);
170 1.1.10.2 msaitoh return (len);
171 1.1.10.2 msaitoh }
172 1.1.10.2 msaitoh
173 1.1.10.2 msaitoh /*! \file */
174