[go: up one dir, main page]

Menu

[r5]: / trunk / configure  Maximize  Restore  History

Download this file

207 lines (159 with data), 4.3 kB

#! /usr/bin/perl

use strict;

# this script will create the certificates for OpenMAP (if necessary)
# After this, the Makefile is created

print STDERR "OpenMAP configuration utility\n";
print STDERR "-----------------------------\n\n";

my $basedir = "config";

print STDERR "Checking for config directory...";
if (! -e $basedir) {
	print STDERR "\n   Creating config directory '$basedir'\n";
	mkdir $basedir;
	chmod 0700, $basedir;
}
else {
	print STDERR "OK\n";
}

if (! -e $basedir."/.data") {
	mkdir $basedir."/.data";
}

print STDERR "Checking version...";

my $updateversion = 0;

if (! -e $basedir."/.data/version") {
	$updateversion = 1;
}
else {
	if (open my $fd, "VERSION") {
		my $currentversion = <$fd>;
		chomp $currentversion;

		close $fd;

		my $version = "";

		if (open my $fd, $basedir."/.data/version") {
			$version = <$fd>;
			chomp $version;

			close $fd;
		}

		if ($currentversion ne $version) {
			$updateversion = 1;
		}
	}
	else {
		die "PANIC: could not open VERSION file!\n";
	}
}

if ($updateversion) {
	print "\n   Updating version information.\n";
	system("cp VERSION ${basedir}/.data/version");
}
else {
	print STDERR "OK\n";
}

print STDERR "Checking for hostname...";

my $baseurl;

if (-e $basedir."/.data/baseurl.txt") {
	print STDERR "OK\n";
	if (open my $fd, $basedir."/.data/baseurl.txt") {
		$baseurl = <$fd>;
		chomp $baseurl;

		close $fd;
	}
}
else {
	my $ok = 0;

	while (!$ok) {
		print STDERR "\n   Please enter the hostname of your openmap server: ";

		my $line = <STDIN>;
		chomp $line;

		if ($line =~ /[-a-z0-9\._]+/) {
			$ok = 1;
			if (open my $fd, ">".$basedir."/.data/baseurl.txt") {
				print $fd $line;
				$baseurl = $line;
				close $fd;
			}
			else {
				die ">>> Error while writing base URL file!\n";
			}
		}
		else {
			print STDERR "   >>>Wrong format!";
		}
	}
}

print STDERR "Checking for ca.key...";

my $cakey = $basedir."/.data/ca.key";

if (! -e $cakey) {
	print STDERR "\n   Creating new CA key\n";
	system("openssl", "genrsa", "-out", $cakey, 4096);
}
else {
	print STDERR "OK\n";
}

print STDERR "Checking for ca.crt...";

my $cacrt = $basedir."/ca.pem";

if (! -e $cacrt) {
	print STDERR "\n   Creating new CA certificate\n   Please answer the following questions:\n\n";
	system("openssl", "req", "-new", "-x509", "-days", 1826, "-key", $cakey, "-out", $cacrt);
}
else {
	print STDERR "OK\n";
}

print STDERR "Checking for openmap.key...";

my $iakey = $basedir."/.data/openmap.key";

if (! -e $iakey) {
	print STDERR "\n   Generating new private key\n";

	system("openssl", "genrsa", "-out", $iakey, 4096);
}
else {
	print STDERR "OK\n";
}

print STDERR "Checking for ia.csr...";

my $iacsr = $basedir."/.data/ia.csr";

if (! -e $iacsr) {
	print STDERR "\n   Generating new certificate request\n   Please ansewr the following questions, CN needs to be '$baseurl':\n\n";

	system("openssl", "req", "-new", "-key", $iakey, "-out", $iacsr);
}
else {
	print STDERR "OK\n";
}

print STDERR "Checking for openmap.crt and openmap.pem...";

my $iacrt = $basedir."/.data/openmap.crt";

if (! -e $iacrt) {
	print STDERR "\n   Generating and signing new certificate\n";

	system("openssl", "x509", "-req", "-days", 730, "-in", $iacsr, "-CA", $cacrt, "-CAkey", $cakey, "-CAcreateserial", "-out", $iacrt);

	my $iapem = $basedir."/.data/openmap.pem";

	system("cat '$iakey' '$iacrt' >  '$iapem'");
}
else {
	print STDERR "OK\n";
}

print STDERR "Checking for Makefile...";

if (! -e "Makefile") {
	print STDERR "\n   Generating new Makefile\n";

	if (open my $fd, ">Makefile") {
		print $fd "all: server windows linux\n";
		print $fd "\n";
		print $fd "server: windows\n";
		print $fd "\tcd server; make\n";
		print $fd "\n";
		print $fd "linux:\n";
		print $fd "\tcd linux; make\n";
		print $fd "\n";
		print $fd "windows:\n";
		print $fd "\tcd windows; make\n";

		close $fd;
	}
	else {
		die ">>>Error while writing Makefile!";
	}
}
else {
	print STDERR "OK\n";
}

print STDERR "Finished.\n\n";
print STDERR "==========================================\n";
print STDERR "\n";
print STDERR "Your root certificate is ${basedir}/ca.pem\n";
print STDERR "You may want to distribute it to your clients\n";
print STDERR "\n";
print STDERR "==========================================\n";