blob: cb139bb036d6ef7d25d3f15daee2bb465b6894f2 [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"
Martin v. Löwisdbd55b32002-03-01 10:38:44 +00005#include "structseq.h"
Guido van Rossum864407d1991-04-10 19:48:25 +00006
7#include <sys/types.h>
8#include <pwd.h>
Guido van Rossum864407d1991-04-10 19:48:25 +00009
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000010static PyStructSequence_Field struct_pwd_type_fields[] = {
11 {"pw_name", "user name"},
12 {"pw_passwd", "password"},
13 {"pw_uid", "user id"},
14 {"pw_gid", "group id"},
15 {"pw_gecos", "real name"},
16 {"pw_dir", "home directory"},
17 {"pw_shell", "shell program"},
18 {0}
19};
20
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000021PyDoc_STRVAR(struct_passwd__doc__,
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000022"pwd.struct_passwd: Results from getpw*() routines.\n\n\
23This object may be accessed either as a tuple of\n\
24 (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000025or via the object attributes as named in the above tuple.");
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000026
27static PyStructSequence_Desc struct_pwd_type_desc = {
28 "pwd.struct_passwd",
29 struct_passwd__doc__,
30 struct_pwd_type_fields,
31 7,
32};
33
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000034PyDoc_STRVAR(pwd__doc__,
35"This module provides access to the Unix password database.\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +000036It is available on all Unix versions.\n\
37\n\
38Password database entries are reported as 7-tuples containing the following\n\
39items from the password database (see `<pwd.h>'), in order:\n\
40pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
41The uid and gid items are integers, all others are strings. An\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000042exception is raised if the entry asked for cannot be found.");
Guido van Rossum3e79c441998-03-03 22:03:26 +000043
44
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000045static PyTypeObject StructPwdType;
46
Barry Warsaw50c5cf11996-12-11 16:54:40 +000047static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000048mkpwent(struct passwd *p)
Guido van Rossum864407d1991-04-10 19:48:25 +000049{
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000050 int setIndex = 0;
51 PyObject *v = PyStructSequence_New(&StructPwdType);
52 if (v == NULL)
53 return NULL;
54
55#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
56#define SETS(i,val) PyStructSequence_SET_ITEM(v, i, PyString_FromString(val))
57
58 SETS(setIndex++, p->pw_name);
59 SETS(setIndex++, p->pw_passwd);
60 SETI(setIndex++, p->pw_uid);
61 SETI(setIndex++, p->pw_gid);
62 SETS(setIndex++, p->pw_gecos);
63 SETS(setIndex++, p->pw_dir);
64 SETS(setIndex++, p->pw_shell);
65
66#undef SETS
67#undef SETI
68
69 if (PyErr_Occurred()) {
70 Py_XDECREF(v);
71 return NULL;
72 }
73
74 return v;
Guido van Rossum864407d1991-04-10 19:48:25 +000075}
76
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000077PyDoc_STRVAR(pwd_getpwuid__doc__,
78"getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
79 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +000080Return the password database entry for the given numeric user ID.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000081See pwd.__doc__ for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +000082
Barry Warsaw50c5cf11996-12-11 16:54:40 +000083static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000084pwd_getpwuid(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +000085{
86 int uid;
87 struct passwd *p;
Neal Norwitzba3a16c2002-03-31 15:27:00 +000088 if (!PyArg_ParseTuple(args, "i:getpwuid", &uid))
Guido van Rossum864407d1991-04-10 19:48:25 +000089 return NULL;
90 if ((p = getpwuid(uid)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000091 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000092 return NULL;
93 }
94 return mkpwent(p);
95}
96
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000097PyDoc_STRVAR(pwd_getpwnam__doc__,
98"getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
99 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000100Return the password database entry for the given user name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000101See pwd.__doc__ for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000102
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000103static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000104pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000105{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000106 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +0000107 struct passwd *p;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000108 if (!PyArg_ParseTuple(args, "s:getpwnam", &name))
Guido van Rossum864407d1991-04-10 19:48:25 +0000109 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000110 if ((p = getpwnam(name)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000111 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +0000112 return NULL;
113 }
114 return mkpwent(p);
115}
116
Guido van Rossum1171ee61997-08-22 20:42:00 +0000117#ifdef HAVE_GETPWENT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000118PyDoc_STRVAR(pwd_getpwall__doc__,
119"getpwall() -> list_of_entries\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000120Return a list of all available password database entries, \
121in arbitrary order.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000122See pwd.__doc__ for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000123
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000124static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000125pwd_getpwall(PyObject *self)
Guido van Rossum864407d1991-04-10 19:48:25 +0000126{
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000127 PyObject *d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000128 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000129 if ((d = PyList_New(0)) == NULL)
Guido van Rossum864407d1991-04-10 19:48:25 +0000130 return NULL;
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000131#if defined(PYOS_OS2) && defined(PYCC_GCC)
132 if ((p = getpwuid(0)) != NULL) {
133#else
Guido van Rossum864407d1991-04-10 19:48:25 +0000134 setpwent();
135 while ((p = getpwent()) != NULL) {
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000136#endif
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000137 PyObject *v = mkpwent(p);
138 if (v == NULL || PyList_Append(d, v) != 0) {
139 Py_XDECREF(v);
140 Py_DECREF(d);
Guido van Rossum864407d1991-04-10 19:48:25 +0000141 return NULL;
142 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000143 Py_DECREF(v);
Guido van Rossum864407d1991-04-10 19:48:25 +0000144 }
Fred Drake8e68eb62001-03-11 03:03:07 +0000145 endpwent();
Guido van Rossum864407d1991-04-10 19:48:25 +0000146 return d;
147}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000148#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000149
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000150static PyMethodDef pwd_methods[] = {
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000151 {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
152 {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000153#ifdef HAVE_GETPWENT
Neil Schemenauercc07ec12002-03-29 19:58:25 +0000154 {"getpwall", (PyCFunction)pwd_getpwall,
155 METH_NOARGS, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000156#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000157 {NULL, NULL} /* sentinel */
158};
159
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000160PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000161initpwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000162{
Neal Norwitz726e0132002-04-15 16:29:00 +0000163 PyObject *m;
Fred Drake88c93442002-04-13 21:07:45 +0000164 m = Py_InitModule3("pwd", pwd_methods, pwd__doc__);
165
Martin v. Löwisdbd55b32002-03-01 10:38:44 +0000166 PyStructSequence_InitType(&StructPwdType, &struct_pwd_type_desc);
Fred Drake88c93442002-04-13 21:07:45 +0000167 Py_INCREF((PyObject *) &StructPwdType);
168 PyModule_AddObject(m, "struct_pwent", (PyObject *) &StructPwdType);
Guido van Rossum864407d1991-04-10 19:48:25 +0000169}