r13924: Split more prototypes out of include/proto.h + initial work on header
[jelmer/samba4-debian.git] / source / 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 #include "libcli/security/proto.h"
32
33 /*
34   convert a ldif formatted objectSid to a NDR formatted blob
35 */
36 static int ldif_read_objectSid(struct ldb_context *ldb, void *mem_ctx,
37                                const struct ldb_val *in, struct ldb_val *out)
38 {
39         struct dom_sid *sid;
40         NTSTATUS status;
41         sid = dom_sid_parse_talloc(mem_ctx, (const char *)in->data);
42         if (sid == NULL) {
43                 return -1;
44         }
45         status = ndr_push_struct_blob(out, mem_ctx, sid, 
46                                       (ndr_push_flags_fn_t)ndr_push_dom_sid);
47         talloc_free(sid);
48         if (!NT_STATUS_IS_OK(status)) {
49                 return -1;
50         }
51         return 0;
52 }
53
54 /*
55   convert a NDR formatted blob to a ldif formatted objectSid
56 */
57 static int ldif_write_objectSid(struct ldb_context *ldb, void *mem_ctx,
58                                 const struct ldb_val *in, struct ldb_val *out)
59 {
60         struct dom_sid *sid;
61         NTSTATUS status;
62         sid = talloc(mem_ctx, struct dom_sid);
63         if (sid == NULL) {
64                 return -1;
65         }
66         status = ndr_pull_struct_blob(in, sid, sid, 
67                                       (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
68         if (!NT_STATUS_IS_OK(status)) {
69                 talloc_free(sid);
70                 return -1;
71         }
72         out->data = (uint8_t *)dom_sid_string(mem_ctx, sid);
73         talloc_free(sid);
74         if (out->data == NULL) {
75                 return -1;
76         }
77         out->length = strlen((const char *)out->data);
78         return 0;
79 }
80
81 static BOOL ldb_comparision_objectSid_isString(const struct ldb_val *v)
82 {
83         /* see if the input if null-terninated */
84         if (v->data[v->length] != '\0') return False;
85         
86         if (strncmp("S-", (const char *)v->data, 2) != 0) return False;
87         return True;
88 }
89
90 /*
91   compare two objectSids
92 */
93 static int ldb_comparison_objectSid(struct ldb_context *ldb, void *mem_ctx,
94                                     const struct ldb_val *v1, const struct ldb_val *v2)
95 {
96         if (ldb_comparision_objectSid_isString(v1)) {
97                 if (ldb_comparision_objectSid_isString(v2)) {
98                         return strcmp((const char *)v1->data, (const char *)v2->data);
99                 } else {
100                         struct ldb_val v;
101                         int ret;
102                         if (ldif_read_objectSid(ldb, mem_ctx, v1, &v) != 0) {
103                                 return -1;
104                         }
105                         ret = ldb_comparison_binary(ldb, mem_ctx, &v, v2);
106                         talloc_free(v.data);
107                         return ret;
108                 }
109         }
110         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
111 }
112
113 /*
114   canonicalise a objectSid
115 */
116 static int ldb_canonicalise_objectSid(struct ldb_context *ldb, void *mem_ctx,
117                                       const struct ldb_val *in, struct ldb_val *out)
118 {
119         if (ldb_comparision_objectSid_isString(in)) {
120                 return ldif_read_objectSid(ldb, mem_ctx, in, out);
121         }
122         return ldb_handler_copy(ldb, mem_ctx, in, out);
123 }
124
125 /*
126   convert a ldif formatted objectGUID to a NDR formatted blob
127 */
128 static int ldif_read_objectGUID(struct ldb_context *ldb, void *mem_ctx,
129                                 const struct ldb_val *in, struct ldb_val *out)
130 {
131         struct GUID guid;
132         NTSTATUS status;
133
134         status = GUID_from_string((const char *)in->data, &guid);
135         if (!NT_STATUS_IS_OK(status)) {
136                 return -1;
137         }
138
139         status = ndr_push_struct_blob(out, mem_ctx, &guid,
140                                       (ndr_push_flags_fn_t)ndr_push_GUID);
141         if (!NT_STATUS_IS_OK(status)) {
142                 return -1;
143         }
144         return 0;
145 }
146
147 /*
148   convert a NDR formatted blob to a ldif formatted objectGUID
149 */
150 static int ldif_write_objectGUID(struct ldb_context *ldb, void *mem_ctx,
151                                  const struct ldb_val *in, struct ldb_val *out)
152 {
153         struct GUID guid;
154         NTSTATUS status;
155         status = ndr_pull_struct_blob(in, mem_ctx, &guid,
156                                       (ndr_pull_flags_fn_t)ndr_pull_GUID);
157         if (!NT_STATUS_IS_OK(status)) {
158                 return -1;
159         }
160         out->data = (uint8_t *)GUID_string(mem_ctx, &guid);
161         if (out->data == NULL) {
162                 return -1;
163         }
164         out->length = strlen((const char *)out->data);
165         return 0;
166 }
167
168 static BOOL ldb_comparision_objectGUID_isString(const struct ldb_val *v)
169 {
170         struct GUID guid;
171         NTSTATUS status;
172
173         /* see if the input if null-terninated */
174         if (v->data[v->length] != '\0') return False;
175
176         status = GUID_from_string((const char *)v->data, &guid);
177         if (!NT_STATUS_IS_OK(status)) {
178                 return False;
179         }
180
181         return True;
182 }
183
184 /*
185   compare two objectGUIDs
186 */
187 static int ldb_comparison_objectGUID(struct ldb_context *ldb, void *mem_ctx,
188                                      const struct ldb_val *v1, const struct ldb_val *v2)
189 {
190         if (ldb_comparision_objectGUID_isString(v1)) {
191                 if (ldb_comparision_objectGUID_isString(v2)) {
192                         return strcmp((const char *)v1->data, (const char *)v2->data);
193                 } else {
194                         struct ldb_val v;
195                         int ret;
196                         if (ldif_read_objectGUID(ldb, mem_ctx, v1, &v) != 0) {
197                                 return -1;
198                         }
199                         ret = ldb_comparison_binary(ldb, mem_ctx, &v, v2);
200                         talloc_free(v.data);
201                         return ret;
202                 }
203         }
204         return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
205 }
206
207 /*
208   canonicalise a objectGUID
209 */
210 static int ldb_canonicalise_objectGUID(struct ldb_context *ldb, void *mem_ctx,
211                                        const struct ldb_val *in, struct ldb_val *out)
212 {
213         if (ldb_comparision_objectGUID_isString(in)) {
214                 return ldif_read_objectGUID(ldb, mem_ctx, in, out);
215         }
216         return ldb_handler_copy(ldb, mem_ctx, in, out);
217 }
218
219
220 /*
221   convert a ldif (SDDL) formatted ntSecurityDescriptor to a NDR formatted blob
222 */
223 static int ldif_read_ntSecurityDescriptor(struct ldb_context *ldb, void *mem_ctx,
224                                           const struct ldb_val *in, struct ldb_val *out)
225 {
226         struct security_descriptor *sd;
227         NTSTATUS status;
228         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
229         if (domain_sid == NULL) {
230                 return ldb_handler_copy(ldb, mem_ctx, in, out);
231         }
232         sd = sddl_decode(mem_ctx, (const char *)in->data, domain_sid);
233         if (sd == NULL) {
234                 return -1;
235         }
236         status = ndr_push_struct_blob(out, mem_ctx, sd, 
237                                       (ndr_push_flags_fn_t)ndr_push_security_descriptor);
238         talloc_free(sd);
239         if (!NT_STATUS_IS_OK(status)) {
240                 return -1;
241         }
242         return 0;
243 }
244
245 /*
246   convert a NDR formatted blob to a ldif formatted ntSecurityDescriptor (SDDL format)
247 */
248 static int ldif_write_ntSecurityDescriptor(struct ldb_context *ldb, void *mem_ctx,
249                                            const struct ldb_val *in, struct ldb_val *out)
250 {
251         struct security_descriptor *sd;
252         NTSTATUS status;
253         const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
254
255         if (domain_sid == NULL) {
256                 return ldb_handler_copy(ldb, mem_ctx, in, out);
257         }
258
259         sd = talloc(mem_ctx, struct security_descriptor);
260         if (sd == NULL) {
261                 return -1;
262         }
263         status = ndr_pull_struct_blob(in, sd, sd, 
264                                       (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
265         if (!NT_STATUS_IS_OK(status)) {
266                 talloc_free(sd);
267                 return -1;
268         }
269         out->data = (uint8_t *)sddl_encode(mem_ctx, sd, domain_sid);
270         talloc_free(sd);
271         if (out->data == NULL) {
272                 return -1;
273         }
274         out->length = strlen((const char *)out->data);
275         return 0;
276 }
277
278 static const struct ldb_attrib_handler samba_handlers[] = {
279         { 
280                 .attr            = "objectSid",
281                 .flags           = 0,
282                 .ldif_read_fn    = ldif_read_objectSid,
283                 .ldif_write_fn   = ldif_write_objectSid,
284                 .canonicalise_fn = ldb_canonicalise_objectSid,
285                 .comparison_fn   = ldb_comparison_objectSid
286         },
287         { 
288                 .attr            = "securityIdentifier",
289                 .flags           = 0,
290                 .ldif_read_fn    = ldif_read_objectSid,
291                 .ldif_write_fn   = ldif_write_objectSid,
292                 .canonicalise_fn = ldb_canonicalise_objectSid,
293                 .comparison_fn   = ldb_comparison_objectSid
294         },
295         { 
296                 .attr            = "ntSecurityDescriptor",
297                 .flags           = 0,
298                 .ldif_read_fn    = ldif_read_ntSecurityDescriptor,
299                 .ldif_write_fn   = ldif_write_ntSecurityDescriptor,
300                 .canonicalise_fn = ldb_handler_copy,
301                 .comparison_fn   = ldb_comparison_binary
302         },
303         { 
304                 .attr            = "objectGUID",
305                 .flags           = 0,
306                 .ldif_read_fn    = ldif_read_objectGUID,
307                 .ldif_write_fn   = ldif_write_objectGUID,
308                 .canonicalise_fn = ldb_canonicalise_objectGUID,
309                 .comparison_fn   = ldb_comparison_objectGUID
310         },
311         { 
312                 .attr            = "invocationId",
313                 .flags           = 0,
314                 .ldif_read_fn    = ldif_read_objectGUID,
315                 .ldif_write_fn   = ldif_write_objectGUID,
316                 .canonicalise_fn = ldb_canonicalise_objectGUID,
317                 .comparison_fn   = ldb_comparison_objectGUID
318         },
319         { 
320                 .attr            = "schemaIDGUID",
321                 .flags           = 0,
322                 .ldif_read_fn    = ldif_read_objectGUID,
323                 .ldif_write_fn   = ldif_write_objectGUID,
324                 .canonicalise_fn = ldb_canonicalise_objectGUID,
325                 .comparison_fn   = ldb_comparison_objectGUID
326         }
327 };
328
329 /*
330   register the samba ldif handlers
331 */
332 int ldb_register_samba_handlers(struct ldb_context *ldb)
333 {
334         return ldb_set_attrib_handlers(ldb, samba_handlers, ARRAY_SIZE(samba_handlers));
335 }