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 "utils/net.h"
22 #define ALLOC_CHECK(mem) do { \
24 d_fprintf(stderr, "Out of memory!\n"); \
29 /***********************************************************
30 Helper function for net_idmap_dump. Dump one entry.
31 **********************************************************/
32 static int net_idmap_dump_one_entry(TDB_CONTEXT
*tdb
,
37 if (strcmp((char *)key
.dptr
, "USER HWM") == 0) {
38 printf("USER HWM %d\n", IVAL(data
.dptr
,0));
42 if (strcmp((char *)key
.dptr
, "GROUP HWM") == 0) {
43 printf("GROUP HWM %d\n", IVAL(data
.dptr
,0));
47 if (strncmp((char *)key
.dptr
, "S-", 2) != 0)
50 printf("%s %s\n", data
.dptr
, key
.dptr
);
54 /***********************************************************
55 Dump the current idmap
56 **********************************************************/
57 static int net_idmap_dump(int argc
, const char **argv
)
59 TDB_CONTEXT
*idmap_tdb
;
62 return net_help_idmap( argc
, argv
);
64 idmap_tdb
= tdb_open_log(argv
[0], 0, TDB_DEFAULT
, O_RDONLY
, 0);
66 if (idmap_tdb
== NULL
) {
67 d_fprintf(stderr
, "Could not open idmap: %s\n", argv
[0]);
71 tdb_traverse(idmap_tdb
, net_idmap_dump_one_entry
, NULL
);
78 /***********************************************************
79 Write entries from stdin to current local idmap
80 **********************************************************/
82 static int net_idmap_restore(int argc
, const char **argv
)
87 if (! winbind_ping()) {
88 d_fprintf(stderr
, "To use net idmap Winbindd must be running.\n");
92 ctx
= talloc_new(NULL
);
96 input
= fopen(argv
[0], "r");
101 while (!feof(input
)) {
102 char line
[128], sid_string
[128];
108 if (fgets(line
, 127, input
) == NULL
)
113 if ( (len
> 0) && (line
[len
-1] == '\n') )
116 if (sscanf(line
, "GID %lu %128s", &idval
, sid_string
) == 2) {
117 map
.xid
.type
= ID_TYPE_GID
;
119 } else if (sscanf(line
, "UID %lu %128s", &idval
, sid_string
) == 2) {
120 map
.xid
.type
= ID_TYPE_UID
;
122 } else if (sscanf(line
, "USER HWM %lu", &idval
) == 1) {
124 if (! winbind_set_uid_hwm(idval
)) {
125 d_fprintf(stderr
, "Could not set USER HWM\n");
128 } else if (sscanf(line
, "GROUP HWM %lu", &idval
) == 1) {
130 if (! winbind_set_gid_hwm(idval
)) {
131 d_fprintf(stderr
, "Could not set GROUP HWM\n");
135 d_fprintf(stderr
, "ignoring invalid line [%s]\n", line
);
139 if (!string_to_sid(&sid
, sid_string
)) {
140 d_fprintf(stderr
, "ignoring invalid sid [%s]\n", sid_string
);
145 if (!winbind_set_mapping(&map
)) {
146 d_fprintf(stderr
, "Could not set mapping of %s %lu to sid %s\n",
147 (map
.xid
.type
== ID_TYPE_GID
) ? "GID" : "UID",
148 (unsigned long)map
.xid
.id
,
149 sid_string_tos(map
.sid
));
155 if (input
!= stdin
) {
163 /***********************************************************
164 Delete a SID mapping from a winbindd_idmap.tdb
165 **********************************************************/
166 static int net_idmap_delete(int argc
, const char **argv
)
168 d_printf("Not Implemented yet\n");
172 static int net_idmap_set(int argc
, const char **argv
)
174 d_printf("Not Implemented yet\n");
177 bool idmap_store_secret(const char *backend
, bool alloc
,
178 const char *domain
, const char *identity
,
186 r
= asprintf(&tmp
, "IDMAP_ALLOC_%s", backend
);
188 r
= asprintf(&tmp
, "IDMAP_%s_%s", backend
, domain
);
191 if (r
< 0) return false;
193 strupper_m(tmp
); /* make sure the key is case insensitive */
194 ret
= secrets_store_generic(tmp
, identity
, secret
);
201 static int net_idmap_secret(int argc
, const char **argv
)
212 return net_help_idmap(argc
, argv
);
217 ctx
= talloc_new(NULL
);
220 if (strcmp(argv
[0], "alloc") == 0) {
222 backend
= lp_idmap_alloc_backend();
224 domain
= talloc_strdup(ctx
, argv
[0]);
227 opt
= talloc_asprintf(ctx
, "idmap config %s", domain
);
230 backend
= talloc_strdup(ctx
, lp_parm_const_string(-1, opt
, "backend", "tdb"));
231 ALLOC_CHECK(backend
);
234 if ( ( ! backend
) || ( ! strequal(backend
, "ldap"))) {
235 d_fprintf(stderr
, "The only currently supported backend is LDAP\n");
242 dn
= lp_parm_const_string(-1, opt
, "ldap_user_dn", NULL
);
244 d_fprintf(stderr
, "Missing ldap_user_dn option for domain %s\n", domain
);
249 ret
= idmap_store_secret("ldap", false, domain
, dn
, secret
);
251 dn
= lp_parm_const_string(-1, "idmap alloc config", "ldap_user_dn", NULL
);
253 d_fprintf(stderr
, "Missing ldap_user_dn option for alloc backend\n");
258 ret
= idmap_store_secret("ldap", true, NULL
, dn
, secret
);
262 d_fprintf(stderr
, "Failed to store secret\n");
267 d_printf("Secret stored\n");
271 int net_help_idmap(int argc
, const char **argv
)
273 d_printf("net idmap dump <inputfile>\n"\
274 " Dump current id mapping\n");
276 d_printf("net idmap restore\n"\
277 " Restore entries from stdin\n");
279 /* Deliberately *not* document net idmap delete */
281 d_printf("net idmap secret <DOMAIN>|alloc <secret>\n"\
282 " Set the secret for the specified DOMAIN (or the alloc module)\n");
287 static int net_idmap_aclmapset(int argc
, const char **argv
)
291 DOM_SID src_sid
, dst_sid
;
293 struct db_context
*db
;
294 struct db_record
*rec
;
298 d_fprintf(stderr
, "usage: net idmap aclmapset <tdb> "
299 "<src-sid> <dst-sid>\n");
303 if (!(mem_ctx
= talloc_init("net idmap aclmapset"))) {
304 d_fprintf(stderr
, "talloc_init failed\n");
308 if (!(db
= db_open(mem_ctx
, argv
[0], 0, TDB_DEFAULT
,
309 O_RDWR
|O_CREAT
, 0600))) {
310 d_fprintf(stderr
, "db_open failed: %s\n", strerror(errno
));
314 if (!string_to_sid(&src_sid
, argv
[1])) {
315 d_fprintf(stderr
, "%s is not a valid sid\n", argv
[1]);
319 if (!string_to_sid(&dst_sid
, argv
[2])) {
320 d_fprintf(stderr
, "%s is not a valid sid\n", argv
[2]);
324 if (!(src
= sid_string_talloc(mem_ctx
, &src_sid
))
325 || !(dst
= sid_string_talloc(mem_ctx
, &dst_sid
))) {
326 d_fprintf(stderr
, "talloc_strdup failed\n");
330 if (!(rec
= db
->fetch_locked(
331 db
, mem_ctx
, string_term_tdb_data(src
)))) {
332 d_fprintf(stderr
, "could not fetch db record\n");
336 status
= rec
->store(rec
, string_term_tdb_data(dst
), 0);
339 if (!NT_STATUS_IS_OK(status
)) {
340 d_fprintf(stderr
, "could not store record: %s\n",
347 TALLOC_FREE(mem_ctx
);
351 /***********************************************************
352 Look at the current idmap
353 **********************************************************/
354 int net_idmap(int argc
, const char **argv
)
356 struct functable func
[] = {
357 {"dump", net_idmap_dump
},
358 {"restore", net_idmap_restore
},
359 {"setmap", net_idmap_set
},
360 {"delete", net_idmap_delete
},
361 {"secret", net_idmap_secret
},
362 {"aclmapset", net_idmap_aclmapset
},
363 {"help", net_help_idmap
},
367 return net_run_function(argc
, argv
, func
, net_help_idmap
);