Convert some more files to GPLv3.
[jra/samba/.git] / source4 / scripting / libjs / provision.js
index 2dfc941a66e886488f3df63391c4a84d02b60400..51e2785762bd79a29c2beba5b478f08066f80a05 100644 (file)
@@ -1,7 +1,7 @@
 /*
        backend code for provisioning a Samba4 server
        Copyright Andrew Tridgell 2005
-       Released under the GNU GPL v2 or later
+       Released under the GNU GPL version 3 or later
 */
 
 sys = sys_init();
@@ -170,7 +170,7 @@ function ldb_erase(info, ldb)
        /* and the rest */
        attrs = new Array("dn");
        var basedn = "";
-       var res = ldb.search("(&(|(objectclass=*)(dn=*))(!(dn=@BASEINFO)))", basedn, ldb.SCOPE_SUBTREE, attrs);
+       var res = ldb.search("(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))", basedn, ldb.SCOPE_SUBTREE, attrs);
        var i;
        if (res.error != 0) {
                ldb_delete(info, ldb);
@@ -180,7 +180,7 @@ function ldb_erase(info, ldb)
                ldb.del(res.msgs[i].dn);
        }
 
-       var res = ldb.search("(&(|(objectclass=*)(dn=*))(!(dn=@BASEINFO)))", basedn, ldb.SCOPE_SUBTREE, attrs);
+       var res = ldb.search("(&(|(objectclass=*)(distinguishedName=*))(!(distinguishedName=@BASEINFO)))", basedn, ldb.SCOPE_SUBTREE, attrs);
        if (res.error != 0 || res.msgs.length != 0) {
                ldb_delete(info, ldb);
                return;
@@ -198,14 +198,17 @@ function ldb_erase_partitions(info, ldb, ldapbackend)
        var j;
 
        var res = ldb.search("(objectClass=*)", "", ldb.SCOPE_BASE, rootDSE_attrs);
-       assert(res.error == 0);
+       if (res.error != 0) {
+               info.message("rootdse search failed: " + res.errstr + "\n");
+               assert(res.error == 0);
+       }
        assert(res.msgs.length == 1);
        if (typeof(res.msgs[0].namingContexts) == "undefined") {
                return;
        }       
        for (j=0; j<res.msgs[0].namingContexts.length; j++) {
-               var anything = "(|(objectclass=*)(dn=*))";
-               var attrs = new Array("dn");
+               var anything = "(|(objectclass=*)(distinguishedName=*))";
+               var attrs = new Array("distinguishedName");
                var basedn = res.msgs[0].namingContexts[j];
                var k;
                var previous_remaining = 1;
@@ -221,8 +224,12 @@ function ldb_erase_partitions(info, ldb, ldapbackend)
                        var res2 = ldb.search(anything, basedn, ldb.SCOPE_SUBTREE, attrs);
                        var i;
                        if (res2.error != 0) {
-                               info.message("ldb search failed: " + res.errstr + "\n");
-                               continue;
+                               if (res2.error == 32) {
+                                       break;
+                               } else {
+                                       info.message("ldb search(2) failed: " + res2.errstr + "\n");
+                                       continue;
+                               }
                        }
                        previous_remaining = current_remaining;
                        current_remaining = res2.msgs.length;
@@ -232,7 +239,7 @@ function ldb_erase_partitions(info, ldb, ldapbackend)
                        
                        var res3 = ldb.search(anything, basedn, ldb.SCOPE_SUBTREE, attrs);
                        if (res3.error != 0) {
-                               info.message("ldb search failed: " + res.errstr + "\n");
+                               info.message("ldb search(3) failed: " + res3.errstr + "\n");
                                continue;
                        }
                        if (res3.msgs.length != 0) {
@@ -379,9 +386,10 @@ function provision_default_paths(subobj)
        var dnsdomain = strlower(subobj.REALM);
        var lp = loadparm_init();
        var paths = new Object();
-       paths.smbconf = lp.get("config file");
+       paths.smbconf = lp.filename()
        paths.shareconf = lp.get("private dir") + "/" + "share.ldb";
        paths.samdb = lp.get("sam database");
+       paths.idmapdb = lp.get("idmap database");
        paths.secrets = lp.get("secrets database");
        paths.templates = lp.get("private dir") + "/" + "templates.ldb";
        paths.keytab = "secrets.keytab";
@@ -391,9 +399,6 @@ function provision_default_paths(subobj)
        paths.named_conf = lp.get("private dir") + "/named.conf";
        paths.winsdb = "wins.ldb";
        paths.ldapdir = lp.get("private dir") + "/ldap";
-       paths.ldap_basedn_ldif = paths.ldapdir + "/" + dnsdomain + ".ldif";
-       paths.ldap_config_basedn_ldif = paths.ldapdir + "/" + dnsdomain + "-config.ldif";
-       paths.ldap_schema_basedn_ldif = paths.ldapdir + "/" + dnsdomain + "-schema.ldif";
 
        paths.s4_ldapi_socket = lp.get("private dir") + "/ldapi";
        paths.phpldapadminconfig = lp.get("private dir") + "/phpldapadmin-config.php";
@@ -480,9 +485,6 @@ function provision_fix_subobj(subobj, paths)
        subobj.ADMINPASS_B64   = ldb.encode(subobj.ADMINPASS);
        subobj.DNSPASS_B64     = ldb.encode(subobj.DNSPASS);
 
-       var rdns = split(",", subobj.DOMAINDN);
-       subobj.RDN_DC = substr(rdns[0], strlen("DC="));
-
        subobj.SAM_LDB          = "tdb://" + paths.samdb;
        subobj.SECRETS_KEYTAB   = paths.keytab;
        subobj.DNS_KEYTAB       = paths.dns_keytab;
@@ -523,11 +525,15 @@ function provision_become_dc(subobj, message, erase, paths, session_info)
        var ok = provision_fix_subobj(subobj, paths);
        assert(ok);
 
+       if (subobj.BACKEND_MOD == undefined) {
+               subobj.BACKEND_MOD = "repl_meta_data";
+       }
+
        info.subobj = subobj;
        info.message = message;
        info.session_info = session_info;
 
-       message("Setting up teplates into " + paths.templates + "\n");
+       message("Setting up templates into " + paths.templates + "\n");
        setup_ldb("provision_templates.ldif", info, paths.templates);
 
        /* Also wipes the database */
@@ -563,6 +569,44 @@ function provision_become_dc(subobj, message, erase, paths, session_info)
        return true;
 }
 
+function load_schema(subobj, message, samdb)
+{
+       var lp = loadparm_init();
+       var src = lp.get("setup directory") + "/" + "schema.ldif";
+
+       if (! sys.stat(src)) {
+               message("Template file not found: %s\n",src);
+               assert(0);
+       }
+
+       var schema_data = sys.file_load(src);
+
+       src = lp.get("setup directory") + "/" + "schema_samba4.ldif";
+
+       if (! sys.stat(src)) {
+               message("Template file not found: %s\n",src);
+               assert(0);
+       }
+
+       schema_data = schema_data + sys.file_load(src);
+
+       schema_data = substitute_var(schema_data, subobj);
+
+       src = lp.get("setup directory") + "/" + "provision_schema_basedn_modify.ldif";
+
+       if (! sys.stat(src)) {
+               message("Template file not found: %s\n",src);
+               assert(0);
+       }
+
+       var head_data = sys.file_load(src);
+       head_data = substitute_var(head_data, subobj);
+
+       var ok = samdb.attach_dsdb_schema_from_ldif(head_data, schema_data);
+       return ok;
+}
+
+
 /*
   provision samba4 - caution, this wipes all existing data!
 */
@@ -571,10 +615,21 @@ function provision(subobj, message, blank, paths, session_info, credentials, lda
        var lp = loadparm_init();
        var sys = sys_init();
        var info = new Object();
+       random_init(local);
 
        var ok = provision_fix_subobj(subobj, paths);
        assert(ok);
 
+       if (strlower(subobj.SERVERROLE) == strlower("domain controller")) {
+               if (subobj.BACKEND_MOD == undefined) {
+                       subobj.BACKEND_MOD = "repl_meta_data";
+               }
+       } else {
+               if (subobj.BACKEND_MOD == undefined) {
+                       subobj.BACKEND_MOD = "objectguid";
+               }
+       }
+
        if (subobj.DOMAINGUID != undefined) {
                subobj.DOMAINGUID_MOD = sprintf("replace: objectGUID\nobjectGUID: %s\n-", subobj.DOMAINGUID);
        } else {
@@ -622,9 +677,12 @@ function provision(subobj, message, blank, paths, session_info, credentials, lda
        var reg = reg_open();
        reg.apply_patchfile(lp.get("setup directory") + "/provision.reg")
 
-       message("Setting up teplates into " + paths.templates + "\n");
+       message("Setting up templates into " + paths.templates + "\n");
        setup_ldb("provision_templates.ldif", info, paths.templates);
 
+       message("Setting up " + paths.idmapdb +"\n");
+       setup_ldb("idmap_init.ldif", info, paths.idmapdb);
+
        message("Setting up sam.ldb partitions\n");
        /* Also wipes the database */
        setup_ldb("provision_partitions.ldif", info, paths.samdb);
@@ -648,8 +706,29 @@ function provision(subobj, message, blank, paths, session_info, credentials, lda
        }
        samdb.close();
 
+       message("Pre-loading the Samba4 and AD schema\n");
+       
        samdb = open_ldb(info, paths.samdb, false);
 
+       samdb.set_domain_sid(subobj.DOMAINSID);
+
+       if (strlower(subobj.SERVERROLE) == strlower("domain controller")) {
+               if (subobj.INVOCATIONID == undefined) {
+                       subobj.INVOCATIONID = randguid();
+               }
+               samdb.set_ntds_invocationId(subobj.INVOCATIONID);
+               if (subobj.BACKEND_MOD == undefined) {
+                       subobj.BACKEND_MOD = "repl_meta_data";
+               }
+       } else {
+               if (subobj.BACKEND_MOD == undefined) {
+                       subobj.BACKEND_MOD = "objectguid";
+               }
+       }
+
+       var load_schema_ok = load_schema(subobj, message, samdb);
+       assert(load_schema_ok.is_ok);
+
        message("Adding DomainDN: " + subobj.DOMAINDN + " (permitted to fail)\n");
        var add_ok = setup_add_ldif("provision_basedn.ldif", info, samdb, true);
        message("Modifying DomainDN: " + subobj.DOMAINDN + "\n");
@@ -692,16 +771,6 @@ function provision(subobj, message, blank, paths, session_info, credentials, lda
        message("Setting up sam.ldb AD schema\n");
        setup_add_ldif("schema.ldif", info, samdb, false);
 
-       // (hack) Reload, now we have the schema loaded.  
-       var commit_ok = samdb.transaction_commit();
-       if (!commit_ok) {
-               info.message("samdb commit failed: " + samdb.errstring() + "\n");
-               assert(commit_ok);
-       }
-       samdb.close();
-
-       samdb = open_ldb(info, paths.samdb, false);
-
        message("Setting up sam.ldb configuration data\n");
        setup_add_ldif("provision_configuration.ldif", info, samdb, false);
 
@@ -824,6 +893,12 @@ function provision_schema(subobj, message, tmp_schema_path, paths)
        /* This will erase anything in the tmp db */
        var samdb = open_ldb(info, tmp_schema_path, true);
 
+       message("Setting up sam.ldb attributes\n");
+       setup_add_ldif("provision_init.ldif", info, samdb, false);
+
+       message("Setting up sam.ldb rootDSE\n");
+       setup_add_ldif("provision_rootdse_add.ldif", info, samdb, false);
+
        message("Adding schema container (permitted to fail)\n");
        var add_ok = setup_add_ldif("provision_schema_basedn.ldif", info, samdb, true);
        message("Modifying schema container\n");
@@ -892,34 +967,6 @@ function provision_dns(subobj, message, paths, session_info, credentials)
        message("Please install the zone located in " + paths.dns + " into your DNS server.  A sample BIND configuration snippit is at " + paths.named_conf + "\n");
 }
 
-/* Write out a DNS zone file, from the info in the current database */
-function provision_ldapbase(subobj, message, paths)
-{
-       var ok = provision_fix_subobj(subobj, paths);
-       assert(ok);
-
-       message("Setting up LDAP base entry: " + subobj.DOMAINDN + " \n");
-       var rdns = split(",", subobj.DOMAINDN);
-       subobj.EXTENSIBLEOBJECT = "objectClass: extensibleObject";
-
-       subobj.RDN_DC = substr(rdns[0], strlen("DC="));
-
-       sys.mkdir(paths.ldapdir, 0700);
-
-       setup_file("provision_basedn.ldif", 
-                  message, paths.ldap_basedn_ldif, 
-                  subobj);
-
-       setup_file("provision_configuration_basedn.ldif", 
-                  message, paths.ldap_config_basedn_ldif, 
-                  subobj);
-
-       setup_file("provision_schema_basedn.ldif", 
-                  message, paths.ldap_schema_basedn_ldif, 
-                  subobj);
-
-}
-
 
 /*
   guess reasonably default options for provisioning
@@ -944,7 +991,6 @@ function provision_guess()
        subobj.VERSION      = version();
        subobj.HOSTIP       = hostip();
        subobj.DOMAINSID    = randsid();
-       subobj.INVOCATIONID = randguid();
        subobj.POLICYGUID   = randguid();
        subobj.KRBTGTPASS   = randpass(12);
        subobj.MACHINEPASS  = randpass(12);
@@ -952,9 +998,6 @@ function provision_guess()
        subobj.ADMINPASS    = randpass(12);
        subobj.LDAPMANAGERPASS     = randpass(12);
        subobj.DEFAULTSITE  = "Default-First-Site-Name";
-       subobj.NEWGUID      = randguid;
-       subobj.NTTIME       = nttime;
-       subobj.LDAPTIME     = ldaptime;
        subobj.DATESTRING   = datestring;
        subobj.ROOT         = findnss(nss.getpwnam, "root");
        subobj.NOBODY       = findnss(nss.getpwnam, "nobody");
@@ -976,6 +1019,7 @@ function provision_guess()
        var modules_list     = new Array("rootdse",
                                         "paged_results",
                                         "ranged_results",
+                                        "anr",
                                         "server_sort",
                                         "extended_dn",
                                         "asq",
@@ -995,14 +1039,10 @@ function provision_guess()
        subobj.DOMAINDN_LDB = "users.ldb";
        subobj.CONFIGDN_LDB = "configuration.ldb";
        subobj.SCHEMADN_LDB = "schema.ldb";
-       subobj.DOMAINDN_MOD = "pdc_fsmo,password_hash";
-       subobj.CONFIGDN_MOD = "naming_fsmo";
-       subobj.SCHEMADN_MOD = "schema_fsmo";
-       subobj.DOMAINDN_MOD2 = ",objectguid";
-       subobj.CONFIGDN_MOD2 = ",objectguid";
-       subobj.SCHEMADN_MOD2 = ",objectguid";
-
-       subobj.EXTENSIBLEOBJECT = "# no objectClass: extensibleObject for local ldb";
+       subobj.DOMAINDN_MOD = "pdc_fsmo,password_hash,instancetype";
+       subobj.CONFIGDN_MOD = "naming_fsmo,instancetype";
+       subobj.SCHEMADN_MOD = "schema_fsmo,instancetype";
+
        subobj.ACI              = "# no aci for local ldb";
 
        return subobj;
@@ -1088,7 +1128,7 @@ unixName: %s
 sambaPassword: %s
 objectClass: user
 ",
-                          user_dn, username, dom_users,
+                          user_dn, username,
                           unixname, password);
        /*
          add the user to the users group as well
@@ -1098,7 +1138,7 @@ dn: %s
 changetype: modify
 add: member
 member: %s
-", 
+",
                               dom_users, user_dn);
 
 
@@ -1134,7 +1174,7 @@ member: %s
 // crh has a paragraph on this in his book (1.4.1.1)
 function valid_netbios_name(name)
 {
-       if (strlen(name) > 13) return false;
+       if (strlen(name) > 15) return false;
        return true;
 }