Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2 | /* UNIX password file access module */ |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 3 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 5 | #include "posixmodule.h" |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 7 | #include <pwd.h> |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 8 | |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 9 | static PyStructSequence_Field struct_pwd_type_fields[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 10 | {"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öwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 18 | }; |
| 19 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 20 | PyDoc_STRVAR(struct_passwd__doc__, |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 21 | "pwd.struct_passwd: Results from getpw*() routines.\n\n\ |
| 22 | This 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öwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 24 | or via the object attributes as named in the above tuple."); |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 25 | |
| 26 | static PyStructSequence_Desc struct_pwd_type_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | "pwd.struct_passwd", |
| 28 | struct_passwd__doc__, |
| 29 | struct_pwd_type_fields, |
| 30 | 7, |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 33 | PyDoc_STRVAR(pwd__doc__, |
| 34 | "This module provides access to the Unix password database.\n\ |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 35 | It is available on all Unix versions.\n\ |
| 36 | \n\ |
| 37 | Password database entries are reported as 7-tuples containing the following\n\ |
| 38 | items from the password database (see `<pwd.h>'), in order:\n\ |
| 39 | pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\ |
| 40 | The uid and gid items are integers, all others are strings. An\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 41 | exception is raised if the entry asked for cannot be found."); |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 42 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 44 | static int initialized; |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 45 | static PyTypeObject StructPwdType; |
| 46 | |
Martin v. Löwis | 29275c9 | 2002-09-17 09:34:06 +0000 | [diff] [blame] | 47 | static void |
Neal Norwitz | eb8b3a6 | 2007-08-24 23:26:23 +0000 | [diff] [blame] | 48 | sets(PyObject *v, int i, const char* val) |
Martin v. Löwis | 29275c9 | 2002-09-17 09:34:06 +0000 | [diff] [blame] | 49 | { |
Neal Norwitz | 3d7a90d | 2007-10-27 05:40:06 +0000 | [diff] [blame] | 50 | if (val) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | PyObject *o = PyUnicode_DecodeFSDefault(val); |
| 52 | PyStructSequence_SET_ITEM(v, i, o); |
Neal Norwitz | 3d7a90d | 2007-10-27 05:40:06 +0000 | [diff] [blame] | 53 | } |
Martin v. Löwis | 29275c9 | 2002-09-17 09:34:06 +0000 | [diff] [blame] | 54 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | PyStructSequence_SET_ITEM(v, i, Py_None); |
| 56 | Py_INCREF(Py_None); |
Martin v. Löwis | 29275c9 | 2002-09-17 09:34:06 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 60 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 61 | mkpwent(struct passwd *p) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 62 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | int setIndex = 0; |
| 64 | PyObject *v = PyStructSequence_New(&StructPwdType); |
| 65 | if (v == NULL) |
| 66 | return NULL; |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 67 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 68 | #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val)) |
Martin v. Löwis | 29275c9 | 2002-09-17 09:34:06 +0000 | [diff] [blame] | 69 | #define SETS(i,val) sets(v, i, val) |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 70 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | SETS(setIndex++, p->pw_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | SETS(setIndex++, p->pw_passwd); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 73 | PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid)); |
| 74 | PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | SETS(setIndex++, p->pw_gecos); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | SETS(setIndex++, p->pw_dir); |
| 77 | SETS(setIndex++, p->pw_shell); |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 78 | |
| 79 | #undef SETS |
| 80 | #undef SETI |
| 81 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | if (PyErr_Occurred()) { |
| 83 | Py_XDECREF(v); |
| 84 | return NULL; |
| 85 | } |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 86 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | return v; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 90 | PyDoc_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 Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 93 | Return the password database entry for the given numeric user ID.\n\ |
Alexander Belopolsky | 977a684 | 2010-08-16 20:17:07 +0000 | [diff] [blame] | 94 | See help(pwd) for more on password database entries."); |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 95 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 96 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 97 | pwd_getpwuid(PyObject *self, PyObject *args) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 98 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 99 | uid_t uid; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | struct passwd *p; |
Serhiy Storchaka | 55e2238 | 2013-02-11 20:32:47 +0200 | [diff] [blame] | 101 | 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | return NULL; |
Serhiy Storchaka | 55e2238 | 2013-02-11 20:32:47 +0200 | [diff] [blame] | 106 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | if ((p = getpwuid(uid)) == NULL) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 108 | PyObject *uid_obj = _PyLong_FromUid(uid); |
| 109 | if (uid_obj == NULL) |
| 110 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | PyErr_Format(PyExc_KeyError, |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 112 | "getpwuid(): uid not found: %S", uid_obj); |
| 113 | Py_DECREF(uid_obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | return NULL; |
| 115 | } |
| 116 | return mkpwent(p); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 119 | PyDoc_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 Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 122 | Return the password database entry for the given user name.\n\ |
Alexander Belopolsky | 977a684 | 2010-08-16 20:17:07 +0000 | [diff] [blame] | 123 | See help(pwd) for more on password database entries."); |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 124 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 125 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 126 | pwd_getpwnam(PyObject *self, PyObject *args) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 127 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | char *name; |
| 129 | struct passwd *p; |
| 130 | PyObject *arg, *bytes, *retval = NULL; |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | if (!PyArg_ParseTuple(args, "U:getpwnam", &arg)) |
| 133 | return NULL; |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 134 | if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | 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öwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 144 | out: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | Py_DECREF(bytes); |
| 146 | return retval; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 149 | #ifdef HAVE_GETPWENT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 150 | PyDoc_STRVAR(pwd_getpwall__doc__, |
| 151 | "getpwall() -> list_of_entries\n\ |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 152 | Return a list of all available password database entries, \ |
| 153 | in arbitrary order.\n\ |
Alexander Belopolsky | 977a684 | 2010-08-16 20:17:07 +0000 | [diff] [blame] | 154 | See help(pwd) for more on password database entries."); |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 155 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 156 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 157 | pwd_getpwall(PyObject *self) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 158 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | PyObject *d; |
| 160 | struct passwd *p; |
| 161 | if ((d = PyList_New(0)) == NULL) |
| 162 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 163 | setpwent(); |
| 164 | while ((p = getpwent()) != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | 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 Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 176 | } |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 177 | #endif |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 178 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 179 | static PyMethodDef pwd_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__}, |
| 181 | {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 182 | #ifdef HAVE_GETPWENT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 183 | {"getpwall", (PyCFunction)pwd_getpwall, |
| 184 | METH_NOARGS, pwd_getpwall__doc__}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 185 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 189 | static struct PyModuleDef pwdmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | PyModuleDef_HEAD_INIT, |
| 191 | "pwd", |
| 192 | pwd__doc__, |
| 193 | -1, |
| 194 | pwd_methods, |
| 195 | NULL, |
| 196 | NULL, |
| 197 | NULL, |
| 198 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 202 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 203 | PyInit_pwd(void) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 | PyObject *m; |
| 206 | m = PyModule_Create(&pwdmodule); |
| 207 | if (m == NULL) |
| 208 | return NULL; |
Fred Drake | 88c9344 | 2002-04-13 21:07:45 +0000 | [diff] [blame] | 209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | if (!initialized) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 211 | if (PyStructSequence_InitType2(&StructPwdType, |
| 212 | &struct_pwd_type_desc) < 0) |
| 213 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | initialized = 1; |
| 215 | } |
| 216 | Py_INCREF((PyObject *) &StructPwdType); |
| 217 | PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType); |
| 218 | return m; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 219 | } |