s3-smbd: Added a change_to_user_by_session() function.
[Samba.git] / source3 / utils / net_idmap.c
blobe059a6432c0c4cd59e59abe6698b9cb2d75d7cc6
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.h"
26 #include "../libcli/security/security.h"
27 #include "net_idmap_check.h"
29 #define ALLOC_CHECK(mem) do { \
30 if (!mem) { \
31 d_fprintf(stderr, _("Out of memory!\n")); \
32 talloc_free(ctx); \
33 return -1; \
34 } } while(0)
36 /***********************************************************
37 Helper function for net_idmap_dump. Dump one entry.
38 **********************************************************/
39 static int net_idmap_dump_one_entry(struct db_record *rec,
40 void *unused)
42 if (strcmp((char *)rec->key.dptr, "USER HWM") == 0) {
43 printf(_("USER HWM %d\n"), IVAL(rec->value.dptr,0));
44 return 0;
47 if (strcmp((char *)rec->key.dptr, "GROUP HWM") == 0) {
48 printf(_("GROUP HWM %d\n"), IVAL(rec->value.dptr,0));
49 return 0;
52 if (strncmp((char *)rec->key.dptr, "S-", 2) != 0)
53 return 0;
55 printf("%s %s\n", rec->value.dptr, rec->key.dptr);
56 return 0;
59 static const char* net_idmap_dbfile(struct net_context *c)
61 const char* dbfile = NULL;
63 if (c->opt_db != NULL) {
64 dbfile = talloc_strdup(talloc_tos(), c->opt_db);
65 if (dbfile == NULL) {
66 d_fprintf(stderr, _("Out of memory!\n"));
68 } else if (strequal(lp_idmap_backend(), "tdb")) {
69 dbfile = state_path("winbindd_idmap.tdb");
70 if (dbfile == NULL) {
71 d_fprintf(stderr, _("Out of memory!\n"));
73 } else if (strequal(lp_idmap_backend(), "tdb2")) {
74 dbfile = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
75 if (dbfile == NULL) {
76 dbfile = talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
77 lp_private_dir());
79 if (dbfile == NULL) {
80 d_fprintf(stderr, _("Out of memory!\n"));
82 } else {
83 char* backend = talloc_strdup(talloc_tos(), lp_idmap_backend());
84 char* args = strchr(backend, ':');
85 if (args != NULL) {
86 *args = '\0';
89 d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
90 backend);
92 talloc_free(backend);
95 return dbfile;
98 /***********************************************************
99 Dump the current idmap
100 **********************************************************/
101 static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
103 struct db_context *db;
104 TALLOC_CTX *mem_ctx;
105 const char* dbfile;
106 int ret = -1;
108 if ( argc > 1 || c->display_usage) {
109 d_printf("%s\n%s",
110 _("Usage:"),
111 _("net idmap dump [[--db=]<inputfile>]\n"
112 " Dump current ID mapping.\n"
113 " inputfile\tTDB file to read mappings from.\n"));
114 return c->display_usage?0:-1;
117 mem_ctx = talloc_stackframe();
119 dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c);
120 if (dbfile == NULL) {
121 goto done;
123 d_fprintf(stderr, _("dumping id mapping from %s\n"), dbfile);
125 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0);
126 if (db == NULL) {
127 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
128 dbfile, strerror(errno));
129 goto done;
132 db->traverse_read(db, net_idmap_dump_one_entry, NULL);
133 ret = 0;
135 done:
136 talloc_free(mem_ctx);
137 return ret;
140 /***********************************************************
141 Write entries from stdin to current local idmap
142 **********************************************************/
144 static int net_idmap_store_id_mapping(struct db_context *db,
145 enum id_type type,
146 unsigned long idval,
147 const char *sid_string)
149 NTSTATUS status;
150 char *idstr = NULL;
152 switch(type) {
153 case ID_TYPE_UID:
154 idstr = talloc_asprintf(talloc_tos(), "UID %lu", idval);
155 break;
156 case ID_TYPE_GID:
157 idstr = talloc_asprintf(talloc_tos(), "GID %lu", idval);
158 break;
159 default:
160 d_fprintf(stderr, "Invalid id mapping type: %d\n", type);
161 return -1;
164 status = dbwrap_store_bystring(db, idstr,
165 string_term_tdb_data(sid_string),
166 TDB_REPLACE);
167 if (!NT_STATUS_IS_OK(status)) {
168 d_fprintf(stderr, "Error storing ID -> SID: "
169 "%s\n", nt_errstr(status));
170 talloc_free(idstr);
171 return -1;
173 status = dbwrap_store_bystring(db, sid_string,
174 string_term_tdb_data(idstr),
175 TDB_REPLACE);
176 if (!NT_STATUS_IS_OK(status)) {
177 d_fprintf(stderr, "Error storing SID -> ID: "
178 "%s\n", nt_errstr(status));
179 talloc_free(idstr);
180 return -1;
183 return 0;
186 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
188 TALLOC_CTX *mem_ctx;
189 FILE *input = NULL;
190 struct db_context *db;
191 const char *dbfile = NULL;
192 int ret = 0;
194 if (c->display_usage) {
195 d_printf("%s\n%s",
196 _("Usage:"),
197 _("net idmap restore [--db=<TDB>] [<inputfile>]\n"
198 " Restore ID mappings from file\n"
199 " TDB\tFile to store ID mappings to."
200 " inputfile\tFile to load ID mappings from. If not "
201 "given, load data from stdin.\n"));
202 return 0;
205 mem_ctx = talloc_stackframe();
207 dbfile = net_idmap_dbfile(c);
209 if (dbfile == NULL) {
210 ret = -1;
211 goto done;
214 d_fprintf(stderr, _("restoring id mapping to %s\n"), dbfile);
216 if (argc == 1) {
217 input = fopen(argv[0], "r");
218 if (input == NULL) {
219 d_fprintf(stderr, _("Could not open input file (%s): %s\n"),
220 argv[0], strerror(errno));
221 ret = -1;
222 goto done;
224 } else {
225 input = stdin;
228 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
229 if (db == NULL) {
230 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
231 dbfile, strerror(errno));
232 ret = -1;
233 goto done;
236 if (db->transaction_start(db) != 0) {
237 d_fprintf(stderr, _("Failed to start transaction.\n"));
238 ret = -1;
239 goto done;
242 while (!feof(input)) {
243 char line[128], sid_string[128];
244 int len;
245 unsigned long idval;
247 if (fgets(line, 127, input) == NULL)
248 break;
250 len = strlen(line);
252 if ( (len > 0) && (line[len-1] == '\n') )
253 line[len-1] = '\0';
255 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2)
257 ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
258 idval, sid_string);
259 if (ret != 0) {
260 break;
262 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2)
264 ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
265 idval, sid_string);
266 if (ret != 0) {
267 break;
269 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
270 ret = dbwrap_store_int32(db, "USER HWM", idval);
271 if (ret != 0) {
272 d_fprintf(stderr, _("Could not store USER HWM.\n"));
273 break;
275 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
276 ret = dbwrap_store_int32(db, "GROUP HWM", idval);
277 if (ret != 0) {
278 d_fprintf(stderr,
279 _("Could not store GROUP HWM.\n"));
280 break;
282 } else {
283 d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
284 line);
285 continue;
289 if (ret == 0) {
290 if(db->transaction_commit(db) != 0) {
291 d_fprintf(stderr, _("Failed to commit transaction.\n"));
292 ret = -1;
294 } else {
295 if (db->transaction_cancel(db) != 0) {
296 d_fprintf(stderr, _("Failed to cancel transaction.\n"));
300 done:
301 if ((input != NULL) && (input != stdin)) {
302 fclose(input);
305 talloc_free(mem_ctx);
306 return ret;
309 static
310 NTSTATUS dbwrap_delete_mapping(struct db_context *db, TDB_DATA key1, bool force)
312 TALLOC_CTX* mem_ctx = talloc_tos();
313 struct db_record *rec1=NULL, *rec2=NULL;
314 TDB_DATA key2;
315 bool is_valid_mapping;
316 NTSTATUS status = NT_STATUS_OK;
318 rec1 = db->fetch_locked(db, mem_ctx, key1);
319 if (rec1 == NULL) {
320 DEBUG(1, ("failed to fetch: %.*s\n", (int)key1.dsize, key1.dptr));
321 status = NT_STATUS_NO_MEMORY;
322 goto done;
324 key2 = rec1->value;
325 if (key2.dptr == NULL) {
326 DEBUG(1, ("could not find %.*s\n", (int)key1.dsize, key1.dptr));
327 status = NT_STATUS_NOT_FOUND;
328 goto done;
331 DEBUG(2, ("mapping: %.*s -> %.*s\n",
332 (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr));
334 rec2 = db->fetch_locked(db, mem_ctx, key2);
335 if (rec2 == NULL) {
336 DEBUG(1, ("failed to fetch: %.*s\n", (int)key2.dsize, key2.dptr));
337 status = NT_STATUS_NO_MEMORY;
338 goto done;
341 is_valid_mapping = tdb_data_equal(key1, rec2->value);
343 if (!is_valid_mapping) {
344 DEBUG(1, ("invalid mapping: %.*s -> %.*s -> %.*s\n",
345 (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr,
346 (int)rec2->value.dsize, rec2->value.dptr ));
347 if ( !force ) {
348 status = NT_STATUS_FILE_INVALID;
349 goto done;
353 status = rec1->delete_rec(rec1);
354 if (!NT_STATUS_IS_OK(status)) {
355 DEBUG(1, ("failed to delete: %.*s\n", (int)key1.dsize, key1.dptr));
356 goto done;
359 if (is_valid_mapping) {
360 status = rec2->delete_rec(rec2);
361 if (!NT_STATUS_IS_OK(status)) {
362 DEBUG(1, ("failed to delete: %.*s\n", (int)key2.dsize, key2.dptr));
365 done:
366 TALLOC_FREE(rec1);
367 TALLOC_FREE(rec2);
368 return status;
371 static
372 NTSTATUS delete_mapping_action(struct db_context *db, void* data)
374 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, false);
376 static
377 NTSTATUS delete_mapping_action_force(struct db_context *db, void* data)
379 return dbwrap_delete_mapping(db, *(TDB_DATA*)data, true);
382 /***********************************************************
383 Delete a SID mapping from a winbindd_idmap.tdb
384 **********************************************************/
385 static bool delete_args_ok(int argc, const char **argv)
387 if (argc != 1)
388 return false;
389 if (strncmp(argv[0], "S-", 2) == 0)
390 return true;
391 if (strncmp(argv[0], "GID ", 4) == 0)
392 return true;
393 if (strncmp(argv[0], "UID ", 4) == 0)
394 return true;
395 return false;
398 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
400 int ret = -1;
401 struct db_context *db;
402 TALLOC_CTX *mem_ctx;
403 TDB_DATA key;
404 NTSTATUS status;
405 const char* dbfile;
407 if ( !delete_args_ok(argc,argv) || c->display_usage) {
408 d_printf("%s\n%s",
409 _("Usage:"),
410 _("net idmap delete [-f] [--db=<TDB>] <ID>\n"
411 " Delete mapping of ID from TDB.\n"
412 " -f\tforce\n"
413 " TDB\tidmap database\n"
414 " ID\tSID|GID|UID\n"));
415 return c->display_usage ? 0 : -1;
418 mem_ctx = talloc_stackframe();
420 dbfile = net_idmap_dbfile(c);
421 if (dbfile == NULL) {
422 goto done;
424 d_fprintf(stderr, _("deleting id mapping from %s\n"), dbfile);
426 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR, 0);
427 if (db == NULL) {
428 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
429 dbfile, strerror(errno));
430 goto done;
433 key = string_term_tdb_data(argv[0]);
435 status = dbwrap_trans_do(db, (c->opt_force
436 ? delete_mapping_action_force
437 : delete_mapping_action), &key);
439 if (!NT_STATUS_IS_OK(status)) {
440 d_fprintf(stderr, _("could not delete mapping: %s\n"),
441 nt_errstr(status));
442 goto done;
444 ret = 0;
445 done:
446 talloc_free(mem_ctx);
447 return ret;
450 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
452 d_printf("%s\n", _("Not implemented yet"));
453 return -1;
455 static bool idmap_store_secret(const char *backend,
456 const char *domain,
457 const char *identity,
458 const char *secret)
460 char *tmp;
461 int r;
462 bool ret;
464 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
466 if (r < 0) return false;
468 strupper_m(tmp); /* make sure the key is case insensitive */
469 ret = secrets_store_generic(tmp, identity, secret);
471 free(tmp);
472 return ret;
476 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
478 TALLOC_CTX *ctx;
479 const char *secret;
480 const char *dn;
481 char *domain;
482 char *backend;
483 char *opt = NULL;
484 bool ret;
486 if (argc != 2 || c->display_usage) {
487 d_printf("%s\n%s",
488 _("Usage:\n"),
489 _("net idmap secret <DOMAIN> <secret>\n"
490 " Set the secret for the specified domain\n"
491 " DOMAIN\tDomain to set secret for.\n"
492 " secret\tNew secret to set.\n"));
493 return c->display_usage?0:-1;
496 secret = argv[1];
498 ctx = talloc_new(NULL);
499 ALLOC_CHECK(ctx);
501 domain = talloc_strdup(ctx, argv[0]);
502 ALLOC_CHECK(domain);
504 opt = talloc_asprintf(ctx, "idmap config %s", domain);
505 ALLOC_CHECK(opt);
507 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
508 ALLOC_CHECK(backend);
510 if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
511 d_fprintf(stderr,
512 _("The only currently supported backend is LDAP\n"));
513 talloc_free(ctx);
514 return -1;
517 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
518 if ( ! dn) {
519 d_fprintf(stderr,
520 _("Missing ldap_user_dn option for domain %s\n"),
521 domain);
522 talloc_free(ctx);
523 return -1;
526 ret = idmap_store_secret("ldap", domain, dn, secret);
528 if ( ! ret) {
529 d_fprintf(stderr, _("Failed to store secret\n"));
530 talloc_free(ctx);
531 return -1;
534 d_printf(_("Secret stored\n"));
535 return 0;
538 static int net_idmap_check(struct net_context *c, int argc, const char **argv)
540 const char* dbfile;
541 struct check_options opts;
543 if ( argc > 1 || c->display_usage) {
544 d_printf("%s\n%s",
545 _("Usage:"),
546 _("net idmap check [-v] [-r] [-a] [-T] [-f] [-l] [[--db=]<TDB>]\n"
547 " Check an idmap database.\n"
548 " --verbose,-v\tverbose\n"
549 " --repair,-r\trepair\n"
550 " --auto,-a\tnoninteractive mode\n"
551 " --test,-T\tdry run\n"
552 " --fore,-f\tforce\n"
553 " --lock,-l\tlock db while doing the check\n"
554 " TDB\tidmap database\n"));
555 return c->display_usage ? 0 : -1;
558 dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c);
559 if (dbfile == NULL) {
560 return -1;
562 d_fprintf(stderr, _("check database: %s\n"), dbfile);
564 opts = (struct check_options) {
565 .lock = c->opt_lock || c->opt_long_list_entries,
566 .test = c->opt_testmode,
567 .automatic = c->opt_auto,
568 .verbose = c->opt_verbose,
569 .force = c->opt_force,
570 .repair = c->opt_repair || c->opt_reboot,
573 return net_idmap_check_db(dbfile, &opts);
576 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
578 TALLOC_CTX *mem_ctx;
579 int result = -1;
580 struct dom_sid src_sid, dst_sid;
581 char *src, *dst;
582 struct db_context *db;
583 struct db_record *rec;
584 NTSTATUS status;
586 if (argc != 3 || c->display_usage) {
587 d_fprintf(stderr, "%s net idmap aclmapset <tdb> "
588 "<src-sid> <dst-sid>\n", _("Usage:"));
589 return -1;
592 if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
593 d_fprintf(stderr, _("talloc_init failed\n"));
594 return -1;
597 if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
598 O_RDWR|O_CREAT, 0600))) {
599 d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
600 goto fail;
603 if (!string_to_sid(&src_sid, argv[1])) {
604 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
605 goto fail;
608 if (!string_to_sid(&dst_sid, argv[2])) {
609 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
610 goto fail;
613 if (!(src = sid_string_talloc(mem_ctx, &src_sid))
614 || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
615 d_fprintf(stderr, _("talloc_strdup failed\n"));
616 goto fail;
619 if (!(rec = db->fetch_locked(
620 db, mem_ctx, string_term_tdb_data(src)))) {
621 d_fprintf(stderr, _("could not fetch db record\n"));
622 goto fail;
625 status = rec->store(rec, string_term_tdb_data(dst), 0);
626 TALLOC_FREE(rec);
628 if (!NT_STATUS_IS_OK(status)) {
629 d_fprintf(stderr, _("could not store record: %s\n"),
630 nt_errstr(status));
631 goto fail;
634 result = 0;
635 fail:
636 TALLOC_FREE(mem_ctx);
637 return result;
640 /***********************************************************
641 Look at the current idmap
642 **********************************************************/
643 int net_idmap(struct net_context *c, int argc, const char **argv)
645 struct functable func[] = {
647 "dump",
648 net_idmap_dump,
649 NET_TRANSPORT_LOCAL,
650 N_("Dump the current ID mappings"),
651 N_("net idmap dump\n"
652 " Dump the current ID mappings")
655 "restore",
656 net_idmap_restore,
657 NET_TRANSPORT_LOCAL,
658 N_("Restore entries from stdin"),
659 N_("net idmap restore\n"
660 " Restore entries from stdin")
663 "setmap",
664 net_idmap_set,
665 NET_TRANSPORT_LOCAL,
666 N_("Not implemented yet"),
667 N_("net idmap setmap\n"
668 " Not implemented yet")
671 "delete",
672 net_idmap_delete,
673 NET_TRANSPORT_LOCAL,
674 N_("Delete ID mapping"),
675 N_("net idmap delete <ID>\n"
676 " Delete ID mapping")
679 "secret",
680 net_idmap_secret,
681 NET_TRANSPORT_LOCAL,
682 N_("Set secret for specified domain"),
683 N_("net idmap secret <DOMAIN> <secret>\n"
684 " Set secret for specified domain")
687 "aclmapset",
688 net_idmap_aclmapset,
689 NET_TRANSPORT_LOCAL,
690 N_("Set acl map"),
691 N_("net idmap aclmapset\n"
692 " Set acl map")
695 "check",
696 net_idmap_check,
697 NET_TRANSPORT_LOCAL,
698 N_("Check id mappings"),
699 N_("net idmap check\n"
700 " Check id mappings")
702 {NULL, NULL, 0, NULL, NULL}
705 return net_run_function(c, argc, argv, "net idmap", func);