summaryrefslogtreecommitdiffstats
path: root/src/tests/intg/sssd_group.py
blob: b6a37c5014cd4ff52ad26a2a273e709d5526f709 (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
#
# Module for simulation of utility "getent group -s sss" from coreutils
#
# Copyright (c) 2016 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from ctypes import (c_int, c_char_p, c_ulong, POINTER, Structure,
                    create_string_buffer)
from sssd_nss import NssReturnCode, SssdNssError, nss_sss_ctypes_loader

GROUP_BUFLEN = 1024


class Group(Structure):
    _fields_ = [("gr_name", c_char_p),
                ("gr_passwd", c_char_p),
                ("gr_gid", c_int),
                ("gr_mem", POINTER(c_char_p))]


def getgrnam_r(name, result_p, buffer_p, buflen):
    """
    ctypes wrapper for:
        enum nss_status _nss_sss_getgrnam_r(const char *name,
                                            struct group *result,
                                            char *buffer,
                                            size_t buflen,
                                            int *errnop)
    """
    func = nss_sss_ctypes_loader("_nss_sss_getgrnam_r")
    func.restype = c_int
    func.argtypes = [c_char_p, POINTER(Group),
                     c_char_p, c_ulong, POINTER(c_int)]

    errno = POINTER(c_int)(c_int(0))

    name = name.encode('utf-8')
    res = func(c_char_p(name), result_p, buffer_p, buflen, errno)

    return (int(res), int(errno[0]), result_p)


def getgrgid_r(gid, result_p, buffer_p, buflen):
    """
    ctypes wrapper for:
        enum nss_status _nss_sss_getgrgid_r(gid_t gid,
                                            struct passwd *result,
                                            char *buffer,
                                            size_t buflen,
                                            int *errnop)
    """
    func = nss_sss_ctypes_loader("_nss_sss_getgrgid_r")
    func.restype = c_int
    func.argtypes = [c_ulong, POINTER(Group),
                     c_char_p, c_ulong, POINTER(c_int)]

    errno = POINTER(c_int)(c_int(0))

    res = func(gid, result_p, buffer_p, buflen, errno)

    return (int(res), int(errno[0]), result_p)


def set_group_dict(res, result_p):
    if res != NssReturnCode.SUCCESS:
        return dict()

    group_dict = dict()
    group_dict['name'] = result_p[0].gr_name.decode('utf-8')
    group_dict['gid'] = result_p[0].gr_gid
    group_dict['mem'] = list()

    i = 0
    while result_p[0].gr_mem[i] is not None:
        grp_name = result_p[0].gr_mem[i].decode('utf-8')
        group_dict['mem'].append(grp_name)
        i = i + 1

    return group_dict


def call_sssd_getgrnam(name):
    """
    A Python wrapper to retrieve a group by name. Returns:
        (res, group_dict)
    if res is NssReturnCode.SUCCESS, then group_dict contains the keys
    corresponding to the C passwd structure fields. Otherwise, the dictionary
    is empty and errno indicates the error code
    """
    result = Group()
    result_p = POINTER(Group)(result)
    buff = create_string_buffer(GROUP_BUFLEN)

    res, errno, result_p = getgrnam_r(name, result_p, buff, GROUP_BUFLEN)
    if errno != 0:
        raise SssdNssError(errno, "getgrnam_r")

    group_dict = set_group_dict(res, result_p)
    return res, group_dict


def call_sssd_getgrgid(gid):
    """
    A Python wrapper to retrieve a group by GID. Returns:
        (res, group_dict)
    if res is NssReturnCode.SUCCESS, then group_dict contains the keys
    corresponding to the C passwd structure fields. Otherwise, the dictionary
    is empty and errno indicates the error code
    """
    result = Group()
    result_p = POINTER(Group)(result)
    buff = create_string_buffer(GROUP_BUFLEN)

    res, errno, result_p = getgrgid_r(gid, result_p, buff, GROUP_BUFLEN)
    if errno != 0:
        raise SssdNssError(errno, "getgrgid_r")

    group_dict = set_group_dict(res, result_p)
    return res, group_dict