Blob


1 #!/usr/bin/perl
3 use strict;
4 use warnings;
5 use OpenBSD::Pledge;
6 use OpenBSD::Unveil;
7 use Data::Dumper;
8 use File::Copy qw(copy);
10 my $vmconf = "/etc/vm.conf";
11 my $zonedir = "/var/nsd/zones/master/";
12 my $hostname = "host.example.com";
13 my $ipv4path = "/home/username/ipv4s";
14 my $isopath = "/home/iso/install73.iso";
15 my @ipv4s;
16 if (!(-s "$ipv4path")) {
17 print "No IPv4 addresses in $ipv4path!\n";
18 die;
19 } else {
20 @ipv4s = readarray($ipv4path);
21 }
23 `doas chmod -R g+w $zonedir`;
25 # Read from filename and return array of lines without trailing newlines
26 sub readarray {
27 my ($filename) = @_;
28 open(my $fh, '<', $filename) or die "Could not read file '$filename' $!";
29 chomp(my @lines = <$fh>);
30 close $fh;
31 return @lines;
32 }
34 # Read from filename and return as string
35 sub readstr {
36 my ($filename) = @_;
37 open my $fh, '<', $filename or die "Could not read file '$filename' $!";
38 my $str = do { local $/; <$fh> };
39 close $fh;
40 return $str;
41 }
43 # Write str to filename
44 sub writefile {
45 my ($filename, $str) = @_;
46 open(my $fh, '>', "$filename") or die "Could not write to $filename";
47 print $fh $str;
48 close $fh;
49 }
51 # Append str to filename
52 sub appendfile {
53 my ($filename, $str) = @_;
54 open(my $fh, '>>', "$filename") or die "Could not append to $filename";
55 print $fh $str;
56 close $fh;
57 }
59 sub date {
60 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
61 my $localtime = sprintf("%04d%02d%02d", $year+1900, $mon+1, $mday);
62 return $localtime;
63 }
65 sub setdns {
66 my ($domain, $ip) = @_;
67 my $filename = "$zonedir/$hostname";
68 my $subdomain;
69 if ($domain =~ /^([a-zA-Z][-\.a-zA-Z0-9]+)\.$hostname$/) {
70 $subdomain = $1;
71 } else {
72 return 0;
73 }
74 my @lines = readarray($filename);
75 foreach my $line (@lines) {
76 # increment the zone's serial number
77 if ($line =~ /(\d{8})(\d{2})((\s+\d+){4}\s*\))/) {
78 my $date = date();
79 my $serial = 0;
80 if ($date <= $1) { $serial = $2+1; }
81 $line = $`.$date.sprintf("%02d",$serial).$3.$';
82 }
83 }
84 if ($ip =~ /^([0-9\.]+)$/) { # if IPv4
85 push(@lines, "$subdomain 3600 IN A $ip");
86 } elsif ($ip =~ /:/) { # if IPv6
87 push(@lines, "$subdomain 3600 IN AAAA $ip");
88 } elsif (!defined($ip)) { # delete records
89 @lines = grep !/\b$subdomain\s*3600\s*IN/, @lines;
90 }
91 # trailing newline necessary
92 writefile("$filename.bak", join("\n", @lines)."\n");
93 copy "$filename.bak", $filename;
94 if (system("doas -u _nsd nsd-control reload")) {
95 return 0;
96 } else {
97 return 1;
98 }
99 }
101 # create A and AAAA records for subdomain, set the rDNS,
102 # and return the new ipv6 address
103 sub nextdns {
104 my ($subdomain) = @_;
105 my $ipv4 = shift(@ipv4s);
106 my $ipv6;
107 my $fqdn = "$subdomain.$hostname";
108 if ($ipv4 =~ /^[0-9]+\.[0-9]+\.[0-9]+\.([0-9]+)$/) {
109 $ipv6 = "2602:fccf:1:1".sprintf("%03d",$1)."::";
111 writefile($ipv4path, join("\n", @ipv4s));
112 my $success = setdns($fqdn, $ipv4) && setdns($fqdn, $ipv6) && setdns("ns1.$fqdn", $ipv4) && setdns("ns2.$fqdn", $ipv4);
113 print "IP: $ipv4 $ipv6\n";
114 return $success;
117 sub createshell {
118 my ($username, $password) = @_;
119 print "Username: $username\n";
120 print "Password: $password\n";
121 system "doas groupadd $username";
122 system "doas adduser -batch $username $username $username `encrypt $password`";
123 system "doas usermod -G vmdusers $username";
124 system "doas chmod -R o-rwx /home/$username";
125 system "doas su -l $username -c \"vmctl create -s 20G $username.qcow2\"";
126 print "VM created for $username!\n";
127 my @vmconf = readarray($vmconf);
128 my $lladdr;
129 foreach my $line (@vmconf) {
130 if ($line =~ /lladdr (.*)/) {
131 $lladdr = $1;
134 if (defined($lladdr) && $lladdr =~ /([0-9a-fA-F]{2})$/) {
135 $lladdr = $`.($1+1);
137 my $block = <<"EOF";
138 vm "$username" {
139 owner $username
140 memory 2048M
141 cdrom "$isopath"
142 disk /home/$username/$username.qcow2
143 interface {
144 locked lladdr $lladdr
145 switch "switch0"
148 EOF
149 appendfile($vmconf, $block);
150 `doas vmctl reload`;
153 my $nargs = $#ARGV + 1;
154 if ($nargs != 1) {
155 print "\nUsage: install.pl username\n";
156 exit;
158 my $username = $ARGV[0];
159 my $password = join'', map +(0..9,'a'..'z','A'..'Z')[rand(10+26*2)], 1..12;
161 createshell($username, $password);
162 nextdns($username);