summaryrefslogtreecommitdiff
path: root/usr.sbin/rpki-client/rsync.c
blob: 6fa439a4b57e2115259b0260ef3fafca4314e32d (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*	$OpenBSD: rsync.c,v 1.53 2024/11/21 13:12:19 claudio Exp $ */
/*
 * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <err.h>
#include <errno.h>
#include <poll.h>
#include <resolv.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <imsg.h>

#include "extern.h"

#define	__STRINGIFY(x)	#x
#define	STRINGIFY(x)	__STRINGIFY(x)

/*
 * A running rsync process.
 * We can have multiple of these simultaneously and need to keep track
 * of which process maps to which request.
 */
struct rsync {
	TAILQ_ENTRY(rsync)	 entry;
	char			*uri; /* uri of this rsync proc */
	char			*dst; /* destination directory */
	char			*compdst; /* compare against directory */
	unsigned int		 id; /* identity of request */
	pid_t			 pid; /* pid of process or 0 if unassociated */
};

static TAILQ_HEAD(, rsync)	states = TAILQ_HEAD_INITIALIZER(states);

/*
 * Return the base of a rsync URI (rsync://hostname/module). The
 * caRepository provided by the RIR CAs point deeper than they should
 * which would result in many rsync calls for almost every subdirectory.
 * This is inefficient so instead crop the URI to a common base.
 * The returned string needs to be freed by the caller.
 */
char *
rsync_base_uri(const char *uri)
{
	const char *host, *module, *rest;
	char *base_uri;

	/* Case-insensitive rsync URI. */
	if (strncasecmp(uri, RSYNC_PROTO, RSYNC_PROTO_LEN) != 0) {
		warnx("%s: not using rsync schema", uri);
		return NULL;
	}

	/* Parse the non-zero-length hostname. */
	host = uri + RSYNC_PROTO_LEN;

	if ((module = strchr(host, '/')) == NULL) {
		warnx("%s: missing rsync module", uri);
		return NULL;
	} else if (module == host) {
		warnx("%s: zero-length rsync host", uri);
		return NULL;
	}

	/* The non-zero-length module follows the hostname. */
	module++;
	if (*module == '\0') {
		warnx("%s: zero-length rsync module", uri);
		return NULL;
	}

	/* The path component is optional. */
	if ((rest = strchr(module, '/')) == NULL) {
		if ((base_uri = strdup(uri)) == NULL)
			err(1, NULL);
		return base_uri;
	} else if (rest == module) {
		warnx("%s: zero-length module", uri);
		return NULL;
	}

	if ((base_uri = strndup(uri, rest - uri)) == NULL)
		err(1, NULL);
	return base_uri;
}

/*
 * The directory passed as --compare-dest needs to be relative to
 * the destination directory. This function takes care of that.
 */
static char *
rsync_fixup_dest(char *destdir, char *compdir)
{
	const char *dotdot = "../../../../../../";	/* should be enough */
	int dirs = 1;
	char *fn;
	char c;

	while ((c = *destdir++) != '\0')
		if (c == '/')
			dirs++;

	if (dirs > 6)
		/* too deep for us */
		return NULL;

	if ((asprintf(&fn, "%.*s%s", dirs * 3, dotdot, compdir)) == -1)
		err(1, NULL);
	return fn;
}

static pid_t
exec_rsync(const char *prog, const char *bind_addr, char *uri, char *dst,
    char *compdst)
{
	pid_t pid;
	char *args[32];
	char *reldst;
	int i;

	if ((pid = fork()) == -1)
		err(1, "fork");

	if (pid == 0) {
		if (pledge("stdio exec", NULL) == -1)
			err(1, "pledge");
		i = 0;
		args[i++] = (char *)prog;
		args[i++] = "-rtO";
		args[i++] = "--no-motd";
		args[i++] = "--min-size=" STRINGIFY(MIN_FILE_SIZE);
		args[i++] = "--max-size=" STRINGIFY(MAX_FILE_SIZE);
		args[i++] = "--contimeout=" STRINGIFY(MAX_CONN_TIMEOUT);
		args[i++] = "--timeout=" STRINGIFY(MAX_IO_TIMEOUT);
		args[i++] = "--include=*/";
		args[i++] = "--include=*.cer";
		args[i++] = "--include=*.crl";
		args[i++] = "--include=*.gbr";
		args[i++] = "--include=*.mft";
		args[i++] = "--include=*.roa";
		args[i++] = "--include=*.asa";
		args[i++] = "--include=*.tak";
		args[i++] = "--include=*.spl";
		args[i++] = "--exclude=*";
		if (bind_addr != NULL) {
			args[i++] = "--address";
			args[i++] = (char *)bind_addr;
		}
		if (compdst != NULL &&
		    (reldst = rsync_fixup_dest(dst, compdst)) != NULL) {
			args[i++] = "--compare-dest";
			args[i++] = reldst;
		}
		args[i++] = uri;
		args[i++] = dst;
		args[i] = NULL;
		/* XXX args overflow not prevented */
		execvp(args[0], args);
		err(1, "%s: execvp", prog);
	}

	return pid;
}

static void
rsync_new(unsigned int id, char *uri, char *dst, char *compdst)
{
	struct rsync *s;

	if ((s = calloc(1, sizeof(*s))) == NULL)
		err(1, NULL);

	s->id = id;
	s->uri = uri;
	s->dst = dst;
	s->compdst = compdst;

	TAILQ_INSERT_TAIL(&states, s, entry);
}

static void
rsync_free(struct rsync *s)
{
	TAILQ_REMOVE(&states, s, entry);
	free(s->uri);
	free(s->dst);
	free(s->compdst);
	free(s);
}

static void
proc_child(int signal)
{

	/* Nothing: just discard. */
}

/*
 * Process used for synchronising repositories.
 * This simply waits to be told which repository to synchronise, then
 * does so.
 * It then responds with the identifier of the repo that it updated.
 * It only exits cleanly when fd is closed.
 */
void
proc_rsync(char *prog, char *bind_addr, int fd)
{
	int			 nprocs = 0, npending = 0, rc = 0;
	struct pollfd		 pfd;
	struct msgbuf		 msgq;
	struct ibuf		*b, *inbuf = NULL;
	sigset_t		 mask, oldmask;
	struct rsync		*s, *ns;

	if (pledge("stdio rpath proc exec unveil", NULL) == -1)
		err(1, "pledge");

	pfd.fd = fd;
	msgbuf_init(&msgq);
	msgq.fd = fd;

	/*
	 * Unveil the command we want to run.
	 * If this has a pathname component in it, interpret as a file
	 * and unveil the file directly.
	 * Otherwise, look up the command in our PATH.
	 */

	if (strchr(prog, '/') == NULL) {
		const char *pp;
		char *save, *cmd, *path;
		struct stat stt;

		if (getenv("PATH") == NULL)
			errx(1, "PATH is unset");
		if ((path = strdup(getenv("PATH"))) == NULL)
			err(1, NULL);
		save = path;
		while ((pp = strsep(&path, ":")) != NULL) {
			if (*pp == '\0')
				continue;
			if (asprintf(&cmd, "%s/%s", pp, prog) == -1)
				err(1, NULL);
			if (lstat(cmd, &stt) == -1) {
				free(cmd);
				continue;
			} else if (unveil(cmd, "x") == -1)
				err(1, "%s: unveil", cmd);
			free(cmd);
			break;
		}
		free(save);
	} else if (unveil(prog, "x") == -1)
		err(1, "%s: unveil", prog);

	if (pledge("stdio proc exec", NULL) == -1)
		err(1, "pledge");

	/* Initialise retriever for children exiting. */

	if (sigemptyset(&mask) == -1)
		err(1, NULL);
	if (signal(SIGCHLD, proc_child) == SIG_ERR)
		err(1, NULL);
	if (sigaddset(&mask, SIGCHLD) == -1)
		err(1, NULL);
	if (sigprocmask(SIG_BLOCK, &mask, &oldmask) == -1)
		err(1, NULL);

	for (;;) {
		char *uri, *dst, *compdst;
		unsigned int id;
		pid_t pid;
		int st;

		pfd.events = 0;
		pfd.events |= POLLIN;
		if (msgbuf_queuelen(&msgq) > 0)
			pfd.events |= POLLOUT;

		if (npending > 0 && nprocs < MAX_RSYNC_REQUESTS) {
			TAILQ_FOREACH(s, &states, entry) {
				if (s->pid == 0) {
					s->pid = exec_rsync(prog, bind_addr,
					    s->uri, s->dst, s->compdst);
					if (++nprocs >= MAX_RSYNC_REQUESTS)
						break;
					if (--npending == 0)
						break;
				}
			}
		}

		if (ppoll(&pfd, 1, NULL, &oldmask) == -1) {
			if (errno != EINTR)
				err(1, "ppoll");

			/*
			 * If we've received an EINTR, it means that one
			 * of our children has exited and we can reap it
			 * and look up its identifier.
			 * Then we respond to the parent.
			 */

			while ((pid = waitpid(WAIT_ANY, &st, WNOHANG)) > 0) {
				int ok = 1;

				TAILQ_FOREACH(s, &states, entry)
					if (s->pid == pid)
						break;
				if (s == NULL)
					errx(1, "waitpid: %d unexpected", pid);

				if (!WIFEXITED(st)) {
					warnx("rsync %s terminated abnormally",
					    s->uri);
					rc = 1;
					ok = 0;
				} else if (WEXITSTATUS(st) != 0) {
					warnx("rsync %s failed", s->uri);
					ok = 0;
				}

				b = io_new_buffer();
				io_simple_buffer(b, &s->id, sizeof(s->id));
				io_simple_buffer(b, &ok, sizeof(ok));
				io_close_buffer(&msgq, b);

				rsync_free(s);
				nprocs--;
			}
			if (pid == -1 && errno != ECHILD)
				err(1, "waitpid");

			continue;
		}

		if (pfd.revents & POLLOUT) {
			if (msgbuf_write(&msgq) == -1) {
				if (errno == EPIPE)
					errx(1, "write: connection closed");
				else
					err(1, "write");
			}
		}

		/* connection closed */
		if (pfd.revents & POLLHUP)
			break;

		if (!(pfd.revents & POLLIN))
			continue;

		b = io_buf_read(fd, &inbuf);
		if (b == NULL)
			continue;

		/* Read host and module. */
		io_read_buf(b, &id, sizeof(id));
		io_read_str(b, &dst);
		io_read_str(b, &compdst);
		io_read_str(b, &uri);

		ibuf_free(b);

		if (dst != NULL) {
			rsync_new(id, uri, dst, compdst);
			npending++;
		} else {
			TAILQ_FOREACH(s, &states, entry)
				if (s->id == id)
					break;
			if (s != NULL) {
				if (s->pid != 0)
					kill(s->pid, SIGTERM);
				else
					rsync_free(s);
			}
		}
	}

	/* No need for these to be hanging around. */
	TAILQ_FOREACH_SAFE(s, &states, entry, ns) {
		if (s->pid != 0)
			kill(s->pid, SIGTERM);
		rsync_free(s);
	}

	msgbuf_clear(&msgq);
	exit(rc);
}