This file is indexed.

/usr/share/perl5/EBox/FileSystem.pm is in zentyal-common 2.3.3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# Copyright (C) 2008-2012 eBox Technologies S.L.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

package EBox::FileSystem;
use strict;
use warnings;


use base 'Exporter';
our @EXPORT_OK = qw(makePrivateDir cleanDir isSubdir dirDiskUsage dirFileSystem);
use Params::Validate;
use EBox::Validate;
use EBox::Gettext;
use EBox::Sudo;

use constant FSTAB_PATH => '/etc/fstab';
use constant MTAB_PATH => '/etc/mtab';

# Group: Public procedures

# Procedure: makePrivateDir
#
#       Creates  a  directory owned by the user running this
#       process and with private permissions.
#
# Parameters:
#
#       path - The path of the directory to be created, if it exists it must
#              already have proper ownership and permissions.
#
# Exceptions:
#
#       Internal & External - The path exists and is not a directory or has wrong
#                  ownership or permissions. Or it does not exist and
#                  cannot be created.
sub makePrivateDir # (path)
{
    my ($dir) = @_;
    validate_pos(@_, 1);

    if (-e $dir) {
        if (  not -d $dir) {
            throw EBox::Exceptions::Internal( "Cannot create private directory $dir: file exists");
        }
        else {
            return EBox::Validate::isPrivateDir($dir, 1);
        }
    }

    mkdir($dir, 0700) or throw EBox::Exceptions::Internal("Could not create directory: $dir");
}

# Procedure: cleanDir
#
#       take action to assure that one or more directories have not
#       any file into them. To achieve so, these files may be deleted or
#       directories created
#
# Parameters:
#
#      dirs - Array list of directories
#
sub cleanDir
{
    my @dirs = @_;
    if (@dirs == 0) {
        throw EBox::Exceptions::Internal('cleanDir must be supplied at least a dir as parameter');
    }

    EBox::Validate::checkFilePath($_, 'directory')  foreach  (@dirs);

    foreach my $d (@dirs) {
        my $dir;
        my $mode = 0700;

        if (ref $d eq 'HASH' ) {
            $dir  = $d->{name};
            $mode = $d->{mode}
        } else {
            $dir = $d;
        }

        if (-e $dir) {
            if (! -d $dir) {
                throw EBox::Exceptions::Internal("$dir exists and is not a directory");
            }

            system "rm -rf '$dir'/*";
            if ($? != 0) {
                throw EBox::Exceptions::Internal "Error cleaning $dir: $!";
            }
        } else {
            mkdir ($dir, $mode) or  throw EBox::Exceptions::Internal("Could not create directory: $dir");
        }
    }
}

# Function: isSubdir
#
#    Find if a directory is a sub dir of another. A directory is
#    always a subdirectory of itself
#
# Parameters:
#
#    $subDir - String the directory which we want to check if it is a
#    sub directory. It must be a abolute path
#
#     $parentDir - the possible parent directory
#
# Returns:
#
#    boolean - Whether the first directory is a subdirectory of the
#    second or not
#
sub isSubdir
{
    my ($subDir, $parentDir) = @_;

    foreach ($subDir, $parentDir) {
        if (! EBox::Validate::checkAbsoluteFilePath($_)) {
            throw EBox::Exceptions::Internal("isSubdir can only called with absolute paths. Argumentes were ($subDir, $parentDir)))");
        }
    }

    # normalize paths
    $subDir .= '/';
    $parentDir .= '/';
    $subDir =~ s{/+}{/}g;
    $parentDir =~ s{/+}{/}g;

    return $subDir =~ m/^$parentDir/;
}

# Function: permissionsFromStat
#     examines a File::stat  result object and extract the permissions value
#
# Parameters:
#      $stat - stat result object
#
# Returns:
#       the permissions as string
#
sub permissionsFromStat
{
    my ($stat) = @_;
    return sprintf("%04o", $stat->mode & 07777);
}

# Function: dirDiskUsage
#
#     Get the space used up by the files and subdirectories in a
#     directory
#
# Parameters:
#
#      dir       - String directory
#      blockSize - Int size of the block in bytes. Default: 1 Kb
#
# Returns:
#
#       Int - the space used in block size units
#
sub dirDiskUsage
{
    my ($dir, $blockSize) = @_;
    defined $dir or
        throw EBox::Exceptions::MissingArgument('dir');
    defined $blockSize or
        $blockSize = 1024;

    (-d $dir) or
        throw EBox::Exceptions::External(__x('Directory not found: {d}', d => $dir));

    my $duCommand = "/usr/bin/du --summarize --block-size=$blockSize '$dir'";

    my @duOutput = @{ EBox::Sudo::silentRoot($duCommand) };

    my ($blockCount) = split '\s', $duOutput[0], 2; # du outputs the block count first
        return $blockCount;
}


