xref: /illumos-gate/usr/src/lib/pysolaris/common/misc.c (revision 48edc7cf07b5dccc3ad84bf2dafe4150bd666d60)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <Python.h>
27 #include <zone.h>
28 #include <libintl.h>
29 #include <idmap.h>
30 #include <directory.h>
31 
32 extern int sid_to_id(char *sid, boolean_t user, uid_t *id);
33 
34 static PyObject *
35 py_sid_to_id(PyObject *self, PyObject *args)
36 {
37 	char *sid;
38 	int err, isuser;
39 	uid_t id;
40 
41 	if (!PyArg_ParseTuple(args, "si", &sid, &isuser))
42 		return (NULL);
43 
44 	err = sid_to_id(sid, isuser, &id);
45 	if (err) {
46 		PyErr_SetString(PyExc_KeyError, sid);
47 		return (NULL);
48 	}
49 
50 	return (Py_BuildValue("I", id));
51 }
52 
53 /*
54  * Translate the sid string ("S-1-...") to the user@domain name, if
55  * possible.
56  */
57 static PyObject *
58 py_sid_to_name(PyObject *self, PyObject *args)
59 {
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;
65 
66 	if (!PyArg_ParseTuple(args, "si", &sid, &isuser))
67 		return (NULL);
68 
69 	err = sid_to_id(sid, isuser, &pid);
70 	if (err) {
71 		PyErr_SetString(PyExc_KeyError, sid);
72 		return (NULL);
73 	}
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);
81 	}
82 
83 	if (name == NULL) {
84 		PyErr_SetString(PyExc_KeyError, sid);
85 		return (NULL);
86 	}
87 
88 	ret = PyString_FromString(name);
89 	free(name);
90 	return (ret);
91 }
92 
93 static PyObject *
94 py_isglobalzone(PyObject *self, PyObject *args)
95 {
96 	return (Py_BuildValue("i", getzoneid() == GLOBAL_ZONEID));
97 }
98 
99 static PyObject *
100 py_gettext(PyObject *self, PyObject *args)
101 {
102 	char *message, *result;
103 	PyObject *ret = NULL;
104 
105 	if (!PyArg_ParseTuple(args, "s", &message))
106 		return (NULL);
107 
108 	result = dgettext(TEXT_DOMAIN, message);
109 
110 	ret = Py_BuildValue("s", result);
111 	return (ret);
112 }
113 
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}
122 };
123 
124 void
125 initmisc(void)
126 {
127 	PyObject *solaris_misc = Py_InitModule("solaris.misc", solarismethods);
128 }
129