~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Modules/pwdmodule.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* UNIX password file access module */
 
3
 
 
4
#include "Python.h"
 
5
#include "posixmodule.h"
 
6
 
 
7
#include <pwd.h>
 
8
 
 
9
static PyStructSequence_Field struct_pwd_type_fields[] = {
 
10
    {"pw_name", "user name"},
 
11
    {"pw_passwd", "password"},
 
12
    {"pw_uid", "user id"},
 
13
    {"pw_gid", "group id"},
 
14
    {"pw_gecos", "real name"},
 
15
    {"pw_dir", "home directory"},
 
16
    {"pw_shell", "shell program"},
 
17
    {0}
 
18
};
 
19
 
 
20
PyDoc_STRVAR(struct_passwd__doc__,
 
21
"pwd.struct_passwd: Results from getpw*() routines.\n\n\
 
22
This object may be accessed either as a tuple of\n\
 
23
  (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
    "pwd.struct_passwd",
 
28
    struct_passwd__doc__,
 
29
    struct_pwd_type_fields,
 
30
    7,
 
31
};
 
32
 
 
33
PyDoc_STRVAR(pwd__doc__,
 
34
"This module provides access to the Unix password database.\n\
 
35
It is available on all Unix versions.\n\
 
36
\n\
 
37
Password database entries are reported as 7-tuples containing the following\n\
 
38
items from the password database (see `<pwd.h>'), in order:\n\
 
39
pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
 
40
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
static PyTypeObject StructPwdType;
 
46
 
 
47
static void
 
48
sets(PyObject *v, int i, const char* val)
 
49
{
 
50
  if (val) {
 
51
      PyObject *o = PyUnicode_DecodeFSDefault(val);
 
52
      PyStructSequence_SET_ITEM(v, i, o);
 
53
  }
 
54
  else {
 
55
      PyStructSequence_SET_ITEM(v, i, Py_None);
 
56
      Py_INCREF(Py_None);
 
57
  }
 
58
}
 
59
 
 
60
static PyObject *
 
61
mkpwent(struct passwd *p)
 
62
{
 
63
    int setIndex = 0;
 
64
    PyObject *v = PyStructSequence_New(&StructPwdType);
 
65
    if (v == NULL)
 
66
        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
    PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
 
78
    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
    SETS(setIndex++, p->pw_dir);
 
85
    SETS(setIndex++, p->pw_shell);
 
86
 
 
87
#undef SETS
 
88
#undef SETI
 
89
 
 
90
    if (PyErr_Occurred()) {
 
91
        Py_XDECREF(v);
 
92
        return NULL;
 
93
    }
 
94
 
 
95
    return v;
 
96
}
 
97
 
 
98
PyDoc_STRVAR(pwd_getpwuid__doc__,
 
99
"getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
 
100
                  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
 
 
104
static PyObject *
 
105
pwd_getpwuid(PyObject *self, PyObject *args)
 
106
{
 
107
    uid_t uid;
 
108
    struct passwd *p;
 
109
    if (!PyArg_ParseTuple(args, "O&:getpwuid", _Py_Uid_Converter, &uid)) {
 
110
        if (PyErr_ExceptionMatches(PyExc_OverflowError))
 
111
            PyErr_Format(PyExc_KeyError,
 
112
                         "getpwuid(): uid not found");
 
113
        return NULL;
 
114
    }
 
115
    if ((p = getpwuid(uid)) == NULL) {
 
116
        PyObject *uid_obj = _PyLong_FromUid(uid);
 
117
        if (uid_obj == NULL)
 
118
            return NULL;
 
119
        PyErr_Format(PyExc_KeyError,
 
120
                     "getpwuid(): uid not found: %S", uid_obj);
 
121
        Py_DECREF(uid_obj);
 
122
        return NULL;
 
123
    }
 
124
    return mkpwent(p);
 
125
}
 
126
 
 
127
PyDoc_STRVAR(pwd_getpwnam__doc__,
 
128
"getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
 
129
                    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
 
 
133
static PyObject *
 
134
pwd_getpwnam(PyObject *self, PyObject *args)
 
135
{
 
136
    char *name;
 
137
    struct passwd *p;
 
138
    PyObject *arg, *bytes, *retval = NULL;
 
139
 
 
140
    if (!PyArg_ParseTuple(args, "U:getpwnam", &arg))
 
141
        return NULL;
 
142
    if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
 
143
        return NULL;
 
144
    if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
 
145
        goto out;
 
146
    if ((p = getpwnam(name)) == NULL) {
 
147
        PyErr_Format(PyExc_KeyError,
 
148
                     "getpwnam(): name not found: %s", name);
 
149
        goto out;
 
150
    }
 
151
    retval = mkpwent(p);
 
152
out:
 
153
    Py_DECREF(bytes);
 
154
    return retval;
 
155
}
 
156
 
 
157
#ifdef HAVE_GETPWENT
 
158
PyDoc_STRVAR(pwd_getpwall__doc__,
 
159
"getpwall() -> list_of_entries\n\
 
160
Return a list of all available password database entries, \
 
161
in arbitrary order.\n\
 
162
See help(pwd) for more on password database entries.");
 
163
 
 
164
static PyObject *
 
165
pwd_getpwall(PyObject *self)
 
166
{
 
167
    PyObject *d;
 
168
    struct passwd *p;
 
169
    if ((d = PyList_New(0)) == NULL)
 
170
        return NULL;
 
171
    setpwent();
 
172
    while ((p = getpwent()) != NULL) {
 
173
        PyObject *v = mkpwent(p);
 
174
        if (v == NULL || PyList_Append(d, v) != 0) {
 
175
            Py_XDECREF(v);
 
176
            Py_DECREF(d);
 
177
            endpwent();
 
178
            return NULL;
 
179
        }
 
180
        Py_DECREF(v);
 
181
    }
 
182
    endpwent();
 
183
    return d;
 
184
}
 
185
#endif
 
186
 
 
187
static PyMethodDef pwd_methods[] = {
 
188
    {"getpwuid",        pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
 
189
    {"getpwnam",        pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
 
190
#ifdef HAVE_GETPWENT
 
191
    {"getpwall",        (PyCFunction)pwd_getpwall,
 
192
        METH_NOARGS,  pwd_getpwall__doc__},
 
193
#endif
 
194
    {NULL,              NULL}           /* sentinel */
 
195
};
 
196
 
 
197
static struct PyModuleDef pwdmodule = {
 
198
    PyModuleDef_HEAD_INIT,
 
199
    "pwd",
 
200
    pwd__doc__,
 
201
    -1,
 
202
    pwd_methods,
 
203
    NULL,
 
204
    NULL,
 
205
    NULL,
 
206
    NULL
 
207
};
 
208
 
 
209
 
 
210
PyMODINIT_FUNC
 
211
PyInit_pwd(void)
 
212
{
 
213
    PyObject *m;
 
214
    m = PyModule_Create(&pwdmodule);
 
215
    if (m == NULL)
 
216
        return NULL;
 
217
 
 
218
    if (!initialized) {
 
219
        if (PyStructSequence_InitType2(&StructPwdType,
 
220
                                       &struct_pwd_type_desc) < 0)
 
221
            return NULL;
 
222
        initialized = 1;
 
223
    }
 
224
    Py_INCREF((PyObject *) &StructPwdType);
 
225
    PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
 
226
    return m;
 
227
}