1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-24 21:34:56 +03:00

This commit make winbindd copy winbindd_idmap.tdb into idmap.tdb on the

first run if idmap.tdb is not found, and then eventually convert it to the
new format.
This is done to unify winbind and idmap databases and to make a backup of
winbindd_idmap.tdb in case you want to downgrade (of course it will not be
updated).
This is needed because idmap.tdb contains also local mappings, not only
foreign domains mappings.

Added some other fixes/improvements

Simo.
This commit is contained in:
Simo Sorce 0001-01-01 00:00:00 +00:00
parent 4070ff1c69
commit cf17261519
4 changed files with 42 additions and 156 deletions

View File

@ -41,7 +41,7 @@ typedef union unid_t {
struct idmap_methods {
/* Called when backend is first loaded */
NTSTATUS (*init)(const char *init_str);
NTSTATUS (*init)(void);
NTSTATUS (*get_sid_from_id)(DOM_SID *sid, unid_t id, int id_type);
NTSTATUS (*get_id_from_sid)(unid_t *id, int *id_type, const DOM_SID *sid);

View File

@ -106,7 +106,7 @@ static int tdb_convert_fn(TDB_CONTEXT * tdb, TDB_DATA key, TDB_DATA data,
/*****************************************************************************
Convert the idmap database from an older version.
*****************************************************************************/
static BOOL tdb_idmap_convert(const char *idmap_name)
static BOOL tdb_idmap_convert(void)
{
int32 vers = tdb_fetch_int32(idmap_tdb, "IDMAP_VERSION");
BOOL bigendianheader =
@ -280,8 +280,27 @@ static BOOL tdb_get_id_from_sid(DOM_SID * sid, uid_t * id, BOOL isgroup)
*****************************************************************************/
static BOOL tdb_idmap_init(void)
{
SMB_STRUCT_STAT stbuf;
/* move to the new database on first startup */
if (!file_exist(lock_path("idmap.tdb"), &stbuf)) {
if (file_exist(lock_path("winbindd_idmap.tdb"), &stbuf)) {
char *cmd = NULL;
/* lazy file copy */
if (asprintf(&cmd, "cp -p %s/winbindd_idmap.tdb %s/idmap.tdb", lp_lockdir(), lp_lockdir()) != -1) {
system(cmd);
free(cmd);
}
if (!file_exist(lock_path("idmap.tdb"), &stbuf)) {
DEBUG(0, ("idmap_init: Unable to make a new database copy\n"));
return False;
}
}
}
/* Open tdb cache */
if (!(idmap_tdb = tdb_open_log(lock_path("winbindd_idmap.tdb"), 0,
if (!(idmap_tdb = tdb_open_log(lock_path("idmap.tdb"), 0,
TDB_DEFAULT, O_RDWR | O_CREAT,
0600))) {
DEBUG(0,
@ -290,9 +309,8 @@ static BOOL tdb_idmap_init(void)
}
/* possibly convert from an earlier version */
if (!tdb_idmap_convert(lock_path("winbindd_idmap.tdb"))) {
DEBUG(0,
("winbindd_idmap_init: Unable to open idmap database\n"));
if (!tdb_idmap_convert()) {
DEBUG(0, ("winbindd_idmap_init: Unable to open idmap database\n"));
return False;
}

View File

@ -45,143 +45,6 @@ static struct idmap_state {
gid_t gid_low, gid_high; /* Range of gids to allocate */
} idmap_state;
/* FIXME: let handle conversions when all things work ok.
I think it is better to handle the conversion at
upgrade time and leave the old db intact.
That would also make easier to go back to 2.2 if needed
---SSS */
#if 0
/* convert one record to the new format */
static int tdb_convert_fn(TDB_CONTEXT * tdb, TDB_DATA key, TDB_DATA data,
void *ignored)
{
struct winbindd_domain *domain;
char *p;
DOM_SID sid;
uint32 rid;
fstring keystr;
fstring dom_name;
TDB_DATA key2;
p = strchr(key.dptr, '/');
if (!p)
return 0;
*p = 0;
fstrcpy(dom_name, key.dptr);
*p++ = '/';
domain = find_domain_from_name(dom_name);
if (!domain) {
/* We must delete the old record. */
DEBUG(0,
("winbindd: tdb_convert_fn : Unable to find domain %s\n",
dom_name));
DEBUG(0,
("winbindd: tdb_convert_fn : deleting record %s\n",
key.dptr));
tdb_delete(idmap_tdb, key);
return 0;
}
rid = atoi(p);
sid_copy(&sid, &domain->sid);
sid_append_rid(&sid, rid);
sid_to_string(keystr, &sid);
key2.dptr = keystr;
key2.dsize = strlen(keystr) + 1;
if (tdb_store(idmap_tdb, key2, data, TDB_INSERT) != 0) {
/* not good! */
DEBUG(0,
("winbindd: tdb_convert_fn : Unable to update record %s\n",
key2.dptr));
DEBUG(0,
("winbindd: tdb_convert_fn : conversion failed - idmap corrupt ?\n"));
return -1;
}
if (tdb_store(idmap_tdb, data, key2, TDB_REPLACE) != 0) {
/* not good! */
DEBUG(0,
("winbindd: tdb_convert_fn : Unable to update record %s\n",
data.dptr));
DEBUG(0,
("winbindd: tdb_convert_fn : conversion failed - idmap corrupt ?\n"));
return -1;
}
tdb_delete(idmap_tdb, key);
return 0;
}
/*****************************************************************************
Convert the idmap database from an older version.
*****************************************************************************/
static BOOL tdb_idmap_convert(const char *idmap_name)
{
int32 vers = tdb_fetch_int32(idmap_tdb, "IDMAP_VERSION");
BOOL bigendianheader =
(idmap_tdb->flags & TDB_BIGENDIAN) ? True : False;
if (vers == IDMAP_VERSION)
return True;
if (((vers == -1) && bigendianheader)
|| (IREV(vers) == IDMAP_VERSION)) {
/* Arrggghh ! Bytereversed or old big-endian - make order independent ! */
/*
* high and low records were created on a
* big endian machine and will need byte-reversing.
*/
int32 wm;
wm = tdb_fetch_int32(idmap_tdb, HWM_USER);
if (wm != -1) {
wm = IREV(wm);
} else
wm = server_state.uid_low;
if (tdb_store_int32(idmap_tdb, HWM_USER, wm) == -1) {
DEBUG(0,
("tdb_idmap_convert: Unable to byteswap user hwm in idmap database\n"));
return False;
}
wm = tdb_fetch_int32(idmap_tdb, HWM_GROUP);
if (wm != -1) {
wm = IREV(wm);
} else
wm = server_state.gid_low;
if (tdb_store_int32(idmap_tdb, HWM_GROUP, wm) == -1) {
DEBUG(0,
("tdb_idmap_convert: Unable to byteswap group hwm in idmap database\n"));
return False;
}
}
/* the old format stored as DOMAIN/rid - now we store the SID direct */
tdb_traverse(idmap_tdb, tdb_convert_fn, NULL);
if (tdb_store_int32(idmap_tdb, "IDMAP_VERSION", IDMAP_VERSION) ==
-1) {
DEBUG(0,
("tdb_idmap_convert: Unable to byteswap group hwm in idmap database\n"));
return False;
}
return True;
}
#endif
/* Allocate either a user or group id from the pool */
static NTSTATUS db_allocate_id(unid_t *id, int id_type)
{
@ -387,25 +250,26 @@ static NTSTATUS db_set_mapping(DOM_SID *sid, unid_t id, int id_type)
/*****************************************************************************
Initialise idmap database.
*****************************************************************************/
static NTSTATUS db_idmap_init(const char *db_name)
static NTSTATUS db_idmap_init(void)
{
SMB_STRUCT_STAT stbuf;
/* move to the new database on first startup */
if (!file_exist(lock_path("idmap.tdb"), &stbuf)) {
if (file_exist(lock_path("winbindd_idmap.tdb"), &stbuf)) {
DEBUG(0, ("idmap_init: winbindd_idmap.tdb is present and idmap.tdb is not!\nPlease RUN winbindd first to convert the db to the new format!\n"));
return NT_STATUS_UNSUCCESSFUL;
}
}
/* Open tdb cache */
if (!(idmap_tdb = tdb_open_log(lock_path(db_name), 0,
if (!(idmap_tdb = tdb_open_log(lock_path("idmap.tdb"), 0,
TDB_DEFAULT, O_RDWR | O_CREAT,
0600))) {
DEBUG(0, ("idmap_init: Unable to open idmap database\n"));
return NT_STATUS_UNSUCCESSFUL;
}
#if 0
/* possibly convert from an earlier version */
if (!tdb_idmap_convert(lock_path("winbind_idmap.tdb"))) {
DEBUG(0,
("idmap_init: Unable to open old idmap database\n"));
return False;
}
#endif
/* Create high water marks for group and user id */
if (tdb_fetch_int32(idmap_tdb, HWM_USER) == -1) {
if (tdb_store_int32(idmap_tdb, HWM_USER, idmap_state.uid_low) == -1) {

View File

@ -38,7 +38,6 @@ static NTSTATUS db_get_sid_from_id(DOM_SID *sid, unid_t id, int id_type)
struct winbindd_request request;
struct winbindd_response response;
int result, operation;
fstring sid_str;
ZERO_STRUCT(request);
ZERO_STRUCT(response);
@ -82,6 +81,8 @@ static NTSTATUS db_get_id_from_sid(unid_t *id, int *id_type, const DOM_SID *sid)
return NT_STATUS_INVALID_PARAMETER;
}
/* setup request */
ZERO_STRUCT(request);
ZERO_STRUCT(response);
@ -96,6 +97,9 @@ static NTSTATUS db_get_id_from_sid(unid_t *id, int *id_type, const DOM_SID *sid)
return NT_STATUS_INVALID_PARAMETER;
}
sid_to_string(sid_str, sid);
fstrcpy(request.data.sid, sid_str);
/* Make The Request */
result = winbindd_request(operation, &request, &response);
@ -118,7 +122,7 @@ static NTSTATUS db_set_mapping(DOM_SID *sid, unid_t id, int id_type) {
/*****************************************************************************
Initialise idmap database.
*****************************************************************************/
static NTSTATUS db_init(const char *db_name) {
static NTSTATUS db_init(void) {
return NT_STATUS_OK;
}