spwdmodule.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* UNIX shadow password file access module */
  2. /* A lot of code has been taken from pwdmodule.c */
  3. /* For info also see http://www.unixpapa.com/incnote/passwd.html */
  4. #include "Python.h"
  5. #include <sys/types.h>
  6. #ifdef HAVE_SHADOW_H
  7. #include <shadow.h>
  8. #endif
  9. #include "clinic/spwdmodule.c.h"
  10. /*[clinic input]
  11. module spwd
  12. [clinic start generated code]*/
  13. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c0b841b90a6a07ce]*/
  14. PyDoc_STRVAR(spwd__doc__,
  15. "This module provides access to the Unix shadow password database.\n\
  16. It is available on various Unix versions.\n\
  17. \n\
  18. Shadow password database entries are reported as 9-tuples of type struct_spwd,\n\
  19. containing the following items from the password database (see `<shadow.h>'):\n\
  20. sp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max, sp_warn, sp_inact, sp_expire, sp_flag.\n\
  21. The sp_namp and sp_pwdp are strings, the rest are integers.\n\
  22. An exception is raised if the entry asked for cannot be found.\n\
  23. You have to be root to be able to use this module.");
  24. #if defined(HAVE_GETSPNAM) || defined(HAVE_GETSPENT)
  25. static PyStructSequence_Field struct_spwd_type_fields[] = {
  26. {"sp_namp", "login name"},
  27. {"sp_pwdp", "encrypted password"},
  28. {"sp_lstchg", "date of last change"},
  29. {"sp_min", "min #days between changes"},
  30. {"sp_max", "max #days between changes"},
  31. {"sp_warn", "#days before pw expires to warn user about it"},
  32. {"sp_inact", "#days after pw expires until account is disabled"},
  33. {"sp_expire", "#days since 1970-01-01 when account expires"},
  34. {"sp_flag", "reserved"},
  35. {"sp_nam", "login name; deprecated"}, /* Backward compatibility */
  36. {"sp_pwd", "encrypted password; deprecated"}, /* Backward compatibility */
  37. {0}
  38. };
  39. PyDoc_STRVAR(struct_spwd__doc__,
  40. "spwd.struct_spwd: Results from getsp*() routines.\n\n\
  41. This object may be accessed either as a 9-tuple of\n\
  42. (sp_namp,sp_pwdp,sp_lstchg,sp_min,sp_max,sp_warn,sp_inact,sp_expire,sp_flag)\n\
  43. or via the object attributes as named in the above tuple.");
  44. static PyStructSequence_Desc struct_spwd_type_desc = {
  45. "spwd.struct_spwd",
  46. struct_spwd__doc__,
  47. struct_spwd_type_fields,
  48. 9,
  49. };
  50. typedef struct {
  51. PyTypeObject *StructSpwdType;
  52. } spwdmodulestate;
  53. static inline spwdmodulestate*
  54. get_spwd_state(PyObject *module)
  55. {
  56. void *state = PyModule_GetState(module);
  57. assert(state != NULL);
  58. return (spwdmodulestate *)state;
  59. }
  60. static struct PyModuleDef spwdmodule;
  61. static void
  62. sets(PyObject *v, int i, const char* val)
  63. {
  64. if (val) {
  65. PyObject *o = PyUnicode_DecodeFSDefault(val);
  66. PyStructSequence_SET_ITEM(v, i, o);
  67. } else {
  68. PyStructSequence_SET_ITEM(v, i, Py_None);
  69. Py_INCREF(Py_None);
  70. }
  71. }
  72. static PyObject *mkspent(PyObject *module, struct spwd *p)
  73. {
  74. int setIndex = 0;
  75. PyObject *v = PyStructSequence_New(get_spwd_state(module)->StructSpwdType);
  76. if (v == NULL)
  77. return NULL;
  78. #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
  79. #define SETS(i,val) sets(v, i, val)
  80. SETS(setIndex++, p->sp_namp);
  81. SETS(setIndex++, p->sp_pwdp);
  82. SETI(setIndex++, p->sp_lstchg);
  83. SETI(setIndex++, p->sp_min);
  84. SETI(setIndex++, p->sp_max);
  85. SETI(setIndex++, p->sp_warn);
  86. SETI(setIndex++, p->sp_inact);
  87. SETI(setIndex++, p->sp_expire);
  88. SETI(setIndex++, p->sp_flag);
  89. SETS(setIndex++, p->sp_namp); /* Backward compatibility for sp_nam */
  90. SETS(setIndex++, p->sp_pwdp); /* Backward compatibility for sp_pwd */
  91. #undef SETS
  92. #undef SETI
  93. if (PyErr_Occurred()) {
  94. Py_DECREF(v);
  95. return NULL;
  96. }
  97. return v;
  98. }
  99. #endif /* HAVE_GETSPNAM || HAVE_GETSPENT */
  100. #ifdef HAVE_GETSPNAM
  101. /*[clinic input]
  102. spwd.getspnam
  103. arg: unicode
  104. /
  105. Return the shadow password database entry for the given user name.
  106. See `help(spwd)` for more on shadow password database entries.
  107. [clinic start generated code]*/
  108. static PyObject *
  109. spwd_getspnam_impl(PyObject *module, PyObject *arg)
  110. /*[clinic end generated code: output=701250cf57dc6ebe input=dd89429e6167a00f]*/
  111. {
  112. char *name;
  113. struct spwd *p;
  114. PyObject *bytes, *retval = NULL;
  115. if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
  116. return NULL;
  117. /* check for embedded null bytes */
  118. if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
  119. goto out;
  120. if ((p = getspnam(name)) == NULL) {
  121. if (errno != 0)
  122. PyErr_SetFromErrno(PyExc_OSError);
  123. else
  124. PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
  125. goto out;
  126. }
  127. retval = mkspent(module, p);
  128. out:
  129. Py_DECREF(bytes);
  130. return retval;
  131. }
  132. #endif /* HAVE_GETSPNAM */
  133. #ifdef HAVE_GETSPENT
  134. /*[clinic input]
  135. spwd.getspall
  136. Return a list of all available shadow password database entries, in arbitrary order.
  137. See `help(spwd)` for more on shadow password database entries.
  138. [clinic start generated code]*/
  139. static PyObject *
  140. spwd_getspall_impl(PyObject *module)
  141. /*[clinic end generated code: output=4fda298d6bf6d057 input=b2c84b7857d622bd]*/
  142. {
  143. PyObject *d;
  144. struct spwd *p;
  145. if ((d = PyList_New(0)) == NULL)
  146. return NULL;
  147. setspent();
  148. while ((p = getspent()) != NULL) {
  149. PyObject *v = mkspent(module, p);
  150. if (v == NULL || PyList_Append(d, v) != 0) {
  151. Py_XDECREF(v);
  152. Py_DECREF(d);
  153. endspent();
  154. return NULL;
  155. }
  156. Py_DECREF(v);
  157. }
  158. endspent();
  159. return d;
  160. }
  161. #endif /* HAVE_GETSPENT */
  162. static PyMethodDef spwd_methods[] = {
  163. #ifdef HAVE_GETSPNAM
  164. SPWD_GETSPNAM_METHODDEF
  165. #endif
  166. #ifdef HAVE_GETSPENT
  167. SPWD_GETSPALL_METHODDEF
  168. #endif
  169. {NULL, NULL} /* sentinel */
  170. };
  171. static int
  172. spwdmodule_exec(PyObject *module)
  173. {
  174. spwdmodulestate *state = get_spwd_state(module);
  175. state->StructSpwdType = PyStructSequence_NewType(&struct_spwd_type_desc);
  176. if (state->StructSpwdType == NULL) {
  177. return -1;
  178. }
  179. if (PyModule_AddType(module, state->StructSpwdType) < 0) {
  180. return -1;
  181. }
  182. return 0;
  183. }
  184. static PyModuleDef_Slot spwdmodule_slots[] = {
  185. {Py_mod_exec, spwdmodule_exec},
  186. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  187. {0, NULL}
  188. };
  189. static int spwdmodule_traverse(PyObject *m, visitproc visit, void *arg) {
  190. Py_VISIT(get_spwd_state(m)->StructSpwdType);
  191. return 0;
  192. }
  193. static int spwdmodule_clear(PyObject *m) {
  194. Py_CLEAR(get_spwd_state(m)->StructSpwdType);
  195. return 0;
  196. }
  197. static void spwdmodule_free(void *m) {
  198. spwdmodule_clear((PyObject *)m);
  199. }
  200. static struct PyModuleDef spwdmodule = {
  201. PyModuleDef_HEAD_INIT,
  202. .m_name = "spwd",
  203. .m_doc = spwd__doc__,
  204. .m_size = sizeof(spwdmodulestate),
  205. .m_methods = spwd_methods,
  206. .m_slots = spwdmodule_slots,
  207. .m_traverse = spwdmodule_traverse,
  208. .m_clear = spwdmodule_clear,
  209. .m_free = spwdmodule_free,
  210. };
  211. PyMODINIT_FUNC
  212. PyInit_spwd(void)
  213. {
  214. if (PyErr_WarnEx(PyExc_DeprecationWarning,
  215. "'spwd' is deprecated and slated for removal in "
  216. "Python 3.13",
  217. 7)) {
  218. return NULL;
  219. }
  220. return PyModuleDef_Init(&spwdmodule);
  221. }