Skip to contentSkip to conversation

kernel-shark: Retrieve the home of root from "/etc/passwd"

PATCH In review Yordan Karadzhov (VMware) 3 participants 6 messages Nov 28, 2019 – Dec 13, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel-shark: Retrieve the home of root from "/etc/passwd"

Do not assume that root is always at "/root". Instead read the "/etc/passwd" file and searches for the user id of 0. Return the home path for that user. On any error just quietly default back to "/root".

---
 kernel-shark/src/KsMainWindow.cpp | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
kernel-shark/src/KsMainWindow.cpp +25 -1
index bd6c338..fa887fc 100644
@@ -422,6 +422,30 @@ QString KsMainWindow::_getCacheDir()
 		QDir().mkpath(dir);
 	};
 
+	auto lamRootHome = [] () {
+		QFile fPswd("/etc/passwd");
+		QString home("/root");
+		QStringList userInfo;
+
+		fPswd.open(QIODevice::ReadOnly);
+		if (!fPswd.isOpen())
+			return home;
+
+		QTextStream s(&fPswd);
+		while (!s.atEnd()) {
+			userInfo = s.readLine().split(':');
+
+			/* Check the User Id. */
+			if (userInfo[2].toInt() == 0) {
+				home = userInfo[5];
+				break;
+			}
+		}
+
+		fPswd.close();
+		return home;
+	};
+
 	dir = getenv("KS_USER_CACHE_DIR");
 	if (!dir.isEmpty()) {
 		if (!QDir(dir).exists())
@@ -432,7 +456,7 @@ QString KsMainWindow::_getCacheDir()
 		dir += "/kernelshark";
 
 		if (geteuid() == 0)
-			dir.replace(QDir::homePath(), "/root");
+			dir.replace(QDir::homePath(), lamRootHome());
 
 		if (!QDir(dir).exists())
 			lamMakePath(false);
Signature · 1 line
2.20.1
#1 lore raw

On Thu, Nov 28, 2019 at 1:46 PM Yordan Karadzhov (VMware) <[email protected]> wrote:
[...]

Quoted 10 lines
kernel-shark/src/KsMainWindow.cpp +10 -0
+       auto lamRootHome = [] () {
+               QFile fPswd("/etc/passwd");
+               QString home("/root");
+               QStringList userInfo;
+
+               fPswd.open(QIODevice::ReadOnly);
+               if (!fPswd.isOpen())
+                       return home;
+
+               QTextStream s(&fPswd);

kernel-shark/src/KsMainWindow.cpp QString KsMainWindow::_getCacheDir() · line 444

Quoted 10 lines
kernel-shark/src/KsMainWindow.cpp +10 -0
+               while (!s.atEnd()) {
+                       userInfo = s.readLine().split(':');
+
+                       /* Check the User Id. */
+                       if (userInfo[2].toInt() == 0) {
+                               home = userInfo[5];
+                               break;
+                       }
+               }
+

Another option is to use getpwent(3) [0] instead of parsing /etc/passwd by hand.

Cheers,

-- Slavi

[0] http://man7.org/linux/man-pages/man3/getpwent.3.html

#2 lore raw
Quoted 17 lines · On Thu, 28 Nov 2019 16:27:11 +0200 Slavomir Kaslev <[email protected]>…

On Thu, Nov 28, 2019 at 1:46 PM Yordan Karadzhov (VMware) <[email protected]> wrote:
[...]

Quoted 14 lines
kernel-shark/src/KsMainWindow.cpp +14 -0
+       auto lamRootHome = [] () {
+               QFile fPswd("/etc/passwd");
+               QString home("/root");
+               QStringList userInfo;
+
+               fPswd.open(QIODevice::ReadOnly);
+               if (!fPswd.isOpen())
+                       return home;
+
+               QTextStream s(&fPswd);
+               while (!s.atEnd()) {
+                       userInfo = s.readLine().split(':');
+
+                       /* Check the User Id. */
Quoted 8 lines

kernel-shark/src/KsMainWindow.cpp QString KsMainWindow::_getCacheDir() · line 444

Quoted 6 lines
kernel-shark/src/KsMainWindow.cpp +6 -0
+                       if (userInfo[2].toInt() == 0) {
+                               home = userInfo[5];
+                               break;
+                       }
+               }
+  

Another option is to use getpwent(3) [0] instead of parsing /etc/passwd by hand.

Thank you! My google fu is starting to show its age, as I did a bit of searching looking for a library function to parse passwd, knowing one had to be there, but came up empty. This is exactly what I was looking for.

Yes, Yordan, I think using this would be the better option as if I had found it, it would have been what I would have suggested in the first place.

-- Steve

#3 lore raw
In reply to Slavomir Kaslev
Quoted 17 lines · Slavomir Kaslev

On Thu, Nov 28, 2019 at 1:46 PM Yordan Karadzhov (VMware) <[email protected]> wrote:
[...]

Quoted 14 lines
kernel-shark/src/KsMainWindow.cpp +14 -0
+       auto lamRootHome = [] () {
+               QFile fPswd("/etc/passwd");
+               QString home("/root");
+               QStringList userInfo;
+
+               fPswd.open(QIODevice::ReadOnly);
+               if (!fPswd.isOpen())
+                       return home;
+
+               QTextStream s(&fPswd);
+               while (!s.atEnd()) {
+                       userInfo = s.readLine().split(':');
+
+                       /* Check the User Id. */
Quoted 9 lines

kernel-shark/src/KsMainWindow.cpp QString KsMainWindow::_getCacheDir() · line 444

Quoted 6 lines
kernel-shark/src/KsMainWindow.cpp +6 -0
+                       if (userInfo[2].toInt() == 0) {
+                               home = userInfo[5];
+                               break;
+                       }
+               }
+

Another option is to use getpwent(3) [0] instead of parsing /etc/passwd by hand.

Hi Slavi,

Thanks for pointing out this!
This simplifies the patch a lot. I am sending a new version.

cheers,
Yordan

Quoted 6 lines

Cheers,

-- Slavi

[0] http://man7.org/linux/man-pages/man3/getpwent.3.html

#4 lore raw

On Thu, Dec 12, 2019 at 3:30 PM Yordan Karadzhov (VMware) <[email protected]> wrote:
[...]

Quoted 2 lines

Thanks for pointing out this!
This simplifies the patch a lot. I am sending a new version.

I just saw the patch and I think I mislead you (sorry about that) by referring you to getpwent(3), since getpwuid(3) is a better match for this use-case and will make the patch even smaller.

Signature · 1 line
Slavomir Kaslev
#5 lore raw
Quoted 10 lines · Slavomir Kaslev

On Thu, Dec 12, 2019 at 3:30 PM Yordan Karadzhov (VMware) <[email protected]> wrote:
[...]

Quoted 2 lines

Thanks for pointing out this!
This simplifies the patch a lot. I am sending a new version.

I just saw the patch and I think I mislead you (sorry about that) by referring you to getpwent(3), since getpwuid(3) is a better match for this use-case and will make the patch even smaller.

Yes, getpwuid(3) is perfect. Thanks a lot!
Sending v3.

Y.