Convert some more files to GPLv3.
[jra/samba/.git] / source4 / scripting / libjs / provision.js
index 3f3b1796886f33ee578a165b2b5f85842eefda50..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,10 +386,12 @@ 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";
        paths.dns_keytab = "dns.keytab";
        paths.dns_keytab_abs = lp.get("private dir") + "/" + paths.dns_keytab;
@@ -390,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";
@@ -463,9 +469,7 @@ function provision_fix_subobj(subobj, paths)
        subobj.REALM       = strupper(subobj.REALM);
        subobj.HOSTNAME    = strlower(subobj.HOSTNAME);
        subobj.DOMAIN      = strupper(subobj.DOMAIN);
-       assert(valid_netbios_name(subobj.DOMAIN));
        subobj.NETBIOSNAME = strupper(subobj.HOSTNAME);
-       assert(valid_netbios_name(subobj.NETBIOSNAME));
        subobj.DNSDOMAIN    = strlower(subobj.REALM);
        subobj.DNSNAME      = sprintf("%s.%s", 
                                      strlower(subobj.HOSTNAME), 
@@ -481,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;
@@ -524,10 +525,17 @@ 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 templates into " + paths.templates + "\n");
+       setup_ldb("provision_templates.ldif", info, paths.templates);
+
        /* Also wipes the database */
        message("Setting up " + paths.samdb + " partitions\n");
        setup_ldb("provision_partitions.ldif", info, paths.samdb);
@@ -548,9 +556,6 @@ function provision_become_dc(subobj, message, erase, paths, session_info)
        message("Setting up " + paths.samdb + " indexes\n");
        setup_add_ldif("provision_index.ldif", info, samdb, false);
 
-       message("Setting up " + paths.samdb + " templates\n");
-       setup_add_ldif("provision_templates.ldif", info, samdb, false);
-
        ok = samdb.transaction_commit();
        assert(ok);
 
@@ -564,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!
 */
@@ -572,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 {
@@ -623,6 +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 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);
@@ -646,45 +706,64 @@ 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");
-       var modify_ok = setup_ldb_modify("provision_basedn_modify.ldif", info, samdb);
-       if (!modify_ok) {
+       var modify_basedn_ok = setup_ldb_modify("provision_basedn_modify.ldif", info, samdb);
+       if (!modify_basedn_ok) {
                if (!add_ok) {
                        message("%s", "Failed to both add and modify " + subobj.DOMAINDN + " in target " + subobj.DOMAINDN_LDB + ": " + samdb.errstring() + "\n");
                        message("Perhaps you need to run the provision script with the --ldap-base-dn option, and add this record to the backend manually\n"); 
                };
-               assert(modify_ok);
+               assert(modify_basedn_ok);
        };
 
        message("Adding configuration container (permitted to fail)\n");
-       var add_ok = setup_add_ldif("provision_configuration_basedn.ldif", info, samdb, true);
+       var add_config_ok = setup_add_ldif("provision_configuration_basedn.ldif", info, samdb, true);
        message("Modifying configuration container\n");
-       var modify_ok = setup_ldb_modify("provision_configuration_basedn_modify.ldif", info, samdb);
-       if (!modify_ok) {
-               if (!add_ok) {
+       var modify_config_ok = setup_ldb_modify("provision_configuration_basedn_modify.ldif", info, samdb);
+       if (!modify_config_ok) {
+               if (!add_config_ok) {
                        message("%s", "Failed to both add and modify " + subobj.CONFIGDN + " in target " + subobj.CONFIGDN_LDB + ": " + samdb.errstring() + "\n");
                        message("Perhaps you need to run the provision script with the --ldap-base-dn option, and add this record to the backend manually\n"); 
-                       assert(modify_ok);
                }
-               assert(modify_ok);
+               assert(modify_config_ok);
        }
 
        message("Adding schema container (permitted to fail)\n");
-       var add_ok = setup_add_ldif("provision_schema_basedn.ldif", info, samdb, true);
+       var add_schema_ok = setup_add_ldif("provision_schema_basedn.ldif", info, samdb, true);
        message("Modifying schema container\n");
-       var modify_ok = setup_ldb_modify("provision_schema_basedn_modify.ldif", info, samdb);
-       if (!modify_ok) {
-               if (!add_ok) {
+       var modify_schema_ok = setup_ldb_modify("provision_schema_basedn_modify.ldif", info, samdb);
+       if (!modify_schema_ok) {
+               if (!add_schema_ok) {
                        message("%s", "Failed to both add and modify " + subobj.SCHEMADN + " in target " + subobj.SCHEMADN_LDB + ": " + samdb.errstring() + "\n");
                        message("Perhaps you need to run the provision script with the --ldap-base-dn option, and add this record to the backend manually\n"); 
-                       assert(modify_ok);
                }
                message("Failed to modify the schema container: " + samdb.errstring() + "\n");
-               assert(modify_ok);
+               assert(modify_schema_ok);
        }
 
        message("Setting up sam.ldb Samba4 schema\n");
@@ -692,45 +771,31 @@ 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);
 
        message("Setting up display specifiers\n");
        setup_add_ldif("display_specifiers.ldif", info, samdb, false);
-       message("Setting up sam.ldb templates\n");
-       setup_add_ldif("provision_templates.ldif", info, samdb, false);
 
        message("Adding users container (permitted to fail)\n");
-       var add_ok = setup_add_ldif("provision_users_add.ldif", info, samdb, true);
+       var add_users_ok = setup_add_ldif("provision_users_add.ldif", info, samdb, true);
        message("Modifying users container\n");
-       var modify_ok = setup_ldb_modify("provision_users_modify.ldif", info, samdb);
-       if (!modify_ok) {
-               if (!add_ok) {
+       var modify_users_ok = setup_ldb_modify("provision_users_modify.ldif", info, samdb);
+       if (!modify_users_ok) {
+               if (!add_users_ok) {
                        message("Failed to both add and modify the users container\n");
-                       assert(modify_ok);
                }
-               assert(modify_ok);
+               assert(modify_users_ok);
        }
        message("Adding computers container (permitted to fail)\n");
-       var add_ok = setup_add_ldif("provision_computers_add.ldif", info, samdb, true);
+       var add_computers_ok = setup_add_ldif("provision_computers_add.ldif", info, samdb, true);
        message("Modifying computers container\n");
-       var modify_ok = setup_ldb_modify("provision_computers_modify.ldif", info, samdb);
-       if (!modify_ok) {
-               if (!add_ok) {
+       var modify_computers_ok = setup_ldb_modify("provision_computers_modify.ldif", info, samdb);
+       if (!modify_computers_ok) {
+               if (!add_computers_ok) {
                        message("Failed to both add and modify the computers container\n");
-                       assert(modify_ok);
                }
-               assert(modify_ok);
+               assert(modify_computers_ok);
        }
 
        message("Setting up sam.ldb data\n");
@@ -828,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");
@@ -896,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
@@ -948,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);
@@ -956,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");
@@ -977,32 +1016,33 @@ function provision_guess()
        //   module when expanding the objectclass list)
        // - partition must be last
        // - each partition has its own module list then
-       modules_list        = new Array("rootdse",
-                                       "paged_results",
-                                       "server_sort",
-                                       "extended_dn",
-                                       "asq",
-                                       "samldb",
-                                       "rdn_name",
-                                       "objectclass",
-                                       "kludge_acl",
-                                       "operational",
-                                       "subtree_rename",
-                                       "linked_attributes",
-                                       "show_deleted",
-                                       "partition");
+       var modules_list     = new Array("rootdse",
+                                        "paged_results",
+                                        "ranged_results",
+                                        "anr",
+                                        "server_sort",
+                                        "extended_dn",
+                                        "asq",
+                                        "samldb",
+                                        "rdn_name",
+                                        "objectclass",
+                                        "kludge_acl",
+                                        "operational");
+       var tdb_modules_list = new Array("subtree_rename",
+                                        "subtree_delete",
+                                        "linked_attributes");
+       var modules_list2    = new Array("show_deleted",
+                                        "partition");
        subobj.MODULES_LIST = join(",", modules_list);
+       subobj.TDB_MODULES_LIST = "," + join(",", tdb_modules_list);
+       subobj.MODULES_LIST2 = join(",", modules_list2);
        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;
 }