From 77062e68f8675a678a6d136b85fbe1dd2bdb75f1 Mon Sep 17 00:00:00 2001
From: Panu Matilainen <pmatilai@redhat.com>
Date: Tue, 2 Feb 2021 14:26:48 +0200
Subject: [PATCH] Honor requested file permissions when creating ndb database
 files

Prior to this, ndb files were using hardcoded 0666 permissions whereas
rpm generally defaults to 0644.
---
 lib/backend/ndb/glue.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/backend/ndb/glue.c b/lib/backend/ndb/glue.c
index a1599d8fa..96ab88679 100644
--- a/lib/backend/ndb/glue.c
+++ b/lib/backend/ndb/glue.c
@@ -129,13 +129,13 @@ static int ndb_Open(rpmdb rdb, rpmDbiTagVal rpmtag, dbiIndex * dbip, int flags)
 	char *path = rstrscat(NULL, dbhome, "/", rdb->db_ops->path, NULL);
 	rpmlog(RPMLOG_DEBUG, "opening  db index       %s mode=0x%x\n", path, rdb->db_mode);
 	if ((rdb->db_flags & RPMDB_FLAG_SALVAGE) == 0)
-	    rc = rpmpkgOpen(&pkgdb, path, oflags, 0666);
+	    rc = rpmpkgOpen(&pkgdb, path, oflags, rdb->db_perms);
 	else
 	    rc = rpmpkgSalvage(&pkgdb, path);
  	if (rc && errno == ENOENT && (rdb->db_flags & RPMDB_FLAG_SALVAGE) == 0) {
 	    oflags = O_RDWR|O_CREAT;
 	    dbi->dbi_flags |= DBI_CREATED;
-	    rc = rpmpkgOpen(&pkgdb, path, oflags, 0666);
+	    rc = rpmpkgOpen(&pkgdb, path, oflags, rdb->db_perms);
 	}
 	if (rc) {
 	    perror("rpmpkgOpen");
@@ -161,16 +161,16 @@ static int ndb_Open(rpmdb rdb, rpmDbiTagVal rpmtag, dbiIndex * dbip, int flags)
 
 	    /* Open indexes readwrite if possible */
 	    ioflags = O_RDWR;
-	    rc = rpmxdbOpen(&ndbenv->xdb, rdb->db_pkgs->dbi_db, path, ioflags, 0666);
+	    rc = rpmxdbOpen(&ndbenv->xdb, rdb->db_pkgs->dbi_db, path, ioflags, rdb->db_perms);
 	    if (rc && (errno == EACCES || errno == EROFS)) {
 		/* If it is not asked for rw explicitly, try to open ro */
 		if (!(oflags & O_RDWR)) {
 		    ioflags = O_RDONLY;
-		    rc = rpmxdbOpen(&ndbenv->xdb, rdb->db_pkgs->dbi_db, path, ioflags, 0666);
+		    rc = rpmxdbOpen(&ndbenv->xdb, rdb->db_pkgs->dbi_db, path, ioflags, rdb->db_perms);
 		}
 	    } else if (rc && errno == ENOENT) {
 		ioflags = O_CREAT|O_RDWR;
-		rc = rpmxdbOpen(&ndbenv->xdb, rdb->db_pkgs->dbi_db, path, ioflags, 0666);
+		rc = rpmxdbOpen(&ndbenv->xdb, rdb->db_pkgs->dbi_db, path, ioflags, rdb->db_perms);
 		created = 1;
 	    }
 	    if (rc) {
-- 
2.27.0

