blob: 285fd9c98431d6f6058bd0db0191d9c21b585cbf [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 Storchakae4ad8aa2013-02-12 09:24:16 +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
Martin v. Löwisdbd55b32002-03-01 10:38:44 +00009static PyStructSequence_Field struct_pwd_type_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000010 {"pw_name", "user name"},
11 {"pw_passwd", "password"},
12 {"pw_uid", "user id"},
13 {"pw_gid", "group id"},
14 {"pw_gecos", "real name"},
15 {"pw_dir", "home directory"},
16 {"pw_shell", "shell program"},
17 {0}
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000018};
19
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000020PyDoc_STRVAR(struct_passwd__doc__,
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000021"pwd.struct_passwd: Results from getpw*() routines.\n\n\
22This object may be accessed either as a tuple of\n\
23 (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000024or via the object attributes as named in the above tuple.");
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000025
26static PyStructSequence_Desc struct_pwd_type_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 "pwd.struct_passwd",
28 struct_passwd__doc__,
29 struct_pwd_type_fields,
30 7,
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000031};
32
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000033PyDoc_STRVAR(pwd__doc__,
34"This module provides access to the Unix password database.\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +000035It is available on all Unix versions.\n\
36\n\
37Password database entries are reported as 7-tuples containing the following\n\
38items from the password database (see `<pwd.h>'), in order:\n\
39pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
40The uid and gid items are integers, all others are strings. An\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041exception is raised if the entry asked for cannot be found.");
Guido van Rossum3e79c441998-03-03 22:03:26 +000042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000044static int initialized;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000045static PyTypeObject StructPwdType;
46
Martin v. Löwis29275c92002-09-17 09:34:06 +000047static void
Neal Norwitzeb8b3a62007-08-24 23:26:23 +000048sets(PyObject *v, int i, const char* val)
Martin v. Löwis29275c92002-09-17 09:34:06 +000049{
Neal Norwitz3d7a90d2007-10-27 05:40:06 +000050 if (val) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 PyObject *o = PyUnicode_DecodeFSDefault(val);
52 PyStructSequence_SET_ITEM(v, i, o);
Neal Norwitz3d7a90d2007-10-27 05:40:06 +000053 }
Martin v. Löwis29275c92002-09-17 09:34:06 +000054 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 PyStructSequence_SET_ITEM(v, i, Py_None);
56 Py_INCREF(Py_None);
Martin v. Löwis29275c92002-09-17 09:34:06 +000057 }
58}
59
Barry Warsaw50c5cf11996-12-11 16:54:40 +000060static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000061mkpwent(struct passwd *p)
Guido van Rossum864407d1991-04-10 19:48:25 +000062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 int setIndex = 0;
64 PyObject *v = PyStructSequence_New(&StructPwdType);
65 if (v == NULL)
66 return NULL;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000067
Christian Heimes217cfd12007-12-02 14:31:20 +000068#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
Martin v. Löwis29275c92002-09-17 09:34:06 +000069#define SETS(i,val) sets(v, i, val)
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 SETS(setIndex++, p->pw_name);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000072#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 SETS(setIndex++, "");
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000074#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 SETS(setIndex++, p->pw_passwd);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000076#endif
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +020077 PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
78 PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000079#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 SETS(setIndex++, "");
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000081#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 SETS(setIndex++, p->pw_gecos);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000083#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 SETS(setIndex++, p->pw_dir);
85 SETS(setIndex++, p->pw_shell);
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000086
87#undef SETS
88#undef SETI
89
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 if (PyErr_Occurred()) {
91 Py_XDECREF(v);
92 return NULL;
93 }
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 return v;
Guido van Rossum864407d1991-04-10 19:48:25 +000096}
97
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000098PyDoc_STRVAR(pwd_getpwuid__doc__,
99"getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
100 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000101Return the password database entry for the given numeric user ID.\n\
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000102See help(pwd) for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000103
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000104static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000105pwd_getpwuid(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000106{
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +0200107 uid_t uid;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 struct passwd *p;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +0200109 if (!PyArg_ParseTuple(args, "O&:getpwuid", _Py_Uid_Converter, &uid)) {
110 if (PyErr_ExceptionMatches(PyExc_OverflowError))
111 PyErr_Format(PyExc_KeyError,
112 "getpwuid(): uid not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 return NULL;
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +0200114 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 if ((p = getpwuid(uid)) == NULL) {
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +0200116 PyObject *uid_obj = _PyLong_FromUid(uid);
117 if (uid_obj == NULL)
118 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 PyErr_Format(PyExc_KeyError,
Serhiy Storchakae4ad8aa2013-02-12 09:24:16 +0200120 "getpwuid(): uid not found: %S", uid_obj);
121 Py_DECREF(uid_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 return NULL;
123 }
124 return mkpwent(p);
Guido van Rossum864407d1991-04-10 19:48:25 +0000125}
126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000127PyDoc_STRVAR(pwd_getpwnam__doc__,
128"getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
129 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000130Return the password database entry for the given user name.\n\
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000131See help(pwd) for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000132
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000133static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000134pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 char *name;
137 struct passwd *p;
138 PyObject *arg, *bytes, *retval = NULL;
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 if (!PyArg_ParseTuple(args, "U:getpwnam", &arg))
141 return NULL;
Victor Stinnerae6265f2010-05-15 16:27:27 +0000142 if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 return NULL;
144 if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
145 goto out;
146 if ((p = getpwnam(name)) == NULL) {
147 PyErr_Format(PyExc_KeyError,
148 "getpwnam(): name not found: %s", name);
149 goto out;
150 }
151 retval = mkpwent(p);
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000152out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 Py_DECREF(bytes);
154 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000155}
156
Guido van Rossum1171ee61997-08-22 20:42:00 +0000157#ifdef HAVE_GETPWENT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000158PyDoc_STRVAR(pwd_getpwall__doc__,
159"getpwall() -> list_of_entries\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000160Return a list of all available password database entries, \
161in arbitrary order.\n\
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000162See help(pwd) for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000163
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000164static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000165pwd_getpwall(PyObject *self)
Guido van Rossum864407d1991-04-10 19:48:25 +0000166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 PyObject *d;
168 struct passwd *p;
169 if ((d = PyList_New(0)) == NULL)
170 return NULL;
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000171#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 if ((p = getpwuid(0)) != NULL) {
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000173#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 setpwent();
175 while ((p = getpwent()) != NULL) {
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000176#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 PyObject *v = mkpwent(p);
178 if (v == NULL || PyList_Append(d, v) != 0) {
179 Py_XDECREF(v);
180 Py_DECREF(d);
181 endpwent();
182 return NULL;
183 }
184 Py_DECREF(v);
185 }
186 endpwent();
187 return d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000188}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000189#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000190
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000191static PyMethodDef pwd_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
193 {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000194#ifdef HAVE_GETPWENT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 {"getpwall", (PyCFunction)pwd_getpwall,
196 METH_NOARGS, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000197#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 {NULL, NULL} /* sentinel */
Guido van Rossum864407d1991-04-10 19:48:25 +0000199};
200
Martin v. Löwis1a214512008-06-11 05:26:20 +0000201static struct PyModuleDef pwdmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 PyModuleDef_HEAD_INIT,
203 "pwd",
204 pwd__doc__,
205 -1,
206 pwd_methods,
207 NULL,
208 NULL,
209 NULL,
210 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000211};
212
213
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000214PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000215PyInit_pwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 PyObject *m;
218 m = PyModule_Create(&pwdmodule);
219 if (m == NULL)
220 return NULL;
Fred Drake88c93442002-04-13 21:07:45 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 if (!initialized) {
223 PyStructSequence_InitType(&StructPwdType,
224 &struct_pwd_type_desc);
225 initialized = 1;
226 }
227 Py_INCREF((PyObject *) &StructPwdType);
228 PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
229 return m;
Guido van Rossum864407d1991-04-10 19:48:25 +0000230}