|
0 |
- |
1 |
#!/usr/bin/python2.7 |
|
|
2 |
# coding: utf-8 |
3 |
# |
4 |
# Univention Management Console module: |
5 |
# System Diagnosis UMC module |
6 |
# |
7 |
# Copyright 2017 Univention GmbH |
8 |
# |
9 |
# http://www.univention.de/ |
10 |
# |
11 |
# All rights reserved. |
12 |
# |
13 |
# The source code of this program is made available |
14 |
# under the terms of the GNU Affero General Public License version 3 |
15 |
# (GNU AGPL V3) as published by the Free Software Foundation. |
16 |
# |
17 |
# Binary versions of this program provided by Univention to you as |
18 |
# well as other copyrighted, protected or trademarked materials like |
19 |
# Logos, graphics, fonts, specific documentations and configurations, |
20 |
# cryptographic keys etc. are subject to a license agreement between |
21 |
# you and Univention and not subject to the GNU AGPL V3. |
22 |
# |
23 |
# In the case you use this program under the terms of the GNU AGPL V3, |
24 |
# the program is provided in the hope that it will be useful, |
25 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
26 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
27 |
# GNU Affero General Public License for more details. |
28 |
# |
29 |
# You should have received a copy of the GNU Affero General Public |
30 |
# License with the Debian GNU/Linux or Univention distribution in file |
31 |
# /usr/share/common-licenses/AGPL-3; if not, see |
32 |
# <http://www.gnu.org/licenses/>. |
33 |
|
34 |
import os |
35 |
import ldap |
36 |
import socket |
37 |
|
38 |
try: |
39 |
import samba.param |
40 |
import samba.credentials |
41 |
from samba.dcerpc import drsuapi |
42 |
from samba import drs_utils |
43 |
except ImportError: |
44 |
SAMBA_AVAILABLE = False |
45 |
else: |
46 |
SAMBA_AVAILABLE = True |
47 |
|
48 |
import univention.uldap |
49 |
from univention.management.console.modules.diagnostic import Warning |
50 |
|
51 |
from univention.lib.i18n import Translation |
52 |
_ = Translation('univention-management-console-module-diagnostic').translate |
53 |
|
54 |
title = _('Check Samba replication status for errors') |
55 |
description = _('No errors found.'), |
56 |
|
57 |
|
58 |
def is_service_active(service): |
59 |
lo = univention.uldap.getMachineConnection() |
60 |
raw_filter = '(&(univentionService=%s)(cn=%s))' |
61 |
filter_expr = ldap.filter.filter_format(raw_filter, (service, socket.gethostname())) |
62 |
for (dn, _attr) in lo.search(filter_expr, attr=['cn']): |
63 |
if dn is not None: |
64 |
return True |
65 |
return False |
66 |
|
67 |
|
68 |
class DRSUAPI(object): |
69 |
def __init__(self, dc=None): |
70 |
(self.load_param, self.credentials) = self.samba_credentials() |
71 |
self.server = dc or self.netcmd_dnsname(self.load_param) |
72 |
drs_tuple = drs_utils.drsuapi_connect(self.server, self.load_param, self.credentials) |
73 |
(self.drsuapi, self.handle, _bind_supported_extensions) = drs_tuple |
74 |
|
75 |
@staticmethod |
76 |
def netcmd_dnsname(lp): |
77 |
'''return the full DNS name of our own host. Used as a default |
78 |
for hostname when running status queries''' |
79 |
return lp.get('netbios name').lower() + "." + lp.get('realm').lower() |
80 |
|
81 |
@staticmethod |
82 |
def samba_credentials(): |
83 |
load_param = samba.param.LoadParm() |
84 |
load_param.set("debug level", "0") |
85 |
if os.getenv("SMB_CONF_PATH") is not None: |
86 |
load_param.load(os.getenv("SMB_CONF_PATH")) |
87 |
else: |
88 |
load_param.load_default() |
89 |
credentials = samba.credentials.Credentials() |
90 |
credentials.guess(load_param) |
91 |
if not credentials.authentication_requested(): |
92 |
credentials.set_machine_account(load_param) |
93 |
return (load_param, credentials) |
94 |
|
95 |
def _replica_info(self, info_type): |
96 |
req1 = drsuapi.DsReplicaGetInfoRequest1() |
97 |
req1.info_type = info_type |
98 |
(info_type, info) = self.drsuapi.DsReplicaGetInfo(self.handle, 1, req1) |
99 |
return (info_type, info) |
100 |
|
101 |
def neighbours(self): |
102 |
(info_type, info) = self._replica_info(drsuapi.DRSUAPI_DS_REPLICA_INFO_NEIGHBORS) |
103 |
for neighbour in info.array: |
104 |
yield neighbour |
105 |
|
106 |
(info_type, info) = self._replica_info(drsuapi.DRSUAPI_DS_REPLICA_INFO_REPSTO) |
107 |
for neighbour in info.array: |
108 |
yield neighbour |
109 |
|
110 |
def replication_problems(self): |
111 |
for neighbour in self.neighbours(): |
112 |
(ecode, estring) = neighbour.result_last_attempt |
113 |
if ecode != 0: |
114 |
yield ReplicationProblem(neighbour) |
115 |
|
116 |
|
117 |
class ReplicationProblem(Exception): |
118 |
def __init__(self, neighbour): |
119 |
super(ReplicationProblem, self).__init__(neighbour) |
120 |
self.neighbour = neighbour |
121 |
|
122 |
def __str__(self): |
123 |
msg = _('In {nc!r}: error during DRS replication from {source}.') |
124 |
source = self._parse_ntds_dn(self.neighbour.source_dsa_obj_dn) |
125 |
return msg.format(nc=self.neighbour.naming_context_dn.encode(), |
126 |
source=source) |
127 |
|
128 |
@staticmethod |
129 |
def _parse_ntds_dn(dn): |
130 |
exploded = ldap.dn.str2dn(dn) |
131 |
if len(exploded) >= 5: |
132 |
(first, second, third, fourth, fifth) = exploded[:5] |
133 |
valid_ntds_dn = all((first[0][1] == 'NTDS Settings', |
134 |
third[0][1] == 'Servers', fifth[0][1] == 'Sites')) |
135 |
if valid_ntds_dn: |
136 |
return '{}/{}'.format(fourth[0][1], second[0][1]) |
137 |
return dn |
138 |
|
139 |
|
140 |
def run(): |
141 |
if not is_service_active('Samba 4') or not SAMBA_AVAILABLE: |
142 |
return |
143 |
|
144 |
drs = DRSUAPI() |
145 |
problems = list(drs.replication_problems()) |
146 |
if problems: |
147 |
ed = [_('`samba-tool drs showrepl` returned a problem with the replication.')] |
148 |
ed.extend(str(error) for error in problems) |
149 |
raise Warning(description='\n'.join(ed)) |
150 |
|
151 |
|
152 |
if __name__ == '__main__': |
153 |
from univention.management.console.modules.diagnostic import main |
154 |
main() |
1 |
`samba_tool_showrepl.py` (po) |
155 |
`samba_tool_showrepl.py` (po) |
2 |
-- |
|
|
3 |
.../umc/python/diagnostic/de.po | 20 ++++++++++++++++++-- |
156 |
.../umc/python/diagnostic/de.po | 20 ++++++++++++++++++-- |
4 |
1 file changed, 18 insertions(+), 2 deletions(-) |
157 |
1 file changed, 18 insertions(+), 2 deletions(-) |