1 | #!/usr/bin/env python
|
---|
2 | #
|
---|
3 | # Unix SMB/CIFS implementation.
|
---|
4 | # Extended attributes (re)building
|
---|
5 | # Copyright (C) Matthieu Patou <mat@matws.net> 2009
|
---|
6 | #
|
---|
7 | # Based on provision a Samba4 server by
|
---|
8 | # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
|
---|
9 | # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2008
|
---|
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 optparse
|
---|
26 | import os
|
---|
27 | import sys
|
---|
28 | # Find right directory when running from source tree
|
---|
29 | sys.path.insert(0, "bin/python")
|
---|
30 |
|
---|
31 | import samba
|
---|
32 | from samba.credentials import DONT_USE_KERBEROS
|
---|
33 | from samba.auth import system_session
|
---|
34 | from samba import Ldb
|
---|
35 | from ldb import SCOPE_SUBTREE, SCOPE_BASE
|
---|
36 | import ldb
|
---|
37 | import samba.getopt as options
|
---|
38 | from samba import param
|
---|
39 | from samba.provision import ProvisionNames, provision_paths_from_lp
|
---|
40 | from samba.schema import get_dnsyntax_attributes, get_linked_attributes
|
---|
41 |
|
---|
42 | parser = optparse.OptionParser("provision [options]")
|
---|
43 | sambaopts = options.SambaOptions(parser)
|
---|
44 | parser.add_option_group(sambaopts)
|
---|
45 | parser.add_option_group(options.VersionOptions(parser))
|
---|
46 | credopts = options.CredentialsOptions(parser)
|
---|
47 | parser.add_option_group(credopts)
|
---|
48 | parser.add_option("--targetdir", type="string", metavar="DIR",
|
---|
49 | help="Set target directory")
|
---|
50 |
|
---|
51 | opts = parser.parse_args()[0]
|
---|
52 |
|
---|
53 | def message(text):
|
---|
54 | """print a message if quiet is not set."""
|
---|
55 | if not opts.quiet:
|
---|
56 | print text
|
---|
57 |
|
---|
58 | if len(sys.argv) == 1:
|
---|
59 | opts.interactive = True
|
---|
60 |
|
---|
61 | lp = sambaopts.get_loadparm()
|
---|
62 | smbconf = lp.configfile
|
---|
63 |
|
---|
64 | creds = credopts.get_credentials(lp)
|
---|
65 |
|
---|
66 | creds.set_kerberos_state(DONT_USE_KERBEROS)
|
---|
67 |
|
---|
68 | session = system_session()
|
---|
69 |
|
---|
70 |
|
---|
71 | def get_paths(targetdir=None,smbconf=None):
|
---|
72 | if targetdir is not None:
|
---|
73 | if (not os.path.exists(os.path.join(targetdir, "etc"))):
|
---|
74 | os.makedirs(os.path.join(targetdir, "etc"))
|
---|
75 | smbconf = os.path.join(targetdir, "etc", "smb.conf")
|
---|
76 | if smbconf is None:
|
---|
77 | smbconf = param.default_path()
|
---|
78 |
|
---|
79 | if not os.path.exists(smbconf):
|
---|
80 | print >>sys.stderr, "Unable to find smb.conf .. "+smbconf
|
---|
81 | parser.print_usage()
|
---|
82 | sys.exit(1)
|
---|
83 |
|
---|
84 | lp = param.LoadParm()
|
---|
85 | lp.load(smbconf)
|
---|
86 | paths = provision_paths_from_lp(lp,"foo")
|
---|
87 | return paths
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 | def rebuild_en_dn(credentials,session_info,paths):
|
---|
92 | lp = param.LoadParm()
|
---|
93 | lp.load(paths.smbconf)
|
---|
94 | names = ProvisionNames()
|
---|
95 | names.domain = lp.get("workgroup")
|
---|
96 | names.realm = lp.get("realm")
|
---|
97 | names.rootdn = "DC=" + names.realm.replace(".",",DC=")
|
---|
98 |
|
---|
99 | attrs = ["dn" ]
|
---|
100 | dn = ""
|
---|
101 | sam_ldb = Ldb(paths.samdb, session_info=session_info, credentials=credentials,lp=lp)
|
---|
102 | attrs2 = ["schemaNamingContext"]
|
---|
103 | res2 = sam_ldb.search(expression="(objectClass=*)",base="", scope=SCOPE_BASE, attrs=attrs2)
|
---|
104 | attrs.extend(get_linked_attributes(ldb.Dn(sam_ldb,str(res2[0]["schemaNamingContext"])),sam_ldb).keys())
|
---|
105 | attrs.extend(get_dnsyntax_attributes(ldb.Dn(sam_ldb,str(res2[0]["schemaNamingContext"])),sam_ldb))
|
---|
106 | sam_ldb.transaction_start()
|
---|
107 | res = sam_ldb.search(expression="(cn=*)", scope=SCOPE_SUBTREE, attrs=attrs,controls=["search_options:1:2"])
|
---|
108 | mod = ""
|
---|
109 | for i in range (0,len(res)):
|
---|
110 | #print >>sys.stderr,res[i].dn
|
---|
111 | dn = res[i].dn
|
---|
112 | for att in res[i]:
|
---|
113 | if ( (att != "dn" and att != "cn") and not (res[i][att] is None) ):
|
---|
114 | m = ldb.Message()
|
---|
115 | m.dn = ldb.Dn(sam_ldb, str(dn))
|
---|
116 | saveatt = []
|
---|
117 | for j in range (0,len( res[i][att])):
|
---|
118 | mod = mod +att +": "+str(res[i][att][j])+"\n"
|
---|
119 | saveatt.append(str(res[i][att][j]))
|
---|
120 | m[att] = ldb.MessageElement(saveatt, ldb.FLAG_MOD_REPLACE, att)
|
---|
121 | sam_ldb.modify(m)
|
---|
122 | res3 = sam_ldb.search(expression="(&(distinguishedName=%s)(%s=*))"%(dn,att),scope=SCOPE_SUBTREE, attrs=[att],controls=["search_options:1:2"])
|
---|
123 | if( len(res3) == 0 or (len(res3[0][att])!= len(saveatt))):
|
---|
124 | print >>sys.stderr, str(dn) + " has no attr " +att+ " or a wrong value"
|
---|
125 | for satt in saveatt:
|
---|
126 | print >>sys.stderr,str(att)+" = "+satt
|
---|
127 | sam_ldb.transaction_cancel()
|
---|
128 | sam_ldb.transaction_commit()
|
---|
129 |
|
---|
130 |
|
---|
131 | paths = get_paths(targetdir=opts.targetdir, smbconf=smbconf)
|
---|
132 |
|
---|
133 | rebuild_en_dn(creds,session,paths)
|
---|
134 |
|
---|