summaryrefslogtreecommitdiffstats
path: root/commands/sssd/lmi/scripts/sssd/__init__.py
blob: 07ecd5b0e7be2197cb2d5b24fc6ec1f5105397e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# SSSD Providers
#
# Copyright (C) 2013-2014 Red Hat, Inc.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are those
# of the authors and should not be interpreted as representing official policies,
# either expressed or implied, of the FreeBSD Project.
#
#
# Authors: Pavel Brezina <pbrezina@redhat.com>
#
"""
LMI SSSD provider client library.

This set of functions can list and manage SSSD's responders and domains.
"""

from lmi.scripts.common.errors import LmiFailed
from lmi.shell.LMIInstanceName import LMIInstanceName
from lmi.scripts.common import get_computer_system
from lmi.scripts.common import get_logger
import pywbem
import lmi.scripts.common

LOG = get_logger(__name__)

def debug_level(level):
    """
    Return hexadecimal representation of debug level.
    
    :type level: int
    :param level: Debug level.
    :rtype: string
    """
    return "%#.4x" % level

#
# SSSD
#

def set_debug_level(ns, level, until_restart, components):
    rval = 0
    for component in ns.LMI_SSSDComponent.instances():
        found = False
        if components is not None and len(components) > 0:
            for name in components:
                if component.Name == name:
                    found = True
                    continue
        if not found:
            continue
        if until_restart:
            (rval, _, msg) = component.SetDebugLevelTemporarily({'debug_level' : int(level, 16)})
        else:
            (rval, _, msg) = component.SetDebugLevelPermanently({'debug_level' : int(level, 16)})
        if rval == 0:
            LOG().info('Debug level of "%s" changed to "%#.4x".',
                       component.Name, level)
        elif msg:
            LOG().error('Operation failed on "%s": %s.',
                        component.Name, errorstr)
    return rval

#
# Services
#

def list_services(ns, kind='all'):
    for svc in ns.LMI_SSSDResponder.instances():
        if kind == 'disabled' and svc.IsEnabled == True:
            continue
        if kind == 'enabled' and svc.IsEnabled == False:
            continue
        yield svc
        
def get_service(ns, service):
    keys = {'Name': service}
    try:
        inst = ns.LMI_SSSDResponder.new_instance_name(keys).to_instance()
    except pywbem.CIMError, err:
        if err[0] == pywbem.CIM_ERR_NOT_FOUND:
            raise LmiFailed("Cannot find the service: %s" % service)
        raise
    return inst

def enable_service(ns, service):
    instance = get_service(ns, service)
    (rval, _, msg) = instance.Enable()
    if rval == 0:
        LOG().info('Service "%s" enabled', service)
    elif msg:
        LOG().error('Operation failed on "%s": %s.', service, errorstr)
    return rval

def disable_service(ns, service):
    instance = get_service(ns, service)
    (rval, _, msg) = instance.Disable()
    if rval == 0:
        LOG().info('Service "%s" disabled', service)
    elif msg:
        LOG().error('Operation failed on "%s": %s.', service, errorstr)
    return rval

#
# Domains
#

def list_backends(ns, kind='all'):
    for backend in ns.LMI_SSSDBackend.instances():
        if kind == 'disabled' and backend.IsEnabled == True:
            continue
        if kind == 'enabled' and backend.IsEnabled == False:
            continue
        yield backend
        
def get_provider(ns, type, backend):
    for provider in backend.associators(AssocClass="LMI_SSSDBackendProvider"):
        if provider.Type == type:
            return provider.Module
    return 'ldap'

def get_domain(ns, domain):
    keys = {'Name': domain}
    try:
        inst = ns.LMI_SSSDDomain.new_instance_name(keys).to_instance()
    except pywbem.CIMError, err:
        if err[0] == pywbem.CIM_ERR_NOT_FOUND:
            raise LmiFailed("Cannot find the domain: %s" % service)
        raise
    return inst

def get_backend(ns, domain):
    keys = {'Name': domain}
    try:
        inst = ns.LMI_SSSDBackend.new_instance_name(keys).to_instance()
    except pywbem.CIMError, err:
        if err[0] == pywbem.CIM_ERR_NOT_FOUND:
            raise LmiFailed("Cannot find the backend: %s" % service)
        raise
    return inst

def enable_backend(ns, domain):
    instance = get_backend(ns, domain)
    (rval, _, msg) = instance.Enable()
    if rval == 0:
        LOG().info('Domain "%s" enabled', domain)
    elif msg:
        LOG().error('Operation failed on "%s": %s.', domain, errorstr)
    return rval

def disable_backend(ns, domain):
    instance = get_backend(ns, domain)
    (rval, _, msg) = instance.Disable()
    if rval == 0:
        LOG().info('Domain "%s" disabled', domain)
    elif msg:
        LOG().error('Operation failed on "%s": %s.', domain, errorstr)
    return rval

#
# Subdomains
#

def list_subdomains_names(ns, domain):
    subdomains = domain.associators(AssocClass="LMI_SSSDDomainSubdomain",
                                    ResultRole="Subdomain")
    
    for subdomain in subdomains:
        yield subdomain.Name
        
def list_subdomains_comma_separated(ns, domain):
    return ', '.join(list_subdomains_names(ns, domain))