Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into 4-0-abartlet
[samba.git] / source4 / scripting / python / samba / provision.py
1 #
2 # Unix SMB/CIFS implementation.
3 # backend code for provisioning a Samba4 server
4
5 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
6 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008
7 #
8 # Based on the original in EJS:
9 # Copyright (C) Andrew Tridgell <tridge@samba.org> 2005
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #   
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #   
21 # You should have received a copy of the GNU General Public License
22 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 #
24
25 """Functions for setting up a Samba configuration."""
26
27 from base64 import b64encode
28 import os
29 import pwd
30 import grp
31 import time
32 import uuid, misc
33 import socket
34 import param
35 import registry
36 import samba
37 from auth import system_session
38 from samba import Ldb, substitute_var, valid_netbios_name, check_all_substituted
39 from samba.samdb import SamDB
40 from samba.idmap import IDmapDB
41 import security
42 import urllib
43 from ldb import SCOPE_SUBTREE, SCOPE_ONELEVEL, SCOPE_BASE, LdbError, \
44         LDB_ERR_NO_SUCH_OBJECT, timestring, CHANGETYPE_MODIFY, CHANGETYPE_NONE
45
46 __docformat__ = "restructuredText"
47
48 DEFAULTSITE = "Default-First-Site-Name"
49
50 class InvalidNetbiosName(Exception):
51     """A specified name was not a valid NetBIOS name."""
52     def __init__(self, name):
53         super(InvalidNetbiosName, self).__init__("The name '%r' is not a valid NetBIOS name" % name)
54
55
56 class ProvisionPaths(object):
57     def __init__(self):
58         self.shareconf = None
59         self.hklm = None
60         self.hkcu = None
61         self.hkcr = None
62         self.hku = None
63         self.hkpd = None
64         self.hkpt = None
65         self.samdb = None
66         self.idmapdb = None
67         self.secrets = None
68         self.keytab = None
69         self.dns_keytab = None
70         self.dns = None
71         self.winsdb = None
72         self.private_dir = None
73         self.ldapdir = None
74         self.slapdconf = None
75         self.modulesconf = None
76         self.memberofconf = None
77         self.fedoradsinf = None
78         self.fedoradspartitions = None
79         self.olmmron = None
80         self.olmmrserveridsconf = None
81         self.olmmrsyncreplconf = None
82
83 class ProvisionNames(object):
84     def __init__(self):
85         self.rootdn = None
86         self.domaindn = None
87         self.configdn = None
88         self.schemadn = None
89         self.ldapmanagerdn = None
90         self.dnsdomain = None
91         self.realm = None
92         self.netbiosname = None
93         self.domain = None
94         self.hostname = None
95         self.sitename = None
96         self.smbconf = None
97     
98
99 class ProvisionResult(object):
100     def __init__(self):
101         self.paths = None
102         self.domaindn = None
103         self.lp = None
104         self.samdb = None
105
106 def check_install(lp, session_info, credentials):
107     """Check whether the current install seems ok.
108     
109     :param lp: Loadparm context
110     :param session_info: Session information
111     :param credentials: Credentials
112     """
113     if lp.get("realm") == "":
114         raise Exception("Realm empty")
115     ldb = Ldb(lp.get("sam database"), session_info=session_info, 
116             credentials=credentials, lp=lp)
117     if len(ldb.search("(cn=Administrator)")) != 1:
118         raise "No administrator account found"
119
120
121 def findnss(nssfn, names):
122     """Find a user or group from a list of possibilities.
123     
124     :param nssfn: NSS Function to try (should raise KeyError if not found)
125     :param names: Names to check.
126     :return: Value return by first names list.
127     """
128     for name in names:
129         try:
130             return nssfn(name)
131         except KeyError:
132             pass
133     raise KeyError("Unable to find user/group %r" % names)
134
135
136 findnss_uid = lambda names: findnss(pwd.getpwnam, names)[2]
137 findnss_gid = lambda names: findnss(grp.getgrnam, names)[2]
138
139
140 def read_and_sub_file(file, subst_vars):
141     """Read a file and sub in variables found in it
142     
143     :param file: File to be read (typically from setup directory)
144      param subst_vars: Optional variables to subsitute in the file.
145     """
146     data = open(file, 'r').read()
147     if subst_vars is not None:
148         data = substitute_var(data, subst_vars)
149     check_all_substituted(data)
150     return data
151
152
153 def setup_add_ldif(ldb, ldif_path, subst_vars=None):
154     """Setup a ldb in the private dir.
155     
156     :param ldb: LDB file to import data into
157     :param ldif_path: Path of the LDIF file to load
158     :param subst_vars: Optional variables to subsitute in LDIF.
159     """
160     assert isinstance(ldif_path, str)
161
162     data = read_and_sub_file(ldif_path, subst_vars)
163     ldb.add_ldif(data)
164
165
166 def setup_modify_ldif(ldb, ldif_path, subst_vars=None):
167     """Modify a ldb in the private dir.
168     
169     :param ldb: LDB object.
170     :param ldif_path: LDIF file path.
171     :param subst_vars: Optional dictionary with substitution variables.
172     """
173     data = read_and_sub_file(ldif_path, subst_vars)
174
175     ldb.modify_ldif(data)
176
177
178 def setup_ldb(ldb, ldif_path, subst_vars):
179     """Import a LDIF a file into a LDB handle, optionally substituting variables.
180
181     :note: Either all LDIF data will be added or none (using transactions).
182
183     :param ldb: LDB file to import into.
184     :param ldif_path: Path to the LDIF file.
185     :param subst_vars: Dictionary with substitution variables.
186     """
187     assert ldb is not None
188     ldb.transaction_start()
189     try:
190         setup_add_ldif(ldb, ldif_path, subst_vars)
191     except:
192         ldb.transaction_cancel()
193         raise
194     ldb.transaction_commit()
195
196
197 def setup_file(template, fname, subst_vars):
198     """Setup a file in the private dir.
199
200     :param template: Path of the template file.
201     :param fname: Path of the file to create.
202     :param subst_vars: Substitution variables.
203     """
204     f = fname
205
206     if os.path.exists(f):
207         os.unlink(f)
208
209     data = read_and_sub_file(template, subst_vars)
210     open(f, 'w').write(data)
211
212
213 def provision_paths_from_lp(lp, dnsdomain):
214     """Set the default paths for provisioning.
215
216     :param lp: Loadparm context.
217     :param dnsdomain: DNS Domain name
218     """
219     paths = ProvisionPaths()
220     paths.private_dir = lp.get("private dir")
221     paths.keytab = "secrets.keytab"
222     paths.dns_keytab = "dns.keytab"
223
224     paths.shareconf = os.path.join(paths.private_dir, "share.ldb")
225     paths.samdb = os.path.join(paths.private_dir, lp.get("sam database") or "samdb.ldb")
226     paths.idmapdb = os.path.join(paths.private_dir, lp.get("idmap database") or "idmap.ldb")
227     paths.secrets = os.path.join(paths.private_dir, lp.get("secrets database") or "secrets.ldb")
228     paths.templates = os.path.join(paths.private_dir, "templates.ldb")
229     paths.dns = os.path.join(paths.private_dir, dnsdomain + ".zone")
230     paths.namedconf = os.path.join(paths.private_dir, "named.conf")
231     paths.namedtxt = os.path.join(paths.private_dir, "named.txt")
232     paths.krb5conf = os.path.join(paths.private_dir, "krb5.conf")
233     paths.winsdb = os.path.join(paths.private_dir, "wins.ldb")
234     paths.s4_ldapi_path = os.path.join(paths.private_dir, "ldapi")
235     paths.phpldapadminconfig = os.path.join(paths.private_dir, 
236                                             "phpldapadmin-config.php")
237     paths.ldapdir = os.path.join(paths.private_dir, 
238                                  "ldap")
239     paths.slapdconf = os.path.join(paths.ldapdir, 
240                                    "slapd.conf")
241     paths.modulesconf = os.path.join(paths.ldapdir, 
242                                      "modules.conf")
243     paths.memberofconf = os.path.join(paths.ldapdir, 
244                                       "memberof.conf")
245     paths.fedoradsinf = os.path.join(paths.ldapdir, 
246                                      "fedorads.inf")
247     paths.fedoradspartitions = os.path.join(paths.ldapdir, 
248                                             "fedorads-partitions.ldif")
249     paths.olmmrserveridsconf = os.path.join(paths.ldapdir, 
250                                             "mmr_serverids.conf")
251     paths.olmmrsyncreplconf = os.path.join(paths.ldapdir, 
252                                            "mmr_syncrepl.conf")
253     paths.hklm = "hklm.ldb"
254     paths.hkcr = "hkcr.ldb"
255     paths.hkcu = "hkcu.ldb"
256     paths.hku = "hku.ldb"
257     paths.hkpd = "hkpd.ldb"
258     paths.hkpt = "hkpt.ldb"
259
260     paths.sysvol = lp.get("path", "sysvol")
261
262     paths.netlogon = lp.get("path", "netlogon")
263
264     paths.smbconf = lp.configfile()
265
266     return paths
267
268
269 def guess_names(lp=None, hostname=None, domain=None, dnsdomain=None, serverrole=None,
270                 rootdn=None, domaindn=None, configdn=None, schemadn=None, serverdn=None, 
271                 sitename=None):
272     """Guess configuration settings to use."""
273
274     if hostname is None:
275         hostname = socket.gethostname().split(".")[0].lower()
276
277     netbiosname = hostname.upper()
278     if not valid_netbios_name(netbiosname):
279         raise InvalidNetbiosName(netbiosname)
280
281     hostname = hostname.lower()
282
283     if dnsdomain is None:
284         dnsdomain = lp.get("realm")
285
286     if serverrole is None:
287         serverrole = lp.get("server role")
288
289     assert dnsdomain is not None
290     realm = dnsdomain.upper()
291
292     if lp.get("realm").upper() != realm:
293         raise Exception("realm '%s' in %s must match chosen realm '%s'" %
294                         (lp.get("realm"), lp.configfile(), realm))
295     
296     dnsdomain = dnsdomain.lower()
297
298     if serverrole == "domain controller":
299         if domain is None:
300             domain = lp.get("workgroup")
301         if domaindn is None:
302             domaindn = "DC=" + dnsdomain.replace(".", ",DC=")
303         if lp.get("workgroup").upper() != domain.upper():
304             raise Exception("workgroup '%s' in smb.conf must match chosen domain '%s'",
305                         lp.get("workgroup"), domain)
306     else:
307         domain = netbiosname
308         if domaindn is None:
309             domaindn = "CN=" + netbiosname
310         
311     assert domain is not None
312     domain = domain.upper()
313     if not valid_netbios_name(domain):
314         raise InvalidNetbiosName(domain)
315         
316     if rootdn is None:
317        rootdn = domaindn
318        
319     if configdn is None:
320         configdn = "CN=Configuration," + rootdn
321     if schemadn is None:
322         schemadn = "CN=Schema," + configdn
323
324     if sitename is None:
325         sitename=DEFAULTSITE
326
327     names = ProvisionNames()
328     names.rootdn = rootdn
329     names.domaindn = domaindn
330     names.configdn = configdn
331     names.schemadn = schemadn
332     names.ldapmanagerdn = "CN=Manager," + rootdn
333     names.dnsdomain = dnsdomain
334     names.domain = domain
335     names.realm = realm
336     names.netbiosname = netbiosname
337     names.hostname = hostname
338     names.sitename = sitename
339     names.serverdn = "CN=%s,CN=Servers,CN=%s,CN=Sites,%s" % (netbiosname, sitename, configdn)
340  
341     return names
342     
343
344 def make_smbconf(smbconf, setup_path, hostname, domain, realm, serverrole, 
345                  targetdir):
346     if hostname is None:
347         hostname = socket.gethostname().split(".")[0].lower()
348
349     if serverrole is None:
350         serverrole = "standalone"
351
352     assert serverrole in ("domain controller", "member server", "standalone")
353     if serverrole == "domain controller":
354         smbconfsuffix = "dc"
355     elif serverrole == "member server":
356         smbconfsuffix = "member"
357     elif serverrole == "standalone":
358         smbconfsuffix = "standalone"
359
360     assert domain is not None
361     assert realm is not None
362
363     default_lp = param.LoadParm()
364     #Load non-existant file
365     default_lp.load(smbconf)
366     
367     if targetdir is not None:
368         privatedir_line = "private dir = " + os.path.abspath(os.path.join(targetdir, "private"))
369         lockdir_line = "lock dir = " + os.path.abspath(targetdir)
370
371         default_lp.set("lock dir", os.path.abspath(targetdir))
372     else:
373         privatedir_line = ""
374         lockdir_line = ""
375
376     sysvol = os.path.join(default_lp.get("lock dir"), "sysvol")
377     netlogon = os.path.join(sysvol, realm.lower(), "scripts")
378
379     setup_file(setup_path("provision.smb.conf.%s" % smbconfsuffix), 
380                smbconf, {
381             "HOSTNAME": hostname,
382             "DOMAIN": domain,
383             "REALM": realm,
384             "SERVERROLE": serverrole,
385             "NETLOGONPATH": netlogon,
386             "SYSVOLPATH": sysvol,
387             "PRIVATEDIR_LINE": privatedir_line,
388             "LOCKDIR_LINE": lockdir_line
389             })
390
391
392
393 def setup_name_mappings(samdb, idmap, sid, domaindn, root_uid, nobody_uid,
394                         users_gid, wheel_gid):
395     """setup reasonable name mappings for sam names to unix names.
396
397     :param samdb: SamDB object.
398     :param idmap: IDmap db object.
399     :param sid: The domain sid.
400     :param domaindn: The domain DN.
401     :param root_uid: uid of the UNIX root user.
402     :param nobody_uid: uid of the UNIX nobody user.
403     :param users_gid: gid of the UNIX users group.
404     :param wheel_gid: gid of the UNIX wheel group."""
405     # add some foreign sids if they are not present already
406     samdb.add_foreign(domaindn, "S-1-5-7", "Anonymous")
407     samdb.add_foreign(domaindn, "S-1-1-0", "World")
408     samdb.add_foreign(domaindn, "S-1-5-2", "Network")
409     samdb.add_foreign(domaindn, "S-1-5-18", "System")
410     samdb.add_foreign(domaindn, "S-1-5-11", "Authenticated Users")
411
412     idmap.setup_name_mapping("S-1-5-7", idmap.TYPE_UID, nobody_uid)
413     idmap.setup_name_mapping("S-1-5-32-544", idmap.TYPE_GID, wheel_gid)
414
415     idmap.setup_name_mapping(sid + "-500", idmap.TYPE_UID, root_uid)
416     idmap.setup_name_mapping(sid + "-513", idmap.TYPE_GID, users_gid)
417
418
419 def setup_samdb_partitions(samdb_path, setup_path, message, lp, session_info, 
420                            credentials, names,
421                            serverrole, ldap_backend=None, 
422                            ldap_backend_type=None, erase=False):
423     """Setup the partitions for the SAM database. 
424     
425     Alternatively, provision() may call this, and then populate the database.
426     
427     :note: This will wipe the Sam Database!
428     
429     :note: This function always removes the local SAM LDB file. The erase 
430         parameter controls whether to erase the existing data, which 
431         may not be stored locally but in LDAP.
432     """
433     assert session_info is not None
434
435     samdb = SamDB(samdb_path, session_info=session_info, 
436                   credentials=credentials, lp=lp)
437
438     # Wipes the database
439     try:
440         samdb.erase()
441     except:
442         os.unlink(samdb_path)
443
444     samdb = SamDB(samdb_path, session_info=session_info, 
445                   credentials=credentials, lp=lp)
446
447     #Add modules to the list to activate them by default
448     #beware often order is important
449     #
450     # Some Known ordering constraints:
451     # - rootdse must be first, as it makes redirects from "" -> cn=rootdse
452     # - objectclass must be before password_hash, because password_hash checks
453     #   that the objectclass is of type person (filled in by objectclass
454     #   module when expanding the objectclass list)
455     # - partition must be last
456     # - each partition has its own module list then
457     modules_list = ["rootdse",
458                     "paged_results",
459                     "ranged_results",
460                     "anr",
461                     "server_sort",
462                     "extended_dn",
463                     "asq",
464                     "rdn_name",
465                     "objectclass",
466                     "samldb",
467                     "kludge_acl",
468                     "operational"]
469     tdb_modules_list = [
470                     "subtree_rename",
471                     "subtree_delete",
472                     "linked_attributes"]
473     modules_list2 = ["show_deleted",
474                     "partition"]
475  
476     domaindn_ldb = "users.ldb"
477     if ldap_backend is not None:
478         domaindn_ldb = ldap_backend
479     configdn_ldb = "configuration.ldb"
480     if ldap_backend is not None:
481         configdn_ldb = ldap_backend
482     schemadn_ldb = "schema.ldb"
483     if ldap_backend is not None:
484         schema_ldb = ldap_backend
485         schemadn_ldb = ldap_backend
486         
487     if ldap_backend_type == "fedora-ds":
488         backend_modules = ["nsuniqueid", "paged_searches"]
489         # We can handle linked attributes here, as we don't have directory-side subtree operations
490         tdb_modules_list = ["linked_attributes"]
491     elif ldap_backend_type == "openldap":
492         backend_modules = ["normalise", "entryuuid", "paged_searches"]
493         # OpenLDAP handles subtree renames, so we don't want to do any of these things
494         tdb_modules_list = None
495     elif ldap_backend is not None:
496         raise "LDAP Backend specified, but LDAP Backend Type not specified"
497     elif serverrole == "domain controller":
498         backend_modules = ["repl_meta_data"]
499     else:
500         backend_modules = ["objectguid"]
501
502     if tdb_modules_list is None:
503         tdb_modules_list_as_string = ""
504     else:
505         tdb_modules_list_as_string = ","+",".join(tdb_modules_list)
506         
507     samdb.transaction_start()
508     try:
509         setup_add_ldif(samdb, setup_path("provision_partitions.ldif"), {
510                 "SCHEMADN": names.schemadn, 
511                 "SCHEMADN_LDB": schemadn_ldb,
512                 "SCHEMADN_MOD2": ",objectguid",
513                 "CONFIGDN": names.configdn,
514                 "CONFIGDN_LDB": configdn_ldb,
515                 "DOMAINDN": names.domaindn,
516                 "DOMAINDN_LDB": domaindn_ldb,
517                 "SCHEMADN_MOD": "schema_fsmo,instancetype",
518                 "CONFIGDN_MOD": "naming_fsmo,instancetype",
519                 "DOMAINDN_MOD": "pdc_fsmo,password_hash,instancetype",
520                 "MODULES_LIST": ",".join(modules_list),
521                 "TDB_MODULES_LIST": tdb_modules_list_as_string,
522                 "MODULES_LIST2": ",".join(modules_list2),
523                 "BACKEND_MOD": ",".join(backend_modules),
524         })
525
526     except:
527         samdb.transaction_cancel()
528         raise
529
530     samdb.transaction_commit()
531     
532     samdb = SamDB(samdb_path, session_info=session_info, 
533                   credentials=credentials, lp=lp)
534
535     samdb.transaction_start()
536     try:
537         message("Setting up sam.ldb attributes")
538         samdb.load_ldif_file_add(setup_path("provision_init.ldif"))
539
540         message("Setting up sam.ldb rootDSE")
541         setup_samdb_rootdse(samdb, setup_path, names)
542
543         if erase:
544             message("Erasing data from partitions")
545             samdb.erase_partitions()
546
547     except:
548         samdb.transaction_cancel()
549         raise
550
551     samdb.transaction_commit()
552     
553     return samdb
554
555
556 def secretsdb_become_dc(secretsdb, setup_path, domain, realm, dnsdomain, 
557                         netbiosname, domainsid, keytab_path, samdb_url, 
558                         dns_keytab_path, dnspass, machinepass):
559     """Add DC-specific bits to a secrets database.
560     
561     :param secretsdb: Ldb Handle to the secrets database
562     :param setup_path: Setup path function
563     :param machinepass: Machine password
564     """
565     setup_ldb(secretsdb, setup_path("secrets_dc.ldif"), { 
566             "MACHINEPASS_B64": b64encode(machinepass),
567             "DOMAIN": domain,
568             "REALM": realm,
569             "DNSDOMAIN": dnsdomain,
570             "DOMAINSID": str(domainsid),
571             "SECRETS_KEYTAB": keytab_path,
572             "NETBIOSNAME": netbiosname,
573             "SAM_LDB": samdb_url,
574             "DNS_KEYTAB": dns_keytab_path,
575             "DNSPASS_B64": b64encode(dnspass),
576             })
577
578
579 def setup_secretsdb(path, setup_path, session_info, credentials, lp):
580     """Setup the secrets database.
581
582     :param path: Path to the secrets database.
583     :param setup_path: Get the path to a setup file.
584     :param session_info: Session info.
585     :param credentials: Credentials
586     :param lp: Loadparm context
587     :return: LDB handle for the created secrets database
588     """
589     if os.path.exists(path):
590         os.unlink(path)
591     secrets_ldb = Ldb(path, session_info=session_info, credentials=credentials,
592                       lp=lp)
593     secrets_ldb.erase()
594     secrets_ldb.load_ldif_file_add(setup_path("secrets_init.ldif"))
595     secrets_ldb = Ldb(path, session_info=session_info, credentials=credentials,
596                       lp=lp)
597     secrets_ldb.load_ldif_file_add(setup_path("secrets.ldif"))
598
599     if credentials is not None and credentials.authentication_requested():
600         if credentials.get_bind_dn() is not None:
601             setup_add_ldif(secrets_ldb, setup_path("secrets_simple_ldap.ldif"), {
602                     "LDAPMANAGERDN": credentials.get_bind_dn(),
603                     "LDAPMANAGERPASS_B64": b64encode(credentials.get_password())
604                     })
605         else:
606             setup_add_ldif(secrets_ldb, setup_path("secrets_sasl_ldap.ldif"), {
607                     "LDAPADMINUSER": credentials.get_username(),
608                     "LDAPADMINREALM": credentials.get_realm(),
609                     "LDAPADMINPASS_B64": b64encode(credentials.get_password())
610                     })
611
612     return secrets_ldb
613
614
615 def setup_templatesdb(path, setup_path, session_info, credentials, lp):
616     """Setup the templates database.
617
618     :param path: Path to the database.
619     :param setup_path: Function for obtaining the path to setup files.
620     :param session_info: Session info
621     :param credentials: Credentials
622     :param lp: Loadparm context
623     """
624     templates_ldb = SamDB(path, session_info=session_info,
625                           credentials=credentials, lp=lp)
626     # Wipes the database
627     try:
628         templates_ldb.erase()
629     except:
630         os.unlink(path)
631
632     templates_ldb.load_ldif_file_add(setup_path("provision_templates_init.ldif"))
633
634     templates_ldb = SamDB(path, session_info=session_info,
635                           credentials=credentials, lp=lp)
636
637     templates_ldb.load_ldif_file_add(setup_path("provision_templates.ldif"))
638
639
640 def setup_registry(path, setup_path, session_info, credentials, lp):
641     """Setup the registry.
642     
643     :param path: Path to the registry database
644     :param setup_path: Function that returns the path to a setup.
645     :param session_info: Session information
646     :param credentials: Credentials
647     :param lp: Loadparm context
648     """
649     reg = registry.Registry()
650     hive = registry.open_ldb(path, session_info=session_info, 
651                          credentials=credentials, lp_ctx=lp)
652     reg.mount_hive(hive, "HKEY_LOCAL_MACHINE")
653     provision_reg = setup_path("provision.reg")
654     assert os.path.exists(provision_reg)
655     reg.diff_apply(provision_reg)
656
657
658 def setup_idmapdb(path, setup_path, session_info, credentials, lp):
659     """Setup the idmap database.
660
661     :param path: path to the idmap database
662     :param setup_path: Function that returns a path to a setup file
663     :param session_info: Session information
664     :param credentials: Credentials
665     :param lp: Loadparm context
666     """
667     if os.path.exists(path):
668         os.unlink(path)
669
670     idmap_ldb = IDmapDB(path, session_info=session_info,
671                         credentials=credentials, lp=lp)
672
673     idmap_ldb.erase()
674     idmap_ldb.load_ldif_file_add(setup_path("idmap_init.ldif"))
675     return idmap_ldb
676
677
678 def setup_samdb_rootdse(samdb, setup_path, names):
679     """Setup the SamDB rootdse.
680
681     :param samdb: Sam Database handle
682     :param setup_path: Obtain setup path
683     """
684     setup_add_ldif(samdb, setup_path("provision_rootdse_add.ldif"), {
685         "SCHEMADN": names.schemadn, 
686         "NETBIOSNAME": names.netbiosname,
687         "DNSDOMAIN": names.dnsdomain,
688         "REALM": names.realm,
689         "DNSNAME": "%s.%s" % (names.hostname, names.dnsdomain),
690         "DOMAINDN": names.domaindn,
691         "ROOTDN": names.rootdn,
692         "CONFIGDN": names.configdn,
693         "SERVERDN": names.serverdn,
694         })
695         
696
697 def setup_self_join(samdb, names,
698                     machinepass, dnspass, 
699                     domainsid, invocationid, setup_path,
700                     policyguid):
701     """Join a host to its own domain."""
702     assert isinstance(invocationid, str)
703     setup_add_ldif(samdb, setup_path("provision_self_join.ldif"), { 
704               "CONFIGDN": names.configdn, 
705               "SCHEMADN": names.schemadn,
706               "DOMAINDN": names.domaindn,
707               "SERVERDN": names.serverdn,
708               "INVOCATIONID": invocationid,
709               "NETBIOSNAME": names.netbiosname,
710               "DEFAULTSITE": names.sitename,
711               "DNSNAME": "%s.%s" % (names.hostname, names.dnsdomain),
712               "MACHINEPASS_B64": b64encode(machinepass),
713               "DNSPASS_B64": b64encode(dnspass),
714               "REALM": names.realm,
715               "DOMAIN": names.domain,
716               "DNSDOMAIN": names.dnsdomain})
717     setup_add_ldif(samdb, setup_path("provision_group_policy.ldif"), { 
718               "POLICYGUID": policyguid,
719               "DNSDOMAIN": names.dnsdomain,
720               "DOMAINSID": str(domainsid),
721               "DOMAINDN": names.domaindn})
722
723
724 def setup_samdb(path, setup_path, session_info, credentials, lp, 
725                 names, message, 
726                 domainsid, aci, domainguid, policyguid, 
727                 fill, adminpass, krbtgtpass, 
728                 machinepass, invocationid, dnspass,
729                 serverrole, ldap_backend=None, 
730                 ldap_backend_type=None):
731     """Setup a complete SAM Database.
732     
733     :note: This will wipe the main SAM database file!
734     """
735
736     erase = (fill != FILL_DRS)
737
738     # Also wipes the database
739     setup_samdb_partitions(path, setup_path, message=message, lp=lp,
740                            credentials=credentials, session_info=session_info,
741                            names=names, 
742                            ldap_backend=ldap_backend, serverrole=serverrole,
743                            ldap_backend_type=ldap_backend_type, erase=erase)
744
745     samdb = SamDB(path, session_info=session_info, 
746                   credentials=credentials, lp=lp)
747
748     if fill == FILL_DRS:
749        # We want to finish here, but setup the index before we do so
750         message("Setting up sam.ldb index")
751         samdb.load_ldif_file_add(setup_path("provision_index.ldif"))
752         return samdb
753
754     message("Pre-loading the Samba 4 and AD schema")
755     samdb.set_domain_sid(domainsid)
756     if serverrole == "domain controller":
757         samdb.set_invocation_id(invocationid)
758
759     load_schema(setup_path, samdb, names.schemadn, names.netbiosname, 
760                 names.configdn, names.sitename, names.serverdn,
761                 names.hostname)
762
763     samdb.transaction_start()
764         
765     try:
766         message("Adding DomainDN: %s (permitted to fail)" % names.domaindn)
767         if serverrole == "domain controller":
768             domain_oc = "domainDNS"
769         else:
770             domain_oc = "samba4LocalDomain"
771
772         setup_add_ldif(samdb, setup_path("provision_basedn.ldif"), {
773                 "DOMAINDN": names.domaindn,
774                 "ACI": aci,
775                 "DOMAIN_OC": domain_oc
776                 })
777
778         message("Modifying DomainDN: " + names.domaindn + "")
779         if domainguid is not None:
780             domainguid_mod = "replace: objectGUID\nobjectGUID: %s\n-" % domainguid
781         else:
782             domainguid_mod = ""
783
784         setup_modify_ldif(samdb, setup_path("provision_basedn_modify.ldif"), {
785             "LDAPTIME": timestring(int(time.time())),
786             "DOMAINSID": str(domainsid),
787             "SCHEMADN": names.schemadn, 
788             "NETBIOSNAME": names.netbiosname,
789             "DEFAULTSITE": names.sitename,
790             "CONFIGDN": names.configdn,
791             "SERVERDN": names.serverdn,
792             "POLICYGUID": policyguid,
793             "DOMAINDN": names.domaindn,
794             "DOMAINGUID_MOD": domainguid_mod,
795             })
796
797         message("Adding configuration container (permitted to fail)")
798         setup_add_ldif(samdb, setup_path("provision_configuration_basedn.ldif"), {
799             "CONFIGDN": names.configdn, 
800             "ACI": aci,
801             })
802         message("Modifying configuration container")
803         setup_modify_ldif(samdb, setup_path("provision_configuration_basedn_modify.ldif"), {
804             "CONFIGDN": names.configdn, 
805             "SCHEMADN": names.schemadn,
806             })
807
808         message("Adding schema container (permitted to fail)")
809         setup_add_ldif(samdb, setup_path("provision_schema_basedn.ldif"), {
810             "SCHEMADN": names.schemadn,
811             "ACI": aci,
812             })
813         message("Modifying schema container")
814
815         prefixmap = open(setup_path("prefixMap.txt"), 'r').read()
816
817         setup_modify_ldif(samdb, 
818             setup_path("provision_schema_basedn_modify.ldif"), {
819             "SCHEMADN": names.schemadn,
820             "NETBIOSNAME": names.netbiosname,
821             "DEFAULTSITE": names.sitename,
822             "CONFIGDN": names.configdn,
823             "SERVERDN": names.serverdn,
824             "PREFIXMAP_B64": b64encode(prefixmap)
825             })
826
827         message("Setting up sam.ldb Samba4 schema")
828         setup_add_ldif(samdb, setup_path("schema_samba4.ldif"), 
829                        {"SCHEMADN": names.schemadn })
830         message("Setting up sam.ldb AD schema")
831         setup_add_ldif(samdb, setup_path("schema.ldif"), 
832                        {"SCHEMADN": names.schemadn})
833
834         message("Setting up sam.ldb configuration data")
835         setup_add_ldif(samdb, setup_path("provision_configuration.ldif"), {
836             "CONFIGDN": names.configdn,
837             "NETBIOSNAME": names.netbiosname,
838             "DEFAULTSITE": names.sitename,
839             "DNSDOMAIN": names.dnsdomain,
840             "DOMAIN": names.domain,
841             "SCHEMADN": names.schemadn,
842             "DOMAINDN": names.domaindn,
843             "SERVERDN": names.serverdn
844             })
845
846         message("Setting up display specifiers")
847         setup_add_ldif(samdb, setup_path("display_specifiers.ldif"), 
848                        {"CONFIGDN": names.configdn})
849
850         message("Adding users container (permitted to fail)")
851         setup_add_ldif(samdb, setup_path("provision_users_add.ldif"), {
852                 "DOMAINDN": names.domaindn})
853         message("Modifying users container")
854         setup_modify_ldif(samdb, setup_path("provision_users_modify.ldif"), {
855                 "DOMAINDN": names.domaindn})
856         message("Adding computers container (permitted to fail)")
857         setup_add_ldif(samdb, setup_path("provision_computers_add.ldif"), {
858                 "DOMAINDN": names.domaindn})
859         message("Modifying computers container")
860         setup_modify_ldif(samdb, setup_path("provision_computers_modify.ldif"), {
861                 "DOMAINDN": names.domaindn})
862         message("Setting up sam.ldb data")
863         setup_add_ldif(samdb, setup_path("provision.ldif"), {
864             "DOMAINDN": names.domaindn,
865             "NETBIOSNAME": names.netbiosname,
866             "DEFAULTSITE": names.sitename,
867             "CONFIGDN": names.configdn,
868             "SERVERDN": names.serverdn
869             })
870
871         if fill == FILL_FULL:
872             message("Setting up sam.ldb users and groups")
873             setup_add_ldif(samdb, setup_path("provision_users.ldif"), {
874                 "DOMAINDN": names.domaindn,
875                 "DOMAINSID": str(domainsid),
876                 "CONFIGDN": names.configdn,
877                 "ADMINPASS_B64": b64encode(adminpass),
878                 "KRBTGTPASS_B64": b64encode(krbtgtpass),
879                 })
880
881             if serverrole == "domain controller":
882                 message("Setting up self join")
883                 setup_self_join(samdb, names=names, invocationid=invocationid, 
884                                 dnspass=dnspass,  
885                                 machinepass=machinepass, 
886                                 domainsid=domainsid, policyguid=policyguid,
887                                 setup_path=setup_path)
888
889     #We want to setup the index last, as adds are faster unindexed
890         message("Setting up sam.ldb index")
891         samdb.load_ldif_file_add(setup_path("provision_index.ldif"))
892     except:
893         samdb.transaction_cancel()
894         raise
895
896     samdb.transaction_commit()
897     return samdb
898
899
900 FILL_FULL = "FULL"
901 FILL_NT4SYNC = "NT4SYNC"
902 FILL_DRS = "DRS"
903
904 def provision(setup_dir, message, session_info, 
905               credentials, smbconf=None, targetdir=None, samdb_fill=FILL_FULL, realm=None, 
906               rootdn=None, domaindn=None, schemadn=None, configdn=None, 
907               serverdn=None,
908               domain=None, hostname=None, hostip=None, hostip6=None, 
909               domainsid=None, adminpass=None, krbtgtpass=None, domainguid=None, 
910               policyguid=None, invocationid=None, machinepass=None, 
911               dnspass=None, root=None, nobody=None, nogroup=None, users=None, 
912               wheel=None, backup=None, aci=None, serverrole=None, 
913               ldap_backend=None, ldap_backend_type=None, sitename=None):
914     """Provision samba4
915     
916     :note: caution, this wipes all existing data!
917     """
918
919     def setup_path(file):
920         return os.path.join(setup_dir, file)
921
922     if domainsid is None:
923         domainsid = security.random_sid()
924     else:
925         domainsid = security.Sid(domainsid)
926
927     if policyguid is None:
928         policyguid = str(uuid.uuid4())
929     if adminpass is None:
930         adminpass = misc.random_password(12)
931     if krbtgtpass is None:
932         krbtgtpass = misc.random_password(12)
933     if machinepass is None:
934         machinepass  = misc.random_password(12)
935     if dnspass is None:
936         dnspass = misc.random_password(12)
937     root_uid = findnss_uid([root or "root"])
938     nobody_uid = findnss_uid([nobody or "nobody"])
939     users_gid = findnss_gid([users or "users"])
940     if wheel is None:
941         wheel_gid = findnss_gid(["wheel", "adm"])
942     else:
943         wheel_gid = findnss_gid([wheel])
944     if aci is None:
945         aci = "# no aci for local ldb"
946
947     if targetdir is not None:
948         if (not os.path.exists(os.path.join(targetdir, "etc"))):
949             os.makedirs(os.path.join(targetdir, "etc"))
950         smbconf = os.path.join(targetdir, "etc", "smb.conf")
951
952     # only install a new smb.conf if there isn't one there already
953     if not os.path.exists(smbconf):
954         make_smbconf(smbconf, setup_path, hostname, domain, realm, serverrole, 
955                      targetdir)
956
957     lp = param.LoadParm()
958     lp.load(smbconf)
959
960     names = guess_names(lp=lp, hostname=hostname, domain=domain, 
961                         dnsdomain=realm, serverrole=serverrole, sitename=sitename,
962                         rootdn=rootdn, domaindn=domaindn, configdn=configdn, schemadn=schemadn,
963                         serverdn=serverdn)
964
965     paths = provision_paths_from_lp(lp, names.dnsdomain)
966
967     if hostip is None:
968         hostip = socket.getaddrinfo(names.hostname, None, socket.AF_INET, socket.AI_CANONNAME, socket.IPPROTO_IP)[0][-1][0]
969
970     if hostip6 is None:
971         try:
972             hostip6 = socket.getaddrinfo(names.hostname, None, socket.AF_INET6, socket.AI_CANONNAME, socket.IPPROTO_IP)[0][-1][0]
973         except socket.gaierror: 
974             pass
975
976     if serverrole is None:
977         serverrole = lp.get("server role")
978
979     assert serverrole in ("domain controller", "member server", "standalone")
980     if invocationid is None and serverrole == "domain controller":
981         invocationid = str(uuid.uuid4())
982
983     if not os.path.exists(paths.private_dir):
984         os.mkdir(paths.private_dir)
985
986     ldapi_url = "ldapi://%s" % urllib.quote(paths.s4_ldapi_path, safe="")
987     
988     if ldap_backend is not None:
989         if ldap_backend == "ldapi":
990             # provision-backend will set this path suggested slapd command line / fedorads.inf
991             ldap_backend = "ldapi://%s" % urllib.quote(os.path.join(paths.private_dir, "ldap", "ldapi"), safe="")
992              
993     # only install a new shares config db if there is none
994     if not os.path.exists(paths.shareconf):
995         message("Setting up share.ldb")
996         share_ldb = Ldb(paths.shareconf, session_info=session_info, 
997                         credentials=credentials, lp=lp)
998         share_ldb.load_ldif_file_add(setup_path("share.ldif"))
999
1000      
1001     message("Setting up secrets.ldb")
1002     secrets_ldb = setup_secretsdb(paths.secrets, setup_path, 
1003                                   session_info=session_info, 
1004                                   credentials=credentials, lp=lp)
1005
1006     message("Setting up the registry")
1007     setup_registry(paths.hklm, setup_path, session_info, 
1008                    credentials=credentials, lp=lp)
1009
1010     message("Setting up templates db")
1011     setup_templatesdb(paths.templates, setup_path, session_info=session_info, 
1012                       credentials=credentials, lp=lp)
1013
1014     message("Setting up idmap db")
1015     idmap = setup_idmapdb(paths.idmapdb, setup_path, session_info=session_info,
1016                           credentials=credentials, lp=lp)
1017
1018     samdb = setup_samdb(paths.samdb, setup_path, session_info=session_info, 
1019                         credentials=credentials, lp=lp, names=names,
1020                         message=message, 
1021                         domainsid=domainsid, 
1022                         aci=aci, domainguid=domainguid, policyguid=policyguid, 
1023                         fill=samdb_fill, 
1024                         adminpass=adminpass, krbtgtpass=krbtgtpass,
1025                         invocationid=invocationid, 
1026                         machinepass=machinepass, dnspass=dnspass,
1027                         serverrole=serverrole, ldap_backend=ldap_backend, 
1028                         ldap_backend_type=ldap_backend_type)
1029
1030     if lp.get("server role") == "domain controller":
1031         if paths.netlogon is None:
1032             message("Existing smb.conf does not have a [netlogon] share, but you are configuring a DC.")
1033             message("Please either remove %s or see the template at %s" % 
1034                     ( paths.smbconf, setup_path("provision.smb.conf.dc")))
1035             assert(paths.netlogon is not None)
1036
1037         if paths.sysvol is None:
1038             message("Existing smb.conf does not have a [sysvol] share, but you are configuring a DC.")
1039             message("Please either remove %s or see the template at %s" % 
1040                     (paths.smbconf, setup_path("provision.smb.conf.dc")))
1041             assert(paths.sysvol is not None)            
1042             
1043         policy_path = os.path.join(paths.sysvol, names.dnsdomain, "Policies", 
1044                                    "{" + policyguid + "}")
1045         os.makedirs(policy_path, 0755)
1046         open(os.path.join(policy_path, "GPT.INI"), 'w').write("")
1047         os.makedirs(os.path.join(policy_path, "Machine"), 0755)
1048         os.makedirs(os.path.join(policy_path, "User"), 0755)
1049         if not os.path.isdir(paths.netlogon):
1050             os.makedirs(paths.netlogon, 0755)
1051
1052     if samdb_fill == FILL_FULL:
1053         setup_name_mappings(samdb, idmap, str(domainsid), names.domaindn,
1054                             root_uid=root_uid, nobody_uid=nobody_uid,
1055                             users_gid=users_gid, wheel_gid=wheel_gid)
1056
1057         message("Setting up sam.ldb rootDSE marking as synchronized")
1058         setup_modify_ldif(samdb, setup_path("provision_rootdse_modify.ldif"))
1059
1060         # Only make a zone file on the first DC, it should be replicated with DNS replication
1061         if serverrole == "domain controller":
1062             secrets_ldb = Ldb(paths.secrets, session_info=session_info, 
1063                               credentials=credentials, lp=lp)
1064             secretsdb_become_dc(secrets_ldb, setup_path, domain=domain, realm=names.realm,
1065                                 netbiosname=names.netbiosname, domainsid=domainsid, 
1066                                 keytab_path=paths.keytab, samdb_url=paths.samdb, 
1067                                 dns_keytab_path=paths.dns_keytab, dnspass=dnspass, 
1068                                 machinepass=machinepass, dnsdomain=names.dnsdomain)
1069
1070             samdb = SamDB(paths.samdb, session_info=session_info, 
1071                       credentials=credentials, lp=lp)
1072
1073             domainguid = samdb.searchone(basedn=domaindn, attribute="objectGUID")
1074             assert isinstance(domainguid, str)
1075             hostguid = samdb.searchone(basedn=domaindn, attribute="objectGUID",
1076                                        expression="(&(objectClass=computer)(cn=%s))" % names.hostname,
1077                                        scope=SCOPE_SUBTREE)
1078             assert isinstance(hostguid, str)
1079
1080             create_zone_file(paths.dns, setup_path, dnsdomain=names.dnsdomain,
1081                              domaindn=names.domaindn, hostip=hostip,
1082                              hostip6=hostip6, hostname=names.hostname,
1083                              dnspass=dnspass, realm=names.realm,
1084                              domainguid=domainguid, hostguid=hostguid)
1085
1086             create_named_conf(paths.namedconf, setup_path, realm=names.realm,
1087                               dnsdomain=names.dnsdomain, private_dir=paths.private_dir)
1088
1089             create_named_txt(paths.namedtxt, setup_path, realm=names.realm,
1090                               dnsdomain=names.dnsdomain, private_dir=paths.private_dir,
1091                               keytab_name=paths.dns_keytab)
1092             message("See %s for an example configuration include file for BIND" % paths.namedconf)
1093             message("and %s for further documentation required for secure DNS updates" % paths.namedtxt)
1094
1095             create_krb5_conf(paths.krb5conf, setup_path, dnsdomain=names.dnsdomain,
1096                              hostname=names.hostname, realm=names.realm)
1097             message("A Kerberos configuration suitable for Samba 4 has been generated at %s" % paths.krb5conf)
1098
1099     create_phpldapadmin_config(paths.phpldapadminconfig, setup_path, 
1100                                ldapi_url)
1101
1102     message("Please install the phpLDAPadmin configuration located at %s into /etc/phpldapadmin/config.php" % paths.phpldapadminconfig)
1103
1104     message("Once the above files are installed, your Samba4 server will be ready to use")
1105     message("Server Role:    %s" % serverrole)
1106     message("Hostname:       %s" % names.hostname)
1107     message("NetBIOS Domain: %s" % names.domain)
1108     message("DNS Domain:     %s" % names.dnsdomain)
1109     message("DOMAIN SID:     %s" % str(domainsid))
1110     message("Admin password: %s" % adminpass)
1111
1112     result = ProvisionResult()
1113     result.domaindn = domaindn
1114     result.paths = paths
1115     result.lp = lp
1116     result.samdb = samdb
1117     return result
1118
1119
1120 def provision_become_dc(setup_dir=None,
1121                         smbconf=None, targetdir=None, realm=None, 
1122                         rootdn=None, domaindn=None, schemadn=None, configdn=None,
1123                         serverdn=None,
1124                         domain=None, hostname=None, domainsid=None, 
1125                         adminpass=None, krbtgtpass=None, domainguid=None, 
1126                         policyguid=None, invocationid=None, machinepass=None, 
1127                         dnspass=None, root=None, nobody=None, nogroup=None, users=None, 
1128                         wheel=None, backup=None, aci=None, serverrole=None, 
1129                         ldap_backend=None, ldap_backend_type=None, sitename=None):
1130
1131     def message(text):
1132         """print a message if quiet is not set."""
1133         print text
1134
1135     return provision(setup_dir, message, system_session(), None,
1136               smbconf=smbconf, targetdir=targetdir, samdb_fill=FILL_DRS, realm=realm, 
1137               rootdn=rootdn, domaindn=domaindn, schemadn=schemadn, configdn=configdn, serverdn=serverdn,
1138               domain=domain, hostname=hostname, hostip="127.0.0.1", domainsid=domainsid, machinepass=machinepass, serverrole="domain controller", sitename=sitename)
1139     
1140
1141 def setup_db_config(setup_path, dbdir):
1142     """Setup a Berkeley database.
1143     
1144     :param setup_path: Setup path function.
1145     :param dbdir: Database directory."""
1146     if not os.path.isdir(os.path.join(dbdir, "bdb-logs")):
1147         os.makedirs(os.path.join(dbdir, "bdb-logs"), 0700)
1148     if not os.path.isdir(os.path.join(dbdir, "tmp")):
1149         os.makedirs(os.path.join(dbdir, "tmp"), 0700)
1150     
1151     setup_file(setup_path("DB_CONFIG"), os.path.join(dbdir, "DB_CONFIG"),
1152                {"LDAPDBDIR": dbdir})
1153     
1154
1155
1156 def provision_backend(setup_dir=None, message=None,
1157                       smbconf=None, targetdir=None, realm=None, 
1158                       rootdn=None, domaindn=None, schemadn=None, configdn=None,
1159                       domain=None, hostname=None, adminpass=None, root=None, serverrole=None, 
1160                       ldap_backend_type=None, ldap_backend_port=None,
1161                       ol_mmr_urls=None):
1162
1163     def setup_path(file):
1164         return os.path.join(setup_dir, file)
1165
1166     if hostname is None:
1167         hostname = socket.gethostname().split(".")[0].lower()
1168
1169     if root is None:
1170         root = findnss(pwd.getpwnam, ["root"])[0]
1171
1172     if adminpass is None:
1173         adminpass = misc.random_password(12)
1174
1175     if targetdir is not None:
1176         if (not os.path.exists(os.path.join(targetdir, "etc"))):
1177             os.makedirs(os.path.join(targetdir, "etc"))
1178         smbconf = os.path.join(targetdir, "etc", "smb.conf")
1179
1180     # only install a new smb.conf if there isn't one there already
1181     if not os.path.exists(smbconf):
1182         make_smbconf(smbconf, setup_path, hostname, domain, realm, serverrole, 
1183                      targetdir)
1184
1185     lp = param.LoadParm()
1186     lp.load(smbconf)
1187
1188     names = guess_names(lp=lp, hostname=hostname, domain=domain, 
1189                         dnsdomain=realm, serverrole=serverrole, 
1190                         rootdn=rootdn, domaindn=domaindn, configdn=configdn, 
1191                         schemadn=schemadn)
1192
1193     paths = provision_paths_from_lp(lp, names.dnsdomain)
1194
1195     if not os.path.isdir(paths.ldapdir):
1196         os.makedirs(paths.ldapdir, 0700)
1197     schemadb_path = os.path.join(paths.ldapdir, "schema-tmp.ldb")
1198     try:
1199         os.unlink(schemadb_path)
1200     except:
1201         pass
1202
1203     schemadb = Ldb(schemadb_path, lp=lp)
1204  
1205     prefixmap = open(setup_path("prefixMap.txt"), 'r').read()
1206
1207     setup_add_ldif(schemadb, setup_path("provision_schema_basedn.ldif"), 
1208                    {"SCHEMADN": names.schemadn,
1209                     "ACI": "#",
1210                     })
1211     setup_modify_ldif(schemadb, 
1212                       setup_path("provision_schema_basedn_modify.ldif"), \
1213                           {"SCHEMADN": names.schemadn,
1214                            "NETBIOSNAME": names.netbiosname,
1215                            "DEFAULTSITE": DEFAULTSITE,
1216                            "CONFIGDN": names.configdn,
1217                            "SERVERDN": names.serverdn,
1218                            "PREFIXMAP_B64": b64encode(prefixmap)
1219                            })
1220     
1221     setup_add_ldif(schemadb, setup_path("schema_samba4.ldif"), 
1222                    {"SCHEMADN": names.schemadn })
1223     setup_add_ldif(schemadb, setup_path("schema.ldif"), 
1224                    {"SCHEMADN": names.schemadn})
1225
1226     if ldap_backend_type == "fedora-ds":
1227         if ldap_backend_port is not None:
1228             serverport = "ServerPort=%d" % ldap_backend_port
1229         else:
1230             serverport = ""
1231
1232         setup_file(setup_path("fedorads.inf"), paths.fedoradsinf, 
1233                    {"ROOT": root,
1234                     "HOSTNAME": hostname,
1235                     "DNSDOMAIN": names.dnsdomain,
1236                     "LDAPDIR": paths.ldapdir,
1237                     "DOMAINDN": names.domaindn,
1238                     "LDAPMANAGERDN": names.ldapmanagerdn,
1239                     "LDAPMANAGERPASS": adminpass, 
1240                     "SERVERPORT": serverport})
1241         
1242         setup_file(setup_path("fedorads-partitions.ldif"), paths.fedoradspartitions, 
1243                    {"CONFIGDN": names.configdn,
1244                     "SCHEMADN": names.schemadn,
1245                     })
1246         
1247         mapping = "schema-map-fedora-ds-1.0"
1248         backend_schema = "99_ad.ldif"
1249         
1250         slapdcommand="Initailise Fedora DS with: setup-ds.pl --file=%s" % paths.fedoradsinf
1251        
1252         ldapuser = "--simple-bind-dn=" + names.ldapmanagerdn
1253
1254     elif ldap_backend_type == "openldap":
1255         attrs = ["linkID", "lDAPDisplayName"]
1256         res = schemadb.search(expression="(&(&(linkID=*)(!(linkID:1.2.840.113556.1.4.803:=1)))(objectclass=attributeSchema))", base=names.schemadn, scope=SCOPE_SUBTREE, attrs=attrs)
1257
1258         memberof_config = "# Generated from schema in %s\n" % schemadb_path
1259         refint_attributes = ""
1260         for i in range (0, len(res)):
1261             expression = "(&(objectclass=attributeSchema)(linkID=%d))" % (int(res[i]["linkID"][0])+1)
1262             target = schemadb.searchone(basedn=names.schemadn, 
1263                                         expression=expression, 
1264                                         attribute="lDAPDisplayName", 
1265                                         scope=SCOPE_SUBTREE)
1266             if target is not None:
1267                 refint_attributes = refint_attributes + " " + target + " " + res[i]["lDAPDisplayName"][0]
1268             
1269                 memberof_config += read_and_sub_file(setup_path("memberof.conf"),
1270                                                      { "MEMBER_ATTR" : str(res[i]["lDAPDisplayName"][0]),
1271                                                        "MEMBEROF_ATTR" : str(target) })
1272
1273         refint_config = read_and_sub_file(setup_path("refint.conf"),
1274                                             { "LINK_ATTRS" : refint_attributes})
1275
1276 # generate serverids, ldap-urls and syncrepl-blocks for mmr hosts
1277         mmr_on_config = ""
1278         mmr_serverids_config = ""
1279         mmr_syncrepl_schema_config = "" 
1280         mmr_syncrepl_config_config = "" 
1281         mmr_syncrepl_user_config = "" 
1282         
1283         if ol_mmr_urls is not None:
1284                 url_list=filter(None,ol_mmr_urls.split(' ')) 
1285                 if (len(url_list) == 1):
1286                     url_list=filter(None,ol_mmr_urls.split(',')) 
1287                      
1288
1289                 mmr_on_config = "MirrorMode On"
1290                 serverid=0
1291                 for url in url_list:
1292                         serverid=serverid+1
1293                         mmr_serverids_config += read_and_sub_file(setup_path("mmr_serverids.conf"),
1294                                                                      { "SERVERID" : str(serverid),
1295                                                                        "LDAPSERVER" : url })
1296                         rid=serverid*10
1297                         rid=rid+1
1298                         mmr_syncrepl_schema_config += read_and_sub_file(setup_path("mmr_syncrepl.conf"),
1299                                                                      {  "RID" : str(rid),
1300                                                                         "MMRDN": names.schemadn,
1301                                                                         "LDAPSERVER" : url,
1302                                                                         "MMR_PASSWORD": adminpass})
1303
1304                         rid=rid+1
1305                         mmr_syncrepl_config_config += read_and_sub_file(setup_path("mmr_syncrepl.conf"),
1306                                                                      {  "RID" : str(rid),
1307                                                                         "MMRDN": names.configdn,
1308                                                                         "LDAPSERVER" : url,
1309                                                                         "MMR_PASSWORD": adminpass})
1310
1311                         rid=rid+1
1312                         mmr_syncrepl_user_config += read_and_sub_file(setup_path("mmr_syncrepl.conf"),
1313                                                                      {  "RID" : str(rid),
1314                                                                         "MMRDN": names.domaindn,
1315                                                                         "LDAPSERVER" : url,
1316                                                                         "MMR_PASSWORD": adminpass })
1317
1318
1319         setup_file(setup_path("slapd.conf"), paths.slapdconf,
1320                    {"DNSDOMAIN": names.dnsdomain,
1321                     "LDAPDIR": paths.ldapdir,
1322                     "DOMAINDN": names.domaindn,
1323                     "CONFIGDN": names.configdn,
1324                     "SCHEMADN": names.schemadn,
1325                     "MEMBEROF_CONFIG": memberof_config,
1326                     "MIRRORMODE": mmr_on_config,
1327                     "MMR_SERVERIDS_CONFIG": mmr_serverids_config,
1328                     "MMR_SYNCREPL_SCHEMA_CONFIG": mmr_syncrepl_schema_config,
1329                     "MMR_SYNCREPL_CONFIG_CONFIG": mmr_syncrepl_config_config,
1330                     "MMR_SYNCREPL_USER_CONFIG": mmr_syncrepl_user_config,
1331                     "MMR_PASSWORD": adminpass,
1332                     "REFINT_CONFIG": refint_config})
1333         setup_file(setup_path("modules.conf"), paths.modulesconf,
1334                    {"REALM": names.realm})
1335         
1336         setup_db_config(setup_path, os.path.join(paths.ldapdir, "db", "user"))
1337         setup_db_config(setup_path, os.path.join(paths.ldapdir, "db", "config"))
1338         setup_db_config(setup_path, os.path.join(paths.ldapdir, "db", "schema"))
1339
1340         if not os.path.exists(os.path.join(paths.ldapdir, "db", "samba",  "cn=samba")):
1341             os.makedirs(os.path.join(paths.ldapdir, "db", "samba",  "cn=samba"), 0700)
1342
1343         setup_file(setup_path("cn=samba.ldif"), 
1344                    os.path.join(paths.ldapdir, "db", "samba",  "cn=samba.ldif"),
1345                    { "UUID": str(uuid.uuid4()), 
1346                      "LDAPTIME": timestring(int(time.time()))} )
1347         setup_file(setup_path("cn=samba-admin.ldif"), 
1348                               os.path.join(paths.ldapdir, "db", "samba",  "cn=samba", "cn=samba-admin.ldif"),
1349                               {"LDAPADMINPASS_B64": b64encode(adminpass),
1350                                "UUID": str(uuid.uuid4()), 
1351                                "LDAPTIME": timestring(int(time.time()))} )
1352
1353         mapping = "schema-map-openldap-2.3"
1354         backend_schema = "backend-schema.schema"
1355
1356         ldapi_uri = "ldapi://" + urllib.quote(os.path.join(paths.private_dir, "ldap", "ldapi"), safe="")
1357         if ldap_backend_port is not None:
1358             server_port_string = " -h ldap://0.0.0.0:%d" % ldap_backend_port
1359         else:
1360             server_port_string = ""
1361
1362         slapdcommand="Start slapd with:    slapd -f " + paths.ldapdir + "/slapd.conf -h " + ldapi_uri + server_port_string
1363
1364         ldapuser = "--username=samba-admin"
1365
1366             
1367     schema_command = "bin/ad2oLschema --option=convert:target=" + ldap_backend_type + " -I " + setup_path(mapping) + " -H tdb://" + schemadb_path + " -O " + os.path.join(paths.ldapdir, backend_schema)
1368             
1369     os.system(schema_command)
1370
1371     message("Your %s Backend for Samba4 is now configured, and is ready to be started" % ldap_backend_type)
1372     message("Server Role:         %s" % serverrole)
1373     message("Hostname:            %s" % names.hostname)
1374     message("DNS Domain:          %s" % names.dnsdomain)
1375     message("Base DN:             %s" % names.domaindn)
1376
1377     if ldap_backend_type == "openldap":
1378         message("LDAP admin user:     samba-admin")
1379     else:
1380         message("LDAP admin DN:       %s" % names.ldapmanagerdn)
1381
1382     message("LDAP admin password: %s" % adminpass)
1383     message(slapdcommand)
1384     message("Run provision with:  --ldap-backend=ldapi --ldap-backend-type=" + ldap_backend_type + " --password=" + adminpass + " " + ldapuser)
1385
1386 def create_phpldapadmin_config(path, setup_path, ldapi_uri):
1387     """Create a PHP LDAP admin configuration file.
1388
1389     :param path: Path to write the configuration to.
1390     :param setup_path: Function to generate setup paths.
1391     """
1392     setup_file(setup_path("phpldapadmin-config.php"), path, 
1393             {"S4_LDAPI_URI": ldapi_uri})
1394
1395
1396 def create_zone_file(path, setup_path, dnsdomain, domaindn, 
1397                      hostip, hostip6, hostname, dnspass, realm, domainguid, hostguid):
1398     """Write out a DNS zone file, from the info in the current database.
1399
1400     :param path: Path of the new zone file.
1401     :param setup_path: Setup path function.
1402     :param dnsdomain: DNS Domain name
1403     :param domaindn: DN of the Domain
1404     :param hostip: Local IPv4 IP
1405     :param hostip6: Local IPv6 IP
1406     :param hostname: Local hostname
1407     :param dnspass: Password for DNS
1408     :param realm: Realm name
1409     :param domainguid: GUID of the domain.
1410     :param hostguid: GUID of the host.
1411     """
1412     assert isinstance(domainguid, str)
1413
1414     if hostip6 is not None:
1415         hostip6_base_line = "            IN AAAA    " + hostip6
1416         hostip6_host_line = hostname + "        IN AAAA    " + hostip6
1417     else:
1418         hostip6_base_line = ""
1419         hostip6_host_line = ""
1420
1421     setup_file(setup_path("provision.zone"), path, {
1422             "DNSPASS_B64": b64encode(dnspass),
1423             "HOSTNAME": hostname,
1424             "DNSDOMAIN": dnsdomain,
1425             "REALM": realm,
1426             "HOSTIP": hostip,
1427             "DOMAINGUID": domainguid,
1428             "DATESTRING": time.strftime("%Y%m%d%H"),
1429             "DEFAULTSITE": DEFAULTSITE,
1430             "HOSTGUID": hostguid,
1431             "HOSTIP6_BASE_LINE": hostip6_base_line,
1432             "HOSTIP6_HOST_LINE": hostip6_host_line,
1433         })
1434
1435
1436 def create_named_conf(path, setup_path, realm, dnsdomain,
1437                       private_dir):
1438     """Write out a file containing zone statements suitable for inclusion in a
1439     named.conf file (including GSS-TSIG configuration).
1440     
1441     :param path: Path of the new named.conf file.
1442     :param setup_path: Setup path function.
1443     :param realm: Realm name
1444     :param dnsdomain: DNS Domain name
1445     :param private_dir: Path to private directory
1446     :param keytab_name: File name of DNS keytab file
1447     """
1448
1449     setup_file(setup_path("named.conf"), path, {
1450             "DNSDOMAIN": dnsdomain,
1451             "REALM": realm,
1452             "REALM_WC": "*." + ".".join(realm.split(".")[1:]),
1453             "PRIVATE_DIR": private_dir
1454             })
1455
1456 def create_named_txt(path, setup_path, realm, dnsdomain,
1457                       private_dir, keytab_name):
1458     """Write out a file containing zone statements suitable for inclusion in a
1459     named.conf file (including GSS-TSIG configuration).
1460     
1461     :param path: Path of the new named.conf file.
1462     :param setup_path: Setup path function.
1463     :param realm: Realm name
1464     :param dnsdomain: DNS Domain name
1465     :param private_dir: Path to private directory
1466     :param keytab_name: File name of DNS keytab file
1467     """
1468
1469     setup_file(setup_path("named.txt"), path, {
1470             "DNSDOMAIN": dnsdomain,
1471             "REALM": realm,
1472             "DNS_KEYTAB": keytab_name,
1473             "DNS_KEYTAB_ABS": os.path.join(private_dir, keytab_name),
1474             "PRIVATE_DIR": private_dir
1475         })
1476
1477 def create_krb5_conf(path, setup_path, dnsdomain, hostname, realm):
1478     """Write out a file containing zone statements suitable for inclusion in a
1479     named.conf file (including GSS-TSIG configuration).
1480     
1481     :param path: Path of the new named.conf file.
1482     :param setup_path: Setup path function.
1483     :param dnsdomain: DNS Domain name
1484     :param hostname: Local hostname
1485     :param realm: Realm name
1486     """
1487
1488     setup_file(setup_path("krb5.conf"), path, {
1489             "DNSDOMAIN": dnsdomain,
1490             "HOSTNAME": hostname,
1491             "REALM": realm,
1492         })
1493
1494
1495 def load_schema(setup_path, samdb, schemadn, netbiosname, configdn, sitename,
1496                 serverdn, servername):
1497     """Load schema for the SamDB.
1498     
1499     :param samdb: Load a schema into a SamDB.
1500     :param setup_path: Setup path function.
1501     :param schemadn: DN of the schema
1502     :param netbiosname: NetBIOS name of the host.
1503     :param configdn: DN of the configuration
1504     :param serverdn: DN of the server
1505     :param servername: Host name of the server
1506     """
1507     schema_data = open(setup_path("schema.ldif"), 'r').read()
1508     schema_data += open(setup_path("schema_samba4.ldif"), 'r').read()
1509     schema_data = substitute_var(schema_data, {"SCHEMADN": schemadn})
1510     check_all_substituted(schema_data)
1511     prefixmap = open(setup_path("prefixMap.txt"), 'r').read()
1512     prefixmap = b64encode(prefixmap)
1513
1514     head_data = open(setup_path("provision_schema_basedn_modify.ldif"), 'r').read()
1515     head_data = substitute_var(head_data, {
1516                     "SCHEMADN": schemadn,
1517                     "NETBIOSNAME": netbiosname,
1518                     "CONFIGDN": configdn,
1519                     "DEFAULTSITE": sitename,
1520                     "PREFIXMAP_B64": prefixmap,
1521                     "SERVERDN": serverdn,
1522                     "SERVERNAME": servername,
1523     })
1524     check_all_substituted(head_data)
1525     samdb.attach_schema_from_ldif(head_data, schema_data)
1526