Python-2.7.3/Modules/pwdmodule.c

No issues found

  1 /* UNIX password file access module */
  2 
  3 #include "Python.h"
  4 #include "structseq.h"
  5 
  6 #include <sys/types.h>
  7 #include <pwd.h>
  8 
  9 static PyStructSequence_Field struct_pwd_type_fields[] = {
 10     {"pw_name", "user name"},
 11     {"pw_passwd", "password"},
 12     {"pw_uid", "user id"},
 13     {"pw_gid", "group id"},
 14     {"pw_gecos", "real name"},
 15     {"pw_dir", "home directory"},
 16     {"pw_shell", "shell program"},
 17     {0}
 18 };
 19 
 20 PyDoc_STRVAR(struct_passwd__doc__,
 21 "pwd.struct_passwd: Results from getpw*() routines.\n\n\
 22 This object may be accessed either as a tuple of\n\
 23   (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)\n\
 24 or via the object attributes as named in the above tuple.");
 25 
 26 static PyStructSequence_Desc struct_pwd_type_desc = {
 27     "pwd.struct_passwd",
 28     struct_passwd__doc__,
 29     struct_pwd_type_fields,
 30     7,
 31 };
 32 
 33 PyDoc_STRVAR(pwd__doc__,
 34 "This module provides access to the Unix password database.\n\
 35 It is available on all Unix versions.\n\
 36 \n\
 37 Password database entries are reported as 7-tuples containing the following\n\
 38 items from the password database (see `<pwd.h>'), in order:\n\
 39 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.\n\
 40 The uid and gid items are integers, all others are strings. An\n\
 41 exception is raised if the entry asked for cannot be found.");
 42 
 43 
 44 static int initialized;
 45 static PyTypeObject StructPwdType;
 46 
 47 static void
 48 sets(PyObject *v, int i, char* val)
 49 {
 50   if (val)
 51       PyStructSequence_SET_ITEM(v, i, PyString_FromString(val));
 52   else {
 53       PyStructSequence_SET_ITEM(v, i, Py_None);
 54       Py_INCREF(Py_None);
 55   }
 56 }
 57 
 58 static PyObject *
 59 mkpwent(struct passwd *p)
 60 {
 61     int setIndex = 0;
 62     PyObject *v = PyStructSequence_New(&StructPwdType);
 63     if (v == NULL)
 64         return NULL;
 65 
 66 #define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
 67 #define SETS(i,val) sets(v, i, val)
 68 
 69     SETS(setIndex++, p->pw_name);
 70 #ifdef __VMS
 71     SETS(setIndex++, "");
 72 #else
 73     SETS(setIndex++, p->pw_passwd);
 74 #endif
 75     PyStructSequence_SET_ITEM(v, setIndex++, _PyObject_FromUid(p->pw_uid));
 76     PyStructSequence_SET_ITEM(v, setIndex++, _PyObject_FromGid(p->pw_gid));
 77 #ifdef __VMS
 78     SETS(setIndex++, "");
 79 #else
 80     SETS(setIndex++, p->pw_gecos);
 81 #endif
 82     SETS(setIndex++, p->pw_dir);
 83     SETS(setIndex++, p->pw_shell);
 84 
 85 #undef SETS
 86 #undef SETI
 87 
 88     if (PyErr_Occurred()) {
 89         Py_XDECREF(v);
 90         return NULL;
 91     }
 92 
 93     return v;
 94 }
 95 
 96 PyDoc_STRVAR(pwd_getpwuid__doc__,
 97 "getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,\n\
 98                   pw_gid,pw_gecos,pw_dir,pw_shell)\n\
 99 Return the password database entry for the given numeric user ID.\n\
100 See help(pwd) for more on password database entries.");
101 
102 static PyObject *
103 pwd_getpwuid(PyObject *self, PyObject *args)
104 {
105     uid_t uid;
106     struct passwd *p;
107     if (!PyArg_ParseTuple(args, "O&:getpwuid",
108                           _PyArg_ParseUid, &uid))
109         return NULL;
110     if ((p = getpwuid(uid)) == NULL) {
111         PyErr_Format(PyExc_KeyError,
112                      "getpwuid(): uid not found: %lu", (unsigned long)uid);
113         return NULL;
114     }
115     return mkpwent(p);
116 }
117 
118 PyDoc_STRVAR(pwd_getpwnam__doc__,
119 "getpwnam(name) -> (pw_name,pw_passwd,pw_uid,\n\
120                     pw_gid,pw_gecos,pw_dir,pw_shell)\n\
121 Return the password database entry for the given user name.\n\
122 See help(pwd) for more on password database entries.");
123 
124 static PyObject *
125 pwd_getpwnam(PyObject *self, PyObject *args)
126 {
127     char *name;
128     struct passwd *p;
129     if (!PyArg_ParseTuple(args, "s:getpwnam", &name))
130         return NULL;
131     if ((p = getpwnam(name)) == NULL) {
132         PyErr_Format(PyExc_KeyError,
133                      "getpwnam(): name not found: %s", name);
134         return NULL;
135     }
136     return mkpwent(p);
137 }
138 
139 #ifdef HAVE_GETPWENT
140 PyDoc_STRVAR(pwd_getpwall__doc__,
141 "getpwall() -> list_of_entries\n\
142 Return a list of all available password database entries, \
143 in arbitrary order.\n\
144 See help(pwd) for more on password database entries.");
145 
146 static PyObject *
147 pwd_getpwall(PyObject *self)
148 {
149     PyObject *d;
150     struct passwd *p;
151     if ((d = PyList_New(0)) == NULL)
152         return NULL;
153 #if defined(PYOS_OS2) && defined(PYCC_GCC)
154     if ((p = getpwuid(0)) != NULL) {
155 #else
156     setpwent();
157     while ((p = getpwent()) != NULL) {
158 #endif
159         PyObject *v = mkpwent(p);
160         if (v == NULL || PyList_Append(d, v) != 0) {
161             Py_XDECREF(v);
162             Py_DECREF(d);
163             endpwent();
164             return NULL;
165         }
166         Py_DECREF(v);
167     }
168     endpwent();
169     return d;
170 }
171 #endif
172 
173 static PyMethodDef pwd_methods[] = {
174     {"getpwuid",        pwd_getpwuid, METH_VARARGS, pwd_getpwuid__doc__},
175     {"getpwnam",        pwd_getpwnam, METH_VARARGS, pwd_getpwnam__doc__},
176 #ifdef HAVE_GETPWENT
177     {"getpwall",        (PyCFunction)pwd_getpwall,
178         METH_NOARGS,  pwd_getpwall__doc__},
179 #endif
180     {NULL,              NULL}           /* sentinel */
181 };
182 
183 PyMODINIT_FUNC
184 initpwd(void)
185 {
186     PyObject *m;
187     m = Py_InitModule3("pwd", pwd_methods, pwd__doc__);
188     if (m == NULL)
189         return;
190 
191     if (!initialized)
192         PyStructSequence_InitType(&StructPwdType,
193                                   &struct_pwd_type_desc);
194     Py_INCREF((PyObject *) &StructPwdType);
195     PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
196     /* And for b/w compatibility (this was defined by mistake): */
197     Py_INCREF((PyObject *) &StructPwdType);
198     PyModule_AddObject(m, "struct_pwent", (PyObject *) &StructPwdType);
199     initialized = 1;
200 }