/usr/share/perl5/IkiWiki/Plugin/ikiwikihosting.pm is in ikiwiki-hosting-common 0.20170622ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | #!/usr/bin/perl
# Adds config file settings to record ikiwiki-hosting information,
# and contains essential hosting functionality.
package IkiWiki::Plugin::ikiwikihosting;
use warnings;
use strict;
use IkiWiki 3.00;
use URI::Escape;
sub import {
hook(type => "getsetup", id => "ikiwikihosting", call => \&getsetup);
hook(type => "sessioncgi", id => "ikiwikihosting", call => \&sessioncgi);
hook(type => "formbuilder_setup", id => "ikiwikihosting",
call => \&formbuilder_setup, last => 1);
hook(type => "checkconfig", id => "ikiwikihosting", call => \&checkconfig);
hook(type => "genwrapper", id => "ikiwikihosting", call => \&genwrapper);
}
sub getsetup () {
return
plugin => {
safe => 0,
rebuild => undef,
section => "core",
},
urlalias => {
type => "string",
example => [],
description => "list of urls that alias to the main url",
safe => 0,
rebuild => 0,
},
owner => {
type => "string",
description => "openid or email of primary site owner",
safe => 0,
rebuild => 0,
},
parent => {
type => "string",
description => "optional hostname of site this one was branched from",
safe => 0,
rebuild => 0,
},
hostname => {
type => "string",
description => "internal hostname of this site",
safe => 0,
rebuild => 0,
},
created => {
type => "integer",
description => "site creation datestamp",
safe => 0,
rebuild => 0,
},
log_period => {
type => "integer",
example => 7,
description => "how many days to retain logs",
safe => 1,
rebuild => 0,
},
ipv6_disabled => {
type => "boolean",
example => 1,
description => "disable IPv6?",
safe => 1,
rebuild => 0,
},
redirect_to_https => {
type => "boolean",
example => 1,
description => "redirect from http to https?",
safe => 1,
rebuild => 0,
},
use_letsencrypt => {
type => "boolean",
example => 1,
description => "use Lets Encrypt to generate https certificate?",
safe => 1,
rebuild => 0,
},
}
sub sessioncgi ($$) {
my $cgi=shift;
my $session=shift;
return unless defined $cgi->param("do");
if ($cgi->param("do") eq "deletesite") {
deletesite($cgi, $session);
}
elsif ($cgi->param("do") eq "setupdns") {
setupdns($cgi, $session);
}
elsif ($cgi->param("do") eq "setupsshkeys") {
setupsshkeys($cgi, $session);
}
elsif ($cgi->param("do") eq "sshkey") {
print "Content-Type: text/plain\n\n";
print readfile(get_ssh_public_key())."\n";
exit 0;
}
}
sub is_admin_or_owner ($) {
my $session=shift;
IkiWiki::is_admin($session->param("name")) ||
$config{owner} eq $session->param("name");
}
sub formbuilder_setup (@) {
my %params=@_;
my $form=$params{form};
my $cgi=$params{cgi};
if ($form->title eq "preferences" &&
is_admin_or_owner($params{session})) {
if ($config{controlsitecgiurl}) {
push @{$params{buttons}}, "Control Panel";
if ($form->submitted && $form->submitted eq "Control Panel") {
controlpanelredir($cgi);
}
}
eval q{use IkiWiki::Hosting};
error $@ if $@;
IkiWiki::Hosting::readconfig();
if ($config{allow_analog_reports}) {
push @{$params{buttons}}, "Statistics";
if ($form->submitted && $form->submitted eq "Statistics") {
analog_report($cgi, $params{session});
}
}
}
if ($form->title eq "setup" &&
is_admin_or_owner($params{session})) {
push @{$params{buttons}}, "DNS";
if ($form->submitted && $form->submitted eq "DNS") {
setupdns($cgi, $params{session});
}
push @{$params{buttons}}, "SSH keys";
if ($form->submitted && $form->submitted eq "SSH keys") {
setupsshkeys($cgi, $params{session});
}
}
if (defined $cgi->param('welcome')) {
eval q{use IkiWiki::Hosting};
error $@ if $@;
my $t=IkiWiki::Hosting::ikisite_template($cgi->param('welcome').".tmpl");
$form->tmpl_param(message => $t->output);
}
}
sub deletesite ($$){
my $cgi=shift;
my $session=shift;
IkiWiki::needsignin($cgi, $session);
if (! is_admin_or_owner($session)) {
error "You are not logged in as an admin or site owner.";
}
eval q{use CGI::FormBuilder};
error($@) if $@;
my $f = CGI::FormBuilder->new(
name => "deletesite",
header => 0,
charset => "utf-8",
method => 'POST',
javascript => 0,
params => $cgi,
action => IkiWiki::cgiurl(),
stylesheet => 1,
fields => [qw{do}],
);
$f->field(name => "do", type => "hidden", value => "deletesite", force => 1);
$f->field(name => "sid", type => "hidden", value => $session->id,
force => 1);
$f->title("Delete this site?");
my @buttons=("Delete Entire Site", "Cancel");
if ($f->submitted eq $buttons[1]) { # Cancel
controlpanelredir($cgi);
}
elsif ($f->submitted eq $buttons[0] && $f->validate) {
IkiWiki::checksessionexpiry($cgi, $session, $cgi->param('sid'));
eval q{use IkiWiki::Hosting};
error $@ if $@;
# This is running as the site user, but ikisite delete
# needs to run as root. So, use ikisite-wrapper to run
# it, getting a nonce first.
$ENV{IKISITE_NONCE}=IkiWiki::Hosting::getshell("ikisite",
"createnonce", $config{hostname});
# Drop the lock, to avoid any interference with site
# deletion.
IkiWiki::unlockwiki();
IkiWiki::Hosting::shell("ikisite-wrapper", "delete", $config{hostname});
# Have to take the user somewhere now that their
# site is gone..
controlpanelredir($cgi);
}
else {
IkiWiki::showform($f, \@buttons, $session, $cgi);
}
exit 0;
}
sub setupdns ($$){
my $cgi=shift;
my $session=shift;
IkiWiki::needsignin($cgi, $session);
if (! is_admin_or_owner($session)) {
error "You are not logged in as an admin or site owner.";
}
eval q{use IkiWiki::Hosting};
error $@ if $@;
my $template=IkiWiki::Hosting::ikisite_template("setupdns.tmpl");
$template->param(internal_hostname => $config{hostname});
$template->param(sid => $session->id);
if ($cgi->param('submit')) {
IkiWiki::checksessionexpiry($cgi, $session, $cgi->param('sid'));
my $external=$cgi->param('external');
my @alias=split(' ', $cgi->param('alias'));
my %alias=map { $_ => 1 } @alias;
if (lc $external ne lc $config{hostname}) {
if ($external =~ /^www\.(.*)$/i) {
$alias{$1}=1;
}
else {
$alias{"www.$external"}=1;
}
}
# This is running as the site user, but ikisite domains
# needs to run as root. So, use ikisite-wrapper to run
# it, getting a nonce first.
$ENV{IKISITE_NONCE}=IkiWiki::Hosting::getshell("ikisite",
"createnonce", $config{hostname});
# Drop the lock, to avoid interference with site
# rebuild.
IkiWiki::unlockwiki();
# Gather any error output from ikisite domains, to display
# to the user.
eval q{use IPC::Open3};
error $@ if $@;
my $pid=open3(undef, \*STDIN, \*STDIN,
"ikisite-wrapper", "domains", $config{hostname},
(lc $external ne lc $config{hostname} ? "--external=$external" : ()),
(map { "--alias=$_" } keys %alias),
);
my $error;
{
local $/=undef;
$error=<STDIN>;
$error=~s/\n/<br \/>/g;
}
waitpid( $pid, 0);
my $ret=$?;
IkiWiki::Hosting::shell("ikisite-wrapper", "deletenonce",
$config{hostname}, "--nonce=$ENV{IKISITE_NONCE}");
delete $ENV{IKISITE_NONCE};
if (! $ret) {
$template->param(dns_ok => 1);
$template->param(external_hostname => external_hostname($config{hostname}));
$template->param(alias => join("\n", alias($config{hostname}))."\n");
}
else {
$template->param(error => $error);
# preserve bad input values to allow correcting
$template->param(external_hostname => $external);
$template->param(alias => join("\n", @alias)."\n");
# special exit codes used for missing and wrong dns
if ($ret >> 8 == 2) {
$template->param(dns_needed => 1);
}
elsif ($ret >> 8 == 3) {
$template->param(dns_wrong => 1);
}
}
}
else {
$template->param(first => 1);
$template->param(external_hostname => external_hostname($config{hostname}));
$template->param(alias => join("\n", alias($config{hostname}))."\n");
}
IkiWiki::printheader($session);
print IkiWiki::cgitemplate($cgi, "DNS", $template->output);
exit 0;
}
sub setupsshkeys ($$) {
my $cgi=shift;
my $session=shift;
IkiWiki::needsignin($cgi, $session);
if (! is_admin_or_owner($session)) {
error "You are not logged in as an admin or site owner.";
}
eval q{use IkiWiki::Hosting};
error $@ if $@;
my $template=IkiWiki::Hosting::ikisite_template("setupsshkeys.tmpl");
$template->param(sid => $session->id);
my $changed=0;
if ($cgi->param('submit')) {
IkiWiki::checksessionexpiry($cgi, $session, $cgi->param('sid'));
if (defined $cgi->param("add") &&
length $cgi->param("add")) {
eval { IkiWiki::Hosting::shell("ikisite",
"enablesshkey", $config{hostname},
"--key=".$cgi->param("add"),
) };
if ($@) {
$template->param(add_error => "Unable to add SSH key. Check format.");
$template->param(add => $cgi->param("add"));
}
else {
$changed=1;
}
}
}
if (defined $cgi->param("delete") &&
length $cgi->param("delete")) {
print STDERR "delete $@ ".$cgi->param("delete")."\n";
eval { IkiWiki::Hosting::shell("ikisite",
"disablesshkey", $config{hostname},
"--key=".$cgi->param("delete"),
) };
if ($@) {
$template->param(delete_error => "Unable to delete SSH key.");
}
else {
$changed=1;
}
}
my @keys=split "\n", IkiWiki::Hosting::getshell("ikisite", "sshkeys", $config{hostname});
$template->param(keys => [map {
key => $_,
key_urlencoded => uri_escape($_),
}, grep defined, @keys]);
# Ikiwiki will be running as the site's username,
my $username=(getpwuid($<))[0];
if (defined $username && length $username &&
defined $config{hostname} && length $config{hostname}) {
$template->param(sshaddress => "$username\@$config{hostname}");
}
IkiWiki::printheader($session);
print IkiWiki::cgitemplate($cgi, "SSH keys", $template->output);
handlechangedsetup() if $changed;
exit 0;
}
sub controlpanelredir ($) {
my $cgi=shift;
eval q{use IkiWiki::Hosting};
error $@ if $@;
IkiWiki::Hosting::readconfig();
unless ($config{controlsitecgiurl}) {
error "This wiki does not have a control panel.";
}
IkiWiki::redirect($cgi, $config{controlsitecgiurl}."?do=controlpanel");
}
sub external_hostname ($) {
my $internal_hostname=shift;
eval q{use URI};
error $@ if $@;
eval q{use IkiWiki::Hosting};
error $@ if $@;
my $url=IkiWiki::Hosting::getshell("ikisite", "getsetup",
$internal_hostname, "url");
my $u=URI->new($url);
if (! $u->can("host")) {
error "bad url for $internal_hostname: $url\n";
}
return $u->host;
}
sub alias ($) {
my $internal_hostname=shift;
eval q{use URI};
error $@ if $@;
eval q{use IkiWiki::Hosting};
error $@ if $@;
my $alias=IkiWiki::Hosting::yamlgetshell("ikisite", "getsetup",
$internal_hostname, "urlalias");
my @alias;
if ($alias) {
foreach my $u (map { URI->new($_) } @$alias) {
if ($u->can("host")) {
push @alias, $u->host;
}
}
}
return @alias;
}
sub checkconfig {
if (! defined $config{ipv6_disabled}) {
$config{ipv6_disabled}=0;
}
if (! defined $config{redirect_to_https}) {
$config{redirect_to_https}=0;
}
}
sub genwrapper {
# This hook is called whenever wrappers are being built,
# and that happens when configuration has changed.
handlechangedsetup();
# C code to inject into wrapper.
return "";
}
sub handlechangedsetup {
# Locking requires this be done in the background.
eval q{use IkiWiki::Hosting};
error $@ if $@;
my $pid=IkiWiki::Hosting::daemonize();
if ($pid) {
return;
}
# Drop lock; ikisite takes the lock itself.
IkiWiki::unlockwiki();
# Commit any changes to the setup file into the setup branch.
IkiWiki::Hosting::getshell("ikisite", "commitsetup", $config{hostname});
# Re-enable the site for changes to eg, redirect_to_https or dns.
# This can't enable any site that's not enabled, since the site has
# to be enabled already for this cgi to be run.
IkiWiki::Hosting::getshell("ikisite-wrapper", "enable", $config{hostname});
exit;
}
# Returns path of ssh public key.
# If no key exists, one is created. (Can be a little slow.)
sub get_ssh_public_key {
my @keys;
my $findkeys=sub { @keys=glob("$ENV{HOME}/.ssh/id_*.pub") };
$findkeys->();
if (! @keys) {
my $ret=system("ssh-keygen", "-q", "-t", "rsa",
"-f", "$ENV{HOME}/.ssh/id_rsa",
"-N", "",
"-C", $config{wikiname});
$findkeys->();
if ($ret !=0 || ! @keys) {
error "ssh-keygen failed";
}
}
return shift(@keys);
}
sub analog_report ($$) {
my $cgi=shift;
my $session=shift;
IkiWiki::needsignin($cgi, $session);
if (! is_admin_or_owner($session)) {
error "You are not logged in as an admin or site owner.";
}
print "Content-type: text/html\n\n";
system("ikisite", $config{hostname}, "analog", "--"
, "-a" # html output
, "+CHOSTNAME 'this site'"
);
exit 0;
}
1
|