Guido van Rossum | 864407d | 1991-04-10 19:48:25 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | Netherlands. |
| 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 | |
| 25 | /* Passwd/group file access module */ |
| 26 | |
| 27 | #include "allobjects.h" |
| 28 | #include "modsupport.h" |
| 29 | |
| 30 | #include <sys/types.h> |
| 31 | #include <pwd.h> |
| 32 | #include <grp.h> |
| 33 | |
| 34 | |
| 35 | /* Module pwd */ |
| 36 | |
| 37 | |
| 38 | static object *mkpwent(p) |
| 39 | struct passwd *p; |
| 40 | { |
| 41 | object *v; |
| 42 | if ((v = newtupleobject(7)) == NULL) |
| 43 | return NULL; |
| 44 | #define ISET(i, member) settupleitem(v, i, newintobject((long)p->member)) |
| 45 | #define SSET(i, member) settupleitem(v, i, newstringobject(p->member)) |
| 46 | SSET(0, pw_name); |
| 47 | SSET(1, pw_passwd); |
| 48 | ISET(2, pw_uid); |
| 49 | ISET(3, pw_gid); |
| 50 | SSET(4, pw_gecos); |
| 51 | SSET(5, pw_dir); |
| 52 | SSET(6, pw_shell); |
| 53 | #undef SSET |
| 54 | #undef ISET |
| 55 | if (err_occurred()) { |
| 56 | DECREF(v); |
| 57 | return NULL; |
| 58 | } |
| 59 | return v; |
| 60 | } |
| 61 | |
| 62 | static object *pwd_getpwuid(self, args) |
| 63 | object *self, *args; |
| 64 | { |
| 65 | int uid; |
| 66 | struct passwd *p; |
| 67 | if (!getintarg(args, &uid)) |
| 68 | return NULL; |
| 69 | if ((p = getpwuid(uid)) == NULL) { |
| 70 | err_setstr(RuntimeError, "getpwuid(): uid not found"); |
| 71 | return NULL; |
| 72 | } |
| 73 | return mkpwent(p); |
| 74 | } |
| 75 | |
| 76 | static object *pwd_getpwnam(self, args) |
| 77 | object *self, *args; |
| 78 | { |
| 79 | object *name; |
| 80 | struct passwd *p; |
| 81 | if (!getstrarg(args, &name)) |
| 82 | return NULL; |
| 83 | if ((p = getpwnam(getstringvalue(name))) == NULL) { |
| 84 | err_setstr(RuntimeError, "getpwnam(): name not found"); |
| 85 | return NULL; |
| 86 | } |
| 87 | return mkpwent(p); |
| 88 | } |
| 89 | |
| 90 | static object *pwd_getpwall(self, args) |
| 91 | object *self, *args; |
| 92 | { |
| 93 | object *d; |
| 94 | struct passwd *p; |
| 95 | if (!getnoarg(args)) |
| 96 | return NULL; |
| 97 | if ((d = newlistobject(0)) == NULL) |
| 98 | return NULL; |
| 99 | setpwent(); |
| 100 | while ((p = getpwent()) != NULL) { |
| 101 | object *v = mkpwent(p); |
| 102 | if (v == NULL || addlistitem(d, v) != 0) { |
| 103 | XDECREF(v); |
| 104 | DECREF(d); |
| 105 | return NULL; |
| 106 | } |
| 107 | } |
| 108 | return d; |
| 109 | } |
| 110 | |
| 111 | static struct methodlist pwd_methods[] = { |
| 112 | {"getpwuid", pwd_getpwuid}, |
| 113 | {"getpwnam", pwd_getpwnam}, |
| 114 | {"getpwall", pwd_getpwall}, |
| 115 | {NULL, NULL} /* sentinel */ |
| 116 | }; |
| 117 | |
| 118 | void |
| 119 | initpwd() |
| 120 | { |
| 121 | initmodule("pwd", pwd_methods); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /* Module grp */ |
| 126 | |
| 127 | |
| 128 | static object *mkgrent(p) |
| 129 | struct group *p; |
| 130 | { |
| 131 | object *v, *w; |
| 132 | char **member; |
| 133 | if ((v = newtupleobject(4)) == NULL) |
| 134 | return NULL; |
| 135 | #define ISET(i, member) settupleitem(v, i, newintobject((long)p->member)) |
| 136 | #define SSET(i, member) settupleitem(v, i, newstringobject(p->member)) |
| 137 | SSET(0, gr_name); |
| 138 | SSET(1, gr_passwd); |
| 139 | ISET(2, gr_gid); |
| 140 | #undef SSET |
| 141 | #undef ISET |
| 142 | if (err_occurred()) { |
| 143 | DECREF(v); |
| 144 | return NULL; |
| 145 | } |
| 146 | if ((w = newlistobject(0)) == NULL) { |
| 147 | DECREF(v); |
| 148 | return NULL; |
| 149 | } |
| 150 | (void) settupleitem(v, 3, w); /* Cannot fail; eats refcnt */ |
| 151 | for (member = p->gr_mem; *member != NULL; member++) { |
| 152 | object *x = newstringobject(*member); |
| 153 | if (x == NULL || addlistitem(w, x) != 0) { |
| 154 | XDECREF(x); |
| 155 | DECREF(v); |
| 156 | return NULL; |
| 157 | } |
| 158 | } |
| 159 | return v; |
| 160 | } |
| 161 | |
| 162 | static object *grp_getgrgid(self, args) |
| 163 | object *self, *args; |
| 164 | { |
| 165 | int gid; |
| 166 | struct group *p; |
| 167 | if (!getintarg(args, &gid)) |
| 168 | return NULL; |
| 169 | if ((p = getgrgid(gid)) == NULL) { |
| 170 | err_setstr(RuntimeError, "getgrgid(): gid not found"); |
| 171 | return NULL; |
| 172 | } |
| 173 | return mkgrent(p); |
| 174 | } |
| 175 | |
| 176 | static object *grp_getgrnam(self, args) |
| 177 | object *self, *args; |
| 178 | { |
| 179 | object *name; |
| 180 | struct group *p; |
| 181 | if (!getstrarg(args, &name)) |
| 182 | return NULL; |
| 183 | if ((p = getgrnam(getstringvalue(name))) == NULL) { |
| 184 | err_setstr(RuntimeError, "getgrnam(): name not found"); |
| 185 | return NULL; |
| 186 | } |
| 187 | return mkgrent(p); |
| 188 | } |
| 189 | |
| 190 | static object *grp_getgrall(self, args) |
| 191 | object *self, *args; |
| 192 | { |
| 193 | object *d; |
| 194 | struct group *p; |
| 195 | if (!getnoarg(args)) |
| 196 | return NULL; |
| 197 | if ((d = newlistobject(0)) == NULL) |
| 198 | return NULL; |
| 199 | setgrent(); |
| 200 | while ((p = getgrent()) != NULL) { |
| 201 | object *v = mkgrent(p); |
| 202 | if (v == NULL || addlistitem(d, v) != 0) { |
| 203 | XDECREF(v); |
| 204 | DECREF(d); |
| 205 | return NULL; |
| 206 | } |
| 207 | } |
| 208 | return d; |
| 209 | } |
| 210 | |
| 211 | static struct methodlist grp_methods[] = { |
| 212 | {"getgrgid", grp_getgrgid}, |
| 213 | {"getgrnam", grp_getgrnam}, |
| 214 | {"getgrall", grp_getgrall}, |
| 215 | {NULL, NULL} /* sentinel */ |
| 216 | }; |
| 217 | |
| 218 | void |
| 219 | initgrp() |
| 220 | { |
| 221 | initmodule("grp", grp_methods); |
| 222 | } |