blob: 4c1b7db87892cf57044b40de3e0fbbb0a70fdd88 [file] [log] [blame]
Guido van Rossum864407d1991-04-10 19:48:25 +00001
Guido van Rossumb6775db1994-08-01 11:34:53 +00002/* UNIX password file access module */
Guido van Rossum864407d1991-04-10 19:48:25 +00003
Barry Warsaw50c5cf11996-12-11 16:54:40 +00004#include "Python.h"
Guido van Rossum864407d1991-04-10 19:48:25 +00005
6#include <sys/types.h>
7#include <pwd.h>
Guido van Rossum864407d1991-04-10 19:48:25 +00008
Guido van Rossum3e79c441998-03-03 22:03:26 +00009static char pwd__doc__ [] = "\
10This module provides access to the Unix password database.\n\
11It is available on all Unix versions.\n\
12\n\
13Password database entries are reported as 7-tuples containing the following\n\
14items from the password database (see `<pwd.h>'), in order:\n\
15pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
16The uid and gid items are integers, all others are strings. An\n\
17exception is raised if the entry asked for cannot be found.";
18
19
Barry Warsaw50c5cf11996-12-11 16:54:40 +000020static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000021mkpwent(struct passwd *p)
Guido van Rossum864407d1991-04-10 19:48:25 +000022{
Barry Warsawbab218e1996-12-19 22:22:32 +000023 return Py_BuildValue(
24 "(ssllsss)",
25 p->pw_name,
26 p->pw_passwd,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000027#if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
28/* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
29 for later versions you may have to remove this */
Barry Warsawbab218e1996-12-19 22:22:32 +000030 (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */
31 (long)p->pw_short_pad2,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000032#else
Barry Warsawbab218e1996-12-19 22:22:32 +000033 (long)p->pw_uid,
34 (long)p->pw_gid,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000035#endif
Barry Warsawbab218e1996-12-19 22:22:32 +000036 p->pw_gecos,
37 p->pw_dir,
38 p->pw_shell);
Guido van Rossum864407d1991-04-10 19:48:25 +000039}
40
Guido van Rossum3e79c441998-03-03 22:03:26 +000041static char pwd_getpwuid__doc__[] = "\
42getpwuid(uid) -> entry\n\
43Return the password database entry for the given numeric user ID.\n\
44See pwd.__doc__ for more on password database entries.";
45
Barry Warsaw50c5cf11996-12-11 16:54:40 +000046static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000047pwd_getpwuid(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +000048{
49 int uid;
50 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000051 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossum864407d1991-04-10 19:48:25 +000052 return NULL;
53 if ((p = getpwuid(uid)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000054 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000055 return NULL;
56 }
57 return mkpwent(p);
58}
59
Guido van Rossum3e79c441998-03-03 22:03:26 +000060static char pwd_getpwnam__doc__[] = "\
61getpwnam(name) -> entry\n\
62Return the password database entry for the given user name.\n\
63See pwd.__doc__ for more on password database entries.";
64
Barry Warsaw50c5cf11996-12-11 16:54:40 +000065static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000066pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +000067{
Guido van Rossumef0a00e1992-01-27 16:51:30 +000068 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +000069 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000070 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum864407d1991-04-10 19:48:25 +000071 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +000072 if ((p = getpwnam(name)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000073 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000074 return NULL;
75 }
76 return mkpwent(p);
77}
78
Guido van Rossum1171ee61997-08-22 20:42:00 +000079#ifdef HAVE_GETPWENT
Guido van Rossum3e79c441998-03-03 22:03:26 +000080static char pwd_getpwall__doc__[] = "\
81getpwall() -> list_of_entries\n\
82Return a list of all available password database entries, \
83in arbitrary order.\n\
84See pwd.__doc__ for more on password database entries.";
85
Barry Warsaw50c5cf11996-12-11 16:54:40 +000086static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000087pwd_getpwall(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +000088{
Barry Warsaw50c5cf11996-12-11 16:54:40 +000089 PyObject *d;
Guido van Rossum864407d1991-04-10 19:48:25 +000090 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000091 if (!PyArg_NoArgs(args))
Guido van Rossum864407d1991-04-10 19:48:25 +000092 return NULL;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000093 if ((d = PyList_New(0)) == NULL)
Guido van Rossum864407d1991-04-10 19:48:25 +000094 return NULL;
95 setpwent();
96 while ((p = getpwent()) != NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000097 PyObject *v = mkpwent(p);
98 if (v == NULL || PyList_Append(d, v) != 0) {
99 Py_XDECREF(v);
100 Py_DECREF(d);
Guido van Rossum864407d1991-04-10 19:48:25 +0000101 return NULL;
102 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000103 Py_DECREF(v);
Guido van Rossum864407d1991-04-10 19:48:25 +0000104 }
Fred Drake8e68eb62001-03-11 03:03:07 +0000105 endpwent();
Guido van Rossum864407d1991-04-10 19:48:25 +0000106 return d;
107}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000108#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000109
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000110static PyMethodDef pwd_methods[] = {
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000111 {"getpwuid", pwd_getpwuid, METH_OLDARGS, pwd_getpwuid__doc__},
112 {"getpwnam", pwd_getpwnam, METH_OLDARGS, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000113#ifdef HAVE_GETPWENT
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000114 {"getpwall", pwd_getpwall, METH_OLDARGS, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000115#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000116 {NULL, NULL} /* sentinel */
117};
118
Guido van Rossum3886bb61998-12-04 18:50:17 +0000119DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000120initpwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000121{
Guido van Rossum3e79c441998-03-03 22:03:26 +0000122 Py_InitModule4("pwd", pwd_methods, pwd__doc__,
123 (PyObject *)NULL, PYTHON_API_VERSION);
Guido van Rossum864407d1991-04-10 19:48:25 +0000124}