Guest User

vfs_greyhole.c

a guest
Sep 3rd, 2011
118
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /*
  3. Copyright Guillaume Boudreau, 2009
  4.  
  5. This file is part of Greyhole.
  6.  
  7. It was created based on vfs_extd_audit.c, by Tim Potter, Alexander
  8. Bokovoy, John H Terpstra & Stefan (metze) Metzmacher.
  9.  
  10. Greyhole is free software: you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation, either version 3 of the License, or
  13. (at your option) any later version.
  14.  
  15. Greyhole is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with Greyhole.  If not, see <http://www.gnu.org/licenses/>.
  22. */
  23.  
  24. #include "includes.h"
  25. #include "system/syslog.h"
  26. #include "system/filesys.h"
  27. #include "smbd/smbd.h"
  28.  
  29. static int vfs_greyhole_debug_level = DBGC_VFS;
  30.  
  31. #undef DBGC_CLASS
  32. #define DBGC_CLASS vfs_greyhole_debug_level
  33.  
  34. #define vfs_greyhole_init init_samba_module
  35.  
  36. /* Function prototypes */
  37.  
  38. static int greyhole_connect(vfs_handle_struct *handle, const char *svc, const char *user);
  39. static int greyhole_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode);
  40. static int greyhole_rmdir(vfs_handle_struct *handle, const char *path);
  41. static int greyhole_open(vfs_handle_struct *handle, const struct smb_filename *fname, files_struct *fsp, int flags, mode_t mode);
  42. static int greyhole_close(vfs_handle_struct *handle, files_struct *fsp);
  43. static int greyhole_rename(vfs_handle_struct *handle, const struct smb_filename *oldname, const struct smb_filename *newname);
  44. static int greyhole_unlink(vfs_handle_struct *handle, const struct smb_filename *path);
  45.  
  46. /* VFS operations */
  47.  
  48. static struct vfs_fn_pointers vfs_greyhole_fns = {
  49.    
  50.     /* Disk operations */
  51.    
  52.     .connect_fn = greyhole_connect,
  53.    
  54.     /* Directory operations */
  55.    
  56.     .mkdir = greyhole_mkdir,
  57.     .rmdir = greyhole_rmdir,
  58.    
  59.     /* File operations */
  60.    
  61.     .open_fn = greyhole_open,
  62.     .close_fn = greyhole_close,
  63.     .rename = greyhole_rename,
  64.     .unlink = greyhole_unlink
  65. };
  66.  
  67. static int greyhole_syslog_facility(vfs_handle_struct *handle)
  68. {
  69.     static const struct enum_list enum_log_facilities[] = {
  70.         { LOG_USER, "USER" },
  71.         { LOG_LOCAL0, "LOCAL0" },
  72.         { LOG_LOCAL1, "LOCAL1" },
  73.         { LOG_LOCAL2, "LOCAL2" },
  74.         { LOG_LOCAL3, "LOCAL3" },
  75.         { LOG_LOCAL4, "LOCAL4" },
  76.         { LOG_LOCAL5, "LOCAL5" },
  77.         { LOG_LOCAL6, "LOCAL6" },
  78.         { LOG_LOCAL7, "LOCAL7" }
  79.     };
  80.  
  81.     int facility;
  82.  
  83.     facility = lp_parm_enum(SNUM(handle->conn), "greyhole", "facility", enum_log_facilities, LOG_LOCAL6);
  84.  
  85.     return facility;
  86. }
  87.  
  88. /* Implementation of vfs_ops.  Pass everything on to the default
  89.    operation but log event first. */
  90.  
  91. static int greyhole_connect(vfs_handle_struct *handle, const char *svc, const char *user)
  92. {
  93.     int result;
  94.  
  95.     if (!handle) {
  96.         return -1;
  97.     }
  98.  
  99.     openlog("smbd_greyhole", 0, greyhole_syslog_facility(handle));
  100.  
  101.     result = SMB_VFS_NEXT_CONNECT(handle, svc, user);
  102.  
  103.     return result;
  104. }
  105.  
  106. static int greyhole_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
  107. {
  108.     int result;
  109.     FILE *spoolf;
  110.     char filename[38];
  111.     struct timeval tp;
  112.  
  113.     result = SMB_VFS_NEXT_MKDIR(handle, path, mode);
  114.  
  115.     if (result >= 0) {
  116.         gettimeofday(&tp, (struct timezone *) NULL);
  117.         snprintf(filename, 37, "/var/spool/greyhole/%.0f", ((double) (tp.tv_sec)*1000000.0) + (((double) tp.tv_usec)));
  118.         spoolf = fopen(filename, "wt");
  119.         fprintf(spoolf, "mkdir\n%s\n%s\n\n",
  120.             lp_servicename(handle->conn->params->service),
  121.             path);
  122.         fclose(spoolf);
  123.     }
  124.  
  125.     return result;
  126. }
  127.  
  128. static int greyhole_rmdir(vfs_handle_struct *handle, const char *path)
  129. {
  130.     int result;
  131.     FILE *spoolf;
  132.     char filename[38];
  133.     struct timeval tp;
  134.  
  135.     result = SMB_VFS_NEXT_RMDIR(handle, path);
  136.  
  137.     if (result >= 0) {
  138.         gettimeofday(&tp, (struct timezone *) NULL);
  139.         snprintf(filename, 37, "/var/spool/greyhole/%.0f", ((double) (tp.tv_sec)*1000000.0) + (((double) tp.tv_usec)));
  140.         spoolf = fopen(filename, "wt");
  141.         fprintf(spoolf, "rmdir\n%s\n%s\n\n",
  142.             lp_servicename(handle->conn->params->service),
  143.             path);
  144.         fclose(spoolf);
  145.     }
  146.    
  147.     return result;
  148. }
  149.  
  150. static int greyhole_open(vfs_handle_struct *handle, const struct smb_filename *fname, files_struct *fsp, int flags, mode_t mode)
  151. {
  152.     int result;
  153.     FILE *spoolf;
  154.     char filename[38];
  155.     struct timeval tp;
  156.  
  157.     result = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
  158.  
  159.     if (result >= 0) {
  160.         if ((flags & O_WRONLY) || (flags & O_RDWR)) {
  161.             gettimeofday(&tp, (struct timezone *) NULL);
  162.             snprintf(filename, 37, "/var/spool/greyhole/%.0f", ((double) (tp.tv_sec)*1000000.0) + (((double) tp.tv_usec)));
  163.             spoolf = fopen(filename, "wt");
  164.             fprintf(spoolf, "open\n%s\n%s\n%d\n%s\n",
  165.                 lp_servicename(handle->conn->params->service),
  166.                 fname->base_name,
  167.                 result,
  168.                 "for writing ");
  169.             fclose(spoolf);
  170.         }
  171.     }
  172.  
  173.     return result;
  174. }
  175.  
  176. static int greyhole_close(vfs_handle_struct *handle, files_struct *fsp)
  177. {
  178.     int result;
  179.     FILE *spoolf;
  180.     char filename[38];
  181.     struct timeval tp;
  182.  
  183.     result = SMB_VFS_NEXT_CLOSE(handle, fsp);
  184.  
  185.     if (result >= 0) {
  186.         gettimeofday(&tp, (struct timezone *) NULL);
  187.         snprintf(filename, 37, "/var/spool/greyhole/%.0f", ((double) (tp.tv_sec)*1000000.0) + (((double) tp.tv_usec)));
  188.         spoolf = fopen(filename, "wt");
  189.         fprintf(spoolf, "close\n%s\n%d\n\n",
  190.             lp_servicename(handle->conn->params->service),
  191.             fsp->fh->fd);
  192.         fclose(spoolf);
  193.     }
  194.  
  195.     return result;
  196. }
  197.  
  198. static int greyhole_rename(vfs_handle_struct *handle, const struct smb_filename *oldname, const struct smb_filename *newname)
  199. {
  200.     int result;
  201.     FILE *spoolf;
  202.     char filename[38];
  203.     struct timeval tp;
  204.  
  205.     result = SMB_VFS_NEXT_RENAME(handle, oldname, newname);
  206.  
  207.     if (result >= 0) {
  208.         gettimeofday(&tp, (struct timezone *) NULL);
  209.         snprintf(filename, 37, "/var/spool/greyhole/%.0f", ((double) (tp.tv_sec)*1000000.0) + (((double) tp.tv_usec)));
  210.         spoolf = fopen(filename, "wt");
  211.         fprintf(spoolf, "rename\n%s\n%s\n%s\n\n",
  212.             lp_servicename(handle->conn->params->service),
  213.             oldname->base_name,
  214.             newname->base_name);
  215.         fclose(spoolf);
  216.     }
  217.  
  218.     return result;
  219. }
  220.  
  221. static int greyhole_unlink(vfs_handle_struct *handle, const struct smb_filename *path)
  222. {
  223.     int result;
  224.     FILE *spoolf;
  225.     char filename[38];
  226.     struct timeval tp;
  227.  
  228.     result = SMB_VFS_NEXT_UNLINK(handle, path);
  229.  
  230.     if (result >= 0) {
  231.         gettimeofday(&tp, (struct timezone *) NULL);
  232.         snprintf(filename, 37, "/var/spool/greyhole/%.0f", ((double) (tp.tv_sec)*1000000.0) + (((double) tp.tv_usec)));
  233.         spoolf = fopen(filename, "wt");
  234.         fprintf(spoolf, "unlink\n%s\n%s\n\n",
  235.             lp_servicename(handle->conn->params->service),
  236.             path->base_name);
  237.         fclose(spoolf);
  238.     }
  239.  
  240.     return result;
  241. }
  242.  
  243. NTSTATUS vfs_greyhole_init(void);
  244. NTSTATUS vfs_greyhole_init(void)
  245. {
  246.     NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "greyhole", &vfs_greyhole_fns);
  247.    
  248.     if (!NT_STATUS_IS_OK(ret))
  249.         return ret;
  250.  
  251.     vfs_greyhole_debug_level = debug_add_class("greyhole");
  252.     if (vfs_greyhole_debug_level == -1) {
  253.         vfs_greyhole_debug_level = DBGC_VFS;
  254.         DEBUG(0, ("vfs_greyhole: Couldn't register custom debugging class!\n"));
  255.     } else {
  256.         DEBUG(10, ("vfs_greyhole: Debug class number of 'greyhole': %d\n", vfs_greyhole_debug_level));
  257.     }
  258.    
  259.     return ret;
  260. }
RAW Paste Data