summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: 6687cace221afc7cca17c35d99a85c3557fd9223 (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. addenv("$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. addenv("REV", argv[2]);
  40. else if ((s=getenv("REV")))
  41. addenv("REV", s);
  42. EOF
  43. }
  44. if ($config{rcs} eq "tla" && $config{notify}) {
  45. $envsave.=<<"EOF"
  46. if ((s=getenv("ARCH_VERSION")))
  47. addenv("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. #include <stdio.h>
  61. #include <sys/types.h>
  62. #include <unistd.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. extern char **environ;
  66. char *newenviron[$#envsave+5];
  67. int i=0;
  68. addenv(char *var, char *val) {
  69. char *s=malloc(strlen(var)+1+strlen(val)+1);
  70. if (!s)
  71. perror("malloc");
  72. sprintf(s, "%s=%s", var, val);
  73. newenviron[i++]=s;
  74. }
  75. int main (int argc, char **argv) {
  76. /* Sanitize environment. */
  77. char *s;
  78. $envsave
  79. newenviron[i++]="HOME=$ENV{HOME}";
  80. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  81. newenviron[i]=NULL;
  82. environ=newenviron;
  83. if (setregid(getegid(), -1) != 0 || setreuid(geteuid(), -1) != 0) {
  84. perror("failed to drop real uid/gid");
  85. exit(1);
  86. }
  87. execl("$this", "$this", NULL);
  88. perror("exec $this");
  89. exit(1);
  90. }
  91. EOF
  92. close OUT;
  93. if (system("gcc", "$wrapper.c", "-o", "$wrapper.new") != 0) {
  94. #translators: The parameter is a C filename.
  95. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  96. }
  97. unlink("$wrapper.c");
  98. if (defined $config{wrappergroup}) {
  99. my $gid=(getgrnam($config{wrappergroup}))[2];
  100. if (! defined $gid) {
  101. error(sprintf("bad wrappergroup"));
  102. }
  103. if (! chown(-1, $gid, "$wrapper.new")) {
  104. error("chown $wrapper.new: $!");
  105. }
  106. }
  107. if (defined $config{wrappermode} &&
  108. ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
  109. error("chmod $wrapper.new: $!");
  110. }
  111. if (! rename("$wrapper.new", $wrapper)) {
  112. error("rename $wrapper.new $wrapper: $!");
  113. }
  114. #translators: The parameter is a filename.
  115. printf(gettext("successfully generated %s"), $wrapper);
  116. print "\n";
  117. } #}}}
  118. 1