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 | |
| 21 | static char struct_passwd__doc__[] = |
| 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\ |
| 25 | or via the object attributes as named in the above tuple.\n"; |
| 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 | |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 34 | static char pwd__doc__ [] = "\ |
| 35 | This module provides access to the Unix password database.\n\ |
| 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\ |
| 42 | exception is raised if the entry asked for cannot be found."; |
| 43 | |
| 44 | |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 45 | static PyTypeObject StructPwdType; |
| 46 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 47 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 48 | mkpwent(struct passwd *p) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 49 | { |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 50 | int setIndex = 0; |
| 51 | PyObject *v = PyStructSequence_New(&StructPwdType); |
| 52 | if (v == NULL) |
| 53 | return NULL; |
| 54 | |
| 55 | #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val)) |
| 56 | #define SETS(i,val) PyStructSequence_SET_ITEM(v, i, PyString_FromString(val)) |
| 57 | |
| 58 | SETS(setIndex++, p->pw_name); |
| 59 | SETS(setIndex++, p->pw_passwd); |
| 60 | SETI(setIndex++, p->pw_uid); |
| 61 | SETI(setIndex++, p->pw_gid); |
| 62 | SETS(setIndex++, p->pw_gecos); |
| 63 | SETS(setIndex++, p->pw_dir); |
| 64 | SETS(setIndex++, p->pw_shell); |
| 65 | |
| 66 | #undef SETS |
| 67 | #undef SETI |
| 68 | |
| 69 | if (PyErr_Occurred()) { |
| 70 | Py_XDECREF(v); |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | return v; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 77 | static char pwd_getpwuid__doc__[] = "\ |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 78 | getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\ |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 79 | Return the password database entry for the given numeric user ID.\n\ |
| 80 | See pwd.__doc__ for more on password database entries."; |
| 81 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 82 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 83 | pwd_getpwuid(PyObject *self, PyObject *args) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 84 | { |
| 85 | int uid; |
| 86 | struct passwd *p; |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 87 | if (!PyArg_ParseTuple(args, "i:getpwuid", &uid)) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 88 | return NULL; |
| 89 | if ((p = getpwuid(uid)) == NULL) { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 90 | PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found"); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 91 | return NULL; |
| 92 | } |
| 93 | return mkpwent(p); |
| 94 | } |
| 95 | |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 96 | static char pwd_getpwnam__doc__[] = "\ |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 97 | getpwnam(name) -> (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\ |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 98 | Return the password database entry for the given user name.\n\ |
| 99 | See pwd.__doc__ for more on password database entries."; |
| 100 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 101 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 102 | pwd_getpwnam(PyObject *self, PyObject *args) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 103 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 104 | char *name; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 105 | struct passwd *p; |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 106 | if (!PyArg_ParseTuple(args, "s:getpwnam", &name)) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 107 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 108 | if ((p = getpwnam(name)) == NULL) { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 109 | PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found"); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 110 | return NULL; |
| 111 | } |
| 112 | return mkpwent(p); |
| 113 | } |
| 114 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 115 | #ifdef HAVE_GETPWENT |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 116 | static char pwd_getpwall__doc__[] = "\ |
| 117 | getpwall() -> list_of_entries\n\ |
| 118 | Return a list of all available password database entries, \ |
| 119 | in arbitrary order.\n\ |
| 120 | See pwd.__doc__ for more on password database entries."; |
| 121 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 122 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 123 | pwd_getpwall(PyObject *self) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 124 | { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 125 | PyObject *d; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 126 | struct passwd *p; |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 127 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 128 | return NULL; |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 129 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 130 | if ((p = getpwuid(0)) != NULL) { |
| 131 | #else |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 132 | setpwent(); |
| 133 | while ((p = getpwent()) != NULL) { |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 134 | #endif |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 135 | PyObject *v = mkpwent(p); |
| 136 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 137 | Py_XDECREF(v); |
| 138 | Py_DECREF(d); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 139 | return NULL; |
| 140 | } |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 141 | Py_DECREF(v); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 142 | } |
Fred Drake | 8e68eb6 | 2001-03-11 03:03:07 +0000 | [diff] [blame] | 143 | endpwent(); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 144 | return d; |
| 145 | } |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 146 | #endif |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 147 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 148 | static PyMethodDef pwd_methods[] = { |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 149 | {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__}, |
| 150 | {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 151 | #ifdef HAVE_GETPWENT |
Neil Schemenauer | cc07ec1 | 2002-03-29 19:58:25 +0000 | [diff] [blame] | 152 | {"getpwall", (PyCFunction)pwd_getpwall, |
| 153 | METH_NOARGS, pwd_getpwall__doc__}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 154 | #endif |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 155 | {NULL, NULL} /* sentinel */ |
| 156 | }; |
| 157 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 158 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 159 | initpwd(void) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 160 | { |
Neal Norwitz | 726e013 | 2002-04-15 16:29:00 +0000 | [diff] [blame] | 161 | PyObject *m; |
Fred Drake | 88c9344 | 2002-04-13 21:07:45 +0000 | [diff] [blame] | 162 | m = Py_InitModule3("pwd", pwd_methods, pwd__doc__); |
| 163 | |
Martin v. Löwis | dbd55b3 | 2002-03-01 10:38:44 +0000 | [diff] [blame] | 164 | PyStructSequence_InitType(&StructPwdType, &struct_pwd_type_desc); |
Fred Drake | 88c9344 | 2002-04-13 21:07:45 +0000 | [diff] [blame] | 165 | Py_INCREF((PyObject *) &StructPwdType); |
| 166 | PyModule_AddObject(m, "struct_pwent", (PyObject *) &StructPwdType); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 167 | } |