blob: cfb2c034fa4b8573b84e64acd3383657ac62832e [file] [log] [blame]
Guido van Rossum864407d1991-04-10 19:48:25 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum864407d1991-04-10 19:48:25 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum864407d1991-04-10 19:48:25 +00009******************************************************************/
10
Guido van Rossumb6775db1994-08-01 11:34:53 +000011/* UNIX password file access module */
Guido van Rossum864407d1991-04-10 19:48:25 +000012
Barry Warsaw50c5cf11996-12-11 16:54:40 +000013#include "Python.h"
Guido van Rossum864407d1991-04-10 19:48:25 +000014
15#include <sys/types.h>
16#include <pwd.h>
Guido van Rossum864407d1991-04-10 19:48:25 +000017
Guido van Rossum3e79c441998-03-03 22:03:26 +000018static char pwd__doc__ [] = "\
19This module provides access to the Unix password database.\n\
20It is available on all Unix versions.\n\
21\n\
22Password database entries are reported as 7-tuples containing the following\n\
23items from the password database (see `<pwd.h>'), in order:\n\
24pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
25The uid and gid items are integers, all others are strings. An\n\
26exception is raised if the entry asked for cannot be found.";
27
28
Barry Warsaw50c5cf11996-12-11 16:54:40 +000029static PyObject *
30mkpwent(p)
Guido van Rossum864407d1991-04-10 19:48:25 +000031 struct passwd *p;
32{
Guido van Rossumbcc20741998-08-04 22:53:56 +000033#ifdef __BEOS__
34 /* For faking the GECOS field. - [cjh] */
35 char *be_user = NULL;
36
37 be_user = getenv( "USER" );
38#endif
39
Barry Warsawbab218e1996-12-19 22:22:32 +000040 return Py_BuildValue(
41 "(ssllsss)",
42 p->pw_name,
43 p->pw_passwd,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000044#if defined(NeXT) && defined(_POSIX_SOURCE) && defined(__LITTLE_ENDIAN__)
45/* Correct a bug present on Intel machines in NextStep 3.2 and 3.3;
46 for later versions you may have to remove this */
Barry Warsawbab218e1996-12-19 22:22:32 +000047 (long)p->pw_short_pad1, /* ugh-NeXT broke the padding */
48 (long)p->pw_short_pad2,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000049#else
Barry Warsawbab218e1996-12-19 22:22:32 +000050 (long)p->pw_uid,
51 (long)p->pw_gid,
Guido van Rossum03e8ffa1995-02-07 15:38:56 +000052#endif
Guido van Rossumbcc20741998-08-04 22:53:56 +000053#ifdef __BEOS__
54/* BeOS doesn't have a GECOS field, oddly enough. - [cjh] */
55 be_user ? be_user : "baron",
56#else
Barry Warsawbab218e1996-12-19 22:22:32 +000057 p->pw_gecos,
Guido van Rossumbcc20741998-08-04 22:53:56 +000058#endif
Barry Warsawbab218e1996-12-19 22:22:32 +000059 p->pw_dir,
60 p->pw_shell);
Guido van Rossum864407d1991-04-10 19:48:25 +000061}
62
Guido van Rossum3e79c441998-03-03 22:03:26 +000063static char pwd_getpwuid__doc__[] = "\
64getpwuid(uid) -> entry\n\
65Return the password database entry for the given numeric user ID.\n\
66See pwd.__doc__ for more on password database entries.";
67
Barry Warsaw50c5cf11996-12-11 16:54:40 +000068static PyObject *
69pwd_getpwuid(self, args)
70 PyObject *self;
71 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +000072{
73 int uid;
74 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000075 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossum864407d1991-04-10 19:48:25 +000076 return NULL;
77 if ((p = getpwuid(uid)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000078 PyErr_SetString(PyExc_KeyError, "getpwuid(): uid not found");
Guido van Rossum864407d1991-04-10 19:48:25 +000079 return NULL;
80 }
81 return mkpwent(p);
82}
83
Guido van Rossum3e79c441998-03-03 22:03:26 +000084static char pwd_getpwnam__doc__[] = "\
85getpwnam(name) -> entry\n\
86Return the password database entry for the given user name.\n\
87See pwd.__doc__ for more on password database entries.";
88
Barry Warsaw50c5cf11996-12-11 16:54:40 +000089static PyObject *
90pwd_getpwnam(self, args)
91 PyObject *self;
92 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +000093{
Guido van Rossumef0a00e1992-01-27 16:51:30 +000094 char *name;
Guido van Rossum864407d1991-04-10 19:48:25 +000095 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +000096 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum864407d1991-04-10 19:48:25 +000097 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +000098 if ((p = getpwnam(name)) == NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +000099 PyErr_SetString(PyExc_KeyError, "getpwnam(): name not found");
Guido van Rossum864407d1991-04-10 19:48:25 +0000100 return NULL;
101 }
102 return mkpwent(p);
103}
104
Guido van Rossum1171ee61997-08-22 20:42:00 +0000105#ifdef HAVE_GETPWENT
Guido van Rossum3e79c441998-03-03 22:03:26 +0000106static char pwd_getpwall__doc__[] = "\
107getpwall() -> list_of_entries\n\
108Return a list of all available password database entries, \
109in arbitrary order.\n\
110See pwd.__doc__ for more on password database entries.";
111
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000112static PyObject *
113pwd_getpwall(self, args)
114 PyObject *self;
115 PyObject *args;
Guido van Rossum864407d1991-04-10 19:48:25 +0000116{
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000117 PyObject *d;
Guido van Rossum864407d1991-04-10 19:48:25 +0000118 struct passwd *p;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000119 if (!PyArg_NoArgs(args))
Guido van Rossum864407d1991-04-10 19:48:25 +0000120 return NULL;
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000121 if ((d = PyList_New(0)) == NULL)
Guido van Rossum864407d1991-04-10 19:48:25 +0000122 return NULL;
123 setpwent();
124 while ((p = getpwent()) != NULL) {
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000125 PyObject *v = mkpwent(p);
126 if (v == NULL || PyList_Append(d, v) != 0) {
127 Py_XDECREF(v);
128 Py_DECREF(d);
Guido van Rossum864407d1991-04-10 19:48:25 +0000129 return NULL;
130 }
Barry Warsaw4bc9d391997-01-09 22:22:05 +0000131 Py_DECREF(v);
Guido van Rossum864407d1991-04-10 19:48:25 +0000132 }
133 return d;
134}
Guido van Rossum1171ee61997-08-22 20:42:00 +0000135#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000136
Barry Warsaw50c5cf11996-12-11 16:54:40 +0000137static PyMethodDef pwd_methods[] = {
Guido van Rossum3e79c441998-03-03 22:03:26 +0000138 {"getpwuid", pwd_getpwuid, 0, pwd_getpwuid__doc__},
139 {"getpwnam", pwd_getpwnam, 0, pwd_getpwnam__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000140#ifdef HAVE_GETPWENT
Guido van Rossum3e79c441998-03-03 22:03:26 +0000141 {"getpwall", pwd_getpwall, 0, pwd_getpwall__doc__},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000142#endif
Guido van Rossum864407d1991-04-10 19:48:25 +0000143 {NULL, NULL} /* sentinel */
144};
145
Guido van Rossum3886bb61998-12-04 18:50:17 +0000146DL_EXPORT(void)
Guido van Rossum864407d1991-04-10 19:48:25 +0000147initpwd()
148{
Guido van Rossum3e79c441998-03-03 22:03:26 +0000149 Py_InitModule4("pwd", pwd_methods, pwd__doc__,
150 (PyObject *)NULL, PYTHON_API_VERSION);
Guido van Rossum864407d1991-04-10 19:48:25 +0000151}