s3:net_idmap_dump add missing braces
[Samba/gebeck_regimport.git] / source3 / utils / net_idmap.c
blob9302be2a40ca645e55d0746405ee0777478aca35
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;
64 printf("%s %s\n", value.dptr, key.dptr);
65 return 0;
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);
75 if (!backend) {
76 backend = lp_idmap_backend();
79 if (c->opt_db != NULL) {
80 dbfile = talloc_strdup(talloc_tos(), c->opt_db);
81 if (dbfile == NULL) {
82 d_fprintf(stderr, _("Out of memory!\n"));
84 } else if (strequal(backend, "tdb")) {
85 dbfile = state_path("winbindd_idmap.tdb");
86 if (dbfile == NULL) {
87 d_fprintf(stderr, _("Out of memory!\n"));
89 } else if (strequal(backend, "tdb2")) {
90 dbfile = talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
91 lp_private_dir());
92 if (dbfile == NULL) {
93 d_fprintf(stderr, _("Out of memory!\n"));
95 } else {
96 char *_backend = talloc_strdup(talloc_tos(), backend);
97 char* args = strchr(_backend, ':');
98 if (args != NULL) {
99 *args = '\0';
102 d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
103 _backend);
105 talloc_free(_backend);
108 return dbfile;
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;
117 TALLOC_CTX *mem_ctx;
118 const char* dbfile;
119 NTSTATUS status;
120 int ret = -1;
122 if ( argc > 1 || c->display_usage) {
123 d_printf("%s\n%s",
124 _("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) {
135 goto done;
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);
141 if (db == NULL) {
142 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
143 dbfile, strerror(errno));
144 goto done;
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"));
150 ret = -1;
151 goto done;
154 ret = 0;
156 done:
157 talloc_free(mem_ctx);
158 return ret;
161 /***********************************************************
162 Write entries from stdin to current local idmap
163 **********************************************************/
165 static int net_idmap_store_id_mapping(struct db_context *db,
166 enum id_type type,
167 unsigned long idval,
168 const char *sid_string)
170 NTSTATUS status;
171 char *idstr = NULL;
173 switch(type) {
174 case ID_TYPE_UID:
175 idstr = talloc_asprintf(talloc_tos(), "UID %lu", idval);
176 break;
177 case ID_TYPE_GID:
178 idstr = talloc_asprintf(talloc_tos(), "GID %lu", idval);
179 break;
180 default:
181 d_fprintf(stderr, "Invalid id mapping type: %d\n", type);
182 return -1;
185 status = dbwrap_store_bystring(db, idstr,
186 string_term_tdb_data(sid_string),
187 TDB_REPLACE);
188 if (!NT_STATUS_IS_OK(status)) {
189 d_fprintf(stderr, "Error storing ID -> SID: "
190 "%s\n", nt_errstr(status));
191 talloc_free(idstr);
192 return -1;
194 status = dbwrap_store_bystring(db, sid_string,
195 string_term_tdb_data(idstr),
196 TDB_REPLACE);
197 if (!NT_STATUS_IS_OK(status)) {
198 d_fprintf(stderr, "Error storing SID -> ID: "
199 "%s\n", nt_errstr(status));
200 talloc_free(idstr);
201 return -1;
204 return 0;
207 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
209 TALLOC_CTX *mem_ctx;
210 FILE *input = NULL;
211 struct db_context *db;
212 const char *dbfile = NULL;
213 int ret = 0;
215 if (c->display_usage) {
216 d_printf("%s\n%s",
217 _("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"));
223 return 0;
226 mem_ctx = talloc_stackframe();
228 dbfile = net_idmap_dbfile(c);
230 if (dbfile == NULL) {
231 ret = -1;
232 goto done;
235 d_fprintf(stderr, _("restoring id mapping to %s\n"), dbfile);
237 if (argc == 1) {
238 input = fopen(argv[0], "r");
239 if (input == NULL) {
240 d_fprintf(stderr, _("Could not open input file (%s): %s\n"),
241 argv[0], strerror(errno));
242 ret = -1;
243 goto done;
245 } else {
246 input = stdin;
249 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644,
250 DBWRAP_LOCK_ORDER_1);
251 if (db == NULL) {
252 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
253 dbfile, strerror(errno));
254 ret = -1;
255 goto done;
258 if (dbwrap_transaction_start(db) != 0) {
259 d_fprintf(stderr, _("Failed to start transaction.\n"));
260 ret = -1;
261 goto done;
264 while (!feof(input)) {
265 char line[128], sid_string[128];
266 int len;
267 unsigned long idval;
268 NTSTATUS status;
270 if (fgets(line, 127, input) == NULL)
271 break;
273 len = strlen(line);
275 if ( (len > 0) && (line[len-1] == '\n') )
276 line[len-1] = '\0';
278 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2)
280 ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
281 idval, sid_string);
282 if (ret != 0) {
283 break;
285 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2)
287 ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
288 idval, sid_string);
289 if (ret != 0) {
290 break;
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)) {
296 d_fprintf(stderr,
297 _("Could not store USER HWM: %s\n"),
298 nt_errstr(status));
299 break;
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)) {
305 d_fprintf(stderr,
306 _("Could not store GROUP HWM: %s\n"),
307 nt_errstr(status));
308 break;
310 } else {
311 d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
312 line);
313 continue;
317 if (ret == 0) {
318 if(dbwrap_transaction_commit(db) != 0) {
319 d_fprintf(stderr, _("Failed to commit transaction.\n"));
320 ret = -1;
322 } else {
323 if (dbwrap_transaction_cancel(db) != 0) {
324 d_fprintf(stderr, _("Failed to cancel transaction.\n"));
328 done:
329 if ((input != NULL) && (input != stdin)) {
330 fclose(input);
333 talloc_free(mem_ctx);
334 return ret;
337 static
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;
342 TDB_DATA key2;
343 bool is_valid_mapping;
344 NTSTATUS status = NT_STATUS_OK;
345 TDB_DATA value;
347 rec1 = dbwrap_fetch_locked(db, mem_ctx, key1);
348 if (rec1 == NULL) {
349 DEBUG(1, ("failed to fetch: %.*s\n", (int)key1.dsize, key1.dptr));
350 status = NT_STATUS_NO_MEMORY;
351 goto done;
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;
357 goto done;
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);
364 if (rec2 == NULL) {
365 DEBUG(1, ("failed to fetch: %.*s\n", (int)key2.dsize, key2.dptr));
366 status = NT_STATUS_NO_MEMORY;
367 goto done;
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 ));
377 if ( !force ) {
378 status = NT_STATUS_FILE_INVALID;
379 goto done;
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));
386 goto done;
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));
395 done:
396 TALLOC_FREE(rec1);
397 TALLOC_FREE(rec2);
398 return status;
401 static
402 NTSTATUS delete_mapping_action(struct db_context *db, void* data)
404 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, false);
406 static
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)
417 if (argc != 1)
418 return false;
419 if (strncmp(argv[0], "S-", 2) == 0)
420 return true;
421 if (strncmp(argv[0], "GID ", 4) == 0)
422 return true;
423 if (strncmp(argv[0], "UID ", 4) == 0)
424 return true;
425 return false;
428 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
430 int ret = -1;
431 struct db_context *db;
432 TALLOC_CTX *mem_ctx;
433 TDB_DATA key;
434 NTSTATUS status;
435 const char* dbfile;
437 if ( !delete_args_ok(argc,argv) || c->display_usage) {
438 d_printf("%s\n%s",
439 _("Usage:"),
440 _("net idmap delete [-f] [--db=<TDB>] <ID>\n"
441 " Delete mapping of ID from TDB.\n"
442 " -f\tforce\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) {
452 goto done;
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);
458 if (db == NULL) {
459 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
460 dbfile, strerror(errno));
461 goto done;
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"),
472 nt_errstr(status));
473 goto done;
475 ret = 0;
476 done:
477 talloc_free(mem_ctx);
478 return ret;
481 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
483 d_printf("%s\n", _("Not implemented yet"));
484 return -1;
486 static bool idmap_store_secret(const char *backend,
487 const char *domain,
488 const char *identity,
489 const char *secret)
491 char *tmp;
492 int r;
493 bool ret;
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)) {
501 free(tmp);
502 return false;
504 ret = secrets_store_generic(tmp, identity, secret);
506 free(tmp);
507 return ret;
511 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
513 TALLOC_CTX *ctx;
514 const char *secret;
515 const char *dn;
516 char *domain;
517 char *backend;
518 char *opt = NULL;
519 bool ret;
521 if (argc != 2 || c->display_usage) {
522 d_printf("%s\n%s",
523 _("Usage:\n"),
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;
531 secret = argv[1];
533 ctx = talloc_new(NULL);
534 ALLOC_CHECK(ctx);
536 domain = talloc_strdup(ctx, argv[0]);
537 ALLOC_CHECK(domain);
539 opt = talloc_asprintf(ctx, "idmap config %s", domain);
540 ALLOC_CHECK(opt);
542 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
543 ALLOC_CHECK(backend);
545 if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
546 d_fprintf(stderr,
547 _("The only currently supported backend is LDAP\n"));
548 talloc_free(ctx);
549 return -1;
552 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
553 if ( ! dn) {
554 d_fprintf(stderr,
555 _("Missing ldap_user_dn option for domain %s\n"),
556 domain);
557 talloc_free(ctx);
558 return -1;
561 ret = idmap_store_secret("ldap", domain, dn, secret);
563 if ( ! ret) {
564 d_fprintf(stderr, _("Failed to store secret\n"));
565 talloc_free(ctx);
566 return -1;
569 d_printf(_("Secret stored\n"));
570 return 0;
573 static int net_idmap_check(struct net_context *c, int argc, const char **argv)
575 const char* dbfile;
576 struct check_options opts;
578 if ( argc > 1 || c->display_usage) {
579 d_printf("%s\n%s",
580 _("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) {
595 return -1;
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)
613 TALLOC_CTX *mem_ctx;
614 int result = -1;
615 struct dom_sid src_sid, dst_sid;
616 char *src, *dst;
617 struct db_context *db;
618 struct db_record *rec;
619 NTSTATUS status;
621 if (argc != 3 || c->display_usage) {
622 d_fprintf(stderr, "%s net idmap aclmapset <tdb> "
623 "<src-sid> <dst-sid>\n", _("Usage:"));
624 return -1;
627 if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
628 d_fprintf(stderr, _("talloc_init failed\n"));
629 return -1;
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));
636 goto fail;
639 if (!string_to_sid(&src_sid, argv[1])) {
640 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
641 goto fail;
644 if (!string_to_sid(&dst_sid, argv[2])) {
645 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
646 goto fail;
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"));
652 goto fail;
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"));
658 goto fail;
661 status = dbwrap_record_store(rec, string_term_tdb_data(dst), 0);
662 TALLOC_FREE(rec);
664 if (!NT_STATUS_IS_OK(status)) {
665 d_fprintf(stderr, _("could not store record: %s\n"),
666 nt_errstr(status));
667 goto fail;
670 result = 0;
671 fail:
672 TALLOC_FREE(mem_ctx);
673 return result;
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[] = {
683 "dump",
684 net_idmap_dump,
685 NET_TRANSPORT_LOCAL,
686 N_("Dump the current ID mappings"),
687 N_("net idmap dump\n"
688 " Dump the current ID mappings")
691 "restore",
692 net_idmap_restore,
693 NET_TRANSPORT_LOCAL,
694 N_("Restore entries from stdin"),
695 N_("net idmap restore\n"
696 " Restore entries from stdin")
699 "setmap",
700 net_idmap_set,
701 NET_TRANSPORT_LOCAL,
702 N_("Not implemented yet"),
703 N_("net idmap setmap\n"
704 " Not implemented yet")
707 "delete",
708 net_idmap_delete,
709 NET_TRANSPORT_LOCAL,
710 N_("Delete ID mapping"),
711 N_("net idmap delete <ID>\n"
712 " Delete ID mapping")
715 "secret",
716 net_idmap_secret,
717 NET_TRANSPORT_LOCAL,
718 N_("Set secret for specified domain"),
719 N_("net idmap secret <DOMAIN> <secret>\n"
720 " Set secret for specified domain")
723 "aclmapset",
724 net_idmap_aclmapset,
725 NET_TRANSPORT_LOCAL,
726 N_("Set acl map"),
727 N_("net idmap aclmapset\n"
728 " Set acl map")
731 "check",
732 net_idmap_check,
733 NET_TRANSPORT_LOCAL,
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);