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 | |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 9 | static char pwd__doc__ [] = "\ |
| 10 | This module provides access to the Unix password database.\n\ |
| 11 | It is available on all Unix versions.\n\ |
| 12 | \n\ |
| 13 | Password database entries are reported as 7-tuples containing the following\n\ |
| 14 | items from the password database (see `<pwd.h>'), in order:\n\ |
| 15 | pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\ |
| 16 | The uid and gid items are integers, all others are strings. An\n\ |
| 17 | exception is raised if the entry asked for cannot be found."; |
| 18 | |
| 19 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 20 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 21 | mkpwent(struct passwd *p) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 22 | { |
Barry Warsaw | bab218e | 1996-12-19 22:22:32 +0000 | [diff] [blame] | 23 | return Py_BuildValue( |
| 24 | "(ssllsss)", |
| 25 | p->pw_name, |
| 26 | p->pw_passwd, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 27 | #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__) |
| 28 | /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3; |
| 29 | for later versions you may have to remove this */ |
Barry Warsaw | bab218e | 1996-12-19 22:22:32 +0000 | [diff] [blame] | 30 | (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */ |
| 31 | (long)p->pw_short_pad2, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 32 | #else |
Barry Warsaw | bab218e | 1996-12-19 22:22:32 +0000 | [diff] [blame] | 33 | (long)p->pw_uid, |
| 34 | (long)p->pw_gid, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 35 | #endif |
Barry Warsaw | bab218e | 1996-12-19 22:22:32 +0000 | [diff] [blame] | 36 | p->pw_gecos, |
| 37 | p->pw_dir, |
| 38 | p->pw_shell); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 41 | static char pwd_getpwuid__doc__[] = "\ |
| 42 | getpwuid(uid) -> entry\n\ |
| 43 | Return the password database entry for the given numeric user ID.\n\ |
| 44 | See pwd.__doc__ for more on password database entries."; |
| 45 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 46 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 47 | pwd_getpwuid(PyObject *self, PyObject *args) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 48 | { |
| 49 | int uid; |
| 50 | struct passwd *p; |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 51 | if (!PyArg_Parse(args, "i", &uid)) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 52 | return NULL; |
| 53 | if ((p = getpwuid(uid)) == NULL) { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 54 | PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found"); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 55 | return NULL; |
| 56 | } |
| 57 | return mkpwent(p); |
| 58 | } |
| 59 | |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 60 | static char pwd_getpwnam__doc__[] = "\ |
| 61 | getpwnam(name) -> entry\n\ |
| 62 | Return the password database entry for the given user name.\n\ |
| 63 | See pwd.__doc__ for more on password database entries."; |
| 64 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 65 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 66 | pwd_getpwnam(PyObject *self, PyObject *args) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 67 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 68 | char *name; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 69 | struct passwd *p; |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 70 | if (!PyArg_Parse(args, "s", &name)) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 71 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 72 | if ((p = getpwnam(name)) == NULL) { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 73 | PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found"); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 74 | return NULL; |
| 75 | } |
| 76 | return mkpwent(p); |
| 77 | } |
| 78 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 79 | #ifdef HAVE_GETPWENT |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 80 | static char pwd_getpwall__doc__[] = "\ |
| 81 | getpwall() -> list_of_entries\n\ |
| 82 | Return a list of all available password database entries, \ |
| 83 | in arbitrary order.\n\ |
| 84 | See pwd.__doc__ for more on password database entries."; |
| 85 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 86 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 87 | pwd_getpwall(PyObject *self, PyObject *args) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 88 | { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 89 | PyObject *d; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 90 | struct passwd *p; |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 91 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 92 | return NULL; |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 93 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 94 | return NULL; |
| 95 | setpwent(); |
| 96 | while ((p = getpwent()) != NULL) { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 97 | PyObject *v = mkpwent(p); |
| 98 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 99 | Py_XDECREF(v); |
| 100 | Py_DECREF(d); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 101 | return NULL; |
| 102 | } |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 103 | Py_DECREF(v); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 104 | } |
Fred Drake | 8e68eb6 | 2001-03-11 03:03:07 +0000 | [diff] [blame] | 105 | endpwent(); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 106 | return d; |
| 107 | } |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 108 | #endif |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 109 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 110 | static PyMethodDef pwd_methods[] = { |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 111 | {"getpwuid", pwd_getpwuid, METH_OLDARGS, pwd_getpwuid__doc__}, |
| 112 | {"getpwnam", pwd_getpwnam, METH_OLDARGS, pwd_getpwnam__doc__}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 113 | #ifdef HAVE_GETPWENT |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 114 | {"getpwall", pwd_getpwall, METH_OLDARGS, pwd_getpwall__doc__}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 115 | #endif |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 116 | {NULL, NULL} /* sentinel */ |
| 117 | }; |
| 118 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 119 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 120 | initpwd(void) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 121 | { |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 122 | Py_InitModule4("pwd", pwd_methods, pwd__doc__, |
| 123 | (PyObject *)NULL, PYTHON_API_VERSION); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 124 | } |