blob: 47edd5af20813626ca1910dacf36868eb899d71c [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
Guido van Rossum3e79c441998-03-03 22:03:26 +00009static char pwd__doc__ [] = "\
10This module provides access to the Unix password database.\n\
11It is available on all Unix versions.\n\
12\n\
13Password database entries are reported as 7-tuples containing the following\n\
14items from the password database (see `<pwd.h>'), in order:\n\
15pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
16The uid and gid items are integers, all others are strings. An\n\
17exception is raised if the entry asked for cannot be found.";
18
19
Barry Warsaw50c5cf11996-12-11 16:54:40 +000020static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000021mkpwent(struct passwd *p)
Guido van Rossum864407d1991-04-10 19:48:25 +000022{
Barry Warsawbab218e1996-12-19 22:22:32 +000023 return Py_BuildValue(
24 "(ssllsss)",
25 p->pw_name,
26 p->pw_passwd,
Barry Warsawbab218e1996-12-19 22:22:32 +000027 (long)p->pw_uid,
28 (long)p->pw_gid,
Barry Warsawbab218e1996-12-19 22:22:32 +000029 p->pw_gecos,
30 p->pw_dir,
31 p->pw_shell);
Guido van Rossum864407d1991-04-10 19:48:25 +000032}
33
Guido van Rossum3e79c441998-03-03 22:03:26 +000034static char pwd_getpwuid__doc__[] = "\
35getpwuid(uid) -> entry\n\
36Return the password database entry for the given numeric user ID.\n\
37See pwd.__doc__ for more on password database entries.";
38
Barry Warsaw50c5cf11996-12-11 16:54:40 +000039static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000040pwd_getpwuid(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +000041{
42 int uid;
43 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000044 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossum864407d1991-04-10 19:48:25 +000045 return NULL;
46 if ((p = getpwuid(uid)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000047 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000048 return NULL;
49 }
50 return mkpwent(p);
51}
52
Guido van Rossum3e79c441998-03-03 22:03:26 +000053static char pwd_getpwnam__doc__[] = "\
54getpwnam(name) -> entry\n\
55Return the password database entry for the given user name.\n\
56See pwd.__doc__ for more on password database entries.";
57
Barry Warsaw50c5cf11996-12-11 16:54:40 +000058static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000059pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +000060{
Guido van Rossumef0a00e1992-01-27 16:51:30 +000061 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +000062 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000063 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum864407d1991-04-10 19:48:25 +000064 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +000065 if ((p = getpwnam(name)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000066 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000067 return NULL;
68 }
69 return mkpwent(p);
70}
71
Guido van Rossum1171ee61997-08-22 20:42:00 +000072#ifdef HAVE_GETPWENT
Guido van Rossum3e79c441998-03-03 22:03:26 +000073static char pwd_getpwall__doc__[] = "\
74getpwall() -> list_of_entries\n\
75Return a list of all available password database entries, \
76in arbitrary order.\n\
77See pwd.__doc__ for more on password database entries.";
78
Barry Warsaw50c5cf11996-12-11 16:54:40 +000079static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000080pwd_getpwall(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +000081{
Barry Warsaw50c5cf11996-12-11 16:54:40 +000082 PyObject *d;
Guido van Rossum864407d1991-04-10 19:48:25 +000083 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000084 if (!PyArg_NoArgs(args))
Guido van Rossum864407d1991-04-10 19:48:25 +000085 return NULL;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000086 if ((d = PyList_New(0)) == NULL)
Guido van Rossum864407d1991-04-10 19:48:25 +000087 return NULL;
88 setpwent();
89 while ((p = getpwent()) != NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000090 PyObject *v = mkpwent(p);
91 if (v == NULL || PyList_Append(d, v) != 0) {
92 Py_XDECREF(v);
93 Py_DECREF(d);
Guido van Rossum864407d1991-04-10 19:48:25 +000094 return NULL;
95 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +000096 Py_DECREF(v);
Guido van Rossum864407d1991-04-10 19:48:25 +000097 }
Fred Drake8e68eb62001-03-11 03:03:07 +000098 endpwent();
Guido van Rossum864407d1991-04-10 19:48:25 +000099 return d;
100}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000101#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000102
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000103static PyMethodDef pwd_methods[] = {
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000104 {"getpwuid", pwd_getpwuid, METH_OLDARGS, pwd_getpwuid__doc__},
105 {"getpwnam", pwd_getpwnam, METH_OLDARGS, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000106#ifdef HAVE_GETPWENT
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000107 {"getpwall", pwd_getpwall, METH_OLDARGS, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000108#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000109 {NULL, NULL} /* sentinel */
110};
111
Guido van Rossum3886bb61998-12-04 18:50:17 +0000112DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000113initpwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000114{
Guido van Rossum3e79c441998-03-03 22:03:26 +0000115 Py_InitModule4("pwd", pwd_methods, pwd__doc__,
116 (PyObject *)NULL, PYTHON_API_VERSION);
Guido van Rossum864407d1991-04-10 19:48:25 +0000117}