8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek# Module for simulation of utility "getent passwd -s sss" from coreutils
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek# Copyright (c) 2016 Red Hat, Inc.
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# 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# 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 Hrozekfrom ctypes import (c_int, c_char_p, c_ulong, POINTER,
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozekfrom sssd_nss import NssReturnCode, SssdNssError, nss_sss_ctypes_loader
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')
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 Hrozekdef getpwnam_r(name, result_p, buffer_p, buflen):
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 func = nss_sss_ctypes_loader("_nss_sss_getpwnam_r")
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek res = func(c_char_p(name), result_p, buffer_p, buflen, errno)
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozekdef getpwuid_r(uid, result_p, buffer_p, buflen):
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 func = nss_sss_ctypes_loader("_nss_sss_getpwuid_r")
137e105ac8ca3476d2f74d24ae13860774937000Jakub Hrozek res = func(uid, result_p, buffer_p, buflen, errno)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek ctypes wrapper for:
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek void setpwent(void)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek func = nss_sss_ctypes_loader("_nss_sss_setpwent")
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek ctypes wrapper for:
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek void endpwent(void)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek func = nss_sss_ctypes_loader("_nss_sss_endpwent")
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 func = nss_sss_ctypes_loader("_nss_sss_getpwent_r")
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek func.argtypes = [POINTER(Passwd), c_char_p, c_ulong, POINTER(c_int)]
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek res, errno, result_p = getpwent_r(result_p, buff, PASSWD_BUFLEN)
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 res, errno, result_p = getpwnam_r(name, result_p, buff, PASSWD_BUFLEN)
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 res, errno, result_p = getpwuid_r(uid, result_p, buff, PASSWD_BUFLEN)
8578fba1500d43ad9632784462c255bf8bb360feJakub Hrozek enumerate users from sssd module only