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/includes.h"
28 #include "librpc/gen_ndr/ndr_security.h"
29 #include "librpc/gen_ndr/ndr_misc.h"
30 #include "dsdb/samdb/samdb.h"
33 convert a ldif formatted objectSid to a NDR formatted blob
35 static int ldif_read_objectSid(struct ldb_context *ldb, void *mem_ctx,
36 const struct ldb_val *in, struct ldb_val *out)
40 sid = dom_sid_parse_talloc(mem_ctx, (const char *)in->data);
44 status = ndr_push_struct_blob(out, mem_ctx, sid,
45 (ndr_push_flags_fn_t)ndr_push_dom_sid);
47 if (!NT_STATUS_IS_OK(status)) {
54 convert a NDR formatted blob to a ldif formatted objectSid
56 static int ldif_write_objectSid(struct ldb_context *ldb, void *mem_ctx,
57 const struct ldb_val *in, struct ldb_val *out)
61 sid = talloc(mem_ctx, struct dom_sid);
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)) {
71 out->data = (uint8_t *)dom_sid_string(mem_ctx, sid);
73 if (out->data == NULL) {
76 out->length = strlen((const char *)out->data);
80 static BOOL ldb_comparision_objectSid_isString(const struct ldb_val *v)
82 /* see if the input if null-terninated */
83 if (v->data[v->length] != '\0') return False;
85 if (strncmp("S-", (const char *)v->data, 2) != 0) return False;
90 compare two objectSids
92 static int ldb_comparison_objectSid(struct ldb_context *ldb, void *mem_ctx,
93 const struct ldb_val *v1, const struct ldb_val *v2)
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);
101 if (ldif_read_objectSid(ldb, mem_ctx, v1, &v) != 0) {
104 ret = ldb_comparison_binary(ldb, mem_ctx, &v, v2);
109 return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
113 canonicalise a objectSid
115 static int ldb_canonicalise_objectSid(struct ldb_context *ldb, void *mem_ctx,
116 const struct ldb_val *in, struct ldb_val *out)
118 if (ldb_comparision_objectSid_isString(in)) {
119 return ldif_read_objectSid(ldb, mem_ctx, in, out);
121 return ldb_handler_copy(ldb, mem_ctx, in, out);
125 convert a ldif formatted objectGUID to a NDR formatted blob
127 static int ldif_read_objectGUID(struct ldb_context *ldb, void *mem_ctx,
128 const struct ldb_val *in, struct ldb_val *out)
133 status = GUID_from_string((const char *)in->data, &guid);
134 if (!NT_STATUS_IS_OK(status)) {
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)) {
147 convert a NDR formatted blob to a ldif formatted objectGUID
149 static int ldif_write_objectGUID(struct ldb_context *ldb, void *mem_ctx,
150 const struct ldb_val *in, struct ldb_val *out)
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)) {
159 out->data = (uint8_t *)GUID_string(mem_ctx, &guid);
160 if (out->data == NULL) {
163 out->length = strlen((const char *)out->data);
167 static BOOL ldb_comparision_objectGUID_isString(const struct ldb_val *v)
172 /* see if the input if null-terninated */
173 if (v->data[v->length] != '\0') return False;
175 status = GUID_from_string((const char *)v->data, &guid);
176 if (!NT_STATUS_IS_OK(status)) {
184 compare two objectGUIDs
186 static int ldb_comparison_objectGUID(struct ldb_context *ldb, void *mem_ctx,
187 const struct ldb_val *v1, const struct ldb_val *v2)
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);
195 if (ldif_read_objectGUID(ldb, mem_ctx, v1, &v) != 0) {
198 ret = ldb_comparison_binary(ldb, mem_ctx, &v, v2);
203 return ldb_comparison_binary(ldb, mem_ctx, v1, v2);
207 canonicalise a objectGUID
209 static int ldb_canonicalise_objectGUID(struct ldb_context *ldb, void *mem_ctx,
210 const struct ldb_val *in, struct ldb_val *out)
212 if (ldb_comparision_objectGUID_isString(in)) {
213 return ldif_read_objectGUID(ldb, mem_ctx, in, out);
215 return ldb_handler_copy(ldb, mem_ctx, in, out);
220 convert a ldif (SDDL) formatted ntSecurityDescriptor to a NDR formatted blob
222 static int ldif_read_ntSecurityDescriptor(struct ldb_context *ldb, void *mem_ctx,
223 const struct ldb_val *in, struct ldb_val *out)
225 struct security_descriptor *sd;
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);
231 sd = sddl_decode(mem_ctx, (const char *)in->data, domain_sid);
235 status = ndr_push_struct_blob(out, mem_ctx, sd,
236 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
238 if (!NT_STATUS_IS_OK(status)) {
245 convert a NDR formatted blob to a ldif formatted ntSecurityDescriptor (SDDL format)
247 static int ldif_write_ntSecurityDescriptor(struct ldb_context *ldb, void *mem_ctx,
248 const struct ldb_val *in, struct ldb_val *out)
250 struct security_descriptor *sd;
252 const struct dom_sid *domain_sid = samdb_domain_sid(ldb);
254 if (domain_sid == NULL) {
255 return ldb_handler_copy(ldb, mem_ctx, in, out);
258 sd = talloc(mem_ctx, struct security_descriptor);
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)) {
268 out->data = (uint8_t *)sddl_encode(mem_ctx, sd, domain_sid);
270 if (out->data == NULL) {
273 out->length = strlen((const char *)out->data);
277 static const struct ldb_attrib_handler samba_handlers[] = {
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
287 .attr = "securityIdentifier",
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
295 .attr = "ntSecurityDescriptor",
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
303 .attr = "objectGUID",
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
311 .attr = "invocationId",
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
319 .attr = "schemaIDGUID",
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
329 register the samba ldif handlers
331 int ldb_register_samba_handlers(struct ldb_context *ldb)
333 return ldb_set_attrib_handlers(ldb, samba_handlers, ARRAY_SIZE(samba_handlers));