blob: 36192a5704331d59c6e69040e58d2e7604f76928 [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
134 Py_BEGIN_ALLOW_THREADS
135 int status;
136 Py_ssize_t bufsize;
137 struct passwd pwd;
138
139 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
140 if (bufsize == -1) {
141 bufsize = DEFAULT_BUFFER_SIZE;
142 }
143
144 while(1) {
145 buf2 = PyMem_RawRealloc(buf, bufsize);
146 if (buf2 == NULL) {
147 nomem = 1;
148 break;
149 }
150 buf = buf2;
151 status = getpwuid_r(uid, &pwd, buf, bufsize, &p);
152 if (status != 0) {
153 p = NULL;
154 }
155 if (p != NULL || status != ERANGE) {
156 break;
157 }
158 if (bufsize > (PY_SSIZE_T_MAX >> 1)) {
159 nomem = 1;
160 break;
161 }
162 bufsize <<= 1;
163 }
164
165 Py_END_ALLOW_THREADS
166#else
167 p = getpwuid(uid);
168#endif
169 if (p == NULL) {
170 PyMem_RawFree(buf);
171 if (nomem == 1) {
172 return PyErr_NoMemory();
173 }
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200174 PyObject *uid_obj = _PyLong_FromUid(uid);
175 if (uid_obj == NULL)
176 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 PyErr_Format(PyExc_KeyError,
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200178 "getpwuid(): uid not found: %S", uid_obj);
179 Py_DECREF(uid_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 return NULL;
181 }
William Grzybowski23e65b22018-09-07 09:06:15 -0300182 retval = mkpwent(p);
183#ifdef HAVE_GETPWUID_R
184 PyMem_RawFree(buf);
185#endif
186 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000187}
188
Brett Cannon3d25e162014-08-22 14:03:51 -0400189/*[clinic input]
190pwd.getpwnam
191
192 arg: unicode
193 /
194
195Return the password database entry for the given user name.
196
197See `help(pwd)` for more on password database entries.
198[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000199
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000200static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300201pwd_getpwnam_impl(PyObject *module, PyObject *arg)
202/*[clinic end generated code: output=6abeee92430e43d2 input=d5f7e700919b02d3]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000203{
William Grzybowski23e65b22018-09-07 09:06:15 -0300204 char *buf = NULL, *buf2 = NULL, *name;
205 int nomem = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 struct passwd *p;
Brett Cannon3d25e162014-08-22 14:03:51 -0400207 PyObject *bytes, *retval = NULL;
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000208
Victor Stinnerae6265f2010-05-15 16:27:27 +0000209 if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 return NULL;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300211 /* check for embedded null bytes */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
213 goto out;
William Grzybowski23e65b22018-09-07 09:06:15 -0300214#ifdef HAVE_GETPWNAM_R
215 Py_BEGIN_ALLOW_THREADS
216 int status;
217 Py_ssize_t bufsize;
218 struct passwd pwd;
219
220 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
221 if (bufsize == -1) {
222 bufsize = DEFAULT_BUFFER_SIZE;
223 }
224
225 while(1) {
226 buf2 = PyMem_RawRealloc(buf, bufsize);
227 if (buf2 == NULL) {
228 nomem = 1;
229 break;
230 }
231 buf = buf2;
232 status = getpwnam_r(name, &pwd, buf, bufsize, &p);
233 if (status != 0) {
234 p = NULL;
235 }
236 if (p != NULL || status != ERANGE) {
237 break;
238 }
239 if (bufsize > (PY_SSIZE_T_MAX >> 1)) {
240 nomem = 1;
241 break;
242 }
243 bufsize <<= 1;
244 }
245
246 Py_END_ALLOW_THREADS
247#else
248 p = getpwnam(name);
249#endif
250 if (p == NULL) {
251 if (nomem == 1) {
252 PyErr_NoMemory();
253 }
254 else {
255 PyErr_Format(PyExc_KeyError,
256 "getpwnam(): name not found: %s", name);
257 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 goto out;
259 }
260 retval = mkpwent(p);
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000261out:
William Grzybowski23e65b22018-09-07 09:06:15 -0300262 PyMem_RawFree(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 Py_DECREF(bytes);
264 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000265}
266
Guido van Rossum1171ee61997-08-22 20:42:00 +0000267#ifdef HAVE_GETPWENT
Brett Cannon3d25e162014-08-22 14:03:51 -0400268/*[clinic input]
269pwd.getpwall
270
271Return a list of all available password database entries, in arbitrary order.
272
273See help(pwd) for more on password database entries.
274[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000275
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000276static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300277pwd_getpwall_impl(PyObject *module)
278/*[clinic end generated code: output=4853d2f5a0afac8a input=d7ecebfd90219b85]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 PyObject *d;
281 struct passwd *p;
282 if ((d = PyList_New(0)) == NULL)
283 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 setpwent();
285 while ((p = getpwent()) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 PyObject *v = mkpwent(p);
287 if (v == NULL || PyList_Append(d, v) != 0) {
288 Py_XDECREF(v);
289 Py_DECREF(d);
290 endpwent();
291 return NULL;
292 }
293 Py_DECREF(v);
294 }
295 endpwent();
296 return d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000297}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000298#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000299
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000300static PyMethodDef pwd_methods[] = {
Brett Cannon3d25e162014-08-22 14:03:51 -0400301 PWD_GETPWUID_METHODDEF
302 PWD_GETPWNAM_METHODDEF
Guido van Rossum1171ee61997-08-22 20:42:00 +0000303#ifdef HAVE_GETPWENT
Brett Cannon3d25e162014-08-22 14:03:51 -0400304 PWD_GETPWALL_METHODDEF
Guido van Rossum1171ee61997-08-22 20:42:00 +0000305#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 {NULL, NULL} /* sentinel */
Guido van Rossum864407d1991-04-10 19:48:25 +0000307};
308
Martin v. Löwis1a214512008-06-11 05:26:20 +0000309static struct PyModuleDef pwdmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 PyModuleDef_HEAD_INIT,
311 "pwd",
312 pwd__doc__,
313 -1,
314 pwd_methods,
315 NULL,
316 NULL,
317 NULL,
318 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000319};
320
321
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000322PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000323PyInit_pwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 PyObject *m;
326 m = PyModule_Create(&pwdmodule);
327 if (m == NULL)
328 return NULL;
Fred Drake88c93442002-04-13 21:07:45 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 if (!initialized) {
Victor Stinner1c8f0592013-07-22 22:24:54 +0200331 if (PyStructSequence_InitType2(&StructPwdType,
332 &struct_pwd_type_desc) < 0)
333 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 initialized = 1;
335 }
336 Py_INCREF((PyObject *) &StructPwdType);
337 PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
338 return m;
Guido van Rossum864407d1991-04-10 19:48:25 +0000339}