1 /*
2 * Copyright (C) 2009-2010, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 package org.eclipse.jgit.transport.resolver;
45
46 import java.io.File;
47 import java.io.IOException;
48 import java.util.Collection;
49 import java.util.Map;
50 import java.util.concurrent.ConcurrentHashMap;
51 import java.util.concurrent.CopyOnWriteArrayList;
52
53 import org.eclipse.jgit.errors.RepositoryNotFoundException;
54 import org.eclipse.jgit.lib.Constants;
55 import org.eclipse.jgit.lib.Repository;
56 import org.eclipse.jgit.lib.RepositoryCache;
57 import org.eclipse.jgit.lib.RepositoryCache.FileKey;
58 import org.eclipse.jgit.util.FS;
59
60 /**
61 * Default resolver serving from the local filesystem.
62 *
63 * @param <C>
64 * type of connection
65 */
66 public class FileResolver<C> implements RepositoryResolver<C> {
67 private volatile boolean exportAll;
68
69 private final Map<String, Repository> exports;
70
71 private final Collection<File> exportBase;
72
73 /** Initialize an empty file based resolver. */
74 public FileResolver() {
75 exports = new ConcurrentHashMap<String, Repository>();
76 exportBase = new CopyOnWriteArrayList<File>();
77 }
78
79 /**
80 * Create a new resolver for the given path.
81 *
82 * @param basePath
83 * the base path all repositories are rooted under.
84 * @param exportAll
85 * if true, exports all repositories, ignoring the check for the
86 * {@code git-daemon-export-ok} files.
87 */
88 public FileResolver(final File basePath, final boolean exportAll) {
89 this();
90 exportDirectory(basePath);
91 setExportAll(exportAll);
92 }
93
94 public Repository open(final C req, final String name)
95 throws RepositoryNotFoundException, ServiceNotEnabledException {
96 if (isUnreasonableName(name))
97 throw new RepositoryNotFoundException(name);
98
99 Repository db = exports.get(nameWithDotGit(name));
100 if (db != null) {
101 db.incrementOpen();
102 return db;
103 }
104
105 for (File base : exportBase) {
106 File dir = FileKey.resolve(new File(base, name), FS.DETECTED);
107 if (dir == null)
108 continue;
109
110 try {
111 FileKey key = FileKey.exact(dir, FS.DETECTED);
112 db = RepositoryCache.open(key, true);
113 } catch (IOException e) {
114 throw new RepositoryNotFoundException(name, e);
115 }
116
117 try {
118 if (isExportOk(req, name, db)) {
119 // We have to leak the open count to the caller, they
120 // are responsible for closing the repository if we
121 // complete successfully.
122 return db;
123 } else
124 throw new ServiceNotEnabledException();
125
126 } catch (RuntimeException e) {
127 db.close();
128 throw new RepositoryNotFoundException(name, e);
129
130 } catch (IOException e) {
131 db.close();
132 throw new RepositoryNotFoundException(name, e);
133
134 } catch (ServiceNotEnabledException e) {
135 db.close();
136 throw e;
137 }
138 }
139
140 if (exportBase.size() == 1) {
141 File dir = new File(exportBase.iterator().next(), name);
142 throw new RepositoryNotFoundException(name,
143 new RepositoryNotFoundException(dir));
144 }
145
146 throw new RepositoryNotFoundException(name);
147 }
148
149 /**
150 * @return false if <code>git-daemon-export-ok</code> is required to export
151 * a repository; true if <code>git-daemon-export-ok</code> is
152 * ignored.
153 * @see #setExportAll(boolean)
154 */
155 public boolean isExportAll() {
156 return exportAll;
157 }
158
159 /**
160 * Set whether or not to export all repositories.
161 * <p>
162 * If false (the default), repositories must have a
163 * <code>git-daemon-export-ok</code> file to be accessed through this
164 * daemon.
165 * <p>
166 * If true, all repositories are available through the daemon, whether or
167 * not <code>git-daemon-export-ok</code> exists.
168 *
169 * @param export
170 */
171 public void setExportAll(final boolean export) {
172 exportAll = export;
173 }
174
175 /**
176 * Add a single repository to the set that is exported by this daemon.
177 * <p>
178 * The existence (or lack-thereof) of <code>git-daemon-export-ok</code> is
179 * ignored by this method. The repository is always published.
180 *
181 * @param name
182 * name the repository will be published under.
183 * @param db
184 * the repository instance.
185 */
186 public void exportRepository(String name, Repository db) {
187 exports.put(nameWithDotGit(name), db);
188 }
189
190 /**
191 * Recursively export all Git repositories within a directory.
192 *
193 * @param dir
194 * the directory to export. This directory must not itself be a
195 * git repository, but any directory below it which has a file
196 * named <code>git-daemon-export-ok</code> will be published.
197 */
198 public void exportDirectory(final File dir) {
199 exportBase.add(dir);
200 }
201
202 /**
203 * Check if this repository can be served.
204 * <p>
205 * The default implementation of this method returns true only if either
206 * {@link #isExportAll()} is true, or the {@code git-daemon-export-ok} file
207 * is present in the repository's directory.
208 *
209 * @param req
210 * the current HTTP request.
211 * @param repositoryName
212 * name of the repository, as present in the URL.
213 * @param db
214 * the opened repository instance.
215 * @return true if the repository is accessible; false if not.
216 * @throws IOException
217 * the repository could not be accessed, the caller will claim
218 * the repository does not exist.
219 */
220 protected boolean isExportOk(C req, String repositoryName, Repository db)
221 throws IOException {
222 if (isExportAll())
223 return true;
224 else if (db.getDirectory() != null)
225 return new File(db.getDirectory(), "git-daemon-export-ok").exists(); //$NON-NLS-1$
226 else
227 return false;
228 }
229
230 private static String nameWithDotGit(String name) {
231 if (name.endsWith(Constants.DOT_GIT_EXT))
232 return name;
233 return name + Constants.DOT_GIT_EXT;
234 }
235
236 private static boolean isUnreasonableName(final String name) {
237 if (name.length() == 0)
238 return true; // no empty paths
239
240 if (name.indexOf('\\') >= 0)
241 return true; // no windows/dos style paths
242 if (new File(name).isAbsolute())
243 return true; // no absolute paths
244
245 if (name.startsWith("../")) //$NON-NLS-1$
246 return true; // no "l../etc/passwd"
247 if (name.contains("/../")) //$NON-NLS-1$
248 return true; // no "foo/../etc/passwd"
249 if (name.contains("/./")) //$NON-NLS-1$
250 return true; // "foo/./foo" is insane to ask
251 if (name.contains("//")) //$NON-NLS-1$
252 return true; // double slashes is sloppy, don't use it
253
254 return false; // is a reasonable name
255 }
256 }