summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: 927368fae964d678ab04aa866154b61677f80176 (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_wrappers () {
  9. debug(gettext("generating wrappers.."));
  10. my %origconfig=(%config);
  11. foreach my $wrapper (@{$config{wrappers}}) {
  12. %config=(%origconfig, %{$wrapper});
  13. $config{verbose}=$config{setupverbose}
  14. if exists $config{setupverbose};
  15. $config{syslog}=$config{setupsyslog}
  16. if exists $config{setupsyslog};
  17. delete @config{qw(setupsyslog setupverbose wrappers genwrappers rebuild)};
  18. checkconfig();
  19. if (! $config{cgi} && ! $config{post_commit} &&
  20. ! $config{test_receive}) {
  21. $config{post_commit}=1;
  22. }
  23. gen_wrapper();
  24. }
  25. %config=(%origconfig);
  26. }
  27. sub gen_wrapper () {
  28. $config{srcdir}=File::Spec->rel2abs($config{srcdir});
  29. $config{destdir}=File::Spec->rel2abs($config{destdir});
  30. my $this=File::Spec->rel2abs($0);
  31. if (! -x $this) {
  32. error(sprintf(gettext("%s doesn't seem to be executable"), $this));
  33. }
  34. if ($config{setup}) {
  35. error(gettext("cannot create a wrapper that uses a setup file"));
  36. }
  37. my $wrapper=possibly_foolish_untaint($config{wrapper});
  38. if (! defined $wrapper || ! length $wrapper) {
  39. error(gettext("wrapper filename not specified"));
  40. }
  41. delete $config{wrapper};
  42. my @envsave;
  43. push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
  44. CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
  45. HTTP_COOKIE REMOTE_USER HTTPS REDIRECT_STATUS
  46. REDIRECT_URL} if $config{cgi};
  47. my $envsave="";
  48. foreach my $var (@envsave) {
  49. $envsave.=<<"EOF";
  50. if ((s=getenv("$var")))
  51. addenv("$var", s);
  52. EOF
  53. }
  54. my @wrapper_hooks;
  55. run_hooks(genwrapper => sub { push @wrapper_hooks, shift->() });
  56. my $check_commit_hook="";
  57. my $pre_exec="";
  58. if ($config{post_commit}) {
  59. # Optimise checking !commit_hook_enabled() ,
  60. # so that ikiwiki does not have to be started if the
  61. # hook is disabled.
  62. #
  63. # Note that perl's flock may be implemented using fcntl
  64. # or lockf on some systems. If so, and if there is no
  65. # interop between the locking systems, the true C flock will
  66. # always succeed, and this optimisation won't work.
  67. # The perl code will later correctly check the lock,
  68. # so the right thing will still happen, though without
  69. # the benefit of this optimisation.
  70. $check_commit_hook=<<"EOF";
  71. {
  72. int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
  73. if (fd != -1) {
  74. if (flock(fd, LOCK_SH | LOCK_NB) != 0)
  75. exit(0);
  76. close(fd);
  77. }
  78. }
  79. EOF
  80. }
  81. elsif ($config{cgi}) {
  82. # Avoid more than one ikiwiki cgi running at a time by
  83. # taking a cgi lock. Since ikiwiki uses several MB of
  84. # memory, a pile up of processes could cause thrashing
  85. # otherwise. The fd of the lock is stored in
  86. # IKIWIKI_CGILOCK_FD so unlockwiki can close it.
  87. $pre_exec=<<"EOF";
  88. lockfd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
  89. if (lockfd != -1 && flock(lockfd, LOCK_EX) == 0) {
  90. char *fd_s=malloc(8);
  91. sprintf(fd_s, "%i", lockfd);
  92. setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
  93. }
  94. EOF
  95. }
  96. my $set_background_command='';
  97. if (defined $config{wrapper_background_command} &&
  98. length $config{wrapper_background_command}) {
  99. my $background_command=delete $config{wrapper_background_command};
  100. $set_background_command=~s/"/\\"/g;
  101. $set_background_command='#define BACKGROUND_COMMAND "'.$background_command.'"';
  102. }
  103. $Data::Dumper::Indent=0; # no newlines
  104. my $configstring=Data::Dumper->Dump([\%config], ['*config']);
  105. $configstring=~s/\\/\\\\/g;
  106. $configstring=~s/"/\\"/g;
  107. $configstring=~s/\n/\\n/g;
  108. writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
  109. /* A wrapper for ikiwiki, can be safely made suid. */
  110. #include <stdio.h>
  111. #include <sys/types.h>
  112. #include <sys/stat.h>
  113. #include <fcntl.h>
  114. #include <unistd.h>
  115. #include <stdlib.h>
  116. #include <string.h>
  117. #include <sys/file.h>
  118. extern char **environ;
  119. char *newenviron[$#envsave+6];
  120. int i=0;
  121. void addenv(char *var, char *val) {
  122. char *s=malloc(strlen(var)+1+strlen(val)+1);
  123. if (!s)
  124. perror("malloc");
  125. sprintf(s, "%s=%s", var, val);
  126. newenviron[i++]=s;
  127. }
  128. int main (int argc, char **argv) {
  129. int lockfd=-1;
  130. char *s;
  131. $check_commit_hook
  132. @wrapper_hooks
  133. $envsave
  134. newenviron[i++]="HOME=$ENV{HOME}";
  135. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  136. #ifdef __TINYC__
  137. /* old tcc versions do not support modifying environ directly */
  138. if (clearenv() != 0) {
  139. perror("clearenv");
  140. exit(1);
  141. }
  142. for (; i>0; i--)
  143. putenv(newenviron[i-1]);
  144. #else
  145. newenviron[i]=NULL;
  146. environ=newenviron;
  147. #endif
  148. if (setregid(getegid(), -1) != 0 &&
  149. setregid(getegid(), -1) != 0) {
  150. perror("failed to drop real gid");
  151. exit(1);
  152. }
  153. if (setreuid(geteuid(), -1) != 0 &&
  154. setreuid(geteuid(), -1) != 0) {
  155. perror("failed to drop real uid");
  156. exit(1);
  157. }
  158. $pre_exec
  159. $set_background_command
  160. #ifdef BACKGROUND_COMMAND
  161. if (lockfd != -1) {
  162. close(lockfd);
  163. }
  164. pid_t pid=fork();
  165. if (pid == -1) {
  166. perror("fork");
  167. exit(1);
  168. }
  169. else if (pid == 0) {
  170. execl("$this", "$this", NULL);
  171. perror("exec $this");
  172. exit(1);
  173. }
  174. else {
  175. waitpid(pid, NULL, 0);
  176. if (daemon(1, 0) == 0) {
  177. system(BACKGROUND_COMMAND);
  178. exit(0);
  179. }
  180. else {
  181. perror("daemon");
  182. exit(1);
  183. }
  184. }
  185. #else
  186. execl("$this", "$this", NULL);
  187. perror("exec $this");
  188. exit(1);
  189. #endif
  190. }
  191. EOF
  192. my @cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
  193. push @cc, possibly_foolish_untaint($ENV{CFLAGS}) if exists $ENV{CFLAGS};
  194. if (system(@cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
  195. #translators: The parameter is a C filename.
  196. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  197. }
  198. unlink("$wrapper.c");
  199. if (defined $config{wrappergroup}) {
  200. my $gid=(getgrnam($config{wrappergroup}))[2];
  201. if (! defined $gid) {
  202. error(sprintf("bad wrappergroup"));
  203. }
  204. if (! chown(-1, $gid, "$wrapper.new")) {
  205. error("chown $wrapper.new: $!");
  206. }
  207. }
  208. if (defined $config{wrappermode} &&
  209. ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
  210. error("chmod $wrapper.new: $!");
  211. }
  212. if (! rename("$wrapper.new", $wrapper)) {
  213. error("rename $wrapper.new $wrapper: $!");
  214. }
  215. #translators: The parameter is a filename.
  216. printf(gettext("successfully generated %s"), $wrapper);
  217. print "\n";
  218. }
  219. 1