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