blob: 4d787baf3d16b7a195635c173babb7e70042f402 [file] [log] [blame]
Guido van Rossum864407d1991-04-10 19:48:25 +00001/***********************************************************
Guido van Rossumbab9d031992-04-05 14:26:55 +00002Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Guido van Rossum864407d1991-04-10 19:48:25 +00003Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF 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
38static 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
62static 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) {
Guido van Rossum0e8e8721991-12-24 13:28:38 +000070 err_setstr(KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000071 return NULL;
72 }
73 return mkpwent(p);
74}
75
76static object *pwd_getpwnam(self, args)
77 object *self, *args;
78{
Guido van Rossumef0a00e1992-01-27 16:51:30 +000079 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +000080 struct passwd *p;
81 if (!getstrarg(args, &name))
82 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +000083 if ((p = getpwnam(name)) == NULL) {
Guido van Rossum0e8e8721991-12-24 13:28:38 +000084 err_setstr(KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000085 return NULL;
86 }
87 return mkpwent(p);
88}
89
90static 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
111static struct methodlist pwd_methods[] = {
112 {"getpwuid", pwd_getpwuid},
113 {"getpwnam", pwd_getpwnam},
114 {"getpwall", pwd_getpwall},
115 {NULL, NULL} /* sentinel */
116};
117
118void
119initpwd()
120{
121 initmodule("pwd", pwd_methods);
122}
123
124
125/* Module grp */
126
127
128static 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
162static 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) {
Guido van Rossum0e8e8721991-12-24 13:28:38 +0000170 err_setstr(KeyError, "getgrgid(): gid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +0000171 return NULL;
172 }
173 return mkgrent(p);
174}
175
176static object *grp_getgrnam(self, args)
177 object *self, *args;
178{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000179 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +0000180 struct group *p;
181 if (!getstrarg(args, &name))
182 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000183 if ((p = getgrnam(name)) == NULL) {
Guido van Rossum0e8e8721991-12-24 13:28:38 +0000184 err_setstr(KeyError, "getgrnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +0000185 return NULL;
186 }
187 return mkgrent(p);
188}
189
190static 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
211static struct methodlist grp_methods[] = {
212 {"getgrgid", grp_getgrgid},
213 {"getgrnam", grp_getgrnam},
214 {"getgrall", grp_getgrall},
215 {NULL, NULL} /* sentinel */
216};
217
218void
219initgrp()
220{
221 initmodule("grp", grp_methods);
222}