summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: cf85738d68163763e8ac2bb1c7fc2ab04ef69187 (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. if ($config{test_receive}) {
  36. require IkiWiki::Receive;
  37. }
  38. my @wrapper_hooks;
  39. run_hooks(genwrapper => sub { push @wrapper_hooks, shift->() });
  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. @wrapper_hooks
  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. my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
  133. if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
  134. #translators: The parameter is a C filename.
  135. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  136. }
  137. unlink("$wrapper.c");
  138. if (defined $config{wrappergroup}) {
  139. my $gid=(getgrnam($config{wrappergroup}))[2];
  140. if (! defined $gid) {
  141. error(sprintf("bad wrappergroup"));
  142. }
  143. if (! chown(-1, $gid, "$wrapper.new")) {
  144. error("chown $wrapper.new: $!");
  145. }
  146. }
  147. if (defined $config{wrappermode} &&
  148. ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
  149. error("chmod $wrapper.new: $!");
  150. }
  151. if (! rename("$wrapper.new", $wrapper)) {
  152. error("rename $wrapper.new $wrapper: $!");
  153. }
  154. #translators: The parameter is a filename.
  155. printf(gettext("successfully generated %s"), $wrapper);
  156. print "\n";
  157. }
  158. 1