dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / pysolaris / common / misc.c
blob13280d461f6dd08e26e2462a8920f557ba77c7e4
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <Python.h>
27 #include <zone.h>
28 #include <libintl.h>
29 #include <idmap.h>
30 #include <directory.h>
32 extern int sid_to_id(char *sid, boolean_t user, uid_t *id);
34 static PyObject *
35 py_sid_to_id(PyObject *self, PyObject *args)
37 char *sid;
38 int err, isuser;
39 uid_t id;
41 if (!PyArg_ParseTuple(args, "si", &sid, &isuser))
42 return (NULL);
44 err = sid_to_id(sid, isuser, &id);
45 if (err) {
46 PyErr_SetString(PyExc_KeyError, sid);
47 return (NULL);
50 return (Py_BuildValue("I", id));
54 * Translate the sid string ("S-1-...") to the user@domain name, if
55 * possible.
57 static PyObject *
58 py_sid_to_name(PyObject *self, PyObject *args)
60 int isuser, err, flag = IDMAP_REQ_FLG_USE_CACHE;
61 char *name, *sid;
62 idmap_stat stat;
63 uid_t pid;
64 PyObject *ret;
66 if (!PyArg_ParseTuple(args, "si", &sid, &isuser))
67 return (NULL);
69 err = sid_to_id(sid, isuser, &pid);
70 if (err) {
71 PyErr_SetString(PyExc_KeyError, sid);
72 return (NULL);
74 if (isuser)
75 stat = idmap_getwinnamebyuid(pid, flag, &name, NULL);
76 else
77 stat = idmap_getwinnamebygid(pid, flag, &name, NULL);
78 if (stat < 0) {
79 PyErr_SetString(PyExc_KeyError, sid);
80 return (NULL);
83 if (name == NULL) {
84 PyErr_SetString(PyExc_KeyError, sid);
85 return (NULL);
88 ret = PyString_FromString(name);
89 free(name);
90 return (ret);
93 static PyObject *
94 py_isglobalzone(PyObject *self, PyObject *args)
96 return (Py_BuildValue("i", getzoneid() == GLOBAL_ZONEID));
99 static PyObject *
100 py_gettext(PyObject *self, PyObject *args)
102 char *message, *result;
103 PyObject *ret = NULL;
105 if (!PyArg_ParseTuple(args, "s", &message))
106 return (NULL);
108 result = dgettext(TEXT_DOMAIN, message);
110 ret = Py_BuildValue("s", result);
111 return (ret);
114 static PyMethodDef solarismethods[] = {
115 {"sid_to_id", py_sid_to_id, METH_VARARGS, "Map SID to UID/GID."},
116 {"sid_to_name", py_sid_to_name, METH_VARARGS,
117 "Map SID to name@domain."},
118 {"isglobalzone", py_isglobalzone, METH_NOARGS,
119 "Determine if this is the global zone."},
120 {"gettext", py_gettext, METH_VARARGS, "Native call to gettext(3C)"},
121 {NULL, NULL, 0, NULL}
124 void
125 initmisc(void)
127 PyObject *solaris_misc = Py_InitModule("solaris.misc", solarismethods);