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.

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