net: add "net idmap get ranges" operation for autorid
authorMichael Adam <obnox@samba.org>
Wed, 18 Sep 2013 00:39:51 +0000 (02:39 +0200)
committerMichael Adam <obnox@samba.org>
Tue, 1 Oct 2013 08:49:25 +0000 (10:49 +0200)
Implemented usint the idmap_autorid_iterate_domain_ranges_read() function.
Based on earlier patch by Atul Kulkarni <atul.kulkarni@in.ibm.com>.

Signed-off-by: Michael Adam <obnox@samba.org>
source3/utils/net_idmap.c

index d699f78602e0e25d8384521d9b461e9eedb74e33..3a89197f548e9ba0c13cf33633dea9a1f1d00956 100644 (file)
@@ -1058,6 +1058,76 @@ done:
        return ret;
 }
 
+static NTSTATUS net_idmap_autorid_print_range(struct db_context *db,
+                                             const char *domsid,
+                                             uint32_t range_index,
+                                             uint32_t rangenum,
+                                             void *private_data)
+{
+       if (range_index == 0) {
+               printf("RANGE %"PRIu32": %s\n", rangenum, domsid);
+       } else {
+               printf("RANGE %"PRIu32": %s#%"PRIu32"\n", rangenum, domsid,
+                      range_index);
+       }
+
+       return NT_STATUS_OK;
+}
+
+static void net_idmap_autorid_get_ranges_usage(void)
+{
+       d_printf("%s\n%s",
+                _("Usage:"),
+                _("net idmap get ranges <SID> [--db=<inputfile>]\n"
+                  "  Get all ranges for a given domain.\n"
+                  "    SID\t\tSID of the domain\n"
+                  "    inputfile\tTDB file to add mapping to.\n"));
+}
+
+static int net_idmap_autorid_get_ranges(struct net_context *c, int argc,
+                                       const char **argv)
+{
+       int ret = -1;
+       TALLOC_CTX *mem_ctx;
+       struct db_context *db = NULL;
+       fstring domsid;
+       NTSTATUS status;
+
+       if (c->display_usage) {
+               net_idmap_autorid_get_ranges_usage();
+               return 0;
+       }
+
+       if (argc != 1) {
+               net_idmap_autorid_get_ranges_usage();
+               return -1;
+       }
+
+       fstrcpy(domsid, argv[0]);
+
+       mem_ctx = talloc_stackframe();
+       if (!net_idmap_opendb_autorid(mem_ctx, c, true, &db)) {
+               goto done;
+       }
+
+       status = idmap_autorid_iterate_domain_ranges_read(db,
+                                               domsid,
+                                               net_idmap_autorid_print_range,
+                                               NULL, /* private_data */
+                                               NULL  /* count */);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, "%s: %s\n",
+                         _("Error getting domain ranges"), nt_errstr(status));
+               goto done;
+       }
+
+       ret = 0;
+
+done:
+       talloc_free(mem_ctx);
+       return ret;
+}
+
 static int net_idmap_autorid_get_config(struct net_context *c, int argc,
                                        const char **argv)
 {
@@ -1111,6 +1181,14 @@ static int net_idmap_get(struct net_context *c, int argc, const char **argv)
                        N_("net idmap get range\n"
                           "  Get the range for a domain and range-index")
                },
+               {
+                       "ranges",
+                       net_idmap_autorid_get_ranges,
+                       NET_TRANSPORT_LOCAL,
+                       N_("Get all ranges for a domain"),
+                       N_("net idmap get ranges <SID>\n"
+                          "  Get all ranges for a domain")
+               },
                {
                        "config",
                        net_idmap_autorid_get_config,