pwdmodule.c 5.79 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1

2
/* UNIX password file access module */
Guido van Rossum's avatar
Guido van Rossum committed
3

Barry Warsaw's avatar
Barry Warsaw committed
4
#include "Python.h"
5
#include "posixmodule.h"
Guido van Rossum's avatar
Guido van Rossum committed
6 7 8

#include <pwd.h>

9
static PyStructSequence_Field struct_pwd_type_fields[] = {
10 11 12 13 14 15 16 17
    {"pw_name", "user name"},
    {"pw_passwd", "password"},
    {"pw_uid", "user id"},
    {"pw_gid", "group id"},
    {"pw_gecos", "real name"},
    {"pw_dir", "home directory"},
    {"pw_shell", "shell program"},
    {0}
18 19
};

20
PyDoc_STRVAR(struct_passwd__doc__,
21 22 23
"pwd.struct_passwd: Results from getpw*() routines.\n\n\
This object may be accessed either as a tuple of\n\
  (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
24
or via the object attributes as named in the above tuple.");
25 26

static PyStructSequence_Desc struct_pwd_type_desc = {
27 28 29 30
    "pwd.struct_passwd",
    struct_passwd__doc__,
    struct_pwd_type_fields,
    7,
31 32
};

33 34
PyDoc_STRVAR(pwd__doc__,
"This module provides access to the Unix password database.\n\
35 36 37 38 39 40
It is available on all Unix versions.\n\
\n\
Password database entries are reported as 7-tuples containing the following\n\
items from the password database (see `<pwd.h>'), in order:\n\
pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
The uid and gid items are integers, all others are strings. An\n\
41
exception is raised if the entry asked for cannot be found.");
42

43

44
static int initialized;
45 46
static PyTypeObject StructPwdType;

47
static void
48
sets(PyObject *v, int i, const char* val)
49
{
50
  if (val) {
51 52
      PyObject *o = PyUnicode_DecodeFSDefault(val);
      PyStructSequence_SET_ITEM(v, i, o);
53
  }
54
  else {
55 56
      PyStructSequence_SET_ITEM(v, i, Py_None);
      Py_INCREF(Py_None);
57 58 59
  }
}

Barry Warsaw's avatar
Barry Warsaw committed
60
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
61
mkpwent(struct passwd *p)
Guido van Rossum's avatar
Guido van Rossum committed
62
{
63 64 65 66
    int setIndex = 0;
    PyObject *v = PyStructSequence_New(&StructPwdType);
    if (v == NULL)
        return NULL;
67

68
#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
69
#define SETS(i,val) sets(v, i, val)
70

71
    SETS(setIndex++, p->pw_name);
72
#ifdef __VMS
73
    SETS(setIndex++, "");
74
#else
75
    SETS(setIndex++, p->pw_passwd);
76
#endif
77 78
    PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
    PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
79
#ifdef __VMS
80
    SETS(setIndex++, "");
81
#else
82
    SETS(setIndex++, p->pw_gecos);
83
#endif
84 85
    SETS(setIndex++, p->pw_dir);
    SETS(setIndex++, p->pw_shell);
86 87 88 89

#undef SETS
#undef SETI

90 91 92 93
    if (PyErr_Occurred()) {
        Py_XDECREF(v);
        return NULL;
    }
94

95
    return v;
Guido van Rossum's avatar
Guido van Rossum committed
96 97
}

98 99 100
PyDoc_STRVAR(pwd_getpwuid__doc__,
"getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
                  pw_gid,pw_gecos,pw_dir,pw_shell)\n\
101
Return the password database entry for the given numeric user ID.\n\
102
See help(pwd) for more on password database entries.");
103

Barry Warsaw's avatar
Barry Warsaw committed
104
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
105
pwd_getpwuid(PyObject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
106
{
107
    uid_t uid;
108
    struct passwd *p;
109 110 111 112
    if (!PyArg_ParseTuple(args, "O&:getpwuid", _Py_Uid_Converter, &uid)) {
        if (PyErr_ExceptionMatches(PyExc_OverflowError))
            PyErr_Format(PyExc_KeyError,
                         "getpwuid(): uid not found");
113
        return NULL;
114
    }
115
    if ((p = getpwuid(uid)) == NULL) {
116 117 118
        PyObject *uid_obj = _PyLong_FromUid(uid);
        if (uid_obj == NULL)
            return NULL;
119
        PyErr_Format(PyExc_KeyError,
120 121
                     "getpwuid(): uid not found: %S", uid_obj);
        Py_DECREF(uid_obj);
122 123 124
        return NULL;
    }
    return mkpwent(p);
Guido van Rossum's avatar
Guido van Rossum committed
125 126
}

127 128 129
PyDoc_STRVAR(pwd_getpwnam__doc__,
"getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
                    pw_gid,pw_gecos,pw_dir,pw_shell)\n\
130
Return the password database entry for the given user name.\n\
131
See help(pwd) for more on password database entries.");
132

Barry Warsaw's avatar
Barry Warsaw committed
133
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
134
pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
135
{
136 137 138 139 140 141
    char *name;
    struct passwd *p;
    PyObject *arg, *bytes, *retval = NULL;

    if (!PyArg_ParseTuple(args, "U:getpwnam", &arg))
        return NULL;
142
    if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
143 144 145 146 147 148 149 150 151
        return NULL;
    if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
        goto out;
    if ((p = getpwnam(name)) == NULL) {
        PyErr_Format(PyExc_KeyError,
                     "getpwnam(): name not found: %s", name);
        goto out;
    }
    retval = mkpwent(p);
152
out:
153 154
    Py_DECREF(bytes);
    return retval;
Guido van Rossum's avatar
Guido van Rossum committed
155 156
}

157
#ifdef HAVE_GETPWENT
158 159
PyDoc_STRVAR(pwd_getpwall__doc__,
"getpwall() -> list_of_entries\n\
160 161
Return a list of all available password database entries, \
in arbitrary order.\n\
162
See help(pwd) for more on password database entries.");
163

Barry Warsaw's avatar
Barry Warsaw committed
164
static PyObject *
165
pwd_getpwall(PyObject *self)
Guido van Rossum's avatar
Guido van Rossum committed
166
{
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    PyObject *d;
    struct passwd *p;
    if ((d = PyList_New(0)) == NULL)
        return NULL;
    setpwent();
    while ((p = getpwent()) != NULL) {
        PyObject *v = mkpwent(p);
        if (v == NULL || PyList_Append(d, v) != 0) {
            Py_XDECREF(v);
            Py_DECREF(d);
            endpwent();
            return NULL;
        }
        Py_DECREF(v);
    }
    endpwent();
    return d;
Guido van Rossum's avatar
Guido van Rossum committed
184
}
185
#endif
Guido van Rossum's avatar
Guido van Rossum committed
186

Barry Warsaw's avatar
Barry Warsaw committed
187
static PyMethodDef pwd_methods[] = {
188 189
    {"getpwuid",        pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
    {"getpwnam",        pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
190
#ifdef HAVE_GETPWENT
191 192
    {"getpwall",        (PyCFunction)pwd_getpwall,
        METH_NOARGS,  pwd_getpwall__doc__},
193
#endif
194
    {NULL,              NULL}           /* sentinel */
Guido van Rossum's avatar
Guido van Rossum committed
195 196
};

197
static struct PyModuleDef pwdmodule = {
198 199 200 201 202 203 204 205 206
    PyModuleDef_HEAD_INIT,
    "pwd",
    pwd__doc__,
    -1,
    pwd_methods,
    NULL,
    NULL,
    NULL,
    NULL
207 208 209
};


210
PyMODINIT_FUNC
211
PyInit_pwd(void)
Guido van Rossum's avatar
Guido van Rossum committed
212
{
213 214 215 216 217 218
    PyObject *m;
    m = PyModule_Create(&pwdmodule);
    if (m == NULL)
        return NULL;

    if (!initialized) {
219 220 221
        if (PyStructSequence_InitType2(&StructPwdType,
                                       &struct_pwd_type_desc) < 0)
            return NULL;
222 223 224 225 226
        initialized = 1;
    }
    Py_INCREF((PyObject *) &StructPwdType);
    PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
    return m;
Guido van Rossum's avatar
Guido van Rossum committed
227
}