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