smbtorture Remove random file name before we start RW2
[Samba.git] / source3 / utils / net_idmap.c
blobe7bac53e5fb408dddabaecaf0a712457f4576d85
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 #define FOO(x) (x)
21 #include "includes.h"
22 #include "utils/net.h"
23 #include "secrets.h"
24 #include "idmap.h"
25 #include "dbwrap.h"
26 #include "../libcli/security/security.h"
28 #define ALLOC_CHECK(mem) do { \
29 if (!mem) { \
30 d_fprintf(stderr, _("Out of memory!\n")); \
31 talloc_free(ctx); \
32 return -1; \
33 } } while(0)
35 /***********************************************************
36 Helper function for net_idmap_dump. Dump one entry.
37 **********************************************************/
38 static int net_idmap_dump_one_entry(struct db_record *rec,
39 void *unused)
41 if (strcmp((char *)rec->key.dptr, "USER HWM") == 0) {
42 printf(_("USER HWM %d\n"), IVAL(rec->value.dptr,0));
43 return 0;
46 if (strcmp((char *)rec->key.dptr, "GROUP HWM") == 0) {
47 printf(_("GROUP HWM %d\n"), IVAL(rec->value.dptr,0));
48 return 0;
51 if (strncmp((char *)rec->key.dptr, "S-", 2) != 0)
52 return 0;
54 printf("%s %s\n", rec->value.dptr, rec->key.dptr);
55 return 0;
58 static const char* net_idmap_dbfile(struct net_context *c)
60 const char* dbfile = NULL;
62 if (c->opt_db != NULL) {
63 dbfile = talloc_strdup(talloc_tos(), c->opt_db);
64 if (dbfile == NULL) {
65 d_fprintf(stderr, _("Out of memory!\n"));
67 } else if (strequal(lp_idmap_backend(), "tdb")) {
68 dbfile = state_path("winbindd_idmap.tdb");
69 if (dbfile == NULL) {
70 d_fprintf(stderr, _("Out of memory!\n"));
72 } else if (strequal(lp_idmap_backend(), "tdb2")) {
73 dbfile = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
74 if (dbfile == NULL) {
75 dbfile = talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
76 lp_private_dir());
78 if (dbfile == NULL) {
79 d_fprintf(stderr, _("Out of memory!\n"));
81 } else {
82 char* backend = talloc_strdup(talloc_tos(), lp_idmap_backend());
83 char* args = strchr(backend, ':');
84 if (args != NULL) {
85 *args = '\0';
88 d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
89 backend);
91 talloc_free(backend);
94 return dbfile;
97 /***********************************************************
98 Dump the current idmap
99 **********************************************************/
100 static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
102 struct db_context *db;
103 TALLOC_CTX *mem_ctx;
104 const char* dbfile;
105 int ret = -1;
107 if ( argc > 1 || c->display_usage) {
108 d_printf("%s\n%s",
109 _("Usage:"),
110 _("net idmap dump [[--db=]<inputfile>]\n"
111 " Dump current ID mapping.\n"
112 " inputfile\tTDB file to read mappings from.\n"));
113 return c->display_usage?0:-1;
116 mem_ctx = talloc_stackframe();
118 dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c);
119 if (dbfile == NULL) {
120 goto done;
122 d_fprintf(stderr, _("dumping id mapping from %s\n"), dbfile);
124 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0);
125 if (db == NULL) {
126 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
127 dbfile, strerror(errno));
128 goto done;
131 db->traverse_read(db, net_idmap_dump_one_entry, NULL);
132 ret = 0;
134 done:
135 talloc_free(mem_ctx);
136 return ret;
139 /***********************************************************
140 Write entries from stdin to current local idmap
141 **********************************************************/
143 static int net_idmap_store_id_mapping(struct db_context *db,
144 enum id_type type,
145 unsigned long idval,
146 const char *sid_string)
148 NTSTATUS status;
149 char *idstr = NULL;
151 switch(type) {
152 case ID_TYPE_UID:
153 idstr = talloc_asprintf(talloc_tos(), "UID %lu", idval);
154 break;
155 case ID_TYPE_GID:
156 idstr = talloc_asprintf(talloc_tos(), "GID %lu", idval);
157 break;
158 default:
159 d_fprintf(stderr, "Invalid id mapping type: %d\n", type);
160 return -1;
163 status = dbwrap_store_bystring(db, idstr,
164 string_term_tdb_data(sid_string),
165 TDB_REPLACE);
166 if (!NT_STATUS_IS_OK(status)) {
167 d_fprintf(stderr, "Error storing ID -> SID: "
168 "%s\n", nt_errstr(status));
169 talloc_free(idstr);
170 return -1;
172 status = dbwrap_store_bystring(db, sid_string,
173 string_term_tdb_data(idstr),
174 TDB_REPLACE);
175 if (!NT_STATUS_IS_OK(status)) {
176 d_fprintf(stderr, "Error storing SID -> ID: "
177 "%s\n", nt_errstr(status));
178 talloc_free(idstr);
179 return -1;
182 return 0;
185 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
187 TALLOC_CTX *mem_ctx;
188 FILE *input = NULL;
189 struct db_context *db;
190 const char *dbfile = NULL;
191 int ret = 0;
193 if (c->display_usage) {
194 d_printf("%s\n%s",
195 _("Usage:"),
196 _("net idmap restore [--db=<TDB>] [<inputfile>]\n"
197 " Restore ID mappings from file\n"
198 " TDB\tFile to store ID mappings to."
199 " inputfile\tFile to load ID mappings from. If not "
200 "given, load data from stdin.\n"));
201 return 0;
204 mem_ctx = talloc_stackframe();
206 dbfile = net_idmap_dbfile(c);
208 if (dbfile == NULL) {
209 ret = -1;
210 goto done;
213 d_fprintf(stderr, _("restoring id mapping to %s\n"), dbfile);
215 if (argc == 1) {
216 input = fopen(argv[0], "r");
217 if (input == NULL) {
218 d_fprintf(stderr, _("Could not open input file (%s): %s\n"),
219 argv[0], strerror(errno));
220 ret = -1;
221 goto done;
223 } else {
224 input = stdin;
227 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
228 if (db == NULL) {
229 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
230 dbfile, strerror(errno));
231 ret = -1;
232 goto done;
235 if (db->transaction_start(db) != 0) {
236 d_fprintf(stderr, _("Failed to start transaction.\n"));
237 ret = -1;
238 goto done;
241 while (!feof(input)) {
242 char line[128], sid_string[128];
243 int len;
244 unsigned long idval;
246 if (fgets(line, 127, input) == NULL)
247 break;
249 len = strlen(line);
251 if ( (len > 0) && (line[len-1] == '\n') )
252 line[len-1] = '\0';
254 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2)
256 ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
257 idval, sid_string);
258 if (ret != 0) {
259 break;
261 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2)
263 ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
264 idval, sid_string);
265 if (ret != 0) {
266 break;
268 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
269 ret = dbwrap_store_int32(db, "USER HWM", idval);
270 if (ret != 0) {
271 d_fprintf(stderr, _("Could not store USER HWM.\n"));
272 break;
274 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
275 ret = dbwrap_store_int32(db, "GROUP HWM", idval);
276 if (ret != 0) {
277 d_fprintf(stderr,
278 _("Could not store GROUP HWM.\n"));
279 break;
281 } else {
282 d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
283 line);
284 continue;
288 if (ret == 0) {
289 if(db->transaction_commit(db) != 0) {
290 d_fprintf(stderr, _("Failed to commit transaction.\n"));
291 ret = -1;
293 } else {
294 if (db->transaction_cancel(db) != 0) {
295 d_fprintf(stderr, _("Failed to cancel transaction.\n"));
299 done:
300 if ((input != NULL) && (input != stdin)) {
301 fclose(input);
304 talloc_free(mem_ctx);
305 return ret;
308 static
309 NTSTATUS dbwrap_delete_mapping(struct db_context *db, TDB_DATA key1, bool force)
311 TALLOC_CTX* mem_ctx = talloc_tos();
312 struct db_record *rec1=NULL, *rec2=NULL;
313 TDB_DATA key2;
314 bool is_valid_mapping;
315 NTSTATUS status = NT_STATUS_OK;
317 rec1 = db->fetch_locked(db, mem_ctx, key1);
318 if (rec1 == NULL) {
319 DEBUG(1, ("failed to fetch: %.*s\n", (int)key1.dsize, key1.dptr));
320 status = NT_STATUS_NO_MEMORY;
321 goto done;
323 key2 = rec1->value;
324 if (key2.dptr == NULL) {
325 DEBUG(1, ("could not find %.*s\n", (int)key1.dsize, key1.dptr));
326 status = NT_STATUS_NOT_FOUND;
327 goto done;
330 DEBUG(2, ("mapping: %.*s -> %.*s\n",
331 (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr));
333 rec2 = db->fetch_locked(db, mem_ctx, key2);
334 if (rec2 == NULL) {
335 DEBUG(1, ("failed to fetch: %.*s\n", (int)key2.dsize, key2.dptr));
336 status = NT_STATUS_NO_MEMORY;
337 goto done;
340 is_valid_mapping = tdb_data_equal(key1, rec2->value);
342 if (!is_valid_mapping) {
343 DEBUG(1, ("invalid mapping: %.*s -> %.*s -> %.*s\n",
344 (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr,
345 (int)rec2->value.dsize, rec2->value.dptr ));
346 if ( !force ) {
347 status = NT_STATUS_FILE_INVALID;
348 goto done;
352 status = rec1->delete_rec(rec1);
353 if (!NT_STATUS_IS_OK(status)) {
354 DEBUG(1, ("failed to delete: %.*s\n", (int)key1.dsize, key1.dptr));
355 goto done;
358 if (is_valid_mapping) {
359 status = rec2->delete_rec(rec2);
360 if (!NT_STATUS_IS_OK(status)) {
361 DEBUG(1, ("failed to delete: %.*s\n", (int)key2.dsize, key2.dptr));
364 done:
365 TALLOC_FREE(rec1);
366 TALLOC_FREE(rec2);
367 return status;
370 static
371 NTSTATUS delete_mapping_action(struct db_context *db, void* data)
373 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, false);
375 static
376 NTSTATUS delete_mapping_action_force(struct db_context *db, void* data)
378 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, true);
381 /***********************************************************
382 Delete a SID mapping from a winbindd_idmap.tdb
383 **********************************************************/
384 static bool delete_args_ok(int argc, const char **argv)
386 if (argc != 1)
387 return false;
388 if (strncmp(argv[0], "S-", 2) == 0)
389 return true;
390 if (strncmp(argv[0], "GID ", 4) == 0)
391 return true;
392 if (strncmp(argv[0], "UID ", 4) == 0)
393 return true;
394 return false;
397 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
399 int ret = -1;
400 struct db_context *db;
401 TALLOC_CTX *mem_ctx;
402 TDB_DATA key;
403 NTSTATUS status;
404 const char* dbfile;
406 if ( !delete_args_ok(argc,argv) || c->display_usage) {
407 d_printf("%s\n%s",
408 _("Usage:"),
409 _("net idmap delete [-f] [--db=<TDB>] <ID>\n"
410 " Delete mapping of ID from TDB.\n"
411 " -f\tforce\n"
412 " TDB\tidmap database\n"
413 " ID\tSID|GID|UID\n"));
414 return c->display_usage ? 0 : -1;
417 mem_ctx = talloc_stackframe();
419 dbfile = net_idmap_dbfile(c);
420 if (dbfile == NULL) {
421 goto done;
423 d_fprintf(stderr, _("deleting id mapping from %s\n"), dbfile);
425 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR, 0);
426 if (db == NULL) {
427 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
428 dbfile, strerror(errno));
429 goto done;
432 key = string_term_tdb_data(argv[0]);
434 status = dbwrap_trans_do(db, (c->opt_force
435 ? delete_mapping_action_force
436 : delete_mapping_action), &key);
438 if (!NT_STATUS_IS_OK(status)) {
439 d_fprintf(stderr, _("could not delete mapping: %s\n"),
440 nt_errstr(status));
441 goto done;
443 ret = 0;
444 done:
445 talloc_free(mem_ctx);
446 return ret;
449 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
451 d_printf("%s\n", _("Not implemented yet"));
452 return -1;
454 static bool idmap_store_secret(const char *backend,
455 const char *domain,
456 const char *identity,
457 const char *secret)
459 char *tmp;
460 int r;
461 bool ret;
463 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
465 if (r < 0) return false;
467 strupper_m(tmp); /* make sure the key is case insensitive */
468 ret = secrets_store_generic(tmp, identity, secret);
470 free(tmp);
471 return ret;
475 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
477 TALLOC_CTX *ctx;
478 const char *secret;
479 const char *dn;
480 char *domain;
481 char *backend;
482 char *opt = NULL;
483 bool ret;
485 if (argc != 2 || c->display_usage) {
486 d_printf("%s\n%s",
487 _("Usage:\n"),
488 _("net idmap secret <DOMAIN> <secret>\n"
489 " Set the secret for the specified domain\n"
490 " DOMAIN\tDomain to set secret for.\n"
491 " secret\tNew secret to set.\n"));
492 return c->display_usage?0:-1;
495 secret = argv[1];
497 ctx = talloc_new(NULL);
498 ALLOC_CHECK(ctx);
500 domain = talloc_strdup(ctx, argv[0]);
501 ALLOC_CHECK(domain);
503 opt = talloc_asprintf(ctx, "idmap config %s", domain);
504 ALLOC_CHECK(opt);
506 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
507 ALLOC_CHECK(backend);
509 if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
510 d_fprintf(stderr,
511 _("The only currently supported backend is LDAP\n"));
512 talloc_free(ctx);
513 return -1;
516 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
517 if ( ! dn) {
518 d_fprintf(stderr,
519 _("Missing ldap_user_dn option for domain %s\n"),
520 domain);
521 talloc_free(ctx);
522 return -1;
525 ret = idmap_store_secret("ldap", domain, dn, secret);
527 if ( ! ret) {
528 d_fprintf(stderr, _("Failed to store secret\n"));
529 talloc_free(ctx);
530 return -1;
533 d_printf(_("Secret stored\n"));
534 return 0;
537 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
539 TALLOC_CTX *mem_ctx;
540 int result = -1;
541 struct dom_sid src_sid, dst_sid;
542 char *src, *dst;
543 struct db_context *db;
544 struct db_record *rec;
545 NTSTATUS status;
547 if (argc != 3 || c->display_usage) {
548 d_fprintf(stderr, "%s net idmap aclmapset <tdb> "
549 "<src-sid> <dst-sid>\n", _("Usage:"));
550 return -1;
553 if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
554 d_fprintf(stderr, _("talloc_init failed\n"));
555 return -1;
558 if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
559 O_RDWR|O_CREAT, 0600))) {
560 d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
561 goto fail;
564 if (!string_to_sid(&src_sid, argv[1])) {
565 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
566 goto fail;
569 if (!string_to_sid(&dst_sid, argv[2])) {
570 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
571 goto fail;
574 if (!(src = sid_string_talloc(mem_ctx, &src_sid))
575 || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
576 d_fprintf(stderr, _("talloc_strdup failed\n"));
577 goto fail;
580 if (!(rec = db->fetch_locked(
581 db, mem_ctx, string_term_tdb_data(src)))) {
582 d_fprintf(stderr, _("could not fetch db record\n"));
583 goto fail;
586 status = rec->store(rec, string_term_tdb_data(dst), 0);
587 TALLOC_FREE(rec);
589 if (!NT_STATUS_IS_OK(status)) {
590 d_fprintf(stderr, _("could not store record: %s\n"),
591 nt_errstr(status));
592 goto fail;
595 result = 0;
596 fail:
597 TALLOC_FREE(mem_ctx);
598 return result;
601 /***********************************************************
602 Look at the current idmap
603 **********************************************************/
604 int net_idmap(struct net_context *c, int argc, const char **argv)
606 struct functable func[] = {
608 "dump",
609 net_idmap_dump,
610 NET_TRANSPORT_LOCAL,
611 N_("Dump the current ID mappings"),
612 N_("net idmap dump\n"
613 " Dump the current ID mappings")
616 "restore",
617 net_idmap_restore,
618 NET_TRANSPORT_LOCAL,
619 N_("Restore entries from stdin"),
620 N_("net idmap restore\n"
621 " Restore entries from stdin")
624 "setmap",
625 net_idmap_set,
626 NET_TRANSPORT_LOCAL,
627 N_("Not implemented yet"),
628 N_("net idmap setmap\n"
629 " Not implemented yet")
632 "delete",
633 net_idmap_delete,
634 NET_TRANSPORT_LOCAL,
635 N_("Delete ID mapping"),
636 N_("net idmap delete <ID>\n"
637 " Delete ID mapping")
640 "secret",
641 net_idmap_secret,
642 NET_TRANSPORT_LOCAL,
643 N_("Set secret for specified domain"),
644 N_("net idmap secret <DOMAIN> <secret>\n"
645 " Set secret for specified domain")
648 "aclmapset",
649 net_idmap_aclmapset,
650 NET_TRANSPORT_LOCAL,
651 N_("Set acl map"),
652 N_("net idmap aclmapset\n"
653 " Set acl map")
655 {NULL, NULL, 0, NULL, NULL}
658 return net_run_function(c, argc, argv, "net idmap", func);