summaryrefslogtreecommitdiff
path: root/libexec/pkg-serve/pkg-serve.c
blob: 56770ef37f88495ae1e307185761ddc6bab08e09 (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
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2026 Baptiste Daroussin <bapt@FreeBSD.org>
 */

/*
 * Speaks the same protocol as "pkg ssh" (see pkg-ssh(8)):
 *   -> ok: pkg-serve <version>
 *   <- get <file> <mtime>
 *   -> ok: <size>\n<data>   or   ok: 0\n   or   ko: <error>\n
 *   <- quit
 */

#include <sys/capsicum.h>
#include <sys/stat.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define VERSION "0.1"
#define BUFSZ 32768

static void
usage(void)
{
	fprintf(stderr, "usage: pkg-serve basedir\n");
	exit(EXIT_FAILURE);
}

int
main(int argc, char *argv[])
{
	struct stat st;
	cap_rights_t rights;
	char *line = NULL;
	char *file, *age;
	size_t linecap = 0, r, toread;
	ssize_t linelen;
	off_t remaining;
	time_t mtime;
	char *end;
	int fd, ffd;
	char buf[BUFSZ];
	const char *basedir;

	if (argc != 2)
		usage();

	basedir = argv[1];

	if ((fd = open(basedir, O_DIRECTORY | O_RDONLY | O_CLOEXEC)) < 0)
		err(EXIT_FAILURE, "open(%s)", basedir);

	cap_rights_init(&rights, CAP_READ, CAP_FSTATAT, CAP_LOOKUP,
	    CAP_FCNTL);
	if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
		err(EXIT_FAILURE, "cap_rights_limit");

	if (cap_enter() < 0 && errno != ENOSYS)
		err(EXIT_FAILURE, "cap_enter");

	printf("ok: pkg-serve " VERSION "\n");
	fflush(stdout);

	while ((linelen = getline(&line, &linecap, stdin)) > 0) {
		/* trim newline */
		if (linelen > 0 && line[linelen - 1] == '\n')
			line[--linelen] = '\0';

		if (linelen == 0)
			continue;

		if (strcmp(line, "quit") == 0)
			break;

		if (strncmp(line, "get ", 4) != 0) {
			printf("ko: unknown command '%s'\n", line);
			fflush(stdout);
			continue;
		}

		file = line + 4;

		if (*file == '\0') {
			printf("ko: bad command get, expecting 'get file age'\n");
			fflush(stdout);
			continue;
		}

		/* skip leading slash */
		if (*file == '/')
			file++;

		/* find the age argument */
		age = file;
		while (*age != '\0' && !isspace((unsigned char)*age))
			age++;

		if (*age == '\0') {
			printf("ko: bad command get, expecting 'get file age'\n");
			fflush(stdout);
			continue;
		}

		*age++ = '\0';

		/* skip whitespace */
		while (isspace((unsigned char)*age))
			age++;

		if (*age == '\0') {
			printf("ko: bad command get, expecting 'get file age'\n");
			fflush(stdout);
			continue;
		}

		errno = 0;
		mtime = (time_t)strtoimax(age, &end, 10);
		if (errno != 0 || *end != '\0' || end == age) {
			printf("ko: bad number %s\n", age);
			fflush(stdout);
			continue;
		}

		if (fstatat(fd, file, &st, AT_RESOLVE_BENEATH) == -1) {
			printf("ko: file not found\n");
			fflush(stdout);
			continue;
		}

		if (!S_ISREG(st.st_mode)) {
			printf("ko: not a file\n");
			fflush(stdout);
			continue;
		}

		if (st.st_mtime <= mtime) {
			printf("ok: 0\n");
			fflush(stdout);
			continue;
		}

		if ((ffd = openat(fd, file, O_RDONLY | O_RESOLVE_BENEATH)) == -1) {
			printf("ko: file not found\n");
			fflush(stdout);
			continue;
		}

		printf("ok: %" PRIdMAX "\n", (intmax_t)st.st_size);
		fflush(stdout);

		remaining = st.st_size;
		while (remaining > 0) {
			toread = sizeof(buf);
			if ((off_t)toread > remaining)
				toread = (size_t)remaining;
			r = read(ffd, buf, toread);
			if (r <= 0)
				break;
			if (fwrite(buf, 1, r, stdout) != r)
				break;
			remaining -= r;
		}
		close(ffd);
		if (remaining > 0)
			errx(EXIT_FAILURE, "%s: file truncated during transfer",
			    file);
		fflush(stdout);
	}

	return (EXIT_SUCCESS);
}