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+ if ucred_inc is not None and tsol_inc is not None:
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+ * The above copyright notice and this permission notice shall be included in
12816N/A+ * all copies or substantial portions of the Software.
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+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
12816N/A+ * Use is subject to license terms.
12816N/A+#define pyucred_getlongid(name, type) \
12816N/A+ pyucred_get##name(pyucred_t *uc) \
12816N/A+ PyErr_SetFromErrno(PyExc_OSError); \
12816N/A+ if ((val = ucred_get##name(uc->ucred)) == -1) { \
12816N/A+ PyErr_SetFromErrno(PyExc_OSError); \
12816N/A+ return (Py_BuildValue("l", (long)val)); \
12816N/A+pyucred_getlongid(projid, projid_t)
12816N/A+pyucred_getlongid(zoneid, zoneid_t)
12816N/A+pyucred_getgroups(pyucred_t *uc)
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ if ((len = ucred_getgroups(uc->ucred, &groups)) == -1) {
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ if ((list = PyList_New(len)) == NULL)
12816N/A+ PyObject *gid = Py_BuildValue("l", (long)groups[i]);
12816N/A+ if (PyList_SetItem(list, i, gid) == -1)
12816N/A+pyucred_getlabel(pyucred_t *uc)
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ label = ucred_getlabel(uc->ucred);
12816N/A+ return (Py_BuildValue("s", ""));
12816N/A+ if (label_to_str(label, &str, M_LABEL, DEF_NAMES) == -1) {
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ ret = Py_BuildValue("s", str);
12816N/A+pyucred_getpflags(pyucred_t *uc, PyObject *args, PyObject *kwargs)
12816N/A+ static char *kwlist[] = { "flags", NULL };
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist,
12816N/A+ if ((flags = ucred_getpflags(uc->ucred, flags)) == (uint_t)-1) {
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ return (Py_BuildValue("i", flags));
12816N/A+pyucred_has_priv(pyucred_t *uc, PyObject *args, PyObject *kwargs)
12816N/A+ static char *kwlist[] = { "set", "priv", NULL };
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ss", kwlist,
12816N/A+ if ((privs = ucred_getprivset(uc->ucred, set)) == NULL) {
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+ if (priv_ismember(privs, priv)) {
12816N/A+PyDoc_STRVAR(pyucred_getlabel_doc,
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+ "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+ "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+ "Currently, the following privilege sets are defined, as "
12816N/A+ "described in privileges(5):\n"
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+ { "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+ { "getzoneid", (PyCFunction)pyucred_getzoneid, METH_NOARGS,
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+ { "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+pyucred_init(PyObject *self, PyObject *args, PyObject *kwargs)
12816N/A+ pyucred_t *uc = (pyucred_t *)self;
12816N/A+pyucred_dealloc(PyObject *self)
12816N/A+ pyucred_t *uc = (pyucred_t *)self;
12816N/A+static PyTypeObject pyucred_type = {
12816N/A+ sizeof (pyucred_t), /*tp_basicsize*/
12816N/A+ pyucred_dealloc, /*tp_dealloc*/
12816N/A+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
12816N/A+ "user credentials", /*tp_doc */
12816N/A+ pyucred_methods, /* tp_methods */
12816N/A+ (initproc)pyucred_init, /* tp_init */
12816N/A+ self = (pyucred_t *)PyObject_CallObject((PyObject *)&pyucred_type, NULL);
12816N/A+pyucred_get(PyObject *o, PyObject *args, PyObject *kwargs)
12816N/A+ static char *kwlist[] = { "pid", NULL };
12816N/A+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist,
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
12816N/A+pyucred_getpeer(PyObject *o, PyObject *args, PyObject *kwargs)
12816N/A+ static char *kwlist[] = { "fd", NULL };
12816N/A+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist,
12816N/A+ if (getpeerucred(fd, &ucred) == -1) {
12816N/A+ PyErr_SetFromErrno(PyExc_OSError);
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+ "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+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+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+ m = Py_InitModule3("ucred", pyucred_module_methods,
12816N/A+ if (PyType_Ready(&pyucred_type) < 0)
12816N/A+ PyModule_AddObject(m, "ucred", (PyObject *)&pyucred_type);