auth/credentials: don't ignore "client use kerberos" and --use-kerberos for machine...
[samba.git] / source3 / utils / net_idmap.c
index de2d50975bb6d317104928844a50d8e1b60a4861..027b74131ddab16bc7f18957d08c4cf3ae1c883b 100644 (file)
@@ -28,6 +28,7 @@
 #include "net_idmap_check.h"
 #include "util_tdb.h"
 #include "idmap_autorid_tdb.h"
+#include "lib/util/smb_strtox.h"
 
 #define ALLOC_CHECK(mem) do { \
        if (!mem) { \
@@ -129,10 +130,11 @@ static int net_idmap_dump_one_tdb_entry(struct db_record *rec,
        return 0;
 }
 
-static const char* net_idmap_dbfile(struct net_context *c,
-                                   struct net_idmap_ctx *ctx)
+/* returns db path for idmap backend alloced on talloc_tos */
+static char *net_idmap_dbfile(struct net_context *c,
+                             struct net_idmap_ctx *ctx)
 {
-       const char* dbfile = NULL;
+       char *dbfile = NULL;
        const char *backend = NULL;
 
        backend = lp_idmap_default_backend();
@@ -147,7 +149,7 @@ static const char* net_idmap_dbfile(struct net_context *c,
                        d_fprintf(stderr, _("Out of memory!\n"));
                }
        } else if (strequal(backend, "tdb")) {
-               dbfile = state_path("winbindd_idmap.tdb");
+               dbfile = state_path(talloc_tos(), "winbindd_idmap.tdb");
                if (dbfile == NULL) {
                        d_fprintf(stderr, _("Out of memory!\n"));
                }
@@ -160,7 +162,7 @@ static const char* net_idmap_dbfile(struct net_context *c,
                }
                ctx->backend = TDB;
        } else if (strequal(backend, "autorid")) {
-               dbfile = state_path("autorid.tdb");
+               dbfile = state_path(talloc_tos(), "autorid.tdb");
                if (dbfile == NULL) {
                        d_fprintf(stderr, _("Out of memory!\n"));
                }
@@ -187,7 +189,7 @@ static bool net_idmap_opendb_autorid(TALLOC_CTX *mem_ctx,
                                     struct db_context **db)
 {
        bool ret = false;
-       const char *dbfile;
+       char *dbfile = NULL;
        struct net_idmap_ctx ctx = { .backend = AUTORID };
 
        if (c == NULL) {
@@ -206,7 +208,7 @@ static bool net_idmap_opendb_autorid(TALLOC_CTX *mem_ctx,
 
        if (readonly) {
                *db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0,
-                            DBWRAP_LOCK_ORDER_1);
+                            DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
                if (*db == NULL) {
                        d_fprintf(stderr,
                                  _("Could not open autorid db (%s): %s\n"),
@@ -227,6 +229,7 @@ static bool net_idmap_opendb_autorid(TALLOC_CTX *mem_ctx,
        ret = true;
 
 done:
+       talloc_free(dbfile);
        return ret;
 }
 
@@ -261,7 +264,7 @@ static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
        d_fprintf(stderr, _("dumping id mapping from %s\n"), dbfile);
 
        db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0,
-                    DBWRAP_LOCK_ORDER_1);
+                    DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
        if (db == NULL) {
                d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
                          dbfile, strerror(errno));
@@ -387,7 +390,7 @@ static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
        }
 
        db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644,
-                    DBWRAP_LOCK_ORDER_1);
+                    DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
        if (db == NULL) {
                d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
                          dbfile, strerror(errno));
@@ -415,14 +418,14 @@ static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
                if ( (len > 0) && (line[len-1] == '\n') )
                        line[len-1] = '\0';
 
-               if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2)
+               if (sscanf(line, "GID %lu %127s", &idval, sid_string) == 2)
                {
                        ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
                                                         idval, sid_string);
                        if (ret != 0) {
                                break;
                        }
-               } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2)
+               } else if (sscanf(line, "UID %lu %127s", &idval, sid_string) == 2)
                {
                        ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
                                                         idval, sid_string);
@@ -598,7 +601,7 @@ static int net_idmap_delete_mapping(struct net_context *c, int argc,
        d_fprintf(stderr, _("deleting id mapping from %s\n"), dbfile);
 
        db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR, 0,
-                    DBWRAP_LOCK_ORDER_1);
+                    DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
        if (db == NULL) {
                d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
                          dbfile, strerror(errno));
