blob: b7e0c92a87ba656e9ed5ab9c830ebc1a3b222d23 [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
Barry Warsaw50c5cf11996-12-11 16:54:40 +000039static PyObject *
40mkpwent(p)
Guido van Rossum864407d1991-04-10 19:48:25 +000041 struct passwd *p;
42{
Barry Warsawbab218e1996-12-19 22:22:32 +000043 return Py_BuildValue(
44 "(ssllsss)",
45 p->pw_name,
46 p->pw_passwd,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000047#if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
48/* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
49 for later versions you may have to remove this */
Barry Warsawbab218e1996-12-19 22:22:32 +000050 (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */
51 (long)p->pw_short_pad2,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000052#else
Barry Warsawbab218e1996-12-19 22:22:32 +000053 (long)p->pw_uid,
54 (long)p->pw_gid,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000055#endif
Barry Warsawbab218e1996-12-19 22:22:32 +000056 p->pw_gecos,
57 p->pw_dir,
58 p->pw_shell);
Guido van Rossum864407d1991-04-10 19:48:25 +000059}
60
Barry Warsaw50c5cf11996-12-11 16:54:40 +000061static PyObject *
62pwd_getpwuid(self, args)
63 PyObject *self;
64 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +000065{
66 int uid;
67 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000068 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossum864407d1991-04-10 19:48:25 +000069 return NULL;
70 if ((p = getpwuid(uid)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000071 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000072 return NULL;
73 }
74 return mkpwent(p);
75}
76
Barry Warsaw50c5cf11996-12-11 16:54:40 +000077static PyObject *
78pwd_getpwnam(self, args)
79 PyObject *self;
80 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +000081{
Guido van Rossumef0a00e1992-01-27 16:51:30 +000082 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +000083 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000084 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum864407d1991-04-10 19:48:25 +000085 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +000086 if ((p = getpwnam(name)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000087 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000088 return NULL;
89 }
90 return mkpwent(p);
91}
92
Guido van Rossum1171ee61997-08-22 20:42:00 +000093#ifdef HAVE_GETPWENT
Barry Warsaw50c5cf11996-12-11 16:54:40 +000094static PyObject *
95pwd_getpwall(self, args)
96 PyObject *self;
97 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +000098{
Barry Warsaw50c5cf11996-12-11 16:54:40 +000099 PyObject *d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000100 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000101 if (!PyArg_NoArgs(args))
Guido van Rossum864407d1991-04-10 19:48:25 +0000102 return NULL;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000103 if ((d = PyList_New(0)) == NULL)
Guido van Rossum864407d1991-04-10 19:48:25 +0000104 return NULL;
105 setpwent();
106 while ((p = getpwent()) != NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000107 PyObject *v = mkpwent(p);
108 if (v == NULL || PyList_Append(d, v) != 0) {
109 Py_XDECREF(v);
110 Py_DECREF(d);
Guido van Rossum864407d1991-04-10 19:48:25 +0000111 return NULL;
112 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000113 Py_DECREF(v);
Guido van Rossum864407d1991-04-10 19:48:25 +0000114 }
115 return d;
116}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000117#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000118
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000119static PyMethodDef pwd_methods[] = {
Guido van Rossum864407d1991-04-10 19:48:25 +0000120 {"getpwuid", pwd_getpwuid},
121 {"getpwnam", pwd_getpwnam},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000122#ifdef HAVE_GETPWENT
Guido van Rossum864407d1991-04-10 19:48:25 +0000123 {"getpwall", pwd_getpwall},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000124#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000125 {NULL, NULL} /* sentinel */
126};
127
128void
129initpwd()
130{
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000131 Py_InitModule("pwd", pwd_methods);
Guido van Rossum864407d1991-04-10 19:48:25 +0000132}