grpmodule.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* UNIX group file access module */
  2. #include "Python.h"
  3. #include "posixmodule.h"
  4. #include <grp.h>
  5. #include "clinic/grpmodule.c.h"
  6. /*[clinic input]
  7. module grp
  8. [clinic start generated code]*/
  9. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=cade63f2ed1bd9f8]*/
  10. static PyStructSequence_Field struct_group_type_fields[] = {
  11. {"gr_name", "group name"},
  12. {"gr_passwd", "password"},
  13. {"gr_gid", "group id"},
  14. {"gr_mem", "group members"},
  15. {0}
  16. };
  17. PyDoc_STRVAR(struct_group__doc__,
  18. "grp.struct_group: Results from getgr*() routines.\n\n\
  19. This object may be accessed either as a tuple of\n\
  20. (gr_name,gr_passwd,gr_gid,gr_mem)\n\
  21. or via the object attributes as named in the above tuple.\n");
  22. static PyStructSequence_Desc struct_group_type_desc = {
  23. "grp.struct_group",
  24. struct_group__doc__,
  25. struct_group_type_fields,
  26. 4,
  27. };
  28. typedef struct {
  29. PyTypeObject *StructGrpType;
  30. } grpmodulestate;
  31. static inline grpmodulestate*
  32. get_grp_state(PyObject *module)
  33. {
  34. void *state = PyModule_GetState(module);
  35. assert(state != NULL);
  36. return (grpmodulestate *)state;
  37. }
  38. static struct PyModuleDef grpmodule;
  39. #define DEFAULT_BUFFER_SIZE 1024
  40. static PyObject *
  41. mkgrent(PyObject *module, struct group *p)
  42. {
  43. int setIndex = 0;
  44. PyObject *v, *w;
  45. char **member;
  46. v = PyStructSequence_New(get_grp_state(module)->StructGrpType);
  47. if (v == NULL)
  48. return NULL;
  49. if ((w = PyList_New(0)) == NULL) {
  50. Py_DECREF(v);
  51. return NULL;
  52. }
  53. for (member = p->gr_mem; ; member++) {
  54. char *group_member;
  55. // member can be misaligned
  56. memcpy(&group_member, member, sizeof(group_member));
  57. if (group_member == NULL) {
  58. break;
  59. }
  60. PyObject *x = PyUnicode_DecodeFSDefault(group_member);
  61. if (x == NULL || PyList_Append(w, x) != 0) {
  62. Py_XDECREF(x);
  63. Py_DECREF(w);
  64. Py_DECREF(v);
  65. return NULL;
  66. }
  67. Py_DECREF(x);
  68. }
  69. #define SET(i,val) PyStructSequence_SET_ITEM(v, i, val)
  70. SET(setIndex++, PyUnicode_DecodeFSDefault(p->gr_name));
  71. if (p->gr_passwd)
  72. SET(setIndex++, PyUnicode_DecodeFSDefault(p->gr_passwd));
  73. else {
  74. SET(setIndex++, Py_None);
  75. Py_INCREF(Py_None);
  76. }
  77. SET(setIndex++, _PyLong_FromGid(p->gr_gid));
  78. SET(setIndex++, w);
  79. #undef SET
  80. if (PyErr_Occurred()) {
  81. Py_DECREF(v);
  82. return NULL;
  83. }
  84. return v;
  85. }
  86. /*[clinic input]
  87. grp.getgrgid
  88. id: object
  89. Return the group database entry for the given numeric group ID.
  90. If id is not valid, raise KeyError.
  91. [clinic start generated code]*/
  92. static PyObject *
  93. grp_getgrgid_impl(PyObject *module, PyObject *id)
  94. /*[clinic end generated code: output=30797c289504a1ba input=15fa0e2ccf5cda25]*/
  95. {
  96. PyObject *retval = NULL;
  97. int nomem = 0;
  98. char *buf = NULL, *buf2 = NULL;
  99. gid_t gid;
  100. struct group *p;
  101. if (!_Py_Gid_Converter(id, &gid)) {
  102. return NULL;
  103. }
  104. #ifdef HAVE_GETGRGID_R
  105. int status;
  106. Py_ssize_t bufsize;
  107. /* Note: 'grp' will be used via pointer 'p' on getgrgid_r success. */
  108. struct group grp;
  109. Py_BEGIN_ALLOW_THREADS
  110. bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
  111. if (bufsize == -1) {
  112. bufsize = DEFAULT_BUFFER_SIZE;
  113. }
  114. while (1) {
  115. buf2 = PyMem_RawRealloc(buf, bufsize);
  116. if (buf2 == NULL) {
  117. p = NULL;
  118. nomem = 1;
  119. break;
  120. }
  121. buf = buf2;
  122. status = getgrgid_r(gid, &grp, buf, bufsize, &p);
  123. if (status != 0) {
  124. p = NULL;
  125. }
  126. if (p != NULL || status != ERANGE) {
  127. break;
  128. }
  129. if (bufsize > (PY_SSIZE_T_MAX >> 1)) {
  130. nomem = 1;
  131. break;
  132. }
  133. bufsize <<= 1;
  134. }
  135. Py_END_ALLOW_THREADS
  136. #else
  137. p = getgrgid(gid);
  138. #endif
  139. if (p == NULL) {
  140. PyMem_RawFree(buf);
  141. if (nomem == 1) {
  142. return PyErr_NoMemory();
  143. }
  144. PyObject *gid_obj = _PyLong_FromGid(gid);
  145. if (gid_obj == NULL)
  146. return NULL;
  147. PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %S", gid_obj);
  148. Py_DECREF(gid_obj);
  149. return NULL;
  150. }
  151. retval = mkgrent(module, p);
  152. #ifdef HAVE_GETGRGID_R
  153. PyMem_RawFree(buf);
  154. #endif
  155. return retval;
  156. }
  157. /*[clinic input]
  158. grp.getgrnam
  159. name: unicode
  160. Return the group database entry for the given group name.
  161. If name is not valid, raise KeyError.
  162. [clinic start generated code]*/
  163. static PyObject *
  164. grp_getgrnam_impl(PyObject *module, PyObject *name)
  165. /*[clinic end generated code: output=67905086f403c21c input=08ded29affa3c863]*/
  166. {
  167. char *buf = NULL, *buf2 = NULL, *name_chars;
  168. int nomem = 0;
  169. struct group *p;
  170. PyObject *bytes, *retval = NULL;
  171. if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL)
  172. return NULL;
  173. /* check for embedded null bytes */
  174. if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
  175. goto out;
  176. #ifdef HAVE_GETGRNAM_R
  177. int status;
  178. Py_ssize_t bufsize;
  179. /* Note: 'grp' will be used via pointer 'p' on getgrnam_r success. */
  180. struct group grp;
  181. Py_BEGIN_ALLOW_THREADS
  182. bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
  183. if (bufsize == -1) {
  184. bufsize = DEFAULT_BUFFER_SIZE;
  185. }
  186. while(1) {
  187. buf2 = PyMem_RawRealloc(buf, bufsize);
  188. if (buf2 == NULL) {
  189. p = NULL;
  190. nomem = 1;
  191. break;
  192. }
  193. buf = buf2;
  194. status = getgrnam_r(name_chars, &grp, buf, bufsize, &p);
  195. if (status != 0) {
  196. p = NULL;
  197. }
  198. if (p != NULL || status != ERANGE) {
  199. break;
  200. }
  201. if (bufsize > (PY_SSIZE_T_MAX >> 1)) {
  202. nomem = 1;
  203. break;
  204. }
  205. bufsize <<= 1;
  206. }
  207. Py_END_ALLOW_THREADS
  208. #else
  209. p = getgrnam(name_chars);
  210. #endif
  211. if (p == NULL) {
  212. if (nomem == 1) {
  213. PyErr_NoMemory();
  214. }
  215. else {
  216. PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %R", name);
  217. }
  218. goto out;
  219. }
  220. retval = mkgrent(module, p);
  221. out:
  222. PyMem_RawFree(buf);
  223. Py_DECREF(bytes);
  224. return retval;
  225. }
  226. /*[clinic input]
  227. grp.getgrall
  228. Return a list of all available group entries, in arbitrary order.
  229. An entry whose name starts with '+' or '-' represents an instruction
  230. to use YP/NIS and may not be accessible via getgrnam or getgrgid.
  231. [clinic start generated code]*/
  232. static PyObject *
  233. grp_getgrall_impl(PyObject *module)
  234. /*[clinic end generated code: output=585dad35e2e763d7 input=d7df76c825c367df]*/
  235. {
  236. PyObject *d;
  237. struct group *p;
  238. if ((d = PyList_New(0)) == NULL)
  239. return NULL;
  240. setgrent();
  241. while ((p = getgrent()) != NULL) {
  242. PyObject *v = mkgrent(module, p);
  243. if (v == NULL || PyList_Append(d, v) != 0) {
  244. Py_XDECREF(v);
  245. Py_DECREF(d);
  246. endgrent();
  247. return NULL;
  248. }
  249. Py_DECREF(v);
  250. }
  251. endgrent();
  252. return d;
  253. }
  254. static PyMethodDef grp_methods[] = {
  255. GRP_GETGRGID_METHODDEF
  256. GRP_GETGRNAM_METHODDEF
  257. GRP_GETGRALL_METHODDEF
  258. {NULL, NULL}
  259. };
  260. PyDoc_STRVAR(grp__doc__,
  261. "Access to the Unix group database.\n\
  262. \n\
  263. Group entries are reported as 4-tuples containing the following fields\n\
  264. from the group database, in order:\n\
  265. \n\
  266. gr_name - name of the group\n\
  267. gr_passwd - group password (encrypted); often empty\n\
  268. gr_gid - numeric ID of the group\n\
  269. gr_mem - list of members\n\
  270. \n\
  271. The gid is an integer, name and password are strings. (Note that most\n\
  272. users are not explicitly listed as members of the groups they are in\n\
  273. according to the password database. Check both databases to get\n\
  274. complete membership information.)");
  275. static int
  276. grpmodule_exec(PyObject *module)
  277. {
  278. grpmodulestate *state = get_grp_state(module);
  279. state->StructGrpType = PyStructSequence_NewType(&struct_group_type_desc);
  280. if (state->StructGrpType == NULL) {
  281. return -1;
  282. }
  283. if (PyModule_AddType(module, state->StructGrpType) < 0) {
  284. return -1;
  285. }
  286. return 0;
  287. }
  288. static PyModuleDef_Slot grpmodule_slots[] = {
  289. {Py_mod_exec, grpmodule_exec},
  290. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  291. {0, NULL}
  292. };
  293. static int grpmodule_traverse(PyObject *m, visitproc visit, void *arg) {
  294. Py_VISIT(get_grp_state(m)->StructGrpType);
  295. return 0;
  296. }
  297. static int grpmodule_clear(PyObject *m) {
  298. Py_CLEAR(get_grp_state(m)->StructGrpType);
  299. return 0;
  300. }
  301. static void grpmodule_free(void *m) {
  302. grpmodule_clear((PyObject *)m);
  303. }
  304. static struct PyModuleDef grpmodule = {
  305. PyModuleDef_HEAD_INIT,
  306. .m_name = "grp",
  307. .m_doc = grp__doc__,
  308. .m_size = sizeof(grpmodulestate),
  309. .m_methods = grp_methods,
  310. .m_slots = grpmodule_slots,
  311. .m_traverse = grpmodule_traverse,
  312. .m_clear = grpmodule_clear,
  313. .m_free = grpmodule_free,
  314. };
  315. PyMODINIT_FUNC
  316. PyInit_grp(void)
  317. {
  318. return PyModuleDef_Init(&grpmodule);
  319. }