s3-auth Add auth hook for PAC parsing
[Samba.git] / source4 / setup / provision
blobcea8823d09676c8e64aa1badf8c19152d816c0e6
1 #!/usr/bin/env python
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
8 # Based on the original in EJS:
9 # Copyright (C) Andrew Tridgell 2005
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.
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.
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/>.
25 import logging
26 import optparse
27 import sys
28 import tempfile
30 # Find right directory when running from source tree
31 sys.path.insert(0, "bin/python")
33 import samba
34 import samba.ntacls
35 import os
36 from samba.credentials import DONT_USE_KERBEROS
37 from samba.auth import system_session
38 import samba.getopt as options
39 from samba.provision import (
40 provision,
41 FILL_FULL,
42 FILL_NT4SYNC,
43 FILL_DRS,
44 ProvisioningError,
46 from samba.dsdb import (
47 DS_DOMAIN_FUNCTION_2000,
48 DS_DOMAIN_FUNCTION_2003,
49 DS_DOMAIN_FUNCTION_2008,
50 DS_DOMAIN_FUNCTION_2008_R2,
53 # how do we make this case insensitive??
55 parser = optparse.OptionParser("provision [options]")
56 sambaopts = options.SambaOptions(parser)
57 parser.add_option_group(sambaopts)
58 parser.add_option_group(options.VersionOptions(parser))
59 credopts = options.CredentialsOptions(parser)
60 parser.add_option_group(credopts)
61 parser.add_option("--interactive", help="Ask for names", action="store_true")
62 parser.add_option("--domain", type="string", metavar="DOMAIN",
63 help="set domain")
64 parser.add_option("--domain-guid", type="string", metavar="GUID",
65 help="set domainguid (otherwise random)")
66 parser.add_option("--domain-sid", type="string", metavar="SID",
67 help="set domainsid (otherwise random)")
68 parser.add_option("--ntds-guid", type="string", metavar="GUID",
69 help="set NTDS object GUID (otherwise random)")
70 parser.add_option("--invocationid", type="string", metavar="GUID",
71 help="set invocationid (otherwise random)")
72 parser.add_option("--host-name", type="string", metavar="HOSTNAME",
73 help="set hostname")
74 parser.add_option("--host-ip", type="string", metavar="IPADDRESS",
75 help="set IPv4 ipaddress")
76 parser.add_option("--host-ip6", type="string", metavar="IP6ADDRESS",
77 help="set IPv6 ipaddress")
78 parser.add_option("--adminpass", type="string", metavar="PASSWORD",
79 help="choose admin password (otherwise random)")
80 parser.add_option("--krbtgtpass", type="string", metavar="PASSWORD",
81 help="choose krbtgt password (otherwise random)")
82 parser.add_option("--machinepass", type="string", metavar="PASSWORD",
83 help="choose machine password (otherwise random)")
84 parser.add_option("--dns-backend", type="choice", metavar="NAMESERVER-BACKEND",
85 choices=["SAMBA_INTERNAL", "BIND9_FLATFILE", "BIND9_DLZ", "NONE"],
86 help="The DNS server backend. SAMBA_INTERNAL is the builtin name server, " \
87 "BIND9_FLATFILE uses bind9 text database to store zone information, " \
88 "BIND9_DLZ uses samba4 AD to store zone information (default), " \
89 "NONE skips the DNS setup entirely (not recommended)")
90 parser.add_option("--dnspass", type="string", metavar="PASSWORD",
91 help="choose dns password (otherwise random)")
92 parser.add_option("--ldapadminpass", type="string", metavar="PASSWORD",
93 help="choose password to set between Samba and it's LDAP backend (otherwise random)")
94 parser.add_option("--root", type="string", metavar="USERNAME",
95 help="choose 'root' unix username")
96 parser.add_option("--nobody", type="string", metavar="USERNAME",
97 help="choose 'nobody' user")
98 parser.add_option("--wheel", type="string", metavar="GROUPNAME",
99 help="choose 'wheel' privileged group")
100 parser.add_option("--users", type="string", metavar="GROUPNAME",
101 help="choose 'users' group")
102 parser.add_option("--quiet", help="Be quiet", action="store_true")
103 parser.add_option("--blank", action="store_true",
104 help="do not add users or groups, just the structure")
105 parser.add_option("--ldap-backend-type", type="choice", metavar="LDAP-BACKEND-TYPE",
106 help="Test initialisation support for unsupported LDAP backend type (fedora-ds or openldap) DO NOT USE",
107 choices=["fedora-ds", "openldap"])
108 parser.add_option("--server-role", type="choice", metavar="ROLE",
109 choices=["domain controller", "dc", "member server", "member", "standalone"],
110 help="The server role (domain controller | dc | member server | member | standalone). Default is dc.")
111 parser.add_option("--function-level", type="choice", metavar="FOR-FUN-LEVEL",
112 choices=["2000", "2003", "2008", "2008_R2"],
113 help="The domain and forest function level (2000 | 2003 | 2008 | 2008_R2 - always native). Default is (Windows) 2003 Native.")
114 parser.add_option("--next-rid", type="int", metavar="NEXTRID", default=1000,
115 help="The initial nextRid value (only needed for upgrades). Default is 1000.")
116 parser.add_option("--partitions-only",
117 help="Configure Samba's partitions, but do not modify them (ie, join a BDC)", action="store_true")
118 parser.add_option("--targetdir", type="string", metavar="DIR",
119 help="Set target directory")
120 parser.add_option("--ol-mmr-urls", type="string", metavar="LDAPSERVER",
121 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\"")
122 parser.add_option("--slapd-path", type="string", metavar="SLAPD-PATH",
123 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.")
124 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")
126 opts = parser.parse_args()[0]
128 logger = logging.getLogger("provision")
129 logger.addHandler(logging.StreamHandler(sys.stdout))
130 if opts.quiet:
131 logger.setLevel(logging.WARNING)
132 else:
133 logger.setLevel(logging.INFO)
135 if len(sys.argv) == 1:
136 opts.interactive = True
138 if opts.interactive:
139 from getpass import getpass
140 import socket
142 def ask(prompt, default=None):
143 if default is not None:
144 print "%s [%s]: " % (prompt, default),
145 else:
146 print "%s: " % (prompt,),
147 return sys.stdin.readline().rstrip("\n") or default
148 try:
149 default = socket.getfqdn().split(".", 1)[1].upper()
150 except IndexError:
151 default = None
152 opts.realm = ask("Realm", default)
153 if opts.realm in (None, ""):
154 print >>sys.stderr, "No realm set!"
155 sys.exit(1)
157 try:
158 default = opts.realm.split(".")[0]
159 except IndexError:
160 default = None
161 opts.domain = ask("Domain", default)
162 if opts.domain is None:
163 print >> sys.stderr, "No domain set!"
164 sys.exit(1)
166 opts.server_role = ask("Server Role (dc, member, standalone)", "dc")
167 for i in range(3):
168 opts.adminpass = getpass("Administrator password: ")
169 if not opts.adminpass:
170 print >>sys.stderr, "Invalid administrator password."
171 else:
172 break
173 else:
174 if opts.realm in (None, ""):
175 opts.realm = sambaopts._lp.get('realm')
176 if opts.realm is None or opts.domain is None:
177 if opts.realm is None:
178 print >>sys.stderr, "No realm set!"
179 if opts.domain is None:
180 print >> sys.stderr, "No domain set!"
181 parser.print_usage()
182 sys.exit(1)
184 if not opts.adminpass:
185 logger.info("Administrator password will be set randomly!")
187 lp = sambaopts.get_loadparm()
188 smbconf = lp.configfile
190 server_role = opts.server_role
192 if server_role is None:
193 server_role = "domain controller"
195 if opts.function_level is None:
196 dom_for_fun_level = DS_DOMAIN_FUNCTION_2003
197 elif opts.function_level == "2000":
198 dom_for_fun_level = DS_DOMAIN_FUNCTION_2000
199 elif opts.function_level == "2003":
200 dom_for_fun_level = DS_DOMAIN_FUNCTION_2003
201 elif opts.function_level == "2008":
202 dom_for_fun_level = DS_DOMAIN_FUNCTION_2008
203 elif opts.function_level == "2008_R2":
204 dom_for_fun_level = DS_DOMAIN_FUNCTION_2008_R2
206 if opts.dns_backend is None:
207 dns_backend = "BIND9_DLZ"
208 else:
209 dns_backend = opts.dns_backend
211 creds = credopts.get_credentials(lp)
213 creds.set_kerberos_state(DONT_USE_KERBEROS)
215 samdb_fill = FILL_FULL
216 if opts.blank:
217 samdb_fill = FILL_NT4SYNC
218 elif opts.partitions_only:
219 samdb_fill = FILL_DRS
221 eadb = True
222 if opts.use_xattrs == "yes":
223 eadb = False
224 elif opts.use_xattrs == "auto" and not lp.get("posix:eadb"):
225 if opts.targetdir:
226 file = tempfile.NamedTemporaryFile(prefix=os.path.abspath(opts.targetdir))
227 else:
228 file = tempfile.NamedTemporaryFile(prefix=os.path.abspath(os.path.dirname(lp.get("private dir"))))
229 try:
230 samba.ntacls.setntacl(lp, file.name,
231 "O:S-1-5-32G:S-1-5-32", "S-1-5-32", "native")
232 eadb = False
233 except:
234 logger.info("You are not root or your system do not support xattr, using tdb backend for attributes. "
235 "If you intend to use this provision in production, rerun the script as root on a system supporting xattrs.")
236 file.close()
238 session = system_session()
239 try:
240 provision(logger,
241 session, creds, smbconf=smbconf, targetdir=opts.targetdir,
242 samdb_fill=samdb_fill, realm=opts.realm, domain=opts.domain,
243 domainguid=opts.domain_guid, domainsid=opts.domain_sid,
244 hostname=opts.host_name,
245 hostip=opts.host_ip, hostip6=opts.host_ip6,
246 ntdsguid=opts.ntds_guid,
247 invocationid=opts.invocationid, adminpass=opts.adminpass,
248 krbtgtpass=opts.krbtgtpass, machinepass=opts.machinepass,
249 dns_backend=dns_backend,
250 dnspass=opts.dnspass, root=opts.root, nobody=opts.nobody,
251 wheel=opts.wheel, users=opts.users,
252 serverrole=server_role, dom_for_fun_level=dom_for_fun_level,
253 backend_type=opts.ldap_backend_type,
254 ldapadminpass=opts.ldapadminpass, ol_mmr_urls=opts.ol_mmr_urls,
255 slapd_path=opts.slapd_path,
256 useeadb=eadb, next_rid=opts.next_rid, lp=lp)
257 except ProvisioningError, e:
258 print str(e)
259 sys.exit(1)