blob: 3c5401ab8b32323dd70397c3d22ff8f9dfd7e945 [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 Rossumfd71b9e2000-06-30 23:50:40 +00007Copyright (c) 2000, BeOpen.com.
8Copyright (c) 1995-2000, Corporation for National Research Initiatives.
9Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
10All rights reserved.
Guido van Rossum864407d1991-04-10 19:48:25 +000011
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000012See the file "Misc/COPYRIGHT" for information on usage and
13redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum864407d1991-04-10 19:48:25 +000014
15******************************************************************/
16
Guido van Rossumb6775db1994-08-01 11:34:53 +000017/* UNIX password file access module */
Guido van Rossum864407d1991-04-10 19:48:25 +000018
Barry Warsaw50c5cf11996-12-11 16:54:40 +000019#include "Python.h"
Guido van Rossum864407d1991-04-10 19:48:25 +000020
21#include <sys/types.h>
22#include <pwd.h>
Guido van Rossum864407d1991-04-10 19:48:25 +000023
Guido van Rossum3e79c441998-03-03 22:03:26 +000024static char pwd__doc__ [] = "\
25This module provides access to the Unix password database.\n\
26It is available on all Unix versions.\n\
27\n\
28Password database entries are reported as 7-tuples containing the following\n\
29items from the password database (see `<pwd.h>'), in order:\n\
30pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
31The uid and gid items are integers, all others are strings. An\n\
32exception is raised if the entry asked for cannot be found.";
33
34
Barry Warsaw50c5cf11996-12-11 16:54:40 +000035static PyObject *
36mkpwent(p)
Guido van Rossum864407d1991-04-10 19:48:25 +000037 struct passwd *p;
38{
Guido van Rossumbcc20741998-08-04 22:53:56 +000039#ifdef __BEOS__
40 /* For faking the GECOS field. - [cjh] */
41 char *be_user = NULL;
42
43 be_user = getenv( "USER" );
44#endif
45
Barry Warsawbab218e1996-12-19 22:22:32 +000046 return Py_BuildValue(
47 "(ssllsss)",
48 p->pw_name,
49 p->pw_passwd,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000050#if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
51/* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
52 for later versions you may have to remove this */
Barry Warsawbab218e1996-12-19 22:22:32 +000053 (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */
54 (long)p->pw_short_pad2,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000055#else
Barry Warsawbab218e1996-12-19 22:22:32 +000056 (long)p->pw_uid,
57 (long)p->pw_gid,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000058#endif
Guido van Rossumbcc20741998-08-04 22:53:56 +000059#ifdef __BEOS__
60/* BeOS doesn't have a GECOS field, oddly enough. - [cjh] */
61 be_user ? be_user : "baron",
62#else
Barry Warsawbab218e1996-12-19 22:22:32 +000063 p->pw_gecos,
Guido van Rossumbcc20741998-08-04 22:53:56 +000064#endif
Barry Warsawbab218e1996-12-19 22:22:32 +000065 p->pw_dir,
66 p->pw_shell);
Guido van Rossum864407d1991-04-10 19:48:25 +000067}
68
Guido van Rossum3e79c441998-03-03 22:03:26 +000069static char pwd_getpwuid__doc__[] = "\
70getpwuid(uid) -> entry\n\
71Return the password database entry for the given numeric user ID.\n\
72See pwd.__doc__ for more on password database entries.";
73
Barry Warsaw50c5cf11996-12-11 16:54:40 +000074static PyObject *
75pwd_getpwuid(self, args)
76 PyObject *self;
77 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +000078{
79 int uid;
80 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000081 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossum864407d1991-04-10 19:48:25 +000082 return NULL;
83 if ((p = getpwuid(uid)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000084 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000085 return NULL;
86 }
87 return mkpwent(p);
88}
89
Guido van Rossum3e79c441998-03-03 22:03:26 +000090static char pwd_getpwnam__doc__[] = "\
91getpwnam(name) -> entry\n\
92Return the password database entry for the given user name.\n\
93See pwd.__doc__ for more on password database entries.";
94
Barry Warsaw50c5cf11996-12-11 16:54:40 +000095static PyObject *
96pwd_getpwnam(self, args)
97 PyObject *self;
98 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +000099{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000100 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +0000101 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000102 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum864407d1991-04-10 19:48:25 +0000103 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000104 if ((p = getpwnam(name)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000105 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +0000106 return NULL;
107 }
108 return mkpwent(p);
109}
110
Guido van Rossum1171ee61997-08-22 20:42:00 +0000111#ifdef HAVE_GETPWENT
Guido van Rossum3e79c441998-03-03 22:03:26 +0000112static char pwd_getpwall__doc__[] = "\
113getpwall() -> list_of_entries\n\
114Return a list of all available password database entries, \
115in arbitrary order.\n\
116See pwd.__doc__ for more on password database entries.";
117
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000118static PyObject *
119pwd_getpwall(self, args)
120 PyObject *self;
121 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +0000122{
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000123 PyObject *d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000124 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000125 if (!PyArg_NoArgs(args))
Guido van Rossum864407d1991-04-10 19:48:25 +0000126 return NULL;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000127 if ((d = PyList_New(0)) == NULL)
Guido van Rossum864407d1991-04-10 19:48:25 +0000128 return NULL;
129 setpwent();
130 while ((p = getpwent()) != NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000131 PyObject *v = mkpwent(p);
132 if (v == NULL || PyList_Append(d, v) != 0) {
133 Py_XDECREF(v);
134 Py_DECREF(d);
Guido van Rossum864407d1991-04-10 19:48:25 +0000135 return NULL;
136 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000137 Py_DECREF(v);
Guido van Rossum864407d1991-04-10 19:48:25 +0000138 }
139 return d;
140}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000141#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000142
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000143static PyMethodDef pwd_methods[] = {
Guido van Rossum3e79c441998-03-03 22:03:26 +0000144 {"getpwuid", pwd_getpwuid, 0, pwd_getpwuid__doc__},
145 {"getpwnam", pwd_getpwnam, 0, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000146#ifdef HAVE_GETPWENT
Guido van Rossum3e79c441998-03-03 22:03:26 +0000147 {"getpwall", pwd_getpwall, 0, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000148#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000149 {NULL, NULL} /* sentinel */
150};
151
Guido van Rossum3886bb61998-12-04 18:50:17 +0000152DL_EXPORT(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000153initpwd()
154{
Guido van Rossum3e79c441998-03-03 22:03:26 +0000155 Py_InitModule4("pwd", pwd_methods, pwd__doc__,
156 (PyObject *)NULL, PYTHON_API_VERSION);
Guido van Rossum864407d1991-04-10 19:48:25 +0000157}