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" |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 5 | #include "structseq.h" |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 6 | |
| 7 | #include <sys/types.h> |
| 8 | #include <pwd.h> |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 9 | |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 10 | static PyStructSequence_Field struct_pwd_type_fields[] = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 11 | {"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öwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 19 | }; |
| 20 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 21 | PyDoc_STRVAR(struct_passwd__doc__, |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 22 | "pwd.struct_passwd: Results from getpw*() routines.\n\n\ |
| 23 | This 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öwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 25 | 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] | 26 | |
| 27 | static PyStructSequence_Desc struct_pwd_type_desc = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 28 | "pwd.struct_passwd", |
| 29 | struct_passwd__doc__, |
| 30 | struct_pwd_type_fields, |
| 31 | 7, |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 34 | PyDoc_STRVAR(pwd__doc__, |
| 35 | "This module provides access to the Unix password database.\n\ |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 36 | It is available on all Unix versions.\n\ |
| 37 | \n\ |
| 38 | Password database entries are reported as 7-tuples containing the following\n\ |
| 39 | items from the password database (see `<pwd.h>'), in order:\n\ |
| 40 | pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\ |
| 41 | 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] | 42 | exception is raised if the entry asked for cannot be found."); |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 43 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 44 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 45 | static int initialized; |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 46 | static PyTypeObject StructPwdType; |
| 47 | |
Martin v. Löwis | 29275c9 | 2002-09-17 09:34:06 +0000 | [diff] [blame] | 48 | static void |
Neal Norwitz | eb8b3a6 | 2007-08-24 23:26:23 +0000 | [diff] [blame] | 49 | sets(PyObject *v, int i, const char* val) |
Martin v. Löwis | 29275c9 | 2002-09-17 09:34:06 +0000 | [diff] [blame] | 50 | { |
Neal Norwitz | 3d7a90d | 2007-10-27 05:40:06 +0000 | [diff] [blame] | 51 | if (val) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 52 | PyObject *o = PyUnicode_Decode(val, strlen(val), |
| 53 | Py_FileSystemDefaultEncoding, |
| 54 | "surrogateescape"); |
| 55 | PyStructSequence_SET_ITEM(v, i, o); |
Neal Norwitz | 3d7a90d | 2007-10-27 05:40:06 +0000 | [diff] [blame] | 56 | } |
Martin v. Löwis | 29275c9 | 2002-09-17 09:34:06 +0000 | [diff] [blame] | 57 | else { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 58 | PyStructSequence_SET_ITEM(v, i, Py_None); |
| 59 | Py_INCREF(Py_None); |
Martin v. Löwis | 29275c9 | 2002-09-17 09:34:06 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 63 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 64 | mkpwent(struct passwd *p) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 65 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 66 | int setIndex = 0; |
| 67 | PyObject *v = PyStructSequence_New(&StructPwdType); |
| 68 | if (v == NULL) |
| 69 | return NULL; |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 70 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 71 | #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] | 72 | #define SETS(i,val) sets(v, i, val) |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 73 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 74 | SETS(setIndex++, p->pw_name); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 75 | #ifdef __VMS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 76 | SETS(setIndex++, ""); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 77 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 78 | SETS(setIndex++, p->pw_passwd); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 79 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 80 | SETI(setIndex++, p->pw_uid); |
| 81 | SETI(setIndex++, p->pw_gid); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 82 | #ifdef __VMS |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 83 | SETS(setIndex++, ""); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 84 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 85 | SETS(setIndex++, p->pw_gecos); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 86 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 87 | SETS(setIndex++, p->pw_dir); |
| 88 | SETS(setIndex++, p->pw_shell); |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 89 | |
| 90 | #undef SETS |
| 91 | #undef SETI |
| 92 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 93 | if (PyErr_Occurred()) { |
| 94 | Py_XDECREF(v); |
| 95 | return NULL; |
| 96 | } |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 97 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 98 | return v; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 101 | PyDoc_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 Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 104 | Return the password database entry for the given numeric user ID.\n\ |
Alexander Belopolsky | 102594f | 2010-08-16 20:26:04 +0000 | [diff] [blame] | 105 | See help(pwd) for more on password database entries."); |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 106 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 107 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 108 | pwd_getpwuid(PyObject *self, PyObject *args) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 109 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 110 | 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 Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 122 | PyDoc_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 Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 125 | Return the password database entry for the given user name.\n\ |
Alexander Belopolsky | 102594f | 2010-08-16 20:26:04 +0000 | [diff] [blame] | 126 | See help(pwd) for more on password database entries."); |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 127 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 128 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 129 | pwd_getpwnam(PyObject *self, PyObject *args) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 130 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 131 | char *name; |
| 132 | struct passwd *p; |
| 133 | PyObject *arg, *bytes, *retval = NULL; |
Martin v. Löwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 134 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 135 | 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öwis | b6a748b | 2009-05-29 15:23:17 +0000 | [diff] [blame] | 149 | out: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 150 | Py_DECREF(bytes); |
| 151 | return retval; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 154 | #ifdef HAVE_GETPWENT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 155 | PyDoc_STRVAR(pwd_getpwall__doc__, |
| 156 | "getpwall() -> list_of_entries\n\ |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 157 | Return a list of all available password database entries, \ |
| 158 | in arbitrary order.\n\ |
Alexander Belopolsky | 102594f | 2010-08-16 20:26:04 +0000 | [diff] [blame] | 159 | See help(pwd) for more on password database entries."); |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 160 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 161 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 162 | pwd_getpwall(PyObject *self) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 163 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 164 | PyObject *d; |
| 165 | struct passwd *p; |
| 166 | if ((d = PyList_New(0)) == NULL) |
| 167 | return NULL; |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 168 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 169 | if ((p = getpwuid(0)) != NULL) { |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 170 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 171 | setpwent(); |
| 172 | while ((p = getpwent()) != NULL) { |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 173 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 174 | 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 Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 185 | } |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 186 | #endif |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 187 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 188 | static PyMethodDef pwd_methods[] = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 189 | {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__}, |
| 190 | {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 191 | #ifdef HAVE_GETPWENT |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 192 | {"getpwall", (PyCFunction)pwd_getpwall, |
| 193 | METH_NOARGS, pwd_getpwall__doc__}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 194 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 195 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 198 | static struct PyModuleDef pwdmodule = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 199 | PyModuleDef_HEAD_INIT, |
| 200 | "pwd", |
| 201 | pwd__doc__, |
| 202 | -1, |
| 203 | pwd_methods, |
| 204 | NULL, |
| 205 | NULL, |
| 206 | NULL, |
| 207 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 211 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 212 | PyInit_pwd(void) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 213 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 214 | PyObject *m; |
| 215 | m = PyModule_Create(&pwdmodule); |
| 216 | if (m == NULL) |
| 217 | return NULL; |
Fred Drake | 88c9344 | 2002-04-13 21:07:45 +0000 | [diff] [blame] | 218 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 219 | 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 Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 227 | } |