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 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 25 | /* UNIX password file access module */ |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 26 | |
| 27 | #include "allobjects.h" |
| 28 | #include "modsupport.h" |
| 29 | |
| 30 | #include <sys/types.h> |
| 31 | #include <pwd.h> |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 32 | |
| 33 | static object *mkpwent(p) |
| 34 | struct passwd *p; |
| 35 | { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 36 | return mkvalue("(ssllsss)", |
| 37 | p->pw_name, |
| 38 | p->pw_passwd, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 39 | #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__) |
| 40 | /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3; |
| 41 | for later versions you may have to remove this */ |
| 42 | (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */ |
| 43 | (long)p->pw_short_pad2, |
| 44 | #else |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 45 | (long)p->pw_uid, |
| 46 | (long)p->pw_gid, |
Guido van Rossum | 03e8ffa | 1995-02-07 15:38:56 +0000 | [diff] [blame] | 47 | #endif |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 48 | p->pw_gecos, |
| 49 | p->pw_dir, |
| 50 | p->pw_shell); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static object *pwd_getpwuid(self, args) |
| 54 | object *self, *args; |
| 55 | { |
| 56 | int uid; |
| 57 | struct passwd *p; |
| 58 | if (!getintarg(args, &uid)) |
| 59 | return NULL; |
| 60 | if ((p = getpwuid(uid)) == NULL) { |
Guido van Rossum | 0e8e872 | 1991-12-24 13:28:38 +0000 | [diff] [blame] | 61 | err_setstr(KeyError, "getpwuid(): uid not found"); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 62 | return NULL; |
| 63 | } |
| 64 | return mkpwent(p); |
| 65 | } |
| 66 | |
| 67 | static object *pwd_getpwnam(self, args) |
| 68 | object *self, *args; |
| 69 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 70 | char *name; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 71 | struct passwd *p; |
| 72 | if (!getstrarg(args, &name)) |
| 73 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 74 | if ((p = getpwnam(name)) == NULL) { |
Guido van Rossum | 0e8e872 | 1991-12-24 13:28:38 +0000 | [diff] [blame] | 75 | err_setstr(KeyError, "getpwnam(): name not found"); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 76 | return NULL; |
| 77 | } |
| 78 | return mkpwent(p); |
| 79 | } |
| 80 | |
| 81 | static object *pwd_getpwall(self, args) |
| 82 | object *self, *args; |
| 83 | { |
| 84 | object *d; |
| 85 | struct passwd *p; |
| 86 | if (!getnoarg(args)) |
| 87 | return NULL; |
| 88 | if ((d = newlistobject(0)) == NULL) |
| 89 | return NULL; |
| 90 | setpwent(); |
| 91 | while ((p = getpwent()) != NULL) { |
| 92 | object *v = mkpwent(p); |
| 93 | if (v == NULL || addlistitem(d, v) != 0) { |
| 94 | XDECREF(v); |
| 95 | DECREF(d); |
| 96 | return NULL; |
| 97 | } |
| 98 | } |
| 99 | return d; |
| 100 | } |
| 101 | |
| 102 | static struct methodlist pwd_methods[] = { |
| 103 | {"getpwuid", pwd_getpwuid}, |
| 104 | {"getpwnam", pwd_getpwnam}, |
| 105 | {"getpwall", pwd_getpwall}, |
| 106 | {NULL, NULL} /* sentinel */ |
| 107 | }; |
| 108 | |
| 109 | void |
| 110 | initpwd() |
| 111 | { |
| 112 | initmodule("pwd", pwd_methods); |
| 113 | } |