]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/nspawn/nspawn-setuid.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / nspawn / nspawn-setuid.c
index c4ad172512296eb6449da65adcb558d4ac86c106..8f92608bdc751651bfc89acdebf41db26d9ab076 100644 (file)
@@ -1,21 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 /***
-  This file is part of systemd.
-
   Copyright 2015 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
 #include <grp.h>
@@ -48,30 +33,18 @@ static int spawn_getent(const char *database, const char *key, pid_t *rpid) {
                 return log_error_errno(errno, "Failed to allocate pipe: %m");
 
         r = safe_fork("(getent)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
-        if (r < 0)
+        if (r < 0) {
+                safe_close_pair(pipe_fds);
                 return r;
+        }
         if (r == 0) {
-                int nullfd;
                 char *empty_env = NULL;
 
-                if (dup3(pipe_fds[1], STDOUT_FILENO, 0) < 0)
-                        _exit(EXIT_FAILURE);
+                safe_close(pipe_fds[0]);
 
-                safe_close_above_stdio(pipe_fds[0]);
-                safe_close_above_stdio(pipe_fds[1]);
-
-                nullfd = open("/dev/null", O_RDWR);
-                if (nullfd < 0)
-                        _exit(EXIT_FAILURE);
-
-                if (dup3(nullfd, STDIN_FILENO, 0) < 0)
+                if (rearrange_stdio(-1, pipe_fds[1], -1) < 0)
                         _exit(EXIT_FAILURE);
 
-                if (dup3(nullfd, STDERR_FILENO, 0) < 0)
-                        _exit(EXIT_FAILURE);
-
-                safe_close_above_stdio(nullfd);
-
                 close_all_fds(NULL, 0);
 
                 execle("/usr/bin/getent", "getent", database, key, NULL, &empty_env);
@@ -238,8 +211,8 @@ int change_uid_gid(const char *user, char **_home) {
         if (r < 0)
                 return log_error_errno(r, "Failed to make home root directory: %m");
 
-        r = mkdir_safe(home, 0755, uid, gid, false);
-        if (r < 0 && r != -EEXIST)
+        r = mkdir_safe(home, 0755, uid, gid, 0);
+        if (r < 0 && !IN_SET(r, -EEXIST, -ENOTDIR))
                 return log_error_errno(r, "Failed to make home directory: %m");
 
         (void) fchown(STDIN_FILENO, uid, gid);
@@ -255,10 +228,8 @@ int change_uid_gid(const char *user, char **_home) {
         if (setresuid(uid, uid, uid) < 0)
                 return log_error_errno(errno, "setresuid() failed: %m");
 
-        if (_home) {
-                *_home = home;
-                home = NULL;
-        }
+        if (_home)
+                *_home = TAKE_PTR(home);
 
         return 0;
 }