606443a6edfba0bf9575e46585f267e7ed75bc24
[metze/samba/wip.git] / source4 / setup / provision
1 #!/usr/bin/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 getopt
26 import optparse
27 import os, sys
28
29 import samba
30
31 from auth import system_session
32 import samba.getopt as options
33 from samba.provision import (provision, 
34                              FILL_FULL, FILL_NT4SYNC,
35                              FILL_DRS)
36
37 parser = optparse.OptionParser("provision [options]")
38 sambaopts = options.SambaOptions(parser)
39 parser.add_option_group(sambaopts)
40 parser.add_option_group(options.VersionOptions(parser))
41 credopts = options.CredentialsOptions(parser)
42 parser.add_option_group(credopts)
43 parser.add_option("--setupdir", type="string", metavar="DIR", 
44                 help="directory with setup files")
45 parser.add_option("--realm", type="string", metavar="REALM", help="set realm")
46 parser.add_option("--domain", type="string", metavar="DOMAIN",
47                                   help="set domain")
48 parser.add_option("--domain-guid", type="string", metavar="GUID", 
49                 help="set domainguid (otherwise random)")
50 parser.add_option("--domain-sid", type="string", metavar="SID", 
51                 help="set domainsid (otherwise random)")
52 parser.add_option("--policy-guid", type="string", metavar="GUID",
53                                   help="set policy guid")
54 parser.add_option("--host-name", type="string", metavar="HOSTNAME", 
55                 help="set hostname")
56 parser.add_option("--host-ip", type="string", metavar="IPADDRESS", 
57                 help="set ipaddress")
58 parser.add_option("--host-guid", type="string", metavar="GUID", 
59                 help="set hostguid (otherwise random)")
60 parser.add_option("--invocationid", type="string", metavar="GUID", 
61                 help="set invocationid (otherwise random)")
62 parser.add_option("--adminpass", type="string", metavar="PASSWORD", 
63                 help="choose admin password (otherwise random)")
64 parser.add_option("--krbtgtpass", type="string", metavar="PASSWORD", 
65                 help="choose krbtgt password (otherwise random)")
66 parser.add_option("--machinepass", type="string", metavar="PASSWORD", 
67                 help="choose machine password (otherwise random)")
68 parser.add_option("--dnspass", type="string", metavar="PASSWORD", 
69                 help="choose dns password (otherwise random)")
70 parser.add_option("--root", type="string", metavar="USERNAME", 
71                 help="choose 'root' unix username")
72 parser.add_option("--nobody", type="string", metavar="USERNAME", 
73                 help="choose 'nobody' user")
74 parser.add_option("--nogroup", type="string", metavar="GROUPNAME", 
75                 help="choose 'nogroup' group")
76 parser.add_option("--wheel", type="string", metavar="GROUPNAME", 
77                 help="choose 'wheel' privileged group")
78 parser.add_option("--users", type="string", metavar="GROUPNAME", 
79                 help="choose 'users' group")
80 parser.add_option("--quiet", help="Be quiet", action="store_true")
81 parser.add_option("--blank", action="store_true",
82                 help="do not add users or groups, just the structure")
83 parser.add_option("--ldap-backend", type="string", metavar="LDAPSERVER", 
84                 help="LDAP server to use for this provision")
85 parser.add_option("--ldap-backend-type", type="choice", metavar="LDAP-BACKEND-TYPE", 
86                 help="LDB mapping module to use for the LDAP backend",
87                 choices=["fedora-ds", "openldap"])
88 parser.add_option("--aci", type="string", metavar="ACI", 
89                 help="An arbitary LDIF fragment, particularly useful to loading a backend ACI value into a target LDAP server. You must provide at least a realm and domain")
90 parser.add_option("--server-role", type="choice", metavar="ROLE",
91                           choices=["domain controller", "member server"],
92                 help="Set server role to provision for (default standalone)")
93 parser.add_option("--partitions-only", 
94                 help="Configure Samba's partitions, but do not modify them (ie, join a BDC)", action="store_true")
95 parser.add_option("--targetdir", type="string", metavar="DIR", 
96                           help="Set target directory")
97
98 opts = parser.parse_args()[0]
99
100 def message(text):
101         """print a message if quiet is not set."""
102         if not opts.quiet:
103                 print text
104
105 if opts.realm is None or opts.domain is None:
106         if opts.realm is None:
107                 print >>sys.stderr, "No realm set"
108         if opts.domain is None:
109                 print >>sys.stderr, "No domain set"
110         parser.print_usage()
111         sys.exit(1)
112
113 # cope with an initially blank smb.conf 
114
115 if sambaopts.get_loadparm_path() is not None:
116     smbconf = sambaopts.get_loadparm_path()
117
118 if opts.aci is not None:
119         print "set ACI: %s" % opts.aci
120
121 creds = credopts.get_credentials()
122
123 setup_dir = opts.setupdir
124 if setup_dir is None:
125         setup_dir = "setup"
126
127 samdb_fill = FILL_FULL
128 if opts.blank:
129     samdb_fill = FILL_NT4SYNC
130 elif opts.partitions_only:
131     samdb_fill = FILL_DRS
132
133 provision(setup_dir, message, 
134           system_session(), creds, smbconf=smbconf, 
135           samdb_fill=samdb_fill, realm=opts.realm,
136           domainguid=opts.domain_guid, domainsid=opts.domain_sid,
137           policyguid=opts.policy_guid, hostname=opts.host_name,
138           hostip=opts.host_ip, hostguid=opts.host_guid, 
139           invocationid=opts.invocationid, adminpass=opts.adminpass,
140           krbtgtpass=opts.krbtgtpass, machinepass=opts.machinepass,
141           dnspass=opts.dnspass, root=opts.root, nobody=opts.nobody,
142           nogroup=opts.nogroup, wheel=opts.wheel, users=opts.users,
143           aci=opts.aci, serverrole=opts.server_role, 
144           ldap_backend=opts.ldap_backend, 
145           ldap_backend_type=opts.ldap_backend_type)
146
147 message("To reproduce this provision, run with:")
148 def shell_escape(arg):
149     if " " in arg:
150         return '"%s"' % arg
151     return arg
152 message(" ".join([shell_escape(arg) for arg in sys.argv]))
153
154 message("All OK")