blob: 00ea348a2abfb14ea12c7a1f983c855bacc4aacf [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) {
Guido van Rossum864407d1991-04-10 19:48:25 +0000123 return NULL;
124 }
Guido van Rossum864407d1991-04-10 19:48:25 +0000125 for (member = p->gr_mem; *member != NULL; member++) {
126 object *x = newstringobject(*member);
127 if (x == NULL || addlistitem(w, x) != 0) {
128 XDECREF(x);
Guido van Rossume5372401993-03-16 12:15:04 +0000129 DECREF(w);
Guido van Rossum864407d1991-04-10 19:48:25 +0000130 return NULL;
131 }
132 }
Guido van Rossume5372401993-03-16 12:15:04 +0000133 v = mkvalue("(sslO)",
134 p->gr_name,
135 p->gr_passwd,
136 (long)p->gr_gid,
137 w);
138 DECREF(w);
Guido van Rossum864407d1991-04-10 19:48:25 +0000139 return v;
140}
141
142static object *grp_getgrgid(self, args)
143 object *self, *args;
144{
145 int gid;
146 struct group *p;
147 if (!getintarg(args, &gid))
148 return NULL;
149 if ((p = getgrgid(gid)) == NULL) {
Guido van Rossum0e8e8721991-12-24 13:28:38 +0000150 err_setstr(KeyError, "getgrgid(): gid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +0000151 return NULL;
152 }
153 return mkgrent(p);
154}
155
156static object *grp_getgrnam(self, args)
157 object *self, *args;
158{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000159 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +0000160 struct group *p;
161 if (!getstrarg(args, &name))
162 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000163 if ((p = getgrnam(name)) == NULL) {
Guido van Rossum0e8e8721991-12-24 13:28:38 +0000164 err_setstr(KeyError, "getgrnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +0000165 return NULL;
166 }
167 return mkgrent(p);
168}
169
170static object *grp_getgrall(self, args)
171 object *self, *args;
172{
173 object *d;
174 struct group *p;
175 if (!getnoarg(args))
176 return NULL;
177 if ((d = newlistobject(0)) == NULL)
178 return NULL;
179 setgrent();
180 while ((p = getgrent()) != NULL) {
181 object *v = mkgrent(p);
182 if (v == NULL || addlistitem(d, v) != 0) {
183 XDECREF(v);
184 DECREF(d);
185 return NULL;
186 }
187 }
188 return d;
189}
190
191static struct methodlist grp_methods[] = {
192 {"getgrgid", grp_getgrgid},
193 {"getgrnam", grp_getgrnam},
194 {"getgrall", grp_getgrall},
195 {NULL, NULL} /* sentinel */
196};
197
198void
199initgrp()
200{
201 initmodule("grp", grp_methods);
202}