deploy.pl/_archive/passgen.pl

109 lines
3.1 KiB
Perl
Raw Normal View History

2024-05-20 13:26:09 +02:00
#!/usr/bin/env perl
#
# Copyright (c) 2019-2020 Mischa Peters <mischa @ openbsd.amsterdam>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
use 5.024;
use strict;
use warnings;
use autodie;
use Cwd qw(cwd);
use User::pwent;
# fuction to parse _deploy.conf and vm*.txt files
# all variables are stripped and added to either %vms or %conf
sub get_variables {
my ($hash_name, @files) = @_;
my %hash;
my $filename;
my $vm_name;
my $vm_number;
for my $file (@files) {
# When hash is 'vms' use the vm_name as key
# Otherwise use 'conf' as key
if ($hash_name eq "vms") {
($filename = $file) =~ s/.*\///;
($vm_name = $filename) =~ s/\.txt//;
($vm_number = $vm_name) =~ s/^vm//;
$hash{$vm_name}{'vm_number'} = $vm_number;
}
open my $fh, "<", "$file";
while (my $row = <$fh>) {
next if ($row =~ /^\s*($|#)/);
chomp($row);
(my $key, my $val) = split(/=/, $row, 2);
if ($hash_name eq "vms") {
($hash{$vm_name}{$key} .= $val) =~ s/^"+|"+$//g;
} else {
($hash{$hash_name}{$key} .= $val) =~ s/^"+|"+$//g;
}
}
close $fh;
}
return %hash;
}
# function to create accounts on the host for vmctl(8) access
sub change_accounts {
my %conf = %{$_[0]};
my %vms = %{$_[1]};
printf "useradd(8) creation:\n";
for my $vm_name (sort keys %vms) {
my $_instance = $vms{$vm_name}{'instance'} || $vm_name;
my $_owner = $vms{$vm_name}{'owner'} || $vms{$vm_name}{'username'};
my $jot_pass = qx(jot -rcs '' 20 43 125);
chomp($jot_pass);
my $encrypt_pass = qx(encrypt '${jot_pass}');
chomp($encrypt_pass);
my $output = qx(/usr/sbin/usermod -p '${encrypt_pass}' $_owner);
printf "%s - %s - %s\n", $_owner, $encrypt_pass, $jot_pass;
}
}
# function to print all keys & values for debug purposes
sub debug_parse {
my %conf = %{$_[0]};
my %vms = %{$_[1]};
for my $vm_name (sort keys %vms) {
for my $key (keys %{$vms{$vm_name}}) {
printf "VMS: %s %s = %s\n", $vm_name, $key, $vms{$vm_name}{$key};
}
}
}
# check if _deploy.conf exists in current working directory
my %conf;
my $dir = cwd;
if (-e "$dir/_deploy.conf") {
%conf = get_variables('conf', "$dir/_deploy.conf");
} else {
printf "Unable to find config file in current directory (%s).\n", $dir;
printf "Create the config file _deploy.conf in %s.\n", $dir;
exit 1;
}
# parse all vm*.txt files in the VMS directory
my %vms;
my @files = glob "$conf{'conf'}{'VMS'}/*.txt";
%vms = get_variables('vms', @files);
# run all functions
#debug_parse(\%conf, \%vms);
change_accounts(\%conf, \%vms);