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<>();
76 exportBase = new CopyOnWriteArrayList<>();
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 @Override
95 public Repository open(final C req, final String name)
96 throws RepositoryNotFoundException, ServiceNotEnabledException {
97 if (isUnreasonableName(name))
98 throw new RepositoryNotFoundException(name);
99
100 Repository db = exports.get(nameWithDotGit(name));
101 if (db != null) {
102 db.incrementOpen();
103 return db;
104 }
105
106 for (File base : exportBase) {
107 File dir = FileKey.resolve(new File(base, name), FS.DETECTED);
108 if (dir == null)
109 continue;
110
111 try {
112 FileKey key = FileKey.exact(dir, FS.DETECTED);
113 db = RepositoryCache.open(key, true);
114 } catch (IOException e) {
115 throw new RepositoryNotFoundException(name, e);
116 }
117
118 try {
119 if (isExportOk(req, name, db)) {
120 // We have to leak the open count to the caller, they
121 // are responsible for closing the repository if we
122 // complete successfully.
123 return db;
124 } else
125 throw new ServiceNotEnabledException();
126
127 } catch (RuntimeException e) {
128 db.close();
129 throw new RepositoryNotFoundException(name, e);
130
131 } catch (IOException e) {
132 db.close();
133 throw new RepositoryNotFoundException(name, e);
134
135 } catch (ServiceNotEnabledException e) {
136 db.close();
137 throw e;
138 }
139 }
140
141 if (exportBase.size() == 1) {
142 File dir = new File(exportBase.iterator().next(), name);
143 throw new RepositoryNotFoundException(name,
144 new RepositoryNotFoundException(dir));
145 }
146
147 throw new RepositoryNotFoundException(name);
148 }
149
150 /**
151 * @return false if <code>git-daemon-export-ok</code> is required to export
152 * a repository; true if <code>git-daemon-export-ok</code> is
153 * ignored.
154 * @see #setExportAll(boolean)
155 */
156 public boolean isExportAll() {
157 return exportAll;
158 }
159
160 /**
161 * Set whether or not to export all repositories.
162 * <p>
163 * If false (the default), repositories must have a
164 * <code>git-daemon-export-ok</code> file to be accessed through this
165 * daemon.
166 * <p>
167 * If true, all repositories are available through the daemon, whether or
168 * not <code>git-daemon-export-ok</code> exists.
169 *
170 * @param export
171 */
172 public void setExportAll(final boolean export) {
173 exportAll = export;
174 }
175
176 /**
177 * Add a single repository to the set that is exported by this daemon.
178 * <p>
179 * The existence (or lack-thereof) of <code>git-daemon-export-ok</code> is
180 * ignored by this method. The repository is always published.
181 *
182 * @param name
183 * name the repository will be published under.
184 * @param db
185 * the repository instance.
186 */
187 public void exportRepository(String name, Repository db) {
188 exports.put(nameWithDotGit(name), db);
189 }
190
191 /**
192 * Recursively export all Git repositories within a directory.
193 *
194 * @param dir
195 * the directory to export. This directory must not itself be a
196 * git repository, but any directory below it which has a file
197 * named <code>git-daemon-export-ok</code> will be published.
198 */
199 public void exportDirectory(final File dir) {
200 exportBase.add(dir);
201 }
202
203 /**
204 * Check if this repository can be served.
205 * <p>
206 * The default implementation of this method returns true only if either
207 * {@link #isExportAll()} is true, or the {@code git-daemon-export-ok} file
208 * is present in the repository's directory.
209 *
210 * @param req
211 * the current HTTP request.
212 * @param repositoryName
213 * name of the repository, as present in the URL.
214 * @param db
215 * the opened repository instance.
216 * @return true if the repository is accessible; false if not.
217 * @throws IOException
218 * the repository could not be accessed, the caller will claim
219 * the repository does not exist.
220 */
221 protected boolean isExportOk(C req, String repositoryName, Repository db)
222 throws IOException {
223 if (isExportAll())
224 return true;
225 else if (db.getDirectory() != null)
226 return new File(db.getDirectory(), "git-daemon-export-ok").exists(); //$NON-NLS-1$
227 else
228 return false;
229 }
230
231 private static String nameWithDotGit(String name) {
232 if (name.endsWith(Constants.DOT_GIT_EXT))
233 return name;
234 return name + Constants.DOT_GIT_EXT;
235 }
236
237 private static boolean isUnreasonableName(final String name) {
238 if (name.length() == 0)
239 return true; // no empty paths
240
241 if (name.indexOf('\\') >= 0)
242 return true; // no windows/dos style paths
243 if (new File(name).isAbsolute())
244 return true; // no absolute paths
245
246 if (name.startsWith("../")) //$NON-NLS-1$
247 return true; // no "l../etc/passwd"
248 if (name.contains("/../")) //$NON-NLS-1$
249 return true; // no "foo/../etc/passwd"
250 if (name.contains("/./")) //$NON-NLS-1$
251 return true; // "foo/./foo" is insane to ask
252 if (name.contains("//")) //$NON-NLS-1$
253 return true; // double slashes is sloppy, don't use it
254
255 return false; // is a reasonable name
256 }
257 }