summaryrefslogtreecommitdiffabout
path: root/src/nssync.c
blob: 9f8fb9ddc52c8dadf6d445a76bd8a0c5ac47d58a (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* 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 <sys/stat.h>
#include <fcntl.h>

int lint_mode;
int dry_run_mode;
int preprocess_only;
int debug_level;
char *config_file = SYSCONFDIR "/nssync.conf";
char *slave_status_file;
char *pidfile;
int force;
char *tempdir;
char *reload_command = "/usr/sbin/rndc reload";
char *compare_command = "cmp $oldfile $newfile > /dev/null";
unsigned error_count;
unsigned changed_zones;

#include "cmdline.h"

void
verror(const char *fmt, va_list ap)
{
	fprintf(stderr, "%s: ", program_name);
	vfprintf(stderr, fmt, ap);
	fputc('\n', stderr);
}

void
error(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	verror(fmt, ap);
	va_end(ap);
}

void
debug_printf(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	fprintf(stderr, "%s: DEBUG: ", program_name);
	vfprintf(stderr, fmt, ap);
	fputc('\n', stderr);
	va_end(ap);
}
		

int
copy_file(const char *file_name, FILE *infile, const char *dst_file)
{
	int out_fd;
	struct stat st;
	int rc;
	char *buf;
	size_t bufsize;
	size_t fsize;
  
	changed_zones++;
	if (dry_run_mode)
		return 0;
	
	if (fstat(fileno(infile), &st)) {
		error("cannot stat source file %s: %s",
		      file_name, strerror(errno));
		return 1;
	}

	out_fd = open(dst_file, O_WRONLY|O_TRUNC|O_CREAT, 0640);
	if (out_fd == -1) {
		error("cannot create destination file %s: %s",
			dst_file, strerror(errno));
		return 1;
	}

	buf = NULL;
	fsize = st.st_size;

	for (bufsize = fsize; bufsize > 0 && (buf = malloc (bufsize)) == NULL;
	     bufsize /= 2)
		;
	if (bufsize == 0)
		grecs_alloc_die ();

	rc = 0;
	fseeko(infile, 0, SEEK_SET);
	while (fsize > 0) {
		size_t rest;
		size_t rdbytes;
		
		rest = fsize > bufsize ? bufsize : fsize;
		rdbytes = fread(buf, 1, rest, infile);
		if (rdbytes == -1) {
			error("unexpected error reading %s: %s",
			      file_name, strerror(errno));
			rc = 1;
			break;
		}
		rest = write(out_fd, buf, rdbytes);
		if (rest == -1)	{
			error("unexpected error writing to %s: %s",
			      dst_file, strerror(errno));
			rc = 1;
			break;
		} else if (rest != rdbytes) {
			error("short write on %s", dst_file);
			rc = 1;
		}
		fsize -= rdbytes;
	}
	free(buf);

	close(out_fd);
	if (rc)
		unlink(dst_file);

	return rc;
}


int
compare(const char *oldfile, const char *newfile)
{
	const char *env[5];
	struct wordsplit ws;
	int rc;

	if (access(newfile, F_OK) && errno == ENOENT) {
		debug(2,("destination file %s does not exist", newfile));
		return 1;
	}
	
	env[0] = "oldfile";
	env[1] = oldfile;
	env[2] = "newfile";
	env[3] = newfile;
	env[4] = 0;
	ws.ws_env = env;
	if (wordsplit(compare_command, &ws,
		      WRDSF_NOCMD | WRDSF_ENV | WRDSF_ENV_KV |
		      WRDSF_NOSPLIT | WRDSF_KEEPUNDEF)) {
		error("cannot split compare-command: %s",
		      wordsplit_strerror(&ws));
		exit(EX_SOFTWARE);
	}

	debug(2,("running %s", ws.ws_wordv[0]));
	rc = system(ws.ws_wordv[0]);
	wordsplit_free(&ws);
	debug(2,("command returned %d", rc));
	return rc;
}


static void
synchronize(struct nssync *sp)
{
	const char *file_name;
	mode_t um;
	size_t size = 0;
	const char *env[5];
	struct wordsplit ws;
	int rc;
	
	debug(1, ("processing %s", sp->tag));
	if (grecs_asprintf(&sp->temp_file_name, &size,
			   "%s/%s.tmp", tempdir, sp->tag))
		grecs_alloc_die();
	
	um = umask(077);
	sp->fp = fopen(sp->temp_file_name, "w+");
	umask(um);
	if (!sp->fp) {
		error("cannot open %s: %s", sp->temp_file_name,
		      strerror(errno));
		return;
	}
	format_zones(sp);
	if (!dry_run_mode)
		unlink(sp->temp_file_name);
	free(sp->temp_file_name);
}

void
check_slave_status()
{
	FILE *fp = fopen(slave_status_file, "r");
	char *saved_file = NULL;
	char *saved_off;
	char *buf = NULL;
	size_t size = 0;
	char *sql_file, *sql_off;
	
	if (fp) {
		if (grecs_getline(&buf, &size, fp) > 0) {
			buf[strlen(buf)-1] = 0;
			saved_file = buf;
			saved_off = strchr(saved_file, ' ');
			if (saved_off)
				*saved_off++ = 0;
		}
		fclose(fp);
	}

	if (sql_get_slave_status(&sql_file, &sql_off)) 
		unlink(slave_status_file);
	else if (!force && saved_file && saved_off &&
		 strcmp(sql_file, saved_file) == 0 &&
		 strcmp(sql_off, saved_off) == 0) {
		debug(1, ("slave status hasn't changed: nothing to do"));
		exit(0);
	}
	fp = fopen(slave_status_file, "w");
	if (fp) {
		fprintf(fp, "%s %s\n", sql_file, sql_off);
		fclose(fp);
	}
	free(sql_file);
	free(sql_off);
}

void
create_pidfile()
{
	FILE *fp;

	if (!pidfile)
		return;
	
	fp = fopen(pidfile, "w");
	if (!fp) {
		error(_("cannot create pidfile `%s': %s"),
		      pidfile, strerror(errno));
		exit(EX_TEMPFAIL);
	}
	fprintf(fp, "%lu", (unsigned long) getpid());
	fclose(fp);
}

void
remove_pidfile()
{
	if (pidfile && unlink(pidfile))
		error(_("cannot unlink pidfile `%s': %s"),
		      pidfile, strerror(errno));
}

/* Check whether pidfile NAME exists and if so, whether its PID is still
   active. Exit if it is. */
void
check_pidfile()
{
	unsigned long pid;
	FILE *fp;

	if (!pidfile)
		return;
	
	fp = fopen(pidfile, "r");
	
	if (!fp) {
		if (errno == ENOENT)
			return;
		error(_("cannot open pidfile `%s': %s"),
		      pidfile, strerror(errno));
		exit(EX_TEMPFAIL);
	}
	if (fscanf(fp, "%lu", &pid) != 1) {
		error(_("cannot get pid from pidfile `%s'"), pidfile);
	} else {
		if (kill(pid, 0) == 0) {
			error(_("%s appears to run with pid %lu. "
				"If it does not, remove `%s' and retry."),
			      program_name, pid, pidfile);
			exit(EX_TEMPFAIL);
		}
	}
		
	fclose(fp);
	if (unlink(pidfile)) {
		error(_("cannot unlink pidfile `%s': %s"),
		      pidfile, strerror(errno));
		exit(EX_NOPERM);
	}

	create_pidfile();
	atexit(remove_pidfile);
}



int
main(int argc, char **argv)
{
	struct grecs_list_entry *ep;
	
	config_init();
	parse_options(argc, argv);
        if (preprocess_only)
		exit(grecs_preproc_run(config_file, grecs_preprocessor) ?
		     EX_CONFIG : 0);
	config_parse();
	sql_connect();

	check_pidfile();
	
	if (slave_status_file)
		check_slave_status();
	
	for (ep = synclist->head; ep; ep = ep->next)
		synchronize(ep->data);

	if (error_count)
		exit(EX_UNAVAILABLE);

	for (ep = synclist->head; ep; ep = ep->next)
		flush_zone_list(ep->data);

	if (error_count)
		exit(EX_UNAVAILABLE);

	if (changed_zones) {
		debug(1,("about to run %s", reload_command));
		if (!dry_run_mode) {
			int rc = system(reload_command);
			if (rc) {
				debug(1,("reload command returned %d", rc));
				exit(EX_UNAVAILABLE);
			}
		}
	}
	
	exit(0);
}

Return to:

Send suggestions and report system problems to the System administrator.