along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#define FOO(x) (x)
#include "includes.h"
+#include "system/filesys.h"
#include "utils/net.h"
#include "secrets.h"
#include "idmap.h"
-#include "dbwrap.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_open.h"
#include "../libcli/security/security.h"
+#include "net_idmap_check.h"
+#include "util_tdb.h"
#define ALLOC_CHECK(mem) do { \
if (!mem) { \
static int net_idmap_dump_one_entry(struct db_record *rec,
void *unused)
{
- if (strcmp((char *)rec->key.dptr, "USER HWM") == 0) {
- printf(_("USER HWM %d\n"), IVAL(rec->value.dptr,0));
+ TDB_DATA key;
+ TDB_DATA value;
+
+ key = dbwrap_record_get_key(rec);
+ value = dbwrap_record_get_value(rec);
+
+ if (strcmp((char *)key.dptr, "USER HWM") == 0) {
+ printf(_("USER HWM %d\n"), IVAL(value.dptr,0));
return 0;
}
- if (strcmp((char *)rec->key.dptr, "GROUP HWM") == 0) {
- printf(_("GROUP HWM %d\n"), IVAL(rec->value.dptr,0));
+ if (strcmp((char *)key.dptr, "GROUP HWM") == 0) {
+ printf(_("GROUP HWM %d\n"), IVAL(value.dptr,0));
return 0;
}
- if (strncmp((char *)rec->key.dptr, "S-", 2) != 0)
+ if (strncmp((char *)key.dptr, "S-", 2) != 0)
return 0;
- printf("%s %s\n", rec->value.dptr, rec->key.dptr);
+ printf("%s %s\n", value.dptr, key.dptr);
return 0;
}
+static const char* net_idmap_dbfile(struct net_context *c)
+{
+ const char* dbfile = NULL;
+
+ 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")) {
+ 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);
+ if (dbfile == NULL) {
+ dbfile = talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
+ lp_private_dir());
+ }
+ if (dbfile == NULL) {
+ d_fprintf(stderr, _("Out of memory!\n"));
+ }
+ } else {
+ char* backend = talloc_strdup(talloc_tos(), lp_idmap_backend());
+ char* args = strchr(backend, ':');
+ if (args != NULL) {
+ *args = '\0';
+ }
+
+ d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
+ backend);
+
+ talloc_free(backend);
+ }
+
+ return dbfile;
+}
+
/***********************************************************
Dump the current idmap
**********************************************************/
{
struct db_context *db;
TALLOC_CTX *mem_ctx;
+ const char* dbfile;
+ NTSTATUS status;
+ int ret = -1;
- if ( argc != 1 || c->display_usage) {
+ if ( argc > 1 || c->display_usage) {
d_printf("%s\n%s",
_("Usage:"),
- _("net idmap dump <inputfile>\n"
+ _("net idmap dump [[--db=]<inputfile>]\n"
" Dump current ID mapping.\n"
" inputfile\tTDB file to read mappings from.\n"));
return c->display_usage?0:-1;
mem_ctx = talloc_stackframe();
- db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
+ dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c);
+ 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,
+ DBWRAP_LOCK_ORDER_1);
if (db == NULL) {
d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
- argv[0], strerror(errno));
- talloc_free(mem_ctx);
- return -1;
+ dbfile, strerror(errno));
+ goto done;
}
- db->traverse_read(db, net_idmap_dump_one_entry, NULL);
+ status = dbwrap_traverse_read(db, net_idmap_dump_one_entry, NULL, NULL);
+ if (!NT_STATUS_IS_OK(status)) {
+ d_fprintf(stderr, _("error traversing the database\n"));
+ ret = -1;
+ goto done;
+ }
- talloc_free(mem_ctx);
+ ret = 0;
- return 0;
+done:
+ talloc_free(mem_ctx);
+ return ret;
}
/***********************************************************
TALLOC_CTX *mem_ctx;
FILE *input = NULL;
struct db_context *db;
- char *dbfile = NULL;
+ const char *dbfile = NULL;
int ret = 0;
if (c->display_usage) {
d_printf("%s\n%s",
_("Usage:"),
- _("net idmap restore [<inputfile>]\n"
+ _("net idmap restore [--db=<TDB>] [<inputfile>]\n"
" Restore ID mappings from file\n"
+ " TDB\tFile to store ID mappings to."
" inputfile\tFile to load ID mappings from. If not "
"given, load data from stdin.\n"));
return 0;
mem_ctx = talloc_stackframe();
- if (strequal(lp_idmap_backend(), "tdb")) {
- dbfile = state_path("winbindd_idmap.tdb");
- if (dbfile == NULL) {
- d_fprintf(stderr, _("Out of memory!\n"));
- return -1;
- }
- } else if (strequal(lp_idmap_backend(), "tdb2")) {
- dbfile = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
- if (dbfile == NULL) {
- dbfile = talloc_asprintf(mem_ctx, "%s/idmap2.tdb",
- lp_private_dir());
- }
- } else {
- char *backend, *args;
-
- backend = talloc_strdup(mem_ctx, lp_idmap_backend());
- args = strchr(backend, ':');
- if (args != NULL) {
- *args = '\0';
- }
-
- d_printf(_("Sorry, 'net idmap restore' is currently not "
- "supported for idmap backend = %s.\n"
- "Only tdb and tdb2 are supported.\n"),
- backend);
+ dbfile = net_idmap_dbfile(c);
+ if (dbfile == NULL) {
ret = -1;
goto done;
}
- d_fprintf(stderr, _("restoring id mapping to %s data base in '%s'\n"),
- lp_idmap_backend(), dbfile);
+ d_fprintf(stderr, _("restoring id mapping to %s\n"), dbfile);
if (argc == 1) {
input = fopen(argv[0], "r");
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));
goto done;
}
- if (db->transaction_start(db) != 0) {
+ if (dbwrap_transaction_start(db) != 0) {
d_fprintf(stderr, _("Failed to start transaction.\n"));
ret = -1;
goto done;
char line[128], sid_string[128];
int len;
unsigned long idval;
+ NTSTATUS status;
if (fgets(line, 127, input) == NULL)
break;
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 {
}
if (ret == 0) {
- if(db->transaction_commit(db) != 0) {
+ if(dbwrap_transaction_commit(db) != 0) {
d_fprintf(stderr, _("Failed to commit transaction.\n"));
ret = -1;
}
} else {
- if (db->transaction_cancel(db) != 0) {
+ if (dbwrap_transaction_cancel(db) != 0) {
d_fprintf(stderr, _("Failed to cancel transaction.\n"));
}
}
return ret;
}
+static
+NTSTATUS dbwrap_delete_mapping(struct db_context *db, TDB_DATA key1, bool force)
+{
+ TALLOC_CTX* mem_ctx = talloc_tos();
+ struct db_record *rec1=NULL, *rec2=NULL;
+ TDB_DATA key2;
+ bool is_valid_mapping;
+ NTSTATUS status = NT_STATUS_OK;
+ TDB_DATA value;
+
+ rec1 = dbwrap_fetch_locked(db, mem_ctx, key1);
+ if (rec1 == NULL) {
+ DEBUG(1, ("failed to fetch: %.*s\n", (int)key1.dsize, key1.dptr));
+ status = NT_STATUS_NO_MEMORY;
+ goto done;
+ }
+ key2 = dbwrap_record_get_value(rec1);
+ if (key2.dptr == NULL) {
+ DEBUG(1, ("could not find %.*s\n", (int)key1.dsize, key1.dptr));
+ status = NT_STATUS_NOT_FOUND;
+ goto done;
+ }
+
+ DEBUG(2, ("mapping: %.*s -> %.*s\n",
+ (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr));
+
+ rec2 = dbwrap_fetch_locked(db, mem_ctx, key2);
+ if (rec2 == NULL) {
+ DEBUG(1, ("failed to fetch: %.*s\n", (int)key2.dsize, key2.dptr));
+ status = NT_STATUS_NO_MEMORY;
+ goto done;
+ }
+
+ value = dbwrap_record_get_value(rec2);
+ is_valid_mapping = tdb_data_equal(key1, value);
+
+ if (!is_valid_mapping) {
+ DEBUG(1, ("invalid mapping: %.*s -> %.*s -> %.*s\n",
+ (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr,
+ (int)value.dsize, value.dptr ));
+ if ( !force ) {
+ status = NT_STATUS_FILE_INVALID;
+ goto done;
+ }
+ }
+
+ status = dbwrap_record_delete(rec1);
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(1, ("failed to delete: %.*s\n", (int)key1.dsize, key1.dptr));
+ goto done;
+ }
+
+ if (is_valid_mapping) {
+ status = dbwrap_record_delete(rec2);
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(1, ("failed to delete: %.*s\n", (int)key2.dsize, key2.dptr));
+ }
+ }
+done:
+ TALLOC_FREE(rec1);
+ TALLOC_FREE(rec2);
+ return status;
+}
+
+static
+NTSTATUS delete_mapping_action(struct db_context *db, void* data)
+{
+ return dbwrap_delete_mapping(db, *(TDB_DATA*)data, false);
+}
+static
+NTSTATUS delete_mapping_action_force(struct db_context *db, void* data)
+{
+ return dbwrap_delete_mapping(db, *(TDB_DATA*)data, true);
+}
+
/***********************************************************
Delete a SID mapping from a winbindd_idmap.tdb
**********************************************************/
+static bool delete_args_ok(int argc, const char **argv)
+{
+ if (argc != 1)
+ return false;
+ if (strncmp(argv[0], "S-", 2) == 0)
+ return true;
+ if (strncmp(argv[0], "GID ", 4) == 0)
+ return true;
+ if (strncmp(argv[0], "UID ", 4) == 0)
+ return true;
+ return false;
+}
+
static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
{
- d_printf("%s\n", _("Not implemented yet"));
- return -1;
+ int ret = -1;
+ struct db_context *db;
+ TALLOC_CTX *mem_ctx;
+ TDB_DATA key;
+ NTSTATUS status;
+ const char* dbfile;
+
+ if ( !delete_args_ok(argc,argv) || c->display_usage) {
+ d_printf("%s\n%s",
+ _("Usage:"),
+ _("net idmap delete [-f] [--db=<TDB>] <ID>\n"
+ " Delete mapping of ID from TDB.\n"
+ " -f\tforce\n"
+ " TDB\tidmap database\n"
+ " ID\tSID|GID|UID\n"));
+ return c->display_usage ? 0 : -1;
+ }
+
+ mem_ctx = talloc_stackframe();
+
+ dbfile = net_idmap_dbfile(c);
+ 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,
+ DBWRAP_LOCK_ORDER_1);
+ if (db == NULL) {
+ d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
+ dbfile, strerror(errno));
+ goto done;
+ }
+
+ key = string_term_tdb_data(argv[0]);
+
+ status = dbwrap_trans_do(db, (c->opt_force
+ ? delete_mapping_action_force
+ : delete_mapping_action), &key);
+
+ if (!NT_STATUS_IS_OK(status)) {
+ d_fprintf(stderr, _("could not delete mapping: %s\n"),
+ nt_errstr(status));
+ goto done;
+ }
+ ret = 0;
+done:
+ talloc_free(mem_ctx);
+ return ret;
}
static int net_idmap_set(struct net_context *c, int argc, const char **argv)
return 0;
}
+static int net_idmap_check(struct net_context *c, int argc, const char **argv)
+{
+ const char* dbfile;
+ struct check_options opts;
+
+ if ( argc > 1 || c->display_usage) {
+ d_printf("%s\n%s",
+ _("Usage:"),
+ _("net idmap check [-v] [-r] [-a] [-T] [-f] [-l] [[--db=]<TDB>]\n"
+ " Check an idmap database.\n"
+ " --verbose,-v\tverbose\n"
+ " --repair,-r\trepair\n"
+ " --auto,-a\tnoninteractive mode\n"
+ " --test,-T\tdry run\n"
+ " --fore,-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);
+ if (dbfile == NULL) {
+ return -1;
+ }
+ d_fprintf(stderr, _("check database: %s\n"), dbfile);
+
+ opts = (struct check_options) {
+ .lock = c->opt_lock || c->opt_long_list_entries,
+ .test = c->opt_testmode,
+ .automatic = c->opt_auto,
+ .verbose = c->opt_verbose,
+ .force = c->opt_force,
+ .repair = c->opt_repair || c->opt_reboot,
+ };
+
+ return net_idmap_check_db(dbfile, &opts);
+}
+
static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
{
TALLOC_CTX *mem_ctx;
}
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;
}
goto fail;
}
- if (!(rec = db->fetch_locked(
+ if (!(rec = dbwrap_fetch_locked(
db, mem_ctx, string_term_tdb_data(src)))) {
d_fprintf(stderr, _("could not fetch db record\n"));
goto fail;
}
- status = rec->store(rec, string_term_tdb_data(dst), 0);
+ status = dbwrap_record_store(rec, string_term_tdb_data(dst), 0);
TALLOC_FREE(rec);
if (!NT_STATUS_IS_OK(status)) {
"delete",
net_idmap_delete,
NET_TRANSPORT_LOCAL,
- N_("Not implemented yet"),
- N_("net idmap delete\n"
- " Not implemented yet")
+ N_("Delete ID mapping"),
+ N_("net idmap delete <ID>\n"
+ " Delete ID mapping")
},
{
"secret",
N_("net idmap aclmapset\n"
" Set acl map")
},
+ {
+ "check",
+ net_idmap_check,
+ NET_TRANSPORT_LOCAL,
+ N_("Check id mappings"),
+ N_("net idmap check\n"
+ " Check id mappings")
+ },
{NULL, NULL, 0, NULL, NULL}
};