source: vendor/3.6.0/source4/scripting/bin/rebuildextendeddn

Last change on this file was 740, checked in by Silvan Scherrer, 13 years ago

Samba Server: update vendor to 3.6.0

File size: 4.5 KB
Line 
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#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 3 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program. If not, see <http://www.gnu.org/licenses/>.
24#
25
26import optparse
27import os
28import sys
29# Find right directory when running from source tree
30sys.path.insert(0, "bin/python")
31
32import samba
33from samba.credentials import DONT_USE_KERBEROS
34from samba.auth import system_session
35from samba import Ldb
36from ldb import SCOPE_SUBTREE, SCOPE_BASE
37import ldb
38import samba.getopt as options
39from samba import param
40from samba.provision import ProvisionNames, provision_paths_from_lp
41from samba.schema import get_dnsyntax_attributes, get_linked_attributes
42
43parser = optparse.OptionParser("provision [options]")
44sambaopts = options.SambaOptions(parser)
45parser.add_option_group(sambaopts)
46parser.add_option_group(options.VersionOptions(parser))
47credopts = options.CredentialsOptions(parser)
48parser.add_option_group(credopts)
49parser.add_option("--targetdir", type="string", metavar="DIR",
50 help="Set target directory")
51
52opts = parser.parse_args()[0]
53
54def message(text):
55 """print a message if quiet is not set."""
56 if not opts.quiet:
57 print text
58
59if len(sys.argv) == 1:
60 opts.interactive = True
61
62lp = sambaopts.get_loadparm()
63smbconf = lp.configfile
64
65creds = credopts.get_credentials(lp)
66
67creds.set_kerberos_state(DONT_USE_KERBEROS)
68
69session = system_session()
70
71
72def get_paths(targetdir=None,smbconf=None):
73 if targetdir is not None:
74 if (not os.path.exists(os.path.join(targetdir, "etc"))):
75 os.makedirs(os.path.join(targetdir, "etc"))
76 smbconf = os.path.join(targetdir, "etc", "smb.conf")
77 if smbconf is None:
78 smbconf = param.default_path()
79
80 if not os.path.exists(smbconf):
81 print >>sys.stderr, "Unable to find smb.conf .. "+smbconf
82 parser.print_usage()
83 sys.exit(1)
84
85 lp = param.LoadParm()
86 lp.load(smbconf)
87 paths = provision_paths_from_lp(lp,"foo")
88 return paths
89
90
91
92def rebuild_en_dn(credentials,session_info,paths):
93 lp = param.LoadParm()
94 lp.load(paths.smbconf)
95 names = ProvisionNames()
96 names.domain = lp.get("workgroup")
97 names.realm = lp.get("realm")
98 names.rootdn = "DC=" + names.realm.replace(".",",DC=")
99
100 attrs = ["dn" ]
101 dn = ""
102 sam_ldb = Ldb(paths.samdb, session_info=session_info, credentials=credentials,lp=lp)
103 attrs2 = ["schemaNamingContext"]
104 res2 = sam_ldb.search(expression="(objectClass=*)",base="", scope=SCOPE_BASE, attrs=attrs2)
105 attrs.extend(get_linked_attributes(ldb.Dn(sam_ldb,str(res2[0]["schemaNamingContext"])),sam_ldb).keys())
106 attrs.extend(get_dnsyntax_attributes(ldb.Dn(sam_ldb,str(res2[0]["schemaNamingContext"])),sam_ldb)),
107 sam_ldb.transaction_start()
108 res = sam_ldb.search(expression="(cn=*)", scope=SCOPE_SUBTREE, attrs=attrs,controls=["search_options:1:2"]
109)
110 mod = ""
111 for i in range (0,len(res)):
112 #print >>sys.stderr,res[i].dn
113 dn = res[i].dn
114 for att in res[i]:
115 if ( (att != "dn" and att != "cn") and not (res[i][att] is None) ):
116 m = ldb.Message()
117 m.dn = ldb.Dn(sam_ldb, str(dn))
118 saveatt = []
119 for j in range (0,len( res[i][att])):
120 mod = mod +att +": "+str(res[i][att][j])+"\n"
121 saveatt.append(str(res[i][att][j]))
122 m[att] = ldb.MessageElement(saveatt, ldb.FLAG_MOD_REPLACE, att)
123 sam_ldb.modify(m)
124 res3 = sam_ldb.search(expression="(&(dn=%s)(%s=*))"%(dn,att),scope=SCOPE_SUBTREE, attrs=[att],controls=["search_options:1:2"])
125 if( len(res3) == 0 or (len(res3[0][att])!= len(saveatt))):
126 print >>sys.stderr, str(dn) + " has no attr " +att+ " or a wrong value"
127 for satt in saveatt:
128 print >>sys.stderr,str(att)+" = "+satt
129 sam_ldb.transaction_cancel()
130 sam_ldb.transaction_commit()
131
132
133
134
135paths = get_paths(targetdir=opts.targetdir,smbconf=smbconf)
136
137
138rebuild_en_dn(creds,session,paths)
139
Note: See TracBrowser for help on using the repository browser.