waf: Stop automaticaly changing dashes to underscores in library names.
[sfrench/samba-autobuild/.git] / source4 / utils / oLschema2ldif.c
index 6c4e6a9c80afd8f2a9f48ff2075fc090ead39a2f..29ed3bd38ed0e73523da0f92c9a25982bab6c629 100644 (file)
  */
 
 #include "includes.h"
-#include "ldb_includes.h"
+#include "ldb.h"
 #include "tools/cmdline.h"
 #include "dsdb/samdb/samdb.h"
+#include "../lib/crypto/sha256.h"
+#include "../librpc/gen_ndr/ndr_misc.h"
+#include "lib/cmdline/popt_common.h"
 
 #define SCHEMA_UNKNOWN 0
 #define SCHEMA_NAME 1
@@ -336,6 +339,14 @@ static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry)
         char *c, *s;
         int n;
 
+       SHA256_CTX sha256_context;
+       uint8_t digest[SHA256_DIGEST_LENGTH];
+
+       struct GUID guid;
+
+       bool isAttribute = false;
+       bool single_valued = false;
+
        ctx = talloc_new(mem_ctx);
        msg = ldb_msg_new(ctx);
 
@@ -351,6 +362,7 @@ static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry)
                if (strncmp(c, "attributetype", 13) == 0) {
                        c += 13;
                        MSG_ADD_STRING("objectClass", "attributeSchema");
+                       isAttribute = true;
                        break;
                }
                goto failed;
@@ -374,7 +386,22 @@ static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry)
        /* get attributeID */
        n = strcspn(c, " \t");
        s = talloc_strndup(msg, c, n);
-       MSG_ADD_STRING("attributeID", s);
+       if (isAttribute) {
+               MSG_ADD_STRING("attributeID", s);
+       } else {
+               MSG_ADD_STRING("governsID", s);
+       }
+
+       SHA256_Init(&sha256_context);
+       SHA256_Update(&sha256_context, (uint8_t*)s, strlen(s));
+       SHA256_Final(digest, &sha256_context);
+
+       memcpy(&guid, digest, sizeof(struct GUID));
+
+       if (dsdb_msg_add_guid(msg, &guid, "schemaIdGuid") != 0) {
+               goto failed;
+       }
+
        c += n;
        c = skip_spaces(c);     
 
@@ -416,7 +443,7 @@ static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry)
                        break;
 
                case SCHEMA_SINGLE_VALUE:
-                       MSG_ADD_STRING("isSingleValued", "TRUE");
+                       single_valued = true;
                        break;
 
                case SCHEMA_EQUALITY:
@@ -433,12 +460,23 @@ static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry)
 
                case SCHEMA_SYNTAX:
                {
-                       const struct dsdb_syntax *map = 
-                               find_syntax_map_by_standard_oid(token->value);
+                       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:
@@ -450,6 +488,12 @@ static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry)
                }
        }
 
+       if (isAttribute) {
+               MSG_ADD_STRING("isSingleValued", single_valued ? "TRUE" : "FALSE");
+       } else {
+               MSG_ADD_STRING("defaultObjectCategory", ldb_dn_get_linearized(msg->dn));
+       }
+
        talloc_steal(mem_ctx, msg);
        talloc_free(ctx);
        return msg;
@@ -540,55 +584,86 @@ static struct schema_conv process_file(FILE *in, FILE *out)
        return ret;
 }
 
+static struct options {
+       const char *basedn;
+       const char *input;
+       const char *output;
+} options;
+
+static 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
+       { NULL }
+};
+
+
 static void usage(void)
 {
-       printf("Usage: oLschema2ldif -H NONE <options>\n");
+       poptContext pc;
+       printf("Usage: oLschema2ldif <options>\n");
        printf("\nConvert OpenLDAP schema to AD-like LDIF format\n\n");
-       printf("Options:\n");
-       printf("  -I inputfile     inputfile of OpenLDAP style schema otherwise STDIN\n");
-       printf("  -O outputfile    outputfile otherwise STDOUT\n");
-       printf("  -o options       pass options like modules to activate\n");
-       printf("              e.g: -o modules:timestamps\n");
-       printf("\n");
        printf("Converts records from an openLdap formatted schema to an ldif schema\n\n");
+       pc = poptGetContext("oLschema2ldif", 0, NULL, popt_options, 
+                           POPT_CONTEXT_KEEP_FIRST);
+       poptPrintHelp(pc, stdout, 0);
        exit(1);
 }
 
+
  int main(int argc, const char **argv)
 {
        TALLOC_CTX *ctx;
        struct schema_conv ret;
-       struct ldb_cmdline *options;
        FILE *in = stdin;
        FILE *out = stdout;
+       poptContext pc;
+       int opt;
+
        ctx = talloc_new(NULL);
        ldb_ctx = ldb_init(ctx, NULL);
 
        setenv("LDB_URL", "NONE", 1);
-       options = ldb_cmdline_process(ldb_ctx, argc, argv, usage);
 
-       if (options->basedn == NULL) {
-               perror("Base DN not specified");
+       pc = poptGetContext(argv[0], argc, argv, popt_options, 
+                           POPT_CONTEXT_KEEP_FIRST);
+
+       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);
        } else {
-               basedn = ldb_dn_new(ctx, ldb_ctx, options->basedn);
+               basedn = ldb_dn_new(ctx, ldb_ctx, options.basedn);
                if ( ! ldb_dn_validate(basedn)) {
-                       perror("Malformed Base DN");
+                       printf("Malformed Base DN\n");
+                       usage();
                        exit(1);
                }
        }
 
-       if (options->input) {
-               in = fopen(options->input, "r");
+       if (options.input) {
+               in = fopen(options.input, "r");
                if (!in) {
-                       perror(options->input);
+                       perror(options.input);
+                       usage();
                        exit(1);
                }
        }
-       if (options->output) {
-               out = fopen(options->output, "w");
+       if (options.output) {
+               out = fopen(options.output, "w");
                if (!out) {
-                       perror(options->output);
+                       perror(options.output);
+                       usage();
                        exit(1);
                }
        }