security update
[ikiwiki.git] / ikiwiki
1 #!/usr/bin/perl -T
2 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
3
4 package IkiWiki;
5 use warnings;
6 use strict;
7 use File::Spec;
8 use HTML::Template;
9 use lib '.'; # For use without installation, removed by Makefile.
10
11 use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
12             %renderedfiles %pagesources %inlinepages};
13
14 sub usage () { #{{{
15         die "usage: ikiwiki [options] source dest\n";
16 } #}}}
17
18 sub getconfig () { #{{{
19         if (! exists $ENV{WRAPPED_OPTIONS}) {
20                 %config=(
21                         wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$|\.rss$)},
22                         wiki_link_regexp => qr/\[\[(?:([^\s\]\|]+)\|)?([^\s\]]+)\]\]/,
23                         wiki_processor_regexp => qr/\[\[(\w+)\s+([^\]]+)\]\]/,
24                         wiki_file_regexp => qr/(^[-[:alnum:]_.:\/+]+$)/,
25                         verbose => 0,
26                         wikiname => "wiki",
27                         default_pageext => ".mdwn",
28                         cgi => 0,
29                         svn => 1,
30                         notify => 0,
31                         url => '',
32                         cgiurl => '',
33                         historyurl => '',
34                         diffurl => '',
35                         anonok => 0,
36                         rss => 0,
37                         sanitize => 1,
38                         rebuild => 0,
39                         refresh => 0,
40                         getctime => 0,
41                         hyperestraier => 0,
42                         wrapper => undef,
43                         wrappermode => undef,
44                         svnrepo => undef,
45                         svnpath => "trunk",
46                         srcdir => undef,
47                         destdir => undef,
48                         templatedir => "/usr/share/ikiwiki/templates",
49                         underlaydir => "/usr/share/ikiwiki/basewiki",
50                         setup => undef,
51                         adminuser => undef,
52                         adminemail => undef,
53                 );
54
55                 eval q{use Getopt::Long};
56                 GetOptions(
57                         "setup|s=s" => \$config{setup},
58                         "wikiname=s" => \$config{wikiname},
59                         "verbose|v!" => \$config{verbose},
60                         "rebuild!" => \$config{rebuild},
61                         "refresh!" => \$config{refresh},
62                         "getctime" => \$config{getctime},
63                         "wrappermode=i" => \$config{wrappermode},
64                         "svn!" => \$config{svn},
65                         "anonok!" => \$config{anonok},
66                         "hyperestraier" => \$config{hyperestraier},
67                         "rss!" => \$config{rss},
68                         "cgi!" => \$config{cgi},
69                         "notify!" => \$config{notify},
70                         "sanitize!" => \$config{sanitize},
71                         "url=s" => \$config{url},
72                         "cgiurl=s" => \$config{cgiurl},
73                         "historyurl=s" => \$config{historyurl},
74                         "diffurl=s" => \$config{diffurl},
75                         "svnrepo" => \$config{svnrepo},
76                         "svnpath" => \$config{svnpath},
77                         "adminemail=s" => \$config{adminemail},
78                         "exclude=s@" => sub {
79                                 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
80                         },
81                         "adminuser=s@" => sub {
82                                 push @{$config{adminuser}}, $_[1]
83                         },
84                         "templatedir=s" => sub {
85                                 $config{templatedir}=possibly_foolish_untaint($_[1])
86                         },
87                         "underlaydir=s" => sub {
88                                 $config{underlaydir}=possibly_foolish_untaint($_[1])
89                         },
90                         "wrapper:s" => sub {
91                                 $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap"
92                         },
93                 ) || usage();
94
95                 if (! $config{setup}) {
96                         usage() unless @ARGV == 2;
97                         $config{srcdir} = possibly_foolish_untaint(shift @ARGV);
98                         $config{destdir} = possibly_foolish_untaint(shift @ARGV);
99                         checkconfig();
100                 }
101         }
102         else {
103                 # wrapper passes a full config structure in the environment
104                 # variable
105                 eval possibly_foolish_untaint($ENV{WRAPPED_OPTIONS});
106                 checkconfig();
107         }
108 } #}}}
109
110 sub checkconfig () { #{{{
111         if ($config{cgi} && ! length $config{url}) {
112                 error("Must specify url to wiki with --url when using --cgi\n");
113         }
114         if ($config{rss} && ! length $config{url}) {
115                 error("Must specify url to wiki with --url when using --rss\n");
116         }
117         if ($config{hyperestraier} && ! length $config{url}) {
118                 error("Must specify --url when using --hyperestraier\n");
119         }
120         
121         $config{wikistatedir}="$config{srcdir}/.ikiwiki"
122                 unless exists $config{wikistatedir};
123         
124         if ($config{svn}) {
125                 require IkiWiki::Rcs::SVN;
126                 $config{rcs}=1;
127         }
128         else {
129                 require IkiWiki::Rcs::Stub;
130                 $config{rcs}=0;
131         }
132 } #}}}
133
134 sub error ($) { #{{{
135         if ($config{cgi}) {
136                 print "Content-type: text/html\n\n";
137                 print misctemplate("Error", "<p>Error: @_</p>");
138         }
139         die @_;
140 } #}}}
141
142 sub possibly_foolish_untaint ($) { #{{{
143         my $tainted=shift;
144         my ($untainted)=$tainted=~/(.*)/;
145         return $untainted;
146 } #}}}
147
148 sub debug ($) { #{{{
149         return unless $config{verbose};
150         if (! $config{cgi}) {
151                 print "@_\n";
152         }
153         else {
154                 print STDERR "@_\n";
155         }
156 } #}}}
157
158 sub basename ($) { #{{{
159         my $file=shift;
160
161         $file=~s!.*/+!!;
162         return $file;
163 } #}}}
164
165 sub dirname ($) { #{{{
166         my $file=shift;
167
168         $file=~s!/*[^/]+$!!;
169         return $file;
170 } #}}}
171
172 sub pagetype ($) { #{{{
173         my $page=shift;
174         
175         if ($page =~ /\.mdwn$/) {
176                 return ".mdwn";
177         }
178         else {
179                 return "unknown";
180         }
181 } #}}}
182
183 sub pagename ($) { #{{{
184         my $file=shift;
185
186         my $type=pagetype($file);
187         my $page=$file;
188         $page=~s/\Q$type\E*$// unless $type eq 'unknown';
189         return $page;
190 } #}}}
191
192 sub htmlpage ($) { #{{{
193         my $page=shift;
194
195         return $page.".html";
196 } #}}}
197
198 sub srcfile ($) { #{{{
199         my $file=shift;
200
201         return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
202         return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
203         error("internal error: $file cannot be found");
204 } #}}}
205
206 sub readfile ($;$) { #{{{
207         my $file=shift;
208         my $binary=shift;
209
210         if (-l $file) {
211                 error("cannot read a symlink ($file)");
212         }
213         
214         local $/=undef;
215         open (IN, $file) || error("failed to read $file: $!");
216         binmode(IN) if $binary;
217         my $ret=<IN>;
218         close IN;
219         return $ret;
220 } #}}}
221
222 sub writefile ($$$;$) { #{{{
223         my $file=shift; # can include subdirs
224         my $destdir=shift; # directory to put file in
225         my $content=shift;
226         my $binary=shift;
227         
228         my $test=$file;
229         while (length $test) {
230                 if (-l "$destdir/$test") {
231                         error("cannot write to a symlink ($test)");
232                 }
233                 $test=dirname($test);
234         }
235
236         my $dir=dirname("$destdir/$file");
237         if (! -d $dir) {
238                 my $d="";
239                 foreach my $s (split(m!/+!, $dir)) {
240                         $d.="$s/";
241                         if (! -d $d) {
242                                 mkdir($d) || error("failed to create directory $d: $!");
243                         }
244                 }
245         }
246         
247         open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
248         binmode(OUT) if $binary;
249         print OUT $content;
250         close OUT;
251 } #}}}
252
253 sub bestlink ($$) { #{{{
254         # Given a page and the text of a link on the page, determine which
255         # existing page that link best points to. Prefers pages under a
256         # subdirectory with the same name as the source page, failing that
257         # goes down the directory tree to the base looking for matching
258         # pages.
259         my $page=shift;
260         my $link=lc(shift);
261         
262         my $cwd=$page;
263         do {
264                 my $l=$cwd;
265                 $l.="/" if length $l;
266                 $l.=$link;
267
268                 if (exists $links{$l}) {
269                         #debug("for $page, \"$link\", use $l");
270                         return $l;
271                 }
272         } while $cwd=~s!/?[^/]+$!!;
273
274         #print STDERR "warning: page $page, broken link: $link\n";
275         return "";
276 } #}}}
277
278 sub isinlinableimage ($) { #{{{
279         my $file=shift;
280         
281         $file=~/\.(png|gif|jpg|jpeg)$/i;
282 } #}}}
283
284 sub pagetitle ($) { #{{{
285         my $page=shift;
286         $page=~s/__(\d+)__/&#$1;/g;
287         $page=~y/_/ /;
288         return $page;
289 } #}}}
290
291 sub titlepage ($) { #{{{
292         my $title=shift;
293         $title=~y/ /_/;
294         $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
295         return $title;
296 } #}}}
297
298 sub cgiurl (@) { #{{{
299         my %params=@_;
300
301         return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
302 } #}}}
303
304 sub styleurl (;$) { #{{{
305         my $page=shift;
306
307         return "$config{url}/style.css" if ! defined $page;
308         
309         $page=~s/[^\/]+$//;
310         $page=~s/[^\/]+\//..\//g;
311         return $page."style.css";
312 } #}}}
313
314 sub htmllink ($$;$$$) { #{{{
315         my $page=shift;
316         my $link=shift;
317         my $noimageinline=shift; # don't turn links into inline html images
318         my $forcesubpage=shift; # force a link to a subpage
319         my $linktext=shift; # set to force the link text to something
320
321         my $bestlink;
322         if (! $forcesubpage) {
323                 $bestlink=bestlink($page, $link);
324         }
325         else {
326                 $bestlink="$page/".lc($link);
327         }
328
329         $linktext=pagetitle(basename($link)) unless defined $linktext;
330         
331         return $linktext if length $bestlink && $page eq $bestlink;
332         
333         # TODO BUG: %renderedfiles may not have it, if the linked to page
334         # was also added and isn't yet rendered! Note that this bug is
335         # masked by the bug mentioned below that makes all new files
336         # be rendered twice.
337         if (! grep { $_ eq $bestlink } values %renderedfiles) {
338                 $bestlink=htmlpage($bestlink);
339         }
340         if (! grep { $_ eq $bestlink } values %renderedfiles) {
341                 return "<span><a href=\"".
342                         cgiurl(do => "create", page => $link, from =>$page).
343                         "\">?</a>$linktext</span>"
344         }
345         
346         $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
347         
348         if (! $noimageinline && isinlinableimage($bestlink)) {
349                 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
350         }
351         return "<a href=\"$bestlink\">$linktext</a>";
352 } #}}}
353
354 sub indexlink () { #{{{
355         return "<a href=\"$config{url}\">$config{wikiname}</a>";
356 } #}}}
357
358 sub lockwiki () { #{{{
359         # Take an exclusive lock on the wiki to prevent multiple concurrent
360         # run issues. The lock will be dropped on program exit.
361         if (! -d $config{wikistatedir}) {
362                 mkdir($config{wikistatedir});
363         }
364         open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
365                 error ("cannot write to $config{wikistatedir}/lockfile: $!");
366         if (! flock(WIKILOCK, 2 | 4)) {
367                 debug("wiki seems to be locked, waiting for lock");
368                 my $wait=600; # arbitrary, but don't hang forever to 
369                               # prevent process pileup
370                 for (1..600) {
371                         return if flock(WIKILOCK, 2 | 4);
372                         sleep 1;
373                 }
374                 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
375         }
376 } #}}}
377
378 sub unlockwiki () { #{{{
379         close WIKILOCK;
380 } #}}}
381
382 sub loadindex () { #{{{
383         open (IN, "$config{wikistatedir}/index") || return;
384         while (<IN>) {
385                 $_=possibly_foolish_untaint($_);
386                 chomp;
387                 my %items;
388                 $items{link}=[];
389                 foreach my $i (split(/ /, $_)) {
390                         my ($item, $val)=split(/=/, $i, 2);
391                         push @{$items{$item}}, $val;
392                 }
393
394                 next unless exists $items{src}; # skip bad lines for now
395
396                 my $page=pagename($items{src}[0]);
397                 if (! $config{rebuild}) {
398                         $pagesources{$page}=$items{src}[0];
399                         $oldpagemtime{$page}=$items{mtime}[0];
400                         $oldlinks{$page}=[@{$items{link}}];
401                         $links{$page}=[@{$items{link}}];
402                         $inlinepages{$page}=join(" ", @{$items{inlinepage}})
403                                 if exists $items{inlinepage};
404                         $renderedfiles{$page}=$items{dest}[0];
405                 }
406                 $pagectime{$page}=$items{ctime}[0];
407         }
408         close IN;
409 } #}}}
410
411 sub saveindex () { #{{{
412         if (! -d $config{wikistatedir}) {
413                 mkdir($config{wikistatedir});
414         }
415         open (OUT, ">$config{wikistatedir}/index") || 
416                 error("cannot write to $config{wikistatedir}/index: $!");
417         foreach my $page (keys %oldpagemtime) {
418                 next unless $oldpagemtime{$page};
419                 my $line="mtime=$oldpagemtime{$page} ".
420                         "ctime=$pagectime{$page} ".
421                         "src=$pagesources{$page} ".
422                         "dest=$renderedfiles{$page}";
423                 $line.=" link=$_" foreach @{$links{$page}};
424                 if (exists $inlinepages{$page}) {
425                         $line.=" inlinepage=$_" foreach split " ", $inlinepages{$page};
426                 }
427                 print OUT $line."\n";
428         }
429         close OUT;
430 } #}}}
431
432 sub misctemplate ($$) { #{{{
433         my $title=shift;
434         my $pagebody=shift;
435         
436         my $template=HTML::Template->new(
437                 filename => "$config{templatedir}/misc.tmpl"
438         );
439         $template->param(
440                 title => $title,
441                 indexlink => indexlink(),
442                 wikiname => $config{wikiname},
443                 pagebody => $pagebody,
444                 styleurl => styleurl(),
445                 baseurl => "$config{url}/",
446         );
447         return $template->output;
448 }#}}}
449
450 sub glob_match ($$) { #{{{
451         my $page=shift;
452         my $glob=shift;
453
454         # turn glob into safe regexp
455         $glob=quotemeta($glob);
456         $glob=~s/\\\*/.*/g;
457         $glob=~s/\\\?/./g;
458         $glob=~s!\\/!/!g;
459         
460         $page=~/^$glob$/i;
461 } #}}}
462
463 sub globlist_match ($$) { #{{{
464         my $page=shift;
465         my @globlist=split(" ", shift);
466
467         # check any negated globs first
468         foreach my $glob (@globlist) {
469                 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
470         }
471
472         foreach my $glob (@globlist) {
473                 return 1 if glob_match($page, $glob);
474         }
475         
476         return 0;
477 } #}}}
478
479 sub main () { #{{{
480         getconfig();
481         
482         if ($config{cgi}) {
483                 lockwiki();
484                 loadindex();
485                 require IkiWiki::CGI;
486                 cgi();
487         }
488         elsif ($config{setup}) {
489                 require IkiWiki::Setup;
490                 setup();
491         }
492         elsif ($config{wrapper}) {
493                 lockwiki();
494                 require IkiWiki::Wrapper;
495                 gen_wrapper();
496         }
497         else {
498                 lockwiki();
499                 loadindex();
500                 require IkiWiki::Render;
501                 rcs_update();
502                 rcs_notify() if $config{notify};
503                 rcs_getctime() if $config{getctime};
504                 refresh();
505                 saveindex();
506         }
507 } #}}}
508
509 main;