summaryrefslogtreecommitdiffstats
path: root/src/home/homework-cifs.c
blob: 2736095f939789839c40a16b336c9237b483ab46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "dirent-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-util.h"
#include "fs-util.h"
#include "homework-cifs.h"
#include "homework-mount.h"
#include "mount-util.h"
#include "process-util.h"
#include "strv.h"
#include "tmpfile-util.h"

int home_prepare_cifs(
                UserRecord *h,
                bool already_activated,
                HomeSetup *setup) {

        assert(h);
        assert(setup);
        assert(user_record_storage(h) == USER_CIFS);

        if (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;

                r = home_unshare_and_mount(NULL, NULL, false, user_record_mount_flags(h));
                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 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=" UID_FMT ",forcegid,file_mode=0%3o,dir_mode=0%3o",
                                     p, h->uid, h->uid, h->access_mode, h->access_mode) < 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, "/run/systemd/user-home-mount",
                                      "-o", options, NULL);

                                log_error_errno(errno, "Failed to execute fsck: %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;
                }

                if (!mounted)
                        return log_error_errno(ENOKEY, "Failed to mount home directory with supplied password.");

                setup->root_fd = open("/run/systemd/user-home-mount", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
        }
        if (setup->root_fd < 0)
                return log_error_errno(errno, "Failed to open home directory: %m");

        return 0;
}

int home_activate_cifs(
                UserRecord *h,
                PasswordCache *cache,
                UserRecord **ret_home) {

        _cleanup_(home_setup_undo) HomeSetup setup = HOME_SETUP_INIT;
        _cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
        const char *hdo, *hd;
        int r;

        assert(h);
        assert(user_record_storage(h) == USER_CIFS);
        assert(ret_home);

        if (!h->cifs_service)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "User record lacks CIFS service, refusing.");

        assert_se(hdo = user_record_home_directory(h));
        hd = strdupa(hdo); /* copy the string out, since it might change later in the home record object */

        r = home_prepare_cifs(h, false, &setup);
        if (r < 0)
                return r;

        r = home_refresh(h, &setup, NULL, cache, NULL, &new_home);
        if (r < 0)
                return r;

        setup.root_fd = safe_close(setup.root_fd);

        r = home_move_mount(NULL, hd);
        if (r < 0)
                return r;

        setup.undo_mount = false;

        log_info("Everything completed.");

        *ret_home = TAKE_PTR(new_home);
        return 1;
}

int home_create_cifs(UserRecord *h, UserRecord **ret_home) {
        _cleanup_(home_setup_undo) HomeSetup setup = HOME_SETUP_INIT;
        _cleanup_(user_record_unrefp) UserRecord *new_home = NULL;
        _cleanup_(closedirp) DIR *d = NULL;
        _cleanup_close_ int copy = -1;
        int r;

        assert(h);
        assert(user_record_storage(h) == USER_CIFS);
        assert(ret_home);

        if (!h->cifs_service)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "User record lacks CIFS service, refusing.");

        if (access("/sbin/mount.cifs", F_OK) < 0) {
                if (errno == ENOENT)
                        return log_error_errno(SYNTHETIC_ERRNO(ENOLINK), "/sbin/mount.cifs is missing.");

                return log_error_errno(errno, "Unable to detect whether /sbin/mount.cifs exists: %m");
        }

        r = home_prepare_cifs(h, false, &setup);
        if (r < 0)
                return r;

        copy = fcntl(setup.root_fd, F_DUPFD_CLOEXEC, 3);
        if (copy < 0)
                return -errno;

        d = take_fdopendir(&copy);
        if (!d)
                return -errno;

        errno = 0;
        if (readdir_no_dot(d))
                return log_error_errno(SYNTHETIC_ERRNO(ENOTEMPTY), "Selected CIFS directory not empty, refusing.");
        if (errno != 0)
                return log_error_errno(errno, "Failed to detect if CIFS directory is empty: %m");

        r = home_populate(h, setup.root_fd);
        if (r < 0)
                return r;

        r = home_sync_and_statfs(setup.root_fd, NULL);
        if (r < 0)
                return r;

        r = user_record_clone(h, USER_RECORD_LOAD_MASK_SECRET, &new_home);
        if (r < 0)
                return log_error_errno(r, "Failed to clone record: %m");

        r = user_record_add_binding(
                        new_home,
                        USER_CIFS,
                        NULL,
                        SD_ID128_NULL,
                        SD_ID128_NULL,
                        SD_ID128_NULL,
                        NULL,
                        NULL,
                        UINT64_MAX,
                        NULL,
                        NULL,
                        h->uid,
                        (gid_t) h->uid);
        if (r < 0)
                return log_error_errno(r, "Failed to add binding to record: %m");

        log_info("Everything completed.");

        *ret_home = TAKE_PTR(new_home);
        return 0;
}