summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: 85d2591175a3904c97f94bf95566b5dd7cf89336 (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. delete $config{wrapper};
  19. my @envsave;
  20. push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
  21. CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
  22. HTTP_COOKIE} if $config{cgi};
  23. my $envsave="";
  24. foreach my $var (@envsave) {
  25. $envsave.=<<"EOF"
  26. if ((s=getenv("$var")))
  27. asprintf(&newenviron[i++], "%s=%s", "$var", s);
  28. EOF
  29. }
  30. $Data::Dumper::Indent=0; # no newlines
  31. my $configstring=Data::Dumper->Dump([\%config], ['*config']);
  32. $configstring=~s/\\/\\\\/g;
  33. $configstring=~s/"/\\"/g;
  34. open(OUT, ">$wrapper.c") || error("failed to write $wrapper.c: $!");;
  35. print OUT <<"EOF";
  36. /* A wrapper for ikiwiki, can be safely made suid. */
  37. #define _GNU_SOURCE
  38. #include <stdio.h>
  39. #include <unistd.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. extern char **environ;
  43. int main (int argc, char **argv) {
  44. /* Sanitize environment. */
  45. char *s;
  46. char *newenviron[$#envsave+4];
  47. int i=0;
  48. $envsave
  49. newenviron[i++]="HOME=$ENV{HOME}";
  50. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  51. newenviron[i]=NULL;
  52. environ=newenviron;
  53. execl("$this", "$this", NULL);
  54. perror("failed to run $this");
  55. exit(1);
  56. }
  57. EOF
  58. close OUT;
  59. if (system("gcc", "$wrapper.c", "-o", $wrapper) != 0) {
  60. error("failed to compile $wrapper.c");
  61. }
  62. unlink("$wrapper.c");
  63. if (defined $config{wrappermode} &&
  64. ! chmod(oct($config{wrappermode}), $wrapper)) {
  65. error("chmod $wrapper: $!");
  66. }
  67. print "successfully generated $wrapper\n";
  68. } #}}}
  69. 1