summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: dcf3e5c364ba2380ba04f3c78f0fca551f0badc3 (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. my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
  94. if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
  95. #translators: The parameter is a C filename.
  96. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  97. }
  98. unlink("$wrapper.c");
  99. if (defined $config{wrappergroup}) {
  100. my $gid=(getgrnam($config{wrappergroup}))[2];
  101. if (! defined $gid) {
  102. error(sprintf("bad wrappergroup"));
  103. }
  104. if (! chown(-1, $gid, "$wrapper.new")) {
  105. error("chown $wrapper.new: $!");
  106. }
  107. }
  108. if (defined $config{wrappermode} &&
  109. ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
  110. error("chmod $wrapper.new: $!");
  111. }
  112. if (! rename("$wrapper.new", $wrapper)) {
  113. error("rename $wrapper.new $wrapper: $!");
  114. }
  115. #translators: The parameter is a filename.
  116. printf(gettext("successfully generated %s"), $wrapper);
  117. print "\n";
  118. } #}}}
  119. 1