ctdb: Use str_list_add_printf() in lock_helper_args()
[samba.git] / source3 / utils / net_idmap.c
index ec2b05087ee83aa5c12cc31067ada8984fba3e48..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) {
@@ -227,6 +229,7 @@ static bool net_idmap_opendb_autorid(TALLOC_CTX *mem_ctx,
        ret = true;
 
 done:
+       talloc_free(dbfile);
        return ret;
 }
 
@@ -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);
@@ -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;
 }
@@ -1305,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",
@@ -1318,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;
        }
@@ -1332,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;
        }
 
@@ -1346,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;
 }
 
 /***********************************************************