blob: f6d36742ef366950efb4f51989d1efbecc0627d9 [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"
Martin v. Löwisdbd55b32002-03-01 10:38:44 +00005#include "structseq.h"
Guido van Rossum864407d1991-04-10 19:48:25 +00006
7#include <sys/types.h>
8#include <pwd.h>
Guido van Rossum864407d1991-04-10 19:48:25 +00009
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000010static PyStructSequence_Field struct_pwd_type_fields[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000011 {"pw_name", "user name"},
12 {"pw_passwd", "password"},
13 {"pw_uid", "user id"},
14 {"pw_gid", "group id"},
15 {"pw_gecos", "real name"},
16 {"pw_dir", "home directory"},
17 {"pw_shell", "shell program"},
18 {0}
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000019};
20
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000021PyDoc_STRVAR(struct_passwd__doc__,
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000022"pwd.struct_passwd: Results from getpw*() routines.\n\n\
23This object may be accessed either as a tuple of\n\
24 (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000025or via the object attributes as named in the above tuple.");
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000026
27static PyStructSequence_Desc struct_pwd_type_desc = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000028 "pwd.struct_passwd",
29 struct_passwd__doc__,
30 struct_pwd_type_fields,
31 7,
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000032};
33
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000034PyDoc_STRVAR(pwd__doc__,
35"This module provides access to the Unix password database.\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +000036It is available on all Unix versions.\n\
37\n\
38Password database entries are reported as 7-tuples containing the following\n\
39items from the password database (see `<pwd.h>'), in order:\n\
40pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
41The uid and gid items are integers, all others are strings. An\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000042exception is raised if the entry asked for cannot be found.");
Guido van Rossum3e79c441998-03-03 22:03:26 +000043
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000044
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000045static int initialized;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000046static PyTypeObject StructPwdType;
47
Martin v. Löwis29275c92002-09-17 09:34:06 +000048static void
Neal Norwitzeb8b3a62007-08-24 23:26:23 +000049sets(PyObject *v, int i, const char* val)
Martin v. Löwis29275c92002-09-17 09:34:06 +000050{
Neal Norwitz3d7a90d2007-10-27 05:40:06 +000051 if (val) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000052 PyObject *o = PyUnicode_Decode(val, strlen(val),
53 Py_FileSystemDefaultEncoding,
54 "surrogateescape");
55 PyStructSequence_SET_ITEM(v, i, o);
Neal Norwitz3d7a90d2007-10-27 05:40:06 +000056 }
Martin v. Löwis29275c92002-09-17 09:34:06 +000057 else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000058 PyStructSequence_SET_ITEM(v, i, Py_None);
59 Py_INCREF(Py_None);
Martin v. Löwis29275c92002-09-17 09:34:06 +000060 }
61}
62
Barry Warsaw50c5cf11996-12-11 16:54:40 +000063static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000064mkpwent(struct passwd *p)
Guido van Rossum864407d1991-04-10 19:48:25 +000065{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000066 int setIndex = 0;
67 PyObject *v = PyStructSequence_New(&StructPwdType);
68 if (v == NULL)
69 return NULL;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000070
Christian Heimes217cfd12007-12-02 14:31:20 +000071#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
Martin v. Löwis29275c92002-09-17 09:34:06 +000072#define SETS(i,val) sets(v, i, val)
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000073
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000074 SETS(setIndex++, p->pw_name);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000075#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000076 SETS(setIndex++, "");
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000077#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000078 SETS(setIndex++, p->pw_passwd);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000079#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000080 SETI(setIndex++, p->pw_uid);
81 SETI(setIndex++, p->pw_gid);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000082#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000083 SETS(setIndex++, "");
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000084#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000085 SETS(setIndex++, p->pw_gecos);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000086#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000087 SETS(setIndex++, p->pw_dir);
88 SETS(setIndex++, p->pw_shell);
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000089
90#undef SETS
91#undef SETI
92
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000093 if (PyErr_Occurred()) {
94 Py_XDECREF(v);
95 return NULL;
96 }
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000097
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000098 return v;
Guido van Rossum864407d1991-04-10 19:48:25 +000099}
100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000101PyDoc_STRVAR(pwd_getpwuid__doc__,
102"getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
103 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000104Return the password database entry for the given numeric user ID.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000105See pwd.__doc__ for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000106
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000107static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000108pwd_getpwuid(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000109{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000110 unsigned int uid;
111 struct passwd *p;
112 if (!PyArg_ParseTuple(args, "I:getpwuid", &uid))
113 return NULL;
114 if ((p = getpwuid(uid)) == NULL) {
115 PyErr_Format(PyExc_KeyError,
116 "getpwuid(): uid not found: %d", uid);
117 return NULL;
118 }
119 return mkpwent(p);
Guido van Rossum864407d1991-04-10 19:48:25 +0000120}
121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000122PyDoc_STRVAR(pwd_getpwnam__doc__,
123"getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
124 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000125Return the password database entry for the given user name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000126See pwd.__doc__ for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000127
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000128static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000129pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000130{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000131 char *name;
132 struct passwd *p;
133 PyObject *arg, *bytes, *retval = NULL;
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000134
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000135 if (!PyArg_ParseTuple(args, "U:getpwnam", &arg))
136 return NULL;
137 if ((bytes = PyUnicode_AsEncodedString(arg,
138 Py_FileSystemDefaultEncoding,
139 "surrogateescape")) == NULL)
140 return NULL;
141 if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
142 goto out;
143 if ((p = getpwnam(name)) == NULL) {
144 PyErr_Format(PyExc_KeyError,
145 "getpwnam(): name not found: %s", name);
146 goto out;
147 }
148 retval = mkpwent(p);
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000149out:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000150 Py_DECREF(bytes);
151 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000152}
153
Guido van Rossum1171ee61997-08-22 20:42:00 +0000154#ifdef HAVE_GETPWENT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000155PyDoc_STRVAR(pwd_getpwall__doc__,
156"getpwall() -> list_of_entries\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000157Return a list of all available password database entries, \
158in arbitrary order.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000159See pwd.__doc__ for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000160
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000161static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000162pwd_getpwall(PyObject *self)
Guido van Rossum864407d1991-04-10 19:48:25 +0000163{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000164 PyObject *d;
165 struct passwd *p;
166 if ((d = PyList_New(0)) == NULL)
167 return NULL;
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000168#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000169 if ((p = getpwuid(0)) != NULL) {
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000170#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000171 setpwent();
172 while ((p = getpwent()) != NULL) {
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000173#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000174 PyObject *v = mkpwent(p);
175 if (v == NULL || PyList_Append(d, v) != 0) {
176 Py_XDECREF(v);
177 Py_DECREF(d);
178 endpwent();
179 return NULL;
180 }
181 Py_DECREF(v);
182 }
183 endpwent();
184 return d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000185}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000186#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000187
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000188static PyMethodDef pwd_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000189 {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
190 {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000191#ifdef HAVE_GETPWENT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000192 {"getpwall", (PyCFunction)pwd_getpwall,
193 METH_NOARGS, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000194#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000195 {NULL, NULL} /* sentinel */
Guido van Rossum864407d1991-04-10 19:48:25 +0000196};
197
Martin v. Löwis1a214512008-06-11 05:26:20 +0000198static struct PyModuleDef pwdmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000199 PyModuleDef_HEAD_INIT,
200 "pwd",
201 pwd__doc__,
202 -1,
203 pwd_methods,
204 NULL,
205 NULL,
206 NULL,
207 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000208};
209
210
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000211PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000212PyInit_pwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000213{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000214 PyObject *m;
215 m = PyModule_Create(&pwdmodule);
216 if (m == NULL)
217 return NULL;
Fred Drake88c93442002-04-13 21:07:45 +0000218
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000219 if (!initialized) {
220 PyStructSequence_InitType(&StructPwdType,
221 &struct_pwd_type_desc);
222 initialized = 1;
223 }
224 Py_INCREF((PyObject *) &StructPwdType);
225 PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
226 return m;
Guido van Rossum864407d1991-04-10 19:48:25 +0000227}