krb5pac.idl: add PAC_CREDENTIAL related structures
[Samba.git] / python / samba / sites.py
blob7111cfa4f686d52459d069aaacd4d4c0b20c58e9
1 # python site manipulation code
2 # Copyright Matthieu Patou <mat@matws.net> 2011
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 """Manipulating sites."""
20 import ldb
21 from ldb import FLAG_MOD_ADD, LdbError
24 class SiteException(Exception):
25 """Base element for Sites errors"""
27 def __init__(self, value):
28 self.value = value
30 def __str__(self):
31 return "%s: %s" % (self.__class__.__name__, self.value)
34 class SiteNotFoundException(SiteException):
35 """Raised when the site is not found and it's expected to exists."""
38 class SiteAlreadyExistsException(SiteException):
39 """Raised when the site is not found and it's expected not to exists."""
42 class SiteServerNotEmptyException(SiteException):
43 """Raised when the site still has servers attached."""
46 def create_site(samdb, configDn, siteName):
47 """
48 Create a site
50 :param samdb: A samdb connection
51 :param configDn: The DN of the configuration partition
52 :param siteName: Name of the site to create
53 :return: True upon success
54 :raise SiteAlreadyExists: if the site to be created already exists.
55 """
57 ret = samdb.search(base=configDn, scope=ldb.SCOPE_SUBTREE,
58 expression='(&(objectclass=Site)(cn=%s))' % siteName)
59 if len(ret) != 0:
60 raise SiteAlreadyExistsException('A site with the name %s already exists' % siteName)
62 m = ldb.Message()
63 m.dn = ldb.Dn(samdb, "Cn=%s,CN=Sites,%s" % (siteName, str(configDn)))
64 m["objectclass"] = ldb.MessageElement("site", FLAG_MOD_ADD, "objectclass")
66 samdb.add(m)
68 m2 = ldb.Message()
69 m2.dn = ldb.Dn(samdb, "Cn=NTDS Site Settings,%s" % str(m.dn))
70 m2["objectclass"] = ldb.MessageElement("nTDSSiteSettings", FLAG_MOD_ADD, "objectclass")
72 samdb.add(m2)
74 m3 = ldb.Message()
75 m3.dn = ldb.Dn(samdb, "Cn=Servers,%s" % str(m.dn))
76 m3["objectclass"] = ldb.MessageElement("serversContainer", FLAG_MOD_ADD, "objectclass")
78 samdb.add(m3)
80 return True
83 def delete_site(samdb, configDn, siteName):
84 """
85 Delete a site
87 :param samdb: A samdb connection
88 :param configDn: The DN of the configuration partition
89 :param siteName: Name of the site to delete
90 :return: True upon success
91 :raise SiteNotFoundException: if the site to be deleted do not exists.
92 :raise SiteServerNotEmpty: if the site has still servers in it.
93 """
95 dnsite = ldb.Dn(samdb, "CN=Sites")
96 if dnsite.add_base(configDn) == False:
97 raise SiteException("dnsites.add_base() failed")
98 if dnsite.add_child("CN=X") == False:
99 raise SiteException("dnsites.add_child() failed")
100 dnsite.set_component(0, "CN", siteName)
102 dnservers = ldb.Dn(samdb, "CN=Servers")
103 dnservers.add_base(dnsite)
105 try:
106 ret = samdb.search(base=dnsite, scope=ldb.SCOPE_BASE,
107 expression="objectClass=site")
108 if len(ret) != 1:
109 raise SiteNotFoundException('Site %s does not exist' % siteName)
110 except LdbError as (enum, estr):
111 if enum == ldb.ERR_NO_SUCH_OBJECT:
112 raise SiteNotFoundException('Site %s does not exist' % siteName)
114 ret = samdb.search(base=dnservers, scope=ldb.SCOPE_ONELEVEL,
115 expression='(objectclass=server)')
116 if len(ret) != 0:
117 raise SiteServerNotEmptyException('Site %s still has servers in it, move them before removal' % siteName)
119 samdb.delete(dnsite, ["tree_delete:0"])
121 return True