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"
24 #define ALLOC_CHECK(mem) do { \
26 d_fprintf(stderr, _("Out of memory!\n")); \
31 /***********************************************************
32 Helper function for net_idmap_dump. Dump one entry.
33 **********************************************************/
34 static int net_idmap_dump_one_entry(TDB_CONTEXT *tdb,
39 if (strcmp((char *)key.dptr, "USER HWM") == 0) {
40 printf(_("USER HWM %d\n"), IVAL(data.dptr,0));
44 if (strcmp((char *)key.dptr, "GROUP HWM") == 0) {
45 printf(_("GROUP HWM %d\n"), IVAL(data.dptr,0));
49 if (strncmp((char *)key.dptr, "S-", 2) != 0)
52 printf("%s %s\n", data.dptr, key.dptr);
56 /***********************************************************
57 Dump the current idmap
58 **********************************************************/
59 static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
61 TDB_CONTEXT *idmap_tdb;
63 if ( argc != 1 || c->display_usage) {
64 d_printf(_("Usage:\n"),
65 _("net idmap dump <inputfile>\n"
66 " Dump current ID mapping.\n"
67 " inputfile\tTDB file to read mappings from.\n"));
68 return c->display_usage?0:-1;
71 idmap_tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
73 if (idmap_tdb == NULL) {
74 d_fprintf(stderr, _("Could not open idmap: %s\n"), argv[0]);
78 tdb_traverse(idmap_tdb, net_idmap_dump_one_entry, NULL);
85 /***********************************************************
86 Write entries from stdin to current local idmap
87 **********************************************************/
89 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
94 if (c->display_usage) {
95 d_printf(_("Usage:\n"),
96 _("net idmap restore [inputfile]\n"
97 " Restore ID mappings from file\n"
98 " inputfile\tFile to load ID mappings from. If "
99 "not given, load data from stdin.\n"));
103 if (! winbind_ping()) {
105 _("To use net idmap Winbindd must be running.\n"));
109 ctx = talloc_new(NULL);
113 input = fopen(argv[0], "r");
118 while (!feof(input)) {
119 char line[128], sid_string[128];
121 struct wbcDomainSid sid;
122 enum id_type type = ID_TYPE_NOT_SPECIFIED;
126 if (fgets(line, 127, input) == NULL)
131 if ( (len > 0) && (line[len-1] == '\n') )
134 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2) {
136 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2) {
138 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
140 wbc_status = wbcSetUidHwm(idval);
141 if (!WBC_ERROR_IS_OK(wbc_status)) {
143 _("Could not set USER HWM: %s\n"),
144 wbcErrorString(wbc_status));
147 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
149 wbc_status = wbcSetGidHwm(idval);
150 if (!WBC_ERROR_IS_OK(wbc_status)) {
152 _("Could not set GROUP HWM: %s\n"),
153 wbcErrorString(wbc_status));
157 d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
162 wbc_status = wbcStringToSid(sid_string, &sid);
163 if (!WBC_ERROR_IS_OK(wbc_status)) {
164 d_fprintf(stderr, _("ignoring invalid sid [%s]: %s\n"),
165 sid_string, wbcErrorString(wbc_status));
169 if (type == ID_TYPE_UID) {
170 wbc_status = wbcSetUidMapping(idval, &sid);
172 wbc_status = wbcSetGidMapping(idval, &sid);
174 if (!WBC_ERROR_IS_OK(wbc_status)) {
176 _("Could not set mapping of %s %lu to sid %s: %s\n"),
177 (type == ID_TYPE_GID) ? "GID" : "UID",
179 wbcErrorString(wbc_status));
184 if (input != stdin) {
192 /***********************************************************
193 Delete a SID mapping from a winbindd_idmap.tdb
194 **********************************************************/
195 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
197 d_printf(_("Not implemented yet"),"\n");
201 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
203 d_printf(_("Not implemented yet"),"\n");
206 bool idmap_store_secret(const char *backend, bool alloc,
207 const char *domain, const char *identity,
215 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
217 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
220 if (r < 0) return false;
222 strupper_m(tmp); /* make sure the key is case insensitive */
223 ret = secrets_store_generic(tmp, identity, secret);
230 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
240 if (argc != 2 || c->display_usage) {
241 d_printf(_("Usage:\n"),
242 _("net idmap secret {<DOMAIN>|alloc} <secret>\n"
243 " Set the secret for the specified domain "
244 "(or alloc module)\n"
245 " DOMAIN\tDomain to set secret for.\n"
246 " alloc\tSet secret for the alloc module\n"
247 " secret\tNew secret to set.\n"));
248 return c->display_usage?0:-1;
253 ctx = talloc_new(NULL);
256 if (strcmp(argv[0], "alloc") == 0) {
258 backend = lp_idmap_alloc_backend();
260 domain = talloc_strdup(ctx, argv[0]);
263 opt = talloc_asprintf(ctx, "idmap config %s", domain);
266 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
267 ALLOC_CHECK(backend);
270 if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
272 _("The only currently supported backend is LDAP\n"));
279 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
282 _("Missing ldap_user_dn option for domain "
288 ret = idmap_store_secret("ldap", false, domain, dn, secret);
290 dn = lp_parm_const_string(-1, "idmap alloc config", "ldap_user_dn", NULL);
293 _("Missing ldap_user_dn option for alloc "
299 ret = idmap_store_secret("ldap", true, NULL, dn, secret);
303 d_fprintf(stderr, _("Failed to store secret\n"));
308 d_printf(_("Secret stored\n"));
312 int net_help_idmap(struct net_context *c, int argc, const char **argv)
314 d_printf(_("net idmap dump <inputfile>\n"
315 " Dump current id mapping\n"));
317 d_printf(_("net idmap restore\n"
318 " Restore entries from stdin\n"));
320 /* Deliberately *not* document net idmap delete */
322 d_printf(_("net idmap secret <DOMAIN>|alloc <secret>\n"
323 " Set the secret for the specified DOMAIN (or the alloc "
329 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
333 DOM_SID src_sid, dst_sid;
335 struct db_context *db;
336 struct db_record *rec;
339 if (argc != 3 || c->display_usage) {
340 d_fprintf(stderr, _("Usage:")," net idmap aclmapset <tdb> "
341 "<src-sid> <dst-sid>\n");
345 if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
346 d_fprintf(stderr, _("talloc_init failed\n"));
350 if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
351 O_RDWR|O_CREAT, 0600))) {
352 d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
356 if (!string_to_sid(&src_sid, argv[1])) {
357 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
361 if (!string_to_sid(&dst_sid, argv[2])) {
362 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
366 if (!(src = sid_string_talloc(mem_ctx, &src_sid))
367 || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
368 d_fprintf(stderr, _("talloc_strdup failed\n"));
372 if (!(rec = db->fetch_locked(
373 db, mem_ctx, string_term_tdb_data(src)))) {
374 d_fprintf(stderr, _("could not fetch db record\n"));
378 status = rec->store(rec, string_term_tdb_data(dst), 0);
381 if (!NT_STATUS_IS_OK(status)) {
382 d_fprintf(stderr, _("could not store record: %s\n"),
389 TALLOC_FREE(mem_ctx);
393 /***********************************************************
394 Look at the current idmap
395 **********************************************************/
396 int net_idmap(struct net_context *c, int argc, const char **argv)
398 struct functable func[] = {
403 N_("Dump the current ID mappings"),
404 N_("net idmap dump\n"
405 " Dump the current ID mappings")
411 N_("Restore entries from stdin"),
412 N_("net idmap restore\n"
413 " Restore entries from stdin")
419 N_("Not implemented yet"),
420 N_("net idmap setmap\n"
421 " Not implemented yet")
427 N_("Not implemented yet"),
428 N_("net idmap delete\n"
429 " Not implemented yet")
435 N_("Set secret for specified domain"),
436 N_("net idmap secret {<DOMAIN>|alloc} <secret>\n"
437 " Set secret for specified domain or alloc module")
444 N_("net idmap aclmapset\n"
447 {NULL, NULL, 0, NULL, NULL}
450 return net_run_function(c, argc, argv, "net idmap", func);