summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: 99237d3b5c5b46b6b5afb85436938b1cbbd329c4 (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. if ($config{post_commit}) {
  41. # Optimise checking !commit_hook_enabled() ,
  42. # so that ikiwiki does not have to be started if the
  43. # hook is disabled.
  44. #
  45. # Note that perl's flock may be implemented using fcntl
  46. # or lockf on some systems. If so, and if there is no
  47. # interop between the locking systems, the true C flock will
  48. # always succeed, and this optimisation won't work.
  49. # The perl code will later correctly check the lock,
  50. # so the right thing will still happen, though without
  51. # the benefit of this optimisation.
  52. $check_commit_hook=<<"EOF";
  53. {
  54. int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR);
  55. if (fd != -1) {
  56. if (flock(fd, LOCK_SH | LOCK_NB) != 0)
  57. exit(0);
  58. close(fd);
  59. }
  60. }
  61. EOF
  62. }
  63. $Data::Dumper::Indent=0; # no newlines
  64. my $configstring=Data::Dumper->Dump([\%config], ['*config']);
  65. $configstring=~s/\\/\\\\/g;
  66. $configstring=~s/"/\\"/g;
  67. $configstring=~s/\n/\\n/g;
  68. #translators: The first parameter is a filename, and the second is
  69. #translators: a (probably not translated) error message.
  70. open(OUT, ">$wrapper.c") || error(sprintf(gettext("failed to write %s: %s"), "$wrapper.c", $!));;
  71. print OUT <<"EOF";
  72. /* A wrapper for ikiwiki, can be safely made suid. */
  73. #include <stdio.h>
  74. #include <sys/types.h>
  75. #include <sys/stat.h>
  76. #include <fcntl.h>
  77. #include <unistd.h>
  78. #include <stdlib.h>
  79. #include <string.h>
  80. #include <sys/file.h>
  81. extern char **environ;
  82. char *newenviron[$#envsave+6];
  83. int i=0;
  84. addenv(char *var, char *val) {
  85. char *s=malloc(strlen(var)+1+strlen(val)+1);
  86. if (!s)
  87. perror("malloc");
  88. sprintf(s, "%s=%s", var, val);
  89. newenviron[i++]=s;
  90. }
  91. int main (int argc, char **argv) {
  92. char *s;
  93. $check_commit_hook
  94. $test_receive
  95. $envsave
  96. newenviron[i++]="HOME=$ENV{HOME}";
  97. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  98. newenviron[i]=NULL;
  99. environ=newenviron;
  100. if (setregid(getegid(), -1) != 0 &&
  101. setregid(getegid(), -1) != 0) {
  102. perror("failed to drop real gid");
  103. exit(1);
  104. }
  105. if (setreuid(geteuid(), -1) != 0 &&
  106. setreuid(geteuid(), -1) != 0) {
  107. perror("failed to drop real uid");
  108. exit(1);
  109. }
  110. execl("$this", "$this", NULL);
  111. perror("exec $this");
  112. exit(1);
  113. }
  114. EOF
  115. close OUT;
  116. my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
  117. if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
  118. #translators: The parameter is a C filename.
  119. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  120. }
  121. unlink("$wrapper.c");
  122. if (defined $config{wrappergroup}) {
  123. my $gid=(getgrnam($config{wrappergroup}))[2];
  124. if (! defined $gid) {
  125. error(sprintf("bad wrappergroup"));
  126. }
  127. if (! chown(-1, $gid, "$wrapper.new")) {
  128. error("chown $wrapper.new: $!");
  129. }
  130. }
  131. if (defined $config{wrappermode} &&
  132. ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
  133. error("chmod $wrapper.new: $!");
  134. }
  135. if (! rename("$wrapper.new", $wrapper)) {
  136. error("rename $wrapper.new $wrapper: $!");
  137. }
  138. #translators: The parameter is a filename.
  139. printf(gettext("successfully generated %s"), $wrapper);
  140. print "\n";
  141. } #}}}
  142. 1