summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: 79b9eb3e32499229aaa43f8884a776ae8471ccb4 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use Cwd q{abs_path};
  6. use Data::Dumper;
  7. use 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. $Data::Dumper::Indent=0; # no newlines
  35. my $configstring=Data::Dumper->Dump([\%config], ['*config']);
  36. $configstring=~s/\\/\\\\/g;
  37. $configstring=~s/"/\\"/g;
  38. $configstring=~s/\n/\\n/g;
  39. #translators: The first parameter is a filename, and the second is
  40. #translators: a (probably not translated) error message.
  41. open(OUT, ">$wrapper.c") || error(sprintf(gettext("failed to write %s: %s"), "$wrapper.c", $!));;
  42. print OUT <<"EOF";
  43. /* A wrapper for ikiwiki, can be safely made suid. */
  44. #include <stdio.h>
  45. #include <sys/types.h>
  46. #include <unistd.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. extern char **environ;
  50. char *newenviron[$#envsave+5];
  51. int i=0;
  52. addenv(char *var, char *val) {
  53. char *s=malloc(strlen(var)+1+strlen(val)+1);
  54. if (!s)
  55. perror("malloc");
  56. sprintf(s, "%s=%s", var, val);
  57. newenviron[i++]=s;
  58. }
  59. int main (int argc, char **argv) {
  60. /* Sanitize environment. */
  61. char *s;
  62. $envsave
  63. newenviron[i++]="HOME=$ENV{HOME}";
  64. newenviron[i++]="WRAPPED_OPTIONS=$configstring";
  65. newenviron[i]=NULL;
  66. environ=newenviron;
  67. if (setregid(getegid(), -1) != 0 &&
  68. setregid(getegid(), -1) != 0) {
  69. perror("failed to drop real gid");
  70. exit(1);
  71. }
  72. if (setreuid(geteuid(), -1) != 0 &&
  73. setreuid(geteuid(), -1) != 0) {
  74. perror("failed to drop real uid");
  75. exit(1);
  76. }
  77. execl("$this", "$this", NULL);
  78. perror("exec $this");
  79. exit(1);
  80. }
  81. EOF
  82. close OUT;
  83. my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
  84. if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
  85. #translators: The parameter is a C filename.
  86. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  87. }
  88. unlink("$wrapper.c");
  89. if (defined $config{wrappergroup}) {
  90. my $gid=(getgrnam($config{wrappergroup}))[2];
  91. if (! defined $gid) {
  92. error(sprintf("bad wrappergroup"));
  93. }
  94. if (! chown(-1, $gid, "$wrapper.new")) {
  95. error("chown $wrapper.new: $!");
  96. }
  97. }
  98. if (defined $config{wrappermode} &&
  99. ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
  100. error("chmod $wrapper.new: $!");
  101. }
  102. if (! rename("$wrapper.new", $wrapper)) {
  103. error("rename $wrapper.new $wrapper: $!");
  104. }
  105. #translators: The parameter is a filename.
  106. printf(gettext("successfully generated %s"), $wrapper);
  107. print "\n";
  108. } #}}}
  109. 1