summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: 0a2b8d4f83c80b648d33d2f17474dc5957e91769 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki;
  3. use warnings;
  4. use strict;
  5. use File::Spec;
  6. use Data::Dumper;
  7. use IkiWiki;
  8. sub gen_wrapper () { #{{{
  9. $config{srcdir}=File::Spec->rel2abs($config{srcdir});
  10. $config{destdir}=File::Spec->rel2abs($config{destdir});
  11. my $this=File::Spec->rel2abs($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 HTTPS} 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+6];
  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. asprintf(&s, "CALLER_UID=%i", getuid());
  66. newenviron[i++]=s;
  67. newenviron[i]=NULL;
  68. environ=newenviron;
  69. if (setregid(getegid(), -1) != 0 &&
  70. setregid(getegid(), -1) != 0) {
  71. perror("failed to drop real gid");
  72. exit(1);
  73. }
  74. if (setreuid(geteuid(), -1) != 0 &&
  75. setreuid(geteuid(), -1) != 0) {
  76. perror("failed to drop real uid");
  77. exit(1);
  78. }
  79. execl("$this", "$this", NULL);
  80. perror("exec $this");
  81. exit(1);
  82. }
  83. EOF
  84. close OUT;
  85. my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
  86. if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
  87. #translators: The parameter is a C filename.
  88. error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
  89. }
  90. unlink("$wrapper.c");
  91. if (defined $config{wrappergroup}) {
  92. my $gid=(getgrnam($config{wrappergroup}))[2];
  93. if (! defined $gid) {
  94. error(sprintf("bad wrappergroup"));
  95. }
  96. if (! chown(-1, $gid, "$wrapper.new")) {
  97. error("chown $wrapper.new: $!");
  98. }
  99. }
  100. if (defined $config{wrappermode} &&
  101. ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
  102. error("chmod $wrapper.new: $!");
  103. }
  104. if (! rename("$wrapper.new", $wrapper)) {
  105. error("rename $wrapper.new $wrapper: $!");
  106. }
  107. #translators: The parameter is a filename.
  108. printf(gettext("successfully generated %s"), $wrapper);
  109. print "\n";
  110. } #}}}
  111. 1