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"
25 #include "dbwrap/dbwrap.h"
26 #include "dbwrap/dbwrap_open.h"
27 #include "../libcli/security/security.h"
28 #include "net_idmap_check.h"
30 #include "idmap_autorid_tdb.h"
31 #include "lib/util/smb_strtox.h"
33 #define ALLOC_CHECK(mem) do { \
35 d_fprintf(stderr, _("Out of memory!\n")); \
40 enum idmap_dump_backend {
45 struct net_idmap_ctx {
46 enum idmap_dump_backend backend;
49 static int net_idmap_dump_one_autorid_entry(struct db_record *rec,
55 key = dbwrap_record_get_key(rec);
56 value = dbwrap_record_get_value(rec);
58 if (strncmp((char *)key.dptr, "CONFIG", 6) == 0) {
59 char *config = talloc_array(talloc_tos(), char, value.dsize+1);
60 memcpy(config, value.dptr, value.dsize);
61 config[value.dsize] = '\0';
62 printf("CONFIG: %s\n", config);
67 if (strncmp((char *)key.dptr, "NEXT RANGE", 10) == 0) {
68 printf("RANGE HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
72 if (strncmp((char *)key.dptr, "NEXT ALLOC UID", 14) == 0) {
73 printf("UID HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
77 if (strncmp((char *)key.dptr, "NEXT ALLOC GID", 14) == 0) {
78 printf("GID HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
82 if (strncmp((char *)key.dptr, "UID", 3) == 0 ||
83 strncmp((char *)key.dptr, "GID", 3) == 0)
85 /* mapped entry from allocation pool */
86 printf("%s %s\n", value.dptr, key.dptr);
90 if ((strncmp((char *)key.dptr, "S-1-5-", 6) == 0 ||
91 strncmp((char *)key.dptr, "ALLOC", 5) == 0) &&
92 value.dsize == sizeof(uint32_t))
94 /* this is a domain range assignment */
95 uint32_t range = IVAL(value.dptr, 0);
96 printf("RANGE %"PRIu32": %s\n", range, key.dptr);
103 /***********************************************************
104 Helper function for net_idmap_dump. Dump one entry.
105 **********************************************************/
106 static int net_idmap_dump_one_tdb_entry(struct db_record *rec,
112 key = dbwrap_record_get_key(rec);
113 value = dbwrap_record_get_value(rec);
115 if (strcmp((char *)key.dptr, "USER HWM") == 0) {
116 printf(_("USER HWM %d\n"), IVAL(value.dptr,0));
120 if (strcmp((char *)key.dptr, "GROUP HWM") == 0) {
121 printf(_("GROUP HWM %d\n"), IVAL(value.dptr,0));
125 if (strncmp((char *)key.dptr, "S-", 2) != 0) {
129 printf("%s %s\n", value.dptr, key.dptr);
133 /* returns db path for idmap backend alloced on talloc_tos */
134 static char *net_idmap_dbfile(struct net_context *c,
135 struct net_idmap_ctx *ctx)
138 const char *backend = NULL;
140 backend = lp_idmap_default_backend();
142 d_printf(_("Internal error: 'idmap config * : backend' is not set!\n"));
146 if (c->opt_db != NULL) {
147 dbfile = talloc_strdup(talloc_tos(), c->opt_db);
148 if (dbfile == NULL) {
149 d_fprintf(stderr, _("Out of memory!\n"));
151 } else if (strequal(backend, "tdb")) {
152 dbfile = state_path(talloc_tos(), "winbindd_idmap.tdb");
153 if (dbfile == NULL) {
154 d_fprintf(stderr, _("Out of memory!\n"));
157 } else if (strequal(backend, "tdb2")) {
158 dbfile = talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
160 if (dbfile == NULL) {
161 d_fprintf(stderr, _("Out of memory!\n"));
164 } else if (strequal(backend, "autorid")) {
165 dbfile = state_path(talloc_tos(), "autorid.tdb");
166 if (dbfile == NULL) {
167 d_fprintf(stderr, _("Out of memory!\n"));
169 ctx->backend = AUTORID;
171 char *_backend = talloc_strdup(talloc_tos(), backend);
172 char* args = strchr(_backend, ':');
177 d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
180 talloc_free(_backend);
186 static bool net_idmap_opendb_autorid(TALLOC_CTX *mem_ctx,
187 struct net_context *c,
189 struct db_context **db)
193 struct net_idmap_ctx ctx = { .backend = AUTORID };
199 dbfile = net_idmap_dbfile(c, &ctx);
200 if (dbfile == NULL) {
204 if (ctx.backend != AUTORID) {
205 d_fprintf(stderr, _("Unsupported backend\n"));
210 *db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0,
211 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
214 _("Could not open autorid db (%s): %s\n"),
215 dbfile, strerror(errno));
220 status = idmap_autorid_db_init(dbfile, mem_ctx, db);
221 if (!NT_STATUS_IS_OK(status)) {
223 _("Error calling idmap_autorid_db_init: %s\n"),
237 /***********************************************************
238 Dump the current idmap
239 **********************************************************/
240 static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
242 struct db_context *db;
247 struct net_idmap_ctx ctx = { .backend = TDB };
249 if ( argc > 1 || c->display_usage) {
252 _("net idmap dump [[--db=]<inputfile>]\n"
253 " Dump current ID mapping.\n"
254 " inputfile\tTDB file to read mappings from.\n"));
255 return c->display_usage?0:-1;
258 mem_ctx = talloc_stackframe();
260 dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c, &ctx);
261 if (dbfile == NULL) {
264 d_fprintf(stderr, _("dumping id mapping from %s\n"), dbfile);
266 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0,
267 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
269 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
270 dbfile, strerror(errno));
274 if (ctx.backend == AUTORID) {
275 status = dbwrap_traverse_read(db,
276 net_idmap_dump_one_autorid_entry,
279 status = dbwrap_traverse_read(db,
280 net_idmap_dump_one_tdb_entry,
283 if (!NT_STATUS_IS_OK(status)) {
284 d_fprintf(stderr, _("error traversing the database\n"));
292 talloc_free(mem_ctx);
296 /***********************************************************
297 Write entries from stdin to current local idmap
298 **********************************************************/
300 static int net_idmap_store_id_mapping(struct db_context *db,
303 const char *sid_string)
310 idstr = talloc_asprintf(talloc_tos(), "UID %lu", idval);
313 idstr = talloc_asprintf(talloc_tos(), "GID %lu", idval);
316 d_fprintf(stderr, "Invalid id mapping type: %d\n", type);
320 status = dbwrap_store_bystring(db, idstr,
321 string_term_tdb_data(sid_string),
323 if (!NT_STATUS_IS_OK(status)) {
324 d_fprintf(stderr, "Error storing ID -> SID: "
325 "%s\n", nt_errstr(status));
329 status = dbwrap_store_bystring(db, sid_string,
330 string_term_tdb_data(idstr),
332 if (!NT_STATUS_IS_OK(status)) {
333 d_fprintf(stderr, "Error storing SID -> ID: "
334 "%s\n", nt_errstr(status));
342 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
346 struct db_context *db;
347 const char *dbfile = NULL;
349 struct net_idmap_ctx ctx = { .backend = TDB };
351 if (c->display_usage) {
354 _("net idmap restore [--db=<TDB>] [<inputfile>]\n"
355 " Restore ID mappings from file\n"
356 " TDB\tFile to store ID mappings to."
357 " inputfile\tFile to load ID mappings from. If not "
358 "given, load data from stdin.\n"));
362 mem_ctx = talloc_stackframe();
364 dbfile = net_idmap_dbfile(c, &ctx);
366 if (dbfile == NULL) {
371 if (ctx.backend != TDB) {
372 d_fprintf(stderr, _("Sorry, restoring of non-TDB databases is "
373 "currently not supported\n"));
378 d_fprintf(stderr, _("restoring id mapping to %s\n"), dbfile);
381 input = fopen(argv[0], "r");
383 d_fprintf(stderr, _("Could not open input file (%s): %s\n"),
384 argv[0], strerror(errno));
392 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644,
393 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
395 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
396 dbfile, strerror(errno));
401 if (dbwrap_transaction_start(db) != 0) {
402 d_fprintf(stderr, _("Failed to start transaction.\n"));
407 while (!feof(input)) {
408 char line[128], sid_string[128];
413 if (fgets(line, 127, input) == NULL)
418 if ( (len > 0) && (line[len-1] == '\n') )
421 if (sscanf(line, "GID %lu %127s", &idval, sid_string) == 2)
423 ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
428 } else if (sscanf(line, "UID %lu %127s", &idval, sid_string) == 2)
430 ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
435 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
436 status = dbwrap_store_int32_bystring(
437 db, "USER HWM", idval);
438 if (!NT_STATUS_IS_OK(status)) {
440 _("Could not store USER HWM: %s\n"),
444 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
445 status = dbwrap_store_int32_bystring(
446 db, "GROUP HWM", idval);
447 if (!NT_STATUS_IS_OK(status)) {
449 _("Could not store GROUP HWM: %s\n"),
454 d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
461 if(dbwrap_transaction_commit(db) != 0) {
462 d_fprintf(stderr, _("Failed to commit transaction.\n"));
466 if (dbwrap_transaction_cancel(db) != 0) {
467 d_fprintf(stderr, _("Failed to cancel transaction.\n"));
472 if ((input != NULL) && (input != stdin)) {
476 talloc_free(mem_ctx);
481 NTSTATUS dbwrap_delete_mapping(struct db_context *db, TDB_DATA key1, bool force)
483 TALLOC_CTX *mem_ctx = talloc_stackframe();
484 bool is_valid_mapping;
485 NTSTATUS status = NT_STATUS_OK;
491 status = dbwrap_fetch(db, mem_ctx, key1, &val1);
492 if (!NT_STATUS_IS_OK(status)) {
493 DEBUG(1, ("failed to fetch: %.*s\n", (int)key1.dsize, key1.dptr));
497 if (val1.dptr == NULL) {
498 DEBUG(1, ("invalid mapping: %.*s -> empty value\n",
499 (int)key1.dsize, key1.dptr));
500 status = NT_STATUS_FILE_INVALID;
504 DEBUG(2, ("mapping: %.*s -> %.*s\n",
505 (int)key1.dsize, key1.dptr, (int)val1.dsize, val1.dptr));
507 status = dbwrap_fetch(db, mem_ctx, val1, &val2);
508 if (!NT_STATUS_IS_OK(status)) {
509 DEBUG(1, ("failed to fetch: %.*s\n", (int)val1.dsize, val1.dptr));
513 is_valid_mapping = tdb_data_equal(key1, val2);
515 if (!is_valid_mapping) {
516 DEBUG(1, ("invalid mapping: %.*s -> %.*s -> %.*s\n",
517 (int)key1.dsize, key1.dptr,
518 (int)val1.dsize, val1.dptr,
519 (int)val2.dsize, val2.dptr));
521 status = NT_STATUS_FILE_INVALID;
526 status = dbwrap_delete(db, key1);
527 if (!NT_STATUS_IS_OK(status)) {
528 DEBUG(1, ("failed to delete: %.*s\n", (int)key1.dsize, key1.dptr));
532 if (!is_valid_mapping) {
536 status = dbwrap_delete(db, val1);
537 if (!NT_STATUS_IS_OK(status)) {
538 DEBUG(1, ("failed to delete: %.*s\n", (int)val1.dsize, val1.dptr));
542 talloc_free(mem_ctx);
547 NTSTATUS delete_mapping_action(struct db_context *db, void* data)
549 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, false);
552 NTSTATUS delete_mapping_action_force(struct db_context *db, void* data)
554 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, true);
557 /***********************************************************
558 Delete a SID mapping from a winbindd_idmap.tdb
559 **********************************************************/
560 static bool delete_args_ok(int argc, const char **argv)
564 if (strncmp(argv[0], "S-", 2) == 0)
566 if (strncmp(argv[0], "GID ", 4) == 0)
568 if (strncmp(argv[0], "UID ", 4) == 0)
573 static int net_idmap_delete_mapping(struct net_context *c, int argc,
577 struct db_context *db;
582 struct net_idmap_ctx ctx = { .backend = TDB };
584 if ( !delete_args_ok(argc,argv) || c->display_usage) {
587 _("net idmap delete mapping [-f] [--db=<TDB>] <ID>\n"
588 " Delete mapping of ID from TDB.\n"
590 " TDB\tidmap database\n"
591 " ID\tSID|GID|UID\n"));
592 return c->display_usage ? 0 : -1;
595 mem_ctx = talloc_stackframe();
597 dbfile = net_idmap_dbfile(c, &ctx);
598 if (dbfile == NULL) {
601 d_fprintf(stderr, _("deleting id mapping from %s\n"), dbfile);
603 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR, 0,
604 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
606 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
607 dbfile, strerror(errno));
611 key = string_term_tdb_data(argv[0]);
613 status = dbwrap_trans_do(db, (c->opt_force
614 ? delete_mapping_action_force
615 : delete_mapping_action), &key);
617 if (!NT_STATUS_IS_OK(status)) {
618 d_fprintf(stderr, _("could not delete mapping: %s\n"),
624 talloc_free(mem_ctx);
628 static bool parse_uint32(const char *str, uint32_t *result)
633 val = smb_strtoul(str, NULL, 10, &error, SMB_STR_FULL_STR_CONV);
638 *result = val; /* Potential crop */
642 static void net_idmap_autorid_delete_range_usage(void)
646 _("net idmap delete range [-f] [--db=<TDB>] <RANGE>|(<SID>[ <INDEX>])\n"
647 " Delete a domain range mapping from the database.\n"
649 " TDB\tidmap database\n"
650 " RANGE\tthe range number to delete\n"
651 " SID\t\tSID of the domain\n"
652 " INDEX\trange index number do delete for the domain\n"));
655 static int net_idmap_autorid_delete_range(struct net_context *c, int argc,
659 struct db_context *db = NULL;
662 uint32_t range_index;
664 TALLOC_CTX *mem_ctx = NULL;
666 bool force = (c->opt_force != 0);
668 if (c->display_usage) {
669 net_idmap_autorid_delete_range_usage();
673 if (argc < 1 || argc > 2) {
674 net_idmap_autorid_delete_range_usage();
678 mem_ctx = talloc_stackframe();
679 if (!net_idmap_opendb_autorid(mem_ctx, c, false, &db)) {
683 ok = parse_uint32(argv[0], &rangenum);
685 d_printf("%s: %"PRIu32"\n", _("Deleting range number"),
688 status = idmap_autorid_delete_range_by_num(db, rangenum,
690 if (!NT_STATUS_IS_OK(status)) {
691 d_fprintf(stderr, "%s: %s\n",
692 _("Failed to delete domain range mapping"),
705 ok = parse_uint32(argv[1], &range_index);
708 _("Invalid index specification"), argv[1]);
709 net_idmap_autorid_delete_range_usage();
714 status = idmap_autorid_delete_range_by_sid(db, domsid, range_index,
716 if (!NT_STATUS_IS_OK(status)) {
717 d_fprintf(stderr, "%s: %s\n",
718 _("Failed to delete domain range mapping"),
726 talloc_free(mem_ctx);
730 static void net_idmap_autorid_delete_ranges_usage(void)
734 _("net idmap delete ranges [-f] [--db=<TDB>] <SID>\n"
735 " Delete all domain range mappings for a given domain.\n"
737 " TDB\tidmap database\n"
738 " SID\t\tSID of the domain\n"));
741 static int net_idmap_autorid_delete_ranges(struct net_context *c, int argc,
745 struct db_context *db = NULL;
748 TALLOC_CTX *mem_ctx = NULL;
749 bool force = (c->opt_force != 0);
752 if (c->display_usage) {
753 net_idmap_autorid_delete_ranges_usage();
758 net_idmap_autorid_delete_ranges_usage();
764 mem_ctx = talloc_stackframe();
765 if (!net_idmap_opendb_autorid(mem_ctx, c, false, &db)) {
769 status = idmap_autorid_delete_domain_ranges(db, domsid, force, &count);
770 if (!NT_STATUS_IS_OK(status)) {
771 d_fprintf(stderr, "%s %s: %s\n",
772 _("Failed to delete domain range mappings for "
779 d_printf(_("deleted %d domain mappings\n"), count);
784 talloc_free(mem_ctx);
788 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
790 struct functable func[] = {
793 net_idmap_delete_mapping,
795 N_("Delete ID mapping"),
796 N_("net idmap delete mapping <ID>\n"
797 " Delete ID mapping")
801 net_idmap_autorid_delete_range,
803 N_("Delete a domain range mapping"),
804 N_("net idmap delete range <RANGE>|(<SID>[ <INDEX>])\n"
805 " Delete a domain range mapping")
809 net_idmap_autorid_delete_ranges,
811 N_("Delete all domain range mappings for a given "
813 N_("net idmap delete ranges <SID>\n"
814 " Delete a domain range mapping")
816 {NULL, NULL, 0, NULL, NULL}
819 return net_run_function(c, argc, argv, "net idmap delete", func);
823 static int net_idmap_set_mapping(struct net_context *c,
824 int argc, const char **argv)
826 d_printf("%s\n", _("Not implemented yet"));
830 static void net_idmap_autorid_set_range_usage(void)
834 _("net idmap set range"
835 " <range> <SID> [<index>] [--db=<inputfile>]\n"
836 " Store a domain-range mapping for a given domain.\n"
837 " range\tRange number to be set for the domain\n"
838 " SID\t\tSID of the domain\n"
839 " index\trange-index number to be set for the domain\n"
840 " inputfile\tTDB file to add mapping to.\n"));
843 static int net_idmap_autorid_set_range(struct net_context *c,
844 int argc, const char **argv)
848 struct db_context *db = NULL;
851 uint32_t range_index = 0;
855 if (c->display_usage) {
856 net_idmap_autorid_set_range_usage();
860 if (argc < 2 || argc > 3) {
861 net_idmap_autorid_set_range_usage();
865 ok = parse_uint32(argv[0], &rangenum);
867 d_printf("%s: %s\n", _("Invalid range specification"),
869 net_idmap_autorid_set_range_usage();
876 ok = parse_uint32(argv[2], &range_index);
879 _("Invalid index specification"), argv[2]);
880 net_idmap_autorid_set_range_usage();
885 mem_ctx = talloc_stackframe();
886 if (!net_idmap_opendb_autorid(mem_ctx, c, false, &db)) {
890 status = idmap_autorid_setrange(db, domsid, range_index, rangenum);
891 if (!NT_STATUS_IS_OK(status)) {
892 d_fprintf(stderr, "%s: %s\n",
893 _("Failed to save domain mapping"),
901 TALLOC_FREE(mem_ctx);
905 static bool idmap_store_secret(const char *backend,
907 const char *identity,
914 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
916 if (r < 0) return false;
918 /* make sure the key is case insensitive */
919 if (!strupper_m(tmp)) {
923 ret = secrets_store_generic(tmp, identity, secret);
930 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
940 if (argc != 2 || c->display_usage) {
943 _("net idmap set secret <DOMAIN> <secret>\n"
944 " Set the secret for the specified domain\n"
945 " DOMAIN\tDomain to set secret for.\n"
946 " secret\tNew secret to set.\n"));
947 return c->display_usage?0:-1;
952 ctx = talloc_new(NULL);
955 domain = talloc_strdup(ctx, argv[0]);
958 opt = talloc_asprintf(ctx, "idmap config %s", domain);
961 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
962 ALLOC_CHECK(backend);
964 if ((!backend) || (!strequal(backend, "ldap") &&
965 !strequal(backend, "rfc2307"))) {
967 _("The only currently supported backend are LDAP "
973 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
976 _("Missing ldap_user_dn option for domain %s\n"),
982 ret = idmap_store_secret("ldap", domain, dn, secret);
985 d_fprintf(stderr, _("Failed to store secret\n"));
990 d_printf(_("Secret stored\n"));
994 static int net_idmap_autorid_set_config(struct net_context *c,
995 int argc, const char **argv)
1000 struct db_context *db = NULL;
1002 if (argc != 1 || c->display_usage) {
1005 _("net idmap set config <config>"
1006 " [--db=<inputfile>]\n"
1007 " Update CONFIG entry in autorid.\n"
1008 " config\tConfig string to be stored\n"
1009 " inputfile\tTDB file to update config.\n"));
1010 return c->display_usage ? 0 : -1;
1013 mem_ctx = talloc_stackframe();
1015 if (!net_idmap_opendb_autorid(mem_ctx, c, false, &db)) {
1019 status = idmap_autorid_saveconfigstr(db, argv[0]);
1020 if (!NT_STATUS_IS_OK(status)) {
1021 printf("Error storing the config in the database: %s\n",
1029 TALLOC_FREE(mem_ctx);
1033 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
1035 struct functable func[] = {
1038 net_idmap_set_mapping,
1039 NET_TRANSPORT_LOCAL,
1040 N_("Not implemented yet"),
1041 N_("net idmap set mapping\n"
1042 " Not implemented yet")
1046 net_idmap_autorid_set_range,
1047 NET_TRANSPORT_LOCAL,
1048 N_("Store a domain-range mapping"),
1049 N_("net idmap set range\n"
1050 " Store a domain-range mapping")
1054 net_idmap_autorid_set_config,
1055 NET_TRANSPORT_LOCAL,
1056 N_("Save the global configuration in the autorid database"),
1057 N_("net idmap set config \n"
1058 " Save the global configuration in the autorid database ")
1063 NET_TRANSPORT_LOCAL,
1064 N_("Set secret for specified domain"),
1065 N_("net idmap set secret <DOMAIN> <secret>\n"
1066 " Set secret for specified domain")
1068 {NULL, NULL, 0, NULL, NULL}
1071 return net_run_function(c, argc, argv, "net idmap set", func);
1074 static void net_idmap_autorid_get_range_usage(void)
1078 _("net idmap get range <SID> [<index>] [--db=<inputfile>]\n"
1079 " Get the range for a given domain and index.\n"
1080 " SID\t\tSID of the domain\n"
1081 " index\trange-index number to be retrieved\n"
1082 " inputfile\tTDB file to add mapping to.\n"));
1086 static int net_idmap_autorid_get_range(struct net_context *c, int argc,
1090 TALLOC_CTX *mem_ctx;
1091 struct db_context *db = NULL;
1094 uint32_t range_index = 0;
1100 if (c->display_usage) {
1101 net_idmap_autorid_get_range_usage();
1105 if (argc < 1 || argc > 2) {
1106 net_idmap_autorid_get_range_usage();
1113 ok = parse_uint32(argv[1], &range_index);
1115 d_printf("%s: %s\n",
1116 _("Invalid index specification"), argv[1]);
1117 net_idmap_autorid_get_range_usage();
1122 mem_ctx = talloc_stackframe();
1123 if (!net_idmap_opendb_autorid(mem_ctx, c, true, &db)) {
1127 status = idmap_autorid_getrange(db, domsid, range_index, &rangenum,
1129 if (!NT_STATUS_IS_OK(status)) {
1130 d_fprintf(stderr, "%s: %s\n",
1131 _("Failed to load domain range"), nt_errstr(status));
1135 if (range_index == 0) {
1136 keystr = talloc_strdup(mem_ctx, domsid);
1138 keystr = talloc_asprintf(mem_ctx, "%s#%"PRIu32, domsid,
1142 printf("RANGE %"PRIu32": %s (low id: %"PRIu32")\n",
1143 rangenum, keystr, low_id);
1148 TALLOC_FREE(mem_ctx);
1152 static NTSTATUS net_idmap_autorid_print_range(struct db_context *db,
1154 uint32_t range_index,
1158 if (range_index == 0) {
1159 printf("RANGE %"PRIu32": %s\n", rangenum, domsid);
1161 printf("RANGE %"PRIu32": %s#%"PRIu32"\n", rangenum, domsid,
1165 return NT_STATUS_OK;
1168 static void net_idmap_autorid_get_ranges_usage(void)
1172 _("net idmap get ranges [<SID>] [--db=<inputfile>]\n"
1173 " Get all ranges for a given domain.\n"
1174 " SID\t\tSID of the domain - list all ranges if omitted\n"
1175 " inputfile\tTDB file to add mapping to.\n"));
1178 static int net_idmap_autorid_get_ranges(struct net_context *c, int argc,
1182 TALLOC_CTX *mem_ctx;
1183 struct db_context *db = NULL;
1187 if (c->display_usage) {
1188 net_idmap_autorid_get_ranges_usage();
1194 } else if (argc == 1) {
1197 net_idmap_autorid_get_ranges_usage();
1201 mem_ctx = talloc_stackframe();
1202 if (!net_idmap_opendb_autorid(mem_ctx, c, true, &db)) {
1206 status = idmap_autorid_iterate_domain_ranges_read(db,
1208 net_idmap_autorid_print_range,
1209 NULL, /* private_data */
1211 if (!NT_STATUS_IS_OK(status)) {
1212 d_fprintf(stderr, "%s: %s\n",
1213 _("Error getting domain ranges"), nt_errstr(status));
1220 talloc_free(mem_ctx);
1224 static int net_idmap_autorid_get_config(struct net_context *c, int argc,
1229 TALLOC_CTX *mem_ctx;
1231 struct db_context *db = NULL;
1233 if (argc > 0 || c->display_usage) {
1236 _("net idmap get config"
1237 " [--db=<inputfile>]\n"
1238 " Get CONFIG entry from autorid database\n"
1239 " inputfile\tTDB file to read config from.\n"));
1240 return c->display_usage ? 0 : -1;
1243 mem_ctx = talloc_stackframe();
1245 if (!net_idmap_opendb_autorid(mem_ctx, c, true, &db)) {
1249 status = idmap_autorid_getconfigstr(db, mem_ctx, &config);
1250 if (!NT_STATUS_IS_OK(status)) {
1251 d_fprintf(stderr, "%s: %s\n",
1252 _("Error: unable to read config entry"),
1257 printf("CONFIG: %s\n", config);
1261 TALLOC_FREE(mem_ctx);
1266 static int net_idmap_get(struct net_context *c, int argc, const char **argv)
1268 struct functable func[] = {
1271 net_idmap_autorid_get_range,
1272 NET_TRANSPORT_LOCAL,
1273 N_("Get the range for a domain and range-index"),
1274 N_("net idmap get range\n"
1275 " Get the range for a domain and range-index")
1279 net_idmap_autorid_get_ranges,
1280 NET_TRANSPORT_LOCAL,
1281 N_("Get all ranges for a domain"),
1282 N_("net idmap get ranges <SID>\n"
1283 " Get all ranges for a domain")
1287 net_idmap_autorid_get_config,
1288 NET_TRANSPORT_LOCAL,
1289 N_("Get the global configuration from the autorid database"),
1290 N_("net idmap get config \n"
1291 " Get the global configuration from the autorid database ")
1293 {NULL, NULL, 0, NULL, NULL}
1296 return net_run_function(c, argc, argv, "net idmap get", func);
1299 static int net_idmap_check(struct net_context *c, int argc, const char **argv)
1302 struct check_options opts;
1303 struct net_idmap_ctx ctx = { .backend = TDB };
1306 if ( argc > 1 || c->display_usage) {
1309 _("net idmap check [-v] [-r] [-a] [-T] [-f] [-l] [[--db=]<TDB>]\n"
1310 " Check an idmap database.\n"
1311 " --verbose,-v\tverbose\n"
1312 " --repair,-r\trepair\n"
1313 " --auto,-a\tnoninteractive mode\n"
1314 " --test,-T\tdry run\n"
1315 " --force,-f\tforce\n"
1316 " --lock,-l\tlock db while doing the check\n"
1317 " TDB\tidmap database\n"));
1318 return c->display_usage ? 0 : -1;
1322 dbfile = talloc_strdup(talloc_tos(), argv[0]);
1324 dbfile = net_idmap_dbfile(c, &ctx);
1326 if (dbfile == NULL) {
1330 if (ctx.backend != TDB) {
1331 d_fprintf(stderr, _("Sorry, checking of non-TDB databases is "
1332 "currently not supported\n"));
1333 talloc_free(dbfile);
1337 d_fprintf(stderr, _("check database: %s\n"), dbfile);
1339 opts = (struct check_options) {
1340 .lock = c->opt_lock || c->opt_long_list_entries,
1341 .test = c->opt_testmode,
1342 .automatic = c->opt_auto,
1343 .verbose = c->opt_verbose,
1344 .force = c->opt_force,
1345 .repair = c->opt_repair || c->opt_reboot,
1348 ret = net_idmap_check_db(dbfile, &opts);
1349 talloc_free(dbfile);
1353 /***********************************************************
1354 Look at the current idmap
1355 **********************************************************/
1356 int net_idmap(struct net_context *c, int argc, const char **argv)
1358 struct functable func[] = {
1362 NET_TRANSPORT_LOCAL,
1363 N_("Dump the current ID mapping database"),
1364 N_("net idmap dump\n"
1365 " Dump the current ID mappings")
1370 NET_TRANSPORT_LOCAL,
1371 N_("Restore entries from a file or stdin"),
1372 N_("net idmap restore\n"
1373 " Restore entries from stdin")
1378 NET_TRANSPORT_LOCAL,
1379 N_("Read data from the ID mapping database"),
1380 N_("net idmap get\n"
1381 " Read data from the ID mapping database")
1386 NET_TRANSPORT_LOCAL,
1387 N_("Write data to the ID mapping database"),
1388 N_("net idmap set\n"
1389 " Write data to the ID mapping database")
1394 NET_TRANSPORT_LOCAL,
1395 N_("Delete entries from the ID mapping database"),
1396 N_("net idmap delete\n"
1397 " Delete entries from the ID mapping database")
1402 NET_TRANSPORT_LOCAL,
1403 N_("Check id mappings"),
1404 N_("net idmap check\n"
1405 " Check id mappings")
1407 {NULL, NULL, 0, NULL, NULL}
1410 return net_run_function(c, argc, argv, "net idmap", func);