2 * Accept new wiki pages or modifications to existing ones via POST method.
4 * Talks to the server at /srv/wiki.service.
18 /* go from possibly-latin1 url with escapes to utf */
27 u = halloc(hc, strlen(s)+1);
28 for(t = u; c = *s; s++){
31 if(n >= '0' && n <= '9')
33 else if(n >= 'A' && n <= 'F')
35 else if(n >= 'a' && n <= 'f')
41 if(n >= '0' && n <= '9')
43 else if(n >= 'A' && n <= 'F')
45 else if(n >= 'a' && n <= 'f')
56 /* latin1 heuristic */
57 v = halloc(hc, UTFmax*strlen(u) + 1);
61 /* in decoding error, assume latin1 */
62 if((n=chartorune(&r, s)) == 1 && r == 0x80)
65 t += runetochar(t, &r);
74 MaxLog = 100*1024, /* limit on length of any one log request */
84 * This check shouldn't be needed;
85 * filename folding is already supposed to have happened.
88 while(s = strchr(s,'/')){
89 if(s[1]=='.' && s[2]=='.')
118 mountwiki(HConnect *c, char *service)
123 /* already in (possibly private) namespace? */
124 snprint(buf, sizeof buf, "/mnt/wiki.%s/new", service);
125 if (access(buf, AREAD) == 0){
126 if (bind(buf, "/mnt/wiki", MREPL) < 0){
127 syslog(0, LOG, "%s bind /mnt/wiki failed: %r",
130 exits("bind /mnt/wiki failed");
135 /* old way: public wikifs from /srv */
136 snprint(buf, sizeof buf, "/srv/wiki.%s", service);
137 if((fd = open(buf, ORDWR)) < 0){
138 syslog(0, LOG, "%s open %s failed: %r", buf, hp->remotesys);
142 if(mount(fd, -1, "/mnt/wiki", MREPL, "") < 0){
143 syslog(0, LOG, "%s mount /mnt/wiki failed: %r", hp->remotesys);
151 dowiki(HConnect *c, char *title, char *author, char *comment, char *base, ulong version, char *text)
157 if((fd = open("/mnt/wiki/new", ORDWR)) < 0){
158 syslog(0, LOG, "%s open /mnt/wiki/new failed: %r", hp->remotesys);
164 if((i++,fprint(fd, "%s\nD%lud\nA%s (%s)\n", title, version, author, hp->remotesys) < 0)
165 || (i++,(comment && comment[0] && fprint(fd, "C%s\n", comment) < 0))
166 || (i++,fprint(fd, "\n") < 0)
167 || (i++,(text[0] && write(fd, text, strlen(text)) != strlen(text)))){
168 syslog(0, LOG, "%s write failed %d %ld fd %d: %r", hp->remotesys, i, strlen(text), fd);
173 err = write(fd, "", 0);
175 syslog(0, LOG, "%s commit failed %d: %r", hp->remotesys, err);
178 if((n = read(fd, tmp, sizeof(tmp)-1)) <= 0){
180 werrstr("short read");
181 syslog(0, LOG, "%s read failed: %r", hp->remotesys);
188 p = halloc(c, l=strlen(base)+strlen(tmp)+40);
189 snprint(p, l, "%s/%s/%s.html", base, tmp, err ? "werror" : "index");
195 main(int argc, char **argv)
198 char *s, *t, *p, *f[10];
199 char *text, *title, *service, *base, *author, *comment, *url;
203 hc = init(argc, argv);
206 if(dangerous(hc->req.uri)){
211 if(hparseheaders(hc, HSTIMEOUT) < 0)
214 if(hc->head.expectother){
215 hfail(hc, HExpectFail, nil);
218 if(hc->head.expectcont){
219 hprint(hout, "100 Continue\r\n");
220 hprint(hout, "\r\n");
225 if(strcmp(hc->req.meth, "POST") == 0){
226 hin = hbodypush(&hc->hin, hc->head.contlen, hc->head.transenc);
229 s = hreadbuf(hin, hin->pos);
233 hfail(hc, HBadReq, nil);
240 hunallowed(hc, "GET, HEAD, PUT");
245 hfail(hc, HNoData, "wiki");
256 nf = getfields(s, f, nelem(f), 1, "&");
258 if((p = strchr(f[i], '=')) == nil)
261 if(strcmp(f[i], "title")==0)
263 else if(strcmp(f[i], "version")==0)
264 version = strtoul(unhttp(p), 0, 10);
265 else if(strcmp(f[i], "text")==0)
267 else if(strcmp(f[i], "service")==0)
269 else if(strcmp(f[i], "comment")==0)
271 else if(strcmp(f[i], "author")==0)
273 else if(strcmp(f[i], "base")==0)
277 syslog(0, LOG, "%s post s %s t '%s' v %ld a %s c %s b %s t 0x%p",
278 hp->remotesys, service, title, (long)version, author, comment, base, text);
280 title = unhttp(title);
281 comment = unhttp(comment);
282 service = unhttp(service);
284 author = unhttp(author);
287 if(title==nil || version==~0 || text==nil || text[0]=='\0' || base == nil
288 || service == nil || strchr(title, '\n') || strchr(comment, '\n')
289 || dangerous(service) || strchr(service, '/') || strlen(service)>20){
290 syslog(0, LOG, "%s failed dangerous", hp->remotesys);
295 syslog(0, LOG, "%s post s %s t '%s' v %ld a %s c %s",
296 hp->remotesys, service, title, (long)version, author, comment);
298 if(strlen(text) > MaxLog)
301 mountwiki(hc, service);
302 url = dowiki(hc, title, author, comment, base, version, text);
303 hredirected(hc, "303 See Other", url);