8bcb79fabf0146895224c4c74a0fd701eed597ce
[ira/wip.git] / source3 / utils / net_idmap.c
1 /*
2    Samba Unix/Linux SMB client library
3    Distributed SMB/CIFS Server Management Utility
4    Copyright (C) 2003 Andrew Bartlett (abartlet@samba.org)
5
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.
10
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.
15
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/>.
18 */
19
20 #define FOO(x) (x)
21 #include "includes.h"
22 #include "utils/net.h"
23 #include "secrets.h"
24 #include "idmap.h"
25
26 #define ALLOC_CHECK(mem) do { \
27         if (!mem) { \
28                 d_fprintf(stderr, _("Out of memory!\n")); \
29                 talloc_free(ctx); \
30                 return -1; \
31         } } while(0)
32
33 /***********************************************************
34  Helper function for net_idmap_dump. Dump one entry.
35  **********************************************************/
36 static int net_idmap_dump_one_entry(struct db_record *rec,
37                                     void *unused)
38 {
39         if (strcmp((char *)rec->key.dptr, "USER HWM") == 0) {
40                 printf(_("USER HWM %d\n"), IVAL(rec->value.dptr,0));
41                 return 0;
42         }
43
44         if (strcmp((char *)rec->key.dptr, "GROUP HWM") == 0) {
45                 printf(_("GROUP HWM %d\n"), IVAL(rec->value.dptr,0));
46                 return 0;
47         }
48
49         if (strncmp((char *)rec->key.dptr, "S-", 2) != 0)
50                 return 0;
51
52         printf("%s %s\n", rec->value.dptr, rec->key.dptr);
53         return 0;
54 }
55
56 /***********************************************************
57  Dump the current idmap
58  **********************************************************/
59 static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
60 {
61         struct db_context *db;
62         TALLOC_CTX *mem_ctx;
63
64         if ( argc != 1  || c->display_usage) {
65                 d_printf("%s\n%s",
66                          _("Usage:"),
67                          _("net idmap dump <inputfile>\n"
68                            "  Dump current ID mapping.\n"
69                            "    inputfile\tTDB file to read mappings from.\n"));
70                 return c->display_usage?0:-1;
71         }
72
73         mem_ctx = talloc_stackframe();
74
75         db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
76         if (db == NULL) {
77                 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
78                           argv[0], strerror(errno));
79                 talloc_free(mem_ctx);
80                 return -1;
81         }
82
83         db->traverse_read(db, net_idmap_dump_one_entry, NULL);
84
85         talloc_free(mem_ctx);
86
87         return 0;
88 }
89
90 /***********************************************************
91  Write entries from stdin to current local idmap
92  **********************************************************/
93
94 static int net_idmap_store_id_mapping(struct db_context *db,
95                                       enum id_type type,
96                                       unsigned long idval,
97                                       const char *sid_string)
98 {
99         NTSTATUS status;
100         char *idstr = NULL;
101
102         switch(type) {
103         case ID_TYPE_UID:
104                 idstr = talloc_asprintf(talloc_tos(), "UID %lu", idval);
105                 break;
106         case ID_TYPE_GID:
107                 idstr = talloc_asprintf(talloc_tos(), "GID %lu", idval);
108                 break;
109         default:
110                 d_fprintf(stderr, "Invalid id mapping type: %d\n", type);
111                 return -1;
112         }
113
114         status = dbwrap_store_bystring(db, idstr,
115                                        string_term_tdb_data(sid_string),
116                                        TDB_REPLACE);
117         if (!NT_STATUS_IS_OK(status)) {
118                 d_fprintf(stderr, "Error storing ID -> SID: "
119                          "%s\n", nt_errstr(status));
120                 talloc_free(idstr);
121                 return -1;
122         }
123         status = dbwrap_store_bystring(db, sid_string,
124                                        string_term_tdb_data(idstr),
125                                        TDB_REPLACE);
126         if (!NT_STATUS_IS_OK(status)) {
127                 d_fprintf(stderr, "Error storing SID -> ID: "
128                          "%s\n", nt_errstr(status));
129                 talloc_free(idstr);
130                 return -1;
131         }
132
133         return 0;
134 }
135
136 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
137 {
138         TALLOC_CTX *mem_ctx;
139         FILE *input = NULL;
140         struct db_context *db;
141         char *dbfile = NULL;
142         int ret = 0;
143
144         if (c->display_usage) {
145                 d_printf("%s\n%s",
146                          _("Usage:"),
147                          _("net idmap restore [<inputfile>]\n"
148                            "  Restore ID mappings from file\n"
149                            "    inputfile\tFile to load ID mappings from. If not "
150                            "given, load data from stdin.\n"));
151                 return 0;
152         }
153
154         mem_ctx = talloc_stackframe();
155
156         if (strequal(lp_idmap_backend(), "tdb")) {
157                 dbfile = state_path("winbindd_idmap.tdb");
158                 if (dbfile == NULL) {
159                         d_fprintf(stderr, _("Out of memory!\n"));
160                         return -1;
161                 }
162         } else if (strequal(lp_idmap_backend(), "tdb2")) {
163                 dbfile = lp_parm_talloc_string(-1, "tdb", "idmap2.tdb", NULL);
164                 if (dbfile == NULL) {
165                         dbfile = talloc_asprintf(mem_ctx, "%s/idmap2.tdb",
166                                                  lp_private_dir());
167                 }
168         } else {
169                 char *backend, *args;
170
171                 backend = talloc_strdup(mem_ctx, lp_idmap_backend());
172                 args = strchr(backend, ':');
173                 if (args != NULL) {
174                         *args = '\0';
175                 }
176
177                 d_printf(_("Sorry, 'net idmap restore' is currently not "
178                            "supported for idmap backend = %s.\n"
179                            "Only tdb and tdb2 are supported.\n"),
180                          backend);
181
182                 ret = -1;
183                 goto done;
184         }
185
186         d_fprintf(stderr, _("restoring id mapping to %s data base in '%s'\n"),
187                   lp_idmap_backend(), dbfile);
188
189         if (argc == 1) {
190                 input = fopen(argv[0], "r");
191         } else {
192                 input = stdin;
193         }
194
195         db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
196         if (db == NULL) {
197                 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
198                           dbfile, strerror(errno));
199                 ret = -1;
200                 goto done;
201         }
202
203         if (db->transaction_start(db) != 0) {
204                 d_fprintf(stderr, _("Failed to start transaction.\n"));
205                 ret = -1;
206                 goto done;
207         }
208
209         while (!feof(input)) {
210                 char line[128], sid_string[128];
211                 int len;
212                 unsigned long idval;
213
214                 if (fgets(line, 127, input) == NULL)
215                         break;
216
217                 len = strlen(line);
218
219                 if ( (len > 0) && (line[len-1] == '\n') )
220                         line[len-1] = '\0';
221
222                 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2)
223                 {
224                         ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
225                                                          idval, sid_string);
226                         if (ret != 0) {
227                                 break;
228                         }
229                 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2)
230                 {
231                         ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
232                                                          idval, sid_string);
233                         if (ret != 0) {
234                                 break;
235                         }
236                 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
237                         ret = dbwrap_store_int32(db, "USER HWM", idval);
238                         if (ret != 0) {
239                                 d_fprintf(stderr, _("Could not store USER HWM.\n"));
240                                 break;
241                         }
242                 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
243                         ret = dbwrap_store_int32(db, "GROUP HWM", idval);
244                         if (ret != 0) {
245                                 d_fprintf(stderr,
246                                           _("Could not store GROUP HWM.\n"));
247                                 break;
248                         }
249                 } else {
250                         d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
251                                   line);
252                         continue;
253                 }
254         }
255
256         if (ret == 0) {
257                 if(db->transaction_commit(db) != 0) {
258                         d_fprintf(stderr, _("Failed to commit transaction.\n"));
259                         ret = -1;
260                 }
261         } else {
262                 if (db->transaction_cancel(db) != 0) {
263                         d_fprintf(stderr, _("Failed to cancel transaction.\n"));
264                 }
265         }
266
267 done:
268         if ((input != NULL) && (input != stdin)) {
269                 fclose(input);
270         }
271
272         talloc_free(mem_ctx);
273         return ret;
274 }
275
276 /***********************************************************
277  Delete a SID mapping from a winbindd_idmap.tdb
278  **********************************************************/
279 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
280 {
281         d_printf("%s\n", _("Not implemented yet"));
282         return -1;
283 }
284
285 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
286 {
287         d_printf("%s\n", _("Not implemented yet"));
288         return -1;
289 }
290 bool idmap_store_secret(const char *backend, bool alloc,
291                         const char *domain, const char *identity,
292                         const char *secret)
293 {
294         char *tmp;
295         int r;
296         bool ret;
297
298         if (alloc) {
299                 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
300         } else {
301                 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
302         }
303
304         if (r < 0) return false;
305
306         strupper_m(tmp); /* make sure the key is case insensitive */
307         ret = secrets_store_generic(tmp, identity, secret);
308
309         free(tmp);
310         return ret;
311 }
312
313
314 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
315 {
316         TALLOC_CTX *ctx;
317         const char *secret;
318         const char *dn;
319         char *domain;
320         char *backend;
321         char *opt = NULL;
322         bool ret;
323
324         if (argc != 2 || c->display_usage) {
325                 d_printf("%s\n%s",
326                          _("Usage:\n"),
327                          _("net idmap secret <DOMAIN> <secret>\n"
328                            "  Set the secret for the specified domain\n"
329                            "    DOMAIN\tDomain to set secret for.\n"
330                            "    secret\tNew secret to set.\n"));
331                 return c->display_usage?0:-1;
332         }
333
334         secret = argv[1];
335
336         ctx = talloc_new(NULL);
337         ALLOC_CHECK(ctx);
338
339         domain = talloc_strdup(ctx, argv[0]);
340         ALLOC_CHECK(domain);
341
342         opt = talloc_asprintf(ctx, "idmap config %s", domain);
343         ALLOC_CHECK(opt);
344
345         backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
346         ALLOC_CHECK(backend);
347
348         if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
349                 d_fprintf(stderr,
350                           _("The only currently supported backend is LDAP\n"));
351                 talloc_free(ctx);
352                 return -1;
353         }
354
355         dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
356         if ( ! dn) {
357                 d_fprintf(stderr,
358                           _("Missing ldap_user_dn option for domain %s\n"),
359                           domain);
360                 talloc_free(ctx);
361                 return -1;
362         }
363
364         ret = idmap_store_secret("ldap", false, domain, dn, secret);
365
366         if ( ! ret) {
367                 d_fprintf(stderr, _("Failed to store secret\n"));
368                 talloc_free(ctx);
369                 return -1;
370         }
371
372         d_printf(_("Secret stored\n"));
373         return 0;
374 }
375
376 int net_help_idmap(struct net_context *c, int argc, const char **argv)
377 {
378         d_printf(_("net idmap dump <inputfile>\n"
379                    "    Dump current id mapping\n"));
380
381         d_printf(_("net idmap restore\n"
382                    "    Restore entries from stdin\n"));
383
384         /* Deliberately *not* document net idmap delete */
385
386         d_printf(_("net idmap secret <DOMAIN>|alloc <secret>\n"
387                    "    Set the secret for the specified DOMAIN (or the alloc "
388                    "module)\n"));
389
390         return -1;
391 }
392
393 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
394 {
395         TALLOC_CTX *mem_ctx;
396         int result = -1;
397         struct dom_sid src_sid, dst_sid;
398         char *src, *dst;
399         struct db_context *db;
400         struct db_record *rec;
401         NTSTATUS status;
402
403         if (argc != 3 || c->display_usage) {
404                 d_fprintf(stderr, "%s net idmap aclmapset <tdb> "
405                           "<src-sid> <dst-sid>\n", _("Usage:"));
406                 return -1;
407         }
408
409         if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
410                 d_fprintf(stderr, _("talloc_init failed\n"));
411                 return -1;
412         }
413
414         if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
415                            O_RDWR|O_CREAT, 0600))) {
416                 d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
417                 goto fail;
418         }
419
420         if (!string_to_sid(&src_sid, argv[1])) {
421                 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
422                 goto fail;
423         }
424
425         if (!string_to_sid(&dst_sid, argv[2])) {
426                 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
427                 goto fail;
428         }
429
430         if (!(src = sid_string_talloc(mem_ctx, &src_sid))
431             || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
432                 d_fprintf(stderr, _("talloc_strdup failed\n"));
433                 goto fail;
434         }
435
436         if (!(rec = db->fetch_locked(
437                       db, mem_ctx, string_term_tdb_data(src)))) {
438                 d_fprintf(stderr, _("could not fetch db record\n"));
439                 goto fail;
440         }
441
442         status = rec->store(rec, string_term_tdb_data(dst), 0);
443         TALLOC_FREE(rec);
444
445         if (!NT_STATUS_IS_OK(status)) {
446                 d_fprintf(stderr, _("could not store record: %s\n"),
447                           nt_errstr(status));
448                 goto fail;
449         }
450
451         result = 0;
452 fail:
453         TALLOC_FREE(mem_ctx);
454         return result;
455 }
456
457 /***********************************************************
458  Look at the current idmap
459  **********************************************************/
460 int net_idmap(struct net_context *c, int argc, const char **argv)
461 {
462         struct functable func[] = {
463                 {
464                         "dump",
465                         net_idmap_dump,
466                         NET_TRANSPORT_LOCAL,
467                         N_("Dump the current ID mappings"),
468                         N_("net idmap dump\n"
469                            "  Dump the current ID mappings")
470                 },
471                 {
472                         "restore",
473                         net_idmap_restore,
474                         NET_TRANSPORT_LOCAL,
475                         N_("Restore entries from stdin"),
476                         N_("net idmap restore\n"
477                            "  Restore entries from stdin")
478                 },
479                 {
480                         "setmap",
481                         net_idmap_set,
482                         NET_TRANSPORT_LOCAL,
483                         N_("Not implemented yet"),
484                         N_("net idmap setmap\n"
485                            "  Not implemented yet")
486                 },
487                 {
488                         "delete",
489                         net_idmap_delete,
490                         NET_TRANSPORT_LOCAL,
491                         N_("Not implemented yet"),
492                         N_("net idmap delete\n"
493                            "  Not implemented yet")
494                 },
495                 {
496                         "secret",
497                         net_idmap_secret,
498                         NET_TRANSPORT_LOCAL,
499                         N_("Set secret for specified domain"),
500                         N_("net idmap secret {<DOMAIN>|alloc} <secret>\n"
501                            "  Set secret for specified domain or alloc module")
502                 },
503                 {
504                         "aclmapset",
505                         net_idmap_aclmapset,
506                         NET_TRANSPORT_LOCAL,
507                         N_("Set acl map"),
508                         N_("net idmap aclmapset\n"
509                            "  Set acl map")
510                 },
511                 {NULL, NULL, 0, NULL, NULL}
512         };
513
514         return net_run_function(c, argc, argv, "net idmap", func);
515 }
516
517