blob: 5775473697d72ad027f01934f361ff1d3b737992 [file] [log] [blame]
Guido van Rossum864407d1991-04-10 19:48:25 +00001/***********************************************************
Guido van Rossumb6775db1994-08-01 11:34:53 +00002Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
Guido van Rossume5372401993-03-16 12:15:04 +00003Amsterdam, 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
Guido van Rossumb6775db1994-08-01 11:34:53 +000025/* UNIX password file access module */
Guido van Rossum864407d1991-04-10 19:48:25 +000026
27#include "allobjects.h"
28#include "modsupport.h"
29
30#include <sys/types.h>
31#include <pwd.h>
Guido van Rossum864407d1991-04-10 19:48:25 +000032
33static object *mkpwent(p)
34 struct passwd *p;
35{
Guido van Rossume5372401993-03-16 12:15:04 +000036 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 Rossum864407d1991-04-10 19:48:25 +000044}
45
46static 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 Rossum0e8e8721991-12-24 13:28:38 +000054 err_setstr(KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000055 return NULL;
56 }
57 return mkpwent(p);
58}
59
60static object *pwd_getpwnam(self, args)
61 object *self, *args;
62{
Guido van Rossumef0a00e1992-01-27 16:51:30 +000063 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +000064 struct passwd *p;
65 if (!getstrarg(args, &name))
66 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +000067 if ((p = getpwnam(name)) == NULL) {
Guido van Rossum0e8e8721991-12-24 13:28:38 +000068 err_setstr(KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000069 return NULL;
70 }
71 return mkpwent(p);
72}
73
74static 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
95static struct methodlist pwd_methods[] = {
96 {"getpwuid", pwd_getpwuid},
97 {"getpwnam", pwd_getpwnam},
98 {"getpwall", pwd_getpwall},
99 {NULL, NULL} /* sentinel */
100};
101
102void
103initpwd()
104{
105 initmodule("pwd", pwd_methods);
106}