summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: e761085fb8d54cad74fdd41fe29071e93cac5c0d (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. open(OUT, ">$wrapper.c") || error(sprintf(gettext("failed to write %s: %s"), "$wrapper.c", $!));;
  56. print OUT <<"EOF";
  57. /* A wrapper for ikiwiki, can be safely made suid. */
  58. #define _GNU_SOURCE
  59. #include <stdio.h>
  60. #include <sys/types.h>
  61. #include <unistd.h>
  62. #include <stdlib.h>
  63. #include <string.h>
  64. extern char **environ;
  65. int main (int argc, char **argv) {
  66. /* Sanitize environment. */
  67. char *s;
  68. char *newenviron[$#envsave+5];
  69. int i=0;
  70. $envsave
  71. newenviron[i++]="HOME=$ENV{HOME}";
  72. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  73. newenviron[i]=NULL;
  74. environ=newenviron;
  75. if (setregid(getegid(), -1) != 0 || setreuid(geteuid(), -1) != 0) {
  76. perror("failed to drop real uid/gid");
  77. exit(1);
  78. }
  79. execl("$this", "$this", NULL);
  80. perror("failed to run $this");
  81. exit(1);
  82. }
  83. EOF
  84. close OUT;
  85. if (system("gcc", "$wrapper.c", "-o", $wrapper) != 0) {
  86. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  87. }
  88. unlink("$wrapper.c");
  89. if (defined $config{wrappermode} &&
  90. ! chmod(oct($config{wrappermode}), $wrapper)) {
  91. error("chmod $wrapper: $!");
  92. }
  93. printf(gettext("successfully generated %s\n"), $wrapper);
  94. } #}}}
  95. 1