diff options
author | Michael Hanselmann <public@hansmi.ch> | 2019-03-23 02:57:06 +0100 |
---|---|---|
committer | Andrew Bartlett <abartlet@samba.org> | 2019-03-26 03:03:23 +0000 |
commit | 0c7c44a284a26790081c000f5b8f4ed32f9f21d7 (patch) | |
tree | fe92bb99bf9294407b7f5d992836d06ea3613de7 | |
parent | af3253013a5633b6cbd247d6feda0b3f5bc50b74 (diff) | |
download | samba-0c7c44a284a26790081c000f5b8f4ed32f9f21d7.tar.gz |
Split oLschema2ldif into library and binary
The oLschema2ldif program was contained in a single file, making reuse
of its parsing logic elsewhere impossible. With this change the majority
of the code is moved to a new file, "lib.c", while the CLI interface is
now in a "main.c" file.
End-of-line whitespace is also removed.
Signed-off-by: Michael Hanselmann <public@hansmi.ch>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
-rw-r--r-- | source4/utils/oLschema2ldif/lib.c (renamed from source4/utils/oLschema2ldif.c) | 136 | ||||
-rw-r--r-- | source4/utils/oLschema2ldif/lib.h | 45 | ||||
-rw-r--r-- | source4/utils/oLschema2ldif/main.c | 132 | ||||
-rw-r--r-- | source4/utils/oLschema2ldif/oLschema2ldif.1.xml (renamed from source4/utils/man/oLschema2ldif.1.xml) | 0 | ||||
-rw-r--r-- | source4/utils/oLschema2ldif/wscript_build | 12 | ||||
-rw-r--r-- | source4/utils/wscript_build | 8 | ||||
-rw-r--r-- | wscript_build | 2 |
7 files changed, 206 insertions, 129 deletions
diff --git a/source4/utils/oLschema2ldif.c b/source4/utils/oLschema2ldif/lib.c index c46799d737a..8c85ce85a7c 100644 --- a/source4/utils/oLschema2ldif.c +++ b/source4/utils/oLschema2ldif/lib.c @@ -1,4 +1,4 @@ -/* +/* ldb database library Copyright (C) Simo Sorce 2005 @@ -6,7 +6,7 @@ ** 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 @@ -32,11 +32,10 @@ */ #include "includes.h" +#include "./lib.h" #include "ldb.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 @@ -53,19 +52,11 @@ #define SCHEMA_SYNTAX 12 #define SCHEMA_DESC 13 -struct schema_conv { - int count; - int failures; -}; - struct schema_token { int type; char *value; }; -struct ldb_context *ldb_ctx; -struct ldb_dn *basedn; - static int check_braces(const char *string) { size_t b; @@ -335,7 +326,7 @@ static struct schema_token *get_next_schema_token(TALLOC_CTX *ctx, char **string return token; } -static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry) +static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, struct conv_options *opt, const char *entry) { TALLOC_CTX *ctx; struct ldb_message *msg; @@ -424,7 +415,7 @@ static struct ldb_message *process_entry(TALLOC_CTX *mem_ctx, const char *entry) MSG_ADD_STRING("cn", token->value); MSG_ADD_STRING("name", token->value); MSG_ADD_STRING("lDAPDisplayName", token->value); - msg->dn = ldb_dn_copy(msg, basedn); + msg->dn = ldb_dn_copy(msg, opt->basedn); ldb_dn_add_child_fmt(msg->dn, "CN=%s,CN=Schema,CN=Configuration", token->value); break; @@ -513,18 +504,17 @@ failed: return NULL; } -static struct schema_conv process_file(FILE *in, FILE *out) +struct schema_conv process_file(TALLOC_CTX *mem_ctx, struct conv_options *opt) { - TALLOC_CTX *ctx; 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; - ctx = talloc_new(NULL); - ret.count = 0; ret.failures = 0; line = 0; @@ -543,23 +533,23 @@ static struct schema_conv process_file(FILE *in, FILE *out) } t = 0; - entry = talloc_array(ctx, char, 1024); + entry = talloc_array(mem_ctx, char, 1024); if (entry == NULL) exit(-1); - do { + do { if (c == '\n') { int ret2 = 0; entry[t] = '\0'; ret2 = check_braces(entry); if (ret2 == 0) { ret.count++; - ldif.msg = process_entry(ctx, entry); + 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(ldb_ctx, out, &ldif); + ldb_ldif_write_file(opt->ldb_ctx, out, &ldif); break; } if (ret2 == 2) { @@ -573,22 +563,22 @@ static struct schema_conv process_file(FILE *in, FILE *out) t++; } if ((t % 1023) == 0) { - entry = talloc_realloc(ctx, entry, char, t + 1024); + entry = talloc_realloc(mem_ctx, entry, char, t + 1024); if (entry == NULL) exit(-1); } - } while ((c = fgetc(in)) != EOF); + } while ((c = fgetc(in)) != EOF); if (c != '\n') { entry[t] = '\0'; if (check_braces(entry) == 0) { ret.count++; - ldif.msg = process_entry(ctx, entry); + 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(ldb_ctx, out, &ldif); + ldb_ldif_write_file(opt->ldb_ctx, out, &ldif); } else { fprintf(stderr, "malformed entry on line %d\n", line); ret.failures++; @@ -600,97 +590,3 @@ 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) -{ - poptContext pc; - 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"); - 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; - 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); - - 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); - if ( ! ldb_dn_validate(basedn)) { - printf("Malformed Base DN\n"); - usage(); - exit(1); - } - } - - if (options.input) { - in = fopen(options.input, "r"); - if (!in) { - perror(options.input); - usage(); - exit(1); - } - } - if (options.output) { - out = fopen(options.output, "w"); - if (!out) { - perror(options.output); - usage(); - exit(1); - } - } - - ret = process_file(in, out); - - fclose(in); - fclose(out); - - printf("Converted %d records with %d failures\n", ret.count, ret.failures); - - return 0; -} diff --git a/source4/utils/oLschema2ldif/lib.h b/source4/utils/oLschema2ldif/lib.h new file mode 100644 index 00000000000..f271b4be938 --- /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 00000000000..10bfaad1ba2 --- /dev/null +++ b/source4/utils/oLschema2ldif/main.c @@ -0,0 +1,132 @@ +/* + 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/popt_common.h" + +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) +{ + poptContext pc; + 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"); + 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; + poptContext pc; + struct conv_options copt; + int opt; + + ctx = talloc_new(NULL); + + setenv("LDB_URL", "NONE", 1); + + 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); + } + + 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); + + return 0; +} diff --git a/source4/utils/man/oLschema2ldif.1.xml b/source4/utils/oLschema2ldif/oLschema2ldif.1.xml index 3d323b79ff7..3d323b79ff7 100644 --- a/source4/utils/man/oLschema2ldif.1.xml +++ b/source4/utils/oLschema2ldif/oLschema2ldif.1.xml diff --git a/source4/utils/oLschema2ldif/wscript_build b/source4/utils/oLschema2ldif/wscript_build new file mode 100644 index 00000000000..5e87b7a385f --- /dev/null +++ b/source4/utils/oLschema2ldif/wscript_build @@ -0,0 +1,12 @@ +#!/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 POPT_SAMBA', + ) diff --git a/source4/utils/wscript_build b/source4/utils/wscript_build deleted file mode 100644 index 166413a7ec3..00000000000 --- a/source4/utils/wscript_build +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env python - -bld.SAMBA_BINARY('oLschema2ldif', - source='oLschema2ldif.c', - manpages='man/oLschema2ldif.1', - deps='samdb POPT_SAMBA' - ) - diff --git a/wscript_build b/wscript_build index b07ae1b5df2..771eb1fa0f7 100644 --- a/wscript_build +++ b/wscript_build @@ -94,7 +94,7 @@ bld.RECURSE('source4/nbt_server') bld.RECURSE('source4/wrepl_server') bld.RECURSE('source4/cldap_server') bld.RECURSE('source4/ntp_signd') -bld.RECURSE('source4/utils') +bld.RECURSE('source4/utils/oLschema2ldif') bld.RECURSE('source4/ntvfs') bld.RECURSE('source4/torture') bld.RECURSE('librpc') |