summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: ee547cefb9e7945ea12015a3197a2a79737e3fa7 (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. $Data::Dumper::Indent=0; # no newlines
  45. my $configstring=Data::Dumper->Dump([\%config], ['*config']);
  46. $configstring=~s/\\/\\\\/g;
  47. $configstring=~s/"/\\"/g;
  48. open(OUT, ">$wrapper.c") || error("failed to write $wrapper.c: $!");;
  49. print OUT <<"EOF";
  50. /* A wrapper for ikiwiki, can be safely made suid. */
  51. #define _GNU_SOURCE
  52. #include <stdio.h>
  53. #include <unistd.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. extern char **environ;
  57. int main (int argc, char **argv) {
  58. /* Sanitize environment. */
  59. char *s;
  60. char *newenviron[$#envsave+5];
  61. int i=0;
  62. $envsave
  63. newenviron[i++]="HOME=$ENV{HOME}";
  64. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  65. newenviron[i]=NULL;
  66. environ=newenviron;
  67. execl("$this", "$this", NULL);
  68. perror("failed to run $this");
  69. exit(1);
  70. }
  71. EOF
  72. close OUT;
  73. if (system("gcc", "$wrapper.c", "-o", $wrapper) != 0) {
  74. error("failed to compile $wrapper.c");
  75. }
  76. unlink("$wrapper.c");
  77. if (defined $config{wrappermode} &&
  78. ! chmod(oct($config{wrappermode}), $wrapper)) {
  79. error("chmod $wrapper: $!");
  80. }
  81. print "successfully generated $wrapper\n";
  82. } #}}}
  83. 1