summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: f175b4a0ba168242ebb8396d6608cdb001cb0b34 (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 @wrapper_hooks;
  36. run_hooks(genwrapper => sub { push @wrapper_hooks, shift->() });
  37. my $check_commit_hook="";
  38. my $pre_exec="";
  39. if ($config{post_commit}) {
  40. # Optimise checking !commit_hook_enabled() ,
  41. # so that ikiwiki does not have to be started if the
  42. # hook is disabled.
  43. #
  44. # Note that perl's flock may be implemented using fcntl
  45. # or lockf on some systems. If so, and if there is no
  46. # interop between the locking systems, the true C flock will
  47. # always succeed, and this optimisation won't work.
  48. # The perl code will later correctly check the lock,
  49. # so the right thing will still happen, though without
  50. # the benefit of this optimisation.
  51. $check_commit_hook=<<"EOF";
  52. {
  53. int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
  54. if (fd != -1) {
  55. if (flock(fd, LOCK_SH | LOCK_NB) != 0)
  56. exit(0);
  57. close(fd);
  58. }
  59. }
  60. EOF
  61. }
  62. elsif ($config{cgi}) {
  63. # Avoid more than one ikiwiki cgi running at a time by
  64. # taking a cgi lock. Since ikiwiki uses several MB of
  65. # memory, a pile up of processes could cause thrashing
  66. # otherwise. The fd of the lock is stored in
  67. # IKIWIKI_CGILOCK_FD so unlockwiki can close it.
  68. $pre_exec=<<"EOF";
  69. {
  70. int fd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
  71. if (fd != -1 && flock(fd, LOCK_EX) == 0) {
  72. char *fd_s=malloc(8);
  73. sprintf(fd_s, "%i", fd);
  74. setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
  75. }
  76. }
  77. EOF
  78. }
  79. $Data::Dumper::Indent=0; # no newlines
  80. my $configstring=Data::Dumper->Dump([\%config], ['*config']);
  81. $configstring=~s/\\/\\\\/g;
  82. $configstring=~s/"/\\"/g;
  83. $configstring=~s/\n/\\n/g;
  84. writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
  85. /* A wrapper for ikiwiki, can be safely made suid. */
  86. #include <stdio.h>
  87. #include <sys/types.h>
  88. #include <sys/stat.h>
  89. #include <fcntl.h>
  90. #include <unistd.h>
  91. #include <stdlib.h>
  92. #include <string.h>
  93. #include <sys/file.h>
  94. char *newenviron[$#envsave+6];
  95. int i=0;
  96. void addenv(char *var, char *val) {
  97. char *s=malloc(strlen(var)+1+strlen(val)+1);
  98. if (!s)
  99. perror("malloc");
  100. sprintf(s, "%s=%s", var, val);
  101. newenviron[i++]=s;
  102. }
  103. int main (int argc, char **argv) {
  104. char *s;
  105. $check_commit_hook
  106. @wrapper_hooks
  107. $envsave
  108. newenviron[i++]="HOME=$ENV{HOME}";
  109. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  110. if (clearenv() != 0) {
  111. perror("clearenv");
  112. exit(1);
  113. }
  114. for (; i>0; i--)
  115. putenv(newenviron[i-1]);
  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. push @cc, possibly_foolish_untaint($ENV{CFLAGS}) if exists $ENV{CFLAGS};
  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