Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 32 | /* UNIX password file access module */ |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 33 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 35 | |
| 36 | #include <sys/types.h> |
| 37 | #include <pwd.h> |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 39 | static char pwd__doc__ [] = "\ |
| 40 | This module provides access to the Unix password database.\n\ |
| 41 | It is available on all Unix versions.\n\ |
| 42 | \n\ |
| 43 | Password database entries are reported as 7-tuples containing the following\n\ |
| 44 | items from the password database (see `<pwd.h>'), in order:\n\ |
| 45 | pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\ |
| 46 | The uid and gid items are integers, all others are strings. An\n\ |
| 47 | exception is raised if the entry asked for cannot be found."; |
| 48 | |
| 49 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 50 | static PyObject * |
| 51 | mkpwent(p) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 52 | struct passwd *p; |
| 53 | { |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 54 | #ifdef __BEOS__ |
| 55 | /* For faking the GECOS field. - [cjh] */ |
| 56 | char *be_user = NULL; |
| 57 | |
| 58 | be_user = getenv( "USER" ); |
| 59 | #endif |
| 60 | |
Barry Warsaw | bab218e | 1996-12-19 22:22:32 +0000 | [diff] [blame] | 61 | return Py_BuildValue( |
| 62 | "(ssllsss)", |
| 63 | p->pw_name, |
| 64 | p->pw_passwd, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 65 | #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__) |
| 66 | /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3; |
| 67 | for later versions you may have to remove this */ |
Barry Warsaw | bab218e | 1996-12-19 22:22:32 +0000 | [diff] [blame] | 68 | (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */ |
| 69 | (long)p->pw_short_pad2, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 70 | #else |
Barry Warsaw | bab218e | 1996-12-19 22:22:32 +0000 | [diff] [blame] | 71 | (long)p->pw_uid, |
| 72 | (long)p->pw_gid, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 73 | #endif |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 74 | #ifdef __BEOS__ |
| 75 | /* BeOS doesn't have a GECOS field, oddly enough. - [cjh] */ |
| 76 | be_user ? be_user : "baron", |
| 77 | #else |
Barry Warsaw | bab218e | 1996-12-19 22:22:32 +0000 | [diff] [blame] | 78 | p->pw_gecos, |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 79 | #endif |
Barry Warsaw | bab218e | 1996-12-19 22:22:32 +0000 | [diff] [blame] | 80 | p->pw_dir, |
| 81 | p->pw_shell); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 84 | static char pwd_getpwuid__doc__[] = "\ |
| 85 | getpwuid(uid) -> entry\n\ |
| 86 | Return the password database entry for the given numeric user ID.\n\ |
| 87 | See pwd.__doc__ for more on password database entries."; |
| 88 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 89 | static PyObject * |
| 90 | pwd_getpwuid(self, args) |
| 91 | PyObject *self; |
| 92 | PyObject *args; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 93 | { |
| 94 | int uid; |
| 95 | struct passwd *p; |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 96 | if (!PyArg_Parse(args, "i", &uid)) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 97 | return NULL; |
| 98 | if ((p = getpwuid(uid)) == NULL) { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 99 | PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found"); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 100 | return NULL; |
| 101 | } |
| 102 | return mkpwent(p); |
| 103 | } |
| 104 | |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 105 | static char pwd_getpwnam__doc__[] = "\ |
| 106 | getpwnam(name) -> entry\n\ |
| 107 | Return the password database entry for the given user name.\n\ |
| 108 | See pwd.__doc__ for more on password database entries."; |
| 109 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 110 | static PyObject * |
| 111 | pwd_getpwnam(self, args) |
| 112 | PyObject *self; |
| 113 | PyObject *args; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 114 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 115 | char *name; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 116 | struct passwd *p; |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 117 | if (!PyArg_Parse(args, "s", &name)) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 118 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 119 | if ((p = getpwnam(name)) == NULL) { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 120 | PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found"); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 121 | return NULL; |
| 122 | } |
| 123 | return mkpwent(p); |
| 124 | } |
| 125 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 126 | #ifdef HAVE_GETPWENT |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 127 | static char pwd_getpwall__doc__[] = "\ |
| 128 | getpwall() -> list_of_entries\n\ |
| 129 | Return a list of all available password database entries, \ |
| 130 | in arbitrary order.\n\ |
| 131 | See pwd.__doc__ for more on password database entries."; |
| 132 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 133 | static PyObject * |
| 134 | pwd_getpwall(self, args) |
| 135 | PyObject *self; |
| 136 | PyObject *args; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 137 | { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 138 | PyObject *d; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 139 | struct passwd *p; |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 140 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 141 | return NULL; |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 142 | if ((d = PyList_New(0)) == NULL) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 143 | return NULL; |
| 144 | setpwent(); |
| 145 | while ((p = getpwent()) != NULL) { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 146 | PyObject *v = mkpwent(p); |
| 147 | if (v == NULL || PyList_Append(d, v) != 0) { |
| 148 | Py_XDECREF(v); |
| 149 | Py_DECREF(d); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 150 | return NULL; |
| 151 | } |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 152 | Py_DECREF(v); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 153 | } |
| 154 | return d; |
| 155 | } |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 156 | #endif |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 157 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 158 | static PyMethodDef pwd_methods[] = { |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 159 | {"getpwuid", pwd_getpwuid, 0, pwd_getpwuid__doc__}, |
| 160 | {"getpwnam", pwd_getpwnam, 0, pwd_getpwnam__doc__}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 161 | #ifdef HAVE_GETPWENT |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 162 | {"getpwall", pwd_getpwall, 0, pwd_getpwall__doc__}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 163 | #endif |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 164 | {NULL, NULL} /* sentinel */ |
| 165 | }; |
| 166 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame^] | 167 | DL_EXPORT(void) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 168 | initpwd() |
| 169 | { |
Guido van Rossum | 3e79c44 | 1998-03-03 22:03:26 +0000 | [diff] [blame] | 170 | Py_InitModule4("pwd", pwd_methods, pwd__doc__, |
| 171 | (PyObject *)NULL, PYTHON_API_VERSION); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 172 | } |