Blocked revisions 73580-73582 via svnmerge
[python/dscho.git] / Modules / pwdmodule.c
blob1547cdf27aabb613221ea8141a5898a09772b951
2 /* UNIX password file access module */
4 #include "Python.h"
5 #include "structseq.h"
7 #include <sys/types.h>
8 #include <pwd.h>
10 static PyStructSequence_Field struct_pwd_type_fields[] = {
11 {"pw_name", "user name"},
12 {"pw_passwd", "password"},
13 {"pw_uid", "user id"},
14 {"pw_gid", "group id"},
15 {"pw_gecos", "real name"},
16 {"pw_dir", "home directory"},
17 {"pw_shell", "shell program"},
18 {0}
21 PyDoc_STRVAR(struct_passwd__doc__,
22 "pwd.struct_passwd: Results from getpw*() routines.\n\n\
23 This object may be accessed either as a tuple of\n\
24 (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
25 or via the object attributes as named in the above tuple.");
27 static PyStructSequence_Desc struct_pwd_type_desc = {
28 "pwd.struct_passwd",
29 struct_passwd__doc__,
30 struct_pwd_type_fields,
34 PyDoc_STRVAR(pwd__doc__,
35 "This module provides access to the Unix password database.\n\
36 It is available on all Unix versions.\n\
37 \n\
38 Password database entries are reported as 7-tuples containing the following\n\
39 items from the password database (see `<pwd.h>'), in order:\n\
40 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
41 The uid and gid items are integers, all others are strings. An\n\
42 exception is raised if the entry asked for cannot be found.");
45 static int initialized;
46 static PyTypeObject StructPwdType;
48 static void
49 sets(PyObject *v, int i, const char* val)
51 if (val) {
52 PyObject *o = PyUnicode_Decode(val, strlen(val),
53 Py_FileSystemDefaultEncoding,
54 "surrogateescape");
55 PyStructSequence_SET_ITEM(v, i, o);
57 else {
58 PyStructSequence_SET_ITEM(v, i, Py_None);
59 Py_INCREF(Py_None);
63 static PyObject *
64 mkpwent(struct passwd *p)
66 int setIndex = 0;
67 PyObject *v = PyStructSequence_New(&StructPwdType);
68 if (v == NULL)
69 return NULL;
71 #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
72 #define SETS(i,val) sets(v, i, val)
74 SETS(setIndex++, p->pw_name);
75 #ifdef __VMS
76 SETS(setIndex++, "");
77 #else
78 SETS(setIndex++, p->pw_passwd);
79 #endif
80 SETI(setIndex++, p->pw_uid);
81 SETI(setIndex++, p->pw_gid);
82 #ifdef __VMS
83 SETS(setIndex++, "");
84 #else
85 SETS(setIndex++, p->pw_gecos);
86 #endif
87 SETS(setIndex++, p->pw_dir);
88 SETS(setIndex++, p->pw_shell);
90 #undef SETS
91 #undef SETI
93 if (PyErr_Occurred()) {
94 Py_XDECREF(v);
95 return NULL;
98 return v;
101 PyDoc_STRVAR(pwd_getpwuid__doc__,
102 "getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
103 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
104 Return the password database entry for the given numeric user ID.\n\
105 See pwd.__doc__ for more on password database entries.");
107 static PyObject *
108 pwd_getpwuid(PyObject *self, PyObject *args)
110 unsigned int uid;
111 struct passwd *p;
112 if (!PyArg_ParseTuple(args, "I:getpwuid", &uid))
113 return NULL;
114 if ((p = getpwuid(uid)) == NULL) {
115 PyErr_Format(PyExc_KeyError,
116 "getpwuid(): uid not found: %d", uid);
117 return NULL;
119 return mkpwent(p);
122 PyDoc_STRVAR(pwd_getpwnam__doc__,
123 "getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
124 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
125 Return the password database entry for the given user name.\n\
126 See pwd.__doc__ for more on password database entries.");
128 static PyObject *
129 pwd_getpwnam(PyObject *self, PyObject *args)
131 char *name;
132 struct passwd *p;
133 PyObject *arg, *bytes, *retval = NULL;
135 if (!PyArg_ParseTuple(args, "U:getpwnam", &arg))
136 return NULL;
137 if ((bytes = PyUnicode_AsEncodedString(arg,
138 Py_FileSystemDefaultEncoding,
139 "surrogateescape")) == NULL)
140 return NULL;
141 if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
142 goto out;
143 if ((p = getpwnam(name)) == NULL) {
144 PyErr_Format(PyExc_KeyError,
145 "getpwnam(): name not found: %s", name);
146 goto out;
148 retval = mkpwent(p);
149 out:
150 Py_DECREF(bytes);
151 return retval;
154 #ifdef HAVE_GETPWENT
155 PyDoc_STRVAR(pwd_getpwall__doc__,
156 "getpwall() -> list_of_entries\n\
157 Return a list of all available password database entries, \
158 in arbitrary order.\n\
159 See pwd.__doc__ for more on password database entries.");
161 static PyObject *
162 pwd_getpwall(PyObject *self)
164 PyObject *d;
165 struct passwd *p;
166 if ((d = PyList_New(0)) == NULL)
167 return NULL;
168 #if defined(PYOS_OS2) && defined(PYCC_GCC)
169 if ((p = getpwuid(0)) != NULL) {
170 #else
171 setpwent();
172 while ((p = getpwent()) != NULL) {
173 #endif
174 PyObject *v = mkpwent(p);
175 if (v == NULL || PyList_Append(d, v) != 0) {
176 Py_XDECREF(v);
177 Py_DECREF(d);
178 endpwent();
179 return NULL;
181 Py_DECREF(v);
183 endpwent();
184 return d;
186 #endif
188 static PyMethodDef pwd_methods[] = {
189 {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
190 {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
191 #ifdef HAVE_GETPWENT
192 {"getpwall", (PyCFunction)pwd_getpwall,
193 METH_NOARGS, pwd_getpwall__doc__},
194 #endif
195 {NULL, NULL} /* sentinel */
198 static struct PyModuleDef pwdmodule = {
199 PyModuleDef_HEAD_INIT,
200 "pwd",
201 pwd__doc__,
203 pwd_methods,
204 NULL,
205 NULL,
206 NULL,
207 NULL
211 PyMODINIT_FUNC
212 PyInit_pwd(void)
214 PyObject *m;
215 m = PyModule_Create(&pwdmodule);
216 if (m == NULL)
217 return NULL;
219 if (!initialized) {
220 PyStructSequence_InitType(&StructPwdType,
221 &struct_pwd_type_desc);
222 initialized = 1;
224 Py_INCREF((PyObject *) &StructPwdType);
225 PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
226 return m;