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_restore(struct net_context *c, int argc, const char **argv)
98 if (c->display_usage) {
101 _("net idmap restore [inputfile]\n"
102 " Restore ID mappings from file\n"
103 " inputfile\tFile to load ID mappings from. If "
104 "not given, load data from stdin.\n"));
108 if (! winbind_ping()) {
110 _("To use net idmap Winbindd must be running.\n"));
114 ctx = talloc_new(NULL);
118 input = fopen(argv[0], "r");
123 while (!feof(input)) {
124 char line[128], sid_string[128];
126 struct wbcDomainSid sid;
127 enum id_type type = ID_TYPE_NOT_SPECIFIED;
131 if (fgets(line, 127, input) == NULL)
136 if ( (len > 0) && (line[len-1] == '\n') )
139 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2) {
141 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2) {
143 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
145 wbc_status = wbcSetUidHwm(idval);
146 if (!WBC_ERROR_IS_OK(wbc_status)) {
148 _("Could not set USER HWM: %s\n"),
149 wbcErrorString(wbc_status));
152 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
154 wbc_status = wbcSetGidHwm(idval);
155 if (!WBC_ERROR_IS_OK(wbc_status)) {
157 _("Could not set GROUP HWM: %s\n"),
158 wbcErrorString(wbc_status));
162 d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
167 wbc_status = wbcStringToSid(sid_string, &sid);
168 if (!WBC_ERROR_IS_OK(wbc_status)) {
169 d_fprintf(stderr, _("ignoring invalid sid [%s]: %s\n"),
170 sid_string, wbcErrorString(wbc_status));
174 if (type == ID_TYPE_UID) {
175 wbc_status = wbcSetUidMapping(idval, &sid);
177 wbc_status = wbcSetGidMapping(idval, &sid);
179 if (!WBC_ERROR_IS_OK(wbc_status)) {
181 _("Could not set mapping of %s %lu to sid %s: %s\n"),
182 (type == ID_TYPE_GID) ? "GID" : "UID",
184 wbcErrorString(wbc_status));
189 if (input != stdin) {
197 /***********************************************************
198 Delete a SID mapping from a winbindd_idmap.tdb
199 **********************************************************/
200 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
202 d_printf("%s\n", _("Not implemented yet"));
206 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
208 d_printf("%s\n", _("Not implemented yet"));
211 bool idmap_store_secret(const char *backend, bool alloc,
212 const char *domain, const char *identity,
220 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
222 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
225 if (r < 0) return false;
227 strupper_m(tmp); /* make sure the key is case insensitive */
228 ret = secrets_store_generic(tmp, identity, secret);
235 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
245 if (argc != 2 || c->display_usage) {
248 _("net idmap secret {<DOMAIN>|alloc} <secret>\n"
249 " Set the secret for the specified domain "
250 "(or alloc module)\n"
251 " DOMAIN\tDomain to set secret for.\n"
252 " alloc\tSet secret for the alloc module\n"
253 " secret\tNew secret to set.\n"));
254 return c->display_usage?0:-1;
259 ctx = talloc_new(NULL);
262 if (strcmp(argv[0], "alloc") == 0) {
264 backend = lp_idmap_alloc_backend();
266 domain = talloc_strdup(ctx, argv[0]);
269 opt = talloc_asprintf(ctx, "idmap config %s", domain);
272 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
273 ALLOC_CHECK(backend);
276 if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
278 _("The only currently supported backend is LDAP\n"));
285 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
288 _("Missing ldap_user_dn option for domain "
294 ret = idmap_store_secret("ldap", false, domain, dn, secret);
296 dn = lp_parm_const_string(-1, "idmap alloc config", "ldap_user_dn", NULL);
299 _("Missing ldap_user_dn option for alloc "
305 ret = idmap_store_secret("ldap", true, NULL, dn, secret);
309 d_fprintf(stderr, _("Failed to store secret\n"));
314 d_printf(_("Secret stored\n"));
318 int net_help_idmap(struct net_context *c, int argc, const char **argv)
320 d_printf(_("net idmap dump <inputfile>\n"
321 " Dump current id mapping\n"));
323 d_printf(_("net idmap restore\n"
324 " Restore entries from stdin\n"));
326 /* Deliberately *not* document net idmap delete */
328 d_printf(_("net idmap secret <DOMAIN>|alloc <secret>\n"
329 " Set the secret for the specified DOMAIN (or the alloc "
335 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
339 struct dom_sid src_sid, dst_sid;
341 struct db_context *db;
342 struct db_record *rec;
345 if (argc != 3 || c->display_usage) {
346 d_fprintf(stderr, "%s net idmap aclmapset <tdb> "
347 "<src-sid> <dst-sid>\n", _("Usage:"));
351 if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
352 d_fprintf(stderr, _("talloc_init failed\n"));
356 if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
357 O_RDWR|O_CREAT, 0600))) {
358 d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
362 if (!string_to_sid(&src_sid, argv[1])) {
363 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
367 if (!string_to_sid(&dst_sid, argv[2])) {
368 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
372 if (!(src = sid_string_talloc(mem_ctx, &src_sid))
373 || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
374 d_fprintf(stderr, _("talloc_strdup failed\n"));
378 if (!(rec = db->fetch_locked(
379 db, mem_ctx, string_term_tdb_data(src)))) {
380 d_fprintf(stderr, _("could not fetch db record\n"));
384 status = rec->store(rec, string_term_tdb_data(dst), 0);
387 if (!NT_STATUS_IS_OK(status)) {
388 d_fprintf(stderr, _("could not store record: %s\n"),
395 TALLOC_FREE(mem_ctx);
399 /***********************************************************
400 Look at the current idmap
401 **********************************************************/
402 int net_idmap(struct net_context *c, int argc, const char **argv)
404 struct functable func[] = {
409 N_("Dump the current ID mappings"),
410 N_("net idmap dump\n"
411 " Dump the current ID mappings")
417 N_("Restore entries from stdin"),
418 N_("net idmap restore\n"
419 " Restore entries from stdin")
425 N_("Not implemented yet"),
426 N_("net idmap setmap\n"
427 " Not implemented yet")
433 N_("Not implemented yet"),
434 N_("net idmap delete\n"
435 " Not implemented yet")
441 N_("Set secret for specified domain"),
442 N_("net idmap secret {<DOMAIN>|alloc} <secret>\n"
443 " Set secret for specified domain or alloc module")
450 N_("net idmap aclmapset\n"
453 {NULL, NULL, 0, NULL, NULL}
456 return net_run_function(c, argc, argv, "net idmap", func);