r5048: made the provision.pl script much less error prone (you don't need to
[sfrench/samba-autobuild/.git] / source4 / script / provision.pl
index b1070d20134f8cb3cb13a920b8e6aafbe736e456..1730f0859a7d2c27de07b7cf9f084f01063c7a4e 100755 (executable)
@@ -1,10 +1,12 @@
 #!/usr/bin/perl -w
 
 use strict;
+use Socket;
 use Getopt::Long;
 
 my $opt_hostname = `hostname`;
 chomp $opt_hostname;
+my $opt_hostip;
 my $opt_realm;
 my $opt_domain;
 my $opt_adminpass;
@@ -12,8 +14,12 @@ my $opt_nobody;
 my $opt_nogroup;
 my $opt_wheel;
 my $opt_users;
+my $dnsdomain;
+my $netbiosname;
 my $dnsname;
 my $basedn;
+my $defaultsite = "Default-First-Site-Name";
+my $usn = 1;
 
 # return the current NTTIME as an integer
 sub nttime()
@@ -36,7 +42,9 @@ sub randguid()
        return sprintf("%08x-%04x-%04x-%04x-%08x%04x", $r1, $r2, $r3, $r4, $r5, $r6);
 }
 
-my $domainguid = randguid();
+my $opt_domainguid = randguid();
+my $opt_hostguid = randguid();
+my $opt_invocationid = randguid();
 
 sub randsid()
 {
@@ -44,7 +52,7 @@ sub randsid()
                       int(rand(10**8)), int(rand(10**8)), int(rand(10**8)));
 }
 
-my $domainsid = randsid();
+my $opt_domainsid = randsid();
 
 # generate a random password. Poor algorithm :(
 sub randpass()
@@ -58,6 +66,8 @@ sub randpass()
        return $pass;
 }
 
+my $joinpass = randpass();
+
 sub ldaptime()
 {
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =  gmtime(time);
@@ -76,7 +86,7 @@ sub substitute($)
        }
 
        if ($var eq "DOMAINSID") {
-               return $domainsid;
+               return $opt_domainsid;
        }
 
        if ($var eq "DOMAIN") {
@@ -87,14 +97,26 @@ sub substitute($)
                return $opt_realm;
        }
 
+       if ($var eq "DNSDOMAIN") {
+               return $dnsdomain;
+       }
+
        if ($var eq "HOSTNAME") {
                return $opt_hostname;
        }
 
+       if ($var eq "NETBIOSNAME") {
+               return $netbiosname;
+       }
+
        if ($var eq "DNSNAME") {
                return $dnsname;
        }
 
+       if ($var eq "HOSTIP") {
+               return $opt_hostip;
+       }
+
        if ($var eq "LDAPTIME") {
                return ldaptime();
        }
@@ -103,10 +125,38 @@ sub substitute($)
                return randguid();
        }
 
+       if ($var eq "NEWSCHEMAGUID") {
+               return randguid();
+       }
+
+       if ($var eq "DOMAINGUID") {
+               return $opt_domainguid;
+       }
+
+       if ($var eq "HOSTGUID") {
+               return $opt_hostguid;
+       }
+
+       if ($var eq "INVOCATIONID") {
+               return $opt_invocationid;
+       }
+
+       if ($var eq "DEFAULTSITE") {
+               return $defaultsite;
+       }
+
        if ($var eq "ADMINPASS") {
                return $opt_adminpass;
        }
 
+       if ($var eq "RANDPASS") {
+           return randpass();
+       }
+
+       if ($var eq "JOINPASS") {
+           return $joinpass;
+       }
+
        if ($var eq "NTTIME") {
                return "" . nttime();
        }
@@ -127,9 +177,32 @@ sub substitute($)
                return $opt_users;
        }
 
+       if ($var eq "USN") {
+               my $ret = $usn;
+               $usn = $ret + 1;
+               return $ret;
+       }
+
        die "ERROR: Uknown substitution variable $var\n";
 }
 
