summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: 5eb96f4ae0d168598fe9d6a54a644294fbcb517a (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+7];
  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++]="PATH=$ENV{PATH}";
  136. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  137. #ifdef __TINYC__
  138. /* old tcc versions do not support modifying environ directly */
  139. if (clearenv() != 0) {
  140. perror("clearenv");
  141. exit(1);
  142. }
  143. for (; i>0; i--)
  144. putenv(newenviron[i-1]);
  145. #else
  146. newenviron[i]=NULL;
  147. environ=newenviron;
  148. #endif
  149. if (setregid(getegid(), -1) != 0 &&
  150. setregid(getegid(), -1) != 0) {
  151. perror("failed to drop real gid");
  152. exit(1);
  153. }
  154. if (setreuid(geteuid(), -1) != 0 &&
  155. setreuid(geteuid(), -1) != 0) {
  156. perror("failed to drop real uid");
  157. exit(1);
  158. }
  159. $pre_exec
  160. $set_background_command
  161. #ifdef BACKGROUND_COMMAND
  162. if (lockfd != -1) {
  163. close(lockfd);
  164. }
  165. pid_t pid=fork();
  166. if (pid == -1) {
  167. perror("fork");
  168. exit(1);
  169. }
  170. else if (pid == 0) {
  171. execl("$this", "$this", NULL);
  172. perror("exec $this");
  173. exit(1);
  174. }
  175. else {
  176. waitpid(pid, NULL, 0);
  177. if (daemon(1, 0) == 0) {
  178. system(BACKGROUND_COMMAND);
  179. exit(0);
  180. }
  181. else {
  182. perror("daemon");
  183. exit(1);
  184. }
  185. }
  186. #else
  187. execl("$this", "$this", NULL);
  188. perror("exec $this");
  189. exit(1);
  190. #endif
  191. }
  192. EOF
  193. my @cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
  194. push @cc, possibly_foolish_untaint($ENV{CFLAGS}) if exists $ENV{CFLAGS};
  195. if (system(@cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
  196. #translators: The parameter is a C filename.
  197. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  198. }
  199. unlink("$wrapper.c");
  200. if (defined $config{wrappergroup}) {
  201. my $gid=(getgrnam($config{wrappergroup}))[2];
  202. if (! defined $gid) {
  203. error(sprintf("bad wrappergroup"));
  204. }
  205. if (! chown(-1, $gid, "$wrapper.new")) {
  206. error("chown $wrapper.new: $!");
  207. }
  208. }
  209. if (defined $config{wrappermode} &&
  210. ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
  211. error("chmod $wrapper.new: $!");
  212. }
  213. if (! rename("$wrapper.new", $wrapper)) {
  214. error("rename $wrapper.new $wrapper: $!");
  215. }
  216. #translators: The parameter is a filename.
  217. printf(gettext("successfully generated %s"), $wrapper);
  218. print "\n";
  219. }
  220. 1