s4-lsaprc: Don't call lsa_OpenPolicy2 in lsa_LookupNames4.
[Samba/gebeck_regimport.git] / source3 / utils / net_idmap.c
blob28f9ed938eeca636b774adef9ed8b1727abb3dfc
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;
71 if (c->opt_db != NULL) {
72 dbfile = talloc_strdup(talloc_tos(), c->opt_db);
73 if (dbfile == NULL) {
74 d_fprintf(stderr, _("Out of memory!\n"));
76 } else if (strequal(lp_idmap_backend(), "tdb")) {
77 dbfile = state_path("winbindd_idmap.tdb");
78 if (dbfile == NULL) {
79 d_fprintf(stderr, _("Out of memory!\n"));
81 } else if (strequal(lp_idmap_backend(), "tdb2")) {
82 dbfile = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
83 if (dbfile == NULL) {
84 dbfile = talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
85 lp_private_dir());
87 if (dbfile == NULL) {
88 d_fprintf(stderr, _("Out of memory!\n"));
90 } else {
91 char* backend = talloc_strdup(talloc_tos(), lp_idmap_backend());
92 char* args = strchr(backend, ':');
93 if (args != NULL) {
94 *args = '\0';
97 d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
98 backend);
100 talloc_free(backend);
103 return dbfile;
106 /***********************************************************
107 Dump the current idmap
108 **********************************************************/
109 static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
111 struct db_context *db;
112 TALLOC_CTX *mem_ctx;
113 const char* dbfile;
114 NTSTATUS status;
115 int ret = -1;
117 if ( argc > 1 || c->display_usage) {
118 d_printf("%s\n%s",
119 _("Usage:"),
120 _("net idmap dump [[--db=]<inputfile>]\n"
121 " Dump current ID mapping.\n"
122 " inputfile\tTDB file to read mappings from.\n"));
123 return c->display_usage?0:-1;
126 mem_ctx = talloc_stackframe();
128 dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c);
129 if (dbfile == NULL) {
130 goto done;
132 d_fprintf(stderr, _("dumping id mapping from %s\n"), dbfile);
134 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0,
135 DBWRAP_LOCK_ORDER_1);
136 if (db == NULL) {
137 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
138 dbfile, strerror(errno));
139 goto done;
142 status = dbwrap_traverse_read(db, net_idmap_dump_one_entry, NULL, NULL);
143 if (!NT_STATUS_IS_OK(status)) {
144 d_fprintf(stderr, _("error traversing the database\n"));
145 ret = -1;
146 goto done;
149 ret = 0;
151 done:
152 talloc_free(mem_ctx);
153 return ret;
156 /***********************************************************
157 Write entries from stdin to current local idmap
158 **********************************************************/
160 static int net_idmap_store_id_mapping(struct db_context *db,
161 enum id_type type,
162 unsigned long idval,
163 const char *sid_string)
165 NTSTATUS status;
166 char *idstr = NULL;
168 switch(type) {
169 case ID_TYPE_UID:
170 idstr = talloc_asprintf(talloc_tos(), "UID %lu", idval);
171 break;
172 case ID_TYPE_GID:
173 idstr = talloc_asprintf(talloc_tos(), "GID %lu", idval);
174 break;
175 default:
176 d_fprintf(stderr, "Invalid id mapping type: %d\n", type);
177 return -1;
180 status = dbwrap_store_bystring(db, idstr,
181 string_term_tdb_data(sid_string),
182 TDB_REPLACE);
183 if (!NT_STATUS_IS_OK(status)) {
184 d_fprintf(stderr, "Error storing ID -> SID: "
185 "%s\n", nt_errstr(status));
186 talloc_free(idstr);
187 return -1;
189 status = dbwrap_store_bystring(db, sid_string,
190 string_term_tdb_data(idstr),
191 TDB_REPLACE);
192 if (!NT_STATUS_IS_OK(status)) {
193 d_fprintf(stderr, "Error storing SID -> ID: "
194 "%s\n", nt_errstr(status));
195 talloc_free(idstr);
196 return -1;
199 return 0;
202 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
204 TALLOC_CTX *mem_ctx;
205 FILE *input = NULL;
206 struct db_context *db;
207 const char *dbfile = NULL;
208 int ret = 0;
210 if (c->display_usage) {
211 d_printf("%s\n%s",
212 _("Usage:"),
213 _("net idmap restore [--db=<TDB>] [<inputfile>]\n"
214 " Restore ID mappings from file\n"
215 " TDB\tFile to store ID mappings to."
216 " inputfile\tFile to load ID mappings from. If not "
217 "given, load data from stdin.\n"));
218 return 0;
221 mem_ctx = talloc_stackframe();
223 dbfile = net_idmap_dbfile(c);
225 if (dbfile == NULL) {
226 ret = -1;
227 goto done;
230 d_fprintf(stderr, _("restoring id mapping to %s\n"), dbfile);
232 if (argc == 1) {
233 input = fopen(argv[0], "r");
234 if (input == NULL) {
235 d_fprintf(stderr, _("Could not open input file (%s): %s\n"),
236 argv[0], strerror(errno));
237 ret = -1;
238 goto done;
240 } else {
241 input = stdin;
244 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644,
245 DBWRAP_LOCK_ORDER_1);
246 if (db == NULL) {
247 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
248 dbfile, strerror(errno));
249 ret = -1;
250 goto done;
253 if (dbwrap_transaction_start(db) != 0) {
254 d_fprintf(stderr, _("Failed to start transaction.\n"));
255 ret = -1;
256 goto done;
259 while (!feof(input)) {
260 char line[128], sid_string[128];
261 int len;
262 unsigned long idval;
263 NTSTATUS status;
265 if (fgets(line, 127, input) == NULL)
266 break;
268 len = strlen(line);
270 if ( (len > 0) && (line[len-1] == '\n') )
271 line[len-1] = '\0';
273 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2)
275 ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
276 idval, sid_string);
277 if (ret != 0) {
278 break;
280 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2)
282 ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
283 idval, sid_string);
284 if (ret != 0) {
285 break;
287 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
288 status = dbwrap_store_int32_bystring(
289 db, "USER HWM", idval);
290 if (!NT_STATUS_IS_OK(status)) {
291 d_fprintf(stderr,
292 _("Could not store USER HWM: %s\n"),
293 nt_errstr(status));
294 break;
296 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
297 status = dbwrap_store_int32_bystring(
298 db, "GROUP HWM", idval);
299 if (!NT_STATUS_IS_OK(status)) {
300 d_fprintf(stderr,
301 _("Could not store GROUP HWM: %s\n"),
302 nt_errstr(status));
303 break;
305 } else {
306 d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
307 line);
308 continue;
312 if (ret == 0) {
313 if(dbwrap_transaction_commit(db) != 0) {
314 d_fprintf(stderr, _("Failed to commit transaction.\n"));
315 ret = -1;
317 } else {
318 if (dbwrap_transaction_cancel(db) != 0) {
319 d_fprintf(stderr, _("Failed to cancel transaction.\n"));
323 done:
324 if ((input != NULL) && (input != stdin)) {
325 fclose(input);
328 talloc_free(mem_ctx);
329 return ret;
332 static
333 NTSTATUS dbwrap_delete_mapping(struct db_context *db, TDB_DATA key1, bool force)
335 TALLOC_CTX* mem_ctx = talloc_tos();
336 struct db_record *rec1=NULL, *rec2=NULL;
337 TDB_DATA key2;
338 bool is_valid_mapping;
339 NTSTATUS status = NT_STATUS_OK;
340 TDB_DATA value;
342 rec1 = dbwrap_fetch_locked(db, mem_ctx, key1);
343 if (rec1 == NULL) {
344 DEBUG(1, ("failed to fetch: %.*s\n", (int)key1.dsize, key1.dptr));
345 status = NT_STATUS_NO_MEMORY;
346 goto done;
348 key2 = dbwrap_record_get_value(rec1);
349 if (key2.dptr == NULL) {
350 DEBUG(1, ("could not find %.*s\n", (int)key1.dsize, key1.dptr));
351 status = NT_STATUS_NOT_FOUND;
352 goto done;
355 DEBUG(2, ("mapping: %.*s -> %.*s\n",
356 (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr));
358 rec2 = dbwrap_fetch_locked(db, mem_ctx, key2);
359 if (rec2 == NULL) {
360 DEBUG(1, ("failed to fetch: %.*s\n", (int)key2.dsize, key2.dptr));
361 status = NT_STATUS_NO_MEMORY;
362 goto done;
365 value = dbwrap_record_get_value(rec2);
366 is_valid_mapping = tdb_data_equal(key1, value);
368 if (!is_valid_mapping) {
369 DEBUG(1, ("invalid mapping: %.*s -> %.*s -> %.*s\n",
370 (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr,
371 (int)value.dsize, value.dptr ));
372 if ( !force ) {
373 status = NT_STATUS_FILE_INVALID;
374 goto done;
378 status = dbwrap_record_delete(rec1);
379 if (!NT_STATUS_IS_OK(status)) {
380 DEBUG(1, ("failed to delete: %.*s\n", (int)key1.dsize, key1.dptr));
381 goto done;
384 if (is_valid_mapping) {
385 status = dbwrap_record_delete(rec2);
386 if (!NT_STATUS_IS_OK(status)) {
387 DEBUG(1, ("failed to delete: %.*s\n", (int)key2.dsize, key2.dptr));
390 done:
391 TALLOC_FREE(rec1);
392 TALLOC_FREE(rec2);
393 return status;
396 static
397 NTSTATUS delete_mapping_action(struct db_context *db, void* data)
399 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, false);
401 static
402 NTSTATUS delete_mapping_action_force(struct db_context *db, void* data)
404 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, true);
407 /***********************************************************
408 Delete a SID mapping from a winbindd_idmap.tdb
409 **********************************************************/
410 static bool delete_args_ok(int argc, const char **argv)
412 if (argc != 1)
413 return false;
414 if (strncmp(argv[0], "S-", 2) == 0)
415 return true;
416 if (strncmp(argv[0], "GID ", 4) == 0)
417 return true;
418 if (strncmp(argv[0], "UID ", 4) == 0)
419 return true;
420 return false;
423 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
425 int ret = -1;
426 struct db_context *db;
427 TALLOC_CTX *mem_ctx;
428 TDB_DATA key;
429 NTSTATUS status;
430 const char* dbfile;
432 if ( !delete_args_ok(argc,argv) || c->display_usage) {
433 d_printf("%s\n%s",
434 _("Usage:"),
435 _("net idmap delete [-f] [--db=<TDB>] <ID>\n"
436 " Delete mapping of ID from TDB.\n"
437 " -f\tforce\n"
438 " TDB\tidmap database\n"
439 " ID\tSID|GID|UID\n"));
440 return c->display_usage ? 0 : -1;
443 mem_ctx = talloc_stackframe();
445 dbfile = net_idmap_dbfile(c);
446 if (dbfile == NULL) {
447 goto done;
449 d_fprintf(stderr, _("deleting id mapping from %s\n"), dbfile);
451 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR, 0,
452 DBWRAP_LOCK_ORDER_1);
453 if (db == NULL) {
454 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
455 dbfile, strerror(errno));
456 goto done;
459 key = string_term_tdb_data(argv[0]);
461 status = dbwrap_trans_do(db, (c->opt_force
462 ? delete_mapping_action_force
463 : delete_mapping_action), &key);
465 if (!NT_STATUS_IS_OK(status)) {
466 d_fprintf(stderr, _("could not delete mapping: %s\n"),
467 nt_errstr(status));
468 goto done;
470 ret = 0;
471 done:
472 talloc_free(mem_ctx);
473 return ret;
476 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
478 d_printf("%s\n", _("Not implemented yet"));
479 return -1;
481 static bool idmap_store_secret(const char *backend,
482 const char *domain,
483 const char *identity,
484 const char *secret)
486 char *tmp;
487 int r;
488 bool ret;
490 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
492 if (r < 0) return false;
494 strupper_m(tmp); /* make sure the key is case insensitive */
495 ret = secrets_store_generic(tmp, identity, secret);
497 free(tmp);
498 return ret;
502 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
504 TALLOC_CTX *ctx;
505 const char *secret;
506 const char *dn;
507 char *domain;
508 char *backend;
509 char *opt = NULL;
510 bool ret;
512 if (argc != 2 || c->display_usage) {
513 d_printf("%s\n%s",
514 _("Usage:\n"),
515 _("net idmap secret <DOMAIN> <secret>\n"
516 " Set the secret for the specified domain\n"
517 " DOMAIN\tDomain to set secret for.\n"
518 " secret\tNew secret to set.\n"));
519 return c->display_usage?0:-1;
522 secret = argv[1];
524 ctx = talloc_new(NULL);
525 ALLOC_CHECK(ctx);
527 domain = talloc_strdup(ctx, argv[0]);
528 ALLOC_CHECK(domain);
530 opt = talloc_asprintf(ctx, "idmap config %s", domain);
531 ALLOC_CHECK(opt);
533 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
534 ALLOC_CHECK(backend);
536 if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
537 d_fprintf(stderr,
538 _("The only currently supported backend is LDAP\n"));
539 talloc_free(ctx);
540 return -1;
543 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
544 if ( ! dn) {
545 d_fprintf(stderr,
546 _("Missing ldap_user_dn option for domain %s\n"),
547 domain);
548 talloc_free(ctx);
549 return -1;
552 ret = idmap_store_secret("ldap", domain, dn, secret);
554 if ( ! ret) {
555 d_fprintf(stderr, _("Failed to store secret\n"));
556 talloc_free(ctx);
557 return -1;
560 d_printf(_("Secret stored\n"));
561 return 0;
564 static int net_idmap_check(struct net_context *c, int argc, const char **argv)
566 const char* dbfile;
567 struct check_options opts;
569 if ( argc > 1 || c->display_usage) {
570 d_printf("%s\n%s",
571 _("Usage:"),
572 _("net idmap check [-v] [-r] [-a] [-T] [-f] [-l] [[--db=]<TDB>]\n"
573 " Check an idmap database.\n"
574 " --verbose,-v\tverbose\n"
575 " --repair,-r\trepair\n"
576 " --auto,-a\tnoninteractive mode\n"
577 " --test,-T\tdry run\n"
578 " --fore,-f\tforce\n"
579 " --lock,-l\tlock db while doing the check\n"
580 " TDB\tidmap database\n"));
581 return c->display_usage ? 0 : -1;
584 dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c);
585 if (dbfile == NULL) {
586 return -1;
588 d_fprintf(stderr, _("check database: %s\n"), dbfile);
590 opts = (struct check_options) {
591 .lock = c->opt_lock || c->opt_long_list_entries,
592 .test = c->opt_testmode,
593 .automatic = c->opt_auto,
594 .verbose = c->opt_verbose,
595 .force = c->opt_force,
596 .repair = c->opt_repair || c->opt_reboot,
599 return net_idmap_check_db(dbfile, &opts);
602 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
604 TALLOC_CTX *mem_ctx;
605 int result = -1;
606 struct dom_sid src_sid, dst_sid;
607 char *src, *dst;
608 struct db_context *db;
609 struct db_record *rec;
610 NTSTATUS status;
612 if (argc != 3 || c->display_usage) {
613 d_fprintf(stderr, "%s net idmap aclmapset <tdb> "
614 "<src-sid> <dst-sid>\n", _("Usage:"));
615 return -1;
618 if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
619 d_fprintf(stderr, _("talloc_init failed\n"));
620 return -1;
623 if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
624 O_RDWR|O_CREAT, 0600,
625 DBWRAP_LOCK_ORDER_1))) {
626 d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
627 goto fail;
630 if (!string_to_sid(&src_sid, argv[1])) {
631 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
632 goto fail;
635 if (!string_to_sid(&dst_sid, argv[2])) {
636 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
637 goto fail;
640 if (!(src = sid_string_talloc(mem_ctx, &src_sid))
641 || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
642 d_fprintf(stderr, _("talloc_strdup failed\n"));
643 goto fail;
646 if (!(rec = dbwrap_fetch_locked(
647 db, mem_ctx, string_term_tdb_data(src)))) {
648 d_fprintf(stderr, _("could not fetch db record\n"));
649 goto fail;
652 status = dbwrap_record_store(rec, string_term_tdb_data(dst), 0);
653 TALLOC_FREE(rec);
655 if (!NT_STATUS_IS_OK(status)) {
656 d_fprintf(stderr, _("could not store record: %s\n"),
657 nt_errstr(status));
658 goto fail;
661 result = 0;
662 fail:
663 TALLOC_FREE(mem_ctx);
664 return result;
667 /***********************************************************
668 Look at the current idmap
669 **********************************************************/
670 int net_idmap(struct net_context *c, int argc, const char **argv)
672 struct functable func[] = {
674 "dump",
675 net_idmap_dump,
676 NET_TRANSPORT_LOCAL,
677 N_("Dump the current ID mappings"),
678 N_("net idmap dump\n"
679 " Dump the current ID mappings")
682 "restore",
683 net_idmap_restore,
684 NET_TRANSPORT_LOCAL,
685 N_("Restore entries from stdin"),
686 N_("net idmap restore\n"
687 " Restore entries from stdin")
690 "setmap",
691 net_idmap_set,
692 NET_TRANSPORT_LOCAL,
693 N_("Not implemented yet"),
694 N_("net idmap setmap\n"
695 " Not implemented yet")
698 "delete",
699 net_idmap_delete,
700 NET_TRANSPORT_LOCAL,
701 N_("Delete ID mapping"),
702 N_("net idmap delete <ID>\n"
703 " Delete ID mapping")
706 "secret",
707 net_idmap_secret,
708 NET_TRANSPORT_LOCAL,
709 N_("Set secret for specified domain"),
710 N_("net idmap secret <DOMAIN> <secret>\n"
711 " Set secret for specified domain")
714 "aclmapset",
715 net_idmap_aclmapset,
716 NET_TRANSPORT_LOCAL,
717 N_("Set acl map"),
718 N_("net idmap aclmapset\n"
719 " Set acl map")
722 "check",
723 net_idmap_check,
724 NET_TRANSPORT_LOCAL,
725 N_("Check id mappings"),
726 N_("net idmap check\n"
727 " Check id mappings")
729 {NULL, NULL, 0, NULL, NULL}
732 return net_run_function(c, argc, argv, "net idmap", func);