blob: 784e9d0349fa790dbd9ff5adb3cc4fb2d4525cd6 [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
Martin v. Löwis29275c92002-09-17 09:34:06 +000053static void
Neal Norwitzeb8b3a62007-08-24 23:26:23 +000054sets(PyObject *v, int i, const char* val)
Martin v. Löwis29275c92002-09-17 09:34:06 +000055{
Neal Norwitz3d7a90d2007-10-27 05:40:06 +000056 if (val) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 PyObject *o = PyUnicode_DecodeFSDefault(val);
58 PyStructSequence_SET_ITEM(v, i, o);
Neal Norwitz3d7a90d2007-10-27 05:40:06 +000059 }
Martin v. Löwis29275c92002-09-17 09:34:06 +000060 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 PyStructSequence_SET_ITEM(v, i, Py_None);
62 Py_INCREF(Py_None);
Martin v. Löwis29275c92002-09-17 09:34:06 +000063 }
64}
65
Barry Warsaw50c5cf11996-12-11 16:54:40 +000066static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000067mkpwent(struct passwd *p)
Guido van Rossum864407d1991-04-10 19:48:25 +000068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 int setIndex = 0;
70 PyObject *v = PyStructSequence_New(&StructPwdType);
71 if (v == NULL)
72 return NULL;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000073
Christian Heimes217cfd12007-12-02 14:31:20 +000074#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
Martin v. Löwis29275c92002-09-17 09:34:06 +000075#define SETS(i,val) sets(v, i, val)
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 SETS(setIndex++, p->pw_name);
Stefan Krah45009772016-04-26 11:43:21 +020078#if defined(HAVE_STRUCT_PASSWD_PW_PASSWD) && !defined(__ANDROID__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 SETS(setIndex++, p->pw_passwd);
Stefan Krah267b6392016-04-26 01:09:18 +020080#else
81 SETS(setIndex++, "");
82#endif
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020083 PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
84 PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
Stefan Krah267b6392016-04-26 01:09:18 +020085#if defined(HAVE_STRUCT_PASSWD_PW_GECOS)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 SETS(setIndex++, p->pw_gecos);
Stefan Krah267b6392016-04-26 01:09:18 +020087#else
88 SETS(setIndex++, "");
89#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 SETS(setIndex++, p->pw_dir);
91 SETS(setIndex++, p->pw_shell);
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000092
93#undef SETS
94#undef SETI
95
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 if (PyErr_Occurred()) {
97 Py_XDECREF(v);
98 return NULL;
99 }
Martin v. Löwisdbd55b32002-03-01 10:38:44 +0000100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 return v;
Guido van Rossum864407d1991-04-10 19:48:25 +0000102}
103
Brett Cannon3d25e162014-08-22 14:03:51 -0400104/*[clinic input]
105pwd.getpwuid
106
107 uidobj: object
108 /
109
110Return the password database entry for the given numeric user ID.
111
112See `help(pwd)` for more on password database entries.
113[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000114
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000115static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300116pwd_getpwuid(PyObject *module, PyObject *uidobj)
117/*[clinic end generated code: output=c4ee1d4d429b86c4 input=ae64d507a1c6d3e8]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000118{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200119 uid_t uid;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 struct passwd *p;
Brett Cannon3d25e162014-08-22 14:03:51 -0400121
122 if (!_Py_Uid_Converter(uidobj, &uid)) {
Serhiy Storchaka55e22382013-02-11 20:32:47 +0200123 if (PyErr_ExceptionMatches(PyExc_OverflowError))
124 PyErr_Format(PyExc_KeyError,
125 "getpwuid(): uid not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 return NULL;
Serhiy Storchaka55e22382013-02-11 20:32:47 +0200127 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 if ((p = getpwuid(uid)) == NULL) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200129 PyObject *uid_obj = _PyLong_FromUid(uid);
130 if (uid_obj == NULL)
131 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 PyErr_Format(PyExc_KeyError,
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200133 "getpwuid(): uid not found: %S", uid_obj);
134 Py_DECREF(uid_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 return NULL;
136 }
137 return mkpwent(p);
Guido van Rossum864407d1991-04-10 19:48:25 +0000138}
139
Brett Cannon3d25e162014-08-22 14:03:51 -0400140/*[clinic input]
141pwd.getpwnam
142
143 arg: unicode
144 /
145
146Return the password database entry for the given user name.
147
148See `help(pwd)` for more on password database entries.
149[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000150
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000151static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300152pwd_getpwnam_impl(PyObject *module, PyObject *arg)
153/*[clinic end generated code: output=6abeee92430e43d2 input=d5f7e700919b02d3]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 char *name;
156 struct passwd *p;
Brett Cannon3d25e162014-08-22 14:03:51 -0400157 PyObject *bytes, *retval = NULL;
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000158
Victor Stinnerae6265f2010-05-15 16:27:27 +0000159 if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 return NULL;
161 if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
162 goto out;
163 if ((p = getpwnam(name)) == NULL) {
164 PyErr_Format(PyExc_KeyError,
165 "getpwnam(): name not found: %s", name);
166 goto out;
167 }
168 retval = mkpwent(p);
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000169out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 Py_DECREF(bytes);
171 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000172}
173
Guido van Rossum1171ee61997-08-22 20:42:00 +0000174#ifdef HAVE_GETPWENT
Brett Cannon3d25e162014-08-22 14:03:51 -0400175/*[clinic input]
176pwd.getpwall
177
178Return a list of all available password database entries, in arbitrary order.
179
180See help(pwd) for more on password database entries.
181[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000182
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000183static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300184pwd_getpwall_impl(PyObject *module)
185/*[clinic end generated code: output=4853d2f5a0afac8a input=d7ecebfd90219b85]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 PyObject *d;
188 struct passwd *p;
189 if ((d = PyList_New(0)) == NULL)
190 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 setpwent();
192 while ((p = getpwent()) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 PyObject *v = mkpwent(p);
194 if (v == NULL || PyList_Append(d, v) != 0) {
195 Py_XDECREF(v);
196 Py_DECREF(d);
197 endpwent();
198 return NULL;
199 }
200 Py_DECREF(v);
201 }
202 endpwent();
203 return d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000204}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000205#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000206
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000207static PyMethodDef pwd_methods[] = {
Brett Cannon3d25e162014-08-22 14:03:51 -0400208 PWD_GETPWUID_METHODDEF
209 PWD_GETPWNAM_METHODDEF
Guido van Rossum1171ee61997-08-22 20:42:00 +0000210#ifdef HAVE_GETPWENT
Brett Cannon3d25e162014-08-22 14:03:51 -0400211 PWD_GETPWALL_METHODDEF
Guido van Rossum1171ee61997-08-22 20:42:00 +0000212#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 {NULL, NULL} /* sentinel */
Guido van Rossum864407d1991-04-10 19:48:25 +0000214};
215
Martin v. Löwis1a214512008-06-11 05:26:20 +0000216static struct PyModuleDef pwdmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 PyModuleDef_HEAD_INIT,
218 "pwd",
219 pwd__doc__,
220 -1,
221 pwd_methods,
222 NULL,
223 NULL,
224 NULL,
225 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000226};
227
228
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000229PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000230PyInit_pwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 PyObject *m;
233 m = PyModule_Create(&pwdmodule);
234 if (m == NULL)
235 return NULL;
Fred Drake88c93442002-04-13 21:07:45 +0000236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 if (!initialized) {
Victor Stinner1c8f0592013-07-22 22:24:54 +0200238 if (PyStructSequence_InitType2(&StructPwdType,
239 &struct_pwd_type_desc) < 0)
240 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 initialized = 1;
242 }
243 Py_INCREF((PyObject *) &StructPwdType);
244 PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
245 return m;
Guido van Rossum864407d1991-04-10 19:48:25 +0000246}