]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | \r |
2 | /* UNIX password file access module */\r | |
3 | \r | |
4 | #include "Python.h"\r | |
5 | #include "structseq.h"\r | |
6 | \r | |
7 | #include <sys/types.h>\r | |
8 | #include <pwd.h>\r | |
9 | \r | |
10 | static PyStructSequence_Field struct_pwd_type_fields[] = {\r | |
11 | {"pw_name", "user name"},\r | |
12 | {"pw_passwd", "password"},\r | |
13 | {"pw_uid", "user id"},\r | |
14 | {"pw_gid", "group id"},\r | |
15 | {"pw_gecos", "real name"},\r | |
16 | {"pw_dir", "home directory"},\r | |
17 | {"pw_shell", "shell program"},\r | |
18 | {0}\r | |
19 | };\r | |
20 | \r | |
21 | PyDoc_STRVAR(struct_passwd__doc__,\r | |
22 | "pwd.struct_passwd: Results from getpw*() routines.\n\n\\r | |
23 | This object may be accessed either as a tuple of\n\\r | |
24 | (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\\r | |
25 | or via the object attributes as named in the above tuple.");\r | |
26 | \r | |
27 | static PyStructSequence_Desc struct_pwd_type_desc = {\r | |
28 | "pwd.struct_passwd",\r | |
29 | struct_passwd__doc__,\r | |
30 | struct_pwd_type_fields,\r | |
31 | 7,\r | |
32 | };\r | |
33 | \r | |
34 | PyDoc_STRVAR(pwd__doc__,\r | |
35 | "This module provides access to the Unix password database.\n\\r | |
36 | It is available on all Unix versions.\n\\r | |
37 | \n\\r | |
38 | Password database entries are reported as 7-tuples containing the following\n\\r | |
39 | items from the password database (see `<pwd.h>'), in order:\n\\r | |
40 | pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\\r | |
41 | The uid and gid items are integers, all others are strings. An\n\\r | |
42 | exception is raised if the entry asked for cannot be found.");\r | |
43 | \r | |
44 | \r | |
45 | static int initialized;\r | |
46 | static PyTypeObject StructPwdType;\r | |
47 | \r | |
48 | static void\r | |
49 | sets(PyObject *v, int i, char* val)\r | |
50 | {\r | |
51 | if (val)\r | |
52 | PyStructSequence_SET_ITEM(v, i, PyString_FromString(val));\r | |
53 | else {\r | |
54 | PyStructSequence_SET_ITEM(v, i, Py_None);\r | |
55 | Py_INCREF(Py_None);\r | |
56 | }\r | |
57 | }\r | |
58 | \r | |
59 | static PyObject *\r | |
60 | mkpwent(struct passwd *p)\r | |
61 | {\r | |
62 | int setIndex = 0;\r | |
63 | PyObject *v = PyStructSequence_New(&StructPwdType);\r | |
64 | if (v == NULL)\r | |
65 | return NULL;\r | |
66 | \r | |
67 | #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))\r | |
68 | #define SETS(i,val) sets(v, i, val)\r | |
69 | \r | |
70 | SETS(setIndex++, p->pw_name);\r | |
71 | #ifdef __VMS\r | |
72 | SETS(setIndex++, "");\r | |
73 | #else\r | |
74 | SETS(setIndex++, p->pw_passwd);\r | |
75 | #endif\r | |
76 | SETI(setIndex++, p->pw_uid);\r | |
77 | SETI(setIndex++, p->pw_gid);\r | |
78 | #ifdef __VMS\r | |
79 | SETS(setIndex++, "");\r | |
80 | #else\r | |
81 | SETS(setIndex++, p->pw_gecos);\r | |
82 | #endif\r | |
83 | SETS(setIndex++, p->pw_dir);\r | |
84 | SETS(setIndex++, p->pw_shell);\r | |
85 | \r | |
86 | #undef SETS\r | |
87 | #undef SETI\r | |
88 | \r | |
89 | if (PyErr_Occurred()) {\r | |
90 | Py_XDECREF(v);\r | |
91 | return NULL;\r | |
92 | }\r | |
93 | \r | |
94 | return v;\r | |
95 | }\r | |
96 | \r | |
97 | PyDoc_STRVAR(pwd_getpwuid__doc__,\r | |
98 | "getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\\r | |
99 | pw_gid,pw_gecos,pw_dir,pw_shell)\n\\r | |
100 | Return the password database entry for the given numeric user ID.\n\\r | |
101 | See help(pwd) for more on password database entries.");\r | |
102 | \r | |
103 | static PyObject *\r | |
104 | pwd_getpwuid(PyObject *self, PyObject *args)\r | |
105 | {\r | |
106 | unsigned int uid;\r | |
107 | struct passwd *p;\r | |
108 | if (!PyArg_ParseTuple(args, "I:getpwuid", &uid))\r | |
109 | return NULL;\r | |
110 | if ((p = getpwuid(uid)) == NULL) {\r | |
111 | PyErr_Format(PyExc_KeyError,\r | |
112 | "getpwuid(): uid not found: %d", uid);\r | |
113 | return NULL;\r | |
114 | }\r | |
115 | return mkpwent(p);\r | |
116 | }\r | |
117 | \r | |
118 | PyDoc_STRVAR(pwd_getpwnam__doc__,\r | |
119 | "getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\\r | |
120 | pw_gid,pw_gecos,pw_dir,pw_shell)\n\\r | |
121 | Return the password database entry for the given user name.\n\\r | |
122 | See help(pwd) for more on password database entries.");\r | |
123 | \r | |
124 | static PyObject *\r | |
125 | pwd_getpwnam(PyObject *self, PyObject *args)\r | |
126 | {\r | |
127 | char *name;\r | |
128 | struct passwd *p;\r | |
129 | if (!PyArg_ParseTuple(args, "s:getpwnam", &name))\r | |
130 | return NULL;\r | |
131 | if ((p = getpwnam(name)) == NULL) {\r | |
132 | PyErr_Format(PyExc_KeyError,\r | |
133 | "getpwnam(): name not found: %s", name);\r | |
134 | return NULL;\r | |
135 | }\r | |
136 | return mkpwent(p);\r | |
137 | }\r | |
138 | \r | |
139 | #ifdef HAVE_GETPWENT\r | |
140 | PyDoc_STRVAR(pwd_getpwall__doc__,\r | |
141 | "getpwall() -> list_of_entries\n\\r | |
142 | Return a list of all available password database entries, \\r | |
143 | in arbitrary order.\n\\r | |
144 | See help(pwd) for more on password database entries.");\r | |
145 | \r | |
146 | static PyObject *\r | |
147 | pwd_getpwall(PyObject *self)\r | |
148 | {\r | |
149 | PyObject *d;\r | |
150 | struct passwd *p;\r | |
151 | if ((d = PyList_New(0)) == NULL)\r | |
152 | return NULL;\r | |
153 | #if defined(PYOS_OS2) && defined(PYCC_GCC)\r | |
154 | if ((p = getpwuid(0)) != NULL) {\r | |
155 | #else\r | |
156 | setpwent();\r | |
157 | while ((p = getpwent()) != NULL) {\r | |
158 | #endif\r | |
159 | PyObject *v = mkpwent(p);\r | |
160 | if (v == NULL || PyList_Append(d, v) != 0) {\r | |
161 | Py_XDECREF(v);\r | |
162 | Py_DECREF(d);\r | |
163 | endpwent();\r | |
164 | return NULL;\r | |
165 | }\r | |
166 | Py_DECREF(v);\r | |
167 | }\r | |
168 | endpwent();\r | |
169 | return d;\r | |
170 | }\r | |
171 | #endif\r | |
172 | \r | |
173 | static PyMethodDef pwd_methods[] = {\r | |
174 | {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},\r | |
175 | {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},\r | |
176 | #ifdef HAVE_GETPWENT\r | |
177 | {"getpwall", (PyCFunction)pwd_getpwall,\r | |
178 | METH_NOARGS, pwd_getpwall__doc__},\r | |
179 | #endif\r | |
180 | {NULL, NULL} /* sentinel */\r | |
181 | };\r | |
182 | \r | |
183 | PyMODINIT_FUNC\r | |
184 | initpwd(void)\r | |
185 | {\r | |
186 | PyObject *m;\r | |
187 | m = Py_InitModule3("pwd", pwd_methods, pwd__doc__);\r | |
188 | if (m == NULL)\r | |
189 | return;\r | |
190 | \r | |
191 | if (!initialized)\r | |
192 | PyStructSequence_InitType(&StructPwdType,\r | |
193 | &struct_pwd_type_desc);\r | |
194 | Py_INCREF((PyObject *) &StructPwdType);\r | |
195 | PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);\r | |
196 | /* And for b/w compatibility (this was defined by mistake): */\r | |
197 | Py_INCREF((PyObject *) &StructPwdType);\r | |
198 | PyModule_AddObject(m, "struct_pwent", (PyObject *) &StructPwdType);\r | |
199 | initialized = 1;\r | |
200 | }\r |