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.

GitBlitServlet.java 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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;
  17. import java.io.IOException;
  18. import java.text.MessageFormat;
  19. import javax.servlet.ServletException;
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpServletResponse;
  22. import org.eclipse.jgit.http.server.GitServlet;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import com.gitblit.Constants.AccessRestrictionType;
  26. import com.gitblit.wicket.models.RepositoryModel;
  27. public class GitBlitServlet extends GitServlet {
  28. private static final long serialVersionUID = 1L;
  29. private final Logger logger = LoggerFactory.getLogger(GitBlitServlet.class);
  30. public GitBlitServlet() {
  31. super();
  32. }
  33. @Override
  34. protected void service(final HttpServletRequest req, final HttpServletResponse rsp) throws ServletException, IOException {
  35. // admins have full git access to all repositories
  36. if (req.isUserInRole(Constants.ADMIN_ROLE)) {
  37. // admins can do whatever
  38. super.service(req, rsp);
  39. return;
  40. }
  41. // try to intercept repository names for authenticated access
  42. String url = req.getRequestURI().substring(req.getServletPath().length());
  43. if (url.charAt(0) == '/' && url.length() > 1) {
  44. url = url.substring(1);
  45. }
  46. int forwardSlash = url.indexOf('/');
  47. if (forwardSlash > -1) {
  48. String repository = url.substring(0, forwardSlash);
  49. String function = url.substring(forwardSlash + 1);
  50. String query = req.getQueryString() == null ? "":req.getQueryString();
  51. RepositoryModel model = GitBlit.self().getRepositoryModel(repository);
  52. if (model != null) {
  53. if (model.isFrozen || model.accessRestriction.atLeast(AccessRestrictionType.PUSH)) {
  54. boolean authorizedUser = req.isUserInRole(repository);
  55. if (function.startsWith("git-receive-pack") || (query.indexOf("service=git-receive-pack") > -1)) {
  56. // Push request
  57. if (!model.isFrozen && authorizedUser) {
  58. // clone-restricted or push-authorized
  59. super.service(req, rsp);
  60. return;
  61. } else {
  62. // user is unauthorized to push to this repository
  63. logger.warn(MessageFormat.format("user {0} is not authorized to push to {1} ", req.getUserPrincipal().getName(), repository));
  64. rsp.sendError(HttpServletResponse.SC_FORBIDDEN, MessageFormat.format("you are not authorized to push to {0} ", repository));
  65. return;
  66. }
  67. } else if (function.startsWith("git-upload-pack") || (query.indexOf("service=git-upload-pack") > -1)) {
  68. // Clone request
  69. boolean cloneRestricted = model.accessRestriction.atLeast(AccessRestrictionType.CLONE);
  70. if (!cloneRestricted || (cloneRestricted && authorizedUser)) {
  71. // push-restricted or clone-authorized
  72. super.service(req, rsp);
  73. return;
  74. } else {
  75. // user is unauthorized to clone this repository
  76. logger.warn(MessageFormat.format("user {0} is not authorized to clone {1} ", req.getUserPrincipal().getName(), repository));
  77. rsp.sendError(HttpServletResponse.SC_FORBIDDEN, MessageFormat.format("you are not authorized to clone {0} ", repository));
  78. return;
  79. }
  80. }
  81. }
  82. }
  83. }
  84. // pass-through to git servlet
  85. super.service(req, rsp);
  86. }
  87. }