1 This patch provides Python ucred support.
3 diff --git Python-2.6.4/Modules/ucred.c Python-2.6.4/Modules/ucred.c
6 +++ Python-2.6.4/Modules/ucred.c
9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
10 + * of this software and associated documentation files (the "Software"), to
11 + * deal in the Software without restriction, including without limitation the
12 + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13 + * sell copies of the Software, and to permit persons to whom the Software is
14 + * furnished to do so, subject to the following conditions:
16 + * The above copyright notice and this permission notice shall be included in
17 + * all copies or substantial portions of the Software.
19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 + * DEALINGS IN THE SOFTWARE.
27 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
36 +#include <tsol/label.h>
43 +#define pyucred_getlongid(name, type) \
45 + pyucred_get##name(pyucred_t *uc) \
49 + if (uc->ucred == NULL) { \
51 + PyErr_SetFromErrno(PyExc_OSError); \
55 + if ((val = ucred_get##name(uc->ucred)) == -1) { \
56 + PyErr_SetFromErrno(PyExc_OSError); \
60 + return (Py_BuildValue("l", (long)val)); \
63 +pyucred_getlongid(euid, uid_t)
64 +pyucred_getlongid(ruid, uid_t)
65 +pyucred_getlongid(suid, uid_t)
66 +pyucred_getlongid(egid, gid_t)
67 +pyucred_getlongid(rgid, gid_t)
68 +pyucred_getlongid(sgid, gid_t)
69 +pyucred_getlongid(pid, pid_t)
70 +pyucred_getlongid(projid, projid_t)
71 +pyucred_getlongid(zoneid, zoneid_t)
74 +pyucred_getgroups(pyucred_t *uc)
76 + const gid_t *groups;
81 + if (uc->ucred == NULL) {
83 + PyErr_SetFromErrno(PyExc_OSError);
87 + if ((len = ucred_getgroups(uc->ucred, &groups)) == -1) {
88 + PyErr_SetFromErrno(PyExc_OSError);
92 + if ((list = PyList_New(len)) == NULL)
95 + for (i = 0; i < len; i++) {
96 + PyObject *gid = Py_BuildValue("l", (long)groups[i]);
97 + if (PyList_SetItem(list, i, gid) == -1)
105 +pyucred_getlabel(pyucred_t *uc)
111 + if (uc->ucred == NULL) {
113 + PyErr_SetFromErrno(PyExc_OSError);
117 + label = ucred_getlabel(uc->ucred);
119 + return (Py_BuildValue("s", ""));
121 + if (label_to_str(label, &str, M_LABEL, DEF_NAMES) == -1) {
122 + PyErr_SetFromErrno(PyExc_OSError);
126 + ret = Py_BuildValue("s", str);
132 +pyucred_getpflags(pyucred_t *uc, PyObject *args, PyObject *kwargs)
134 + static char *kwlist[] = { "flags", NULL };
137 + if (uc->ucred == NULL) {
139 + PyErr_SetFromErrno(PyExc_OSError);
143 + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist,
147 + if ((flags = ucred_getpflags(uc->ucred, flags)) == (uint_t)-1) {
148 + PyErr_SetFromErrno(PyExc_OSError);
152 + return (Py_BuildValue("i", flags));
156 +pyucred_has_priv(pyucred_t *uc, PyObject *args, PyObject *kwargs)
158 + static char *kwlist[] = { "set", "priv", NULL };
159 + const priv_set_t *privs;
163 + if (uc->ucred == NULL) {
165 + PyErr_SetFromErrno(PyExc_OSError);
169 + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ss", kwlist,
173 + if ((privs = ucred_getprivset(uc->ucred, set)) == NULL) {
174 + PyErr_SetFromErrno(PyExc_OSError);
178 + if (priv_ismember(privs, priv)) {
179 + Py_INCREF(Py_True);
183 + Py_INCREF(Py_False);
187 +PyDoc_STRVAR(pyucred_getlabel_doc,
188 + "getlabel() -> string\n"
190 + "Return the Trusted Extensions label string, or an "
191 + "empty string if not available. The label string is "
192 + "converted using the default name and M_LABEL (human-readable). "
193 + "Raises OSError. See label_to_str(3TSOL).");
194 +PyDoc_STRVAR(pyucred_getpflags_doc,
195 + "getpflags(flags) -> int\n"
197 + "Return the values of the specified privilege flags.");
198 +PyDoc_STRVAR(pyucred_has_priv_doc,
199 + "has_priv(set, priv) -> bool\n"
201 + "Return true if the given privilege is set in the "
202 + "specified set. Raises OSError if the set or privilege is "
203 + "invalid, or a problem occurs.\n"
205 + "Currently, the following privilege sets are defined, as "
206 + "described in privileges(5):\n"
213 +static PyMethodDef pyucred_methods[] = {
214 + { "geteuid", (PyCFunction)pyucred_geteuid, METH_NOARGS,
215 + "Return the effective user ID." },
216 + { "getruid", (PyCFunction)pyucred_getruid, METH_NOARGS,
217 + "Return the real user ID." },
218 + { "getsuid", (PyCFunction)pyucred_getsuid, METH_NOARGS,
219 + "Return the saved user ID." },
220 + { "getegid", (PyCFunction)pyucred_getegid, METH_NOARGS,
221 + "Return the effective group ID." },
222 + { "getrgid", (PyCFunction)pyucred_getrgid, METH_NOARGS,
223 + "Return the real group ID." },
224 + { "getsgid", (PyCFunction)pyucred_getsgid, METH_NOARGS,
225 + "Return the saved group ID." },
226 + { "getpid", (PyCFunction)pyucred_getpid, METH_NOARGS,
227 + "Return the effective user ID." },
228 + { "getprojid", (PyCFunction)pyucred_getprojid, METH_NOARGS,
229 + "Return the project ID." },
230 + { "getzoneid", (PyCFunction)pyucred_getzoneid, METH_NOARGS,
231 + "Return the zone ID." },
232 + { "getgroups", (PyCFunction)pyucred_getgroups, METH_NOARGS,
233 + "Return a list of group IDs." },
234 + { "getlabel", (PyCFunction)pyucred_getlabel, METH_NOARGS,
235 + pyucred_getlabel_doc },
236 + { "getpflags", (PyCFunction)pyucred_getpflags,
237 + METH_VARARGS|METH_KEYWORDS, pyucred_getpflags_doc },
238 + { "has_priv", (PyCFunction)pyucred_has_priv,
239 + METH_VARARGS|METH_KEYWORDS, pyucred_has_priv_doc },
244 +pyucred_init(PyObject *self, PyObject *args, PyObject *kwargs)
246 + pyucred_t *uc = (pyucred_t *)self;
252 +pyucred_dealloc(PyObject *self)
254 + pyucred_t *uc = (pyucred_t *)self;
255 + if (uc->ucred != NULL)
256 + ucred_free(uc->ucred);
257 + self->ob_type->tp_free(self);
260 +static PyTypeObject pyucred_type = {
261 + PyObject_HEAD_INIT(NULL)
263 + "ucred.ucred", /*tp_name*/
264 + sizeof (pyucred_t), /*tp_basicsize*/
266 + pyucred_dealloc, /*tp_dealloc*/
272 + 0, /*tp_as_number*/
273 + 0, /*tp_as_sequence*/
274 + 0, /*tp_as_mapping*/
280 + 0, /*tp_as_buffer*/
281 + Py_TPFLAGS_DEFAULT, /*tp_flags*/
282 + "user credentials", /*tp_doc */
283 + 0, /* tp_traverse */
285 + 0, /* tp_richcompare */
286 + 0, /* tp_weaklistoffset */
288 + 0, /* tp_iternext */
289 + pyucred_methods, /* tp_methods */
290 + 0, /* tp_members */
294 + 0, /* tp_descr_get */
295 + 0, /* tp_descr_set */
296 + 0, /* tp_dictoffset */
297 + (initproc)pyucred_init, /* tp_init */
303 +pyucred_new(const ucred_t *uc)
307 + self = (pyucred_t *)PyObject_CallObject((PyObject *)&pyucred_type, NULL);
312 + self->ucred = (ucred_t *)uc;
314 + return ((PyObject *)self);
318 +pyucred_get(PyObject *o, PyObject *args, PyObject *kwargs)
320 + static char *kwlist[] = { "pid", NULL };
321 + ucred_t *ucred = NULL;
324 + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist,
328 + ucred = ucred_get(pid);
330 + if (ucred == NULL) {
331 + PyErr_SetFromErrno(PyExc_OSError);
335 + return (pyucred_new(ucred));
339 +pyucred_getpeer(PyObject *o, PyObject *args, PyObject *kwargs)
341 + static char *kwlist[] = { "fd", NULL };
342 + ucred_t *ucred = NULL;
345 + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist,
349 + if (getpeerucred(fd, &ucred) == -1) {
350 + PyErr_SetFromErrno(PyExc_OSError);
354 + return (pyucred_new(ucred));
357 +PyDoc_STRVAR(pyucred_get_doc,
358 + "get(pid) -> ucred\n"
360 + "Return the credentials of the specified process ID. "
361 + "Raises OSError. See ucred_get(3C).");
362 +PyDoc_STRVAR(pyucred_getpeer_doc,
363 + "getpeer(fd) -> ucred\n"
365 + "Return the credentials of the peer endpoint of a "
366 + "connection-oriented socket (SOCK_STREAM) or STREAM fd "
367 + "at the time the endpoint was created or the connection "
368 + "was established. Raises OSError. See getpeerucred(3C).");
370 +static struct PyMethodDef pyucred_module_methods[] = {
371 + { "get", (PyCFunction) pyucred_get,
372 + METH_VARARGS|METH_KEYWORDS, pyucred_get_doc },
373 + { "getpeer", (PyCFunction) pyucred_getpeer,
374 + METH_VARARGS|METH_KEYWORDS, pyucred_getpeer_doc },
375 + { NULL, NULL, 0, NULL }
378 +PyDoc_STRVAR(pyucred_module_doc,
379 + "This module provides an interface to the user credential access "
380 + "methods, obtainable either by process ID or file descriptor.");
387 + m = Py_InitModule3("ucred", pyucred_module_methods,
388 + pyucred_module_doc);
390 + pyucred_type.tp_new = PyType_GenericNew;
391 + if (PyType_Ready(&pyucred_type) < 0)
394 + Py_INCREF(&pyucred_type);
396 + PyModule_AddObject(m, "ucred", (PyObject *)&pyucred_type);
398 --- Python-2.7.6/setup.py.~2~ 2014-05-14 13:07:52.803164982 -0700
399 +++ Python-2.7.6/setup.py 2014-05-14 13:07:52.917214713 -0700
400 @@ -1536,6 +1536,13 @@
404 + # ucred module (Solaris)
405 + ucred_inc = find_file('ucred.h', [], inc_dirs)
406 + tsol_inc = find_file('tsol/label.h', [], inc_dirs)
407 + if ucred_inc is not None and tsol_inc is not None:
408 + exts.append( Extension('ucred', ['ucred.c'],
409 + libraries = ['tsol']) )
411 # Thomas Heller's _ctypes module
412 self.detect_ctypes(inc_dirs, lib_dirs)
414 --- /dev/null 2011-02-12 03:14:16.000000000 -0600
415 +++ Python-2.6.4/Lib/test/ucredtest.py 2011-01-20 13:52:42.945657919 -0600
417 +#!/usr/bin/python2.7
422 +uc = ucred.get(os.getpid())
424 +print "pid = %d" % uc.getpid()
425 +print "euid = %d" % uc.geteuid()
426 +print "ruid = %d" % uc.getruid()
427 +print "suid = %d" % uc.getsuid()
428 +print "egid = %d" % uc.getegid()
429 +print "rgid = %d" % uc.getrgid()
430 +print "sgid = %d" % uc.getsgid()
431 +print "zoneid = %d" % uc.getzoneid()
432 +print "projid = %d" % uc.getprojid()
433 +print "groups = %s" % uc.getgroups()
434 +print "label = %s" % uc.getlabel()
436 +print "getpflags(0x1) = %d" % uc.getpflags(0x1)
437 +print "getpflags(0x2) = %d" % uc.getpflags(0x2)
438 +print "has_priv(Effective, proc_fork) = %d" % uc.has_priv("Effective", "proc_fork")
439 +print "has_priv(Permitted, proc_fork) = %d" % uc.has_priv("Permitted", "proc_fork")
440 +print "has_priv(Inheritable, proc_fork) = %d" % uc.has_priv("Inheritable", "proc_fork")
441 +print "has_priv(Limit, file_setid) = %d" % uc.has_priv("Limit", "file_setid")
442 +print "has_priv(Effective, file_setid) = %d" % uc.has_priv("Effective", "file_setid")
444 + uc.has_priv("Effective", "proc_bork")
448 + uc.has_priv("Defective", "proc_fork")
452 + uc.has_priv("Defective", "proc_bork")