summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: 7d27abd06a6e742b66349115ef4107136f36e11f (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Cwd q{abs_path};
  5. use Data::Dumper;
  6. use IkiWiki;
  7. package IkiWiki;
  8. sub gen_wrapper () { #{{{
  9. $config{srcdir}=abs_path($config{srcdir});
  10. $config{destdir}=abs_path($config{destdir});
  11. my $this=abs_path($0);
  12. if (! -x $this) {
  13. error("$this doesn't seem to be executable");
  14. }
  15. if ($config{setup}) {
  16. error("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("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} if $config{cgi};
  27. my $envsave="";
  28. foreach my $var (@envsave) {
  29. $envsave.=<<"EOF"
  30. if ((s=getenv("$var")))
  31. asprintf(&newenviron[i++], "%s=%s", "$var", s);
  32. EOF
  33. }
  34. if ($config{rcs} eq "svn" && $config{notify}) {
  35. # Support running directly as hooks/post-commit by passing
  36. # $2 in REV in the environment.
  37. $envsave.=<<"EOF"
  38. if (argc == 3)
  39. asprintf(&newenviron[i++], "REV=%s", argv[2]);
  40. else if ((s=getenv("REV")))
  41. asprintf(&newenviron[i++], "%s=%s", "REV", s);
  42. EOF
  43. }
  44. # This is only set by plugins, which append to it on startup, so
  45. # avoid storing it in the wrapper.
  46. $config{headercontent}="";
  47. $Data::Dumper::Indent=0; # no newlines
  48. my $configstring=Data::Dumper->Dump([\%config], ['*config']);
  49. $configstring=~s/\\/\\\\/g;
  50. $configstring=~s/"/\\"/g;
  51. $configstring=~s/\n/\\\n/g;
  52. open(OUT, ">$wrapper.c") || error("failed to write $wrapper.c: $!");;
  53. print OUT <<"EOF";
  54. /* A wrapper for ikiwiki, can be safely made suid. */
  55. #define _GNU_SOURCE
  56. #include <stdio.h>
  57. #include <unistd.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. extern char **environ;
  61. int main (int argc, char **argv) {
  62. /* Sanitize environment. */
  63. char *s;
  64. char *newenviron[$#envsave+5];
  65. int i=0;
  66. $envsave
  67. newenviron[i++]="HOME=$ENV{HOME}";
  68. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  69. newenviron[i]=NULL;
  70. environ=newenviron;
  71. execl("$this", "$this", NULL);
  72. perror("failed to run $this");
  73. exit(1);
  74. }
  75. EOF
  76. close OUT;
  77. if (system("gcc", "$wrapper.c", "-o", $wrapper) != 0) {
  78. error("failed to compile $wrapper.c");
  79. }
  80. unlink("$wrapper.c");
  81. if (defined $config{wrappermode} &&
  82. ! chmod(oct($config{wrappermode}), $wrapper)) {
  83. error("chmod $wrapper: $!");
  84. }
  85. print "successfully generated $wrapper\n";
  86. } #}}}
  87. 1