blob: 281c30b26ee88f3715e036d2d607a77a95700964 [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);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 SETS(setIndex++, p->pw_passwd);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020079 PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
80 PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 SETS(setIndex++, p->pw_gecos);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 SETS(setIndex++, p->pw_dir);
83 SETS(setIndex++, p->pw_shell);
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000084
85#undef SETS
86#undef SETI
87
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 if (PyErr_Occurred()) {
89 Py_XDECREF(v);
90 return NULL;
91 }
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 return v;
Guido van Rossum864407d1991-04-10 19:48:25 +000094}
95
Brett Cannon3d25e162014-08-22 14:03:51 -040096/*[clinic input]
97pwd.getpwuid
98
99 uidobj: object
100 /
101
102Return the password database entry for the given numeric user ID.
103
104See `help(pwd)` for more on password database entries.
105[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000106
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000107static PyObject *
Brett Cannon3d25e162014-08-22 14:03:51 -0400108pwd_getpwuid(PyModuleDef *module, PyObject *uidobj)
109/*[clinic end generated code: output=cba29ae4c2bcb8e1 input=ae64d507a1c6d3e8]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000110{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200111 uid_t uid;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 struct passwd *p;
Brett Cannon3d25e162014-08-22 14:03:51 -0400113
114 if (!_Py_Uid_Converter(uidobj, &uid)) {
Serhiy Storchaka55e22382013-02-11 20:32:47 +0200115 if (PyErr_ExceptionMatches(PyExc_OverflowError))
116 PyErr_Format(PyExc_KeyError,
117 "getpwuid(): uid not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 return NULL;
Serhiy Storchaka55e22382013-02-11 20:32:47 +0200119 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 if ((p = getpwuid(uid)) == NULL) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200121 PyObject *uid_obj = _PyLong_FromUid(uid);
122 if (uid_obj == NULL)
123 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 PyErr_Format(PyExc_KeyError,
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200125 "getpwuid(): uid not found: %S", uid_obj);
126 Py_DECREF(uid_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 return NULL;
128 }
129 return mkpwent(p);
Guido van Rossum864407d1991-04-10 19:48:25 +0000130}
131
Brett Cannon3d25e162014-08-22 14:03:51 -0400132/*[clinic input]
133pwd.getpwnam
134
135 arg: unicode
136 /
137
138Return the password database entry for the given user name.
139
140See `help(pwd)` for more on password database entries.
141[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000142
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000143static PyObject *
Brett Cannon3d25e162014-08-22 14:03:51 -0400144pwd_getpwnam_impl(PyModuleDef *module, PyObject *arg)
145/*[clinic end generated code: output=66848d42d386fca3 input=d5f7e700919b02d3]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 char *name;
148 struct passwd *p;
Brett Cannon3d25e162014-08-22 14:03:51 -0400149 PyObject *bytes, *retval = NULL;
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000150
Victor Stinnerae6265f2010-05-15 16:27:27 +0000151 if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 return NULL;
153 if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
154 goto out;
155 if ((p = getpwnam(name)) == NULL) {
156 PyErr_Format(PyExc_KeyError,
157 "getpwnam(): name not found: %s", name);
158 goto out;
159 }
160 retval = mkpwent(p);
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000161out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 Py_DECREF(bytes);
163 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000164}
165
Guido van Rossum1171ee61997-08-22 20:42:00 +0000166#ifdef HAVE_GETPWENT
Brett Cannon3d25e162014-08-22 14:03:51 -0400167/*[clinic input]
168pwd.getpwall
169
170Return a list of all available password database entries, in arbitrary order.
171
172See help(pwd) for more on password database entries.
173[clinic start generated code]*/
Guido van Rossum3e79c441998-03-03 22:03:26 +0000174
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000175static PyObject *
Brett Cannon3d25e162014-08-22 14:03:51 -0400176pwd_getpwall_impl(PyModuleDef *module)
177/*[clinic end generated code: output=ab30e37bf26d431d input=d7ecebfd90219b85]*/
Guido van Rossum864407d1991-04-10 19:48:25 +0000178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 PyObject *d;
180 struct passwd *p;
181 if ((d = PyList_New(0)) == NULL)
182 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 setpwent();
184 while ((p = getpwent()) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 PyObject *v = mkpwent(p);
186 if (v == NULL || PyList_Append(d, v) != 0) {
187 Py_XDECREF(v);
188 Py_DECREF(d);
189 endpwent();
190 return NULL;
191 }
192 Py_DECREF(v);
193 }
194 endpwent();
195 return d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000196}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000197#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000198
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000199static PyMethodDef pwd_methods[] = {
Brett Cannon3d25e162014-08-22 14:03:51 -0400200 PWD_GETPWUID_METHODDEF
201 PWD_GETPWNAM_METHODDEF
Guido van Rossum1171ee61997-08-22 20:42:00 +0000202#ifdef HAVE_GETPWENT
Brett Cannon3d25e162014-08-22 14:03:51 -0400203 PWD_GETPWALL_METHODDEF
Guido van Rossum1171ee61997-08-22 20:42:00 +0000204#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 {NULL, NULL} /* sentinel */
Guido van Rossum864407d1991-04-10 19:48:25 +0000206};
207
Martin v. Löwis1a214512008-06-11 05:26:20 +0000208static struct PyModuleDef pwdmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 PyModuleDef_HEAD_INIT,
210 "pwd",
211 pwd__doc__,
212 -1,
213 pwd_methods,
214 NULL,
215 NULL,
216 NULL,
217 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000218};
219
220
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000221PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000222PyInit_pwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 PyObject *m;
225 m = PyModule_Create(&pwdmodule);
226 if (m == NULL)
227 return NULL;
Fred Drake88c93442002-04-13 21:07:45 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if (!initialized) {
Victor Stinner1c8f0592013-07-22 22:24:54 +0200230 if (PyStructSequence_InitType2(&StructPwdType,
231 &struct_pwd_type_desc) < 0)
232 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 initialized = 1;
234 }
235 Py_INCREF((PyObject *) &StructPwdType);
236 PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
237 return m;
Guido van Rossum864407d1991-04-10 19:48:25 +0000238}