summaryrefslogtreecommitdiffstats
path: root/bin/tests/system/rpz/dnsrps.c
blob: 55d8a9106c87e92cf9367065c3779280d6b28442 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

/*
 * -a		exit(0) if dnsrps is available or dlopen() msg if not
 * -p		print the path to dnsrpzd configured in dnsrps so that
 *		    dnsrpzd can be run by a setup.sh script.
 *		    Exit(1) if dnsrps is not available
 * -n domain	print the serial number of a domain to check if a new
 *		    version of a policy zone has been transferred to dnsrpzd.
 *		    Exit(1) if dnsrps is not available
 * -w sec.ond	wait for seconds, because `sleep 0.1` is not portable
 */

#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <isc/print.h>
#include <isc/util.h>

#ifdef USE_DNSRPS
#define LIBRPZ_LIB_OPEN DNSRPS_LIB_OPEN
#include <dns/librpz.h>

librpz_t *librpz;
#else  /* ifdef USE_DNSRPS */
typedef struct {
	char c[120];
} librpz_emsg_t;
#endif /* ifdef USE_DNSRPS */

static bool
link_dnsrps(librpz_emsg_t *emsg);

#define USAGE "usage: [-ap] [-n domain] [-w sec.onds]\n"

int
main(int argc, char **argv) {
#ifdef USE_DNSRPS
	char cstr[sizeof("zone ") + 1024 + 10];
	librpz_clist_t *clist;
	librpz_client_t *client;
	librpz_rsp_t *rsp;
	uint32_t serial;
#endif /* ifdef USE_DNSRPS */
	double seconds;
	librpz_emsg_t emsg;
	char *p;
	int i;

	while ((i = getopt(argc, argv, "apn:w:")) != -1) {
		switch (i) {
		case 'a':
			if (!link_dnsrps(&emsg)) {
				printf("I:%s\n", emsg.c);
				return (1);
			}
			return (0);

		case 'p':
			if (!link_dnsrps(&emsg)) {
				fprintf(stderr, "## %s\n", emsg.c);
				return (1);
			}
#ifdef USE_DNSRPS
			printf("%s\n", librpz->dnsrpzd_path);
#else  /* ifdef USE_DNSRPS */
			UNREACHABLE();
#endif /* ifdef USE_DNSRPS */
			return (0);

		case 'n':
			if (!link_dnsrps(&emsg)) {
				fprintf(stderr, "## %s\n", emsg.c);
				return (1);
			}
#ifdef USE_DNSRPS
			/*
			 * Get the serial number of a policy zone from
			 * a running dnsrpzd daemon.
			 */
			clist = librpz->clist_create(&emsg, NULL, NULL, NULL,
						     NULL, NULL);
			if (clist == NULL) {
				fprintf(stderr, "## %s: %s\n", optarg, emsg.c);
				return (1);
			}
			snprintf(cstr, sizeof(cstr),
				 "zone %s; dnsrpzd \"\";"
				 " dnsrpzd-sock dnsrpzd.sock;"
				 " dnsrpzd-rpzf dnsrpzd.rpzf",
				 optarg);
			client = librpz->client_create(&emsg, clist, cstr,
						       true);
			if (client == NULL) {
				fprintf(stderr, "## %s\n", emsg.c);
				return (1);
			}

			rsp = NULL;
			if (!librpz->rsp_create(&emsg, &rsp, NULL, client, true,
						false) ||
			    rsp == NULL)
			{
				fprintf(stderr, "## %s\n", emsg.c);
				librpz->client_detach(&client);
				return (1);
			}

			if (!librpz->soa_serial(&emsg, &serial, optarg, rsp)) {
				fprintf(stderr, "## %s\n", emsg.c);
				librpz->client_detach(&client);
				return (1);
			}
			librpz->rsp_detach(&rsp);
			librpz->client_detach(&client);
			printf("%u\n", serial);
#else  /* ifdef USE_DNSRPS */
			UNREACHABLE();
#endif /* ifdef USE_DNSRPS */
			return (0);

		case 'w':
			seconds = strtod(optarg, &p);
			if (seconds <= 0 || *p != '\0') {
				fprintf(stderr, USAGE);
				return (1);
			}
			usleep((int)(seconds * 1000.0 * 1000.0));
			return (0);

		default:
			fprintf(stderr, USAGE);
			return (1);
		}
	}
	fprintf(stderr, USAGE);
	return (1);
}

static bool
link_dnsrps(librpz_emsg_t *emsg) {
#ifdef USE_DNSRPS
	librpz = librpz_lib_open(emsg, NULL, DNSRPS_LIBRPZ_PATH);
	if (librpz == NULL) {
		return (false);
	}

	return (true);
#else  /* ifdef USE_DNSRPS */
	snprintf(emsg->c, sizeof(emsg->c), "DNSRPS not configured");
	return (false);
#endif /* ifdef USE_DNSRPS */
}