diff options
author | Lennart Poettering <lennart@poettering.net> | 2021-10-21 18:45:27 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2021-10-27 22:37:56 +0200 |
commit | 812e5876966216388216d31afb0e6367d2a87b4d (patch) | |
tree | 67246c21dc9b943ea1b101d7988455cd73ae5bcb | |
parent | homework: pass header user record in home_activate_cifs() (diff) | |
download | systemd-812e5876966216388216d31afb0e6367d2a87b4d.tar.xz systemd-812e5876966216388216d31afb0e6367d2a87b4d.zip |
homework: rework home_setup_cifs() to store "mounted" variable in HomeSetup
We already have a field for that, let's use it. Let's also reduce the
indentation level a bit.
No change in behaviour.
-rw-r--r-- | src/home/homework-cifs.c | 131 |
1 files changed, 68 insertions, 63 deletions
diff --git a/src/home/homework-cifs.c b/src/home/homework-cifs.c index 43e0ad5c1e..399df37b41 100644 --- a/src/home/homework-cifs.c +++ b/src/home/homework-cifs.c @@ -18,80 +18,85 @@ int home_setup_cifs( HomeSetupFlags flags, HomeSetup *setup) { + char **pw; + int r; + assert(h); - assert(setup); assert(user_record_storage(h) == USER_CIFS); + assert(setup); + assert(!setup->undo_mount); + assert(setup->root_fd < 0); - if (FLAGS_SET(flags, HOME_SETUP_ALREADY_ACTIVATED)) + if (FLAGS_SET(flags, HOME_SETUP_ALREADY_ACTIVATED)) { setup->root_fd = open(user_record_home_directory(h), O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW); - else { - bool mounted = false; - char **pw; - int r; + if (setup->root_fd < 0) + return log_error_errno(errno, "Failed to open home directory: %m"); + + return 0; + } - r = home_unshare_and_mkdir(); + r = home_unshare_and_mkdir(); + if (r < 0) + return r; + + STRV_FOREACH(pw, h->password) { + _cleanup_(unlink_and_freep) char *p = NULL; + _cleanup_free_ char *options = NULL; + _cleanup_(fclosep) FILE *f = NULL; + pid_t mount_pid; + int exit_status; + + r = fopen_temporary(NULL, &f, &p); if (r < 0) - return r; + return log_error_errno(r, "Failed to create temporary credentials file: %m"); - STRV_FOREACH(pw, h->password) { - _cleanup_(unlink_and_freep) char *p = NULL; - _cleanup_free_ char *options = NULL; - _cleanup_(fclosep) FILE *f = NULL; - pid_t mount_pid; - int exit_status; - - r = fopen_temporary(NULL, &f, &p); - if (r < 0) - return log_error_errno(r, "Failed to create temporary credentials file: %m"); - - fprintf(f, - "username=%s\n" - "password=%s\n", - user_record_cifs_user_name(h), - *pw); - - if (h->cifs_domain) - fprintf(f, "domain=%s\n", h->cifs_domain); - - r = fflush_and_check(f); - if (r < 0) - return log_error_errno(r, "Failed to write temporary credentials file: %m"); - - f = safe_fclose(f); - - if (asprintf(&options, "credentials=%s,uid=" UID_FMT ",forceuid,gid=" GID_FMT ",forcegid,file_mode=0%3o,dir_mode=0%3o", - p, h->uid, user_record_gid(h), user_record_access_mode(h), user_record_access_mode(h)) < 0) - return log_oom(); - - r = safe_fork("(mount)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_STDOUT_TO_STDERR, &mount_pid); - if (r < 0) - return r; - if (r == 0) { - /* Child */ - execl("/bin/mount", "/bin/mount", "-n", "-t", "cifs", - h->cifs_service, HOME_RUNTIME_WORK_DIR, - "-o", options, NULL); - - log_error_errno(errno, "Failed to execute mount: %m"); - _exit(EXIT_FAILURE); - } - - exit_status = wait_for_terminate_and_check("mount", mount_pid, WAIT_LOG_ABNORMAL|WAIT_LOG_NON_ZERO_EXIT_STATUS); - if (exit_status < 0) - return exit_status; - if (exit_status != EXIT_SUCCESS) - return -EPROTO; - - mounted = true; - break; + fprintf(f, + "username=%s\n" + "password=%s\n", + user_record_cifs_user_name(h), + *pw); + + if (h->cifs_domain) + fprintf(f, "domain=%s\n", h->cifs_domain); + + r = fflush_and_check(f); + if (r < 0) + return log_error_errno(r, "Failed to write temporary credentials file: %m"); + + f = safe_fclose(f); + + if (asprintf(&options, "credentials=%s,uid=" UID_FMT ",forceuid,gid=" GID_FMT ",forcegid,file_mode=0%3o,dir_mode=0%3o", + p, h->uid, user_record_gid(h), user_record_access_mode(h), user_record_access_mode(h)) < 0) + return log_oom(); + + r = safe_fork("(mount)", FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_STDOUT_TO_STDERR, &mount_pid); + if (r < 0) + return r; + if (r == 0) { + /* Child */ + execl("/bin/mount", "/bin/mount", "-n", "-t", "cifs", + h->cifs_service, HOME_RUNTIME_WORK_DIR, + "-o", options, NULL); + + log_error_errno(errno, "Failed to execute mount: %m"); + _exit(EXIT_FAILURE); } - if (!mounted) - return log_error_errno(SYNTHETIC_ERRNO(ENOKEY), - "Failed to mount home directory with supplied password."); + exit_status = wait_for_terminate_and_check("mount", mount_pid, WAIT_LOG_ABNORMAL|WAIT_LOG_NON_ZERO_EXIT_STATUS); + if (exit_status < 0) + return exit_status; + if (exit_status != EXIT_SUCCESS) + return -EPROTO; - setup->root_fd = open(HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW); + setup->undo_mount = true; + break; } + + if (!setup->undo_mount) + return log_error_errno(SYNTHETIC_ERRNO(ENOKEY), + "Failed to mount home directory with supplied password."); + + setup->root_fd = open(HOME_RUNTIME_WORK_DIR, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW); if (setup->root_fd < 0) return log_error_errno(errno, "Failed to open home directory: %m"); |