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.

678 lines
19 KiB

10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
Add single sign-on support via SSPI on Windows (#8463) * Add single sign-on support via SSPI on Windows * Ensure plugins implement interface * Ensure plugins implement interface * Move functions used only by the SSPI auth method to sspi_windows.go * Field SSPISeparatorReplacement of AuthenticationForm should not be required via binding, as binding will insist the field is non-empty even if another login type is selected * Fix breaking of oauth authentication on download links. Do not create new session with SSPI authentication on download links. * Update documentation for the new 'SPNEGO with SSPI' login source * Mention in documentation that ROOT_URL should contain the FQDN of the server * Make sure that Contexter is not checking for active login sources when the ORM engine is not initialized (eg. when installing) * Always initialize and free SSO methods, even if they are not enabled, as a method can be activated while the app is running (from Authentication sources) * Add option in SSPIConfig for removing of domains from logon names * Update helper text for StripDomainNames option * Make sure handleSignIn() is called after a new user object is created by SSPI auth method * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Only make a query to the DB to check if SSPI is enabled on handlers that need that information for templates * Remove code duplication * Log errors in ActiveLoginSources Co-Authored-By: Lauris BH <lauris@nix.lv> * Revert suffix of randomly generated E-mails for Reverse proxy authentication Co-Authored-By: Lauris BH <lauris@nix.lv> * Revert unneeded white-space change in template Co-Authored-By: Lauris BH <lauris@nix.lv> * Add copyright comments at the top of new files * Use loopback name for randomly generated emails * Add locale tag for the SSPISeparatorReplacement field with proper casing * Revert casing of SSPISeparatorReplacement field in locale file, moving it up, next to other form fields * Update docs/content/doc/features/authentication.en-us.md Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Remove Priority() method and define the order in which SSO auth methods should be executed in one place * Log authenticated username only if it's not empty * Rephrase helper text for automatic creation of users * Return error if more than one active SSPI auth source is found * Change newUser() function to return error, letting caller log/handle the error * Move isPublicResource, isPublicPage and handleSignIn functions outside SSPI auth method to allow other SSO methods to reuse them if needed * Refactor initialization of the list containing SSO auth methods * Validate SSPI settings on POST * Change SSPI to only perform authentication on its own login page, API paths and download links. Leave Toggle middleware to redirect non authenticated users to login page * Make 'Default language' in SSPI config empty, unless changed by admin * Show error if admin tries to add a second authentication source of type SSPI * Simplify declaration of global variable * Rebuild gitgraph.js on Linux * Make sure config values containing only whitespace are not accepted
4 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Add single sign-on support via SSPI on Windows (#8463) * Add single sign-on support via SSPI on Windows * Ensure plugins implement interface * Ensure plugins implement interface * Move functions used only by the SSPI auth method to sspi_windows.go * Field SSPISeparatorReplacement of AuthenticationForm should not be required via binding, as binding will insist the field is non-empty even if another login type is selected * Fix breaking of oauth authentication on download links. Do not create new session with SSPI authentication on download links. * Update documentation for the new 'SPNEGO with SSPI' login source * Mention in documentation that ROOT_URL should contain the FQDN of the server * Make sure that Contexter is not checking for active login sources when the ORM engine is not initialized (eg. when installing) * Always initialize and free SSO methods, even if they are not enabled, as a method can be activated while the app is running (from Authentication sources) * Add option in SSPIConfig for removing of domains from logon names * Update helper text for StripDomainNames option * Make sure handleSignIn() is called after a new user object is created by SSPI auth method * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Remove default value from text of form field helper Co-Authored-By: Lauris BH <lauris@nix.lv> * Only make a query to the DB to check if SSPI is enabled on handlers that need that information for templates * Remove code duplication * Log errors in ActiveLoginSources Co-Authored-By: Lauris BH <lauris@nix.lv> * Revert suffix of randomly generated E-mails for Reverse proxy authentication Co-Authored-By: Lauris BH <lauris@nix.lv> * Revert unneeded white-space change in template Co-Authored-By: Lauris BH <lauris@nix.lv> * Add copyright comments at the top of new files * Use loopback name for randomly generated emails * Add locale tag for the SSPISeparatorReplacement field with proper casing * Revert casing of SSPISeparatorReplacement field in locale file, moving it up, next to other form fields * Update docs/content/doc/features/authentication.en-us.md Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Remove Priority() method and define the order in which SSO auth methods should be executed in one place * Log authenticated username only if it's not empty * Rephrase helper text for automatic creation of users * Return error if more than one active SSPI auth source is found * Change newUser() function to return error, letting caller log/handle the error * Move isPublicResource, isPublicPage and handleSignIn functions outside SSPI auth method to allow other SSO methods to reuse them if needed * Refactor initialization of the list containing SSO auth methods * Validate SSPI settings on POST * Change SSPI to only perform authentication on its own login page, API paths and download links. Leave Toggle middleware to redirect non authenticated users to login page * Make 'Default language' in SSPI config empty, unless changed by admin * Show error if admin tries to add a second authentication source of type SSPI * Simplify declaration of global variable * Rebuild gitgraph.js on Linux * Make sure config values containing only whitespace are not accepted
4 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "bytes"
  8. "compress/gzip"
  9. gocontext "context"
  10. "fmt"
  11. "io/ioutil"
  12. "net/http"
  13. "os"
  14. "os/exec"
  15. "path"
  16. "regexp"
  17. "strconv"
  18. "strings"
  19. "sync"
  20. "time"
  21. "code.gitea.io/gitea/models"
  22. "code.gitea.io/gitea/modules/auth/sso"
  23. "code.gitea.io/gitea/modules/base"
  24. "code.gitea.io/gitea/modules/context"
  25. "code.gitea.io/gitea/modules/git"
  26. "code.gitea.io/gitea/modules/log"
  27. "code.gitea.io/gitea/modules/process"
  28. "code.gitea.io/gitea/modules/setting"
  29. "code.gitea.io/gitea/modules/structs"
  30. "code.gitea.io/gitea/modules/timeutil"
  31. "code.gitea.io/gitea/modules/util"
  32. repo_service "code.gitea.io/gitea/services/repository"
  33. )
  34. // HTTP implmentation git smart HTTP protocol
  35. func HTTP(ctx *context.Context) {
  36. if len(setting.Repository.AccessControlAllowOrigin) > 0 {
  37. allowedOrigin := setting.Repository.AccessControlAllowOrigin
  38. // Set CORS headers for browser-based git clients
  39. ctx.Resp.Header().Set("Access-Control-Allow-Origin", allowedOrigin)
  40. ctx.Resp.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, User-Agent")
  41. // Handle preflight OPTIONS request
  42. if ctx.Req.Method == "OPTIONS" {
  43. if allowedOrigin == "*" {
  44. ctx.Status(http.StatusOK)
  45. } else if allowedOrigin == "null" {
  46. ctx.Status(http.StatusForbidden)
  47. } else {
  48. origin := ctx.Req.Header.Get("Origin")
  49. if len(origin) > 0 && origin == allowedOrigin {
  50. ctx.Status(http.StatusOK)
  51. } else {
  52. ctx.Status(http.StatusForbidden)
  53. }
  54. }
  55. return
  56. }
  57. }
  58. username := ctx.Params(":username")
  59. reponame := strings.TrimSuffix(ctx.Params(":reponame"), ".git")
  60. if ctx.Query("go-get") == "1" {
  61. context.EarlyResponseForGoGetMeta(ctx)
  62. return
  63. }
  64. var isPull, receivePack bool
  65. service := ctx.Query("service")
  66. if service == "git-receive-pack" ||
  67. strings.HasSuffix(ctx.Req.URL.Path, "git-receive-pack") {
  68. isPull = false
  69. receivePack = true
  70. } else if service == "git-upload-pack" ||
  71. strings.HasSuffix(ctx.Req.URL.Path, "git-upload-pack") {
  72. isPull = true
  73. } else if service == "git-upload-archive" ||
  74. strings.HasSuffix(ctx.Req.URL.Path, "git-upload-archive") {
  75. isPull = true
  76. } else {
  77. isPull = (ctx.Req.Method == "GET")
  78. }
  79. var accessMode models.AccessMode
  80. if isPull {
  81. accessMode = models.AccessModeRead
  82. } else {
  83. accessMode = models.AccessModeWrite
  84. }
  85. isWiki := false
  86. var unitType = models.UnitTypeCode
  87. if strings.HasSuffix(reponame, ".wiki") {
  88. isWiki = true
  89. unitType = models.UnitTypeWiki
  90. reponame = reponame[:len(reponame)-5]
  91. }
  92. owner, err := models.GetUserByName(username)
  93. if err != nil {
  94. ctx.NotFoundOrServerError("GetUserByName", models.IsErrUserNotExist, err)
  95. return
  96. }
  97. repoExist := true
  98. repo, err := models.GetRepositoryByName(owner.ID, reponame)
  99. if err != nil {
  100. if models.IsErrRepoNotExist(err) {
  101. if redirectRepoID, err := models.LookupRepoRedirect(owner.ID, reponame); err == nil {
  102. context.RedirectToRepo(ctx, redirectRepoID)
  103. return
  104. }
  105. repoExist = false
  106. } else {
  107. ctx.ServerError("GetRepositoryByName", err)
  108. return
  109. }
  110. }
  111. // Don't allow pushing if the repo is archived
  112. if repoExist && repo.IsArchived && !isPull {
  113. ctx.HandleText(http.StatusForbidden, "This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.")
  114. return
  115. }
  116. // Only public pull don't need auth.
  117. isPublicPull := repoExist && !repo.IsPrivate && isPull
  118. var (
  119. askAuth = !isPublicPull || setting.Service.RequireSignInView
  120. authUser *models.User
  121. authUsername string
  122. authPasswd string
  123. environ []string
  124. )
  125. // don't allow anonymous pulls if organization is not public
  126. if isPublicPull {
  127. if err := repo.GetOwner(); err != nil {
  128. ctx.ServerError("GetOwner", err)
  129. return
  130. }
  131. askAuth = askAuth || (repo.Owner.Visibility != structs.VisibleTypePublic)
  132. }
  133. // check access
  134. if askAuth {
  135. authUsername = ctx.Req.Header.Get(setting.ReverseProxyAuthUser)
  136. if setting.Service.EnableReverseProxyAuth && len(authUsername) > 0 {
  137. authUser, err = models.GetUserByName(authUsername)
  138. if err != nil {
  139. ctx.HandleText(401, "reverse proxy login error, got error while running GetUserByName")
  140. return
  141. }
  142. } else {
  143. authHead := ctx.Req.Header.Get("Authorization")
  144. if len(authHead) == 0 {
  145. ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=\".\"")
  146. ctx.Error(http.StatusUnauthorized)
  147. return
  148. }
  149. auths := strings.Fields(authHead)
  150. // currently check basic auth
  151. // TODO: support digit auth
  152. // FIXME: middlewares/context.go did basic auth check already,
  153. // maybe could use that one.
  154. if len(auths) != 2 || auths[0] != "Basic" {
  155. ctx.HandleText(http.StatusUnauthorized, "no basic auth and digit auth")
  156. return
  157. }
  158. authUsername, authPasswd, err = base.BasicAuthDecode(auths[1])
  159. if err != nil {
  160. ctx.HandleText(http.StatusUnauthorized, "no basic auth and digit auth")
  161. return
  162. }
  163. // Check if username or password is a token
  164. isUsernameToken := len(authPasswd) == 0 || authPasswd == "x-oauth-basic"
  165. // Assume username is token
  166. authToken := authUsername
  167. if !isUsernameToken {
  168. // Assume password is token
  169. authToken = authPasswd
  170. }
  171. uid := sso.CheckOAuthAccessToken(authToken)
  172. if uid != 0 {
  173. ctx.Data["IsApiToken"] = true
  174. authUser, err = models.GetUserByID(uid)
  175. if err != nil {
  176. ctx.ServerError("GetUserByID", err)
  177. return
  178. }
  179. }
  180. // Assume password is a token.
  181. token, err := models.GetAccessTokenBySHA(authToken)
  182. if err == nil {
  183. authUser, err = models.GetUserByID(token.UID)
  184. if err != nil {
  185. ctx.ServerError("GetUserByID", err)
  186. return
  187. }
  188. token.UpdatedUnix = timeutil.TimeStampNow()
  189. if err = models.UpdateAccessToken(token); err != nil {
  190. ctx.ServerError("UpdateAccessToken", err)
  191. }
  192. } else if !models.IsErrAccessTokenNotExist(err) && !models.IsErrAccessTokenEmpty(err) {
  193. log.Error("GetAccessTokenBySha: %v", err)
  194. }
  195. if authUser == nil {
  196. // Check username and password
  197. authUser, err = models.UserSignIn(authUsername, authPasswd)
  198. if err != nil {
  199. if models.IsErrUserProhibitLogin(err) {
  200. ctx.HandleText(http.StatusForbidden, "User is not permitted to login")
  201. return
  202. } else if !models.IsErrUserNotExist(err) {
  203. ctx.ServerError("UserSignIn error: %v", err)
  204. return
  205. }
  206. }
  207. if authUser == nil {
  208. ctx.HandleText(http.StatusUnauthorized, fmt.Sprintf("invalid credentials from %s", ctx.RemoteAddr()))
  209. return
  210. }
  211. _, err = models.GetTwoFactorByUID(authUser.ID)
  212. if err == nil {
  213. // TODO: This response should be changed to "invalid credentials" for security reasons once the expectation behind it (creating an app token to authenticate) is properly documented
  214. ctx.HandleText(http.StatusUnauthorized, "Users with two-factor authentication enabled cannot perform HTTP/HTTPS operations via plain username and password. Please create and use a personal access token on the user settings page")
  215. return
  216. } else if !models.IsErrTwoFactorNotEnrolled(err) {
  217. ctx.ServerError("IsErrTwoFactorNotEnrolled", err)
  218. return
  219. }
  220. }
  221. }
  222. if repoExist {
  223. perm, err := models.GetUserRepoPermission(repo, authUser)
  224. if err != nil {
  225. ctx.ServerError("GetUserRepoPermission", err)
  226. return
  227. }
  228. if !perm.CanAccess(accessMode, unitType) {
  229. ctx.HandleText(http.StatusForbidden, "User permission denied")
  230. return
  231. }
  232. if !isPull && repo.IsMirror {
  233. ctx.HandleText(http.StatusForbidden, "mirror repository is read-only")
  234. return
  235. }
  236. }
  237. environ = []string{
  238. models.EnvRepoUsername + "=" + username,
  239. models.EnvRepoName + "=" + reponame,
  240. models.EnvPusherName + "=" + authUser.Name,
  241. models.EnvPusherID + fmt.Sprintf("=%d", authUser.ID),
  242. models.EnvIsDeployKey + "=false",
  243. models.EnvAppURL + "=" + setting.AppURL,
  244. }
  245. if !authUser.KeepEmailPrivate {
  246. environ = append(environ, models.EnvPusherEmail+"="+authUser.Email)
  247. }
  248. if isWiki {
  249. environ = append(environ, models.EnvRepoIsWiki+"=true")
  250. } else {
  251. environ = append(environ, models.EnvRepoIsWiki+"=false")
  252. }
  253. }
  254. if !repoExist {
  255. if !receivePack {
  256. ctx.HandleText(http.StatusNotFound, "Repository not found")
  257. return
  258. }
  259. if owner.IsOrganization() && !setting.Repository.EnablePushCreateOrg {
  260. ctx.HandleText(http.StatusForbidden, "Push to create is not enabled for organizations.")
  261. return
  262. }
  263. if !owner.IsOrganization() && !setting.Repository.EnablePushCreateUser {
  264. ctx.HandleText(http.StatusForbidden, "Push to create is not enabled for users.")
  265. return
  266. }
  267. // Return dummy payload if GET receive-pack
  268. if ctx.Req.Method == http.MethodGet {
  269. dummyInfoRefs(ctx)
  270. return
  271. }
  272. repo, err = repo_service.PushCreateRepo(authUser, owner, reponame)
  273. if err != nil {
  274. log.Error("pushCreateRepo: %v", err)
  275. ctx.Status(http.StatusNotFound)
  276. return
  277. }
  278. }
  279. if isWiki {
  280. // Ensure the wiki is enabled before we allow access to it
  281. if _, err := repo.GetUnit(models.UnitTypeWiki); err != nil {
  282. if models.IsErrUnitTypeNotExist(err) {
  283. ctx.HandleText(http.StatusForbidden, "repository wiki is disabled")
  284. return
  285. }
  286. log.Error("Failed to get the wiki unit in %-v Error: %v", repo, err)
  287. ctx.ServerError("GetUnit(UnitTypeWiki) for "+repo.FullName(), err)
  288. return
  289. }
  290. }
  291. environ = append(environ, models.EnvRepoID+fmt.Sprintf("=%d", repo.ID))
  292. w := ctx.Resp
  293. r := ctx.Req.Request
  294. cfg := &serviceConfig{
  295. UploadPack: true,
  296. ReceivePack: true,
  297. Env: environ,
  298. }
  299. r.URL.Path = strings.ToLower(r.URL.Path) // blue: In case some repo name has upper case name
  300. for _, route := range routes {
  301. if m := route.reg.FindStringSubmatch(r.URL.Path); m != nil {
  302. if setting.Repository.DisableHTTPGit {
  303. w.WriteHeader(http.StatusForbidden)
  304. _, err := w.Write([]byte("Interacting with repositories by HTTP protocol is not allowed"))
  305. if err != nil {
  306. log.Error(err.Error())
  307. }
  308. return
  309. }
  310. if route.method != r.Method {
  311. if r.Proto == "HTTP/1.1" {
  312. w.WriteHeader(http.StatusMethodNotAllowed)
  313. _, err := w.Write([]byte("Method Not Allowed"))
  314. if err != nil {
  315. log.Error(err.Error())
  316. }
  317. } else {
  318. w.WriteHeader(http.StatusBadRequest)
  319. _, err := w.Write([]byte("Bad Request"))
  320. if err != nil {
  321. log.Error(err.Error())
  322. }
  323. }
  324. return
  325. }
  326. file := strings.Replace(r.URL.Path, m[1]+"/", "", 1)
  327. dir, err := getGitRepoPath(m[1])
  328. if err != nil {
  329. log.Error(err.Error())
  330. ctx.NotFound("Smart Git HTTP", err)
  331. return
  332. }
  333. route.handler(serviceHandler{cfg, w, r, dir, file, cfg.Env})
  334. return
  335. }
  336. }
  337. ctx.NotFound("Smart Git HTTP", nil)
  338. }
  339. var (
  340. infoRefsCache []byte
  341. infoRefsOnce sync.Once
  342. )
  343. func dummyInfoRefs(ctx *context.Context) {
  344. infoRefsOnce.Do(func() {
  345. tmpDir, err := ioutil.TempDir(os.TempDir(), "gitea-info-refs-cache")
  346. if err != nil {
  347. log.Error("Failed to create temp dir for git-receive-pack cache: %v", err)
  348. return
  349. }
  350. defer func() {
  351. if err := util.RemoveAll(tmpDir); err != nil {
  352. log.Error("RemoveAll: %v", err)
  353. }
  354. }()
  355. if err := git.InitRepository(tmpDir, true); err != nil {
  356. log.Error("Failed to init bare repo for git-receive-pack cache: %v", err)
  357. return
  358. }
  359. refs, err := git.NewCommand("receive-pack", "--stateless-rpc", "--advertise-refs", ".").RunInDirBytes(tmpDir)
  360. if err != nil {
  361. log.Error(fmt.Sprintf("%v - %s", err, string(refs)))
  362. }
  363. log.Debug("populating infoRefsCache: \n%s", string(refs))
  364. infoRefsCache = refs
  365. })
  366. ctx.Header().Set("Expires", "Fri, 01 Jan 1980 00:00:00 GMT")
  367. ctx.Header().Set("Pragma", "no-cache")
  368. ctx.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate")
  369. ctx.Header().Set("Content-Type", "application/x-git-receive-pack-advertisement")
  370. _, _ = ctx.Write(packetWrite("# service=git-receive-pack\n"))
  371. _, _ = ctx.Write([]byte("0000"))
  372. _, _ = ctx.Write(infoRefsCache)
  373. }
  374. type serviceConfig struct {
  375. UploadPack bool
  376. ReceivePack bool
  377. Env []string
  378. }
  379. type serviceHandler struct {
  380. cfg *serviceConfig
  381. w http.ResponseWriter
  382. r *http.Request
  383. dir string
  384. file string
  385. environ []string
  386. }
  387. func (h *serviceHandler) setHeaderNoCache() {
  388. h.w.Header().Set("Expires", "Fri, 01 Jan 1980 00:00:00 GMT")
  389. h.w.Header().Set("Pragma", "no-cache")
  390. h.w.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate")
  391. }
  392. func (h *serviceHandler) setHeaderCacheForever() {
  393. now := time.Now().Unix()
  394. expires := now + 31536000
  395. h.w.Header().Set("Date", fmt.Sprintf("%d", now))
  396. h.w.Header().Set("Expires", fmt.Sprintf("%d", expires))
  397. h.w.Header().Set("Cache-Control", "public, max-age=31536000")
  398. }
  399. func (h *serviceHandler) sendFile(contentType string) {
  400. reqFile := path.Join(h.dir, h.file)
  401. fi, err := os.Stat(reqFile)
  402. if os.IsNotExist(err) {
  403. h.w.WriteHeader(http.StatusNotFound)
  404. return
  405. }
  406. h.w.Header().Set("Content-Type", contentType)
  407. h.w.Header().Set("Content-Length", fmt.Sprintf("%d", fi.Size()))
  408. h.w.Header().Set("Last-Modified", fi.ModTime().Format(http.TimeFormat))
  409. http.ServeFile(h.w, h.r, reqFile)
  410. }
  411. type route struct {
  412. reg *regexp.Regexp
  413. method string
  414. handler func(serviceHandler)
  415. }
  416. var routes = []route{
  417. {regexp.MustCompile(`(.*?)/git-upload-pack$`), "POST", serviceUploadPack},
  418. {regexp.MustCompile(`(.*?)/git-receive-pack$`), "POST", serviceReceivePack},
  419. {regexp.MustCompile(`(.*?)/info/refs$`), "GET", getInfoRefs},
  420. {regexp.MustCompile(`(.*?)/HEAD$`), "GET", getTextFile},
  421. {regexp.MustCompile(`(.*?)/objects/info/alternates$`), "GET", getTextFile},
  422. {regexp.MustCompile(`(.*?)/objects/info/http-alternates$`), "GET", getTextFile},
  423. {regexp.MustCompile(`(.*?)/objects/info/packs$`), "GET", getInfoPacks},
  424. {regexp.MustCompile(`(.*?)/objects/info/[^/]*$`), "GET", getTextFile},
  425. {regexp.MustCompile(`(.*?)/objects/[0-9a-f]{2}/[0-9a-f]{38}$`), "GET", getLooseObject},
  426. {regexp.MustCompile(`(.*?)/objects/pack/pack-[0-9a-f]{40}\.pack$`), "GET", getPackFile},
  427. {regexp.MustCompile(`(.*?)/objects/pack/pack-[0-9a-f]{40}\.idx$`), "GET", getIdxFile},
  428. }
  429. // one or more key=value pairs separated by colons
  430. var safeGitProtocolHeader = regexp.MustCompile(`^[0-9a-zA-Z]+=[0-9a-zA-Z]+(:[0-9a-zA-Z]+=[0-9a-zA-Z]+)*$`)
  431. func getGitConfig(option, dir string) string {
  432. out, err := git.NewCommand("config", option).RunInDir(dir)
  433. if err != nil {
  434. log.Error("%v - %s", err, out)
  435. }
  436. return out[0 : len(out)-1]
  437. }
  438. func getConfigSetting(service, dir string) bool {
  439. service = strings.Replace(service, "-", "", -1)
  440. setting := getGitConfig("http."+service, dir)
  441. if service == "uploadpack" {
  442. return setting != "false"
  443. }
  444. return setting == "true"
  445. }
  446. func hasAccess(service string, h serviceHandler, checkContentType bool) bool {
  447. if checkContentType {
  448. if h.r.Header.Get("Content-Type") != fmt.Sprintf("application/x-git-%s-request", service) {
  449. return false
  450. }
  451. }
  452. if !(service == "upload-pack" || service == "receive-pack") {
  453. return false
  454. }
  455. if service == "receive-pack" {
  456. return h.cfg.ReceivePack
  457. }
  458. if service == "upload-pack" {
  459. return h.cfg.UploadPack
  460. }
  461. return getConfigSetting(service, h.dir)
  462. }
  463. func serviceRPC(h serviceHandler, service string) {
  464. defer func() {
  465. if err := h.r.Body.Close(); err != nil {
  466. log.Error("serviceRPC: Close: %v", err)
  467. }
  468. }()
  469. if !hasAccess(service, h, true) {
  470. h.w.WriteHeader(http.StatusUnauthorized)
  471. return
  472. }
  473. h.w.Header().Set("Content-Type", fmt.Sprintf("application/x-git-%s-result", service))
  474. var err error
  475. var reqBody = h.r.Body
  476. // Handle GZIP.
  477. if h.r.Header.Get("Content-Encoding") == "gzip" {
  478. reqBody, err = gzip.NewReader(reqBody)
  479. if err != nil {
  480. log.Error("Fail to create gzip reader: %v", err)
  481. h.w.WriteHeader(http.StatusInternalServerError)
  482. return
  483. }
  484. }
  485. // set this for allow pre-receive and post-receive execute
  486. h.environ = append(h.environ, "SSH_ORIGINAL_COMMAND="+service)
  487. if protocol := h.r.Header.Get("Git-Protocol"); protocol != "" && safeGitProtocolHeader.MatchString(protocol) {
  488. h.environ = append(h.environ, "GIT_PROTOCOL="+protocol)
  489. }
  490. ctx, cancel := gocontext.WithCancel(git.DefaultContext)
  491. defer cancel()
  492. var stderr bytes.Buffer
  493. cmd := exec.CommandContext(ctx, git.GitExecutable, service, "--stateless-rpc", h.dir)
  494. cmd.Dir = h.dir
  495. cmd.Env = append(os.Environ(), h.environ...)
  496. cmd.Stdout = h.w
  497. cmd.Stdin = reqBody
  498. cmd.Stderr = &stderr
  499. pid := process.GetManager().Add(fmt.Sprintf("%s %s %s [repo_path: %s]", git.GitExecutable, service, "--stateless-rpc", h.dir), cancel)
  500. defer process.GetManager().Remove(pid)
  501. if err := cmd.Run(); err != nil {
  502. log.Error("Fail to serve RPC(%s): %v - %s", service, err, stderr.String())
  503. return
  504. }
  505. }
  506. func serviceUploadPack(h serviceHandler) {
  507. serviceRPC(h, "upload-pack")
  508. }
  509. func serviceReceivePack(h serviceHandler) {
  510. serviceRPC(h, "receive-pack")
  511. }
  512. func getServiceType(r *http.Request) string {
  513. serviceType := r.FormValue("service")
  514. if !strings.HasPrefix(serviceType, "git-") {
  515. return ""
  516. }
  517. return strings.Replace(serviceType, "git-", "", 1)
  518. }
  519. func updateServerInfo(dir string) []byte {
  520. out, err := git.NewCommand("update-server-info").RunInDirBytes(dir)
  521. if err != nil {
  522. log.Error(fmt.Sprintf("%v - %s", err, string(out)))
  523. }
  524. return out
  525. }
  526. func packetWrite(str string) []byte {
  527. s := strconv.FormatInt(int64(len(str)+4), 16)
  528. if len(s)%4 != 0 {
  529. s = strings.Repeat("0", 4-len(s)%4) + s
  530. }
  531. return []byte(s + str)
  532. }
  533. func getInfoRefs(h serviceHandler) {
  534. h.setHeaderNoCache()
  535. if hasAccess(getServiceType(h.r), h, false) {
  536. service := getServiceType(h.r)
  537. if protocol := h.r.Header.Get("Git-Protocol"); protocol != "" && safeGitProtocolHeader.MatchString(protocol) {
  538. h.environ = append(h.environ, "GIT_PROTOCOL="+protocol)
  539. }
  540. h.environ = append(os.Environ(), h.environ...)
  541. refs, err := git.NewCommand(service, "--stateless-rpc", "--advertise-refs", ".").RunInDirTimeoutEnv(h.environ, -1, h.dir)
  542. if err != nil {
  543. log.Error(fmt.Sprintf("%v - %s", err, string(refs)))
  544. }
  545. h.w.Header().Set("Content-Type", fmt.Sprintf("application/x-git-%s-advertisement", service))
  546. h.w.WriteHeader(http.StatusOK)
  547. _, _ = h.w.Write(packetWrite("# service=git-" + service + "\n"))
  548. _, _ = h.w.Write([]byte("0000"))
  549. _, _ = h.w.Write(refs)
  550. } else {
  551. updateServerInfo(h.dir)
  552. h.sendFile("text/plain; charset=utf-8")
  553. }
  554. }
  555. func getTextFile(h serviceHandler) {
  556. h.setHeaderNoCache()
  557. h.sendFile("text/plain")
  558. }
  559. func getInfoPacks(h serviceHandler) {
  560. h.setHeaderCacheForever()
  561. h.sendFile("text/plain; charset=utf-8")
  562. }
  563. func getLooseObject(h serviceHandler) {
  564. h.setHeaderCacheForever()
  565. h.sendFile("application/x-git-loose-object")
  566. }
  567. func getPackFile(h serviceHandler) {
  568. h.setHeaderCacheForever()
  569. h.sendFile("application/x-git-packed-objects")
  570. }
  571. func getIdxFile(h serviceHandler) {
  572. h.setHeaderCacheForever()
  573. h.sendFile("application/x-git-packed-objects-toc")
  574. }
  575. func getGitRepoPath(subdir string) (string, error) {
  576. if !strings.HasSuffix(subdir, ".git") {
  577. subdir += ".git"
  578. }
  579. fpath := path.Join(setting.RepoRootPath, subdir)
  580. if _, err := os.Stat(fpath); os.IsNotExist(err) {
  581. return "", err
  582. }
  583. return fpath, nil
  584. }