summaryrefslogtreecommitdiffstats
path: root/source4/utils/oLschema2ldif
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 17:20:00 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 17:20:00 +0000
commit8daa83a594a2e98f39d764422bfbdbc62c9efd44 (patch)
tree4099e8021376c7d8c05bdf8503093d80e9c7bad0 /source4/utils/oLschema2ldif
parentInitial commit. (diff)
downloadsamba-8daa83a594a2e98f39d764422bfbdbc62c9efd44.tar.xz
samba-8daa83a594a2e98f39d764422bfbdbc62c9efd44.zip
Adding upstream version 2:4.20.0+dfsg.upstream/2%4.20.0+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'source4/utils/oLschema2ldif')
-rw-r--r--source4/utils/oLschema2ldif/lib.c621
-rw-r--r--source4/utils/oLschema2ldif/lib.h45
-rw-r--r--source4/utils/oLschema2ldif/main.c140
-rw-r--r--source4/utils/oLschema2ldif/oLschema2ldif.1.xml82
-rw-r--r--source4/utils/oLschema2ldif/test.c207
-rw-r--r--source4/utils/oLschema2ldif/wscript_build20
6 files changed, 1115 insertions, 0 deletions
diff --git a/source4/utils/oLschema2ldif/lib.c b/source4/utils/oLschema2ldif/lib.c
new file mode 100644
index 0000000..aefe8f9
--- /dev/null
+++ b/source4/utils/oLschema2ldif/lib.c
@@ -0,0 +1,621 @@
+/*
+ ldb database library
+
+ Copyright (C) Simo Sorce 2005
+
+ ** NOTE! The following LGPL license applies to the ldb
+ ** library. This does NOT imply that all of Samba is released
+ ** under the LGPL
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 3 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+/*
+ * Name: ldb
+ *
+ * Component: oLschema2ldif
+ *
+ * Description: utility to convert an OpenLDAP schema into AD LDIF
+ *
+ * Author: Simo Sorce
+ */
+
+#include "includes.h"
+#include "./lib.h"
+#include "ldb.h"
+#include "../librpc/gen_ndr/ndr_misc.h"
+
+#undef strcasecmp
+
+#include <gnutls/gnutls.h>
+#include <gnutls/crypto.h>
+
+#define SCHEMA_UNKNOWN 0
+#define SCHEMA_NAME 1
+#define SCHEMA_SUP 2
+#define SCHEMA_STRUCTURAL 3
+#define SCHEMA_ABSTRACT 4
+#define SCHEMA_AUXILIARY 5
+#define SCHEMA_MUST 6
+#define SCHEMA_MAY 7
+#define SCHEMA_SINGLE_VALUE 8
+#define SCHEMA_EQUALITY 9
+#define SCHEMA_ORDERING 10
+#define SCHEMA_SUBSTR 11
+#define SCHEMA_SYNTAX 12
+#define SCHEMA_DESC 13
+
+struct schema_token {
+ int type;
+ char *value;
+};
+
+static int check_braces(const char *string)
+{
+ size_t b;
+ char *c;
+
+ b = 0;
+ if ((c = strchr(string, '(')) == NULL) {
+ return -1;
+ }
+ b++;
+ c++;
+ while (b) {
+ c = strpbrk(c, "()");
+ if (c == NULL) return 1;
+ if (*c == '(') b++;
+ if (*c == ')') {
+ b--;
+ if (*(c - 1) != ' ' && c && (*(c + 1) == '\0')) {
+ return 2;
+ }
+ }
+ c++;
+ }
+ return 0;
+}
+
+static char *skip_spaces(char *string) {
+ return (string + strspn(string, " \t\n"));
+}
+
+static int add_multi_string(struct ldb_message *msg, const char *attr, char *values)
+{
+ char *c;
+ char *s;
+ int n;
+
+ c = skip_spaces(values);
+ while (*c) {
+ n = strcspn(c, " \t$");
+ s = talloc_strndup(msg, c, n);
+ if (ldb_msg_add_string(msg, attr, s) != 0) {
+ return -1;
+ }
+ c += n;
+ c += strspn(c, " \t$");
+ }
+
+ return 0;
+}
+
+#define MSG_ADD_STRING(a, v) do { if (ldb_msg_add_string(msg, a, v) != 0) goto failed; } while(0)
+#define MSG_ADD_M_STRING(a, v) do { if (add_multi_string(msg, a, v) != 0) goto failed; } while(0)
+
+static char *get_def_value(TALLOC_CTX *ctx, char **string)
+{
+ char *c = *string;
+ char *value;
+ int n;
+
+ if (*c == '\'') {
+ c++;
+ n = strcspn(c, "\'");
+ value = talloc_strndup(ctx, c, n);
+ c += n;
+ if (*c != '\0') {
+ c++; /* skip closing \' */
+ }
+ } else {
+ n = strcspn(c, " \t\n");
+ value = talloc_strndup(ctx, c, n);
+ c += n;
+ }
+ *string = c;
+
+ return value;
+}
+
+static struct schema_token *get_next_schema_token(TALLOC_CTX *ctx, char **string)
+{
+ char *c = skip_spaces(*string);
+ char *type;
+ struct schema_token *token;
+ int n;
+
+ token = talloc(ctx, struct schema_token);
+
+ n = strcspn(c, " \t\n");
+ type = talloc_strndup(token, c, n);
+ c += n;
+ c = skip_spaces(c);
+
+ if (strcasecmp("NAME", type) == 0) {
+ talloc_free(type);
+ token->type = SCHEMA_NAME;
+ /* we do not support aliases so we get only the first name given and skip others */
+ if (*c == '(') {
+ char *s = strchr(c, ')');
+ if (s == NULL) return NULL;
+ s = skip_spaces(s);
+ *string = s;
+
+ c++;
+ c = skip_spaces(c);
+ }
+
+ token->value = get_def_value(ctx, &c);
+
+ if (*string < c) { /* single name */
+ c = skip_spaces(c);
+ *string = c;
+ }
+ return token;
+ }
+ if (strcasecmp("SUP", type) == 0) {
+ talloc_free(type);
+ token->type = SCHEMA_SUP;
+
+ if (*c == '(') {
+ c++;
+ n = strcspn(c, ")");
+ token->value = talloc_strndup(ctx, c, n);
+ c += n;
+ if (*c == '\0') {
+ talloc_free(token->value);
+ return NULL;
+ }
+ c++;
+ } else {
+ token->value = get_def_value(ctx, &c);
+ }
+
+ c = skip_spaces(c);
+ *string = c;
+ return token;
+ }
+
+ if (strcasecmp("STRUCTURAL", type) == 0) {
+ talloc_free(type);
+ token->type = SCHEMA_STRUCTURAL;
+ *string = c;
+ return token;
+ }
+
+ if (strcasecmp("ABSTRACT", type) == 0) {
+ talloc_free(type);
+ token->type = SCHEMA_ABSTRACT;
+ *string = c;
+ return token;
+ }
+
+ if (strcasecmp("AUXILIARY", type) == 0) {
+ talloc_free(type);
+ token->type = SCHEMA_AUXILIARY;
+ *string = c;
+ return token;
+ }
+
+ if (strcasecmp("MUST", type) == 0) {
+ talloc_free(type);
+ token->type = SCHEMA_MUST;
+
+ if (*c == '(') {
+ c++;
+ n = strcspn(c, ")");
+ token->value = talloc_strndup(ctx, c, n);
+ c += n;
+ if (*c == '\0') {
+ talloc_free(token->value);
+ return NULL;
+ }
+ c++;
+ } else {
+ token->value = get_def_value(ctx, &c);
+ }
+
+ c = skip_spaces(c);
+ *string = c;
+ return token;
+ }
+
+ if (strcasecmp("MAY", type) == 0) {
+ talloc_free(type);
+ token->type = SCHEMA_MAY;
+
+ if (*c == '(') {
+ c++;
+ n = strcspn(c, ")");
+ token->value = talloc_strndup(ctx, c, n);
+ c += n;
+ if (*c == '\0') {
+ talloc_free(token->value);
+ return NULL;
+ }
+ c++;
+ } else {
+ token->value = get_def_value(ctx, &c);
+ }
+
+ c = skip_spaces(c);
+ *string = c;
+ return token;
+ }
+
+ if (strcasecmp("SINGLE-VALUE", type) == 0) {
+ talloc_free(type);
+ token->type = SCHEMA_SINGLE_VALUE;
+ *string = c;
+ return token;
+ }
+
+ if (strcasecmp("EQUALITY", type) == 0) {
+ talloc_free(type);
+ token->type = SCHEMA_EQUALITY;
+
+ token->value = get_def_value(ctx, &c);
+
+ c = skip_spaces(c);
+ *string = c;
+ return token;
+ }
+
+ if (strcasecmp("ORDERING", type) == 0) {
+ talloc_free(type);
+ token->type = SCHEMA_ORDERING;
+
+ token->value = get_def_value(ctx, &c);
+
+ c = skip_spaces(c);
+ *string = c;
+ return token;
+ }
+
+ if (strcasecmp("SUBSTR", type) == 0) {
+ talloc_free(type);
+ token->type = SCHEMA_SUBSTR;
+
+ token->value = get_def_value(ctx, &c);
+
+ c = skip_spaces(c);
+ *string = c;
+ return token;
+ }
+
+ if (strcasecmp("SYNTAX", type) == 0) {
+ talloc_free(type);
+ token->type = SCHEMA_SYNTAX;
+
+ token->value = get_def_value(ctx, &c);
+
+ c = skip_spaces(c);
+ *string = c;
+ return token;
+ }
+
+ if (strcasecmp("DESC", type) == 0) {
+ talloc_free(type);
+ token->type = SCHEMA_DESC;
+
+ token->value = get_def_value(ctx, &c);
+
+ c = skip_spaces(c);
+ *string = c;
+ return token;
+ }
+
+ token->type = SCHEMA_UNKNOWN;
+ token->value = type;
+ if (*c == ')') {
+ *string = c;
+ return token;
+ }
+ if (*c == '\'') {
+ c = strchr(++c, '\'');
+ if (c == NULL || *c == '\0') {
+ return NULL;
+ }
+ c++;
+ } else {
+ c += strcspn(c, " \t\n");
+ }
+ c = skip_spaces(c);
+ *string = c;
+
+ return token;
+}
+
+static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, struct conv_options *opt, const char *entry)
+{
+ TALLOC_CTX *ctx;
+ struct ldb_message *msg;
+ struct schema_token *token;
+ char *c, *s;
+ int n;
+
+ uint8_t digest[gnutls_hash_get_len(GNUTLS_DIG_SHA256)];
+ int rc;
+
+ struct GUID guid;
+
+ bool isAttribute = false;
+ bool single_valued = false;
+
+ ctx = talloc_new(mem_ctx);
+ if (ctx == NULL) {
+ return NULL;
+ }
+ msg = ldb_msg_new(ctx);
+ if (msg == NULL) {
+ goto failed;
+ }
+
+ ldb_msg_add_string(msg, "objectClass", "top");
+
+ c = talloc_strdup(ctx, entry);
+ if (!c) return NULL;
+
+ c = skip_spaces(c);
+
+ switch (*c) {
+ case 'a':
+ if (strncmp(c, "attributetype", 13) == 0) {
+ c += 13;
+ MSG_ADD_STRING("objectClass", "attributeSchema");
+ isAttribute = true;
+ break;
+ }
+ goto failed;
+ case 'o':
+ if (strncmp(c, "objectclass", 11) == 0) {
+ c += 11;
+ MSG_ADD_STRING("objectClass", "classSchema");
+ break;
+ }
+ goto failed;
+ default:
+ goto failed;
+ }
+
+ c = strchr(c, '(');
+ if (c == NULL) goto failed;
+ c++;
+
+ c = skip_spaces(c);
+
+ /* get attributeID */
+ n = strcspn(c, " \t");
+ s = talloc_strndup(msg, c, n);
+ if (isAttribute) {
+ MSG_ADD_STRING("attributeID", s);
+ } else {
+ MSG_ADD_STRING("governsID", s);
+ }
+
+ rc = gnutls_hash_fast(GNUTLS_DIG_SHA256,
+ s,
+ strlen(s),
+ digest);
+ if (rc < 0) {
+ goto failed;
+ }
+
+ memcpy(&guid, digest, sizeof(struct GUID));
+
+ if (dsdb_msg_add_guid(msg, &guid, "schemaIdGuid") != 0) {
+ goto failed;
+ }
+
+ c += n;
+ c = skip_spaces(c);
+
+ while (*c != ')') {
+ token = get_next_schema_token(msg, &c);
+ if (!token) goto failed;
+
+ switch (token->type) {
+ case SCHEMA_NAME:
+ MSG_ADD_STRING("cn", token->value);
+ MSG_ADD_STRING("name", token->value);
+ MSG_ADD_STRING("lDAPDisplayName", token->value);
+ msg->dn = ldb_dn_copy(msg, opt->basedn);
+ ldb_dn_add_child_fmt(msg->dn, "CN=%s,CN=Schema,CN=Configuration", token->value);
+ break;
+
+ case SCHEMA_SUP:
+ MSG_ADD_M_STRING("subClassOf", token->value);
+ break;
+
+ case SCHEMA_STRUCTURAL:
+ MSG_ADD_STRING("objectClassCategory", "1");
+ break;
+
+ case SCHEMA_ABSTRACT:
+ MSG_ADD_STRING("objectClassCategory", "2");
+ break;
+
+ case SCHEMA_AUXILIARY:
+ MSG_ADD_STRING("objectClassCategory", "3");
+ break;
+
+ case SCHEMA_MUST:
+ MSG_ADD_M_STRING("mustContain", token->value);
+ break;
+
+ case SCHEMA_MAY:
+ MSG_ADD_M_STRING("mayContain", token->value);
+ break;
+
+ case SCHEMA_SINGLE_VALUE:
+ single_valued = true;
+ break;
+
+ case SCHEMA_EQUALITY:
+ /* TODO */
+ break;
+
+ case SCHEMA_ORDERING:
+ /* TODO */
+ break;
+
+ case SCHEMA_SUBSTR:
+ /* TODO */
+ break;
+
+ case SCHEMA_SYNTAX:
+ {
+ char *syntax_oid;
+ const struct dsdb_syntax *map;
+ char *oMSyntax;
+
+ n = strcspn(token->value, "{");
+ syntax_oid = talloc_strndup(ctx, token->value, n);
+
+ map = find_syntax_map_by_standard_oid(syntax_oid);
+ if (!map) {
+ break;
+ }
+
+ MSG_ADD_STRING("attributeSyntax", map->attributeSyntax_oid);
+
+ oMSyntax = talloc_asprintf(msg, "%d", map->oMSyntax);
+ MSG_ADD_STRING("oMSyntax", oMSyntax);
+
+ break;
+ }
+ case SCHEMA_DESC:
+ MSG_ADD_STRING("description", token->value);
+ break;
+
+ default:
+ fprintf(stderr, "Unknown Definition: %s\n", token->value);
+ goto failed;
+ }
+ }
+
+ if (isAttribute) {
+ MSG_ADD_STRING("isSingleValued", single_valued ? "TRUE" : "FALSE");
+ } else {
+ if (msg->dn == NULL) {
+ goto failed;
+ }
+ MSG_ADD_STRING("defaultObjectCategory", ldb_dn_get_linearized(msg->dn));
+ }
+
+ talloc_steal(mem_ctx, msg);
+ talloc_free(ctx);
+ return msg;
+
+failed:
+ talloc_free(ctx);
+ return NULL;
+}
+
+struct schema_conv process_file(TALLOC_CTX *mem_ctx, struct conv_options *opt)
+{
+ struct schema_conv ret;
+ char *entry;
+ int c, t, line;
+ struct ldb_ldif ldif;
+ FILE *in = opt->in;
+ FILE *out = opt->out;
+
+ ldif.changetype = LDB_CHANGETYPE_NONE;
+
+ ret.count = 0;
+ ret.failures = 0;
+ line = 0;
+
+ while ((c = fgetc(in)) != EOF) {
+ line++;
+ /* fprintf(stderr, "Parsing line %d\n", line); */
+ if (c == '#') {
+ do {
+ c = fgetc(in);
+ } while (c != EOF && c != '\n');
+ continue;
+ }
+ if (c == '\n') {
+ continue;
+ }
+
+ t = 0;
+ entry = talloc_array(mem_ctx, char, 1024);
+ if (entry == NULL) exit(-1);
+
+ do {
+ if (c == '\n') {
+ int ret2 = 0;
+ entry[t] = '\0';
+ ret2 = check_braces(entry);
+ if (ret2 == 0) {
+ ret.count++;
+ ldif.msg = process_entry(mem_ctx, opt, entry);
+ if (ldif.msg == NULL) {
+ ret.failures++;
+ fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
+ break;
+ }
+ ldb_ldif_write_file(opt->ldb_ctx, out, &ldif);
+ break;
+ }
+ if (ret2 == 2) {
+ fprintf(stderr, "Invalid entry %s, closing braces need to be preceded by a space\n", entry);
+ ret.failures++;
+ break;
+ }
+ line++;
+ } else {
+ entry[t] = c;
+ t++;
+ }
+ if ((t % 1023) == 0) {
+ entry = talloc_realloc(mem_ctx, entry, char, t + 1024);
+ if (entry == NULL) exit(-1);
+ }
+ } while ((c = fgetc(in)) != EOF);
+
+ if (c != '\n') {
+ entry[t] = '\0';
+ if (check_braces(entry) == 0) {
+ ret.count++;
+ ldif.msg = process_entry(mem_ctx, opt, entry);
+ if (ldif.msg == NULL) {
+ ret.failures++;
+ fprintf(stderr, "No valid msg from entry \n[%s]\n at line %d\n", entry, line);
+ break;
+ }
+ ldb_ldif_write_file(opt->ldb_ctx, out, &ldif);
+ } else {
+ fprintf(stderr, "malformed entry on line %d\n", line);
+ ret.failures++;
+ }
+ }
+
+ if (c == EOF) break;
+ }
+
+ return ret;
+}
diff --git a/source4/utils/oLschema2ldif/lib.h b/source4/utils/oLschema2ldif/lib.h
new file mode 100644
index 0000000..f271b4b
--- /dev/null
+++ b/source4/utils/oLschema2ldif/lib.h
@@ -0,0 +1,45 @@
+/*
+ ldb database library
+
+ Copyright (C) Simo Sorce 2005
+
+ ** NOTE! The following LGPL license applies to the ldb
+ ** library. This does NOT imply that all of Samba is released
+ ** under the LGPL
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 3 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _OLSCHEMA2LDIF_LIB_H
+#define _OLSCHEMA2LDIF_LIB_H
+
+#include "includes.h"
+#include "ldb.h"
+#include "dsdb/samdb/samdb.h"
+
+struct schema_conv {
+ int count;
+ int failures;
+};
+
+struct conv_options {
+ struct ldb_context *ldb_ctx;
+ struct ldb_dn *basedn;
+ FILE *in;
+ FILE *out;
+};
+
+struct schema_conv process_file(TALLOC_CTX *mem_ctx, struct conv_options *opt);
+
+#endif /* _OLSCHEMA2LDIF_LIB_H */
diff --git a/source4/utils/oLschema2ldif/main.c b/source4/utils/oLschema2ldif/main.c
new file mode 100644
index 0000000..c312298
--- /dev/null
+++ b/source4/utils/oLschema2ldif/main.c
@@ -0,0 +1,140 @@
+/*
+ ldb database library
+
+ Copyright (C) Simo Sorce 2005
+
+ ** NOTE! The following LGPL license applies to the ldb
+ ** library. This does NOT imply that all of Samba is released
+ ** under the LGPL
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 3 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+/*
+ * Name: ldb
+ *
+ * Component: oLschema2ldif
+ *
+ * Description: utility to convert an OpenLDAP schema into AD LDIF
+ *
+ * Author: Simo Sorce
+ */
+
+#include "includes.h"
+#include "./lib.h"
+#include "lib/cmdline/cmdline.h"
+
+static struct options {
+ const char *basedn;
+ const char *input;
+ const char *output;
+} options;
+
+static void usage(void)
+{
+ printf("Usage: oLschema2ldif [OPTIONS]\n");
+ printf("\nConvert OpenLDAP schema to AD-like LDIF format\n\n");
+ printf("Converts records from an openLdap formatted schema to an ldif schema\n\n");
+ exit(1);
+}
+
+
+ int main(int argc, const char **argv)
+{
+ TALLOC_CTX *ctx;
+ struct schema_conv ret;
+ poptContext pc;
+ struct conv_options copt;
+ int opt;
+
+ struct poptOption popt_options[] = {
+ POPT_AUTOHELP
+ { "basedn", 'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
+ { "input", 'I', POPT_ARG_STRING, &options.input, 0,
+ "inputfile of OpenLDAP style schema otherwise STDIN", "inputfile"},
+ { "output", 'O', POPT_ARG_STRING, &options.output, 0,
+ "outputfile otherwise STDOUT", "outputfile"},
+ POPT_COMMON_VERSION
+ POPT_TABLEEND
+ };
+
+ ctx = talloc_new(NULL);
+ if (ctx == NULL) {
+ exit(ENOMEM);
+ }
+
+ setenv("LDB_URL", "NONE", 1);
+
+ pc = samba_popt_get_context(getprogname(),
+ argc,
+ argv,
+ popt_options,
+ POPT_CONTEXT_KEEP_FIRST);
+ if (pc == NULL) {
+ DBG_ERR("Failed to setup popt context!\n");
+ TALLOC_FREE(ctx);
+ exit(1);
+ }
+
+ while((opt = poptGetNextOpt(pc)) != -1) {
+ fprintf(stderr, "Invalid option %s: %s\n",
+ poptBadOption(pc, 0), poptStrerror(opt));
+ usage();
+ }
+
+ if (options.basedn == NULL) {
+ printf("Base DN not specified\n");
+ usage();
+ exit(1);
+ }
+
+ copt.in = stdin;
+ copt.out = stdout;
+ copt.ldb_ctx = ldb_init(ctx, NULL);
+
+ copt.basedn = ldb_dn_new(ctx, copt.ldb_ctx, options.basedn);
+ if (!ldb_dn_validate(copt.basedn)) {
+ printf("Malformed Base DN\n");
+ usage();
+ exit(1);
+ }
+
+ if (options.input) {
+ copt.in = fopen(options.input, "r");
+ if (!copt.in) {
+ perror(options.input);
+ usage();
+ exit(1);
+ }
+ }
+ if (options.output) {
+ copt.out = fopen(options.output, "w");
+ if (!copt.out) {
+ perror(options.output);
+ usage();
+ exit(1);
+ }
+ }
+
+ ret = process_file(ctx, &copt);
+
+ fclose(copt.in);
+ fclose(copt.out);
+
+ printf("Converted %d records with %d failures\n", ret.count, ret.failures);
+
+ poptFreeContext(pc);
+
+ return 0;
+}
diff --git a/source4/utils/oLschema2ldif/oLschema2ldif.1.xml b/source4/utils/oLschema2ldif/oLschema2ldif.1.xml
new file mode 100644
index 0000000..3d323b7
--- /dev/null
+++ b/source4/utils/oLschema2ldif/oLschema2ldif.1.xml
@@ -0,0 +1,82 @@
+<?xml version="1.0" encoding="iso-8859-1"?>
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
+<refentry id="oLschema2ldif.1">
+
+<refmeta>
+ <refentrytitle>oLschema2ldif</refentrytitle>
+ <manvolnum>1</manvolnum>
+ <refmiscinfo class="source">Samba</refmiscinfo>
+ <refmiscinfo class="manual">System Administration tools</refmiscinfo>
+ <refmiscinfo class="version">4.0</refmiscinfo>
+</refmeta>
+
+
+<refnamediv>
+ <refname>oLschema2ldif</refname>
+ <refpurpose>Converts LDAP schema's to LDB-compatible LDIF</refpurpose>
+</refnamediv>
+
+<refsynopsisdiv>
+ <cmdsynopsis>
+ <command>oLschema2ldif</command>
+ <arg choice="opt">-I INPUT-FILE</arg>
+ <arg choice="opt">-O OUTPUT-FILE</arg>
+ </cmdsynopsis>
+</refsynopsisdiv>
+
+<refsect1>
+ <title>DESCRIPTION</title>
+
+ <para>oLschema2ldif is a simple tool that converts standard OpenLDAP schema files to a LDIF format that is understood by LDB.</para>
+</refsect1>
+
+
+<refsect1>
+ <title>OPTIONS</title>
+
+ <variablelist>
+ <varlistentry>
+ <term>-I input-file</term>
+ <listitem><para>OpenLDAP schema to read. If none are specified,
+the schema file will be read from standard input.
+ </para></listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term>-O output-file</term>
+ <listitem><para>File to write ldif version of schema to.
+ </para></listitem>
+ </varlistentry>
+ </variablelist>
+</refsect1>
+
+<refsect1>
+ <title>VERSION</title>
+
+ <para>This man page is correct for version 4.0 of the Samba suite.</para>
+</refsect1>
+
+<refsect1>
+ <title>SEE ALSO</title>
+
+ <para>ldb(7), ldbmodify, ldbdel, ldif(5)</para>
+
+</refsect1>
+
+<refsect1>
+ <title>AUTHOR</title>
+
+ <para> ldb was written by
+ <ulink url="https://www.samba.org/~tridge/">Andrew Tridgell</ulink>.
+ oLschema2ldif was written by <ulink url="mailto:idra@samba.org">Simo Sorce</ulink>.
+ </para>
+
+ <para>
+If you wish to report a problem or make a suggestion then please see
+the <ulink url="http://ldb.samba.org/"/> web site for
+current contact and maintainer information.
+ </para>
+
+</refsect1>
+
+</refentry>
diff --git a/source4/utils/oLschema2ldif/test.c b/source4/utils/oLschema2ldif/test.c
new file mode 100644
index 0000000..3834ea4
--- /dev/null
+++ b/source4/utils/oLschema2ldif/test.c
@@ -0,0 +1,207 @@
+/*
+ * Unix SMB/CIFS implementation.
+ *
+ * Copyright (C) 2019 Michael Hanselmann <public@hansmi.ch>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include "includes.h"
+#include "./lib.h"
+
+struct test_ctx {
+};
+
+static int setup_context(void **state)
+{
+ struct test_ctx *test_ctx;
+
+ test_ctx = talloc_zero(NULL, struct test_ctx);
+ assert_non_null(test_ctx);
+
+ *state = test_ctx;
+
+ return 0;
+}
+
+static int teardown_context(void **state)
+{
+ struct test_ctx *test_ctx =
+ talloc_get_type_abort(*state, struct test_ctx);
+
+ talloc_free(test_ctx);
+
+ return 0;
+}
+
+static struct schema_conv process_data_blob(void **state, DATA_BLOB input)
+{
+ struct test_ctx *test_ctx =
+ talloc_get_type_abort(*state, struct test_ctx);
+ struct conv_options opt;
+ struct schema_conv ret;
+
+ assert_non_null(test_ctx);
+ assert_non_null(input.data);
+
+ opt.in = fmemopen(input.data, input.length, "r");
+ opt.out = fopen("/dev/null", "w");
+ opt.ldb_ctx = ldb_init(test_ctx, NULL);
+
+ assert_non_null(opt.in);
+ assert_non_null(opt.out);
+ assert_non_null(opt.ldb_ctx);
+
+ opt.basedn = ldb_dn_new(test_ctx, opt.ldb_ctx, "");
+
+ assert_non_null(opt.basedn);
+
+ ret = process_file(test_ctx, &opt);
+
+ fclose(opt.in);
+ fclose(opt.out);
+
+ return ret;
+}
+
+static void test_unknown_syntax_oid(void **state)
+{
+ struct schema_conv ret;
+
+ ret = process_data_blob(state, data_blob_string_const(
+ "attributetype ( 999.555.999.555.999\n"
+ "NAME 'mailLocalAddress'\n"
+ "DESC 'RFC822 email address of this recipient'\n"
+ "EQUALITY caseIgnoreIA5Match\n"
+ "SYNTAX 999.555.999.555.999{256} )\n"
+ ));
+
+ assert_int_equal(ret.count, 1);
+ assert_int_equal(ret.failures, 1);
+}
+
+static void test_unterminated_token_value(void **state)
+{
+ struct schema_conv ret;
+
+ ret = process_data_blob(state, data_blob_string_const(
+ "attributetype ( 2.16.840.1.113730.3.1.47\n"
+ "\tNAME 'mailRoutingAX 1.3.6.1.4.1.1466.115.121.1.26{256}\n"
+ "\tSI GLE-VALUE )\n"
+ ));
+
+ assert_int_equal(ret.count, 1);
+ assert_int_equal(ret.failures, 1);
+}
+
+static void test_unterminated_must_value(void **state)
+{
+ struct schema_conv ret;
+
+ ret = process_data_blob(state, data_blob_string_const(
+ "attributetype ( 1\n"
+ "\tSYNTAX 1./)# MUST ( foobar $\n"
+ ));
+
+ assert_int_equal(ret.count, 1);
+ assert_int_equal(ret.failures, 1);
+}
+
+static void test_unterminated_may_value(void **state)
+{
+ struct schema_conv ret;
+
+ ret = process_data_blob(state, data_blob_string_const(
+ "attributetype ( 1\n"
+ "\tSYNTAX 1.3.6.1.4.1.1466.115.121.1./)# MAY ( javaClassNames $\n"
+ ));
+
+ assert_int_equal(ret.count, 1);
+ assert_int_equal(ret.failures, 1);
+}
+
+static void test_unterminated_sup_value(void **state)
+{
+ struct schema_conv ret;
+
+ ret = process_data_blob(state, data_blob_string_const(
+ "attributetype ( 1\n"
+ "\tSYNTAX 1./)# SUP ( foobar $\n"
+ ));
+
+ assert_int_equal(ret.count, 1);
+ assert_int_equal(ret.failures, 1);
+}
+
+static void test_unknown_token(void **state)
+{
+ struct schema_conv ret;
+
+ ret = process_data_blob(state, data_blob_string_const(
+ "attributetype ( 1\n"
+ "\tFOOBAR 123\n"
+ " )\n"
+ ));
+
+ assert_int_equal(ret.count, 1);
+ assert_int_equal(ret.failures, 1);
+}
+
+static void test_missing_name(void **state)
+{
+ struct schema_conv ret;
+
+ ret = process_data_blob(state, data_blob_string_const(
+ "objectclass ( 1.3.6.3.6.1.4.1.1466.115.121.1.26{256} )"
+ ));
+
+ assert_int_equal(ret.count, 1);
+ assert_int_equal(ret.failures, 1);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test_setup_teardown(test_unknown_syntax_oid,
+ setup_context,
+ teardown_context),
+ cmocka_unit_test_setup_teardown(test_unterminated_token_value,
+ setup_context,
+ teardown_context),
+ cmocka_unit_test_setup_teardown(test_unterminated_must_value,
+ setup_context,
+ teardown_context),
+ cmocka_unit_test_setup_teardown(test_unterminated_may_value,
+ setup_context,
+ teardown_context),
+ cmocka_unit_test_setup_teardown(test_unterminated_sup_value,
+ setup_context,
+ teardown_context),
+ cmocka_unit_test_setup_teardown(test_unknown_token,
+ setup_context,
+ teardown_context),
+ cmocka_unit_test_setup_teardown(test_missing_name,
+ setup_context,
+ teardown_context),
+ };
+
+ cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/source4/utils/oLschema2ldif/wscript_build b/source4/utils/oLschema2ldif/wscript_build
new file mode 100644
index 0000000..bb39bae
--- /dev/null
+++ b/source4/utils/oLschema2ldif/wscript_build
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+
+bld.SAMBA_SUBSYSTEM('oLschema2ldif-lib',
+ source='lib.c',
+ deps='samdb',
+ )
+
+bld.SAMBA_BINARY('oLschema2ldif',
+ source='main.c',
+ manpages='oLschema2ldif.1',
+ deps='oLschema2ldif-lib cmdline',
+ )
+
+bld.SAMBA_BINARY('test_oLschema2ldif',
+ source='test.c',
+ deps='cmocka oLschema2ldif-lib',
+ local_include=False,
+ enabled=bld.CONFIG_SET('HAVE_FMEMOPEN'),
+ install=False,
+ )