BOOL share_info_db_init(void) { const char *vstring = "INFO/version"; int32 vers_id; if (share_tdb) { return True; } share_tdb = tdb_open_log(lock_path("share_info.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600); if (!share_tdb) { DEBUG(0,("Failed to open share info database %s (%s)\n", lock_path("share_info.tdb"), strerror(errno) )); return False; } /* handle a Samba upgrade */ tdb_lock_bystring(share_tdb, vstring); /* Cope with byte-reversed older versions of the db. */ vers_id = tdb_fetch_int32(share_tdb, vstring); if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) { /* Written on a bigendian machine with old fetch_int code. Save as le. */ tdb_store_int32(share_tdb, vstring, SHARE_DATABASE_VERSION_V2); vers_id = SHARE_DATABASE_VERSION_V2; } if (vers_id != SHARE_DATABASE_VERSION_V2) { tdb_traverse(share_tdb, tdb_traverse_delete_fn, NULL); tdb_store_int32(share_tdb, vstring, SHARE_DATABASE_VERSION_V2); } tdb_unlock_bystring(share_tdb, vstring); return True; }
static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db) { int32 vers; bool bigendianheader; struct convert_fn_state s; NTSTATUS status; #if BUILD_TDB2 /* If we are bigendian, tdb is bigendian if NOT converted. */ union { uint16 large; unsigned char small[2]; } u; u.large = 0x0102; if (u.small[0] == 0x01) bigendianheader = !(dbwrap_get_flags(db) & TDB_CONVERT); else { assert(u.small[0] == 0x02); bigendianheader = (dbwrap_get_flags(db) & TDB_CONVERT); } #else bigendianheader = (dbwrap_get_flags(db) & TDB_BIGENDIAN) ? True : False; #endif DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n")); status = dbwrap_fetch_int32(db, "IDMAP_VERSION", &vers); if (!NT_STATUS_IS_OK(status)) { vers = -1; } 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; status = dbwrap_fetch_int32(db, HWM_USER, &wm); if (!NT_STATUS_IS_OK(status)) { wm = -1; } if (wm != -1) { wm = IREV(wm); } else { wm = dom->low_id; } status = dbwrap_store_int32(db, HWM_USER, wm); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("Unable to byteswap user hwm in idmap " "database: %s\n", nt_errstr(status))); return False; } status = dbwrap_fetch_int32(db, HWM_GROUP, &wm); if (!NT_STATUS_IS_OK(status)) { wm = -1; } if (wm != -1) { wm = IREV(wm); } else { wm = dom->low_id; } status = dbwrap_store_int32(db, HWM_GROUP, wm); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("Unable to byteswap group hwm in idmap " "database: %s\n", nt_errstr(status))); return False; } } s.db = db; s.failed = false; /* the old format stored as DOMAIN/rid - now we store the SID direct */ status = dbwrap_traverse(db, convert_fn, &s, NULL); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("Database traverse failed during conversion\n")); return false; } if (s.failed) { DEBUG(0, ("Problem during conversion\n")); return False; } status = dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("Unable to store idmap version in database: %s\n", nt_errstr(status))); return False; } return True; }
/**************************************************************************** Open the group mapping tdb. ****************************************************************************/ NTSTATUS samba3_read_grouptdb(const char *file, TALLOC_CTX *ctx, struct samba3_groupdb *db) { int32_t vers_id; TDB_DATA kbuf, dbuf, newkey; int ret; TDB_CONTEXT *tdb; tdb = tdb_open(file, 0, TDB_DEFAULT, O_RDONLY, 0600); if (!tdb) { DEBUG(0,("Failed to open group mapping database\n")); return NT_STATUS_UNSUCCESSFUL; } /* Cope with byte-reversed older versions of the db. */ vers_id = tdb_fetch_int32(tdb, "INFO/version"); if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) { /* Written on a bigendian machine with old fetch_int code. Save as le. */ vers_id = DATABASE_VERSION_V2; } if (vers_id != DATABASE_VERSION_V2) { DEBUG(0, ("Group database version mismatch: %d\n", vers_id)); return NT_STATUS_UNSUCCESSFUL; } db->groupmappings = NULL; db->groupmap_count = 0; db->aliases = NULL; db->alias_count = 0; for (kbuf = tdb_firstkey(tdb); kbuf.dptr; newkey = tdb_nextkey(tdb, kbuf), free(kbuf.dptr), kbuf=newkey) { struct samba3_groupmapping map; const char *k = (const char *)kbuf.dptr; if (strncmp(k, GROUP_PREFIX, strlen(GROUP_PREFIX)) == 0) { dbuf = tdb_fetch(tdb, kbuf); if (!dbuf.dptr) continue; ZERO_STRUCT(map); map.sid = dom_sid_parse_talloc(ctx, k+strlen(GROUP_PREFIX)); ret = tdb_unpack(tdb, (char *)dbuf.dptr, dbuf.dsize, "dd", &map.gid, &map.sid_name_use); if ( ret == -1 ) { DEBUG(3,("enum_group_mapping: tdb_unpack failure\n")); continue; } map.nt_name = talloc_strdup(ctx, (const char *)(dbuf.dptr+ret)); map.comment = talloc_strdup(ctx, (const char *)(dbuf.dptr+ret+strlen(map.nt_name))); db->groupmappings = talloc_realloc(ctx, db->groupmappings, struct samba3_groupmapping, db->groupmap_count+1); if (!db->groupmappings) return NT_STATUS_NO_MEMORY; db->groupmappings[db->groupmap_count] = map; db->groupmap_count++; } else if (strncmp(k, MEMBEROF_PREFIX, strlen(MEMBEROF_PREFIX)) == 0)
static bool idmap_tdb_upgrade(struct db_context *db) { int32 vers; bool bigendianheader; struct convert_fn_state s; DEBUG(0, ("Upgrading winbindd_idmap.tdb from an old version\n")); bigendianheader = (db->get_flags(db) & TDB_BIGENDIAN) ? True : False; vers = dbwrap_fetch_int32(db, "IDMAP_VERSION"); 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 = dbwrap_fetch_int32(db, HWM_USER); if (wm != -1) { wm = IREV(wm); } else { wm = idmap_tdb_state.low_uid; } if (dbwrap_store_int32(db, HWM_USER, wm) == -1) { DEBUG(0, ("Unable to byteswap user hwm in idmap database\n")); return False; } wm = dbwrap_fetch_int32(db, HWM_GROUP); if (wm != -1) { wm = IREV(wm); } else { wm = idmap_tdb_state.low_gid; } if (dbwrap_store_int32(db, HWM_GROUP, wm) == -1) { DEBUG(0, ("Unable to byteswap group hwm in idmap database\n")); return False; } } s.db = db; s.failed = false; /* the old format stored as DOMAIN/rid - now we store the SID direct */ db->traverse(db, convert_fn, &s); if (s.failed) { DEBUG(0, ("Problem during conversion\n")); return False; } if (dbwrap_store_int32(db, "IDMAP_VERSION", IDMAP_VERSION) == -1) { DEBUG(0, ("Unable to store idmap version in databse\n")); return False; } return True; }
static bool idmap_tdb_upgrade(struct idmap_domain *dom, struct db_context *db) { int32_t vers; struct convert_fn_state s; NTSTATUS status; status = dbwrap_fetch_int32_bystring(db, "IDMAP_VERSION", &vers); if (!NT_STATUS_IS_OK(status)) { vers = -1; } if (IREV(vers) == IDMAP_VERSION) { /* Arrggghh ! Bytereversed - make order independent ! */ /* * high and low records were created on a * big endian machine and will need byte-reversing. */ int32_t wm; status = dbwrap_fetch_int32_bystring(db, HWM_USER, &wm); if (!NT_STATUS_IS_OK(status)) { wm = -1; } if (wm != -1) { wm = IREV(wm); } else { wm = dom->low_id; } status = dbwrap_store_int32_bystring(db, HWM_USER, wm); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("Unable to byteswap user hwm in idmap " "database: %s\n", nt_errstr(status))); return False; } status = dbwrap_fetch_int32_bystring(db, HWM_GROUP, &wm); if (!NT_STATUS_IS_OK(status)) { wm = -1; } if (wm != -1) { wm = IREV(wm); } else { wm = dom->low_id; } status = dbwrap_store_int32_bystring(db, HWM_GROUP, wm); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("Unable to byteswap group hwm in idmap " "database: %s\n", nt_errstr(status))); return False; } } s.db = db; s.failed = false; /* the old format stored as DOMAIN/rid - now we store the SID direct */ status = dbwrap_traverse(db, convert_fn, &s, NULL); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("Database traverse failed during conversion\n")); return false; } if (s.failed) { DEBUG(0, ("Problem during conversion\n")); return False; } status = dbwrap_store_int32_bystring(db, "IDMAP_VERSION", IDMAP_VERSION); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("Unable to store idmap version in database: %s\n", nt_errstr(status))); return False; } return True; }
bool share_info_db_init(void) { const char *vstring = "INFO/version"; int32 vers_id; if (share_db != NULL) { return True; } share_db = db_open(NULL, state_path("share_info.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600); if (share_db == NULL) { DEBUG(0,("Failed to open share info database %s (%s)\n", state_path("share_info.tdb"), strerror(errno) )); return False; } vers_id = dbwrap_fetch_int32(share_db, vstring); if (vers_id == SHARE_DATABASE_VERSION_V2) { return true; } if (share_db->transaction_start(share_db) != 0) { DEBUG(0, ("transaction_start failed\n")); TALLOC_FREE(share_db); return false; } vers_id = dbwrap_fetch_int32(share_db, vstring); if (vers_id == SHARE_DATABASE_VERSION_V2) { /* * Race condition */ if (share_db->transaction_cancel(share_db)) { smb_panic("transaction_cancel failed"); } return true; } /* Cope with byte-reversed older versions of the db. */ if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) { /* Written on a bigendian machine with old fetch_int code. Save as le. */ if (dbwrap_store_int32(share_db, vstring, SHARE_DATABASE_VERSION_V2) != 0) { DEBUG(0, ("dbwrap_store_int32 failed\n")); goto cancel; } vers_id = SHARE_DATABASE_VERSION_V2; } if (vers_id != SHARE_DATABASE_VERSION_V2) { int ret; ret = share_db->traverse(share_db, delete_fn, NULL); if (ret < 0) { DEBUG(0, ("traverse failed\n")); goto cancel; } if (dbwrap_store_int32(share_db, vstring, SHARE_DATABASE_VERSION_V2) != 0) { DEBUG(0, ("dbwrap_store_int32 failed\n")); goto cancel; } } if (share_db->transaction_commit(share_db) != 0) { DEBUG(0, ("transaction_commit failed\n")); return false; } return true; cancel: if (share_db->transaction_cancel(share_db)) { smb_panic("transaction_cancel failed"); } return false; }
/**************************************************************************** Open the group mapping tdb. ****************************************************************************/ static bool init_group_mapping(void) { const char *ldb_path; if (db != NULL) { return true; } db = db_open(NULL, state_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600, DBWRAP_LOCK_ORDER_1); if (db == NULL) { DEBUG(0, ("Failed to open group mapping database: %s\n", strerror(errno))); return false; } ldb_path = state_path("group_mapping.ldb"); if (file_exist(ldb_path) && !mapping_switch(ldb_path)) { unlink(state_path("group_mapping.tdb")); return false; } else { /* handle upgrade from old versions of the database */ #if 0 /* -- Needs conversion to dbwrap -- */ const char *vstring = "INFO/version"; int32 vers_id; GROUP_MAP *map_table = NULL; size_t num_entries = 0; /* handle a Samba upgrade */ tdb_lock_bystring(tdb, vstring); /* Cope with byte-reversed older versions of the db. */ vers_id = tdb_fetch_int32(tdb, vstring); if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) { /* * Written on a bigendian machine with old fetch_int * code. Save as le. */ tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2); vers_id = DATABASE_VERSION_V2; } /* if its an unknown version we remove everthing in the db */ if (vers_id != DATABASE_VERSION_V2) { tdb_wipe_all(tdb); tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2); } tdb_unlock_bystring(tdb, vstring); /* cleanup any map entries with a gid == -1 */ if ( enum_group_mapping( NULL, SID_NAME_UNKNOWN, &map_table, &num_entries, False ) ) { int i; for ( i=0; i<num_entries; i++ ) { if ( map_table[i].gid == -1 ) { group_map_remove( &map_table[i].sid ); } } SAFE_FREE( map_table ); } #endif } return true; }
bool share_info_db_init(void) { const char *vstring = "INFO/version"; int32 vers_id = 0; bool upgrade_ok = true; NTSTATUS status; if (share_db != NULL) { return True; } share_db = db_open(NULL, state_path("share_info.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600); if (share_db == NULL) { DEBUG(0,("Failed to open share info database %s (%s)\n", state_path("share_info.tdb"), strerror(errno) )); return False; } status = dbwrap_fetch_int32(share_db, vstring, &vers_id); if (!NT_STATUS_IS_OK(status)) { vers_id = 0; } if (vers_id == SHARE_DATABASE_VERSION_V3) { return true; } if (dbwrap_transaction_start(share_db) != 0) { DEBUG(0, ("transaction_start failed\n")); TALLOC_FREE(share_db); return false; } status = dbwrap_fetch_int32(share_db, vstring, &vers_id); if (!NT_STATUS_IS_OK(status)) { vers_id = 0; } if (vers_id == SHARE_DATABASE_VERSION_V3) { /* * Race condition */ if (dbwrap_transaction_cancel(share_db)) { smb_panic("transaction_cancel failed"); } return true; } /* Move to at least V2. */ /* Cope with byte-reversed older versions of the db. */ if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) { /* Written on a bigendian machine with old fetch_int code. Save as le. */ status = dbwrap_store_int32(share_db, vstring, SHARE_DATABASE_VERSION_V2); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("dbwrap_store_int32 failed: %s\n", nt_errstr(status))); goto cancel; } vers_id = SHARE_DATABASE_VERSION_V2; } if (vers_id != SHARE_DATABASE_VERSION_V2) { status = dbwrap_traverse(share_db, delete_fn, NULL, NULL); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("traverse failed\n")); goto cancel; } status = dbwrap_store_int32(share_db, vstring, SHARE_DATABASE_VERSION_V2); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("dbwrap_store_int32 failed: %s\n", nt_errstr(status))); goto cancel; } } /* Finally upgrade to version 3, with canonicalized sharenames. */ status = dbwrap_traverse(share_db, upgrade_v2_to_v3, &upgrade_ok, NULL); if (!NT_STATUS_IS_OK(status) || upgrade_ok == false) { DEBUG(0, ("traverse failed\n")); goto cancel; } status = dbwrap_store_int32(share_db, vstring, SHARE_DATABASE_VERSION_V3); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("dbwrap_store_int32 failed: %s\n", nt_errstr(status))); goto cancel; } if (dbwrap_transaction_commit(share_db) != 0) { DEBUG(0, ("transaction_commit failed\n")); return false; } return true; cancel: if (dbwrap_transaction_cancel(share_db)) { smb_panic("transaction_cancel failed"); } return false; }
/**************************************************************************** Open the group mapping tdb. ****************************************************************************/ static bool init_group_mapping(void) { if (db != NULL) { return true; } db = db_open_trans(NULL, state_path("group_mapping.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600); if (db == NULL) { DEBUG(0, ("Failed to open group mapping database: %s\n", strerror(errno))); return false; } #if 0 /* * This code was designed to handle a group mapping version * upgrade. mapping_tdb is not active by default anymore, so ignore * this here. */ { const char *vstring = "INFO/version"; int32 vers_id; GROUP_MAP *map_table = NULL; size_t num_entries = 0; /* handle a Samba upgrade */ tdb_lock_bystring(tdb, vstring); /* Cope with byte-reversed older versions of the db. */ vers_id = tdb_fetch_int32(tdb, vstring); if ((vers_id == DATABASE_VERSION_V1) || (IREV(vers_id) == DATABASE_VERSION_V1)) { /* * Written on a bigendian machine with old fetch_int * code. Save as le. */ tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2); vers_id = DATABASE_VERSION_V2; } /* if its an unknown version we remove everthing in the db */ if (vers_id != DATABASE_VERSION_V2) { tdb_wipe_all(tdb); tdb_store_int32(tdb, vstring, DATABASE_VERSION_V2); } tdb_unlock_bystring(tdb, vstring); /* cleanup any map entries with a gid == -1 */ if ( enum_group_mapping( NULL, SID_NAME_UNKNOWN, &map_table, &num_entries, False ) ) { int i; for ( i=0; i<num_entries; i++ ) { if ( map_table[i].gid == -1 ) { group_map_remove( &map_table[i].sid ); } } SAFE_FREE( map_table ); } } #endif return true; }