您最多选择25个标签 标签必须以中文、字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

11 年前
11 年前
11 年前
11 年前
11 年前
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
6 年前
11 年前
10 年前
11 年前
11 年前
11 年前
11 年前
10 年前
10 年前
10 年前
11 年前
11 年前
11 年前
11 年前
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
6 年前
11 年前
11 年前
10 年前
11 年前
11 年前
11 年前
11 年前
11 年前
10 年前
10 年前
10 年前
10 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
11 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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/timeutil"
  30. repo_service "code.gitea.io/gitea/services/repository"
  31. )
  32. // HTTP implmentation git smart HTTP protocol
  33. func HTTP(ctx *context.Context) {
  34. if len(setting.Repository.AccessControlAllowOrigin) > 0 {
  35. allowedOrigin := setting.Repository.AccessControlAllowOrigin
  36. // Set CORS headers for browser-based git clients
  37. ctx.Resp.Header().Set("Access-Control-Allow-Origin", allowedOrigin)
  38. ctx.Resp.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, User-Agent")
  39. // Handle preflight OPTIONS request
  40. if ctx.Req.Method == "OPTIONS" {
  41. if allowedOrigin == "*" {
  42. ctx.Status(http.StatusOK)
  43. } else if allowedOrigin == "null" {
  44. ctx.Status(http.StatusForbidden)
  45. } else {
  46. origin := ctx.Req.Header.Get("Origin")
  47. if len(origin) > 0 && origin == allowedOrigin {
  48. ctx.Status(http.StatusOK)
  49. } else {
  50. ctx.Status(http.StatusForbidden)
  51. }
  52. }
  53. return
  54. }
  55. }
  56. username := ctx.Params(":username")
  57. reponame := strings.TrimSuffix(ctx.Params(":reponame"), ".git")
  58. if ctx.Query("go-get") == "1" {
  59. context.EarlyResponseForGoGetMeta(ctx)
  60. return
  61. }
  62. var isPull, receivePack bool
  63. service := ctx.Query("service")
  64. if service == "git-receive-pack" ||
  65. strings.HasSuffix(ctx.Req.URL.Path, "git-receive-pack") {
  66. isPull = false
  67. receivePack = true
  68. } else if service == "git-upload-pack" ||
  69. strings.HasSuffix(ctx.Req.URL.Path, "git-upload-pack") {
  70. isPull = true
  71. } else if service == "git-upload-archive" ||
  72. strings.HasSuffix(ctx.Req.URL.Path, "git-upload-archive") {
  73. isPull = true
  74. } else {
  75. isPull = (ctx.Req.Method == "GET")
  76. }
  77. var accessMode models.AccessMode
  78. if isPull {
  79. accessMode = models.AccessModeRead
  80. } else {
  81. accessMode = models.AccessModeWrite
  82. }
  83. isWiki := false
  84. var unitType = models.UnitTypeCode
  85. if strings.HasSuffix(reponame, ".wiki") {
  86. isWiki = true
  87. unitType = models.UnitTypeWiki
  88. reponame = reponame[:len(reponame)-5]
  89. }
  90. owner, err := models.GetUserByName(username)
  91. if err != nil {
  92. ctx.NotFoundOrServerError("GetUserByName", models.IsErrUserNotExist, err)
  93. return
  94. }
  95. repoExist := true
  96. repo, err := models.GetRepositoryByName(owner.ID, reponame)
  97. if err != nil {
  98. if models.IsErrRepoNotExist(err) {
  99. if redirectRepoID, err := models.LookupRepoRedirect(owner.ID, reponame); err == nil {
  100. context.RedirectToRepo(ctx, redirectRepoID)
  101. return
  102. }
  103. repoExist = false
  104. } else {
  105. ctx.ServerError("GetRepositoryByName", err)
  106. return
  107. }
  108. }
  109. // Don't allow pushing if the repo is archived
  110. if repoExist && repo.IsArchived && !isPull {
  111. ctx.HandleText(http.StatusForbidden, "This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.")
  112. return
  113. }
  114. // Only public pull don't need auth.
  115. isPublicPull := repoExist && !repo.IsPrivate && isPull
  116. var (
  117. askAuth = !isPublicPull || setting.Service.RequireSignInView
  118. authUser *models.User
  119. authUsername string
  120. authPasswd string
  121. environ []string
  122. )
  123. // check access
  124. if askAuth {
  125. authUsername = ctx.Req.Header.Get(setting.ReverseProxyAuthUser)
  126. if setting.Service.EnableReverseProxyAuth && len(authUsername) > 0 {
  127. authUser, err = models.GetUserByName(authUsername)
  128. if err != nil {
  129. ctx.HandleText(401, "reverse proxy login error, got error while running GetUserByName")
  130. return
  131. }
  132. } else {
  133. authHead := ctx.Req.Header.Get("Authorization")
  134. if len(authHead) == 0 {
  135. ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=\".\"")
  136. ctx.Error(http.StatusUnauthorized)
  137. return
  138. }
  139. auths := strings.Fields(authHead)
  140. // currently check basic auth
  141. // TODO: support digit auth
  142. // FIXME: middlewares/context.go did basic auth check already,
  143. // maybe could use that one.
  144. if len(auths) != 2 || auths[0] != "Basic" {
  145. ctx.HandleText(http.StatusUnauthorized, "no basic auth and digit auth")
  146. return
  147. }
  148. authUsername, authPasswd, err = base.BasicAuthDecode(auths[1])
  149. if err != nil {
  150. ctx.HandleText(http.StatusUnauthorized, "no basic auth and digit auth")
  151. return
  152. }
  153. // Check if username or password is a token
  154. isUsernameToken := len(authPasswd) == 0 || authPasswd == "x-oauth-basic"
  155. // Assume username is token
  156. authToken := authUsername
  157. if !isUsernameToken {
  158. // Assume password is token
  159. authToken = authPasswd
  160. }
  161. uid := sso.CheckOAuthAccessToken(authToken)
  162. if uid != 0 {
  163. ctx.Data["IsApiToken"] = true
  164. authUser, err = models.GetUserByID(uid)
  165. if err != nil {
  166. ctx.ServerError("GetUserByID", err)
  167. return
  168. }
  169. }
  170. // Assume password is a token.
  171. token, err := models.GetAccessTokenBySHA(authToken)
  172. if err == nil {
  173. authUser, err = models.GetUserByID(token.UID)
  174. if err != nil {
  175. ctx.ServerError("GetUserByID", err)
  176. return
  177. }
  178. token.UpdatedUnix = timeutil.TimeStampNow()
  179. if err = models.UpdateAccessToken(token); err != nil {
  180. ctx.ServerError("UpdateAccessToken", err)
  181. }
  182. } else if !models.IsErrAccessTokenNotExist(err) && !models.IsErrAccessTokenEmpty(err) {
  183. log.Error("GetAccessTokenBySha: %v", err)
  184. }
  185. if authUser == nil {
  186. // Check username and password
  187. authUser, err = models.UserSignIn(authUsername, authPasswd)
  188. if err != nil {
  189. if models.IsErrUserProhibitLogin(err) {
  190. ctx.HandleText(http.StatusForbidden, "User is not permitted to login")
  191. return
  192. } else if !models.IsErrUserNotExist(err) {
  193. ctx.ServerError("UserSignIn error: %v", err)
  194. return
  195. }
  196. }
  197. if authUser == nil {
  198. ctx.HandleText(http.StatusUnauthorized, fmt.Sprintf("invalid credentials from %s", ctx.RemoteAddr()))
  199. return
  200. }
  201. _, err = models.GetTwoFactorByUID(authUser.ID)
  202. if err == nil {
  203. // 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
  204. 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")
  205. return
  206. } else if !models.IsErrTwoFactorNotEnrolled(err) {
  207. ctx.ServerError("IsErrTwoFactorNotEnrolled", err)
  208. return
  209. }
  210. }
  211. }
  212. if repoExist {
  213. perm, err := models.GetUserRepoPermission(repo, authUser)
  214. if err != nil {
  215. ctx.ServerError("GetUserRepoPermission", err)
  216. return
  217. }
  218. if !perm.CanAccess(accessMode, unitType) {
  219. ctx.HandleText(http.StatusForbidden, "User permission denied")
  220. return
  221. }
  222. if !isPull && repo.IsMirror {
  223. ctx.HandleText(http.StatusForbidden, "mirror repository is read-only")
  224. return
  225. }
  226. }
  227. environ = []string{
  228. models.EnvRepoUsername + "=" + username,
  229. models.EnvRepoName + "=" + reponame,
  230. models.EnvPusherName + "=" + authUser.Name,
  231. models.EnvPusherID + fmt.Sprintf("=%d", authUser.ID),
  232. models.EnvIsDeployKey + "=false",
  233. models.EnvRepoSize + "=" + fmt.Sprint(repo.Size),
  234. models.EnvRepoMaxFileSize + "=" + fmt.Sprint(setting.Repository.Upload.FileMaxSize),
  235. models.EnvRepoMaxSize + "=" + fmt.Sprint(setting.Repository.RepoMaxSize),
  236. models.EnvPushSizeCheckFlag + "=" + fmt.Sprint(setting.Repository.Upload.ShellFlag),
  237. }
  238. if !authUser.KeepEmailPrivate {
  239. environ = append(environ, models.EnvPusherEmail+"="+authUser.Email)
  240. }
  241. if isWiki {
  242. environ = append(environ, models.EnvRepoIsWiki+"=true")
  243. } else {
  244. environ = append(environ, models.EnvRepoIsWiki+"=false")
  245. }
  246. }
  247. if !repoExist {
  248. if !receivePack {
  249. ctx.HandleText(http.StatusNotFound, "Repository not found")
  250. return
  251. }
  252. if owner.IsOrganization() && !setting.Repository.EnablePushCreateOrg {
  253. ctx.HandleText(http.StatusForbidden, "Push to create is not enabled for organizations.")
  254. return
  255. }
  256. if !owner.IsOrganization() && !setting.Repository.EnablePushCreateUser {
  257. ctx.HandleText(http.StatusForbidden, "Push to create is not enabled for users.")
  258. return
  259. }
  260. // Return dummy payload if GET receive-pack
  261. if ctx.Req.Method == http.MethodGet {
  262. dummyInfoRefs(ctx)
  263. return
  264. }
  265. repo, err = repo_service.PushCreateRepo(authUser, owner, reponame)
  266. if err != nil {
  267. log.Error("pushCreateRepo: %v", err)
  268. ctx.Status(http.StatusNotFound)
  269. return
  270. }
  271. }
  272. if isWiki {
  273. // Ensure the wiki is enabled before we allow access to it
  274. if _, err := repo.GetUnit(models.UnitTypeWiki); err != nil {
  275. if models.IsErrUnitTypeNotExist(err) {
  276. ctx.HandleText(http.StatusForbidden, "repository wiki is disabled")
  277. return
  278. }
  279. log.Error("Failed to get the wiki unit in %-v Error: %v", repo, err)
  280. ctx.ServerError("GetUnit(UnitTypeWiki) for "+repo.FullName(), err)
  281. return
  282. }
  283. }
  284. environ = append(environ, models.ProtectedBranchRepoID+fmt.Sprintf("=%d", repo.ID))
  285. if service == "git-upload-pack" { // clone_cnt
  286. go repo.IncreaseCloneCnt()
  287. }
  288. if ctx.Req.Method == "POST" {
  289. if strings.HasSuffix(ctx.Req.URL.Path, "git-upload-pack") {
  290. go repo.IncreaseGitCloneCnt()
  291. }
  292. }
  293. w := ctx.Resp
  294. r := ctx.Req.Request
  295. cfg := &serviceConfig{
  296. UploadPack: true,
  297. ReceivePack: true,
  298. Env: environ,
  299. }
  300. for _, route := range routes {
  301. r.URL.Path = strings.ToLower(r.URL.Path) // blue: In case some repo name has upper case name
  302. if m := route.reg.FindStringSubmatch(r.URL.Path); m != nil {
  303. if setting.Repository.DisableHTTPGit {
  304. w.WriteHeader(http.StatusForbidden)
  305. _, err := w.Write([]byte("Interacting with repositories by HTTP protocol is not allowed"))
  306. if err != nil {
  307. log.Error(err.Error())
  308. }
  309. return
  310. }
  311. if route.method != r.Method {
  312. if r.Proto == "HTTP/1.1" {
  313. w.WriteHeader(http.StatusMethodNotAllowed)
  314. _, err := w.Write([]byte("Method Not Allowed"))
  315. if err != nil {
  316. log.Error(err.Error())
  317. }
  318. } else {
  319. w.WriteHeader(http.StatusBadRequest)
  320. _, err := w.Write([]byte("Bad Request"))
  321. if err != nil {
  322. log.Error(err.Error())
  323. }
  324. }
  325. return
  326. }
  327. file := strings.Replace(r.URL.Path, m[1]+"/", "", 1)
  328. dir, err := getGitRepoPath(m[1])
  329. if err != nil {
  330. log.Error(err.Error())
  331. ctx.NotFound("Smart Git HTTP", err)
  332. return
  333. }
  334. route.handler(serviceHandler{cfg, w, r, dir, file, cfg.Env})
  335. return
  336. }
  337. }
  338. ctx.NotFound("Smart Git HTTP", nil)
  339. }
  340. var (
  341. infoRefsCache []byte
  342. infoRefsOnce sync.Once
  343. )
  344. func dummyInfoRefs(ctx *context.Context) {
  345. infoRefsOnce.Do(func() {
  346. tmpDir, err := ioutil.TempDir(os.TempDir(), "gitea-info-refs-cache")
  347. if err != nil {
  348. log.Error("Failed to create temp dir for git-receive-pack cache: %v", err)
  349. return
  350. }
  351. defer func() {
  352. if err := os.RemoveAll(tmpDir); err != nil {
  353. log.Error("RemoveAll: %v", err)
  354. }
  355. }()
  356. if err := git.InitRepository(tmpDir, true); err != nil {
  357. log.Error("Failed to init bare repo for git-receive-pack cache: %v", err)
  358. return
  359. }
  360. refs, err := git.NewCommand("receive-pack", "--stateless-rpc", "--advertise-refs", ".").RunInDirBytes(tmpDir)
  361. if err != nil {
  362. log.Error(fmt.Sprintf("%v - %s", err, string(refs)))
  363. }
  364. log.Debug("populating infoRefsCache: \n%s", string(refs))
  365. infoRefsCache = refs
  366. })
  367. ctx.Header().Set("Expires", "Fri, 01 Jan 1980 00:00:00 GMT")
  368. ctx.Header().Set("Pragma", "no-cache")
  369. ctx.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate")
  370. ctx.Header().Set("Content-Type", "application/x-git-receive-pack-advertisement")
  371. _, _ = ctx.Write(packetWrite("# service=git-receive-pack\n"))
  372. _, _ = ctx.Write([]byte("0000"))
  373. _, _ = ctx.Write(infoRefsCache)
  374. }
  375. type serviceConfig struct {
  376. UploadPack bool
  377. ReceivePack bool
  378. Env []string
  379. }
  380. type serviceHandler struct {
  381. cfg *serviceConfig
  382. w http.ResponseWriter
  383. r *http.Request
  384. dir string
  385. file string
  386. environ []string
  387. }
  388. func (h *serviceHandler) setHeaderNoCache() {
  389. h.w.Header().Set("Expires", "Fri, 01 Jan 1980 00:00:00 GMT")
  390. h.w.Header().Set("Pragma", "no-cache")
  391. h.w.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate")
  392. }
  393. func (h *serviceHandler) setHeaderCacheForever() {
  394. now := time.Now().Unix()
  395. expires := now + 31536000
  396. h.w.Header().Set("Date", fmt.Sprintf("%d", now))
  397. h.w.Header().Set("Expires", fmt.Sprintf("%d", expires))
  398. h.w.Header().Set("Cache-Control", "public, max-age=31536000")
  399. }
  400. func (h *serviceHandler) sendFile(contentType string) {
  401. reqFile := path.Join(h.dir, h.file)
  402. fi, err := os.Stat(reqFile)
  403. if os.IsNotExist(err) {
  404. h.w.WriteHeader(http.StatusNotFound)
  405. return
  406. }
  407. h.w.Header().Set("Content-Type", contentType)
  408. h.w.Header().Set("Content-Length", fmt.Sprintf("%d", fi.Size()))
  409. h.w.Header().Set("Last-Modified", fi.ModTime().Format(http.TimeFormat))
  410. http.ServeFile(h.w, h.r, reqFile)
  411. }
  412. type route struct {
  413. reg *regexp.Regexp
  414. method string
  415. handler func(serviceHandler)
  416. }
  417. var routes = []route{
  418. {regexp.MustCompile(`(.*?)/git-upload-pack$`), "POST", serviceUploadPack},
  419. {regexp.MustCompile(`(.*?)/git-receive-pack$`), "POST", serviceReceivePack},
  420. {regexp.MustCompile(`(.*?)/info/refs$`), "GET", getInfoRefs},
  421. {regexp.MustCompile(`(.*?)/HEAD$`), "GET", getTextFile},
  422. {regexp.MustCompile(`(.*?)/objects/info/alternates$`), "GET", getTextFile},
  423. {regexp.MustCompile(`(.*?)/objects/info/http-alternates$`), "GET", getTextFile},
  424. {regexp.MustCompile(`(.*?)/objects/info/packs$`), "GET", getInfoPacks},
  425. {regexp.MustCompile(`(.*?)/objects/info/[^/]*$`), "GET", getTextFile},
  426. {regexp.MustCompile(`(.*?)/objects/[0-9a-f]{2}/[0-9a-f]{38}$`), "GET", getLooseObject},
  427. {regexp.MustCompile(`(.*?)/objects/pack/pack-[0-9a-f]{40}\.pack$`), "GET", getPackFile},
  428. {regexp.MustCompile(`(.*?)/objects/pack/pack-[0-9a-f]{40}\.idx$`), "GET", getIdxFile},
  429. }
  430. func getGitConfig(option, dir string) string {
  431. out, err := git.NewCommand("config", option).RunInDir(dir)
  432. if err != nil {
  433. log.Error("%v - %s", err, out)
  434. }
  435. return out[0 : len(out)-1]
  436. }
  437. func getConfigSetting(service, dir string) bool {
  438. service = strings.Replace(service, "-", "", -1)
  439. setting := getGitConfig("http."+service, dir)
  440. if service == "uploadpack" {
  441. return setting != "false"
  442. }
  443. return setting == "true"
  444. }
  445. func hasAccess(service string, h serviceHandler, checkContentType bool) bool {
  446. if checkContentType {
  447. if h.r.Header.Get("Content-Type") != fmt.Sprintf("application/x-git-%s-request", service) {
  448. return false
  449. }
  450. }
  451. if !(service == "upload-pack" || service == "receive-pack") {
  452. return false
  453. }
  454. if service == "receive-pack" {
  455. return h.cfg.ReceivePack
  456. }
  457. if service == "upload-pack" {
  458. return h.cfg.UploadPack
  459. }
  460. return getConfigSetting(service, h.dir)
  461. }
  462. func serviceRPC(h serviceHandler, service string) {
  463. defer func() {
  464. if err := h.r.Body.Close(); err != nil {
  465. log.Error("serviceRPC: Close: %v", err)
  466. }
  467. }()
  468. if !hasAccess(service, h, true) {
  469. h.w.WriteHeader(http.StatusUnauthorized)
  470. return
  471. }
  472. h.w.Header().Set("Content-Type", fmt.Sprintf("application/x-git-%s-result", service))
  473. var err error
  474. var reqBody = h.r.Body
  475. // Handle GZIP.
  476. if h.r.Header.Get("Content-Encoding") == "gzip" {
  477. reqBody, err = gzip.NewReader(reqBody)
  478. if err != nil {
  479. log.Error("Fail to create gzip reader: %v", err)
  480. h.w.WriteHeader(http.StatusInternalServerError)
  481. return
  482. }
  483. }
  484. // set this for allow pre-receive and post-receive execute
  485. h.environ = append(h.environ, "SSH_ORIGINAL_COMMAND="+service)
  486. ctx, cancel := gocontext.WithCancel(git.DefaultContext)
  487. defer cancel()
  488. var stderr bytes.Buffer
  489. cmd := exec.CommandContext(ctx, git.GitExecutable, service, "--stateless-rpc", h.dir)
  490. cmd.Dir = h.dir
  491. if service == "receive-pack" {
  492. cmd.Env = append(os.Environ(), h.environ...)
  493. }
  494. cmd.Stdout = h.w
  495. cmd.Stdin = reqBody
  496. cmd.Stderr = &stderr
  497. pid := process.GetManager().Add(fmt.Sprintf("%s %s %s [repo_path: %s]", git.GitExecutable, service, "--stateless-rpc", h.dir), cancel)
  498. defer process.GetManager().Remove(pid)
  499. if err := cmd.Run(); err != nil {
  500. log.Error("Fail to serve RPC(%s): %v - %s", service, err, stderr.String())
  501. return
  502. }
  503. }
  504. func serviceUploadPack(h serviceHandler) {
  505. serviceRPC(h, "upload-pack")
  506. }
  507. func serviceReceivePack(h serviceHandler) {
  508. serviceRPC(h, "receive-pack")
  509. }
  510. func getServiceType(r *http.Request) string {
  511. serviceType := r.FormValue("service")
  512. if !strings.HasPrefix(serviceType, "git-") {
  513. return ""
  514. }
  515. return strings.Replace(serviceType, "git-", "", 1)
  516. }
  517. func updateServerInfo(dir string) []byte {
  518. out, err := git.NewCommand("update-server-info").RunInDirBytes(dir)
  519. if err != nil {
  520. log.Error(fmt.Sprintf("%v - %s", err, string(out)))
  521. }
  522. return out
  523. }
  524. func packetWrite(str string) []byte {
  525. s := strconv.FormatInt(int64(len(str)+4), 16)
  526. if len(s)%4 != 0 {
  527. s = strings.Repeat("0", 4-len(s)%4) + s
  528. }
  529. return []byte(s + str)
  530. }
  531. func getInfoRefs(h serviceHandler) {
  532. h.setHeaderNoCache()
  533. if hasAccess(getServiceType(h.r), h, false) {
  534. service := getServiceType(h.r)
  535. refs, err := git.NewCommand(service, "--stateless-rpc", "--advertise-refs", ".").RunInDirBytes(h.dir)
  536. if err != nil {
  537. log.Error(fmt.Sprintf("%v - %s", err, string(refs)))
  538. }
  539. h.w.Header().Set("Content-Type", fmt.Sprintf("application/x-git-%s-advertisement", service))
  540. h.w.WriteHeader(http.StatusOK)
  541. _, _ = h.w.Write(packetWrite("# service=git-" + service + "\n"))
  542. _, _ = h.w.Write([]byte("0000"))
  543. _, _ = h.w.Write(refs)
  544. } else {
  545. updateServerInfo(h.dir)
  546. h.sendFile("text/plain; charset=utf-8")
  547. }
  548. }
  549. func getTextFile(h serviceHandler) {
  550. h.setHeaderNoCache()
  551. h.sendFile("text/plain")
  552. }
  553. func getInfoPacks(h serviceHandler) {
  554. h.setHeaderCacheForever()
  555. h.sendFile("text/plain; charset=utf-8")
  556. }
  557. func getLooseObject(h serviceHandler) {
  558. h.setHeaderCacheForever()
  559. h.sendFile("application/x-git-loose-object")
  560. }
  561. func getPackFile(h serviceHandler) {
  562. h.setHeaderCacheForever()
  563. h.sendFile("application/x-git-packed-objects")
  564. }
  565. func getIdxFile(h serviceHandler) {
  566. h.setHeaderCacheForever()
  567. h.sendFile("application/x-git-packed-objects-toc")
  568. }
  569. func getGitRepoPath(subdir string) (string, error) {
  570. if !strings.HasSuffix(subdir, ".git") {
  571. subdir += ".git"
  572. }
  573. fpath := path.Join(setting.RepoRootPath, subdir)
  574. if _, err := os.Stat(fpath); os.IsNotExist(err) {
  575. return "", err
  576. }
  577. return fpath, nil
  578. }