pwdmodule.c 8.9 KB

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