@@ -625,23 +628,13 @@ done:
 static bool parse_uint32(const char *str, uint32_t *result)
 {
        unsigned long val;
-       char *endptr;
+       int error = 0;
 
-       val = strtoul(str, &endptr, 10);
-
-       if (str == endptr) {
-               return false;
-       }
-       if (*endptr != '\0') {
-               return false;
-       }
-       if ((val == ULONG_MAX) && (errno == ERANGE)) {
-               return false;
-       }
-       if ((val & UINT32_MAX) != val) {
-               /* overflow */
+       val = smb_strtoul(str, NULL, 10, &error, SMB_STR_FULL_STR_CONV);
+       if (error != 0) {
                return false;
        }
+
        *result = val;          /* Potential crop */
        return true;
 }
@@ -706,6 +699,7 @@ static int net_idmap_autorid_delete_range(struct net_context *c, int argc,
        }
 
        domsid = argv[0];
+       range_index = 0;
 
        if (argc == 2) {
                ok = parse_uint32(argv[1], &range_index);
@@ -737,7 +731,7 @@ static void net_idmap_autorid_delete_ranges_usage(void)
 {
        d_printf("%s\n%s",
                 _("Usage:"),
-                _("net idmap delete ranges [-f] [--db=<TDB>] <SID>)\n"
+                _("net idmap delete ranges [-f] [--db=<TDB>] <SID>\n"
                   "  Delete all domain range mappings for a given domain.\n"
                   "    -f\tforce\n"
                   "    TDB\tidmap database\n"
@@ -814,7 +808,7 @@ static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
                        "ranges",
                        net_idmap_autorid_delete_ranges,
                        NET_TRANSPORT_LOCAL,
-                       N_("Delete all domain range mapping for a given "
+                       N_("Delete all domain range mappings for a given "
                           "domain"),
                        N_("net idmap delete ranges <SID>\n"
                           "  Delete a domain range mapping")
@@ -1304,9 +1298,10 @@ static int net_idmap_get(struct net_context *c, int argc, const char **argv)
 
 static int net_idmap_check(struct net_context *c, int argc, const char **argv)
 {
-       const char* dbfile;
+       char *dbfile;
        struct check_options opts;
        struct net_idmap_ctx ctx = { .backend = TDB };
+       int ret;
 
        if ( argc > 1 || c->display_usage) {
                d_printf("%s\n%s",
@@ -1317,13 +1312,17 @@ static int net_idmap_check(struct net_context *c, int argc, const char **argv)
                           "    --repair,-r\trepair\n"
                           "    --auto,-a\tnoninteractive mode\n"
                           "    --test,-T\tdry run\n"
-                          "    --fore,-f\tforce\n"
+                          "    --force,-f\tforce\n"
                           "    --lock,-l\tlock db while doing the check\n"
                           "    TDB\tidmap database\n"));
                return c->display_usage ? 0 : -1;
        }
 
-       dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c, &ctx);
+       if (argc > 0) {
+               dbfile = talloc_strdup(talloc_tos(), argv[0]);
+       } else {
+               dbfile = net_idmap_dbfile(c, &ctx);
+       }
        if (dbfile == NULL) {
                return -1;
        }
@@ -1331,6 +1330,7 @@ static int net_idmap_check(struct net_context *c, int argc, const char **argv)
        if (ctx.backend != TDB) {
                d_fprintf(stderr, _("Sorry, checking of non-TDB databases is "
                                    "currently not supported\n"));
+               talloc_free(dbfile);
                return -1;
        }
 
@@ -1345,7 +1345,9 @@ static int net_idmap_check(struct net_context *c, int argc, const char **argv)
                .repair = c->opt_repair || c->opt_reboot,
        };
 
-       return net_idmap_check_db(dbfile, &opts);
+       ret = net_idmap_check_db(dbfile, &opts);
+       talloc_free(dbfile);
+       return ret;
 }
 
 /***********************************************************
@@ -1394,14 +1396,6 @@ int net_idmap(struct net_context *c, int argc, const char **argv)
                        N_("net idmap delete\n"
                           "  Delete entries from the ID mapping database")
                },
-               {
-                       "secret",
-                       net_idmap_secret,
-                       NET_TRANSPORT_LOCAL,
-                       N_("Set secret for specified domain"),
-                       N_("net idmap secret <DOMAIN> <secret>\n"
-                          "  Set secret for specified domain")
-               },
                {
                        "check",
                        net_idmap_check,