blob: 14d3f9dcb1c60fde21d2f4b97125504ad5d2f03e [file] [log] [blame]
Guido van Rossum864407d1991-04-10 19:48:25 +00001
Guido van Rossumb6775db1994-08-01 11:34:53 +00002/* UNIX password file access module */
Guido van Rossum864407d1991-04-10 19:48:25 +00003
Barry Warsaw50c5cf11996-12-11 16:54:40 +00004#include "Python.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005#include "posixmodule.h"
Guido van Rossum864407d1991-04-10 19:48:25 +00006
Guido van Rossum864407d1991-04-10 19:48:25 +00007#include <pwd.h>
Guido van Rossum864407d1991-04-10 19:48:25 +00008
Brett Cannon3d25e162014-08-22 14:03:51 -04009#include "clinic/pwdmodule.c.h"
10/*[clinic input]
Brett Cannon3d25e162014-08-22 14:03:51 -040011module pwd
12[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030013/*[clinic end generated code: output=da39a3ee5e6b4b0d input=60f628ef356b97b6]*/
Brett Cannon3d25e162014-08-22 14:03:51 -040014
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000015static PyStructSequence_Field struct_pwd_type_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016 {"pw_name", "user name"},
17 {"pw_passwd", "password"},
18 {"pw_uid", "user id"},
19 {"pw_gid", "group id"},
20 {"pw_gecos", "real name"},
21 {"pw_dir", "home directory"},
22 {"pw_shell", "shell program"},
23 {0}
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000024};
25
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000026PyDoc_STRVAR(struct_passwd__doc__,
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000027"pwd.struct_passwd: Results from getpw*() routines.\n\n\
28This object may be accessed either as a tuple of\n\
29 (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030or via the object attributes as named in the above tuple.");
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000031
32static PyStructSequence_Desc struct_pwd_type_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 "pwd.struct_passwd",
34 struct_passwd__doc__,
35 struct_pwd_type_fields,
36 7,
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000037};
38
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000039PyDoc_STRVAR(pwd__doc__,
40"This module provides access to the Unix password database.\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +000041It is available on all Unix versions.\n\
42\n\
43Password database entries are reported as 7-tuples containing the following\n\
44items from the password database (see `<pwd.h>'), in order:\n\
45pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
46The uid and gid items are integers, all others are strings. An\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000047exception is raised if the entry asked for cannot be found.");
Guido van Rossum3e79c441998-03-03 22:03:26 +000048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049
Dino Viehlandb7f8e522019-09-10 13:59:43 +010050typedef struct {
51 PyTypeObject *StructPwdType;
52} pwdmodulestate;
Hai Shif707d942020-03-16 21:15:01 +080053
54static inline pwdmodulestate*
55get_pwd_state(PyObject *module)
56{
57 void *state = PyModule_GetState(module);
58 assert(state != NULL);
59 return (pwdmodulestate *)state;
60}
61
Dino Viehlandb7f8e522019-09-10 13:59:43 +010062static struct PyModuleDef pwdmodule;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000063
William Grzybowski23e65b22018-09-07 09:06:15 -030064#define DEFAULT_BUFFER_SIZE 1024
65
Martin v. Löwis29275c92002-09-17 09:34:06 +000066static void
Neal Norwitzeb8b3a62007-08-24 23:26:23 +000067sets(PyObject *v, int i, const char* val)
Martin v. Löwis29275c92002-09-17 09:34:06 +000068{
Neal Norwitz3d7a90d2007-10-27 05:40:06 +000069 if (val) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 PyObject *o = PyUnicode_DecodeFSDefault(val);
71 PyStructSequence_SET_ITEM(v, i, o);
Neal Norwitz3d7a90d2007-10-27 05:40:06 +000072 }
Martin v. Löwis29275c92002-09-17 09:34:06 +000073 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 PyStructSequence_SET_ITEM(v, i, Py_None);
75 Py_INCREF(Py_None);
Martin v. Löwis29275c92002-09-17 09:34:06 +000076 }
77}
78
Barry Warsaw50c5cf11996-12-11 16:54:40 +000079static PyObject *
Christian Heimesfa2eee92020-11-19 08:47:32 +010080mkpwent(PyObject *module, struct passwd *p)
Guido van Rossum864407d1991-04-10 19:48:25 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 int setIndex = 0;
Christian Heimesfa2eee92020-11-19 08:47:32 +010083 PyObject *v = PyStructSequence_New(get_pwd_state(module)->StructPwdType);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 if (v == NULL)
85 return NULL;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000086
Christian Heimes217cfd12007-12-02 14:31:20 +000087#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
Martin v. Löwis29275c92002-09-17 09:34:06 +000088#define SETS(i,val) sets(v, i, val)
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 SETS(setIndex++, p->pw_name);
Stefan Krah45009772016-04-26 11:43:21 +020091#if defined(HAVE_STRUCT_PASSWD_PW_PASSWD) && !defined(__ANDROID__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 SETS(setIndex++, p->pw_passwd);
Stefan Krah267b6392016-04-26 01:09:18 +020093#else
94 SETS(setIndex++, "");
95#endif
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020096 PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
97 PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
Stefan Krah267b6392016-04-26 01:09:18 +020098#if defined(HAVE_STRUCT_PASSWD_PW_GECOS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 SETS(setIndex++, p->pw_gecos);
Stefan Krah267b6392016-04-26 01:09:18 +0200100#else
101 SETS(setIndex++, "");
102#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 SETS(setIndex++, p->pw_dir);
104 SETS(setIndex++, p->pw_shell);
Martin v. Löwisdbd55b32002-03-01 10:38:44 +0000105
106#undef SETS
107#undef SETI
108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 if (PyErr_Occurred()) {
110 Py_XDECREF(v);
111 return NULL;
112 }
Martin v. Löwisdbd55b32002-03-01 10:38:44 +0000113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 return v;
Guido van Rossum864407d1991-04-10 19:48:25 +0000115}
116
Brett Cannon3d25e162014-08-22 14:03:51 -0400117/*[clinic input]
118pwd.getpwuid
119
120 uidobj: object
121 /
122
123Return the password database entry for the given numeric user ID.
124
125See `help(pwd)` for more on password database entries.
126[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000127
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000128static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300129pwd_getpwuid(PyObject *module, PyObject *uidobj)
130/*[clinic end generated code: output=c4ee1d4d429b86c4 input=ae64d507a1c6d3e8]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000131{
William Grzybowski23e65b22018-09-07 09:06:15 -0300132 PyObject *retval = NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200133 uid_t uid;
William Grzybowski23e65b22018-09-07 09:06:15 -0300134 int nomem = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 struct passwd *p;
William Grzybowski23e65b22018-09-07 09:06:15 -0300136 char *buf = NULL, *buf2 = NULL;
Brett Cannon3d25e162014-08-22 14:03:51 -0400137
138 if (!_Py_Uid_Converter(uidobj, &uid)) {
Serhiy Storchaka55e22382013-02-11 20:32:47 +0200139 if (PyErr_ExceptionMatches(PyExc_OverflowError))
140 PyErr_Format(PyExc_KeyError,
141 "getpwuid(): uid not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 return NULL;
Serhiy Storchaka55e22382013-02-11 20:32:47 +0200143 }
William Grzybowski23e65b22018-09-07 09:06:15 -0300144#ifdef HAVE_GETPWUID_R
William Grzybowski23e65b22018-09-07 09:06:15 -0300145 int status;
146 Py_ssize_t bufsize;
Alexey Izbysheve359bc22018-11-04 18:44:16 +0300147 /* Note: 'pwd' will be used via pointer 'p' on getpwuid_r success. */
William Grzybowski23e65b22018-09-07 09:06:15 -0300148 struct passwd pwd;
149
Alexey Izbysheve359bc22018-11-04 18:44:16 +0300150 Py_BEGIN_ALLOW_THREADS
William Grzybowski23e65b22018-09-07 09:06:15 -0300151 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
152 if (bufsize == -1) {
153 bufsize = DEFAULT_BUFFER_SIZE;
154 }
155
156 while(1) {
157 buf2 = PyMem_RawRealloc(buf, bufsize);
158 if (buf2 == NULL) {
Zackery Spytz570e3712018-11-05 12:26:40 -0700159 p = NULL;
William Grzybowski23e65b22018-09-07 09:06:15 -0300160 nomem = 1;
161 break;
162 }
163 buf = buf2;
164 status = getpwuid_r(uid, &pwd, buf, bufsize, &p);
165 if (status != 0) {
166 p = NULL;
167 }
168 if (p != NULL || status != ERANGE) {
169 break;
170 }
171 if (bufsize > (PY_SSIZE_T_MAX >> 1)) {
172 nomem = 1;
173 break;
174 }
175 bufsize <<= 1;
176 }
177
178 Py_END_ALLOW_THREADS
179#else
180 p = getpwuid(uid);
181#endif
182 if (p == NULL) {
183 PyMem_RawFree(buf);
184 if (nomem == 1) {
185 return PyErr_NoMemory();
186 }
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200187 PyObject *uid_obj = _PyLong_FromUid(uid);
188 if (uid_obj == NULL)
189 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 PyErr_Format(PyExc_KeyError,
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200191 "getpwuid(): uid not found: %S", uid_obj);
192 Py_DECREF(uid_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 return NULL;
194 }
Christian Heimesfa2eee92020-11-19 08:47:32 +0100195 retval = mkpwent(module, p);
William Grzybowski23e65b22018-09-07 09:06:15 -0300196#ifdef HAVE_GETPWUID_R
197 PyMem_RawFree(buf);
198#endif
199 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000200}
201
Brett Cannon3d25e162014-08-22 14:03:51 -0400202/*[clinic input]
203pwd.getpwnam
204
William Grzybowski28658482018-09-07 14:10:39 -0300205 name: unicode
Brett Cannon3d25e162014-08-22 14:03:51 -0400206 /
207
208Return the password database entry for the given user name.
209
210See `help(pwd)` for more on password database entries.
211[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000212
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000213static PyObject *
William Grzybowski28658482018-09-07 14:10:39 -0300214pwd_getpwnam_impl(PyObject *module, PyObject *name)
215/*[clinic end generated code: output=359ce1ddeb7a824f input=a6aeb5e3447fb9e0]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000216{
William Grzybowski28658482018-09-07 14:10:39 -0300217 char *buf = NULL, *buf2 = NULL, *name_chars;
William Grzybowski23e65b22018-09-07 09:06:15 -0300218 int nomem = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 struct passwd *p;
Brett Cannon3d25e162014-08-22 14:03:51 -0400220 PyObject *bytes, *retval = NULL;
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000221
William Grzybowski28658482018-09-07 14:10:39 -0300222 if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 return NULL;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300224 /* check for embedded null bytes */
William Grzybowski28658482018-09-07 14:10:39 -0300225 if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 goto out;
William Grzybowski23e65b22018-09-07 09:06:15 -0300227#ifdef HAVE_GETPWNAM_R
William Grzybowski23e65b22018-09-07 09:06:15 -0300228 int status;
229 Py_ssize_t bufsize;
Alexey Izbysheve359bc22018-11-04 18:44:16 +0300230 /* Note: 'pwd' will be used via pointer 'p' on getpwnam_r success. */
William Grzybowski23e65b22018-09-07 09:06:15 -0300231 struct passwd pwd;
232
Alexey Izbysheve359bc22018-11-04 18:44:16 +0300233 Py_BEGIN_ALLOW_THREADS
William Grzybowski23e65b22018-09-07 09:06:15 -0300234 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
235 if (bufsize == -1) {
236 bufsize = DEFAULT_BUFFER_SIZE;
237 }
238
239 while(1) {
240 buf2 = PyMem_RawRealloc(buf, bufsize);
241 if (buf2 == NULL) {
Zackery Spytz570e3712018-11-05 12:26:40 -0700242 p = NULL;
William Grzybowski23e65b22018-09-07 09:06:15 -0300243 nomem = 1;
244 break;
245 }
246 buf = buf2;
William Grzybowski28658482018-09-07 14:10:39 -0300247 status = getpwnam_r(name_chars, &pwd, buf, bufsize, &p);
William Grzybowski23e65b22018-09-07 09:06:15 -0300248 if (status != 0) {
249 p = NULL;
250 }
251 if (p != NULL || status != ERANGE) {
252 break;
253 }
254 if (bufsize > (PY_SSIZE_T_MAX >> 1)) {
255 nomem = 1;
256 break;
257 }
258 bufsize <<= 1;
259 }
260
261 Py_END_ALLOW_THREADS
262#else
William Grzybowski28658482018-09-07 14:10:39 -0300263 p = getpwnam(name_chars);
William Grzybowski23e65b22018-09-07 09:06:15 -0300264#endif
265 if (p == NULL) {
266 if (nomem == 1) {
267 PyErr_NoMemory();
268 }
269 else {
270 PyErr_Format(PyExc_KeyError,
William Grzybowski34c7f0c2018-12-05 17:10:18 -0200271 "getpwnam(): name not found: %R", name);
William Grzybowski23e65b22018-09-07 09:06:15 -0300272 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 goto out;
274 }
Christian Heimesfa2eee92020-11-19 08:47:32 +0100275 retval = mkpwent(module, p);
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000276out:
William Grzybowski23e65b22018-09-07 09:06:15 -0300277 PyMem_RawFree(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 Py_DECREF(bytes);
279 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000280}
281
Guido van Rossum1171ee61997-08-22 20:42:00 +0000282#ifdef HAVE_GETPWENT
Brett Cannon3d25e162014-08-22 14:03:51 -0400283/*[clinic input]
284pwd.getpwall
285
286Return a list of all available password database entries, in arbitrary order.
287
288See help(pwd) for more on password database entries.
289[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000290
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000291static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300292pwd_getpwall_impl(PyObject *module)
293/*[clinic end generated code: output=4853d2f5a0afac8a input=d7ecebfd90219b85]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 PyObject *d;
296 struct passwd *p;
297 if ((d = PyList_New(0)) == NULL)
298 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 setpwent();
300 while ((p = getpwent()) != NULL) {
Christian Heimesfa2eee92020-11-19 08:47:32 +0100301 PyObject *v = mkpwent(module, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 if (v == NULL || PyList_Append(d, v) != 0) {
303 Py_XDECREF(v);
304 Py_DECREF(d);
305 endpwent();
306 return NULL;
307 }
308 Py_DECREF(v);
309 }
310 endpwent();
311 return d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000312}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000313#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000314
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000315static PyMethodDef pwd_methods[] = {
Brett Cannon3d25e162014-08-22 14:03:51 -0400316 PWD_GETPWUID_METHODDEF
317 PWD_GETPWNAM_METHODDEF
Guido van Rossum1171ee61997-08-22 20:42:00 +0000318#ifdef HAVE_GETPWENT
Brett Cannon3d25e162014-08-22 14:03:51 -0400319 PWD_GETPWALL_METHODDEF
Guido van Rossum1171ee61997-08-22 20:42:00 +0000320#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 {NULL, NULL} /* sentinel */
Guido van Rossum864407d1991-04-10 19:48:25 +0000322};
323
Christian Heimesfa2eee92020-11-19 08:47:32 +0100324static int
325pwdmodule_exec(PyObject *module)
326{
327 pwdmodulestate *state = get_pwd_state(module);
328
329 state->StructPwdType = PyStructSequence_NewType(&struct_pwd_type_desc);
330 if (state->StructPwdType == NULL) {
331 return -1;
332 }
333 if (PyModule_AddType(module, state->StructPwdType) < 0) {
334 return -1;
335 }
336 return 0;
337}
338
339static PyModuleDef_Slot pwdmodule_slots[] = {
340 {Py_mod_exec, pwdmodule_exec},
341 {0, NULL}
342};
343
Dino Viehlandb7f8e522019-09-10 13:59:43 +0100344static int pwdmodule_traverse(PyObject *m, visitproc visit, void *arg) {
Hai Shif707d942020-03-16 21:15:01 +0800345 Py_VISIT(get_pwd_state(m)->StructPwdType);
Dino Viehlandb7f8e522019-09-10 13:59:43 +0100346 return 0;
347}
348static int pwdmodule_clear(PyObject *m) {
Hai Shif707d942020-03-16 21:15:01 +0800349 Py_CLEAR(get_pwd_state(m)->StructPwdType);
Dino Viehlandb7f8e522019-09-10 13:59:43 +0100350 return 0;
351}
352static void pwdmodule_free(void *m) {
353 pwdmodule_clear((PyObject *)m);
354}
355
Martin v. Löwis1a214512008-06-11 05:26:20 +0000356static struct PyModuleDef pwdmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 PyModuleDef_HEAD_INIT,
Christian Heimesfa2eee92020-11-19 08:47:32 +0100358 .m_name = "pwd",
359 .m_doc = pwd__doc__,
360 .m_size = sizeof(pwdmodulestate),
361 .m_methods = pwd_methods,
362 .m_slots = pwdmodule_slots,
363 .m_traverse = pwdmodule_traverse,
364 .m_clear = pwdmodule_clear,
365 .m_free = pwdmodule_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +0000366};
367
368
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000369PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000370PyInit_pwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000371{
Christian Heimesfa2eee92020-11-19 08:47:32 +0100372 return PyModuleDef_Init(&pwdmodule);
Guido van Rossum864407d1991-04-10 19:48:25 +0000373}