r23271: merge service control pidl change for CloseServiceHandle() from SAMBA_3_0_26
[Samba.git] / source3 / modules / vfs_cap.c
blobab99031e4d638c73935532c4f1d30b061cc65b5c
1 /*
2 * CAP VFS module for Samba 3.x Version 0.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
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 2 of the License, or
12 * (at your option) any later version.
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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "includes.h"
27 /* cap functions */
28 static char *capencode(char *to, const char *from);
29 static char *capdecode(char *to, const char *from);
31 static SMB_BIG_UINT cap_disk_free(vfs_handle_struct *handle, const char *path,
32 BOOL small_query, SMB_BIG_UINT *bsize,
33 SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
35 pstring cappath;
36 capencode(cappath, path);
37 return SMB_VFS_NEXT_DISK_FREE(handle, cappath, small_query, bsize,
38 dfree, dsize);
41 static SMB_STRUCT_DIR *cap_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
43 pstring capname;
44 capencode(capname, fname);
45 return SMB_VFS_NEXT_OPENDIR(handle, capname, mask, attr);
48 static SMB_STRUCT_DIRENT *cap_readdir(vfs_handle_struct *handle, SMB_STRUCT_DIR *dirp)
50 SMB_STRUCT_DIRENT *result;
51 DEBUG(3,("cap: cap_readdir\n"));
52 result = SMB_VFS_NEXT_READDIR(handle, dirp);
53 if (result) {
54 DEBUG(3,("cap: cap_readdir: %s\n", result->d_name));
55 capdecode(result->d_name, result->d_name);
57 return result;
60 static int cap_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
62 pstring cappath;
63 capencode(cappath, path);
64 return SMB_VFS_NEXT_MKDIR(handle, cappath, mode);
67 static int cap_rmdir(vfs_handle_struct *handle, const char *path)
69 pstring cappath;
70 capencode(cappath, path);
71 return SMB_VFS_NEXT_RMDIR(handle, cappath);
74 static int cap_open(vfs_handle_struct *handle, const char *fname, files_struct *fsp, int flags, mode_t mode)
76 pstring capname;
77 DEBUG(3,("cap: cap_open for %s\n", fname));
78 capencode(capname, fname);
79 return SMB_VFS_NEXT_OPEN(handle, capname, fsp, flags, mode);
82 static int cap_rename(vfs_handle_struct *handle, const char *oldname, const char *newname)
84 pstring capold, capnew;
85 capencode(capold, oldname);
86 capencode(capnew, newname);
88 return SMB_VFS_NEXT_RENAME(handle, capold, capnew);
91 static int cap_stat(vfs_handle_struct *handle, const char *fname, SMB_STRUCT_STAT *sbuf)
93 pstring capname;
94 capencode(capname, fname);
95 return SMB_VFS_NEXT_STAT(handle, capname, sbuf);
98 static int cap_lstat(vfs_handle_struct *handle, const char *path, SMB_STRUCT_STAT *sbuf)
100 pstring cappath;
101 capencode(cappath, path);
102 return SMB_VFS_NEXT_LSTAT(handle, cappath, sbuf);
105 static int cap_unlink(vfs_handle_struct *handle, const char *path)
107 pstring cappath;
108 capencode(cappath, path);
109 return SMB_VFS_NEXT_UNLINK(handle, cappath);
112 static int cap_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
114 pstring cappath;
115 capencode(cappath, path);
116 return SMB_VFS_NEXT_CHMOD(handle, cappath, mode);
119 static int cap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
121 pstring cappath;
122 capencode(cappath, path);
123 return SMB_VFS_NEXT_CHOWN(handle, cappath, uid, gid);
126 static int cap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
128 pstring cappath;
129 capencode(cappath, path);
130 return SMB_VFS_NEXT_LCHOWN(handle, cappath, uid, gid);
133 static int cap_chdir(vfs_handle_struct *handle, const char *path)
135 pstring cappath;
136 DEBUG(3,("cap: cap_chdir for %s\n", path));
137 capencode(cappath, path);
138 return SMB_VFS_NEXT_CHDIR(handle, cappath);
141 static int cap_ntimes(vfs_handle_struct *handle, const char *path, const struct timespec ts[2])
143 pstring cappath;
144 capencode(cappath, path);
145 return SMB_VFS_NEXT_NTIMES(handle, cappath, ts);
149 static BOOL cap_symlink(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
151 pstring capoldpath, capnewpath;
152 capencode(capoldpath, oldpath);
153 capencode(capnewpath, newpath);
154 return SMB_VFS_NEXT_SYMLINK(handle, capoldpath, capnewpath);
157 static BOOL cap_readlink(vfs_handle_struct *handle, const char *path, char *buf, size_t bufsiz)
159 pstring cappath;
160 capencode(cappath, path);
161 return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
164 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
166 pstring capoldpath, capnewpath;
167 capencode(capoldpath, oldpath);
168 capencode(capnewpath, newpath);
169 return SMB_VFS_NEXT_LINK(handle, capoldpath, capnewpath);
172 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
174 pstring cappath;
175 capencode(cappath, path);
176 return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
179 static char *cap_realpath(vfs_handle_struct *handle, const char *path, char *resolved_path)
181 /* monyo need capencode'ed and capdecode'ed? */
182 pstring cappath;
183 capencode(cappath, path);
184 return SMB_VFS_NEXT_REALPATH(handle, path, resolved_path);
187 static BOOL cap_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp, const char *name, uint32 security_info_sent, struct security_descriptor *psd)
189 pstring capname;
190 capencode(capname, name);
191 return SMB_VFS_NEXT_SET_NT_ACL(handle, fsp, capname, security_info_sent, psd);
194 static int cap_chmod_acl(vfs_handle_struct *handle, const char *name, mode_t mode)
196 pstring capname;
197 capencode(capname, name);
199 /* If the underlying VFS doesn't have ACL support... */
200 if (!handle->vfs_next.ops.chmod_acl) {
201 errno = ENOSYS;
202 return -1;
204 return SMB_VFS_NEXT_CHMOD_ACL(handle, capname, mode);
207 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle, const char *path_p, SMB_ACL_TYPE_T type)
209 pstring cappath_p;
210 capencode(cappath_p, path_p);
211 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cappath_p, type);
214 static int cap_sys_acl_set_file(vfs_handle_struct *handle, const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
216 pstring capname;
217 capencode(capname, name);
218 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, capname, acltype, theacl);
221 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle, const char *path)
223 pstring cappath;
224 capencode(cappath, path);
225 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cappath);
228 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
230 pstring cappath, capname;
231 capencode(cappath, path);
232 capencode(capname, name);
233 return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
236 static ssize_t cap_lgetxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t
237 size)
239 pstring cappath, capname;
240 capencode(cappath, path);
241 capencode(capname, name);
242 return SMB_VFS_NEXT_LGETXATTR(handle, cappath, capname, value, size);
245 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp,int fd, const char *name, void *value, size_t size)
247 pstring capname;
248 capencode(capname, name);
249 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, fd, capname, value, size);
252 static ssize_t cap_listxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
254 pstring cappath;
255 capencode(cappath, path);
256 return SMB_VFS_NEXT_LISTXATTR(handle, cappath, list, size);
259 static ssize_t cap_llistxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
261 pstring cappath;
262 capencode(cappath, path);
263 return SMB_VFS_NEXT_LLISTXATTR(handle, cappath, list, size);
266 static int cap_removexattr(vfs_handle_struct *handle, const char *path, const char *name)
268 pstring cappath, capname;
269 capencode(cappath, path);
270 capencode(capname, name);
271 return SMB_VFS_NEXT_REMOVEXATTR(handle, cappath, capname);
274 static int cap_lremovexattr(vfs_handle_struct *handle, const char *path, const char *name)
276 pstring cappath, capname;
277 capencode(cappath, path);
278 capencode(capname, name);
279 return SMB_VFS_NEXT_LREMOVEXATTR(handle, cappath, capname);
282 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp,int fd, const char *name)
284 pstring capname;
285 capencode(capname, name);
286 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, fd, capname);
289 static int cap_setxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
291 pstring cappath, capname;
292 capencode(cappath, path);
293 capencode(capname, name);
294 return SMB_VFS_NEXT_SETXATTR(handle, cappath, capname, value, size, flags);
297 static int cap_lsetxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
299 pstring cappath, capname;
300 capencode(cappath, path);
301 capencode(capname, name);
302 return SMB_VFS_NEXT_LSETXATTR(handle, cappath, capname, value, size, flags);
305 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)
307 pstring capname;
308 capencode(capname, name);
309 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, fd, capname, value, size, flags);
312 /* VFS operations structure */
314 static vfs_op_tuple cap_op_tuples[] = {
316 /* Disk operations */
318 {SMB_VFS_OP(cap_disk_free), SMB_VFS_OP_DISK_FREE, SMB_VFS_LAYER_TRANSPARENT},
320 /* Directory operations */
322 {SMB_VFS_OP(cap_opendir), SMB_VFS_OP_OPENDIR, SMB_VFS_LAYER_TRANSPARENT},
323 {SMB_VFS_OP(cap_readdir), SMB_VFS_OP_READDIR, SMB_VFS_LAYER_TRANSPARENT},
324 {SMB_VFS_OP(cap_mkdir), SMB_VFS_OP_MKDIR, SMB_VFS_LAYER_TRANSPARENT},
325 {SMB_VFS_OP(cap_rmdir), SMB_VFS_OP_RMDIR, SMB_VFS_LAYER_TRANSPARENT},
327 /* File operations */
329 {SMB_VFS_OP(cap_open), SMB_VFS_OP_OPEN, SMB_VFS_LAYER_TRANSPARENT},
330 {SMB_VFS_OP(cap_rename), SMB_VFS_OP_RENAME, SMB_VFS_LAYER_TRANSPARENT},
331 {SMB_VFS_OP(cap_stat), SMB_VFS_OP_STAT, SMB_VFS_LAYER_TRANSPARENT},
332 {SMB_VFS_OP(cap_lstat), SMB_VFS_OP_LSTAT, SMB_VFS_LAYER_TRANSPARENT},
333 {SMB_VFS_OP(cap_unlink), SMB_VFS_OP_UNLINK, SMB_VFS_LAYER_TRANSPARENT},
334 {SMB_VFS_OP(cap_chmod), SMB_VFS_OP_CHMOD, SMB_VFS_LAYER_TRANSPARENT},
335 {SMB_VFS_OP(cap_chown), SMB_VFS_OP_CHOWN, SMB_VFS_LAYER_TRANSPARENT},
336 {SMB_VFS_OP(cap_lchown), SMB_VFS_OP_LCHOWN, SMB_VFS_LAYER_TRANSPARENT},
337 {SMB_VFS_OP(cap_chdir), SMB_VFS_OP_CHDIR, SMB_VFS_LAYER_TRANSPARENT},
338 {SMB_VFS_OP(cap_ntimes), SMB_VFS_OP_NTIMES, SMB_VFS_LAYER_TRANSPARENT},
339 {SMB_VFS_OP(cap_symlink), SMB_VFS_OP_SYMLINK, SMB_VFS_LAYER_TRANSPARENT},
340 {SMB_VFS_OP(cap_readlink), SMB_VFS_OP_READLINK, SMB_VFS_LAYER_TRANSPARENT},
341 {SMB_VFS_OP(cap_link), SMB_VFS_OP_LINK, SMB_VFS_LAYER_TRANSPARENT},
342 {SMB_VFS_OP(cap_mknod), SMB_VFS_OP_MKNOD, SMB_VFS_LAYER_TRANSPARENT},
343 {SMB_VFS_OP(cap_realpath), SMB_VFS_OP_REALPATH, SMB_VFS_LAYER_TRANSPARENT},
345 /* NT File ACL operations */
347 {SMB_VFS_OP(cap_set_nt_acl), SMB_VFS_OP_SET_NT_ACL, SMB_VFS_LAYER_TRANSPARENT},
349 /* POSIX ACL operations */
351 {SMB_VFS_OP(cap_chmod_acl), SMB_VFS_OP_CHMOD_ACL, SMB_VFS_LAYER_TRANSPARENT},
353 {SMB_VFS_OP(cap_sys_acl_get_file), SMB_VFS_OP_SYS_ACL_GET_FILE, SMB_VFS_LAYER_TRANSPARENT},
354 {SMB_VFS_OP(cap_sys_acl_set_file), SMB_VFS_OP_SYS_ACL_SET_FILE, SMB_VFS_LAYER_TRANSPARENT},
355 {SMB_VFS_OP(cap_sys_acl_delete_def_file), SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE, SMB_VFS_LAYER_TRANSPARENT},
357 /* EA operations. */
358 {SMB_VFS_OP(cap_getxattr), SMB_VFS_OP_GETXATTR, SMB_VFS_LAYER_TRANSPARENT},
359 {SMB_VFS_OP(cap_lgetxattr), SMB_VFS_OP_LGETXATTR, SMB_VFS_LAYER_TRANSPARENT},
360 {SMB_VFS_OP(cap_fgetxattr), SMB_VFS_OP_FGETXATTR, SMB_VFS_LAYER_TRANSPARENT},
361 {SMB_VFS_OP(cap_listxattr), SMB_VFS_OP_LISTXATTR, SMB_VFS_LAYER_TRANSPARENT},
362 {SMB_VFS_OP(cap_llistxattr), SMB_VFS_OP_LLISTXATTR, SMB_VFS_LAYER_TRANSPARENT},
363 {SMB_VFS_OP(cap_removexattr), SMB_VFS_OP_REMOVEXATTR, SMB_VFS_LAYER_TRANSPARENT},
364 {SMB_VFS_OP(cap_lremovexattr), SMB_VFS_OP_LREMOVEXATTR, SMB_VFS_LAYER_TRANSPARENT},
365 {SMB_VFS_OP(cap_fremovexattr), SMB_VFS_OP_FREMOVEXATTR, SMB_VFS_LAYER_TRANSPARENT},
366 {SMB_VFS_OP(cap_setxattr), SMB_VFS_OP_SETXATTR, SMB_VFS_LAYER_TRANSPARENT},
367 {SMB_VFS_OP(cap_lsetxattr), SMB_VFS_OP_LSETXATTR, SMB_VFS_LAYER_TRANSPARENT},
368 {SMB_VFS_OP(cap_fsetxattr), SMB_VFS_OP_FSETXATTR, SMB_VFS_LAYER_TRANSPARENT},
370 {NULL, SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
373 NTSTATUS vfs_cap_init(void);
374 NTSTATUS vfs_cap_init(void)
376 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap", cap_op_tuples);
379 /* For CAP functions */
380 #define hex_tag ':'
381 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
382 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
383 #define is_hex(s) ((s)[0] == hex_tag)
385 static unsigned char hex2bin_table[256] = {
386 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
387 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
388 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
389 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
390 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
391 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
392 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
393 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
394 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
395 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
396 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
397 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
398 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
399 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
400 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
401 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
402 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
403 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
405 static unsigned char bin2hex_table[256] = "0123456789abcdef";
407 /*******************************************************************
408 original code -> ":xx" - CAP format
409 ********************************************************************/
410 static char *capencode(char *to, const char *from)
412 pstring cvtbuf;
413 char *out;
415 if (to == from) {
416 from = pstrcpy ((char *) cvtbuf, from);
419 for (out = to; *from && (out - to < sizeof(pstring)-7);) {
420 /* buffer husoku error */
421 if ((unsigned char)*from >= 0x80) {
422 *out++ = hex_tag;
423 *out++ = bin2hex (((*from)>>4)&0x0f);
424 *out++ = bin2hex ((*from)&0x0f);
425 from++;
427 else {
428 *out++ = *from++;
431 *out = '\0';
432 return to;
435 /*******************************************************************
436 CAP -> original code
437 ********************************************************************/
438 /* ":xx" -> a byte */
439 static char *capdecode(char *to, const char *from)
441 pstring cvtbuf;
442 char *out;
444 if (to == from) {
445 from = pstrcpy ((char *) cvtbuf, from);
447 for (out = to; *from && (out - to < sizeof(pstring)-3);) {
448 if (is_hex(from)) {
449 *out++ = (hex2bin (from[1])<<4) | (hex2bin (from[2]));
450 from += 3;
451 } else {
452 *out++ = *from++;
455 *out = '\0';
456 return to;