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.

GitblitReceivePackFactory.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2011 gitblit.com.
  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. package com.gitblit.git;
  17. import java.util.HashSet;
  18. import java.util.Set;
  19. import javax.servlet.http.HttpServletRequest;
  20. import org.eclipse.jgit.lib.PersonIdent;
  21. import org.eclipse.jgit.lib.Repository;
  22. import org.eclipse.jgit.transport.ReceivePack;
  23. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  24. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  25. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import com.gitblit.Constants.Transport;
  29. import com.gitblit.IStoredSettings;
  30. import com.gitblit.Keys;
  31. import com.gitblit.manager.IGitblit;
  32. import com.gitblit.models.RepositoryModel;
  33. import com.gitblit.models.UserModel;
  34. import com.gitblit.transport.git.GitDaemonClient;
  35. import com.gitblit.transport.ssh.SshDaemonClient;
  36. import com.gitblit.utils.HttpUtils;
  37. import com.gitblit.utils.StringUtils;
  38. /**
  39. * The receive pack factory creates the receive pack which processes pushes.
  40. *
  41. * @author James Moger
  42. *
  43. * @param <X> the connection type
  44. */
  45. public class GitblitReceivePackFactory<X> implements ReceivePackFactory<X> {
  46. protected final Logger logger = LoggerFactory.getLogger(GitblitReceivePackFactory.class);
  47. private final IStoredSettings settings;
  48. private final IGitblit gitblit;
  49. public GitblitReceivePackFactory(IGitblit gitblit) {
  50. super();
  51. this.settings = gitblit.getSettings();
  52. this.gitblit = gitblit;
  53. }
  54. @Override
  55. public ReceivePack create(X req, Repository db)
  56. throws ServiceNotEnabledException, ServiceNotAuthorizedException {
  57. UserModel user = UserModel.ANONYMOUS;
  58. String repositoryName = "";
  59. String origin = "";
  60. String gitblitUrl = "";
  61. int timeout = 0;
  62. Transport transport = null;
  63. if (req instanceof HttpServletRequest) {
  64. // http/https request may or may not be authenticated
  65. HttpServletRequest client = (HttpServletRequest) req;
  66. repositoryName = client.getAttribute("gitblitRepositoryName").toString();
  67. origin = client.getRemoteHost();
  68. gitblitUrl = HttpUtils.getGitblitURL(client);
  69. // determine pushing user
  70. String username = client.getRemoteUser();
  71. if (!StringUtils.isEmpty(username)) {
  72. UserModel u = gitblit.getUserModel(username);
  73. if (u != null) {
  74. user = u;
  75. }
  76. }
  77. // determine the transport
  78. if ("http".equals(client.getScheme())) {
  79. transport = Transport.HTTP;
  80. } else if ("https".equals(client.getScheme())) {
  81. transport = Transport.HTTPS;
  82. }
  83. } else if (req instanceof GitDaemonClient) {
  84. // git daemon request is always anonymous
  85. GitDaemonClient client = (GitDaemonClient) req;
  86. repositoryName = client.getRepositoryName();
  87. origin = client.getRemoteAddress().getHostAddress();
  88. // set timeout from Git daemon
  89. timeout = client.getDaemon().getTimeout();
  90. transport = Transport.GIT;
  91. } else if (req instanceof SshDaemonClient) {
  92. // SSH request is always authenticated
  93. SshDaemonClient client = (SshDaemonClient) req;
  94. repositoryName = client.getRepositoryName();
  95. origin = client.getRemoteAddress().toString();
  96. user = client.getUser();
  97. transport = Transport.SSH;
  98. }
  99. if (!acceptPush(transport)) {
  100. throw new ServiceNotAuthorizedException();
  101. }
  102. boolean allowAnonymousPushes = settings.getBoolean(Keys.git.allowAnonymousPushes, false);
  103. if (!allowAnonymousPushes && UserModel.ANONYMOUS.equals(user)) {
  104. // prohibit anonymous pushes
  105. throw new ServiceNotEnabledException();
  106. }
  107. String url = settings.getString(Keys.web.canonicalUrl, null);
  108. if (StringUtils.isEmpty(url)) {
  109. url = gitblitUrl;
  110. }
  111. final RepositoryModel repository = gitblit.getRepositoryModel(repositoryName);
  112. // Determine which receive pack to use for pushes
  113. final GitblitReceivePack rp;
  114. if (gitblit.getTicketService().isAcceptingNewPatchsets(repository)) {
  115. rp = new PatchsetReceivePack(gitblit, db, repository, user);
  116. } else {
  117. rp = new GitblitReceivePack(gitblit, db, repository, user);
  118. }
  119. rp.setGitblitUrl(url);
  120. rp.setRefLogIdent(new PersonIdent(user.username, user.username + "@" + origin));
  121. rp.setTimeout(timeout);
  122. return rp;
  123. }
  124. protected boolean acceptPush(Transport byTransport) {
  125. if (byTransport == null) {
  126. logger.info("Unknown transport, push rejected!");
  127. return false;
  128. }
  129. Set<Transport> transports = new HashSet<Transport>();
  130. for (String value : gitblit.getSettings().getStrings(Keys.git.acceptedPushTransports)) {
  131. Transport transport = Transport.fromString(value);
  132. if (transport == null) {
  133. logger.info(String.format("Ignoring unknown registered transport %s", value));
  134. continue;
  135. }
  136. transports.add(transport);
  137. }
  138. if (transports.isEmpty()) {
  139. // no transports are explicitly specified, all are acceptable
  140. return true;
  141. }
  142. // verify that the transport is permitted
  143. return transports.contains(byTransport);
  144. }
  145. }