001package org.hl7.fhir.utilities.filesystem;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033
034import java.io.File;
035import java.io.FileInputStream;
036import java.io.FileNotFoundException;
037import java.io.FileOutputStream;
038import java.io.IOException;
039import java.io.InputStream;
040import java.nio.file.Path;
041import java.util.ArrayList;
042import java.util.List;
043
044import org.hl7.fhir.utilities.Utilities;
045
046/**
047 * see security.md - manages access to the local file system by the FHIR HAPI Core library
048 * 
049 * By using accessPolicy, allowedPaths and accessor, a host java application can control 
050 * whether this library has direct access to the file system (and which paths it is allowed to access),
051 * or whether the host application provides controlled access, or whether no access is allowed at all
052 * (in which case other information providers need to be provided)
053 *  
054 * @author Grahame
055 *
056 */
057public class ManagedFileAccess {
058  
059  public interface IFileAccessor {
060    File file(String pathname) throws IOException;
061    FileInputStream inStream(String pathname);
062    FileOutputStream outStream(String pathname);
063    CSFile csfile(String pathname);
064    File[] listFiles(File file) throws IOException; // file would be returned from file() above
065  }
066
067  public enum FileAccessPolicy {
068    DIRECT, // open access to the local file system, though access can be restricted only to files under the paths in AllowedPaths
069    MANAGED, // no access except by the FileSystemProxyProvider
070    PROHIBITED, // no access at all to File() services
071  }
072
073  private static FileAccessPolicy accessPolicy = FileAccessPolicy.DIRECT; // for legacy reasons
074  private static List<String> allowedPaths = new ArrayList<>();
075  private static IFileAccessor accessor;
076  
077  
078  public static FileAccessPolicy getAccessPolicy() {
079    return accessPolicy;
080  }
081
082  public static void setAccessPolicy(FileAccessPolicy accessPolicy) {
083    ManagedFileAccess.accessPolicy = accessPolicy;
084  }
085
086  private static boolean inAllowedPaths(String pathname) {
087    if (allowedPaths.isEmpty()) {
088      return true;
089    }
090    for (String s : allowedPaths) {
091      if (pathname.startsWith(s)) {
092        return true;
093      }
094    }
095    return false;
096  }
097
098  /** 
099   * Open a file, conforming to local security policy 
100   **/
101  public static File file(String pathname) throws IOException {
102    switch (accessPolicy) {
103    case DIRECT:
104      if (!inAllowedPaths(pathname)) {
105        throw new IOException("The pathname '"+pathname+"' cannot be accessed by policy");
106      }
107      return new File(pathname);
108    case MANAGED:
109      return accessor.file(pathname);
110    case PROHIBITED:
111      throw new IOException("Access to files is not allowed by local security policy");
112    default:
113      throw new IOException("Internal Error");
114    }
115  }
116
117  public static File file(String path, String filepath) throws IOException {
118    switch (accessPolicy) {
119    case DIRECT:
120      if (!inAllowedPaths(path)) {
121        throw new IOException("The path '"+path+"' cannot be accessed by policy");
122      }
123      return new File(path, filepath);
124    case MANAGED:
125      return accessor.file(Utilities.path(path, filepath));
126    case PROHIBITED:
127      throw new IOException("Access to files is not allowed by local security policy");
128    default:
129      throw new IOException("Internal Error");
130    }
131  }
132  /** 
133   * Open a FileInputStream, conforming to local security policy 
134   **/
135  public static FileInputStream inStream(String pathname) throws IOException {
136    switch (accessPolicy) {
137    case DIRECT:
138      if (!inAllowedPaths(pathname)) {
139        throw new IOException("The pathname '"+pathname+"' cannot be accessed by policy");
140      }
141      return new FileInputStream(pathname);
142    case MANAGED:
143      return accessor.inStream(pathname);
144    case PROHIBITED:
145      throw new IOException("Access to files is not allowed by local security policy");
146    default:
147      throw new IOException("Internal Error");
148    }
149  }
150
151  /**
152   * This doesn't do anything, but it's useful for it to exist, to make code inspection easier
153   * @param file
154   * @return
155   * @throws FileNotFoundException
156   */
157  public static FileInputStream inStream(File file) throws FileNotFoundException {
158    return new FileInputStream(file);
159  }
160
161  /** 
162   * Open a FileInputStream, conforming to local security policy 
163   **/
164
165  public static FileOutputStream outStream(String pathname) throws IOException {
166    switch (accessPolicy) {
167    case DIRECT:
168      if (!inAllowedPaths(pathname)) {
169        throw new IOException("The pathname '"+pathname+"' cannot be accessed by policy");
170      }
171      return new FileOutputStream(pathname);
172    case MANAGED:
173      return accessor.outStream(pathname);
174    case PROHIBITED:
175      throw new IOException("Access to files is not allowed by local security policy");
176    default:
177      throw new IOException("Internal Error");
178    }
179  }
180
181  /**
182   * This doesn't do anything, but it's useful for it to exist, to make code inspection easier
183   * @param file
184   * @return
185   * @throws FileNotFoundException
186   */
187  public static FileOutputStream outStream(File file) throws FileNotFoundException {
188    return new FileOutputStream(file);
189  }
190  
191  /** 
192   * same as file() but checks case is correct when opening directly (for consistency across platforms) 
193   **/
194  public static CSFile csfile(String pathname) throws IOException {
195    switch (accessPolicy) {
196    case DIRECT:
197      if (!inAllowedPaths(pathname)) {
198        throw new IOException("The pathname '"+pathname+"' cannot be accessed by policy");
199      }
200      return new CSFile(pathname);
201    case MANAGED:
202      return accessor.csfile(pathname);
203    case PROHIBITED:
204      throw new IOException("Access to files is not allowed by local security policy");
205    default:
206      throw new IOException("Internal Error");
207    }
208  }
209
210  public static File fromPath(Path path) throws IOException {
211    switch (accessPolicy) {
212    case DIRECT:
213      if (!inAllowedPaths(path.toString())) {
214        throw new IOException("The pathname '"+path.toString()+"' cannot be accessed by policy");
215      }
216      return path.toFile();
217    case MANAGED:
218      return accessor.file(path.toString());
219    case PROHIBITED:
220      throw new IOException("Access to files is not allowed by local security policy");
221    default:
222      throw new IOException("Internal Error");
223    }
224  }
225
226  public static File[] listFiles(File f) throws IOException {
227    switch (accessPolicy) {
228    case DIRECT:
229      if (!inAllowedPaths(f.getAbsolutePath())) {
230        throw new IOException("The pathname '"+f.getAbsolutePath()+"' cannot be accessed by policy");
231      }
232      return f.listFiles();
233    case MANAGED:
234      return accessor.listFiles(f);
235    case PROHIBITED:
236      throw new IOException("Access to files is not allowed by local security policy");
237    default:
238      throw new IOException("Internal Error");
239    }
240  }
241
242}