blob: 632ffe3be8caf277da989bf03f11670f9aab92d0 [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
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 Storchaka7cf55992013-02-10 21:56:49 +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 Storchaka7cf55992013-02-10 21:56:49 +0200107 uid_t uid;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 struct passwd *p;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200109 if (!PyArg_ParseTuple(args, "O&:getpwuid", _Py_Uid_Converter, &uid))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 return NULL;
111 if ((p = getpwuid(uid)) == NULL) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200112 PyObject *uid_obj = _PyLong_FromUid(uid);
113 if (uid_obj == NULL)
114 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 PyErr_Format(PyExc_KeyError,
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200116 "getpwuid(): uid not found: %S", uid_obj);
117 Py_DECREF(uid_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 return NULL;
119 }
120 return mkpwent(p);
Guido van Rossum864407d1991-04-10 19:48:25 +0000121}
122
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000123PyDoc_STRVAR(pwd_getpwnam__doc__,
124"getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
125 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000126Return the password database entry for the given user name.\n\
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000127See help(pwd) for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000128
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000129static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000130pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 char *name;
133 struct passwd *p;
134 PyObject *arg, *bytes, *retval = NULL;
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 if (!PyArg_ParseTuple(args, "U:getpwnam", &arg))
137 return NULL;
Victor Stinnerae6265f2010-05-15 16:27:27 +0000138 if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 return NULL;
140 if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
141 goto out;
142 if ((p = getpwnam(name)) == NULL) {
143 PyErr_Format(PyExc_KeyError,
144 "getpwnam(): name not found: %s", name);
145 goto out;
146 }
147 retval = mkpwent(p);
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000148out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 Py_DECREF(bytes);
150 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000151}
152
Guido van Rossum1171ee61997-08-22 20:42:00 +0000153#ifdef HAVE_GETPWENT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000154PyDoc_STRVAR(pwd_getpwall__doc__,
155"getpwall() -> list_of_entries\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000156Return a list of all available password database entries, \
157in arbitrary order.\n\
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000158See help(pwd) for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000159
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000160static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000161pwd_getpwall(PyObject *self)
Guido van Rossum864407d1991-04-10 19:48:25 +0000162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 PyObject *d;
164 struct passwd *p;
165 if ((d = PyList_New(0)) == NULL)
166 return NULL;
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000167#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 if ((p = getpwuid(0)) != NULL) {
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000169#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 setpwent();
171 while ((p = getpwent()) != NULL) {
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000172#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 PyObject *v = mkpwent(p);
174 if (v == NULL || PyList_Append(d, v) != 0) {
175 Py_XDECREF(v);
176 Py_DECREF(d);
177 endpwent();
178 return NULL;
179 }
180 Py_DECREF(v);
181 }
182 endpwent();
183 return d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000184}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000185#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000186
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000187static PyMethodDef pwd_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
189 {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000190#ifdef HAVE_GETPWENT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 {"getpwall", (PyCFunction)pwd_getpwall,
192 METH_NOARGS, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000193#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 {NULL, NULL} /* sentinel */
Guido van Rossum864407d1991-04-10 19:48:25 +0000195};
196
Martin v. Löwis1a214512008-06-11 05:26:20 +0000197static struct PyModuleDef pwdmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 PyModuleDef_HEAD_INIT,
199 "pwd",
200 pwd__doc__,
201 -1,
202 pwd_methods,
203 NULL,
204 NULL,
205 NULL,
206 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000207};
208
209
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000210PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000211PyInit_pwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 PyObject *m;
214 m = PyModule_Create(&pwdmodule);
215 if (m == NULL)
216 return NULL;
Fred Drake88c93442002-04-13 21:07:45 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if (!initialized) {
219 PyStructSequence_InitType(&StructPwdType,
220 &struct_pwd_type_desc);
221 initialized = 1;
222 }
223 Py_INCREF((PyObject *) &StructPwdType);
224 PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
225 return m;
Guido van Rossum864407d1991-04-10 19:48:25 +0000226}