blob: b5ef2557db77d938a0f36f6d2cd634d488cddedb [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;
53#define modulestate(o) ((pwdmodulestate *)PyModule_GetState(o))
54#define modulestate_global modulestate(PyState_FindModule(&pwdmodule))
55
56static struct PyModuleDef pwdmodule;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000057
William Grzybowski23e65b22018-09-07 09:06:15 -030058#define DEFAULT_BUFFER_SIZE 1024
59
Martin v. Löwis29275c92002-09-17 09:34:06 +000060static void
Neal Norwitzeb8b3a62007-08-24 23:26:23 +000061sets(PyObject *v, int i, const char* val)
Martin v. Löwis29275c92002-09-17 09:34:06 +000062{
Neal Norwitz3d7a90d2007-10-27 05:40:06 +000063 if (val) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 PyObject *o = PyUnicode_DecodeFSDefault(val);
65 PyStructSequence_SET_ITEM(v, i, o);
Neal Norwitz3d7a90d2007-10-27 05:40:06 +000066 }
Martin v. Löwis29275c92002-09-17 09:34:06 +000067 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 PyStructSequence_SET_ITEM(v, i, Py_None);
69 Py_INCREF(Py_None);
Martin v. Löwis29275c92002-09-17 09:34:06 +000070 }
71}
72
Barry Warsaw50c5cf11996-12-11 16:54:40 +000073static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000074mkpwent(struct passwd *p)
Guido van Rossum864407d1991-04-10 19:48:25 +000075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 int setIndex = 0;
Dino Viehlandb7f8e522019-09-10 13:59:43 +010077 PyObject *v = PyStructSequence_New(modulestate_global->StructPwdType);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (v == NULL)
79 return NULL;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000080
Christian Heimes217cfd12007-12-02 14:31:20 +000081#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
Martin v. Löwis29275c92002-09-17 09:34:06 +000082#define SETS(i,val) sets(v, i, val)
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 SETS(setIndex++, p->pw_name);
Stefan Krah45009772016-04-26 11:43:21 +020085#if defined(HAVE_STRUCT_PASSWD_PW_PASSWD) && !defined(__ANDROID__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 SETS(setIndex++, p->pw_passwd);
Stefan Krah267b6392016-04-26 01:09:18 +020087#else
88 SETS(setIndex++, "");
89#endif
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020090 PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
91 PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
Stefan Krah267b6392016-04-26 01:09:18 +020092#if defined(HAVE_STRUCT_PASSWD_PW_GECOS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 SETS(setIndex++, p->pw_gecos);
Stefan Krah267b6392016-04-26 01:09:18 +020094#else
95 SETS(setIndex++, "");
96#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 SETS(setIndex++, p->pw_dir);
98 SETS(setIndex++, p->pw_shell);
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000099
100#undef SETS
101#undef SETI
102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 if (PyErr_Occurred()) {
104 Py_XDECREF(v);
105 return NULL;
106 }
Martin v. Löwisdbd55b32002-03-01 10:38:44 +0000107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 return v;
Guido van Rossum864407d1991-04-10 19:48:25 +0000109}
110
Brett Cannon3d25e162014-08-22 14:03:51 -0400111/*[clinic input]
112pwd.getpwuid
113
114 uidobj: object
115 /
116
117Return the password database entry for the given numeric user ID.
118
119See `help(pwd)` for more on password database entries.
120[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000121
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000122static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300123pwd_getpwuid(PyObject *module, PyObject *uidobj)
124/*[clinic end generated code: output=c4ee1d4d429b86c4 input=ae64d507a1c6d3e8]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000125{
William Grzybowski23e65b22018-09-07 09:06:15 -0300126 PyObject *retval = NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200127 uid_t uid;
William Grzybowski23e65b22018-09-07 09:06:15 -0300128 int nomem = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 struct passwd *p;
William Grzybowski23e65b22018-09-07 09:06:15 -0300130 char *buf = NULL, *buf2 = NULL;
Brett Cannon3d25e162014-08-22 14:03:51 -0400131
132 if (!_Py_Uid_Converter(uidobj, &uid)) {
Serhiy Storchaka55e22382013-02-11 20:32:47 +0200133 if (PyErr_ExceptionMatches(PyExc_OverflowError))
134 PyErr_Format(PyExc_KeyError,
135 "getpwuid(): uid not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 return NULL;
Serhiy Storchaka55e22382013-02-11 20:32:47 +0200137 }
William Grzybowski23e65b22018-09-07 09:06:15 -0300138#ifdef HAVE_GETPWUID_R
William Grzybowski23e65b22018-09-07 09:06:15 -0300139 int status;
140 Py_ssize_t bufsize;
Alexey Izbysheve359bc22018-11-04 18:44:16 +0300141 /* Note: 'pwd' will be used via pointer 'p' on getpwuid_r success. */
William Grzybowski23e65b22018-09-07 09:06:15 -0300142 struct passwd pwd;
143
Alexey Izbysheve359bc22018-11-04 18:44:16 +0300144 Py_BEGIN_ALLOW_THREADS
William Grzybowski23e65b22018-09-07 09:06:15 -0300145 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
146 if (bufsize == -1) {
147 bufsize = DEFAULT_BUFFER_SIZE;
148 }
149
150 while(1) {
151 buf2 = PyMem_RawRealloc(buf, bufsize);
152 if (buf2 == NULL) {
Zackery Spytz570e3712018-11-05 12:26:40 -0700153 p = NULL;
William Grzybowski23e65b22018-09-07 09:06:15 -0300154 nomem = 1;
155 break;
156 }
157 buf = buf2;
158 status = getpwuid_r(uid, &pwd, buf, bufsize, &p);
159 if (status != 0) {
160 p = NULL;
161 }
162 if (p != NULL || status != ERANGE) {
163 break;
164 }
165 if (bufsize > (PY_SSIZE_T_MAX >> 1)) {
166 nomem = 1;
167 break;
168 }
169 bufsize <<= 1;
170 }
171
172 Py_END_ALLOW_THREADS
173#else
174 p = getpwuid(uid);
175#endif
176 if (p == NULL) {
177 PyMem_RawFree(buf);
178 if (nomem == 1) {
179 return PyErr_NoMemory();
180 }
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200181 PyObject *uid_obj = _PyLong_FromUid(uid);
182 if (uid_obj == NULL)
183 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 PyErr_Format(PyExc_KeyError,
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200185 "getpwuid(): uid not found: %S", uid_obj);
186 Py_DECREF(uid_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 return NULL;
188 }
William Grzybowski23e65b22018-09-07 09:06:15 -0300189 retval = mkpwent(p);
190#ifdef HAVE_GETPWUID_R
191 PyMem_RawFree(buf);
192#endif
193 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000194}
195
Brett Cannon3d25e162014-08-22 14:03:51 -0400196/*[clinic input]
197pwd.getpwnam
198
William Grzybowski28658482018-09-07 14:10:39 -0300199 name: unicode
Brett Cannon3d25e162014-08-22 14:03:51 -0400200 /
201
202Return the password database entry for the given user name.
203
204See `help(pwd)` for more on password database entries.
205[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000206
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000207static PyObject *
William Grzybowski28658482018-09-07 14:10:39 -0300208pwd_getpwnam_impl(PyObject *module, PyObject *name)
209/*[clinic end generated code: output=359ce1ddeb7a824f input=a6aeb5e3447fb9e0]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000210{
William Grzybowski28658482018-09-07 14:10:39 -0300211 char *buf = NULL, *buf2 = NULL, *name_chars;
William Grzybowski23e65b22018-09-07 09:06:15 -0300212 int nomem = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 struct passwd *p;
Brett Cannon3d25e162014-08-22 14:03:51 -0400214 PyObject *bytes, *retval = NULL;
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000215
William Grzybowski28658482018-09-07 14:10:39 -0300216 if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 return NULL;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300218 /* check for embedded null bytes */
William Grzybowski28658482018-09-07 14:10:39 -0300219 if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 goto out;
William Grzybowski23e65b22018-09-07 09:06:15 -0300221#ifdef HAVE_GETPWNAM_R
William Grzybowski23e65b22018-09-07 09:06:15 -0300222 int status;
223 Py_ssize_t bufsize;
Alexey Izbysheve359bc22018-11-04 18:44:16 +0300224 /* Note: 'pwd' will be used via pointer 'p' on getpwnam_r success. */
William Grzybowski23e65b22018-09-07 09:06:15 -0300225 struct passwd pwd;
226
Alexey Izbysheve359bc22018-11-04 18:44:16 +0300227 Py_BEGIN_ALLOW_THREADS
William Grzybowski23e65b22018-09-07 09:06:15 -0300228 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
229 if (bufsize == -1) {
230 bufsize = DEFAULT_BUFFER_SIZE;
231 }
232
233 while(1) {
234 buf2 = PyMem_RawRealloc(buf, bufsize);
235 if (buf2 == NULL) {
Zackery Spytz570e3712018-11-05 12:26:40 -0700236 p = NULL;
William Grzybowski23e65b22018-09-07 09:06:15 -0300237 nomem = 1;
238 break;
239 }
240 buf = buf2;
William Grzybowski28658482018-09-07 14:10:39 -0300241 status = getpwnam_r(name_chars, &pwd, buf, bufsize, &p);
William Grzybowski23e65b22018-09-07 09:06:15 -0300242 if (status != 0) {
243 p = NULL;
244 }
245 if (p != NULL || status != ERANGE) {
246 break;
247 }
248 if (bufsize > (PY_SSIZE_T_MAX >> 1)) {
249 nomem = 1;
250 break;
251 }
252 bufsize <<= 1;
253 }
254
255 Py_END_ALLOW_THREADS
256#else
William Grzybowski28658482018-09-07 14:10:39 -0300257 p = getpwnam(name_chars);
William Grzybowski23e65b22018-09-07 09:06:15 -0300258#endif
259 if (p == NULL) {
260 if (nomem == 1) {
261 PyErr_NoMemory();
262 }
263 else {
264 PyErr_Format(PyExc_KeyError,
William Grzybowski34c7f0c2018-12-05 17:10:18 -0200265 "getpwnam(): name not found: %R", name);
William Grzybowski23e65b22018-09-07 09:06:15 -0300266 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 goto out;
268 }
269 retval = mkpwent(p);
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000270out:
William Grzybowski23e65b22018-09-07 09:06:15 -0300271 PyMem_RawFree(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 Py_DECREF(bytes);
273 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000274}
275
Guido van Rossum1171ee61997-08-22 20:42:00 +0000276#ifdef HAVE_GETPWENT
Brett Cannon3d25e162014-08-22 14:03:51 -0400277/*[clinic input]
278pwd.getpwall
279
280Return a list of all available password database entries, in arbitrary order.
281
282See help(pwd) for more on password database entries.
283[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000284
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000285static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300286pwd_getpwall_impl(PyObject *module)
287/*[clinic end generated code: output=4853d2f5a0afac8a input=d7ecebfd90219b85]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 PyObject *d;
290 struct passwd *p;
291 if ((d = PyList_New(0)) == NULL)
292 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 setpwent();
294 while ((p = getpwent()) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 PyObject *v = mkpwent(p);
296 if (v == NULL || PyList_Append(d, v) != 0) {
297 Py_XDECREF(v);
298 Py_DECREF(d);
299 endpwent();
300 return NULL;
301 }
302 Py_DECREF(v);
303 }
304 endpwent();
305 return d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000306}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000307#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000308
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000309static PyMethodDef pwd_methods[] = {
Brett Cannon3d25e162014-08-22 14:03:51 -0400310 PWD_GETPWUID_METHODDEF
311 PWD_GETPWNAM_METHODDEF
Guido van Rossum1171ee61997-08-22 20:42:00 +0000312#ifdef HAVE_GETPWENT
Brett Cannon3d25e162014-08-22 14:03:51 -0400313 PWD_GETPWALL_METHODDEF
Guido van Rossum1171ee61997-08-22 20:42:00 +0000314#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 {NULL, NULL} /* sentinel */
Guido van Rossum864407d1991-04-10 19:48:25 +0000316};
317
Dino Viehlandb7f8e522019-09-10 13:59:43 +0100318static int pwdmodule_traverse(PyObject *m, visitproc visit, void *arg) {
319 Py_VISIT(modulestate(m)->StructPwdType);
320 return 0;
321}
322static int pwdmodule_clear(PyObject *m) {
323 Py_CLEAR(modulestate(m)->StructPwdType);
324 return 0;
325}
326static void pwdmodule_free(void *m) {
327 pwdmodule_clear((PyObject *)m);
328}
329
Martin v. Löwis1a214512008-06-11 05:26:20 +0000330static struct PyModuleDef pwdmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 PyModuleDef_HEAD_INIT,
332 "pwd",
333 pwd__doc__,
Dino Viehlandb7f8e522019-09-10 13:59:43 +0100334 sizeof(pwdmodulestate),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 pwd_methods,
336 NULL,
Dino Viehlandb7f8e522019-09-10 13:59:43 +0100337 pwdmodule_traverse,
338 pwdmodule_clear,
339 pwdmodule_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +0000340};
341
342
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000343PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000344PyInit_pwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 PyObject *m;
Dino Viehlandb7f8e522019-09-10 13:59:43 +0100347 if ((m = PyState_FindModule(&pwdmodule)) != NULL) {
348 Py_INCREF(m);
349 return m;
350 }
351 if ((m = PyModule_Create(&pwdmodule)) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 return NULL;
Fred Drake88c93442002-04-13 21:07:45 +0000353
Dino Viehlandb7f8e522019-09-10 13:59:43 +0100354 pwdmodulestate *state = PyModule_GetState(m);
355 state->StructPwdType = PyStructSequence_NewType(&struct_pwd_type_desc);
356 if (state->StructPwdType == NULL) {
357 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 }
Dino Viehlandb7f8e522019-09-10 13:59:43 +0100359 Py_INCREF(state->StructPwdType);
360 PyModule_AddObject(m, "struct_passwd", (PyObject *) state->StructPwdType);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 return m;
Guido van Rossum864407d1991-04-10 19:48:25 +0000362}