s3:net_idmap_dump support dumping autorid backend
[metze/samba/wip.git] / source3 / utils / net_idmap.c
index 08ef920dde8d47c8b40a644dd8a0d0d140f84eeb..f8818328df6be05156fdd39baf0195fffb4243ed 100644 (file)
                return -1; \
        } } while(0)
 
+enum idmap_dump_backend {
+       TDB,
+       AUTORID
+};
+
+struct idmap_dump_ctx {
+       enum idmap_dump_backend backend;
+};
+
+static int net_idmap_dump_one_autorid_entry(struct db_record *rec,
+                                           void *unused)
+{
+       TDB_DATA key;
+       TDB_DATA value;
+
+       key = dbwrap_record_get_key(rec);
+       value = dbwrap_record_get_value(rec);
+
+       if (strncmp((char *)key.dptr, "CONFIG", 6) == 0) {
+               char *config = talloc_array(talloc_tos(), char, value.dsize+1);
+               memcpy(config, value.dptr, value.dsize);
+               config[value.dsize] = '\0';
+               printf("CONFIG: %s\n", config);
+               talloc_free(config);
+               return 0;
+       }
+
+       if (strncmp((char *)key.dptr, "NEXT RANGE", 10) == 0) {
+               printf("RANGE HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
+               return 0;
+       }
+
+       if (strncmp((char *)key.dptr, "NEXT ALLOC UID", 14) == 0) {
+               printf("UID HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
+               return 0;
+       }
+
+       if (strncmp((char *)key.dptr, "NEXT ALLOC GID", 14) == 0) {
+               printf("GID HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
+               return 0;
+       }
+
+       if (strncmp((char *)key.dptr, "UID", 3) == 0 ||
+           strncmp((char *)key.dptr, "GID", 3) == 0)
+       {
+               /* mapped entry from allocation pool */
+               printf("%s %s\n", value.dptr, key.dptr);
+               return 0;
+       }
+
+       if ((strncmp((char *)key.dptr, "S-1-5-", 6) == 0 ||
+            strncmp((char *)key.dptr, "ALLOC", 5) == 0) &&
+           value.dsize == sizeof(uint32_t))
+       {
+               /* this is a domain range assignment */
+               uint32_t range = IVAL(value.dptr, 0);
+               printf("RANGE %"PRIu32": %s\n", range, key.dptr);
+               return 0;
+       }
+
+       return 0;
+}
+
 /***********************************************************
  Helper function for net_idmap_dump. Dump one entry.
  **********************************************************/
-static int net_idmap_dump_one_entry(struct db_record *rec,
-                                   void *unused)
+static int net_idmap_dump_one_tdb_entry(struct db_record *rec,
+                                       void *unused)
 {
        TDB_DATA key;
        TDB_DATA value;
@@ -57,47 +120,61 @@ static int net_idmap_dump_one_entry(struct db_record *rec,
                return 0;
        }
 
-       if (strncmp((char *)key.dptr, "S-", 2) != 0)
+       if (strncmp((char *)key.dptr, "S-", 2) != 0) {
                return 0;
+       }
 
        printf("%s %s\n", value.dptr, key.dptr);
        return 0;
 }
 
-static const char* net_idmap_dbfile(struct net_context *c)
+static const char* net_idmap_dbfile(struct net_context *c,
+                                   struct idmap_dump_ctx *ctx)
 {
        const char* dbfile = NULL;
+       const char *backend = NULL;
+
+       /* prefer idmap config * : backend over idmap backend parameter */
+       backend = lp_parm_const_string(-1, "idmap config *", "backend", NULL);
+       if (!backend) {
+               backend = lp_idmap_backend();
+       }
 
        if (c->opt_db != NULL) {
                dbfile = talloc_strdup(talloc_tos(), c->opt_db);
                if (dbfile == NULL) {
                        d_fprintf(stderr, _("Out of memory!\n"));
                }
-       } else if (strequal(lp_idmap_backend(), "tdb")) {
+       } else if (strequal(backend, "tdb")) {
                dbfile = state_path("winbindd_idmap.tdb");
                if (dbfile == NULL) {
                        d_fprintf(stderr, _("Out of memory!\n"));
                }
-       } else if (strequal(lp_idmap_backend(), "tdb2")) {
-               dbfile = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
+               ctx->backend = TDB;
+       } else if (strequal(backend, "tdb2")) {
+               dbfile = talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
+                                        lp_private_dir());
                if (dbfile == NULL) {
-                       dbfile = talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
-                                                lp_private_dir());
+                       d_fprintf(stderr, _("Out of memory!\n"));
                }
+               ctx->backend = TDB;
+       } else if (strequal(backend, "autorid")) {
+               dbfile = state_path("autorid.tdb");
                if (dbfile == NULL) {
                        d_fprintf(stderr, _("Out of memory!\n"));
                }
+               ctx->backend = AUTORID;
        } else {
-               char* backend = talloc_strdup(talloc_tos(), lp_idmap_backend());
-               char* args = strchr(backend, ':');
+               char *_backend = talloc_strdup(talloc_tos(), backend);
+               char* args = strchr(_backend, ':');
                if (args != NULL) {
                        *args = '\0';
                }
 
                d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
-                        backend);
+                          _backend);
 
-               talloc_free(backend);
+               talloc_free(_backend);
        }
 
        return dbfile;
