s4-provision Use ProvisioningError and the eadb
[samba.git] / source4 / setup / upgrade_from_s3
1 #!/usr/bin/env python
2 #
3 # Upgrade from Samba3
4 # Copyright Jelmer Vernooij 2005-2007
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 import logging
20 import optparse
21 import os, sys
22 import tempfile
23
24 # Find right directory when running from source tree
25 sys.path.insert(0, "bin/python")
26
27 import samba
28 import samba.getopt as options
29 from samba.auth import system_session
30 from samba.upgrade import upgrade_from_samba3
31 from samba.samba3 import Samba3
32 from samba.samba3 import param as s3param
33 from samba.provision import ProvisioningError
34
35 def get_testparm_var(testparm, varname):
36     cmd = "%s -s -l --parameter-name='%s' 2>/dev/null" % (testparm, varname)
37     output = os.popen(cmd, 'r').readline()
38     return output.strip()
39
40 cmd_help = """upgrade_from_s3 [options] <configuration_file> <targetdir>
41
42 Input arguments are:
43   <configuration_file> - path to existing smb.conf
44   <targetdir>          - directory in which samba4 database will be created
45
46 In addition, specify either samba3 database directory (with --libdir) or
47 samba3 testparm utility (with --testparm).  """
48
49 parser = optparse.OptionParser(cmd_help)
50 parser.add_option_group(options.VersionOptions(parser))
51 parser.add_option("--quiet", help="Be quiet")
52 parser.add_option("--libdir", type="string", metavar="DIR",
53                   help="samba3 database directory")
54 parser.add_option("--testparm", type="string", metavar="PATH",
55                   help="samba3 testparm utility")
56 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")
57
58 opts, args = parser.parse_args()
59
60 if len(args) < 2:
61     parser.print_usage()
62     sys.exit(1)
63
64 smbconf = args[0]
65 if not os.path.exists(smbconf):
66     print("error: %s does not exist" % smbconf)
67     sys.exit(1)
68
69 targetdir = args[1]
70 if not os.path.isdir(targetdir):
71     print("error: %s is not a directory" % targetdir)
72     sys.exit(1)
73
74 libdir = opts.libdir
75 testparm = opts.testparm
76
77 if not libdir and not testparm:
78     print("error: please specify either libdir or testparm")
79     sys.exit(1)
80
81 if libdir and testparm:
82     print("warning: both libdir and testparm specified, libdir will be ignored.")
83     libdir = None
84
85 logger = logging.getLogger("upgrade")
86 logger.addHandler(logging.StreamHandler(sys.stdout))
87 if opts.quiet:
88         logger.setLevel(logging.WARNING)
89 else:
90         logger.setLevel(logging.INFO)
91
92 s3conf = s3param.get_context()
93
94 eadb = True
95 if opts.use_xattrs == "yes":
96         eadb = False
97 elif opts.use_xattrs == "auto" and not s3conf.get("posix:eadb"):
98         file = tempfile.NamedTemporaryFile()
99         try:
100                 samba.ntacls.setntacl(lp, file.name,
101                         "O:S-1-5-32G:S-1-5-32", "S-1-5-32", "native")
102                 eadb = False
103         except:
104                 logger.info("You are not root or your system do not support xattr, using tdb backend for attributes. "
105                             "If you intend to use this provision in production, rerun the script as root on a system supporting xattrs.")
106         file.close()
107
108 # Set correct default values from libdir or testparm
109 paths = {}
110 if libdir:
111     paths["state directory"] = libdir
112     paths["private dir"] = libdir
113     paths["lock directory"] = libdir
114 else:
115     paths["state directory"] = get_testparm_var(testparm, "state directory")
116     paths["private dir"] = get_testparm_var(testparm, "private dir")
117     paths["lock directory"] = get_testparm_var(testparm, "lock directory")
118
119 for p in paths:
120     s3conf.set(p, paths[p])
121
122 # load smb.conf parameters
123 logger.info("Reading smb.conf")
124 s3conf.load(smbconf)
125 samba3 = Samba3(smbconf, s3conf)
126
127 logger.info("Provisioning")
128 try:
129     upgrade_from_samba3(samba3, logger, targetdir, session_info=system_session(), useeadb=eadb)
130 except ProvisioningError, e:
131         print str(e)
132         exit(1)