samba-tool domian join: Only print adminpass warning on subdomain creation
[metze/samba/wip.git] / python / samba / netcmd / domain.py
1 # domain management
2 #
3 # Copyright Matthias Dieter Wallnoefer 2009
4 # Copyright Andrew Kroeger 2009
5 # Copyright Jelmer Vernooij 2007-2012
6 # Copyright Giampaolo Lauria 2011
7 # Copyright Matthieu Patou <mat@matws.net> 2011
8 # Copyright Andrew Bartlett 2008
9 # Copyright Stefan Metzmacher 2012
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 import samba.getopt as options
26 import ldb
27 import string
28 import os
29 import sys
30 import tempfile
31 import logging
32 from samba.net import Net, LIBNET_JOIN_AUTOMATIC
33 import samba.ntacls
34 from samba.join import join_RODC, join_DC, join_subdomain
35 from samba.auth import system_session
36 from samba.samdb import SamDB
37 from samba.dcerpc import drsuapi
38 from samba.dcerpc.samr import DOMAIN_PASSWORD_COMPLEX, DOMAIN_PASSWORD_STORE_CLEARTEXT
39 from samba.netcmd import (
40     Command,
41     CommandError,
42     SuperCommand,
43     Option
44     )
45 from samba.netcmd.common import netcmd_get_domain_infos_via_cldap
46 from samba.samba3 import Samba3
47 from samba.samba3 import param as s3param
48 from samba.upgrade import upgrade_from_samba3
49 from samba.drs_utils import (
50                             sendDsReplicaSync, drsuapi_connect, drsException,
51                             sendRemoveDsServer)
52
53
54 from samba.dsdb import (
55     DS_DOMAIN_FUNCTION_2000,
56     DS_DOMAIN_FUNCTION_2003,
57     DS_DOMAIN_FUNCTION_2003_MIXED,
58     DS_DOMAIN_FUNCTION_2008,
59     DS_DOMAIN_FUNCTION_2008_R2,
60     DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL,
61     DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL,
62     UF_WORKSTATION_TRUST_ACCOUNT,
63     UF_SERVER_TRUST_ACCOUNT,
64     UF_TRUSTED_FOR_DELEGATION
65     )
66
67 from samba.credentials import DONT_USE_KERBEROS
68 from samba.provision import (
69     provision,
70     FILL_FULL,
71     FILL_NT4SYNC,
72     FILL_DRS,
73     ProvisioningError,
74     )
75
76 def get_testparm_var(testparm, smbconf, varname):
77     cmd = "%s -s -l --parameter-name='%s' %s 2>/dev/null" % (testparm, varname, smbconf)
78     output = os.popen(cmd, 'r').readline()
79     return output.strip()
80
81 try:
82    import samba.dckeytab
83    class cmd_domain_export_keytab(Command):
84        """Dump Kerberos keys of the domain into a keytab."""
85
86        synopsis = "%prog <keytab> [options]"
87
88        takes_optiongroups = {
89            "sambaopts": options.SambaOptions,
90            "credopts": options.CredentialsOptions,
91            "versionopts": options.VersionOptions,
92            }
93
94        takes_options = [
95            Option("--principal", help="extract only this principal", type=str),
96            ]
97
98        takes_args = ["keytab"]
99
100        def run(self, keytab, credopts=None, sambaopts=None, versionopts=None, principal=None):
101            lp = sambaopts.get_loadparm()
102            net = Net(None, lp)
103            net.export_keytab(keytab=keytab, principal=principal)
104 except:
105    cmd_domain_export_keytab = None
106
107
108 class cmd_domain_info(Command):
109     """Print basic info about a domain and the DC passed as parameter."""
110
111     synopsis = "%prog <ip_address> [options]"
112
113     takes_options = [
114         ]
115
116     takes_optiongroups = {
117         "sambaopts": options.SambaOptions,
118         "credopts": options.CredentialsOptions,
119         "versionopts": options.VersionOptions,
120         }
121
122     takes_args = ["address"]
123
124     def run(self, address, credopts=None, sambaopts=None, versionopts=None):
125         lp = sambaopts.get_loadparm()
126         try:
127             res = netcmd_get_domain_infos_via_cldap(lp, None, address)
128         except RuntimeError:
129             raise CommandError("Invalid IP address '" + address + "'!")
130         self.outf.write("Forest           : %s\n" % res.forest)
131         self.outf.write("Domain           : %s\n" % res.dns_domain)
132         self.outf.write("Netbios domain   : %s\n" % res.domain_name)
133         self.outf.write("DC name          : %s\n" % res.pdc_dns_name)
134         self.outf.write("DC netbios name  : %s\n" % res.pdc_name)
135         self.outf.write("Server site      : %s\n" % res.server_site)
136         self.outf.write("Client site      : %s\n" % res.client_site)
137
138
139 class cmd_domain_provision(Command):
140     """Provision a domain."""
141
142     synopsis = "%prog [options]"
143
144     takes_optiongroups = {
145         "sambaopts": options.SambaOptions,
146         "versionopts": options.VersionOptions,
147         "credopts": options.CredentialsOptions,
148     }
149
150     takes_options = [
151          Option("--interactive", help="Ask for names", action="store_true"),
152          Option("--domain", type="string", metavar="DOMAIN",
153                 help="set domain"),
154          Option("--domain-guid", type="string", metavar="GUID",
155                 help="set domainguid (otherwise random)"),
156          Option("--domain-sid", type="string", metavar="SID",
157                 help="set domainsid (otherwise random)"),
158          Option("--ntds-guid", type="string", metavar="GUID",
159                 help="set NTDS object GUID (otherwise random)"),
160          Option("--invocationid", type="string", metavar="GUID",
161                 help="set invocationid (otherwise random)"),
162          Option("--host-name", type="string", metavar="HOSTNAME",
163                 help="set hostname"),
164          Option("--host-ip", type="string", metavar="IPADDRESS",
165                 help="set IPv4 ipaddress"),
166          Option("--host-ip6", type="string", metavar="IP6ADDRESS",
167                 help="set IPv6 ipaddress"),
168          Option("--adminpass", type="string", metavar="PASSWORD",
169                 help="choose admin password (otherwise random)"),
170          Option("--krbtgtpass", type="string", metavar="PASSWORD",
171                 help="choose krbtgt password (otherwise random)"),
172          Option("--machinepass", type="string", metavar="PASSWORD",
173                 help="choose machine password (otherwise random)"),
174          Option("--dns-backend", type="choice", metavar="NAMESERVER-BACKEND",
175                 choices=["SAMBA_INTERNAL", "BIND9_FLATFILE", "BIND9_DLZ", "NONE"],
176                 help="The DNS server backend. SAMBA_INTERNAL is the builtin name server (default), "
177                      "BIND9_FLATFILE uses bind9 text database to store zone information, "
178                      "BIND9_DLZ uses samba4 AD to store zone information, "
179                      "NONE skips the DNS setup entirely (not recommended)",
180                 default="SAMBA_INTERNAL"),
181          Option("--dnspass", type="string", metavar="PASSWORD",
182                 help="choose dns password (otherwise random)"),
183          Option("--ldapadminpass", type="string", metavar="PASSWORD",
184                 help="choose password to set between Samba and it's LDAP backend (otherwise random)"),
185          Option("--root", type="string", metavar="USERNAME",
186                 help="choose 'root' unix username"),
187          Option("--nobody", type="string", metavar="USERNAME",
188                 help="choose 'nobody' user"),
189          Option("--users", type="string", metavar="GROUPNAME",
190                 help="choose 'users' group"),
191          Option("--quiet", help="Be quiet", action="store_true"),
192          Option("--blank", action="store_true",
193                 help="do not add users or groups, just the structure"),
194          Option("--ldap-backend-type", type="choice", metavar="LDAP-BACKEND-TYPE",
195                 help="Test initialisation support for unsupported LDAP backend type (fedora-ds or openldap) DO NOT USE",
196                 choices=["fedora-ds", "openldap"]),
197          Option("--server-role", type="choice", metavar="ROLE",
198                 choices=["domain controller", "dc", "member server", "member", "standalone"],
199                 help="The server role (domain controller | dc | member server | member | standalone). Default is dc.",
200                 default="domain controller"),
201          Option("--function-level", type="choice", metavar="FOR-FUN-LEVEL",
202                 choices=["2000", "2003", "2008", "2008_R2"],
203                 help="The domain and forest function level (2000 | 2003 | 2008 | 2008_R2 - always native). Default is (Windows) 2003 Native.",
204                 default="2003"),
205          Option("--next-rid", type="int", metavar="NEXTRID", default=1000,
206                 help="The initial nextRid value (only needed for upgrades).  Default is 1000."),
207          Option("--partitions-only",
208                 help="Configure Samba's partitions, but do not modify them (ie, join a BDC)", action="store_true"),
209          Option("--targetdir", type="string", metavar="DIR",
210                 help="Set target directory"),
211          Option("--ol-mmr-urls", type="string", metavar="LDAPSERVER",
212                 help="List of LDAP-URLS [ ldap://<FQHN>:<PORT>/  (where <PORT> has to be different than 389!) ] separated with comma (\",\") for use with OpenLDAP-MMR (Multi-Master-Replication), e.g.: \"ldap://s4dc1:9000,ldap://s4dc2:9000\""),
213          Option("--use-xattrs", type="choice", choices=["yes", "no", "auto"], help="Define if we should use the native fs capabilities or a tdb file for storing attributes likes ntacl, auto tries to make an inteligent guess based on the user rights and system capabilities", default="auto"),
214          Option("--use-ntvfs", action="store_true", help="Use NTVFS for the fileserver (default = no)"),
215          Option("--use-rfc2307", action="store_true", help="Use AD to store posix attributes (default = no)"),
216         ]
217     takes_args = []
218
219     def run(self, sambaopts=None, credopts=None, versionopts=None,
220             interactive=None,
221             domain=None,
222             domain_guid=None,
223             domain_sid=None,
224             ntds_guid=None,
225             invocationid=None,
226             host_name=None,
227             host_ip=None,
228             host_ip6=None,
229             adminpass=None,
230             krbtgtpass=None,
231             machinepass=None,
232             dns_backend=None,
233             dns_forwarder=None,
234             dnspass=None,
235             ldapadminpass=None,
236             root=None,
237             nobody=None,
238             users=None,
239             quiet=None,
240             blank=None,
241             ldap_backend_type=None,
242             server_role=None,
243             function_level=None,
244             next_rid=None,
245             partitions_only=None,
246             targetdir=None,
247             ol_mmr_urls=None,
248             use_xattrs=None,
249             use_ntvfs=None,
250             use_rfc2307=None):
251
252         self.logger = self.get_logger("provision")
253         if quiet:
254             self.logger.setLevel(logging.WARNING)
255         else:
256             self.logger.setLevel(logging.INFO)
257
258         lp = sambaopts.get_loadparm()
259         smbconf = lp.configfile
260
261         creds = credopts.get_credentials(lp)
262
263         creds.set_kerberos_state(DONT_USE_KERBEROS)
264
265         if dns_forwarder is not None:
266             suggested_forwarder = dns_forwarder
267         else:
268             suggested_forwarder = self._get_nameserver_ip()
269             if suggested_forwarder is None:
270                 suggested_forwarder = "none"
271
272         if len(self.raw_argv) == 1:
273             interactive = True
274
275         if interactive:
276             from getpass import getpass
277             import socket
278
279             def ask(prompt, default=None):
280                 if default is not None:
281                     print "%s [%s]: " % (prompt, default),
282                 else:
283                     print "%s: " % (prompt,),
284                 return sys.stdin.readline().rstrip("\n") or default
285
286             try:
287                 default = socket.getfqdn().split(".", 1)[1].upper()
288             except IndexError:
289                 default = None
290             realm = ask("Realm", default)
291             if realm in (None, ""):
292                 raise CommandError("No realm set!")
293
294             try:
295                 default = realm.split(".")[0]
296             except IndexError:
297                 default = None
298             domain = ask("Domain", default)
299             if domain is None:
300                 raise CommandError("No domain set!")
301
302             server_role = ask("Server Role (dc, member, standalone)", "dc")
303
304             dns_backend = ask("DNS backend (SAMBA_INTERNAL, BIND9_FLATFILE, BIND9_DLZ, NONE)", "SAMBA_INTERNAL")
305             if dns_backend in (None, ''):
306                 raise CommandError("No DNS backend set!")
307
308             if dns_backend == "SAMBA_INTERNAL":
309                 dns_forwarder = ask("DNS forwarder IP address (write 'none' to disable forwarding)", suggested_forwarder)
310                 if dns_forwarder.lower() in (None, 'none'):
311                     suggested_forwarder = None
312                     dns_forwarder = None
313
314             while True:
315                 adminpassplain = getpass("Administrator password: ")
316                 if not adminpassplain:
317                     self.errf.write("Invalid administrator password.\n")
318                 else:
319                     adminpassverify = getpass("Retype password: ")
320                     if not adminpassplain == adminpassverify:
321                         self.errf.write("Sorry, passwords do not match.\n")
322                     else:
323                         adminpass = adminpassplain
324                         break
325
326         else:
327             realm = sambaopts._lp.get('realm')
328             if realm is None:
329                 raise CommandError("No realm set!")
330             if domain is None:
331                 raise CommandError("No domain set!")
332
333         if not adminpass:
334             self.logger.info("Administrator password will be set randomly!")
335
336         if function_level == "2000":
337             dom_for_fun_level = DS_DOMAIN_FUNCTION_2000
338         elif function_level == "2003":
339             dom_for_fun_level = DS_DOMAIN_FUNCTION_2003
340         elif function_level == "2008":
341             dom_for_fun_level = DS_DOMAIN_FUNCTION_2008
342         elif function_level == "2008_R2":
343             dom_for_fun_level = DS_DOMAIN_FUNCTION_2008_R2
344
345         if dns_backend == "SAMBA_INTERNAL" and dns_forwarder is None:
346             dns_forwarder = suggested_forwarder
347
348         samdb_fill = FILL_FULL
349         if blank:
350             samdb_fill = FILL_NT4SYNC
351         elif partitions_only:
352             samdb_fill = FILL_DRS
353
354         if targetdir is not None:
355             if not os.path.isdir(targetdir):
356                 os.mkdir(targetdir)
357
358         eadb = True
359
360         if use_xattrs == "yes":
361             eadb = False
362         elif use_xattrs == "auto" and not lp.get("posix:eadb"):
363             if targetdir:
364                 file = tempfile.NamedTemporaryFile(dir=os.path.abspath(targetdir))
365             else:
366                 file = tempfile.NamedTemporaryFile(dir=os.path.abspath(os.path.dirname(lp.get("private dir"))))
367             try:
368                 try:
369                     samba.ntacls.setntacl(lp, file.name,
370                                           "O:S-1-5-32G:S-1-5-32", "S-1-5-32", "native")
371                     eadb = False
372                 except Exception:
373                     self.logger.info("You are not root or your system do not support xattr, using tdb backend for attributes. ")
374             finally:
375                 file.close()
376
377         if eadb:
378             self.logger.info("not using extended attributes to store ACLs and other metadata. If you intend to use this provision in production, rerun the script as root on a system supporting xattrs.")
379
380         session = system_session()
381         try:
382             result = provision(self.logger,
383                   session, creds, smbconf=smbconf, targetdir=targetdir,
384                   samdb_fill=samdb_fill, realm=realm, domain=domain,
385                   domainguid=domain_guid, domainsid=domain_sid,
386                   hostname=host_name,
387                   hostip=host_ip, hostip6=host_ip6,
388                   ntdsguid=ntds_guid,
389                   invocationid=invocationid, adminpass=adminpass,
390                   krbtgtpass=krbtgtpass, machinepass=machinepass,
391                   dns_backend=dns_backend, dns_forwarder=dns_forwarder,
392                   dnspass=dnspass, root=root, nobody=nobody,
393                   users=users,
394                   serverrole=server_role, dom_for_fun_level=dom_for_fun_level,
395                   backend_type=ldap_backend_type,
396                   ldapadminpass=ldapadminpass, ol_mmr_urls=ol_mmr_urls,
397                   useeadb=eadb, next_rid=next_rid, lp=lp, use_ntvfs=use_ntvfs,
398                   use_rfc2307=use_rfc2307, skip_sysvolacl=False)
399         except ProvisioningError, e:
400             raise CommandError("Provision failed", e)
401
402         result.report_logger(self.logger)
403
404     def _get_nameserver_ip(self):
405         """Grab the nameserver IP address from /etc/resolv.conf."""
406         from os import path
407         RESOLV_CONF="/etc/resolv.conf"
408
409         if not path.isfile(RESOLV_CONF):
410             self.logger.warning("Failed to locate %s" % RESOLV_CONF)
411             return None
412
413         handle = None
414         try:
415             handle = open(RESOLV_CONF, 'r')
416             for line in handle:
417                 if not line.startswith('nameserver'):
418                     continue
419                 # we want the last non-space continuous string of the line
420                 return line.strip().split()[-1]
421         finally:
422             if handle is not None:
423                 handle.close()
424
425         self.logger.warning("No nameserver found in %s" % RESOLV_CONF)
426
427
428 class cmd_domain_dcpromo(Command):
429     """Promote an existing domain member or NT4 PDC to an AD DC."""
430
431     synopsis = "%prog <dnsdomain> [DC|RODC] [options]"
432
433     takes_optiongroups = {
434         "sambaopts": options.SambaOptions,
435         "versionopts": options.VersionOptions,
436         "credopts": options.CredentialsOptions,
437     }
438
439     takes_options = [
440         Option("--server", help="DC to join", type=str),
441         Option("--site", help="site to join", type=str),
442         Option("--targetdir", help="where to store provision", type=str),
443         Option("--domain-critical-only",
444                help="only replicate critical domain objects",
445                action="store_true"),
446         Option("--machinepass", type=str, metavar="PASSWORD",
447                help="choose machine password (otherwise random)"),
448         Option("--use-ntvfs", help="Use NTVFS for the fileserver (default = no)",
449                action="store_true"),
450         Option("--dns-backend", type="choice", metavar="NAMESERVER-BACKEND",
451                choices=["SAMBA_INTERNAL", "BIND9_DLZ", "NONE"],
452                help="The DNS server backend. SAMBA_INTERNAL is the builtin name server (default), "
453                    "BIND9_DLZ uses samba4 AD to store zone information, "
454                    "NONE skips the DNS setup entirely (this DC will not be a DNS server)",
455                default="SAMBA_INTERNAL"),
456         Option("--quiet", help="Be quiet", action="store_true"),
457         Option("--verbose", help="Be verbose", action="store_true")
458         ]
459
460     takes_args = ["domain", "role?"]
461
462     def run(self, domain, role=None, sambaopts=None, credopts=None,
463             versionopts=None, server=None, site=None, targetdir=None,
464             domain_critical_only=False, parent_domain=None, machinepass=None,
465             use_ntvfs=False, dns_backend=None,
466             quiet=False, verbose=False):
467         lp = sambaopts.get_loadparm()
468         creds = credopts.get_credentials(lp)
469         net = Net(creds, lp, server=credopts.ipaddress)
470
471         if site is None:
472             site = "Default-First-Site-Name"
473
474         logger = self.get_logger()
475         if verbose:
476             logger.setLevel(logging.DEBUG)
477         elif quiet:
478             logger.setLevel(logging.WARNING)
479         else:
480             logger.setLevel(logging.INFO)
481
482         if site is None:
483             site = "Default-First-Site-Name"
484
485         netbios_name = lp.get("netbios name")
486
487         if not role is None:
488             role = role.upper()
489
490         if role == "DC":
491             join_DC(logger=logger, server=server, creds=creds, lp=lp, domain=domain,
492                     site=site, netbios_name=netbios_name, targetdir=targetdir,
493                     domain_critical_only=domain_critical_only,
494                     machinepass=machinepass, use_ntvfs=use_ntvfs,
495                     dns_backend=dns_backend,
496                     promote_existing=True)
497         elif role == "RODC":
498             join_RODC(logger=logger, server=server, creds=creds, lp=lp, domain=domain,
499                       site=site, netbios_name=netbios_name, targetdir=targetdir,
500                       domain_critical_only=domain_critical_only,
501                       machinepass=machinepass, use_ntvfs=use_ntvfs, dns_backend=dns_backend,
502                       promote_existing=True)
503         else:
504             raise CommandError("Invalid role '%s' (possible values: DC, RODC)" % role)
505
506
507 class cmd_domain_join(Command):
508     """Join domain as either member or backup domain controller."""
509
510     synopsis = "%prog <dnsdomain> [DC|RODC|MEMBER|SUBDOMAIN] [options]"
511
512     takes_optiongroups = {
513         "sambaopts": options.SambaOptions,
514         "versionopts": options.VersionOptions,
515         "credopts": options.CredentialsOptions,
516     }
517
518     takes_options = [
519         Option("--server", help="DC to join", type=str),
520         Option("--site", help="site to join", type=str),
521         Option("--targetdir", help="where to store provision", type=str),
522         Option("--parent-domain", help="parent domain to create subdomain under", type=str),
523         Option("--domain-critical-only",
524                help="only replicate critical domain objects",
525                action="store_true"),
526         Option("--machinepass", type=str, metavar="PASSWORD",
527                help="choose machine password (otherwise random)"),
528         Option("--adminpass", type="string", metavar="PASSWORD",
529                help="choose adminstrator password when joining as a subdomain (otherwise random)"),
530         Option("--use-ntvfs", help="Use NTVFS for the fileserver (default = no)",
531                action="store_true"),
532         Option("--dns-backend", type="choice", metavar="NAMESERVER-BACKEND",
533                choices=["SAMBA_INTERNAL", "BIND9_DLZ", "NONE"],
534                help="The DNS server backend. SAMBA_INTERNAL is the builtin name server (default), "
535                    "BIND9_DLZ uses samba4 AD to store zone information, "
536                    "NONE skips the DNS setup entirely (this DC will not be a DNS server)",
537                default="SAMBA_INTERNAL"),
538         Option("--quiet", help="Be quiet", action="store_true"),
539         Option("--verbose", help="Be verbose", action="store_true")
540        ]
541
542     takes_args = ["domain", "role?"]
543
544     def run(self, domain, role=None, sambaopts=None, credopts=None,
545             versionopts=None, server=None, site=None, targetdir=None,
546             domain_critical_only=False, parent_domain=None, machinepass=None,
547             use_ntvfs=False, dns_backend=None, adminpass=None,
548             quiet=False, verbose=False):
549         lp = sambaopts.get_loadparm()
550         creds = credopts.get_credentials(lp)
551         net = Net(creds, lp, server=credopts.ipaddress)
552
553         if site is None:
554             site = "Default-First-Site-Name"
555
556         logger = self.get_logger()
557         if verbose:
558             logger.setLevel(logging.DEBUG)
559         elif quiet:
560             logger.setLevel(logging.WARNING)
561         else:
562             logger.setLevel(logging.INFO)
563
564         netbios_name = lp.get("netbios name")
565
566         if not role is None:
567             role = role.upper()
568
569         if role is None or role == "MEMBER":
570             (join_password, sid, domain_name) = net.join_member(
571                 domain, netbios_name, LIBNET_JOIN_AUTOMATIC,
572                 machinepass=machinepass)
573
574             self.errf.write("Joined domain %s (%s)\n" % (domain_name, sid))
575         elif role == "DC":
576             join_DC(logger=logger, server=server, creds=creds, lp=lp, domain=domain,
577                     site=site, netbios_name=netbios_name, targetdir=targetdir,
578                     domain_critical_only=domain_critical_only,
579                     machinepass=machinepass, use_ntvfs=use_ntvfs, dns_backend=dns_backend)
580         elif role == "RODC":
581             join_RODC(logger=logger, server=server, creds=creds, lp=lp, domain=domain,
582                       site=site, netbios_name=netbios_name, targetdir=targetdir,
583                       domain_critical_only=domain_critical_only,
584                       machinepass=machinepass, use_ntvfs=use_ntvfs,
585                       dns_backend=dns_backend)
586         elif role == "SUBDOMAIN":
587             if not adminpass:
588                 logger.info("Administrator password will be set randomly!")
589
590             netbios_domain = lp.get("workgroup")
591             if parent_domain is None:
592                 parent_domain = ".".join(domain.split(".")[1:])
593             join_subdomain(logger=logger, server=server, creds=creds, lp=lp, dnsdomain=domain,
594                            parent_domain=parent_domain, site=site,
595                            netbios_name=netbios_name, netbios_domain=netbios_domain,
596                            targetdir=targetdir, machinepass=machinepass,
597                            use_ntvfs=use_ntvfs, dns_backend=dns_backend,
598                            adminpass=adminpass)
599         else:
600             raise CommandError("Invalid role '%s' (possible values: MEMBER, DC, RODC, SUBDOMAIN)" % role)
601
602
603 class cmd_domain_demote(Command):
604     """Demote ourselves from the role of Domain Controller."""
605
606     synopsis = "%prog [options]"
607
608     takes_options = [
609         Option("--server", help="DC to force replication before demote", type=str),
610         Option("--targetdir", help="where provision is stored", type=str),
611         ]
612
613     takes_optiongroups = {
614         "sambaopts": options.SambaOptions,
615         "credopts": options.CredentialsOptions,
616         "versionopts": options.VersionOptions,
617         }
618
619     def run(self, sambaopts=None, credopts=None,
620             versionopts=None, server=None, targetdir=None):
621         lp = sambaopts.get_loadparm()
622         creds = credopts.get_credentials(lp)
623         net = Net(creds, lp, server=credopts.ipaddress)
624
625         netbios_name = lp.get("netbios name")
626         samdb = SamDB(session_info=system_session(), credentials=creds, lp=lp)
627         if not server:
628             res = samdb.search(expression='(&(objectClass=computer)(serverReferenceBL=*))', attrs=["dnsHostName", "name"])
629             if (len(res) == 0):
630                 raise CommandError("Unable to search for servers")
631
632             if (len(res) == 1):
633                 raise CommandError("You are the latest server in the domain")
634
635             server = None
636             for e in res:
637                 if str(e["name"]).lower() != netbios_name.lower():
638                     server = e["dnsHostName"]
639                     break
640
641         ntds_guid = samdb.get_ntds_GUID()
642         msg = samdb.search(base=str(samdb.get_config_basedn()),
643             scope=ldb.SCOPE_SUBTREE, expression="(objectGUID=%s)" % ntds_guid,
644             attrs=['options'])
645         if len(msg) == 0 or "options" not in msg[0]:
646             raise CommandError("Failed to find options on %s" % ntds_guid)
647
648         ntds_dn = msg[0].dn
649         dsa_options = int(str(msg[0]['options']))
650
651         res = samdb.search(expression="(fSMORoleOwner=%s)" % str(ntds_dn),
652                             controls=["search_options:1:2"])
653
654         if len(res) != 0:
655             raise CommandError("Current DC is still the owner of %d role(s), use the role command to transfer roles to another DC" % len(res))
656
657         self.errf.write("Using %s as partner server for the demotion\n" %
658                         server)
659         (drsuapiBind, drsuapi_handle, supportedExtensions) = drsuapi_connect(server, lp, creds)
660
661         self.errf.write("Desactivating inbound replication\n")
662
663         nmsg = ldb.Message()
664         nmsg.dn = msg[0].dn
665
666         dsa_options |= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
667         nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
668         samdb.modify(nmsg)
669
670         if not (dsa_options & DS_NTDSDSA_OPT_DISABLE_OUTBOUND_REPL) and not samdb.am_rodc():
671
672             self.errf.write("Asking partner server %s to synchronize from us\n"
673                             % server)
674             for part in (samdb.get_schema_basedn(),
675                             samdb.get_config_basedn(),
676                             samdb.get_root_basedn()):
677                 try:
678                     sendDsReplicaSync(drsuapiBind, drsuapi_handle, ntds_guid, str(part), drsuapi.DRSUAPI_DRS_WRIT_REP)
679                 except drsException, e:
680                     self.errf.write(
681                         "Error while demoting, "
682                         "re-enabling inbound replication\n")
683                     dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
684                     nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
685                     samdb.modify(nmsg)
686                     raise CommandError("Error while sending a DsReplicaSync for partion %s" % str(part), e)
687         try:
688             remote_samdb = SamDB(url="ldap://%s" % server,
689                                 session_info=system_session(),
690                                 credentials=creds, lp=lp)
691
692             self.errf.write("Changing userControl and container\n")
693             res = remote_samdb.search(base=str(remote_samdb.get_root_basedn()),
694                                 expression="(&(objectClass=user)(sAMAccountName=%s$))" %
695                                             netbios_name.upper(),
696                                 attrs=["userAccountControl"])
697             dc_dn = res[0].dn
698             uac = int(str(res[0]["userAccountControl"]))
699
700         except Exception, e:
701             self.errf.write(
702                 "Error while demoting, re-enabling inbound replication\n")
703             dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
704             nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
705             samdb.modify(nmsg)
706             raise CommandError("Error while changing account control", e)
707
708         if (len(res) != 1):
709             self.errf.write(
710                 "Error while demoting, re-enabling inbound replication")
711             dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
712             nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
713             samdb.modify(nmsg)
714             raise CommandError("Unable to find object with samaccountName = %s$"
715                                " in the remote dc" % netbios_name.upper())
716
717         olduac = uac
718
719         uac ^= (UF_SERVER_TRUST_ACCOUNT|UF_TRUSTED_FOR_DELEGATION)
720         uac |= UF_WORKSTATION_TRUST_ACCOUNT
721
722         msg = ldb.Message()
723         msg.dn = dc_dn
724
725         msg["userAccountControl"] = ldb.MessageElement("%d" % uac,
726                                                         ldb.FLAG_MOD_REPLACE,
727                                                         "userAccountControl")
728         try:
729             remote_samdb.modify(msg)
730         except Exception, e:
731             self.errf.write(
732                 "Error while demoting, re-enabling inbound replication")
733             dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
734             nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
735             samdb.modify(nmsg)
736
737             raise CommandError("Error while changing account control", e)
738
739         parent = msg.dn.parent()
740         rdn = str(res[0].dn)
741         rdn = string.replace(rdn, ",%s" % str(parent), "")
742         # Let's move to the Computer container
743         i = 0
744         newrdn = rdn
745
746         computer_dn = ldb.Dn(remote_samdb, "CN=Computers,%s" % str(remote_samdb.get_root_basedn()))
747         res = remote_samdb.search(base=computer_dn, expression=rdn, scope=ldb.SCOPE_ONELEVEL)
748
749         if (len(res) != 0):
750             res = remote_samdb.search(base=computer_dn, expression="%s-%d" % (rdn, i),
751                                         scope=ldb.SCOPE_ONELEVEL)
752             while(len(res) != 0 and i < 100):
753                 i = i + 1
754                 res = remote_samdb.search(base=computer_dn, expression="%s-%d" % (rdn, i),
755                                             scope=ldb.SCOPE_ONELEVEL)
756
757             if i == 100:
758                 self.errf.write(
759                     "Error while demoting, re-enabling inbound replication\n")
760                 dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
761                 nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
762                 samdb.modify(nmsg)
763
764                 msg = ldb.Message()
765                 msg.dn = dc_dn
766
767                 msg["userAccountControl"] = ldb.MessageElement("%d" % uac,
768                                                         ldb.FLAG_MOD_REPLACE,
769                                                         "userAccountControl")
770
771                 remote_samdb.modify(msg)
772
773                 raise CommandError("Unable to find a slot for renaming %s,"
774                                     " all names from %s-1 to %s-%d seemed used" %
775                                     (str(dc_dn), rdn, rdn, i - 9))
776
777             newrdn = "%s-%d" % (rdn, i)
778
779         try:
780             newdn = ldb.Dn(remote_samdb, "%s,%s" % (newrdn, str(computer_dn)))
781             remote_samdb.rename(dc_dn, newdn)
782         except Exception, e:
783             self.errf.write(
784                 "Error while demoting, re-enabling inbound replication\n")
785             dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
786             nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
787             samdb.modify(nmsg)
788
789             msg = ldb.Message()
790             msg.dn = dc_dn
791
792             msg["userAccountControl"] = ldb.MessageElement("%d" % uac,
793                                                     ldb.FLAG_MOD_REPLACE,
794                                                     "userAccountControl")
795
796             remote_samdb.modify(msg)
797             raise CommandError("Error while renaming %s to %s" % (str(dc_dn), str(newdn)), e)
798
799
800         server_dsa_dn = samdb.get_serverName()
801         domain = remote_samdb.get_root_basedn()
802
803         try:
804             sendRemoveDsServer(drsuapiBind, drsuapi_handle, server_dsa_dn, domain)
805         except drsException, e:
806             self.errf.write(
807                 "Error while demoting, re-enabling inbound replication\n")
808             dsa_options ^= DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL
809             nmsg["options"] = ldb.MessageElement(str(dsa_options), ldb.FLAG_MOD_REPLACE, "options")
810             samdb.modify(nmsg)
811
812             msg = ldb.Message()
813             msg.dn = newdn
814
815             msg["userAccountControl"] = ldb.MessageElement("%d" % uac,
816                                                     ldb.FLAG_MOD_REPLACE,
817                                                     "userAccountControl")
818             print str(dc_dn)
819             remote_samdb.modify(msg)
820             remote_samdb.rename(newdn, dc_dn)
821             raise CommandError("Error while sending a removeDsServer", e)
822
823         for s in ("CN=Entreprise,CN=Microsoft System Volumes,CN=System,CN=Configuration",
824                   "CN=%s,CN=Microsoft System Volumes,CN=System,CN=Configuration" % lp.get("realm"),
825                   "CN=Domain System Volumes (SYSVOL share),CN=File Replication Service,CN=System"):
826             try:
827                 remote_samdb.delete(ldb.Dn(remote_samdb,
828                                     "%s,%s,%s" % (str(rdn), s, str(remote_samdb.get_root_basedn()))))
829             except ldb.LdbError, l:
830                 pass
831
832         for s in ("CN=Entreprise,CN=NTFRS Subscriptions",
833                   "CN=%s, CN=NTFRS Subscriptions" % lp.get("realm"),
834                   "CN=Domain system Volumes (SYSVOL Share), CN=NTFRS Subscriptions",
835                   "CN=NTFRS Subscriptions"):
836             try:
837                 remote_samdb.delete(ldb.Dn(remote_samdb,
838                                     "%s,%s" % (s, str(newdn))))
839             except ldb.LdbError, l:
840                 pass
841
842         self.errf.write("Demote successfull\n")
843
844
845 class cmd_domain_level(Command):
846     """Raise domain and forest function levels."""
847
848     synopsis = "%prog (show|raise <options>) [options]"
849
850     takes_optiongroups = {
851         "sambaopts": options.SambaOptions,
852         "credopts": options.CredentialsOptions,
853         "versionopts": options.VersionOptions,
854         }
855
856     takes_options = [
857         Option("-H", "--URL", help="LDB URL for database or target server", type=str,
858                metavar="URL", dest="H"),
859         Option("--quiet", help="Be quiet", action="store_true"),
860         Option("--forest-level", type="choice", choices=["2003", "2008", "2008_R2"],
861             help="The forest function level (2003 | 2008 | 2008_R2)"),
862         Option("--domain-level", type="choice", choices=["2003", "2008", "2008_R2"],
863             help="The domain function level (2003 | 2008 | 2008_R2)")
864             ]
865
866     takes_args = ["subcommand"]
867
868     def run(self, subcommand, H=None, forest_level=None, domain_level=None,
869             quiet=False, credopts=None, sambaopts=None, versionopts=None):
870         lp = sambaopts.get_loadparm()
871         creds = credopts.get_credentials(lp, fallback_machine=True)
872
873         samdb = SamDB(url=H, session_info=system_session(),
874             credentials=creds, lp=lp)
875
876         domain_dn = samdb.domain_dn()
877
878         res_forest = samdb.search("CN=Partitions,%s" % samdb.get_config_basedn(),
879           scope=ldb.SCOPE_BASE, attrs=["msDS-Behavior-Version"])
880         assert len(res_forest) == 1
881
882         res_domain = samdb.search(domain_dn, scope=ldb.SCOPE_BASE,
883           attrs=["msDS-Behavior-Version", "nTMixedDomain"])
884         assert len(res_domain) == 1
885
886         res_dc_s = samdb.search("CN=Sites,%s" % samdb.get_config_basedn(),
887           scope=ldb.SCOPE_SUBTREE, expression="(objectClass=nTDSDSA)",
888           attrs=["msDS-Behavior-Version"])
889         assert len(res_dc_s) >= 1
890
891         try:
892             level_forest = int(res_forest[0]["msDS-Behavior-Version"][0])
893             level_domain = int(res_domain[0]["msDS-Behavior-Version"][0])
894             level_domain_mixed = int(res_domain[0]["nTMixedDomain"][0])
895
896             min_level_dc = int(res_dc_s[0]["msDS-Behavior-Version"][0]) # Init value
897             for msg in res_dc_s:
898                 if int(msg["msDS-Behavior-Version"][0]) < min_level_dc:
899                     min_level_dc = int(msg["msDS-Behavior-Version"][0])
900
901             if level_forest < 0 or level_domain < 0:
902                 raise CommandError("Domain and/or forest function level(s) is/are invalid. Correct them or reprovision!")
903             if min_level_dc < 0:
904                 raise CommandError("Lowest function level of a DC is invalid. Correct this or reprovision!")
905             if level_forest > level_domain:
906                 raise CommandError("Forest function level is higher than the domain level(s). Correct this or reprovision!")
907             if level_domain > min_level_dc:
908                 raise CommandError("Domain function level is higher than the lowest function level of a DC. Correct this or reprovision!")
909
910         except KeyError:
911             raise CommandError("Could not retrieve the actual domain, forest level and/or lowest DC function level!")
912
913         if subcommand == "show":
914             self.message("Domain and forest function level for domain '%s'" % domain_dn)
915             if level_forest == DS_DOMAIN_FUNCTION_2000 and level_domain_mixed != 0:
916                 self.message("\nATTENTION: You run SAMBA 4 on a forest function level lower than Windows 2000 (Native). This isn't supported! Please raise!")
917             if level_domain == DS_DOMAIN_FUNCTION_2000 and level_domain_mixed != 0:
918                 self.message("\nATTENTION: You run SAMBA 4 on a domain function level lower than Windows 2000 (Native). This isn't supported! Please raise!")
919             if min_level_dc == DS_DOMAIN_FUNCTION_2000 and level_domain_mixed != 0:
920                 self.message("\nATTENTION: You run SAMBA 4 on a lowest function level of a DC lower than Windows 2003. This isn't supported! Please step-up or upgrade the concerning DC(s)!")
921
922             self.message("")
923
924             if level_forest == DS_DOMAIN_FUNCTION_2000:
925                 outstr = "2000"
926             elif level_forest == DS_DOMAIN_FUNCTION_2003_MIXED:
927                 outstr = "2003 with mixed domains/interim (NT4 DC support)"
928             elif level_forest == DS_DOMAIN_FUNCTION_2003:
929                 outstr = "2003"
930             elif level_forest == DS_DOMAIN_FUNCTION_2008:
931                 outstr = "2008"
932             elif level_forest == DS_DOMAIN_FUNCTION_2008_R2:
933                 outstr = "2008 R2"
934             else:
935                 outstr = "higher than 2008 R2"
936             self.message("Forest function level: (Windows) " + outstr)
937
938             if level_domain == DS_DOMAIN_FUNCTION_2000 and level_domain_mixed != 0:
939                 outstr = "2000 mixed (NT4 DC support)"
940             elif level_domain == DS_DOMAIN_FUNCTION_2000 and level_domain_mixed == 0:
941                 outstr = "2000"
942             elif level_domain == DS_DOMAIN_FUNCTION_2003_MIXED:
943                 outstr = "2003 with mixed domains/interim (NT4 DC support)"
944             elif level_domain == DS_DOMAIN_FUNCTION_2003:
945                 outstr = "2003"
946             elif level_domain == DS_DOMAIN_FUNCTION_2008:
947                 outstr = "2008"
948             elif level_domain == DS_DOMAIN_FUNCTION_2008_R2:
949                 outstr = "2008 R2"
950             else:
951                 outstr = "higher than 2008 R2"
952             self.message("Domain function level: (Windows) " + outstr)
953
954             if min_level_dc == DS_DOMAIN_FUNCTION_2000:
955                 outstr = "2000"
956             elif min_level_dc == DS_DOMAIN_FUNCTION_2003:
957                 outstr = "2003"
958             elif min_level_dc == DS_DOMAIN_FUNCTION_2008:
959                 outstr = "2008"
960             elif min_level_dc == DS_DOMAIN_FUNCTION_2008_R2:
961                 outstr = "2008 R2"
962             else:
963                 outstr = "higher than 2008 R2"
964             self.message("Lowest function level of a DC: (Windows) " + outstr)
965
966         elif subcommand == "raise":
967             msgs = []
968
969             if domain_level is not None:
970                 if domain_level == "2003":
971                     new_level_domain = DS_DOMAIN_FUNCTION_2003
972                 elif domain_level == "2008":
973                     new_level_domain = DS_DOMAIN_FUNCTION_2008
974                 elif domain_level == "2008_R2":
975                     new_level_domain = DS_DOMAIN_FUNCTION_2008_R2
976
977                 if new_level_domain <= level_domain and level_domain_mixed == 0:
978                     raise CommandError("Domain function level can't be smaller than or equal to the actual one!")
979
980                 if new_level_domain > min_level_dc:
981                     raise CommandError("Domain function level can't be higher than the lowest function level of a DC!")
982
983                 # Deactivate mixed/interim domain support
984                 if level_domain_mixed != 0:
985                     # Directly on the base DN
986                     m = ldb.Message()
987                     m.dn = ldb.Dn(samdb, domain_dn)
988                     m["nTMixedDomain"] = ldb.MessageElement("0",
989                       ldb.FLAG_MOD_REPLACE, "nTMixedDomain")
990                     samdb.modify(m)
991                     # Under partitions
992                     m = ldb.Message()
993                     m.dn = ldb.Dn(samdb, "CN=" + lp.get("workgroup") + ",CN=Partitions,%s" % samdb.get_config_basedn())
994                     m["nTMixedDomain"] = ldb.MessageElement("0",
995                       ldb.FLAG_MOD_REPLACE, "nTMixedDomain")
996                     try:
997                         samdb.modify(m)
998                     except ldb.LdbError, (enum, emsg):
999                         if enum != ldb.ERR_UNWILLING_TO_PERFORM:
1000                             raise
1001
1002                 # Directly on the base DN
1003                 m = ldb.Message()
1004                 m.dn = ldb.Dn(samdb, domain_dn)
1005                 m["msDS-Behavior-Version"]= ldb.MessageElement(
1006                   str(new_level_domain), ldb.FLAG_MOD_REPLACE,
1007                             "msDS-Behavior-Version")
1008                 samdb.modify(m)
1009                 # Under partitions
1010                 m = ldb.Message()
1011                 m.dn = ldb.Dn(samdb, "CN=" + lp.get("workgroup")
1012                   + ",CN=Partitions,%s" % samdb.get_config_basedn())
1013                 m["msDS-Behavior-Version"]= ldb.MessageElement(
1014                   str(new_level_domain), ldb.FLAG_MOD_REPLACE,
1015                           "msDS-Behavior-Version")
1016                 try:
1017                     samdb.modify(m)
1018                 except ldb.LdbError, (enum, emsg):
1019                     if enum != ldb.ERR_UNWILLING_TO_PERFORM:
1020                         raise
1021
1022                 level_domain = new_level_domain
1023                 msgs.append("Domain function level changed!")
1024
1025             if forest_level is not None:
1026                 if forest_level == "2003":
1027                     new_level_forest = DS_DOMAIN_FUNCTION_2003
1028                 elif forest_level == "2008":
1029                     new_level_forest = DS_DOMAIN_FUNCTION_2008
1030                 elif forest_level == "2008_R2":
1031                     new_level_forest = DS_DOMAIN_FUNCTION_2008_R2
1032                 if new_level_forest <= level_forest:
1033                     raise CommandError("Forest function level can't be smaller than or equal to the actual one!")
1034                 if new_level_forest > level_domain:
1035                     raise CommandError("Forest function level can't be higher than the domain function level(s). Please raise it/them first!")
1036                 m = ldb.Message()
1037                 m.dn = ldb.Dn(samdb, "CN=Partitions,%s" % samdb.get_config_basedn())
1038                 m["msDS-Behavior-Version"]= ldb.MessageElement(
1039                   str(new_level_forest), ldb.FLAG_MOD_REPLACE,
1040                           "msDS-Behavior-Version")
1041                 samdb.modify(m)
1042                 msgs.append("Forest function level changed!")
1043             msgs.append("All changes applied successfully!")
1044             self.message("\n".join(msgs))
1045         else:
1046             raise CommandError("invalid argument: '%s' (choose from 'show', 'raise')" % subcommand)
1047
1048
1049 class cmd_domain_passwordsettings(Command):
1050     """Set password settings.
1051
1052     Password complexity, history length, minimum password length, the minimum
1053     and maximum password age) on a Samba4 server.
1054     """
1055
1056     synopsis = "%prog (show|set <options>) [options]"
1057
1058     takes_optiongroups = {
1059         "sambaopts": options.SambaOptions,
1060         "versionopts": options.VersionOptions,
1061         "credopts": options.CredentialsOptions,
1062         }
1063
1064     takes_options = [
1065         Option("-H", "--URL", help="LDB URL for database or target server", type=str,
1066                metavar="URL", dest="H"),
1067         Option("--quiet", help="Be quiet", action="store_true"),
1068         Option("--complexity", type="choice", choices=["on","off","default"],
1069           help="The password complexity (on | off | default). Default is 'on'"),
1070         Option("--store-plaintext", type="choice", choices=["on","off","default"],
1071           help="Store plaintext passwords where account have 'store passwords with reversible encryption' set (on | off | default). Default is 'off'"),
1072         Option("--history-length",
1073           help="The password history length (<integer> | default).  Default is 24.", type=str),
1074         Option("--min-pwd-length",
1075           help="The minimum password length (<integer> | default).  Default is 7.", type=str),
1076         Option("--min-pwd-age",
1077           help="The minimum password age (<integer in days> | default).  Default is 1.", type=str),
1078         Option("--max-pwd-age",
1079           help="The maximum password age (<integer in days> | default).  Default is 43.", type=str),
1080           ]
1081
1082     takes_args = ["subcommand"]
1083
1084     def run(self, subcommand, H=None, min_pwd_age=None, max_pwd_age=None,
1085             quiet=False, complexity=None, store_plaintext=None, history_length=None,
1086             min_pwd_length=None, credopts=None, sambaopts=None,
1087             versionopts=None):
1088         lp = sambaopts.get_loadparm()
1089         creds = credopts.get_credentials(lp)
1090
1091         samdb = SamDB(url=H, session_info=system_session(),
1092             credentials=creds, lp=lp)
1093
1094         domain_dn = samdb.domain_dn()
1095         res = samdb.search(domain_dn, scope=ldb.SCOPE_BASE,
1096           attrs=["pwdProperties", "pwdHistoryLength", "minPwdLength",
1097                  "minPwdAge", "maxPwdAge"])
1098         assert(len(res) == 1)
1099         try:
1100             pwd_props = int(res[0]["pwdProperties"][0])
1101             pwd_hist_len = int(res[0]["pwdHistoryLength"][0])
1102             cur_min_pwd_len = int(res[0]["minPwdLength"][0])
1103             # ticks -> days
1104             cur_min_pwd_age = int(abs(int(res[0]["minPwdAge"][0])) / (1e7 * 60 * 60 * 24))
1105             if int(res[0]["maxPwdAge"][0]) == -0x8000000000000000:
1106                 cur_max_pwd_age = 0
1107             else:
1108                 cur_max_pwd_age = int(abs(int(res[0]["maxPwdAge"][0])) / (1e7 * 60 * 60 * 24))
1109         except Exception, e:
1110             raise CommandError("Could not retrieve password properties!", e)
1111
1112         if subcommand == "show":
1113             self.message("Password informations for domain '%s'" % domain_dn)
1114             self.message("")
1115             if pwd_props & DOMAIN_PASSWORD_COMPLEX != 0:
1116                 self.message("Password complexity: on")
1117             else:
1118                 self.message("Password complexity: off")
1119             if pwd_props & DOMAIN_PASSWORD_STORE_CLEARTEXT != 0:
1120                 self.message("Store plaintext passwords: on")
1121             else:
1122                 self.message("Store plaintext passwords: off")
1123             self.message("Password history length: %d" % pwd_hist_len)
1124             self.message("Minimum password length: %d" % cur_min_pwd_len)
1125             self.message("Minimum password age (days): %d" % cur_min_pwd_age)
1126             self.message("Maximum password age (days): %d" % cur_max_pwd_age)
1127         elif subcommand == "set":
1128             msgs = []
1129             m = ldb.Message()
1130             m.dn = ldb.Dn(samdb, domain_dn)
1131
1132             if complexity is not None:
1133                 if complexity == "on" or complexity == "default":
1134                     pwd_props = pwd_props | DOMAIN_PASSWORD_COMPLEX
1135                     msgs.append("Password complexity activated!")
1136                 elif complexity == "off":
1137                     pwd_props = pwd_props & (~DOMAIN_PASSWORD_COMPLEX)
1138                     msgs.append("Password complexity deactivated!")
1139
1140             if store_plaintext is not None:
1141                 if store_plaintext == "on" or store_plaintext == "default":
1142                     pwd_props = pwd_props | DOMAIN_PASSWORD_STORE_CLEARTEXT
1143                     msgs.append("Plaintext password storage for changed passwords activated!")
1144                 elif store_plaintext == "off":
1145                     pwd_props = pwd_props & (~DOMAIN_PASSWORD_STORE_CLEARTEXT)
1146                     msgs.append("Plaintext password storage for changed passwords deactivated!")
1147
1148             if complexity is not None or store_plaintext is not None:
1149                 m["pwdProperties"] = ldb.MessageElement(str(pwd_props),
1150                   ldb.FLAG_MOD_REPLACE, "pwdProperties")
1151
1152             if history_length is not None:
1153                 if history_length == "default":
1154                     pwd_hist_len = 24
1155                 else:
1156                     pwd_hist_len = int(history_length)
1157
1158                 if pwd_hist_len < 0 or pwd_hist_len > 24:
1159                     raise CommandError("Password history length must be in the range of 0 to 24!")
1160
1161                 m["pwdHistoryLength"] = ldb.MessageElement(str(pwd_hist_len),
1162                   ldb.FLAG_MOD_REPLACE, "pwdHistoryLength")
1163                 msgs.append("Password history length changed!")
1164
1165             if min_pwd_length is not None:
1166                 if min_pwd_length == "default":
1167                     min_pwd_len = 7
1168                 else:
1169                     min_pwd_len = int(min_pwd_length)
1170
1171                 if min_pwd_len < 0 or min_pwd_len > 14:
1172                     raise CommandError("Minimum password length must be in the range of 0 to 14!")
1173
1174                 m["minPwdLength"] = ldb.MessageElement(str(min_pwd_len),
1175                   ldb.FLAG_MOD_REPLACE, "minPwdLength")
1176                 msgs.append("Minimum password length changed!")
1177
1178             if min_pwd_age is not None:
1179                 if min_pwd_age == "default":
1180                     min_pwd_age = 1
1181                 else:
1182                     min_pwd_age = int(min_pwd_age)
1183
1184                 if min_pwd_age < 0 or min_pwd_age > 998:
1185                     raise CommandError("Minimum password age must be in the range of 0 to 998!")
1186
1187                 # days -> ticks
1188                 min_pwd_age_ticks = -int(min_pwd_age * (24 * 60 * 60 * 1e7))
1189
1190                 m["minPwdAge"] = ldb.MessageElement(str(min_pwd_age_ticks),
1191                   ldb.FLAG_MOD_REPLACE, "minPwdAge")
1192                 msgs.append("Minimum password age changed!")
1193
1194             if max_pwd_age is not None:
1195                 if max_pwd_age == "default":
1196                     max_pwd_age = 43
1197                 else:
1198                     max_pwd_age = int(max_pwd_age)
1199
1200                 if max_pwd_age < 0 or max_pwd_age > 999:
1201                     raise CommandError("Maximum password age must be in the range of 0 to 999!")
1202
1203                 # days -> ticks
1204                 if max_pwd_age == 0:
1205                     max_pwd_age_ticks = -0x8000000000000000
1206                 else:
1207                     max_pwd_age_ticks = -int(max_pwd_age * (24 * 60 * 60 * 1e7))
1208
1209                 m["maxPwdAge"] = ldb.MessageElement(str(max_pwd_age_ticks),
1210                   ldb.FLAG_MOD_REPLACE, "maxPwdAge")
1211                 msgs.append("Maximum password age changed!")
1212
1213             if max_pwd_age > 0 and min_pwd_age >= max_pwd_age:
1214                 raise CommandError("Maximum password age (%d) must be greater than minimum password age (%d)!" % (max_pwd_age, min_pwd_age))
1215
1216             if len(m) == 0:
1217                 raise CommandError("You must specify at least one option to set. Try --help")
1218             samdb.modify(m)
1219             msgs.append("All changes applied successfully!")
1220             self.message("\n".join(msgs))
1221         else:
1222             raise CommandError("Wrong argument '%s'!" % subcommand)
1223
1224
1225 class cmd_domain_classicupgrade(Command):
1226     """Upgrade from Samba classic (NT4-like) database to Samba AD DC database.
1227
1228     Specify either a directory with all Samba classic DC databases and state files (with --dbdir) or
1229     the testparm utility from your classic installation (with --testparm).
1230     """
1231
1232     synopsis = "%prog [options] <classic_smb_conf>"
1233
1234     takes_optiongroups = {
1235         "sambaopts": options.SambaOptions,
1236         "versionopts": options.VersionOptions
1237     }
1238
1239     takes_options = [
1240         Option("--dbdir", type="string", metavar="DIR",
1241                   help="Path to samba classic DC database directory"),
1242         Option("--testparm", type="string", metavar="PATH",
1243                   help="Path to samba classic DC testparm utility from the previous installation.  This allows the default paths of the previous installation to be followed"),
1244         Option("--targetdir", type="string", metavar="DIR",
1245                   help="Path prefix where the new Samba 4.0 AD domain should be initialised"),
1246         Option("--quiet", help="Be quiet", action="store_true"),
1247         Option("--verbose", help="Be verbose", action="store_true"),
1248         Option("--use-xattrs", type="choice", choices=["yes","no","auto"], metavar="[yes|no|auto]",
1249                    help="Define if we should use the native fs capabilities or a tdb file for storing attributes likes ntacl, auto tries to make an inteligent guess based on the user rights and system capabilities", default="auto"),
1250         Option("--use-ntvfs", help="Use NTVFS for the fileserver (default = no)",
1251                action="store_true"),
1252         Option("--dns-backend", type="choice", metavar="NAMESERVER-BACKEND",
1253                choices=["SAMBA_INTERNAL", "BIND9_FLATFILE", "BIND9_DLZ", "NONE"],
1254                help="The DNS server backend. SAMBA_INTERNAL is the builtin name server (default), "
1255                    "BIND9_FLATFILE uses bind9 text database to store zone information, "
1256                    "BIND9_DLZ uses samba4 AD to store zone information, "
1257                    "NONE skips the DNS setup entirely (this DC will not be a DNS server)",
1258                default="SAMBA_INTERNAL")
1259     ]
1260
1261     takes_args = ["smbconf"]
1262
1263     def run(self, smbconf=None, targetdir=None, dbdir=None, testparm=None,
1264             quiet=False, verbose=False, use_xattrs=None, sambaopts=None, versionopts=None,
1265             dns_backend=None, use_ntvfs=False):
1266
1267         if not os.path.exists(smbconf):
1268             raise CommandError("File %s does not exist" % smbconf)
1269
1270         if testparm and not os.path.exists(testparm):
1271             raise CommandError("Testparm utility %s does not exist" % testparm)
1272
1273         if dbdir and not os.path.exists(dbdir):
1274             raise CommandError("Directory %s does not exist" % dbdir)
1275
1276         if not dbdir and not testparm:
1277             raise CommandError("Please specify either dbdir or testparm")
1278
1279         logger = self.get_logger()
1280         if verbose:
1281             logger.setLevel(logging.DEBUG)
1282         elif quiet:
1283             logger.setLevel(logging.WARNING)
1284         else:
1285             logger.setLevel(logging.INFO)
1286
1287         if dbdir and testparm:
1288             logger.warning("both dbdir and testparm specified, ignoring dbdir.")
1289             dbdir = None
1290
1291         lp = sambaopts.get_loadparm()
1292
1293         s3conf = s3param.get_context()
1294
1295         if sambaopts.realm:
1296             s3conf.set("realm", sambaopts.realm)
1297
1298         if targetdir is not None:
1299             if not os.path.isdir(targetdir):
1300                 os.mkdir(targetdir)
1301
1302         eadb = True
1303         if use_xattrs == "yes":
1304             eadb = False
1305         elif use_xattrs == "auto" and not s3conf.get("posix:eadb"):
1306             if targetdir:
1307                 tmpfile = tempfile.NamedTemporaryFile(dir=os.path.abspath(targetdir))
1308             else:
1309                 tmpfile = tempfile.NamedTemporaryFile(dir=os.path.abspath(os.path.dirname(lp.get("private dir"))))
1310             try:
1311                 try:
1312                     samba.ntacls.setntacl(lp, tmpfile.name,
1313                                 "O:S-1-5-32G:S-1-5-32", "S-1-5-32", "native")
1314                     eadb = False
1315                 except Exception:
1316                     # FIXME: Don't catch all exceptions here
1317                     logger.info("You are not root or your system do not support xattr, using tdb backend for attributes. "
1318                                 "If you intend to use this provision in production, rerun the script as root on a system supporting xattrs.")
1319             finally:
1320                 tmpfile.close()
1321
1322         # Set correct default values from dbdir or testparm
1323         paths = {}
1324         if dbdir:
1325             paths["state directory"] = dbdir
1326             paths["private dir"] = dbdir
1327             paths["lock directory"] = dbdir
1328             paths["smb passwd file"] = dbdir + "/smbpasswd"
1329         else:
1330             paths["state directory"] = get_testparm_var(testparm, smbconf, "state directory")
1331             paths["private dir"] = get_testparm_var(testparm, smbconf, "private dir")
1332             paths["smb passwd file"] = get_testparm_var(testparm, smbconf, "smb passwd file")
1333             paths["lock directory"] = get_testparm_var(testparm, smbconf, "lock directory")
1334             # "testparm" from Samba 3 < 3.4.x is not aware of the parameter
1335             # "state directory", instead make use of "lock directory"
1336             if len(paths["state directory"]) == 0:
1337                 paths["state directory"] = paths["lock directory"]
1338
1339         for p in paths:
1340             s3conf.set(p, paths[p])
1341
1342         # load smb.conf parameters
1343         logger.info("Reading smb.conf")
1344         s3conf.load(smbconf)
1345         samba3 = Samba3(smbconf, s3conf)
1346
1347         logger.info("Provisioning")
1348         upgrade_from_samba3(samba3, logger, targetdir, session_info=system_session(),
1349                             useeadb=eadb, dns_backend=dns_backend, use_ntvfs=use_ntvfs)
1350
1351
1352 class cmd_domain_samba3upgrade(cmd_domain_classicupgrade):
1353     __doc__ = cmd_domain_classicupgrade.__doc__
1354
1355     # This command is present for backwards compatibility only,
1356     # and should not be shown.
1357
1358     hidden = True
1359
1360
1361 class cmd_domain(SuperCommand):
1362     """Domain management."""
1363
1364     subcommands = {}
1365     subcommands["demote"] = cmd_domain_demote()
1366     if cmd_domain_export_keytab is not None:
1367         subcommands["exportkeytab"] = cmd_domain_export_keytab()
1368     subcommands["info"] = cmd_domain_info()
1369     subcommands["provision"] = cmd_domain_provision()
1370     subcommands["join"] = cmd_domain_join()
1371     subcommands["dcpromo"] = cmd_domain_dcpromo()
1372     subcommands["level"] = cmd_domain_level()
1373     subcommands["passwordsettings"] = cmd_domain_passwordsettings()
1374     subcommands["classicupgrade"] = cmd_domain_classicupgrade()
1375     subcommands["samba3upgrade"] = cmd_domain_samba3upgrade()