summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: bd134c9a35c2fa87eef5a505e43984bdc26cc1d5 (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. lockfd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
  70. if (lockfd != -1 && flock(lockfd, LOCK_EX) == 0) {
  71. char *fd_s=malloc(8);
  72. sprintf(fd_s, "%i", lockfd);
  73. setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
  74. }
  75. EOF
  76. }
  77. my $set_background_command='';
  78. if (defined $config{wrapper_background_command} &&
  79. length $config{wrapper_background_command}) {
  80. my $background_command=delete $config{wrapper_background_command};
  81. $set_background_command=~s/"/\\"/g;
  82. $set_background_command='#define BACKGROUND_COMMAND "'.$background_command.'"';
  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. void 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 main (int argc, char **argv) {
  110. int lockfd=-1;
  111. char *s;
  112. $check_commit_hook
  113. @wrapper_hooks
  114. $envsave
  115. newenviron[i++]="HOME=$ENV{HOME}";
  116. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  117. #ifdef __TINYC__
  118. /* old tcc versions do not support modifying environ directly */
  119. if (clearenv() != 0) {
  120. perror("clearenv");
  121. exit(1);
  122. }
  123. for (; i>0; i--)
  124. putenv(newenviron[i-1]);
  125. #else
  126. newenviron[i]=NULL;
  127. environ=newenviron;
  128. #endif
  129. if (setregid(getegid(), -1) != 0 &&
  130. setregid(getegid(), -1) != 0) {
  131. perror("failed to drop real gid");
  132. exit(1);
  133. }
  134. if (setreuid(geteuid(), -1) != 0 &&
  135. setreuid(geteuid(), -1) != 0) {
  136. perror("failed to drop real uid");
  137. exit(1);
  138. }
  139. $pre_exec
  140. $set_background_command
  141. #ifdef BACKGROUND_COMMAND
  142. if (lockfd != -1) {
  143. close(lockfd);
  144. }
  145. pid_t pid=fork();
  146. if (pid == -1) {
  147. perror("fork");
  148. exit(1);
  149. }
  150. else if (pid == 0) {
  151. execl("$this", "$this", NULL);
  152. perror("exec $this");
  153. exit(1);
  154. }
  155. else {
  156. waitpid(pid, NULL, 0);
  157. if (daemon(1, 0) == 0) {
  158. system(BACKGROUND_COMMAND);
  159. exit(0);
  160. }
  161. else {
  162. perror("daemon");
  163. exit(1);
  164. }
  165. }
  166. #else
  167. execl("$this", "$this", NULL);
  168. perror("exec $this");
  169. exit(1);
  170. #endif
  171. }
  172. EOF
  173. my @cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
  174. push @cc, possibly_foolish_untaint($ENV{CFLAGS}) if exists $ENV{CFLAGS};
  175. if (system(@cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
  176. #translators: The parameter is a C filename.
  177. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  178. }
  179. unlink("$wrapper.c");
  180. if (defined $config{wrappergroup}) {
  181. my $gid=(getgrnam($config{wrappergroup}))[2];
  182. if (! defined $gid) {
  183. error(sprintf("bad wrappergroup"));
  184. }
  185. if (! chown(-1, $gid, "$wrapper.new")) {
  186. error("chown $wrapper.new: $!");
  187. }
  188. }
  189. if (defined $config{wrappermode} &&
  190. ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
  191. error("chmod $wrapper.new: $!");
  192. }
  193. if (! rename("$wrapper.new", $wrapper)) {
  194. error("rename $wrapper.new $wrapper: $!");
  195. }
  196. #translators: The parameter is a filename.
  197. printf(gettext("successfully generated %s"), $wrapper);
  198. print "\n";
  199. }
  200. 1