net: more whitespace cleanup
[Samba.git] / source / utils / net_idmap.c
blobf3d0c30a4d9139c9a522bb05fa55af6462e39843
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 )
63 return net_help_idmap(c, argc, argv );
65 idmap_tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
67 if (idmap_tdb == NULL) {
68 d_fprintf(stderr, "Could not open idmap: %s\n", argv[0]);
69 return -1;
72 tdb_traverse(idmap_tdb, net_idmap_dump_one_entry, NULL);
74 tdb_close(idmap_tdb);
76 return 0;
79 /***********************************************************
80 Write entries from stdin to current local idmap
81 **********************************************************/
83 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
85 TALLOC_CTX *ctx;
86 FILE *input;
88 if (! winbind_ping()) {
89 d_fprintf(stderr, "To use net idmap Winbindd must be running.\n");
90 return -1;
93 ctx = talloc_new(NULL);
94 ALLOC_CHECK(ctx);
96 if (argc == 1) {
97 input = fopen(argv[0], "r");
98 } else {
99 input = stdin;
102 while (!feof(input)) {
103 char line[128], sid_string[128];
104 int len;
105 struct wbcDomainSid sid;
106 enum id_type type = ID_TYPE_NOT_SPECIFIED;
107 unsigned long idval;
108 wbcErr wbc_status;
110 if (fgets(line, 127, input) == NULL)
111 break;
113 len = strlen(line);
115 if ( (len > 0) && (line[len-1] == '\n') )
116 line[len-1] = '\0';
118 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2) {
119 type = ID_TYPE_GID;
120 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2) {
121 type = ID_TYPE_UID;
122 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
123 /* set uid hwm */
124 wbc_status = wbcSetUidHwm(idval);
125 if (!WBC_ERROR_IS_OK(wbc_status)) {
126 d_fprintf(stderr, "Could not set USER HWM: %s\n",
127 wbcErrorString(wbc_status));
129 continue;
130 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
131 /* set gid hwm */
132 wbc_status = wbcSetGidHwm(idval);
133 if (!WBC_ERROR_IS_OK(wbc_status)) {
134 d_fprintf(stderr, "Could not set GROUP HWM: %s\n",
135 wbcErrorString(wbc_status));
137 continue;
138 } else {
139 d_fprintf(stderr, "ignoring invalid line [%s]\n", line);
140 continue;
143 wbc_status = wbcStringToSid(sid_string, &sid);
144 if (!WBC_ERROR_IS_OK(wbc_status)) {
145 d_fprintf(stderr, "ignoring invalid sid [%s]: %s\n",
146 sid_string, wbcErrorString(wbc_status));
147 continue;
150 if (type == ID_TYPE_UID) {
151 wbc_status = wbcSetUidMapping(idval, &sid);
152 } else {
153 wbc_status = wbcSetGidMapping(idval, &sid);
155 if (!WBC_ERROR_IS_OK(wbc_status)) {
156 d_fprintf(stderr, "Could not set mapping of %s %lu to sid %s: %s\n",
157 (type == ID_TYPE_GID) ? "GID" : "UID",
158 idval, sid_string,
159 wbcErrorString(wbc_status));
160 continue;
164 if (input != stdin) {
165 fclose(input);
168 talloc_free(ctx);
169 return 0;
172 /***********************************************************
173 Delete a SID mapping from a winbindd_idmap.tdb
174 **********************************************************/
175 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
177 d_printf("Not Implemented yet\n");
178 return -1;
181 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
183 d_printf("Not Implemented yet\n");
184 return -1;
186 bool idmap_store_secret(const char *backend, bool alloc,
187 const char *domain, const char *identity,
188 const char *secret)
190 char *tmp;
191 int r;
192 bool ret;
194 if (alloc) {
195 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
196 } else {
197 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
200 if (r < 0) return false;
202 strupper_m(tmp); /* make sure the key is case insensitive */
203 ret = secrets_store_generic(tmp, identity, secret);
205 free(tmp);
206 return ret;
210 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
212 TALLOC_CTX *ctx;
213 const char *secret;
214 const char *dn;
215 char *domain;
216 char *backend;
217 char *opt = NULL;
218 bool ret;
220 if (argc != 2) {
221 return net_help_idmap(c, argc, argv);
224 secret = argv[1];
226 ctx = talloc_new(NULL);
227 ALLOC_CHECK(ctx);
229 if (strcmp(argv[0], "alloc") == 0) {
230 domain = NULL;
231 backend = lp_idmap_alloc_backend();
232 } else {
233 domain = talloc_strdup(ctx, argv[0]);
234 ALLOC_CHECK(domain);
236 opt = talloc_asprintf(ctx, "idmap config %s", domain);
237 ALLOC_CHECK(opt);
239 backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
240 ALLOC_CHECK(backend);
243 if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
244 d_fprintf(stderr, "The only currently supported backend is LDAP\n");
245 talloc_free(ctx);
246 return -1;
249 if (domain) {
251 dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
252 if ( ! dn) {
253 d_fprintf(stderr, "Missing ldap_user_dn option for domain %s\n", domain);
254 talloc_free(ctx);
255 return -1;
258 ret = idmap_store_secret("ldap", false, domain, dn, secret);
259 } else {
260 dn = lp_parm_const_string(-1, "idmap alloc config", "ldap_user_dn", NULL);
261 if ( ! dn) {
262 d_fprintf(stderr, "Missing ldap_user_dn option for alloc backend\n");
263 talloc_free(ctx);
264 return -1;
267 ret = idmap_store_secret("ldap", true, NULL, dn, secret);
270 if ( ! ret) {
271 d_fprintf(stderr, "Failed to store secret\n");
272 talloc_free(ctx);
273 return -1;
276 d_printf("Secret stored\n");
277 return 0;
280 int net_help_idmap(struct net_context *c, int argc, const char **argv)
282 d_printf("net idmap dump <inputfile>\n"\
283 " Dump current id mapping\n");
285 d_printf("net idmap restore\n"\
286 " Restore entries from stdin\n");
288 /* Deliberately *not* document net idmap delete */
290 d_printf("net idmap secret <DOMAIN>|alloc <secret>\n"\
291 " Set the secret for the specified DOMAIN (or the alloc module)\n");
293 return -1;
296 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
298 TALLOC_CTX *mem_ctx;
299 int result = -1;
300 DOM_SID src_sid, dst_sid;
301 char *src, *dst;
302 struct db_context *db;
303 struct db_record *rec;
304 NTSTATUS status;
306 if (argc != 3) {
307 d_fprintf(stderr, "usage: net idmap aclmapset <tdb> "
308 "<src-sid> <dst-sid>\n");
309 return -1;
312 if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
313 d_fprintf(stderr, "talloc_init failed\n");
314 return -1;
317 if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
318 O_RDWR|O_CREAT, 0600))) {
319 d_fprintf(stderr, "db_open failed: %s\n", strerror(errno));
320 goto fail;
323 if (!string_to_sid(&src_sid, argv[1])) {
324 d_fprintf(stderr, "%s is not a valid sid\n", argv[1]);
325 goto fail;
328 if (!string_to_sid(&dst_sid, argv[2])) {
329 d_fprintf(stderr, "%s is not a valid sid\n", argv[2]);
330 goto fail;
333 if (!(src = sid_string_talloc(mem_ctx, &src_sid))
334 || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
335 d_fprintf(stderr, "talloc_strdup failed\n");
336 goto fail;
339 if (!(rec = db->fetch_locked(
340 db, mem_ctx, string_term_tdb_data(src)))) {
341 d_fprintf(stderr, "could not fetch db record\n");
342 goto fail;
345 status = rec->store(rec, string_term_tdb_data(dst), 0);
346 TALLOC_FREE(rec);
348 if (!NT_STATUS_IS_OK(status)) {
349 d_fprintf(stderr, "could not store record: %s\n",
350 nt_errstr(status));
351 goto fail;
354 result = 0;
355 fail:
356 TALLOC_FREE(mem_ctx);
357 return result;
360 /***********************************************************
361 Look at the current idmap
362 **********************************************************/
363 int net_idmap(struct net_context *c, int argc, const char **argv)
365 struct functable func[] = {
366 {"dump", net_idmap_dump},
367 {"restore", net_idmap_restore},
368 {"setmap", net_idmap_set },
369 {"delete", net_idmap_delete},
370 {"secret", net_idmap_secret},
371 {"aclmapset", net_idmap_aclmapset},
372 {"help", net_help_idmap},
373 {NULL, NULL}
376 return net_run_function(c, argc, argv, func, net_help_idmap);