]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Modules/pwdmodule.c
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Modules / pwdmodule.c
CommitLineData
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
10static 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
21PyDoc_STRVAR(struct_passwd__doc__,\r
22"pwd.struct_passwd: Results from getpw*() routines.\n\n\\r
23This 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
25or via the object attributes as named in the above tuple.");\r
26\r
27static 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
34PyDoc_STRVAR(pwd__doc__,\r
35"This module provides access to the Unix password database.\n\\r
36It is available on all Unix versions.\n\\r
37\n\\r
38Password database entries are reported as 7-tuples containing the following\n\\r
39items from the password database (see `<pwd.h>'), in order:\n\\r
40pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\\r
41The uid and gid items are integers, all others are strings. An\n\\r
42exception is raised if the entry asked for cannot be found.");\r
43\r
44\r
45static int initialized;\r
46static PyTypeObject StructPwdType;\r
47\r
48static void\r
49sets(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
59static PyObject *\r
60mkpwent(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
97PyDoc_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
100Return the password database entry for the given numeric user ID.\n\\r
101See help(pwd) for more on password database entries.");\r
102\r
103static PyObject *\r
104pwd_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
118PyDoc_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
121Return the password database entry for the given user name.\n\\r
122See help(pwd) for more on password database entries.");\r
123\r
124static PyObject *\r
125pwd_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
140PyDoc_STRVAR(pwd_getpwall__doc__,\r
141"getpwall() -> list_of_entries\n\\r
142Return a list of all available password database entries, \\r
143in arbitrary order.\n\\r
144See help(pwd) for more on password database entries.");\r
145\r
146static PyObject *\r
147pwd_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
173static 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
183PyMODINIT_FUNC\r
184initpwd(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