2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#include <Python.h>
2N/A#include <zone.h>
2N/A#include <libintl.h>
2N/A#include <directory.h>
2N/A
2N/A#ifdef __lint
2N/A#define dgettext(x, y) y
2N/A#endif
2N/A
2N/A#define _(s) dgettext(TEXT_DOMAIN, s)
2N/A
2N/Aextern int sid_to_id(char *sid, boolean_t user, uid_t *id);
2N/A
2N/Astatic PyObject *
2N/Apy_sid_to_id(PyObject *self, PyObject *args)
2N/A{
2N/A char *sid;
2N/A int err, isuser;
2N/A uid_t id;
2N/A
2N/A if (!PyArg_ParseTuple(args, "si", &sid, &isuser))
2N/A return (NULL);
2N/A
2N/A err = sid_to_id(sid, isuser, &id);
2N/A if (err) {
2N/A PyErr_SetString(PyExc_KeyError, sid);
2N/A return (NULL);
2N/A }
2N/A
2N/A return (Py_BuildValue("I", id));
2N/A}
2N/A
2N/A/*
2N/A * Translate the sid string ("S-1-...") to the user@domain name, if
2N/A * possible.
2N/A */
2N/Astatic PyObject *
2N/Apy_sid_to_name(PyObject *self, PyObject *args)
2N/A{
2N/A int isuser;
2N/A char *name, *sid;
2N/A directory_error_t e;
2N/A uint64_t classes;
2N/A PyObject *ret;
2N/A
2N/A if (!PyArg_ParseTuple(args, "si", &sid, &isuser))
2N/A return (NULL);
2N/A e = directory_name_from_sid(NULL, sid, &name, &classes);
2N/A if (e != NULL) {
2N/A directory_error_free(e);
2N/A PyErr_SetString(PyExc_KeyError, sid);
2N/A return (NULL);
2N/A }
2N/A if (name == NULL) {
2N/A PyErr_SetString(PyExc_KeyError, sid);
2N/A return (NULL);
2N/A }
2N/A if (isuser) {
2N/A if (!(classes & DIRECTORY_CLASS_USER)) {
2N/A free(name);
2N/A PyErr_SetString(PyExc_KeyError, sid);
2N/A return (NULL);
2N/A }
2N/A } else {
2N/A if (!(classes & DIRECTORY_CLASS_GROUP)) {
2N/A free(name);
2N/A PyErr_SetString(PyExc_KeyError, sid);
2N/A return (NULL);
2N/A }
2N/A }
2N/A
2N/A ret = PyString_FromString(name);
2N/A free(name);
2N/A return (ret);
2N/A}
2N/A
2N/Astatic PyObject *
2N/Apy_isglobalzone(PyObject *self, PyObject *args)
2N/A{
2N/A return (Py_BuildValue("i", getzoneid() == GLOBAL_ZONEID));
2N/A}
2N/A
2N/Astatic PyObject *
2N/Apy_gettext(PyObject *self, PyObject *args)
2N/A{
2N/A char *message, *result;
2N/A PyObject *ret = NULL;
2N/A
2N/A if (!PyArg_ParseTuple(args, "s", &message))
2N/A return (NULL);
2N/A
2N/A result = dgettext(TEXT_DOMAIN, message);
2N/A
2N/A ret = Py_BuildValue("s", result);
2N/A return (ret);
2N/A}
2N/A
2N/Astatic PyMethodDef solarismethods[] = {
2N/A {"sid_to_id", py_sid_to_id, METH_VARARGS, "Map SID to UID/GID."},
2N/A {"sid_to_name", py_sid_to_name, METH_VARARGS,
2N/A "Map SID to name@domain."},
2N/A {"isglobalzone", py_isglobalzone, METH_NOARGS,
2N/A "Determine if this is the global zone."},
2N/A {"gettext", py_gettext, METH_VARARGS, "Native call to gettext(3C)"},
2N/A {NULL, NULL, 0, NULL}
2N/A};
2N/A
2N/Avoid
2N/Ainitmisc(void)
2N/A{
2N/A char *noop;
2N/A
2N/A noop = _("noop");
2N/A PyObject *solaris_misc = Py_InitModule("solaris.misc", solarismethods);
2N/A}