summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: 7a2d4381a079aaf5af71a3a4447486a76f5e0c9b (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use File::Spec;
  6. use Data::Dumper;
  7. use IkiWiki;
  8. sub gen_wrapper () { #{{{
  9. $config{srcdir}=File::Spec->rel2abs($config{srcdir});
  10. $config{destdir}=File::Spec->rel2abs($config{destdir});
  11. my $this=File::Spec->rel2abs($0);
  12. if (! -x $this) {
  13. error(sprintf(gettext("%s doesn't seem to be executable"), $this));
  14. }
  15. if ($config{setup}) {
  16. error(gettext("cannot create a wrapper that uses a setup file"));
  17. }
  18. my $wrapper=possibly_foolish_untaint($config{wrapper});
  19. if (! defined $wrapper || ! length $wrapper) {
  20. error(gettext("wrapper filename not specified"));
  21. }
  22. delete $config{wrapper};
  23. my @envsave;
  24. push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
  25. CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
  26. HTTP_COOKIE REMOTE_USER HTTPS} if $config{cgi};
  27. my $envsave="";
  28. foreach my $var (@envsave) {
  29. $envsave.=<<"EOF";
  30. if ((s=getenv("$var")))
  31. addenv("$var", s);
  32. EOF
  33. }
  34. my $test_receive="";
  35. if ($config{test_receive}) {
  36. require IkiWiki::Receive;
  37. $test_receive=IkiWiki::Receive::gen_wrapper();
  38. }
  39. my $check_commit_hook="";
  40. my $pre_exec="";
  41. if ($config{post_commit}) {
  42. # Optimise checking !commit_hook_enabled() ,
  43. # so that ikiwiki does not have to be started if the
  44. # hook is disabled.
  45. #
  46. # Note that perl's flock may be implemented using fcntl
  47. # or lockf on some systems. If so, and if there is no
  48. # interop between the locking systems, the true C flock will
  49. # always succeed, and this optimisation won't work.
  50. # The perl code will later correctly check the lock,
  51. # so the right thing will still happen, though without
  52. # the benefit of this optimisation.
  53. $check_commit_hook=<<"EOF";
  54. {
  55. int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
  56. if (fd != -1) {
  57. if (flock(fd, LOCK_SH | LOCK_NB) != 0)
  58. exit(0);
  59. close(fd);
  60. }
  61. }
  62. EOF
  63. }
  64. elsif ($config{cgi}) {
  65. # Avoid more than one ikiwiki cgi running at a time by
  66. # taking a cgi lock. Since ikiwiki uses several MB of
  67. # memory, a pile up of processes could cause thrashing
  68. # otherwise.
  69. $pre_exec=<<"EOF";
  70. {
  71. int fd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
  72. if (fd != -1)
  73. flock(fd, LOCK_EX);
  74. }
  75. EOF
  76. }
  77. $Data::Dumper::Indent=0; # no newlines
  78. my $configstring=Data::Dumper->Dump([\%config], ['*config']);
  79. $configstring=~s/\\/\\\\/g;
  80. $configstring=~s/"/\\"/g;
  81. $configstring=~s/\n/\\n/g;
  82. #translators: The first parameter is a filename, and the second is
  83. #translators: a (probably not translated) error message.
  84. open(OUT, ">$wrapper.c") || error(sprintf(gettext("failed to write %s: %s"), "$wrapper.c", $!));;
  85. print OUT <<"EOF";
  86. /* A wrapper for ikiwiki, can be safely made suid. */
  87. #include <stdio.h>
  88. #include <sys/types.h>
  89. #include <sys/stat.h>
  90. #include <fcntl.h>
  91. #include <unistd.h>
  92. #include <stdlib.h>
  93. #include <string.h>
  94. #include <sys/file.h>
  95. extern char **environ;
  96. char *newenviron[$#envsave+6];
  97. int i=0;
  98. addenv(char *var, char *val) {
  99. char *s=malloc(strlen(var)+1+strlen(val)+1);
  100. if (!s)
  101. perror("malloc");
  102. sprintf(s, "%s=%s", var, val);
  103. newenviron[i++]=s;
  104. }
  105. int main (int argc, char **argv) {
  106. char *s;
  107. $check_commit_hook
  108. $test_receive
  109. $envsave
  110. newenviron[i++]="HOME=$ENV{HOME}";
  111. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  112. newenviron[i]=NULL;
  113. environ=newenviron;
  114. if (setregid(getegid(), -1) != 0 &&
  115. setregid(getegid(), -1) != 0) {
  116. perror("failed to drop real gid");
  117. exit(1);
  118. }
  119. if (setreuid(geteuid(), -1) != 0 &&
  120. setreuid(geteuid(), -1) != 0) {
  121. perror("failed to drop real uid");
  122. exit(1);
  123. }
  124. $pre_exec
  125. execl("$this", "$this", NULL);
  126. perror("exec $this");
  127. exit(1);
  128. }
  129. EOF
  130. close OUT;
  131. my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
  132. if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
  133. #translators: The parameter is a C filename.
  134. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  135. }
  136. unlink("$wrapper.c");
  137. if (defined $config{wrappergroup}) {
  138. my $gid=(getgrnam($config{wrappergroup}))[2];
  139. if (! defined $gid) {
  140. error(sprintf("bad wrappergroup"));
  141. }
  142. if (! chown(-1, $gid, "$wrapper.new")) {
  143. error("chown $wrapper.new: $!");
  144. }
  145. }
  146. if (defined $config{wrappermode} &&
  147. ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
  148. error("chmod $wrapper.new: $!");
  149. }
  150. if (! rename("$wrapper.new", $wrapper)) {
  151. error("rename $wrapper.new $wrapper: $!");
  152. }
  153. #translators: The parameter is a filename.
  154. printf(gettext("successfully generated %s"), $wrapper);
  155. print "\n";
  156. } #}}}
  157. 1