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.nio.file.Path;
040import java.util.ArrayList;
041import java.util.List;
042
043import org.hl7.fhir.utilities.Utilities;
044
045/**
046 * see security.md - manages access to the local file system by the FHIR HAPI Core library
047 * 
048 * By using accessPolicy, allowedPaths and accessor, a host java application can control 
049 * whether this library has direct access to the file system (and which paths it is allowed to access),
050 * or whether the host application provides controlled access, or whether no access is allowed at all
051 * (in which case other information providers need to be provided)
052 *  
053 * @author Grahame
054 *
055 */
056public class ManagedFileAccess {
057  
058  public interface IFileAccessor {
059    File file(String pathname) throws IOException;
060    FileInputStream inStream(String pathname);
061    FileOutputStream outStream(String pathname);
062    CSFile csfile(String pathname);
063    File[] listFiles(File file) throws IOException; // file would be returned from file() above
064  }
065
066  public enum FileAccessPolicy {
067    DIRECT, // open access to the local file system, though access can be restricted only to files under the paths in AllowedPaths
068    MANAGED, // no access except by the FileSystemProxyProvider
069    PROHIBITED, // no access at all to File() services
070  }
071
072  private static FileAccessPolicy accessPolicy = FileAccessPolicy.DIRECT; // for legacy reasons
073  private static List<String> allowedPaths = new ArrayList<>();
074  private static IFileAccessor accessor;
075  
076  
077  public static FileAccessPolicy getAccessPolicy() {
078    return accessPolicy;
079  }
080
081  public static void setAccessPolicy(FileAccessPolicy accessPolicy) {
082    ManagedFileAccess.accessPolicy = accessPolicy;
083  }
084
085  private static boolean inAllowedPaths(String pathname) {
086    if (allowedPaths.isEmpty()) {
087      return true;
088    }
089    for (String s : allowedPaths) {
090      if (pathname.startsWith(s)) {
091        return true;
092      }
093    }
094    return false;
095  }
096
097  /** 
098   * Open a file, conforming to local security policy 
099   **/
100  public static File file(String pathname) throws IOException {
101    switch (accessPolicy) {
102    case DIRECT:
103      if (!inAllowedPaths(pathname)) {
104        throw new IOException("The pathname '"+pathname+"' cannot be accessed by policy");
105      }
106      return new File(pathname);
107    case MANAGED:
108      return accessor.file(pathname);
109    case PROHIBITED:
110      throw new IOException("Access to files is not allowed by local security policy");
111    default:
112      throw new IOException("Internal Error");
113    }
114  }
115
116  public static File file(String path, String filepath) throws IOException {
117    switch (accessPolicy) {
118    case DIRECT:
119      if (!inAllowedPaths(path)) {
120        throw new IOException("The path '"+path+"' cannot be accessed by policy");
121      }
122      return new File(path, filepath);
123    case MANAGED:
124      return accessor.file(Utilities.path(path, filepath));
125    case PROHIBITED:
126      throw new IOException("Access to files is not allowed by local security policy");
127    default:
128      throw new IOException("Internal Error");
129    }
130  }
131
132  public static File file(File root, String filepath) throws IOException {
133    return file(root.getAbsolutePath(), filepath);
134  }
135
136  public static File csfile(String path, String filepath) throws IOException {
137    switch (accessPolicy) {
138      case DIRECT:
139        if (!inAllowedPaths(path)) {
140          throw new IOException("The path '"+path+"' cannot be accessed by policy");
141        }
142        return new CSFile(path, filepath);
143      case MANAGED:
144        return accessor.csfile(Utilities.path(path, filepath));
145      case PROHIBITED:
146        throw new IOException("Access to files is not allowed by local security policy");
147      default:
148        throw new IOException("Internal Error");
149    }
150  }
151
152  public static File csfile(File root, String filepath) throws IOException {
153    return csfile(root.getAbsolutePath(), filepath);
154  }
155
156  /**
157   * Open a FileInputStream, conforming to local security policy 
158   **/
159  public static FileInputStream inStream(String pathname) throws IOException {
160    switch (accessPolicy) {
161    case DIRECT:
162      if (!inAllowedPaths(pathname)) {
163        throw new IOException("The pathname '"+pathname+"' cannot be accessed by policy");
164      }
165      return new FileInputStream(pathname);
166    case MANAGED:
167      return accessor.inStream(pathname);
168    case PROHIBITED:
169      throw new IOException("Access to files is not allowed by local security policy");
170    default:
171      throw new IOException("Internal Error");
172    }
173  }
174
175  /**
176   * This doesn't do anything, but it's useful for it to exist, to make code inspection easier
177   * @param file
178   * @return
179   * @throws FileNotFoundException
180   */
181  public static FileInputStream inStream(File file) throws FileNotFoundException {
182    return new FileInputStream(file);
183  }
184
185  /** 
186   * Open a FileInputStream, conforming to local security policy 
187   **/
188
189  public static FileOutputStream outStream(String pathname) throws IOException {
190    switch (accessPolicy) {
191    case DIRECT:
192      if (!inAllowedPaths(pathname)) {
193        throw new IOException("The pathname '"+pathname+"' cannot be accessed by policy");
194      }
195      return new FileOutputStream(pathname);
196    case MANAGED:
197      return accessor.outStream(pathname);
198    case PROHIBITED:
199      throw new IOException("Access to files is not allowed by local security policy");
200    default:
201      throw new IOException("Internal Error");
202    }
203  }
204
205  /**
206   * This doesn't do anything, but it's useful for it to exist, to make code inspection easier
207   * @param file
208   * @return
209   * @throws FileNotFoundException
210   */
211  public static FileOutputStream outStream(File file) throws FileNotFoundException {
212    return new FileOutputStream(file);
213  }
214  
215  /** 
216   * same as file() but checks case is correct when opening directly (for consistency across platforms) 
217   **/
218  public static CSFile csfile(String pathname) throws IOException {
219    switch (accessPolicy) {
220    case DIRECT:
221      if (!inAllowedPaths(pathname)) {
222        throw new IOException("The pathname '"+pathname+"' cannot be accessed by policy");
223      }
224      return new CSFile(pathname);
225    case MANAGED:
226      return accessor.csfile(pathname);
227    case PROHIBITED:
228      throw new IOException("Access to files is not allowed by local security policy");
229    default:
230      throw new IOException("Internal Error");
231    }
232  }
233
234  public static File fromPath(Path path) throws IOException {
235    switch (accessPolicy) {
236    case DIRECT:
237      if (!inAllowedPaths(path.toString())) {
238        throw new IOException("The pathname '"+path.toString()+"' cannot be accessed by policy");
239      }
240      return path.toFile();
241    case MANAGED:
242      return accessor.file(path.toString());
243    case PROHIBITED:
244      throw new IOException("Access to files is not allowed by local security policy");
245    default:
246      throw new IOException("Internal Error");
247    }
248  }
249
250  public static File[] listFiles(File f) throws IOException {
251    switch (accessPolicy) {
252    case DIRECT:
253      if (!inAllowedPaths(f.getAbsolutePath())) {
254        throw new IOException("The pathname '"+f.getAbsolutePath()+"' cannot be accessed by policy");
255      }
256      return f.listFiles();
257    case MANAGED:
258      return accessor.listFiles(f);
259    case PROHIBITED:
260      throw new IOException("Access to files is not allowed by local security policy");
261    default:
262      throw new IOException("Internal Error");
263    }
264  }
265
266}