# Function: staticFileSystems
#
#      Return static file systems information as seen in /etc/fstab
#      file
#
#  Parameters:
#    bind - whether to include or not bind filesystems (name, default false)
#
# Returns:
#
#      Hash ref - with the file system as key and a hash with its
#      properties as value.
#
#      The properties are: mountPoint, type, options, dump and pass
#      The properties have the same format that the fields in the
#      fstab file.
#
sub staticFileSystems
{
    return _fileSystems(FSTAB_PATH, @_);
}


# Function: fileSystems
#
#   return mounted file systems information as seen in /etc/mtab
#
#  Parameters:
#    bind - whether to include or not bind filesystems (name, default false)
#
# Returns:
#      a hash reference with the file system as key and a hash with his
#      properties as value.
#      The properties are: mountPoint, type, options, dump and pass
#      The properties have the same format that the fields in the fstab file
sub fileSystems
{
    return _fileSystems(MTAB_PATH, @_);
}

#  Function: partitionsFileSystems
#
#   return the file system data for mounted disk partitions
#
# Parameters:
#  includeRemovables - include removable FS (now detected as FS under /media)
#
# Returns:
#      a hash reference with the file system as key and a hash with his
#      properties as value.
#      The properties are: mountPoint, type, options, dump and pass
#      The properties have the same format that the fields in the fstab file
#
my %noDeviceFs = (
    proc => 1,
    devpts => 1,
    tmpfs => 1,
    securityfs => 1,
    selinuxfs => 1,
    fuse => 1,
    devtmpfs => 1,
    binfmt_misc => 1,
);

sub partitionsFileSystems
{
    my ($includeRemovable) = @_;

    my %fileSys = %{  fileSystems() };

    while (my ($fs, $attrs) = each %fileSys) {
        # remove non-device filesystems
        if ($fs eq 'none') {
                delete $fileSys{$fs};
                next;
        }

        my $type = $attrs->{type};
        if (exists $noDeviceFs{$type} and $noDeviceFs{$type}) {
                delete $fileSys{$fs};
                next;
        }

        if (not $includeRemovable) {
            # remove removable media files
            my $mpoint = $attrs->{mountPoint};
            if ($mpoint =~ m{^/media/}) {
                delete $fileSys{$fs};
                next;
            }
        }
    }

    return \%fileSys;
}

# Group: Private procedures

sub _fileSystems
{
    my ($tabFile, %options) = @_;
    my $includeBind = $options{bind};
    my %fileSystems;

    my $FH;
    open $FH, $tabFile or
      throw EBox::Exceptions::Internal($tabFile . ' cannot be opened');
    while (my $line = <$FH>) {
        chomp $line;

        my ($lineData) = split '#', $line, 2; # remove comments

        next if not $lineData;
        next if ($lineData =~ m/^\s*$/); # discard empty lines

        my ($fsys, $mpoint, $type, $options, $dump, $pass) = split '\s+', $lineData;
        if ($fsys eq 'none') {
            # none file sys are ignored by now
            next;
        }

        my @options = split /,/, $options;
        my $bind = grep { $_ eq 'bind' } @options;
        if ($bind and not $includeBind) {
            # ignoring binded filesystems
            next;
        }

        my $attrs = {
            mountPoint => $mpoint,
            type => $type,
            options => $options,
            dump => $dump,
            pass => $pass,
        };

        $fileSystems{$fsys} = $attrs;
    }

    close $FH or
      throw EBox::Exceptions::Internal('Cannot properly close ' . FSTAB_PATH);

    return \%fileSystems;
}

#  Function: dirFileSystem
#
#  Returns:
#     the file system in which the directory resides
#
sub dirFileSystem
{
    my ($dir) = @_;
    (-d $dir) or
        throw EBox::Exceptions::External(__x('Directory not found: {d}', d=>$dir));

    my $fs;
    my $dirToCheck = $dir;
    my $realFSFound    = 0;
    while (not $realFSFound) {
        my $dfOutput = EBox::Sudo::root("df '$dirToCheck'");
        my $infoLine =$dfOutput->[1];
        chomp $infoLine;
        ($fs) = split '\s+', $infoLine;
        defined $fs or
            throw EBox::Exceptions::Internal("Cannot find file system for directory $dir");
        if (EBox::Sudo::fileTest('-d', $fs)) {
            # this is a bind fs..
            $dirToCheck = $fs;
        } else {
            $realFSFound = 1;
        }
    }

    return $fs;
}

1;