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