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  public static File file(File root, String filepath) throws IOException {
134    return file(root.getAbsolutePath(), filepath);
135  }
136  /** 
137   * Open a FileInputStream, conforming to local security policy 
138   **/
139  public static FileInputStream inStream(String pathname) throws IOException {
140    switch (accessPolicy) {
141    case DIRECT:
142      if (!inAllowedPaths(pathname)) {
143        throw new IOException("The pathname '"+pathname+"' cannot be accessed by policy");
144      }
145      return new FileInputStream(pathname);
146    case MANAGED:
147      return accessor.inStream(pathname);
148    case PROHIBITED:
149      throw new IOException("Access to files is not allowed by local security policy");
150    default:
151      throw new IOException("Internal Error");
152    }
153  }
154
155  /**
156   * This doesn't do anything, but it's useful for it to exist, to make code inspection easier
157   * @param file
158   * @return
159   * @throws FileNotFoundException
160   */
161  public static FileInputStream inStream(File file) throws FileNotFoundException {
162    return new FileInputStream(file);
163  }
164
165  /** 
166   * Open a FileInputStream, conforming to local security policy 
167   **/
168
169  public static FileOutputStream outStream(String pathname) throws IOException {
170    switch (accessPolicy) {
171    case DIRECT:
172      if (!inAllowedPaths(pathname)) {
173        throw new IOException("The pathname '"+pathname+"' cannot be accessed by policy");
174      }
175      return new FileOutputStream(pathname);
176    case MANAGED:
177      return accessor.outStream(pathname);
178    case PROHIBITED:
179      throw new IOException("Access to files is not allowed by local security policy");
180    default:
181      throw new IOException("Internal Error");
182    }
183  }
184
185  /**
186   * This doesn't do anything, but it's useful for it to exist, to make code inspection easier
187   * @param file
188   * @return
189   * @throws FileNotFoundException
190   */
191  public static FileOutputStream outStream(File file) throws FileNotFoundException {
192    return new FileOutputStream(file);
193  }
194  
195  /** 
196   * same as file() but checks case is correct when opening directly (for consistency across platforms) 
197   **/
198  public static CSFile csfile(String pathname) throws IOException {
199    switch (accessPolicy) {
200    case DIRECT:
201      if (!inAllowedPaths(pathname)) {
202        throw new IOException("The pathname '"+pathname+"' cannot be accessed by policy");
203      }
204      return new CSFile(pathname);
205    case MANAGED:
206      return accessor.csfile(pathname);
207    case PROHIBITED:
208      throw new IOException("Access to files is not allowed by local security policy");
209    default:
210      throw new IOException("Internal Error");
211    }
212  }
213
214  public static File fromPath(Path path) throws IOException {
215    switch (accessPolicy) {
216    case DIRECT:
217      if (!inAllowedPaths(path.toString())) {
218        throw new IOException("The pathname '"+path.toString()+"' cannot be accessed by policy");
219      }
220      return path.toFile();
221    case MANAGED:
222      return accessor.file(path.toString());
223    case PROHIBITED:
224      throw new IOException("Access to files is not allowed by local security policy");
225    default:
226      throw new IOException("Internal Error");
227    }
228  }
229
230  public static File[] listFiles(File f) throws IOException {
231    switch (accessPolicy) {
232    case DIRECT:
233      if (!inAllowedPaths(f.getAbsolutePath())) {
234        throw new IOException("The pathname '"+f.getAbsolutePath()+"' cannot be accessed by policy");
235      }
236      return f.listFiles();
237    case MANAGED:
238      return accessor.listFiles(f);
239    case PROHIBITED:
240      throw new IOException("Access to files is not allowed by local security policy");
241    default:
242      throw new IOException("Internal Error");
243    }
244  }
245
246}