blob: 1e0903a3887c74fb2b11c8eafd485d7ba9f49592 [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"
Guido van Rossum864407d1991-04-10 19:48:25 +00005
6#include <sys/types.h>
7#include <pwd.h>
Guido van Rossum864407d1991-04-10 19:48:25 +00008
Martin v. Löwisdbd55b32002-03-01 10:38:44 +00009static PyStructSequence_Field struct_pwd_type_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000010 {"pw_name", "user name"},
11 {"pw_passwd", "password"},
12 {"pw_uid", "user id"},
13 {"pw_gid", "group id"},
14 {"pw_gecos", "real name"},
15 {"pw_dir", "home directory"},
16 {"pw_shell", "shell program"},
17 {0}
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000018};
19
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000020PyDoc_STRVAR(struct_passwd__doc__,
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000021"pwd.struct_passwd: Results from getpw*() routines.\n\n\
22This object may be accessed either as a tuple of\n\
23 (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000024or via the object attributes as named in the above tuple.");
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000025
26static PyStructSequence_Desc struct_pwd_type_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 "pwd.struct_passwd",
28 struct_passwd__doc__,
29 struct_pwd_type_fields,
30 7,
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000031};
32
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000033PyDoc_STRVAR(pwd__doc__,
34"This module provides access to the Unix password database.\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +000035It is available on all Unix versions.\n\
36\n\
37Password database entries are reported as 7-tuples containing the following\n\
38items from the password database (see `<pwd.h>'), in order:\n\
39pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
40The uid and gid items are integers, all others are strings. An\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041exception is raised if the entry asked for cannot be found.");
Guido van Rossum3e79c441998-03-03 22:03:26 +000042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000044static int initialized;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000045static PyTypeObject StructPwdType;
46
Martin v. Löwis29275c92002-09-17 09:34:06 +000047static void
Neal Norwitzeb8b3a62007-08-24 23:26:23 +000048sets(PyObject *v, int i, const char* val)
Martin v. Löwis29275c92002-09-17 09:34:06 +000049{
Neal Norwitz3d7a90d2007-10-27 05:40:06 +000050 if (val) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 PyObject *o = PyUnicode_DecodeFSDefault(val);
52 PyStructSequence_SET_ITEM(v, i, o);
Neal Norwitz3d7a90d2007-10-27 05:40:06 +000053 }
Martin v. Löwis29275c92002-09-17 09:34:06 +000054 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 PyStructSequence_SET_ITEM(v, i, Py_None);
56 Py_INCREF(Py_None);
Martin v. Löwis29275c92002-09-17 09:34:06 +000057 }
58}
59
Barry Warsaw50c5cf11996-12-11 16:54:40 +000060static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000061mkpwent(struct passwd *p)
Guido van Rossum864407d1991-04-10 19:48:25 +000062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 int setIndex = 0;
64 PyObject *v = PyStructSequence_New(&StructPwdType);
65 if (v == NULL)
66 return NULL;
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000067
Christian Heimes217cfd12007-12-02 14:31:20 +000068#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
Martin v. Löwis29275c92002-09-17 09:34:06 +000069#define SETS(i,val) sets(v, i, val)
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 SETS(setIndex++, p->pw_name);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000072#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 SETS(setIndex++, "");
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000074#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 SETS(setIndex++, p->pw_passwd);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000076#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 SETI(setIndex++, p->pw_uid);
78 SETI(setIndex++, p->pw_gid);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000079#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 SETS(setIndex++, "");
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000081#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 SETS(setIndex++, p->pw_gecos);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000083#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 SETS(setIndex++, p->pw_dir);
85 SETS(setIndex++, p->pw_shell);
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000086
87#undef SETS
88#undef SETI
89
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 if (PyErr_Occurred()) {
91 Py_XDECREF(v);
92 return NULL;
93 }
Martin v. Löwisdbd55b32002-03-01 10:38:44 +000094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 return v;
Guido van Rossum864407d1991-04-10 19:48:25 +000096}
97
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000098PyDoc_STRVAR(pwd_getpwuid__doc__,
99"getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
100 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000101Return the password database entry for the given numeric user ID.\n\
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000102See help(pwd) for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000103
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000104static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000105pwd_getpwuid(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 unsigned int uid;
108 struct passwd *p;
109 if (!PyArg_ParseTuple(args, "I:getpwuid", &uid))
110 return NULL;
111 if ((p = getpwuid(uid)) == NULL) {
112 PyErr_Format(PyExc_KeyError,
113 "getpwuid(): uid not found: %d", uid);
114 return NULL;
115 }
116 return mkpwent(p);
Guido van Rossum864407d1991-04-10 19:48:25 +0000117}
118
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000119PyDoc_STRVAR(pwd_getpwnam__doc__,
120"getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
121 pw_gid,pw_gecos,pw_dir,pw_shell)\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000122Return the password database entry for the given user name.\n\
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000123See help(pwd) for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000124
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000125static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000126pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 char *name;
129 struct passwd *p;
130 PyObject *arg, *bytes, *retval = NULL;
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 if (!PyArg_ParseTuple(args, "U:getpwnam", &arg))
133 return NULL;
Victor Stinnerae6265f2010-05-15 16:27:27 +0000134 if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 return NULL;
136 if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
137 goto out;
138 if ((p = getpwnam(name)) == NULL) {
139 PyErr_Format(PyExc_KeyError,
140 "getpwnam(): name not found: %s", name);
141 goto out;
142 }
143 retval = mkpwent(p);
Martin v. Löwisb6a748b2009-05-29 15:23:17 +0000144out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 Py_DECREF(bytes);
146 return retval;
Guido van Rossum864407d1991-04-10 19:48:25 +0000147}
148
Guido van Rossum1171ee61997-08-22 20:42:00 +0000149#ifdef HAVE_GETPWENT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000150PyDoc_STRVAR(pwd_getpwall__doc__,
151"getpwall() -> list_of_entries\n\
Guido van Rossum3e79c441998-03-03 22:03:26 +0000152Return a list of all available password database entries, \
153in arbitrary order.\n\
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000154See help(pwd) for more on password database entries.");
Guido van Rossum3e79c441998-03-03 22:03:26 +0000155
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000156static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000157pwd_getpwall(PyObject *self)
Guido van Rossum864407d1991-04-10 19:48:25 +0000158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 PyObject *d;
160 struct passwd *p;
161 if ((d = PyList_New(0)) == NULL)
162 return NULL;
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000163#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 if ((p = getpwuid(0)) != NULL) {
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000165#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 setpwent();
167 while ((p = getpwent()) != NULL) {
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000168#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 PyObject *v = mkpwent(p);
170 if (v == NULL || PyList_Append(d, v) != 0) {
171 Py_XDECREF(v);
172 Py_DECREF(d);
173 endpwent();
174 return NULL;
175 }
176 Py_DECREF(v);
177 }
178 endpwent();
179 return d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000180}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000181#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000182
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000183static PyMethodDef pwd_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 {"getpwuid", pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
185 {"getpwnam", pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000186#ifdef HAVE_GETPWENT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 {"getpwall", (PyCFunction)pwd_getpwall,
188 METH_NOARGS, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000189#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 {NULL, NULL} /* sentinel */
Guido van Rossum864407d1991-04-10 19:48:25 +0000191};
192
Martin v. Löwis1a214512008-06-11 05:26:20 +0000193static struct PyModuleDef pwdmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 PyModuleDef_HEAD_INIT,
195 "pwd",
196 pwd__doc__,
197 -1,
198 pwd_methods,
199 NULL,
200 NULL,
201 NULL,
202 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000203};
204
205
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000206PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000207PyInit_pwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 PyObject *m;
210 m = PyModule_Create(&pwdmodule);
211 if (m == NULL)
212 return NULL;
Fred Drake88c93442002-04-13 21:07:45 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if (!initialized) {
215 PyStructSequence_InitType(&StructPwdType,
216 &struct_pwd_type_desc);
217 initialized = 1;
218 }
219 Py_INCREF((PyObject *) &StructPwdType);
220 PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
221 return m;
Guido van Rossum864407d1991-04-10 19:48:25 +0000222}