1 /*
2 Copyright (c) 2011 James Ahlborn
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package com.healthmarketscience.jackcess.util;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.nio.file.AccessDeniedException;
22
23 import com.healthmarketscience.jackcess.Database;
24 import com.healthmarketscience.jackcess.DatabaseBuilder;
25 import com.healthmarketscience.jackcess.impl.DatabaseImpl;
26
27 /**
28 * Resolver for linked databases.
29 *
30 * @author James Ahlborn
31 * @usage _intermediate_class_
32 */
33 @FunctionalInterface
34 public interface LinkResolver
35 {
36 /**
37 * Link resolver which opens whatever file name the linking database
38 * specifies, including absolute, network (UNC) and device paths.
39 * <p>
40 * Since the linkee file name comes from the database file itself, a
41 * database from an untrusted source can use this resolver to make jackcess
42 * read an arbitrary local file or open a network connection. Only use this
43 * resolver when the linked database file names can be trusted.
44 * @usage _general_field_
45 */
46 public static final LinkResolver UNRESTRICTED = (linkerDb, linkeeFileName) -> {
47 // if linker is read-only, open linkee read-only
48 boolean readOnly = ((linkerDb instanceof DatabaseImpl) &&
49 ((DatabaseImpl)linkerDb).isReadOnly());
50 return new DatabaseBuilder(new File(linkeeFileName))
51 .setReadOnly(readOnly).open();
52 };
53
54 /**
55 * Link resolver which refuses to open any linked database.
56 * <p>
57 * This is the resolver used if none is provided. Enabling the
58 * {@value com.healthmarketscience.jackcess.Database#ALLOW_LINK_RESOLUTION_PROPERTY}
59 * system property selects {@link #UNRESTRICTED} instead. An application
60 * which uses linked databases must configure either {@link #UNRESTRICTED}
61 * or a resolver which enforces its own policy for which file names are
62 * acceptable.
63 * @usage _general_field_
64 */
65 public static final LinkResolver DEFAULT = (linkerDb, linkeeFileName) -> {
66 throw new AccessDeniedException(
67 linkeeFileName, null,
68 "Linked database resolution is disabled. Configure a LinkResolver " +
69 "on the Database (e.g. LinkResolver.UNRESTRICTED for trusted file " +
70 "names) to enable it");
71 };
72
73 /**
74 * Returns the appropriate Database instance for the linkeeFileName from the
75 * given linkerDb.
76 */
77 public Databasem/healthmarketscience/jackcess/Database.html#Database">Database resolveLinkedDatabase(Database linkerDb, String linkeeFileName)
78 throws IOException;
79 }