summaryrefslogtreecommitdiff
path: root/IkiWiki/Wrapper.pm
blob: 8e513c1f6f8037d49ff17128d1812bae47f46814 (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. package IkiWiki;
  5. sub gen_wrapper () { #{{{
  6. eval q{use Cwd 'abs_path'};
  7. $config{srcdir}=abs_path($config{srcdir});
  8. $config{destdir}=abs_path($config{destdir});
  9. my $this=abs_path($0);
  10. if (! -x $this) {
  11. error("$this doesn't seem to be executable");
  12. }
  13. if ($config{setup}) {
  14. error("cannot create a wrapper that uses a setup file");
  15. }
  16. my @params=($config{srcdir}, $config{destdir},
  17. "--wikiname=$config{wikiname}",
  18. "--templatedir=$config{templatedir}");
  19. push @params, "--verbose" if $config{verbose};
  20. push @params, "--rebuild" if $config{rebuild};
  21. push @params, "--nosvn" if !$config{svn};
  22. push @params, "--cgi" if $config{cgi};
  23. push @params, "--url=$config{url}" if length $config{url};
  24. push @params, "--cgiurl=$config{cgiurl}" if length $config{cgiurl};
  25. push @params, "--historyurl=$config{historyurl}" if length $config{historyurl};
  26. push @params, "--diffurl=$config{diffurl}" if length $config{diffurl};
  27. push @params, "--anonok" if $config{anonok};
  28. push @params, "--adminuser=$_" foreach @{$config{adminuser}};
  29. my $params=join(" ", @params);
  30. my $call='';
  31. foreach my $p ($this, $this, @params) {
  32. $call.=qq{"$p", };
  33. }
  34. $call.="NULL";
  35. my @envsave;
  36. push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
  37. CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
  38. HTTP_COOKIE} if $config{cgi};
  39. my $envsave="";
  40. foreach my $var (@envsave) {
  41. $envsave.=<<"EOF"
  42. if ((s=getenv("$var")))
  43. asprintf(&newenviron[i++], "%s=%s", "$var", s);
  44. EOF
  45. }
  46. open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
  47. print OUT <<"EOF";
  48. /* A wrapper for ikiwiki, can be safely made suid. */
  49. #define _GNU_SOURCE
  50. #include <stdio.h>
  51. #include <unistd.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. extern char **environ;
  55. int main (int argc, char **argv) {
  56. /* Sanitize environment. */
  57. char *s;
  58. char *newenviron[$#envsave+3];
  59. int i=0;
  60. $envsave
  61. newenviron[i++]="HOME=$ENV{HOME}";
  62. newenviron[i]=NULL;
  63. environ=newenviron;
  64. if (argc == 2 && strcmp(argv[1], "--params") == 0) {
  65. printf("$params\\n");
  66. exit(0);
  67. }
  68. execl($call);
  69. perror("failed to run $this");
  70. exit(1);
  71. }
  72. EOF
  73. close OUT;
  74. if (system("gcc", "ikiwiki-wrap.c", "-o", possibly_foolish_untaint($config{wrapper})) != 0) {
  75. error("failed to compile ikiwiki-wrap.c");
  76. }
  77. unlink("ikiwiki-wrap.c");
  78. if (defined $config{wrappermode} &&
  79. ! chmod(oct($config{wrappermode}), possibly_foolish_untaint($config{wrapper}))) {
  80. error("chmod $config{wrapper}: $!");
  81. }
  82. print "successfully generated $config{wrapper}\n";
  83. } #}}}
  84. 1