summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: a3ecccd5be131b3e745a8d79db869f84a5acf1a9 (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("$this doesn't seem to be executable");
  14. }
  15. if ($config{setup}) {
  16. error("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("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("failed to write $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("failed to compile $wrapper.c");
  87. }
  88. unlink("$wrapper.c");
  89. if (defined $config{wrappermode} &&
  90. ! chmod(oct($config{wrappermode}), $wrapper)) {
  91. error("chmod $wrapper: $!");
  92. }
  93. print "successfully generated $wrapper\n";
  94. } #}}}
  95. 1