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