adding files from 3.0
[samba.git] / examples / VFS / skel.c
1 /* 
2  * Skeleton VFS module.  Implements passthrough operation of all VFS
3  * calls to disk functions.
4  *
5  * Copyright (C) Tim Potter, 1999-2000
6  * Copyright (C) Alexander Bokovoy, 2002
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 2 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, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <sys/stat.h>
27 #ifdef HAVE_UTIME_H
28 #include <utime.h>
29 #endif
30 #ifdef HAVE_DIRENT_H
31 #include <dirent.h>
32 #endif
33 #ifdef HAVE_FCNTL_H
34 #include <fcntl.h>
35 #endif
36 #include <errno.h>
37 #include <string.h>
38
39 #include <includes.h>
40 #include <vfs.h>
41
42 static struct vfs_ops default_vfs_ops;   /* For passthrough operation */
43 static struct smb_vfs_handle_struct *skel_handle; /* use skel_handle->data for storing per-instance private data */
44
45 static int skel_connect(struct connection_struct *conn, const char *service, const char *user)    
46 {
47         return default_vfs_ops.connect(conn, service, user);
48 }
49
50 static void skel_disconnect(struct connection_struct *conn)
51 {
52         default_vfs_ops.disconnect(conn);
53 }
54
55 static SMB_BIG_UINT skel_disk_free(struct connection_struct *conn, const char *path,
56         BOOL small_query, SMB_BIG_UINT *bsize,
57         SMB_BIG_UINT *dfree, SMB_BIG_UINT *dsize)
58 {
59         return default_vfs_ops.disk_free(conn, path, small_query, bsize, 
60                                          dfree, dsize);
61 }
62
63 static DIR *skel_opendir(struct connection_struct *conn, const char *fname)
64 {
65         return default_vfs_ops.opendir(conn, fname);
66 }
67
68 static struct dirent *skel_readdir(struct connection_struct *conn, DIR *dirp)
69 {
70         return default_vfs_ops.readdir(conn, dirp);
71 }
72
73 static int skel_mkdir(struct connection_struct *conn, const char *path, mode_t mode)
74 {
75         return default_vfs_ops.mkdir(conn, path, mode);
76 }
77
78 static int skel_rmdir(struct connection_struct *conn, const char *path)
79 {
80         return default_vfs_ops.rmdir(conn, path);
81 }
82
83 static int skel_closedir(struct connection_struct *conn, DIR *dir)
84 {
85         return default_vfs_ops.closedir(conn, dir);
86 }
87
88 static int skel_open(struct connection_struct *conn, const char *fname, int flags, mode_t mode)
89 {
90         return default_vfs_ops.open(conn, fname, flags, mode);
91 }
92
93 static int skel_close(struct files_struct *fsp, int fd)
94 {
95         return default_vfs_ops.close(fsp, fd);
96 }
97
98 static ssize_t skel_read(struct files_struct *fsp, int fd, void *data, size_t n)
99 {
100         return default_vfs_ops.read(fsp, fd, data, n);
101 }
102
103 static ssize_t skel_write(struct files_struct *fsp, int fd, const void *data, size_t n)
104 {
105         return default_vfs_ops.write(fsp, fd, data, n);
106 }
107
108 static SMB_OFF_T skel_lseek(struct files_struct *fsp, int filedes, SMB_OFF_T offset, int whence)
109 {
110         return default_vfs_ops.lseek(fsp, filedes, offset, whence);
111 }
112
113 static int skel_rename(struct connection_struct *conn, const char *old, const char *new)
114 {
115         return default_vfs_ops.rename(conn, old, new);
116 }
117
118 static int skel_fsync(struct files_struct *fsp, int fd)
119 {
120         return default_vfs_ops.fsync(fsp, fd);
121 }
122
123 static int skel_stat(struct connection_struct *conn, const char *fname, SMB_STRUCT_STAT *sbuf)
124 {
125         return default_vfs_ops.stat(conn, fname, sbuf);
126 }
127
128 static int skel_fstat(struct files_struct *fsp, int fd, SMB_STRUCT_STAT *sbuf)
129 {
130         return default_vfs_ops.fstat(fsp, fd, sbuf);
131 }
132
133 static int skel_lstat(struct connection_struct *conn, const char *path, SMB_STRUCT_STAT *sbuf)
134 {
135         return default_vfs_ops.lstat(conn, path, sbuf);
136 }
137
138 static int skel_unlink(struct connection_struct *conn, const char *path)
139 {
140         return default_vfs_ops.unlink(conn, path);
141 }
142
143 static int skel_chmod(struct connection_struct *conn, const char *path, mode_t mode)
144 {
145         return default_vfs_ops.chmod(conn, path, mode);
146 }
147
148 static int skel_fchmod(struct files_struct *fsp, int fd, mode_t mode)
149 {
150         return default_vfs_ops.fchmod(fsp, fd, mode);
151 }
152
153 static int skel_chown(struct connection_struct *conn, const char *path, uid_t uid, gid_t gid)
154 {
155         return default_vfs_ops.chown(conn, path, uid, gid);
156 }
157
158 static int skel_fchown(struct files_struct *fsp, int fd, uid_t uid, gid_t gid)
159 {
160         return default_vfs_ops.fchown(fsp, fd, uid, gid);
161 }
162
163 static int skel_chdir(struct connection_struct *conn, const char *path)
164 {
165         return default_vfs_ops.chdir(conn, path);
166 }
167
168 static char *skel_getwd(struct connection_struct *conn, char *buf)
169 {
170         return default_vfs_ops.getwd(conn, buf);
171 }
172
173 static int skel_utime(struct connection_struct *conn, const char *path, struct utimbuf *times)
174 {
175         return default_vfs_ops.utime(conn, path, times);
176 }
177
178 static int skel_ftruncate(struct files_struct *fsp, int fd, SMB_OFF_T offset)
179 {
180         return default_vfs_ops.ftruncate(fsp, fd, offset);
181 }
182
183 static BOOL skel_lock(struct files_struct *fsp, int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
184 {
185         return default_vfs_ops.lock(fsp, fd, op, offset, count, type);
186 }
187
188 static BOOL skel_symlink(struct connection_struct *conn, const char *oldpath, const char *newpath)
189 {
190         return default_vfs_ops.symlink(conn, oldpath, newpath);
191 }
192
193 static BOOL skel_readlink(struct connection_struct *conn, const char *path, char *buf, size_t bufsiz)
194 {
195         return default_vfs_ops.readlink(conn, path, buf, bufsiz);
196 }
197
198 static int skel_link(struct connection_struct *conn, const char *oldpath, const char *newpath)
199 {
200         return default_vfs_ops.link(conn, oldpath, newpath);
201 }
202
203 static int skel_mknod(struct connection_struct *conn, const char *path, mode_t mode, SMB_DEV_T dev)
204 {
205         return default_vfs_ops.mknod(conn, path, mode, dev);
206 }
207
208 static char *skel_realpath(struct connection_struct *conn, const char *path, char *resolved_path)
209 {
210         return default_vfs_ops.realpath(conn, path, resolved_path);
211 }
212
213 static size_t skel_fget_nt_acl(struct files_struct *fsp, int fd, struct security_descriptor_info **ppdesc)
214 {
215         return default_vfs_ops.fget_nt_acl(fsp, fd, ppdesc);
216 }
217
218 static size_t skel_get_nt_acl(struct files_struct *fsp, const char *name, struct security_descriptor_info **ppdesc)
219 {
220         return default_vfs_ops.get_nt_acl(fsp, name, ppdesc);
221 }
222
223 static BOOL skel_fset_nt_acl(struct files_struct *fsp, int fd, uint32 security_info_sent, struct security_descriptor_info *psd)
224 {
225         return default_vfs_ops.fset_nt_acl(fsp, fd, security_info_sent, psd);
226 }
227
228 static BOOL skel_set_nt_acl(struct files_struct *fsp, const char *name, uint32 security_info_sent, struct security_descriptor_info *psd)
229 {
230         return default_vfs_ops.set_nt_acl(fsp, name, security_info_sent, psd);
231 }
232
233 static BOOL skel_chmod_acl(struct connection_struct *conn, const char *name, mode_t mode)
234 {
235         /* If the underlying VFS doesn't have ACL support... */
236         if (!default_vfs_ops.chmod_acl) {
237                 errno = ENOSYS;
238                 return -1;
239         }
240         return default_vfs_ops.chmod_acl(conn, name, mode);
241 }
242
243 static BOOL skel_fchmod_acl(struct files_struct *fsp, int fd, mode_t mode)
244 {
245         /* If the underlying VFS doesn't have ACL support... */
246         if (!default_vfs_ops.fchmod_acl) {
247                 errno = ENOSYS;
248                 return -1;
249         }
250         return default_vfs_ops.fchmod_acl(fsp, fd, mode);
251 }
252
253 static int skel_sys_acl_get_entry(struct connection_struct *conn, SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p)
254 {
255         return default_vfs_ops.sys_acl_get_entry(conn, theacl, entry_id, entry_p);
256 }
257
258 static int skel_sys_acl_get_tag_type(struct connection_struct *conn, SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p)
259 {
260         return default_vfs_ops.sys_acl_get_tag_type(conn, entry_d, tag_type_p);
261 }
262
263 static int skel_sys_acl_get_permset(struct connection_struct *conn, SMB_ACL_ENTRY_T entry_d, SMB_ACL_PERMSET_T *permset_p)
264 {
265         return default_vfs_ops.sys_acl_get_permset(conn, entry_d, permset_p);
266 }
267
268 static void *skel_sys_acl_get_qualifier(struct connection_struct *conn, SMB_ACL_ENTRY_T entry_d)
269 {
270         return default_vfs_ops.sys_acl_get_qualifier(conn, entry_d);
271 }
272
273 static SMB_ACL_T skel_sys_acl_get_file(struct connection_struct *conn, const char *path_p, SMB_ACL_TYPE_T type)
274 {
275         return default_vfs_ops.sys_acl_get_file(conn, path_p, type);
276 }
277
278 static SMB_ACL_T skel_sys_acl_get_fd(struct files_struct *fsp, int fd)
279 {
280         return default_vfs_ops.sys_acl_get_fd(fsp, fd);
281 }
282
283 static int skel_sys_acl_clear_perms(struct connection_struct *conn, SMB_ACL_PERMSET_T permset)
284 {
285         return default_vfs_ops.sys_acl_clear_perms(conn, permset);
286 }
287
288 static int skel_sys_acl_add_perm(struct connection_struct *conn, SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
289 {
290         return default_vfs_ops.sys_acl_add_perm(conn, permset, perm);
291 }
292
293 static char *skel_sys_acl_to_text(struct connection_struct *conn, SMB_ACL_T theacl, ssize_t *plen)
294 {
295         return default_vfs_ops.sys_acl_to_text(conn, theacl, plen);
296 }
297
298 static SMB_ACL_T skel_sys_acl_init(struct connection_struct *conn, int count)
299 {
300         return default_vfs_ops.sys_acl_init(conn, count);
301 }
302
303 static int skel_sys_acl_create_entry(struct connection_struct *conn, SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry)
304 {
305         return default_vfs_ops.sys_acl_create_entry(conn, pacl, pentry);
306 }
307
308 static int skel_sys_acl_set_tag_type(struct connection_struct *conn, SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tagtype)
309 {
310         return default_vfs_ops.sys_acl_set_tag_type(conn, entry, tagtype);
311 }
312
313 static int skel_sys_acl_set_qualifier(struct connection_struct *conn, SMB_ACL_ENTRY_T entry, void *qual)
314 {
315         return default_vfs_ops.sys_acl_set_qualifier(conn, entry, qual);
316 }
317
318 static int skel_sys_acl_set_permset(struct connection_struct *conn, SMB_ACL_ENTRY_T entry, SMB_ACL_PERMSET_T permset)
319 {
320         return default_vfs_ops.sys_acl_set_permset(conn, entry, permset);
321 }
322
323 static int skel_sys_acl_valid(struct connection_struct *conn, SMB_ACL_T theacl )
324 {
325         return default_vfs_ops.sys_acl_valid(conn, theacl );
326 }
327
328 static int skel_sys_acl_set_file(struct connection_struct *conn, const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
329 {
330         return default_vfs_ops.sys_acl_set_file(conn, name, acltype, theacl);
331 }
332
333 static int skel_sys_acl_set_fd(struct files_struct *fsp, int fd, SMB_ACL_T theacl)
334 {
335         return default_vfs_ops.sys_acl_set_fd(fsp, fd, theacl);
336 }
337
338 static int skel_sys_acl_delete_def_file(struct connection_struct *conn, const char *path)
339 {
340         return default_vfs_ops.sys_acl_delete_def_file(conn, path);
341 }
342
343 static int skel_sys_acl_get_perm(struct connection_struct *conn, SMB_ACL_PERMSET_T permset, SMB_ACL_PERM_T perm)
344 {
345         return default_vfs_ops.sys_acl_get_perm(conn, permset, perm);
346 }
347
348 static int skel_sys_acl_free_text(struct connection_struct *conn, char *text)
349 {
350         return default_vfs_ops.sys_acl_free_text(conn, text);
351 }
352
353 static int skel_sys_acl_free_acl(struct connection_struct *conn, SMB_ACL_T posix_acl)
354 {
355         return default_vfs_ops.sys_acl_free_acl(conn, posix_acl);
356 }
357
358 static int skel_sys_acl_free_qualifier(struct connection_struct *conn, void *qualifier, SMB_ACL_TAG_T tagtype)
359 {
360         return default_vfs_ops.sys_acl_free_qualifier(conn, qualifier, tagtype);
361 }
362
363
364 /* VFS operations structure */
365
366 static vfs_op_tuple skel_ops[] = {
367
368         /* Disk operations */
369
370         {skel_connect,                  SMB_VFS_OP_CONNECT,             SMB_VFS_LAYER_TRANSPARENT},
371         {skel_disconnect,               SMB_VFS_OP_DISCONNECT,          SMB_VFS_LAYER_TRANSPARENT},
372         {skel_disk_free,                SMB_VFS_OP_DISK_FREE,           SMB_VFS_LAYER_TRANSPARENT},
373         
374         /* Directory operations */
375
376         {skel_opendir,                  SMB_VFS_OP_OPENDIR,             SMB_VFS_LAYER_TRANSPARENT},
377         {skel_readdir,                  SMB_VFS_OP_READDIR,             SMB_VFS_LAYER_TRANSPARENT},
378         {skel_mkdir,                    SMB_VFS_OP_MKDIR,               SMB_VFS_LAYER_TRANSPARENT},
379         {skel_rmdir,                    SMB_VFS_OP_RMDIR,               SMB_VFS_LAYER_TRANSPARENT},
380         {skel_closedir,                 SMB_VFS_OP_CLOSEDIR,            SMB_VFS_LAYER_TRANSPARENT},
381
382         /* File operations */
383
384         {skel_open,                     SMB_VFS_OP_OPEN,                SMB_VFS_LAYER_TRANSPARENT},
385         {skel_close,                    SMB_VFS_OP_CLOSE,               SMB_VFS_LAYER_TRANSPARENT},
386         {skel_read,                     SMB_VFS_OP_READ,                SMB_VFS_LAYER_TRANSPARENT},
387         {skel_write,                    SMB_VFS_OP_WRITE,               SMB_VFS_LAYER_TRANSPARENT},
388         {skel_lseek,                    SMB_VFS_OP_LSEEK,               SMB_VFS_LAYER_TRANSPARENT},
389         {skel_rename,                   SMB_VFS_OP_RENAME,              SMB_VFS_LAYER_TRANSPARENT},
390         {skel_fsync,                    SMB_VFS_OP_FSYNC,               SMB_VFS_LAYER_TRANSPARENT},
391         {skel_stat,                     SMB_VFS_OP_STAT,                SMB_VFS_LAYER_TRANSPARENT},
392         {skel_fstat,                    SMB_VFS_OP_FSTAT,               SMB_VFS_LAYER_TRANSPARENT},
393         {skel_lstat,                    SMB_VFS_OP_LSTAT,               SMB_VFS_LAYER_TRANSPARENT},
394         {skel_unlink,                   SMB_VFS_OP_UNLINK,              SMB_VFS_LAYER_TRANSPARENT},
395         {skel_chmod,                    SMB_VFS_OP_CHMOD,               SMB_VFS_LAYER_TRANSPARENT},
396         {skel_fchmod,                   SMB_VFS_OP_FCHMOD,              SMB_VFS_LAYER_TRANSPARENT},
397         {skel_chown,                    SMB_VFS_OP_CHOWN,               SMB_VFS_LAYER_TRANSPARENT},
398         {skel_fchown,                   SMB_VFS_OP_FCHOWN,              SMB_VFS_LAYER_TRANSPARENT},
399         {skel_chdir,                    SMB_VFS_OP_CHDIR,               SMB_VFS_LAYER_TRANSPARENT},
400         {skel_getwd,                    SMB_VFS_OP_GETWD,               SMB_VFS_LAYER_TRANSPARENT},
401         {skel_utime,                    SMB_VFS_OP_UTIME,               SMB_VFS_LAYER_TRANSPARENT},
402         {skel_ftruncate,                SMB_VFS_OP_FTRUNCATE,           SMB_VFS_LAYER_TRANSPARENT},
403         {skel_lock,                     SMB_VFS_OP_LOCK,                SMB_VFS_LAYER_TRANSPARENT},
404         {skel_symlink,                  SMB_VFS_OP_SYMLINK,             SMB_VFS_LAYER_TRANSPARENT},
405         {skel_readlink,                 SMB_VFS_OP_READLINK,            SMB_VFS_LAYER_TRANSPARENT},
406         {skel_link,                     SMB_VFS_OP_LINK,                SMB_VFS_LAYER_TRANSPARENT},
407         {skel_mknod,                    SMB_VFS_OP_MKNOD,               SMB_VFS_LAYER_TRANSPARENT},
408         {skel_realpath,                 SMB_VFS_OP_REALPATH,            SMB_VFS_LAYER_TRANSPARENT},
409
410         /* NT File ACL operations */
411
412         {skel_fget_nt_acl,              SMB_VFS_OP_FGET_NT_ACL,         SMB_VFS_LAYER_TRANSPARENT},
413         {skel_get_nt_acl,               SMB_VFS_OP_GET_NT_ACL,          SMB_VFS_LAYER_TRANSPARENT},
414         {skel_fset_nt_acl,              SMB_VFS_OP_FSET_NT_ACL,         SMB_VFS_LAYER_TRANSPARENT},
415         {skel_set_nt_acl,               SMB_VFS_OP_SET_NT_ACL,          SMB_VFS_LAYER_TRANSPARENT},
416
417         /* POSIX ACL operations */
418
419         {skel_chmod_acl,                SMB_VFS_OP_CHMOD_ACL,           SMB_VFS_LAYER_TRANSPARENT},
420         {skel_fchmod_acl,               SMB_VFS_OP_FCHMOD_ACL,          SMB_VFS_LAYER_TRANSPARENT},
421
422         {skel_sys_acl_get_entry,        SMB_VFS_OP_SYS_ACL_GET_ENTRY,           SMB_VFS_LAYER_TRANSPARENT},
423         {skel_sys_acl_get_tag_type,     SMB_VFS_OP_SYS_ACL_GET_TAG_TYPE,        SMB_VFS_LAYER_TRANSPARENT},
424         {skel_sys_acl_get_permset,      SMB_VFS_OP_SYS_ACL_GET_PERMSET,         SMB_VFS_LAYER_TRANSPARENT},
425         {skel_sys_acl_get_qualifier,    SMB_VFS_OP_SYS_ACL_GET_QUALIFIER,       SMB_VFS_LAYER_TRANSPARENT},
426         {skel_sys_acl_get_file,         SMB_VFS_OP_SYS_ACL_GET_FILE,            SMB_VFS_LAYER_TRANSPARENT},
427         {skel_sys_acl_get_fd,           SMB_VFS_OP_SYS_ACL_GET_FD,              SMB_VFS_LAYER_TRANSPARENT},
428         {skel_sys_acl_clear_perms,      SMB_VFS_OP_SYS_ACL_CLEAR_PERMS,         SMB_VFS_LAYER_TRANSPARENT},
429         {skel_sys_acl_add_perm,         SMB_VFS_OP_SYS_ACL_ADD_PERM,            SMB_VFS_LAYER_TRANSPARENT},
430         {skel_sys_acl_to_text,          SMB_VFS_OP_SYS_ACL_TO_TEXT,             SMB_VFS_LAYER_TRANSPARENT},
431         {skel_sys_acl_init,             SMB_VFS_OP_SYS_ACL_INIT,                SMB_VFS_LAYER_TRANSPARENT},
432         {skel_sys_acl_create_entry,     SMB_VFS_OP_SYS_ACL_CREATE_ENTRY,        SMB_VFS_LAYER_TRANSPARENT},
433         {skel_sys_acl_set_tag_type,     SMB_VFS_OP_SYS_ACL_SET_TAG_TYPE,        SMB_VFS_LAYER_TRANSPARENT},
434         {skel_sys_acl_set_qualifier,    SMB_VFS_OP_SYS_ACL_SET_QUALIFIER,       SMB_VFS_LAYER_TRANSPARENT},
435         {skel_sys_acl_set_permset,      SMB_VFS_OP_SYS_ACL_SET_PERMSET,         SMB_VFS_LAYER_TRANSPARENT},
436         {skel_sys_acl_valid,            SMB_VFS_OP_SYS_ACL_VALID,               SMB_VFS_LAYER_TRANSPARENT},
437         {skel_sys_acl_set_file,         SMB_VFS_OP_SYS_ACL_SET_FILE,            SMB_VFS_LAYER_TRANSPARENT},
438         {skel_sys_acl_set_fd,           SMB_VFS_OP_SYS_ACL_SET_FD,              SMB_VFS_LAYER_TRANSPARENT},
439         {skel_sys_acl_delete_def_file,  SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,     SMB_VFS_LAYER_TRANSPARENT},
440         {skel_sys_acl_get_perm,         SMB_VFS_OP_SYS_ACL_GET_PERM,            SMB_VFS_LAYER_TRANSPARENT},
441         {skel_sys_acl_free_text,        SMB_VFS_OP_SYS_ACL_FREE_TEXT,           SMB_VFS_LAYER_TRANSPARENT},
442         {skel_sys_acl_free_acl,         SMB_VFS_OP_SYS_ACL_FREE_ACL,            SMB_VFS_LAYER_TRANSPARENT},
443         {skel_sys_acl_free_qualifier,   SMB_VFS_OP_SYS_ACL_FREE_QUALIFIER,      SMB_VFS_LAYER_TRANSPARENT},
444         
445         {NULL,  SMB_VFS_OP_NOOP,        SMB_VFS_LAYER_NOOP}
446 };
447
448 /* VFS initialisation - return initialized vfs_op_tuple array back to Samba */
449
450 static vfs_op_tuple *skel_init(const struct vfs_ops *def_vfs_ops,
451                         struct smb_vfs_handle_struct *vfs_handle)
452 {
453         DEBUG(3, ("Initialising default vfs hooks\n"));
454
455         memcpy(&default_vfs_ops, def_vfs_ops, sizeof(struct vfs_ops));
456         
457         /* Remember vfs_handle for further allocation and referencing of private
458            information in vfs_handle->data
459         */
460         skel_handle = vfs_handle;
461         return skel_ops;
462 }
463
464 NTSTATUS init_module(void)
465 {
466         return smb_register_vfs("skel", skel_init, SMB_VFS_INTERFACE_VERSION);
467 }