summaryrefslogtreecommitdiff
path: root/sys/src/cmd/ip/httpd/wikipost.c
blob: 31fbedda1249ddf1db4bdddcc8e047b4e92b936d (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
/*
 * Accept new wiki pages or modifications to existing ones via POST method.
 *
 * Talks to the server at /srv/wiki.service.
 */
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "httpd.h"
#include "httpsrv.h"

#define LOG "wiki"

HConnect *hc;
HSPriv *hp;


/* go from possibly-latin1 url with escapes to utf */
char *
_urlunesc(char *s)
{
	char *t, *v, *u;
	Rune r;
	int c, n;

	/* unescape */
	u = halloc(hc, strlen(s)+1);
	for(t = u; c = *s; s++){
		if(c == '%'){
			n = s[1];
			if(n >= '0' && n <= '9')
				n = n - '0';
			else if(n >= 'A' && n <= 'F')
				n = n - 'A' + 10;
			else if(n >= 'a' && n <= 'f')
				n = n - 'a' + 10;
			else
				break;
			r = n;
			n = s[2];
			if(n >= '0' && n <= '9')
				n = n - '0';
			else if(n >= 'A' && n <= 'F')
				n = n - 'A' + 10;
			else if(n >= 'a' && n <= 'f')
				n = n - 'a' + 10;
			else
				break;
			s += 2;
			c = r*16+n;
		}
		*t++ = c;
	}
	*t = 0;

	/* latin1 heuristic */
	v = halloc(hc, UTFmax*strlen(u) + 1);
	s = u;
	t = v;
	while(*s){
		/* in decoding error, assume latin1 */
		if((n=chartorune(&r, s)) == 1 && r == 0x80)
			r = *s;
		s += n;
		t += runetochar(t, &r);
	}
	*t = 0;

	return v;
}

enum
{
	MaxLog		= 100*1024,		/* limit on length of any one log request */
};

static int
dangerous(char *s)
{
	if(s == nil)
		return 1;

	/*
	 * This check shouldn't be needed;
	 * filename folding is already supposed to have happened.
	 * But I'm paranoid.
	 */
	while(s = strchr(s,'/')){
		if(s[1]=='.' && s[2]=='.')
			return 1;
		s++;
	}
	return 0;
}

char*
unhttp(char *s)
{
	char *p, *r, *w;

	if(s == nil)
		return nil;

	for(p=s; *p; p++)
		if(*p=='+')
			*p = ' ';
	s = _urlunesc(s);

	for(r=w=s; *r; r++){
		if(*r != '\r')
			*w++ = *r;
	}
	*w = '\0';
	return s;
}

void
mountwiki(HConnect *c, char *service)
{
	char buf[128];
	int fd;

	/* already in (possibly private) namespace? */
	snprint(buf, sizeof buf, "/mnt/wiki.%s/new", service);
	if (access(buf, AREAD) == 0){
		if (bind(buf, "/mnt/wiki", MREPL) < 0){
			syslog(0, LOG, "%s bind /mnt/wiki failed: %r",
				hp->remotesys);
			hfail(c, HNotFound);
			exits("bind /mnt/wiki failed");
		}
		return;
	}

	/* old way: public wikifs from /srv */
	snprint(buf, sizeof buf, "/srv/wiki.%s", service);
	if((fd = open(buf, ORDWR)) < 0){
		syslog(0, LOG, "%s open %s failed: %r", buf, hp->remotesys);
		hfail(c, HNotFound);
		exits("failed");
	}
	if(mount(fd, -1, "/mnt/wiki", MREPL, "") < 0){
		syslog(0, LOG, "%s mount /mnt/wiki failed: %r", hp->remotesys);
		hfail(c, HNotFound);
		exits("failed");
	}
	close(fd);
}

char*
dowiki(HConnect *c, char *title, char *author, char *comment, char *base, ulong version, char *text)
{
	int fd, l, n, err;
	char *p, tmp[256];
int i;

	if((fd = open("/mnt/wiki/new", ORDWR)) < 0){
		syslog(0, LOG, "%s open /mnt/wiki/new failed: %r", hp->remotesys);
		hfail(c, HNotFound);
		exits("failed");
	}

i=0;
	if((i++,fprint(fd, "%s\nD%lud\nA%s (%s)\n", title, version, author, hp->remotesys) < 0)
	|| (i++,(comment && comment[0] && fprint(fd, "C%s\n", comment) < 0))
	|| (i++,fprint(fd, "\n") < 0)
	|| (i++,(text[0] && write(fd, text, strlen(text)) != strlen(text)))){
		syslog(0, LOG, "%s write failed %d %ld fd %d: %r", hp->remotesys, i, strlen(text), fd);
		hfail(c, HInternal);
		exits("failed");
	}

	err = write(fd, "", 0);
	if(err)
		syslog(0, LOG, "%s commit failed %d: %r", hp->remotesys, err);

	seek(fd, 0, 0);
	if((n = read(fd, tmp, sizeof(tmp)-1)) <= 0){
		if(n == 0)
			werrstr("short read");
		syslog(0, LOG, "%s read failed: %r", hp->remotesys);
		hfail(c, HInternal);
		exits("failed");
	}

	tmp[n] = '\0';

	p = halloc(c, l=strlen(base)+strlen(tmp)+40);
	snprint(p, l, "%s/%s/%s.html", base, tmp, err ? "werror" : "index");
	return p;
}


void
main(int argc, char **argv)
{
	Hio *hin, *hout;
	char *s, *t, *p, *f[10];
	char *text, *title, *service, *base, *author, *comment, *url;
	int i, nf;
	ulong version;

	hc = init(argc, argv);
	hp = hc->private;

	if(dangerous(hc->req.uri)){
		hfail(hc, HSyntax);
		exits("failed");
	}

	if(hparseheaders(hc, HSTIMEOUT) < 0)
		exits("failed");
	hout = &hc->hout;
	if(hc->head.expectother){
		hfail(hc, HExpectFail, nil);
		exits("failed");
	}
	if(hc->head.expectcont){
		hprint(hout, "100 Continue\r\n");
		hprint(hout, "\r\n");
		hflush(hout);
	}

	s = nil;
	if(strcmp(hc->req.meth, "POST") == 0){
		hin = hbodypush(&hc->hin, hc->head.contlen, hc->head.transenc);
		if(hin != nil){
			alarm(15*60*1000);
			s = hreadbuf(hin, hin->pos);
			alarm(0);
		}
		if(s == nil){
			hfail(hc, HBadReq, nil);
			exits("failed");
		}
		t = strchr(s, '\n');
		if(t != nil)
			*t = '\0';
	}else{
		hunallowed(hc, "GET, HEAD, PUT");
		exits("unallowed");
	}

	if(s == nil){
		hfail(hc, HNoData, "wiki");
		exits("failed");
	}

	text = nil;
	title = nil;
	service = nil;
	author = "???";
	comment = "";
	base = nil;
	version = ~0;
	nf = getfields(s, f, nelem(f), 1, "&");
	for(i=0; i<nf; i++){
		if((p = strchr(f[i], '=')) == nil)
			continue;
		*p++ = '\0';
		if(strcmp(f[i], "title")==0)
			title = p;
		else if(strcmp(f[i], "version")==0)
			version = strtoul(unhttp(p), 0, 10);
		else if(strcmp(f[i], "text")==0)
			text = p;
		else if(strcmp(f[i], "service")==0)
			service = p;
		else if(strcmp(f[i], "comment")==0)
			comment = p;
		else if(strcmp(f[i], "author")==0)
			author = p;
		else if(strcmp(f[i], "base")==0)
			base = p;
	}

	syslog(0, LOG, "%s post s %s t '%s' v %ld a %s c %s b %s t 0x%p",
		hp->remotesys, service, title, (long)version, author, comment, base, text);

	title = unhttp(title);
	comment = unhttp(comment);
	service = unhttp(service);
	text = unhttp(text);
	author = unhttp(author);
	base = unhttp(base);

	if(title==nil || version==~0 || text==nil || text[0]=='\0' || base == nil 
	|| service == nil || strchr(title, '\n') || strchr(comment, '\n')
	|| dangerous(service) || strchr(service, '/') || strlen(service)>20){
		syslog(0, LOG, "%s failed dangerous", hp->remotesys);
		hfail(hc, HSyntax);
		exits("failed");
	}

	syslog(0, LOG, "%s post s %s t '%s' v %ld a %s c %s",
		hp->remotesys, service, title, (long)version, author, comment);

	if(strlen(text) > MaxLog)
		text[MaxLog] = '\0';

	mountwiki(hc, service);
	url = dowiki(hc, title, author, comment, base, version, text);
	hredirected(hc, "303 See Other", url);
	exits(nil);
}