xref: /titanic_50/usr/src/lib/pysolaris/common/misc.c (revision 69a119caa6570c7077699161b7c28b6ee9f8b0f4)
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 <directory.h>
30 
31 #ifdef __lint
32 #define	dgettext(x, y) y
33 #endif
34 
35 #define	_(s) dgettext(TEXT_DOMAIN, s)
36 
37 extern int sid_to_id(char *sid, boolean_t user, uid_t *id);
38 
39 static PyObject *
40 py_sid_to_id(PyObject *self, PyObject *args)
41 {
42 	char *sid;
43 	int err, isuser;
44 	uid_t id;
45 
46 	if (!PyArg_ParseTuple(args, "si", &sid, &isuser))
47 		return (NULL);
48 
49 	err = sid_to_id(sid, isuser, &id);
50 	if (err) {
51 		PyErr_SetString(PyExc_KeyError, sid);
52 		return (NULL);
53 	}
54 
55 	return (Py_BuildValue("I", id));
56 }
57 
58 /*
59  * Translate the sid string ("S-1-...") to the user@domain name, if
60  * possible.
61  */
62 static PyObject *
63 py_sid_to_name(PyObject *self, PyObject *args)
64 {
65 	int isuser;
66 	char *name, *sid;
67 	directory_error_t e;
68 	uint64_t classes;
69 	PyObject *ret;
70 
71 	if (!PyArg_ParseTuple(args, "si", &sid, &isuser))
72 		return (NULL);
73 	e = directory_name_from_sid(NULL, sid, &name, &classes);
74 	if (e != NULL) {
75 		directory_error_free(e);
76 		PyErr_SetString(PyExc_KeyError, sid);
77 		return (NULL);
78 	}
79 	if (name == NULL) {
80 		PyErr_SetString(PyExc_KeyError, sid);
81 		return (NULL);
82 	}
83 	if (isuser) {
84 		if (!(classes & DIRECTORY_CLASS_USER)) {
85 			free(name);
86 			PyErr_SetString(PyExc_KeyError, sid);
87 			return (NULL);
88 		}
89 	} else {
90 		if (!(classes & DIRECTORY_CLASS_GROUP)) {
91 			free(name);
92 			PyErr_SetString(PyExc_KeyError, sid);
93 			return (NULL);
94 		}
95 	}
96 
97 	ret = PyString_FromString(name);
98 	free(name);
99 	return (ret);
100 }
101 
102 static PyObject *
103 py_isglobalzone(PyObject *self, PyObject *args)
104 {
105 	return (Py_BuildValue("i", getzoneid() == GLOBAL_ZONEID));
106 }
107 
108 static PyObject *
109 py_gettext(PyObject *self, PyObject *args)
110 {
111 	char *message, *result;
112 	PyObject *ret = NULL;
113 
114 	if (!PyArg_ParseTuple(args, "s", &message))
115 		return (NULL);
116 
117 	result = dgettext(TEXT_DOMAIN, message);
118 
119 	ret = Py_BuildValue("s", result);
120 	return (ret);
121 }
122 
123 static PyMethodDef solarismethods[] = {
124 	{"sid_to_id", py_sid_to_id, METH_VARARGS, "Map SID to UID/GID."},
125 	{"sid_to_name", py_sid_to_name, METH_VARARGS,
126 	    "Map SID to name@domain."},
127 	{"isglobalzone", py_isglobalzone, METH_NOARGS,
128 	    "Determine if this is the global zone."},
129 	{"gettext", py_gettext, METH_VARARGS, "Native call to gettext(3C)"},
130 	{NULL, NULL, 0, NULL}
131 };
132 
133 void
134 initmisc(void)
135 {
136 	char *noop;
137 
138 	noop = _("noop");
139 	PyObject *solaris_misc = Py_InitModule("solaris.misc", solarismethods);
140 }
141