summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: 9415d4a17646a47ef41228bf8bada9a3795d8b28 (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(sprintf(gettext("%s doesn't seem to be executable"), $this));
  14. }
  15. if ($config{setup}) {
  16. error(gettext("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(gettext("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 REMOTE_USER} 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. if ($config{rcs} eq "tla" && $config{notify}) {
  45. $envsave.=<<"EOF"
  46. if ((s=getenv("ARCH_VERSION")))
  47. asprintf(&newenviron[i++], "%s=%s", "ARCH_VERSION", s);
  48. EOF
  49. }
  50. $Data::Dumper::Indent=0; # no newlines
  51. my $configstring=Data::Dumper->Dump([\%config], ['*config']);
  52. $configstring=~s/\\/\\\\/g;
  53. $configstring=~s/"/\\"/g;
  54. $configstring=~s/\n/\\n/g;
  55. #translators: The first parameter is a filename, and the second is
  56. #translators: a (probably not translated) error message.
  57. open(OUT, ">$wrapper.c") || error(sprintf(gettext("failed to write %s: %s"), "$wrapper.c", $!));;
  58. print OUT <<"EOF";
  59. /* A wrapper for ikiwiki, can be safely made suid. */
  60. #define _GNU_SOURCE
  61. #include <stdio.h>
  62. #include <sys/types.h>
  63. #include <unistd.h>
  64. #include <stdlib.h>
  65. #include <string.h>
  66. extern char **environ;
  67. int main (int argc, char **argv) {
  68. /* Sanitize environment. */
  69. char *s;
  70. char *newenviron[$#envsave+5];
  71. int i=0;
  72. $envsave
  73. newenviron[i++]="HOME=$ENV{HOME}";
  74. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  75. newenviron[i]=NULL;
  76. environ=newenviron;
  77. if (setregid(getegid(), -1) != 0 || setreuid(geteuid(), -1) != 0) {
  78. perror("failed to drop real uid/gid");
  79. exit(1);
  80. }
  81. execl("$this", "$this", NULL);
  82. perror("failed to run $this");
  83. exit(1);
  84. }
  85. EOF
  86. close OUT;
  87. if (system("gcc", "$wrapper.c", "-o", $wrapper) != 0) {
  88. #translators: The parameter is a C filename.
  89. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  90. }
  91. unlink("$wrapper.c");
  92. if (defined $config{wrappermode} &&
  93. ! chmod(oct($config{wrappermode}), $wrapper)) {
  94. error("chmod $wrapper: $!");
  95. }
  96. #translators: The parameter is a filename.
  97. printf(gettext("successfully generated %s"), $wrapper);
  98. print "\n";
  99. } #}}}
  100. 1