blob: c03dce8b83121118bc55f1638e172ddf833d6ead [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{
Guido van Rossumbcc20741998-08-04 22:53:56 +000032#ifdef __BEOS__
33 /* For faking the GECOS field. - [cjh] */
34 char *be_user = NULL;
35
36 be_user = getenv( "USER" );
37#endif
38
Barry Warsawbab218e1996-12-19 22:22:32 +000039 return Py_BuildValue(
40 "(ssllsss)",
41 p->pw_name,
42 p->pw_passwd,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000043#if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
44/* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
45 for later versions you may have to remove this */
Barry Warsawbab218e1996-12-19 22:22:32 +000046 (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */
47 (long)p->pw_short_pad2,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000048#else
Barry Warsawbab218e1996-12-19 22:22:32 +000049 (long)p->pw_uid,
50 (long)p->pw_gid,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000051#endif
Guido van Rossumbcc20741998-08-04 22:53:56 +000052#ifdef __BEOS__
53/* BeOS doesn't have a GECOS field, oddly enough. - [cjh] */
54 be_user ? be_user : "baron",
55#else
Barry Warsawbab218e1996-12-19 22:22:32 +000056 p->pw_gecos,
Guido van Rossumbcc20741998-08-04 22:53:56 +000057#endif
Barry Warsawbab218e1996-12-19 22:22:32 +000058 p->pw_dir,
59 p->pw_shell);
Guido van Rossum864407d1991-04-10 19:48:25 +000060}
61
Guido van Rossum3e79c441998-03-03 22:03:26 +000062static char pwd_getpwuid__doc__[] = "\
63getpwuid(uid) -> entry\n\
64Return the password database entry for the given numeric user ID.\n\
65See pwd.__doc__ for more on password database entries.";
66
Barry Warsaw50c5cf11996-12-11 16:54:40 +000067static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000068pwd_getpwuid(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +000069{
70 int uid;
71 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000072 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossum864407d1991-04-10 19:48:25 +000073 return NULL;
74 if ((p = getpwuid(uid)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000075 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000076 return NULL;
77 }
78 return mkpwent(p);
79}
80
Guido van Rossum3e79c441998-03-03 22:03:26 +000081static char pwd_getpwnam__doc__[] = "\
82getpwnam(name) -> entry\n\
83Return the password database entry for the given user name.\n\
84See pwd.__doc__ for more on password database entries.";
85
Barry Warsaw50c5cf11996-12-11 16:54:40 +000086static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +000087pwd_getpwnam(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +000088{
Guido van Rossumef0a00e1992-01-27 16:51:30 +000089 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +000090 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000091 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum864407d1991-04-10 19:48:25 +000092 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +000093 if ((p = getpwnam(name)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000094 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000095 return NULL;
96 }
97 return mkpwent(p);
98}
99
Guido van Rossum1171ee61997-08-22 20:42:00 +0000100#ifdef HAVE_GETPWENT
Guido van Rossum3e79c441998-03-03 22:03:26 +0000101static char pwd_getpwall__doc__[] = "\
102getpwall() -> list_of_entries\n\
103Return a list of all available password database entries, \
104in arbitrary order.\n\
105See pwd.__doc__ for more on password database entries.";
106
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000107static PyObject *
Peter Schneider-Kamp39e0e5a2000-07-10 13:12:27 +0000108pwd_getpwall(PyObject *self, PyObject *args)
Guido van Rossum864407d1991-04-10 19:48:25 +0000109{
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000110 PyObject *d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000111 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000112 if (!PyArg_NoArgs(args))
Guido van Rossum864407d1991-04-10 19:48:25 +0000113 return NULL;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000114 if ((d = PyList_New(0)) == NULL)
Guido van Rossum864407d1991-04-10 19:48:25 +0000115 return NULL;
116 setpwent();
117 while ((p = getpwent()) != NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000118 PyObject *v = mkpwent(p);
119 if (v == NULL || PyList_Append(d, v) != 0) {
120 Py_XDECREF(v);
121 Py_DECREF(d);
Guido van Rossum864407d1991-04-10 19:48:25 +0000122 return NULL;
123 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000124 Py_DECREF(v);
Guido van Rossum864407d1991-04-10 19:48:25 +0000125 }
126 return d;
127}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000128#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000129
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000130static PyMethodDef pwd_methods[] = {
Guido van Rossum3e79c441998-03-03 22:03:26 +0000131 {"getpwuid", pwd_getpwuid, 0, pwd_getpwuid__doc__},
132 {"getpwnam", pwd_getpwnam, 0, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000133#ifdef HAVE_GETPWENT
Guido van Rossum3e79c441998-03-03 22:03:26 +0000134 {"getpwall", pwd_getpwall, 0, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000135#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000136 {NULL, NULL} /* sentinel */
137};
138
Guido van Rossum3886bb61998-12-04 18:50:17 +0000139DL_EXPORT(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000140initpwd()
141{
Guido van Rossum3e79c441998-03-03 22:03:26 +0000142 Py_InitModule4("pwd", pwd_methods, pwd__doc__,
143 (PyObject *)NULL, PYTHON_API_VERSION);
Guido van Rossum864407d1991-04-10 19:48:25 +0000144}