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