summary refs log tree commit diff
path: root/www/temp.causal.agency/up.c
blob: 9e7b4ff745c039348547e3de5f50212567f11b28 (plain) (blame)
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
/* Copyright (C) 2020  June McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <err.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/capsicum.h>
#include <sys/types.h>
#include <sysexits.h>
#include <time.h>
#include <unistd.h>

#include <kcgi.h>
#include <kcgihtml.h>

static int cwd = -1;

static const struct kvalid Key = { NULL, "file" };

static enum kcgi_err head(struct kreq *req, enum khttp http, enum kmime mime) {
	return khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[http])
		|| khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[mime]);
}

static enum kcgi_err fail(struct kreq *req, enum khttp http) {
	return head(req, http, KMIME_TEXT_PLAIN)
		|| khttp_body(req)
		|| khttp_printf(req, "%s\n", khttps[http]);
}

static enum kcgi_err handle(struct kreq *req) {
	if (req->page) return fail(req, KHTTP_404);

	if (req->method == KMETHOD_GET) {
		struct khtmlreq html;
		struct khtmlreq *h = &html;
		return head(req, KHTTP_200, KMIME_TEXT_HTML)
			|| khttp_body(req)
			|| khtml_open(h, req, 0)
			|| khtml_elem(h, KELEM_DOCTYPE)
			|| khtml_elem(h, KELEM_TITLE)
			|| khtml_puts(h, "Upload")
			|| khtml_closeelem(h, 1)
			|| khtml_attr(
				h, KELEM_FORM,
				KATTR_METHOD, "post",
				KATTR_ACTION, "",
				KATTR_ENCTYPE, "multipart/form-data",
				KATTR__MAX
			)
			|| khtml_attr(
				h, KELEM_INPUT,
				KATTR_TYPE, "file",
				KATTR_NAME, Key.name,
				KATTR__MAX
			)
			|| khtml_attr(
				h, KELEM_INPUT,
				KATTR_TYPE, "submit",
				KATTR_VALUE, "Upload",
				KATTR__MAX
			)
			|| khtml_close(h);

	} else if (req->method == KMETHOD_POST) {
		struct kpair *field = req->fieldmap[0];
		if (!field || !field->valsz) return fail(req, KHTTP_400);

		char name[256];
		const char *ext = strrchr(field->file, '.');
		if (!ext) ext = "";
		snprintf(
			name, sizeof(name), "%jx%08x%s",
			(intmax_t)time(NULL), arc4random(), ext
		);

		int fd = openat(cwd, name, O_CREAT | O_EXCL | O_WRONLY, 0644);
		if (fd < 0) {
			warn("openat");
			return fail(req, KHTTP_507);
		}
		ssize_t len = write(fd, field->val, field->valsz);
		int error = close(fd);
		if (len < 0 || error) {
			warn("write");
			return fail(req, KHTTP_507);
		}

		return head(req, KHTTP_303, KMIME_TEXT_PLAIN)
			|| khttp_head(req, kresps[KRESP_LOCATION], "/%s", name)
			|| khttp_body(req)
			|| khttp_puts(req, name);

	} else {
		return fail(req, KHTTP_405);
	}
}

static void sandbox(void) {
	cwd = open(".", O_DIRECTORY);
	if (cwd < 0) err(EX_CONFIG, ".");

	int error = cap_enter();
	if (error) err(EX_OSERR, "cap_enter");

	cap_rights_t rights;
	cap_rights_init(&rights, CAP_LOOKUP, CAP_CREATE, CAP_PWRITE);
	error = cap_rights_limit(cwd, &rights);
	if (error) err(EX_OSERR, "cap_rights_limit");
}

int main(void) {
	const char *page = "up";
	if (khttp_fcgi_test()) {
		struct kfcgi *fcgi;
		enum kcgi_err error = khttp_fcgi_init(&fcgi, &Key, 1, &page, 1, 0);
		if (error) errx(EX_CONFIG, "khttp_fcgi_init: %s", kcgi_strerror(error));
		sandbox();
		for (
			struct kreq req;
			KCGI_OK == (error = khttp_fcgi_parse(fcgi, &req));
			khttp_free(&req)
		) {
			error = handle(&req);
			if (error && error != KCGI_HUP) break;
		}
		if (error != KCGI_EXIT) {
			errx(EX_PROTOCOL, "khttp_fcgi_parse: %s", kcgi_strerror(error));
		}
		khttp_fcgi_free(fcgi);
	} else {
		struct kreq req;
		enum kcgi_err error = khttp_parse(&req, &Key, 1, &page, 1, 0);
		if (error) errx(EX_PROTOCOL, "khttp_parse: %s", kcgi_strerror(error));
		error = handle(&req);
		if (error) errx(EX_PROTOCOL, "%s", kcgi_strerror(error));
		khttp_free(&req);
	}
}
ux/kernel/git/torvalds/linux.git/log/ Using something like "range" and "qwerty123456", it returns an "Internal Server Error" and the following in the logs: > [Tue Jun 10 17:45:32 2014] [error] [client 172.21.1.6] fatal: > ambiguous argument 'qwerty123456': unknown revision or path not in the > working tree., referer: > http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/ > [Tue Jun 10 17:45:32 2014] [error] [client 172.21.1.6] Use '--' to > separate paths from revisions, like this:, referer: > http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/ > [Tue Jun 10 17:45:32 2014] [error] [client 172.21.1.6] 'git <command> > [<revision>...] -- [<file>...]', referer: > http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/ > [Tue Jun 10 17:45:32 2014] [error] [client 172.21.1.6] Premature end > of script headers: cgit, referer: > http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/ The cache will kick in, so if you search for the same string again, it'll show an empty range, so you have to change the bogus strings each time. This is because we just pass the arguments straight to Git's revision parsing machinery which die()s if it cannot parse an argument, printing the above to stderr and exiting. The patch below makes it a bit friendlier by just ignoring unhandled arguments, but I can't see an easy way to report errors when we can't parse revision arguments without losing the flexibility of supporting all of the revision specifiers supported by Git. Reported-by: Konstantin Ryabitsev <mricon@kernel.org> 2014-06-28git: update for git 2.0Christian Hesse prefixcmp() and suffixcmp() have been remove, functionality is now provided by starts_with() and ends_with(). Retrurn values have been changed, so instead of just renaming we have to fix logic. Everything else looks just fine. 2014-04-17remove trailing whitespaces from source filesChristian Hesse 2014-04-12git: update to 1.9.2Christian Hesse Everything works just bumping the version in Makefile and commit hash in submodule. No code changes required. 2014-04-05Fix cgit_parse_url when a repo url is contained in another repo urlJulian Maurice For example, if I have two repos (remove-suffix is enabled): /foo /foo/bar http://cgit/foo/bar/ is interpreted as "repository 'foo', command 'bar'" instead of "repository 'foo/bar'" 2014-03-20Makefile: use more reliable git tarball mirrorJason A. Donenfeld 2014-03-20git: update to 1.9.1Christian Hesse