18142addbf70f0db32953df495384a12da2cce9c
[ira/wip.git] / source4 / setup / provision
1 #!/usr/bin/env python
2 #
3 # Unix SMB/CIFS implementation.
4 # provision a Samba4 server
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 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 import logging
26 import optparse
27 import sys
28 import tempfile
29
30 # Find right directory when running from source tree
31 sys.path.insert(0, "bin/python")
32
33 import samba
34 import samba.ntacls
35 from samba.credentials import DONT_USE_KERBEROS
36 from samba.auth import system_session
37 import samba.getopt as options
38 from samba.provision import provision, FILL_FULL, FILL_NT4SYNC, FILL_DRS, find_setup_dir, ProvisioningError
39 from samba.dsdb import (
40         DS_DOMAIN_FUNCTION_2000,
41         DS_DOMAIN_FUNCTION_2003,
42         DS_DOMAIN_FUNCTION_2008,
43         DS_DOMAIN_FUNCTION_2008_R2,
44         )
45
46 # how do we make this case insensitive??
47
48 parser = optparse.OptionParser("provision [options]")
49 sambaopts = options.SambaOptions(parser)
50 parser.add_option_group(sambaopts)
51 parser.add_option_group(options.VersionOptions(parser))
52 credopts = options.CredentialsOptions(parser)
53 parser.add_option_group(credopts)
54 parser.add_option("--interactive", help="Ask for names", action="store_true")
55 parser.add_option("--setupdir", type="string", metavar="DIR", 
56                 help="directory with setup files")
57 parser.add_option("--domain", type="string", metavar="DOMAIN",
58                                   help="set domain")
59 parser.add_option("--domain-guid", type="string", metavar="GUID", 
60                 help="set domainguid (otherwise random)")
61 parser.add_option("--domain-sid", type="string", metavar="SID", 
62                 help="set domainsid (otherwise random)")
63 parser.add_option("--ntds-guid", type="string", metavar="GUID", 
64                   help="set NTDS object GUID (otherwise random)")
65 parser.add_option("--invocationid", type="string", metavar="GUID", 
66                   help="set invocationid (otherwise random)")
67 parser.add_option("--host-name", type="string", metavar="HOSTNAME", 
68                 help="set hostname")
69 parser.add_option("--host-ip", type="string", metavar="IPADDRESS", 
70                 help="set IPv4 ipaddress")
71 parser.add_option("--host-ip6", type="string", metavar="IP6ADDRESS", 
72                 help="set IPv6 ipaddress")
73 parser.add_option("--adminpass", type="string", metavar="PASSWORD", 
74                 help="choose admin password (otherwise random)")
75 parser.add_option("--krbtgtpass", type="string", metavar="PASSWORD", 
76                 help="choose krbtgt password (otherwise random)")
77 parser.add_option("--machinepass", type="string", metavar="PASSWORD", 
78                 help="choose machine password (otherwise random)")
79 parser.add_option("--dnspass", type="string", metavar="PASSWORD", 
80                 help="choose dns password (otherwise random)")
81 parser.add_option("--ldapadminpass", type="string", metavar="PASSWORD", 
82                 help="choose password to set between Samba and it's LDAP backend (otherwise random)")
83 parser.add_option("--root", type="string", metavar="USERNAME", 
84                 help="choose 'root' unix username")
85 parser.add_option("--nobody", type="string", metavar="USERNAME", 
86                 help="choose 'nobody' user")
87 parser.add_option("--wheel", type="string", metavar="GROUPNAME", 
88                 help="choose 'wheel' privileged group")
89 parser.add_option("--users", type="string", metavar="GROUPNAME", 
90                 help="choose 'users' group")
91 parser.add_option("--quiet", help="Be quiet", action="store_true")
92 parser.add_option("--blank", action="store_true",
93                 help="do not add users or groups, just the structure")
94 parser.add_option("--ldap-backend-extra-port", type="int", metavar="LDAP-BACKEND-EXTRA-PORT", 
95                 help="Additional TCP port for LDAP backend server (to use for replication)")
96 parser.add_option("--ldap-backend-forced-uri", type="string", metavar="LDAP-BACKEND-FORCED-URI", 
97                 help="Force the LDAP backend connection to be to a particular URI.  Use this ONLY for 'existing' backends, or when debugging the interaction with the LDAP backend and you need to intercept the LDAP traffic")
98 parser.add_option("--ldap-backend-type", type="choice", metavar="LDAP-BACKEND-TYPE", 
99                 help="LDAP backend type (fedora-ds or openldap)",
100                 choices=["fedora-ds", "openldap"])
101 parser.add_option("--ldap-backend-nosync", help="Configure LDAP backend not to call fsync() (for performance in test environments)", action="store_true")
102 parser.add_option("--server-role", type="choice", metavar="ROLE",
103                   choices=["domain controller", "dc", "member server", "member", "standalone"],
104                 help="The server role (domain controller | dc | member server | member | standalone). Default is standalone.")
105 parser.add_option("--function-level", type="choice", metavar="FOR-FUN-LEVEL",
106                   choices=["2000", "2003", "2008", "2008_R2"],
107                 help="The domain and forest function level (2003 | 2008 | 2008_R2). Default is (Windows) 2003 (Native).")
108 parser.add_option("--next-rid", type="int", metavar="NEXTRID", default=1000,
109                 help="The initial nextRid value (only needed for upgrades).  Default is 1000.")
110 parser.add_option("--partitions-only", 
111                 help="Configure Samba's partitions, but do not modify them (ie, join a BDC)", action="store_true")
112 parser.add_option("--targetdir", type="string", metavar="DIR", 
113                           help="Set target directory")
114 parser.add_option("--ol-mmr-urls", type="string", metavar="LDAPSERVER",
115                 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\"")
116 parser.add_option("--slapd-path", type="string", metavar="SLAPD-PATH", 
117                 help="Path to slapd for LDAP backend [e.g.:'/usr/local/libexec/slapd']. Required for Setup with LDAP-Backend. OpenLDAP Version >= 2.4.17 should be used.") 
118 parser.add_option("--setup-ds-path", type="string", metavar="SETUP_DS-PATH", 
119                 help="Path to setup-ds.pl script for Fedora DS LDAP backend [e.g.:'/usr/sbin/setup-ds.pl']. Required for Setup with Fedora DS backend.") 
120 parser.add_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")
121 parser.add_option("--ldap-dryrun-mode", help="Configure LDAP backend, but do not run any binaries and exit early.  Used only for the test environment.  DO NOT USE", action="store_true")
122
123 opts = parser.parse_args()[0]
124
125 logger = logging.getLogger("provision")
126 logger.addHandler(logging.StreamHandler(sys.stdout))
127 if opts.quiet:
128         logger.setLevel(logging.WARNING)
129 else:
130         logger.setLevel(logging.INFO)
131
132 if len(sys.argv) == 1:
133         opts.interactive = True
134
135 if opts.interactive:
136         from getpass import getpass
137         import socket
138         def ask(prompt, default=None):
139                 if default is not None:
140                         print "%s [%s]: " % (prompt,default),
141                 else:
142                         print "%s: " % (prompt,),
143                 return sys.stdin.readline().rstrip("\n") or default
144         try:
145                 default = socket.getfqdn().split(".", 1)[1].upper()
146         except IndexError:
147                 default = None
148         opts.realm = ask("Realm", default)
149         if opts.realm in (None, ""):
150                 print >>sys.stderr, "No realm set!"
151                 sys.exit(1)
152
153         try:
154                 default = opts.realm.split(".")[0]
155         except IndexError:
156                 default = None
157         opts.domain = ask("Domain", default)
158         if opts.domain is None:
159                 print >> sys.stderr, "No domain set!"
160                 sys.exit(1)
161
162         opts.server_role = ask("Server Role (dc, member, standalone)", "dc")
163         for i in range(3):
164                 opts.adminpass = getpass("Administrator password: ")
165                 if not opts.adminpass:
166                         print >>sys.stderr, "Invalid administrator password."
167                 else:
168                         break
169 else:
170         if opts.realm in (None, ""):
171                 opts.realm = sambaopts._lp.get('realm')
172         if opts.realm is None or opts.domain is None:
173                 if opts.realm is None:
174                         print >>sys.stderr, "No realm set!"
175                 if opts.domain is None:
176                         print >> sys.stderr, "No domain set!"
177                 parser.print_usage()
178                 sys.exit(1)
179
180 if not opts.adminpass:
181         logger.info("Administrator password will be set randomly!")
182
183 lp = sambaopts.get_loadparm()
184 smbconf = lp.configfile
185
186 if opts.server_role == "dc":
187         server_role = "domain controller"
188 elif opts.server_role == "member":
189         server_role = "member server"
190 else:
191         server_role = opts.server_role
192
193 if opts.function_level is None:
194         dom_for_fun_level = None
195 elif opts.function_level == "2000":
196         dom_for_fun_level = DS_DOMAIN_FUNCTION_2000
197 elif opts.function_level == "2003":
198         dom_for_fun_level = DS_DOMAIN_FUNCTION_2003
199 elif opts.function_level == "2008":
200         dom_for_fun_level = DS_DOMAIN_FUNCTION_2008
201 elif opts.function_level == "2008_R2":
202         dom_for_fun_level = DS_DOMAIN_FUNCTION_2008_R2
203
204 creds = credopts.get_credentials(lp)
205
206 creds.set_kerberos_state(DONT_USE_KERBEROS)
207
208 setup_dir = opts.setupdir
209 if setup_dir is None:
210         setup_dir = find_setup_dir()
211
212 samdb_fill = FILL_FULL
213 if opts.blank:
214     samdb_fill = FILL_NT4SYNC
215 elif opts.partitions_only:
216     samdb_fill = FILL_DRS
217
218 eadb = True
219 if opts.use_xattrs == "yes":
220         eadb = False
221 elif opts.use_xattrs == "auto" and not lp.get("posix:eadb"):
222         file = tempfile.NamedTemporaryFile()
223         try:
224                 samba.ntacls.setntacl(lp, file.name, 
225                         "O:S-1-5-32G:S-1-5-32", "S-1-5-32", "native")
226                 eadb = False
227         except:
228                 logger.info("You are not root or your system do not support xattr, using tdb backend for attributes. "
229                             "If you intend to use this provision in production, rerun the script as root on a system supporting xattrs.")
230         file.close()
231
232
233 if opts.ldap_backend_type == "existing":
234         if opts.ldap_backend_forced_uri is not None:
235                 logger.warn("You have specified to use an existing LDAP server as the backend, please make sure an LDAP server is running at %s" % opts.ldap_backend_forced_uri)
236         else:
237                 logger.info("You have specified to use an existing LDAP server as the backend, please make sure an LDAP server is running at the default location")
238 else:
239         if opts.ldap_backend_forced_uri is not None:
240                 logger.warn("You have specified to use an fixed URI %s for connecting to your LDAP server backend.  This is NOT RECOMMENDED, as our default communiation over ldapi:// is more secure and much less prone to unexpected failure or interaction" % opts.ldap_backend_forced_uri)
241         
242 session = system_session()
243 try:
244         provision(setup_dir, logger,
245                   session, creds, smbconf=smbconf, targetdir=opts.targetdir,
246                   samdb_fill=samdb_fill, realm=opts.realm, domain=opts.domain,
247                   domainguid=opts.domain_guid, domainsid=opts.domain_sid,
248                   hostname=opts.host_name,
249                   hostip=opts.host_ip, hostip6=opts.host_ip6,
250                   ntdsguid=opts.ntds_guid,
251                   invocationid=opts.invocationid, adminpass=opts.adminpass,
252                   krbtgtpass=opts.krbtgtpass, machinepass=opts.machinepass,
253                   dnspass=opts.dnspass, root=opts.root, nobody=opts.nobody,
254                   wheel=opts.wheel, users=opts.users,
255                   serverrole=server_role, dom_for_fun_level=dom_for_fun_level,
256                   ldap_backend_extra_port=opts.ldap_backend_extra_port,
257                   ldap_backend_forced_uri=opts.ldap_backend_forced_uri,
258                   backend_type=opts.ldap_backend_type,
259                   ldapadminpass=opts.ldapadminpass, ol_mmr_urls=opts.ol_mmr_urls,
260                   slapd_path=opts.slapd_path, setup_ds_path=opts.setup_ds_path,
261                   nosync=opts.ldap_backend_nosync, ldap_dryrun_mode=opts.ldap_dryrun_mode, 
262                   useeadb=eadb, next_rid=opts.next_rid, lp=lp)
263 except ProvisioningError, e:
264         print str(e)
265         exit(1)