r26611: Tridge didn't write this...
[samba.git] / source4 / lib / ldb / tools / ad2oLschema.c
index d04260103d6c414eeb4120fc61700aff2abfa392..dec8a5f9726056cf90c5c40d4fbd5d59ae3e9c6e 100644 (file)
@@ -10,7 +10,7 @@
    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 2 of the License, or (at your option) any later version.
+   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
@@ -18,8 +18,7 @@
    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, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
@@ -29,7 +28,7 @@
  *
  *  Description: utility to convert an AD schema into the format required by OpenLDAP
  *
- *  Author: Andrew Tridgell
+ *  Author: Andrew Bartlett
  */
 
 #include "includes.h"
@@ -37,6 +36,8 @@
 #include "system/locale.h"
 #include "tools/cmdline.h"
 #include "tools/convert.h"
+#include "param/param.h"
+#include "lib/cmdline/popt_common.h"
 
 struct schema_conv {
        int count;
@@ -195,9 +196,11 @@ static int fetch_objectclass_schema(struct ldb_context *ldb, struct ldb_dn *sche
 static struct ldb_dn *find_schema_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ctx) 
 {
        const char *rootdse_attrs[] = {"schemaNamingContext", NULL};
+       const char *no_attrs[] = { NULL };
        struct ldb_dn *schemadn;
        struct ldb_dn *basedn = ldb_dn_new(mem_ctx, ldb, NULL);
        struct ldb_result *rootdse_res;
+       struct ldb_result *schema_res;
        int ldb_ret;
        if (!basedn) {
                return NULL;
@@ -206,8 +209,25 @@ static struct ldb_dn *find_schema_dn(struct ldb_context *ldb, TALLOC_CTX *mem_ct
        /* Search for rootdse */
        ldb_ret = ldb_search(ldb, basedn, LDB_SCOPE_BASE, NULL, rootdse_attrs, &rootdse_res);
        if (ldb_ret != LDB_SUCCESS) {
-               printf("Search failed: %s\n", ldb_errstring(ldb));
-               return NULL;
+               ldb_ret = ldb_search(ldb, basedn, LDB_SCOPE_SUBTREE, 
+                                "(&(objectClass=dMD)(cn=Schema))", 
+                                no_attrs, &schema_res);
+               if (ldb_ret) {
+                       printf("cn=Schema Search failed: %s\n", ldb_errstring(ldb));
+                       return NULL;
+               }
+
+               talloc_steal(mem_ctx, schema_res);
+
+               if (schema_res->count != 1) {
+                       printf("Failed to find rootDSE");
+                       return NULL;
+               }
+               
+               schemadn = talloc_steal(mem_ctx, schema_res->msgs[0]->dn);
+               talloc_free(schema_res);
+               return schemadn;
+               
        }
        
        talloc_steal(mem_ctx, rootdse_res);
@@ -336,7 +356,7 @@ static struct schema_conv process_convert(struct ldb_context *ldb, enum convert_
                const char *description = ldb_msg_find_attr_as_string(msg, "description", NULL);
                const char *oid = ldb_msg_find_attr_as_string(msg, "attributeID", NULL);
                const char *syntax = ldb_msg_find_attr_as_string(msg, "attributeSyntax", NULL);
-               BOOL single_value = ldb_msg_find_attr_as_bool(msg, "isSingleValued", False);
+               bool single_value = ldb_msg_find_attr_as_bool(msg, "isSingleValued", false);
                const struct syntax_map *map = find_syntax_map_by_ad_oid(syntax);
                char *schema_entry = NULL;
                int j;
@@ -658,7 +678,7 @@ static struct schema_conv process_convert(struct ldb_context *ldb, enum convert_
                }
        }
 
-       target_str = lp_parm_string(-1, "convert", "target");
+       target_str = lp_parm_string(cmdline_lp_ctx, NULL, "convert", "target");
 
        if (!target_str || strcasecmp(target_str, "openldap") == 0) {
                target = TARGET_OPENLDAP;