f8818328df6be05156fdd39baf0195fffb4243ed
[metze/samba/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 #include "includes.h"
21 #include "system/filesys.h"
22 #include "utils/net.h"
23 #include "secrets.h"
24 #include "idmap.h"
25 #include "dbwrap/dbwrap.h"
26 #include "dbwrap/dbwrap_open.h"
27 #include "../libcli/security/security.h"
28 #include "net_idmap_check.h"
29 #include "util_tdb.h"
30
31 #define ALLOC_CHECK(mem) do { \
32         if (!mem) { \
33                 d_fprintf(stderr, _("Out of memory!\n")); \
34                 talloc_free(ctx); \
35                 return -1; \
36         } } while(0)
37
38 enum idmap_dump_backend {
39         TDB,
40         AUTORID
41 };
42
43 struct idmap_dump_ctx {
44         enum idmap_dump_backend backend;
45 };
46
47 static int net_idmap_dump_one_autorid_entry(struct db_record *rec,
48                                             void *unused)
49 {
50         TDB_DATA key;
51         TDB_DATA value;
52
53         key = dbwrap_record_get_key(rec);
54         value = dbwrap_record_get_value(rec);
55
56         if (strncmp((char *)key.dptr, "CONFIG", 6) == 0) {
57                 char *config = talloc_array(talloc_tos(), char, value.dsize+1);
58                 memcpy(config, value.dptr, value.dsize);
59                 config[value.dsize] = '\0';
60                 printf("CONFIG: %s\n", config);
61                 talloc_free(config);
62                 return 0;
63         }
64
65         if (strncmp((char *)key.dptr, "NEXT RANGE", 10) == 0) {
66                 printf("RANGE HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
67                 return 0;
68         }
69
70         if (strncmp((char *)key.dptr, "NEXT ALLOC UID", 14) == 0) {
71                 printf("UID HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
72                 return 0;
73         }
74
75         if (strncmp((char *)key.dptr, "NEXT ALLOC GID", 14) == 0) {
76                 printf("GID HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
77                 return 0;
78         }
79
80         if (strncmp((char *)key.dptr, "UID", 3) == 0 ||
81             strncmp((char *)key.dptr, "GID", 3) == 0)
82         {
83                 /* mapped entry from allocation pool */
84                 printf("%s %s\n", value.dptr, key.dptr);
85                 return 0;
86         }
87
88         if ((strncmp((char *)key.dptr, "S-1-5-", 6) == 0 ||
89              strncmp((char *)key.dptr, "ALLOC", 5) == 0) &&
90             value.dsize == sizeof(uint32_t))
91         {
92                 /* this is a domain range assignment */
93                 uint32_t range = IVAL(value.dptr, 0);
94                 printf("RANGE %"PRIu32": %s\n", range, key.dptr);
95                 return 0;
96         }
97
98         return 0;
99 }
100
101 /***********************************************************
102  Helper function for net_idmap_dump. Dump one entry.
103  **********************************************************/
104 static int net_idmap_dump_one_tdb_entry(struct db_record *rec,
105                                         void *unused)
106 {
107         TDB_DATA key;
108         TDB_DATA value;
109
110         key = dbwrap_record_get_key(rec);
111         value = dbwrap_record_get_value(rec);
112
113         if (strcmp((char *)key.dptr, "USER HWM") == 0) {
114                 printf(_("USER HWM %d\n"), IVAL(value.dptr,0));
115                 return 0;
116         }
117
118         if (strcmp((char *)key.dptr, "GROUP HWM") == 0) {
119                 printf(_("GROUP HWM %d\n"), IVAL(value.dptr,0));
120                 return 0;
121         }
122
123         if (strncmp((char *)key.dptr, "S-", 2) != 0) {
124                 return 0;
125         }
126
127         printf("%s %s\n", value.dptr, key.dptr);
128         return 0;
129 }
130
131 static const char* net_idmap_dbfile(struct net_context *c,
132                                     struct idmap_dump_ctx *ctx)
133 {
134         const char* dbfile = NULL;
135         const char *backend = NULL;
136
137         /* prefer idmap config * : backend over idmap backend parameter */
138         backend = lp_parm_const_string(-1, "idmap config *", "backend", NULL);
139         if (!backend) {
140                 backend = lp_idmap_backend();
141         }
142
143         if (c->opt_db != NULL) {
144                 dbfile = talloc_strdup(talloc_tos(), c->opt_db);
145                 if (dbfile == NULL) {
146                         d_fprintf(stderr, _("Out of memory!\n"));
147                 }
148         } else if (strequal(backend, "tdb")) {
149                 dbfile = state_path("winbindd_idmap.tdb");
150                 if (dbfile == NULL) {
151                         d_fprintf(stderr, _("Out of memory!\n"));
152                 }
153                 ctx->backend = TDB;
154         } else if (strequal(backend, "tdb2")) {
155                 dbfile = talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
156                                          lp_private_dir());
157                 if (dbfile == NULL) {
158                         d_fprintf(stderr, _("Out of memory!\n"));
159                 }
160                 ctx->backend = TDB;
161         } else if (strequal(backend, "autorid")) {
162                 dbfile = state_path("autorid.tdb");
163                 if (dbfile == NULL) {
164                         d_fprintf(stderr, _("Out of memory!\n"));
165                 }
166                 ctx->backend = AUTORID;
167         } else {
168                 char *_backend = talloc_strdup(talloc_tos(), backend);
169                 char* args = strchr(_backend, ':');
170                 if (args != NULL) {
171                         *args = '\0';
172                 }
173
174                 d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
175                            _backend);
176
177                 talloc_free(_backend);
178         }
179
180         return dbfile;
181 }
182
183 /***********************************************************
184  Dump the current idmap
185  **********************************************************/
186 static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
187 {
188         struct db_context *db;
189         TALLOC_CTX *mem_ctx;
190         const char* dbfile;
191         NTSTATUS status;
192         int ret = -1;
193         struct idmap_dump_ctx ctx = { .backend = TDB };
194
195         if ( argc > 1  || c->display_usage) {
196                 d_printf("%s\n%s",
197                          _("Usage:"),
198                          _("net idmap dump [[--db=]<inputfile>]\n"
199                            "  Dump current ID mapping.\n"
200                            "    inputfile\tTDB file to read mappings from.\n"));
201                 return c->display_usage?0:-1;
202         }
203
204         mem_ctx = talloc_stackframe();
205
206         dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c, &ctx);
207         if (dbfile == NULL) {
208                 goto done;
209         }
210         d_fprintf(stderr, _("dumping id mapping from %s\n"), dbfile);
211
212         db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0,
213                      DBWRAP_LOCK_ORDER_1);
214         if (db == NULL) {
215                 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
216                           dbfile, strerror(errno));
217                 goto done;
218         }
219
220         if (ctx.backend == AUTORID) {
221                 status = dbwrap_traverse_read(db,
222                                               net_idmap_dump_one_autorid_entry,
223                                               NULL, NULL);
224         } else {
225                 status = dbwrap_traverse_read(db,
226                                               net_idmap_dump_one_tdb_entry,
227                                               NULL, NULL);
228         }
229         if (!NT_STATUS_IS_OK(status)) {
230                 d_fprintf(stderr, _("error traversing the database\n"));
231                 ret = -1;
232                 goto done;
233         }
234
235         ret = 0;
236
237 done:
238         talloc_free(mem_ctx);
239         return ret;
240 }
241
242 /***********************************************************
243  Write entries from stdin to current local idmap
244  **********************************************************/
245
246 static int net_idmap_store_id_mapping(struct db_context *db,
247                                       enum id_type type,
248                                       unsigned long idval,
249                                       const char *sid_string)
250 {
251         NTSTATUS status;
252         char *idstr = NULL;
253
254         switch(type) {
255         case ID_TYPE_UID:
256                 idstr = talloc_asprintf(talloc_tos(), "UID %lu", idval);
257                 break;
258         case ID_TYPE_GID:
259                 idstr = talloc_asprintf(talloc_tos(), "GID %lu", idval);
260                 break;
261         default:
262                 d_fprintf(stderr, "Invalid id mapping type: %d\n", type);
263                 return -1;
264         }
265
266         status = dbwrap_store_bystring(db, idstr,
267                                        string_term_tdb_data(sid_string),
268                                        TDB_REPLACE);
269         if (!NT_STATUS_IS_OK(status)) {
270                 d_fprintf(stderr, "Error storing ID -> SID: "
271                          "%s\n", nt_errstr(status));
272                 talloc_free(idstr);
273                 return -1;
274         }
275         status = dbwrap_store_bystring(db, sid_string,
276                                        string_term_tdb_data(idstr),
277                                        TDB_REPLACE);
278         if (!NT_STATUS_IS_OK(status)) {
279                 d_fprintf(stderr, "Error storing SID -> ID: "
280                          "%s\n", nt_errstr(status));
281                 talloc_free(idstr);
282                 return -1;
283         }
284
285         return 0;
286 }
287
288 static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
289 {
290         TALLOC_CTX *mem_ctx;
291         FILE *input = NULL;
292         struct db_context *db;
293         const char *dbfile = NULL;
294         int ret = 0;
295         struct idmap_dump_ctx ctx = { .backend = TDB };
296
297         if (c->display_usage) {
298                 d_printf("%s\n%s",
299                          _("Usage:"),
300                          _("net idmap restore [--db=<TDB>] [<inputfile>]\n"
301                            "  Restore ID mappings from file\n"
302                            "    TDB\tFile to store ID mappings to."
303                            "    inputfile\tFile to load ID mappings from. If not "
304                            "given, load data from stdin.\n"));
305                 return 0;
306         }
307
308         mem_ctx = talloc_stackframe();
309
310         dbfile = net_idmap_dbfile(c, &ctx);
311
312         if (dbfile == NULL) {
313                 ret = -1;
314                 goto done;
315         }
316
317         if (ctx.backend != TDB) {
318                 d_fprintf(stderr, _("Sorry, restoring of non-TDB databases is "
319                                     "currently not supported\n"));
320                 ret = -1;
321                 goto done;
322         }
323
324         d_fprintf(stderr, _("restoring id mapping to %s\n"), dbfile);
325
326         if (argc == 1) {
327                 input = fopen(argv[0], "r");
328                 if (input == NULL) {
329                         d_fprintf(stderr, _("Could not open input file (%s): %s\n"),
330                                   argv[0], strerror(errno));
331                         ret = -1;
332                         goto done;
333                 }
334         } else {
335                 input = stdin;
336         }
337
338         db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644,
339                      DBWRAP_LOCK_ORDER_1);
340         if (db == NULL) {
341                 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
342                           dbfile, strerror(errno));
343                 ret = -1;
344                 goto done;
345         }
346
347         if (dbwrap_transaction_start(db) != 0) {
348                 d_fprintf(stderr, _("Failed to start transaction.\n"));
349                 ret = -1;
350                 goto done;
351         }
352
353         while (!feof(input)) {
354                 char line[128], sid_string[128];
355                 int len;
356                 unsigned long idval;
357                 NTSTATUS status;
358
359                 if (fgets(line, 127, input) == NULL)
360                         break;
361
362                 len = strlen(line);
363
364                 if ( (len > 0) && (line[len-1] == '\n') )
365                         line[len-1] = '\0';
366
367                 if (sscanf(line, "GID %lu %128s", &idval, sid_string) == 2)
368                 {
369                         ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
370                                                          idval, sid_string);
371                         if (ret != 0) {
372                                 break;
373                         }
374                 } else if (sscanf(line, "UID %lu %128s", &idval, sid_string) == 2)
375                 {
376                         ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
377                                                          idval, sid_string);
378                         if (ret != 0) {
379                                 break;
380                         }
381                 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
382                         status = dbwrap_store_int32_bystring(
383                                 db, "USER HWM", idval);
384                         if (!NT_STATUS_IS_OK(status)) {
385                                 d_fprintf(stderr,
386                                           _("Could not store USER HWM: %s\n"),
387                                           nt_errstr(status));
388                                 break;
389                         }
390                 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
391                         status = dbwrap_store_int32_bystring(
392                                 db, "GROUP HWM", idval);
393                         if (!NT_STATUS_IS_OK(status)) {
394                                 d_fprintf(stderr,
395                                           _("Could not store GROUP HWM: %s\n"),
396                                           nt_errstr(status));
397                                 break;
398                         }
399                 } else {
400                         d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
401                                   line);
402                         continue;
403                 }
404         }
405
406         if (ret == 0) {
407                 if(dbwrap_transaction_commit(db) != 0) {
408                         d_fprintf(stderr, _("Failed to commit transaction.\n"));
409                         ret = -1;
410                 }
411         } else {
412                 if (dbwrap_transaction_cancel(db) != 0) {
413                         d_fprintf(stderr, _("Failed to cancel transaction.\n"));
414                 }
415         }
416
417 done:
418         if ((input != NULL) && (input != stdin)) {
419                 fclose(input);
420         }
421
422         talloc_free(mem_ctx);
423         return ret;
424 }
425
426 static
427 NTSTATUS dbwrap_delete_mapping(struct db_context *db, TDB_DATA key1, bool force)
428 {
429         TALLOC_CTX* mem_ctx = talloc_tos();
430         struct db_record *rec1=NULL, *rec2=NULL;
431         TDB_DATA key2;
432         bool is_valid_mapping;
433         NTSTATUS status = NT_STATUS_OK;
434         TDB_DATA value;
435
436         rec1 = dbwrap_fetch_locked(db, mem_ctx, key1);
437         if (rec1 == NULL) {
438                 DEBUG(1, ("failed to fetch: %.*s\n", (int)key1.dsize, key1.dptr));
439                 status = NT_STATUS_NO_MEMORY;
440                 goto done;
441         }
442         key2 = dbwrap_record_get_value(rec1);
443         if (key2.dptr == NULL) {
444                 DEBUG(1, ("could not find %.*s\n", (int)key1.dsize, key1.dptr));
445                 status = NT_STATUS_NOT_FOUND;
446                 goto done;
447         }
448
449         DEBUG(2, ("mapping: %.*s -> %.*s\n",
450                   (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr));
451
452         rec2 = dbwrap_fetch_locked(db, mem_ctx, key2);
453         if (rec2 == NULL) {
454                 DEBUG(1, ("failed to fetch: %.*s\n", (int)key2.dsize, key2.dptr));
455                 status = NT_STATUS_NO_MEMORY;
456                 goto done;
457         }
458
459         value = dbwrap_record_get_value(rec2);
460         is_valid_mapping = tdb_data_equal(key1, value);
461
462         if (!is_valid_mapping) {
463                 DEBUG(1, ("invalid mapping: %.*s -> %.*s -> %.*s\n",
464                           (int)key1.dsize, key1.dptr, (int)key2.dsize, key2.dptr,
465                           (int)value.dsize, value.dptr ));
466                 if ( !force ) {
467                         status = NT_STATUS_FILE_INVALID;
468                         goto done;
469                 }
470         }
471
472         status = dbwrap_record_delete(rec1);
473         if (!NT_STATUS_IS_OK(status)) {
474                 DEBUG(1, ("failed to delete: %.*s\n", (int)key1.dsize, key1.dptr));
475                 goto done;
476         }
477
478         if (is_valid_mapping) {
479                 status = dbwrap_record_delete(rec2);
480                 if (!NT_STATUS_IS_OK(status)) {
481                         DEBUG(1, ("failed to delete: %.*s\n", (int)key2.dsize, key2.dptr));
482                 }
483         }
484 done:
485         TALLOC_FREE(rec1);
486         TALLOC_FREE(rec2);
487         return status;
488 }
489
490 static
491 NTSTATUS delete_mapping_action(struct db_context *db, void* data)
492 {
493         return dbwrap_delete_mapping(db, *(TDB_DATA*)data, false);
494 }
495 static
496 NTSTATUS delete_mapping_action_force(struct db_context *db, void* data)
497 {
498         return dbwrap_delete_mapping(db, *(TDB_DATA*)data, true);
499 }
500
501 /***********************************************************
502  Delete a SID mapping from a winbindd_idmap.tdb
503  **********************************************************/
504 static bool delete_args_ok(int argc, const char **argv)
505 {
506         if (argc != 1)
507                 return false;
508         if (strncmp(argv[0], "S-", 2) == 0)
509                 return true;
510         if (strncmp(argv[0], "GID ", 4) == 0)
511                 return true;
512         if (strncmp(argv[0], "UID ", 4) == 0)
513                 return true;
514         return false;
515 }
516
517 static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
518 {
519         int ret = -1;
520         struct db_context *db;
521         TALLOC_CTX *mem_ctx;
522         TDB_DATA key;
523         NTSTATUS status;
524         const char* dbfile;
525         struct idmap_dump_ctx ctx = { .backend = TDB };
526
527         if ( !delete_args_ok(argc,argv) || c->display_usage) {
528                 d_printf("%s\n%s",
529                          _("Usage:"),
530                          _("net idmap delete [-f] [--db=<TDB>] <ID>\n"
531                            "  Delete mapping of ID from TDB.\n"
532                            "    -f\tforce\n"
533                            "    TDB\tidmap database\n"
534                            "    ID\tSID|GID|UID\n"));
535                 return c->display_usage ? 0 : -1;
536         }
537
538         mem_ctx = talloc_stackframe();
539
540         dbfile = net_idmap_dbfile(c, &ctx);
541         if (dbfile == NULL) {
542                 goto done;
543         }
544         d_fprintf(stderr, _("deleting id mapping from %s\n"), dbfile);
545
546         db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR, 0,
547                      DBWRAP_LOCK_ORDER_1);
548         if (db == NULL) {
549                 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
550                           dbfile, strerror(errno));
551                 goto done;
552         }
553
554         key = string_term_tdb_data(argv[0]);
555
556         status = dbwrap_trans_do(db, (c->opt_force
557                                       ? delete_mapping_action_force
558                                       : delete_mapping_action),  &key);
559
560         if (!NT_STATUS_IS_OK(status)) {
561                 d_fprintf(stderr, _("could not delete mapping: %s\n"),
562                           nt_errstr(status));
563                 goto done;
564         }
565         ret = 0;
566 done:
567         talloc_free(mem_ctx);
568         return ret;
569 }
570
571 static int net_idmap_set(struct net_context *c, int argc, const char **argv)
572 {
573         d_printf("%s\n", _("Not implemented yet"));
574         return -1;
575 }
576 static bool idmap_store_secret(const char *backend,
577                                const char *domain,
578                                const char *identity,
579                                const char *secret)
580 {
581         char *tmp;
582         int r;
583         bool ret;
584
585         r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
586
587         if (r < 0) return false;
588
589         /* make sure the key is case insensitive */
590         if (!strupper_m(tmp)) {
591                 free(tmp);
592                 return false;
593         }
594         ret = secrets_store_generic(tmp, identity, secret);
595
596         free(tmp);
597         return ret;
598 }
599
600
601 static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
602 {
603         TALLOC_CTX *ctx;
604         const char *secret;
605         const char *dn;
606         char *domain;
607         char *backend;
608         char *opt = NULL;
609         bool ret;
610
611         if (argc != 2 || c->display_usage) {
612                 d_printf("%s\n%s",
613                          _("Usage:\n"),
614                          _("net idmap secret <DOMAIN> <secret>\n"
615                            "  Set the secret for the specified domain\n"
616                            "    DOMAIN\tDomain to set secret for.\n"
617                            "    secret\tNew secret to set.\n"));
618                 return c->display_usage?0:-1;
619         }
620
621         secret = argv[1];
622
623         ctx = talloc_new(NULL);
624         ALLOC_CHECK(ctx);
625
626         domain = talloc_strdup(ctx, argv[0]);
627         ALLOC_CHECK(domain);
628
629         opt = talloc_asprintf(ctx, "idmap config %s", domain);
630         ALLOC_CHECK(opt);
631
632         backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
633         ALLOC_CHECK(backend);
634
635         if ( ( ! backend) || ( ! strequal(backend, "ldap"))) {
636                 d_fprintf(stderr,
637                           _("The only currently supported backend is LDAP\n"));
638                 talloc_free(ctx);
639                 return -1;
640         }
641
642         dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
643         if ( ! dn) {
644                 d_fprintf(stderr,
645                           _("Missing ldap_user_dn option for domain %s\n"),
646                           domain);
647                 talloc_free(ctx);
648                 return -1;
649         }
650
651         ret = idmap_store_secret("ldap", domain, dn, secret);
652
653         if ( ! ret) {
654                 d_fprintf(stderr, _("Failed to store secret\n"));
655                 talloc_free(ctx);
656                 return -1;
657         }
658
659         d_printf(_("Secret stored\n"));
660         return 0;
661 }
662
663 static int net_idmap_check(struct net_context *c, int argc, const char **argv)
664 {
665         const char* dbfile;
666         struct check_options opts;
667         struct idmap_dump_ctx ctx = { .backend = TDB };
668
669         if ( argc > 1 || c->display_usage) {
670                 d_printf("%s\n%s",
671                          _("Usage:"),
672                          _("net idmap check  [-v] [-r] [-a] [-T] [-f] [-l] [[--db=]<TDB>]\n"
673                            "  Check an idmap database.\n"
674                            "    --verbose,-v\tverbose\n"
675                            "    --repair,-r\trepair\n"
676                            "    --auto,-a\tnoninteractive mode\n"
677                            "    --test,-T\tdry run\n"
678                            "    --fore,-f\tforce\n"
679                            "    --lock,-l\tlock db while doing the check\n"
680                            "    TDB\tidmap database\n"));
681                 return c->display_usage ? 0 : -1;
682         }
683
684         dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c, &ctx);
685         if (dbfile == NULL) {
686                 return -1;
687         }
688
689         if (ctx.backend != TDB) {
690                 d_fprintf(stderr, _("Sorry, checking of non-TDB databases is "
691                                     "currently not supported\n"));
692                 return -1;
693         }
694
695         d_fprintf(stderr, _("check database: %s\n"), dbfile);
696
697         opts = (struct check_options) {
698                 .lock = c->opt_lock || c->opt_long_list_entries,
699                 .test = c->opt_testmode,
700                 .automatic = c->opt_auto,
701                 .verbose = c->opt_verbose,
702                 .force = c->opt_force,
703                 .repair = c->opt_repair || c->opt_reboot,
704         };
705
706         return net_idmap_check_db(dbfile, &opts);
707 }
708
709 static int net_idmap_aclmapset(struct net_context *c, int argc, const char **argv)
710 {
711         TALLOC_CTX *mem_ctx;
712         int result = -1;
713         struct dom_sid src_sid, dst_sid;
714         char *src, *dst;
715         struct db_context *db;
716         struct db_record *rec;
717         NTSTATUS status;
718
719         if (argc != 3 || c->display_usage) {
720                 d_fprintf(stderr, "%s net idmap aclmapset <tdb> "
721                           "<src-sid> <dst-sid>\n", _("Usage:"));
722                 return -1;
723         }
724
725         if (!(mem_ctx = talloc_init("net idmap aclmapset"))) {
726                 d_fprintf(stderr, _("talloc_init failed\n"));
727                 return -1;
728         }
729
730         if (!(db = db_open(mem_ctx, argv[0], 0, TDB_DEFAULT,
731                            O_RDWR|O_CREAT, 0600,
732                            DBWRAP_LOCK_ORDER_1))) {
733                 d_fprintf(stderr, _("db_open failed: %s\n"), strerror(errno));
734                 goto fail;
735         }
736
737         if (!string_to_sid(&src_sid, argv[1])) {
738                 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[1]);
739                 goto fail;
740         }
741
742         if (!string_to_sid(&dst_sid, argv[2])) {
743                 d_fprintf(stderr, _("%s is not a valid sid\n"), argv[2]);
744                 goto fail;
745         }
746
747         if (!(src = sid_string_talloc(mem_ctx, &src_sid))
748             || !(dst = sid_string_talloc(mem_ctx, &dst_sid))) {
749                 d_fprintf(stderr, _("talloc_strdup failed\n"));
750                 goto fail;
751         }
752
753         if (!(rec = dbwrap_fetch_locked(
754                       db, mem_ctx, string_term_tdb_data(src)))) {
755                 d_fprintf(stderr, _("could not fetch db record\n"));
756                 goto fail;
757         }
758
759         status = dbwrap_record_store(rec, string_term_tdb_data(dst), 0);
760         TALLOC_FREE(rec);
761
762         if (!NT_STATUS_IS_OK(status)) {
763                 d_fprintf(stderr, _("could not store record: %s\n"),
764                           nt_errstr(status));
765                 goto fail;
766         }
767
768         result = 0;
769 fail:
770         TALLOC_FREE(mem_ctx);
771         return result;
772 }
773
774 /***********************************************************
775  Look at the current idmap
776  **********************************************************/
777 int net_idmap(struct net_context *c, int argc, const char **argv)
778 {
779         struct functable func[] = {
780                 {
781                         "dump",
782                         net_idmap_dump,
783                         NET_TRANSPORT_LOCAL,
784                         N_("Dump the current ID mappings"),
785                         N_("net idmap dump\n"
786                            "  Dump the current ID mappings")
787                 },
788                 {
789                         "restore",
790                         net_idmap_restore,
791                         NET_TRANSPORT_LOCAL,
792                         N_("Restore entries from stdin"),
793                         N_("net idmap restore\n"
794                            "  Restore entries from stdin")
795                 },
796                 {
797                         "setmap",
798                         net_idmap_set,
799                         NET_TRANSPORT_LOCAL,
800                         N_("Not implemented yet"),
801                         N_("net idmap setmap\n"
802                            "  Not implemented yet")
803                 },
804                 {
805                         "delete",
806                         net_idmap_delete,
807                         NET_TRANSPORT_LOCAL,
808                         N_("Delete ID mapping"),
809                         N_("net idmap delete <ID>\n"
810                            "  Delete ID mapping")
811                 },
812                 {
813                         "secret",
814                         net_idmap_secret,
815                         NET_TRANSPORT_LOCAL,
816                         N_("Set secret for specified domain"),
817                         N_("net idmap secret <DOMAIN> <secret>\n"
818                            "  Set secret for specified domain")
819                 },
820                 {
821                         "aclmapset",
822                         net_idmap_aclmapset,
823                         NET_TRANSPORT_LOCAL,
824                         N_("Set acl map"),
825                         N_("net idmap aclmapset\n"
826                            "  Set acl map")
827                 },
828                 {
829                         "check",
830                         net_idmap_check,
831                         NET_TRANSPORT_LOCAL,
832                         N_("Check id mappings"),
833                         N_("net idmap check\n"
834                            "  Check id mappings")
835                 },
836                 {NULL, NULL, 0, NULL, NULL}
837         };
838
839         return net_run_function(c, argc, argv, "net idmap", func);
840 }
841
842