r12829: fix ldb headers, to not include '<...>' files in .c files
[kamenim/samba.git] / source4 / lib / ldb / samba / ldif_handlers.c
1 /* 
2    ldb database library - ldif handlers for Samba
3
4    Copyright (C) Andrew Tridgell  2005
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 #include "includes.h"
26 #include "ldb/include/includes.h"
27
28 #include "librpc/gen_ndr/ndr_security.h"
29 #include "librpc/gen_ndr/ndr_misc.h"
30 #include "dsdb/samdb/samdb.h"
31
32 /*
33   convert a ldif formatted objectSid to a NDR formatted blob
34 */
35 static int ldif_read_objectSid(struct ldb_context *ldb, void *mem_ctx,
36                                const struct ldb_val *in, struct ldb_val *out)
37 {
38         struct dom_sid *sid;
39         NTSTATUS status;
40         sid = dom_sid_parse_talloc(mem_ctx, (const char *)in->data);
41         if (sid == NULL) {
42                 return -1;
43         }
44         status = ndr_push_struct_blob(out, mem_ctx, sid, 
45                                       (ndr_push_flags_fn_t)ndr_push_dom_sid);
46         talloc_free(sid);
47         if (!NT_STATUS_IS_OK(status)) {
48                 return -1;
49         }
50         return 0;
51 }
52
53 /*
54   convert a NDR formatted blob to a ldif formatted objectSid
55 */
56 static int ldif_write_objectSid(struct ldb_context *ldb, void *mem_ctx,
57                                 const struct ldb_val *in, struct ldb_val *out)
58 {
59         struct dom_sid *sid;
60         NTSTATUS status;
61         sid = talloc(mem_ctx, struct dom_sid);
62         if (sid == NULL) {
63                 return -1;
64         }
65         status = ndr_pull_struct_blob(in, sid, sid, 
66                                       (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
67         if (!NT_STATUS_IS_OK(status)) {
68                 talloc_free(sid);
69                 return -1;
70         }
71         out->data = (uint8_t *)dom_sid_string(mem_ctx, sid);
72         talloc_free(sid);
73         if (out->data == NULL) {
74                 return -1;
75         }
76         out->length = strlen((const char *)out->data);
77         return 0;
78 }
79
80 static BOOL ldb_comparision_objectSid_isString(const struct ldb_val *v)
81 {
82         /* see if the input if null-terninated */
83         if (v->data[v->length] != '\0') return False;
84         
85         if (strncmp("S-", (const char *)v->data, 2) != 0) return False;
86         return True;
87 }
88
89 /*
90   compare two objectSids
91 */
92 static int ldb_comparison_objectSid(struct ldb_context *ldb, void *mem_ctx,
93                                     const struct ldb_val *v1, const struct ldb_val *v2)
94 {
95         if (ldb_comparision_objectSid_isString(v1)) {
96                 if (ldb_comparision_objectSid_isString(v1)) {
97                         return strcmp((const char *)v1->data, (const char *)v2->data);
98                 } else {
99                         struct ldb_val v;
100                         int ret;
101                         if (ldif_read_objectSid(ldb, mem_ctx, v1, &v) != 0) {
102                                 return -1;
103                         }
104                         ret = ldb_comparison_binary(ldb, mem_ctx, &v, v2);
105                         talloc_free(v.data);
106                         return ret;
107                 }
108         }
109         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
110 }
111
112 /*
113   canonicalise a objectSid
114 */
115 static int ldb_canonicalise_objectSid(struct ldb_context *ldb, void *mem_ctx,
116                                       const struct ldb_val *in, struct ldb_val *out)
117 {
118         if (ldb_comparision_objectSid_isString(in)) {
119                 return ldif_read_objectSid(ldb, mem_ctx, in, out);
120         }
121         return ldb_handler_copy(ldb, mem_ctx, in, out);
122 }
123
124 /*
125   convert a ldif formatted objectGUID to a NDR formatted blob
126 */
127 static int ldif_read_objectGUID(struct ldb_context *ldb, void *mem_ctx,
128                                 const struct ldb_val *in, struct ldb_val *out)
129 {
130         struct GUID guid;
131         NTSTATUS status;
132
133         status = GUID_from_string((const char *)in->data, &guid);
134         if (!NT_STATUS_IS_OK(status)) {
135                 return -1;
136         }
137
138         status = ndr_push_struct_blob(out, mem_ctx, &guid,
139                                       (ndr_push_flags_fn_t)ndr_push_GUID);
140         if (!NT_STATUS_IS_OK(status)) {
141                 return -1;
142         }
143         return 0;
144 }
145
146 /*
147   convert a NDR formatted blob to a ldif formatted objectGUID
148 */
149 static int ldif_write_objectGUID(struct ldb_context *ldb, void *mem_ctx,
150                                  const struct ldb_val *in, struct ldb_val *out)
151 {
152         struct GUID guid;
153         NTSTATUS status;
154         status = ndr_pull_struct_blob(in, mem_ctx, &guid,
155                                       (ndr_pull_flags_fn_t)ndr_pull_GUID);
156         if (!NT_STATUS_IS_OK(status)) {
157                 return -1;
158         }
159         out->data = (uint8_t *)GUID_string(mem_ctx, &guid);
160         if (out->data == NULL) {
161                 return -1;
162         }
163         out->length = strlen((const char *)out->data);
164         return 0;
165 }
166
167 static BOOL ldb_comparision_objectGUID_isString(const struct ldb_val *v)
168 {
169         struct GUID guid;
170         NTSTATUS status;
171
172         /* see if the input if null-terninated */
173         if (v->data[v->length] != '\0') return False;
174
175         status = GUID_from_string((const char *)v->data, &guid);
176         if (!NT_STATUS_IS_OK(status)) {
177                 return False;
178         }
179
180         return True;
181 }
182
183 /*
184   compare two objectGUIDs
185 */
186 static int ldb_comparison_objectGUID(struct ldb_context *ldb, void *mem_ctx,
187                                      const struct ldb_val *v1, const struct ldb_val *v2)
188 {
189         if (ldb_comparision_objectGUID_isString(v1)) {
190                 if (ldb_comparision_objectGUID_isString(v2)) {
191                         return strcmp((const char *)v1->data, (const char *)v2->data);
192                 } else {
193                         struct ldb_val v;
194                         int ret;
195                         if (ldif_read_objectGUID(ldb, mem_ctx, v1, &v) != 0) {
196                                 return -1;
197                         }
198                         ret = ldb_comparison_binary(ldb, mem_ctx, &v, v2);
199                         talloc_free(v.data);
200                         return ret;
201                 }
202         }
203         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
204 }
205
206 /*
207   canonicalise a objectGUID
208 */
209 static int ldb_canonicalise_objectGUID(struct ldb_context *ldb, void *mem_ctx,
210                                        const struct ldb_val *in, struct ldb_val *out)
211 {
212         if (ldb_comparision_objectGUID_isString(in)) {
213                 return ldif_read_objectGUID(ldb, mem_ctx, in, out);
214         }
215         return ldb_handler_copy(ldb, mem_ctx, in, out);
216 }
217
218
219 /*
220   convert a ldif (SDDL) formatted ntSecurityDescriptor to a NDR formatted blob
221 */
222 static int ldif_read_ntSecurityDescriptor(struct ldb_context *ldb, void *mem_ctx,
223                                           const struct ldb_val *in, struct ldb_val *out)
224 {
225         struct security_descriptor *sd;
226         NTSTATUS status;
227         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
228         if (domain_sid == NULL) {
229                 return ldb_handler_copy(ldb, mem_ctx, in, out);
230         }
231         sd = sddl_decode(mem_ctx, (const char *)in->data, domain_sid);
232         if (sd == NULL) {
233                 return -1;
234         }
235         status = ndr_push_struct_blob(out, mem_ctx, sd, 
236                                       (ndr_push_flags_fn_t)ndr_push_security_descriptor);
237         talloc_free(sd);
238         if (!NT_STATUS_IS_OK(status)) {
239                 return -1;
240         }
241         return 0;
242 }
243
244 /*
245   convert a NDR formatted blob to a ldif formatted ntSecurityDescriptor (SDDL format)
246 */
247 static int ldif_write_ntSecurityDescriptor(struct ldb_context *ldb, void *mem_ctx,
248                                            const struct ldb_val *in, struct ldb_val *out)
249 {
250         struct security_descriptor *sd;
251         NTSTATUS status;
252         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
253
254         if (domain_sid == NULL) {
255                 return ldb_handler_copy(ldb, mem_ctx, in, out);
256         }
257
258         sd = talloc(mem_ctx, struct security_descriptor);
259         if (sd == NULL) {
260                 return -1;
261         }
262         status = ndr_pull_struct_blob(in, sd, sd, 
263                                       (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
264         if (!NT_STATUS_IS_OK(status)) {
265                 talloc_free(sd);
266                 return -1;
267         }
268         out->data = (uint8_t *)sddl_encode(mem_ctx, sd, domain_sid);
269         talloc_free(sd);
270         if (out->data == NULL) {
271                 return -1;
272         }
273         out->length = strlen((const char *)out->data);
274         return 0;
275 }
276
277 static const struct ldb_attrib_handler samba_handlers[] = {
278         { 
279                 .attr            = "objectSid",
280                 .flags           = 0,
281                 .ldif_read_fn    = ldif_read_objectSid,
282                 .ldif_write_fn   = ldif_write_objectSid,
283                 .canonicalise_fn = ldb_canonicalise_objectSid,
284                 .comparison_fn   = ldb_comparison_objectSid
285         },
286         { 
287                 .attr            = "securityIdentifier",
288                 .flags           = 0,
289                 .ldif_read_fn    = ldif_read_objectSid,
290                 .ldif_write_fn   = ldif_write_objectSid,
291                 .canonicalise_fn = ldb_canonicalise_objectSid,
292                 .comparison_fn   = ldb_comparison_objectSid
293         },
294         { 
295                 .attr            = "ntSecurityDescriptor",
296                 .flags           = 0,
297                 .ldif_read_fn    = ldif_read_ntSecurityDescriptor,
298                 .ldif_write_fn   = ldif_write_ntSecurityDescriptor,
299                 .canonicalise_fn = ldb_handler_copy,
300                 .comparison_fn   = ldb_comparison_binary
301         },
302         { 
303                 .attr            = "objectGUID",
304                 .flags           = 0,
305                 .ldif_read_fn    = ldif_read_objectGUID,
306                 .ldif_write_fn   = ldif_write_objectGUID,
307                 .canonicalise_fn = ldb_canonicalise_objectGUID,
308                 .comparison_fn   = ldb_comparison_objectGUID
309         },
310         { 
311                 .attr            = "invocationId",
312                 .flags           = 0,
313                 .ldif_read_fn    = ldif_read_objectGUID,
314                 .ldif_write_fn   = ldif_write_objectGUID,
315                 .canonicalise_fn = ldb_canonicalise_objectGUID,
316                 .comparison_fn   = ldb_comparison_objectGUID
317         },
318         { 
319                 .attr            = "schemaIDGUID",
320                 .flags           = 0,
321                 .ldif_read_fn    = ldif_read_objectGUID,
322                 .ldif_write_fn   = ldif_write_objectGUID,
323                 .canonicalise_fn = ldb_canonicalise_objectGUID,
324                 .comparison_fn   = ldb_comparison_objectGUID
325         }
326 };
327
328 /*
329   register the samba ldif handlers
330 */
331 int ldb_register_samba_handlers(struct ldb_context *ldb)
332 {
333         return ldb_set_attrib_handlers(ldb, samba_handlers, ARRAY_SIZE(samba_handlers));
334 }