summaryrefslogtreecommitdiffstats
path: root/src/python/pysss_nss_idmap.c
blob: cf68a68a8114280d67c2fd19c8726caa955176aa (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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"));
}