blob: eb7a1cc3eb8417f3297a812b943d049de5c1403c [file] [log] [blame]
Guido van Rossum864407d1991-04-10 19:48:25 +00001/***********************************************************
Guido van Rossume5372401993-03-16 12:15:04 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
Guido van Rossum864407d1991-04-10 19:48:25 +00004
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{
Guido van Rossume5372401993-03-16 12:15:04 +000041 return mkvalue("(ssllsss)",
42 p->pw_name,
43 p->pw_passwd,
44 (long)p->pw_uid,
45 (long)p->pw_gid,
46 p->pw_gecos,
47 p->pw_dir,
48 p->pw_shell);
Guido van Rossum864407d1991-04-10 19:48:25 +000049}
50
51static object *pwd_getpwuid(self, args)
52 object *self, *args;
53{
54 int uid;
55 struct passwd *p;
56 if (!getintarg(args, &uid))
57 return NULL;
58 if ((p = getpwuid(uid)) == NULL) {
Guido van Rossum0e8e8721991-12-24 13:28:38 +000059 err_setstr(KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000060 return NULL;
61 }
62 return mkpwent(p);
63}
64
65static object *pwd_getpwnam(self, args)
66 object *self, *args;
67{
Guido van Rossumef0a00e1992-01-27 16:51:30 +000068 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +000069 struct passwd *p;
70 if (!getstrarg(args, &name))
71 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +000072 if ((p = getpwnam(name)) == NULL) {
Guido van Rossum0e8e8721991-12-24 13:28:38 +000073 err_setstr(KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000074 return NULL;
75 }
76 return mkpwent(p);
77}
78
79static object *pwd_getpwall(self, args)
80 object *self, *args;
81{
82 object *d;
83 struct passwd *p;
84 if (!getnoarg(args))
85 return NULL;
86 if ((d = newlistobject(0)) == NULL)
87 return NULL;
88 setpwent();
89 while ((p = getpwent()) != NULL) {
90 object *v = mkpwent(p);
91 if (v == NULL || addlistitem(d, v) != 0) {
92 XDECREF(v);
93 DECREF(d);
94 return NULL;
95 }
96 }
97 return d;
98}
99
100static struct methodlist pwd_methods[] = {
101 {"getpwuid", pwd_getpwuid},
102 {"getpwnam", pwd_getpwnam},
103 {"getpwall", pwd_getpwall},
104 {NULL, NULL} /* sentinel */
105};
106
107void
108initpwd()
109{
110 initmodule("pwd", pwd_methods);
111}
112
113
114/* Module grp */
115
116
117static object *mkgrent(p)
118 struct group *p;
119{
120 object *v, *w;
121 char **member;
Guido van Rossum864407d1991-04-10 19:48:25 +0000122 if ((w = newlistobject(0)) == NULL) {
123 DECREF(v);
124 return NULL;
125 }
Guido van Rossum864407d1991-04-10 19:48:25 +0000126 for (member = p->gr_mem; *member != NULL; member++) {
127 object *x = newstringobject(*member);
128 if (x == NULL || addlistitem(w, x) != 0) {
129 XDECREF(x);
Guido van Rossume5372401993-03-16 12:15:04 +0000130 DECREF(w);
Guido van Rossum864407d1991-04-10 19:48:25 +0000131 return NULL;
132 }
133 }
Guido van Rossume5372401993-03-16 12:15:04 +0000134 v = mkvalue("(sslO)",
135 p->gr_name,
136 p->gr_passwd,
137 (long)p->gr_gid,
138 w);
139 DECREF(w);
Guido van Rossum864407d1991-04-10 19:48:25 +0000140 return v;
141}
142
143static object *grp_getgrgid(self, args)
144 object *self, *args;
145{
146 int gid;
147 struct group *p;
148 if (!getintarg(args, &gid))
149 return NULL;
150 if ((p = getgrgid(gid)) == NULL) {
Guido van Rossum0e8e8721991-12-24 13:28:38 +0000151 err_setstr(KeyError, "getgrgid(): gid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +0000152 return NULL;
153 }
154 return mkgrent(p);
155}
156
157static object *grp_getgrnam(self, args)
158 object *self, *args;
159{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000160 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +0000161 struct group *p;
162 if (!getstrarg(args, &name))
163 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000164 if ((p = getgrnam(name)) == NULL) {
Guido van Rossum0e8e8721991-12-24 13:28:38 +0000165 err_setstr(KeyError, "getgrnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +0000166 return NULL;
167 }
168 return mkgrent(p);
169}
170
171static object *grp_getgrall(self, args)
172 object *self, *args;
173{
174 object *d;
175 struct group *p;
176 if (!getnoarg(args))
177 return NULL;
178 if ((d = newlistobject(0)) == NULL)
179 return NULL;
180 setgrent();
181 while ((p = getgrent()) != NULL) {
182 object *v = mkgrent(p);
183 if (v == NULL || addlistitem(d, v) != 0) {
184 XDECREF(v);
185 DECREF(d);
186 return NULL;
187 }
188 }
189 return d;
190}
191
192static struct methodlist grp_methods[] = {
193 {"getgrgid", grp_getgrgid},
194 {"getgrnam", grp_getgrnam},
195 {"getgrall", grp_getgrall},
196 {NULL, NULL} /* sentinel */
197};
198
199void
200initgrp()
201{
202 initmodule("grp", grp_methods);
203}