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 | Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3 | Amsterdam, 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, |
| 39 | (long)p->pw_uid, |
| 40 | (long)p->pw_gid, |
| 41 | p->pw_gecos, |
| 42 | p->pw_dir, |
| 43 | p->pw_shell); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static object *pwd_getpwuid(self, args) |
| 47 | object *self, *args; |
| 48 | { |
| 49 | int uid; |
| 50 | struct passwd *p; |
| 51 | if (!getintarg(args, &uid)) |
| 52 | return NULL; |
| 53 | if ((p = getpwuid(uid)) == NULL) { |
Guido van Rossum | 0e8e872 | 1991-12-24 13:28:38 +0000 | [diff] [blame] | 54 | err_setstr(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 | |
| 60 | static object *pwd_getpwnam(self, args) |
| 61 | object *self, *args; |
| 62 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 63 | char *name; |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 64 | struct passwd *p; |
| 65 | if (!getstrarg(args, &name)) |
| 66 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 67 | if ((p = getpwnam(name)) == NULL) { |
Guido van Rossum | 0e8e872 | 1991-12-24 13:28:38 +0000 | [diff] [blame] | 68 | err_setstr(KeyError, "getpwnam(): name not found"); |
Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 69 | return NULL; |
| 70 | } |
| 71 | return mkpwent(p); |
| 72 | } |
| 73 | |
| 74 | static object *pwd_getpwall(self, args) |
| 75 | object *self, *args; |
| 76 | { |
| 77 | object *d; |
| 78 | struct passwd *p; |
| 79 | if (!getnoarg(args)) |
| 80 | return NULL; |
| 81 | if ((d = newlistobject(0)) == NULL) |
| 82 | return NULL; |
| 83 | setpwent(); |
| 84 | while ((p = getpwent()) != NULL) { |
| 85 | object *v = mkpwent(p); |
| 86 | if (v == NULL || addlistitem(d, v) != 0) { |
| 87 | XDECREF(v); |
| 88 | DECREF(d); |
| 89 | return NULL; |
| 90 | } |
| 91 | } |
| 92 | return d; |
| 93 | } |
| 94 | |
| 95 | static struct methodlist pwd_methods[] = { |
| 96 | {"getpwuid", pwd_getpwuid}, |
| 97 | {"getpwnam", pwd_getpwnam}, |
| 98 | {"getpwall", pwd_getpwall}, |
| 99 | {NULL, NULL} /* sentinel */ |
| 100 | }; |
| 101 | |
| 102 | void |
| 103 | initpwd() |
| 104 | { |
| 105 | initmodule("pwd", pwd_methods); |
| 106 | } |