- return True;
-}
-
-/* Get a group rid from a gid */
-
-BOOL winbindd_idmap_get_rid_from_gid(gid_t gid, uint32 *group_rid,
- struct winbindd_domain **domain)
-{
- DOM_SID sid;
-
- if (!get_sid_from_id((int)gid, &sid, True)) {
- return False;
- }
-
- *domain = find_domain_from_sid(&sid);
- if (! *domain) return False;
-
- sid_split_rid(&sid, group_rid);
-
- return True;
-}
-
-/* convert one record to the new format */
-static int 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: convert_fn : Unable to find domain %s\n", dom_name ));
- DEBUG(0,("winbindd: 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: convert_fn : Unable to update record %s\n", key2.dptr ));
- DEBUG(0,("winbindd: convert_fn : conversion failed - idmap corrupt ?\n"));
- return -1;
- }
-
- if (tdb_store(idmap_tdb, data, key2, TDB_REPLACE) != 0) {
- /* not good! */
- DEBUG(0,("winbindd: convert_fn : Unable to update record %s\n", data.dptr ));
- DEBUG(0,("winbindd: convert_fn : conversion failed - idmap corrupt ?\n"));
- return -1;
- }
-
- tdb_delete(idmap_tdb, key);
-
- return 0;
-}
-
-#if 0
-/*****************************************************************************
- Make a backup copy of the old idmap just to be safe.... JRA.
-*****************************************************************************/
-
-static BOOL backup_old_idmap(const char *idmap_name)
-{
- pstring new_name;
- int outfd = -1;
- SMB_OFF_T size;
- struct stat st;
-
- pstrcpy(new_name, idmap_name);
- pstrcat(new_name, ".bak");
-
- DEBUG(10,("backup_old_idmap: backing up %s to %s before upgrade.\n",
- idmap_name, new_name ));
-
- if (tdb_lockall(idmap_tdb) == -1) {
- DEBUG(10,("backup_old_idmap: failed to lock %s. Error %s\n",
- idmap_name, tdb_errorstr(idmap_tdb) ));
- return False;
- }
- if ((outfd = open(new_name, O_CREAT|O_EXCL|O_RDWR, 0600)) == -1) {
- DEBUG(10,("backup_old_idmap: failed to open %s. Error %s\n",
- new_name, strerror(errno) ));
- goto fail;
- }
-
- if (fstat(idmap_tdb->fd, &st) == -1) {
- DEBUG(10,("backup_old_idmap: failed to fstat %s. Error %s\n",
- idmap_name, strerror(errno) ));
- goto fail;
- }
-
- size = (SMB_OFF_T)st.st_size;
-
- if (transfer_file(idmap_tdb->fd, outfd, size) != size ) {
- DEBUG(10,("backup_old_idmap: failed to copy %s. Error %s\n",
- idmap_name, strerror(errno) ));
- goto fail;
- }
-
- if (close(outfd) == -1) {
- DEBUG(10,("backup_old_idmap: failed to close %s. Error %s\n",
- idmap_name, strerror(errno) ));
- outfd = -1;
- goto fail;
- }
- tdb_unlockall(idmap_tdb);
- return True;
-
-fail:
-
- if (outfd != -1)
- close(outfd);
- tdb_unlockall(idmap_tdb);
- return False;
-}
-#endif
-
-/*****************************************************************************
- Convert the idmap database from an older version.
-*****************************************************************************/
-
-static BOOL 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 0
- /* Make a backup copy before doing anything else.... */
- if (!backup_old_idmap(idmap_name))
- return False;
-#endif
-
- 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, ("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, ("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, convert_fn, NULL);
-
- if (tdb_store_int32(idmap_tdb, "IDMAP_VERSION", IDMAP_VERSION) == -1) {
- DEBUG(0, ("idmap_convert: Unable to byteswap group hwm in idmap database\n"));
- return False;
- }
-
- return True;
-}
-
-/*****************************************************************************
- Initialise idmap database.
-*****************************************************************************/
-
-BOOL winbindd_idmap_init(void)
-{
- /* Open tdb cache */
-
- if (!(idmap_tdb = tdb_open_log(lock_path("winbindd_idmap.tdb"), 0,
- TDB_DEFAULT, O_RDWR | O_CREAT, 0600))) {
- DEBUG(0, ("winbindd_idmap_init: Unable to open idmap database\n"));
- return False;
- }
-
- /* possibly convert from an earlier version */
- if (!idmap_convert(lock_path("winbindd_idmap.tdb"))) {
- DEBUG(0, ("winbindd_idmap_init: Unable to open idmap database\n"));
- return False;
- }
-
- /* 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, server_state.uid_low) == -1) {
- DEBUG(0, ("winbindd_idmap_init: Unable to initialise user hwm in idmap database\n"));
- return False;
- }
- }
-
- if (tdb_fetch_int32(idmap_tdb, HWM_GROUP) == -1) {
- if (tdb_store_int32(idmap_tdb, HWM_GROUP, server_state.gid_low) == -1) {
- DEBUG(0, ("winbindd_idmap_init: Unable to initialise group hwm in idmap database\n"));
- return False;
- }
- }
-
- return True;