2006-03-23 07:51:15 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2006-03-23 08:37:16 +01:00
|
|
|
use Cwd q{abs_path};
|
2008-01-08 00:12:12 +01:00
|
|
|
use Data::Dumper ;
|
2006-05-02 08:53:33 +02:00
|
|
|
use IkiWiki;
|
2006-03-23 07:51:15 +01:00
|
|
|
|
|
|
|
package IkiWiki;
|
|
|
|
|
|
|
|
sub gen_wrapper () { #{{{
|
|
|
|
$config{srcdir}=abs_path($config{srcdir});
|
|
|
|
$config{destdir}=abs_path($config{destdir});
|
|
|
|
my $this=abs_path($0);
|
|
|
|
if (! -x $this) {
|
2006-12-29 05:38:40 +01:00
|
|
|
error(sprintf(gettext("%s doesn't seem to be executable"), $this));
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($config{setup}) {
|
2006-12-29 05:38:40 +01:00
|
|
|
error(gettext("cannot create a wrapper that uses a setup file"));
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
2006-03-23 08:37:16 +01:00
|
|
|
my $wrapper=possibly_foolish_untaint($config{wrapper});
|
2006-04-20 15:34:11 +02:00
|
|
|
if (! defined $wrapper || ! length $wrapper) {
|
2006-12-29 05:38:40 +01:00
|
|
|
error(gettext("wrapper filename not specified"));
|
2006-04-20 15:34:11 +02:00
|
|
|
}
|
2006-03-23 08:37:16 +01:00
|
|
|
delete $config{wrapper};
|
2006-03-23 07:51:15 +01:00
|
|
|
|
|
|
|
my @envsave;
|
|
|
|
push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
|
|
|
|
CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
|
2006-10-03 00:56:09 +02:00
|
|
|
HTTP_COOKIE REMOTE_USER} if $config{cgi};
|
2006-03-23 07:51:15 +01:00
|
|
|
my $envsave="";
|
|
|
|
foreach my $var (@envsave) {
|
|
|
|
$envsave.=<<"EOF"
|
|
|
|
if ((s=getenv("$var")))
|
2007-07-09 02:39:55 +02:00
|
|
|
addenv("$var", s);
|
2006-03-23 07:51:15 +01:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2006-03-23 08:42:24 +01:00
|
|
|
$Data::Dumper::Indent=0; # no newlines
|
2006-03-23 08:37:16 +01:00
|
|
|
my $configstring=Data::Dumper->Dump([\%config], ['*config']);
|
|
|
|
$configstring=~s/\\/\\\\/g;
|
|
|
|
$configstring=~s/"/\\"/g;
|
2007-06-03 18:24:22 +02:00
|
|
|
$configstring=~s/\n/\\n/g;
|
2006-03-23 08:37:16 +01:00
|
|
|
|
2007-01-04 13:00:23 +01:00
|
|
|
#translators: The first parameter is a filename, and the second is
|
|
|
|
#translators: a (probably not translated) error message.
|
2006-12-29 05:38:40 +01:00
|
|
|
open(OUT, ">$wrapper.c") || error(sprintf(gettext("failed to write %s: %s"), "$wrapper.c", $!));;
|
2006-03-23 07:51:15 +01:00
|
|
|
print OUT <<"EOF";
|
|
|
|
/* A wrapper for ikiwiki, can be safely made suid. */
|
|
|
|
#include <stdio.h>
|
2006-09-04 05:50:27 +02:00
|
|
|
#include <sys/types.h>
|
2006-03-23 07:51:15 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
extern char **environ;
|
2007-07-09 02:39:55 +02:00
|
|
|
char *newenviron[$#envsave+5];
|
|
|
|
int i=0;
|
|
|
|
|
|
|
|
addenv(char *var, char *val) {
|
|
|
|
char *s=malloc(strlen(var)+1+strlen(val)+1);
|
|
|
|
if (!s)
|
|
|
|
perror("malloc");
|
|
|
|
sprintf(s, "%s=%s", var, val);
|
|
|
|
newenviron[i++]=s;
|
|
|
|
}
|
2006-03-23 07:51:15 +01:00
|
|
|
|
|
|
|
int main (int argc, char **argv) {
|
|
|
|
/* Sanitize environment. */
|
|
|
|
char *s;
|
|
|
|
$envsave
|
|
|
|
newenviron[i++]="HOME=$ENV{HOME}";
|
2006-03-23 08:37:16 +01:00
|
|
|
newenviron[i++]="WRAPPED_OPTIONS=$configstring";
|
2006-03-23 07:51:15 +01:00
|
|
|
newenviron[i]=NULL;
|
|
|
|
environ=newenviron;
|
|
|
|
|
2008-01-02 00:48:34 +01:00
|
|
|
if (setregid(getegid(), -1) != 0 &&
|
|
|
|
setregid(getegid(), -1) != 0) {
|
|
|
|
perror("failed to drop real gid");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (setreuid(geteuid(), -1) != 0 &&
|
|
|
|
setreuid(geteuid(), -1) != 0) {
|
|
|
|
perror("failed to drop real uid");
|
2006-09-04 05:38:02 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2006-03-23 08:37:16 +01:00
|
|
|
execl("$this", "$this", NULL);
|
2007-07-09 02:39:55 +02:00
|
|
|
perror("exec $this");
|
2006-03-23 07:51:15 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
close OUT;
|
2007-11-25 21:50:31 +01:00
|
|
|
|
|
|
|
my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
|
|
|
|
if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
|
2007-01-04 13:00:23 +01:00
|
|
|
#translators: The parameter is a C filename.
|
2006-12-29 05:38:40 +01:00
|
|
|
error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
2006-03-26 04:30:44 +02:00
|
|
|
unlink("$wrapper.c");
|
2007-11-14 15:27:11 +01:00
|
|
|
if (defined $config{wrappergroup}) {
|
|
|
|
my $gid=(getgrnam($config{wrappergroup}))[2];
|
|
|
|
if (! defined $gid) {
|
|
|
|
error(sprintf("bad wrappergroup"));
|
|
|
|
}
|
|
|
|
if (! chown(-1, $gid, "$wrapper.new")) {
|
|
|
|
error("chown $wrapper.new: $!");
|
|
|
|
}
|
|
|
|
}
|
2006-03-23 07:51:15 +01:00
|
|
|
if (defined $config{wrappermode} &&
|
2007-11-14 15:27:11 +01:00
|
|
|
! chmod(oct($config{wrappermode}), "$wrapper.new")) {
|
|
|
|
error("chmod $wrapper.new: $!");
|
|
|
|
}
|
|
|
|
if (! rename("$wrapper.new", $wrapper)) {
|
|
|
|
error("rename $wrapper.new $wrapper: $!");
|
2006-03-23 07:51:15 +01:00
|
|
|
}
|
2007-01-04 13:00:23 +01:00
|
|
|
#translators: The parameter is a filename.
|
2007-01-14 05:35:10 +01:00
|
|
|
printf(gettext("successfully generated %s"), $wrapper);
|
|
|
|
print "\n";
|
2006-03-23 07:51:15 +01:00
|
|
|
} #}}}
|
|
|
|
|
|
|
|
1
|