aboutsummaryrefslogtreecommitdiff
path: root/src/config.c
blob: 6cf5609cd74a0f9e7850e95555385d56fdc33fc0 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* This file is part of NSsync 
   Copyright (C) 2011 Sergey Poznyakoff
  
   NSsync is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
  
   NSsync is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License
   along with NSsync.  If not, see <http://www.gnu.org/licenses/>. */

#include "nssync.h"
#include <limits.h>

struct grecs_list *synclist;

static struct grecs_keyword sync_kw[] = {
	{ "path",
	  NULL, "Path of a keyword in named.conf which holds file name for this synchronization block",
	  grecs_type_string, NULL, offsetof(struct nssync, kwpath) },
	{ "soa-query",
	  NULL, "Set query for retrieving SOA records",
	  grecs_type_string, NULL, offsetof(struct nssync, soa_query) },
	{ "ns-query",
	  NULL, "Set query for retrieving NS and similar records",
	  grecs_type_string, NULL, offsetof(struct nssync, ns_query) },
	{ "rr-query",
	  NULL, "Set query for retrieving the rest of RRs",
	  grecs_type_string, NULL, offsetof(struct nssync, rr_query) },
	{ "rev-rr-query",
	  NULL, "Set query for retrieving RRs from reverse delegation zones",
	  grecs_type_string, NULL, offsetof(struct nssync, rev_rr_query) },
	{ NULL }
};

static int
cb_sync(enum grecs_callback_command cmd,
	grecs_locus_t *locus,
	void *varptr,
	grecs_value_t *value,
	void *cb_data)
{
	int err = 0;
	struct nssync *sp;
	void **pdata = cb_data;

	switch (cmd) {
	case grecs_callback_section_begin:
		if (!value || value->type != GRECS_TYPE_STRING) {
			grecs_error(locus, 0, _("tag must be a string"));
			return 0;
		}
		sp = grecs_zalloc(sizeof(*sp));
		sp->tag = grecs_strdup(value->v.string);
		*pdata = sp;
		break;

	case grecs_callback_section_end:
		sp = *pdata;
		if (!sp->kwpath) {
			grecs_error(locus, 0, "path not defined");
			err = 1;
		}
		if (!sp->soa_query) {
			grecs_error(locus, 0, "soa-query not defined");
			err = 1;
		}
		if (!sp->rr_query) {
			grecs_error(locus, 0, "rr-query not defined");
			err = 1;
		}
		if (err)
			grecs_free(sp);
		else {
			if (!synclist)
				synclist = grecs_list_create();
			grecs_list_append(synclist, sp);
		}
		*pdata = NULL;
		break;

	case grecs_callback_set_value:
		grecs_error(locus, 0, _("invalid use of block statement"));
	}
	return err;
}
		
static struct grecs_keyword nssync_kw[] = {
	{ "sql-config-file",
	  "file", "Read MySQL configuration from <file>",
	  grecs_type_string, &sql_config_file },
	{ "sql-config-group",
	  "name", "Read the named group from the SQL configuration file",
	  grecs_type_string, &sql_config_group },
	{ "host",
	  "host", "Set SQL server hostname or IP address",
	  grecs_type_string, &sql_host },
	{ "database",
	  "dbname", "Set database name",
	  grecs_type_string, &database_name },
	{ "user",
	  "name", "Set SQL user name",
	  grecs_type_string, &sql_user },
	{ "password",
	  "arg", "Set SQL user password",
	  grecs_type_string, &sql_password },
	{ "ssl-ca",
	  "file", "File name of the Certificate Authority (CA) certificate",
	  grecs_type_string, &sql_cacert },

	{ "tempdir",
	  NULL, "Name for the temporary directory (must exist)",
	  grecs_type_string, &tempdir },
	
	{ "named-conf",
	  NULL, "Name of the named configuration file",
	  grecs_type_string, &named_conf_file },
	{ "bind-include-path",
	  NULL, "Set include path for BIND configuration",
	  grecs_type_string|GRECS_LIST, &bind_include_path },

	{ "compare-command",
	  NULL, "Command to compare two zone files",
	  grecs_type_string, &compare_command },
	
	{ "sync", N_("tag: string"), N_("Define a synchrinization block"),
	  grecs_type_section, NULL, 0, cb_sync, NULL, sync_kw },
	
	{ NULL }
};

void
config_help()
{
	static char docstring[] =
		N_("Configuration file structure for NSsync.\n");
	grecs_format_docstring(docstring, 0, stdout);
	grecs_format_statement_array(nssync_kw, 1, 0, stdout);
}

void
config_init()
{
	grecs_include_path_setup(DEFAULT_VERSION_INCLUDE_DIR,
				 DEFAULT_INCLUDE_DIR, NULL);
	grecs_preprocessor = DEFAULT_PREPROCESSOR;
	grecs_log_to_stderr = 1;
}

static int
sql_host_fixup()
{
	char *p;

	if (!sql_host)
		return 0;
	p = strchr(sql_host, ':');
	if (p) {
		*p++ = 0;
		if (p[0] == '/') {
			sql_socket = grecs_strdup (p);
			grecs_free(sql_host);
			sql_host = grecs_strdup ("localhost");
		} else {
			char *end;
			unsigned long n = strtoul(p, &end, 10);
			if (*end) {
				grecs_error (NULL, 0,
					     "invalid port number (near %s)",
					     end);
				return 1;
			}
			if (n == 0 || n > USHRT_MAX) {
				grecs_error (NULL, 0,
					     "port number out of range 1..%d",
					     USHRT_MAX);
				return 1;
			}
			sql_port = n;
		}
	}
	return 0;
}

void
config_parse()
{
	int err = 0;
	struct grecs_node *tree = grecs_parse(config_file);
	if (!tree)
		exit(EX_CONFIG);
	grecs_tree_reduce(tree, nssync_kw, GRECS_AGGR);
	if (grecs_tree_process(tree, nssync_kw))
		exit(EX_CONFIG);
	grecs_tree_free(tree);

	if (!synclist) {
		error("nothing to do!");
		err = 1;
	}
	
	if (sql_host_fixup())
		err = 1;

	source_named_conf();

	if (err)
		exit(EX_CONFIG);
	if (lint_mode)
		exit(0);
	if (!tempdir) {
		tempdir = getenv("TMP");
		if (!tempdir)
			tempdir = "/tmp";
	}
}

Return to:

Send suggestions and report system problems to the System administrator.