pwdmodule.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. typedef struct {
  41. PyTypeObject *StructPwdType;
  42. } pwdmodulestate;
  43. static inline pwdmodulestate*
  44. get_pwd_state(PyObject *module)
  45. {
  46. void *state = PyModule_GetState(module);
  47. assert(state != NULL);
  48. return (pwdmodulestate *)state;
  49. }
  50. static struct PyModuleDef pwdmodule;
  51. #define DEFAULT_BUFFER_SIZE 1024
  52. static PyObject *
  53. mkpwent(PyObject *module, struct passwd *p)
  54. {
  55. PyObject *v = PyStructSequence_New(get_pwd_state(module)->StructPwdType);
  56. if (v == NULL) {
  57. return NULL;
  58. }
  59. int setIndex = 0;
  60. #define SET_STRING(VAL) \
  61. SET_RESULT((VAL) ? PyUnicode_DecodeFSDefault((VAL)) : Py_NewRef(Py_None))
  62. #define SET_RESULT(CALL) \
  63. do { \
  64. PyObject *item = (CALL); \
  65. if (item == NULL) { \
  66. goto error; \
  67. } \
  68. PyStructSequence_SET_ITEM(v, setIndex++, item); \
  69. } while(0)
  70. SET_STRING(p->pw_name);
  71. #if defined(HAVE_STRUCT_PASSWD_PW_PASSWD) && !defined(__ANDROID__)
  72. SET_STRING(p->pw_passwd);
  73. #else
  74. SET_STRING("");
  75. #endif
  76. SET_RESULT(_PyLong_FromUid(p->pw_uid));
  77. SET_RESULT(_PyLong_FromGid(p->pw_gid));
  78. #if defined(HAVE_STRUCT_PASSWD_PW_GECOS)
  79. SET_STRING(p->pw_gecos);
  80. #else
  81. SET_STRING("");
  82. #endif
  83. SET_STRING(p->pw_dir);
  84. SET_STRING(p->pw_shell);
  85. #undef SET_STRING
  86. #undef SET_RESULT
  87. return v;
  88. error:
  89. Py_DECREF(v);
  90. return NULL;
  91. }
  92. /*[clinic input]
  93. pwd.getpwuid
  94. uidobj: object
  95. /
  96. Return the password database entry for the given numeric user ID.
  97. See `help(pwd)` for more on password database entries.
  98. [clinic start generated code]*/
  99. static PyObject *
  100. pwd_getpwuid(PyObject *module, PyObject *uidobj)
  101. /*[clinic end generated code: output=c4ee1d4d429b86c4 input=ae64d507a1c6d3e8]*/
  102. {
  103. PyObject *retval = NULL;
  104. uid_t uid;
  105. int nomem = 0;
  106. struct passwd *p;
  107. char *buf = NULL, *buf2 = NULL;
  108. if (!_Py_Uid_Converter(uidobj, &uid)) {
  109. if (PyErr_ExceptionMatches(PyExc_OverflowError))
  110. PyErr_Format(PyExc_KeyError,
  111. "getpwuid(): uid not found");
  112. return NULL;
  113. }
  114. #ifdef HAVE_GETPWUID_R
  115. int status;
  116. Py_ssize_t bufsize;
  117. /* Note: 'pwd' will be used via pointer 'p' on getpwuid_r success. */
  118. struct passwd pwd;
  119. Py_BEGIN_ALLOW_THREADS
  120. bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  121. if (bufsize == -1) {
  122. bufsize = DEFAULT_BUFFER_SIZE;
  123. }
  124. while(1) {
  125. buf2 = PyMem_RawRealloc(buf, bufsize);
  126. if (buf2 == NULL) {
  127. p = NULL;
  128. nomem = 1;
  129. break;
  130. }
  131. buf = buf2;
  132. status = getpwuid_r(uid, &pwd, buf, bufsize, &p);
  133. if (status != 0) {
  134. p = NULL;
  135. }
  136. if (p != NULL || status != ERANGE) {
  137. break;
  138. }
  139. if (bufsize > (PY_SSIZE_T_MAX >> 1)) {
  140. nomem = 1;
  141. break;
  142. }
  143. bufsize <<= 1;
  144. }
  145. Py_END_ALLOW_THREADS
  146. #else
  147. p = getpwuid(uid);
  148. #endif
  149. if (p == NULL) {
  150. PyMem_RawFree(buf);
  151. if (nomem == 1) {
  152. return PyErr_NoMemory();
  153. }
  154. PyObject *uid_obj = _PyLong_FromUid(uid);
  155. if (uid_obj == NULL)
  156. return NULL;
  157. PyErr_Format(PyExc_KeyError,
  158. "getpwuid(): uid not found: %S", uid_obj);
  159. Py_DECREF(uid_obj);
  160. return NULL;
  161. }
  162. retval = mkpwent(module, p);
  163. #ifdef HAVE_GETPWUID_R
  164. PyMem_RawFree(buf);
  165. #endif
  166. return retval;
  167. }
  168. /*[clinic input]
  169. pwd.getpwnam
  170. name: unicode
  171. /
  172. Return the password database entry for the given user name.
  173. See `help(pwd)` for more on password database entries.
  174. [clinic start generated code]*/
  175. static PyObject *
  176. pwd_getpwnam_impl(PyObject *module, PyObject *name)
  177. /*[clinic end generated code: output=359ce1ddeb7a824f input=a6aeb5e3447fb9e0]*/
  178. {
  179. char *buf = NULL, *buf2 = NULL, *name_chars;
  180. int nomem = 0;
  181. struct passwd *p;
  182. PyObject *bytes, *retval = NULL;
  183. if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL)
  184. return NULL;
  185. /* check for embedded null bytes */
  186. if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
  187. goto out;
  188. #ifdef HAVE_GETPWNAM_R
  189. int status;
  190. Py_ssize_t bufsize;
  191. /* Note: 'pwd' will be used via pointer 'p' on getpwnam_r success. */
  192. struct passwd pwd;
  193. Py_BEGIN_ALLOW_THREADS
  194. bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  195. if (bufsize == -1) {
  196. bufsize = DEFAULT_BUFFER_SIZE;
  197. }
  198. while(1) {
  199. buf2 = PyMem_RawRealloc(buf, bufsize);
  200. if (buf2 == NULL) {
  201. p = NULL;
  202. nomem = 1;
  203. break;
  204. }
  205. buf = buf2;
  206. status = getpwnam_r(name_chars, &pwd, buf, bufsize, &p);
  207. if (status != 0) {
  208. p = NULL;
  209. }
  210. if (p != NULL || status != ERANGE) {
  211. break;
  212. }
  213. if (bufsize > (PY_SSIZE_T_MAX >> 1)) {
  214. nomem = 1;
  215. break;
  216. }
  217. bufsize <<= 1;
  218. }
  219. Py_END_ALLOW_THREADS
  220. #else
  221. p = getpwnam(name_chars);
  222. #endif
  223. if (p == NULL) {
  224. if (nomem == 1) {
  225. PyErr_NoMemory();
  226. }
  227. else {
  228. PyErr_Format(PyExc_KeyError,
  229. "getpwnam(): name not found: %R", name);
  230. }
  231. goto out;
  232. }
  233. retval = mkpwent(module, p);
  234. out:
  235. PyMem_RawFree(buf);
  236. Py_DECREF(bytes);
  237. return retval;
  238. }
  239. #ifdef HAVE_GETPWENT
  240. /*[clinic input]
  241. pwd.getpwall
  242. Return a list of all available password database entries, in arbitrary order.
  243. See help(pwd) for more on password database entries.
  244. [clinic start generated code]*/
  245. static PyObject *
  246. pwd_getpwall_impl(PyObject *module)
  247. /*[clinic end generated code: output=4853d2f5a0afac8a input=d7ecebfd90219b85]*/
  248. {
  249. PyObject *d;
  250. struct passwd *p;
  251. if ((d = PyList_New(0)) == NULL)
  252. return NULL;
  253. setpwent();
  254. while ((p = getpwent()) != NULL) {
  255. PyObject *v = mkpwent(module, p);
  256. if (v == NULL || PyList_Append(d, v) != 0) {
  257. Py_XDECREF(v);
  258. Py_DECREF(d);
  259. endpwent();
  260. return NULL;
  261. }
  262. Py_DECREF(v);
  263. }
  264. endpwent();
  265. return d;
  266. }
  267. #endif
  268. static PyMethodDef pwd_methods[] = {
  269. PWD_GETPWUID_METHODDEF
  270. PWD_GETPWNAM_METHODDEF
  271. #ifdef HAVE_GETPWENT
  272. PWD_GETPWALL_METHODDEF
  273. #endif
  274. {NULL, NULL} /* sentinel */
  275. };
  276. static int
  277. pwdmodule_exec(PyObject *module)
  278. {
  279. pwdmodulestate *state = get_pwd_state(module);
  280. state->StructPwdType = PyStructSequence_NewType(&struct_pwd_type_desc);
  281. if (state->StructPwdType == NULL) {
  282. return -1;
  283. }
  284. if (PyModule_AddType(module, state->StructPwdType) < 0) {
  285. return -1;
  286. }
  287. return 0;
  288. }
  289. static PyModuleDef_Slot pwdmodule_slots[] = {
  290. {Py_mod_exec, pwdmodule_exec},
  291. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  292. {0, NULL}
  293. };
  294. static int pwdmodule_traverse(PyObject *m, visitproc visit, void *arg) {
  295. Py_VISIT(get_pwd_state(m)->StructPwdType);
  296. return 0;
  297. }
  298. static int pwdmodule_clear(PyObject *m) {
  299. Py_CLEAR(get_pwd_state(m)->StructPwdType);
  300. return 0;
  301. }
  302. static void pwdmodule_free(void *m) {
  303. pwdmodule_clear((PyObject *)m);
  304. }
  305. static struct PyModuleDef pwdmodule = {
  306. PyModuleDef_HEAD_INIT,
  307. .m_name = "pwd",
  308. .m_doc = pwd__doc__,
  309. .m_size = sizeof(pwdmodulestate),
  310. .m_methods = pwd_methods,
  311. .m_slots = pwdmodule_slots,
  312. .m_traverse = pwdmodule_traverse,
  313. .m_clear = pwdmodule_clear,
  314. .m_free = pwdmodule_free,
  315. };
  316. PyMODINIT_FUNC
  317. PyInit_pwd(void)
  318. {
  319. return PyModuleDef_Init(&pwdmodule);
  320. }