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