summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: 31e30ad2ebb3cd2e8da33996a2e3b8cbcf890da9 (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. The fd of the lock is stored in
  69. # IKIWIKI_CGILOCK_FD so unlockwiki can close it.
  70. $pre_exec=<<"EOF";
  71. {
  72. int fd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
  73. if (fd != -1 && flock(fd, LOCK_EX) == 0) {
  74. char *fd_s;
  75. asprintf(&fd_s, "%i", fd);
  76. setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
  77. }
  78. }
  79. EOF
  80. }
  81. $Data::Dumper::Indent=0; # no newlines
  82. my $configstring=Data::Dumper->Dump([\%config], ['*config']);
  83. $configstring=~s/\\/\\\\/g;
  84. $configstring=~s/"/\\"/g;
  85. $configstring=~s/\n/\\n/g;
  86. #translators: The first parameter is a filename, and the second is
  87. #translators: a (probably not translated) error message.
  88. open(OUT, ">$wrapper.c") || error(sprintf(gettext("failed to write %s: %s"), "$wrapper.c", $!));;
  89. print OUT <<"EOF";
  90. /* A wrapper for ikiwiki, can be safely made suid. */
  91. #include <stdio.h>
  92. #include <sys/types.h>
  93. #include <sys/stat.h>
  94. #include <fcntl.h>
  95. #include <unistd.h>
  96. #include <stdlib.h>
  97. #include <string.h>
  98. #include <sys/file.h>
  99. extern char **environ;
  100. char *newenviron[$#envsave+6];
  101. int i=0;
  102. addenv(char *var, char *val) {
  103. char *s=malloc(strlen(var)+1+strlen(val)+1);
  104. if (!s)
  105. perror("malloc");
  106. sprintf(s, "%s=%s", var, val);
  107. newenviron[i++]=s;
  108. }
  109. int main (int argc, char **argv) {
  110. char *s;
  111. $check_commit_hook
  112. $test_receive
  113. $envsave
  114. newenviron[i++]="HOME=$ENV{HOME}";
  115. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  116. newenviron[i]=NULL;
  117. environ=newenviron;
  118. if (setregid(getegid(), -1) != 0 &&
  119. setregid(getegid(), -1) != 0) {
  120. perror("failed to drop real gid");
  121. exit(1);
  122. }
  123. if (setreuid(geteuid(), -1) != 0 &&
  124. setreuid(geteuid(), -1) != 0) {
  125. perror("failed to drop real uid");
  126. exit(1);
  127. }
  128. $pre_exec
  129. execl("$this", "$this", NULL);
  130. perror("exec $this");
  131. exit(1);
  132. }
  133. EOF
  134. close OUT;
  135. my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
  136. if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
  137. #translators: The parameter is a C filename.
  138. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  139. }
  140. unlink("$wrapper.c");
  141. if (defined $config{wrappergroup}) {
  142. my $gid=(getgrnam($config{wrappergroup}))[2];
  143. if (! defined $gid) {
  144. error(sprintf("bad wrappergroup"));
  145. }
  146. if (! chown(-1, $gid, "$wrapper.new")) {
  147. error("chown $wrapper.new: $!");
  148. }
  149. }
  150. if (defined $config{wrappermode} &&
  151. ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
  152. error("chmod $wrapper.new: $!");
  153. }
  154. if (! rename("$wrapper.new", $wrapper)) {
  155. error("rename $wrapper.new $wrapper: $!");
  156. }
  157. #translators: The parameter is a filename.
  158. printf(gettext("successfully generated %s"), $wrapper);
  159. print "\n";
  160. } #}}}
  161. 1