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