blob: ec0e848f7b2b4d7bb2bf2a04a5f76e2b715d579b [file] [log] [blame]
Guido van Rossum864407d1991-04-10 19:48:25 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The 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,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000039#if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
40/* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
41 for later versions you may have to remove this */
42 (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */
43 (long)p->pw_short_pad2,
44#else
Guido van Rossume5372401993-03-16 12:15:04 +000045 (long)p->pw_uid,
46 (long)p->pw_gid,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000047#endif
Guido van Rossume5372401993-03-16 12:15:04 +000048 p->pw_gecos,
49 p->pw_dir,
50 p->pw_shell);
Guido van Rossum864407d1991-04-10 19:48:25 +000051}
52
53static object *pwd_getpwuid(self, args)
54 object *self, *args;
55{
56 int uid;
57 struct passwd *p;
58 if (!getintarg(args, &uid))
59 return NULL;
60 if ((p = getpwuid(uid)) == NULL) {
Guido van Rossum0e8e8721991-12-24 13:28:38 +000061 err_setstr(KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000062 return NULL;
63 }
64 return mkpwent(p);
65}
66
67static object *pwd_getpwnam(self, args)
68 object *self, *args;
69{
Guido van Rossumef0a00e1992-01-27 16:51:30 +000070 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +000071 struct passwd *p;
72 if (!getstrarg(args, &name))
73 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +000074 if ((p = getpwnam(name)) == NULL) {
Guido van Rossum0e8e8721991-12-24 13:28:38 +000075 err_setstr(KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000076 return NULL;
77 }
78 return mkpwent(p);
79}
80
81static object *pwd_getpwall(self, args)
82 object *self, *args;
83{
84 object *d;
85 struct passwd *p;
86 if (!getnoarg(args))
87 return NULL;
88 if ((d = newlistobject(0)) == NULL)
89 return NULL;
90 setpwent();
91 while ((p = getpwent()) != NULL) {
92 object *v = mkpwent(p);
93 if (v == NULL || addlistitem(d, v) != 0) {
94 XDECREF(v);
95 DECREF(d);
96 return NULL;
97 }
98 }
99 return d;
100}
101
102static struct methodlist pwd_methods[] = {
103 {"getpwuid", pwd_getpwuid},
104 {"getpwnam", pwd_getpwnam},
105 {"getpwall", pwd_getpwall},
106 {NULL, NULL} /* sentinel */
107};
108
109void
110initpwd()
111{
112 initmodule("pwd", pwd_methods);
113}