summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: 6555fe625f8e542e786f3b6d7d05a72772ea207f (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. writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
  88. /* A wrapper for ikiwiki, can be safely made suid. */
  89. #include <stdio.h>
  90. #include <sys/types.h>
  91. #include <sys/stat.h>
  92. #include <fcntl.h>
  93. #include <unistd.h>
  94. #include <stdlib.h>
  95. #include <string.h>
  96. #include <sys/file.h>
  97. extern char **environ;
  98. char *newenviron[$#envsave+6];
  99. int i=0;
  100. addenv(char *var, char *val) {
  101. char *s=malloc(strlen(var)+1+strlen(val)+1);
  102. if (!s)
  103. perror("malloc");
  104. sprintf(s, "%s=%s", var, val);
  105. newenviron[i++]=s;
  106. }
  107. int main (int argc, char **argv) {
  108. char *s;
  109. $check_commit_hook
  110. $test_receive
  111. $envsave
  112. newenviron[i++]="HOME=$ENV{HOME}";
  113. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  114. newenviron[i]=NULL;
  115. environ=newenviron;
  116. if (setregid(getegid(), -1) != 0 &&
  117. setregid(getegid(), -1) != 0) {
  118. perror("failed to drop real gid");
  119. exit(1);
  120. }
  121. if (setreuid(geteuid(), -1) != 0 &&
  122. setreuid(geteuid(), -1) != 0) {
  123. perror("failed to drop real uid");
  124. exit(1);
  125. }
  126. $pre_exec
  127. execl("$this", "$this", NULL);
  128. perror("exec $this");
  129. exit(1);
  130. }
  131. EOF
  132. close OUT;
  133. my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
  134. if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
  135. #translators: The parameter is a C filename.
  136. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  137. }
  138. unlink("$wrapper.c");
  139. if (defined $config{wrappergroup}) {
  140. my $gid=(getgrnam($config{wrappergroup}))[2];
  141. if (! defined $gid) {
  142. error(sprintf("bad wrappergroup"));
  143. }
  144. if (! chown(-1, $gid, "$wrapper.new")) {
  145. error("chown $wrapper.new: $!");
  146. }
  147. }
  148. if (defined $config{wrappermode} &&
  149. ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
  150. error("chmod $wrapper.new: $!");
  151. }
  152. if (! rename("$wrapper.new", $wrapper)) {
  153. error("rename $wrapper.new $wrapper: $!");
  154. }
  155. #translators: The parameter is a filename.
  156. printf(gettext("successfully generated %s"), $wrapper);
  157. print "\n";
  158. }
  159. 1