[PATCH] fill_user_info: fake user info for 'root' if it can not be found

Tom Gundersen teg at jklm.no
Mon Feb 25 07:14:14 PST 2013


This should make make libdbus a bit more robust against bugs or configuration
errors. In particular, it should make it work on an empty /etc or in case nss is
broken, misconfigured or simply missing.

I found this useful whilst hacking on systemd in the initramfs. With this
change, it would be possible to automatically deduce (using ldd, etc) everything
that should be included in a initramfs only based on a set of systemd service
files and udev rules, otherwise we need to keep a hardcoded list of files to
make lidbus work. See
<http://lists.freedesktop.org/archives/systemd-devel/2013-February/009205.html>,
for how it currently looks.
---
 dbus/dbus-sysdeps-unix.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c
index b4ecc96..e80772b 100644
--- a/dbus/dbus-sysdeps-unix.c
+++ b/dbus/dbus-sysdeps-unix.c
@@ -2105,6 +2105,20 @@ fill_user_info (DBusUserInfo       *info,
             break;
           }
       }
+    //Make sure 'root' always exists, even if nss is not configured correctly
+    if ((result != 0 || p != &p_str) && (uid == 0 || strcmp(username_c, "root") == 0))
+      {
+        _dbus_warn ("Failed to find 'root' user, use fallback entry.");
+        p_str.pw_name = "root";
+        p_str.pw_passwd = "x";
+        p_str.pw_uid = 0;
+        p_str.pw_gid = 0;
+        p_str.pw_gecos = NULL;
+        p_str.pw_dir = "/root";
+        p_str.pw_shell = "/bin/sh";
+        p = &p_str;
+        result = 0;
+      }
     if (result == 0 && p == &p_str)
       {
         if (!fill_user_info_from_passwd (p, info, error))
@@ -2128,12 +2142,26 @@ fill_user_info (DBusUserInfo       *info,
   {
     /* I guess we're screwed on thread safety here */
     struct passwd *p;
+    struct passwd p_str;
 
     if (uid != DBUS_UID_UNSET)
       p = getpwuid (uid);
     else
       p = getpwnam (username_c);
 
+    //Make sure 'root' always exists, even if nss is not configured correctly
+    if (p == NULL && (uid == 0 || strcmp(username_c, "root") == 0))
+      {
+        _dbus_warn ("Failed to find 'root' user, use fallback entry.");
+        p_str.pw_name = "root";
+        p_str.pw_passwd = "x";
+        p_str.pw_uid = 0;
+        p_str.pw_gid = 0;
+        p_str.pw_gecos = NULL;
+        p_str.pw_dir = "/root";
+        p_str.pw_shell = "/bin/sh";
+        p = &p_str;
+      }
     if (p != NULL)
       {
         if (!fill_user_info_from_passwd (p, info, error))
-- 
1.8.1.4



More information about the dbus mailing list