blob: c2907f6e981599e87e5c6affe3940a03dbf40eeb [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"
Serhiy Storchakada5c2a02013-02-12 09:27:53 +02006#include "posixmodule.h"
Guido van Rossum864407d1991-04-10 19:48:25 +00007
Guido van Rossum864407d1991-04-10 19:48:25 +00008#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[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000011 {"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}
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000019};
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 = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000028 "pwd.struct_passwd",
29 struct_passwd__doc__,
30 struct_pwd_type_fields,
31 7,
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000032};
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
Antoine Pitrouc83ea132010-05-09 14:46:46 +000044
Martin v. Löwis19ab6c92006-04-16 18:55:50 +000045static int initialized;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000046static PyTypeObject StructPwdType;
47
Martin v. Löwis29275c92002-09-17 09:34:06 +000048static void
49sets(PyObject *v, int i, char* val)
50{
51 if (val)
Antoine Pitrouc83ea132010-05-09 14:46:46 +000052 PyStructSequence_SET_ITEM(v, i, PyString_FromString(val));
Martin v. Löwis29275c92002-09-17 09:34:06 +000053 else {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000054 PyStructSequence_SET_ITEM(v, i, Py_None);
55 Py_INCREF(Py_None);
Martin v. Löwis29275c92002-09-17 09:34:06 +000056 }
57}
58
Barry Warsaw50c5cf11996-12-11 16:54:40 +000059static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000060mkpwent(struct passwd *p)
Guido van Rossum864407d1991-04-10 19:48:25 +000061{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000062 int setIndex = 0;
63 PyObject *v = PyStructSequence_New(&StructPwdType);
64 if (v == NULL)
65 return NULL;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000066
67#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
Martin v. Löwis29275c92002-09-17 09:34:06 +000068#define SETS(i,val) sets(v, i, val)
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000069
Antoine Pitrouc83ea132010-05-09 14:46:46 +000070 SETS(setIndex++, p->pw_name);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000071#ifdef __VMS
Antoine Pitrouc83ea132010-05-09 14:46:46 +000072 SETS(setIndex++, "");
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000073#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +000074 SETS(setIndex++, p->pw_passwd);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000075#endif
Serhiy Storchakada5c2a02013-02-12 09:27:53 +020076 PyStructSequence_SET_ITEM(v, setIndex++, _PyInt_FromUid(p->pw_uid));
77 PyStructSequence_SET_ITEM(v, setIndex++, _PyInt_FromGid(p->pw_gid));
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000078#ifdef __VMS
Antoine Pitrouc83ea132010-05-09 14:46:46 +000079 SETS(setIndex++, "");
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000080#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +000081 SETS(setIndex++, p->pw_gecos);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000082#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +000083 SETS(setIndex++, p->pw_dir);
84 SETS(setIndex++, p->pw_shell);
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000085
86#undef SETS
87#undef SETI
88
Antoine Pitrouc83ea132010-05-09 14:46:46 +000089 if (PyErr_Occurred()) {
90 Py_XDECREF(v);
91 return NULL;
92 }
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000093
Antoine Pitrouc83ea132010-05-09 14:46:46 +000094 return v;
Guido van Rossum864407d1991-04-10 19:48:25 +000095}
96
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000097PyDoc_STRVAR(pwd_getpwuid__doc__,
98"getpwuid(uid) -> (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 numeric user ID.\n\
Alexander Belopolskyb8de9fa2010-08-16 20:30:26 +0000101See help(pwd) 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_getpwuid(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000105{
Serhiy Storchakada5c2a02013-02-12 09:27:53 +0200106 uid_t uid;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000107 struct passwd *p;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +0200108 if (!PyArg_ParseTuple(args, "O&:getpwuid", _Py_Uid_Converter, &uid)) {
109 if (PyErr_ExceptionMatches(PyExc_OverflowError))
110 PyErr_Format(PyExc_KeyError,
111 "getpwuid(): uid not found");
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000112 return NULL;
Serhiy Storchakada5c2a02013-02-12 09:27:53 +0200113 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000114 if ((p = getpwuid(uid)) == NULL) {
Serhiy Storchakada5c2a02013-02-12 09:27:53 +0200115 if (uid < 0)
116 PyErr_Format(PyExc_KeyError,
117 "getpwuid(): uid not found: %ld", (long)uid);
118 else
119 PyErr_Format(PyExc_KeyError,
120 "getpwuid(): uid not found: %lu", (unsigned long)uid);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000121 return NULL;
122 }
123 return mkpwent(p);
Guido van Rossum864407d1991-04-10 19:48:25 +0000124}
125
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000126PyDoc_STRVAR(pwd_getpwnam__doc__,
127"getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
128 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000129Return the password database entry for the given user name.\n\
Alexander Belopolskyb8de9fa2010-08-16 20:30:26 +0000130See help(pwd) for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000131
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000132static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000133pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000134{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000135 char *name;
136 struct passwd *p;
137 if (!PyArg_ParseTuple(args, "s:getpwnam", &name))
138 return NULL;
139 if ((p = getpwnam(name)) == NULL) {
140 PyErr_Format(PyExc_KeyError,
141 "getpwnam(): name not found: %s", name);
142 return NULL;
143 }
144 return mkpwent(p);
Guido van Rossum864407d1991-04-10 19:48:25 +0000145}
146
Guido van Rossum1171ee61997-08-22 20:42:00 +0000147#ifdef HAVE_GETPWENT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000148PyDoc_STRVAR(pwd_getpwall__doc__,
149"getpwall() -> list_of_entries\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000150Return a list of all available password database entries, \
151in arbitrary order.\n\
Alexander Belopolskyb8de9fa2010-08-16 20:30:26 +0000152See help(pwd) for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000153
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000154static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000155pwd_getpwall(PyObject *self)
Guido van Rossum864407d1991-04-10 19:48:25 +0000156{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000157 PyObject *d;
158 struct passwd *p;
159 if ((d = PyList_New(0)) == NULL)
160 return NULL;
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000161#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000162 if ((p = getpwuid(0)) != NULL) {
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000163#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000164 setpwent();
165 while ((p = getpwent()) != NULL) {
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000166#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000167 PyObject *v = mkpwent(p);
168 if (v == NULL || PyList_Append(d, v) != 0) {
169 Py_XDECREF(v);
170 Py_DECREF(d);
171 endpwent();
172 return NULL;
173 }
174 Py_DECREF(v);
175 }
176 endpwent();
177 return d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000178}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000179#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000180
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000181static PyMethodDef pwd_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000182 {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
183 {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000184#ifdef HAVE_GETPWENT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000185 {"getpwall", (PyCFunction)pwd_getpwall,
186 METH_NOARGS, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000187#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000188 {NULL, NULL} /* sentinel */
Guido van Rossum864407d1991-04-10 19:48:25 +0000189};
190
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000191PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000192initpwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000193{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000194 PyObject *m;
195 m = Py_InitModule3("pwd", pwd_methods, pwd__doc__);
196 if (m == NULL)
197 return;
Fred Drake88c93442002-04-13 21:07:45 +0000198
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000199 if (!initialized)
200 PyStructSequence_InitType(&StructPwdType,
201 &struct_pwd_type_desc);
202 Py_INCREF((PyObject *) &StructPwdType);
203 PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
204 /* And for b/w compatibility (this was defined by mistake): */
205 Py_INCREF((PyObject *) &StructPwdType);
206 PyModule_AddObject(m, "struct_pwent", (PyObject *) &StructPwdType);
207 initialized = 1;
Guido van Rossum864407d1991-04-10 19:48:25 +0000208}