blob: 7f58ed34087bb278d0ff91a286e5292717d8263a [file] [log] [blame]
Guido van Rossum864407d1991-04-10 19:48:25 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum864407d1991-04-10 19:48:25 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum864407d1991-04-10 19:48:25 +00009******************************************************************/
10
Guido van Rossumb6775db1994-08-01 11:34:53 +000011/* UNIX password file access module */
Guido van Rossum864407d1991-04-10 19:48:25 +000012
Barry Warsaw50c5cf11996-12-11 16:54:40 +000013#include "Python.h"
Guido van Rossum864407d1991-04-10 19:48:25 +000014
15#include <sys/types.h>
16#include <pwd.h>
Guido van Rossum864407d1991-04-10 19:48:25 +000017
Guido van Rossum3e79c441998-03-03 22:03:26 +000018static char pwd__doc__ [] = "\
19This module provides access to the Unix password database.\n\
20It is available on all Unix versions.\n\
21\n\
22Password database entries are reported as 7-tuples containing the following\n\
23items from the password database (see `<pwd.h>'), in order:\n\
24pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
25The uid and gid items are integers, all others are strings. An\n\
26exception is raised if the entry asked for cannot be found.";
27
28
Barry Warsaw50c5cf11996-12-11 16:54:40 +000029static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000030mkpwent(struct passwd *p)
Guido van Rossum864407d1991-04-10 19:48:25 +000031{
Barry Warsawbab218e1996-12-19 22:22:32 +000032 return Py_BuildValue(
33 "(ssllsss)",
34 p->pw_name,
35 p->pw_passwd,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000036#if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
37/* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
38 for later versions you may have to remove this */
Barry Warsawbab218e1996-12-19 22:22:32 +000039 (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */
40 (long)p->pw_short_pad2,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000041#else
Barry Warsawbab218e1996-12-19 22:22:32 +000042 (long)p->pw_uid,
43 (long)p->pw_gid,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000044#endif
Barry Warsawbab218e1996-12-19 22:22:32 +000045 p->pw_gecos,
46 p->pw_dir,
47 p->pw_shell);
Guido van Rossum864407d1991-04-10 19:48:25 +000048}
49
Guido van Rossum3e79c441998-03-03 22:03:26 +000050static char pwd_getpwuid__doc__[] = "\
51getpwuid(uid) -> entry\n\
52Return the password database entry for the given numeric user ID.\n\
53See pwd.__doc__ for more on password database entries.";
54
Barry Warsaw50c5cf11996-12-11 16:54:40 +000055static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000056pwd_getpwuid(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +000057{
58 int uid;
59 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000060 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossum864407d1991-04-10 19:48:25 +000061 return NULL;
62 if ((p = getpwuid(uid)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000063 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000064 return NULL;
65 }
66 return mkpwent(p);
67}
68
Guido van Rossum3e79c441998-03-03 22:03:26 +000069static char pwd_getpwnam__doc__[] = "\
70getpwnam(name) -> entry\n\
71Return the password database entry for the given user name.\n\
72See pwd.__doc__ for more on password database entries.";
73
Barry Warsaw50c5cf11996-12-11 16:54:40 +000074static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000075pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +000076{
Guido van Rossumef0a00e1992-01-27 16:51:30 +000077 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +000078 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000079 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum864407d1991-04-10 19:48:25 +000080 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +000081 if ((p = getpwnam(name)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000082 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000083 return NULL;
84 }
85 return mkpwent(p);
86}
87
Guido van Rossum1171ee61997-08-22 20:42:00 +000088#ifdef HAVE_GETPWENT
Guido van Rossum3e79c441998-03-03 22:03:26 +000089static char pwd_getpwall__doc__[] = "\
90getpwall() -> list_of_entries\n\
91Return a list of all available password database entries, \
92in arbitrary order.\n\
93See pwd.__doc__ for more on password database entries.";
94
Barry Warsaw50c5cf11996-12-11 16:54:40 +000095static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000096pwd_getpwall(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +000097{
Barry Warsaw50c5cf11996-12-11 16:54:40 +000098 PyObject *d;
Guido van Rossum864407d1991-04-10 19:48:25 +000099 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000100 if (!PyArg_NoArgs(args))
Guido van Rossum864407d1991-04-10 19:48:25 +0000101 return NULL;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000102 if ((d = PyList_New(0)) == NULL)
Guido van Rossum864407d1991-04-10 19:48:25 +0000103 return NULL;
104 setpwent();
105 while ((p = getpwent()) != NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000106 PyObject *v = mkpwent(p);
107 if (v == NULL || PyList_Append(d, v) != 0) {
108 Py_XDECREF(v);
109 Py_DECREF(d);
Guido van Rossum864407d1991-04-10 19:48:25 +0000110 return NULL;
111 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000112 Py_DECREF(v);
Guido van Rossum864407d1991-04-10 19:48:25 +0000113 }
114 return d;
115}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000116#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000117
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000118static PyMethodDef pwd_methods[] = {
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000119 {"getpwuid", pwd_getpwuid, METH_OLDARGS, pwd_getpwuid__doc__},
120 {"getpwnam", pwd_getpwnam, METH_OLDARGS, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000121#ifdef HAVE_GETPWENT
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000122 {"getpwall", pwd_getpwall, METH_OLDARGS, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000123#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000124 {NULL, NULL} /* sentinel */
125};
126
Guido van Rossum3886bb61998-12-04 18:50:17 +0000127DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000128initpwd(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000129{
Guido van Rossum3e79c441998-03-03 22:03:26 +0000130 Py_InitModule4("pwd", pwd_methods, pwd__doc__,
131 (PyObject *)NULL, PYTHON_API_VERSION);
Guido van Rossum864407d1991-04-10 19:48:25 +0000132}