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[] = { |
| 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} |
| 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 = { |
| 28 | "pwd.struct_passwd", |
| 29 | struct_passwd__doc__, |
| 30 | struct_pwd_type_fields, |
| 31 | 7, |
| 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 | |
| 44 | |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +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 |
| 49 | sets(PyObject *v, int i, char* val) |
| 50 | { |
| 51 | if (val) |
| 52 | PyStructSequence_SET_ITEM(v, i, PyString_FromString(val)); |
| 53 | else { |
| 54 | PyStructSequence_SET_ITEM(v, i, Py_None); |
| 55 | Py_INCREF(Py_None); |
| 56 | } |
| 57 | } |
| 58 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 59 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 60 | mkpwent(struct passwd *p) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 61 | { |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 62 | int setIndex = 0; |
| 63 | PyObject *v = PyStructSequence_New(&StructPwdType); |
| 64 | if (v == NULL) |
| 65 | return NULL; |
| 66 | |
| 67 | #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val)) |
Martin v. Löwis | 29275c9 | 2002-09-17 09:34:06 +0000 | [diff] [blame] | 68 | #define SETS(i,val) sets(v, i, val) |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 69 | |
| 70 | SETS(setIndex++, p->pw_name); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 71 | #ifdef __VMS |
| 72 | SETS(setIndex++, ""); |
| 73 | #else |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 74 | SETS(setIndex++, p->pw_passwd); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 75 | #endif |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 76 | SETI(setIndex++, p->pw_uid); |
| 77 | SETI(setIndex++, p->pw_gid); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 78 | #ifdef __VMS |
| 79 | SETS(setIndex++, ""); |
| 80 | #else |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 81 | SETS(setIndex++, p->pw_gecos); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 82 | #endif |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 83 | SETS(setIndex++, p->pw_dir); |
| 84 | SETS(setIndex++, p->pw_shell); |
| 85 | |
| 86 | #undef SETS |
| 87 | #undef SETI |
| 88 | |
| 89 | if (PyErr_Occurred()) { |
| 90 | Py_XDECREF(v); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | return v; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 97 | PyDoc_STRVAR(pwd_getpwuid__doc__, |
| 98 | "getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\ |
| 99 | pw_gid,pw_gecos,pw_dir,pw_shell)\n\ |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 100 | Return the password database entry for the given numeric user ID.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 101 | See pwd.__doc__ for more on password database entries."); |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 102 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 103 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 104 | pwd_getpwuid(PyObject *self, PyObject *args) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 105 | { |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 106 | unsigned int uid; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 107 | struct passwd *p; |
Guido van Rossum | 8ee3e5a | 2005-09-14 18:09:42 +0000 | [diff] [blame] | 108 | if (!PyArg_ParseTuple(args, "I:getpwuid", &uid)) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 109 | return NULL; |
| 110 | if ((p = getpwuid(uid)) == NULL) { |
Barry Warsaw | e1b1c87 | 2004-01-20 21:07:23 +0000 | [diff] [blame] | 111 | PyErr_Format(PyExc_KeyError, |
| 112 | "getpwuid(): uid not found: %d", uid); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 113 | return NULL; |
| 114 | } |
| 115 | return mkpwent(p); |
| 116 | } |
| 117 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 118 | PyDoc_STRVAR(pwd_getpwnam__doc__, |
| 119 | "getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\ |
| 120 | pw_gid,pw_gecos,pw_dir,pw_shell)\n\ |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 121 | Return the password database entry for the given user name.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 122 | See pwd.__doc__ for more on password database entries."); |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 123 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 124 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 125 | pwd_getpwnam(PyObject *self, PyObject *args) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 126 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 127 | char *name; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 128 | struct passwd *p; |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 129 | if (!PyArg_ParseTuple(args, "s:getpwnam", &name)) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 130 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 131 | if ((p = getpwnam(name)) == NULL) { |
Barry Warsaw | e1b1c87 | 2004-01-20 21:07:23 +0000 | [diff] [blame] | 132 | PyErr_Format(PyExc_KeyError, |
| 133 | "getpwnam(): name not found: %s", name); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 134 | return NULL; |
| 135 | } |
| 136 | return mkpwent(p); |
| 137 | } |
| 138 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 139 | #ifdef HAVE_GETPWENT |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 140 | PyDoc_STRVAR(pwd_getpwall__doc__, |
| 141 | "getpwall() -> list_of_entries\n\ |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 142 | Return a list of all available password database entries, \ |
| 143 | in arbitrary order.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 144 | See pwd.__doc__ for more on password database entries."); |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 145 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 146 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 147 | pwd_getpwall(PyObject *self) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 148 | { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 149 | PyObject *d; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 150 | struct passwd *p; |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 151 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 152 | return NULL; |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 153 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 154 | if ((p = getpwuid(0)) != NULL) { |
| 155 | #else |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 156 | setpwent(); |
| 157 | while ((p = getpwent()) != NULL) { |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 158 | #endif |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 159 | PyObject *v = mkpwent(p); |
| 160 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 161 | Py_XDECREF(v); |
| 162 | Py_DECREF(d); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 163 | return NULL; |
| 164 | } |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 165 | Py_DECREF(v); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 166 | } |
Fred Drake | 8e68eb6 | 2001-03-11 03:03:07 +0000 | [diff] [blame] | 167 | endpwent(); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 168 | return d; |
| 169 | } |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 170 | #endif |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 171 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 172 | static PyMethodDef pwd_methods[] = { |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 173 | {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__}, |
| 174 | {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 175 | #ifdef HAVE_GETPWENT |
Neil Schemenauer | cc07ec1 | 2002-03-29 19:58:25 +0000 | [diff] [blame] | 176 | {"getpwall", (PyCFunction)pwd_getpwall, |
| 177 | METH_NOARGS, pwd_getpwall__doc__}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 178 | #endif |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 179 | {NULL, NULL} /* sentinel */ |
| 180 | }; |
| 181 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 182 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 183 | initpwd(void) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 184 | { |
Neal Norwitz | 726e013 | 2002-04-15 16:29:00 +0000 | [diff] [blame] | 185 | PyObject *m; |
Fred Drake | 88c9344 | 2002-04-13 21:07:45 +0000 | [diff] [blame] | 186 | m = Py_InitModule3("pwd", pwd_methods, pwd__doc__); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 187 | if (m == NULL) |
| 188 | return; |
Fred Drake | 88c9344 | 2002-04-13 21:07:45 +0000 | [diff] [blame] | 189 | |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 190 | if (!initialized) |
| 191 | PyStructSequence_InitType(&StructPwdType, |
| 192 | &struct_pwd_type_desc); |
Fred Drake | 88c9344 | 2002-04-13 21:07:45 +0000 | [diff] [blame] | 193 | Py_INCREF((PyObject *) &StructPwdType); |
Guido van Rossum | 16b9fa8 | 2003-10-23 23:54:57 +0000 | [diff] [blame] | 194 | PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType); |
| 195 | /* And for b/w compatibility (this was defined by mistake): */ |
Fred Drake | 88c9344 | 2002-04-13 21:07:45 +0000 | [diff] [blame] | 196 | PyModule_AddObject(m, "struct_pwent", (PyObject *) &StructPwdType); |
Martin v. Löwis | 19ab6c9 | 2006-04-16 18:55:50 +0000 | [diff] [blame] | 197 | initialized = 1; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 198 | } |