blob: 00522b9972976795ac7f4c79d94d7ecb62fc0727 [file] [log] [blame]
Guido van Rossum864407d1991-04-10 19:48:25 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum864407d1991-04-10 19:48:25 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum864407d1991-04-10 19:48:25 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum864407d1991-04-10 19:48:25 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum864407d1991-04-10 19:48:25 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum864407d1991-04-10 19:48:25 +000029
30******************************************************************/
31
Guido van Rossumb6775db1994-08-01 11:34:53 +000032/* UNIX password file access module */
Guido van Rossum864407d1991-04-10 19:48:25 +000033
Barry Warsaw50c5cf11996-12-11 16:54:40 +000034#include "Python.h"
Guido van Rossum864407d1991-04-10 19:48:25 +000035
36#include <sys/types.h>
37#include <pwd.h>
Guido van Rossum864407d1991-04-10 19:48:25 +000038
Guido van Rossum3e79c441998-03-03 22:03:26 +000039static char pwd__doc__ [] = "\
40This module provides access to the Unix password database.\n\
41It is available on all Unix versions.\n\
42\n\
43Password database entries are reported as 7-tuples containing the following\n\
44items from the password database (see `<pwd.h>'), in order:\n\
45pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
46The uid and gid items are integers, all others are strings. An\n\
47exception is raised if the entry asked for cannot be found.";
48
49
Barry Warsaw50c5cf11996-12-11 16:54:40 +000050static PyObject *
51mkpwent(p)
Guido van Rossum864407d1991-04-10 19:48:25 +000052 struct passwd *p;
53{
Guido van Rossumbcc20741998-08-04 22:53:56 +000054#ifdef __BEOS__
55 /* For faking the GECOS field. - [cjh] */
56 char *be_user = NULL;
57
58 be_user = getenv( "USER" );
59#endif
60
Barry Warsawbab218e1996-12-19 22:22:32 +000061 return Py_BuildValue(
62 "(ssllsss)",
63 p->pw_name,
64 p->pw_passwd,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000065#if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
66/* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
67 for later versions you may have to remove this */
Barry Warsawbab218e1996-12-19 22:22:32 +000068 (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */
69 (long)p->pw_short_pad2,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000070#else
Barry Warsawbab218e1996-12-19 22:22:32 +000071 (long)p->pw_uid,
72 (long)p->pw_gid,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000073#endif
Guido van Rossumbcc20741998-08-04 22:53:56 +000074#ifdef __BEOS__
75/* BeOS doesn't have a GECOS field, oddly enough. - [cjh] */
76 be_user ? be_user : "baron",
77#else
Barry Warsawbab218e1996-12-19 22:22:32 +000078 p->pw_gecos,
Guido van Rossumbcc20741998-08-04 22:53:56 +000079#endif
Barry Warsawbab218e1996-12-19 22:22:32 +000080 p->pw_dir,
81 p->pw_shell);
Guido van Rossum864407d1991-04-10 19:48:25 +000082}
83
Guido van Rossum3e79c441998-03-03 22:03:26 +000084static char pwd_getpwuid__doc__[] = "\
85getpwuid(uid) -> entry\n\
86Return the password database entry for the given numeric user ID.\n\
87See pwd.__doc__ for more on password database entries.";
88
Barry Warsaw50c5cf11996-12-11 16:54:40 +000089static PyObject *
90pwd_getpwuid(self, args)
91 PyObject *self;
92 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +000093{
94 int uid;
95 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000096 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossum864407d1991-04-10 19:48:25 +000097 return NULL;
98 if ((p = getpwuid(uid)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000099 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +0000100 return NULL;
101 }
102 return mkpwent(p);
103}
104
Guido van Rossum3e79c441998-03-03 22:03:26 +0000105static char pwd_getpwnam__doc__[] = "\
106getpwnam(name) -> entry\n\
107Return the password database entry for the given user name.\n\
108See pwd.__doc__ for more on password database entries.";
109
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000110static PyObject *
111pwd_getpwnam(self, args)
112 PyObject *self;
113 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +0000114{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000115 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +0000116 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000117 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum864407d1991-04-10 19:48:25 +0000118 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000119 if ((p = getpwnam(name)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000120 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +0000121 return NULL;
122 }
123 return mkpwent(p);
124}
125
Guido van Rossum1171ee61997-08-22 20:42:00 +0000126#ifdef HAVE_GETPWENT
Guido van Rossum3e79c441998-03-03 22:03:26 +0000127static char pwd_getpwall__doc__[] = "\
128getpwall() -> list_of_entries\n\
129Return a list of all available password database entries, \
130in arbitrary order.\n\
131See pwd.__doc__ for more on password database entries.";
132
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000133static PyObject *
134pwd_getpwall(self, args)
135 PyObject *self;
136 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +0000137{
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000138 PyObject *d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000139 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000140 if (!PyArg_NoArgs(args))
Guido van Rossum864407d1991-04-10 19:48:25 +0000141 return NULL;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000142 if ((d = PyList_New(0)) == NULL)
Guido van Rossum864407d1991-04-10 19:48:25 +0000143 return NULL;
144 setpwent();
145 while ((p = getpwent()) != NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000146 PyObject *v = mkpwent(p);
147 if (v == NULL || PyList_Append(d, v) != 0) {
148 Py_XDECREF(v);
149 Py_DECREF(d);
Guido van Rossum864407d1991-04-10 19:48:25 +0000150 return NULL;
151 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000152 Py_DECREF(v);
Guido van Rossum864407d1991-04-10 19:48:25 +0000153 }
154 return d;
155}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000156#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000157
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000158static PyMethodDef pwd_methods[] = {
Guido van Rossum3e79c441998-03-03 22:03:26 +0000159 {"getpwuid", pwd_getpwuid, 0, pwd_getpwuid__doc__},
160 {"getpwnam", pwd_getpwnam, 0, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000161#ifdef HAVE_GETPWENT
Guido van Rossum3e79c441998-03-03 22:03:26 +0000162 {"getpwall", pwd_getpwall, 0, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000163#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000164 {NULL, NULL} /* sentinel */
165};
166
Guido van Rossum3886bb61998-12-04 18:50:17 +0000167DL_EXPORT(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000168initpwd()
169{
Guido van Rossum3e79c441998-03-03 22:03:26 +0000170 Py_InitModule4("pwd", pwd_methods, pwd__doc__,
171 (PyObject *)NULL, PYTHON_API_VERSION);
Guido van Rossum864407d1991-04-10 19:48:25 +0000172}