1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* UNIX password file access module */
15 #include <sys/types.h>
18 static char pwd__doc__
[] = "\
19 This module provides access to the Unix password database.\n\
20 It is available on all Unix versions.\n\
22 Password database entries are reported as 7-tuples containing the following\n\
23 items from the password database (see `<pwd.h>'), in order:\n\
24 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
25 The uid and gid items are integers, all others are strings. An\n\
26 exception is raised if the entry asked for cannot be found.";
30 mkpwent(struct passwd
*p
)
33 /* For faking the GECOS field. - [cjh] */
36 be_user
= getenv( "USER" );
43 #if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
44 /* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
45 for later versions you may have to remove this */
46 (long)p
->pw_short_pad1
, /* ugh-NeXT broke the padding */
47 (long)p
->pw_short_pad2
,
53 /* BeOS doesn't have a GECOS field, oddly enough. - [cjh] */
54 be_user
? be_user
: "baron",
62 static char pwd_getpwuid__doc__
[] = "\
63 getpwuid(uid) -> entry\n\
64 Return the password database entry for the given numeric user ID.\n\
65 See pwd.__doc__ for more on password database entries.";
68 pwd_getpwuid(PyObject
*self
, PyObject
*args
)
72 if (!PyArg_Parse(args
, "i", &uid
))
74 if ((p
= getpwuid(uid
)) == NULL
) {
75 PyErr_SetString(PyExc_KeyError
, "getpwuid(): uid not found");
81 static char pwd_getpwnam__doc__
[] = "\
82 getpwnam(name) -> entry\n\
83 Return the password database entry for the given user name.\n\
84 See pwd.__doc__ for more on password database entries.";
87 pwd_getpwnam(PyObject
*self
, PyObject
*args
)
91 if (!PyArg_Parse(args
, "s", &name
))
93 if ((p
= getpwnam(name
)) == NULL
) {
94 PyErr_SetString(PyExc_KeyError
, "getpwnam(): name not found");
101 static char pwd_getpwall__doc__
[] = "\
102 getpwall() -> list_of_entries\n\
103 Return a list of all available password database entries, \
104 in arbitrary order.\n\
105 See pwd.__doc__ for more on password database entries.";
108 pwd_getpwall(PyObject
*self
, PyObject
*args
)
112 if (!PyArg_NoArgs(args
))
114 if ((d
= PyList_New(0)) == NULL
)
117 while ((p
= getpwent()) != NULL
) {
118 PyObject
*v
= mkpwent(p
);
119 if (v
== NULL
|| PyList_Append(d
, v
) != 0) {
130 static PyMethodDef pwd_methods
[] = {
131 {"getpwuid", pwd_getpwuid
, METH_OLDARGS
, pwd_getpwuid__doc__
},
132 {"getpwnam", pwd_getpwnam
, METH_OLDARGS
, pwd_getpwnam__doc__
},
134 {"getpwall", pwd_getpwall
, METH_OLDARGS
, pwd_getpwall__doc__
},
136 {NULL
, NULL
} /* sentinel */
142 Py_InitModule4("pwd", pwd_methods
, pwd__doc__
,
143 (PyObject
*)NULL
, PYTHON_API_VERSION
);