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