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