s3:net_idmap_dump deal with idmap config * : backend config style
[Samba/gebeck_regimport.git] / source3 / utils / net_idmap.c
blob6e6f2055969f029b88799ed11875de04e5159fe0
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 = lp_parm_talloc_string(talloc_tos(),
90 -1, "tdb", "idmap2.tdb", NULL);
91 if (dbfile == NULL) {
92 dbfile = talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
93 lp_private_dir());
95 if (dbfile == NULL) {
96 d_fprintf(stderr, _("Out of memory!\n"));
98 } else {
99 char *_backend = talloc_strdup(talloc_tos(), backend);
100 char* args = strchr(_backend, ':');
101 if (args != NULL) {
102 *args = '\0';
105 d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
106 _backend);
108 talloc_free(_backend);
111 return dbfile;
114 /***********************************************************
115 Dump the current idmap
116 **********************************************************/
117 static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
119 struct db_context *db;
120 TALLOC_CTX *mem_ctx;
121 const char* dbfile;
122 NTSTATUS status;
123 int ret = -1;
125 if ( argc > 1 || c->display_usage) {
126 d_printf("%s\n%s",
127 _("Usage:"),
128 _("net idmap dump [[--db=]<inputfile>]\n"
129 " Dump current ID mapping.\n"
130 " inputfile\tTDB file to read mappings from.\n"));
131 return c->display_usage?0:-1;
134 mem_ctx = talloc_stackframe();
136 dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c);
137 if (dbfile == NULL) {
138 goto done;
140 d_fprintf(stderr, _("dumping id mapping from %s\n"), dbfile);
142 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0,
143 DBWRAP_LOCK_ORDER_1);
144 if (db == NULL) {
145 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
146 dbfile, strerror(errno));
147 goto done;
150 status = dbwrap_traverse_read(db, net_idmap_dump_one_entry, NULL, NULL);
151 if (!NT_STATUS_IS_OK(status)) {
152 d_fprintf(stderr, _("error traversing the database\n"));
153 ret = -1;
154 goto done;
157 ret = 0;
159 done:
160 talloc_free(mem_ctx);
161 return ret;
164 /***********************************************************
165 Write entries from stdin to current local idmap
166 **********************************************************/
168 static int net_idmap_store_id_mapping(struct db_context *db,
169 enum id_type type,
170 unsigned long idval,
171 const char *sid_string)
173 NTSTATUS status;
174 char *idstr = NULL;
176 switch(type) {
177 case ID_TYPE_UID:
178 idstr = talloc_asprintf(talloc_tos(), "UID %lu", idval);
179 break;
180 case ID_TYPE_GID:
181 idstr = talloc_asprintf(talloc_tos(), "GID %lu", idval);
182 break;
183 default:
184 d_fprintf(stderr, "Invalid id mapping type: %d\n", type);
185 return -1;
188 status = dbwrap_store_bystring(db, idstr,
189 string_term_tdb_data(sid_string),
190 TDB_REPLACE);
191 if (!NT_STATUS_IS_OK(status)) {
192 d_fprintf(stderr, "Error storing ID -> SID: "
193 "%s\n", nt_errstr(status));
194 talloc_free(idstr);
195 return -1;
197 status = dbwrap_store_bystring(db, sid_string,
198 string_term_tdb_data(idstr),
199 TDB_REPLACE);
200 if (!NT_STATUS_IS_OK(status)) {
201 d_fprintf(stderr, "Error storing SID -> ID: "
202 "%s\n", nt_errstr(status));
203 talloc_free(idstr);
204 return -1;
207 return 0;
210 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
212 TALLOC_CTX *mem_ctx;
213 FILE *input = NULL;
214 struct db_context *db;
215 const char *dbfile = NULL;
216 int ret = 0;
218 if (c->display_usage) {
219 d_printf("%s\n%s",
220 _("Usage:"),
221 _("net idmap restore [--db=<TDB>] [<inputfile>]\n"
222 " Restore ID mappings from file\n"
223 " TDB\tFile to store ID mappings to."
224 " inputfile\tFile to load ID mappings from. If not "
225 "given, load data from stdin.\n"));
226 return 0;
229 mem_ctx = talloc_stackframe();
231 dbfile = net_idmap_dbfile(c);
233 if (dbfile == NULL) {
234 ret = -1;
235 goto done;
238 d_fprintf(stderr, _("restoring id mapping to %s\n"), dbfile);
240 if (argc == 1) {
241 input = fopen(argv[0], "r");
242 if (input == NULL) {
243 d_fprintf(stderr, _("Could not open input file (%s): %s\n"),
244 argv[0], strerror(errno));
245 ret = -1;
246 goto done;
248 } else {
249 input = stdin;
252 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644,
253 DBWRAP_LOCK_ORDER_1);
254 if (db == NULL) {
255 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
256 dbfile, strerror(errno));
257 ret = -1;
258 goto done;
261 if (dbwrap_transaction_start(db) != 0) {
262 d_fprintf(stderr, _("Failed to start transaction.\n"));
263 ret = -1;
264 goto done;
267 while (!feof(input)) {
268 char line[128], sid_string[128];
269 int len;
270 unsigned long idval;
271 NTSTATUS status;
273 if (fgets(line, 127, input) == NULL)
274 break;
276 len = strlen(line);
278 if ( (len > 0) && (line[len-1] == '\n') )
279 line[len-1] = '\0';
281 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2)
283 ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
284 idval, sid_string);
285 if (ret != 0) {
286 break;
288 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2)
290 ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
291 idval, sid_string);
292 if (ret != 0) {
293 break;
295 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
296 status = dbwrap_store_int32_bystring(
297 db, "USER HWM", idval);
298 if (!NT_STATUS_IS_OK(status)) {
299 d_fprintf(stderr,
300 _("Could not store USER HWM: %s\n"),
301 nt_errstr(status));
302 break;
304 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
305 status = dbwrap_store_int32_bystring(
306 db, "GROUP HWM", idval);
307 if (!NT_STATUS_IS_OK(status)) {
308 d_fprintf(stderr,
309 _("Could not store GROUP HWM: %s\n"),
310 nt_errstr(status));
311 break;
313 } else {
314 d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
315 line);
316 continue;
320 if (ret == 0) {
321 if(dbwrap_transaction_commit(db) != 0) {
322 d_fprintf(stderr, _("Failed to commit transaction.\n"));
323 ret = -1;
325 } else {
326 if (dbwrap_transaction_cancel(db) != 0) {
327 d_fprintf(stderr, _("Failed to cancel transaction.\n"));
331 done:
332 if ((input != NULL) && (input != stdin)) {
333 fclose(input);
336 talloc_free(mem_ctx);
337 return ret;
340 static
341 NTSTATUS dbwrap_delete_mapping(struct db_context *db, TDB_DATA key1, bool force)
343 TALLOC_CTX* mem_ctx = talloc_tos();
344 struct db_record *rec1=NULL, *rec2=NULL;
345 TDB_DATA key2;
346 bool is_valid_mapping;
347 NTSTATUS status = NT_STATUS_OK;
348 TDB_DATA value;
350 rec1 = dbwrap_fetch_locked(db, mem_ctx, key1);
351 if (rec1 == NULL) {
352 DEBUG(1, ("failed to fetch: %.*s\n", (int)key1.dsize, key1.dptr));
353 status = NT_STATUS_NO_MEMORY;
354 goto done;
356 key2 = dbwrap_record_get_value(rec1);
357 if (key2.dptr == NULL) {
358 DEBUG(1, ("could not find %.*s\n", (int)key1.dsize, key1.dptr));
359 status = NT_STATUS_NOT_FOUND;
360 goto done;
363 DEBUG(2, ("mapping: %.*s -> %.*s\n",
364 (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr));
366 rec2 = dbwrap_fetch_locked(db, mem_ctx, key2);
367 if (rec2 == NULL) {
368 DEBUG(1, ("failed to fetch: %.*s\n", (int)key2.dsize, key2.dptr));
369 status = NT_STATUS_NO_MEMORY;
370 goto done;
373 value = dbwrap_record_get_value(rec2);
374 is_valid_mapping = tdb_data_equal(key1, value);
376 if (!is_valid_mapping) {
377 DEBUG(1, ("invalid mapping: %.*s -> %.*s -> %.*s\n",
378 (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr,
379 (int)value.dsize, value.dptr ));
380 if ( !force ) {
381 status = NT_STATUS_FILE_INVALID;
382 goto done;
386 status = dbwrap_record_delete(rec1);
387 if (!NT_STATUS_IS_OK(status)) {
388 DEBUG(1, ("failed to delete: %.*s\n", (int)key1.dsize, key1.dptr));
389 goto done;
392 if (is_valid_mapping) {
393 status = dbwrap_record_delete(rec2);
394 if (!NT_STATUS_IS_OK(status)) {
395 DEBUG(1, ("failed to delete: %.*s\n", (int)key2.dsize, key2.dptr));
398 done:
399 TALLOC_FREE(rec1);
400 TALLOC_FREE(rec2);
401 return status;
404 static
405 NTSTATUS delete_mapping_action(struct db_context *db, void* data)
407 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, false);
409 static
410 NTSTATUS delete_mapping_action_force(struct db_context *db, void* data)
412 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, true);
415 /***********************************************************
416 Delete a SID mapping from a winbindd_idmap.tdb
417 **********************************************************/
418 static bool delete_args_ok(int argc, const char **argv)
420 if (argc != 1)
421 return false;
422 if (strncmp(argv[0], "S-", 2) == 0)
423 return true;
424 if (strncmp(argv[0], "GID ", 4) == 0)
425 return true;
426 if (strncmp(argv[0], "UID ", 4) == 0)
427 return true;
428 return false;
431 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
433 int ret = -1;
434 struct db_context *db;
435 TALLOC_CTX *mem_ctx;
436 TDB_DATA key;
437 NTSTATUS status;
438 const char* dbfile;
440 if ( !delete_args_ok(argc,argv) || c->display_usage) {
441 d_printf("%s\n%s",
442 _("Usage:"),
443 _("net idmap delete [-f] [--db=<TDB>] <ID>\n"
444 " Delete mapping of ID from TDB.\n"
445 " -f\tforce\n"
446 " TDB\tidmap database\n"
447 " ID\tSID|GID|UID\n"));
448 return c->display_usage ? 0 : -1;
451 mem_ctx = talloc_stackframe();
453 dbfile = net_idmap_dbfile(c);
454 if (dbfile == NULL) {
455 goto done;
457 d_fprintf(stderr, _("deleting id mapping from %s\n"), dbfile);
459 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR, 0,
460 DBWRAP_LOCK_ORDER_1);
461 if (db == NULL) {
462 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
463 dbfile, strerror(errno));
464 goto done;
467 key = string_term_tdb_data(argv[0]);
469 status = dbwrap_trans_do(db, (c->opt_force
470 ? delete_mapping_action_force
471 : delete_mapping_action), &key);
473 if (!NT_STATUS_IS_OK(status)) {
474 d_fprintf(stderr, _("could not delete mapping: %s\n"),
475 nt_errstr(status));
476 goto done;
478 ret = 0;
479 done:
480 talloc_free(mem_ctx);
481 return ret;
484 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
486 d_printf("%s\n", _("Not implemented yet"));
487 return -1;
489 static bool idmap_store_secret(const char *backend,
490 const char *domain,
491 const char *identity,
492 const char *secret)
494 char *tmp;
495 int r;
496 bool ret;
498 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
500 if (r < 0) return false;
502 /* make sure the key is case insensitive */
503 if (!strupper_m(tmp)) {
504 free(tmp);
505 return false;
507 ret = secrets_store_generic(tmp, identity, secret);
509 free(tmp);
510 return ret;
514 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
516 TALLOC_CTX *ctx;
517 const char *secret;
518 const char *dn;
519 char *domain;
520 char *backend;
521 char *opt = NULL;
522 bool ret;
524 if (argc != 2 || c->display_usage) {
525 d_printf("%s\n%s",
526 _("Usage:\n"),
527 _("net idmap secret <DOMAIN> <secret>\n"
528 " Set the secret for the specified domain\n"
529 " DOMAIN\tDomain to set secret for.\n"
530 " secret\tNew secret to set.\n"));
531 return c->display_usage?0:-1;
534 secret = argv[1];
536 ctx = talloc_new(NULL);
537 ALLOC_CHECK(ctx);
539 domain = talloc_strdup(ctx, argv[0]);
540 ALLOC_CHECK(domain);
542 opt = talloc_asprintf(ctx, "idmap config %s", domain);
543 ALLOC_CHECK(opt);
545 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
546 ALLOC_CHECK(backend);
548 if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
549 d_fprintf(stderr,
550 _("The only currently supported backend is LDAP\n"));
551 talloc_free(ctx);
552 return -1;
555 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
556 if ( ! dn) {
557 d_fprintf(stderr,
558 _("Missing ldap_user_dn option for domain %s\n"),
559 domain);
560 talloc_free(ctx);
561 return -1;
564 ret = idmap_store_secret("ldap", domain, dn, secret);
566 if ( ! ret) {
567 d_fprintf(stderr, _("Failed to store secret\n"));
568 talloc_free(ctx);
569 return -1;
572 d_printf(_("Secret stored\n"));
573 return 0;
576 static int net_idmap_check(struct net_context *c, int argc, const char **argv)
578 const char* dbfile;
579 struct check_options opts;
581 if ( argc > 1 || c->display_usage) {
582 d_printf("%s\n%s",
583 _("Usage:"),
584 _("net idmap check [-v] [-r] [-a] [-T] [-f] [-l] [[--db=]<TDB>]\n"
585 " Check an idmap database.\n"
586 " --verbose,-v\tverbose\n"
587 " --repair,-r\trepair\n"
588 " --auto,-a\tnoninteractive mode\n"
589 " --test,-T\tdry run\n"
590 " --fore,-f\tforce\n"
591 " --lock,-l\tlock db while doing the check\n"
592 " TDB\tidmap database\n"));
593 return c->display_usage ? 0 : -1;
596 dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c);
597 if (dbfile == NULL) {
598 return -1;
600 d_fprintf(stderr, _("check database: %s\n"), dbfile);
602 opts = (struct check_options) {
603 .lock = c->opt_lock || c->opt_long_list_entries,
604 .test = c->opt_testmode,
605 .automatic = c->opt_auto,
606 .verbose = c->opt_verbose,
607 .force = c->opt_force,
608 .repair = c->opt_repair || c->opt_reboot,
611 return net_idmap_check_db(dbfile, &opts);
614 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
616 TALLOC_CTX *mem_ctx;
617 int result = -1;
618 struct dom_sid src_sid, dst_sid;
619 char *src, *dst;
620 struct db_context *db;
621 struct db_record *rec;
622 NTSTATUS status;
624 if (argc != 3 || c->display_usage) {
625 d_fprintf(stderr, "%s net idmap aclmapset <tdb> "
626 "<src-sid> <dst-sid>\n", _("Usage:"));
627 return -1;
630 if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
631 d_fprintf(stderr, _("talloc_init failed\n"));
632 return -1;
635 if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
636 O_RDWR|O_CREAT, 0600,
637 DBWRAP_LOCK_ORDER_1))) {
638 d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
639 goto fail;
642 if (!string_to_sid(&src_sid, argv[1])) {
643 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
644 goto fail;
647 if (!string_to_sid(&dst_sid, argv[2])) {
648 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
649 goto fail;
652 if (!(src = sid_string_talloc(mem_ctx, &src_sid))
653 || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
654 d_fprintf(stderr, _("talloc_strdup failed\n"));
655 goto fail;
658 if (!(rec = dbwrap_fetch_locked(
659 db, mem_ctx, string_term_tdb_data(src)))) {
660 d_fprintf(stderr, _("could not fetch db record\n"));
661 goto fail;
664 status = dbwrap_record_store(rec, string_term_tdb_data(dst), 0);
665 TALLOC_FREE(rec);
667 if (!NT_STATUS_IS_OK(status)) {
668 d_fprintf(stderr, _("could not store record: %s\n"),
669 nt_errstr(status));
670 goto fail;
673 result = 0;
674 fail:
675 TALLOC_FREE(mem_ctx);
676 return result;
679 /***********************************************************
680 Look at the current idmap
681 **********************************************************/
682 int net_idmap(struct net_context *c, int argc, const char **argv)
684 struct functable func[] = {
686 "dump",
687 net_idmap_dump,
688 NET_TRANSPORT_LOCAL,
689 N_("Dump the current ID mappings"),
690 N_("net idmap dump\n"
691 " Dump the current ID mappings")
694 "restore",
695 net_idmap_restore,
696 NET_TRANSPORT_LOCAL,
697 N_("Restore entries from stdin"),
698 N_("net idmap restore\n"
699 " Restore entries from stdin")
702 "setmap",
703 net_idmap_set,
704 NET_TRANSPORT_LOCAL,
705 N_("Not implemented yet"),
706 N_("net idmap setmap\n"
707 " Not implemented yet")
710 "delete",
711 net_idmap_delete,
712 NET_TRANSPORT_LOCAL,
713 N_("Delete ID mapping"),
714 N_("net idmap delete <ID>\n"
715 " Delete ID mapping")
718 "secret",
719 net_idmap_secret,
720 NET_TRANSPORT_LOCAL,
721 N_("Set secret for specified domain"),
722 N_("net idmap secret <DOMAIN> <secret>\n"
723 " Set secret for specified domain")
726 "aclmapset",
727 net_idmap_aclmapset,
728 NET_TRANSPORT_LOCAL,
729 N_("Set acl map"),
730 N_("net idmap aclmapset\n"
731 " Set acl map")
734 "check",
735 net_idmap_check,
736 NET_TRANSPORT_LOCAL,
737 N_("Check id mappings"),
738 N_("net idmap check\n"
739 " Check id mappings")
741 {NULL, NULL, 0, NULL, NULL}
744 return net_run_function(c, argc, argv, "net idmap", func);