1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | POSIX NTVFS backend - xattr support using a tdb
|
---|
5 |
|
---|
6 | Copyright (C) Andrew Tridgell 2004
|
---|
7 |
|
---|
8 | This program is free software; you can redistribute it and/or modify
|
---|
9 | it under the terms of the GNU General Public License as published by
|
---|
10 | the Free Software Foundation; either version 3 of the License, or
|
---|
11 | (at your option) any later version.
|
---|
12 |
|
---|
13 | This program is distributed in the hope that it will be useful,
|
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | GNU General Public License for more details.
|
---|
17 |
|
---|
18 | You should have received a copy of the GNU General Public License
|
---|
19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "includes.h"
|
---|
23 | #include "lib/util/tdb_wrap.h"
|
---|
24 | #include <tdb.h>
|
---|
25 | #include "vfs_posix.h"
|
---|
26 |
|
---|
27 | #define XATTR_LIST_ATTR ".xattr_list"
|
---|
28 |
|
---|
29 | /*
|
---|
30 | we need to maintain a list of attributes on each file, so that unlink
|
---|
31 | can automatically clean them up
|
---|
32 | */
|
---|
33 | static NTSTATUS xattr_tdb_add_list(struct tdb_wrap *ea_tdb, TALLOC_CTX *ctx, const char *attr_name,
|
---|
34 | const char *fname, int fd)
|
---|
35 | {
|
---|
36 | DATA_BLOB blob;
|
---|
37 | TALLOC_CTX *mem_ctx;
|
---|
38 | const char *s;
|
---|
39 | NTSTATUS status;
|
---|
40 | size_t len;
|
---|
41 |
|
---|
42 | if (strcmp(attr_name, XATTR_LIST_ATTR) == 0) {
|
---|
43 | return NT_STATUS_OK;
|
---|
44 | }
|
---|
45 |
|
---|
46 | mem_ctx = talloc_new(ctx);
|
---|
47 |
|
---|
48 | status = pull_xattr_blob_tdb_raw(ea_tdb, mem_ctx, XATTR_LIST_ATTR,
|
---|
49 | fname, fd, 100, &blob);
|
---|
50 | if (!NT_STATUS_IS_OK(status)) {
|
---|
51 | blob = data_blob(NULL, 0);
|
---|
52 | }
|
---|
53 |
|
---|
54 | for (s=(const char *)blob.data; s < (const char *)(blob.data+blob.length); s += strlen(s) + 1) {
|
---|
55 | if (strcmp(attr_name, s) == 0) {
|
---|
56 | talloc_free(mem_ctx);
|
---|
57 | return NT_STATUS_OK;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | len = strlen(attr_name) + 1;
|
---|
62 |
|
---|
63 | blob.data = talloc_realloc(mem_ctx, blob.data, uint8_t, blob.length + len);
|
---|
64 | if (blob.data == NULL) {
|
---|
65 | talloc_free(mem_ctx);
|
---|
66 | return NT_STATUS_NO_MEMORY;
|
---|
67 | }
|
---|
68 | memcpy(blob.data + blob.length, attr_name, len);
|
---|
69 | blob.length += len;
|
---|
70 |
|
---|
71 | status = push_xattr_blob_tdb_raw(ea_tdb,ctx, XATTR_LIST_ATTR, fname, fd, &blob);
|
---|
72 | talloc_free(mem_ctx);
|
---|
73 |
|
---|
74 | return status;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /*
|
---|
78 | form a key for using in the ea_tdb
|
---|
79 | */
|
---|
80 | static NTSTATUS get_ea_tdb_key(TALLOC_CTX *mem_ctx,
|
---|
81 | const char *attr_name,
|
---|
82 | const char *fname, int fd,
|
---|
83 | TDB_DATA *key)
|
---|
84 | {
|
---|
85 | struct stat st;
|
---|
86 | size_t len = strlen(attr_name);
|
---|
87 |
|
---|
88 | if (fd == -1) {
|
---|
89 | if (stat(fname, &st) == -1) {
|
---|
90 | return NT_STATUS_NOT_FOUND;
|
---|
91 | }
|
---|
92 | } else {
|
---|
93 | if (fstat(fd, &st) == -1) {
|
---|
94 | return NT_STATUS_NOT_FOUND;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | key->dptr = talloc_array(mem_ctx, uint8_t, 16 + len);
|
---|
99 | if (key->dptr == NULL) {
|
---|
100 | return NT_STATUS_NO_MEMORY;
|
---|
101 | }
|
---|
102 | key->dsize = 16 + len;
|
---|
103 |
|
---|
104 | SBVAL(key->dptr, 0, st.st_dev);
|
---|
105 | SBVAL(key->dptr, 8, st.st_ino);
|
---|
106 | memcpy(key->dptr+16, attr_name, len);
|
---|
107 |
|
---|
108 | return NT_STATUS_OK;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 |
|
---|
113 | /*
|
---|
114 | pull a xattr as a blob, using the ea_tdb_context tdb
|
---|
115 | */
|
---|
116 | NTSTATUS pull_xattr_blob_tdb_raw(struct tdb_wrap *ea_tdb,
|
---|
117 | TALLOC_CTX *mem_ctx,
|
---|
118 | const char *attr_name,
|
---|
119 | const char *fname,
|
---|
120 | int fd,
|
---|
121 | size_t estimated_size,
|
---|
122 | DATA_BLOB *blob)
|
---|
123 | {
|
---|
124 | TDB_DATA tkey, tdata;
|
---|
125 | NTSTATUS status;
|
---|
126 |
|
---|
127 | status = get_ea_tdb_key(mem_ctx, attr_name, fname, fd, &tkey);
|
---|
128 | if (!NT_STATUS_IS_OK(status)) {
|
---|
129 | return status;
|
---|
130 | }
|
---|
131 |
|
---|
132 | tdata = tdb_fetch(ea_tdb->tdb, tkey);
|
---|
133 | if (tdata.dptr == NULL) {
|
---|
134 | return NT_STATUS_NOT_FOUND;
|
---|
135 | }
|
---|
136 |
|
---|
137 | *blob = data_blob_talloc(mem_ctx, tdata.dptr, tdata.dsize);
|
---|
138 | free(tdata.dptr);
|
---|
139 | if (blob->data == NULL) {
|
---|
140 | return NT_STATUS_NO_MEMORY;
|
---|
141 | }
|
---|
142 |
|
---|
143 | return NT_STATUS_OK;
|
---|
144 | }
|
---|
145 |
|
---|
146 | NTSTATUS pull_xattr_blob_tdb(struct pvfs_state *pvfs,
|
---|
147 | TALLOC_CTX *mem_ctx,
|
---|
148 | const char *attr_name,
|
---|
149 | const char *fname,
|
---|
150 | int fd,
|
---|
151 | size_t estimated_size,
|
---|
152 | DATA_BLOB *blob)
|
---|
153 | {
|
---|
154 | return pull_xattr_blob_tdb_raw(pvfs->ea_db,mem_ctx,attr_name,fname,fd,estimated_size,blob);
|
---|
155 | }
|
---|
156 |
|
---|
157 | /*
|
---|
158 | push a xattr as a blob, using ea_tdb
|
---|
159 | */
|
---|
160 | NTSTATUS push_xattr_blob_tdb_raw(struct tdb_wrap *ea_tdb,
|
---|
161 | TALLOC_CTX *mem_ctx,
|
---|
162 | const char *attr_name,
|
---|
163 | const char *fname,
|
---|
164 | int fd,
|
---|
165 | const DATA_BLOB *blob)
|
---|
166 | {
|
---|
167 | TDB_DATA tkey, tdata;
|
---|
168 | NTSTATUS status;
|
---|
169 |
|
---|
170 | status = get_ea_tdb_key(mem_ctx, attr_name, fname, fd, &tkey);
|
---|
171 | if (!NT_STATUS_IS_OK(status)) {
|
---|
172 | return status;
|
---|
173 | }
|
---|
174 |
|
---|
175 | tdata.dptr = blob->data;
|
---|
176 | tdata.dsize = blob->length;
|
---|
177 |
|
---|
178 | if (tdb_chainlock(ea_tdb->tdb, tkey) != 0) {
|
---|
179 | talloc_free(tkey.dptr);
|
---|
180 | return NT_STATUS_INTERNAL_DB_CORRUPTION;
|
---|
181 | }
|
---|
182 |
|
---|
183 | status = xattr_tdb_add_list(ea_tdb,mem_ctx, attr_name, fname, fd);
|
---|
184 | if (!NT_STATUS_IS_OK(status)) {
|
---|
185 | goto done;
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (tdb_store(ea_tdb->tdb, tkey, tdata, TDB_REPLACE) == -1) {
|
---|
189 | status = NT_STATUS_INTERNAL_DB_CORRUPTION;
|
---|
190 | }
|
---|
191 |
|
---|
192 | done:
|
---|
193 | tdb_chainunlock(ea_tdb->tdb, tkey);
|
---|
194 | talloc_free(tkey.dptr);
|
---|
195 | return status;
|
---|
196 | }
|
---|
197 | NTSTATUS push_xattr_blob_tdb(struct pvfs_state *pvfs,
|
---|
198 | const char *attr_name,
|
---|
199 | const char *fname,
|
---|
200 | int fd,
|
---|
201 | const DATA_BLOB *blob)
|
---|
202 | {
|
---|
203 | return push_xattr_blob_tdb_raw(pvfs->ea_db,pvfs,attr_name,fname,fd,blob);
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | /*
|
---|
208 | delete a xattr
|
---|
209 | */
|
---|
210 | NTSTATUS delete_xattr_tdb(struct pvfs_state *pvfs, const char *attr_name,
|
---|
211 | const char *fname, int fd)
|
---|
212 | {
|
---|
213 | TDB_DATA tkey;
|
---|
214 | NTSTATUS status;
|
---|
215 |
|
---|
216 | status = get_ea_tdb_key(NULL, attr_name, fname, fd, &tkey);
|
---|
217 | if (!NT_STATUS_IS_OK(status)) {
|
---|
218 | return status;
|
---|
219 | }
|
---|
220 |
|
---|
221 | if (tdb_delete(pvfs->ea_db->tdb, tkey) == -1) {
|
---|
222 | talloc_free(tkey.dptr);
|
---|
223 | return NT_STATUS_INTERNAL_DB_CORRUPTION;
|
---|
224 | }
|
---|
225 |
|
---|
226 | talloc_free(tkey.dptr);
|
---|
227 | return NT_STATUS_OK;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 |
|
---|
232 | /*
|
---|
233 | delete all xattrs for a file
|
---|
234 | */
|
---|
235 | NTSTATUS unlink_xattr_tdb(struct pvfs_state *pvfs, const char *fname)
|
---|
236 | {
|
---|
237 | TALLOC_CTX *mem_ctx = talloc_new(pvfs);
|
---|
238 | DATA_BLOB blob;
|
---|
239 | const char *s;
|
---|
240 | NTSTATUS status;
|
---|
241 |
|
---|
242 | status = pull_xattr_blob_tdb(pvfs, mem_ctx, XATTR_LIST_ATTR,
|
---|
243 | fname, -1, 100, &blob);
|
---|
244 | if (!NT_STATUS_IS_OK(status)) {
|
---|
245 | talloc_free(mem_ctx);
|
---|
246 | return NT_STATUS_OK;
|
---|
247 | }
|
---|
248 |
|
---|
249 | for (s=(const char *)blob.data; s < (const char *)(blob.data+blob.length); s += strlen(s) + 1) {
|
---|
250 | delete_xattr_tdb(pvfs, s, fname, -1);
|
---|
251 | }
|
---|
252 |
|
---|
253 | status = delete_xattr_tdb(pvfs, XATTR_LIST_ATTR, fname, -1);
|
---|
254 | talloc_free(mem_ctx);
|
---|
255 | return status;
|
---|
256 | }
|
---|