user-util: Add default_root_shell_at()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 11 Apr 2023 13:21:51 +0000 (15:21 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 11 Apr 2023 13:21:51 +0000 (15:21 +0200)
src/basic/user-util.c
src/basic/user-util.h

index 551e98b4272dfacd0fc8a74f9b9ea0beb560f81c..cbf00a173a43cb46050280ea254cd143e9999f0f 100644 (file)
@@ -156,21 +156,32 @@ bool is_nologin_shell(const char *shell) {
                            "/usr/bin/true");
 }
 
-const char* default_root_shell(const char *root) {
+const char* default_root_shell_at(int rfd) {
         /* We want to use the preferred shell, i.e. DEFAULT_USER_SHELL, which usually
          * will be /bin/bash. Fall back to /bin/sh if DEFAULT_USER_SHELL is not found,
          * or any access errors. */
 
-        int r = chase(DEFAULT_USER_SHELL, root, CHASE_PREFIX_ROOT, NULL, NULL);
+        assert(rfd >= 0 || rfd == AT_FDCWD);
+
+        int r = chaseat(rfd, DEFAULT_USER_SHELL, CHASE_AT_RESOLVE_IN_ROOT, NULL, NULL);
         if (r < 0 && r != -ENOENT)
-                log_debug_errno(r, "Failed to look up shell '%s%s%s': %m",
-                                strempty(root), root ? "/" : "", DEFAULT_USER_SHELL);
+                log_debug_errno(r, "Failed to look up shell '%s': %m", DEFAULT_USER_SHELL);
         if (r > 0)
                 return DEFAULT_USER_SHELL;
 
         return "/bin/sh";
 }
 
+const char *default_root_shell(const char *root) {
+        _cleanup_close_ int rfd = -EBADF;
+
+        rfd = open(empty_to_root(root), O_CLOEXEC | O_DIRECTORY | O_PATH);
+        if (rfd < 0)
+                return "/bin/sh";
+
+        return default_root_shell_at(rfd);
+}
+
 static int synthesize_user_creds(
                 const char **username,
                 uid_t *uid, gid_t *gid,
index f7ff35d0e07041e9c31a7a96b26f188c1991d38a..5aca6307e05bff7567efbc3cce20dad0a98ca42e 100644 (file)
@@ -131,6 +131,7 @@ int putsgent_sane(const struct sgrp *sg, FILE *stream);
 #endif
 
 bool is_nologin_shell(const char *shell);
+const char* default_root_shell_at(int rfd);
 const char* default_root_shell(const char *root);
 
 int is_this_me(const char *username);