s3:net_idmap_dump remove obsolete support for tdb:idmap2.tdb parameter
[Samba/gebeck_regimport.git] / source3 / utils / net_idmap.c
blob5be57cc9223f30611830471b3a35ce2fd5f97883
1 /*
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/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "utils/net.h"
23 #include "secrets.h"
24 #include "idmap.h"
25 #include "dbwrap/dbwrap.h"
26 #include "dbwrap/dbwrap_open.h"
27 #include "../libcli/security/security.h"
28 #include "net_idmap_check.h"
29 #include "util_tdb.h"
31 #define ALLOC_CHECK(mem) do { \
32 if (!mem) { \
33 d_fprintf(stderr, _("Out of memory!\n")); \
34 talloc_free(ctx); \
35 return -1; \
36 } } while(0)
38 /***********************************************************
39 Helper function for net_idmap_dump. Dump one entry.
40 **********************************************************/
41 static int net_idmap_dump_one_entry(struct db_record *rec,
42 void *unused)
44 TDB_DATA key;
45 TDB_DATA value;
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));
52 return 0;
55 if (strcmp((char *)key.dptr, "GROUP HWM") == 0) {
56 printf(_("GROUP HWM %d\n"), IVAL(value.dptr,0));
57 return 0;
60 if (strncmp((char *)key.dptr, "S-", 2) != 0)
61 return 0;
63 printf("%s %s\n", value.dptr, key.dptr);
64 return 0;
67 static const char* net_idmap_dbfile(struct net_context *c)
69 const char* dbfile = NULL;
70 const char *backend = NULL;
72 /* prefer idmap config * : backend over idmap backend parameter */
73 backend = lp_parm_const_string(-1, "idmap config *", "backend", NULL);
74 if (!backend) {
75 backend = lp_idmap_backend();
78 if (c->opt_db != NULL) {
79 dbfile = talloc_strdup(talloc_tos(), c->opt_db);
80 if (dbfile == NULL) {
81 d_fprintf(stderr, _("Out of memory!\n"));
83 } else if (strequal(backend, "tdb")) {
84 dbfile = state_path("winbindd_idmap.tdb");
85 if (dbfile == NULL) {
86 d_fprintf(stderr, _("Out of memory!\n"));
88 } else if (strequal(backend, "tdb2")) {
89 dbfile = talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
90 lp_private_dir());
91 if (dbfile == NULL) {
92 d_fprintf(stderr, _("Out of memory!\n"));
94 } else {
95 char *_backend = talloc_strdup(talloc_tos(), backend);
96 char* args = strchr(_backend, ':');
97 if (args != NULL) {
98 *args = '\0';
101 d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
102 _backend);
104 talloc_free(_backend);
107 return dbfile;
110 /***********************************************************
111 Dump the current idmap
112 **********************************************************/
113 static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
115 struct db_context *db;
116 TALLOC_CTX *mem_ctx;
117 const char* dbfile;
118 NTSTATUS status;
119 int ret = -1;
121 if ( argc > 1 || c->display_usage) {
122 d_printf("%s\n%s",
123 _("Usage:"),
124 _("net idmap dump [[--db=]<inputfile>]\n"
125 " Dump current ID mapping.\n"
126 " inputfile\tTDB file to read mappings from.\n"));
127 return c->display_usage?0:-1;
130 mem_ctx = talloc_stackframe();
132 dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c);
133 if (dbfile == NULL) {
134 goto done;
136 d_fprintf(stderr, _("dumping id mapping from %s\n"), dbfile);
138 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0,
139 DBWRAP_LOCK_ORDER_1);
140 if (db == NULL) {
141 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
142 dbfile, strerror(errno));
143 goto done;
146 status = dbwrap_traverse_read(db, net_idmap_dump_one_entry, NULL, NULL);
147 if (!NT_STATUS_IS_OK(status)) {
148 d_fprintf(stderr, _("error traversing the database\n"));
149 ret = -1;
150 goto done;
153 ret = 0;
155 done:
156 talloc_free(mem_ctx);
157 return ret;
160 /***********************************************************
161 Write entries from stdin to current local idmap
162 **********************************************************/
164 static int net_idmap_store_id_mapping(struct db_context *db,
165 enum id_type type,
166 unsigned long idval,
167 const char *sid_string)
169 NTSTATUS status;
170 char *idstr = NULL;
172 switch(type) {
173 case ID_TYPE_UID:
174 idstr = talloc_asprintf(talloc_tos(), "UID %lu", idval);
175 break;
176 case ID_TYPE_GID:
177 idstr = talloc_asprintf(talloc_tos(), "GID %lu", idval);
178 break;
179 default:
180 d_fprintf(stderr, "Invalid id mapping type: %d\n", type);
181 return -1;
184 status = dbwrap_store_bystring(db, idstr,
185 string_term_tdb_data(sid_string),
186 TDB_REPLACE);
187 if (!NT_STATUS_IS_OK(status)) {
188 d_fprintf(stderr, "Error storing ID -> SID: "
189 "%s\n", nt_errstr(status));
190 talloc_free(idstr);
191 return -1;
193 status = dbwrap_store_bystring(db, sid_string,
194 string_term_tdb_data(idstr),
195 TDB_REPLACE);
196 if (!NT_STATUS_IS_OK(status)) {
197 d_fprintf(stderr, "Error storing SID -> ID: "
198 "%s\n", nt_errstr(status));
199 talloc_free(idstr);
200 return -1;
203 return 0;
206 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
208 TALLOC_CTX *mem_ctx;
209 FILE *input = NULL;
210 struct db_context *db;
211 const char *dbfile = NULL;
212 int ret = 0;
214 if (c->display_usage) {
215 d_printf("%s\n%s",
216 _("Usage:"),
217 _("net idmap restore [--db=<TDB>] [<inputfile>]\n"
218 " Restore ID mappings from file\n"
219 " TDB\tFile to store ID mappings to."
220 " inputfile\tFile to load ID mappings from. If not "
221 "given, load data from stdin.\n"));
222 return 0;
225 mem_ctx = talloc_stackframe();
227 dbfile = net_idmap_dbfile(c);
229 if (dbfile == NULL) {
230 ret = -1;
231 goto done;
234 d_fprintf(stderr, _("restoring id mapping to %s\n"), dbfile);
236 if (argc == 1) {
237 input = fopen(argv[0], "r");
238 if (input == NULL) {
239 d_fprintf(stderr, _("Could not open input file (%s): %s\n"),
240 argv[0], strerror(errno));
241 ret = -1;
242 goto done;
244 } else {
245 input = stdin;
248 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644,
249 DBWRAP_LOCK_ORDER_1);
250 if (db == NULL) {
251 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
252 dbfile, strerror(errno));
253 ret = -1;
254 goto done;
257 if (dbwrap_transaction_start(db) != 0) {
258 d_fprintf(stderr, _("Failed to start transaction.\n"));
259 ret = -1;
260 goto done;
263 while (!feof(input)) {
264 char line[128], sid_string[128];
265 int len;
266 unsigned long idval;
267 NTSTATUS status;
269 if (fgets(line, 127, input) == NULL)
270 break;
272 len = strlen(line);
274 if ( (len > 0) && (line[len-1] == '\n') )
275 line[len-1] = '\0';
277 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2)
279 ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
280 idval, sid_string);
281 if (ret != 0) {
282 break;
284 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2)
286 ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
287 idval, sid_string);
288 if (ret != 0) {
289 break;
291 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
292 status = dbwrap_store_int32_bystring(
293 db, "USER HWM", idval);
294 if (!NT_STATUS_IS_OK(status)) {
295 d_fprintf(stderr,
296 _("Could not store USER HWM: %s\n"),
297 nt_errstr(status));
298 break;
300 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
301 status = dbwrap_store_int32_bystring(
302 db, "GROUP HWM", idval);
303 if (!NT_STATUS_IS_OK(status)) {
304 d_fprintf(stderr,
305 _("Could not store GROUP HWM: %s\n"),
306 nt_errstr(status));
307 break;
309 } else {
310 d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
311 line);
312 continue;
316 if (ret == 0) {
317 if(dbwrap_transaction_commit(db) != 0) {
318 d_fprintf(stderr, _("Failed to commit transaction.\n"));
319 ret = -1;
321 } else {
322 if (dbwrap_transaction_cancel(db) != 0) {
323 d_fprintf(stderr, _("Failed to cancel transaction.\n"));
327 done:
328 if ((input != NULL) && (input != stdin)) {
329 fclose(input);
332 talloc_free(mem_ctx);
333 return ret;
336 static
337 NTSTATUS dbwrap_delete_mapping(struct db_context *db, TDB_DATA key1, bool force)
339 TALLOC_CTX* mem_ctx = talloc_tos();
340 struct db_record *rec1=NULL, *rec2=NULL;
341 TDB_DATA key2;
342 bool is_valid_mapping;
343 NTSTATUS status = NT_STATUS_OK;
344 TDB_DATA value;
346 rec1 = dbwrap_fetch_locked(db, mem_ctx, key1);
347 if (rec1 == NULL) {
348 DEBUG(1, ("failed to fetch: %.*s\n", (int)key1.dsize, key1.dptr));
349 status = NT_STATUS_NO_MEMORY;
350 goto done;
352 key2 = dbwrap_record_get_value(rec1);
353 if (key2.dptr == NULL) {
354 DEBUG(1, ("could not find %.*s\n", (int)key1.dsize, key1.dptr));
355 status = NT_STATUS_NOT_FOUND;
356 goto done;
359 DEBUG(2, ("mapping: %.*s -> %.*s\n",
360 (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr));
362 rec2 = dbwrap_fetch_locked(db, mem_ctx, key2);
363 if (rec2 == NULL) {
364 DEBUG(1, ("failed to fetch: %.*s\n", (int)key2.dsize, key2.dptr));
365 status = NT_STATUS_NO_MEMORY;
366 goto done;
369 value = dbwrap_record_get_value(rec2);
370 is_valid_mapping = tdb_data_equal(key1, value);
372 if (!is_valid_mapping) {
373 DEBUG(1, ("invalid mapping: %.*s -> %.*s -> %.*s\n",
374 (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr,
375 (int)value.dsize, value.dptr ));
376 if ( !force ) {
377 status = NT_STATUS_FILE_INVALID;
378 goto done;
382 status = dbwrap_record_delete(rec1);
383 if (!NT_STATUS_IS_OK(status)) {
384 DEBUG(1, ("failed to delete: %.*s\n", (int)key1.dsize, key1.dptr));
385 goto done;
388 if (is_valid_mapping) {
389 status = dbwrap_record_delete(rec2);
390 if (!NT_STATUS_IS_OK(status)) {
391 DEBUG(1, ("failed to delete: %.*s\n", (int)key2.dsize, key2.dptr));
394 done:
395 TALLOC_FREE(rec1);
396 TALLOC_FREE(rec2);
397 return status;
400 static
401 NTSTATUS delete_mapping_action(struct db_context *db, void* data)
403 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, false);
405 static
406 NTSTATUS delete_mapping_action_force(struct db_context *db, void* data)
408 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, true);
411 /***********************************************************
412 Delete a SID mapping from a winbindd_idmap.tdb
413 **********************************************************/
414 static bool delete_args_ok(int argc, const char **argv)
416 if (argc != 1)
417 return false;
418 if (strncmp(argv[0], "S-", 2) == 0)
419 return true;
420 if (strncmp(argv[0], "GID ", 4) == 0)
421 return true;
422 if (strncmp(argv[0], "UID ", 4) == 0)
423 return true;
424 return false;
427 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
429 int ret = -1;
430 struct db_context *db;
431 TALLOC_CTX *mem_ctx;
432 TDB_DATA key;
433 NTSTATUS status;
434 const char* dbfile;
436 if ( !delete_args_ok(argc,argv) || c->display_usage) {
437 d_printf("%s\n%s",
438 _("Usage:"),
439 _("net idmap delete [-f] [--db=<TDB>] <ID>\n"
440 " Delete mapping of ID from TDB.\n"
441 " -f\tforce\n"
442 " TDB\tidmap database\n"
443 " ID\tSID|GID|UID\n"));
444 return c->display_usage ? 0 : -1;
447 mem_ctx = talloc_stackframe();
449 dbfile = net_idmap_dbfile(c);
450 if (dbfile == NULL) {
451 goto done;
453 d_fprintf(stderr, _("deleting id mapping from %s\n"), dbfile);
455 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR, 0,
456 DBWRAP_LOCK_ORDER_1);
457 if (db == NULL) {
458 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
459 dbfile, strerror(errno));
460 goto done;
463 key = string_term_tdb_data(argv[0]);
465 status = dbwrap_trans_do(db, (c->opt_force
466 ? delete_mapping_action_force
467 : delete_mapping_action), &key);
469 if (!NT_STATUS_IS_OK(status)) {
470 d_fprintf(stderr, _("could not delete mapping: %s\n"),
471 nt_errstr(status));
472 goto done;
474 ret = 0;
475 done:
476 talloc_free(mem_ctx);
477 return ret;
480 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
482 d_printf("%s\n", _("Not implemented yet"));
483 return -1;
485 static bool idmap_store_secret(const char *backend,
486 const char *domain,
487 const char *identity,
488 const char *secret)
490 char *tmp;
491 int r;
492 bool ret;
494 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
496 if (r < 0) return false;
498 /* make sure the key is case insensitive */
499 if (!strupper_m(tmp)) {
500 free(tmp);
501 return false;
503 ret = secrets_store_generic(tmp, identity, secret);
505 free(tmp);
506 return ret;
510 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
512 TALLOC_CTX *ctx;
513 const char *secret;
514 const char *dn;
515 char *domain;
516 char *backend;
517 char *opt = NULL;
518 bool ret;
520 if (argc != 2 || c->display_usage) {
521 d_printf("%s\n%s",
522 _("Usage:\n"),
523 _("net idmap secret <DOMAIN> <secret>\n"
524 " Set the secret for the specified domain\n"
525 " DOMAIN\tDomain to set secret for.\n"
526 " secret\tNew secret to set.\n"));
527 return c->display_usage?0:-1;
530 secret = argv[1];
532 ctx = talloc_new(NULL);
533 ALLOC_CHECK(ctx);
535 domain = talloc_strdup(ctx, argv[0]);
536 ALLOC_CHECK(domain);
538 opt = talloc_asprintf(ctx, "idmap config %s", domain);
539 ALLOC_CHECK(opt);
541 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
542 ALLOC_CHECK(backend);
544 if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
545 d_fprintf(stderr,
546 _("The only currently supported backend is LDAP\n"));
547 talloc_free(ctx);
548 return -1;
551 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
552 if ( ! dn) {
553 d_fprintf(stderr,
554 _("Missing ldap_user_dn option for domain %s\n"),
555 domain);
556 talloc_free(ctx);
557 return -1;
560 ret = idmap_store_secret("ldap", domain, dn, secret);
562 if ( ! ret) {
563 d_fprintf(stderr, _("Failed to store secret\n"));
564 talloc_free(ctx);
565 return -1;
568 d_printf(_("Secret stored\n"));
569 return 0;
572 static int net_idmap_check(struct net_context *c, int argc, const char **argv)
574 const char* dbfile;
575 struct check_options opts;
577 if ( argc > 1 || c->display_usage) {
578 d_printf("%s\n%s",
579 _("Usage:"),
580 _("net idmap check [-v] [-r] [-a] [-T] [-f] [-l] [[--db=]<TDB>]\n"
581 " Check an idmap database.\n"
582 " --verbose,-v\tverbose\n"
583 " --repair,-r\trepair\n"
584 " --auto,-a\tnoninteractive mode\n"
585 " --test,-T\tdry run\n"
586 " --fore,-f\tforce\n"
587 " --lock,-l\tlock db while doing the check\n"
588 " TDB\tidmap database\n"));
589 return c->display_usage ? 0 : -1;
592 dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c);
593 if (dbfile == NULL) {
594 return -1;
596 d_fprintf(stderr, _("check database: %s\n"), dbfile);
598 opts = (struct check_options) {
599 .lock = c->opt_lock || c->opt_long_list_entries,
600 .test = c->opt_testmode,
601 .automatic = c->opt_auto,
602 .verbose = c->opt_verbose,
603 .force = c->opt_force,
604 .repair = c->opt_repair || c->opt_reboot,
607 return net_idmap_check_db(dbfile, &opts);
610 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
612 TALLOC_CTX *mem_ctx;
613 int result = -1;
614 struct dom_sid src_sid, dst_sid;
615 char *src, *dst;
616 struct db_context *db;
617 struct db_record *rec;
618 NTSTATUS status;
620 if (argc != 3 || c->display_usage) {
621 d_fprintf(stderr, "%s net idmap aclmapset <tdb> "
622 "<src-sid> <dst-sid>\n", _("Usage:"));
623 return -1;
626 if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
627 d_fprintf(stderr, _("talloc_init failed\n"));
628 return -1;
631 if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
632 O_RDWR|O_CREAT, 0600,
633 DBWRAP_LOCK_ORDER_1))) {
634 d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
635 goto fail;
638 if (!string_to_sid(&src_sid, argv[1])) {
639 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
640 goto fail;
643 if (!string_to_sid(&dst_sid, argv[2])) {
644 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
645 goto fail;
648 if (!(src = sid_string_talloc(mem_ctx, &src_sid))
649 || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
650 d_fprintf(stderr, _("talloc_strdup failed\n"));
651 goto fail;
654 if (!(rec = dbwrap_fetch_locked(
655 db, mem_ctx, string_term_tdb_data(src)))) {
656 d_fprintf(stderr, _("could not fetch db record\n"));
657 goto fail;
660 status = dbwrap_record_store(rec, string_term_tdb_data(dst), 0);
661 TALLOC_FREE(rec);
663 if (!NT_STATUS_IS_OK(status)) {
664 d_fprintf(stderr, _("could not store record: %s\n"),
665 nt_errstr(status));
666 goto fail;
669 result = 0;
670 fail:
671 TALLOC_FREE(mem_ctx);
672 return result;
675 /***********************************************************
676 Look at the current idmap
677 **********************************************************/
678 int net_idmap(struct net_context *c, int argc, const char **argv)
680 struct functable func[] = {
682 "dump",
683 net_idmap_dump,
684 NET_TRANSPORT_LOCAL,
685 N_("Dump the current ID mappings"),
686 N_("net idmap dump\n"
687 " Dump the current ID mappings")
690 "restore",
691 net_idmap_restore,
692 NET_TRANSPORT_LOCAL,
693 N_("Restore entries from stdin"),
694 N_("net idmap restore\n"
695 " Restore entries from stdin")
698 "setmap",
699 net_idmap_set,
700 NET_TRANSPORT_LOCAL,
701 N_("Not implemented yet"),
702 N_("net idmap setmap\n"
703 " Not implemented yet")
706 "delete",
707 net_idmap_delete,
708 NET_TRANSPORT_LOCAL,
709 N_("Delete ID mapping"),
710 N_("net idmap delete <ID>\n"
711 " Delete ID mapping")
714 "secret",
715 net_idmap_secret,
716 NET_TRANSPORT_LOCAL,
717 N_("Set secret for specified domain"),
718 N_("net idmap secret <DOMAIN> <secret>\n"
719 " Set secret for specified domain")
722 "aclmapset",
723 net_idmap_aclmapset,
724 NET_TRANSPORT_LOCAL,
725 N_("Set acl map"),
726 N_("net idmap aclmapset\n"
727 " Set acl map")
730 "check",
731 net_idmap_check,
732 NET_TRANSPORT_LOCAL,
733 N_("Check id mappings"),
734 N_("net idmap check\n"
735 " Check id mappings")
737 {NULL, NULL, 0, NULL, NULL}
740 return net_run_function(c, argc, argv, "net idmap", func);