blob: 805d4d9a2c680cbfc3bbbf2f0fffd75700ac5c1c [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
Martin v. Löwis29275c92002-09-17 09:34:06 +000047static void
48sets(PyObject *v, int i, char* val)
49{
50 if (val)
51 PyStructSequence_SET_ITEM(v, i, PyString_FromString(val));
52 else {
53 PyStructSequence_SET_ITEM(v, i, Py_None);
54 Py_INCREF(Py_None);
55 }
56}
57
Barry Warsaw50c5cf11996-12-11 16:54:40 +000058static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000059mkpwent(struct passwd *p)
Guido van Rossum864407d1991-04-10 19:48:25 +000060{
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000061 int setIndex = 0;
62 PyObject *v = PyStructSequence_New(&StructPwdType);
63 if (v == NULL)
64 return NULL;
65
66#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
Martin v. Löwis29275c92002-09-17 09:34:06 +000067#define SETS(i,val) sets(v, i, val)
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000068
69 SETS(setIndex++, p->pw_name);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000070#ifdef __VMS
71 SETS(setIndex++, "");
72#else
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000073 SETS(setIndex++, p->pw_passwd);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000074#endif
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000075 SETI(setIndex++, p->pw_uid);
76 SETI(setIndex++, p->pw_gid);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000077#ifdef __VMS
78 SETS(setIndex++, "");
79#else
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000080 SETS(setIndex++, p->pw_gecos);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000081#endif
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000082 SETS(setIndex++, p->pw_dir);
83 SETS(setIndex++, p->pw_shell);
84
85#undef SETS
86#undef SETI
87
88 if (PyErr_Occurred()) {
89 Py_XDECREF(v);
90 return NULL;
91 }
92
93 return v;
Guido van Rossum864407d1991-04-10 19:48:25 +000094}
95
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000096PyDoc_STRVAR(pwd_getpwuid__doc__,
97"getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
98 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +000099Return the password database entry for the given numeric user ID.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000100See pwd.__doc__ for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000101
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000102static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000103pwd_getpwuid(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000104{
105 int uid;
106 struct passwd *p;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000107 if (!PyArg_ParseTuple(args, "i:getpwuid", &uid))
Guido van Rossum864407d1991-04-10 19:48:25 +0000108 return NULL;
109 if ((p = getpwuid(uid)) == NULL) {
Barry Warsawe1b1c872004-01-20 21:07:23 +0000110 PyErr_Format(PyExc_KeyError,
111 "getpwuid(): uid not found: %d", uid);
Guido van Rossum864407d1991-04-10 19:48:25 +0000112 return NULL;
113 }
114 return mkpwent(p);
115}
116
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000117PyDoc_STRVAR(pwd_getpwnam__doc__,
118"getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
119 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000120Return the password database entry for the given user name.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000121See pwd.__doc__ for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000122
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000123static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000124pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000125{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000126 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +0000127 struct passwd *p;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000128 if (!PyArg_ParseTuple(args, "s:getpwnam", &name))
Guido van Rossum864407d1991-04-10 19:48:25 +0000129 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000130 if ((p = getpwnam(name)) == NULL) {
Barry Warsawe1b1c872004-01-20 21:07:23 +0000131 PyErr_Format(PyExc_KeyError,
132 "getpwnam(): name not found: %s", name);
Guido van Rossum864407d1991-04-10 19:48:25 +0000133 return NULL;
134 }
135 return mkpwent(p);
136}
137
Guido van Rossum1171ee61997-08-22 20:42:00 +0000138#ifdef HAVE_GETPWENT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000139PyDoc_STRVAR(pwd_getpwall__doc__,
140"getpwall() -> list_of_entries\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000141Return a list of all available password database entries, \
142in arbitrary order.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000143See pwd.__doc__ for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000144
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000145static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000146pwd_getpwall(PyObject *self)
Guido van Rossum864407d1991-04-10 19:48:25 +0000147{
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000148 PyObject *d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000149 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000150 if ((d = PyList_New(0)) == NULL)
Guido van Rossum864407d1991-04-10 19:48:25 +0000151 return NULL;
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000152#if defined(PYOS_OS2) && defined(PYCC_GCC)
153 if ((p = getpwuid(0)) != NULL) {
154#else
Guido van Rossum864407d1991-04-10 19:48:25 +0000155 setpwent();
156 while ((p = getpwent()) != NULL) {
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000157#endif
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000158 PyObject *v = mkpwent(p);
159 if (v == NULL || PyList_Append(d, v) != 0) {
160 Py_XDECREF(v);
161 Py_DECREF(d);
Guido van Rossum864407d1991-04-10 19:48:25 +0000162 return NULL;
163 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000164 Py_DECREF(v);
Guido van Rossum864407d1991-04-10 19:48:25 +0000165 }
Fred Drake8e68eb62001-03-11 03:03:07 +0000166 endpwent();
Guido van Rossum864407d1991-04-10 19:48:25 +0000167 return d;
168}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000169#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000170
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000171static PyMethodDef pwd_methods[] = {
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000172 {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
173 {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000174#ifdef HAVE_GETPWENT
Neil Schemenauercc07ec12002-03-29 19:58:25 +0000175 {"getpwall", (PyCFunction)pwd_getpwall,
176 METH_NOARGS, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000177#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000178 {NULL, NULL} /* sentinel */
179};
180
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000181PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000182initpwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000183{
Neal Norwitz726e0132002-04-15 16:29:00 +0000184 PyObject *m;
Fred Drake88c93442002-04-13 21:07:45 +0000185 m = Py_InitModule3("pwd", pwd_methods, pwd__doc__);
186
Martin v. Löwisdbd55b32002-03-01 10:38:44 +0000187 PyStructSequence_InitType(&StructPwdType, &struct_pwd_type_desc);
Fred Drake88c93442002-04-13 21:07:45 +0000188 Py_INCREF((PyObject *) &StructPwdType);
Guido van Rossum16b9fa82003-10-23 23:54:57 +0000189 PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
190 /* And for b/w compatibility (this was defined by mistake): */
Fred Drake88c93442002-04-13 21:07:45 +0000191 PyModule_AddObject(m, "struct_pwent", (PyObject *) &StructPwdType);
Guido van Rossum864407d1991-04-10 19:48:25 +0000192}