File Coverage

lib/File/Policy.pm
Criterion Covered Total %
statement 29 30 96.6
branch 4 6 66.6
condition n/a
subroutine 5 6 83.3
pod 0 2 0.0
total 38 44 86.3


line stmt bran cond sub pod time code
1             ###############################################################################
2             # Purpose : Interface to File I/O policies
3             # Author : John Alden
4             # Created : March 2005
5             # CVS : $Id: Policy.pm,v 1.5 2005/05/18 15:58:21 johna Exp $
6             ###############################################################################
7              
8             package File::Policy;
9              
10 2     2   3278 use strict;
  2         4  
  2         76  
11 2     2   11 use vars qw($VERSION $Aliased);
  2         4  
  2         2243  
12              
13             $VERSION = sprintf"%d.%03d", q$Revision: 1.5 $ =~ /: (\d+)\.(\d+)/;
14             $Aliased = 0;
15              
16             sub import {
17 4     4   95 my $package = shift;
18            
19             #Allow default implementation to be set in a config module
20 4         8 my $default_implementation = 'Default';
21 4         7 eval {
22 4         939 require File::Policy::Config;
23 2         7 $default_implementation = &File::Policy::Config::IMPLEMENTATION;
24             };
25            
26 4         15 my $impl = $default_implementation; #Currently no way of overriding default - could be extended later
27 4 50       28 die("Invalid File::Policy package - $impl") unless($impl =~ /^[\w:]+$/); #Sanitize package name
28 4         10 $impl = "File::Policy::" . $impl;
29 4         21 TRACE("File::Policy Implementation: ". $impl);
30            
31             #Export symbols to caller
32 4         183 eval "require $impl";
33 4 50       25 die("Unable to compile $impl - $@") if($@);
34 4         7 ++ local $Exporter::ExportLevel; #Bypass this module in caller()
35 4         314 $impl->import(@_);
36            
37             #Alias symbols into this package too for fully qualified access
38 4 100       41 unless($Aliased) {
39 2         6 foreach(qw(check_safe get_log_dir get_temp_dir)) {
40 2     2   26 no strict 'refs';
  2         4  
  2         349  
41 6         7 *{"File::Policy::$_"} = *{"${impl}::$_"};
  6         31  
  6         19  
42             }
43 2         34 $Aliased = 1;
44             }
45             }
46              
47             #Stubs compatible with Log::Trace
48 4     4 0 6 sub TRACE{}
49 0     0 0   sub DUMP{}
50              
51             1;
52              
53             =head1 NAME
54              
55             File::Policy - Site policy for file I/O functions
56              
57             =head1 SYNOPSIS
58              
59             use File::Policy;
60             use File::Policy qw/check_safe/; # to import a specific subroutine
61             use File::Policy qw/:all/; # to import all subroutines
62              
63             #Checking I/O policy
64             check_safe($filename, 'r');
65             check_safe($filename, 'w');
66              
67             #Preferred directory locations
68             $logdir = get_log_dir();
69             $tmpdir = get_temp_dir();
70              
71             =head1 DESCRIPTION
72              
73             This defines the policy for file I/O with modules such as File::Slurp::WithinPolicy.
74             The purpose is to allow systems administrators to define locations and restrictions
75             for applications' file I/O and give app developers a policy to follow. Note that the
76             module doesn't ENFORCE the policy - application developers can choose to ignore it
77             (and systems administrators can choose not to install their applications if they do!).
78              
79             You may control which policy gets applied by creating a File::Policy::Config module
80             with an IMPLEMENTATION constant. You may write your own policy as a module within the File::Policy:: namespace.
81              
82             By default (if no File::Policy::Config is present), the File::Policy::Default policy gets applied which doesn't impose
83             any restrictions and provides reasonable default locations for temporary and log files.
84              
85             The motivation behind this module was a standard, flexible approach to allow a site wide file policy to be defined. This will be most useful in large environments where a few sysadmins are responsible for code written by many other people. Simply ensuring that submitted code calls check_safe() ensures file access is sane, reducing the amount of effort required to do a security audit.
86              
87             If your code is not security audit'd, or you are the only developer at your site, this might be overkill. However you may consider it good practise regardless and protection against paths in your code getting corrupted accidently or maliciously in the future.
88              
89             There are two major benefits of using this module. One, sites that do implement a policy can more easily integrate your code in a standard way. If you have a file policy at your site, you can apply different policies (via File::Policy::Config) in different environments (production, integration test, development) and the appropriate policy is automatically applied without having to change your code or configs.
90              
91             =head1 FUNCTIONS
92              
93             =over 4
94              
95             =item check_safe
96              
97             check_safe( FILENAME , MODE );
98              
99             Checks FILENAME is safe - dies if not. MODE is r (read) or w (write).
100              
101             =item get_temp_dir
102              
103             $temporary_directory = get_temp_dir();
104              
105             Returns the path to temporary directory. Note that any return value will have been cleared of a trailing slash.
106              
107             =item get_log_dir
108              
109             $log_directory = get_log_dir();
110              
111             Returns the path to log directory. Note that any return value will have been cleared of a trailing slash.
112              
113             =back
114              
115             =head1 DEFINING YOUR OWN POLICY
116              
117             To implement your own custom policy
118              
119             cp File/Policy/Default.pm File/Policy/YourPolicy.pm
120            
121             and modify YourPolicy accordingly. Then, create File/Policy/Config.pm contaning:
122              
123             use constant IMPLEMENTATION => 'YourPolicy';
124              
125             Now having used File::Policy, calling check_safe in your scripts will enforce your policy
126             (as well as give you access to log and temp paths in locations you recommend).
127              
128             =head1 SEE ALSO
129              
130             L, L
131              
132             =head1 VERSION
133              
134             $Revision: 1.5 $ on $Date: 2005/05/18 15:58:21 $ by $Author: johna $
135              
136             =head1 AUTHOR
137              
138             John Alden
139              
140             =head1 COPYRIGHT
141              
142             (c) BBC 2005. This program is free software; you can redistribute it and/or modify it under the GNU GPL.
143              
144             See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
145              
146             =cut