001: /*
002: * Copyright (c) 2003 The Visigoth Software Society. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowledgement:
019: * "This product includes software developed by the
020: * Visigoth Software Society (http://www.visigoths.org/)."
021: * Alternately, this acknowledgement may appear in the software itself,
022: * if and wherever such third-party acknowledgements normally appear.
023: *
024: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
025: * project contributors may be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact visigoths@visigoths.org.
028: *
029: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
030: * nor may "FreeMarker" or "Visigoth" appear in their names
031: * without prior written permission of the Visigoth Software Society.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Visigoth Software Society. For more
049: * information on the Visigoth Software Society, please see
050: * http://www.visigoths.org/
051: */
052:
053: package freemarker.cache;
054:
055: import java.io.File;
056: import java.io.FileInputStream;
057: import java.io.FileNotFoundException;
058: import java.io.IOException;
059: import java.io.InputStreamReader;
060: import java.io.Reader;
061: import java.security.AccessController;
062: import java.security.PrivilegedAction;
063: import java.security.PrivilegedActionException;
064: import java.security.PrivilegedExceptionAction;
065:
066: import freemarker.template.utility.SecurityUtilities;
067:
068: /**
069: * A {@link TemplateLoader} that uses files in a specified directory as the
070: * source of templates. If contains security checks that will prevent it
071: * serving templates outside the template directory (like <code><include /etc/passwd></code>.
072: * It compares canonical paths for this, so templates that are symbolically
073: * linked into the template directory from outside of it won't work either.
074: * @author Attila Szegedi, szegedia at freemail dot hu
075: * @version $Id: FileTemplateLoader.java,v 1.26 2004/03/29 08:06:22 szegedia Exp $
076: */
077: public class FileTemplateLoader implements TemplateLoader {
078: private static final boolean SEP_IS_SLASH = File.separatorChar == '/';
079: public final File baseDir;
080: private final String canonicalPath;
081:
082: /**
083: * Creates a new file template cache that will use the current directory
084: * (the value of the system property <code>user.dir</code> as the base
085: * directory for loading templates.
086: */
087: public FileTemplateLoader() throws IOException {
088: this (new File(SecurityUtilities.getSystemProperty("user.dir")));
089: }
090:
091: /**
092: * Creates a new file template loader that will use the specified directory
093: * as the base directory for loading templates.
094: * @param baseDir the base directory for loading templates
095: */
096: public FileTemplateLoader(final File baseDir) throws IOException {
097: try {
098: Object[] retval = (Object[]) AccessController
099: .doPrivileged(new PrivilegedExceptionAction() {
100: public Object run() throws IOException {
101: if (!baseDir.exists()) {
102: throw new FileNotFoundException(baseDir
103: + " does not exist.");
104: }
105: if (!baseDir.isDirectory()) {
106: throw new IOException(baseDir
107: + " is not a directory.");
108: }
109: Object[] retval = new Object[2];
110: retval[0] = baseDir.getCanonicalFile();
111: retval[1] = ((File) retval[0]).getPath()
112: + File.separatorChar;
113: return retval;
114: }
115: });
116: this .baseDir = (File) retval[0];
117: this .canonicalPath = (String) retval[1];
118: } catch (PrivilegedActionException e) {
119: throw (IOException) e.getException();
120: }
121: }
122:
123: public Object findTemplateSource(final String name)
124: throws IOException {
125: try {
126: return AccessController
127: .doPrivileged(new PrivilegedExceptionAction() {
128: public Object run() throws IOException {
129: File source = new File(baseDir,
130: SEP_IS_SLASH ? name : name.replace(
131: '/', File.separatorChar));
132: if (!source.isFile()) {
133: return null;
134: }
135: // Security check for inadvertently returning something outside the
136: // template directory.
137: String normalized = source
138: .getCanonicalPath();
139: if (normalized.startsWith(canonicalPath)) {
140: return source;
141: }
142: throw new SecurityException(normalized
143: + " doesn't start with "
144: + canonicalPath);
145: }
146: });
147: } catch (PrivilegedActionException e) {
148: throw (IOException) e.getException();
149: }
150: }
151:
152: public long getLastModified(final Object templateSource) {
153: return ((Long) (AccessController
154: .doPrivileged(new PrivilegedAction() {
155: public Object run() {
156: return new Long(((File) templateSource)
157: .lastModified());
158: }
159: }))).longValue();
160:
161: }
162:
163: public Reader getReader(final Object templateSource,
164: final String encoding) throws IOException {
165: try {
166: return (Reader) AccessController
167: .doPrivileged(new PrivilegedExceptionAction() {
168: public Object run() throws IOException {
169: if (!(templateSource instanceof File)) {
170: throw new IllegalArgumentException(
171: "templateSource is a: "
172: + templateSource
173: .getClass()
174: .getName());
175: }
176: return new InputStreamReader(
177: new FileInputStream(
178: (File) templateSource),
179: encoding);
180: }
181: });
182: } catch (PrivilegedActionException e) {
183: throw (IOException) e.getException();
184: }
185: }
186:
187: public void closeTemplateSource(Object templateSource) {
188: // Do nothing.
189: }
190: }
|