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