You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileResolver.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. package org.eclipse.jgit.transport.resolver;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.util.Collection;
  47. import java.util.Map;
  48. import java.util.concurrent.ConcurrentHashMap;
  49. import java.util.concurrent.CopyOnWriteArrayList;
  50. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.Repository;
  53. import org.eclipse.jgit.lib.RepositoryCache;
  54. import org.eclipse.jgit.lib.RepositoryCache.FileKey;
  55. import org.eclipse.jgit.util.FS;
  56. /**
  57. * Default resolver serving from the local filesystem.
  58. *
  59. * @param <C>
  60. * type of connection
  61. */
  62. public class FileResolver<C> implements RepositoryResolver<C> {
  63. private volatile boolean exportAll;
  64. private final Map<String, Repository> exports;
  65. private final Collection<File> exportBase;
  66. /**
  67. * Initialize an empty file based resolver.
  68. */
  69. public FileResolver() {
  70. exports = new ConcurrentHashMap<>();
  71. exportBase = new CopyOnWriteArrayList<>();
  72. }
  73. /**
  74. * Create a new resolver for the given path.
  75. *
  76. * @param basePath
  77. * the base path all repositories are rooted under.
  78. * @param exportAll
  79. * if true, exports all repositories, ignoring the check for the
  80. * {@code git-daemon-export-ok} files.
  81. */
  82. public FileResolver(File basePath, boolean exportAll) {
  83. this();
  84. exportDirectory(basePath);
  85. setExportAll(exportAll);
  86. }
  87. /** {@inheritDoc} */
  88. @Override
  89. public Repository open(C req, String name)
  90. throws RepositoryNotFoundException, ServiceNotEnabledException {
  91. if (isUnreasonableName(name))
  92. throw new RepositoryNotFoundException(name);
  93. Repository db = exports.get(nameWithDotGit(name));
  94. if (db != null) {
  95. db.incrementOpen();
  96. return db;
  97. }
  98. for (File base : exportBase) {
  99. File dir = FileKey.resolve(new File(base, name), FS.DETECTED);
  100. if (dir == null)
  101. continue;
  102. try {
  103. FileKey key = FileKey.exact(dir, FS.DETECTED);
  104. db = RepositoryCache.open(key, true);
  105. } catch (IOException e) {
  106. throw new RepositoryNotFoundException(name, e);
  107. }
  108. try {
  109. if (isExportOk(req, name, db)) {
  110. // We have to leak the open count to the caller, they
  111. // are responsible for closing the repository if we
  112. // complete successfully.
  113. return db;
  114. } else
  115. throw new ServiceNotEnabledException();
  116. } catch (RuntimeException | IOException e) {
  117. db.close();
  118. throw new RepositoryNotFoundException(name, e);
  119. } catch (ServiceNotEnabledException e) {
  120. db.close();
  121. throw e;
  122. }
  123. }
  124. if (exportBase.size() == 1) {
  125. File dir = new File(exportBase.iterator().next(), name);
  126. throw new RepositoryNotFoundException(name,
  127. new RepositoryNotFoundException(dir));
  128. }
  129. throw new RepositoryNotFoundException(name);
  130. }
  131. /**
  132. * Whether <code>git-daemon-export-ok</code> is required to export a
  133. * repository
  134. *
  135. * @return false if <code>git-daemon-export-ok</code> is required to export
  136. * a repository; true if <code>git-daemon-export-ok</code> is
  137. * ignored.
  138. * @see #setExportAll(boolean)
  139. */
  140. public boolean isExportAll() {
  141. return exportAll;
  142. }
  143. /**
  144. * Set whether or not to export all repositories.
  145. * <p>
  146. * If false (the default), repositories must have a
  147. * <code>git-daemon-export-ok</code> file to be accessed through this
  148. * daemon.
  149. * <p>
  150. * If true, all repositories are available through the daemon, whether or
  151. * not <code>git-daemon-export-ok</code> exists.
  152. *
  153. * @param export a boolean.
  154. */
  155. public void setExportAll(boolean export) {
  156. exportAll = export;
  157. }
  158. /**
  159. * Add a single repository to the set that is exported by this daemon.
  160. * <p>
  161. * The existence (or lack-thereof) of <code>git-daemon-export-ok</code> is
  162. * ignored by this method. The repository is always published.
  163. *
  164. * @param name
  165. * name the repository will be published under.
  166. * @param db
  167. * the repository instance.
  168. */
  169. public void exportRepository(String name, Repository db) {
  170. exports.put(nameWithDotGit(name), db);
  171. }
  172. /**
  173. * Recursively export all Git repositories within a directory.
  174. *
  175. * @param dir
  176. * the directory to export. This directory must not itself be a
  177. * git repository, but any directory below it which has a file
  178. * named <code>git-daemon-export-ok</code> will be published.
  179. */
  180. public void exportDirectory(File dir) {
  181. exportBase.add(dir);
  182. }
  183. /**
  184. * Check if this repository can be served.
  185. * <p>
  186. * The default implementation of this method returns true only if either
  187. * {@link #isExportAll()} is true, or the {@code git-daemon-export-ok} file
  188. * is present in the repository's directory.
  189. *
  190. * @param req
  191. * the current HTTP request.
  192. * @param repositoryName
  193. * name of the repository, as present in the URL.
  194. * @param db
  195. * the opened repository instance.
  196. * @return true if the repository is accessible; false if not.
  197. * @throws java.io.IOException
  198. * the repository could not be accessed, the caller will claim
  199. * the repository does not exist.
  200. */
  201. protected boolean isExportOk(C req, String repositoryName, Repository db)
  202. throws IOException {
  203. if (isExportAll())
  204. return true;
  205. else if (db.getDirectory() != null)
  206. return new File(db.getDirectory(), "git-daemon-export-ok").exists(); //$NON-NLS-1$
  207. else
  208. return false;
  209. }
  210. private static String nameWithDotGit(String name) {
  211. if (name.endsWith(Constants.DOT_GIT_EXT))
  212. return name;
  213. return name + Constants.DOT_GIT_EXT;
  214. }
  215. private static boolean isUnreasonableName(String name) {
  216. if (name.length() == 0)
  217. return true; // no empty paths
  218. if (name.indexOf('\\') >= 0)
  219. return true; // no windows/dos style paths
  220. if (new File(name).isAbsolute())
  221. return true; // no absolute paths
  222. if (name.startsWith("../")) //$NON-NLS-1$
  223. return true; // no "l../etc/passwd"
  224. if (name.contains("/../")) //$NON-NLS-1$
  225. return true; // no "foo/../etc/passwd"
  226. if (name.contains("/./")) //$NON-NLS-1$
  227. return true; // "foo/./foo" is insane to ask
  228. if (name.contains("//")) //$NON-NLS-1$
  229. return true; // double slashes is sloppy, don't use it
  230. return false; // is a reasonable name
  231. }
  232. }