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