LCOV - code coverage report
Current view: top level - source3/modules - vfs_fake_dfq.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 108 119 90.8 %
Date: 2021-09-23 10:06:22 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Fake Disk-Free and Quota VFS module.  Implements passthrough operation
       3             :  * of all VFS calls, except for "disk free" and "get quota" which
       4             :  * are handled by reading a text file named ".dfq" in the current directory.
       5             :  *
       6             :  * This module is intended for testing purposes.
       7             :  *
       8             :  * Copyright (C) Uri Simchoni, 2016
       9             :  *
      10             :  * This program 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             :  * This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
      22             :  */
      23             : 
      24             : #include "includes.h"
      25             : #include "smbd/smbd.h"
      26             : 
      27             : #undef DBGC_CLASS
      28             : #define DBGC_CLASS DBGC_VFS
      29             : 
      30             : static int dfq_get_quota(struct vfs_handle_struct *handle,
      31             :                         const struct smb_filename *smb_fname,
      32             :                         enum SMB_QUOTA_TYPE qtype,
      33             :                         unid_t id,
      34             :                         SMB_DISK_QUOTA *qt);
      35             : 
      36       20160 : static uint64_t dfq_load_param(int snum, const char *path, const char *section,
      37             :                                const char *param, uint64_t def_val)
      38             : {
      39             :         uint64_t ret;
      40             : 
      41       20160 :         char *option =
      42       20160 :             talloc_asprintf(talloc_tos(), "%s/%s/%s", section, param, path);
      43       20160 :         if (option == NULL) {
      44           0 :                 return def_val;
      45             :         }
      46             : 
      47       20160 :         ret = (uint64_t)lp_parm_ulonglong(snum, "fake_dfq", option,
      48             :                                           (unsigned long long)def_val);
      49             : 
      50       20160 :         TALLOC_FREE(option);
      51             : 
      52       20160 :         return ret;
      53             : }
      54             : 
      55          48 : static uint64_t dfq_disk_free(vfs_handle_struct *handle,
      56             :                                 const struct smb_filename *smb_fname,
      57             :                                 uint64_t *bsize,
      58             :                                 uint64_t *dfree,
      59             :                                 uint64_t *dsize)
      60             : {
      61             :         uint64_t free_1k;
      62          48 :         int snum = SNUM(handle->conn);
      63          48 :         uint64_t dfq_bsize = 0;
      64          48 :         struct smb_filename *rpath_fname = NULL;
      65             : 
      66             :         /* look up the params based on real path to be resilient
      67             :          * to refactoring of path<->realpath
      68             :          */
      69          48 :         rpath_fname = SMB_VFS_NEXT_REALPATH(handle, talloc_tos(), smb_fname);
      70          48 :         if (rpath_fname != NULL) {
      71          48 :                 dfq_bsize = dfq_load_param(snum, rpath_fname->base_name,
      72             :                                 "df", "block size", 0);
      73             :         }
      74          48 :         if (dfq_bsize == 0) {
      75           2 :                 TALLOC_FREE(rpath_fname);
      76           2 :                 return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname, bsize, dfree,
      77             :                                               dsize);
      78             :         }
      79             : 
      80          46 :         *bsize = dfq_bsize;
      81          46 :         *dfree = dfq_load_param(snum, rpath_fname->base_name,
      82             :                                 "df", "disk free", 0);
      83          46 :         *dsize = dfq_load_param(snum, rpath_fname->base_name,
      84             :                                 "df", "disk size", 0);
      85             : 
      86          46 :         if ((*bsize) < 1024) {
      87           6 :                 free_1k = (*dfree) / (1024 / (*bsize));
      88             :         } else {
      89          40 :                 free_1k = ((*bsize) / 1024) * (*dfree);
      90             :         }
      91             : 
      92          46 :         TALLOC_FREE(rpath_fname);
      93          46 :         return free_1k;
      94             : }
      95             : 
      96         134 : static int dfq_get_quota(struct vfs_handle_struct *handle,
      97             :                         const struct smb_filename *smb_fname,
      98             :                         enum SMB_QUOTA_TYPE qtype,
      99             :                         unid_t id,
     100             :                         SMB_DISK_QUOTA *qt)
     101             : {
     102         134 :         int rc = 0;
     103             :         int save_errno;
     104         134 :         char *section = NULL;
     105         134 :         int snum = SNUM(handle->conn);
     106         134 :         uint64_t bsize = 0;
     107         134 :         struct smb_filename *rpath_fname = NULL;
     108             : 
     109         134 :         rpath_fname = SMB_VFS_NEXT_REALPATH(handle, talloc_tos(), smb_fname);
     110         134 :         if (rpath_fname == NULL) {
     111           0 :                 goto dflt;
     112             :         }
     113             : 
     114         134 :         switch (qtype) {
     115          50 :         case SMB_USER_QUOTA_TYPE:
     116          50 :                 section = talloc_asprintf(talloc_tos(), "u%llu",
     117          50 :                                           (unsigned long long)id.uid);
     118          50 :                 break;
     119          18 :         case SMB_GROUP_QUOTA_TYPE:
     120          18 :                 section = talloc_asprintf(talloc_tos(), "g%llu",
     121          18 :                                           (unsigned long long)id.gid);
     122          18 :                 break;
     123          48 :         case SMB_USER_FS_QUOTA_TYPE:
     124          48 :                 section = talloc_strdup(talloc_tos(), "udflt");
     125          48 :                 break;
     126          18 :         case SMB_GROUP_FS_QUOTA_TYPE:
     127          18 :                 section = talloc_strdup(talloc_tos(), "gdflt");
     128          18 :                 break;
     129           0 :         default:
     130           0 :                 break;
     131             :         }
     132             : 
     133         134 :         if (section == NULL) {
     134           0 :                 goto dflt;
     135             :         }
     136             : 
     137         134 :         bsize = dfq_load_param(snum, rpath_fname->base_name,
     138             :                                 section, "block size", 4096);
     139         134 :         if (bsize == 0) {
     140           0 :                 goto dflt;
     141             :         }
     142             : 
     143         134 :         if (dfq_load_param(snum, rpath_fname->base_name,
     144             :                                 section, "err", 0) != 0) {
     145           2 :                 errno = ENOTSUP;
     146           2 :                 rc = -1;
     147           2 :                 goto out;
     148             :         }
     149             : 
     150         132 :         if (dfq_load_param(snum, rpath_fname->base_name,
     151             :                                 section, "nosys", 0) != 0) {
     152           2 :                 errno = ENOSYS;
     153           2 :                 rc = -1;
     154           2 :                 goto out;
     155             :         }
     156             : 
     157         130 :         ZERO_STRUCTP(qt);
     158             : 
     159         130 :         qt->bsize = bsize;
     160         130 :         qt->hardlimit = dfq_load_param(snum, rpath_fname->base_name,
     161             :                                 section, "hard limit", 0);
     162         130 :         qt->softlimit = dfq_load_param(snum, rpath_fname->base_name,
     163             :                                 section, "soft limit", 0);
     164         130 :         qt->curblocks = dfq_load_param(snum, rpath_fname->base_name,
     165             :                                 section, "cur blocks", 0);
     166         130 :         qt->ihardlimit =
     167         130 :             dfq_load_param(snum, rpath_fname->base_name,
     168             :                                 section, "inode hard limit", 0);
     169         130 :         qt->isoftlimit =
     170         130 :             dfq_load_param(snum, rpath_fname->base_name,
     171             :                                 section, "inode soft limit", 0);
     172         130 :         qt->curinodes = dfq_load_param(snum, rpath_fname->base_name,
     173             :                                 section, "cur inodes", 0);
     174         130 :         qt->qflags = dfq_load_param(snum, rpath_fname->base_name,
     175             :                                 section, "qflags", QUOTAS_DENY_DISK);
     176             : 
     177         130 :         goto out;
     178             : 
     179           0 : dflt:
     180           0 :         rc = SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname, qtype, id, qt);
     181             : 
     182         134 : out:
     183         134 :         save_errno = errno;
     184         134 :         TALLOC_FREE(section);
     185         134 :         TALLOC_FREE(rpath_fname);
     186         134 :         errno = save_errno;
     187         134 :         return rc;
     188             : }
     189             : 
     190       18710 : static void dfq_fake_stat(struct vfs_handle_struct *handle,
     191             :                           struct smb_filename *smb_fname,
     192             :                           SMB_STRUCT_STAT *sbuf)
     193             : {
     194       18710 :         int snum = SNUM(handle->conn);
     195       18710 :         char *full_path = NULL;
     196       18710 :         char *to_free = NULL;
     197             :         char path[PATH_MAX + 1];
     198             :         int len;
     199             :         gid_t gid;
     200             : 
     201       18710 :         len = full_path_tos(handle->conn->cwd_fsp->fsp_name->base_name,
     202       18710 :                             smb_fname->base_name,
     203             :                             path, sizeof(path),
     204             :                             &full_path, &to_free);
     205       18710 :         if (len == -1) {
     206           0 :                 DBG_ERR("Could not allocate memory in full_path_tos.\n");
     207           0 :                 return;
     208             :         }
     209             : 
     210       18710 :         gid = dfq_load_param(snum, full_path, "stat", "sgid", 0);
     211       18710 :         if (gid != 0) {
     212         102 :                 sbuf->st_ex_gid = gid;
     213         102 :                 sbuf->st_ex_mode |= S_ISGID;
     214             :         }
     215             : 
     216       18710 :         TALLOC_FREE(to_free);
     217             : }
     218             : 
     219        6014 : static int dfq_stat(vfs_handle_struct *handle,
     220             :                     struct smb_filename *smb_fname)
     221             : {
     222             :         int ret;
     223             : 
     224        6014 :         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
     225        6014 :         if (ret == -1) {
     226           8 :                 return ret;
     227             :         }
     228             : 
     229        6006 :         dfq_fake_stat(handle, smb_fname, &smb_fname->st);
     230             : 
     231        6006 :         return 0;
     232             : }
     233             : 
     234       12702 : static int dfq_fstat(vfs_handle_struct *handle,
     235             :                      files_struct *fsp,
     236             :                      SMB_STRUCT_STAT *sbuf)
     237             : {
     238             :         int ret;
     239             : 
     240       12702 :         ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
     241       12702 :         if (ret == -1) {
     242           2 :                 return ret;
     243             :         }
     244             : 
     245       12700 :         dfq_fake_stat(handle, fsp->fsp_name, sbuf);
     246             : 
     247       12700 :         return 0;
     248             : }
     249             : 
     250           4 : static int dfq_lstat(vfs_handle_struct *handle,
     251             :                      struct smb_filename *smb_fname)
     252             : {
     253             :         int ret;
     254             : 
     255           4 :         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
     256           4 :         if (ret == -1) {
     257           0 :                 return ret;
     258             :         }
     259             : 
     260           4 :         dfq_fake_stat(handle, smb_fname, &smb_fname->st);
     261             : 
     262           4 :         return 0;
     263             : }
     264             : 
     265             : struct vfs_fn_pointers vfs_fake_dfq_fns = {
     266             :     /* Disk operations */
     267             : 
     268             :     .disk_free_fn = dfq_disk_free,
     269             :     .get_quota_fn = dfq_get_quota,
     270             : 
     271             :     .stat_fn = dfq_stat,
     272             :     .fstat_fn = dfq_fstat,
     273             :     .lstat_fn = dfq_lstat,
     274             : };
     275             : 
     276             : static_decl_vfs;
     277          96 : NTSTATUS vfs_fake_dfq_init(TALLOC_CTX *ctx)
     278             : {
     279          96 :         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fake_dfq",
     280             :                                 &vfs_fake_dfq_fns);
     281             : }

Generated by: LCOV version 1.13