From c60d6ee5b765ebc823c78f5e673db3f146f23913 Mon Sep 17 00:00:00 2001
From: Cristiano Urban <cristiano.urban@inaf.it>
Date: Tue, 27 Sep 2022 14:33:05 +0200
Subject: [PATCH] Get user UID and GID from transfer node user folder.

Signed-off-by: Cristiano Urban <cristiano.urban@inaf.it>
---
 transfer_service/data_rpc_server.py |  8 ++++----
 transfer_service/system_utils.py    | 20 +++++++++++++-------
 2 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/transfer_service/data_rpc_server.py b/transfer_service/data_rpc_server.py
index cb8ccd2..158fe34 100644
--- a/transfer_service/data_rpc_server.py
+++ b/transfer_service/data_rpc_server.py
@@ -169,10 +169,10 @@ class DataRPCServer(RedisRPCServer):
                                      "errorCode": 2,
                                      "errorMsg": errorMsg }
                         return response
-                uid = os.stat(folderPath).st_uid
-                gid = os.stat(folderPath).st_gid
-                # Check if uid and gid match and avoid privilege escalation
-                if uid == userInfo[1] and gid == userInfo[2] and uid >= 1000 and gid >= 100:
+                uid = userInfo[1]
+                gid = userInfo[2]
+                # Avoid privilege escalation
+                if uid >= 1000 and gid >= 100:
                     # If write permissions are set and the 'store' folder is not empty,
                     # it means that data is ready to be copied, otherwise, nothing can
                     # be done until the write permissions are restored or new data is
diff --git a/transfer_service/system_utils.py b/transfer_service/system_utils.py
index ac2aacf..3531c34 100644
--- a/transfer_service/system_utils.py
+++ b/transfer_service/system_utils.py
@@ -6,11 +6,12 @@
 #
 
 import os
-import pwd
 import re
 import shutil
 import sys
 
+from config import Config
+
 
 class SystemUtils(object):
 
@@ -24,19 +25,24 @@ class SystemUtils(object):
             }
 
     def __init__(self):
-        pass
+        config = Config("/etc/vos_ts/vos_ts.conf")
+        params = config.loadSection("transfer_node")
+        self.vospaceUserBasePath = params["base_path"]
+
 
     def userInfo(self, username):
         """
-        Returns username, UID and GID associated to a given user,
-        using the Python password database module (pwd).
+        Obtains username, UID and GID associated to a given user
+        from the user directory (name.surname) on the transfer node.
         """
         try:
-            info = pwd.getpwnam(username)
-        except KeyError:
+            vospaceUserFolderPath = self.vospaceUserBasePath.replace("{username}", username)
+            uid = os.stat(vospaceUserFolderPath).st_uid
+            gid = os.stat(vospaceUserFolderPath).st_gid
+        except OSError:
             return False
         else:
-            return [ info[0], info[2], info[3] ]
+            return [ username, uid, gid ]
 
     def findIllegalCharacters(self, name):
         """Checks for file/dir names containing illegal characters."""
-- 
GitLab