blob: ebb87120180c8bde0de89b11f1db48d0d4518f81 [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);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 SETS(setIndex++, p->pw_passwd);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020073 PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
74 PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 SETS(setIndex++, p->pw_gecos);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 SETS(setIndex++, p->pw_dir);
77 SETS(setIndex++, p->pw_shell);
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000078
79#undef SETS
80#undef SETI
81
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 if (PyErr_Occurred()) {
83 Py_XDECREF(v);
84 return NULL;
85 }
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 return v;
Guido van Rossum864407d1991-04-10 19:48:25 +000088}
89
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000090PyDoc_STRVAR(pwd_getpwuid__doc__,
91"getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
92 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +000093Return the password database entry for the given numeric user ID.\n\
Alexander Belopolsky977a6842010-08-16 20:17:07 +000094See help(pwd) for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +000095
Barry Warsaw50c5cf11996-12-11 16:54:40 +000096static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000097pwd_getpwuid(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +000098{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020099 uid_t uid;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 struct passwd *p;
Serhiy Storchaka55e22382013-02-11 20:32:47 +0200101 if (!PyArg_ParseTuple(args, "O&:getpwuid", _Py_Uid_Converter, &uid)) {
102 if (PyErr_ExceptionMatches(PyExc_OverflowError))
103 PyErr_Format(PyExc_KeyError,
104 "getpwuid(): uid not found");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 return NULL;
Serhiy Storchaka55e22382013-02-11 20:32:47 +0200106 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 if ((p = getpwuid(uid)) == NULL) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200108 PyObject *uid_obj = _PyLong_FromUid(uid);
109 if (uid_obj == NULL)
110 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 PyErr_Format(PyExc_KeyError,
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200112 "getpwuid(): uid not found: %S", uid_obj);
113 Py_DECREF(uid_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 return NULL;
115 }
116 return mkpwent(p);
Guido van Rossum864407d1991-04-10 19:48:25 +0000117}
118
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000119PyDoc_STRVAR(pwd_getpwnam__doc__,
120"getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
121 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000122Return the password database entry for the given user name.\n\
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000123See help(pwd) for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000124
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000125static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000126pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 char *name;
129 struct passwd *p;
130 PyObject *arg, *bytes, *retval = NULL;
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 if (!PyArg_ParseTuple(args, "U:getpwnam", &arg))
133 return NULL;
Victor Stinnerae6265f2010-05-15 16:27:27 +0000134 if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 return NULL;
136 if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
137 goto out;
138 if ((p = getpwnam(name)) == NULL) {
139 PyErr_Format(PyExc_KeyError,
140 "getpwnam(): name not found: %s", name);
141 goto out;
142 }
143 retval = mkpwent(p);
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000144out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 Py_DECREF(bytes);
146 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000147}
148
Guido van Rossum1171ee61997-08-22 20:42:00 +0000149#ifdef HAVE_GETPWENT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000150PyDoc_STRVAR(pwd_getpwall__doc__,
151"getpwall() -> list_of_entries\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000152Return a list of all available password database entries, \
153in arbitrary order.\n\
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000154See help(pwd) for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000155
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000156static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000157pwd_getpwall(PyObject *self)
Guido van Rossum864407d1991-04-10 19:48:25 +0000158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 PyObject *d;
160 struct passwd *p;
161 if ((d = PyList_New(0)) == NULL)
162 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 setpwent();
164 while ((p = getpwent()) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 PyObject *v = mkpwent(p);
166 if (v == NULL || PyList_Append(d, v) != 0) {
167 Py_XDECREF(v);
168 Py_DECREF(d);
169 endpwent();
170 return NULL;
171 }
172 Py_DECREF(v);
173 }
174 endpwent();
175 return d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000176}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000177#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000178
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000179static PyMethodDef pwd_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
181 {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000182#ifdef HAVE_GETPWENT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 {"getpwall", (PyCFunction)pwd_getpwall,
184 METH_NOARGS, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000185#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 {NULL, NULL} /* sentinel */
Guido van Rossum864407d1991-04-10 19:48:25 +0000187};
188
Martin v. Löwis1a214512008-06-11 05:26:20 +0000189static struct PyModuleDef pwdmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 PyModuleDef_HEAD_INIT,
191 "pwd",
192 pwd__doc__,
193 -1,
194 pwd_methods,
195 NULL,
196 NULL,
197 NULL,
198 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000199};
200
201
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000202PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000203PyInit_pwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 PyObject *m;
206 m = PyModule_Create(&pwdmodule);
207 if (m == NULL)
208 return NULL;
Fred Drake88c93442002-04-13 21:07:45 +0000209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if (!initialized) {
Victor Stinner1c8f0592013-07-22 22:24:54 +0200211 if (PyStructSequence_InitType2(&StructPwdType,
212 &struct_pwd_type_desc) < 0)
213 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 initialized = 1;
215 }
216 Py_INCREF((PyObject *) &StructPwdType);
217 PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
218 return m;
Guido van Rossum864407d1991-04-10 19:48:25 +0000219}