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"
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
,
47 key
= dbwrap_record_get_key(rec
);
48 value
= dbwrap_record_get_value(rec
);
50 if (strcmp((char *)key
.dptr
, "USER HWM") == 0) {
51 printf(_("USER HWM %d\n"), IVAL(value
.dptr
,0));
55 if (strcmp((char *)key
.dptr
, "GROUP HWM") == 0) {
56 printf(_("GROUP HWM %d\n"), IVAL(value
.dptr
,0));
60 if (strncmp((char *)key
.dptr
, "S-", 2) != 0) {
64 printf("%s %s\n", value
.dptr
, key
.dptr
);
68 static const char* net_idmap_dbfile(struct net_context
*c
)
70 const char* dbfile
= NULL
;
71 const char *backend
= NULL
;
73 /* prefer idmap config * : backend over idmap backend parameter */
74 backend
= lp_parm_const_string(-1, "idmap config *", "backend", NULL
);
76 backend
= lp_idmap_backend();
79 if (c
->opt_db
!= NULL
) {
80 dbfile
= talloc_strdup(talloc_tos(), c
->opt_db
);
82 d_fprintf(stderr
, _("Out of memory!\n"));
84 } else if (strequal(backend
, "tdb")) {
85 dbfile
= state_path("winbindd_idmap.tdb");
87 d_fprintf(stderr
, _("Out of memory!\n"));
89 } else if (strequal(backend
, "tdb2")) {
90 dbfile
= talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
93 d_fprintf(stderr
, _("Out of memory!\n"));
96 char *_backend
= talloc_strdup(talloc_tos(), backend
);
97 char* args
= strchr(_backend
, ':');
102 d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
105 talloc_free(_backend
);
111 /***********************************************************
112 Dump the current idmap
113 **********************************************************/
114 static int net_idmap_dump(struct net_context
*c
, int argc
, const char **argv
)
116 struct db_context
*db
;
122 if ( argc
> 1 || c
->display_usage
) {
125 _("net idmap dump [[--db=]<inputfile>]\n"
126 " Dump current ID mapping.\n"
127 " inputfile\tTDB file to read mappings from.\n"));
128 return c
->display_usage
?0:-1;
131 mem_ctx
= talloc_stackframe();
133 dbfile
= (argc
> 0) ? argv
[0] : net_idmap_dbfile(c
);
134 if (dbfile
== NULL
) {
137 d_fprintf(stderr
, _("dumping id mapping from %s\n"), dbfile
);
139 db
= db_open(mem_ctx
, dbfile
, 0, TDB_DEFAULT
, O_RDONLY
, 0,
140 DBWRAP_LOCK_ORDER_1
);
142 d_fprintf(stderr
, _("Could not open idmap db (%s): %s\n"),
143 dbfile
, strerror(errno
));
147 status
= dbwrap_traverse_read(db
, net_idmap_dump_one_entry
, NULL
, NULL
);
148 if (!NT_STATUS_IS_OK(status
)) {
149 d_fprintf(stderr
, _("error traversing the database\n"));
157 talloc_free(mem_ctx
);
161 /***********************************************************
162 Write entries from stdin to current local idmap
163 **********************************************************/
165 static int net_idmap_store_id_mapping(struct db_context
*db
,
168 const char *sid_string
)
175 idstr
= talloc_asprintf(talloc_tos(), "UID %lu", idval
);
178 idstr
= talloc_asprintf(talloc_tos(), "GID %lu", idval
);
181 d_fprintf(stderr
, "Invalid id mapping type: %d\n", type
);
185 status
= dbwrap_store_bystring(db
, idstr
,
186 string_term_tdb_data(sid_string
),
188 if (!NT_STATUS_IS_OK(status
)) {
189 d_fprintf(stderr
, "Error storing ID -> SID: "
190 "%s\n", nt_errstr(status
));
194 status
= dbwrap_store_bystring(db
, sid_string
,
195 string_term_tdb_data(idstr
),
197 if (!NT_STATUS_IS_OK(status
)) {
198 d_fprintf(stderr
, "Error storing SID -> ID: "
199 "%s\n", nt_errstr(status
));
207 static int net_idmap_restore(struct net_context
*c
, int argc
, const char **argv
)
211 struct db_context
*db
;
212 const char *dbfile
= NULL
;
215 if (c
->display_usage
) {
218 _("net idmap restore [--db=<TDB>] [<inputfile>]\n"
219 " Restore ID mappings from file\n"
220 " TDB\tFile to store ID mappings to."
221 " inputfile\tFile to load ID mappings from. If not "
222 "given, load data from stdin.\n"));
226 mem_ctx
= talloc_stackframe();
228 dbfile
= net_idmap_dbfile(c
);
230 if (dbfile
== NULL
) {
235 d_fprintf(stderr
, _("restoring id mapping to %s\n"), dbfile
);
238 input
= fopen(argv
[0], "r");
240 d_fprintf(stderr
, _("Could not open input file (%s): %s\n"),
241 argv
[0], strerror(errno
));
249 db
= db_open(mem_ctx
, dbfile
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0644,
250 DBWRAP_LOCK_ORDER_1
);
252 d_fprintf(stderr
, _("Could not open idmap db (%s): %s\n"),
253 dbfile
, strerror(errno
));
258 if (dbwrap_transaction_start(db
) != 0) {
259 d_fprintf(stderr
, _("Failed to start transaction.\n"));
264 while (!feof(input
)) {
265 char line
[128], sid_string
[128];
270 if (fgets(line
, 127, input
) == NULL
)
275 if ( (len
> 0) && (line
[len
-1] == '\n') )
278 if (sscanf(line
, "GID %lu %128s", &idval
, sid_string
) == 2)
280 ret
= net_idmap_store_id_mapping(db
, ID_TYPE_GID
,
285 } else if (sscanf(line
, "UID %lu %128s", &idval
, sid_string
) == 2)
287 ret
= net_idmap_store_id_mapping(db
, ID_TYPE_UID
,
292 } else if (sscanf(line
, "USER HWM %lu", &idval
) == 1) {
293 status
= dbwrap_store_int32_bystring(
294 db
, "USER HWM", idval
);
295 if (!NT_STATUS_IS_OK(status
)) {
297 _("Could not store USER HWM: %s\n"),
301 } else if (sscanf(line
, "GROUP HWM %lu", &idval
) == 1) {
302 status
= dbwrap_store_int32_bystring(
303 db
, "GROUP HWM", idval
);
304 if (!NT_STATUS_IS_OK(status
)) {
306 _("Could not store GROUP HWM: %s\n"),
311 d_fprintf(stderr
, _("ignoring invalid line [%s]\n"),
318 if(dbwrap_transaction_commit(db
) != 0) {
319 d_fprintf(stderr
, _("Failed to commit transaction.\n"));
323 if (dbwrap_transaction_cancel(db
) != 0) {
324 d_fprintf(stderr
, _("Failed to cancel transaction.\n"));
329 if ((input
!= NULL
) && (input
!= stdin
)) {
333 talloc_free(mem_ctx
);
338 NTSTATUS
dbwrap_delete_mapping(struct db_context
*db
, TDB_DATA key1
, bool force
)
340 TALLOC_CTX
* mem_ctx
= talloc_tos();
341 struct db_record
*rec1
=NULL
, *rec2
=NULL
;
343 bool is_valid_mapping
;
344 NTSTATUS status
= NT_STATUS_OK
;
347 rec1
= dbwrap_fetch_locked(db
, mem_ctx
, key1
);
349 DEBUG(1, ("failed to fetch: %.*s\n", (int)key1
.dsize
, key1
.dptr
));
350 status
= NT_STATUS_NO_MEMORY
;
353 key2
= dbwrap_record_get_value(rec1
);
354 if (key2
.dptr
== NULL
) {
355 DEBUG(1, ("could not find %.*s\n", (int)key1
.dsize
, key1
.dptr
));
356 status
= NT_STATUS_NOT_FOUND
;
360 DEBUG(2, ("mapping: %.*s -> %.*s\n",
361 (int)key1
.dsize
, key1
.dptr
, (int)key2
.dsize
, key2
.dptr
));
363 rec2
= dbwrap_fetch_locked(db
, mem_ctx
, key2
);
365 DEBUG(1, ("failed to fetch: %.*s\n", (int)key2
.dsize
, key2
.dptr
));
366 status
= NT_STATUS_NO_MEMORY
;
370 value
= dbwrap_record_get_value(rec2
);
371 is_valid_mapping
= tdb_data_equal(key1
, value
);
373 if (!is_valid_mapping
) {
374 DEBUG(1, ("invalid mapping: %.*s -> %.*s -> %.*s\n",
375 (int)key1
.dsize
, key1
.dptr
, (int)key2
.dsize
, key2
.dptr
,
376 (int)value
.dsize
, value
.dptr
));
378 status
= NT_STATUS_FILE_INVALID
;
383 status
= dbwrap_record_delete(rec1
);
384 if (!NT_STATUS_IS_OK(status
)) {
385 DEBUG(1, ("failed to delete: %.*s\n", (int)key1
.dsize
, key1
.dptr
));
389 if (is_valid_mapping
) {
390 status
= dbwrap_record_delete(rec2
);
391 if (!NT_STATUS_IS_OK(status
)) {
392 DEBUG(1, ("failed to delete: %.*s\n", (int)key2
.dsize
, key2
.dptr
));
402 NTSTATUS
delete_mapping_action(struct db_context
*db
, void* data
)
404 return dbwrap_delete_mapping(db
, *(TDB_DATA
*)data
, false);
407 NTSTATUS
delete_mapping_action_force(struct db_context
*db
, void* data
)
409 return dbwrap_delete_mapping(db
, *(TDB_DATA
*)data
, true);
412 /***********************************************************
413 Delete a SID mapping from a winbindd_idmap.tdb
414 **********************************************************/
415 static bool delete_args_ok(int argc
, const char **argv
)
419 if (strncmp(argv
[0], "S-", 2) == 0)
421 if (strncmp(argv
[0], "GID ", 4) == 0)
423 if (strncmp(argv
[0], "UID ", 4) == 0)
428 static int net_idmap_delete(struct net_context
*c
, int argc
, const char **argv
)
431 struct db_context
*db
;
437 if ( !delete_args_ok(argc
,argv
) || c
->display_usage
) {
440 _("net idmap delete [-f] [--db=<TDB>] <ID>\n"
441 " Delete mapping of ID from TDB.\n"
443 " TDB\tidmap database\n"
444 " ID\tSID|GID|UID\n"));
445 return c
->display_usage
? 0 : -1;
448 mem_ctx
= talloc_stackframe();
450 dbfile
= net_idmap_dbfile(c
);
451 if (dbfile
== NULL
) {
454 d_fprintf(stderr
, _("deleting id mapping from %s\n"), dbfile
);
456 db
= db_open(mem_ctx
, dbfile
, 0, TDB_DEFAULT
, O_RDWR
, 0,
457 DBWRAP_LOCK_ORDER_1
);
459 d_fprintf(stderr
, _("Could not open idmap db (%s): %s\n"),
460 dbfile
, strerror(errno
));
464 key
= string_term_tdb_data(argv
[0]);
466 status
= dbwrap_trans_do(db
, (c
->opt_force
467 ? delete_mapping_action_force
468 : delete_mapping_action
), &key
);
470 if (!NT_STATUS_IS_OK(status
)) {
471 d_fprintf(stderr
, _("could not delete mapping: %s\n"),
477 talloc_free(mem_ctx
);
481 static int net_idmap_set(struct net_context
*c
, int argc
, const char **argv
)
483 d_printf("%s\n", _("Not implemented yet"));
486 static bool idmap_store_secret(const char *backend
,
488 const char *identity
,
495 r
= asprintf(&tmp
, "IDMAP_%s_%s", backend
, domain
);
497 if (r
< 0) return false;
499 /* make sure the key is case insensitive */
500 if (!strupper_m(tmp
)) {
504 ret
= secrets_store_generic(tmp
, identity
, secret
);
511 static int net_idmap_secret(struct net_context
*c
, int argc
, const char **argv
)
521 if (argc
!= 2 || c
->display_usage
) {
524 _("net idmap secret <DOMAIN> <secret>\n"
525 " Set the secret for the specified domain\n"
526 " DOMAIN\tDomain to set secret for.\n"
527 " secret\tNew secret to set.\n"));
528 return c
->display_usage
?0:-1;
533 ctx
= talloc_new(NULL
);
536 domain
= talloc_strdup(ctx
, argv
[0]);
539 opt
= talloc_asprintf(ctx
, "idmap config %s", domain
);
542 backend
= talloc_strdup(ctx
, lp_parm_const_string(-1, opt
, "backend", "tdb"));
543 ALLOC_CHECK(backend
);
545 if ( ( ! backend
) || ( ! strequal(backend
, "ldap"))) {
547 _("The only currently supported backend is LDAP\n"));
552 dn
= lp_parm_const_string(-1, opt
, "ldap_user_dn", NULL
);
555 _("Missing ldap_user_dn option for domain %s\n"),
561 ret
= idmap_store_secret("ldap", domain
, dn
, secret
);
564 d_fprintf(stderr
, _("Failed to store secret\n"));
569 d_printf(_("Secret stored\n"));
573 static int net_idmap_check(struct net_context
*c
, int argc
, const char **argv
)
576 struct check_options opts
;
578 if ( argc
> 1 || c
->display_usage
) {
581 _("net idmap check [-v] [-r] [-a] [-T] [-f] [-l] [[--db=]<TDB>]\n"
582 " Check an idmap database.\n"
583 " --verbose,-v\tverbose\n"
584 " --repair,-r\trepair\n"
585 " --auto,-a\tnoninteractive mode\n"
586 " --test,-T\tdry run\n"
587 " --fore,-f\tforce\n"
588 " --lock,-l\tlock db while doing the check\n"
589 " TDB\tidmap database\n"));
590 return c
->display_usage
? 0 : -1;
593 dbfile
= (argc
> 0) ? argv
[0] : net_idmap_dbfile(c
);
594 if (dbfile
== NULL
) {
597 d_fprintf(stderr
, _("check database: %s\n"), dbfile
);
599 opts
= (struct check_options
) {
600 .lock
= c
->opt_lock
|| c
->opt_long_list_entries
,
601 .test
= c
->opt_testmode
,
602 .automatic
= c
->opt_auto
,
603 .verbose
= c
->opt_verbose
,
604 .force
= c
->opt_force
,
605 .repair
= c
->opt_repair
|| c
->opt_reboot
,
608 return net_idmap_check_db(dbfile
, &opts
);
611 static int net_idmap_aclmapset(struct net_context
*c
, int argc
, const char **argv
)
615 struct dom_sid src_sid
, dst_sid
;
617 struct db_context
*db
;
618 struct db_record
*rec
;
621 if (argc
!= 3 || c
->display_usage
) {
622 d_fprintf(stderr
, "%s net idmap aclmapset <tdb> "
623 "<src-sid> <dst-sid>\n", _("Usage:"));
627 if (!(mem_ctx
= talloc_init("net idmap aclmapset"))) {
628 d_fprintf(stderr
, _("talloc_init failed\n"));
632 if (!(db
= db_open(mem_ctx
, argv
[0], 0, TDB_DEFAULT
,
633 O_RDWR
|O_CREAT
, 0600,
634 DBWRAP_LOCK_ORDER_1
))) {
635 d_fprintf(stderr
, _("db_open failed: %s\n"), strerror(errno
));
639 if (!string_to_sid(&src_sid
, argv
[1])) {
640 d_fprintf(stderr
, _("%s is not a valid sid\n"), argv
[1]);
644 if (!string_to_sid(&dst_sid
, argv
[2])) {
645 d_fprintf(stderr
, _("%s is not a valid sid\n"), argv
[2]);
649 if (!(src
= sid_string_talloc(mem_ctx
, &src_sid
))
650 || !(dst
= sid_string_talloc(mem_ctx
, &dst_sid
))) {
651 d_fprintf(stderr
, _("talloc_strdup failed\n"));
655 if (!(rec
= dbwrap_fetch_locked(
656 db
, mem_ctx
, string_term_tdb_data(src
)))) {
657 d_fprintf(stderr
, _("could not fetch db record\n"));
661 status
= dbwrap_record_store(rec
, string_term_tdb_data(dst
), 0);
664 if (!NT_STATUS_IS_OK(status
)) {
665 d_fprintf(stderr
, _("could not store record: %s\n"),
672 TALLOC_FREE(mem_ctx
);
676 /***********************************************************
677 Look at the current idmap
678 **********************************************************/
679 int net_idmap(struct net_context
*c
, int argc
, const char **argv
)
681 struct functable func
[] = {
686 N_("Dump the current ID mappings"),
687 N_("net idmap dump\n"
688 " Dump the current ID mappings")
694 N_("Restore entries from stdin"),
695 N_("net idmap restore\n"
696 " Restore entries from stdin")
702 N_("Not implemented yet"),
703 N_("net idmap setmap\n"
704 " Not implemented yet")
710 N_("Delete ID mapping"),
711 N_("net idmap delete <ID>\n"
712 " Delete ID mapping")
718 N_("Set secret for specified domain"),
719 N_("net idmap secret <DOMAIN> <secret>\n"
720 " Set secret for specified domain")
727 N_("net idmap aclmapset\n"
734 N_("Check id mappings"),
735 N_("net idmap check\n"
736 " Check id mappings")
738 {NULL
, NULL
, 0, NULL
, NULL
}
741 return net_run_function(c
, argc
, argv
, "net idmap", func
);