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"
26 #include "../libcli/security/security.h"
28 #define ALLOC_CHECK(mem) do { \
30 d_fprintf(stderr, _("Out of memory!\n")); \
35 /***********************************************************
36 Helper function for net_idmap_dump. Dump one entry.
37 **********************************************************/
38 static int net_idmap_dump_one_entry(struct db_record
*rec
,
41 if (strcmp((char *)rec
->key
.dptr
, "USER HWM") == 0) {
42 printf(_("USER HWM %d\n"), IVAL(rec
->value
.dptr
,0));
46 if (strcmp((char *)rec
->key
.dptr
, "GROUP HWM") == 0) {
47 printf(_("GROUP HWM %d\n"), IVAL(rec
->value
.dptr
,0));
51 if (strncmp((char *)rec
->key
.dptr
, "S-", 2) != 0)
54 printf("%s %s\n", rec
->value
.dptr
, rec
->key
.dptr
);
58 /***********************************************************
59 Dump the current idmap
60 **********************************************************/
61 static int net_idmap_dump(struct net_context
*c
, int argc
, const char **argv
)
63 struct db_context
*db
;
66 if ( argc
!= 1 || c
->display_usage
) {
69 _("net idmap dump <inputfile>\n"
70 " Dump current ID mapping.\n"
71 " inputfile\tTDB file to read mappings from.\n"));
72 return c
->display_usage
?0:-1;
75 mem_ctx
= talloc_stackframe();
77 db
= db_open(mem_ctx
, argv
[0], 0, TDB_DEFAULT
, O_RDONLY
, 0);
79 d_fprintf(stderr
, _("Could not open idmap db (%s): %s\n"),
80 argv
[0], strerror(errno
));
85 db
->traverse_read(db
, net_idmap_dump_one_entry
, NULL
);
92 /***********************************************************
93 Write entries from stdin to current local idmap
94 **********************************************************/
96 static int net_idmap_store_id_mapping(struct db_context
*db
,
99 const char *sid_string
)
106 idstr
= talloc_asprintf(talloc_tos(), "UID %lu", idval
);
109 idstr
= talloc_asprintf(talloc_tos(), "GID %lu", idval
);
112 d_fprintf(stderr
, "Invalid id mapping type: %d\n", type
);
116 status
= dbwrap_store_bystring(db
, idstr
,
117 string_term_tdb_data(sid_string
),
119 if (!NT_STATUS_IS_OK(status
)) {
120 d_fprintf(stderr
, "Error storing ID -> SID: "
121 "%s\n", nt_errstr(status
));
125 status
= dbwrap_store_bystring(db
, sid_string
,
126 string_term_tdb_data(idstr
),
128 if (!NT_STATUS_IS_OK(status
)) {
129 d_fprintf(stderr
, "Error storing SID -> ID: "
130 "%s\n", nt_errstr(status
));
138 static int net_idmap_restore(struct net_context
*c
, int argc
, const char **argv
)
142 struct db_context
*db
;
146 if (c
->display_usage
) {
149 _("net idmap restore [<inputfile>]\n"
150 " Restore ID mappings from file\n"
151 " inputfile\tFile to load ID mappings from. If not "
152 "given, load data from stdin.\n"));
156 mem_ctx
= talloc_stackframe();
158 if (strequal(lp_idmap_backend(), "tdb")) {
159 dbfile
= state_path("winbindd_idmap.tdb");
160 if (dbfile
== NULL
) {
161 d_fprintf(stderr
, _("Out of memory!\n"));
164 } else if (strequal(lp_idmap_backend(), "tdb2")) {
165 dbfile
= lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL
);
166 if (dbfile
== NULL
) {
167 dbfile
= talloc_asprintf(mem_ctx
, "%s/idmap2.tdb",
171 char *backend
, *args
;
173 backend
= talloc_strdup(mem_ctx
, lp_idmap_backend());
174 args
= strchr(backend
, ':');
179 d_printf(_("Sorry, 'net idmap restore' is currently not "
180 "supported for idmap backend = %s.\n"
181 "Only tdb and tdb2 are supported.\n"),
188 d_fprintf(stderr
, _("restoring id mapping to %s data base in '%s'\n"),
189 lp_idmap_backend(), dbfile
);
192 input
= fopen(argv
[0], "r");
197 db
= db_open(mem_ctx
, dbfile
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0644);
199 d_fprintf(stderr
, _("Could not open idmap db (%s): %s\n"),
200 dbfile
, strerror(errno
));
205 if (db
->transaction_start(db
) != 0) {
206 d_fprintf(stderr
, _("Failed to start transaction.\n"));
211 while (!feof(input
)) {
212 char line
[128], sid_string
[128];
216 if (fgets(line
, 127, input
) == NULL
)
221 if ( (len
> 0) && (line
[len
-1] == '\n') )
224 if (sscanf(line
, "GID %lu %128s", &idval
, sid_string
) == 2)
226 ret
= net_idmap_store_id_mapping(db
, ID_TYPE_GID
,
231 } else if (sscanf(line
, "UID %lu %128s", &idval
, sid_string
) == 2)
233 ret
= net_idmap_store_id_mapping(db
, ID_TYPE_UID
,
238 } else if (sscanf(line
, "USER HWM %lu", &idval
) == 1) {
239 ret
= dbwrap_store_int32(db
, "USER HWM", idval
);
241 d_fprintf(stderr
, _("Could not store USER HWM.\n"));
244 } else if (sscanf(line
, "GROUP HWM %lu", &idval
) == 1) {
245 ret
= dbwrap_store_int32(db
, "GROUP HWM", idval
);
248 _("Could not store GROUP HWM.\n"));
252 d_fprintf(stderr
, _("ignoring invalid line [%s]\n"),
259 if(db
->transaction_commit(db
) != 0) {
260 d_fprintf(stderr
, _("Failed to commit transaction.\n"));
264 if (db
->transaction_cancel(db
) != 0) {
265 d_fprintf(stderr
, _("Failed to cancel transaction.\n"));
270 if ((input
!= NULL
) && (input
!= stdin
)) {
274 talloc_free(mem_ctx
);
278 /***********************************************************
279 Delete a SID mapping from a winbindd_idmap.tdb
280 **********************************************************/
281 static int net_idmap_delete(struct net_context
*c
, int argc
, const char **argv
)
283 d_printf("%s\n", _("Not implemented yet"));
287 static int net_idmap_set(struct net_context
*c
, int argc
, const char **argv
)
289 d_printf("%s\n", _("Not implemented yet"));
292 static bool idmap_store_secret(const char *backend
,
294 const char *identity
,
301 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", 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> <secret>\n"
386 " Set the secret for the specified DOMAIN module)\n"));
391 static int net_idmap_aclmapset(struct net_context
*c
, int argc
, const char **argv
)
395 struct dom_sid src_sid
, dst_sid
;
397 struct db_context
*db
;
398 struct db_record
*rec
;
401 if (argc
!= 3 || c
->display_usage
) {
402 d_fprintf(stderr
, "%s net idmap aclmapset <tdb> "
403 "<src-sid> <dst-sid>\n", _("Usage:"));
407 if (!(mem_ctx
= talloc_init("net idmap aclmapset"))) {
408 d_fprintf(stderr
, _("talloc_init failed\n"));
412 if (!(db
= db_open(mem_ctx
, argv
[0], 0, TDB_DEFAULT
,
413 O_RDWR
|O_CREAT
, 0600))) {
414 d_fprintf(stderr
, _("db_open failed: %s\n"), strerror(errno
));
418 if (!string_to_sid(&src_sid
, argv
[1])) {
419 d_fprintf(stderr
, _("%s is not a valid sid\n"), argv
[1]);
423 if (!string_to_sid(&dst_sid
, argv
[2])) {
424 d_fprintf(stderr
, _("%s is not a valid sid\n"), argv
[2]);
428 if (!(src
= sid_string_talloc(mem_ctx
, &src_sid
))
429 || !(dst
= sid_string_talloc(mem_ctx
, &dst_sid
))) {
430 d_fprintf(stderr
, _("talloc_strdup failed\n"));
434 if (!(rec
= db
->fetch_locked(
435 db
, mem_ctx
, string_term_tdb_data(src
)))) {
436 d_fprintf(stderr
, _("could not fetch db record\n"));
440 status
= rec
->store(rec
, string_term_tdb_data(dst
), 0);
443 if (!NT_STATUS_IS_OK(status
)) {
444 d_fprintf(stderr
, _("could not store record: %s\n"),
451 TALLOC_FREE(mem_ctx
);
455 /***********************************************************
456 Look at the current idmap
457 **********************************************************/
458 int net_idmap(struct net_context
*c
, int argc
, const char **argv
)
460 struct functable func
[] = {
465 N_("Dump the current ID mappings"),
466 N_("net idmap dump\n"
467 " Dump the current ID mappings")
473 N_("Restore entries from stdin"),
474 N_("net idmap restore\n"
475 " Restore entries from stdin")
481 N_("Not implemented yet"),
482 N_("net idmap setmap\n"
483 " Not implemented yet")
489 N_("Not implemented yet"),
490 N_("net idmap delete\n"
491 " Not implemented yet")
497 N_("Set secret for specified domain"),
498 N_("net idmap secret <DOMAIN> <secret>\n"
499 " Set secret for specified domain")
506 N_("net idmap aclmapset\n"
509 {NULL
, NULL
, 0, NULL
, NULL
}
512 return net_run_function(c
, argc
, argv
, "net idmap", func
);