+
+####################################################################
+# substitute all variables in a string
+sub apply_substitutions($)
+{
+       my $data = shift;
+       my $res = "";
+       while ($data =~ /(.*?)\$\{(\w*)\}(.*)/s) {
+               my $sub = substitute($2);
+               $res .= "$1$sub";
+               $data = $3;
+       }
+       $res .= $data;
+       return $res;
+}
+
+
 #####################################################################
 # write a string into a file
 sub FileSave($$)
@@ -170,7 +243,6 @@ objectClass: top
 objectClass: foreignSecurityPrincipal
 cn: $sid
 description: $desc
-distinguishedName: CN=$sid,CN=ForeignSecurityPrincipals,\${BASEDN}
 instanceType: 4
 whenCreated: \${LDAPTIME}
 whenChanged: \${LDAPTIME}
@@ -194,14 +266,19 @@ sub ShowHelp()
 Samba4 provisioning
 
 provision.pl [options]
-  --realm     REALM        set realm
-  --domain    DOMAIN       set domain
-  --hostname  HOSTNAME     set hostname
-  --adminpass PASSWORD     choose admin password (otherwise random)
-  --nobody    USERNAME     choose 'nobody' user
-  --nogroup   GROUPNAME    choose 'nogroup' group
-  --wheel     GROUPNAME    choose 'wheel' privileged group
-  --users     GROUPNAME    choose 'users' group
+ --realm       REALM           set realm
+ --domain      DOMAIN          set domain
+ --domain-guid GUID            set domainguid (otherwise random)
+ --domain-sid  SID             set domainsid (otherwise random)
+ --host-name   HOSTNAME        set hostname
+ --host-ip     IPADDRESS       set ipaddress
+ --host-guid   GUID            set hostguid (otherwise random)
+ --invocationid        GUID            set invocationid (otherwise random)
+ --adminpass   PASSWORD        choose admin password (otherwise random)
+ --nobody      USERNAME        choose 'nobody' user
+ --nogroup     GROUPNAME       choose 'nogroup' group
+ --wheel       GROUPNAME       choose 'wheel' privileged group
+ --users       GROUPNAME       choose 'users' group
 
 You must provide at least a realm and domain
 
