summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: d723684463dda687e21be6d18341ce0c6ac0d1d5 (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Cwd q{abs_path};
  5. use Data::Dumper;
  6. package IkiWiki;
  7. sub gen_wrapper () { #{{{
  8. $config{srcdir}=abs_path($config{srcdir});
  9. $config{destdir}=abs_path($config{destdir});
  10. my $this=abs_path($0);
  11. if (! -x $this) {
  12. error("$this doesn't seem to be executable");
  13. }
  14. if ($config{setup}) {
  15. error("cannot create a wrapper that uses a setup file");
  16. }
  17. my $wrapper=possibly_foolish_untaint($config{wrapper});
  18. if (! defined $wrapper || ! length $wrapper) {
  19. error("wrapper filename not specified");
  20. }
  21. delete $config{wrapper};
  22. my @envsave;
  23. push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
  24. CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
  25. HTTP_COOKIE} if $config{cgi};
  26. my $envsave="";
  27. foreach my $var (@envsave) {
  28. $envsave.=<<"EOF"
  29. if ((s=getenv("$var")))
  30. asprintf(&newenviron[i++], "%s=%s", "$var", s);
  31. EOF
  32. }
  33. if ($config{svn} && $config{notify}) {
  34. # Support running directly as hooks/post-commit by passing
  35. # $2 in REV in the environment.
  36. $envsave.=<<"EOF"
  37. if (argc == 3)
  38. asprintf(&newenviron[i++], "REV=%s", argv[2]);
  39. else if ((s=getenv("REV")))
  40. asprintf(&newenviron[i++], "%s=%s", "REV", s);
  41. EOF
  42. }
  43. $Data::Dumper::Indent=0; # no newlines
  44. my $configstring=Data::Dumper->Dump([\%config], ['*config']);
  45. $configstring=~s/\\/\\\\/g;
  46. $configstring=~s/"/\\"/g;
  47. open(OUT, ">$wrapper.c") || error("failed to write $wrapper.c: $!");;
  48. print OUT <<"EOF";
  49. /* A wrapper for ikiwiki, can be safely made suid. */
  50. #define _GNU_SOURCE
  51. #include <stdio.h>
  52. #include <unistd.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. extern char **environ;
  56. int main (int argc, char **argv) {
  57. /* Sanitize environment. */
  58. char *s;
  59. char *newenviron[$#envsave+5];
  60. int i=0;
  61. $envsave
  62. newenviron[i++]="HOME=$ENV{HOME}";
  63. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  64. newenviron[i]=NULL;
  65. environ=newenviron;
  66. execl("$this", "$this", NULL);
  67. perror("failed to run $this");
  68. exit(1);
  69. }
  70. EOF
  71. close OUT;
  72. if (system("gcc", "$wrapper.c", "-o", $wrapper) != 0) {
  73. error("failed to compile $wrapper.c");
  74. }
  75. unlink("$wrapper.c");
  76. if (defined $config{wrappermode} &&
  77. ! chmod(oct($config{wrappermode}), $wrapper)) {
  78. error("chmod $wrapper: $!");
  79. }
  80. print "successfully generated $wrapper\n";
  81. } #}}}
  82. 1