Skip to content

Commit

Permalink
ldb: activating <= and >= indexing for integers
Browse files Browse the repository at this point in the history
Activating <= and >= mdb indexing in samba for int32 and int64 attributes by:
1. Adding index_format_fn to LDB_SYNTAX_SAMBA_INT32 in ldb_samba
2. Cloning the 64bit LDB_SYNTAX_INTEGER type as LDB_SYNTAX_ORDERED_INTEGER
3. Adding index_format_fn to the new type
4. Modifying LargeInteger use the new type in samba schema
5. Bumping the index version to trigger reindexing

Pair-programmed-with: Garming Sam <[email protected]>

Signed-off-by: Aaron Haslett <[email protected]>
Signed-off-by: Garming Sam <[email protected]>
Reviewed-by: Andrew Bartlett <[email protected]>
  • Loading branch information
Aaron Haslett authored and abartlet committed Apr 8, 2019
1 parent f775606 commit c9b2a37
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 3 deletions.
45 changes: 45 additions & 0 deletions lib/ldb-samba/ldif_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,50 @@ static int ldif_canonicalise_int32(struct ldb_context *ldb, void *mem_ctx,
return 0;
}

/* Lexicographically sorted representation for a 32-bit integer */
static int ldif_index_format_int32(struct ldb_context *ldb,
void *mem_ctx,
const struct ldb_val *in,
struct ldb_val *out)
{
int32_t i;
int ret;
char prefix;
size_t len;

ret = val_to_int32(in, &i);
if (ret != LDB_SUCCESS) {
return ret;
}

if (i < 0) {
prefix = 'n';
i = INT32_MAX + i + 1;
} else if (i > 0) {
prefix = 'p';
} else {
prefix = 'o';
}

out->data = (uint8_t *) talloc_asprintf(mem_ctx, "%c%010ld", prefix, (long)i);
if (out->data == NULL) {
ldb_oom(ldb);
return LDB_ERR_OPERATIONS_ERROR;
}

len = talloc_array_length(out->data) - 1;
if (len != 11) {
ldb_debug(ldb, LDB_DEBUG_ERROR,
__location__ ": expected index format str %s to"
" have length 11 but got %zu",
(char*)out->data, len);
return LDB_ERR_OPERATIONS_ERROR;
}

out->length = 11;
return 0;
}

/* Comparison of two 32-bit integers */
static int ldif_comparison_int32(struct ldb_context *ldb, void *mem_ctx,
const struct ldb_val *v1, const struct ldb_val *v2)
Expand Down Expand Up @@ -1427,6 +1471,7 @@ static const struct ldb_schema_syntax samba_syntaxes[] = {
.ldif_read_fn = ldb_handler_copy,
.ldif_write_fn = ldb_handler_copy,
.canonicalise_fn = ldif_canonicalise_int32,
.index_format_fn = ldif_index_format_int32,
.comparison_fn = ldif_comparison_int32,
.operator_fn = samba_syntax_operator_fn
},{
Expand Down
56 changes: 55 additions & 1 deletion lib/ldb/common/attrib_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,52 @@ static int ldb_canonicalise_Integer(struct ldb_context *ldb, void *mem_ctx,
return 0;
}

/*
* Lexicographically ordered format for a ldap Integer
*/
static int ldb_index_format_Integer(struct ldb_context *ldb,
void *mem_ctx,
const struct ldb_val *in,
struct ldb_val *out)
{
int64_t i;
int ret;
char prefix;
size_t len;

ret = val_to_int64(in, &i);
if (ret != LDB_SUCCESS) {
return ret;
}

if (i < 0) {
prefix = 'n';
i = INT64_MAX + i + 1;
} else if (i > 0) {
prefix = 'p';
} else {
prefix = 'o';
}

out->data = (uint8_t *) talloc_asprintf(mem_ctx, "%c%019lld", prefix, (long long)i);
if (out->data == NULL) {
ldb_oom(ldb);
return LDB_ERR_OPERATIONS_ERROR;
}

len = talloc_array_length(out->data) - 1;
if (len != 20) {
ldb_debug(ldb, LDB_DEBUG_ERROR,
__location__ ": expected index format str %s to"
" have length 20 but got %zu",
(char*)out->data, len);
return LDB_ERR_OPERATIONS_ERROR;
}

out->length = 20;
return 0;
}

/*
compare two Integers
*/
Expand Down Expand Up @@ -428,7 +474,15 @@ static const struct ldb_schema_syntax ldb_standard_syntaxes[] = {
.canonicalise_fn = ldb_canonicalise_Integer,
.comparison_fn = ldb_comparison_Integer
},
{
{
.name = LDB_SYNTAX_ORDERED_INTEGER,
.ldif_read_fn = ldb_handler_copy,
.ldif_write_fn = ldb_handler_copy,
.canonicalise_fn = ldb_canonicalise_Integer,
.index_format_fn = ldb_index_format_Integer,
.comparison_fn = ldb_comparison_Integer
},
{
.name = LDB_SYNTAX_OCTET_STRING,
.ldif_read_fn = ldb_handler_copy,
.ldif_write_fn = ldb_handler_copy,
Expand Down
6 changes: 6 additions & 0 deletions lib/ldb/include/ldb.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ const struct ldb_dn_extended_syntax *ldb_dn_extended_syntax_by_name(struct ldb_c
*/
#define LDB_SYNTAX_INTEGER "1.3.6.1.4.1.1466.115.121.1.27"

/**
Custom attribute syntax for an integer whose index is lexicographically
ordered by attribute value in the database.
*/
#define LDB_SYNTAX_ORDERED_INTEGER "LDB_SYNTAX_ORDERED_INTEGER"

/**
LDAP attribute syntax for a boolean
Expand Down
1 change: 1 addition & 0 deletions lib/ldb/pyldb.c
Original file line number Diff line number Diff line change
Expand Up @@ -4410,6 +4410,7 @@ static PyObject* module_init(void)
ADD_LDB_STRING(SYNTAX_DN);
ADD_LDB_STRING(SYNTAX_DIRECTORY_STRING);
ADD_LDB_STRING(SYNTAX_INTEGER);
ADD_LDB_STRING(SYNTAX_ORDERED_INTEGER);
ADD_LDB_STRING(SYNTAX_BOOLEAN);
ADD_LDB_STRING(SYNTAX_OCTET_STRING);
ADD_LDB_STRING(SYNTAX_UTC_TIME);
Expand Down
2 changes: 1 addition & 1 deletion source4/dsdb/schema/schema_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/* change this when we change something in our schema code that
* requires a re-index of the database
*/
#define SAMDB_INDEXING_VERSION "2"
#define SAMDB_INDEXING_VERSION "3"

/*
override the name to attribute handler function
Expand Down
2 changes: 1 addition & 1 deletion source4/dsdb/schema/schema_syntax.c
Original file line number Diff line number Diff line change
Expand Up @@ -2541,7 +2541,7 @@ static const struct dsdb_syntax dsdb_syntaxes[] = {
.validate_ldb = dsdb_syntax_INT64_validate_ldb,
.equality = "integerMatch",
.comment = "Large Integer",
.ldb_syntax = LDB_SYNTAX_INTEGER,
.ldb_syntax = LDB_SYNTAX_ORDERED_INTEGER,
.auto_normalise = true
},{
.name = "String(NT-Sec-Desc)",
Expand Down

0 comments on commit c9b2a37

Please sign in to comment.