blob: e0232b8d589baa844f40b21ceda194ac1c20871e [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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000050static int initialized;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000051static PyTypeObject StructPwdType;
52
William Grzybowski23e65b22018-09-07 09:06:15 -030053#define DEFAULT_BUFFER_SIZE 1024
54
Martin v. Löwis29275c92002-09-17 09:34:06 +000055static void
Neal Norwitzeb8b3a62007-08-24 23:26:23 +000056sets(PyObject *v, int i, const char* val)
Martin v. Löwis29275c92002-09-17 09:34:06 +000057{
Neal Norwitz3d7a90d2007-10-27 05:40:06 +000058 if (val) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 PyObject *o = PyUnicode_DecodeFSDefault(val);
60 PyStructSequence_SET_ITEM(v, i, o);
Neal Norwitz3d7a90d2007-10-27 05:40:06 +000061 }
Martin v. Löwis29275c92002-09-17 09:34:06 +000062 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 PyStructSequence_SET_ITEM(v, i, Py_None);
64 Py_INCREF(Py_None);
Martin v. Löwis29275c92002-09-17 09:34:06 +000065 }
66}
67
Barry Warsaw50c5cf11996-12-11 16:54:40 +000068static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000069mkpwent(struct passwd *p)
Guido van Rossum864407d1991-04-10 19:48:25 +000070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 int setIndex = 0;
72 PyObject *v = PyStructSequence_New(&StructPwdType);
73 if (v == NULL)
74 return NULL;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000075
Christian Heimes217cfd12007-12-02 14:31:20 +000076#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
Martin v. Löwis29275c92002-09-17 09:34:06 +000077#define SETS(i,val) sets(v, i, val)
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 SETS(setIndex++, p->pw_name);
Stefan Krah45009772016-04-26 11:43:21 +020080#if defined(HAVE_STRUCT_PASSWD_PW_PASSWD) && !defined(__ANDROID__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 SETS(setIndex++, p->pw_passwd);
Stefan Krah267b6392016-04-26 01:09:18 +020082#else
83 SETS(setIndex++, "");
84#endif
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020085 PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
86 PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
Stefan Krah267b6392016-04-26 01:09:18 +020087#if defined(HAVE_STRUCT_PASSWD_PW_GECOS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 SETS(setIndex++, p->pw_gecos);
Stefan Krah267b6392016-04-26 01:09:18 +020089#else
90 SETS(setIndex++, "");
91#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 SETS(setIndex++, p->pw_dir);
93 SETS(setIndex++, p->pw_shell);
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000094
95#undef SETS
96#undef SETI
97
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 if (PyErr_Occurred()) {
99 Py_XDECREF(v);
100 return NULL;
101 }
Martin v. Löwisdbd55b32002-03-01 10:38:44 +0000102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 return v;
Guido van Rossum864407d1991-04-10 19:48:25 +0000104}
105
Brett Cannon3d25e162014-08-22 14:03:51 -0400106/*[clinic input]
107pwd.getpwuid
108
109 uidobj: object
110 /
111
112Return the password database entry for the given numeric user ID.
113
114See `help(pwd)` for more on password database entries.
115[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000116
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000117static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300118pwd_getpwuid(PyObject *module, PyObject *uidobj)
119/*[clinic end generated code: output=c4ee1d4d429b86c4 input=ae64d507a1c6d3e8]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000120{
William Grzybowski23e65b22018-09-07 09:06:15 -0300121 PyObject *retval = NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200122 uid_t uid;
William Grzybowski23e65b22018-09-07 09:06:15 -0300123 int nomem = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 struct passwd *p;
William Grzybowski23e65b22018-09-07 09:06:15 -0300125 char *buf = NULL, *buf2 = NULL;
Brett Cannon3d25e162014-08-22 14:03:51 -0400126
127 if (!_Py_Uid_Converter(uidobj, &uid)) {
Serhiy Storchaka55e22382013-02-11 20:32:47 +0200128 if (PyErr_ExceptionMatches(PyExc_OverflowError))
129 PyErr_Format(PyExc_KeyError,
130 "getpwuid(): uid not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 return NULL;
Serhiy Storchaka55e22382013-02-11 20:32:47 +0200132 }
William Grzybowski23e65b22018-09-07 09:06:15 -0300133#ifdef HAVE_GETPWUID_R
William Grzybowski23e65b22018-09-07 09:06:15 -0300134 int status;
135 Py_ssize_t bufsize;
Alexey Izbysheve359bc22018-11-04 18:44:16 +0300136 /* Note: 'pwd' will be used via pointer 'p' on getpwuid_r success. */
William Grzybowski23e65b22018-09-07 09:06:15 -0300137 struct passwd pwd;
138
Alexey Izbysheve359bc22018-11-04 18:44:16 +0300139 Py_BEGIN_ALLOW_THREADS
William Grzybowski23e65b22018-09-07 09:06:15 -0300140 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
141 if (bufsize == -1) {
142 bufsize = DEFAULT_BUFFER_SIZE;
143 }
144
145 while(1) {
146 buf2 = PyMem_RawRealloc(buf, bufsize);
147 if (buf2 == NULL) {
Zackery Spytz570e3712018-11-05 12:26:40 -0700148 p = NULL;
William Grzybowski23e65b22018-09-07 09:06:15 -0300149 nomem = 1;
150 break;
151 }
152 buf = buf2;
153 status = getpwuid_r(uid, &pwd, buf, bufsize, &p);
154 if (status != 0) {
155 p = NULL;
156 }
157 if (p != NULL || status != ERANGE) {
158 break;
159 }
160 if (bufsize > (PY_SSIZE_T_MAX >> 1)) {
161 nomem = 1;
162 break;
163 }
164 bufsize <<= 1;
165 }
166
167 Py_END_ALLOW_THREADS
168#else
169 p = getpwuid(uid);
170#endif
171 if (p == NULL) {
172 PyMem_RawFree(buf);
173 if (nomem == 1) {
174 return PyErr_NoMemory();
175 }
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200176 PyObject *uid_obj = _PyLong_FromUid(uid);
177 if (uid_obj == NULL)
178 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 PyErr_Format(PyExc_KeyError,
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200180 "getpwuid(): uid not found: %S", uid_obj);
181 Py_DECREF(uid_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 return NULL;
183 }
William Grzybowski23e65b22018-09-07 09:06:15 -0300184 retval = mkpwent(p);
185#ifdef HAVE_GETPWUID_R
186 PyMem_RawFree(buf);
187#endif
188 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000189}
190
Brett Cannon3d25e162014-08-22 14:03:51 -0400191/*[clinic input]
192pwd.getpwnam
193
William Grzybowski28658482018-09-07 14:10:39 -0300194 name: unicode
Brett Cannon3d25e162014-08-22 14:03:51 -0400195 /
196
197Return the password database entry for the given user name.
198
199See `help(pwd)` for more on password database entries.
200[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000201
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000202static PyObject *
William Grzybowski28658482018-09-07 14:10:39 -0300203pwd_getpwnam_impl(PyObject *module, PyObject *name)
204/*[clinic end generated code: output=359ce1ddeb7a824f input=a6aeb5e3447fb9e0]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000205{
William Grzybowski28658482018-09-07 14:10:39 -0300206 char *buf = NULL, *buf2 = NULL, *name_chars;
William Grzybowski23e65b22018-09-07 09:06:15 -0300207 int nomem = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 struct passwd *p;
Brett Cannon3d25e162014-08-22 14:03:51 -0400209 PyObject *bytes, *retval = NULL;
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000210
William Grzybowski28658482018-09-07 14:10:39 -0300211 if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 return NULL;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300213 /* check for embedded null bytes */
William Grzybowski28658482018-09-07 14:10:39 -0300214 if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 goto out;
William Grzybowski23e65b22018-09-07 09:06:15 -0300216#ifdef HAVE_GETPWNAM_R
William Grzybowski23e65b22018-09-07 09:06:15 -0300217 int status;
218 Py_ssize_t bufsize;
Alexey Izbysheve359bc22018-11-04 18:44:16 +0300219 /* Note: 'pwd' will be used via pointer 'p' on getpwnam_r success. */
William Grzybowski23e65b22018-09-07 09:06:15 -0300220 struct passwd pwd;
221
Alexey Izbysheve359bc22018-11-04 18:44:16 +0300222 Py_BEGIN_ALLOW_THREADS
William Grzybowski23e65b22018-09-07 09:06:15 -0300223 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
224 if (bufsize == -1) {
225 bufsize = DEFAULT_BUFFER_SIZE;
226 }
227
228 while(1) {
229 buf2 = PyMem_RawRealloc(buf, bufsize);
230 if (buf2 == NULL) {
Zackery Spytz570e3712018-11-05 12:26:40 -0700231 p = NULL;
William Grzybowski23e65b22018-09-07 09:06:15 -0300232 nomem = 1;
233 break;
234 }
235 buf = buf2;
William Grzybowski28658482018-09-07 14:10:39 -0300236 status = getpwnam_r(name_chars, &pwd, buf, bufsize, &p);
William Grzybowski23e65b22018-09-07 09:06:15 -0300237 if (status != 0) {
238 p = NULL;
239 }
240 if (p != NULL || status != ERANGE) {
241 break;
242 }
243 if (bufsize > (PY_SSIZE_T_MAX >> 1)) {
244 nomem = 1;
245 break;
246 }
247 bufsize <<= 1;
248 }
249
250 Py_END_ALLOW_THREADS
251#else
William Grzybowski28658482018-09-07 14:10:39 -0300252 p = getpwnam(name_chars);
William Grzybowski23e65b22018-09-07 09:06:15 -0300253#endif
254 if (p == NULL) {
255 if (nomem == 1) {
256 PyErr_NoMemory();
257 }
258 else {
259 PyErr_Format(PyExc_KeyError,
William Grzybowski34c7f0c2018-12-05 17:10:18 -0200260 "getpwnam(): name not found: %R", name);
William Grzybowski23e65b22018-09-07 09:06:15 -0300261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 goto out;
263 }
264 retval = mkpwent(p);
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000265out:
William Grzybowski23e65b22018-09-07 09:06:15 -0300266 PyMem_RawFree(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 Py_DECREF(bytes);
268 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000269}
270
Guido van Rossum1171ee61997-08-22 20:42:00 +0000271#ifdef HAVE_GETPWENT
Brett Cannon3d25e162014-08-22 14:03:51 -0400272/*[clinic input]
273pwd.getpwall
274
275Return a list of all available password database entries, in arbitrary order.
276
277See help(pwd) for more on password database entries.
278[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000279
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000280static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300281pwd_getpwall_impl(PyObject *module)
282/*[clinic end generated code: output=4853d2f5a0afac8a input=d7ecebfd90219b85]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 PyObject *d;
285 struct passwd *p;
286 if ((d = PyList_New(0)) == NULL)
287 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 setpwent();
289 while ((p = getpwent()) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 PyObject *v = mkpwent(p);
291 if (v == NULL || PyList_Append(d, v) != 0) {
292 Py_XDECREF(v);
293 Py_DECREF(d);
294 endpwent();
295 return NULL;
296 }
297 Py_DECREF(v);
298 }
299 endpwent();
300 return d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000301}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000302#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000303
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000304static PyMethodDef pwd_methods[] = {
Brett Cannon3d25e162014-08-22 14:03:51 -0400305 PWD_GETPWUID_METHODDEF
306 PWD_GETPWNAM_METHODDEF
Guido van Rossum1171ee61997-08-22 20:42:00 +0000307#ifdef HAVE_GETPWENT
Brett Cannon3d25e162014-08-22 14:03:51 -0400308 PWD_GETPWALL_METHODDEF
Guido van Rossum1171ee61997-08-22 20:42:00 +0000309#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 {NULL, NULL} /* sentinel */
Guido van Rossum864407d1991-04-10 19:48:25 +0000311};
312
Martin v. Löwis1a214512008-06-11 05:26:20 +0000313static struct PyModuleDef pwdmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 PyModuleDef_HEAD_INIT,
315 "pwd",
316 pwd__doc__,
317 -1,
318 pwd_methods,
319 NULL,
320 NULL,
321 NULL,
322 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000323};
324
325
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000326PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000327PyInit_pwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 PyObject *m;
330 m = PyModule_Create(&pwdmodule);
331 if (m == NULL)
332 return NULL;
Fred Drake88c93442002-04-13 21:07:45 +0000333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 if (!initialized) {
Victor Stinner1c8f0592013-07-22 22:24:54 +0200335 if (PyStructSequence_InitType2(&StructPwdType,
336 &struct_pwd_type_desc) < 0)
337 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 initialized = 1;
339 }
340 Py_INCREF((PyObject *) &StructPwdType);
341 PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
342 return m;
Guido van Rossum864407d1991-04-10 19:48:25 +0000343}