c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose/*
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose Authors:
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose Sumit Bose <sbose@redhat.com>
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose Alexander Bokovoy <abokovoy@redhat.com>
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose Copyright (C) 2013 Red Hat
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose This program is free software; you can redistribute it and/or modify
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose it under the terms of the GNU General Public License as published by
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose the Free Software Foundation; either version 3 of the License, or
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose (at your option) any later version.
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose This program is distributed in the hope that it will be useful,
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose but WITHOUT ANY WARRANTY; without even the implied warranty of
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose GNU General Public License for more details.
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose You should have received a copy of the GNU General Public License
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose along with this program. If not, see <http://www.gnu.org/licenses/>.
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose*/
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose#include <Python.h>
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose#include "util/sss_python.h"
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose#include "sss_client/idmap/sss_nss_idmap.h"
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose#define SSS_NAME_KEY "name"
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose#define SSS_SID_KEY "sid"
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose#define SSS_ID_KEY "id"
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose#define SSS_TYPE_KEY "type"
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Boseenum lookup_type {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose SIDBYNAME,
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose SIDBYID,
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose NAMEBYSID,
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose IDBYSID,
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose NAMEBYCERT
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose};
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bosestatic int add_dict(PyObject *py_result, PyObject *key, PyObject *res_type,
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyObject *res, PyObject *id_type)
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose{
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose int ret;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyObject *py_dict;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose py_dict = PyDict_New();
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (py_dict == NULL) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return ENOMEM;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose ret = PyDict_SetItem(py_dict, res_type, res);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (ret != 0) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose Py_XDECREF(py_dict);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return ret;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda ret = PyDict_SetItem(py_dict, PyBytes_FromString(SSS_TYPE_KEY), id_type);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (ret != 0) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose Py_XDECREF(py_dict);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return ret;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose ret = PyDict_SetItem(py_result, key, py_dict);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return ret;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose}
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
e1a9780b5c75602618773b1510e076897e129b36Sumit Bosestatic char *py_string_or_unicode_as_string(PyObject *inp)
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose{
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose PyObject *py_str = NULL;
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose if (PyUnicode_Check(inp)) {
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose py_str = PyUnicode_AsUTF8String(inp);
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda } else if (PyBytes_Check(inp)) {
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose py_str = inp;
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose } else {
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose PyErr_Format(PyExc_TypeError, "input must be unicode or a string");
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose return NULL;
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose }
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda return PyBytes_AS_STRING(py_str);
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose}
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bosestatic int do_getsidbyname(PyObject *py_result, PyObject *py_name)
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose{
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose int ret;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose const char *name;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose char *sid = NULL;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose enum sss_id_type id_type;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose name = py_string_or_unicode_as_string(py_name);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (name == NULL) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return EINVAL;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose ret = sss_nss_getsidbyname(name, &sid, &id_type);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (ret == 0) {
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda ret = add_dict(py_result, py_name, PyBytes_FromString(SSS_SID_KEY),
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda PyUnicode_FromString(sid), PYNUMBER_FROMLONG(id_type));
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose free(sid);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return ret;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose}
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bosestatic int do_getnamebysid(PyObject *py_result, PyObject *py_sid)
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose{
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose int ret;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose const char *sid;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose char *name = NULL;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose enum sss_id_type id_type;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose sid = py_string_or_unicode_as_string(py_sid);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (sid == NULL) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return EINVAL;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose ret = sss_nss_getnamebysid(sid, &name, &id_type);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (ret == 0) {
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda ret = add_dict(py_result, py_sid, PyBytes_FromString(SSS_NAME_KEY),
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda PyUnicode_FromString(name), PYNUMBER_FROMLONG(id_type));
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose free(name);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return ret;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose}
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bosestatic int do_getsidbyid(PyObject *py_result, PyObject *py_id)
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose{
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose long id;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose const char *id_str;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose char *endptr;
e506a551187dc92683f0903515dd1d2b57ce05e5Sumit Bose char *sid = NULL;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose int ret;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose enum sss_id_type id_type;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#ifndef IS_PY3K
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (PyInt_Check(py_id)) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose id = PyInt_AS_LONG(py_id);
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda } else
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#endif
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda if (PyLong_Check(py_id)) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose id = PyLong_AsLong(py_id);
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose } else {
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose id_str = py_string_or_unicode_as_string(py_id);
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose if (id_str == NULL) {
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose return EINVAL;
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose errno = 0;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose id = strtol(id_str, &endptr, 10);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (errno != 0 || *endptr != '\0') {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return EINVAL;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (id < 0 || id > UINT32_MAX) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return EINVAL;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose ret = sss_nss_getsidbyid((uint32_t) id, &sid, &id_type);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (ret == 0) {
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda ret = add_dict(py_result, py_id, PyBytes_FromString(SSS_SID_KEY),
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda PyUnicode_FromString(sid), PYNUMBER_FROMLONG(id_type));
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose free(sid);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return ret;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose}
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bosestatic int do_getnamebycert(PyObject *py_result, PyObject *py_cert)
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose{
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose int ret;
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose const char *cert;
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose char *name = NULL;
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose enum sss_id_type id_type;
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose cert = py_string_or_unicode_as_string(py_cert);
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose if (cert == NULL) {
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose return EINVAL;
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose }
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose ret = sss_nss_getnamebycert(cert, &name, &id_type);
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose if (ret == 0) {
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose ret = add_dict(py_result, py_cert, PyBytes_FromString(SSS_NAME_KEY),
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose PyUnicode_FromString(name), PYNUMBER_FROMLONG(id_type));
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose }
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose free(name);
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose return ret;
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose}
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bosestatic int do_getidbysid(PyObject *py_result, PyObject *py_sid)
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose{
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose const char *sid;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose uint32_t id;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose enum sss_id_type id_type;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose int ret;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose sid = py_string_or_unicode_as_string(py_sid);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (sid == NULL) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return EINVAL;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose ret = sss_nss_getidbysid(sid, &id, &id_type);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (ret == 0) {
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda ret = add_dict(py_result, py_sid, PyBytes_FromString(SSS_ID_KEY),
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda PYNUMBER_FROMLONG(id), PYNUMBER_FROMLONG(id_type));
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return ret;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose}
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bosestatic int do_lookup(enum lookup_type type, PyObject *py_result,
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyObject *py_inp)
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose{
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose switch(type) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose case SIDBYNAME:
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return do_getsidbyname(py_result, py_inp);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose break;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose case NAMEBYSID:
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return do_getnamebysid(py_result, py_inp);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose break;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose case SIDBYID:
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return do_getsidbyid(py_result, py_inp);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose break;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose case IDBYSID:
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return do_getidbysid(py_result, py_inp);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose break;
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose case NAMEBYCERT:
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose return do_getnamebycert(py_result, py_inp);
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose break;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose default:
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return ENOSYS;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return ENOSYS;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose}
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bosestatic PyObject *check_args(enum lookup_type type, PyObject *args)
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose{
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyObject *obj, *py_value;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose int ret;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose Py_ssize_t len, i;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyObject *py_result;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (!PyArg_ParseTuple(args, sss_py_const_p(char, "O"), &obj)) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyErr_Format(PyExc_ValueError, "Unable to retrieve argument\n");
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return NULL;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose if (!(PyList_Check(obj) || PyTuple_Check(obj) ||
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda PyBytes_Check(obj) || PyUnicode_Check(obj) ||
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda (type == SIDBYID && (PYNUMBER_CHECK(obj))))) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyErr_Format(PyExc_ValueError,
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose "Only string, long or list or tuples of them " \
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose "are accepted\n");
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return NULL;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose py_result = PyDict_New();
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose Py_XINCREF(py_result);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (py_result == NULL) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyErr_Format(PyExc_MemoryError,
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose "Unable to allocate resulting dictionary\n");
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return NULL;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
4188968d18f31806da808d922bcd48501a02f92dSumit Bose if (PyList_Check(obj) || PyTuple_Check(obj)) {
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose len = PySequence_Size(obj);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose for(i=0; i < len; i++) {
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose py_value = PySequence_GetItem(obj, i);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if ((py_value != NULL) &&
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda (PyBytes_Check(py_value) || PyUnicode_Check(py_value) ||
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda (type == SIDBYID && PYNUMBER_CHECK(py_value)))) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose ret = do_lookup(type, py_result, py_value);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose if (ret != 0) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose /* Skip this name */
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose continue;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose } else {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose ret = do_lookup(type, py_result, obj);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose switch (ret) {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose case 0:
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose case ENOENT: /* nothing found, return empty dict */
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose break;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose case EINVAL:
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose PyErr_Format(PyExc_ValueError, "Unable to retrieve result\n");
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose Py_XDECREF(py_result);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return NULL;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose break;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose default:
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyErr_Format(PyExc_IOError, "Operation not supported\n");
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose Py_XDECREF(py_result);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return NULL;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose Py_XDECREF(py_result);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return py_result;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose}
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BosePyDoc_STRVAR(getsidbyname_doc,
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose"getsidbyname(name or list/tuple of names) -> dict(name => dict(results))\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BoseReturns a dictionary with a dictonary of results for each given name.\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BoseThe result dictonary contain the SID and the type of the object which can be\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Boseaccessed with the key constants SID_KEY and TYPE_KEY, respectively.\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BoseThe return type can be one of the following constants:\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose- ID_NOT_SPECIFIED\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose- ID_USER\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose- ID_GROUP\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose- ID_BOTH"
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bosestatic PyObject * py_getsidbyname(PyObject *module, PyObject *args)
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose{
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return check_args(SIDBYNAME, args);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose}
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BosePyDoc_STRVAR(getsidbyid_doc,
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose"getsidbyid(id or list/tuple of id) -> dict(id => dict(results))\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BoseReturns a dictionary with a dictonary of results for each given POSIX ID.\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BoseThe result dictonary contain the SID and the type of the object which can be\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Boseaccessed with the key constants SID_KEY and TYPE_KEY, respectively."
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bosestatic PyObject * py_getsidbyid(PyObject *module, PyObject *args)
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose{
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return check_args(SIDBYID, args);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose}
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BosePyDoc_STRVAR(getnamebysid_doc,
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose"getnamebysid(sid or list/tuple of sid) -> dict(sid => dict(results))\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BoseReturns a dictionary with a dictonary of results for each given SID.\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BoseThe result dictonary contain the name and the type of the object which can be\n\
fed544f8c960bf346679870843854bf43e83c453Ondrej Kosaccessed with the key constants NAME_KEY and TYPE_KEY, respectively.\n\
fed544f8c960bf346679870843854bf43e83c453Ondrej Kos\n\
fed544f8c960bf346679870843854bf43e83c453Ondrej KosNOTE: getnamebysid currently works only with id_provider set as \"ad\" or \"ipa\""
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bosestatic PyObject * py_getnamebysid(PyObject *module, PyObject *args)
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose{
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return check_args(NAMEBYSID, args);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose}
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BosePyDoc_STRVAR(getidbysid_doc,
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose"getidbysid(sid) -> POSIX ID\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BoseReturns the POSIX ID of the object with the given SID."
e1a9780b5c75602618773b1510e076897e129b36Sumit Bose"getidbysid(sid or list/tuple of sid) -> dict(sid => dict(results))\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BoseReturns a dictionary with a dictonary of results for each given SID.\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BoseThe result dictonary contain the POSIX ID and the type of the object which\n\
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bosecan be accessed with the key constants ID_KEY and TYPE_KEY, respectively."
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bosestatic PyObject * py_getidbysid(PyObject *module, PyObject *args)
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose{
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose return check_args(IDBYSID, args);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose}
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
9c88f837ffacf6548c13825589b327de1a5525f3Sumit BosePyDoc_STRVAR(getnamebycert_doc,
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose"getnamebycert(sid or list/tuple of certificates) -> dict(sid => dict(results))\n\
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose\n\
9c88f837ffacf6548c13825589b327de1a5525f3Sumit BoseReturns a dictionary with a dictonary of results for each given certificates.\n\
9c88f837ffacf6548c13825589b327de1a5525f3Sumit BoseThe result dictonary contain the name and the type of the object which can be\n\
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Boseaccessed with the key constants NAME_KEY and TYPE_KEY, respectively.\n\
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose\n\
9c88f837ffacf6548c13825589b327de1a5525f3Sumit BoseNOTE: getnamebycert currently works only with id_provider set as \"ad\" or \"ipa\""
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose);
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bosestatic PyObject * py_getnamebycert(PyObject *module, PyObject *args)
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose{
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose return check_args(NAMEBYCERT, args);
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose}
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bosestatic PyMethodDef methods[] = {
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose { sss_py_const_p(char, "getsidbyname"), (PyCFunction) py_getsidbyname,
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose METH_VARARGS, getsidbyname_doc },
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose { sss_py_const_p(char, "getsidbyid"), (PyCFunction) py_getsidbyid,
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose METH_VARARGS, getsidbyid_doc },
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose { sss_py_const_p(char, "getnamebysid"), (PyCFunction) py_getnamebysid,
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose METH_VARARGS, getnamebysid_doc },
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose { sss_py_const_p(char, "getidbysid"), (PyCFunction) py_getidbysid,
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose METH_VARARGS, getidbysid_doc },
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose { sss_py_const_p(char, "getnamebycert"), (PyCFunction) py_getnamebycert,
9c88f837ffacf6548c13825589b327de1a5525f3Sumit Bose METH_VARARGS, getnamebycert_doc },
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose { NULL,NULL, 0, NULL }
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose};
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#ifdef IS_PY3K
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrdastatic struct PyModuleDef pysss_nss_idmap_def = {
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda PyModuleDef_HEAD_INIT,
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda "pysss_nss_idmap",
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda NULL,
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda -1,
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda methods,
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda NULL,
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda NULL,
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda NULL,
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda NULL
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda};
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
341a00311680a440d7f979f06c34c70d86c9367aBohuslav KabrdaPyMODINIT_FUNC
341a00311680a440d7f979f06c34c70d86c9367aBohuslav KabrdaPyInit_pysss_nss_idmap(void)
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#else
c080a11e9e88f35e40aff4e476cabbd971833019Sumit BosePyMODINIT_FUNC
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Boseinitpysss_nss_idmap(void)
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#endif
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose{
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyObject *module;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#ifdef IS_PY3K
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda module = PyModule_Create(&pysss_nss_idmap_def);
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#else
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose module = Py_InitModule3(sss_py_const_p(char, "pysss_nss_idmap"),
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose methods,
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose sss_py_const_p(char, "SSSD ID-mapping functions"));
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#endif
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda if (module == NULL)
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda MODINITERROR;
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyModule_AddIntConstant(module, "ID_NOT_SPECIFIED",
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose SSS_ID_TYPE_NOT_SPECIFIED);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyModule_AddIntConstant(module, "ID_USER", SSS_ID_TYPE_UID);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyModule_AddIntConstant(module, "ID_GROUP", SSS_ID_TYPE_GID);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyModule_AddIntConstant(module, "ID_BOTH", SSS_ID_TYPE_BOTH);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyModule_AddStringConstant(module, "SID_KEY", SSS_SID_KEY);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyModule_AddStringConstant(module, "NAME_KEY", SSS_NAME_KEY);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyModule_AddStringConstant(module, "ID_KEY", SSS_ID_KEY);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose PyModule_AddStringConstant(module, "TYPE_KEY", SSS_TYPE_KEY);
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#ifdef IS_PY3K
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda return module;
341a00311680a440d7f979f06c34c70d86c9367aBohuslav Kabrda#endif
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose}