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