blob: 7caf0d2b877fe5e3a54524b2108b79e4764349c1 [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 Warsaw50c5cf11996-12-11 16:54:40 +000043 return Py_BuildValue("(ssllsss)",
Guido van Rossume5372401993-03-16 12:15:04 +000044 p->pw_name,
45 p->pw_passwd,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000046#if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
47/* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
48 for later versions you may have to remove this */
49 (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */
50 (long)p->pw_short_pad2,
51#else
Guido van Rossume5372401993-03-16 12:15:04 +000052 (long)p->pw_uid,
53 (long)p->pw_gid,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000054#endif
Guido van Rossume5372401993-03-16 12:15:04 +000055 p->pw_gecos,
56 p->pw_dir,
57 p->pw_shell);
Guido van Rossum864407d1991-04-10 19:48:25 +000058}
59
Barry Warsaw50c5cf11996-12-11 16:54:40 +000060static PyObject *
61pwd_getpwuid(self, args)
62 PyObject *self;
63 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +000064{
65 int uid;
66 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000067 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossum864407d1991-04-10 19:48:25 +000068 return NULL;
69 if ((p = getpwuid(uid)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000070 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000071 return NULL;
72 }
73 return mkpwent(p);
74}
75
Barry Warsaw50c5cf11996-12-11 16:54:40 +000076static PyObject *
77pwd_getpwnam(self, args)
78 PyObject *self;
79 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +000080{
Guido van Rossumef0a00e1992-01-27 16:51:30 +000081 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +000082 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000083 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum864407d1991-04-10 19:48:25 +000084 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +000085 if ((p = getpwnam(name)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000086 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000087 return NULL;
88 }
89 return mkpwent(p);
90}
91
Barry Warsaw50c5cf11996-12-11 16:54:40 +000092static PyObject *
93pwd_getpwall(self, args)
94 PyObject *self;
95 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +000096{
Barry Warsaw50c5cf11996-12-11 16:54:40 +000097 PyObject *d;
Guido van Rossum864407d1991-04-10 19:48:25 +000098 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000099 if (!PyArg_NoArgs(args))
Guido van Rossum864407d1991-04-10 19:48:25 +0000100 return NULL;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000101 if ((d = PyList_New(0)) == NULL)
Guido van Rossum864407d1991-04-10 19:48:25 +0000102 return NULL;
103 setpwent();
104 while ((p = getpwent()) != NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000105 PyObject *v = mkpwent(p);
106 if (v == NULL || PyList_Append(d, v) != 0) {
107 Py_XDECREF(v);
108 Py_DECREF(d);
Guido van Rossum864407d1991-04-10 19:48:25 +0000109 return NULL;
110 }
111 }
112 return d;
113}
114
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000115static PyMethodDef pwd_methods[] = {
Guido van Rossum864407d1991-04-10 19:48:25 +0000116 {"getpwuid", pwd_getpwuid},
117 {"getpwnam", pwd_getpwnam},
118 {"getpwall", pwd_getpwall},
119 {NULL, NULL} /* sentinel */
120};
121
122void
123initpwd()
124{
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000125 Py_InitModule("pwd", pwd_methods);
Guido van Rossum864407d1991-04-10 19:48:25 +0000126}