s4-python: Move dsdb_convert_schema_to_openldap to dsdb.
[gd/samba-autobuild/.git] / source4 / dsdb / pydsdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2010
4    Copyright (C) Matthias Dieter Wallnöfer          2009
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <Python.h>
21 #include "includes.h"
22 #include "dsdb/samdb/samdb.h"
23 #include "lib/ldb/pyldb.h"
24
25 /* FIXME: These should be in a header file somewhere, once we finish moving
26  * away from SWIG .. */
27 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
28 /*      if (!PyLdb_Check(py_ldb)) { \
29                 PyErr_SetString(py_ldb_get_exception(), "Ldb connection object required"); \
30                 return NULL; \
31         } */\
32         ldb = PyLdb_AsLdbContext(py_ldb);
33
34 static PyObject *py_samdb_server_site_name(PyObject *self, PyObject *args)
35 {
36         PyObject *py_ldb, *result;
37         struct ldb_context *ldb;
38         const char *site;
39         TALLOC_CTX *mem_ctx;
40
41         if (!PyArg_ParseTuple(args, "O", &py_ldb))
42                 return NULL;
43
44         PyErr_LDB_OR_RAISE(py_ldb, ldb);
45
46         mem_ctx = talloc_new(NULL);
47
48         site = samdb_server_site_name(ldb, mem_ctx);
49         if (site == NULL) {
50                 PyErr_SetString(PyExc_RuntimeError, "Failed to find server site");
51                 talloc_free(mem_ctx);
52                 return NULL;
53         }
54
55         result = PyString_FromString(site);
56         talloc_free(mem_ctx);
57         return result;
58 }
59
60 /* XXX: This function really should be in pyldb.c */
61 static PyObject *py_dsdb_set_opaque_integer(PyObject *self, PyObject *args)
62 {
63         PyObject *py_ldb;
64         int value;
65         int *old_val, *new_val;
66         char *py_opaque_name, *opaque_name_talloc;
67         struct ldb_context *ldb;
68         TALLOC_CTX *tmp_ctx;
69
70         if (!PyArg_ParseTuple(args, "Osi", &py_ldb, &py_opaque_name, &value))
71                 return NULL;
72
73         PyErr_LDB_OR_RAISE(py_ldb, ldb);
74
75         /* see if we have a cached copy */
76         old_val = (int *)ldb_get_opaque(ldb, py_opaque_name);
77         /* XXX: We shouldn't just blindly assume that the value that is 
78          * already present has the size of an int and is not shared 
79          * with other code that may rely on it not changing. 
80          * JRV 20100403 */
81
82         if (old_val) {
83                 *old_val = value;
84                 Py_RETURN_NONE;
85         } 
86
87         tmp_ctx = talloc_new(ldb);
88         if (tmp_ctx == NULL) {
89                 PyErr_NoMemory();
90                 return NULL;
91         }
92         
93         new_val = talloc(tmp_ctx, int);
94         if (new_val == NULL) {
95                 talloc_free(tmp_ctx);
96                 PyErr_NoMemory();
97                 return NULL;
98         }
99         
100         opaque_name_talloc = talloc_strdup(tmp_ctx, py_opaque_name);
101         if (opaque_name_talloc == NULL) {
102                 talloc_free(tmp_ctx);
103                 PyErr_NoMemory();
104                 return NULL;
105         }
106         
107         *new_val = value;
108
109         /* cache the domain_sid in the ldb */
110         if (ldb_set_opaque(ldb, opaque_name_talloc, new_val) != LDB_SUCCESS) {
111                 talloc_free(tmp_ctx);
112                 PyErr_SetString(PyExc_RuntimeError,
113                                         "Failed to set opaque integer into the ldb");
114                 return NULL;
115         }
116
117         talloc_steal(ldb, new_val);
118         talloc_steal(ldb, opaque_name_talloc);
119         talloc_free(tmp_ctx);
120
121         Py_RETURN_NONE;
122 }
123
124 static PyObject *py_dsdb_convert_schema_to_openldap(PyObject *self,
125                                                                                                         PyObject *args)
126 {
127         char *target_str, *mapping;
128         PyObject *py_ldb;
129         struct ldb_context *ldb;
130         PyObject *ret;
131         char *retstr;
132
133         if (!PyArg_ParseTuple(args, "Oss", &py_ldb, &target_str, &mapping))
134                 return NULL;
135
136         PyErr_LDB_OR_RAISE(py_ldb, ldb);
137
138         retstr = dsdb_convert_schema_to_openldap(ldb, target_str, mapping);
139         if (retstr == NULL) {
140                 PyErr_SetString(PyExc_RuntimeError,
141                                                 "dsdb_convert_schema_to_openldap failed");
142                 return NULL;
143         } 
144
145         ret = PyString_FromString(retstr);
146         talloc_free(retstr);
147         return ret;
148 }
149
150 static PyMethodDef py_dsdb_methods[] = {
151         { "samdb_server_site_name", (PyCFunction)py_samdb_server_site_name,
152                 METH_VARARGS, "Get the server site name as a string"},
153         { "dsdb_set_opaque_integer", (PyCFunction)py_dsdb_set_opaque_integer,
154                 METH_VARARGS, NULL },
155         { "dsdb_convert_schema_to_openldap",
156                 (PyCFunction)py_dsdb_convert_schema_to_openldap, METH_VARARGS, 
157                 "dsdb_convert_schema_to_openldap(ldb, target_str, mapping) -> str\n"
158                 "Create an OpenLDAP schema from a schema." },
159         { NULL }
160 };
161
162 void initdsdb(void)
163 {
164         PyObject *m;
165
166         m = Py_InitModule3("dsdb", py_dsdb_methods, 
167                            "Python bindings for the directory service databases.");
168         if (m == NULL)
169                 return;
170
171         /* "userAccountControl" flags */
172         PyModule_AddObject(m, "UF_NORMAL_ACCOUNT", PyInt_FromLong(UF_NORMAL_ACCOUNT));
173         PyModule_AddObject(m, "UF_TEMP_DUPLICATE_ACCOUNT", PyInt_FromLong(UF_TEMP_DUPLICATE_ACCOUNT));
174         PyModule_AddObject(m, "UF_SERVER_TRUST_ACCOUNT", PyInt_FromLong(UF_SERVER_TRUST_ACCOUNT));
175         PyModule_AddObject(m, "UF_WORKSTATION_TRUST_ACCOUNT", PyInt_FromLong(UF_WORKSTATION_TRUST_ACCOUNT));
176         PyModule_AddObject(m, "UF_INTERDOMAIN_TRUST_ACCOUNT", PyInt_FromLong(UF_INTERDOMAIN_TRUST_ACCOUNT));
177         PyModule_AddObject(m, "UF_PASSWD_NOTREQD", PyInt_FromLong(UF_PASSWD_NOTREQD));
178         PyModule_AddObject(m, "UF_ACCOUNTDISABLE", PyInt_FromLong(UF_ACCOUNTDISABLE));
179
180         /* "groupType" flags */
181         PyModule_AddObject(m, "GTYPE_SECURITY_BUILTIN_LOCAL_GROUP", PyInt_FromLong(GTYPE_SECURITY_BUILTIN_LOCAL_GROUP));
182         PyModule_AddObject(m, "GTYPE_SECURITY_GLOBAL_GROUP", PyInt_FromLong(GTYPE_SECURITY_GLOBAL_GROUP));
183         PyModule_AddObject(m, "GTYPE_SECURITY_DOMAIN_LOCAL_GROUP", PyInt_FromLong(GTYPE_SECURITY_DOMAIN_LOCAL_GROUP));
184         PyModule_AddObject(m, "GTYPE_SECURITY_UNIVERSAL_GROUP", PyInt_FromLong(GTYPE_SECURITY_UNIVERSAL_GROUP));
185         PyModule_AddObject(m, "GTYPE_DISTRIBUTION_GLOBAL_GROUP", PyInt_FromLong(GTYPE_DISTRIBUTION_GLOBAL_GROUP));
186         PyModule_AddObject(m, "GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP", PyInt_FromLong(GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP));
187         PyModule_AddObject(m, "GTYPE_DISTRIBUTION_UNIVERSAL_GROUP", PyInt_FromLong(GTYPE_DISTRIBUTION_UNIVERSAL_GROUP));
188
189         /* "sAMAccountType" flags */
190         PyModule_AddObject(m, "ATYPE_NORMAL_ACCOUNT", PyInt_FromLong(ATYPE_NORMAL_ACCOUNT));
191         PyModule_AddObject(m, "ATYPE_WORKSTATION_TRUST", PyInt_FromLong(ATYPE_WORKSTATION_TRUST));
192         PyModule_AddObject(m, "ATYPE_INTERDOMAIN_TRUST", PyInt_FromLong(ATYPE_INTERDOMAIN_TRUST));
193         PyModule_AddObject(m, "ATYPE_SECURITY_GLOBAL_GROUP", PyInt_FromLong(ATYPE_SECURITY_GLOBAL_GROUP));
194         PyModule_AddObject(m, "ATYPE_SECURITY_LOCAL_GROUP", PyInt_FromLong(ATYPE_SECURITY_LOCAL_GROUP));
195         PyModule_AddObject(m, "ATYPE_SECURITY_UNIVERSAL_GROUP", PyInt_FromLong(ATYPE_SECURITY_UNIVERSAL_GROUP));
196         PyModule_AddObject(m, "ATYPE_DISTRIBUTION_GLOBAL_GROUP", PyInt_FromLong(ATYPE_DISTRIBUTION_GLOBAL_GROUP));
197         PyModule_AddObject(m, "ATYPE_DISTRIBUTION_LOCAL_GROUP", PyInt_FromLong(ATYPE_DISTRIBUTION_LOCAL_GROUP));
198         PyModule_AddObject(m, "ATYPE_DISTRIBUTION_UNIVERSAL_GROUP", PyInt_FromLong(ATYPE_DISTRIBUTION_UNIVERSAL_GROUP));
199
200         /* "domainFunctionality", "forestFunctionality" flags in the rootDSE */
201         PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2000", PyInt_FromLong(DS_DOMAIN_FUNCTION_2000));
202         PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2003_MIXED", PyInt_FromLong(DS_DOMAIN_FUNCTION_2003_MIXED));
203         PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2003", PyInt_FromLong(DS_DOMAIN_FUNCTION_2003));
204         PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2008", PyInt_FromLong(DS_DOMAIN_FUNCTION_2008));
205         PyModule_AddObject(m, "DS_DOMAIN_FUNCTION_2008_R2", PyInt_FromLong(DS_DOMAIN_FUNCTION_2008_R2));
206
207         /* "domainControllerFunctionality" flags in the rootDSE */
208         PyModule_AddObject(m, "DS_DC_FUNCTION_2000", PyInt_FromLong(DS_DC_FUNCTION_2000));
209         PyModule_AddObject(m, "DS_DC_FUNCTION_2003", PyInt_FromLong(DS_DC_FUNCTION_2003));
210         PyModule_AddObject(m, "DS_DC_FUNCTION_2008", PyInt_FromLong(DS_DC_FUNCTION_2008));
211         PyModule_AddObject(m, "DS_DC_FUNCTION_2008_R2", PyInt_FromLong(DS_DC_FUNCTION_2008_R2));
212 }