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/>.
22 #include "utils/net.h"
25 #define ALLOC_CHECK(mem) do { \
27 d_fprintf(stderr, _("Out of memory!\n")); \
32 /***********************************************************
33 Helper function for net_idmap_dump. Dump one entry.
34 **********************************************************/
35 static int net_idmap_dump_one_entry(struct db_record *rec,
38 if (strcmp((char *)rec->key.dptr, "USER HWM") == 0) {
39 printf(_("USER HWM %d\n"), IVAL(rec->value.dptr,0));
43 if (strcmp((char *)rec->key.dptr, "GROUP HWM") == 0) {
44 printf(_("GROUP HWM %d\n"), IVAL(rec->value.dptr,0));
48 if (strncmp((char *)rec->key.dptr, "S-", 2) != 0)
51 printf("%s %s\n", rec->value.dptr, rec->key.dptr);
55 /***********************************************************
56 Dump the current idmap
57 **********************************************************/
58 static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
60 struct db_context *db;
63 if ( argc != 1 || c->display_usage) {
66 _("net idmap dump <inputfile>\n"
67 " Dump current ID mapping.\n"
68 " inputfile\tTDB file to read mappings from.\n"));
69 return c->display_usage?0:-1;
72 mem_ctx = talloc_stackframe();
74 db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
76 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
77 argv[0], strerror(errno));
82 db->traverse_read(db, net_idmap_dump_one_entry, NULL);
89 /***********************************************************
90 Write entries from stdin to current local idmap
91 **********************************************************/
93 static int net_idmap_store_id_mapping(struct db_context *db,
96 const char *sid_string)
103 idstr = talloc_asprintf(talloc_tos(), "UID %lu", idval);
106 idstr = talloc_asprintf(talloc_tos(), "GID %lu", idval);
109 d_fprintf(stderr, "Invalid id mapping type: %d\n", type);
113 status = dbwrap_store_bystring(db, idstr,
114 string_term_tdb_data(sid_string),
116 if (!NT_STATUS_IS_OK(status)) {
117 d_fprintf(stderr, "Error storing ID -> SID: "
118 "%s\n", nt_errstr(status));
122 status = dbwrap_store_bystring(db, sid_string,
123 string_term_tdb_data(idstr),
125 if (!NT_STATUS_IS_OK(status)) {
126 d_fprintf(stderr, "Error storing SID -> ID: "
127 "%s\n", nt_errstr(status));
135 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
139 struct db_context *db;
143 if (c->display_usage) {
146 _("net idmap restore [<inputfile>]\n"
147 " Restore ID mappings from file\n"
148 " inputfile\tFile to load ID mappings from. If not "
149 "given, load data from stdin.\n"));
153 mem_ctx = talloc_stackframe();
155 if (strequal(lp_idmap_backend(), "tdb")) {
156 dbfile = state_path("winbindd_idmap.tdb");
157 if (dbfile == NULL) {
158 d_fprintf(stderr, _("Out of memory!\n"));
161 } else if (strequal(lp_idmap_backend(), "tdb2")) {
162 dbfile = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
163 if (dbfile == NULL) {
164 dbfile = talloc_asprintf(mem_ctx, "%s/idmap2.tdb",
168 char *backend, *args;
170 backend = talloc_strdup(mem_ctx, lp_idmap_backend());
171 args = strchr(backend, ':');
176 d_printf(_("Sorry, 'net idmap restore' is currently not "
177 "supported for idmap backend = %s.\n"
178 "Only tdb and tdb2 are supported.\n"),
185 d_fprintf(stderr, _("restoring id mapping to %s data base in '%s'\n"),
186 lp_idmap_backend(), dbfile);
189 input = fopen(argv[0], "r");
194 db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
196 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
197 dbfile, strerror(errno));
202 if (db->transaction_start(db) != 0) {
203 d_fprintf(stderr, _("Failed to start transaction.\n"));
208 while (!feof(input)) {
209 char line[128], sid_string[128];
213 if (fgets(line, 127, input) == NULL)
218 if ( (len > 0) && (line[len-1] == '\n') )
221 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2)
223 ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
228 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2)
230 ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
235 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
236 ret = dbwrap_store_int32(db, "USER HWM", idval);
238 d_fprintf(stderr, _("Could not store USER HWM.\n"));
241 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
242 ret = dbwrap_store_int32(db, "GROUP HWM", idval);
245 _("Could not store GROUP HWM.\n"));
249 d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
256 if(db->transaction_commit(db) != 0) {
257 d_fprintf(stderr, _("Failed to commit transaction.\n"));
261 if (db->transaction_cancel(db) != 0) {
262 d_fprintf(stderr, _("Failed to cancel transaction.\n"));
267 if ((input != NULL) && (input != stdin)) {
271 talloc_free(mem_ctx);
275 /***********************************************************
276 Delete a SID mapping from a winbindd_idmap.tdb
277 **********************************************************/
278 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
280 d_printf("%s\n", _("Not implemented yet"));
284 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
286 d_printf("%s\n", _("Not implemented yet"));
289 bool idmap_store_secret(const char *backend, bool alloc,
290 const char *domain, const char *identity,
298 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
300 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
303 if (r < 0) return false;
305 strupper_m(tmp); /* make sure the key is case insensitive */
306 ret = secrets_store_generic(tmp, identity, secret);
313 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
323 if (argc != 2 || c->display_usage) {
326 _("net idmap secret <DOMAIN> <secret>\n"
327 " Set the secret for the specified domain\n"
328 " DOMAIN\tDomain to set secret for.\n"
329 " secret\tNew secret to set.\n"));
330 return c->display_usage?0:-1;
335 ctx = talloc_new(NULL);
338 domain = talloc_strdup(ctx, argv[0]);
341 opt = talloc_asprintf(ctx, "idmap config %s", domain);
344 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
345 ALLOC_CHECK(backend);
347 if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
349 _("The only currently supported backend is LDAP\n"));
354 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
357 _("Missing ldap_user_dn option for domain %s\n"),
363 ret = idmap_store_secret("ldap", false, domain, dn, secret);
366 d_fprintf(stderr, _("Failed to store secret\n"));
371 d_printf(_("Secret stored\n"));
375 int net_help_idmap(struct net_context *c, int argc, const char **argv)
377 d_printf(_("net idmap dump <inputfile>\n"
378 " Dump current id mapping\n"));
380 d_printf(_("net idmap restore\n"
381 " Restore entries from stdin\n"));
383 /* Deliberately *not* document net idmap delete */
385 d_printf(_("net idmap secret <DOMAIN>|alloc <secret>\n"
386 " Set the secret for the specified DOMAIN (or the alloc "
392 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
396 struct dom_sid src_sid, dst_sid;
398 struct db_context *db;
399 struct db_record *rec;
402 if (argc != 3 || c->display_usage) {
403 d_fprintf(stderr, "%s net idmap aclmapset <tdb> "
404 "<src-sid> <dst-sid>\n", _("Usage:"));
408 if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
409 d_fprintf(stderr, _("talloc_init failed\n"));
413 if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
414 O_RDWR|O_CREAT, 0600))) {
415 d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
419 if (!string_to_sid(&src_sid, argv[1])) {
420 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
424 if (!string_to_sid(&dst_sid, argv[2])) {
425 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
429 if (!(src = sid_string_talloc(mem_ctx, &src_sid))
430 || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
431 d_fprintf(stderr, _("talloc_strdup failed\n"));
435 if (!(rec = db->fetch_locked(
436 db, mem_ctx, string_term_tdb_data(src)))) {
437 d_fprintf(stderr, _("could not fetch db record\n"));
441 status = rec->store(rec, string_term_tdb_data(dst), 0);
444 if (!NT_STATUS_IS_OK(status)) {
445 d_fprintf(stderr, _("could not store record: %s\n"),
452 TALLOC_FREE(mem_ctx);
456 /***********************************************************
457 Look at the current idmap
458 **********************************************************/
459 int net_idmap(struct net_context *c, int argc, const char **argv)
461 struct functable func[] = {
466 N_("Dump the current ID mappings"),
467 N_("net idmap dump\n"
468 " Dump the current ID mappings")
474 N_("Restore entries from stdin"),
475 N_("net idmap restore\n"
476 " Restore entries from stdin")
482 N_("Not implemented yet"),
483 N_("net idmap setmap\n"
484 " Not implemented yet")
490 N_("Not implemented yet"),
491 N_("net idmap delete\n"
492 " Not implemented yet")
498 N_("Set secret for specified domain"),
499 N_("net idmap secret {<DOMAIN>|alloc} <secret>\n"
500 " Set secret for specified domain or alloc module")
507 N_("net idmap aclmapset\n"
510 {NULL, NULL, 0, NULL, NULL}
513 return net_run_function(c, argc, argv, "net idmap", func);