2 backend code for provisioning a Samba4 server
3 Copyright Andrew Tridgell 2005
4 Released under the GNU GPL v2 or later
10 return true if the current install seems to be OK
12 function install_ok(session_info, credentials)
14 var lp = loadparm_init();
16 ldb.session_info = session_info;
17 ldb.credentials = credentials;
18 if (lp.get("realm") == "") {
21 var ok = ldb.connect(lp.get("sam database"));
25 var res = ldb.search("(cn=Administrator)");
26 if (res.length != 1) {
33 find a user or group from a list of possibilities
38 assert(arguments.length >= 2);
39 var nssfn = arguments[0];
40 for (i=1;i<arguments.length;i++) {
41 if (nssfn(arguments[i]) != undefined) {
45 printf("Unable to find user/group for %s\n", arguments[1]);
46 assert(i<arguments.length);
50 add a foreign security principle
52 function add_foreign(ldb, subobj, sid, desc)
55 dn: CN=%s,CN=ForeignSecurityPrincipals,%s
57 objectClass: foreignSecurityPrincipal
60 sid, subobj.BASEDN, desc);
61 /* deliberately ignore errors from this, as the records may
68 setup a mapping between a sam name and a unix name
70 function setup_name_mapping(info, ldb, sid, unixname)
72 var attrs = new Array("dn");
73 var res = ldb.search(sprintf("objectSid=%s", sid),
74 info.subobj.BASEDN, ldb.SCOPE_SUBTREE, attrs);
75 if (res.length != 1) {
76 info.message("Failed to find record for objectSid %s\n", sid);
86 var ok = ldb.modify(mod);
88 info.message("name mapping for %s failed - %s\n",
89 sid, ldb.errstring());
96 return current time as a nt time string
100 return "" + sys.nttime();
104 return current time as a ldap time string
108 return sys.ldaptime(sys.nttime());
112 return a date string suitable for a dns zone serial number
114 function datestring()
116 var t = sys.ntgmtime(sys.nttime());
117 return sprintf("%04u%02u%02u%02u",
118 t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour);
126 var list = sys.interfaces();
132 return first part of hostname
136 var s = split(".", sys.hostname());
140 /* the ldb is in bad shape, possibly due to being built from an
141 incompatible previous version of the code, so delete it
143 function ldb_delete(ldb)
145 println("Deleting " + ldb.filename);
146 var lp = loadparm_init();
147 sys.unlink(sprintf("%s/%s", lp.get("private dir"), ldb.filename));
148 ldb.transaction_cancel();
150 var ok = ldb.connect(ldb.filename);
151 ldb.transaction_start();
156 erase an ldb, removing all records
158 function ldb_erase(ldb)
162 /* delete the specials */
163 ldb.del("@INDEXLIST");
164 ldb.del("@ATTRIBUTES");
165 ldb.del("@SUBCLASSES");
167 ldb.del("@PARTITION");
168 ldb.del("@KLUDGEACL");
171 attrs = new Array("dn");
173 var res = ldb.search("(&(|(objectclass=*)(dn=*))(!(dn=@BASEINFO)))", basedn, ldb.SCOPE_SUBTREE, attrs);
175 if (typeof(res) == "undefined") {
179 for (i=0;i<res.length;i++) {
184 var res = ldb.search("(&(|(objectclass=*)(dn=*))(!(dn=@BASEINFO)))", basedn, ldb.SCOPE_SUBTREE, attrs);
185 if (res.length != 0) {
189 assert(res.length == 0);
193 erase an ldb, removing all records
195 function ldb_erase_partitions(info, ldb)
197 var rootDSE_attrs = new Array("namingContexts");
198 var lp = loadparm_init();
201 var res = ldb.search("(objectClass=*)", "", ldb.SCOPE_BASE, rootDSE_attrs);
202 assert(typeof(res) != "undefined");
203 assert(res.length == 1);
204 if (typeof(res[0].namingContexts) == "undefined") {
207 for (j=0; j<res[0].namingContexts.length; j++) {
208 var attrs = new Array("dn");
209 var basedn = res[0].namingContexts[j];
211 var previous_remaining = 1;
212 var current_remaining = 0;
214 for (k=0; k < 10 && (previous_remaining != current_remaining); k++) {
216 var res2 = ldb.search("(|(objectclass=*)(dn=*))", basedn, ldb.SCOPE_SUBTREE, attrs);
218 if (typeof(res2) == "undefined") {
219 info.message("ldb search failed: " + ldb.errstring() + "\n");
222 previous_remaining = current_remaining;
223 current_remaining = res2.length;
224 for (i=0;i<res2.length;i++) {
228 var res3 = ldb.search("(|(objectclass=*)(dn=*))", basedn, ldb.SCOPE_SUBTREE, attrs);
229 if (typeof(res3) == "undefined") {
230 info.message("ldb search failed: " + ldb.errstring() + "\n");
233 if (res3.length != 0) {
234 info.message("Failed to delete all records under " + basedn + ", " + res3.length + " records remaining\n");
240 function open_ldb(info, dbname, erase)
242 var ldb = ldb_init();
243 ldb.session_info = info.session_info;
244 ldb.credentials = info.credentials;
245 ldb.filename = dbname;
247 var connect_ok = ldb.connect(dbname);
249 var lp = loadparm_init();
250 sys.unlink(sprintf("%s/%s", lp.get("private dir"), dbname));
251 connect_ok = ldb.connect(dbname);
255 ldb.transaction_start();
265 setup a ldb in the private dir
267 function setup_add_ldif(ldif, info, ldb, failok)
269 var lp = loadparm_init();
270 var src = lp.get("setup directory") + "/" + ldif;
272 var data = sys.file_load(src);
273 data = substitute_var(data, info.subobj);
275 var add_ok = ldb.add(data);
277 info.message("ldb load failed: " + ldb.errstring() + "\n");
285 function setup_ldb(ldif, info, dbname)
290 if (arguments.length >= 4) {
291 erase = arguments[3];
293 if (arguments.length == 5) {
294 failok = arguments[4];
296 var ldb = open_ldb(info, dbname, erase);
297 if (setup_add_ldif(ldif, info, ldb, failok)) {
298 var commit_ok = ldb.transaction_commit();
300 info.message("ldb commit failed: " + ldb.errstring() + "\n");
307 setup a ldb in the private dir
309 function setup_ldb_modify(ldif, info, ldb)
311 var lp = loadparm_init();
313 var src = lp.get("setup directory") + "/" + ldif;
315 var data = sys.file_load(src);
316 data = substitute_var(data, info.subobj);
318 var mod_ok = ldb.modify(data);
320 info.message("ldb load failed: " + ldb.errstring() + "\n");
327 setup a file in the private dir
329 function setup_file(template, message, fname, subobj)
331 var lp = loadparm_init();
333 var src = lp.get("setup directory") + "/" + template;
337 var data = sys.file_load(src);
338 data = substitute_var(data, subobj);
340 ok = sys.file_save(f, data);
342 message("failed to create file: " + f + "\n");
347 function provision_default_paths(subobj)
349 var lp = loadparm_init();
350 var paths = new Object();
351 paths.smbconf = lp.get("config file");
352 paths.shareconf = lp.get("private dir") + "/" + "share.ldb";
353 paths.hklm = "hklm.ldb";
354 paths.hkcu = "hkcu.ldb";
355 paths.hkcr = "hkcr.ldb";
356 paths.hku = "hku.ldb";
357 paths.hkpd = "hkpd.ldb";
358 paths.hkpt = "hkpt.ldb";
359 paths.samdb = lp.get("sam database");
360 paths.secrets = "secrets.ldb";
361 paths.dns = lp.get("private dir") + "/" + subobj.DNSDOMAIN + ".zone";
362 paths.winsdb = "wins.ldb";
363 paths.ldap_basedn_ldif = lp.get("private dir") + "/" + subobj.DNSDOMAIN + ".ldif";
369 setup reasonable name mappings for sam names to unix names
371 function setup_name_mappings(info, ldb)
373 var lp = loadparm_init();
374 var attrs = new Array("objectSid");
375 var subobj = info.subobj;
377 res = ldb.search("objectSid=*", subobj.BASEDN, ldb.SCOPE_BASE, attrs);
378 assert(res.length == 1 && res[0].objectSid != undefined);
379 var sid = res[0].objectSid;
381 /* add some foreign sids if they are not present already */
382 add_foreign(ldb, subobj, "S-1-5-7", "Anonymous");
383 add_foreign(ldb, subobj, "S-1-1-0", "World");
384 add_foreign(ldb, subobj, "S-1-5-2", "Network");
385 add_foreign(ldb, subobj, "S-1-5-18", "System");
386 add_foreign(ldb, subobj, "S-1-5-11", "Authenticated Users");
388 /* some well known sids */
389 setup_name_mapping(info, ldb, "S-1-5-7", subobj.NOBODY);
390 setup_name_mapping(info, ldb, "S-1-1-0", subobj.NOGROUP);
391 setup_name_mapping(info, ldb, "S-1-5-2", subobj.NOGROUP);
392 setup_name_mapping(info, ldb, "S-1-5-18", subobj.ROOT);
393 setup_name_mapping(info, ldb, "S-1-5-11", subobj.USERS);
394 setup_name_mapping(info, ldb, "S-1-5-32-544", subobj.WHEEL);
395 setup_name_mapping(info, ldb, "S-1-5-32-545", subobj.USERS);
396 setup_name_mapping(info, ldb, "S-1-5-32-546", subobj.NOGROUP);
397 setup_name_mapping(info, ldb, "S-1-5-32-551", subobj.BACKUP);
399 /* and some well known domain rids */
400 setup_name_mapping(info, ldb, sid + "-500", subobj.ROOT);
401 setup_name_mapping(info, ldb, sid + "-518", subobj.WHEEL);
402 setup_name_mapping(info, ldb, sid + "-519", subobj.WHEEL);
403 setup_name_mapping(info, ldb, sid + "-512", subobj.WHEEL);
404 setup_name_mapping(info, ldb, sid + "-513", subobj.USERS);
405 setup_name_mapping(info, ldb, sid + "-520", subobj.WHEEL);
412 provision samba4 - caution, this wipes all existing data!
414 function provision(subobj, message, blank, paths, session_info, credentials)
416 var lp = loadparm_init();
417 var sys = sys_init();
418 var info = new Object();
421 some options need to be upper/lower case
423 subobj.REALM = strupper(subobj.REALM);
424 subobj.HOSTNAME = strlower(subobj.HOSTNAME);
425 subobj.DOMAIN = strupper(subobj.DOMAIN);
426 assert(valid_netbios_name(subobj.DOMAIN));
427 subobj.NETBIOSNAME = strupper(subobj.HOSTNAME);
428 assert(valid_netbios_name(subobj.NETBIOSNAME));
429 var rdns = split(",", subobj.BASEDN);
430 subobj.RDN_DC = substr(rdns[0], strlen("DC="));
432 if (subobj.DOMAINGUID != undefined) {
433 subobj.DOMAINGUID_MOD = sprintf("replace: objectGUID\nobjectGUID: %s\n-", subobj.DOMAINGUID);
435 subobj.DOMAINGUID_MOD = "";
438 if (subobj.HOSTGUID != undefined) {
439 subobj.HOSTGUID_ADD = sprintf("objectGUID: %s", subobj.HOSTGUID);
441 subobj.HOSTGUID_ADD = "";
444 info.subobj = subobj;
445 info.message = message;
446 info.credentials = credentials;
447 info.session_info = session_info;
449 /* only install a new smb.conf if there isn't one there already */
450 var st = sys.stat(paths.smbconf);
451 if (st == undefined) {
452 message("Setting up smb.conf\n");
453 setup_file("provision.smb.conf", info.message, paths.smbconf, subobj);
456 /* only install a new shares config db if there is none */
457 st = sys.stat(paths.shareconf);
458 if (st == undefined) {
459 message("Setting up sconf.ldb\n");
460 setup_ldb("share.ldif", info, paths.shareconf);
462 message("Setting up secrets.ldb\n");
463 setup_ldb("secrets.ldif", info, paths.secrets);
464 message("Setting up keytabs\n");
465 var keytab_ok = credentials_update_all_keytabs();
467 message("Setting up hklm.ldb\n");
468 setup_ldb("hklm.ldif", info, paths.hklm);
470 message("Setting up sam.ldb partitions\n");
471 /* Also wipes the database */
472 setup_ldb("provision_partitions.ldif", info, paths.samdb);
474 var samdb = open_ldb(info, paths.samdb, false);
476 message("Setting up sam.ldb attributes\n");
477 setup_add_ldif("provision_init.ldif", info, samdb, false);
478 message("Erasing data from partitions\n");
479 ldb_erase_partitions(info, samdb);
481 message("Adding baseDN: " + subobj.BASEDN + " (permitted to fail)\n");
482 var add_ok = setup_add_ldif("provision_basedn.ldif", info, samdb, true);
483 message("Modifying baseDN: " + subobj.BASEDN + "\n");
484 var modify_ok = setup_ldb_modify("provision_basedn_modify.ldif", info, samdb);
487 message("Failed to both add and modify " + subobj.BASEDN + " in target " + subobj.LDAPBACKEND + "\n");
488 message("Perhaps you need to run the provision script with the --ldap-base-dn option, and add this record to the backend manually\n");
493 message("Setting up sam.ldb Samba4 schema\n");
494 setup_add_ldif("schema_samba4.ldif", info, samdb, false);
495 message("Setting up sam.ldb AD schema\n");
496 setup_add_ldif("schema.ldif", info, samdb, false);
498 // (hack) Reload, now we have the schema loaded.
499 var commit_ok = samdb.transaction_commit();
501 info.message("samdb commit failed: " + samdb.errstring() + "\n");
506 samdb = open_ldb(info, paths.samdb, false);
508 message("Setting up display specifiers\n");
509 setup_add_ldif("display_specifiers.ldif", info, samdb, false);
510 message("Setting up sam.ldb templates\n");
511 setup_add_ldif("provision_templates.ldif", info, samdb, false);
512 message("Setting up sam.ldb data\n");
513 setup_add_ldif("provision.ldif", info, samdb, false);
515 if (blank != false) {
516 message("Setting up sam.ldb index\n");
517 setup_add_ldif("provision_index.ldif", info, samdb, false);
519 var commit_ok = samdb.transaction_commit();
521 info.message("ldb commit failed: " + samdb.errstring() + "\n");
527 message("Setting up sam.ldb users and groups\n");
528 setup_add_ldif("provision_users.ldif", info, samdb, false);
530 if (setup_name_mappings(info, samdb) == false) {
534 message("Setting up sam.ldb index\n");
535 setup_add_ldif("provision_index.ldif", info, samdb, false);
537 var commit_ok = samdb.transaction_commit();
539 info.message("samdb commit failed: " + samdb.errstring() + "\n");
546 /* Write out a DNS zone file, from the info in the current database */
547 function provision_dns(subobj, message, paths, session_info, credentials)
549 message("Setting up DNS zone: " + subobj.DNSDOMAIN + " \n");
550 var ldb = ldb_init();
551 ldb.session_info = session_info;
552 ldb.credentials = credentials;
554 /* connect to the sam */
555 var ok = ldb.connect(paths.samdb);
558 /* These values may have changed, due to an incoming SamSync,
559 or may not have been specified, so fetch them from the database */
561 var attrs = new Array("objectGUID");
562 res = ldb.search("objectGUID=*", subobj.BASEDN, ldb.SCOPE_BASE, attrs);
563 assert(res.length == 1);
564 assert(res[0].objectGUID != undefined);
565 subobj.DOMAINGUID = res[0].objectGUID;
567 subobj.HOSTGUID = searchone(ldb, subobj.BASEDN, "(&(objectClass=computer)(cn=" + subobj.NETBIOSNAME + "))", "objectGUID");
568 assert(subobj.HOSTGUID != undefined);
570 setup_file("provision.zone",
574 message("Please install the zone located in " + paths.dns + " into your DNS server\n");
577 /* Write out a DNS zone file, from the info in the current database */
578 function provision_ldapbase(subobj, message, paths)
580 message("Setting up LDAP base entry: " + subobj.BASEDN + " \n");
581 var rdns = split(",", subobj.BASEDN);
582 subobj.EXTENSIBLEOBJECT = "objectClass: extensibleObject";
584 subobj.RDN_DC = substr(rdns[0], strlen("DC="));
586 setup_file("provision_basedn.ldif",
587 message, paths.ldap_basedn_ldif,
590 message("Please install the LDIF located in " + paths.ldap_basedn_ldif + " into your LDAP server, and re-run with --ldap-backend=ldap://my.ldap.server\n");
595 guess reasonably default options for provisioning
597 function provision_guess()
599 var subobj = new Object();
600 var nss = nss_init();
601 var lp = loadparm_init();
605 subobj.REALM = strupper(lp.get("realm"));
606 subobj.DOMAIN = lp.get("workgroup");
607 subobj.HOSTNAME = hostname();
609 assert(subobj.REALM);
610 assert(subobj.DOMAIN);
611 assert(subobj.HOSTNAME);
613 subobj.VERSION = version();
614 subobj.HOSTIP = hostip();
615 subobj.DOMAINSID = randsid();
616 subobj.INVOCATIONID = randguid();
617 subobj.KRBTGTPASS = randpass(12);
618 subobj.MACHINEPASS = randpass(12);
619 subobj.ADMINPASS = randpass(12);
620 subobj.DEFAULTSITE = "Default-First-Site-Name";
621 subobj.NEWGUID = randguid;
622 subobj.NTTIME = nttime;
623 subobj.LDAPTIME = ldaptime;
624 subobj.DATESTRING = datestring;
625 subobj.ROOT = findnss(nss.getpwnam, "root");
626 subobj.NOBODY = findnss(nss.getpwnam, "nobody");
627 subobj.NOGROUP = findnss(nss.getgrnam, "nogroup", "nobody");
628 subobj.WHEEL = findnss(nss.getgrnam, "wheel", "root", "staff", "adm");
629 subobj.BACKUP = findnss(nss.getgrnam, "backup", "wheel", "root", "staff");
630 subobj.USERS = findnss(nss.getgrnam, "users", "guest", "other", "unknown", "usr");
631 subobj.DNSDOMAIN = strlower(subobj.REALM);
632 subobj.DNSNAME = sprintf("%s.%s",
633 strlower(subobj.HOSTNAME),
635 rdn_list = split(".", subobj.DNSDOMAIN);
636 subobj.BASEDN = "DC=" + join(",DC=", rdn_list);
637 subobj.LDAPBACKEND = "users.ldb";
638 subobj.LDAPMODULES = "objectguid";
639 subobj.EXTENSIBLEOBJECT = "# no objectClass: extensibleObject for local ldb";
644 search for one attribute as a string
646 function searchone(ldb, basedn, expression, attribute)
648 var attrs = new Array(attribute);
649 res = ldb.search(expression, basedn, ldb.SCOPE_SUBTREE, attrs);
650 if (res.length != 1 ||
651 res[0][attribute] == undefined) {
654 return res[0][attribute];
658 modify an account to remove the
660 function enable_account(ldb, user_dn)
662 var attrs = new Array("userAccountControl");
663 var res = ldb.search(NULL, user_dn, ldb.SCOPE_ONELEVEL, attrs);
664 assert(res.length == 1);
665 var userAccountControl = res[0].userAccountControl;
666 userAccountControl = userAccountControl - 2; /* remove disabled bit */
670 replace: userAccountControl
671 userAccountControl: %u
673 user_dn, userAccountControl);
674 var ok = ldb.modify(mod);
680 add a new user record
682 function newuser(username, unixname, password, message, session_info, credentials)
684 var lp = loadparm_init();
685 var samdb = lp.get("sam database");
686 var ldb = ldb_init();
688 ldb.session_info = session_info;
689 ldb.credentials = credentials;
691 /* connect to the sam */
692 var ok = ldb.connect(samdb);
695 ldb.transaction_start();
697 /* find the DNs for the domain and the domain users group */
698 var attrs = new Array("defaultNamingContext");
699 res = ldb.search("defaultNamingContext=*", "", ldb.SCOPE_BASE, attrs);
700 assert(res.length == 1 && res[0].defaultNamingContext != undefined);
701 var domain_dn = res[0].defaultNamingContext;
702 assert(domain_dn != undefined);
703 var dom_users = searchone(ldb, domain_dn, "name=Domain Users", "dn");
704 assert(dom_users != undefined);
706 var user_dn = sprintf("CN=%s,CN=Users,%s", username, domain_dn);
710 the new user record. note the reliance on the samdb module to fill
721 user_dn, username, dom_users,
724 add the user to the users group as well
726 var modgroup = sprintf("
738 message("Adding user %s\n", user_dn);
741 message("Failed to add %s - %s\n", user_dn, ldb.errstring());
745 message("Modifying group %s\n", dom_users);
746 ok = ldb.modify(modgroup);
748 message("Failed to modify %s - %s\n", dom_users, ldb.errstring());
753 modify the userAccountControl to remove the disabled bit
755 ok = enable_account(ldb, user_dn);
757 ldb.transaction_commit();
762 // Check whether a name is valid as a NetBIOS name.
763 // FIXME: There are probably more constraints here.
764 // crh has a paragraph on this in his book (1.4.1.1)
765 function valid_netbios_name(name)
767 if (strlen(name) > 13) return false;
771 function provision_validate(subobj, message)
773 var lp = loadparm_init();
775 if (!valid_netbios_name(subobj.DOMAIN)) {
776 message("Invalid NetBIOS name for domain\n");
780 if (!valid_netbios_name(subobj.NETBIOSNAME)) {
781 message("Invalid NetBIOS name for host\n");
786 if (strupper(lp.get("workgroup")) != strupper(subobj.DOMAIN)) {
787 message("workgroup '%s' in smb.conf must match chosen domain '%s'\n",
788 lp.get("workgroup"), subobj.DOMAIN);
792 if (strupper(lp.get("realm")) != strupper(subobj.REALM)) {
793 message("realm '%s' in smb.conf must match chosen realm '%s'\n",
794 lp.get("realm"), subobj.REALM);
801 function join_domain(domain, netbios_name, join_type, creds, message)
803 var ctx = NetContext(creds);
804 var joindom = new Object();
805 joindom.domain = domain;
806 joindom.join_type = join_type;
807 joindom.netbios_name = netbios_name;
808 if (!ctx.JoinDomain(joindom)) {
809 message("Domain Join failed: " + joindom.error_string);
815 /* Vampire a remote domain. Session info and credentials are required for for
816 * access to our local database (might be remote ldap)
819 function vampire(domain, session_info, credentials, message) {
820 var ctx = NetContext(credentials);
821 var vampire_ctx = new Object();
822 var machine_creds = credentials_init();
823 machine_creds.set_domain(form.DOMAIN);
824 if (!machine_creds.set_machine_account()) {
825 message("Failed to access domain join information!");
828 vampire_ctx.machine_creds = machine_creds;
829 vampire_ctx.session_info = session_info;
830 if (!ctx.SamSyncLdb(vampire_ctx)) {
831 message("Migration of remote domain to Samba failed: " + vampire_ctx.error_string);