You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
5.7 KiB

35 years ago
35 years ago
30 years ago
35 years ago
30 years ago
26 years ago
35 years ago
35 years ago
30 years ago
35 years ago
35 years ago
30 years ago
35 years ago
35 years ago
30 years ago
35 years ago
35 years ago
35 years ago
30 years ago
35 years ago
35 years ago
35 years ago
  1. /* UNIX password file access module */
  2. #include "Python.h"
  3. #include "posixmodule.h"
  4. #include <pwd.h>
  5. #include "clinic/pwdmodule.c.h"
  6. /*[clinic input]
  7. module pwd
  8. [clinic start generated code]*/
  9. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=60f628ef356b97b6]*/
  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}
  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. static PyStructSequence_Desc struct_pwd_type_desc = {
  26. "pwd.struct_passwd",
  27. struct_passwd__doc__,
  28. struct_pwd_type_fields,
  29. 7,
  30. };
  31. PyDoc_STRVAR(pwd__doc__,
  32. "This module provides access to the Unix password database.\n\
  33. It is available on all Unix versions.\n\
  34. \n\
  35. Password database entries are reported as 7-tuples containing the following\n\
  36. items from the password database (see `<pwd.h>'), in order:\n\
  37. pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
  38. The uid and gid items are integers, all others are strings. An\n\
  39. exception is raised if the entry asked for cannot be found.");
  40. static int initialized;
  41. static PyTypeObject StructPwdType;
  42. static void
  43. sets(PyObject *v, int i, const char* val)
  44. {
  45. if (val) {
  46. PyObject *o = PyUnicode_DecodeFSDefault(val);
  47. PyStructSequence_SET_ITEM(v, i, o);
  48. }
  49. else {
  50. PyStructSequence_SET_ITEM(v, i, Py_None);
  51. Py_INCREF(Py_None);
  52. }
  53. }
  54. static PyObject *
  55. mkpwent(struct passwd *p)
  56. {
  57. int setIndex = 0;
  58. PyObject *v = PyStructSequence_New(&StructPwdType);
  59. if (v == NULL)
  60. return NULL;
  61. #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
  62. #define SETS(i,val) sets(v, i, val)
  63. SETS(setIndex++, p->pw_name);
  64. SETS(setIndex++, p->pw_passwd);
  65. PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
  66. PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
  67. SETS(setIndex++, p->pw_gecos);
  68. SETS(setIndex++, p->pw_dir);
  69. SETS(setIndex++, p->pw_shell);
  70. #undef SETS
  71. #undef SETI
  72. if (PyErr_Occurred()) {
  73. Py_XDECREF(v);
  74. return NULL;
  75. }
  76. return v;
  77. }
  78. /*[clinic input]
  79. pwd.getpwuid
  80. uidobj: object
  81. /
  82. Return the password database entry for the given numeric user ID.
  83. See `help(pwd)` for more on password database entries.
  84. [clinic start generated code]*/
  85. static PyObject *
  86. pwd_getpwuid(PyModuleDef *module, PyObject *uidobj)
  87. /*[clinic end generated code: output=cba29ae4c2bcb8e1 input=ae64d507a1c6d3e8]*/
  88. {
  89. uid_t uid;
  90. struct passwd *p;
  91. if (!_Py_Uid_Converter(uidobj, &uid)) {
  92. if (PyErr_ExceptionMatches(PyExc_OverflowError))
  93. PyErr_Format(PyExc_KeyError,
  94. "getpwuid(): uid not found");
  95. return NULL;
  96. }
  97. if ((p = getpwuid(uid)) == NULL) {
  98. PyObject *uid_obj = _PyLong_FromUid(uid);
  99. if (uid_obj == NULL)
  100. return NULL;
  101. PyErr_Format(PyExc_KeyError,
  102. "getpwuid(): uid not found: %S", uid_obj);
  103. Py_DECREF(uid_obj);
  104. return NULL;
  105. }
  106. return mkpwent(p);
  107. }
  108. /*[clinic input]
  109. pwd.getpwnam
  110. arg: unicode
  111. /
  112. Return the password database entry for the given user name.
  113. See `help(pwd)` for more on password database entries.
  114. [clinic start generated code]*/
  115. static PyObject *
  116. pwd_getpwnam_impl(PyModuleDef *module, PyObject *arg)
  117. /*[clinic end generated code: output=66848d42d386fca3 input=d5f7e700919b02d3]*/
  118. {
  119. char *name;
  120. struct passwd *p;
  121. PyObject *bytes, *retval = NULL;
  122. if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
  123. return NULL;
  124. if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
  125. goto out;
  126. if ((p = getpwnam(name)) == NULL) {
  127. PyErr_Format(PyExc_KeyError,
  128. "getpwnam(): name not found: %s", name);
  129. goto out;
  130. }
  131. retval = mkpwent(p);
  132. out:
  133. Py_DECREF(bytes);
  134. return retval;
  135. }
  136. #ifdef HAVE_GETPWENT
  137. /*[clinic input]
  138. pwd.getpwall
  139. Return a list of all available password database entries, in arbitrary order.
  140. See help(pwd) for more on password database entries.
  141. [clinic start generated code]*/
  142. static PyObject *
  143. pwd_getpwall_impl(PyModuleDef *module)
  144. /*[clinic end generated code: output=ab30e37bf26d431d input=d7ecebfd90219b85]*/
  145. {
  146. PyObject *d;
  147. struct passwd *p;
  148. if ((d = PyList_New(0)) == NULL)
  149. return NULL;
  150. setpwent();
  151. while ((p = getpwent()) != NULL) {
  152. PyObject *v = mkpwent(p);
  153. if (v == NULL || PyList_Append(d, v) != 0) {
  154. Py_XDECREF(v);
  155. Py_DECREF(d);
  156. endpwent();
  157. return NULL;
  158. }
  159. Py_DECREF(v);
  160. }
  161. endpwent();
  162. return d;
  163. }
  164. #endif
  165. static PyMethodDef pwd_methods[] = {
  166. PWD_GETPWUID_METHODDEF
  167. PWD_GETPWNAM_METHODDEF
  168. #ifdef HAVE_GETPWENT
  169. PWD_GETPWALL_METHODDEF
  170. #endif
  171. {NULL, NULL} /* sentinel */
  172. };
  173. static struct PyModuleDef pwdmodule = {
  174. PyModuleDef_HEAD_INIT,
  175. "pwd",
  176. pwd__doc__,
  177. -1,
  178. pwd_methods,
  179. NULL,
  180. NULL,
  181. NULL,
  182. NULL
  183. };
  184. PyMODINIT_FUNC
  185. PyInit_pwd(void)
  186. {
  187. PyObject *m;
  188. m = PyModule_Create(&pwdmodule);
  189. if (m == NULL)
  190. return NULL;
  191. if (!initialized) {
  192. if (PyStructSequence_InitType2(&StructPwdType,
  193. &struct_pwd_type_desc) < 0)
  194. return NULL;
  195. initialized = 1;
  196. }
  197. Py_INCREF((PyObject *) &StructPwdType);
  198. PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
  199. return m;
  200. }