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