summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: 0baf112ffd722c3ca133c92be2dd5982e01801ab (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_args=" return 0;";
  41. run_hooks(wrapperargcheck => sub { $check_args = shift->(); });
  42. my $check_commit_hook="";
  43. my $pre_exec="";
  44. if ($config{post_commit}) {
  45. # Optimise checking !commit_hook_enabled() ,
  46. # so that ikiwiki does not have to be started if the
  47. # hook is disabled.
  48. #
  49. # Note that perl's flock may be implemented using fcntl
  50. # or lockf on some systems. If so, and if there is no
  51. # interop between the locking systems, the true C flock will
  52. # always succeed, and this optimisation won't work.
  53. # The perl code will later correctly check the lock,
  54. # so the right thing will still happen, though without
  55. # the benefit of this optimisation.
  56. $check_commit_hook=<<"EOF";
  57. {
  58. int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
  59. if (fd != -1) {
  60. if (flock(fd, LOCK_SH | LOCK_NB) != 0)
  61. exit(0);
  62. close(fd);
  63. }
  64. }
  65. EOF
  66. }
  67. elsif ($config{cgi}) {
  68. # Avoid more than one ikiwiki cgi running at a time by
  69. # taking a cgi lock. Since ikiwiki uses several MB of
  70. # memory, a pile up of processes could cause thrashing
  71. # otherwise. The fd of the lock is stored in
  72. # IKIWIKI_CGILOCK_FD so unlockwiki can close it.
  73. $pre_exec=<<"EOF";
  74. {
  75. int fd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
  76. if (fd != -1 && flock(fd, LOCK_EX) == 0) {
  77. char *fd_s;
  78. asprintf(&fd_s, "%i", fd);
  79. setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
  80. }
  81. }
  82. EOF
  83. }
  84. $Data::Dumper::Indent=0; # no newlines
  85. my $configstring=Data::Dumper->Dump([\%config], ['*config']);
  86. $configstring=~s/\\/\\\\/g;
  87. $configstring=~s/"/\\"/g;
  88. $configstring=~s/\n/\\n/g;
  89. writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
  90. /* A wrapper for ikiwiki, can be safely made suid. */
  91. #include <stdio.h>
  92. #include <sys/types.h>
  93. #include <sys/stat.h>
  94. #include <fcntl.h>
  95. #include <unistd.h>
  96. #include <stdlib.h>
  97. #include <string.h>
  98. #include <sys/file.h>
  99. extern char **environ;
  100. char *newenviron[$#envsave+6];
  101. int i=0;
  102. addenv(char *var, char *val) {
  103. char *s=malloc(strlen(var)+1+strlen(val)+1);
  104. if (!s)
  105. perror("malloc");
  106. sprintf(s, "%s=%s", var, val);
  107. newenviron[i++]=s;
  108. }
  109. int checkargs(int argc, char **argv) {
  110. $check_args
  111. }
  112. int main (int argc, char **argv) {
  113. char *s;
  114. if (!checkargs(argc, argv))
  115. exit(0);
  116. $check_commit_hook
  117. $test_receive
  118. $envsave
  119. newenviron[i++]="HOME=$ENV{HOME}";
  120. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  121. newenviron[i]=NULL;
  122. environ=newenviron;
  123. if (setregid(getegid(), -1) != 0 &&
  124. setregid(getegid(), -1) != 0) {
  125. perror("failed to drop real gid");
  126. exit(1);
  127. }
  128. if (setreuid(geteuid(), -1) != 0 &&
  129. setreuid(geteuid(), -1) != 0) {
  130. perror("failed to drop real uid");
  131. exit(1);
  132. }
  133. $pre_exec
  134. execl("$this", "$this", NULL);
  135. perror("exec $this");
  136. exit(1);
  137. }
  138. EOF
  139. close OUT;
  140. my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
  141. if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
  142. #translators: The parameter is a C filename.
  143. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  144. }
  145. unlink("$wrapper.c");
  146. if (defined $config{wrappergroup}) {
  147. my $gid=(getgrnam($config{wrappergroup}))[2];
  148. if (! defined $gid) {
  149. error(sprintf("bad wrappergroup"));
  150. }
  151. if (! chown(-1, $gid, "$wrapper.new")) {
  152. error("chown $wrapper.new: $!");
  153. }
  154. }
  155. if (defined $config{wrappermode} &&
  156. ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
  157. error("chmod $wrapper.new: $!");
  158. }
  159. if (! rename("$wrapper.new", $wrapper)) {
  160. error("rename $wrapper.new $wrapper: $!");
  161. }
  162. #translators: The parameter is a filename.
  163. printf(gettext("successfully generated %s"), $wrapper);
  164. print "\n";
  165. }
  166. 1