2 ldb database library - ldif handlers for Samba
4 Copyright (C) Andrew Tridgell 2005
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
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.
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.
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
26 #include "ldb/include/ldb.h"
27 #include "ldb/include/ldb_private.h"
28 #include "librpc/gen_ndr/ndr_misc.h"
29 #include "dsdb/samdb/samdb.h"
32 convert a ldif formatted objectSid to a NDR formatted blob
34 static int ldif_read_objectSid(struct ldb_context *ldb, void *mem_ctx,
35 const struct ldb_val *in, struct ldb_val *out)
39 sid = dom_sid_parse_talloc(mem_ctx, (const char *)in->data);
43 status = ndr_push_struct_blob(out, mem_ctx, sid,
44 (ndr_push_flags_fn_t)ndr_push_dom_sid);
46 if (!NT_STATUS_IS_OK(status)) {
53 convert a NDR formatted blob to a ldif formatted objectSid
55 static int ldif_write_objectSid(struct ldb_context *ldb, void *mem_ctx,
56 const struct ldb_val *in, struct ldb_val *out)
60 sid = talloc(mem_ctx, struct dom_sid);
64 status = ndr_pull_struct_blob(in, sid, sid,
65 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
66 if (!NT_STATUS_IS_OK(status)) {
70 out->data = (uint8_t *)dom_sid_string(mem_ctx, sid);
72 if (out->data == NULL) {
75 out->length = strlen((const char *)out->data);
79 static BOOL ldb_comparision_objectSid_isString(const struct ldb_val *v)
81 /* see if the input if null-terninated */
82 if (v->data[v->length] != '\0') return False;
84 if (strncmp("S-", (const char *)v->data, 2) != 0) return False;
89 compare two objectSids
91 static int ldb_comparison_objectSid(struct ldb_context *ldb, void *mem_ctx,
92 const struct ldb_val *v1, const struct ldb_val *v2)
94 if (ldb_comparision_objectSid_isString(v1)) {
95 if (ldb_comparision_objectSid_isString(v1)) {
96 return strcmp((const char *)v1->data, (const char *)v2->data);
100 if (ldif_read_objectSid(ldb, mem_ctx, v1, &v) != 0) {
103 ret = ldb_comparison_binary(ldb, mem_ctx, &v, v2);
108 return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
112 canonicalise a objectSid
114 static int ldb_canonicalise_objectSid(struct ldb_context *ldb, void *mem_ctx,
115 const struct ldb_val *in, struct ldb_val *out)
117 if (ldb_comparision_objectSid_isString(in)) {
118 return ldif_read_objectSid(ldb, mem_ctx, in, out);
120 return ldb_handler_copy(ldb, mem_ctx, in, out);
124 convert a ldif formatted objectGUID to a NDR formatted blob
126 static int ldif_read_objectGUID(struct ldb_context *ldb, void *mem_ctx,
127 const struct ldb_val *in, struct ldb_val *out)
132 status = GUID_from_string((const char *)in->data, &guid);
133 if (!NT_STATUS_IS_OK(status)) {
137 status = ndr_push_struct_blob(out, mem_ctx, &guid,
138 (ndr_push_flags_fn_t)ndr_push_GUID);
139 if (!NT_STATUS_IS_OK(status)) {
146 convert a NDR formatted blob to a ldif formatted objectGUID
148 static int ldif_write_objectGUID(struct ldb_context *ldb, void *mem_ctx,
149 const struct ldb_val *in, struct ldb_val *out)
153 status = ndr_pull_struct_blob(in, mem_ctx, &guid,
154 (ndr_pull_flags_fn_t)ndr_pull_GUID);
155 if (!NT_STATUS_IS_OK(status)) {
158 out->data = (uint8_t *)GUID_string(mem_ctx, &guid);
159 if (out->data == NULL) {
162 out->length = strlen((const char *)out->data);
166 static BOOL ldb_comparision_objectGUID_isString(const struct ldb_val *v)
171 /* see if the input if null-terninated */
172 if (v->data[v->length] != '\0') return False;
174 status = GUID_from_string((const char *)v->data, &guid);
175 if (!NT_STATUS_IS_OK(status)) {
183 compare two objectGUIDs
185 static int ldb_comparison_objectGUID(struct ldb_context *ldb, void *mem_ctx,
186 const struct ldb_val *v1, const struct ldb_val *v2)
188 if (ldb_comparision_objectGUID_isString(v1)) {
189 if (ldb_comparision_objectGUID_isString(v2)) {
190 return strcmp((const char *)v1->data, (const char *)v2->data);
194 if (ldif_read_objectGUID(ldb, mem_ctx, v1, &v) != 0) {
197 ret = ldb_comparison_binary(ldb, mem_ctx, &v, v2);
202 return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
206 canonicalise a objectGUID
208 static int ldb_canonicalise_objectGUID(struct ldb_context *ldb, void *mem_ctx,
209 const struct ldb_val *in, struct ldb_val *out)
211 if (ldb_comparision_objectGUID_isString(in)) {
212 return ldif_read_objectGUID(ldb, mem_ctx, in, out);
214 return ldb_handler_copy(ldb, mem_ctx, in, out);
219 convert a ldif (SDDL) formatted ntSecurityDescriptor to a NDR formatted blob
221 static int ldif_read_ntSecurityDescriptor(struct ldb_context *ldb, void *mem_ctx,
222 const struct ldb_val *in, struct ldb_val *out)
224 struct security_descriptor *sd;
226 const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
227 if (domain_sid == NULL) {
228 return ldb_handler_copy(ldb, mem_ctx, in, out);
230 sd = sddl_decode(mem_ctx, (const char *)in->data, domain_sid);
234 status = ndr_push_struct_blob(out, mem_ctx, sd,
235 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
237 if (!NT_STATUS_IS_OK(status)) {
244 convert a NDR formatted blob to a ldif formatted ntSecurityDescriptor (SDDL format)
246 static int ldif_write_ntSecurityDescriptor(struct ldb_context *ldb, void *mem_ctx,
247 const struct ldb_val *in, struct ldb_val *out)
249 struct security_descriptor *sd;
251 const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
253 if (domain_sid == NULL) {
254 return ldb_handler_copy(ldb, mem_ctx, in, out);
257 sd = talloc(mem_ctx, struct security_descriptor);
261 status = ndr_pull_struct_blob(in, sd, sd,
262 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
263 if (!NT_STATUS_IS_OK(status)) {
267 out->data = (uint8_t *)sddl_encode(mem_ctx, sd, domain_sid);
269 if (out->data == NULL) {
272 out->length = strlen((const char *)out->data);
276 static const struct ldb_attrib_handler samba_handlers[] = {
280 .ldif_read_fn = ldif_read_objectSid,
281 .ldif_write_fn = ldif_write_objectSid,
282 .canonicalise_fn = ldb_canonicalise_objectSid,
283 .comparison_fn = ldb_comparison_objectSid
286 .attr = "securityIdentifier",
288 .ldif_read_fn = ldif_read_objectSid,
289 .ldif_write_fn = ldif_write_objectSid,
290 .canonicalise_fn = ldb_canonicalise_objectSid,
291 .comparison_fn = ldb_comparison_objectSid
294 .attr = "ntSecurityDescriptor",
296 .ldif_read_fn = ldif_read_ntSecurityDescriptor,
297 .ldif_write_fn = ldif_write_ntSecurityDescriptor,
298 .canonicalise_fn = ldb_handler_copy,
299 .comparison_fn = ldb_comparison_binary
302 .attr = "objectGUID",
304 .ldif_read_fn = ldif_read_objectGUID,
305 .ldif_write_fn = ldif_write_objectGUID,
306 .canonicalise_fn = ldb_canonicalise_objectGUID,
307 .comparison_fn = ldb_comparison_objectGUID
310 .attr = "invocationId",
312 .ldif_read_fn = ldif_read_objectGUID,
313 .ldif_write_fn = ldif_write_objectGUID,
314 .canonicalise_fn = ldb_canonicalise_objectGUID,
315 .comparison_fn = ldb_comparison_objectGUID
318 .attr = "schemaIDGUID",
320 .ldif_read_fn = ldif_read_objectGUID,
321 .ldif_write_fn = ldif_write_objectGUID,
322 .canonicalise_fn = ldb_canonicalise_objectGUID,
323 .comparison_fn = ldb_comparison_objectGUID
328 register the samba ldif handlers
330 int ldb_register_samba_handlers(struct ldb_context *ldb)
332 return ldb_set_attrib_handlers(ldb, samba_handlers, ARRAY_SIZE(samba_handlers));