r26638: libndr: Require explicitly specifying iconv_convenience for ndr_struct_push_b...
[ira/wip.git] / source4 / ntvfs / posix / pvfs_xattr.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - xattr support
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 "vfs_posix.h"
24 #include "lib/util/unix_privs.h"
25 #include "librpc/gen_ndr/ndr_xattr.h"
26 #include "param/param.h"
27
28 /*
29   pull a xattr as a blob
30 */
31 static NTSTATUS pull_xattr_blob(struct pvfs_state *pvfs,
32                                 TALLOC_CTX *mem_ctx,
33                                 const char *attr_name, 
34                                 const char *fname, 
35                                 int fd, 
36                                 size_t estimated_size,
37                                 DATA_BLOB *blob)
38 {
39         NTSTATUS status;
40
41         if (pvfs->ea_db) {
42                 return pull_xattr_blob_tdb(pvfs, mem_ctx, attr_name, fname, 
43                                            fd, estimated_size, blob);
44         }
45
46         status = pull_xattr_blob_system(pvfs, mem_ctx, attr_name, fname, 
47                                         fd, estimated_size, blob);
48
49         /* if the filesystem doesn't support them, then tell pvfs not to try again */
50         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)||
51             NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)||
52             NT_STATUS_EQUAL(status, NT_STATUS_INVALID_SYSTEM_SERVICE)) {
53                 DEBUG(5,("pvfs_xattr: xattr not supported in filesystem: %s\n", nt_errstr(status)));
54                 pvfs->flags &= ~PVFS_FLAG_XATTR_ENABLE;
55                 status = NT_STATUS_NOT_FOUND;
56         }
57
58         return status;
59 }
60
61 /*
62   push a xattr as a blob
63 */
64 static NTSTATUS push_xattr_blob(struct pvfs_state *pvfs,
65                                 const char *attr_name, 
66                                 const char *fname, 
67                                 int fd, 
68                                 const DATA_BLOB *blob)
69 {
70         if (pvfs->ea_db) {
71                 return push_xattr_blob_tdb(pvfs, attr_name, fname, fd, blob);
72         }
73         return push_xattr_blob_system(pvfs, attr_name, fname, fd, blob);
74 }
75
76
77 /*
78   delete a xattr
79 */
80 static NTSTATUS delete_xattr(struct pvfs_state *pvfs, const char *attr_name, 
81                              const char *fname, int fd)
82 {
83         if (pvfs->ea_db) {
84                 return delete_xattr_tdb(pvfs, attr_name, fname, fd);
85         }
86         return delete_xattr_system(pvfs, attr_name, fname, fd);
87 }
88
89 /*
90   a hook called on unlink - allows the tdb xattr backend to cleanup
91 */
92 NTSTATUS pvfs_xattr_unlink_hook(struct pvfs_state *pvfs, const char *fname)
93 {
94         if (pvfs->ea_db) {
95                 return unlink_xattr_tdb(pvfs, fname);
96         }
97         return unlink_xattr_system(pvfs, fname);
98 }
99
100
101 /*
102   load a NDR structure from a xattr
103 */
104 _PUBLIC_ NTSTATUS pvfs_xattr_ndr_load(struct pvfs_state *pvfs,
105                              TALLOC_CTX *mem_ctx,
106                              const char *fname, int fd, const char *attr_name,
107                              void *p, void *pull_fn)
108 {
109         NTSTATUS status;
110         DATA_BLOB blob;
111         enum ndr_err_code ndr_err;
112
113         status = pull_xattr_blob(pvfs, mem_ctx, attr_name, fname, 
114                                  fd, XATTR_DOSATTRIB_ESTIMATED_SIZE, &blob);
115         if (!NT_STATUS_IS_OK(status)) {
116                 return status;
117         }
118
119         /* pull the blob */
120         ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, p, (ndr_pull_flags_fn_t)pull_fn);
121         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
122                 return ndr_map_error2ntstatus(ndr_err);
123         }
124
125         data_blob_free(&blob);
126
127         return NT_STATUS_OK;
128 }
129
130 /*
131   save a NDR structure into a xattr
132 */
133 _PUBLIC_ NTSTATUS pvfs_xattr_ndr_save(struct pvfs_state *pvfs,
134                              const char *fname, int fd, const char *attr_name, 
135                              void *p, void *push_fn)
136 {
137         TALLOC_CTX *mem_ctx = talloc_new(NULL);
138         DATA_BLOB blob;
139         NTSTATUS status;
140         enum ndr_err_code ndr_err;
141
142         ndr_err = ndr_push_struct_blob(&blob, mem_ctx, lp_iconv_convenience(global_loadparm), p, (ndr_push_flags_fn_t)push_fn);
143         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
144                 talloc_free(mem_ctx);
145                 return ndr_map_error2ntstatus(ndr_err);
146         }
147
148         status = push_xattr_blob(pvfs, attr_name, fname, fd, &blob);
149         talloc_free(mem_ctx);
150
151         return status;
152 }
153
154
155 /*
156   fill in file attributes from extended attributes
157 */
158 NTSTATUS pvfs_dosattrib_load(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
159 {
160         NTSTATUS status;
161         struct xattr_DosAttrib attrib;
162         TALLOC_CTX *mem_ctx = talloc_new(name);
163         struct xattr_DosInfo1 *info1;
164         struct xattr_DosInfo2 *info2;
165
166         if (name->stream_name != NULL) {
167                 name->stream_exists = false;
168         } else {
169                 name->stream_exists = true;
170         }
171
172         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
173                 return NT_STATUS_OK;
174         }
175
176         status = pvfs_xattr_ndr_load(pvfs, mem_ctx, name->full_name, 
177                                      fd, XATTR_DOSATTRIB_NAME,
178                                      &attrib, 
179                                      (ndr_pull_flags_fn_t)ndr_pull_xattr_DosAttrib);
180
181         /* not having a DosAttrib is not an error */
182         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
183                 talloc_free(mem_ctx);
184                 return pvfs_stream_info(pvfs, name, fd);
185         }
186
187         if (!NT_STATUS_IS_OK(status)) {
188                 talloc_free(mem_ctx);
189                 return status;
190         }
191
192         switch (attrib.version) {
193         case 1:
194                 info1 = &attrib.info.info1;
195                 name->dos.attrib = pvfs_attrib_normalise(info1->attrib, 
196                                                          name->st.st_mode);
197                 name->dos.ea_size = info1->ea_size;
198                 if (name->st.st_size == info1->size) {
199                         name->dos.alloc_size = 
200                                 pvfs_round_alloc_size(pvfs, info1->alloc_size);
201                 }
202                 if (!null_nttime(info1->create_time)) {
203                         name->dos.create_time = info1->create_time;
204                 }
205                 if (!null_nttime(info1->change_time)) {
206                         name->dos.change_time = info1->change_time;
207                 }
208                 name->dos.flags = 0;
209                 break;
210
211         case 2:
212                 info2 = &attrib.info.info2;
213                 name->dos.attrib = pvfs_attrib_normalise(info2->attrib, 
214                                                          name->st.st_mode);
215                 name->dos.ea_size = info2->ea_size;
216                 if (name->st.st_size == info2->size) {
217                         name->dos.alloc_size = 
218                                 pvfs_round_alloc_size(pvfs, info2->alloc_size);
219                 }
220                 if (!null_nttime(info2->create_time)) {
221                         name->dos.create_time = info2->create_time;
222                 }
223                 if (!null_nttime(info2->change_time)) {
224                         name->dos.change_time = info2->change_time;
225                 }
226                 name->dos.flags = info2->flags;
227                 if (name->dos.flags & XATTR_ATTRIB_FLAG_STICKY_WRITE_TIME) {
228                         name->dos.write_time = info2->write_time;
229                 }
230                 break;
231
232         default:
233                 DEBUG(0,("ERROR: Unsupported xattr DosAttrib version %d on '%s'\n",
234                          attrib.version, name->full_name));
235                 talloc_free(mem_ctx);
236                 return NT_STATUS_INVALID_LEVEL;
237         }
238         talloc_free(mem_ctx);
239         
240         status = pvfs_stream_info(pvfs, name, fd);
241
242         return status;
243 }
244
245
246 /*
247   save the file attribute into the xattr
248 */
249 NTSTATUS pvfs_dosattrib_save(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
250 {
251         struct xattr_DosAttrib attrib;
252         struct xattr_DosInfo2 *info2;
253
254         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
255                 return NT_STATUS_OK;
256         }
257
258         attrib.version = 2;
259         info2 = &attrib.info.info2;
260
261         name->dos.attrib = pvfs_attrib_normalise(name->dos.attrib, name->st.st_mode);
262
263         info2->attrib      = name->dos.attrib;
264         info2->ea_size     = name->dos.ea_size;
265         info2->size        = name->st.st_size;
266         info2->alloc_size  = name->dos.alloc_size;
267         info2->create_time = name->dos.create_time;
268         info2->change_time = name->dos.change_time;
269         info2->write_time  = name->dos.write_time;
270         info2->flags       = name->dos.flags;
271         info2->name        = "";
272
273         return pvfs_xattr_ndr_save(pvfs, name->full_name, fd, 
274                                    XATTR_DOSATTRIB_NAME, &attrib, 
275                                    (ndr_push_flags_fn_t)ndr_push_xattr_DosAttrib);
276 }
277
278
279 /*
280   load the set of DOS EAs
281 */
282 NTSTATUS pvfs_doseas_load(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
283                           struct xattr_DosEAs *eas)
284 {
285         NTSTATUS status;
286         ZERO_STRUCTP(eas);
287         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
288                 return NT_STATUS_OK;
289         }
290         status = pvfs_xattr_ndr_load(pvfs, eas, name->full_name, fd, XATTR_DOSEAS_NAME,
291                                      eas, (ndr_pull_flags_fn_t)ndr_pull_xattr_DosEAs);
292         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
293                 return NT_STATUS_OK;
294         }
295         return status;
296 }
297
298 /*
299   save the set of DOS EAs
300 */
301 NTSTATUS pvfs_doseas_save(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
302                           struct xattr_DosEAs *eas)
303 {
304         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
305                 return NT_STATUS_OK;
306         }
307         return pvfs_xattr_ndr_save(pvfs, name->full_name, fd, XATTR_DOSEAS_NAME, eas, 
308                                    (ndr_push_flags_fn_t)ndr_push_xattr_DosEAs);
309 }
310
311
312 /*
313   load the set of streams from extended attributes
314 */
315 NTSTATUS pvfs_streams_load(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
316                            struct xattr_DosStreams *streams)
317 {
318         NTSTATUS status;
319         ZERO_STRUCTP(streams);
320         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
321                 return NT_STATUS_OK;
322         }
323         status = pvfs_xattr_ndr_load(pvfs, streams, name->full_name, fd, 
324                                      XATTR_DOSSTREAMS_NAME,
325                                      streams, 
326                                      (ndr_pull_flags_fn_t)ndr_pull_xattr_DosStreams);
327         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
328                 return NT_STATUS_OK;
329         }
330         return status;
331 }
332
333 /*
334   save the set of streams into filesystem xattr
335 */
336 NTSTATUS pvfs_streams_save(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
337                            struct xattr_DosStreams *streams)
338 {
339         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
340                 return NT_STATUS_OK;
341         }
342         return pvfs_xattr_ndr_save(pvfs, name->full_name, fd, 
343                                    XATTR_DOSSTREAMS_NAME, 
344                                    streams, 
345                                    (ndr_push_flags_fn_t)ndr_push_xattr_DosStreams);
346 }
347
348
349 /*
350   load the current ACL from extended attributes
351 */
352 NTSTATUS pvfs_acl_load(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
353                        struct xattr_NTACL *acl)
354 {
355         NTSTATUS status;
356         ZERO_STRUCTP(acl);
357         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
358                 return NT_STATUS_NOT_FOUND;
359         }
360         status = pvfs_xattr_ndr_load(pvfs, acl, name->full_name, fd, 
361                                      XATTR_NTACL_NAME,
362                                      acl, 
363                                      (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
364         return status;
365 }
366
367 /*
368   save the acl for a file into filesystem xattr
369 */
370 NTSTATUS pvfs_acl_save(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
371                        struct xattr_NTACL *acl)
372 {
373         NTSTATUS status;
374         void *privs;
375
376         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
377                 return NT_STATUS_OK;
378         }
379
380         /* this xattr is in the "system" namespace, so we need
381            admin privileges to set it */
382         privs = root_privileges();
383         status = pvfs_xattr_ndr_save(pvfs, name->full_name, fd, 
384                                      XATTR_NTACL_NAME, 
385                                      acl, 
386                                      (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
387         talloc_free(privs);
388         return status;
389 }
390
391 /*
392   create a zero length xattr with the given name
393 */
394 NTSTATUS pvfs_xattr_create(struct pvfs_state *pvfs, 
395                            const char *fname, int fd,
396                            const char *attr_prefix,
397                            const char *attr_name)
398 {
399         NTSTATUS status;
400         DATA_BLOB blob = data_blob(NULL, 0);
401         char *aname = talloc_asprintf(NULL, "%s%s", attr_prefix, attr_name);
402         if (aname == NULL) {
403                 return NT_STATUS_NO_MEMORY;
404         }
405         status = push_xattr_blob(pvfs, aname, fname, fd, &blob);
406         talloc_free(aname);
407         return status;
408 }
409
410
411 /*
412   delete a xattr with the given name
413 */
414 NTSTATUS pvfs_xattr_delete(struct pvfs_state *pvfs, 
415                            const char *fname, int fd,
416                            const char *attr_prefix,
417                            const char *attr_name)
418 {
419         NTSTATUS status;
420         char *aname = talloc_asprintf(NULL, "%s%s", attr_prefix, attr_name);
421         if (aname == NULL) {
422                 return NT_STATUS_NO_MEMORY;
423         }
424         status = delete_xattr(pvfs, aname, fname, fd);
425         talloc_free(aname);
426         return status;
427 }
428
429 /*
430   load a xattr with the given name
431 */
432 NTSTATUS pvfs_xattr_load(struct pvfs_state *pvfs, 
433                          TALLOC_CTX *mem_ctx,
434                          const char *fname, int fd,
435                          const char *attr_prefix,
436                          const char *attr_name,
437                          size_t estimated_size,
438                          DATA_BLOB *blob)
439 {
440         NTSTATUS status;
441         char *aname = talloc_asprintf(mem_ctx, "%s%s", attr_prefix, attr_name);
442         if (aname == NULL) {
443                 return NT_STATUS_NO_MEMORY;
444         }
445         status = pull_xattr_blob(pvfs, mem_ctx, aname, fname, fd, estimated_size, blob);
446         talloc_free(aname);
447         return status;
448 }
449
450 /*
451   save a xattr with the given name
452 */
453 NTSTATUS pvfs_xattr_save(struct pvfs_state *pvfs, 
454                          const char *fname, int fd,
455                          const char *attr_prefix,
456                          const char *attr_name,
457                          const DATA_BLOB *blob)
458 {
459         NTSTATUS status;
460         char *aname = talloc_asprintf(NULL, "%s%s", attr_prefix, attr_name);
461         if (aname == NULL) {
462                 return NT_STATUS_NO_MEMORY;
463         }
464         status = push_xattr_blob(pvfs, aname, fname, fd, blob);
465         talloc_free(aname);
466         return status;
467 }
468
469
470 /*
471   probe for system support for xattrs
472 */
473 void pvfs_xattr_probe(struct pvfs_state *pvfs)
474 {
475         TALLOC_CTX *tmp_ctx = talloc_new(pvfs);
476         DATA_BLOB blob;
477         pull_xattr_blob(pvfs, tmp_ctx, "user.XattrProbe", pvfs->base_directory, 
478                         -1, 1, &blob);
479         pull_xattr_blob(pvfs, tmp_ctx, "security.XattrProbe", pvfs->base_directory, 
480                         -1, 1, &blob);
481         talloc_free(tmp_ctx);
482 }