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

http.go 19 kB

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