/Modules/spwdmodule.c

http://unladen-swallow.googlecode.com/ · C · 183 lines · 148 code · 32 blank · 3 comment · 20 complexity · 46f77cc8cd81a44bdecda6b7b9b6411a MD5 · raw file

  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 "structseq.h"
  6. #include <sys/types.h>
  7. #ifdef HAVE_SHADOW_H
  8. #include <shadow.h>
  9. #endif
  10. PyDoc_STRVAR(spwd__doc__,
  11. "This module provides access to the Unix shadow password database.\n\
  12. It is available on various Unix versions.\n\
  13. \n\
  14. Shadow password database entries are reported as 9-tuples of type struct_spwd,\n\
  15. containing the following items from the password database (see `<shadow.h>'):\n\
  16. sp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max, sp_warn, sp_inact, sp_expire, sp_flag.\n\
  17. The sp_namp and sp_pwdp are strings, the rest are integers.\n\
  18. An exception is raised if the entry asked for cannot be found.\n\
  19. You have to be root to be able to use this module.");
  20. #if defined(HAVE_GETSPNAM) || defined(HAVE_GETSPENT)
  21. static PyStructSequence_Field struct_spwd_type_fields[] = {
  22. {"sp_nam", "login name"},
  23. {"sp_pwd", "encrypted password"},
  24. {"sp_lstchg", "date of last change"},
  25. {"sp_min", "min #days between changes"},
  26. {"sp_max", "max #days between changes"},
  27. {"sp_warn", "#days before pw expires to warn user about it"},
  28. {"sp_inact", "#days after pw expires until account is blocked"},
  29. {"sp_expire", "#days since 1970-01-01 until account is disabled"},
  30. {"sp_flag", "reserved"},
  31. {0}
  32. };
  33. PyDoc_STRVAR(struct_spwd__doc__,
  34. "spwd.struct_spwd: Results from getsp*() routines.\n\n\
  35. This object may be accessed either as a 9-tuple of\n\
  36. (sp_nam,sp_pwd,sp_lstchg,sp_min,sp_max,sp_warn,sp_inact,sp_expire,sp_flag)\n\
  37. or via the object attributes as named in the above tuple.");
  38. static PyStructSequence_Desc struct_spwd_type_desc = {
  39. "spwd.struct_spwd",
  40. struct_spwd__doc__,
  41. struct_spwd_type_fields,
  42. 9,
  43. };
  44. static int initialized;
  45. static PyTypeObject StructSpwdType;
  46. static void
  47. sets(PyObject *v, int i, char* val)
  48. {
  49. if (val)
  50. PyStructSequence_SET_ITEM(v, i, PyString_FromString(val));
  51. else {
  52. PyStructSequence_SET_ITEM(v, i, Py_None);
  53. Py_INCREF(Py_None);
  54. }
  55. }
  56. static PyObject *mkspent(struct spwd *p)
  57. {
  58. int setIndex = 0;
  59. PyObject *v = PyStructSequence_New(&StructSpwdType);
  60. if (v == NULL)
  61. return NULL;
  62. #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
  63. #define SETS(i,val) sets(v, i, val)
  64. SETS(setIndex++, p->sp_namp);
  65. SETS(setIndex++, p->sp_pwdp);
  66. SETI(setIndex++, p->sp_lstchg);
  67. SETI(setIndex++, p->sp_min);
  68. SETI(setIndex++, p->sp_max);
  69. SETI(setIndex++, p->sp_warn);
  70. SETI(setIndex++, p->sp_inact);
  71. SETI(setIndex++, p->sp_expire);
  72. SETI(setIndex++, p->sp_flag);
  73. #undef SETS
  74. #undef SETI
  75. if (PyErr_Occurred()) {
  76. Py_DECREF(v);
  77. return NULL;
  78. }
  79. return v;
  80. }
  81. #endif /* HAVE_GETSPNAM || HAVE_GETSPENT */
  82. #ifdef HAVE_GETSPNAM
  83. PyDoc_STRVAR(spwd_getspnam__doc__,
  84. "getspnam(name) -> (sp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max,\n\
  85. sp_warn, sp_inact, sp_expire, sp_flag)\n\
  86. Return the shadow password database entry for the given user name.\n\
  87. See spwd.__doc__ for more on shadow password database entries.");
  88. static PyObject* spwd_getspnam(PyObject *self, PyObject *args)
  89. {
  90. char *name;
  91. struct spwd *p;
  92. if (!PyArg_ParseTuple(args, "s:getspnam", &name))
  93. return NULL;
  94. if ((p = getspnam(name)) == NULL) {
  95. PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
  96. return NULL;
  97. }
  98. return mkspent(p);
  99. }
  100. #endif /* HAVE_GETSPNAM */
  101. #ifdef HAVE_GETSPENT
  102. PyDoc_STRVAR(spwd_getspall__doc__,
  103. "getspall() -> list_of_entries\n\
  104. Return a list of all available shadow password database entries, \
  105. in arbitrary order.\n\
  106. See spwd.__doc__ for more on shadow password database entries.");
  107. static PyObject *
  108. spwd_getspall(PyObject *self, PyObject *args)
  109. {
  110. PyObject *d;
  111. struct spwd *p;
  112. if ((d = PyList_New(0)) == NULL)
  113. return NULL;
  114. setspent();
  115. while ((p = getspent()) != NULL) {
  116. PyObject *v = mkspent(p);
  117. if (v == NULL || PyList_Append(d, v) != 0) {
  118. Py_XDECREF(v);
  119. Py_DECREF(d);
  120. endspent();
  121. return NULL;
  122. }
  123. Py_DECREF(v);
  124. }
  125. endspent();
  126. return d;
  127. }
  128. #endif /* HAVE_GETSPENT */
  129. static PyMethodDef spwd_methods[] = {
  130. #ifdef HAVE_GETSPNAM
  131. {"getspnam", spwd_getspnam, METH_VARARGS, spwd_getspnam__doc__},
  132. #endif
  133. #ifdef HAVE_GETSPENT
  134. {"getspall", spwd_getspall, METH_NOARGS, spwd_getspall__doc__},
  135. #endif
  136. {NULL, NULL} /* sentinel */
  137. };
  138. PyMODINIT_FUNC
  139. initspwd(void)
  140. {
  141. PyObject *m;
  142. m=Py_InitModule3("spwd", spwd_methods, spwd__doc__);
  143. if (m == NULL)
  144. return;
  145. if (!initialized)
  146. PyStructSequence_InitType(&StructSpwdType,
  147. &struct_spwd_type_desc);
  148. Py_INCREF((PyObject *) &StructSpwdType);
  149. PyModule_AddObject(m, "struct_spwd", (PyObject *) &StructSpwdType);
  150. initialized = 1;
  151. }