pwdmodule.c 4.49 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1

2
/* UNIX password file access module */
Guido van Rossum's avatar
Guido van Rossum committed
3

Barry Warsaw's avatar
Barry Warsaw committed
4
#include "Python.h"
5
#include "structseq.h"
Guido van Rossum's avatar
Guido van Rossum committed
6 7 8 9

#include <sys/types.h>
#include <pwd.h>

10 11 12 13 14 15 16 17 18 19 20
static PyStructSequence_Field struct_pwd_type_fields[] = {
	{"pw_name", "user name"},
	{"pw_passwd", "password"},
	{"pw_uid", "user id"},
	{"pw_gid", "group id"}, 
	{"pw_gecos", "real name"}, 
	{"pw_dir", "home directory"},
	{"pw_shell", "shell program"},
	{0}
};

21
PyDoc_STRVAR(struct_passwd__doc__,
22 23 24
"pwd.struct_passwd: Results from getpw*() routines.\n\n\
This object may be accessed either as a tuple of\n\
  (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
25
or via the object attributes as named in the above tuple.");
26 27 28 29 30 31 32 33

static PyStructSequence_Desc struct_pwd_type_desc = {
	"pwd.struct_passwd",
	struct_passwd__doc__,
	struct_pwd_type_fields,
	7,
};

34 35
PyDoc_STRVAR(pwd__doc__,
"This module provides access to the Unix password database.\n\
36 37 38 39 40 41
It is available on all Unix versions.\n\
\n\
Password database entries are reported as 7-tuples containing the following\n\
items from the password database (see `<pwd.h>'), in order:\n\
pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
The uid and gid items are integers, all others are strings. An\n\
42
exception is raised if the entry asked for cannot be found.");
43 44

      
45 46
static PyTypeObject StructPwdType;

47 48 49 50 51 52 53 54 55 56 57
static void
sets(PyObject *v, int i, char* val)
{
  if (val)
	  PyStructSequence_SET_ITEM(v, i, PyString_FromString(val));
  else {
	  PyStructSequence_SET_ITEM(v, i, Py_None);
	  Py_INCREF(Py_None);
  }
}

Barry Warsaw's avatar
Barry Warsaw committed
58
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
59
mkpwent(struct passwd *p)
Guido van Rossum's avatar
Guido van Rossum committed
60
{
61 62 63 64 65 66
	int setIndex = 0;
	PyObject *v = PyStructSequence_New(&StructPwdType);
	if (v == NULL)
		return NULL;

#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
67
#define SETS(i,val) sets(v, i, val)
68 69

	SETS(setIndex++, p->pw_name);
70 71 72
#ifdef __VMS
	SETS(setIndex++, "");
#else
73
	SETS(setIndex++, p->pw_passwd);
74
#endif
75 76
	SETI(setIndex++, p->pw_uid);
	SETI(setIndex++, p->pw_gid);
77 78 79
#ifdef __VMS
	SETS(setIndex++, "");
#else
80
	SETS(setIndex++, p->pw_gecos);
81
#endif
82 83 84 85 86 87 88 89 90 91 92 93
	SETS(setIndex++, p->pw_dir);
	SETS(setIndex++, p->pw_shell);

#undef SETS
#undef SETI

	if (PyErr_Occurred()) {
		Py_XDECREF(v);
		return NULL;
	}

	return v;
Guido van Rossum's avatar
Guido van Rossum committed
94 95
}

96 97 98
PyDoc_STRVAR(pwd_getpwuid__doc__,
"getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
                  pw_gid,pw_gecos,pw_dir,pw_shell)\n\
99
Return the password database entry for the given numeric user ID.\n\
100
See pwd.__doc__ for more on password database entries.");
101

Barry Warsaw's avatar
Barry Warsaw committed
102
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
103
pwd_getpwuid(PyObject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
104 105 106
{
	int uid;
	struct passwd *p;
Neal Norwitz's avatar
Neal Norwitz committed
107
	if (!PyArg_ParseTuple(args, "i:getpwuid", &uid))
Guido van Rossum's avatar
Guido van Rossum committed
108 109
		return NULL;
	if ((p = getpwuid(uid)) == NULL) {
Barry Warsaw's avatar
Barry Warsaw committed
110
		PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
Guido van Rossum's avatar
Guido van Rossum committed
111 112 113 114 115
		return NULL;
	}
	return mkpwent(p);
}

116 117 118
PyDoc_STRVAR(pwd_getpwnam__doc__,
"getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
                    pw_gid,pw_gecos,pw_dir,pw_shell)\n\
119
Return the password database entry for the given user name.\n\
120
See pwd.__doc__ for more on password database entries.");
121

Barry Warsaw's avatar
Barry Warsaw committed
122
static PyObject *
Peter Schneider-Kamp's avatar
Peter Schneider-Kamp committed
123
pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
124
{
125
	char *name;
Guido van Rossum's avatar
Guido van Rossum committed
126
	struct passwd *p;
Neal Norwitz's avatar
Neal Norwitz committed
127
	if (!PyArg_ParseTuple(args, "s:getpwnam", &name))
Guido van Rossum's avatar
Guido van Rossum committed
128
		return NULL;
129
	if ((p = getpwnam(name)) == NULL) {
Barry Warsaw's avatar
Barry Warsaw committed
130
		PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
Guido van Rossum's avatar
Guido van Rossum committed
131 132 133 134 135
		return NULL;
	}
	return mkpwent(p);
}

136
#ifdef HAVE_GETPWENT
137 138
PyDoc_STRVAR(pwd_getpwall__doc__,
"getpwall() -> list_of_entries\n\
139 140
Return a list of all available password database entries, \
in arbitrary order.\n\
141
See pwd.__doc__ for more on password database entries.");
142

Barry Warsaw's avatar
Barry Warsaw committed
143
static PyObject *
144
pwd_getpwall(PyObject *self)
Guido van Rossum's avatar
Guido van Rossum committed
145
{
Barry Warsaw's avatar
Barry Warsaw committed
146
	PyObject *d;
Guido van Rossum's avatar
Guido van Rossum committed
147
	struct passwd *p;
Barry Warsaw's avatar
Barry Warsaw committed
148
	if ((d = PyList_New(0)) == NULL)
Guido van Rossum's avatar
Guido van Rossum committed
149
		return NULL;
150 151 152
#if defined(PYOS_OS2) && defined(PYCC_GCC)
	if ((p = getpwuid(0)) != NULL) {
#else
Guido van Rossum's avatar
Guido van Rossum committed
153 154
	setpwent();
	while ((p = getpwent()) != NULL) {
155
#endif
Barry Warsaw's avatar
Barry Warsaw committed
156 157 158 159
		PyObject *v = mkpwent(p);
		if (v == NULL || PyList_Append(d, v) != 0) {
			Py_XDECREF(v);
			Py_DECREF(d);
Guido van Rossum's avatar
Guido van Rossum committed
160 161
			return NULL;
		}
162
		Py_DECREF(v);
Guido van Rossum's avatar
Guido van Rossum committed
163
	}
164
	endpwent();
Guido van Rossum's avatar
Guido van Rossum committed
165 166
	return d;
}
167
#endif
Guido van Rossum's avatar
Guido van Rossum committed
168

Barry Warsaw's avatar
Barry Warsaw committed
169
static PyMethodDef pwd_methods[] = {
Neal Norwitz's avatar
Neal Norwitz committed
170 171
	{"getpwuid",	pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
	{"getpwnam",	pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
172
#ifdef HAVE_GETPWENT
Neil Schemenauer's avatar
Neil Schemenauer committed
173 174
	{"getpwall",	(PyCFunction)pwd_getpwall,
		METH_NOARGS,  pwd_getpwall__doc__},
175
#endif
Guido van Rossum's avatar
Guido van Rossum committed
176 177 178
	{NULL,		NULL}		/* sentinel */
};

179
PyMODINIT_FUNC
180
initpwd(void)
Guido van Rossum's avatar
Guido van Rossum committed
181
{
182
	PyObject *m;
183 184
	m = Py_InitModule3("pwd", pwd_methods, pwd__doc__);

185
	PyStructSequence_InitType(&StructPwdType, &struct_pwd_type_desc);
186 187
	Py_INCREF((PyObject *) &StructPwdType);
	PyModule_AddObject(m, "struct_pwent", (PyObject *) &StructPwdType);
Guido van Rossum's avatar
Guido van Rossum committed
188
}