xref: /titanic_51/usr/src/lib/pysolaris/common/misc.c (revision e764248d4606662e466d4ee282fa5ef20e3dc578)
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  * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
25  */
26 
27 #include <Python.h>
28 #include <zone.h>
29 #include <libintl.h>
30 #include <idmap.h>
31 #include <directory.h>
32 
33 extern int sid_to_id(char *sid, boolean_t user, uid_t *id);
34 
35 static PyObject *
36 py_sid_to_id(PyObject *self, PyObject *args)
37 {
38 	char *sid;
39 	int err, isuser;
40 	uid_t id;
41 
42 	if (!PyArg_ParseTuple(args, "si", &sid, &isuser))
43 		return (NULL);
44 
45 	err = sid_to_id(sid, isuser, &id);
46 	if (err) {
47 		PyErr_SetString(PyExc_KeyError, sid);
48 		return (NULL);
49 	}
50 
51 	return (Py_BuildValue("I", id));
52 }
53 
54 /*
55  * Translate the sid string ("S-1-...") to the user@domain name, if
56  * possible.
57  */
58 static PyObject *
59 py_sid_to_name(PyObject *self, PyObject *args)
60 {
61 	int isuser, err, flag = IDMAP_REQ_FLG_USE_CACHE;
62 	char *name, *sid;
63 	idmap_stat stat;
64 	uid_t pid;
65 	PyObject *ret;
66 
67 	if (!PyArg_ParseTuple(args, "si", &sid, &isuser))
68 		return (NULL);
69 
70 	err = sid_to_id(sid, isuser, &pid);
71 	if (err) {
72 		PyErr_SetString(PyExc_KeyError, sid);
73 		return (NULL);
74 	}
75 	if (isuser)
76 		stat = idmap_getwinnamebyuid(pid, flag, &name, NULL);
77 	else
78 		stat = idmap_getwinnamebygid(pid, flag, &name, NULL);
79 	if (stat < 0) {
80 		PyErr_SetString(PyExc_KeyError, sid);
81 		return (NULL);
82 	}
83 
84 	if (name == NULL) {
85 		PyErr_SetString(PyExc_KeyError, sid);
86 		return (NULL);
87 	}
88 
89 	ret = PyUnicode_FromString(name);
90 	free(name);
91 	return (ret);
92 }
93 
94 static PyObject *
95 py_isglobalzone(PyObject *self, PyObject *args)
96 {
97 	return (Py_BuildValue("i", getzoneid() == GLOBAL_ZONEID));
98 }
99 
100 static PyObject *
101 py_gettext(PyObject *self, PyObject *args)
102 {
103 	char *message, *result;
104 	PyObject *ret = NULL;
105 
106 	if (!PyArg_ParseTuple(args, "s", &message))
107 		return (NULL);
108 
109 	result = dgettext(TEXT_DOMAIN, message);
110 
111 	ret = Py_BuildValue("s", result);
112 	return (ret);
113 }
114 
115 static PyMethodDef solarismethods[] = {
116 	{"sid_to_id", py_sid_to_id, METH_VARARGS, "Map SID to UID/GID."},
117 	{"sid_to_name", py_sid_to_name, METH_VARARGS,
118 	    "Map SID to name@domain."},
119 	{"isglobalzone", py_isglobalzone, METH_NOARGS,
120 	    "Determine if this is the global zone."},
121 	{"gettext", py_gettext, METH_VARARGS, "Native call to gettext(3C)"},
122 	{NULL, NULL, 0, NULL}
123 };
124 
125 #if PY_MAJOR_VERSION >= 3
126 static struct PyModuleDef solaris_module = {
127 	PyModuleDef_HEAD_INIT,
128 	"solaris.misc",
129 	NULL,
130 	-1,
131 	solarismethods
132 };
133 #endif
134 
135 static PyObject *
136 moduleinit()
137 {
138 #if PY_MAJOR_VERSION >= 3
139 	return (PyModule_Create(&solaris_module));
140 #else
141 	(void) Py_InitModule("solaris.misc", solarismethods);
142 	return (NULL);
143 #endif
144 }
145 
146 #if PY_MAJOR_VERSION >= 3
147 PyMODINIT_FUNC
148 PyInit_misc(void)
149 {
150 	return (moduleinit());
151 }
152 #else
153 PyMODINIT_FUNC
154 initmisc(void)
155 {
156 	(void) moduleinit();
157 }
158 #endif
159