blob: eeed608838b157b3be79ce2db1ee55f856f3a4af [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
21static char struct_passwd__doc__[] =
22"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\
25or via the object attributes as named in the above tuple.\n";
26
27static PyStructSequence_Desc struct_pwd_type_desc = {
28 "pwd.struct_passwd",
29 struct_passwd__doc__,
30 struct_pwd_type_fields,
31 7,
32};
33
Guido van Rossum3e79c441998-03-03 22:03:26 +000034static char pwd__doc__ [] = "\
35This module provides access to the Unix password database.\n\
36It 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\
42exception is raised if the entry asked for cannot be found.";
43
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
Guido van Rossum3e79c441998-03-03 22:03:26 +000077static char pwd_getpwuid__doc__[] = "\
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000078getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +000079Return the password database entry for the given numeric user ID.\n\
80See pwd.__doc__ for more on password database entries.";
81
Barry Warsaw50c5cf11996-12-11 16:54:40 +000082static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000083pwd_getpwuid(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +000084{
85 int uid;
86 struct passwd *p;
Neal Norwitzba3a16c2002-03-31 15:27:00 +000087 if (!PyArg_ParseTuple(args, "i:getpwuid", &uid))
Guido van Rossum864407d1991-04-10 19:48:25 +000088 return NULL;
89 if ((p = getpwuid(uid)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000090 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000091 return NULL;
92 }
93 return mkpwent(p);
94}
95
Guido van Rossum3e79c441998-03-03 22:03:26 +000096static char pwd_getpwnam__doc__[] = "\
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000097getpwnam(name) -> (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +000098Return the password database entry for the given user name.\n\
99See pwd.__doc__ for more on password database entries.";
100
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000101static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000102pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000103{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000104 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +0000105 struct passwd *p;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000106 if (!PyArg_ParseTuple(args, "s:getpwnam", &name))
Guido van Rossum864407d1991-04-10 19:48:25 +0000107 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000108 if ((p = getpwnam(name)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000109 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +0000110 return NULL;
111 }
112 return mkpwent(p);
113}
114
Guido van Rossum1171ee61997-08-22 20:42:00 +0000115#ifdef HAVE_GETPWENT
Guido van Rossum3e79c441998-03-03 22:03:26 +0000116static char pwd_getpwall__doc__[] = "\
117getpwall() -> list_of_entries\n\
118Return a list of all available password database entries, \
119in arbitrary order.\n\
120See pwd.__doc__ for more on password database entries.";
121
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000122static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000123pwd_getpwall(PyObject *self)
Guido van Rossum864407d1991-04-10 19:48:25 +0000124{
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000125 PyObject *d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000126 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000127 if ((d = PyList_New(0)) == NULL)
Guido van Rossum864407d1991-04-10 19:48:25 +0000128 return NULL;
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000129#if defined(PYOS_OS2) && defined(PYCC_GCC)
130 if ((p = getpwuid(0)) != NULL) {
131#else
Guido van Rossum864407d1991-04-10 19:48:25 +0000132 setpwent();
133 while ((p = getpwent()) != NULL) {
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000134#endif
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000135 PyObject *v = mkpwent(p);
136 if (v == NULL || PyList_Append(d, v) != 0) {
137 Py_XDECREF(v);
138 Py_DECREF(d);
Guido van Rossum864407d1991-04-10 19:48:25 +0000139 return NULL;
140 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000141 Py_DECREF(v);
Guido van Rossum864407d1991-04-10 19:48:25 +0000142 }
Fred Drake8e68eb62001-03-11 03:03:07 +0000143 endpwent();
Guido van Rossum864407d1991-04-10 19:48:25 +0000144 return d;
145}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000146#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000147
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000148static PyMethodDef pwd_methods[] = {
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000149 {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
150 {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000151#ifdef HAVE_GETPWENT
Neil Schemenauercc07ec12002-03-29 19:58:25 +0000152 {"getpwall", (PyCFunction)pwd_getpwall,
153 METH_NOARGS, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000154#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000155 {NULL, NULL} /* sentinel */
156};
157
Guido van Rossum3886bb61998-12-04 18:50:17 +0000158DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000159initpwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000160{
Martin v. Löwisdbd55b32002-03-01 10:38:44 +0000161 PyObject *m, *d;
Fred Drake88c93442002-04-13 21:07:45 +0000162 m = Py_InitModule3("pwd", pwd_methods, pwd__doc__);
163
Martin v. Löwisdbd55b32002-03-01 10:38:44 +0000164 PyStructSequence_InitType(&StructPwdType, &struct_pwd_type_desc);
Fred Drake88c93442002-04-13 21:07:45 +0000165 Py_INCREF((PyObject *) &StructPwdType);
166 PyModule_AddObject(m, "struct_pwent", (PyObject *) &StructPwdType);
Guido van Rossum864407d1991-04-10 19:48:25 +0000167}