@@ -113,6 +190,7 @@ static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
        const char* dbfile;
        NTSTATUS status;
        int ret = -1;
+       struct idmap_dump_ctx ctx = { .backend = TDB };
 
        if ( argc > 1  || c->display_usage) {
                d_printf("%s\n%s",
@@ -125,20 +203,29 @@ static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
 
        mem_ctx = talloc_stackframe();
 
-       dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c);
+       dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c, &ctx);
        if (dbfile == NULL) {
                goto done;
        }
        d_fprintf(stderr, _("dumping id mapping from %s\n"), dbfile);
 
-       db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0);
+       db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0,
+                    DBWRAP_LOCK_ORDER_1);
        if (db == NULL) {
                d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
                          dbfile, strerror(errno));
                goto done;
        }
 
-       status = dbwrap_traverse_read(db, net_idmap_dump_one_entry, NULL, NULL);
+       if (ctx.backend == AUTORID) {
+               status = dbwrap_traverse_read(db,
+                                             net_idmap_dump_one_autorid_entry,
+                                             NULL, NULL);
+       } else {
+               status = dbwrap_traverse_read(db,
+                                             net_idmap_dump_one_tdb_entry,
+                                             NULL, NULL);
+       }
        if (!NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, _("error traversing the database\n"));
                ret = -1;
@@ -205,6 +292,7 @@ static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
        struct db_context *db;
        const char *dbfile = NULL;
        int ret = 0;
+       struct idmap_dump_ctx ctx = { .backend = TDB };
 
        if (c->display_usage) {
                d_printf("%s\n%s",
@@ -219,13 +307,20 @@ static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
 
        mem_ctx = talloc_stackframe();
 
-       dbfile = net_idmap_dbfile(c);
+       dbfile = net_idmap_dbfile(c, &ctx);
 
        if (dbfile == NULL) {
                ret = -1;
                goto done;
        }
 
+       if (ctx.backend != TDB) {
+               d_fprintf(stderr, _("Sorry, restoring of non-TDB databases is "
+                                   "currently not supported\n"));
+               ret = -1;
+               goto done;
+       }
+
        d_fprintf(stderr, _("restoring id mapping to %s\n"), dbfile);
 
        if (argc == 1) {
@@ -240,7 +335,8 @@ static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
                input = stdin;
        }
 
-       db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
+       db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644,
+                    DBWRAP_LOCK_ORDER_1);
        if (db == NULL) {
                d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
                          dbfile, strerror(errno));
@@ -258,6 +354,7 @@ static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
                char line[128], sid_string[128];
                int len;
                unsigned long idval;
+               NTSTATUS status;
 
                if (fgets(line, 127, input) == NULL)
                        break;
@@ -282,16 +379,21 @@ static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
                                break;
                        }
                } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
