blob: e37e5fdd07833f70e95156bf9dddd30a9b91f00f [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{
Barry Warsawbab218e1996-12-19 22:22:32 +000054 return Py_BuildValue(
55 "(ssllsss)",
56 p->pw_name,
57 p->pw_passwd,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000058#if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
59/* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
60 for later versions you may have to remove this */
Barry Warsawbab218e1996-12-19 22:22:32 +000061 (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */
62 (long)p->pw_short_pad2,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000063#else
Barry Warsawbab218e1996-12-19 22:22:32 +000064 (long)p->pw_uid,
65 (long)p->pw_gid,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000066#endif
Barry Warsawbab218e1996-12-19 22:22:32 +000067 p->pw_gecos,
68 p->pw_dir,
69 p->pw_shell);
Guido van Rossum864407d1991-04-10 19:48:25 +000070}
71
Guido van Rossum3e79c441998-03-03 22:03:26 +000072static char pwd_getpwuid__doc__[] = "\
73getpwuid(uid) -> entry\n\
74Return the password database entry for the given numeric user ID.\n\
75See pwd.__doc__ for more on password database entries.";
76
Barry Warsaw50c5cf11996-12-11 16:54:40 +000077static PyObject *
78pwd_getpwuid(self, args)
79 PyObject *self;
80 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +000081{
82 int uid;
83 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000084 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossum864407d1991-04-10 19:48:25 +000085 return NULL;
86 if ((p = getpwuid(uid)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000087 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000088 return NULL;
89 }
90 return mkpwent(p);
91}
92
Guido van Rossum3e79c441998-03-03 22:03:26 +000093static char pwd_getpwnam__doc__[] = "\
94getpwnam(name) -> entry\n\
95Return the password database entry for the given user name.\n\
96See pwd.__doc__ for more on password database entries.";
97
Barry Warsaw50c5cf11996-12-11 16:54:40 +000098static PyObject *
99pwd_getpwnam(self, args)
100 PyObject *self;
101 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +0000102{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000103 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +0000104 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000105 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum864407d1991-04-10 19:48:25 +0000106 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000107 if ((p = getpwnam(name)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000108 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +0000109 return NULL;
110 }
111 return mkpwent(p);
112}
113
Guido van Rossum1171ee61997-08-22 20:42:00 +0000114#ifdef HAVE_GETPWENT
Guido van Rossum3e79c441998-03-03 22:03:26 +0000115static char pwd_getpwall__doc__[] = "\
116getpwall() -> list_of_entries\n\
117Return a list of all available password database entries, \
118in arbitrary order.\n\
119See pwd.__doc__ for more on password database entries.";
120
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000121static PyObject *
122pwd_getpwall(self, args)
123 PyObject *self;
124 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +0000125{
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000126 PyObject *d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000127 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000128 if (!PyArg_NoArgs(args))
Guido van Rossum864407d1991-04-10 19:48:25 +0000129 return NULL;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000130 if ((d = PyList_New(0)) == NULL)
Guido van Rossum864407d1991-04-10 19:48:25 +0000131 return NULL;
132 setpwent();
133 while ((p = getpwent()) != NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000134 PyObject *v = mkpwent(p);
135 if (v == NULL || PyList_Append(d, v) != 0) {
136 Py_XDECREF(v);
137 Py_DECREF(d);
Guido van Rossum864407d1991-04-10 19:48:25 +0000138 return NULL;
139 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000140 Py_DECREF(v);
Guido van Rossum864407d1991-04-10 19:48:25 +0000141 }
142 return d;
143}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000144#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000145
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000146static PyMethodDef pwd_methods[] = {
Guido van Rossum3e79c441998-03-03 22:03:26 +0000147 {"getpwuid", pwd_getpwuid, 0, pwd_getpwuid__doc__},
148 {"getpwnam", pwd_getpwnam, 0, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000149#ifdef HAVE_GETPWENT
Guido van Rossum3e79c441998-03-03 22:03:26 +0000150 {"getpwall", pwd_getpwall, 0, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000151#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000152 {NULL, NULL} /* sentinel */
153};
154
155void
156initpwd()
157{
Guido van Rossum3e79c441998-03-03 22:03:26 +0000158 Py_InitModule4("pwd", pwd_methods, pwd__doc__,
159 (PyObject *)NULL, PYTHON_API_VERSION);
Guido van Rossum864407d1991-04-10 19:48:25 +0000160}