2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2003 Andrew Bartlett (abartlet@samba.org)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "utils/net.h"
26 #include "dbwrap/dbwrap_open.h"
27 #include "../libcli/security/security.h"
28 #include "net_idmap_check.h"
31 #define ALLOC_CHECK(mem) do { \
33 d_fprintf(stderr, _("Out of memory!\n")); \
38 /***********************************************************
39 Helper function for net_idmap_dump. Dump one entry.
40 **********************************************************/
41 static int net_idmap_dump_one_entry(struct db_record *rec,
44 if (strcmp((char *)rec->key.dptr, "USER HWM") == 0) {
45 printf(_("USER HWM %d\n"), IVAL(rec->value.dptr,0));
49 if (strcmp((char *)rec->key.dptr, "GROUP HWM") == 0) {
50 printf(_("GROUP HWM %d\n"), IVAL(rec->value.dptr,0));
54 if (strncmp((char *)rec->key.dptr, "S-", 2) != 0)
57 printf("%s %s\n", rec->value.dptr, rec->key.dptr);
61 static const char* net_idmap_dbfile(struct net_context *c)
63 const char* dbfile = NULL;
65 if (c->opt_db != NULL) {
66 dbfile = talloc_strdup(talloc_tos(), c->opt_db);
68 d_fprintf(stderr, _("Out of memory!\n"));
70 } else if (strequal(lp_idmap_backend(), "tdb")) {
71 dbfile = state_path("winbindd_idmap.tdb");
73 d_fprintf(stderr, _("Out of memory!\n"));
75 } else if (strequal(lp_idmap_backend(), "tdb2")) {
76 dbfile = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
78 dbfile = talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
82 d_fprintf(stderr, _("Out of memory!\n"));
85 char* backend = talloc_strdup(talloc_tos(), lp_idmap_backend());
86 char* args = strchr(backend, ':');
91 d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
100 /***********************************************************
101 Dump the current idmap
102 **********************************************************/
103 static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
105 struct db_context *db;
110 if ( argc > 1 || c->display_usage) {
113 _("net idmap dump [[--db=]<inputfile>]\n"
114 " Dump current ID mapping.\n"
115 " inputfile\tTDB file to read mappings from.\n"));
116 return c->display_usage?0:-1;
119 mem_ctx = talloc_stackframe();
121 dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c);
122 if (dbfile == NULL) {
125 d_fprintf(stderr, _("dumping id mapping from %s\n"), dbfile);
127 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0);
129 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
130 dbfile, strerror(errno));
134 db->traverse_read(db, net_idmap_dump_one_entry, NULL);
138 talloc_free(mem_ctx);
142 /***********************************************************
143 Write entries from stdin to current local idmap
144 **********************************************************/
146 static int net_idmap_store_id_mapping(struct db_context *db,
149 const char *sid_string)
156 idstr = talloc_asprintf(talloc_tos(), "UID %lu", idval);
159 idstr = talloc_asprintf(talloc_tos(), "GID %lu", idval);
162 d_fprintf(stderr, "Invalid id mapping type: %d\n", type);
166 status = dbwrap_store_bystring(db, idstr,
167 string_term_tdb_data(sid_string),
169 if (!NT_STATUS_IS_OK(status)) {
170 d_fprintf(stderr, "Error storing ID -> SID: "
171 "%s\n", nt_errstr(status));
175 status = dbwrap_store_bystring(db, sid_string,
176 string_term_tdb_data(idstr),
178 if (!NT_STATUS_IS_OK(status)) {
179 d_fprintf(stderr, "Error storing SID -> ID: "
180 "%s\n", nt_errstr(status));
188 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
192 struct db_context *db;
193 const char *dbfile = NULL;
196 if (c->display_usage) {
199 _("net idmap restore [--db=<TDB>] [<inputfile>]\n"
200 " Restore ID mappings from file\n"
201 " TDB\tFile to store ID mappings to."
202 " inputfile\tFile to load ID mappings from. If not "
203 "given, load data from stdin.\n"));
207 mem_ctx = talloc_stackframe();
209 dbfile = net_idmap_dbfile(c);
211 if (dbfile == NULL) {
216 d_fprintf(stderr, _("restoring id mapping to %s\n"), dbfile);
219 input = fopen(argv[0], "r");
221 d_fprintf(stderr, _("Could not open input file (%s): %s\n"),
222 argv[0], strerror(errno));
230 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
232 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
233 dbfile, strerror(errno));
238 if (db->transaction_start(db) != 0) {
239 d_fprintf(stderr, _("Failed to start transaction.\n"));
244 while (!feof(input)) {
245 char line[128], sid_string[128];
249 if (fgets(line, 127, input) == NULL)
254 if ( (len > 0) && (line[len-1] == '\n') )
257 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2)
259 ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
264 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2)
266 ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
271 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
272 ret = dbwrap_store_int32(db, "USER HWM", idval);
274 d_fprintf(stderr, _("Could not store USER HWM.\n"));
277 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
278 ret = dbwrap_store_int32(db, "GROUP HWM", idval);
281 _("Could not store GROUP HWM.\n"));
285 d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
292 if(db->transaction_commit(db) != 0) {
293 d_fprintf(stderr, _("Failed to commit transaction.\n"));
297 if (db->transaction_cancel(db) != 0) {
298 d_fprintf(stderr, _("Failed to cancel transaction.\n"));
303 if ((input != NULL) && (input != stdin)) {
307 talloc_free(mem_ctx);
312 NTSTATUS dbwrap_delete_mapping(struct db_context *db, TDB_DATA key1, bool force)
314 TALLOC_CTX* mem_ctx = talloc_tos();
315 struct db_record *rec1=NULL, *rec2=NULL;
317 bool is_valid_mapping;
318 NTSTATUS status = NT_STATUS_OK;
320 rec1 = db->fetch_locked(db, mem_ctx, key1);
322 DEBUG(1, ("failed to fetch: %.*s\n", (int)key1.dsize, key1.dptr));
323 status = NT_STATUS_NO_MEMORY;
327 if (key2.dptr == NULL) {
328 DEBUG(1, ("could not find %.*s\n", (int)key1.dsize, key1.dptr));
329 status = NT_STATUS_NOT_FOUND;
333 DEBUG(2, ("mapping: %.*s -> %.*s\n",
334 (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr));
336 rec2 = db->fetch_locked(db, mem_ctx, key2);
338 DEBUG(1, ("failed to fetch: %.*s\n", (int)key2.dsize, key2.dptr));
339 status = NT_STATUS_NO_MEMORY;
343 is_valid_mapping = tdb_data_equal(key1, rec2->value);
345 if (!is_valid_mapping) {
346 DEBUG(1, ("invalid mapping: %.*s -> %.*s -> %.*s\n",
347 (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr,
348 (int)rec2->value.dsize, rec2->value.dptr ));
350 status = NT_STATUS_FILE_INVALID;
355 status = rec1->delete_rec(rec1);
356 if (!NT_STATUS_IS_OK(status)) {
357 DEBUG(1, ("failed to delete: %.*s\n", (int)key1.dsize, key1.dptr));
361 if (is_valid_mapping) {
362 status = rec2->delete_rec(rec2);
363 if (!NT_STATUS_IS_OK(status)) {
364 DEBUG(1, ("failed to delete: %.*s\n", (int)key2.dsize, key2.dptr));
374 NTSTATUS delete_mapping_action(struct db_context *db, void* data)
376 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, false);
379 NTSTATUS delete_mapping_action_force(struct db_context *db, void* data)
381 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, true);
384 /***********************************************************
385 Delete a SID mapping from a winbindd_idmap.tdb
386 **********************************************************/
387 static bool delete_args_ok(int argc, const char **argv)
391 if (strncmp(argv[0], "S-", 2) == 0)
393 if (strncmp(argv[0], "GID ", 4) == 0)
395 if (strncmp(argv[0], "UID ", 4) == 0)
400 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
403 struct db_context *db;
409 if ( !delete_args_ok(argc,argv) || c->display_usage) {
412 _("net idmap delete [-f] [--db=<TDB>] <ID>\n"
413 " Delete mapping of ID from TDB.\n"
415 " TDB\tidmap database\n"
416 " ID\tSID|GID|UID\n"));
417 return c->display_usage ? 0 : -1;
420 mem_ctx = talloc_stackframe();
422 dbfile = net_idmap_dbfile(c);
423 if (dbfile == NULL) {
426 d_fprintf(stderr, _("deleting id mapping from %s\n"), dbfile);
428 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR, 0);
430 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
431 dbfile, strerror(errno));
435 key = string_term_tdb_data(argv[0]);
437 status = dbwrap_trans_do(db, (c->opt_force
438 ? delete_mapping_action_force
439 : delete_mapping_action), &key);
441 if (!NT_STATUS_IS_OK(status)) {
442 d_fprintf(stderr, _("could not delete mapping: %s\n"),
448 talloc_free(mem_ctx);
452 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
454 d_printf("%s\n", _("Not implemented yet"));
457 static bool idmap_store_secret(const char *backend,
459 const char *identity,
466 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
468 if (r < 0) return false;
470 strupper_m(tmp); /* make sure the key is case insensitive */
471 ret = secrets_store_generic(tmp, identity, secret);
478 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
488 if (argc != 2 || c->display_usage) {
491 _("net idmap secret <DOMAIN> <secret>\n"
492 " Set the secret for the specified domain\n"
493 " DOMAIN\tDomain to set secret for.\n"
494 " secret\tNew secret to set.\n"));
495 return c->display_usage?0:-1;
500 ctx = talloc_new(NULL);
503 domain = talloc_strdup(ctx, argv[0]);
506 opt = talloc_asprintf(ctx, "idmap config %s", domain);
509 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
510 ALLOC_CHECK(backend);
512 if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
514 _("The only currently supported backend is LDAP\n"));
519 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
522 _("Missing ldap_user_dn option for domain %s\n"),
528 ret = idmap_store_secret("ldap", domain, dn, secret);
531 d_fprintf(stderr, _("Failed to store secret\n"));
536 d_printf(_("Secret stored\n"));
540 static int net_idmap_check(struct net_context *c, int argc, const char **argv)
543 struct check_options opts;
545 if ( argc > 1 || c->display_usage) {
548 _("net idmap check [-v] [-r] [-a] [-T] [-f] [-l] [[--db=]<TDB>]\n"
549 " Check an idmap database.\n"
550 " --verbose,-v\tverbose\n"
551 " --repair,-r\trepair\n"
552 " --auto,-a\tnoninteractive mode\n"
553 " --test,-T\tdry run\n"
554 " --fore,-f\tforce\n"
555 " --lock,-l\tlock db while doing the check\n"
556 " TDB\tidmap database\n"));
557 return c->display_usage ? 0 : -1;
560 dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c);
561 if (dbfile == NULL) {
564 d_fprintf(stderr, _("check database: %s\n"), dbfile);
566 opts = (struct check_options) {
567 .lock = c->opt_lock || c->opt_long_list_entries,
568 .test = c->opt_testmode,
569 .automatic = c->opt_auto,
570 .verbose = c->opt_verbose,
571 .force = c->opt_force,
572 .repair = c->opt_repair || c->opt_reboot,
575 return net_idmap_check_db(dbfile, &opts);
578 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
582 struct dom_sid src_sid, dst_sid;
584 struct db_context *db;
585 struct db_record *rec;
588 if (argc != 3 || c->display_usage) {
589 d_fprintf(stderr, "%s net idmap aclmapset <tdb> "
590 "<src-sid> <dst-sid>\n", _("Usage:"));
594 if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
595 d_fprintf(stderr, _("talloc_init failed\n"));
599 if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
600 O_RDWR|O_CREAT, 0600))) {
601 d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
605 if (!string_to_sid(&src_sid, argv[1])) {
606 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
610 if (!string_to_sid(&dst_sid, argv[2])) {
611 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
615 if (!(src = sid_string_talloc(mem_ctx, &src_sid))
616 || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
617 d_fprintf(stderr, _("talloc_strdup failed\n"));
621 if (!(rec = db->fetch_locked(
622 db, mem_ctx, string_term_tdb_data(src)))) {
623 d_fprintf(stderr, _("could not fetch db record\n"));
627 status = rec->store(rec, string_term_tdb_data(dst), 0);
630 if (!NT_STATUS_IS_OK(status)) {
631 d_fprintf(stderr, _("could not store record: %s\n"),
638 TALLOC_FREE(mem_ctx);
642 /***********************************************************
643 Look at the current idmap
644 **********************************************************/
645 int net_idmap(struct net_context *c, int argc, const char **argv)
647 struct functable func[] = {
652 N_("Dump the current ID mappings"),
653 N_("net idmap dump\n"
654 " Dump the current ID mappings")
660 N_("Restore entries from stdin"),
661 N_("net idmap restore\n"
662 " Restore entries from stdin")
668 N_("Not implemented yet"),
669 N_("net idmap setmap\n"
670 " Not implemented yet")
676 N_("Delete ID mapping"),
677 N_("net idmap delete <ID>\n"
678 " Delete ID mapping")
684 N_("Set secret for specified domain"),
685 N_("net idmap secret <DOMAIN> <secret>\n"
686 " Set secret for specified domain")
693 N_("net idmap aclmapset\n"
700 N_("Check id mappings"),
701 N_("net idmap check\n"
702 " Check id mappings")
704 {NULL, NULL, 0, NULL, NULL}
707 return net_run_function(c, argc, argv, "net idmap", func);