-                       ret = dbwrap_store_int32(db, "USER HWM", idval);
-                       if (ret != 0) {
-                               d_fprintf(stderr, _("Could not store USER HWM.\n"));
+                       status = dbwrap_store_int32_bystring(
+                               db, "USER HWM", idval);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               d_fprintf(stderr,
+                                         _("Could not store USER HWM: %s\n"),
+                                         nt_errstr(status));
                                break;
                        }
                } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
-                       ret = dbwrap_store_int32(db, "GROUP HWM", idval);
-                       if (ret != 0) {
+                       status = dbwrap_store_int32_bystring(
+                               db, "GROUP HWM", idval);
+                       if (!NT_STATUS_IS_OK(status)) {
                                d_fprintf(stderr,
-                                         _("Could not store GROUP HWM.\n"));
+                                         _("Could not store GROUP HWM: %s\n"),
+                                         nt_errstr(status));
                                break;
                        }
                } else {
@@ -420,6 +522,7 @@ static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
        TDB_DATA key;
        NTSTATUS status;
        const char* dbfile;
+       struct idmap_dump_ctx ctx = { .backend = TDB };
 
        if ( !delete_args_ok(argc,argv) || c->display_usage) {
                d_printf("%s\n%s",
@@ -434,13 +537,14 @@ static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
 
        mem_ctx = talloc_stackframe();
 
-       dbfile = net_idmap_dbfile(c);
+       dbfile = net_idmap_dbfile(c, &ctx);
        if (dbfile == NULL) {
                goto done;
        }
        d_fprintf(stderr, _("deleting id mapping from %s\n"), dbfile);
 
-       db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR, 0);
+       db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR, 0,
+                    DBWRAP_LOCK_ORDER_1);
        if (db == NULL) {
                d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
                          dbfile, strerror(errno));
@@ -482,7 +586,11 @@ static bool idmap_store_secret(const char *backend,
 
        if (r < 0) return false;
 
-       strupper_m(tmp); /* make sure the key is case insensitive */
+       /* make sure the key is case insensitive */
+       if (!strupper_m(tmp)) {
+               free(tmp);
+               return false;
+       }
        ret = secrets_store_generic(tmp, identity, secret);
 
        free(tmp);
@@ -556,6 +664,7 @@ static int net_idmap_check(struct net_context *c, int argc, const char **argv)
 {
        const char* dbfile;
        struct check_options opts;
+       struct idmap_dump_ctx ctx = { .backend = TDB };
 
        if ( argc > 1 || c->display_usage) {
                d_printf("%s\n%s",
@@ -572,10 +681,17 @@ static int net_idmap_check(struct net_context *c, int argc, const char **argv)
                return c->display_usage ? 0 : -1;
        }
 
-       dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c);
+       dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c, &ctx);
        if (dbfile == NULL) {
                return -1;
        }
+
+       if (ctx.backend != TDB) {
+               d_fprintf(stderr, _("Sorry, checking of non-TDB databases is "
+                                   "currently not supported\n"));
+               return -1;
+       }
+
        d_fprintf(stderr, _("check database: %s\n"), dbfile);
 
        opts = (struct check_options) {
@@ -612,7 +728,8 @@ static int net_idmap_aclmapset(struct net_context *c, int argc, const char **arg
        }
 
        if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
-                          O_RDWR|O_CREAT, 0600))) {
+                          O_RDWR|O_CREAT, 0600,
+                          DBWRAP_LOCK_ORDER_1))) {
                d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
                goto fail;
        }