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"
32 #define ALLOC_CHECK(mem) do { \
34 d_fprintf(stderr, _("Out of memory!\n")); \
39 enum idmap_dump_backend
{
44 struct net_idmap_ctx
{
45 enum idmap_dump_backend backend
;
48 static int net_idmap_dump_one_autorid_entry(struct db_record
*rec
,
54 key
= dbwrap_record_get_key(rec
);
55 value
= dbwrap_record_get_value(rec
);
57 if (strncmp((char *)key
.dptr
, "CONFIG", 6) == 0) {
58 char *config
= talloc_array(talloc_tos(), char, value
.dsize
+1);
59 memcpy(config
, value
.dptr
, value
.dsize
);
60 config
[value
.dsize
] = '\0';
61 printf("CONFIG: %s\n", config
);
66 if (strncmp((char *)key
.dptr
, "NEXT RANGE", 10) == 0) {
67 printf("RANGE HWM: %"PRIu32
"\n", IVAL(value
.dptr
, 0));
71 if (strncmp((char *)key
.dptr
, "NEXT ALLOC UID", 14) == 0) {
72 printf("UID HWM: %"PRIu32
"\n", IVAL(value
.dptr
, 0));
76 if (strncmp((char *)key
.dptr
, "NEXT ALLOC GID", 14) == 0) {
77 printf("GID HWM: %"PRIu32
"\n", IVAL(value
.dptr
, 0));
81 if (strncmp((char *)key
.dptr
, "UID", 3) == 0 ||
82 strncmp((char *)key
.dptr
, "GID", 3) == 0)
84 /* mapped entry from allocation pool */
85 printf("%s %s\n", value
.dptr
, key
.dptr
);
89 if ((strncmp((char *)key
.dptr
, "S-1-5-", 6) == 0 ||
90 strncmp((char *)key
.dptr
, "ALLOC", 5) == 0) &&
91 value
.dsize
== sizeof(uint32_t))
93 /* this is a domain range assignment */
94 uint32_t range
= IVAL(value
.dptr
, 0);
95 printf("RANGE %"PRIu32
": %s\n", range
, key
.dptr
);
102 /***********************************************************
103 Helper function for net_idmap_dump. Dump one entry.
104 **********************************************************/
105 static int net_idmap_dump_one_tdb_entry(struct db_record
*rec
,
111 key
= dbwrap_record_get_key(rec
);
112 value
= dbwrap_record_get_value(rec
);
114 if (strcmp((char *)key
.dptr
, "USER HWM") == 0) {
115 printf(_("USER HWM %d\n"), IVAL(value
.dptr
,0));
119 if (strcmp((char *)key
.dptr
, "GROUP HWM") == 0) {
120 printf(_("GROUP HWM %d\n"), IVAL(value
.dptr
,0));
124 if (strncmp((char *)key
.dptr
, "S-", 2) != 0) {
128 printf("%s %s\n", value
.dptr
, key
.dptr
);
132 /* returns db path for idmap backend alloced on talloc_tos */
133 static char *net_idmap_dbfile(struct net_context
*c
,
134 struct net_idmap_ctx
*ctx
)
137 const char *backend
= NULL
;
139 backend
= lp_idmap_default_backend();
141 d_printf(_("Internal error: 'idmap config * : backend' is not set!\n"));
145 if (c
->opt_db
!= NULL
) {
146 dbfile
= talloc_strdup(talloc_tos(), c
->opt_db
);
147 if (dbfile
== NULL
) {
148 d_fprintf(stderr
, _("Out of memory!\n"));
150 } else if (strequal(backend
, "tdb")) {
151 dbfile
= state_path(talloc_tos(), "winbindd_idmap.tdb");
152 if (dbfile
== NULL
) {
153 d_fprintf(stderr
, _("Out of memory!\n"));
156 } else if (strequal(backend
, "tdb2")) {
157 dbfile
= talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
159 if (dbfile
== NULL
) {
160 d_fprintf(stderr
, _("Out of memory!\n"));
163 } else if (strequal(backend
, "autorid")) {
164 dbfile
= state_path(talloc_tos(), "autorid.tdb");
165 if (dbfile
== NULL
) {
166 d_fprintf(stderr
, _("Out of memory!\n"));
168 ctx
->backend
= AUTORID
;
170 char *_backend
= talloc_strdup(talloc_tos(), backend
);
171 char* args
= strchr(_backend
, ':');
176 d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
179 talloc_free(_backend
);
185 static bool net_idmap_opendb_autorid(TALLOC_CTX
*mem_ctx
,
186 struct net_context
*c
,
188 struct db_context
**db
)
192 struct net_idmap_ctx ctx
= { .backend
= AUTORID
};
198 dbfile
= net_idmap_dbfile(c
, &ctx
);
199 if (dbfile
== NULL
) {
203 if (ctx
.backend
!= AUTORID
) {
204 d_fprintf(stderr
, _("Unsupported backend\n"));
209 *db
= db_open(mem_ctx
, dbfile
, 0, TDB_DEFAULT
, O_RDONLY
, 0,
210 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
213 _("Could not open autorid db (%s): %s\n"),
214 dbfile
, strerror(errno
));
219 status
= idmap_autorid_db_init(dbfile
, mem_ctx
, db
);
220 if (!NT_STATUS_IS_OK(status
)) {
222 _("Error calling idmap_autorid_db_init: %s\n"),
236 /***********************************************************
237 Dump the current idmap
238 **********************************************************/
239 static int net_idmap_dump(struct net_context
*c
, int argc
, const char **argv
)
241 struct db_context
*db
;
246 struct net_idmap_ctx ctx
= { .backend
= TDB
};
248 if ( argc
> 1 || c
->display_usage
) {
251 _("net idmap dump [[--db=]<inputfile>]\n"
252 " Dump current ID mapping.\n"
253 " inputfile\tTDB file to read mappings from.\n"));
254 return c
->display_usage
?0:-1;
257 mem_ctx
= talloc_stackframe();
259 dbfile
= (argc
> 0) ? argv
[0] : net_idmap_dbfile(c
, &ctx
);
260 if (dbfile
== NULL
) {
263 d_fprintf(stderr
, _("dumping id mapping from %s\n"), dbfile
);
265 db
= db_open(mem_ctx
, dbfile
, 0, TDB_DEFAULT
, O_RDONLY
, 0,
266 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
268 d_fprintf(stderr
, _("Could not open idmap db (%s): %s\n"),
269 dbfile
, strerror(errno
));
273 if (ctx
.backend
== AUTORID
) {
274 status
= dbwrap_traverse_read(db
,
275 net_idmap_dump_one_autorid_entry
,
278 status
= dbwrap_traverse_read(db
,
279 net_idmap_dump_one_tdb_entry
,
282 if (!NT_STATUS_IS_OK(status
)) {
283 d_fprintf(stderr
, _("error traversing the database\n"));
291 talloc_free(mem_ctx
);
295 /***********************************************************
296 Write entries from stdin to current local idmap
297 **********************************************************/
299 static int net_idmap_store_id_mapping(struct db_context
*db
,
302 const char *sid_string
)
309 idstr
= talloc_asprintf(talloc_tos(), "UID %lu", idval
);
312 idstr
= talloc_asprintf(talloc_tos(), "GID %lu", idval
);
315 d_fprintf(stderr
, "Invalid id mapping type: %d\n", type
);
319 status
= dbwrap_store_bystring(db
, idstr
,
320 string_term_tdb_data(sid_string
),
322 if (!NT_STATUS_IS_OK(status
)) {
323 d_fprintf(stderr
, "Error storing ID -> SID: "
324 "%s\n", nt_errstr(status
));
328 status
= dbwrap_store_bystring(db
, sid_string
,
329 string_term_tdb_data(idstr
),
331 if (!NT_STATUS_IS_OK(status
)) {
332 d_fprintf(stderr
, "Error storing SID -> ID: "
333 "%s\n", nt_errstr(status
));
341 static int net_idmap_restore(struct net_context
*c
, int argc
, const char **argv
)
345 struct db_context
*db
;
346 const char *dbfile
= NULL
;
348 struct net_idmap_ctx ctx
= { .backend
= TDB
};
350 if (c
->display_usage
) {
353 _("net idmap restore [--db=<TDB>] [<inputfile>]\n"
354 " Restore ID mappings from file\n"
355 " TDB\tFile to store ID mappings to."
356 " inputfile\tFile to load ID mappings from. If not "
357 "given, load data from stdin.\n"));
361 mem_ctx
= talloc_stackframe();
363 dbfile
= net_idmap_dbfile(c
, &ctx
);
365 if (dbfile
== NULL
) {
370 if (ctx
.backend
!= TDB
) {
371 d_fprintf(stderr
, _("Sorry, restoring of non-TDB databases is "
372 "currently not supported\n"));
377 d_fprintf(stderr
, _("restoring id mapping to %s\n"), dbfile
);
380 input
= fopen(argv
[0], "r");
382 d_fprintf(stderr
, _("Could not open input file (%s): %s\n"),
383 argv
[0], strerror(errno
));
391 db
= db_open(mem_ctx
, dbfile
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0644,
392 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
394 d_fprintf(stderr
, _("Could not open idmap db (%s): %s\n"),
395 dbfile
, strerror(errno
));
400 if (dbwrap_transaction_start(db
) != 0) {
401 d_fprintf(stderr
, _("Failed to start transaction.\n"));
406 while (!feof(input
)) {
407 char line
[128], sid_string
[128];
412 if (fgets(line
, 127, input
) == NULL
)
417 if ( (len
> 0) && (line
[len
-1] == '\n') )
420 if (sscanf(line
, "GID %lu %127s", &idval
, sid_string
) == 2)
422 ret
= net_idmap_store_id_mapping(db
, ID_TYPE_GID
,
427 } else if (sscanf(line
, "UID %lu %127s", &idval
, sid_string
) == 2)
429 ret
= net_idmap_store_id_mapping(db
, ID_TYPE_UID
,
434 } else if (sscanf(line
, "USER HWM %lu", &idval
) == 1) {
435 status
= dbwrap_store_int32_bystring(
436 db
, "USER HWM", idval
);
437 if (!NT_STATUS_IS_OK(status
)) {
439 _("Could not store USER HWM: %s\n"),
443 } else if (sscanf(line
, "GROUP HWM %lu", &idval
) == 1) {
444 status
= dbwrap_store_int32_bystring(
445 db
, "GROUP HWM", idval
);
446 if (!NT_STATUS_IS_OK(status
)) {
448 _("Could not store GROUP HWM: %s\n"),
453 d_fprintf(stderr
, _("ignoring invalid line [%s]\n"),
460 if(dbwrap_transaction_commit(db
) != 0) {
461 d_fprintf(stderr
, _("Failed to commit transaction.\n"));
465 if (dbwrap_transaction_cancel(db
) != 0) {
466 d_fprintf(stderr
, _("Failed to cancel transaction.\n"));
471 if ((input
!= NULL
) && (input
!= stdin
)) {
475 talloc_free(mem_ctx
);
480 NTSTATUS
dbwrap_delete_mapping(struct db_context
*db
, TDB_DATA key1
, bool force
)
482 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
483 bool is_valid_mapping
;
484 NTSTATUS status
= NT_STATUS_OK
;
490 status
= dbwrap_fetch(db
, mem_ctx
, key1
, &val1
);
491 if (!NT_STATUS_IS_OK(status
)) {
492 DEBUG(1, ("failed to fetch: %.*s\n", (int)key1
.dsize
, key1
.dptr
));
496 if (val1
.dptr
== NULL
) {
497 DEBUG(1, ("invalid mapping: %.*s -> empty value\n",
498 (int)key1
.dsize
, key1
.dptr
));
499 status
= NT_STATUS_FILE_INVALID
;
503 DEBUG(2, ("mapping: %.*s -> %.*s\n",
504 (int)key1
.dsize
, key1
.dptr
, (int)val1
.dsize
, val1
.dptr
));
506 status
= dbwrap_fetch(db
, mem_ctx
, val1
, &val2
);
507 if (!NT_STATUS_IS_OK(status
)) {
508 DEBUG(1, ("failed to fetch: %.*s\n", (int)val1
.dsize
, val1
.dptr
));
512 is_valid_mapping
= tdb_data_equal(key1
, val2
);
514 if (!is_valid_mapping
) {
515 DEBUG(1, ("invalid mapping: %.*s -> %.*s -> %.*s\n",
516 (int)key1
.dsize
, key1
.dptr
,
517 (int)val1
.dsize
, val1
.dptr
,
518 (int)val2
.dsize
, val2
.dptr
));
520 status
= NT_STATUS_FILE_INVALID
;
525 status
= dbwrap_delete(db
, key1
);
526 if (!NT_STATUS_IS_OK(status
)) {
527 DEBUG(1, ("failed to delete: %.*s\n", (int)key1
.dsize
, key1
.dptr
));
531 if (!is_valid_mapping
) {
535 status
= dbwrap_delete(db
, val1
);
536 if (!NT_STATUS_IS_OK(status
)) {
537 DEBUG(1, ("failed to delete: %.*s\n", (int)val1
.dsize
, val1
.dptr
));
541 talloc_free(mem_ctx
);
546 NTSTATUS
delete_mapping_action(struct db_context
*db
, void* data
)
548 return dbwrap_delete_mapping(db
, *(TDB_DATA
*)data
, false);
551 NTSTATUS
delete_mapping_action_force(struct db_context
*db
, void* data
)
553 return dbwrap_delete_mapping(db
, *(TDB_DATA
*)data
, true);
556 /***********************************************************
557 Delete a SID mapping from a winbindd_idmap.tdb
558 **********************************************************/
559 static bool delete_args_ok(int argc
, const char **argv
)
563 if (strncmp(argv
[0], "S-", 2) == 0)
565 if (strncmp(argv
[0], "GID ", 4) == 0)
567 if (strncmp(argv
[0], "UID ", 4) == 0)
572 static int net_idmap_delete_mapping(struct net_context
*c
, int argc
,
576 struct db_context
*db
;
581 struct net_idmap_ctx ctx
= { .backend
= TDB
};
583 if ( !delete_args_ok(argc
,argv
) || c
->display_usage
) {
586 _("net idmap delete mapping [-f] [--db=<TDB>] <ID>\n"
587 " Delete mapping of ID from TDB.\n"
589 " TDB\tidmap database\n"
590 " ID\tSID|GID|UID\n"));
591 return c
->display_usage
? 0 : -1;
594 mem_ctx
= talloc_stackframe();
596 dbfile
= net_idmap_dbfile(c
, &ctx
);
597 if (dbfile
== NULL
) {
600 d_fprintf(stderr
, _("deleting id mapping from %s\n"), dbfile
);
602 db
= db_open(mem_ctx
, dbfile
, 0, TDB_DEFAULT
, O_RDWR
, 0,
603 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
605 d_fprintf(stderr
, _("Could not open idmap db (%s): %s\n"),
606 dbfile
, strerror(errno
));
610 key
= string_term_tdb_data(argv
[0]);
612 status
= dbwrap_trans_do(db
, (c
->opt_force
613 ? delete_mapping_action_force
614 : delete_mapping_action
), &key
);
616 if (!NT_STATUS_IS_OK(status
)) {
617 d_fprintf(stderr
, _("could not delete mapping: %s\n"),
623 talloc_free(mem_ctx
);
627 static bool parse_uint32(const char *str
, uint32_t *result
)
633 val
= strtoul_err(str
, &endptr
, 10, &error
);
638 if (*endptr
!= '\0') {
644 *result
= val
; /* Potential crop */
648 static void net_idmap_autorid_delete_range_usage(void)
652 _("net idmap delete range [-f] [--db=<TDB>] <RANGE>|(<SID>[ <INDEX>])\n"
653 " Delete a domain range mapping from the database.\n"
655 " TDB\tidmap database\n"
656 " RANGE\tthe range number to delete\n"
657 " SID\t\tSID of the domain\n"
658 " INDEX\trange index number do delete for the domain\n"));
661 static int net_idmap_autorid_delete_range(struct net_context
*c
, int argc
,
665 struct db_context
*db
= NULL
;
668 uint32_t range_index
;
670 TALLOC_CTX
*mem_ctx
= NULL
;
672 bool force
= (c
->opt_force
!= 0);
674 if (c
->display_usage
) {
675 net_idmap_autorid_delete_range_usage();
679 if (argc
< 1 || argc
> 2) {
680 net_idmap_autorid_delete_range_usage();
684 mem_ctx
= talloc_stackframe();
685 if (!net_idmap_opendb_autorid(mem_ctx
, c
, false, &db
)) {
689 ok
= parse_uint32(argv
[0], &rangenum
);
691 d_printf("%s: %"PRIu32
"\n", _("Deleting range number"),
694 status
= idmap_autorid_delete_range_by_num(db
, rangenum
,
696 if (!NT_STATUS_IS_OK(status
)) {
697 d_fprintf(stderr
, "%s: %s\n",
698 _("Failed to delete domain range mapping"),
711 ok
= parse_uint32(argv
[1], &range_index
);
714 _("Invalid index specification"), argv
[1]);
715 net_idmap_autorid_delete_range_usage();
720 status
= idmap_autorid_delete_range_by_sid(db
, domsid
, range_index
,
722 if (!NT_STATUS_IS_OK(status
)) {
723 d_fprintf(stderr
, "%s: %s\n",
724 _("Failed to delete domain range mapping"),
732 talloc_free(mem_ctx
);
736 static void net_idmap_autorid_delete_ranges_usage(void)
740 _("net idmap delete ranges [-f] [--db=<TDB>] <SID>\n"
741 " Delete all domain range mappings for a given domain.\n"
743 " TDB\tidmap database\n"
744 " SID\t\tSID of the domain\n"));
747 static int net_idmap_autorid_delete_ranges(struct net_context
*c
, int argc
,
751 struct db_context
*db
= NULL
;
754 TALLOC_CTX
*mem_ctx
= NULL
;
755 bool force
= (c
->opt_force
!= 0);
758 if (c
->display_usage
) {
759 net_idmap_autorid_delete_ranges_usage();
764 net_idmap_autorid_delete_ranges_usage();
770 mem_ctx
= talloc_stackframe();
771 if (!net_idmap_opendb_autorid(mem_ctx
, c
, false, &db
)) {
775 status
= idmap_autorid_delete_domain_ranges(db
, domsid
, force
, &count
);
776 if (!NT_STATUS_IS_OK(status
)) {
777 d_fprintf(stderr
, "%s %s: %s\n",
778 _("Failed to delete domain range mappings for "
785 d_printf(_("deleted %d domain mappings\n"), count
);
790 talloc_free(mem_ctx
);
794 static int net_idmap_delete(struct net_context
*c
, int argc
, const char **argv
)
796 struct functable func
[] = {
799 net_idmap_delete_mapping
,
801 N_("Delete ID mapping"),
802 N_("net idmap delete mapping <ID>\n"
803 " Delete ID mapping")
807 net_idmap_autorid_delete_range
,
809 N_("Delete a domain range mapping"),
810 N_("net idmap delete range <RANGE>|(<SID>[ <INDEX>])\n"
811 " Delete a domain range mapping")
815 net_idmap_autorid_delete_ranges
,
817 N_("Delete all domain range mappings for a given "
819 N_("net idmap delete ranges <SID>\n"
820 " Delete a domain range mapping")
822 {NULL
, NULL
, 0, NULL
, NULL
}
825 return net_run_function(c
, argc
, argv
, "net idmap delete", func
);
829 static int net_idmap_set_mapping(struct net_context
*c
,
830 int argc
, const char **argv
)
832 d_printf("%s\n", _("Not implemented yet"));
836 static void net_idmap_autorid_set_range_usage(void)
840 _("net idmap set range"
841 " <range> <SID> [<index>] [--db=<inputfile>]\n"
842 " Store a domain-range mapping for a given domain.\n"
843 " range\tRange number to be set for the domain\n"
844 " SID\t\tSID of the domain\n"
845 " index\trange-index number to be set for the domain\n"
846 " inputfile\tTDB file to add mapping to.\n"));
849 static int net_idmap_autorid_set_range(struct net_context
*c
,
850 int argc
, const char **argv
)
854 struct db_context
*db
= NULL
;
857 uint32_t range_index
= 0;
861 if (c
->display_usage
) {
862 net_idmap_autorid_set_range_usage();
866 if (argc
< 2 || argc
> 3) {
867 net_idmap_autorid_set_range_usage();
871 ok
= parse_uint32(argv
[0], &rangenum
);
873 d_printf("%s: %s\n", _("Invalid range specification"),
875 net_idmap_autorid_set_range_usage();
882 ok
= parse_uint32(argv
[2], &range_index
);
885 _("Invalid index specification"), argv
[2]);
886 net_idmap_autorid_set_range_usage();
891 mem_ctx
= talloc_stackframe();
892 if (!net_idmap_opendb_autorid(mem_ctx
, c
, false, &db
)) {
896 status
= idmap_autorid_setrange(db
, domsid
, range_index
, rangenum
);
897 if (!NT_STATUS_IS_OK(status
)) {
898 d_fprintf(stderr
, "%s: %s\n",
899 _("Failed to save domain mapping"),
907 TALLOC_FREE(mem_ctx
);
911 static bool idmap_store_secret(const char *backend
,
913 const char *identity
,
920 r
= asprintf(&tmp
, "IDMAP_%s_%s", backend
, domain
);
922 if (r
< 0) return false;
924 /* make sure the key is case insensitive */
925 if (!strupper_m(tmp
)) {
929 ret
= secrets_store_generic(tmp
, identity
, secret
);
936 static int net_idmap_secret(struct net_context
*c
, int argc
, const char **argv
)
946 if (argc
!= 2 || c
->display_usage
) {
949 _("net idmap set secret <DOMAIN> <secret>\n"
950 " Set the secret for the specified domain\n"
951 " DOMAIN\tDomain to set secret for.\n"
952 " secret\tNew secret to set.\n"));
953 return c
->display_usage
?0:-1;
958 ctx
= talloc_new(NULL
);
961 domain
= talloc_strdup(ctx
, argv
[0]);
964 opt
= talloc_asprintf(ctx
, "idmap config %s", domain
);
967 backend
= talloc_strdup(ctx
, lp_parm_const_string(-1, opt
, "backend", "tdb"));
968 ALLOC_CHECK(backend
);
970 if ((!backend
) || (!strequal(backend
, "ldap") &&
971 !strequal(backend
, "rfc2307"))) {
973 _("The only currently supported backend are LDAP "
979 dn
= lp_parm_const_string(-1, opt
, "ldap_user_dn", NULL
);
982 _("Missing ldap_user_dn option for domain %s\n"),
988 ret
= idmap_store_secret("ldap", domain
, dn
, secret
);
991 d_fprintf(stderr
, _("Failed to store secret\n"));
996 d_printf(_("Secret stored\n"));
1000 static int net_idmap_autorid_set_config(struct net_context
*c
,
1001 int argc
, const char **argv
)
1005 TALLOC_CTX
*mem_ctx
;
1006 struct db_context
*db
= NULL
;
1008 if (argc
!= 1 || c
->display_usage
) {
1011 _("net idmap set config <config>"
1012 " [--db=<inputfile>]\n"
1013 " Update CONFIG entry in autorid.\n"
1014 " config\tConfig string to be stored\n"
1015 " inputfile\tTDB file to update config.\n"));
1016 return c
->display_usage
? 0 : -1;
1019 mem_ctx
= talloc_stackframe();
1021 if (!net_idmap_opendb_autorid(mem_ctx
, c
, false, &db
)) {
1025 status
= idmap_autorid_saveconfigstr(db
, argv
[0]);
1026 if (!NT_STATUS_IS_OK(status
)) {
1027 printf("Error storing the config in the database: %s\n",
1035 TALLOC_FREE(mem_ctx
);
1039 static int net_idmap_set(struct net_context
*c
, int argc
, const char **argv
)
1041 struct functable func
[] = {
1044 net_idmap_set_mapping
,
1045 NET_TRANSPORT_LOCAL
,
1046 N_("Not implemented yet"),
1047 N_("net idmap set mapping\n"
1048 " Not implemented yet")
1052 net_idmap_autorid_set_range
,
1053 NET_TRANSPORT_LOCAL
,
1054 N_("Store a domain-range mapping"),
1055 N_("net idmap set range\n"
1056 " Store a domain-range mapping")
1060 net_idmap_autorid_set_config
,
1061 NET_TRANSPORT_LOCAL
,
1062 N_("Save the global configuration in the autorid database"),
1063 N_("net idmap set config \n"
1064 " Save the global configuration in the autorid database ")
1069 NET_TRANSPORT_LOCAL
,
1070 N_("Set secret for specified domain"),
1071 N_("net idmap set secret <DOMAIN> <secret>\n"
1072 " Set secret for specified domain")
1074 {NULL
, NULL
, 0, NULL
, NULL
}
1077 return net_run_function(c
, argc
, argv
, "net idmap set", func
);
1080 static void net_idmap_autorid_get_range_usage(void)
1084 _("net idmap get range <SID> [<index>] [--db=<inputfile>]\n"
1085 " Get the range for a given domain and index.\n"
1086 " SID\t\tSID of the domain\n"
1087 " index\trange-index number to be retrieved\n"
1088 " inputfile\tTDB file to add mapping to.\n"));
1092 static int net_idmap_autorid_get_range(struct net_context
*c
, int argc
,
1096 TALLOC_CTX
*mem_ctx
;
1097 struct db_context
*db
= NULL
;
1100 uint32_t range_index
= 0;
1106 if (c
->display_usage
) {
1107 net_idmap_autorid_get_range_usage();
1111 if (argc
< 1 || argc
> 2) {
1112 net_idmap_autorid_get_range_usage();
1119 ok
= parse_uint32(argv
[1], &range_index
);
1121 d_printf("%s: %s\n",
1122 _("Invalid index specification"), argv
[1]);
1123 net_idmap_autorid_get_range_usage();
1128 mem_ctx
= talloc_stackframe();
1129 if (!net_idmap_opendb_autorid(mem_ctx
, c
, true, &db
)) {
1133 status
= idmap_autorid_getrange(db
, domsid
, range_index
, &rangenum
,
1135 if (!NT_STATUS_IS_OK(status
)) {
1136 d_fprintf(stderr
, "%s: %s\n",
1137 _("Failed to load domain range"), nt_errstr(status
));
1141 if (range_index
== 0) {
1142 keystr
= talloc_strdup(mem_ctx
, domsid
);
1144 keystr
= talloc_asprintf(mem_ctx
, "%s#%"PRIu32
, domsid
,
1148 printf("RANGE %"PRIu32
": %s (low id: %"PRIu32
")\n",
1149 rangenum
, keystr
, low_id
);
1154 TALLOC_FREE(mem_ctx
);
1158 static NTSTATUS
net_idmap_autorid_print_range(struct db_context
*db
,
1160 uint32_t range_index
,
1164 if (range_index
== 0) {
1165 printf("RANGE %"PRIu32
": %s\n", rangenum
, domsid
);
1167 printf("RANGE %"PRIu32
": %s#%"PRIu32
"\n", rangenum
, domsid
,
1171 return NT_STATUS_OK
;
1174 static void net_idmap_autorid_get_ranges_usage(void)
1178 _("net idmap get ranges [<SID>] [--db=<inputfile>]\n"
1179 " Get all ranges for a given domain.\n"
1180 " SID\t\tSID of the domain - list all ranges if omitted\n"
1181 " inputfile\tTDB file to add mapping to.\n"));
1184 static int net_idmap_autorid_get_ranges(struct net_context
*c
, int argc
,
1188 TALLOC_CTX
*mem_ctx
;
1189 struct db_context
*db
= NULL
;
1193 if (c
->display_usage
) {
1194 net_idmap_autorid_get_ranges_usage();
1200 } else if (argc
== 1) {
1203 net_idmap_autorid_get_ranges_usage();
1207 mem_ctx
= talloc_stackframe();
1208 if (!net_idmap_opendb_autorid(mem_ctx
, c
, true, &db
)) {
1212 status
= idmap_autorid_iterate_domain_ranges_read(db
,
1214 net_idmap_autorid_print_range
,
1215 NULL
, /* private_data */
1217 if (!NT_STATUS_IS_OK(status
)) {
1218 d_fprintf(stderr
, "%s: %s\n",
1219 _("Error getting domain ranges"), nt_errstr(status
));
1226 talloc_free(mem_ctx
);
1230 static int net_idmap_autorid_get_config(struct net_context
*c
, int argc
,
1235 TALLOC_CTX
*mem_ctx
;
1237 struct db_context
*db
= NULL
;
1239 if (argc
> 0 || c
->display_usage
) {
1242 _("net idmap get config"
1243 " [--db=<inputfile>]\n"
1244 " Get CONFIG entry from autorid database\n"
1245 " inputfile\tTDB file to read config from.\n"));
1246 return c
->display_usage
? 0 : -1;
1249 mem_ctx
= talloc_stackframe();
1251 if (!net_idmap_opendb_autorid(mem_ctx
, c
, true, &db
)) {
1255 status
= idmap_autorid_getconfigstr(db
, mem_ctx
, &config
);
1256 if (!NT_STATUS_IS_OK(status
)) {
1257 d_fprintf(stderr
, "%s: %s\n",
1258 _("Error: unable to read config entry"),
1263 printf("CONFIG: %s\n", config
);
1267 TALLOC_FREE(mem_ctx
);
1272 static int net_idmap_get(struct net_context
*c
, int argc
, const char **argv
)
1274 struct functable func
[] = {
1277 net_idmap_autorid_get_range
,
1278 NET_TRANSPORT_LOCAL
,
1279 N_("Get the range for a domain and range-index"),
1280 N_("net idmap get range\n"
1281 " Get the range for a domain and range-index")
1285 net_idmap_autorid_get_ranges
,
1286 NET_TRANSPORT_LOCAL
,
1287 N_("Get all ranges for a domain"),
1288 N_("net idmap get ranges <SID>\n"
1289 " Get all ranges for a domain")
1293 net_idmap_autorid_get_config
,
1294 NET_TRANSPORT_LOCAL
,
1295 N_("Get the global configuration from the autorid database"),
1296 N_("net idmap get config \n"
1297 " Get the global configuration from the autorid database ")
1299 {NULL
, NULL
, 0, NULL
, NULL
}
1302 return net_run_function(c
, argc
, argv
, "net idmap get", func
);
1305 static int net_idmap_check(struct net_context
*c
, int argc
, const char **argv
)
1308 struct check_options opts
;
1309 struct net_idmap_ctx ctx
= { .backend
= TDB
};
1312 if ( argc
> 1 || c
->display_usage
) {
1315 _("net idmap check [-v] [-r] [-a] [-T] [-f] [-l] [[--db=]<TDB>]\n"
1316 " Check an idmap database.\n"
1317 " --verbose,-v\tverbose\n"
1318 " --repair,-r\trepair\n"
1319 " --auto,-a\tnoninteractive mode\n"
1320 " --test,-T\tdry run\n"
1321 " --fore,-f\tforce\n"
1322 " --lock,-l\tlock db while doing the check\n"
1323 " TDB\tidmap database\n"));
1324 return c
->display_usage
? 0 : -1;
1328 dbfile
= talloc_strdup(talloc_tos(), argv
[0]);
1330 dbfile
= net_idmap_dbfile(c
, &ctx
);
1332 if (dbfile
== NULL
) {
1336 if (ctx
.backend
!= TDB
) {
1337 d_fprintf(stderr
, _("Sorry, checking of non-TDB databases is "
1338 "currently not supported\n"));
1339 talloc_free(dbfile
);
1343 d_fprintf(stderr
, _("check database: %s\n"), dbfile
);
1345 opts
= (struct check_options
) {
1346 .lock
= c
->opt_lock
|| c
->opt_long_list_entries
,
1347 .test
= c
->opt_testmode
,
1348 .automatic
= c
->opt_auto
,
1349 .verbose
= c
->opt_verbose
,
1350 .force
= c
->opt_force
,
1351 .repair
= c
->opt_repair
|| c
->opt_reboot
,
1354 ret
= net_idmap_check_db(dbfile
, &opts
);
1355 talloc_free(dbfile
);
1359 /***********************************************************
1360 Look at the current idmap
1361 **********************************************************/
1362 int net_idmap(struct net_context
*c
, int argc
, const char **argv
)
1364 struct functable func
[] = {
1368 NET_TRANSPORT_LOCAL
,
1369 N_("Dump the current ID mapping database"),
1370 N_("net idmap dump\n"
1371 " Dump the current ID mappings")
1376 NET_TRANSPORT_LOCAL
,
1377 N_("Restore entries from a file or stdin"),
1378 N_("net idmap restore\n"
1379 " Restore entries from stdin")
1384 NET_TRANSPORT_LOCAL
,
1385 N_("Read data from the ID mapping database"),
1386 N_("net idmap get\n"
1387 " Read data from the ID mapping database")
1392 NET_TRANSPORT_LOCAL
,
1393 N_("Write data to the ID mapping database"),
1394 N_("net idmap set\n"
1395 " Write data to the ID mapping database")
1400 NET_TRANSPORT_LOCAL
,
1401 N_("Delete entries from the ID mapping database"),
1402 N_("net idmap delete\n"
1403 " Delete entries from the ID mapping database")
1408 NET_TRANSPORT_LOCAL
,
1409 N_("Check id mappings"),
1410 N_("net idmap check\n"
1411 " Check id mappings")
1413 {NULL
, NULL
, 0, NULL
, NULL
}
1416 return net_run_function(c
, argc
, argv
, "net idmap", func
);