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
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* This file is part of libmount from util-linux project.
*
* Copyright (C) 2022 Karel Zak <kzak@redhat.com>
*
* libmount 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.
*
*
* This is X-mount.owner=, X-mount.group= and X-mount.mode= implementation.
*
* Please, see the comment in libmount/src/hooks.c to understand how hooks work.
*/
#include <sched.h>
#include "mountP.h"
#include "fileutils.h"
struct hook_data {
uid_t owner;
gid_t group;
mode_t mode;
};
/* de-initiallize this module */
static int hookset_deinit(struct libmnt_context *cxt, const struct libmnt_hookset *hs)
{
void *data;
DBG(HOOK, ul_debugobj(hs, "deinit '%s'", hs->name));
/* remove all our hooks and free hook data */
while (mnt_context_remove_hook(cxt, hs, 0, &data) == 0) {
free(data);
data = NULL;
}
return 0;
}
static int hook_post(
struct libmnt_context *cxt,
const struct libmnt_hookset *hs __attribute__((__unused__)),
void *data)
{
struct hook_data *hd = (struct hook_data *) data;
const char *target;
int rc = 0;
assert(cxt);
if (!hd || !cxt->fs)
return 0;
target = mnt_fs_get_target(cxt->fs);
if (!target)
return 0;
if (hd->owner != (uid_t) -1 || hd->group != (uid_t) -1) {
DBG(CXT, ul_debugobj(cxt, " lchown(%s, %u, %u)", target, hd->owner, hd->group));
if (lchown(target, hd->owner, hd->group) == -1)
return -MNT_ERR_CHOWN;
}
if (hd->mode != (mode_t) -1) {
DBG(CXT, ul_debugobj(cxt, " chmod(%s, %04o)", target, hd->mode));
if (chmod(target, hd->mode) == -1)
return -MNT_ERR_CHMOD;
}
return rc;
}
static inline struct hook_data *new_hook_data(void)
{
struct hook_data *hd = calloc(1, sizeof(*hd));
if (!hd)
return NULL;
hd->owner = (uid_t) -1;
hd->group = (gid_t) -1;
hd->mode = (mode_t) -1;
return hd;
}
static int hook_prepare_options(
struct libmnt_context *cxt,
const struct libmnt_hookset *hs,
void *data __attribute__((__unused__)))
{
struct hook_data *hd = NULL;
struct libmnt_optlist *ol;
struct libmnt_opt *opt;
int rc = 0;
assert(cxt);
assert(cxt->map_userspace);
ol = mnt_context_get_optlist(cxt);
if (!ol)
return -ENOMEM;
opt = mnt_optlist_get_named(ol, "X-mount.owner", cxt->map_userspace);
if (opt) {
const char *value = mnt_opt_get_value(opt);
if (!value)
goto fail;
if (!hd) {
hd = new_hook_data();
if (!hd)
goto fail;
}
if (mnt_parse_uid(value, strlen(value), &hd->owner))
goto fail;
}
opt = mnt_optlist_get_named(ol, "X-mount.group", cxt->map_userspace);
if (opt) {
const char *value = mnt_opt_get_value(opt);
if (!value)
goto fail;
if (!hd) {
hd = new_hook_data();
if (!hd)
goto fail;
}
if (mnt_parse_gid(value, strlen(value), &hd->group))
goto fail;
}
opt = mnt_optlist_get_named(ol, "X-mount.mode", cxt->map_userspace);
if (opt) {
const char *value = mnt_opt_get_value(opt);
if (!value)
goto fail;
if (!hd) {
hd = new_hook_data();
if (!hd)
goto fail;
}
if (mnt_parse_mode(value, strlen(value), &hd->mode))
goto fail;
}
if (hd) {
DBG(CXT, ul_debugobj(cxt, " wanted ownership %d:%d, mode %04o",
hd->owner, hd->group, hd->mode));
rc = mnt_context_append_hook(cxt, hs,
MNT_STAGE_POST,
hd, hook_post);
if (rc < 0)
goto fail;
}
return 0;
fail:
if (rc == 0)
rc = -MNT_ERR_MOUNTOPT;
free(hd);
return rc;
}
const struct libmnt_hookset hookset_owner =
{
.name = "__owner",
.firststage = MNT_STAGE_PREP_OPTIONS,
.firstcall = hook_prepare_options,
.deinit = hookset_deinit
};
|