pyldb: Raise proper exception when attempting to assign a string to a dn
[Samba/fernandojvsilva.git] / source3 / utils / net_idmap.c
blob5a380da02f12e901fa5b95489a1269a68d35f5bc
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 "utils/net.h"
23 #define ALLOC_CHECK(mem) do { \
24 if (!mem) { \
25 d_fprintf(stderr, _("Out of memory!\n")); \
26 talloc_free(ctx); \
27 return -1; \
28 } } while(0)
30 /***********************************************************
31 Helper function for net_idmap_dump. Dump one entry.
32 **********************************************************/
33 static int net_idmap_dump_one_entry(TDB_CONTEXT *tdb,
34 TDB_DATA key,
35 TDB_DATA data,
36 void *unused)
38 if (strcmp((char *)key.dptr, "USER HWM") == 0) {
39 printf(_("USER HWM %d\n"), IVAL(data.dptr,0));
40 return 0;
43 if (strcmp((char *)key.dptr, "GROUP HWM") == 0) {
44 printf(_("GROUP HWM %d\n"), IVAL(data.dptr,0));
45 return 0;
48 if (strncmp((char *)key.dptr, "S-", 2) != 0)
49 return 0;
51 printf("%s %s\n", data.dptr, key.dptr);
52 return 0;
55 /***********************************************************
56 Dump the current idmap
57 **********************************************************/
58 static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
60 TDB_CONTEXT *idmap_tdb;
62 if ( argc != 1 || c->display_usage) {
63 d_printf(_("Usage:\n"
64 "net idmap dump <inputfile>\n"
65 " Dump current ID mapping.\n"
66 " inputfile\tTDB file to read mappings from.\n"));
67 return c->display_usage?0:-1;
70 idmap_tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
72 if (idmap_tdb == NULL) {
73 d_fprintf(stderr, _("Could not open idmap: %s\n"), argv[0]);
74 return -1;
77 tdb_traverse(idmap_tdb, net_idmap_dump_one_entry, NULL);
79 tdb_close(idmap_tdb);
81 return 0;
84 /***********************************************************
85 Write entries from stdin to current local idmap
86 **********************************************************/
88 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
90 TALLOC_CTX *ctx;
91 FILE *input;
93 if (c->display_usage) {
94 d_printf(_("Usage:\n"
95 "net idmap restore [inputfile]\n"
96 " Restore ID mappings from file\n"
97 " inputfile\tFile to load ID mappings from. If "
98 "not given, load data from stdin.\n"));
99 return 0;
102 if (! winbind_ping()) {
103 d_fprintf(stderr,
104 _("To use net idmap Winbindd must be running.\n"));
105 return -1;
108 ctx = talloc_new(NULL);
109 ALLOC_CHECK(ctx);
111 if (argc == 1) {
112 input = fopen(argv[0], "r");
113 } else {
114 input = stdin;
117 while (!feof(input)) {
118 char line[128], sid_string[128];
119 int len;
120 struct wbcDomainSid sid;
121 enum id_type type = ID_TYPE_NOT_SPECIFIED;
122 unsigned long idval;
123 wbcErr wbc_status;
125 if (fgets(line, 127, input) == NULL)
126 break;
128 len = strlen(line);
130 if ( (len > 0) && (line[len-1] == '\n') )
131 line[len-1] = '\0';
133 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2) {
134 type = ID_TYPE_GID;
135 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2) {
136 type = ID_TYPE_UID;
137 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
138 /* set uid hwm */
139 wbc_status = wbcSetUidHwm(idval);
140 if (!WBC_ERROR_IS_OK(wbc_status)) {
141 d_fprintf(stderr,
142 _("Could not set USER HWM: %s\n"),
143 wbcErrorString(wbc_status));
145 continue;
146 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
147 /* set gid hwm */
148 wbc_status = wbcSetGidHwm(idval);
149 if (!WBC_ERROR_IS_OK(wbc_status)) {
150 d_fprintf(stderr,
151 _("Could not set GROUP HWM: %s\n"),
152 wbcErrorString(wbc_status));
154 continue;
155 } else {
156 d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
157 line);
158 continue;
161 wbc_status = wbcStringToSid(sid_string, &sid);
162 if (!WBC_ERROR_IS_OK(wbc_status)) {
163 d_fprintf(stderr, _("ignoring invalid sid [%s]: %s\n"),
164 sid_string, wbcErrorString(wbc_status));
165 continue;
168 if (type == ID_TYPE_UID) {
169 wbc_status = wbcSetUidMapping(idval, &sid);
170 } else {
171 wbc_status = wbcSetGidMapping(idval, &sid);
173 if (!WBC_ERROR_IS_OK(wbc_status)) {
174 d_fprintf(stderr,
175 _("Could not set mapping of %s %lu to sid %s: %s\n"),
176 (type == ID_TYPE_GID) ? "GID" : "UID",
177 idval, sid_string,
178 wbcErrorString(wbc_status));
179 continue;
183 if (input != stdin) {
184 fclose(input);
187 talloc_free(ctx);
188 return 0;
191 /***********************************************************
192 Delete a SID mapping from a winbindd_idmap.tdb
193 **********************************************************/
194 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
196 d_printf(_("Not Implemented yet\n"));
197 return -1;
200 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
202 d_printf(_("Not Implemented yet\n"));
203 return -1;
205 bool idmap_store_secret(const char *backend, bool alloc,
206 const char *domain, const char *identity,
207 const char *secret)
209 char *tmp;
210 int r;
211 bool ret;
213 if (alloc) {
214 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
215 } else {
216 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
219 if (r < 0) return false;
221 strupper_m(tmp); /* make sure the key is case insensitive */
222 ret = secrets_store_generic(tmp, identity, secret);
224 free(tmp);
225 return ret;
229 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
231 TALLOC_CTX *ctx;
232 const char *secret;
233 const char *dn;
234 char *domain;
235 char *backend;
236 char *opt = NULL;
237 bool ret;
239 if (argc != 2 || c->display_usage) {
240 d_printf(_("Usage:\n"
241 "net idmap secret {<DOMAIN>|alloc} <secret>\n"
242 " Set the secret for the specified domain "
243 "(or alloc module)\n"
244 " DOMAIN\tDomain to set secret for.\n"
245 " alloc\tSet secret for the alloc module\n"
246 " secret\tNew secret to set.\n"));
247 return c->display_usage?0:-1;
250 secret = argv[1];
252 ctx = talloc_new(NULL);
253 ALLOC_CHECK(ctx);
255 if (strcmp(argv[0], "alloc") == 0) {
256 domain = NULL;
257 backend = lp_idmap_alloc_backend();
258 } else {
259 domain = talloc_strdup(ctx, argv[0]);
260 ALLOC_CHECK(domain);
262 opt = talloc_asprintf(ctx, "idmap config %s", domain);
263 ALLOC_CHECK(opt);
265 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
266 ALLOC_CHECK(backend);
269 if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
270 d_fprintf(stderr,
271 _("The only currently supported backend is LDAP\n"));
272 talloc_free(ctx);
273 return -1;
276 if (domain) {
278 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
279 if ( ! dn) {
280 d_fprintf(stderr,
281 _("Missing ldap_user_dn option for domain "
282 "%s\n"), domain);
283 talloc_free(ctx);
284 return -1;
287 ret = idmap_store_secret("ldap", false, domain, dn, secret);
288 } else {
289 dn = lp_parm_const_string(-1, "idmap alloc config", "ldap_user_dn", NULL);
290 if ( ! dn) {
291 d_fprintf(stderr,
292 _("Missing ldap_user_dn option for alloc "
293 "backend\n"));
294 talloc_free(ctx);
295 return -1;
298 ret = idmap_store_secret("ldap", true, NULL, dn, secret);
301 if ( ! ret) {
302 d_fprintf(stderr, _("Failed to store secret\n"));
303 talloc_free(ctx);
304 return -1;
307 d_printf(_("Secret stored\n"));
308 return 0;
311 int net_help_idmap(struct net_context *c, int argc, const char **argv)
313 d_printf(_("net idmap dump <inputfile>\n"
314 " Dump current id mapping\n"));
316 d_printf(_("net idmap restore\n"
317 " Restore entries from stdin\n"));
319 /* Deliberately *not* document net idmap delete */
321 d_printf(_("net idmap secret <DOMAIN>|alloc <secret>\n"
322 " Set the secret for the specified DOMAIN (or the alloc "
323 "module)\n"));
325 return -1;
328 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
330 TALLOC_CTX *mem_ctx;
331 int result = -1;
332 DOM_SID src_sid, dst_sid;
333 char *src, *dst;
334 struct db_context *db;
335 struct db_record *rec;
336 NTSTATUS status;
338 if (argc != 3 || c->display_usage) {
339 d_fprintf(stderr, _("usage: net idmap aclmapset <tdb> "
340 "<src-sid> <dst-sid>\n"));
341 return -1;
344 if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
345 d_fprintf(stderr, _("talloc_init failed\n"));
346 return -1;
349 if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
350 O_RDWR|O_CREAT, 0600))) {
351 d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
352 goto fail;
355 if (!string_to_sid(&src_sid, argv[1])) {
356 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
357 goto fail;
360 if (!string_to_sid(&dst_sid, argv[2])) {
361 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
362 goto fail;
365 if (!(src = sid_string_talloc(mem_ctx, &src_sid))
366 || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
367 d_fprintf(stderr, _("talloc_strdup failed\n"));
368 goto fail;
371 if (!(rec = db->fetch_locked(
372 db, mem_ctx, string_term_tdb_data(src)))) {
373 d_fprintf(stderr, _("could not fetch db record\n"));
374 goto fail;
377 status = rec->store(rec, string_term_tdb_data(dst), 0);
378 TALLOC_FREE(rec);
380 if (!NT_STATUS_IS_OK(status)) {
381 d_fprintf(stderr, _("could not store record: %s\n"),
382 nt_errstr(status));
383 goto fail;
386 result = 0;
387 fail:
388 TALLOC_FREE(mem_ctx);
389 return result;
392 /***********************************************************
393 Look at the current idmap
394 **********************************************************/
395 int net_idmap(struct net_context *c, int argc, const char **argv)
397 struct functable func[] = {
399 "dump",
400 net_idmap_dump,
401 NET_TRANSPORT_LOCAL,
402 N_("Dump the current ID mappings"),
403 N_("net idmap dump\n"
404 " Dump the current ID mappings")
407 "restore",
408 net_idmap_restore,
409 NET_TRANSPORT_LOCAL,
410 N_("Restore entries from stdin"),
411 N_("net idmap restore\n"
412 " Restore entries from stdin")
415 "setmap",
416 net_idmap_set,
417 NET_TRANSPORT_LOCAL,
418 N_("Not implemented yet"),
419 N_("net idmap setmap\n"
420 " Not implemented yet")
423 "delete",
424 net_idmap_delete,
425 NET_TRANSPORT_LOCAL,
426 N_("Not implemented yet"),
427 N_("net idmap delete\n"
428 " Not implemented yet")
431 "secret",
432 net_idmap_secret,
433 NET_TRANSPORT_LOCAL,
434 N_("Set secret for specified domain"),
435 N_("net idmap secret {<DOMAIN>|alloc} <secret>\n"
436 " Set secret for specified domain or alloc module")
439 "aclmapset",
440 net_idmap_aclmapset,
441 NET_TRANSPORT_LOCAL,
442 N_("Set acl map"),
443 N_("net idmap aclmapset\n"
444 " Set acl map")
446 {NULL, NULL, 0, NULL, NULL}
449 return net_run_function(c, argc, argv, "net idmap", func);