unbound-control.c revision 1.1.1.1.2.2 1 1.1.1.1.2.2 pgoyette /*
2 1.1.1.1.2.2 pgoyette * checkconf/unbound-control.c - remote control utility for unbound.
3 1.1.1.1.2.2 pgoyette *
4 1.1.1.1.2.2 pgoyette * Copyright (c) 2008, NLnet Labs. All rights reserved.
5 1.1.1.1.2.2 pgoyette *
6 1.1.1.1.2.2 pgoyette * This software is open source.
7 1.1.1.1.2.2 pgoyette *
8 1.1.1.1.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
9 1.1.1.1.2.2 pgoyette * modification, are permitted provided that the following conditions
10 1.1.1.1.2.2 pgoyette * are met:
11 1.1.1.1.2.2 pgoyette *
12 1.1.1.1.2.2 pgoyette * Redistributions of source code must retain the above copyright notice,
13 1.1.1.1.2.2 pgoyette * this list of conditions and the following disclaimer.
14 1.1.1.1.2.2 pgoyette *
15 1.1.1.1.2.2 pgoyette * Redistributions in binary form must reproduce the above copyright notice,
16 1.1.1.1.2.2 pgoyette * this list of conditions and the following disclaimer in the documentation
17 1.1.1.1.2.2 pgoyette * and/or other materials provided with the distribution.
18 1.1.1.1.2.2 pgoyette *
19 1.1.1.1.2.2 pgoyette * Neither the name of the NLNET LABS nor the names of its contributors may
20 1.1.1.1.2.2 pgoyette * be used to endorse or promote products derived from this software without
21 1.1.1.1.2.2 pgoyette * specific prior written permission.
22 1.1.1.1.2.2 pgoyette *
23 1.1.1.1.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 1.1.1.1.2.2 pgoyette * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 1.1.1.1.2.2 pgoyette * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 1.1.1.1.2.2 pgoyette * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 1.1.1.1.2.2 pgoyette * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 1.1.1.1.2.2 pgoyette * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 1.1.1.1.2.2 pgoyette * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 1.1.1.1.2.2 pgoyette * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 1.1.1.1.2.2 pgoyette * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 1.1.1.1.2.2 pgoyette * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 1.1.1.1.2.2 pgoyette * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1.1.1.2.2 pgoyette */
35 1.1.1.1.2.2 pgoyette
36 1.1.1.1.2.2 pgoyette /**
37 1.1.1.1.2.2 pgoyette * \file
38 1.1.1.1.2.2 pgoyette *
39 1.1.1.1.2.2 pgoyette * The remote control utility contacts the unbound server over ssl and
40 1.1.1.1.2.2 pgoyette * sends the command, receives the answer, and displays the result
41 1.1.1.1.2.2 pgoyette * from the commandline.
42 1.1.1.1.2.2 pgoyette */
43 1.1.1.1.2.2 pgoyette
44 1.1.1.1.2.2 pgoyette #include "config.h"
45 1.1.1.1.2.2 pgoyette #ifdef HAVE_GETOPT_H
46 1.1.1.1.2.2 pgoyette #include <getopt.h>
47 1.1.1.1.2.2 pgoyette #endif
48 1.1.1.1.2.2 pgoyette #ifdef HAVE_OPENSSL_SSL_H
49 1.1.1.1.2.2 pgoyette #include <openssl/ssl.h>
50 1.1.1.1.2.2 pgoyette #endif
51 1.1.1.1.2.2 pgoyette #ifdef HAVE_OPENSSL_ERR_H
52 1.1.1.1.2.2 pgoyette #include <openssl/err.h>
53 1.1.1.1.2.2 pgoyette #endif
54 1.1.1.1.2.2 pgoyette #ifdef HAVE_OPENSSL_RAND_H
55 1.1.1.1.2.2 pgoyette #include <openssl/rand.h>
56 1.1.1.1.2.2 pgoyette #endif
57 1.1.1.1.2.2 pgoyette #include "util/log.h"
58 1.1.1.1.2.2 pgoyette #include "util/config_file.h"
59 1.1.1.1.2.2 pgoyette #include "util/locks.h"
60 1.1.1.1.2.2 pgoyette #include "util/net_help.h"
61 1.1.1.1.2.2 pgoyette
62 1.1.1.1.2.2 pgoyette #ifdef HAVE_SYS_UN_H
63 1.1.1.1.2.2 pgoyette #include <sys/un.h>
64 1.1.1.1.2.2 pgoyette #endif
65 1.1.1.1.2.2 pgoyette
66 1.1.1.1.2.2 pgoyette /** Give unbound-control usage, and exit (1). */
67 1.1.1.1.2.2 pgoyette static void
68 1.1.1.1.2.2 pgoyette usage()
69 1.1.1.1.2.2 pgoyette {
70 1.1.1.1.2.2 pgoyette printf("Usage: unbound-control [options] command\n");
71 1.1.1.1.2.2 pgoyette printf(" Remote control utility for unbound server.\n");
72 1.1.1.1.2.2 pgoyette printf("Options:\n");
73 1.1.1.1.2.2 pgoyette printf(" -c file config file, default is %s\n", CONFIGFILE);
74 1.1.1.1.2.2 pgoyette printf(" -s ip[@port] server address, if omitted config is used.\n");
75 1.1.1.1.2.2 pgoyette printf(" -q quiet (don't print anything if it works ok).\n");
76 1.1.1.1.2.2 pgoyette printf(" -h show this usage help.\n");
77 1.1.1.1.2.2 pgoyette printf("Commands:\n");
78 1.1.1.1.2.2 pgoyette printf(" start start server; runs unbound(8)\n");
79 1.1.1.1.2.2 pgoyette printf(" stop stops the server\n");
80 1.1.1.1.2.2 pgoyette printf(" reload reloads the server\n");
81 1.1.1.1.2.2 pgoyette printf(" (this flushes data, stats, requestlist)\n");
82 1.1.1.1.2.2 pgoyette printf(" stats print statistics\n");
83 1.1.1.1.2.2 pgoyette printf(" stats_noreset peek at statistics\n");
84 1.1.1.1.2.2 pgoyette printf(" status display status of server\n");
85 1.1.1.1.2.2 pgoyette printf(" verbosity <number> change logging detail\n");
86 1.1.1.1.2.2 pgoyette printf(" log_reopen close and open the logfile\n");
87 1.1.1.1.2.2 pgoyette printf(" local_zone <name> <type> add new local zone\n");
88 1.1.1.1.2.2 pgoyette printf(" local_zone_remove <name> remove local zone and its contents\n");
89 1.1.1.1.2.2 pgoyette printf(" local_data <RR data...> add local data, for example\n");
90 1.1.1.1.2.2 pgoyette printf(" local_data www.example.com A 192.0.2.1\n");
91 1.1.1.1.2.2 pgoyette printf(" local_data_remove <name> remove local RR data from name\n");
92 1.1.1.1.2.2 pgoyette printf(" dump_cache print cache to stdout\n");
93 1.1.1.1.2.2 pgoyette printf(" load_cache load cache from stdin\n");
94 1.1.1.1.2.2 pgoyette printf(" lookup <name> print nameservers for name\n");
95 1.1.1.1.2.2 pgoyette printf(" flush <name> flushes common types for name from cache\n");
96 1.1.1.1.2.2 pgoyette printf(" types: A, AAAA, MX, PTR, NS,\n");
97 1.1.1.1.2.2 pgoyette printf(" SOA, CNAME, DNAME, SRV, NAPTR\n");
98 1.1.1.1.2.2 pgoyette printf(" flush_type <name> <type> flush name, type from cache\n");
99 1.1.1.1.2.2 pgoyette printf(" flush_zone <name> flush everything at or under name\n");
100 1.1.1.1.2.2 pgoyette printf(" from rr and dnssec caches\n");
101 1.1.1.1.2.2 pgoyette printf(" flush_bogus flush all bogus data\n");
102 1.1.1.1.2.2 pgoyette printf(" flush_negative flush all negative data\n");
103 1.1.1.1.2.2 pgoyette printf(" flush_stats flush statistics, make zero\n");
104 1.1.1.1.2.2 pgoyette printf(" flush_requestlist drop queries that are worked on\n");
105 1.1.1.1.2.2 pgoyette printf(" dump_requestlist show what is worked on by first thread\n");
106 1.1.1.1.2.2 pgoyette printf(" flush_infra [all | ip] remove ping, edns for one IP or all\n");
107 1.1.1.1.2.2 pgoyette printf(" dump_infra show ping and edns entries\n");
108 1.1.1.1.2.2 pgoyette printf(" set_option opt: val set option to value, no reload\n");
109 1.1.1.1.2.2 pgoyette printf(" get_option opt get option value\n");
110 1.1.1.1.2.2 pgoyette printf(" list_stubs list stub-zones and root hints in use\n");
111 1.1.1.1.2.2 pgoyette printf(" list_forwards list forward-zones in use\n");
112 1.1.1.1.2.2 pgoyette printf(" list_insecure list domain-insecure zones\n");
113 1.1.1.1.2.2 pgoyette printf(" list_local_zones list local-zones in use\n");
114 1.1.1.1.2.2 pgoyette printf(" list_local_data list local-data RRs in use\n");
115 1.1.1.1.2.2 pgoyette printf(" insecure_add zone add domain-insecure zone\n");
116 1.1.1.1.2.2 pgoyette printf(" insecure_remove zone remove domain-insecure zone\n");
117 1.1.1.1.2.2 pgoyette printf(" forward_add [+i] zone addr.. add forward-zone with servers\n");
118 1.1.1.1.2.2 pgoyette printf(" forward_remove [+i] zone remove forward zone\n");
119 1.1.1.1.2.2 pgoyette printf(" stub_add [+ip] zone addr.. add stub-zone with servers\n");
120 1.1.1.1.2.2 pgoyette printf(" stub_remove [+i] zone remove stub zone\n");
121 1.1.1.1.2.2 pgoyette printf(" +i also do dnssec insecure point\n");
122 1.1.1.1.2.2 pgoyette printf(" +p set stub to use priming\n");
123 1.1.1.1.2.2 pgoyette printf(" forward [off | addr ...] without arg show forward setup\n");
124 1.1.1.1.2.2 pgoyette printf(" or off to turn off root forwarding\n");
125 1.1.1.1.2.2 pgoyette printf(" or give list of ip addresses\n");
126 1.1.1.1.2.2 pgoyette printf(" ratelimit_list [+a] list ratelimited domains\n");
127 1.1.1.1.2.2 pgoyette printf(" +a list all, also not ratelimited\n");
128 1.1.1.1.2.2 pgoyette printf("Version %s\n", PACKAGE_VERSION);
129 1.1.1.1.2.2 pgoyette printf("BSD licensed, see LICENSE in source package for details.\n");
130 1.1.1.1.2.2 pgoyette printf("Report bugs to %s\n", PACKAGE_BUGREPORT);
131 1.1.1.1.2.2 pgoyette exit(1);
132 1.1.1.1.2.2 pgoyette }
133 1.1.1.1.2.2 pgoyette
134 1.1.1.1.2.2 pgoyette /** exit with ssl error */
135 1.1.1.1.2.2 pgoyette static void ssl_err(const char* s)
136 1.1.1.1.2.2 pgoyette {
137 1.1.1.1.2.2 pgoyette fprintf(stderr, "error: %s\n", s);
138 1.1.1.1.2.2 pgoyette ERR_print_errors_fp(stderr);
139 1.1.1.1.2.2 pgoyette exit(1);
140 1.1.1.1.2.2 pgoyette }
141 1.1.1.1.2.2 pgoyette
142 1.1.1.1.2.2 pgoyette /** setup SSL context */
143 1.1.1.1.2.2 pgoyette static SSL_CTX*
144 1.1.1.1.2.2 pgoyette setup_ctx(struct config_file* cfg)
145 1.1.1.1.2.2 pgoyette {
146 1.1.1.1.2.2 pgoyette char* s_cert=NULL, *c_key=NULL, *c_cert=NULL;
147 1.1.1.1.2.2 pgoyette SSL_CTX* ctx;
148 1.1.1.1.2.2 pgoyette
149 1.1.1.1.2.2 pgoyette if(cfg->remote_control_use_cert) {
150 1.1.1.1.2.2 pgoyette s_cert = fname_after_chroot(cfg->server_cert_file, cfg, 1);
151 1.1.1.1.2.2 pgoyette c_key = fname_after_chroot(cfg->control_key_file, cfg, 1);
152 1.1.1.1.2.2 pgoyette c_cert = fname_after_chroot(cfg->control_cert_file, cfg, 1);
153 1.1.1.1.2.2 pgoyette if(!s_cert || !c_key || !c_cert)
154 1.1.1.1.2.2 pgoyette fatal_exit("out of memory");
155 1.1.1.1.2.2 pgoyette }
156 1.1.1.1.2.2 pgoyette ctx = SSL_CTX_new(SSLv23_client_method());
157 1.1.1.1.2.2 pgoyette if(!ctx)
158 1.1.1.1.2.2 pgoyette ssl_err("could not allocate SSL_CTX pointer");
159 1.1.1.1.2.2 pgoyette if((SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2) & SSL_OP_NO_SSLv2)
160 1.1.1.1.2.2 pgoyette != SSL_OP_NO_SSLv2)
161 1.1.1.1.2.2 pgoyette ssl_err("could not set SSL_OP_NO_SSLv2");
162 1.1.1.1.2.2 pgoyette if(cfg->remote_control_use_cert) {
163 1.1.1.1.2.2 pgoyette if((SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3) & SSL_OP_NO_SSLv3)
164 1.1.1.1.2.2 pgoyette != SSL_OP_NO_SSLv3)
165 1.1.1.1.2.2 pgoyette ssl_err("could not set SSL_OP_NO_SSLv3");
166 1.1.1.1.2.2 pgoyette if(!SSL_CTX_use_certificate_chain_file(ctx,c_cert) ||
167 1.1.1.1.2.2 pgoyette !SSL_CTX_use_PrivateKey_file(ctx,c_key,SSL_FILETYPE_PEM)
168 1.1.1.1.2.2 pgoyette || !SSL_CTX_check_private_key(ctx))
169 1.1.1.1.2.2 pgoyette ssl_err("Error setting up SSL_CTX client key and cert");
170 1.1.1.1.2.2 pgoyette if (SSL_CTX_load_verify_locations(ctx, s_cert, NULL) != 1)
171 1.1.1.1.2.2 pgoyette ssl_err("Error setting up SSL_CTX verify, server cert");
172 1.1.1.1.2.2 pgoyette SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
173 1.1.1.1.2.2 pgoyette
174 1.1.1.1.2.2 pgoyette free(s_cert);
175 1.1.1.1.2.2 pgoyette free(c_key);
176 1.1.1.1.2.2 pgoyette free(c_cert);
177 1.1.1.1.2.2 pgoyette } else {
178 1.1.1.1.2.2 pgoyette /* Use ciphers that don't require authentication */
179 1.1.1.1.2.2 pgoyette if(!SSL_CTX_set_cipher_list(ctx, "aNULL"))
180 1.1.1.1.2.2 pgoyette ssl_err("Error setting NULL cipher!");
181 1.1.1.1.2.2 pgoyette }
182 1.1.1.1.2.2 pgoyette return ctx;
183 1.1.1.1.2.2 pgoyette }
184 1.1.1.1.2.2 pgoyette
185 1.1.1.1.2.2 pgoyette /** contact the server with TCP connect */
186 1.1.1.1.2.2 pgoyette static int
187 1.1.1.1.2.2 pgoyette contact_server(const char* svr, struct config_file* cfg, int statuscmd)
188 1.1.1.1.2.2 pgoyette {
189 1.1.1.1.2.2 pgoyette struct sockaddr_storage addr;
190 1.1.1.1.2.2 pgoyette socklen_t addrlen;
191 1.1.1.1.2.2 pgoyette int addrfamily = 0;
192 1.1.1.1.2.2 pgoyette int fd;
193 1.1.1.1.2.2 pgoyette /* use svr or the first config entry */
194 1.1.1.1.2.2 pgoyette if(!svr) {
195 1.1.1.1.2.2 pgoyette if(cfg->control_ifs)
196 1.1.1.1.2.2 pgoyette svr = cfg->control_ifs->str;
197 1.1.1.1.2.2 pgoyette else svr = "127.0.0.1";
198 1.1.1.1.2.2 pgoyette /* config 0 addr (everything), means ask localhost */
199 1.1.1.1.2.2 pgoyette if(strcmp(svr, "0.0.0.0") == 0)
200 1.1.1.1.2.2 pgoyette svr = "127.0.0.1";
201 1.1.1.1.2.2 pgoyette else if(strcmp(svr, "::0") == 0 ||
202 1.1.1.1.2.2 pgoyette strcmp(svr, "0::0") == 0 ||
203 1.1.1.1.2.2 pgoyette strcmp(svr, "0::") == 0 ||
204 1.1.1.1.2.2 pgoyette strcmp(svr, "::") == 0)
205 1.1.1.1.2.2 pgoyette svr = "::1";
206 1.1.1.1.2.2 pgoyette }
207 1.1.1.1.2.2 pgoyette if(strchr(svr, '@')) {
208 1.1.1.1.2.2 pgoyette if(!extstrtoaddr(svr, &addr, &addrlen))
209 1.1.1.1.2.2 pgoyette fatal_exit("could not parse IP@port: %s", svr);
210 1.1.1.1.2.2 pgoyette #ifdef HAVE_SYS_UN_H
211 1.1.1.1.2.2 pgoyette } else if(svr[0] == '/') {
212 1.1.1.1.2.2 pgoyette struct sockaddr_un* usock = (struct sockaddr_un *) &addr;
213 1.1.1.1.2.2 pgoyette usock->sun_family = AF_LOCAL;
214 1.1.1.1.2.2 pgoyette #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
215 1.1.1.1.2.2 pgoyette usock->sun_len = (socklen_t)sizeof(usock);
216 1.1.1.1.2.2 pgoyette #endif
217 1.1.1.1.2.2 pgoyette (void)strlcpy(usock->sun_path, svr, sizeof(usock->sun_path));
218 1.1.1.1.2.2 pgoyette addrlen = (socklen_t)sizeof(struct sockaddr_un);
219 1.1.1.1.2.2 pgoyette addrfamily = AF_LOCAL;
220 1.1.1.1.2.2 pgoyette #endif
221 1.1.1.1.2.2 pgoyette } else {
222 1.1.1.1.2.2 pgoyette if(!ipstrtoaddr(svr, cfg->control_port, &addr, &addrlen))
223 1.1.1.1.2.2 pgoyette fatal_exit("could not parse IP: %s", svr);
224 1.1.1.1.2.2 pgoyette }
225 1.1.1.1.2.2 pgoyette
226 1.1.1.1.2.2 pgoyette if(addrfamily == 0)
227 1.1.1.1.2.2 pgoyette addrfamily = addr_is_ip6(&addr, addrlen)?AF_INET6:AF_INET;
228 1.1.1.1.2.2 pgoyette fd = socket(addrfamily, SOCK_STREAM, 0);
229 1.1.1.1.2.2 pgoyette if(fd == -1) {
230 1.1.1.1.2.2 pgoyette #ifndef USE_WINSOCK
231 1.1.1.1.2.2 pgoyette fatal_exit("socket: %s", strerror(errno));
232 1.1.1.1.2.2 pgoyette #else
233 1.1.1.1.2.2 pgoyette fatal_exit("socket: %s", wsa_strerror(WSAGetLastError()));
234 1.1.1.1.2.2 pgoyette #endif
235 1.1.1.1.2.2 pgoyette }
236 1.1.1.1.2.2 pgoyette if(connect(fd, (struct sockaddr*)&addr, addrlen) < 0) {
237 1.1.1.1.2.2 pgoyette #ifndef USE_WINSOCK
238 1.1.1.1.2.2 pgoyette log_err_addr("connect", strerror(errno), &addr, addrlen);
239 1.1.1.1.2.2 pgoyette if(errno == ECONNREFUSED && statuscmd) {
240 1.1.1.1.2.2 pgoyette printf("unbound is stopped\n");
241 1.1.1.1.2.2 pgoyette exit(3);
242 1.1.1.1.2.2 pgoyette }
243 1.1.1.1.2.2 pgoyette #else
244 1.1.1.1.2.2 pgoyette log_err_addr("connect", wsa_strerror(WSAGetLastError()), &addr, addrlen);
245 1.1.1.1.2.2 pgoyette if(WSAGetLastError() == WSAECONNREFUSED && statuscmd) {
246 1.1.1.1.2.2 pgoyette printf("unbound is stopped\n");
247 1.1.1.1.2.2 pgoyette exit(3);
248 1.1.1.1.2.2 pgoyette }
249 1.1.1.1.2.2 pgoyette #endif
250 1.1.1.1.2.2 pgoyette exit(1);
251 1.1.1.1.2.2 pgoyette }
252 1.1.1.1.2.2 pgoyette return fd;
253 1.1.1.1.2.2 pgoyette }
254 1.1.1.1.2.2 pgoyette
255 1.1.1.1.2.2 pgoyette /** setup SSL on the connection */
256 1.1.1.1.2.2 pgoyette static SSL*
257 1.1.1.1.2.2 pgoyette setup_ssl(SSL_CTX* ctx, int fd, struct config_file* cfg)
258 1.1.1.1.2.2 pgoyette {
259 1.1.1.1.2.2 pgoyette SSL* ssl;
260 1.1.1.1.2.2 pgoyette X509* x;
261 1.1.1.1.2.2 pgoyette int r;
262 1.1.1.1.2.2 pgoyette
263 1.1.1.1.2.2 pgoyette ssl = SSL_new(ctx);
264 1.1.1.1.2.2 pgoyette if(!ssl)
265 1.1.1.1.2.2 pgoyette ssl_err("could not SSL_new");
266 1.1.1.1.2.2 pgoyette SSL_set_connect_state(ssl);
267 1.1.1.1.2.2 pgoyette (void)SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
268 1.1.1.1.2.2 pgoyette if(!SSL_set_fd(ssl, fd))
269 1.1.1.1.2.2 pgoyette ssl_err("could not SSL_set_fd");
270 1.1.1.1.2.2 pgoyette while(1) {
271 1.1.1.1.2.2 pgoyette ERR_clear_error();
272 1.1.1.1.2.2 pgoyette if( (r=SSL_do_handshake(ssl)) == 1)
273 1.1.1.1.2.2 pgoyette break;
274 1.1.1.1.2.2 pgoyette r = SSL_get_error(ssl, r);
275 1.1.1.1.2.2 pgoyette if(r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE)
276 1.1.1.1.2.2 pgoyette ssl_err("SSL handshake failed");
277 1.1.1.1.2.2 pgoyette /* wants to be called again */
278 1.1.1.1.2.2 pgoyette }
279 1.1.1.1.2.2 pgoyette
280 1.1.1.1.2.2 pgoyette /* check authenticity of server */
281 1.1.1.1.2.2 pgoyette if(SSL_get_verify_result(ssl) != X509_V_OK)
282 1.1.1.1.2.2 pgoyette ssl_err("SSL verification failed");
283 1.1.1.1.2.2 pgoyette if(cfg->remote_control_use_cert) {
284 1.1.1.1.2.2 pgoyette x = SSL_get_peer_certificate(ssl);
285 1.1.1.1.2.2 pgoyette if(!x)
286 1.1.1.1.2.2 pgoyette ssl_err("Server presented no peer certificate");
287 1.1.1.1.2.2 pgoyette X509_free(x);
288 1.1.1.1.2.2 pgoyette }
289 1.1.1.1.2.2 pgoyette
290 1.1.1.1.2.2 pgoyette return ssl;
291 1.1.1.1.2.2 pgoyette }
292 1.1.1.1.2.2 pgoyette
293 1.1.1.1.2.2 pgoyette /** send stdin to server */
294 1.1.1.1.2.2 pgoyette static void
295 1.1.1.1.2.2 pgoyette send_file(SSL* ssl, FILE* in, char* buf, size_t sz)
296 1.1.1.1.2.2 pgoyette {
297 1.1.1.1.2.2 pgoyette while(fgets(buf, (int)sz, in)) {
298 1.1.1.1.2.2 pgoyette if(SSL_write(ssl, buf, (int)strlen(buf)) <= 0)
299 1.1.1.1.2.2 pgoyette ssl_err("could not SSL_write contents");
300 1.1.1.1.2.2 pgoyette }
301 1.1.1.1.2.2 pgoyette }
302 1.1.1.1.2.2 pgoyette
303 1.1.1.1.2.2 pgoyette /** send command and display result */
304 1.1.1.1.2.2 pgoyette static int
305 1.1.1.1.2.2 pgoyette go_cmd(SSL* ssl, int quiet, int argc, char* argv[])
306 1.1.1.1.2.2 pgoyette {
307 1.1.1.1.2.2 pgoyette char pre[10];
308 1.1.1.1.2.2 pgoyette const char* space=" ";
309 1.1.1.1.2.2 pgoyette const char* newline="\n";
310 1.1.1.1.2.2 pgoyette int was_error = 0, first_line = 1;
311 1.1.1.1.2.2 pgoyette int r, i;
312 1.1.1.1.2.2 pgoyette char buf[1024];
313 1.1.1.1.2.2 pgoyette snprintf(pre, sizeof(pre), "UBCT%d ", UNBOUND_CONTROL_VERSION);
314 1.1.1.1.2.2 pgoyette if(SSL_write(ssl, pre, (int)strlen(pre)) <= 0)
315 1.1.1.1.2.2 pgoyette ssl_err("could not SSL_write");
316 1.1.1.1.2.2 pgoyette for(i=0; i<argc; i++) {
317 1.1.1.1.2.2 pgoyette if(SSL_write(ssl, space, (int)strlen(space)) <= 0)
318 1.1.1.1.2.2 pgoyette ssl_err("could not SSL_write");
319 1.1.1.1.2.2 pgoyette if(SSL_write(ssl, argv[i], (int)strlen(argv[i])) <= 0)
320 1.1.1.1.2.2 pgoyette ssl_err("could not SSL_write");
321 1.1.1.1.2.2 pgoyette }
322 1.1.1.1.2.2 pgoyette if(SSL_write(ssl, newline, (int)strlen(newline)) <= 0)
323 1.1.1.1.2.2 pgoyette ssl_err("could not SSL_write");
324 1.1.1.1.2.2 pgoyette
325 1.1.1.1.2.2 pgoyette if(argc == 1 && strcmp(argv[0], "load_cache") == 0) {
326 1.1.1.1.2.2 pgoyette send_file(ssl, stdin, buf, sizeof(buf));
327 1.1.1.1.2.2 pgoyette }
328 1.1.1.1.2.2 pgoyette
329 1.1.1.1.2.2 pgoyette while(1) {
330 1.1.1.1.2.2 pgoyette ERR_clear_error();
331 1.1.1.1.2.2 pgoyette if((r = SSL_read(ssl, buf, (int)sizeof(buf)-1)) <= 0) {
332 1.1.1.1.2.2 pgoyette if(SSL_get_error(ssl, r) == SSL_ERROR_ZERO_RETURN) {
333 1.1.1.1.2.2 pgoyette /* EOF */
334 1.1.1.1.2.2 pgoyette break;
335 1.1.1.1.2.2 pgoyette }
336 1.1.1.1.2.2 pgoyette ssl_err("could not SSL_read");
337 1.1.1.1.2.2 pgoyette }
338 1.1.1.1.2.2 pgoyette buf[r] = 0;
339 1.1.1.1.2.2 pgoyette if(first_line && strncmp(buf, "error", 5) == 0) {
340 1.1.1.1.2.2 pgoyette printf("%s", buf);
341 1.1.1.1.2.2 pgoyette was_error = 1;
342 1.1.1.1.2.2 pgoyette } else if (!quiet)
343 1.1.1.1.2.2 pgoyette printf("%s", buf);
344 1.1.1.1.2.2 pgoyette
345 1.1.1.1.2.2 pgoyette first_line = 0;
346 1.1.1.1.2.2 pgoyette }
347 1.1.1.1.2.2 pgoyette return was_error;
348 1.1.1.1.2.2 pgoyette }
349 1.1.1.1.2.2 pgoyette
350 1.1.1.1.2.2 pgoyette /** go ahead and read config, contact server and perform command and display */
351 1.1.1.1.2.2 pgoyette static int
352 1.1.1.1.2.2 pgoyette go(const char* cfgfile, char* svr, int quiet, int argc, char* argv[])
353 1.1.1.1.2.2 pgoyette {
354 1.1.1.1.2.2 pgoyette struct config_file* cfg;
355 1.1.1.1.2.2 pgoyette int fd, ret;
356 1.1.1.1.2.2 pgoyette SSL_CTX* ctx;
357 1.1.1.1.2.2 pgoyette SSL* ssl;
358 1.1.1.1.2.2 pgoyette
359 1.1.1.1.2.2 pgoyette /* read config */
360 1.1.1.1.2.2 pgoyette if(!(cfg = config_create()))
361 1.1.1.1.2.2 pgoyette fatal_exit("out of memory");
362 1.1.1.1.2.2 pgoyette if(!config_read(cfg, cfgfile, NULL))
363 1.1.1.1.2.2 pgoyette fatal_exit("could not read config file");
364 1.1.1.1.2.2 pgoyette if(!cfg->remote_control_enable)
365 1.1.1.1.2.2 pgoyette log_warn("control-enable is 'no' in the config file.");
366 1.1.1.1.2.2 pgoyette #ifdef UB_ON_WINDOWS
367 1.1.1.1.2.2 pgoyette w_config_adjust_directory(cfg);
368 1.1.1.1.2.2 pgoyette #endif
369 1.1.1.1.2.2 pgoyette ctx = setup_ctx(cfg);
370 1.1.1.1.2.2 pgoyette
371 1.1.1.1.2.2 pgoyette /* contact server */
372 1.1.1.1.2.2 pgoyette fd = contact_server(svr, cfg, argc>0&&strcmp(argv[0],"status")==0);
373 1.1.1.1.2.2 pgoyette ssl = setup_ssl(ctx, fd, cfg);
374 1.1.1.1.2.2 pgoyette
375 1.1.1.1.2.2 pgoyette /* send command */
376 1.1.1.1.2.2 pgoyette ret = go_cmd(ssl, quiet, argc, argv);
377 1.1.1.1.2.2 pgoyette
378 1.1.1.1.2.2 pgoyette SSL_free(ssl);
379 1.1.1.1.2.2 pgoyette #ifndef USE_WINSOCK
380 1.1.1.1.2.2 pgoyette close(fd);
381 1.1.1.1.2.2 pgoyette #else
382 1.1.1.1.2.2 pgoyette closesocket(fd);
383 1.1.1.1.2.2 pgoyette #endif
384 1.1.1.1.2.2 pgoyette SSL_CTX_free(ctx);
385 1.1.1.1.2.2 pgoyette config_delete(cfg);
386 1.1.1.1.2.2 pgoyette return ret;
387 1.1.1.1.2.2 pgoyette }
388 1.1.1.1.2.2 pgoyette
389 1.1.1.1.2.2 pgoyette /** getopt global, in case header files fail to declare it. */
390 1.1.1.1.2.2 pgoyette extern int optind;
391 1.1.1.1.2.2 pgoyette /** getopt global, in case header files fail to declare it. */
392 1.1.1.1.2.2 pgoyette extern char* optarg;
393 1.1.1.1.2.2 pgoyette
394 1.1.1.1.2.2 pgoyette /** Main routine for unbound-control */
395 1.1.1.1.2.2 pgoyette int main(int argc, char* argv[])
396 1.1.1.1.2.2 pgoyette {
397 1.1.1.1.2.2 pgoyette int c, ret;
398 1.1.1.1.2.2 pgoyette int quiet = 0;
399 1.1.1.1.2.2 pgoyette const char* cfgfile = CONFIGFILE;
400 1.1.1.1.2.2 pgoyette char* svr = NULL;
401 1.1.1.1.2.2 pgoyette #ifdef USE_WINSOCK
402 1.1.1.1.2.2 pgoyette int r;
403 1.1.1.1.2.2 pgoyette WSADATA wsa_data;
404 1.1.1.1.2.2 pgoyette #endif
405 1.1.1.1.2.2 pgoyette #ifdef USE_THREAD_DEBUG
406 1.1.1.1.2.2 pgoyette /* stop the file output from unbound-control, overwites the servers */
407 1.1.1.1.2.2 pgoyette extern int check_locking_order;
408 1.1.1.1.2.2 pgoyette check_locking_order = 0;
409 1.1.1.1.2.2 pgoyette #endif /* USE_THREAD_DEBUG */
410 1.1.1.1.2.2 pgoyette log_ident_set("unbound-control");
411 1.1.1.1.2.2 pgoyette log_init(NULL, 0, NULL);
412 1.1.1.1.2.2 pgoyette checklock_start();
413 1.1.1.1.2.2 pgoyette #ifdef USE_WINSOCK
414 1.1.1.1.2.2 pgoyette if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0)
415 1.1.1.1.2.2 pgoyette fatal_exit("WSAStartup failed: %s", wsa_strerror(r));
416 1.1.1.1.2.2 pgoyette /* use registry config file in preference to compiletime location */
417 1.1.1.1.2.2 pgoyette if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile")))
418 1.1.1.1.2.2 pgoyette cfgfile = CONFIGFILE;
419 1.1.1.1.2.2 pgoyette #endif
420 1.1.1.1.2.2 pgoyette
421 1.1.1.1.2.2 pgoyette ERR_load_crypto_strings();
422 1.1.1.1.2.2 pgoyette ERR_load_SSL_strings();
423 1.1.1.1.2.2 pgoyette OpenSSL_add_all_algorithms();
424 1.1.1.1.2.2 pgoyette (void)SSL_library_init();
425 1.1.1.1.2.2 pgoyette
426 1.1.1.1.2.2 pgoyette if(!RAND_status()) {
427 1.1.1.1.2.2 pgoyette /* try to seed it */
428 1.1.1.1.2.2 pgoyette unsigned char buf[256];
429 1.1.1.1.2.2 pgoyette unsigned int seed=(unsigned)time(NULL) ^ (unsigned)getpid();
430 1.1.1.1.2.2 pgoyette unsigned int v = seed;
431 1.1.1.1.2.2 pgoyette size_t i;
432 1.1.1.1.2.2 pgoyette for(i=0; i<256/sizeof(v); i++) {
433 1.1.1.1.2.2 pgoyette memmove(buf+i*sizeof(v), &v, sizeof(v));
434 1.1.1.1.2.2 pgoyette v = v*seed + (unsigned int)i;
435 1.1.1.1.2.2 pgoyette }
436 1.1.1.1.2.2 pgoyette RAND_seed(buf, 256);
437 1.1.1.1.2.2 pgoyette log_warn("no entropy, seeding openssl PRNG with time\n");
438 1.1.1.1.2.2 pgoyette }
439 1.1.1.1.2.2 pgoyette
440 1.1.1.1.2.2 pgoyette /* parse the options */
441 1.1.1.1.2.2 pgoyette while( (c=getopt(argc, argv, "c:s:qh")) != -1) {
442 1.1.1.1.2.2 pgoyette switch(c) {
443 1.1.1.1.2.2 pgoyette case 'c':
444 1.1.1.1.2.2 pgoyette cfgfile = optarg;
445 1.1.1.1.2.2 pgoyette break;
446 1.1.1.1.2.2 pgoyette case 's':
447 1.1.1.1.2.2 pgoyette svr = optarg;
448 1.1.1.1.2.2 pgoyette break;
449 1.1.1.1.2.2 pgoyette case 'q':
450 1.1.1.1.2.2 pgoyette quiet = 1;
451 1.1.1.1.2.2 pgoyette break;
452 1.1.1.1.2.2 pgoyette case '?':
453 1.1.1.1.2.2 pgoyette case 'h':
454 1.1.1.1.2.2 pgoyette default:
455 1.1.1.1.2.2 pgoyette usage();
456 1.1.1.1.2.2 pgoyette }
457 1.1.1.1.2.2 pgoyette }
458 1.1.1.1.2.2 pgoyette argc -= optind;
459 1.1.1.1.2.2 pgoyette argv += optind;
460 1.1.1.1.2.2 pgoyette if(argc == 0)
461 1.1.1.1.2.2 pgoyette usage();
462 1.1.1.1.2.2 pgoyette if(argc >= 1 && strcmp(argv[0], "start")==0) {
463 1.1.1.1.2.2 pgoyette if(execlp("unbound", "unbound", "-c", cfgfile,
464 1.1.1.1.2.2 pgoyette (char*)NULL) < 0) {
465 1.1.1.1.2.2 pgoyette fatal_exit("could not exec unbound: %s",
466 1.1.1.1.2.2 pgoyette strerror(errno));
467 1.1.1.1.2.2 pgoyette }
468 1.1.1.1.2.2 pgoyette }
469 1.1.1.1.2.2 pgoyette
470 1.1.1.1.2.2 pgoyette ret = go(cfgfile, svr, quiet, argc, argv);
471 1.1.1.1.2.2 pgoyette
472 1.1.1.1.2.2 pgoyette #ifdef USE_WINSOCK
473 1.1.1.1.2.2 pgoyette WSACleanup();
474 1.1.1.1.2.2 pgoyette #endif
475 1.1.1.1.2.2 pgoyette checklock_stop();
476 1.1.1.1.2.2 pgoyette return ret;
477 1.1.1.1.2.2 pgoyette }
478