@@ -215,7 +292,12 @@ GetOptions(
            'help|h|?' => \$opt_help, 
            'realm=s' => \$opt_realm,
            'domain=s' => \$opt_domain,
-           'hostname=s' => \$opt_hostname,
+           'domain-guid=s' => \$opt_domainguid,
+           'domain-sid=s' => \$opt_domainsid,
+           'host-name=s' => \$opt_hostname,
+           'host-ip=s' => \$opt_hostip,
+           'host-guid=s' => \$opt_hostguid,
+           'invocationid=s' => \$opt_invocationid,
            'adminpass=s' => \$opt_adminpass,
            'nobody=s' => \$opt_nobody,
            'nogroup=s' => \$opt_nogroup,
@@ -230,7 +312,21 @@ if ($opt_help ||
        ShowHelp();
 }
 
-print "Provisioning host '$opt_hostname' for domain '$opt_domain' in realm '$opt_realm'\n";
+$opt_realm=uc($opt_realm);
+$opt_domain=uc($opt_domain);
+$opt_hostname=lc($opt_hostname);
+$netbiosname=uc($opt_hostname);
+
+if (!$opt_hostip) {
+       my $hip = gethostbyname($opt_hostname);
+       if (defined $hip) {
+               $opt_hostip = inet_ntoa($hip);
+       } else {
+               $opt_hostip = "<0.0.0.0>";
+       }
+}
+
+print "Provisioning host '$opt_hostname'[$opt_hostip] for domain '$opt_domain' in realm '$opt_realm'\n";
 
 if (!$opt_nobody) {
        if (defined getpwnam("nobody")) {
@@ -269,12 +365,15 @@ print "Using nobody='$opt_nobody'  nogroup='$opt_nogroup'  wheel='$opt_wheel'  u
 
 print "generating ldif ...\n";
 
-$dnsname = "$opt_hostname.$opt_realm";
+$dnsdomain = lc($opt_realm);
+$dnsname = lc($opt_hostname).".".$dnsdomain;
 $basedn = "DC=" . join(",DC=", split(/\./, $opt_realm));
 
 my $data = FileLoad("provision.ldif") || die "Unable to load provision.ldif\n";
 
 $data .= add_foreign("S-1-5-7", "Anonymous", "\${NOBODY}");
+$data .= add_foreign("S-1-1-0", "World", "\${NOGROUP}");
+$data .= add_foreign("S-1-5-2", "Network", "\${NOGROUP}");
 $data .= add_foreign("S-1-5-18", "System", "root");
 $data .= add_foreign("S-1-5-11", "Authenticated Users", "\${USERS}");
 
@@ -283,33 +382,62 @@ if (!$opt_adminpass) {
        print "chose random Administrator password '$opt_adminpass'\n";
 }
 
-my $res = "";
+# allow provisioning to be run from the source directory
+$ENV{"PATH"} .= ":bin";
+
 
-print "applying substitutions ...\n";
+my $res = apply_substitutions($data);
 
-while ($data =~ /(.*?)\$\{(\w*)\}(.*)/s) {
-       my $sub = substitute($2);
-       $res .= "$1$sub";
-       $data = $3;
-}
-$res .= $data;
+my $newdb = "newdb." . int(rand(1000));
 
-print "saving ldif to newsam.ldif ...\n";
+print "Putting new database files in $newdb\n";
 
-FileSave("newsam.ldif", $res);
+mkdir($newdb) || die "Unable to create temporary directory $newdb\n";
 
-unlink("newsam.ldb");
+FileSave("$newdb/sam.ldif", $res);
 
-print "creating newsam.ldb ...\n";
+print "creating $newdb/sam.ldb ...\n";
 
-# allow provisioning to be run from the source directory
-$ENV{"PATH"} .= ":bin";
+system("ldbadd -H $newdb/sam.ldb $newdb/sam.ldif") == 0 || die "Failed to create sam.ldb\n";
+
+$data = FileLoad("rootdse.ldif") || die "Unable to load rootdse.ldif\n";
+
+$res = apply_substitutions($data);
+
+FileSave("$newdb/rootdse.ldif", $res);
 
-system("ldbadd -H newsam.ldb newsam.ldif");
+print "creating $newdb/rootdse.ldb ...\n";
 
-print "done
+system("ldbadd -H $newdb/rootdse.ldb $newdb/rootdse.ldif") == 0 || die "Failed to create rootdse.ldb\n";
 
-Please move newsam.ldb to sam.ldb in the lib/private/ directory of your
-Samba4 installation
+$data = FileLoad("secrets.ldif") || die "Unable to load secrets.ldif\n";
+
+$res = apply_substitutions($data);
+
+FileSave("$newdb/secrets.ldif", $res);
+
+print "creating $newdb/secrets.ldb ...\n";
+
+system("ldbadd -H $newdb/secrets.ldb $newdb/secrets.ldif") == 0 || die "Failed to create secrets.ldb\n";
+
+$data = FileLoad("provision.zone") || die "Unable to load provision.zone\n";
+
+$res = apply_substitutions($data);
+
+print "saving dns zone to $newdb/dns.zone ...\n";
+
+FileSave("$dnsdomain.zone", $res);
+
+print "creating $newdb/hklm.ldb ... \n";
+
+system("ldbadd -H $newdb/hklm.ldb hklm.ldif") == 0 || die "Failed to create hklm.ldb\n";
+
+print "
+
+Installation:
+- Please move $newdb/*.ldb to the private/ directory of your
+  Samba4 installation
+- Please use $newdb/dnsdomain.zone in BIND on your dns server
 ";
 
+