s3:net_idmap_delete do not lock two records at the same time
[Samba/gebeck_regimport.git] / source3 / utils / net_idmap.c
blob1fb7be03cf9689f7c1bd3a04988f0f2b7a774604
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 enum idmap_dump_backend {
39 TDB,
40 AUTORID
43 struct idmap_dump_ctx {
44 enum idmap_dump_backend backend;
47 static int net_idmap_dump_one_autorid_entry(struct db_record *rec,
48 void *unused)
50 TDB_DATA key;
51 TDB_DATA value;
53 key = dbwrap_record_get_key(rec);
54 value = dbwrap_record_get_value(rec);
56 if (strncmp((char *)key.dptr, "CONFIG", 6) == 0) {
57 char *config = talloc_array(talloc_tos(), char, value.dsize+1);
58 memcpy(config, value.dptr, value.dsize);
59 config[value.dsize] = '\0';
60 printf("CONFIG: %s\n", config);
61 talloc_free(config);
62 return 0;
65 if (strncmp((char *)key.dptr, "NEXT RANGE", 10) == 0) {
66 printf("RANGE HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
67 return 0;
70 if (strncmp((char *)key.dptr, "NEXT ALLOC UID", 14) == 0) {
71 printf("UID HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
72 return 0;
75 if (strncmp((char *)key.dptr, "NEXT ALLOC GID", 14) == 0) {
76 printf("GID HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
77 return 0;
80 if (strncmp((char *)key.dptr, "UID", 3) == 0 ||
81 strncmp((char *)key.dptr, "GID", 3) == 0)
83 /* mapped entry from allocation pool */
84 printf("%s %s\n", value.dptr, key.dptr);
85 return 0;
88 if ((strncmp((char *)key.dptr, "S-1-5-", 6) == 0 ||
89 strncmp((char *)key.dptr, "ALLOC", 5) == 0) &&
90 value.dsize == sizeof(uint32_t))
92 /* this is a domain range assignment */
93 uint32_t range = IVAL(value.dptr, 0);
94 printf("RANGE %"PRIu32": %s\n", range, key.dptr);
95 return 0;
98 return 0;
101 /***********************************************************
102 Helper function for net_idmap_dump. Dump one entry.
103 **********************************************************/
104 static int net_idmap_dump_one_tdb_entry(struct db_record *rec,
105 void *unused)
107 TDB_DATA key;
108 TDB_DATA value;
110 key = dbwrap_record_get_key(rec);
111 value = dbwrap_record_get_value(rec);
113 if (strcmp((char *)key.dptr, "USER HWM") == 0) {
114 printf(_("USER HWM %d\n"), IVAL(value.dptr,0));
115 return 0;
118 if (strcmp((char *)key.dptr, "GROUP HWM") == 0) {
119 printf(_("GROUP HWM %d\n"), IVAL(value.dptr,0));
120 return 0;
123 if (strncmp((char *)key.dptr, "S-", 2) != 0) {
124 return 0;
127 printf("%s %s\n", value.dptr, key.dptr);
128 return 0;
131 static const char* net_idmap_dbfile(struct net_context *c,
132 struct idmap_dump_ctx *ctx)
134 const char* dbfile = NULL;
135 const char *backend = NULL;
137 backend = lp_idmap_default_backend();
138 if (!backend) {
139 d_printf(_("Internal error: 'idmap config * : backend' is not set!\n"));
140 return NULL;
143 if (c->opt_db != NULL) {
144 dbfile = talloc_strdup(talloc_tos(), c->opt_db);
145 if (dbfile == NULL) {
146 d_fprintf(stderr, _("Out of memory!\n"));
148 } else if (strequal(backend, "tdb")) {
149 dbfile = state_path("winbindd_idmap.tdb");
150 if (dbfile == NULL) {
151 d_fprintf(stderr, _("Out of memory!\n"));
153 ctx->backend = TDB;
154 } else if (strequal(backend, "tdb2")) {
155 dbfile = talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
156 lp_private_dir());
157 if (dbfile == NULL) {
158 d_fprintf(stderr, _("Out of memory!\n"));
160 ctx->backend = TDB;
161 } else if (strequal(backend, "autorid")) {
162 dbfile = state_path("autorid.tdb");
163 if (dbfile == NULL) {
164 d_fprintf(stderr, _("Out of memory!\n"));
166 ctx->backend = AUTORID;
167 } else {
168 char *_backend = talloc_strdup(talloc_tos(), backend);
169 char* args = strchr(_backend, ':');
170 if (args != NULL) {
171 *args = '\0';
174 d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
175 _backend);
177 talloc_free(_backend);
180 return dbfile;
183 /***********************************************************
184 Dump the current idmap
185 **********************************************************/
186 static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
188 struct db_context *db;
189 TALLOC_CTX *mem_ctx;
190 const char* dbfile;
191 NTSTATUS status;
192 int ret = -1;
193 struct idmap_dump_ctx ctx = { .backend = TDB };
195 if ( argc > 1 || c->display_usage) {
196 d_printf("%s\n%s",
197 _("Usage:"),
198 _("net idmap dump [[--db=]<inputfile>]\n"
199 " Dump current ID mapping.\n"
200 " inputfile\tTDB file to read mappings from.\n"));
201 return c->display_usage?0:-1;
204 mem_ctx = talloc_stackframe();
206 dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c, &ctx);
207 if (dbfile == NULL) {
208 goto done;
210 d_fprintf(stderr, _("dumping id mapping from %s\n"), dbfile);
212 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0,
213 DBWRAP_LOCK_ORDER_1);
214 if (db == NULL) {
215 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
216 dbfile, strerror(errno));
217 goto done;
220 if (ctx.backend == AUTORID) {
221 status = dbwrap_traverse_read(db,
222 net_idmap_dump_one_autorid_entry,
223 NULL, NULL);
224 } else {
225 status = dbwrap_traverse_read(db,
226 net_idmap_dump_one_tdb_entry,
227 NULL, NULL);
229 if (!NT_STATUS_IS_OK(status)) {
230 d_fprintf(stderr, _("error traversing the database\n"));
231 ret = -1;
232 goto done;
235 ret = 0;
237 done:
238 talloc_free(mem_ctx);
239 return ret;
242 /***********************************************************
243 Write entries from stdin to current local idmap
244 **********************************************************/
246 static int net_idmap_store_id_mapping(struct db_context *db,
247 enum id_type type,
248 unsigned long idval,
249 const char *sid_string)
251 NTSTATUS status;
252 char *idstr = NULL;
254 switch(type) {
255 case ID_TYPE_UID:
256 idstr = talloc_asprintf(talloc_tos(), "UID %lu", idval);
257 break;
258 case ID_TYPE_GID:
259 idstr = talloc_asprintf(talloc_tos(), "GID %lu", idval);
260 break;
261 default:
262 d_fprintf(stderr, "Invalid id mapping type: %d\n", type);
263 return -1;
266 status = dbwrap_store_bystring(db, idstr,
267 string_term_tdb_data(sid_string),
268 TDB_REPLACE);
269 if (!NT_STATUS_IS_OK(status)) {
270 d_fprintf(stderr, "Error storing ID -> SID: "
271 "%s\n", nt_errstr(status));
272 talloc_free(idstr);
273 return -1;
275 status = dbwrap_store_bystring(db, sid_string,
276 string_term_tdb_data(idstr),
277 TDB_REPLACE);
278 if (!NT_STATUS_IS_OK(status)) {
279 d_fprintf(stderr, "Error storing SID -> ID: "
280 "%s\n", nt_errstr(status));
281 talloc_free(idstr);
282 return -1;
285 return 0;
288 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
290 TALLOC_CTX *mem_ctx;
291 FILE *input = NULL;
292 struct db_context *db;
293 const char *dbfile = NULL;
294 int ret = 0;
295 struct idmap_dump_ctx ctx = { .backend = TDB };
297 if (c->display_usage) {
298 d_printf("%s\n%s",
299 _("Usage:"),
300 _("net idmap restore [--db=<TDB>] [<inputfile>]\n"
301 " Restore ID mappings from file\n"
302 " TDB\tFile to store ID mappings to."
303 " inputfile\tFile to load ID mappings from. If not "
304 "given, load data from stdin.\n"));
305 return 0;
308 mem_ctx = talloc_stackframe();
310 dbfile = net_idmap_dbfile(c, &ctx);
312 if (dbfile == NULL) {
313 ret = -1;
314 goto done;
317 if (ctx.backend != TDB) {
318 d_fprintf(stderr, _("Sorry, restoring of non-TDB databases is "
319 "currently not supported\n"));
320 ret = -1;
321 goto done;
324 d_fprintf(stderr, _("restoring id mapping to %s\n"), dbfile);
326 if (argc == 1) {
327 input = fopen(argv[0], "r");
328 if (input == NULL) {
329 d_fprintf(stderr, _("Could not open input file (%s): %s\n"),
330 argv[0], strerror(errno));
331 ret = -1;
332 goto done;
334 } else {
335 input = stdin;
338 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644,
339 DBWRAP_LOCK_ORDER_1);
340 if (db == NULL) {
341 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
342 dbfile, strerror(errno));
343 ret = -1;
344 goto done;
347 if (dbwrap_transaction_start(db) != 0) {
348 d_fprintf(stderr, _("Failed to start transaction.\n"));
349 ret = -1;
350 goto done;
353 while (!feof(input)) {
354 char line[128], sid_string[128];
355 int len;
356 unsigned long idval;
357 NTSTATUS status;
359 if (fgets(line, 127, input) == NULL)
360 break;
362 len = strlen(line);
364 if ( (len > 0) && (line[len-1] == '\n') )
365 line[len-1] = '\0';
367 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2)
369 ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
370 idval, sid_string);
371 if (ret != 0) {
372 break;
374 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2)
376 ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
377 idval, sid_string);
378 if (ret != 0) {
379 break;
381 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
382 status = dbwrap_store_int32_bystring(
383 db, "USER HWM", idval);
384 if (!NT_STATUS_IS_OK(status)) {
385 d_fprintf(stderr,
386 _("Could not store USER HWM: %s\n"),
387 nt_errstr(status));
388 break;
390 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
391 status = dbwrap_store_int32_bystring(
392 db, "GROUP HWM", idval);
393 if (!NT_STATUS_IS_OK(status)) {
394 d_fprintf(stderr,
395 _("Could not store GROUP HWM: %s\n"),
396 nt_errstr(status));
397 break;
399 } else {
400 d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
401 line);
402 continue;
406 if (ret == 0) {
407 if(dbwrap_transaction_commit(db) != 0) {
408 d_fprintf(stderr, _("Failed to commit transaction.\n"));
409 ret = -1;
411 } else {
412 if (dbwrap_transaction_cancel(db) != 0) {
413 d_fprintf(stderr, _("Failed to cancel transaction.\n"));
417 done:
418 if ((input != NULL) && (input != stdin)) {
419 fclose(input);
422 talloc_free(mem_ctx);
423 return ret;
426 static
427 NTSTATUS dbwrap_delete_mapping(struct db_context *db, TDB_DATA key1, bool force)
429 TALLOC_CTX* mem_ctx = talloc_tos();
430 bool is_valid_mapping;
431 NTSTATUS status = NT_STATUS_OK;
432 TDB_DATA val1, val2;
434 ZERO_STRUCT(val1);
435 ZERO_STRUCT(val2);
437 status = dbwrap_fetch(db, mem_ctx, key1, &val1);
438 if (!NT_STATUS_IS_OK(status)) {
439 DEBUG(1, ("failed to fetch: %.*s\n", (int)key1.dsize, key1.dptr));
440 goto done;
443 if (val1.dptr == NULL) {
444 DEBUG(1, ("invalid mapping: %.*s -> empty value\n",
445 (int)key1.dsize, key1.dptr));
446 status = NT_STATUS_FILE_INVALID;
447 goto done;
450 DEBUG(2, ("mapping: %.*s -> %.*s\n",
451 (int)key1.dsize, key1.dptr, (int)val1.dsize, val1.dptr));
453 status = dbwrap_fetch(db, mem_ctx, val1, &val2);
454 if (!NT_STATUS_IS_OK(status)) {
455 DEBUG(1, ("failed to fetch: %.*s\n", (int)val1.dsize, val1.dptr));
456 goto done;
459 is_valid_mapping = tdb_data_equal(key1, val2);
461 if (!is_valid_mapping) {
462 DEBUG(1, ("invalid mapping: %.*s -> %.*s -> %.*s\n",
463 (int)key1.dsize, key1.dptr,
464 (int)val1.dsize, val1.dptr,
465 (int)val2.dsize, val2.dptr));
466 if ( !force ) {
467 status = NT_STATUS_FILE_INVALID;
468 goto done;
472 status = dbwrap_delete(db, key1);
473 if (!NT_STATUS_IS_OK(status)) {
474 DEBUG(1, ("failed to delete: %.*s\n", (int)key1.dsize, key1.dptr));
475 goto done;
478 if (is_valid_mapping) {
479 status = dbwrap_delete(db, val1);
480 if (!NT_STATUS_IS_OK(status)) {
481 DEBUG(1, ("failed to delete: %.*s\n", (int)val1.dsize, val1.dptr));
485 done:
486 TALLOC_FREE(val1.dptr);
487 TALLOC_FREE(val2.dptr);
488 return status;
491 static
492 NTSTATUS delete_mapping_action(struct db_context *db, void* data)
494 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, false);
496 static
497 NTSTATUS delete_mapping_action_force(struct db_context *db, void* data)
499 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, true);
502 /***********************************************************
503 Delete a SID mapping from a winbindd_idmap.tdb
504 **********************************************************/
505 static bool delete_args_ok(int argc, const char **argv)
507 if (argc != 1)
508 return false;
509 if (strncmp(argv[0], "S-", 2) == 0)
510 return true;
511 if (strncmp(argv[0], "GID ", 4) == 0)
512 return true;
513 if (strncmp(argv[0], "UID ", 4) == 0)
514 return true;
515 return false;
518 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
520 int ret = -1;
521 struct db_context *db;
522 TALLOC_CTX *mem_ctx;
523 TDB_DATA key;
524 NTSTATUS status;
525 const char* dbfile;
526 struct idmap_dump_ctx ctx = { .backend = TDB };
528 if ( !delete_args_ok(argc,argv) || c->display_usage) {
529 d_printf("%s\n%s",
530 _("Usage:"),
531 _("net idmap delete [-f] [--db=<TDB>] <ID>\n"
532 " Delete mapping of ID from TDB.\n"
533 " -f\tforce\n"
534 " TDB\tidmap database\n"
535 " ID\tSID|GID|UID\n"));
536 return c->display_usage ? 0 : -1;
539 mem_ctx = talloc_stackframe();
541 dbfile = net_idmap_dbfile(c, &ctx);
542 if (dbfile == NULL) {
543 goto done;
545 d_fprintf(stderr, _("deleting id mapping from %s\n"), dbfile);
547 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR, 0,
548 DBWRAP_LOCK_ORDER_1);
549 if (db == NULL) {
550 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
551 dbfile, strerror(errno));
552 goto done;
555 key = string_term_tdb_data(argv[0]);
557 status = dbwrap_trans_do(db, (c->opt_force
558 ? delete_mapping_action_force
559 : delete_mapping_action), &key);
561 if (!NT_STATUS_IS_OK(status)) {
562 d_fprintf(stderr, _("could not delete mapping: %s\n"),
563 nt_errstr(status));
564 goto done;
566 ret = 0;
567 done:
568 talloc_free(mem_ctx);
569 return ret;
572 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
574 d_printf("%s\n", _("Not implemented yet"));
575 return -1;
577 static bool idmap_store_secret(const char *backend,
578 const char *domain,
579 const char *identity,
580 const char *secret)
582 char *tmp;
583 int r;
584 bool ret;
586 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
588 if (r < 0) return false;
590 /* make sure the key is case insensitive */
591 if (!strupper_m(tmp)) {
592 free(tmp);
593 return false;
595 ret = secrets_store_generic(tmp, identity, secret);
597 free(tmp);
598 return ret;
602 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
604 TALLOC_CTX *ctx;
605 const char *secret;
606 const char *dn;
607 char *domain;
608 char *backend;
609 char *opt = NULL;
610 bool ret;
612 if (argc != 2 || c->display_usage) {
613 d_printf("%s\n%s",
614 _("Usage:\n"),
615 _("net idmap secret <DOMAIN> <secret>\n"
616 " Set the secret for the specified domain\n"
617 " DOMAIN\tDomain to set secret for.\n"
618 " secret\tNew secret to set.\n"));
619 return c->display_usage?0:-1;
622 secret = argv[1];
624 ctx = talloc_new(NULL);
625 ALLOC_CHECK(ctx);
627 domain = talloc_strdup(ctx, argv[0]);
628 ALLOC_CHECK(domain);
630 opt = talloc_asprintf(ctx, "idmap config %s", domain);
631 ALLOC_CHECK(opt);
633 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
634 ALLOC_CHECK(backend);
636 if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
637 d_fprintf(stderr,
638 _("The only currently supported backend is LDAP\n"));
639 talloc_free(ctx);
640 return -1;
643 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
644 if ( ! dn) {
645 d_fprintf(stderr,
646 _("Missing ldap_user_dn option for domain %s\n"),
647 domain);
648 talloc_free(ctx);
649 return -1;
652 ret = idmap_store_secret("ldap", domain, dn, secret);
654 if ( ! ret) {
655 d_fprintf(stderr, _("Failed to store secret\n"));
656 talloc_free(ctx);
657 return -1;
660 d_printf(_("Secret stored\n"));
661 return 0;
664 static int net_idmap_check(struct net_context *c, int argc, const char **argv)
666 const char* dbfile;
667 struct check_options opts;
668 struct idmap_dump_ctx ctx = { .backend = TDB };
670 if ( argc > 1 || c->display_usage) {
671 d_printf("%s\n%s",
672 _("Usage:"),
673 _("net idmap check [-v] [-r] [-a] [-T] [-f] [-l] [[--db=]<TDB>]\n"
674 " Check an idmap database.\n"
675 " --verbose,-v\tverbose\n"
676 " --repair,-r\trepair\n"
677 " --auto,-a\tnoninteractive mode\n"
678 " --test,-T\tdry run\n"
679 " --fore,-f\tforce\n"
680 " --lock,-l\tlock db while doing the check\n"
681 " TDB\tidmap database\n"));
682 return c->display_usage ? 0 : -1;
685 dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c, &ctx);
686 if (dbfile == NULL) {
687 return -1;
690 if (ctx.backend != TDB) {
691 d_fprintf(stderr, _("Sorry, checking of non-TDB databases is "
692 "currently not supported\n"));
693 return -1;
696 d_fprintf(stderr, _("check database: %s\n"), dbfile);
698 opts = (struct check_options) {
699 .lock = c->opt_lock || c->opt_long_list_entries,
700 .test = c->opt_testmode,
701 .automatic = c->opt_auto,
702 .verbose = c->opt_verbose,
703 .force = c->opt_force,
704 .repair = c->opt_repair || c->opt_reboot,
707 return net_idmap_check_db(dbfile, &opts);
710 /***********************************************************
711 Look at the current idmap
712 **********************************************************/
713 int net_idmap(struct net_context *c, int argc, const char **argv)
715 struct functable func[] = {
717 "dump",
718 net_idmap_dump,
719 NET_TRANSPORT_LOCAL,
720 N_("Dump the current ID mappings"),
721 N_("net idmap dump\n"
722 " Dump the current ID mappings")
725 "restore",
726 net_idmap_restore,
727 NET_TRANSPORT_LOCAL,
728 N_("Restore entries from stdin"),
729 N_("net idmap restore\n"
730 " Restore entries from stdin")
733 "setmap",
734 net_idmap_set,
735 NET_TRANSPORT_LOCAL,
736 N_("Not implemented yet"),
737 N_("net idmap setmap\n"
738 " Not implemented yet")
741 "delete",
742 net_idmap_delete,
743 NET_TRANSPORT_LOCAL,
744 N_("Delete ID mapping"),
745 N_("net idmap delete <ID>\n"
746 " Delete ID mapping")
749 "secret",
750 net_idmap_secret,
751 NET_TRANSPORT_LOCAL,
752 N_("Set secret for specified domain"),
753 N_("net idmap secret <DOMAIN> <secret>\n"
754 " Set secret for specified domain")
757 "check",
758 net_idmap_check,
759 NET_TRANSPORT_LOCAL,
760 N_("Check id mappings"),
761 N_("net idmap check\n"
762 " Check id mappings")
764 {NULL, NULL, 0, NULL, NULL}
767 return net_run_function(c, argc, argv, "net idmap", func);