84c6a6fd8467ff0167e306e59f7cdc45ad2ad976
[samba.git] / source3 / modules / vfs_cap.c
1 /* 
2  * CAP VFS module for Samba 3.x Version 0.3
3  *
4  * Copyright (C) Tim Potter, 1999-2000
5  * Copyright (C) Alexander Bokovoy, 2002-2003
6  * Copyright (C) Stefan (metze) Metzmacher, 2003
7  * Copyright (C) TAKAHASHI Motonobu (monyo), 2003
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *  
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *  
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "includes.h"
25
26 /* cap functions */
27 static char *capencode(char *to, const char *from);
28 static char *capdecode(char *to, const char *from);
29
30 static SMB_BIG_UINT cap_disk_free(vfs_handle_struct *handle, const char *path,
31         BOOL small_query, SMB_BIG_UINT *bsize,
32         SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
33 {
34         pstring cappath;
35         capencode(cappath, path);
36         return SMB_VFS_NEXT_DISK_FREE(handle, cappath, small_query, bsize,
37                                          dfree, dsize);
38 }
39
40 static SMB_STRUCT_DIR *cap_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
41 {
42         pstring capname;
43         capencode(capname, fname);
44         return SMB_VFS_NEXT_OPENDIR(handle, capname, mask, attr);
45 }
46
47 static SMB_STRUCT_DIRENT *cap_readdir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
48 {
49         SMB_STRUCT_DIRENT *result;
50         DEBUG(3,("cap: cap_readdir\n"));
51         result = SMB_VFS_NEXT_READDIR(handle, dirp);
52         if (result) {
53           DEBUG(3,("cap: cap_readdir: %s\n", result->d_name));
54           capdecode(result->d_name, result->d_name);
55         }
56         return result;
57 }
58
59 static int cap_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
60 {
61         pstring cappath;
62         capencode(cappath, path);
63         return SMB_VFS_NEXT_MKDIR(handle, cappath, mode);
64 }
65
66 static int cap_rmdir(vfs_handle_struct *handle, const char *path)
67 {
68         pstring cappath;
69         capencode(cappath, path);
70         return SMB_VFS_NEXT_RMDIR(handle, cappath);
71 }
72
73 static int cap_open(vfs_handle_struct *handle, const char *fname, files_struct *fsp, int flags, mode_t mode)
74 {
75         pstring capname;
76         DEBUG(3,("cap: cap_open for %s\n", fname));
77         capencode(capname, fname);
78         return SMB_VFS_NEXT_OPEN(handle, capname, fsp, flags, mode);
79 }
80
81 static int cap_rename(vfs_handle_struct *handle, const char *oldname, const char *newname)
82 {
83         pstring capold, capnew;
84         capencode(capold, oldname);
85         capencode(capnew, newname);
86
87         return SMB_VFS_NEXT_RENAME(handle, capold, capnew);
88 }
89
90 static int cap_stat(vfs_handle_struct *handle, const char *fname, SMB_STRUCT_STAT *sbuf)
91 {
92         pstring capname;
93         capencode(capname, fname);
94         return SMB_VFS_NEXT_STAT(handle, capname, sbuf);
95 }
96
97 static int cap_lstat(vfs_handle_struct *handle, const char *path, SMB_STRUCT_STAT *sbuf)
98 {
99         pstring cappath;
100         capencode(cappath, path);
101         return SMB_VFS_NEXT_LSTAT(handle, cappath, sbuf);
102 }
103
104 static int cap_unlink(vfs_handle_struct *handle, const char *path)
105 {
106         pstring cappath;
107         capencode(cappath, path);
108         return SMB_VFS_NEXT_UNLINK(handle, cappath);
109 }
110
111 static int cap_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
112 {
113         pstring cappath;
114         capencode(cappath, path);
115         return SMB_VFS_NEXT_CHMOD(handle, cappath, mode);
116 }
117
118 static int cap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
119 {
120         pstring cappath;
121         capencode(cappath, path);
122         return SMB_VFS_NEXT_CHOWN(handle, cappath, uid, gid);
123 }
124
125 static int cap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
126 {
127         pstring cappath;
128         capencode(cappath, path);
129         return SMB_VFS_NEXT_LCHOWN(handle, cappath, uid, gid);
130 }
131
132 static int cap_chdir(vfs_handle_struct *handle, const char *path)
133 {
134         pstring cappath;
135         DEBUG(3,("cap: cap_chdir for %s\n", path));
136         capencode(cappath, path);
137         return SMB_VFS_NEXT_CHDIR(handle, cappath);
138 }
139
140 static int cap_ntimes(vfs_handle_struct *handle, const char *path, const struct timespec ts[2])
141 {
142         pstring cappath;
143         capencode(cappath, path);
144         return SMB_VFS_NEXT_NTIMES(handle, cappath, ts);
145 }
146
147
148 static BOOL cap_symlink(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
149 {
150         pstring capoldpath, capnewpath;
151         capencode(capoldpath, oldpath);
152         capencode(capnewpath, newpath);
153         return SMB_VFS_NEXT_SYMLINK(handle, capoldpath, capnewpath);
154 }
155
156 static BOOL cap_readlink(vfs_handle_struct *handle, const char *path, char *buf, size_t bufsiz)
157 {
158         pstring cappath;
159         capencode(cappath, path);
160         return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
161 }
162
163 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
164 {
165         pstring capoldpath, capnewpath;
166         capencode(capoldpath, oldpath);
167         capencode(capnewpath, newpath);
168         return SMB_VFS_NEXT_LINK(handle, capoldpath, capnewpath);
169 }
170
171 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
172 {
173         pstring cappath;
174         capencode(cappath, path);
175         return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
176 }
177
178 static char *cap_realpath(vfs_handle_struct *handle, const char *path, char *resolved_path)
179 {
180         /* monyo need capencode'ed and capdecode'ed? */
181         pstring cappath;
182         capencode(cappath, path);
183         return SMB_VFS_NEXT_REALPATH(handle, path, resolved_path);
184 }
185
186 static NTSTATUS cap_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp, const char *name, uint32 security_info_sent, struct security_descriptor *psd)
187 {
188         pstring capname;
189         capencode(capname, name);
190         return SMB_VFS_NEXT_SET_NT_ACL(handle, fsp, capname, security_info_sent, psd);
191 }
192
193 static int cap_chmod_acl(vfs_handle_struct *handle, const char *name, mode_t mode)
194 {
195         pstring capname;
196         capencode(capname, name);
197
198         /* If the underlying VFS doesn't have ACL support... */
199         if (!handle->vfs_next.ops.chmod_acl) {
200                 errno = ENOSYS;
201                 return -1;
202         }
203         return SMB_VFS_NEXT_CHMOD_ACL(handle, capname, mode);
204 }
205
206 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle, const char *path_p, SMB_ACL_TYPE_T type)
207 {
208         pstring cappath_p;
209         capencode(cappath_p, path_p);
210         return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cappath_p, type);
211 }
212
213 static int cap_sys_acl_set_file(vfs_handle_struct *handle, const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
214 {
215         pstring capname;
216         capencode(capname, name);
217         return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, capname, acltype, theacl);
218 }
219
220 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle, const char *path)
221 {
222         pstring cappath;
223         capencode(cappath, path);
224         return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cappath);
225 }
226
227 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
228 {
229         pstring cappath, capname;
230         capencode(cappath, path);
231         capencode(capname, name);
232         return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
233 }
234
235 static ssize_t cap_lgetxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t
236 size)
237 {
238         pstring cappath, capname;
239         capencode(cappath, path);
240         capencode(capname, name);
241         return SMB_VFS_NEXT_LGETXATTR(handle, cappath, capname, value, size);
242 }
243
244 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp,int fd, const char *name, void *value, size_t size)
245 {
246         pstring capname;
247         capencode(capname, name);
248         return SMB_VFS_NEXT_FGETXATTR(handle, fsp, fd, capname, value, size);
249 }
250
251 static ssize_t cap_listxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
252 {
253         pstring cappath;
254         capencode(cappath, path);
255         return SMB_VFS_NEXT_LISTXATTR(handle, cappath, list, size);
256 }
257
258 static ssize_t cap_llistxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
259 {
260         pstring cappath;
261         capencode(cappath, path);
262         return SMB_VFS_NEXT_LLISTXATTR(handle, cappath, list, size);
263 }
264
265 static int cap_removexattr(vfs_handle_struct *handle, const char *path, const char *name)
266 {
267         pstring cappath, capname;
268         capencode(cappath, path);
269         capencode(capname, name);
270         return SMB_VFS_NEXT_REMOVEXATTR(handle, cappath, capname);
271 }
272
273 static int cap_lremovexattr(vfs_handle_struct *handle, const char *path, const char *name)
274 {
275         pstring cappath, capname;
276         capencode(cappath, path);
277         capencode(capname, name);
278         return SMB_VFS_NEXT_LREMOVEXATTR(handle, cappath, capname);
279 }
280
281 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp,int fd, const char *name)
282 {
283         pstring capname;
284         capencode(capname, name);
285         return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, fd, capname);
286 }
287
288 static int cap_setxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
289 {
290         pstring cappath, capname;
291         capencode(cappath, path);
292         capencode(capname, name);
293         return SMB_VFS_NEXT_SETXATTR(handle, cappath, capname, value, size, flags);
294 }
295
296 static int cap_lsetxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
297 {
298         pstring cappath, capname;
299         capencode(cappath, path);
300         capencode(capname, name);
301         return SMB_VFS_NEXT_LSETXATTR(handle, cappath, capname, value, size, flags);
302 }
303
304 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp,int fd, const char *name, const void *value, size_t size, int flags)
305 {
306         pstring capname;
307         capencode(capname, name);
308         return SMB_VFS_NEXT_FSETXATTR(handle, fsp, fd, capname, value, size, flags);
309 }
310
311 /* VFS operations structure */
312
313 static vfs_op_tuple cap_op_tuples[] = {
314
315         /* Disk operations */
316
317         {SMB_VFS_OP(cap_disk_free),                     SMB_VFS_OP_DISK_FREE,           SMB_VFS_LAYER_TRANSPARENT},
318         
319         /* Directory operations */
320
321         {SMB_VFS_OP(cap_opendir),                       SMB_VFS_OP_OPENDIR,             SMB_VFS_LAYER_TRANSPARENT},
322         {SMB_VFS_OP(cap_readdir),                       SMB_VFS_OP_READDIR,             SMB_VFS_LAYER_TRANSPARENT},
323         {SMB_VFS_OP(cap_mkdir),                 SMB_VFS_OP_MKDIR,               SMB_VFS_LAYER_TRANSPARENT},
324         {SMB_VFS_OP(cap_rmdir),                 SMB_VFS_OP_RMDIR,               SMB_VFS_LAYER_TRANSPARENT},
325
326         /* File operations */
327
328         {SMB_VFS_OP(cap_open),                          SMB_VFS_OP_OPEN,                SMB_VFS_LAYER_TRANSPARENT},
329         {SMB_VFS_OP(cap_rename),                        SMB_VFS_OP_RENAME,              SMB_VFS_LAYER_TRANSPARENT},
330         {SMB_VFS_OP(cap_stat),                          SMB_VFS_OP_STAT,                SMB_VFS_LAYER_TRANSPARENT},
331         {SMB_VFS_OP(cap_lstat),                 SMB_VFS_OP_LSTAT,               SMB_VFS_LAYER_TRANSPARENT},
332         {SMB_VFS_OP(cap_unlink),                        SMB_VFS_OP_UNLINK,              SMB_VFS_LAYER_TRANSPARENT},
333         {SMB_VFS_OP(cap_chmod),                 SMB_VFS_OP_CHMOD,               SMB_VFS_LAYER_TRANSPARENT},
334         {SMB_VFS_OP(cap_chown),                 SMB_VFS_OP_CHOWN,               SMB_VFS_LAYER_TRANSPARENT},
335         {SMB_VFS_OP(cap_lchown),                SMB_VFS_OP_LCHOWN,              SMB_VFS_LAYER_TRANSPARENT},
336         {SMB_VFS_OP(cap_chdir),                 SMB_VFS_OP_CHDIR,               SMB_VFS_LAYER_TRANSPARENT},
337         {SMB_VFS_OP(cap_ntimes),                        SMB_VFS_OP_NTIMES,              SMB_VFS_LAYER_TRANSPARENT},
338         {SMB_VFS_OP(cap_symlink),                       SMB_VFS_OP_SYMLINK,             SMB_VFS_LAYER_TRANSPARENT},
339         {SMB_VFS_OP(cap_readlink),                      SMB_VFS_OP_READLINK,            SMB_VFS_LAYER_TRANSPARENT},
340         {SMB_VFS_OP(cap_link),                          SMB_VFS_OP_LINK,                SMB_VFS_LAYER_TRANSPARENT},
341         {SMB_VFS_OP(cap_mknod),                 SMB_VFS_OP_MKNOD,               SMB_VFS_LAYER_TRANSPARENT},
342         {SMB_VFS_OP(cap_realpath),                      SMB_VFS_OP_REALPATH,            SMB_VFS_LAYER_TRANSPARENT},
343
344         /* NT File ACL operations */
345
346         {SMB_VFS_OP(cap_set_nt_acl),                    SMB_VFS_OP_SET_NT_ACL,          SMB_VFS_LAYER_TRANSPARENT},
347
348         /* POSIX ACL operations */
349
350         {SMB_VFS_OP(cap_chmod_acl),                     SMB_VFS_OP_CHMOD_ACL,           SMB_VFS_LAYER_TRANSPARENT},
351
352         {SMB_VFS_OP(cap_sys_acl_get_file),              SMB_VFS_OP_SYS_ACL_GET_FILE,            SMB_VFS_LAYER_TRANSPARENT},
353         {SMB_VFS_OP(cap_sys_acl_set_file),              SMB_VFS_OP_SYS_ACL_SET_FILE,            SMB_VFS_LAYER_TRANSPARENT},
354         {SMB_VFS_OP(cap_sys_acl_delete_def_file),       SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,     SMB_VFS_LAYER_TRANSPARENT},
355         
356         /* EA operations. */
357         {SMB_VFS_OP(cap_getxattr),                      SMB_VFS_OP_GETXATTR,                    SMB_VFS_LAYER_TRANSPARENT},
358         {SMB_VFS_OP(cap_lgetxattr),                     SMB_VFS_OP_LGETXATTR,                   SMB_VFS_LAYER_TRANSPARENT},
359         {SMB_VFS_OP(cap_fgetxattr),                     SMB_VFS_OP_FGETXATTR,                   SMB_VFS_LAYER_TRANSPARENT},
360         {SMB_VFS_OP(cap_listxattr),                     SMB_VFS_OP_LISTXATTR,                   SMB_VFS_LAYER_TRANSPARENT},
361         {SMB_VFS_OP(cap_llistxattr),                    SMB_VFS_OP_LLISTXATTR,                  SMB_VFS_LAYER_TRANSPARENT},
362         {SMB_VFS_OP(cap_removexattr),                   SMB_VFS_OP_REMOVEXATTR,                 SMB_VFS_LAYER_TRANSPARENT},
363         {SMB_VFS_OP(cap_lremovexattr),                  SMB_VFS_OP_LREMOVEXATTR,                SMB_VFS_LAYER_TRANSPARENT},
364         {SMB_VFS_OP(cap_fremovexattr),                  SMB_VFS_OP_FREMOVEXATTR,                SMB_VFS_LAYER_TRANSPARENT},
365         {SMB_VFS_OP(cap_setxattr),                      SMB_VFS_OP_SETXATTR,                    SMB_VFS_LAYER_TRANSPARENT},
366         {SMB_VFS_OP(cap_lsetxattr),                     SMB_VFS_OP_LSETXATTR,                   SMB_VFS_LAYER_TRANSPARENT},
367         {SMB_VFS_OP(cap_fsetxattr),                     SMB_VFS_OP_FSETXATTR,                   SMB_VFS_LAYER_TRANSPARENT},
368
369         {NULL,                                          SMB_VFS_OP_NOOP,                        SMB_VFS_LAYER_NOOP}
370 };
371
372 NTSTATUS vfs_cap_init(void);
373 NTSTATUS vfs_cap_init(void)
374 {
375         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap", cap_op_tuples);
376 }
377
378 /* For CAP functions */
379 #define hex_tag ':'
380 #define hex2bin(c)              hex2bin_table[(unsigned char)(c)]
381 #define bin2hex(c)              bin2hex_table[(unsigned char)(c)]
382 #define is_hex(s)               ((s)[0] == hex_tag)
383
384 static unsigned char hex2bin_table[256] = {
385 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
386 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
387 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
388 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
389 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
390 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
391 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
392 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
393 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
394 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
395 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
396 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
397 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
398 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
399 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
400 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
401 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
402 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* 0xf0 */
403 };
404 static unsigned char bin2hex_table[256] = "0123456789abcdef";
405
406 /*******************************************************************
407   original code -> ":xx"  - CAP format
408 ********************************************************************/
409 static char *capencode(char *to, const char *from)
410 {
411   pstring cvtbuf;
412   char *out;
413
414   if (to == from) {
415     from = pstrcpy ((char *) cvtbuf, from);
416   }
417
418   for (out = to; *from && (out - to < sizeof(pstring)-7);) {
419     /* buffer husoku error */
420     if ((unsigned char)*from >= 0x80) {
421       *out++ = hex_tag;
422       *out++ = bin2hex (((*from)>>4)&0x0f);
423       *out++ = bin2hex ((*from)&0x0f);
424       from++;
425     } 
426     else {
427       *out++ = *from++;
428     }
429   }
430   *out = '\0';
431   return to;
432 }
433
434 /*******************************************************************
435   CAP -> original code
436 ********************************************************************/
437 /* ":xx" -> a byte */
438 static char *capdecode(char *to, const char *from)
439 {
440   pstring cvtbuf;
441   char *out;
442
443   if (to == from) {
444     from = pstrcpy ((char *) cvtbuf, from);
445   }
446   for (out = to; *from && (out - to < sizeof(pstring)-3);) {
447     if (is_hex(from)) {
448       *out++ = (hex2bin (from[1])<<4) | (hex2bin (from[2]));
449       from += 3;
450     } else {
451       *out++ = *from++;
452     }
453   }
454   *out = '\0';
455   return to;
456 }