X-Git-Url: https://git.rapsys.eu/acme/blobdiff_plain/f907516ba163b32e2e281b08d3ee1663d726999d..9c3c698790057b5c32bb7daacb9269ddf4b677df:/letsconf diff --git a/letsconf b/letsconf index 7d9c0cf..cc0f077 100755 --- a/letsconf +++ b/letsconf @@ -1,102 +1,141 @@ -#! /usr/bin/php -. +# +# Copyright (C) 2016 - 2017 Raphaël Gertz -# Directory do not exists -if (!is_dir(dirname($argv[1]))) { - echo 'Directory '.dirname($argv[1]).' do not exists'."\n"; - exit(1); -} +# Best practice +use strict; +use warnings; + +# Load required modules +use JSON; +use Tie::IxHash; + +# Load POSIX +use POSIX qw(EXIT_SUCCESS EXIT_FAILURE); + +# XXX: Debug +use Data::Dumper; + +# Init redhat +my @redhat = (); -# Directory do not exists -if (file_exists($argv[1]) && !in_array(filetype($argv[1]), array('file','link'))) { - echo 'File '.$argv[1].' exists and is not a file'."\n"; - exit(1); +# Init debian +my @debian = (); + +# Init root +my %root = (); +tie(%root, 'Tie::IxHash', thumbprint => '/etc/acmepl/thumbprint', certificates => []); + +# Init prod +my $prod = 0; + +# Strip and enable prod +@ARGV = map { if ($_ eq '-p') { $prod = 1; (); } else { $_; } } @ARGV; + +# Strip and enable debug +for (my $i = 0; $i <= $#ARGV; $i++) { + # Match redhat types + if ($ARGV[$i] =~ /^(?:(\-r|\-\-redhat)(?:=([^-][a-zA-Z0-9_\.,-]+))?)$/) { + if (defined($2)) { + push(@redhat, [split(',', $2)]); + # Extract next parameter + } elsif(defined($ARGV[$i+1]) && $ARGV[$i+1] =~ /^([^-][a-zA-Z0-9_\.,-]+)$/) { + push(@redhat, [split(',', $1)]); + $i++; + # Set default + } else { + push(@redhat, ['www.example.com','example.com','...']); + } + # Match debian types + } elsif ($ARGV[$i] =~ /^(?:(\-d|\-\-debian)(?:=([^-][a-zA-Z0-9_\.,-]+))?)$/) { + if (defined($2)) { + push(@debian, [split(',', $2)]); + # Extract next parameter + } elsif(defined($ARGV[$i+1]) && $ARGV[$i+1] =~ /^([^-][a-zA-Z0-9_\.,-]+)$/) { + push(@debian, [split(',', $1)]); + $i++; + # Set default + } else { + push(@debian, ['www.example.com','example.com','...']); + } + } } -# Symlink target do not exists -if (is_link($argv[1]) && !file_exists($argv[1])) { - # Read final link - $target = $argv[1]; - # Extract last link - do { - # Update to next link - $target = readlink($target); - } while (is_link($target)); - echo 'Symlink '.$argv[1].' target '.$target.' do not exists'."\n"; - exit(1); +# Show usage +if (scalar(@ARGV) < 1) { + print "Usage: $0 [(-d|--debian)[=example.com[,...]] [(-r|--redhat)[=example.com[,...]]] [...] > /etc/acmepl/config\n"; + exit EXIT_FAILURE; } -# Not writable -if ( - (is_file($argv[1]) && !is_writable($argv[1])) || - (!file_exists($argv[1]) && !is_writable(dirname($argv[1]))) -) { - echo 'Unable to open '.$argv[1].' for writing'."\n"; - exit(1); +# Append redhat style examples +for my $key (@redhat) { + my $domain = shift @{$key}; + my @domains = $key; + tie(%{$root{certificates}[$#{$root{certificates}}+1]}, 'Tie::IxHash', ( + # Public cert + #XXX: required + cert => '/etc/pki/tls/certs/'.$domain.'.pem', + # Private key + #XXX: required + key => '/etc/pki/tls/private/'.$domain.'.pem', + # Mail address + #XXX: required + mail => 'webmaster@'.$domain, + # Root domain + #XXX: required + domain => $domain, + # Domain list + #XXX: required + domains => @domains, + # Production certificate + #XXX: optional + #XXX: set to 1 for production + prod => $prod + )); } -// Generate config -$config = json_encode( - // Root array - array( - //Thumbprint file - 'thumbprint' => '/etc/acmepl/thumbprint', - //Certificate array - 'certificates' => array( - // Certificate object - array( - // Public cert - //XXX: required - 'cert' => '/etc/pki/tls/certs/httpd.pem', - // Private key - //XXX: required - 'key' => '/etc/pki/tls/private/httpd.pem', - // Mail address - //XXX: required - 'mail' => 'webmaster@example.com', - // Root domain - //XXX: required - 'domain' => 'www.example.com', - // Domain list - //XXX: required - 'domains' => array( - 'example.com', - #... - ), - // Production certificate - //XXX: optional - //XXX: set to 1 for production - 'prod' => 0 - ), - // Other certificate - array( - 'cert' => '/etc/ssl/certs/apache.crt', - 'key' => '/etc/ssl/private/apache.key', - 'mail' => 'postmaster@example.com', - 'domain' => 'mail.example.com', - 'domains' => array( - 'imap.example.com', - 'smtp.example.com' - ), - 'prod' => 0 - ), - #... - ) - ), - // Product a nice result - JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES -); - -# Send to stdout -if ($argv[1] == '-') { - echo $config; -# Save to file -} else { - file_put_contents($argv[1], $config); +# Append debian style examples +for my $key (@debian) { + my $domain = shift @{$key}; + my @domains = $key; + tie(%{$root{certificates}[$#{$root{certificates}}+1]}, 'Tie::IxHash', ( + # Public cert + #XXX: required + cert => '/etc/ssl/certs/'.$domain.'.crt', + # Private key + #XXX: required + key => '/etc/ssl/private/'.$domain.'.key', + # Mail address + #XXX: required + mail => 'webmaster@'.$domain, + # Root domain + #XXX: required + domain => $domain, + # Domain list + #XXX: required + domains => @domains, + # Production certificate + #XXX: optional + #XXX: set to 1 for production + prod => $prod + )); } + +# Display configuration template +print to_json(\%root, {pretty => 1}); + +# Exit with success +exit EXIT_SUCCESS;