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" |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 5 | |
| 6 | #include <sys/types.h> |
| 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); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 72 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | SETS(setIndex++, ""); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 74 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | SETS(setIndex++, p->pw_passwd); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 76 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | SETI(setIndex++, p->pw_uid); |
| 78 | SETI(setIndex++, p->pw_gid); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 79 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | SETS(setIndex++, ""); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 81 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | SETS(setIndex++, p->pw_gecos); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 83 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | SETS(setIndex++, p->pw_dir); |
| 85 | SETS(setIndex++, p->pw_shell); |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 86 | |
| 87 | #undef SETS |
| 88 | #undef SETI |
| 89 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 90 | if (PyErr_Occurred()) { |
| 91 | Py_XDECREF(v); |
| 92 | return NULL; |
| 93 | } |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 94 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | return v; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 98 | PyDoc_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 Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 101 | Return the password database entry for the given numeric user ID.\n\ |
Alexander Belopolsky | 977a684 | 2010-08-16 20:17:07 +0000 | [diff] [blame] | 102 | See help(pwd) for more on password database entries."); |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 103 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 104 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 105 | pwd_getpwuid(PyObject *self, PyObject *args) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 106 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | unsigned int uid; |
| 108 | struct passwd *p; |
| 109 | if (!PyArg_ParseTuple(args, "I:getpwuid", &uid)) |
| 110 | return NULL; |
| 111 | if ((p = getpwuid(uid)) == NULL) { |
| 112 | PyErr_Format(PyExc_KeyError, |
| 113 | "getpwuid(): uid not found: %d", uid); |
| 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; |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 163 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | if ((p = getpwuid(0)) != NULL) { |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 165 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | setpwent(); |
| 167 | while ((p = getpwent()) != NULL) { |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 168 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | PyObject *v = mkpwent(p); |
| 170 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 171 | Py_XDECREF(v); |
| 172 | Py_DECREF(d); |
| 173 | endpwent(); |
| 174 | return NULL; |
| 175 | } |
| 176 | Py_DECREF(v); |
| 177 | } |
| 178 | endpwent(); |
| 179 | return d; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 180 | } |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 181 | #endif |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 182 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 183 | static PyMethodDef pwd_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__}, |
| 185 | {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 186 | #ifdef HAVE_GETPWENT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | {"getpwall", (PyCFunction)pwd_getpwall, |
| 188 | METH_NOARGS, pwd_getpwall__doc__}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 189 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 193 | static struct PyModuleDef pwdmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | PyModuleDef_HEAD_INIT, |
| 195 | "pwd", |
| 196 | pwd__doc__, |
| 197 | -1, |
| 198 | pwd_methods, |
| 199 | NULL, |
| 200 | NULL, |
| 201 | NULL, |
| 202 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 206 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 207 | PyInit_pwd(void) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 208 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | PyObject *m; |
| 210 | m = PyModule_Create(&pwdmodule); |
| 211 | if (m == NULL) |
| 212 | return NULL; |
Fred Drake | 88c9344 | 2002-04-13 21:07:45 +0000 | [diff] [blame] | 213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | if (!initialized) { |
| 215 | PyStructSequence_InitType(&StructPwdType, |
| 216 | &struct_pwd_type_desc); |
| 217 | initialized = 1; |
| 218 | } |
| 219 | Py_INCREF((PyObject *) &StructPwdType); |
| 220 | PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType); |
| 221 | return m; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 222 | } |