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 2 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, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24
25#include "includes.h"
26
27/* cap functions */
28static char *capencode(char *to, const char *from);
29static char *capdecode(char *to, const char *from);
30
31static SMB_BIG_UINT cap_disk_free(vfs_handle_struct *handle, connection_struct *conn, const char *path,
32	BOOL small_query, SMB_BIG_UINT *bsize,
33	SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
34{
35        pstring cappath;
36        capencode(cappath, path);
37	return SMB_VFS_NEXT_DISK_FREE(handle, conn, cappath, small_query, bsize,
38					 dfree, dsize);
39}
40
41static DIR *cap_opendir(vfs_handle_struct *handle, connection_struct *conn, const char *fname)
42{
43        pstring capname;
44        capencode(capname, fname);
45	return SMB_VFS_NEXT_OPENDIR(handle, conn, capname);
46}
47
48static SMB_STRUCT_DIRENT *cap_readdir(vfs_handle_struct *handle, connection_struct *conn, DIR *dirp)
49{
50        SMB_STRUCT_DIRENT *result;
51	DEBUG(3,("cap: cap_readdir\n"));
52	result = SMB_VFS_NEXT_READDIR(handle, conn, dirp);
53	if (result) {
54	  DEBUG(3,("cap: cap_readdir: %s\n", result->d_name));
55	  capdecode(result->d_name, result->d_name);
56        }
57        return result;
58}
59
60static int cap_mkdir(vfs_handle_struct *handle, connection_struct *conn, const char *path, mode_t mode)
61{
62	pstring cappath;
63	capencode(cappath, path);
64	return SMB_VFS_NEXT_MKDIR(handle, conn, cappath, mode);
65}
66
67static int cap_rmdir(vfs_handle_struct *handle, connection_struct *conn, const char *path)
68{
69        pstring cappath;
70        capencode(cappath, path);
71	return SMB_VFS_NEXT_RMDIR(handle, conn, cappath);
72}
73
74static int cap_open(vfs_handle_struct *handle, connection_struct *conn, const char *fname, int flags, mode_t mode)
75{
76        pstring capname;
77	DEBUG(3,("cap: cap_open for %s\n", fname));
78	capencode(capname, fname);
79	return SMB_VFS_NEXT_OPEN(handle, conn, capname, flags, mode);
80}
81
82static int cap_rename(vfs_handle_struct *handle, connection_struct *conn, const char *old, const char *new)
83{
84	pstring capold, capnew;
85	capencode(capold, old);
86	capencode(capnew, new);
87
88	return SMB_VFS_NEXT_RENAME(handle, conn, capold, capnew);
89}
90
91static int cap_stat(vfs_handle_struct *handle, connection_struct *conn, const char *fname, SMB_STRUCT_STAT *sbuf)
92{
93        pstring capname;
94	capencode(capname, fname);
95	return SMB_VFS_NEXT_STAT(handle, conn, capname, sbuf);
96}
97
98static int cap_lstat(vfs_handle_struct *handle, connection_struct *conn, const char *path, SMB_STRUCT_STAT *sbuf)
99{
100	pstring cappath;
101	capencode(cappath, path);
102	return SMB_VFS_NEXT_LSTAT(handle, conn, cappath, sbuf);
103}
104
105static int cap_unlink(vfs_handle_struct *handle, connection_struct *conn, const char *path)
106{
107	pstring cappath;
108	capencode(cappath, path);
109	return SMB_VFS_NEXT_UNLINK(handle, conn, cappath);
110}
111
112static int cap_chmod(vfs_handle_struct *handle, connection_struct *conn, const char *path, mode_t mode)
113{
114        pstring cappath;
115	capencode(cappath, path);
116	return SMB_VFS_NEXT_CHMOD(handle, conn, cappath, mode);
117}
118
119static int cap_chown(vfs_handle_struct *handle, connection_struct *conn, const char *path, uid_t uid, gid_t gid)
120{
121        pstring cappath;
122	capencode(cappath, path);
123	return SMB_VFS_NEXT_CHOWN(handle, conn, cappath, uid, gid);
124}
125
126static int cap_chdir(vfs_handle_struct *handle, connection_struct *conn, const char *path)
127{
128        pstring cappath;
129	DEBUG(3,("cap: cap_chdir for %s\n", path));
130	capencode(cappath, path);
131	return SMB_VFS_NEXT_CHDIR(handle, conn, cappath);
132}
133
134static int cap_utime(vfs_handle_struct *handle, connection_struct *conn, const char *path, struct utimbuf *times)
135{
136        pstring cappath;
137	capencode(cappath, path);
138	return SMB_VFS_NEXT_UTIME(handle, conn, cappath, times);
139}
140
141
142static BOOL cap_symlink(vfs_handle_struct *handle, connection_struct *conn, const char *oldpath, const char *newpath)
143{
144        pstring capoldpath, capnewpath;
145        capencode(capoldpath, oldpath);
146        capencode(capnewpath, newpath);
147	return SMB_VFS_NEXT_SYMLINK(handle, conn, capoldpath, capnewpath);
148}
149
150static BOOL cap_readlink(vfs_handle_struct *handle, connection_struct *conn, const char *path, char *buf, size_t bufsiz)
151{
152        pstring cappath;
153	capencode(cappath, path);
154	return SMB_VFS_NEXT_READLINK(handle, conn, cappath, buf, bufsiz);
155}
156
157static int cap_link(vfs_handle_struct *handle, connection_struct *conn, const char *oldpath, const char *newpath)
158{
159        pstring capoldpath, capnewpath;
160        capencode(capoldpath, oldpath);
161        capencode(capnewpath, newpath);
162	return SMB_VFS_NEXT_LINK(handle, conn, capoldpath, capnewpath);
163}
164
165static int cap_mknod(vfs_handle_struct *handle, connection_struct *conn, const char *path, mode_t mode, SMB_DEV_T dev)
166{
167        pstring cappath;
168	capencode(cappath, path);
169	return SMB_VFS_NEXT_MKNOD(handle, conn, cappath, mode, dev);
170}
171
172static char *cap_realpath(vfs_handle_struct *handle, connection_struct *conn, const char *path, char *resolved_path)
173{
174        /* monyo need capencode'ed and capdecode'ed? */
175        pstring cappath;
176	capencode(cappath, path);
177	return SMB_VFS_NEXT_REALPATH(handle, conn, path, resolved_path);
178}
179
180static BOOL cap_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp, const char *name, uint32 security_info_sent, struct security_descriptor_info *psd)
181{
182        pstring capname;
183	capencode(capname, name);
184	return SMB_VFS_NEXT_SET_NT_ACL(handle, fsp, capname, security_info_sent, psd);
185}
186
187static int cap_chmod_acl(vfs_handle_struct *handle, connection_struct *conn, const char *name, mode_t mode)
188{
189        pstring capname;
190	capencode(capname, name);
191
192	/* If the underlying VFS doesn't have ACL support... */
193	if (!handle->vfs_next.ops.chmod_acl) {
194		errno = ENOSYS;
195		return -1;
196	}
197	return SMB_VFS_NEXT_CHMOD_ACL(handle, conn, capname, mode);
198}
199
200static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle, connection_struct *conn, const char *path_p, SMB_ACL_TYPE_T type)
201{
202        pstring cappath_p;
203	capencode(cappath_p, path_p);
204	return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, conn, cappath_p, type);
205}
206
207static int cap_sys_acl_set_file(vfs_handle_struct *handle, connection_struct *conn, const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
208{
209        pstring capname;
210	capencode(capname, name);
211	return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, conn, capname, acltype, theacl);
212}
213
214static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle, connection_struct *conn, const char *path)
215{
216        pstring cappath;
217	capencode(cappath, path);
218	return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, conn, cappath);
219}
220
221static ssize_t cap_getxattr(vfs_handle_struct *handle, struct connection_struct *conn,const char *path, const char *name, void *value, size_t size)
222{
223        pstring cappath, capname;
224	capencode(cappath, path);
225	capencode(capname, name);
226        return SMB_VFS_NEXT_GETXATTR(handle, conn, cappath, capname, value, size);
227}
228
229static ssize_t cap_lgetxattr(vfs_handle_struct *handle, struct connection_struct *conn,const char *path, const char *name, void *value, size_t
230size)
231{
232        pstring cappath, capname;
233	capencode(cappath, path);
234	capencode(capname, name);
235        return SMB_VFS_NEXT_LGETXATTR(handle, conn, cappath, capname, value, size);
236}
237
238static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp,int fd, const char *name, void *value, size_t size)
239{
240        pstring capname;
241	capencode(capname, name);
242        return SMB_VFS_NEXT_FGETXATTR(handle, fsp, fd, capname, value, size);
243}
244
245static ssize_t cap_listxattr(vfs_handle_struct *handle, struct connection_struct *conn,const char *path, char *list, size_t size)
246{
247        pstring cappath;
248	capencode(cappath, path);
249        return SMB_VFS_NEXT_LISTXATTR(handle, conn, cappath, list, size);
250}
251
252static ssize_t cap_llistxattr(vfs_handle_struct *handle, struct connection_struct *conn,const char *path, char *list, size_t size)
253{
254        pstring cappath;
255	capencode(cappath, path);
256        return SMB_VFS_NEXT_LLISTXATTR(handle, conn, cappath, list, size);
257}
258
259static int cap_removexattr(vfs_handle_struct *handle, struct connection_struct *conn,const char *path, const char *name)
260{
261        pstring cappath, capname;
262	capencode(cappath, path);
263	capencode(capname, name);
264        return SMB_VFS_NEXT_REMOVEXATTR(handle, conn, cappath, capname);
265}
266
267static int cap_lremovexattr(vfs_handle_struct *handle, struct connection_struct *conn,const char *path, const char *name)
268{
269        pstring cappath, capname;
270	capencode(cappath, path);
271	capencode(capname, name);
272        return SMB_VFS_NEXT_LREMOVEXATTR(handle, conn, cappath, capname);
273}
274
275static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp,int fd, const char *name)
276{
277        pstring capname;
278	capencode(capname, name);
279        return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, fd, capname);
280}
281
282static int cap_setxattr(vfs_handle_struct *handle, struct connection_struct *conn,const char *path, const char *name, const void *value, size_t size, int flags)
283{
284        pstring cappath, capname;
285	capencode(cappath, path);
286	capencode(capname, name);
287        return SMB_VFS_NEXT_SETXATTR(handle, conn, cappath, capname, value, size, flags);
288}
289
290static int cap_lsetxattr(vfs_handle_struct *handle, struct connection_struct *conn,const char *path, const char *name, const void *value, size_t size, int flags)
291{
292        pstring cappath, capname;
293	capencode(cappath, path);
294	capencode(capname, name);
295        return SMB_VFS_NEXT_LSETXATTR(handle, conn, cappath, capname, value, size, flags);
296}
297
298static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp,int fd, const char *name, const void *value, size_t size, int flags)
299{
300        pstring capname;
301	capencode(capname, name);
302        return SMB_VFS_NEXT_FSETXATTR(handle, fsp, fd, capname, value, size, flags);
303}
304
305/* VFS operations structure */
306
307static vfs_op_tuple cap_op_tuples[] = {
308
309	/* Disk operations */
310
311	{SMB_VFS_OP(cap_disk_free),			SMB_VFS_OP_DISK_FREE,		SMB_VFS_LAYER_TRANSPARENT},
312
313	/* Directory operations */
314
315	{SMB_VFS_OP(cap_opendir),			SMB_VFS_OP_OPENDIR,		SMB_VFS_LAYER_TRANSPARENT},
316	{SMB_VFS_OP(cap_readdir),			SMB_VFS_OP_READDIR,		SMB_VFS_LAYER_TRANSPARENT},
317	{SMB_VFS_OP(cap_mkdir),			SMB_VFS_OP_MKDIR,		SMB_VFS_LAYER_TRANSPARENT},
318	{SMB_VFS_OP(cap_rmdir),			SMB_VFS_OP_RMDIR,		SMB_VFS_LAYER_TRANSPARENT},
319
320	/* File operations */
321
322	{SMB_VFS_OP(cap_open),				SMB_VFS_OP_OPEN,		SMB_VFS_LAYER_TRANSPARENT},
323	{SMB_VFS_OP(cap_rename),			SMB_VFS_OP_RENAME,		SMB_VFS_LAYER_TRANSPARENT},
324	{SMB_VFS_OP(cap_stat),				SMB_VFS_OP_STAT,		SMB_VFS_LAYER_TRANSPARENT},
325	{SMB_VFS_OP(cap_lstat),			SMB_VFS_OP_LSTAT,		SMB_VFS_LAYER_TRANSPARENT},
326	{SMB_VFS_OP(cap_unlink),			SMB_VFS_OP_UNLINK,		SMB_VFS_LAYER_TRANSPARENT},
327	{SMB_VFS_OP(cap_chmod),			SMB_VFS_OP_CHMOD,		SMB_VFS_LAYER_TRANSPARENT},
328	{SMB_VFS_OP(cap_chown),			SMB_VFS_OP_CHOWN,		SMB_VFS_LAYER_TRANSPARENT},
329	{SMB_VFS_OP(cap_chdir),			SMB_VFS_OP_CHDIR,		SMB_VFS_LAYER_TRANSPARENT},
330	{SMB_VFS_OP(cap_utime),			SMB_VFS_OP_UTIME,		SMB_VFS_LAYER_TRANSPARENT},
331	{SMB_VFS_OP(cap_symlink),			SMB_VFS_OP_SYMLINK,		SMB_VFS_LAYER_TRANSPARENT},
332	{SMB_VFS_OP(cap_readlink),			SMB_VFS_OP_READLINK,		SMB_VFS_LAYER_TRANSPARENT},
333	{SMB_VFS_OP(cap_link),				SMB_VFS_OP_LINK,		SMB_VFS_LAYER_TRANSPARENT},
334	{SMB_VFS_OP(cap_mknod),			SMB_VFS_OP_MKNOD,		SMB_VFS_LAYER_TRANSPARENT},
335	{SMB_VFS_OP(cap_realpath),			SMB_VFS_OP_REALPATH,		SMB_VFS_LAYER_TRANSPARENT},
336
337	/* NT File ACL operations */
338
339	{SMB_VFS_OP(cap_set_nt_acl),			SMB_VFS_OP_SET_NT_ACL,		SMB_VFS_LAYER_TRANSPARENT},
340
341	/* POSIX ACL operations */
342
343	{SMB_VFS_OP(cap_chmod_acl),			SMB_VFS_OP_CHMOD_ACL,		SMB_VFS_LAYER_TRANSPARENT},
344
345	{SMB_VFS_OP(cap_sys_acl_get_file),		SMB_VFS_OP_SYS_ACL_GET_FILE,		SMB_VFS_LAYER_TRANSPARENT},
346	{SMB_VFS_OP(cap_sys_acl_set_file),		SMB_VFS_OP_SYS_ACL_SET_FILE,		SMB_VFS_LAYER_TRANSPARENT},
347	{SMB_VFS_OP(cap_sys_acl_delete_def_file),	SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,	SMB_VFS_LAYER_TRANSPARENT},
348
349	/* EA operations. */
350	{SMB_VFS_OP(cap_getxattr),			SMB_VFS_OP_GETXATTR,			SMB_VFS_LAYER_TRANSPARENT},
351	{SMB_VFS_OP(cap_lgetxattr),			SMB_VFS_OP_LGETXATTR,			SMB_VFS_LAYER_TRANSPARENT},
352	{SMB_VFS_OP(cap_fgetxattr),			SMB_VFS_OP_FGETXATTR,			SMB_VFS_LAYER_TRANSPARENT},
353	{SMB_VFS_OP(cap_listxattr),			SMB_VFS_OP_LISTXATTR,			SMB_VFS_LAYER_TRANSPARENT},
354	{SMB_VFS_OP(cap_llistxattr),			SMB_VFS_OP_LLISTXATTR,			SMB_VFS_LAYER_TRANSPARENT},
355	{SMB_VFS_OP(cap_removexattr),			SMB_VFS_OP_REMOVEXATTR,			SMB_VFS_LAYER_TRANSPARENT},
356	{SMB_VFS_OP(cap_lremovexattr),			SMB_VFS_OP_LREMOVEXATTR,		SMB_VFS_LAYER_TRANSPARENT},
357	{SMB_VFS_OP(cap_fremovexattr),			SMB_VFS_OP_FREMOVEXATTR,		SMB_VFS_LAYER_TRANSPARENT},
358	{SMB_VFS_OP(cap_setxattr),			SMB_VFS_OP_SETXATTR,			SMB_VFS_LAYER_TRANSPARENT},
359	{SMB_VFS_OP(cap_lsetxattr),			SMB_VFS_OP_LSETXATTR,			SMB_VFS_LAYER_TRANSPARENT},
360	{SMB_VFS_OP(cap_fsetxattr),			SMB_VFS_OP_FSETXATTR,			SMB_VFS_LAYER_TRANSPARENT},
361
362	{NULL,						SMB_VFS_OP_NOOP,			SMB_VFS_LAYER_NOOP}
363};
364
365NTSTATUS vfs_cap_init(void)
366{
367	return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap", cap_op_tuples);
368}
369
370/* For CAP functions */
371#define hex_tag ':'
372#define hex2bin(c)		hex2bin_table[(unsigned char)(c)]
373#define bin2hex(c)		bin2hex_table[(unsigned char)(c)]
374#define is_hex(s)		((s)[0] == hex_tag)
375
376static unsigned char hex2bin_table[256] = {
3770, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
3780, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
3790, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
3800, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
3810000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
3820000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
3830, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
3840000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
3850000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
3860, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
3870, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
3880, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
3890, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
3900, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
3910, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
3920, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
3930, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
3940, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* 0xf0 */
395};
396static unsigned char bin2hex_table[256] = "0123456789abcdef";
397
398/*******************************************************************
399  original code -> ":xx"  - CAP format
400********************************************************************/
401static char *capencode(char *to, const char *from)
402{
403  pstring cvtbuf;
404  char *out;
405
406  if (to == from) {
407    from = pstrcpy ((char *) cvtbuf, from);
408  }
409
410  for (out = to; *from && (out - to < sizeof(pstring)-7);) {
411    /* buffer husoku error */
412    if ((unsigned char)*from >= 0x80) {
413      *out++ = hex_tag;
414      *out++ = bin2hex (((*from)>>4)&0x0f);
415      *out++ = bin2hex ((*from)&0x0f);
416      from++;
417    }
418    else {
419      *out++ = *from++;
420    }
421  }
422  *out = '\0';
423  return to;
424}
425
426/*******************************************************************
427  CAP -> original code
428********************************************************************/
429/* ":xx" -> a byte */
430static char *capdecode(char *to, const char *from)
431{
432  pstring cvtbuf;
433  char *out;
434
435  if (to == from) {
436    from = pstrcpy ((char *) cvtbuf, from);
437  }
438  for (out = to; *from && (out - to < sizeof(pstring)-3);) {
439    if (is_hex(from)) {
440      *out++ = (hex2bin (from[1])<<4) | (hex2bin (from[2]));
441      from += 3;
442    } else {
443      *out++ = *from++;
444    }
445  }
446  *out = '\0';
447  return to;
448}
449