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 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 39 | static PyObject * |
| 40 | mkpwent(p) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 41 | struct passwd *p; |
| 42 | { |
Barry Warsaw | bab218e | 1996-12-19 22:22:32 +0000 | [diff] [blame] | 43 | return Py_BuildValue( |
| 44 | "(ssllsss)", |
| 45 | p->pw_name, |
| 46 | p->pw_passwd, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 47 | #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__) |
| 48 | /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3; |
| 49 | for later versions you may have to remove this */ |
Barry Warsaw | bab218e | 1996-12-19 22:22:32 +0000 | [diff] [blame] | 50 | (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */ |
| 51 | (long)p->pw_short_pad2, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 52 | #else |
Barry Warsaw | bab218e | 1996-12-19 22:22:32 +0000 | [diff] [blame] | 53 | (long)p->pw_uid, |
| 54 | (long)p->pw_gid, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 55 | #endif |
Barry Warsaw | bab218e | 1996-12-19 22:22:32 +0000 | [diff] [blame] | 56 | p->pw_gecos, |
| 57 | p->pw_dir, |
| 58 | p->pw_shell); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 61 | static PyObject * |
| 62 | pwd_getpwuid(self, args) |
| 63 | PyObject *self; |
| 64 | PyObject *args; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 65 | { |
| 66 | int uid; |
| 67 | struct passwd *p; |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 68 | if (!PyArg_Parse(args, "i", &uid)) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 69 | return NULL; |
| 70 | if ((p = getpwuid(uid)) == NULL) { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 71 | PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found"); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 72 | return NULL; |
| 73 | } |
| 74 | return mkpwent(p); |
| 75 | } |
| 76 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 77 | static PyObject * |
| 78 | pwd_getpwnam(self, args) |
| 79 | PyObject *self; |
| 80 | PyObject *args; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 81 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 82 | char *name; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 83 | struct passwd *p; |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 84 | if (!PyArg_Parse(args, "s", &name)) |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 85 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 86 | if ((p = getpwnam(name)) == NULL) { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 87 | PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found"); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 88 | return NULL; |
| 89 | } |
| 90 | return mkpwent(p); |
| 91 | } |
| 92 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 93 | static PyObject * |
| 94 | pwd_getpwall(self, args) |
| 95 | PyObject *self; |
| 96 | 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 | } |
| 116 | |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 117 | static PyMethodDef pwd_methods[] = { |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 118 | {"getpwuid", pwd_getpwuid}, |
| 119 | {"getpwnam", pwd_getpwnam}, |
| 120 | {"getpwall", pwd_getpwall}, |
| 121 | {NULL, NULL} /* sentinel */ |
| 122 | }; |
| 123 | |
| 124 | void |
| 125 | initpwd() |
| 126 | { |
Barry Warsaw | 50c5cf1 | 1996-12-11 16:54:40 +0000 | [diff] [blame] | 127 | Py_InitModule("pwd", pwd_methods); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 128 | } |