LCOV - code coverage report
Current view: top level - source3/utils - net_idmap.c (source / functions) Hit Total Coverage
Test: coverage report for master 70ed9daf Lines: 0 665 0.0 %
Date: 2024-01-11 09:59:51 Functions: 0 34 0.0 %

          Line data    Source code
       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             : #include "idmap_autorid_tdb.h"
      31             : #include "lib/util/smb_strtox.h"
      32             : 
      33             : #define ALLOC_CHECK(mem) do { \
      34             :         if (!mem) { \
      35             :                 d_fprintf(stderr, _("Out of memory!\n")); \
      36             :                 talloc_free(ctx); \
      37             :                 return -1; \
      38             :         } } while(0)
      39             : 
      40             : enum idmap_dump_backend {
      41             :         TDB,
      42             :         AUTORID
      43             : };
      44             : 
      45             : struct net_idmap_ctx {
      46             :         enum idmap_dump_backend backend;
      47             : };
      48             : 
      49           0 : static int net_idmap_dump_one_autorid_entry(struct db_record *rec,
      50             :                                             void *unused)
      51             : {
      52           0 :         TDB_DATA key;
      53           0 :         TDB_DATA value;
      54             : 
      55           0 :         key = dbwrap_record_get_key(rec);
      56           0 :         value = dbwrap_record_get_value(rec);
      57             : 
      58           0 :         if (strncmp((char *)key.dptr, "CONFIG", 6) == 0) {
      59           0 :                 char *config = talloc_array(talloc_tos(), char, value.dsize+1);
      60           0 :                 memcpy(config, value.dptr, value.dsize);
      61           0 :                 config[value.dsize] = '\0';
      62           0 :                 printf("CONFIG: %s\n", config);
      63           0 :                 talloc_free(config);
      64           0 :                 return 0;
      65             :         }
      66             : 
      67           0 :         if (strncmp((char *)key.dptr, "NEXT RANGE", 10) == 0) {
      68           0 :                 printf("RANGE HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
      69           0 :                 return 0;
      70             :         }
      71             : 
      72           0 :         if (strncmp((char *)key.dptr, "NEXT ALLOC UID", 14) == 0) {
      73           0 :                 printf("UID HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
      74           0 :                 return 0;
      75             :         }
      76             : 
      77           0 :         if (strncmp((char *)key.dptr, "NEXT ALLOC GID", 14) == 0) {
      78           0 :                 printf("GID HWM: %"PRIu32"\n", IVAL(value.dptr, 0));
      79           0 :                 return 0;
      80             :         }
      81             : 
      82           0 :         if (strncmp((char *)key.dptr, "UID", 3) == 0 ||
      83           0 :             strncmp((char *)key.dptr, "GID", 3) == 0)
      84             :         {
      85             :                 /* mapped entry from allocation pool */
      86           0 :                 printf("%s %s\n", value.dptr, key.dptr);
      87           0 :                 return 0;
      88             :         }
      89             : 
      90           0 :         if ((strncmp((char *)key.dptr, "S-1-5-", 6) == 0 ||
      91           0 :              strncmp((char *)key.dptr, "ALLOC", 5) == 0) &&
      92           0 :             value.dsize == sizeof(uint32_t))
      93             :         {
      94             :                 /* this is a domain range assignment */
      95           0 :                 uint32_t range = IVAL(value.dptr, 0);
      96           0 :                 printf("RANGE %"PRIu32": %s\n", range, key.dptr);
      97           0 :                 return 0;
      98             :         }
      99             : 
     100           0 :         return 0;
     101             : }
     102             : 
     103             : /***********************************************************
     104             :  Helper function for net_idmap_dump. Dump one entry.
     105             :  **********************************************************/
     106           0 : static int net_idmap_dump_one_tdb_entry(struct db_record *rec,
     107             :                                         void *unused)
     108             : {
     109           0 :         TDB_DATA key;
     110           0 :         TDB_DATA value;
     111             : 
     112           0 :         key = dbwrap_record_get_key(rec);
     113           0 :         value = dbwrap_record_get_value(rec);
     114             : 
     115           0 :         if (strcmp((char *)key.dptr, "USER HWM") == 0) {
     116           0 :                 printf(_("USER HWM %d\n"), IVAL(value.dptr,0));
     117           0 :                 return 0;
     118             :         }
     119             : 
     120           0 :         if (strcmp((char *)key.dptr, "GROUP HWM") == 0) {
     121           0 :                 printf(_("GROUP HWM %d\n"), IVAL(value.dptr,0));
     122           0 :                 return 0;
     123             :         }
     124             : 
     125           0 :         if (strncmp((char *)key.dptr, "S-", 2) != 0) {
     126           0 :                 return 0;
     127             :         }
     128             : 
     129           0 :         printf("%s %s\n", value.dptr, key.dptr);
     130           0 :         return 0;
     131             : }
     132             : 
     133             : /* returns db path for idmap backend alloced on talloc_tos */
     134           0 : static char *net_idmap_dbfile(struct net_context *c,
     135             :                               struct net_idmap_ctx *ctx)
     136             : {
     137           0 :         char *dbfile = NULL;
     138           0 :         const char *backend = NULL;
     139             : 
     140           0 :         backend = lp_idmap_default_backend();
     141           0 :         if (!backend) {
     142           0 :                 d_printf(_("Internal error: 'idmap config * : backend' is not set!\n"));
     143           0 :                 return NULL;
     144             :         }
     145             : 
     146           0 :         if (c->opt_db != NULL) {
     147           0 :                 dbfile = talloc_strdup(talloc_tos(), c->opt_db);
     148           0 :                 if (dbfile == NULL) {
     149           0 :                         d_fprintf(stderr, _("Out of memory!\n"));
     150             :                 }
     151           0 :         } else if (strequal(backend, "tdb")) {
     152           0 :                 dbfile = state_path(talloc_tos(), "winbindd_idmap.tdb");
     153           0 :                 if (dbfile == NULL) {
     154           0 :                         d_fprintf(stderr, _("Out of memory!\n"));
     155             :                 }
     156           0 :                 ctx->backend = TDB;
     157           0 :         } else if (strequal(backend, "tdb2")) {
     158           0 :                 dbfile = talloc_asprintf(talloc_tos(), "%s/idmap2.tdb",
     159             :                                          lp_private_dir());
     160           0 :                 if (dbfile == NULL) {
     161           0 :                         d_fprintf(stderr, _("Out of memory!\n"));
     162             :                 }
     163           0 :                 ctx->backend = TDB;
     164           0 :         } else if (strequal(backend, "autorid")) {
     165           0 :                 dbfile = state_path(talloc_tos(), "autorid.tdb");
     166           0 :                 if (dbfile == NULL) {
     167           0 :                         d_fprintf(stderr, _("Out of memory!\n"));
     168             :                 }
     169           0 :                 ctx->backend = AUTORID;
     170             :         } else {
     171           0 :                 char *_backend = talloc_strdup(talloc_tos(), backend);
     172           0 :                 char* args = strchr(_backend, ':');
     173           0 :                 if (args != NULL) {
     174           0 :                         *args = '\0';
     175             :                 }
     176             : 
     177           0 :                 d_printf(_("Sorry, 'idmap backend = %s' is currently not supported\n"),
     178             :                            _backend);
     179             : 
     180           0 :                 talloc_free(_backend);
     181             :         }
     182             : 
     183           0 :         return dbfile;
     184             : }
     185             : 
     186           0 : static bool net_idmap_opendb_autorid(TALLOC_CTX *mem_ctx,
     187             :                                      struct net_context *c,
     188             :                                      bool readonly,
     189             :                                      struct db_context **db)
     190             : {
     191           0 :         bool ret = false;
     192           0 :         char *dbfile = NULL;
     193           0 :         struct net_idmap_ctx ctx = { .backend = AUTORID };
     194             : 
     195           0 :         if (c == NULL) {
     196           0 :                 goto done;
     197             :         }
     198             : 
     199           0 :         dbfile = net_idmap_dbfile(c, &ctx);
     200           0 :         if (dbfile == NULL) {
     201           0 :                 goto done;
     202             :         }
     203             : 
     204           0 :         if (ctx.backend != AUTORID) {
     205           0 :                 d_fprintf(stderr, _("Unsupported backend\n"));
     206           0 :                 goto done;
     207             :         }
     208             : 
     209           0 :         if (readonly) {
     210           0 :                 *db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0,
     211             :                              DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
     212           0 :                 if (*db == NULL) {
     213           0 :                         d_fprintf(stderr,
     214           0 :                                   _("Could not open autorid db (%s): %s\n"),
     215           0 :                                  dbfile, strerror(errno));
     216           0 :                         goto done;
     217             :                 }
     218             :         } else {
     219           0 :                 NTSTATUS status;
     220           0 :                 status = idmap_autorid_db_init(dbfile, mem_ctx, db);
     221           0 :                 if (!NT_STATUS_IS_OK(status)) {
     222           0 :                         d_fprintf(stderr,
     223           0 :                                 _("Error calling idmap_autorid_db_init: %s\n"),
     224             :                                 nt_errstr(status));
     225           0 :                         goto done;
     226             :                 }
     227             :         }
     228             : 
     229           0 :         ret = true;
     230             : 
     231           0 : done:
     232           0 :         talloc_free(dbfile);
     233           0 :         return ret;
     234             : }
     235             : 
     236             : 
     237             : /***********************************************************
     238             :  Dump the current idmap
     239             :  **********************************************************/
     240           0 : static int net_idmap_dump(struct net_context *c, int argc, const char **argv)
     241             : {
     242           0 :         struct db_context *db;
     243           0 :         TALLOC_CTX *mem_ctx;
     244           0 :         const char* dbfile;
     245           0 :         NTSTATUS status;
     246           0 :         int ret = -1;
     247           0 :         struct net_idmap_ctx ctx = { .backend = TDB };
     248             : 
     249           0 :         if ( argc > 1  || c->display_usage) {
     250           0 :                 d_printf("%s\n%s",
     251             :                          _("Usage:"),
     252             :                          _("net idmap dump [[--db=]<inputfile>]\n"
     253             :                            "  Dump current ID mapping.\n"
     254             :                            "    inputfile\tTDB file to read mappings from.\n"));
     255           0 :                 return c->display_usage?0:-1;
     256             :         }
     257             : 
     258           0 :         mem_ctx = talloc_stackframe();
     259             : 
     260           0 :         dbfile = (argc > 0) ? argv[0] : net_idmap_dbfile(c, &ctx);
     261           0 :         if (dbfile == NULL) {
     262           0 :                 goto done;
     263             :         }
     264           0 :         d_fprintf(stderr, _("dumping id mapping from %s\n"), dbfile);
     265             : 
     266           0 :         db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDONLY, 0,
     267             :                      DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
     268           0 :         if (db == NULL) {
     269           0 :                 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
     270           0 :                           dbfile, strerror(errno));
     271           0 :                 goto done;
     272             :         }
     273             : 
     274           0 :         if (ctx.backend == AUTORID) {
     275           0 :                 status = dbwrap_traverse_read(db,
     276             :                                               net_idmap_dump_one_autorid_entry,
     277             :                                               NULL, NULL);
     278             :         } else {
     279           0 :                 status = dbwrap_traverse_read(db,
     280             :                                               net_idmap_dump_one_tdb_entry,
     281             :                                               NULL, NULL);
     282             :         }
     283           0 :         if (!NT_STATUS_IS_OK(status)) {
     284           0 :                 d_fprintf(stderr, _("error traversing the database\n"));
     285           0 :                 ret = -1;
     286           0 :                 goto done;
     287             :         }
     288             : 
     289           0 :         ret = 0;
     290             : 
     291           0 : done:
     292           0 :         talloc_free(mem_ctx);
     293           0 :         return ret;
     294             : }
     295             : 
     296             : /***********************************************************
     297             :  Write entries from stdin to current local idmap
     298             :  **********************************************************/
     299             : 
     300           0 : static int net_idmap_store_id_mapping(struct db_context *db,
     301             :                                       enum id_type type,
     302             :                                       unsigned long idval,
     303             :                                       const char *sid_string)
     304             : {
     305           0 :         NTSTATUS status;
     306           0 :         char *idstr = NULL;
     307             : 
     308           0 :         switch(type) {
     309           0 :         case ID_TYPE_UID:
     310           0 :                 idstr = talloc_asprintf(talloc_tos(), "UID %lu", idval);
     311           0 :                 break;
     312           0 :         case ID_TYPE_GID:
     313           0 :                 idstr = talloc_asprintf(talloc_tos(), "GID %lu", idval);
     314           0 :                 break;
     315           0 :         default:
     316           0 :                 d_fprintf(stderr, "Invalid id mapping type: %d\n", type);
     317           0 :                 return -1;
     318             :         }
     319             : 
     320           0 :         status = dbwrap_store_bystring(db, idstr,
     321             :                                        string_term_tdb_data(sid_string),
     322             :                                        TDB_REPLACE);
     323           0 :         if (!NT_STATUS_IS_OK(status)) {
     324           0 :                 d_fprintf(stderr, "Error storing ID -> SID: "
     325             :                          "%s\n", nt_errstr(status));
     326           0 :                 talloc_free(idstr);
     327           0 :                 return -1;
     328             :         }
     329           0 :         status = dbwrap_store_bystring(db, sid_string,
     330             :                                        string_term_tdb_data(idstr),
     331             :                                        TDB_REPLACE);
     332           0 :         if (!NT_STATUS_IS_OK(status)) {
     333           0 :                 d_fprintf(stderr, "Error storing SID -> ID: "
     334             :                          "%s\n", nt_errstr(status));
     335           0 :                 talloc_free(idstr);
     336           0 :                 return -1;
     337             :         }
     338             : 
     339           0 :         return 0;
     340             : }
     341             : 
     342           0 : static int net_idmap_restore(struct net_context *c, int argc, const char **argv)
     343             : {
     344           0 :         TALLOC_CTX *mem_ctx;
     345           0 :         FILE *input = NULL;
     346           0 :         struct db_context *db;
     347           0 :         const char *dbfile = NULL;
     348           0 :         int ret = 0;
     349           0 :         struct net_idmap_ctx ctx = { .backend = TDB };
     350             : 
     351           0 :         if (c->display_usage) {
     352           0 :                 d_printf("%s\n%s",
     353             :                          _("Usage:"),
     354             :                          _("net idmap restore [--db=<TDB>] [<inputfile>]\n"
     355             :                            "  Restore ID mappings from file\n"
     356             :                            "    TDB\tFile to store ID mappings to."
     357             :                            "    inputfile\tFile to load ID mappings from. If not "
     358             :                            "given, load data from stdin.\n"));
     359           0 :                 return 0;
     360             :         }
     361             : 
     362           0 :         mem_ctx = talloc_stackframe();
     363             : 
     364           0 :         dbfile = net_idmap_dbfile(c, &ctx);
     365             : 
     366           0 :         if (dbfile == NULL) {
     367           0 :                 ret = -1;
     368           0 :                 goto done;
     369             :         }
     370             : 
     371           0 :         if (ctx.backend != TDB) {
     372           0 :                 d_fprintf(stderr, _("Sorry, restoring of non-TDB databases is "
     373             :                                     "currently not supported\n"));
     374           0 :                 ret = -1;
     375           0 :                 goto done;
     376             :         }
     377             : 
     378           0 :         d_fprintf(stderr, _("restoring id mapping to %s\n"), dbfile);
     379             : 
     380           0 :         if (argc == 1) {
     381           0 :                 input = fopen(argv[0], "r");
     382           0 :                 if (input == NULL) {
     383           0 :                         d_fprintf(stderr, _("Could not open input file (%s): %s\n"),
     384           0 :                                   argv[0], strerror(errno));
     385           0 :                         ret = -1;
     386           0 :                         goto done;
     387             :                 }
     388             :         } else {
     389           0 :                 input = stdin;
     390             :         }
     391             : 
     392           0 :         db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644,
     393             :                      DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
     394           0 :         if (db == NULL) {
     395           0 :                 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
     396           0 :                           dbfile, strerror(errno));
     397           0 :                 ret = -1;
     398           0 :                 goto done;
     399             :         }
     400             : 
     401           0 :         if (dbwrap_transaction_start(db) != 0) {
     402           0 :                 d_fprintf(stderr, _("Failed to start transaction.\n"));
     403           0 :                 ret = -1;
     404           0 :                 goto done;
     405             :         }
     406             : 
     407           0 :         while (!feof(input)) {
     408           0 :                 char line[128], sid_string[128];
     409           0 :                 int len;
     410           0 :                 unsigned long idval;
     411           0 :                 NTSTATUS status;
     412             : 
     413           0 :                 if (fgets(line, 127, input) == NULL)
     414           0 :                         break;
     415             : 
     416           0 :                 len = strlen(line);
     417             : 
     418           0 :                 if ( (len > 0) && (line[len-1] == '\n') )
     419           0 :                         line[len-1] = '\0';
     420             : 
     421           0 :                 if (sscanf(line, "GID %lu %127s", &idval, sid_string) == 2)
     422             :                 {
     423           0 :                         ret = net_idmap_store_id_mapping(db, ID_TYPE_GID,
     424             :                                                          idval, sid_string);
     425           0 :                         if (ret != 0) {
     426           0 :                                 break;
     427             :                         }
     428           0 :                 } else if (sscanf(line, "UID %lu %127s", &idval, sid_string) == 2)
     429             :                 {
     430           0 :                         ret = net_idmap_store_id_mapping(db, ID_TYPE_UID,
     431             :                                                          idval, sid_string);
     432           0 :                         if (ret != 0) {
     433           0 :                                 break;
     434             :                         }
     435           0 :                 } else if (sscanf(line, "USER HWM %lu", &idval) == 1) {
     436           0 :                         status = dbwrap_store_int32_bystring(
     437             :                                 db, "USER HWM", idval);
     438           0 :                         if (!NT_STATUS_IS_OK(status)) {
     439           0 :                                 d_fprintf(stderr,
     440           0 :                                           _("Could not store USER HWM: %s\n"),
     441             :                                           nt_errstr(status));
     442           0 :                                 break;
     443             :                         }
     444           0 :                 } else if (sscanf(line, "GROUP HWM %lu", &idval) == 1) {
     445           0 :                         status = dbwrap_store_int32_bystring(
     446             :                                 db, "GROUP HWM", idval);
     447           0 :                         if (!NT_STATUS_IS_OK(status)) {
     448           0 :                                 d_fprintf(stderr,
     449           0 :                                           _("Could not store GROUP HWM: %s\n"),
     450             :                                           nt_errstr(status));
     451           0 :                                 break;
     452             :                         }
     453             :                 } else {
     454           0 :                         d_fprintf(stderr, _("ignoring invalid line [%s]\n"),
     455             :                                   line);
     456           0 :                         continue;
     457             :                 }
     458             :         }
     459             : 
     460           0 :         if (ret == 0) {
     461           0 :                 if(dbwrap_transaction_commit(db) != 0) {
     462           0 :                         d_fprintf(stderr, _("Failed to commit transaction.\n"));
     463           0 :                         ret = -1;
     464             :                 }
     465             :         } else {
     466           0 :                 if (dbwrap_transaction_cancel(db) != 0) {
     467           0 :                         d_fprintf(stderr, _("Failed to cancel transaction.\n"));
     468             :                 }
     469             :         }
     470             : 
     471           0 : done:
     472           0 :         if ((input != NULL) && (input != stdin)) {
     473           0 :                 fclose(input);
     474             :         }
     475             : 
     476           0 :         talloc_free(mem_ctx);
     477           0 :         return ret;
     478             : }
     479             : 
     480             : static
     481           0 : NTSTATUS dbwrap_delete_mapping(struct db_context *db, TDB_DATA key1, bool force)
     482             : {
     483           0 :         TALLOC_CTX *mem_ctx = talloc_stackframe();
     484           0 :         bool is_valid_mapping;
     485           0 :         NTSTATUS status = NT_STATUS_OK;
     486           0 :         TDB_DATA val1, val2;
     487             : 
     488           0 :         ZERO_STRUCT(val1);
     489           0 :         ZERO_STRUCT(val2);
     490             : 
     491           0 :         status = dbwrap_fetch(db, mem_ctx, key1, &val1);
     492           0 :         if (!NT_STATUS_IS_OK(status)) {
     493           0 :                 DEBUG(1, ("failed to fetch: %.*s\n", (int)key1.dsize, key1.dptr));
     494           0 :                 goto done;
     495             :         }
     496             : 
     497           0 :         if (val1.dptr == NULL) {
     498           0 :                 DEBUG(1, ("invalid mapping: %.*s -> empty value\n",
     499             :                           (int)key1.dsize, key1.dptr));
     500           0 :                 status = NT_STATUS_FILE_INVALID;
     501           0 :                 goto done;
     502             :         }
     503             : 
     504           0 :         DEBUG(2, ("mapping: %.*s -> %.*s\n",
     505             :                   (int)key1.dsize, key1.dptr, (int)val1.dsize, val1.dptr));
     506             : 
     507           0 :         status = dbwrap_fetch(db, mem_ctx, val1, &val2);
     508           0 :         if (!NT_STATUS_IS_OK(status)) {
     509           0 :                 DEBUG(1, ("failed to fetch: %.*s\n", (int)val1.dsize, val1.dptr));
     510           0 :                 goto done;
     511             :         }
     512             : 
     513           0 :         is_valid_mapping = tdb_data_equal(key1, val2);
     514             : 
     515           0 :         if (!is_valid_mapping) {
     516           0 :                 DEBUG(1, ("invalid mapping: %.*s -> %.*s -> %.*s\n",
     517             :                           (int)key1.dsize, key1.dptr,
     518             :                           (int)val1.dsize, val1.dptr,
     519             :                           (int)val2.dsize, val2.dptr));
     520           0 :                 if ( !force ) {
     521           0 :                         status = NT_STATUS_FILE_INVALID;
     522           0 :                         goto done;
     523             :                 }
     524             :         }
     525             : 
     526           0 :         status = dbwrap_delete(db, key1);
     527           0 :         if (!NT_STATUS_IS_OK(status)) {
     528           0 :                 DEBUG(1, ("failed to delete: %.*s\n", (int)key1.dsize, key1.dptr));
     529           0 :                 goto done;
     530             :         }
     531             : 
     532           0 :         if (!is_valid_mapping) {
     533           0 :                 goto done;
     534             :         }
     535             : 
     536           0 :         status = dbwrap_delete(db, val1);
     537           0 :         if (!NT_STATUS_IS_OK(status)) {
     538           0 :                 DEBUG(1, ("failed to delete: %.*s\n", (int)val1.dsize, val1.dptr));
     539             :         }
     540             : 
     541           0 : done:
     542           0 :         talloc_free(mem_ctx);
     543           0 :         return status;
     544             : }
     545             : 
     546             : static
     547           0 : NTSTATUS delete_mapping_action(struct db_context *db, void* data)
     548             : {
     549           0 :         return dbwrap_delete_mapping(db, *(TDB_DATA*)data, false);
     550             : }
     551             : static
     552           0 : NTSTATUS delete_mapping_action_force(struct db_context *db, void* data)
     553             : {
     554           0 :         return dbwrap_delete_mapping(db, *(TDB_DATA*)data, true);
     555             : }
     556             : 
     557             : /***********************************************************
     558             :  Delete a SID mapping from a winbindd_idmap.tdb
     559             :  **********************************************************/
     560           0 : static bool delete_args_ok(int argc, const char **argv)
     561             : {
     562           0 :         if (argc != 1)
     563           0 :                 return false;
     564           0 :         if (strncmp(argv[0], "S-", 2) == 0)
     565           0 :                 return true;
     566           0 :         if (strncmp(argv[0], "GID ", 4) == 0)
     567           0 :                 return true;
     568           0 :         if (strncmp(argv[0], "UID ", 4) == 0)
     569           0 :                 return true;
     570           0 :         return false;
     571             : }
     572             : 
     573           0 : static int net_idmap_delete_mapping(struct net_context *c, int argc,
     574             :                                     const char **argv)
     575             : {
     576           0 :         int ret = -1;
     577           0 :         struct db_context *db;
     578           0 :         TALLOC_CTX *mem_ctx;
     579           0 :         TDB_DATA key;
     580           0 :         NTSTATUS status;
     581           0 :         const char* dbfile;
     582           0 :         struct net_idmap_ctx ctx = { .backend = TDB };
     583             : 
     584           0 :         if ( !delete_args_ok(argc,argv) || c->display_usage) {
     585           0 :                 d_printf("%s\n%s",
     586             :                          _("Usage:"),
     587             :                          _("net idmap delete mapping [-f] [--db=<TDB>] <ID>\n"
     588             :                            "  Delete mapping of ID from TDB.\n"
     589             :                            "    -f\tforce\n"
     590             :                            "    TDB\tidmap database\n"
     591             :                            "    ID\tSID|GID|UID\n"));
     592           0 :                 return c->display_usage ? 0 : -1;
     593             :         }
     594             : 
     595           0 :         mem_ctx = talloc_stackframe();
     596             : 
     597           0 :         dbfile = net_idmap_dbfile(c, &ctx);
     598           0 :         if (dbfile == NULL) {
     599           0 :                 goto done;
     600             :         }
     601           0 :         d_fprintf(stderr, _("deleting id mapping from %s\n"), dbfile);
     602             : 
     603           0 :         db = db_open(mem_ctx, dbfile, 0, TDB_DEFAULT, O_RDWR, 0,
     604             :                      DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
     605           0 :         if (db == NULL) {
     606           0 :                 d_fprintf(stderr, _("Could not open idmap db (%s): %s\n"),
     607           0 :                           dbfile, strerror(errno));
     608           0 :                 goto done;
     609             :         }
     610             : 
     611           0 :         key = string_term_tdb_data(argv[0]);
     612             : 
     613           0 :         status = dbwrap_trans_do(db, (c->opt_force
     614             :                                       ? delete_mapping_action_force
     615             :                                       : delete_mapping_action),  &key);
     616             : 
     617           0 :         if (!NT_STATUS_IS_OK(status)) {
     618           0 :                 d_fprintf(stderr, _("could not delete mapping: %s\n"),
     619             :                           nt_errstr(status));
     620           0 :                 goto done;
     621             :         }
     622           0 :         ret = 0;
     623           0 : done:
     624           0 :         talloc_free(mem_ctx);
     625           0 :         return ret;
     626             : }
     627             : 
     628           0 : static bool parse_uint32(const char *str, uint32_t *result)
     629             : {
     630           0 :         unsigned long val;
     631           0 :         int error = 0;
     632             : 
     633           0 :         val = smb_strtoul(str, NULL, 10, &error, SMB_STR_FULL_STR_CONV);
     634           0 :         if (error != 0) {
     635           0 :                 return false;
     636             :         }
     637             : 
     638           0 :         *result = val;          /* Potential crop */
     639           0 :         return true;
     640             : }
     641             : 
     642           0 : static void net_idmap_autorid_delete_range_usage(void)
     643             : {
     644           0 :         d_printf("%s\n%s",
     645             :                  _("Usage:"),
     646             :                  _("net idmap delete range [-f] [--db=<TDB>] <RANGE>|(<SID>[ <INDEX>])\n"
     647             :                    "  Delete a domain range mapping from the database.\n"
     648             :                    "    -f\tforce\n"
     649             :                    "    TDB\tidmap database\n"
     650             :                    "    RANGE\tthe range number to delete\n"
     651             :                    "    SID\t\tSID of the domain\n"
     652             :                    "    INDEX\trange index number do delete for the domain\n"));
     653           0 : }
     654             : 
     655           0 : static int net_idmap_autorid_delete_range(struct net_context *c, int argc,
     656             :                                           const char **argv)
     657             : {
     658           0 :         int ret = -1;
     659           0 :         struct db_context *db = NULL;
     660           0 :         NTSTATUS status;
     661           0 :         uint32_t rangenum;
     662           0 :         uint32_t range_index;
     663           0 :         const char *domsid;
     664           0 :         TALLOC_CTX *mem_ctx = NULL;
     665           0 :         bool ok;
     666           0 :         bool force = (c->opt_force != 0);
     667             : 
     668           0 :         if (c->display_usage) {
     669           0 :                 net_idmap_autorid_delete_range_usage();
     670           0 :                 return 0;
     671             :         }
     672             : 
     673           0 :         if (argc < 1 || argc > 2) {
     674           0 :                 net_idmap_autorid_delete_range_usage();
     675           0 :                 return -1;
     676             :         }
     677             : 
     678           0 :         mem_ctx = talloc_stackframe();
     679           0 :         if (!net_idmap_opendb_autorid(mem_ctx, c, false, &db)) {
     680           0 :                 goto done;
     681             :         }
     682             : 
     683           0 :         ok = parse_uint32(argv[0], &rangenum);
     684           0 :         if (ok) {
     685           0 :                 d_printf("%s: %"PRIu32"\n", _("Deleting range number"),
     686             :                          rangenum);
     687             : 
     688           0 :                 status = idmap_autorid_delete_range_by_num(db, rangenum,
     689             :                                                            force);
     690           0 :                 if (!NT_STATUS_IS_OK(status)) {
     691           0 :                         d_fprintf(stderr, "%s: %s\n",
     692             :                                   _("Failed to delete domain range mapping"),
     693             :                                   nt_errstr(status));
     694             :                 } else {
     695           0 :                         ret = 0;
     696             :                 }
     697             : 
     698           0 :                 goto done;
     699             :         }
     700             : 
     701           0 :         domsid = argv[0];
     702           0 :         range_index = 0;
     703             : 
     704           0 :         if (argc == 2) {
     705           0 :                 ok = parse_uint32(argv[1], &range_index);
     706           0 :                 if (!ok) {
     707           0 :                         d_printf("%s: %s\n",
     708           0 :                                  _("Invalid index specification"), argv[1]);
     709           0 :                         net_idmap_autorid_delete_range_usage();
     710           0 :                         goto done;
     711             :                 }
     712             :         }
     713             : 
     714           0 :         status = idmap_autorid_delete_range_by_sid(db, domsid, range_index,
     715             :                                                    force);
     716           0 :         if (!NT_STATUS_IS_OK(status)) {
     717           0 :                 d_fprintf(stderr, "%s: %s\n",
     718             :                           _("Failed to delete domain range mapping"),
     719             :                           nt_errstr(status));
     720           0 :                 goto done;
     721             :         }
     722             : 
     723           0 :         ret = 0;
     724             : 
     725           0 : done:
     726           0 :         talloc_free(mem_ctx);
     727           0 :         return ret;
     728             : }
     729             : 
     730           0 : static void net_idmap_autorid_delete_ranges_usage(void)
     731             : {
     732           0 :         d_printf("%s\n%s",
     733             :                  _("Usage:"),
     734             :                  _("net idmap delete ranges [-f] [--db=<TDB>] <SID>\n"
     735             :                    "  Delete all domain range mappings for a given domain.\n"
     736             :                    "    -f\tforce\n"
     737             :                    "    TDB\tidmap database\n"
     738             :                    "    SID\t\tSID of the domain\n"));
     739           0 : }
     740             : 
     741           0 : static int net_idmap_autorid_delete_ranges(struct net_context *c, int argc,
     742             :                                            const char **argv)
     743             : {
     744           0 :         int ret = -1;
     745           0 :         struct db_context *db = NULL;
     746           0 :         NTSTATUS status;
     747           0 :         const char *domsid;
     748           0 :         TALLOC_CTX *mem_ctx = NULL;
     749           0 :         bool force = (c->opt_force != 0);
     750           0 :         int count = 0;
     751             : 
     752           0 :         if (c->display_usage) {
     753           0 :                 net_idmap_autorid_delete_ranges_usage();
     754           0 :                 return 0;
     755             :         }
     756             : 
     757           0 :         if (argc != 1) {
     758           0 :                 net_idmap_autorid_delete_ranges_usage();
     759           0 :                 return -1;
     760             :         }
     761             : 
     762           0 :         domsid = argv[0];
     763             : 
     764           0 :         mem_ctx = talloc_stackframe();
     765           0 :         if (!net_idmap_opendb_autorid(mem_ctx, c, false, &db)) {
     766           0 :                 goto done;
     767             :         }
     768             : 
     769           0 :         status = idmap_autorid_delete_domain_ranges(db, domsid, force, &count);
     770           0 :         if (!NT_STATUS_IS_OK(status)) {
     771           0 :                 d_fprintf(stderr, "%s %s: %s\n",
     772             :                           _("Failed to delete domain range mappings for "
     773             :                             "domain"),
     774             :                           domsid,
     775             :                           nt_errstr(status));
     776           0 :                 goto done;
     777             :         }
     778             : 
     779           0 :         d_printf(_("deleted %d domain mappings\n"), count);
     780             : 
     781           0 :         ret = 0;
     782             : 
     783           0 : done:
     784           0 :         talloc_free(mem_ctx);
     785           0 :         return ret;
     786             : }
     787             : 
     788           0 : static int net_idmap_delete(struct net_context *c, int argc, const char **argv)
     789             : {
     790           0 :         struct functable func[] = {
     791             :                 {
     792             :                         "mapping",
     793             :                         net_idmap_delete_mapping,
     794             :                         NET_TRANSPORT_LOCAL,
     795             :                         N_("Delete ID mapping"),
     796             :                         N_("net idmap delete mapping <ID>\n"
     797             :                            "  Delete ID mapping")
     798             :                 },
     799             :                 {
     800             :                         "range",
     801             :                         net_idmap_autorid_delete_range,
     802             :                         NET_TRANSPORT_LOCAL,
     803             :                         N_("Delete a domain range mapping"),
     804             :                         N_("net idmap delete range <RANGE>|(<SID>[ <INDEX>])\n"
     805             :                            "  Delete a domain range mapping")
     806             :                 },
     807             :                 {
     808             :                         "ranges",
     809             :                         net_idmap_autorid_delete_ranges,
     810             :                         NET_TRANSPORT_LOCAL,
     811             :                         N_("Delete all domain range mappings for a given "
     812             :                            "domain"),
     813             :                         N_("net idmap delete ranges <SID>\n"
     814             :                            "  Delete a domain range mapping")
     815             :                 },
     816             :                 {NULL, NULL, 0, NULL, NULL}
     817             :         };
     818             : 
     819           0 :         return net_run_function(c, argc, argv, "net idmap delete", func);
     820             : }
     821             : 
     822             : 
     823           0 : static int net_idmap_set_mapping(struct net_context *c,
     824             :                                  int argc, const char **argv)
     825             : {
     826           0 :         d_printf("%s\n", _("Not implemented yet"));
     827           0 :         return -1;
     828             : }
     829             : 
     830           0 : static void net_idmap_autorid_set_range_usage(void)
     831             : {
     832           0 :         d_printf("%s\n%s",
     833             :                  _("Usage:"),
     834             :                  _("net idmap set range"
     835             :                    " <range> <SID> [<index>] [--db=<inputfile>]\n"
     836             :                    "  Store a domain-range mapping for a given domain.\n"
     837             :                    "    range\tRange number to be set for the domain\n"
     838             :                    "    SID\t\tSID of the domain\n"
     839             :                    "    index\trange-index number to be set for the domain\n"
     840             :                    "    inputfile\tTDB file to add mapping to.\n"));
     841           0 : }
     842             : 
     843           0 : static int net_idmap_autorid_set_range(struct net_context *c,
     844             :                                        int argc, const char **argv)
     845             : {
     846           0 :         int ret = -1;
     847           0 :         TALLOC_CTX *mem_ctx;
     848           0 :         struct db_context *db = NULL;
     849           0 :         const char *domsid;
     850           0 :         uint32_t rangenum;
     851           0 :         uint32_t range_index = 0;
     852           0 :         NTSTATUS status;
     853           0 :         bool ok;
     854             : 
     855           0 :         if (c->display_usage) {
     856           0 :                 net_idmap_autorid_set_range_usage();
     857           0 :                 return 0;
     858             :         }
     859             : 
     860           0 :         if (argc < 2  || argc > 3) {
     861           0 :                 net_idmap_autorid_set_range_usage();
     862           0 :                 return -1;
     863             :         }
     864             : 
     865           0 :         ok = parse_uint32(argv[0], &rangenum);
     866           0 :         if (!ok) {
     867           0 :                 d_printf("%s: %s\n", _("Invalid range specification"),
     868             :                          argv[0]);
     869           0 :                 net_idmap_autorid_set_range_usage();
     870           0 :                 return -1;
     871             :         }
     872             : 
     873           0 :         domsid = argv[1];
     874             : 
     875           0 :         if (argc == 3) {
     876           0 :                 ok = parse_uint32(argv[2], &range_index);
     877           0 :                 if (!ok) {
     878           0 :                         d_printf("%s: %s\n",
     879           0 :                                  _("Invalid index specification"), argv[2]);
     880           0 :                         net_idmap_autorid_set_range_usage();
     881           0 :                         return -1;
     882             :                 }
     883             :         }
     884             : 
     885           0 :         mem_ctx = talloc_stackframe();
     886           0 :         if (!net_idmap_opendb_autorid(mem_ctx, c, false, &db)) {
     887           0 :                 goto done;
     888             :         }
     889             : 
     890           0 :         status = idmap_autorid_setrange(db, domsid, range_index, rangenum);
     891           0 :         if (!NT_STATUS_IS_OK(status)) {
     892           0 :                 d_fprintf(stderr, "%s: %s\n",
     893             :                           _("Failed to save domain mapping"),
     894             :                           nt_errstr(status));
     895           0 :                 goto done;
     896             :         }
     897             : 
     898           0 :         ret = 0;
     899             : 
     900           0 : done:
     901           0 :         TALLOC_FREE(mem_ctx);
     902           0 :         return ret;
     903             : }
     904             : 
     905           0 : static bool idmap_store_secret(const char *backend,
     906             :                                const char *domain,
     907             :                                const char *identity,
     908             :                                const char *secret)
     909             : {
     910           0 :         char *tmp;
     911           0 :         int r;
     912           0 :         bool ret;
     913             : 
     914           0 :         r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
     915             : 
     916           0 :         if (r < 0) return false;
     917             : 
     918             :         /* make sure the key is case insensitive */
     919           0 :         if (!strupper_m(tmp)) {
     920           0 :                 free(tmp);
     921           0 :                 return false;
     922             :         }
     923           0 :         ret = secrets_store_generic(tmp, identity, secret);
     924             : 
     925           0 :         free(tmp);
     926           0 :         return ret;
     927             : }
     928             : 
     929             : 
     930           0 : static int net_idmap_secret(struct net_context *c, int argc, const char **argv)
     931             : {
     932           0 :         TALLOC_CTX *ctx;
     933           0 :         const char *secret;
     934           0 :         const char *dn;
     935           0 :         char *domain;
     936           0 :         char *backend;
     937           0 :         char *opt = NULL;
     938           0 :         bool ret;
     939             : 
     940           0 :         if (argc != 2 || c->display_usage) {
     941           0 :                 d_printf("%s\n%s",
     942             :                          _("Usage:\n"),
     943             :                          _("net idmap set secret <DOMAIN> <secret>\n"
     944             :                            "  Set the secret for the specified domain\n"
     945             :                            "    DOMAIN\tDomain to set secret for.\n"
     946             :                            "    secret\tNew secret to set.\n"));
     947           0 :                 return c->display_usage?0:-1;
     948             :         }
     949             : 
     950           0 :         secret = argv[1];
     951             : 
     952           0 :         ctx = talloc_new(NULL);
     953           0 :         ALLOC_CHECK(ctx);
     954             : 
     955           0 :         domain = talloc_strdup(ctx, argv[0]);
     956           0 :         ALLOC_CHECK(domain);
     957             : 
     958           0 :         opt = talloc_asprintf(ctx, "idmap config %s", domain);
     959           0 :         ALLOC_CHECK(opt);
     960             : 
     961           0 :         backend = talloc_strdup(ctx, lp_parm_const_string(-1, opt, "backend", "tdb"));
     962           0 :         ALLOC_CHECK(backend);
     963             : 
     964           0 :         if ((!backend) || (!strequal(backend, "ldap") &&
     965           0 :                            !strequal(backend, "rfc2307"))) {
     966           0 :                 d_fprintf(stderr,
     967           0 :                           _("The only currently supported backend are LDAP "
     968             :                             "and rfc2307\n"));
     969           0 :                 talloc_free(ctx);
     970           0 :                 return -1;
     971             :         }
     972             : 
     973           0 :         dn = lp_parm_const_string(-1, opt, "ldap_user_dn", NULL);
     974           0 :         if ( ! dn) {
     975           0 :                 d_fprintf(stderr,
     976           0 :                           _("Missing ldap_user_dn option for domain %s\n"),
     977             :                           domain);
     978           0 :                 talloc_free(ctx);
     979           0 :                 return -1;
     980             :         }
     981             : 
     982           0 :         ret = idmap_store_secret("ldap", domain, dn, secret);
     983             : 
     984           0 :         if ( ! ret) {
     985           0 :                 d_fprintf(stderr, _("Failed to store secret\n"));
     986           0 :                 talloc_free(ctx);
     987           0 :                 return -1;
     988             :         }
     989             : 
     990           0 :         d_printf(_("Secret stored\n"));
     991           0 :         return 0;
     992             : }
     993             : 
     994           0 : static int net_idmap_autorid_set_config(struct net_context *c,
     995             :                                         int argc, const char **argv)
     996             : {
     997           0 :         int ret = -1;
     998           0 :         NTSTATUS status;
     999           0 :         TALLOC_CTX *mem_ctx;
    1000           0 :         struct db_context *db = NULL;
    1001             : 
    1002           0 :         if (argc != 1 || c->display_usage) {
    1003           0 :                 d_printf("%s\n%s",
    1004             :                          _("Usage:"),
    1005             :                          _("net idmap set config <config>"
    1006             :                            " [--db=<inputfile>]\n"
    1007             :                            " Update CONFIG entry in autorid.\n"
    1008             :                            "       config\tConfig string to be stored\n"
    1009             :                            "       inputfile\tTDB file to update config.\n"));
    1010           0 :                 return c->display_usage ? 0 : -1;
    1011             :         }
    1012             : 
    1013           0 :         mem_ctx = talloc_stackframe();
    1014             : 
    1015           0 :         if (!net_idmap_opendb_autorid(mem_ctx, c, false, &db)) {
    1016           0 :                 goto done;
    1017             :         }
    1018             : 
    1019           0 :         status = idmap_autorid_saveconfigstr(db, argv[0]);
    1020           0 :         if (!NT_STATUS_IS_OK(status)) {
    1021           0 :                 printf("Error storing the config in the database: %s\n",
    1022             :                        nt_errstr(status));
    1023           0 :                 goto done;
    1024             :         }
    1025             : 
    1026           0 :         ret = 0;
    1027             : 
    1028           0 : done:
    1029           0 :         TALLOC_FREE(mem_ctx);
    1030           0 :         return ret;
    1031             : }
    1032             : 
    1033           0 : static int net_idmap_set(struct net_context *c, int argc, const char **argv)
    1034             : {
    1035           0 :         struct functable func[] = {
    1036             :                 {
    1037             :                         "mapping",
    1038             :                         net_idmap_set_mapping,
    1039             :                         NET_TRANSPORT_LOCAL,
    1040             :                         N_("Not implemented yet"),
    1041             :                         N_("net idmap set mapping\n"
    1042             :                            "  Not implemented yet")
    1043             :                 },
    1044             :                 {
    1045             :                         "range",
    1046             :                         net_idmap_autorid_set_range,
    1047             :                         NET_TRANSPORT_LOCAL,
    1048             :                         N_("Store a domain-range mapping"),
    1049             :                         N_("net idmap set range\n"
    1050             :                            "  Store a domain-range mapping")
    1051             :                 },
    1052             :                 {
    1053             :                         "config",
    1054             :                         net_idmap_autorid_set_config,
    1055             :                         NET_TRANSPORT_LOCAL,
    1056             :                         N_("Save the global configuration in the autorid database"),
    1057             :                         N_("net idmap set config \n"
    1058             :                            "  Save the global configuration in the autorid database ")
    1059             :                 },
    1060             :                 {
    1061             :                         "secret",
    1062             :                         net_idmap_secret,
    1063             :                         NET_TRANSPORT_LOCAL,
    1064             :                         N_("Set secret for specified domain"),
    1065             :                         N_("net idmap set secret <DOMAIN> <secret>\n"
    1066             :                            "  Set secret for specified domain")
    1067             :                 },
    1068             :                 {NULL, NULL, 0, NULL, NULL}
    1069             :         };
    1070             : 
    1071           0 :         return net_run_function(c, argc, argv, "net idmap set", func);
    1072             : }
    1073             : 
    1074           0 : static void net_idmap_autorid_get_range_usage(void)
    1075             : {
    1076           0 :         d_printf("%s\n%s",
    1077             :                  _("Usage:"),
    1078             :                  _("net idmap get range <SID> [<index>] [--db=<inputfile>]\n"
    1079             :                    "  Get the range for a given domain and index.\n"
    1080             :                    "    SID\t\tSID of the domain\n"
    1081             :                    "    index\trange-index number to be retrieved\n"
    1082             :                    "    inputfile\tTDB file to add mapping to.\n"));
    1083           0 : }
    1084             : 
    1085             : 
    1086           0 : static int net_idmap_autorid_get_range(struct net_context *c, int argc,
    1087             :                                        const char **argv)
    1088             : {
    1089           0 :         int ret = -1;
    1090           0 :         TALLOC_CTX *mem_ctx;
    1091           0 :         struct db_context *db = NULL;
    1092           0 :         const char *domsid;
    1093           0 :         uint32_t rangenum;
    1094           0 :         uint32_t range_index = 0;
    1095           0 :         uint32_t low_id;
    1096           0 :         NTSTATUS status;
    1097           0 :         char *keystr;
    1098           0 :         bool ok;
    1099             : 
    1100           0 :         if (c->display_usage) {
    1101           0 :                 net_idmap_autorid_get_range_usage();
    1102           0 :                 return 0;
    1103             :         }
    1104             : 
    1105           0 :         if (argc < 1  || argc > 2) {
    1106           0 :                 net_idmap_autorid_get_range_usage();
    1107           0 :                 return -1;
    1108             :         }
    1109             : 
    1110           0 :         domsid = argv[0];
    1111             : 
    1112           0 :         if (argc == 2) {
    1113           0 :                 ok = parse_uint32(argv[1], &range_index);
    1114           0 :                 if (!ok) {
    1115           0 :                         d_printf("%s: %s\n",
    1116           0 :                                  _("Invalid index specification"), argv[1]);
    1117           0 :                         net_idmap_autorid_get_range_usage();
    1118           0 :                         return -1;
    1119             :                 }
    1120             :         }
    1121             : 
    1122           0 :         mem_ctx = talloc_stackframe();
    1123           0 :         if (!net_idmap_opendb_autorid(mem_ctx, c, true, &db)) {
    1124           0 :                 goto done;
    1125             :         }
    1126             : 
    1127           0 :         status = idmap_autorid_getrange(db, domsid, range_index, &rangenum,
    1128             :                                         &low_id);
    1129           0 :         if (!NT_STATUS_IS_OK(status)) {
    1130           0 :                 d_fprintf(stderr, "%s: %s\n",
    1131             :                           _("Failed to load domain range"), nt_errstr(status));
    1132           0 :                 goto done;
    1133             :         }
    1134             : 
    1135           0 :         if (range_index == 0) {
    1136           0 :                 keystr = talloc_strdup(mem_ctx, domsid);
    1137             :         } else {
    1138           0 :                 keystr = talloc_asprintf(mem_ctx, "%s#%"PRIu32, domsid,
    1139             :                                          range_index);
    1140             :         }
    1141             : 
    1142           0 :         printf("RANGE %"PRIu32": %s (low id: %"PRIu32")\n",
    1143             :                rangenum, keystr, low_id);
    1144             : 
    1145           0 :         ret = 0;
    1146             : 
    1147           0 : done:
    1148           0 :         TALLOC_FREE(mem_ctx);
    1149           0 :         return ret;
    1150             : }
    1151             : 
    1152           0 : static NTSTATUS net_idmap_autorid_print_range(struct db_context *db,
    1153             :                                               const char *domsid,
    1154             :                                               uint32_t range_index,
    1155             :                                               uint32_t rangenum,
    1156             :                                               void *private_data)
    1157             : {
    1158           0 :         if (range_index == 0) {
    1159           0 :                 printf("RANGE %"PRIu32": %s\n", rangenum, domsid);
    1160             :         } else {
    1161           0 :                 printf("RANGE %"PRIu32": %s#%"PRIu32"\n", rangenum, domsid,
    1162             :                        range_index);
    1163             :         }
    1164             : 
    1165           0 :         return NT_STATUS_OK;
    1166             : }
    1167             : 
    1168           0 : static void net_idmap_autorid_get_ranges_usage(void)
    1169             : {
    1170           0 :         d_printf("%s\n%s",
    1171             :                  _("Usage:"),
    1172             :                  _("net idmap get ranges [<SID>] [--db=<inputfile>]\n"
    1173             :                    "  Get all ranges for a given domain.\n"
    1174             :                    "    SID\t\tSID of the domain - list all ranges if omitted\n"
    1175             :                    "    inputfile\tTDB file to add mapping to.\n"));
    1176           0 : }
    1177             : 
    1178           0 : static int net_idmap_autorid_get_ranges(struct net_context *c, int argc,
    1179             :                                         const char **argv)
    1180             : {
    1181           0 :         int ret = -1;
    1182           0 :         TALLOC_CTX *mem_ctx;
    1183           0 :         struct db_context *db = NULL;
    1184           0 :         const char *domsid;
    1185           0 :         NTSTATUS status;
    1186             : 
    1187           0 :         if (c->display_usage) {
    1188           0 :                 net_idmap_autorid_get_ranges_usage();
    1189           0 :                 return 0;
    1190             :         }
    1191             : 
    1192           0 :         if (argc == 0) {
    1193           0 :                 domsid = NULL;
    1194           0 :         } else if (argc == 1) {
    1195           0 :                 domsid = argv[0];
    1196             :         } else {
    1197           0 :                 net_idmap_autorid_get_ranges_usage();
    1198           0 :                 return -1;
    1199             :         }
    1200             : 
    1201           0 :         mem_ctx = talloc_stackframe();
    1202           0 :         if (!net_idmap_opendb_autorid(mem_ctx, c, true, &db)) {
    1203           0 :                 goto done;
    1204             :         }
    1205             : 
    1206           0 :         status = idmap_autorid_iterate_domain_ranges_read(db,
    1207             :                                                 domsid,
    1208             :                                                 net_idmap_autorid_print_range,
    1209             :                                                 NULL, /* private_data */
    1210             :                                                 NULL  /* count */);
    1211           0 :         if (!NT_STATUS_IS_OK(status)) {
    1212           0 :                 d_fprintf(stderr, "%s: %s\n",
    1213             :                           _("Error getting domain ranges"), nt_errstr(status));
    1214           0 :                 goto done;
    1215             :         }
    1216             : 
    1217           0 :         ret = 0;
    1218             : 
    1219           0 : done:
    1220           0 :         talloc_free(mem_ctx);
    1221           0 :         return ret;
    1222             : }
    1223             : 
    1224           0 : static int net_idmap_autorid_get_config(struct net_context *c, int argc,
    1225             :                                         const char **argv)
    1226             : {
    1227           0 :         int ret = -1;
    1228           0 :         char *config;
    1229           0 :         TALLOC_CTX *mem_ctx;
    1230           0 :         NTSTATUS status;
    1231           0 :         struct db_context *db = NULL;
    1232             : 
    1233           0 :         if (argc > 0 || c->display_usage) {
    1234           0 :                 d_printf("%s\n%s",
    1235             :                          _("Usage:"),
    1236             :                          _("net idmap get config"
    1237             :                            " [--db=<inputfile>]\n"
    1238             :                            " Get CONFIG entry from autorid database\n"
    1239             :                            "       inputfile\tTDB file to read config from.\n"));
    1240           0 :                 return c->display_usage ? 0 : -1;
    1241             :         }
    1242             : 
    1243           0 :         mem_ctx = talloc_stackframe();
    1244             : 
    1245           0 :         if (!net_idmap_opendb_autorid(mem_ctx, c, true, &db)) {
    1246           0 :                 goto done;
    1247             :         }
    1248             : 
    1249           0 :         status = idmap_autorid_getconfigstr(db, mem_ctx, &config);
    1250           0 :         if (!NT_STATUS_IS_OK(status)) {
    1251           0 :                 d_fprintf(stderr, "%s: %s\n",
    1252             :                           _("Error: unable to read config entry"),
    1253             :                           nt_errstr(status));
    1254           0 :                 goto done;
    1255             :         }
    1256             : 
    1257           0 :         printf("CONFIG: %s\n", config);
    1258           0 :         ret = 0;
    1259             : 
    1260           0 : done:
    1261           0 :         TALLOC_FREE(mem_ctx);
    1262           0 :         return ret;
    1263             : }
    1264             : 
    1265             : 
    1266           0 : static int net_idmap_get(struct net_context *c, int argc, const char **argv)
    1267             : {
    1268           0 :         struct functable func[] = {
    1269             :                 {
    1270             :                         "range",
    1271             :                         net_idmap_autorid_get_range,
    1272             :                         NET_TRANSPORT_LOCAL,
    1273             :                         N_("Get the range for a domain and range-index"),
    1274             :                         N_("net idmap get range\n"
    1275             :                            "  Get the range for a domain and range-index")
    1276             :                 },
    1277             :                 {
    1278             :                         "ranges",
    1279             :                         net_idmap_autorid_get_ranges,
    1280             :                         NET_TRANSPORT_LOCAL,
    1281             :                         N_("Get all ranges for a domain"),
    1282             :                         N_("net idmap get ranges <SID>\n"
    1283             :                            "  Get all ranges for a domain")
    1284             :                 },
    1285             :                 {
    1286             :                         "config",
    1287             :                         net_idmap_autorid_get_config,
    1288             :                         NET_TRANSPORT_LOCAL,
    1289             :                         N_("Get the global configuration from the autorid database"),
    1290             :                         N_("net idmap get config \n"
    1291             :                            "  Get the global configuration from the autorid database ")
    1292             :                 },
    1293             :                 {NULL, NULL, 0, NULL, NULL}
    1294             :         };
    1295             : 
    1296           0 :         return net_run_function(c, argc, argv, "net idmap get", func);
    1297             : }
    1298             : 
    1299           0 : static int net_idmap_check(struct net_context *c, int argc, const char **argv)
    1300             : {
    1301           0 :         char *dbfile;
    1302           0 :         struct check_options opts;
    1303           0 :         struct net_idmap_ctx ctx = { .backend = TDB };
    1304           0 :         int ret;
    1305             : 
    1306           0 :         if ( argc > 1 || c->display_usage) {
    1307           0 :                 d_printf("%s\n%s",
    1308             :                          _("Usage:"),
    1309             :                          _("net idmap check  [-v] [-r] [-a] [-T] [-f] [-l] [[--db=]<TDB>]\n"
    1310             :                            "  Check an idmap database.\n"
    1311             :                            "    --verbose,-v\tverbose\n"
    1312             :                            "    --repair,-r\trepair\n"
    1313             :                            "    --auto,-a\tnoninteractive mode\n"
    1314             :                            "    --test,-T\tdry run\n"
    1315             :                            "    --force,-f\tforce\n"
    1316             :                            "    --lock,-l\tlock db while doing the check\n"
    1317             :                            "    TDB\tidmap database\n"));
    1318           0 :                 return c->display_usage ? 0 : -1;
    1319             :         }
    1320             : 
    1321           0 :         if (argc > 0) {
    1322           0 :                 dbfile = talloc_strdup(talloc_tos(), argv[0]);
    1323             :         } else {
    1324           0 :                 dbfile = net_idmap_dbfile(c, &ctx);
    1325             :         }
    1326           0 :         if (dbfile == NULL) {
    1327           0 :                 return -1;
    1328             :         }
    1329             : 
    1330           0 :         if (ctx.backend != TDB) {
    1331           0 :                 d_fprintf(stderr, _("Sorry, checking of non-TDB databases is "
    1332             :                                     "currently not supported\n"));
    1333           0 :                 talloc_free(dbfile);
    1334           0 :                 return -1;
    1335             :         }
    1336             : 
    1337           0 :         d_fprintf(stderr, _("check database: %s\n"), dbfile);
    1338             : 
    1339           0 :         opts = (struct check_options) {
    1340           0 :                 .lock = c->opt_lock || c->opt_long_list_entries,
    1341           0 :                 .test = c->opt_testmode,
    1342           0 :                 .automatic = c->opt_auto,
    1343           0 :                 .verbose = c->opt_verbose,
    1344           0 :                 .force = c->opt_force,
    1345           0 :                 .repair = c->opt_repair || c->opt_reboot,
    1346             :         };
    1347             : 
    1348           0 :         ret = net_idmap_check_db(dbfile, &opts);
    1349           0 :         talloc_free(dbfile);
    1350           0 :         return ret;
    1351             : }
    1352             : 
    1353             : /***********************************************************
    1354             :  Look at the current idmap
    1355             :  **********************************************************/
    1356           0 : int net_idmap(struct net_context *c, int argc, const char **argv)
    1357             : {
    1358           0 :         struct functable func[] = {
    1359             :                 {
    1360             :                         "dump",
    1361             :                         net_idmap_dump,
    1362             :                         NET_TRANSPORT_LOCAL,
    1363             :                         N_("Dump the current ID mapping database"),
    1364             :                         N_("net idmap dump\n"
    1365             :                            "  Dump the current ID mappings")
    1366             :                 },
    1367             :                 {
    1368             :                         "restore",
    1369             :                         net_idmap_restore,
    1370             :                         NET_TRANSPORT_LOCAL,
    1371             :                         N_("Restore entries from a file or stdin"),
    1372             :                         N_("net idmap restore\n"
    1373             :                            "  Restore entries from stdin")
    1374             :                 },
    1375             :                 {
    1376             :                         "get",
    1377             :                         net_idmap_get,
    1378             :                         NET_TRANSPORT_LOCAL,
    1379             :                         N_("Read data from the ID mapping database"),
    1380             :                         N_("net idmap get\n"
    1381             :                            "  Read data from the ID mapping database")
    1382             :                 },
    1383             :                 {
    1384             :                         "set",
    1385             :                         net_idmap_set,
    1386             :                         NET_TRANSPORT_LOCAL,
    1387             :                         N_("Write data to the ID mapping database"),
    1388             :                         N_("net idmap set\n"
    1389             :                            "  Write data to the ID mapping database")
    1390             :                 },
    1391             :                 {
    1392             :                         "delete",
    1393             :                         net_idmap_delete,
    1394             :                         NET_TRANSPORT_LOCAL,
    1395             :                         N_("Delete entries from the ID mapping database"),
    1396             :                         N_("net idmap delete\n"
    1397             :                            "  Delete entries from the ID mapping database")
    1398             :                 },
    1399             :                 {
    1400             :                         "check",
    1401             :                         net_idmap_check,
    1402             :                         NET_TRANSPORT_LOCAL,
    1403             :                         N_("Check id mappings"),
    1404             :                         N_("net idmap check\n"
    1405             :                            "  Check id mappings")
    1406             :                 },
    1407             :                 {NULL, NULL, 0, NULL, NULL}
    1408             :         };
    1409             : 
    1410           0 :         return net_run_function(c, argc, argv, "net idmap", func);
    1411             : }
    1412             : 
    1413             : 

Generated by: LCOV version 1.14