diff options
Diffstat (limited to 'src/python/pysss_nss_idmap.c')
-rw-r--r-- | src/python/pysss_nss_idmap.c | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/src/python/pysss_nss_idmap.c b/src/python/pysss_nss_idmap.c new file mode 100644 index 000000000..cf68a68a8 --- /dev/null +++ b/src/python/pysss_nss_idmap.c @@ -0,0 +1,169 @@ +/* + Authors: + Sumit Bose <sbose@redhat.com> + + Copyright (C) 2012 Red Hat + + 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/>. +*/ + +#include <Python.h> +#include "util/sss_python.h" + +#include "sss_client/idmap/sss_nss_idmap.h" + +PyDoc_STRVAR(getsidbyname_doc, +"getsidbyname(name) -> SID string\n\ +\n\ +Returns the SID of the object with the given name." +); + +static PyObject * py_getsidbyname(PyObject *module, PyObject *args) +{ + const char *name; + char *sid; + int ret; + PyObject *py_sid; + + if (!PyArg_ParseTuple(args, sss_py_const_p(char, "s"), &name)) { + PyErr_Format(PyExc_ValueError, "Invalid argument\n"); + return NULL; + } + + ret = sss_nss_getsidbyname(name, &sid); + if (ret != 0) { + PyErr_Format(PyExc_IOError, "Operation not supported."); + return NULL; + } + + py_sid = PyString_FromString(sid); + + free(sid); + + return py_sid; +} + +PyDoc_STRVAR(getsidbyid_doc, +"getsidbyid(id) -> SID string\n\ +\n\ +Returns the SID of the object with the given POSIX ID." +); + +static PyObject * py_getsidbyid(PyObject *module, PyObject *args) +{ + long long id; + char *sid; + int ret; + PyObject *py_sid; + + if (!PyArg_ParseTuple(args, sss_py_const_p(char, "L"), &id)) { + PyErr_Format(PyExc_ValueError, "Invalid argument\n"); + return NULL; + } + + if (id < 0 || id > UINT32_MAX) { + PyErr_Format(PyExc_ValueError, "Invalid value\n"); + return NULL; + } + + ret = sss_nss_getsidbyid((uint32_t) id, &sid); + if (ret != 0) { + PyErr_Format(PyExc_IOError, "Operation not supported."); + return NULL; + } + + py_sid = PyString_FromString(sid); + + free(sid); + + return py_sid; +} + +PyDoc_STRVAR(getnamebysid_doc, +"getnamebysid(sid) -> Name\n\ +\n\ +Returns the name of the object with the given SID." +); + +static PyObject * py_getnamebysid(PyObject *module, PyObject *args) +{ + const char *sid; + char *name; + int ret; + PyObject *py_name; + + if (!PyArg_ParseTuple(args, sss_py_const_p(char, "s"), &sid)) { + PyErr_Format(PyExc_ValueError, "Invalid argument\n"); + return NULL; + } + + ret = sss_nss_getnamebysid(sid, &name); + if (ret != 0) { + PyErr_Format(PyExc_IOError, "Operation not supported."); + return NULL; + } + + py_name = PyString_FromString(name); + + free(name); + + return py_name; +} + +PyDoc_STRVAR(getidbysid_doc, +"getidbysid(sid) -> POSIX ID\n\ +\n\ +Returns the POSIX ID of the object with the given SID." +); + +static PyObject * py_getidbysid(PyObject *module, PyObject *args) +{ + const char *sid; + uint32_t id; + enum sss_id_type id_type; + int ret; + + if (!PyArg_ParseTuple(args, sss_py_const_p(char, "s"), &sid)) { + PyErr_Format(PyExc_ValueError, "Invalid argument\n"); + return NULL; + } + + ret = sss_nss_getidbysid(sid, &id, &id_type); + if (ret != 0) { + PyErr_Format(PyExc_IOError, "Operation not supported."); + return NULL; + } + + return PyInt_FromLong((long) id); +} + +static PyMethodDef methods[] = { + { sss_py_const_p(char, "getsidbyname"), (PyCFunction) py_getsidbyname, + METH_VARARGS, getsidbyname_doc }, + { sss_py_const_p(char, "getsidbyid"), (PyCFunction) py_getsidbyid, + METH_VARARGS, getsidbyid_doc }, + { sss_py_const_p(char, "getnamebysid"), (PyCFunction) py_getnamebysid, + METH_VARARGS, getnamebysid_doc }, + { sss_py_const_p(char, "getidbysid"), (PyCFunction) py_getidbysid, + METH_VARARGS, getidbysid_doc }, + { NULL,NULL, 0, NULL } +}; + + +PyMODINIT_FUNC +initpysss_nss_idmap(void) +{ + Py_InitModule3(sss_py_const_p(char, "pysss_nss_idmap"), + methods, sss_py_const_p(char, "SSSD ID-mapping functions")); +} |