12816N/A--- Python-2.4.4/setup.py 2006-10-08 10:41:25.000000000 -0700
12816N/A+++ Python-new/setup.py 2008-04-08 07:23:37.263050000 -0700
12816N/A@@ -835,6 +835,13 @@ class PyBuildExt(build_ext):
12816N/A if (dl_inc is not None) and (platform not in ['atheos', 'darwin']):
12816N/A exts.append( Extension('dl', ['dlmodule.c']) )
12816N/A
12816N/A+ # ucred module (Solaris)
12816N/A+ ucred_inc = find_file('ucred.h', [], inc_dirs)
12816N/A+ tsol_inc = find_file('tsol/label.h', [], inc_dirs)
12816N/A+ if ucred_inc is not None and tsol_inc is not None:
12816N/A+ exts.append( Extension('ucred', ['ucred.c'],
12816N/A+ libraries = ['tsol']) )
12816N/A+
12816N/A # Platform-specific libraries
12816N/A if platform == 'linux2':
12816N/A # Linux-specific modules
12816N/A--- Python-2.4.4/Modules/ucred.c 1969-12-31 16:00:00.000000000 -0800
12816N/A+++ Python-new/Modules/ucred.c 2008-04-08 07:11:20.059630000 -0700
12816N/A@@ -0,0 +1,391 @@
12816N/A+/*
12816N/A+ * Permission is hereby granted, free of charge, to any person obtaining a copy
12816N/A+ * of this software and associated documentation files (the "Software"), to
12816N/A+ * deal in the Software without restriction, including without limitation the
12816N/A+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12816N/A+ * sell copies of the Software, and to permit persons to whom the Software is
12816N/A+ * furnished to do so, subject to the following conditions:
12816N/A+ *
12816N/A+ * The above copyright notice and this permission notice shall be included in
12816N/A+ * all copies or substantial portions of the Software.
12816N/A+ *
12816N/A+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12816N/A+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12816N/A+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
12816N/A+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
12816N/A+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
12816N/A+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
12816N/A+ * DEALINGS IN THE SOFTWARE.
12816N/A+ *
12816N/A+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
12816N/A+ * Use is subject to license terms.
12816N/A+ */
12816N/A+
12816N/A+#include <Python.h>
12816N/A+
12816N/A+#include <stdio.h>
12816N/A+#include <priv.h>
12816N/A+#include <ucred.h>
12816N/A+#include <ctype.h>
12816N/A+#include <tsol/label.h>
12816N/A+
12816N/A+typedef struct {
12816N/A+ PyObject_HEAD
12816N/A+ ucred_t *ucred;
12816N/A+} pyucred_t;
12816N/A+
12816N/A+#define pyucred_getlongid(name, type) \
12816N/A+ static PyObject * \
12816N/A+ pyucred_get##name(pyucred_t *uc) \
12816N/A+ { \
12816N/A+ type val; \
12816N/A+ \
12816N/A+ if (uc->ucred == NULL) { \
12816N/A+ errno = EINVAL; \
12816N/A+ PyErr_SetFromErrno(PyExc_OSError); \
12816N/A+ return (NULL); \
12816N/A+ } \
12816N/A+ \
12816N/A+ if ((val = ucred_get##name(uc->ucred)) == -1) { \
12816N/A+ PyErr_SetFromErrno(PyExc_OSError); \
12816N/A+ return (NULL); \
12816N/A+ } \
12816N/A+ \
12816N/A+ return (Py_BuildValue("l", (long)val)); \
12816N/A+ }
12816N/A+
12816N/A+pyucred_getlongid(euid, uid_t)
12816N/A+pyucred_getlongid(ruid, uid_t)
12816N/A+pyucred_getlongid(suid, uid_t)
12816N/A+pyucred_getlongid(egid, gid_t)
12816N/A+pyucred_getlongid(rgid, gid_t)
12816N/A+pyucred_getlongid(sgid, gid_t)
12816N/A+pyucred_getlongid(pid, pid_t)
12816N/A+pyucred_getlongid(projid, projid_t)
12816N/A+pyucred_getlongid(zoneid, zoneid_t)
12816N/A+
12816N/A+static PyObject *
12816N/A+pyucred_getgroups(pyucred_t *uc)
12816N/A+{
12816N/A+ const gid_t *groups;
12816N/A+ PyObject *list;
12816N/A+ int len;
12816N/A+ int i;
12816N/A+
12816N/A+ if (uc->ucred == NULL) {
12816N/A+ errno = EINVAL;
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ return (NULL);
12816N/A+ }
12816N/A+
12816N/A+ if ((len = ucred_getgroups(uc->ucred, &groups)) == -1) {
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ return (NULL);
12816N/A+ }
12816N/A+
12816N/A+ if ((list = PyList_New(len)) == NULL)
12816N/A+ return (NULL);
12816N/A+
12816N/A+ for (i = 0; i < len; i++) {
12816N/A+ PyObject *gid = Py_BuildValue("l", (long)groups[i]);
12816N/A+ if (PyList_SetItem(list, i, gid) == -1)
12816N/A+ return (NULL);
12816N/A+ }
12816N/A+
12816N/A+ return (list);
12816N/A+}
12816N/A+
12816N/A+static PyObject *
12816N/A+pyucred_getlabel(pyucred_t *uc)
12816N/A+{
12816N/A+ m_label_t *label;
12816N/A+ PyObject *ret;
12816N/A+ char *str;
12816N/A+
12816N/A+ if (uc->ucred == NULL) {
12816N/A+ errno = EINVAL;
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ return (NULL);
12816N/A+ }
12816N/A+
12816N/A+ label = ucred_getlabel(uc->ucred);
12816N/A+ if (label == NULL)
12816N/A+ return (Py_BuildValue("s", ""));
12816N/A+
12816N/A+ if (label_to_str(label, &str, M_LABEL, DEF_NAMES) == -1) {
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ return (NULL);
12816N/A+ }
12816N/A+
12816N/A+ ret = Py_BuildValue("s", str);
12816N/A+ free(str);
12816N/A+ return (ret);
12816N/A+}
12816N/A+
12816N/A+static PyObject *
12816N/A+pyucred_getpflags(pyucred_t *uc, PyObject *args, PyObject *kwargs)
12816N/A+{
12816N/A+ static char *kwlist[] = { "flags", NULL };
12816N/A+ uint_t flags;
12816N/A+
12816N/A+ if (uc->ucred == NULL) {
12816N/A+ errno = EINVAL;
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ return (NULL);
12816N/A+ }
12816N/A+
12816N/A+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist,
12816N/A+ &flags))
12816N/A+ return (NULL);
12816N/A+
12816N/A+ if ((flags = ucred_getpflags(uc->ucred, flags)) == (uint_t)-1) {
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ return (NULL);
12816N/A+ }
12816N/A+
12816N/A+ return (Py_BuildValue("i", flags));
12816N/A+}
12816N/A+
12816N/A+static PyObject *
12816N/A+pyucred_has_priv(pyucred_t *uc, PyObject *args, PyObject *kwargs)
12816N/A+{
12816N/A+ static char *kwlist[] = { "set", "priv", NULL };
12816N/A+ const priv_set_t *privs;
12816N/A+ const char *set;
12816N/A+ const char *priv;
12816N/A+
12816N/A+ if (uc->ucred == NULL) {
12816N/A+ errno = EINVAL;
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ return (NULL);
12816N/A+ }
12816N/A+
12816N/A+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ss", kwlist,
12816N/A+ &set, &priv))
12816N/A+ return (NULL);
12816N/A+
12816N/A+ if ((privs = ucred_getprivset(uc->ucred, set)) == NULL) {
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ return (NULL);
12816N/A+ }
12816N/A+
12816N/A+ if (priv_ismember(privs, priv)) {
12816N/A+ Py_INCREF(Py_True);
12816N/A+ return Py_True;
12816N/A+ }
12816N/A+
12816N/A+ Py_INCREF(Py_False);
12816N/A+ return Py_False;
12816N/A+}
12816N/A+
12816N/A+PyDoc_STRVAR(pyucred_getlabel_doc,
12816N/A+ "getlabel() -> string\n"
12816N/A+ "\n"
12816N/A+ "Return the Trusted Extensions label string, or an "
12816N/A+ "empty string if not available. The label string is "
12816N/A+ "converted using the default name and M_LABEL (human-readable). "
12816N/A+ "Raises OSError. See label_to_str(3TSOL).");
12816N/A+PyDoc_STRVAR(pyucred_getpflags_doc,
12816N/A+ "getpflags(flags) -> int\n"
12816N/A+ "\n"
12816N/A+ "Return the values of the specified privilege flags.");
12816N/A+PyDoc_STRVAR(pyucred_has_priv_doc,
12816N/A+ "has_priv(set, priv) -> bool\n"
12816N/A+ "\n"
12816N/A+ "Return true if the given privilege is set in the "
12816N/A+ "specified set. Raises OSError if the set or privilege is "
12816N/A+ "invalid, or a problem occurs.\n"
12816N/A+ "\n"
12816N/A+ "Currently, the following privilege sets are defined, as "
12816N/A+ "described in privileges(5):\n"
12816N/A+ "\n"
12816N/A+ "Effective\n"
12816N/A+ "Permitted\n"
12816N/A+ "Inheritable\n"
12816N/A+ "Limit\n");
12816N/A+
12816N/A+static PyMethodDef pyucred_methods[] = {
12816N/A+ { "geteuid", (PyCFunction)pyucred_geteuid, METH_NOARGS,
12816N/A+ "Return the effective user ID." },
12816N/A+ { "getruid", (PyCFunction)pyucred_getruid, METH_NOARGS,
12816N/A+ "Return the real user ID." },
12816N/A+ { "getsuid", (PyCFunction)pyucred_getsuid, METH_NOARGS,
12816N/A+ "Return the saved user ID." },
12816N/A+ { "getegid", (PyCFunction)pyucred_getegid, METH_NOARGS,
12816N/A+ "Return the effective group ID." },
12816N/A+ { "getrgid", (PyCFunction)pyucred_getrgid, METH_NOARGS,
12816N/A+ "Return the real group ID." },
12816N/A+ { "getsgid", (PyCFunction)pyucred_getsgid, METH_NOARGS,
12816N/A+ "Return the saved group ID." },
12816N/A+ { "getpid", (PyCFunction)pyucred_getpid, METH_NOARGS,
12816N/A+ "Return the effective user ID." },
12816N/A+ { "getprojid", (PyCFunction)pyucred_getprojid, METH_NOARGS,
12816N/A+ "Return the project ID." },
12816N/A+ { "getzoneid", (PyCFunction)pyucred_getzoneid, METH_NOARGS,
12816N/A+ "Return the zone ID." },
12816N/A+ { "getgroups", (PyCFunction)pyucred_getgroups, METH_NOARGS,
12816N/A+ "Return a list of group IDs." },
12816N/A+ { "getlabel", (PyCFunction)pyucred_getlabel, METH_NOARGS,
12816N/A+ pyucred_getlabel_doc },
12816N/A+ { "getpflags", (PyCFunction)pyucred_getpflags,
12816N/A+ METH_VARARGS|METH_KEYWORDS, pyucred_getpflags_doc },
12816N/A+ { "has_priv", (PyCFunction)pyucred_has_priv,
12816N/A+ METH_VARARGS|METH_KEYWORDS, pyucred_has_priv_doc },
12816N/A+ { NULL }
12816N/A+};
12816N/A+
12816N/A+static int
12816N/A+pyucred_init(PyObject *self, PyObject *args, PyObject *kwargs)
12816N/A+{
12816N/A+ pyucred_t *uc = (pyucred_t *)self;
12816N/A+ uc->ucred = NULL;
12816N/A+ return (0);
12816N/A+}
12816N/A+
12816N/A+static void
12816N/A+pyucred_dealloc(PyObject *self)
12816N/A+{
12816N/A+ pyucred_t *uc = (pyucred_t *)self;
12816N/A+ if (uc->ucred != NULL)
12816N/A+ ucred_free(uc->ucred);
12816N/A+ self->ob_type->tp_free(self);
12816N/A+}
12816N/A+
12816N/A+static PyTypeObject pyucred_type = {
12816N/A+ PyObject_HEAD_INIT(NULL)
12816N/A+ 0, /*ob_size*/
12816N/A+ "ucred.ucred", /*tp_name*/
12816N/A+ sizeof (pyucred_t), /*tp_basicsize*/
12816N/A+ 0, /*tp_itemsize*/
12816N/A+ pyucred_dealloc, /*tp_dealloc*/
12816N/A+ 0, /*tp_print*/
12816N/A+ 0, /*tp_getattr*/
12816N/A+ 0, /*tp_setattr*/
12816N/A+ 0, /*tp_compare*/
12816N/A+ 0, /*tp_repr*/
12816N/A+ 0, /*tp_as_number*/
12816N/A+ 0, /*tp_as_sequence*/
12816N/A+ 0, /*tp_as_mapping*/
12816N/A+ 0, /*tp_hash */
12816N/A+ 0, /*tp_call*/
12816N/A+ 0, /*tp_str*/
12816N/A+ 0, /*tp_getattro*/
12816N/A+ 0, /*tp_setattro*/
12816N/A+ 0, /*tp_as_buffer*/
12816N/A+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
12816N/A+ "user credentials", /*tp_doc */
12816N/A+ 0, /* tp_traverse */
12816N/A+ 0, /* tp_clear */
12816N/A+ 0, /* tp_richcompare */
12816N/A+ 0, /* tp_weaklistoffset */
12816N/A+ 0, /* tp_iter */
12816N/A+ 0, /* tp_iternext */
12816N/A+ pyucred_methods, /* tp_methods */
12816N/A+ 0, /* tp_members */
12816N/A+ 0, /* tp_getset */
12816N/A+ 0, /* tp_base */
12816N/A+ 0, /* tp_dict */
12816N/A+ 0, /* tp_descr_get */
12816N/A+ 0, /* tp_descr_set */
12816N/A+ 0, /* tp_dictoffset */
12816N/A+ (initproc)pyucred_init, /* tp_init */
12816N/A+ 0, /* tp_alloc */
12816N/A+ 0, /* tp_new */
12816N/A+};
12816N/A+
12816N/A+static PyObject *
12816N/A+pyucred_new(const ucred_t *uc)
12816N/A+{
12816N/A+ pyucred_t *self;
12816N/A+
12816N/A+ self = (pyucred_t *)PyObject_CallObject((PyObject *)&pyucred_type, NULL);
12816N/A+
12816N/A+ if (self == NULL)
12816N/A+ return (NULL);
12816N/A+
12816N/A+ self->ucred = (ucred_t *)uc;
12816N/A+
12816N/A+ return ((PyObject *)self);
12816N/A+}
12816N/A+
12816N/A+static PyObject *
12816N/A+pyucred_get(PyObject *o, PyObject *args, PyObject *kwargs)
12816N/A+{
12816N/A+ static char *kwlist[] = { "pid", NULL };
12816N/A+ ucred_t *ucred = NULL;
12816N/A+ int pid;
12816N/A+
12816N/A+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist,
12816N/A+ &pid))
12816N/A+ return (NULL);
12816N/A+
12816N/A+ ucred = ucred_get(pid);
12816N/A+
12816N/A+ if (ucred == NULL) {
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ return (NULL);
12816N/A+ }
12816N/A+
12816N/A+ return (pyucred_new(ucred));
12816N/A+}
12816N/A+
12816N/A+static PyObject *
12816N/A+pyucred_getpeer(PyObject *o, PyObject *args, PyObject *kwargs)
12816N/A+{
12816N/A+ static char *kwlist[] = { "fd", NULL };
12816N/A+ ucred_t *ucred = NULL;
12816N/A+ int fd;
12816N/A+
12816N/A+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist,
12816N/A+ &fd))
12816N/A+ return (NULL);
12816N/A+
12816N/A+ if (getpeerucred(fd, &ucred) == -1) {
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ return (NULL);
12816N/A+ }
12816N/A+
12816N/A+ return (pyucred_new(ucred));
12816N/A+}
12816N/A+
12816N/A+PyDoc_STRVAR(pyucred_get_doc,
12816N/A+ "get(pid) -> ucred\n"
12816N/A+ "\n"
12816N/A+ "Return the credentials of the specified process ID. "
12816N/A+ "Raises OSError. See ucred_get(3C).");
12816N/A+PyDoc_STRVAR(pyucred_getpeer_doc,
12816N/A+ "getpeer(fd) -> ucred\n"
12816N/A+ "\n"
12816N/A+ "Return the credentials of the peer endpoint of a "
12816N/A+ "connection-oriented socket (SOCK_STREAM) or STREAM fd "
12816N/A+ "at the time the endpoint was created or the connection "
12816N/A+ "was established. Raises OSError. See getpeerucred(3C).");
12816N/A+
12816N/A+static struct PyMethodDef pyucred_module_methods[] = {
12816N/A+ { "get", (PyCFunction) pyucred_get,
12816N/A+ METH_VARARGS|METH_KEYWORDS, pyucred_get_doc },
12816N/A+ { "getpeer", (PyCFunction) pyucred_getpeer,
12816N/A+ METH_VARARGS|METH_KEYWORDS, pyucred_getpeer_doc },
12816N/A+ { NULL, NULL, 0, NULL }
12816N/A+};
12816N/A+
12816N/A+PyDoc_STRVAR(pyucred_module_doc,
12816N/A+ "This module provides an interface to the user credential access "
12816N/A+ "methods, obtainable either by process ID or file descriptor.");
12816N/A+
12816N/A+PyMODINIT_FUNC
12816N/A+initucred(void)
12816N/A+{
12816N/A+ PyObject *m;
12816N/A+
12816N/A+ m = Py_InitModule3("ucred", pyucred_module_methods,
12816N/A+ pyucred_module_doc);
12816N/A+
12816N/A+ pyucred_type.tp_new = PyType_GenericNew;
12816N/A+ if (PyType_Ready(&pyucred_type) < 0)
12816N/A+ return;
12816N/A+
12816N/A+ Py_INCREF(&pyucred_type);
12816N/A+
12816N/A+ PyModule_AddObject(m, "ucred", (PyObject *)&pyucred_type);
12816N/A+}