8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek#
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek# Module for simulation of utility "getent passwd -s sss" from coreutils
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek#
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek# Copyright (c) 2016 Red Hat, Inc.
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek#
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek# This is free software; you can redistribute it and/or modify it
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek# under the terms of the GNU General Public License as published by
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek# the Free Software Foundation; version 2 only
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek#
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek# This program is distributed in the hope that it will be useful, but
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek# WITHOUT ANY WARRANTY; without even the implied warranty of
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek# General Public License for more details.
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek#
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek# You should have received a copy of the GNU General Public License
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek# along with this program. If not, see <http://www.gnu.org/licenses/>.
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek#
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozekfrom ctypes import (c_int, c_char_p, c_ulong, POINTER,
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek Structure, create_string_buffer, get_errno)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozekfrom sssd_nss import NssReturnCode, SssdNssError, nss_sss_ctypes_loader
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub HrozekPASSWD_BUFLEN = 1024
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozekclass Passwd(Structure):
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek _fields_ = [("pw_name", c_char_p),
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek ("pw_passwd", c_char_p),
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek ("pw_uid", c_int),
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek ("pw_gid", c_int),
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek ("pw_gecos", c_char_p),
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek ("pw_dir", c_char_p),
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek ("pw_shell", c_char_p)]
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozekdef set_user_dict(res, result_p):
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek if res != NssReturnCode.SUCCESS:
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek return dict()
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek user_dict = dict()
bac4458c89a589055ae3daf4f72cc7dba886264aLukas Slebodnik user_dict['name'] = result_p[0].pw_name.decode('utf-8')
bac4458c89a589055ae3daf4f72cc7dba886264aLukas Slebodnik user_dict['passwd'] = result_p[0].pw_passwd.decode('utf-8')
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek user_dict['uid'] = result_p[0].pw_uid
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek user_dict['gid'] = result_p[0].pw_gid
bac4458c89a589055ae3daf4f72cc7dba886264aLukas Slebodnik user_dict['gecos'] = result_p[0].pw_gecos.decode('utf-8')
bac4458c89a589055ae3daf4f72cc7dba886264aLukas Slebodnik user_dict['dir'] = result_p[0].pw_dir.decode('utf-8')
bac4458c89a589055ae3daf4f72cc7dba886264aLukas Slebodnik user_dict['shell'] = result_p[0].pw_shell.decode('utf-8')
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek return user_dict
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozekdef getpwnam_r(name, result_p, buffer_p, buflen):
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek """
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek ctypes wrapper for:
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek enum nss_status _nss_sss_getpwnam_r(const char *name,
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek struct passwd *result,
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek char *buffer,
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek size_t buflen,
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek int *errnop)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek """
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek func = nss_sss_ctypes_loader("_nss_sss_getpwnam_r")
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek func.restype = c_int
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek func.argtypes = [c_char_p, POINTER(Passwd),
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek c_char_p, c_ulong, POINTER(c_int)]
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek errno = POINTER(c_int)(c_int(0))
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
bac4458c89a589055ae3daf4f72cc7dba886264aLukas Slebodnik name = name.encode('utf-8')
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek res = func(c_char_p(name), result_p, buffer_p, buflen, errno)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek return (int(res), int(errno[0]), result_p)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozekdef getpwuid_r(uid, result_p, buffer_p, buflen):
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek """
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek ctypes wrapper for:
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek enum nss_status _nss_sss_getpwuid_r(uid_t uid,
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek struct passwd *result,
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek char *buffer,
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek size_t buflen,
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek int *errnop)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek """
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek func = nss_sss_ctypes_loader("_nss_sss_getpwuid_r")
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek func.restype = c_int
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek func.argtypes = [c_ulong, POINTER(Passwd),
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek c_char_p, c_ulong, POINTER(c_int)]
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek errno = POINTER(c_int)(c_int(0))
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek res = func(uid, result_p, buffer_p, buflen, errno)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek return (int(res), int(errno[0]), result_p)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozekdef setpwent():
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek """
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek ctypes wrapper for:
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek void setpwent(void)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek """
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek func = nss_sss_ctypes_loader("_nss_sss_setpwent")
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek func.argtypes = []
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek res = func()
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek assert res == NssReturnCode.SUCCESS
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek errno = get_errno()
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek if errno != 0:
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek raise SssdNssError(errno, "setpwent")
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozekdef endpwent():
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek """
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek ctypes wrapper for:
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek void endpwent(void)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek """
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek func = nss_sss_ctypes_loader("_nss_sss_endpwent")
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek func.argtypes = []
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek res = func()
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek assert res == NssReturnCode.SUCCESS
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek errno = get_errno()
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek if errno != 0:
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek raise SssdNssError(errno, "endpwent")
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozekdef getpwent_r(result_p, buffer_p, buflen):
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek """
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek ctypes wrapper for:
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek enum nss_status _nss_sss_getpwent_r(struct passwd *result,
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek char *buffer, size_t buflen,
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek int *errnop)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek """
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek func = nss_sss_ctypes_loader("_nss_sss_getpwent_r")
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek func.restype = c_int
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek func.argtypes = [POINTER(Passwd), c_char_p, c_ulong, POINTER(c_int)]
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek errno = POINTER(c_int)(c_int(0))
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek res = func(result_p, buffer_p, buflen, errno)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek return (int(res), int(errno[0]), result_p)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozekdef getpwent():
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek result = Passwd()
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek result_p = POINTER(Passwd)(result)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek buff = create_string_buffer(PASSWD_BUFLEN)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek res, errno, result_p = getpwent_r(result_p, buff, PASSWD_BUFLEN)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek if errno != 0:
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek raise SssdNssError(errno, "getpwent_r")
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek user_dict = set_user_dict(res, result_p)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek return res, user_dict
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozekdef call_sssd_getpwnam(name):
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek """
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek A Python wrapper to retrieve a user by name. Returns:
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek (res, user_dict)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek if res is NssReturnCode.SUCCESS, then user_dict contains the keys
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek corresponding to the C passwd structure fields. Otherwise, the dictionary
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek is empty and errno indicates the error code
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek """
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek result = Passwd()
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek result_p = POINTER(Passwd)(result)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek buff = create_string_buffer(PASSWD_BUFLEN)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek res, errno, result_p = getpwnam_r(name, result_p, buff, PASSWD_BUFLEN)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek if errno != 0:
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek raise SssdNssError(errno, "getpwnam_r")
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek user_dict = set_user_dict(res, result_p)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek return res, user_dict
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozekdef call_sssd_getpwuid(uid):
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek """
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek A Python wrapper to retrieve a user by UID. Returns:
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek (res, user_dict)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek if res is NssReturnCode.SUCCESS, then user_dict contains the keys
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek corresponding to the C passwd structure fields. Otherwise, the dictionary
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek is empty and errno indicates the error code
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek """
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek result = Passwd()
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek result_p = POINTER(Passwd)(result)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek buff = create_string_buffer(PASSWD_BUFLEN)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek res, errno, result_p = getpwuid_r(uid, result_p, buff, PASSWD_BUFLEN)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek if errno != 0:
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek raise SssdNssError(errno, "getpwuid_r")
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek user_dict = set_user_dict(res, result_p)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek return res, user_dict
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozekdef call_sssd_enumeration():
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek """
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek enumerate users from sssd module only
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek """
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek setpwent()
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek user_list = []
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek res, user = getpwent()
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek while res == NssReturnCode.SUCCESS:
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek user_list.append(user)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek res, user = getpwent()
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek endpwent()
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek return user_list