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.

serv.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package private
  4. import (
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. asymkey_model "code.gitea.io/gitea/models/asymkey"
  9. "code.gitea.io/gitea/models/perm"
  10. access_model "code.gitea.io/gitea/models/perm/access"
  11. repo_model "code.gitea.io/gitea/models/repo"
  12. "code.gitea.io/gitea/models/unit"
  13. user_model "code.gitea.io/gitea/models/user"
  14. "code.gitea.io/gitea/modules/git"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/private"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/services/context"
  19. repo_service "code.gitea.io/gitea/services/repository"
  20. wiki_service "code.gitea.io/gitea/services/wiki"
  21. )
  22. // ServNoCommand returns information about the provided keyid
  23. func ServNoCommand(ctx *context.PrivateContext) {
  24. keyID := ctx.ParamsInt64(":keyid")
  25. if keyID <= 0 {
  26. ctx.JSON(http.StatusBadRequest, private.Response{
  27. UserMsg: fmt.Sprintf("Bad key id: %d", keyID),
  28. })
  29. }
  30. results := private.KeyAndOwner{}
  31. key, err := asymkey_model.GetPublicKeyByID(ctx, keyID)
  32. if err != nil {
  33. if asymkey_model.IsErrKeyNotExist(err) {
  34. ctx.JSON(http.StatusUnauthorized, private.Response{
  35. UserMsg: fmt.Sprintf("Cannot find key: %d", keyID),
  36. })
  37. return
  38. }
  39. log.Error("Unable to get public key: %d Error: %v", keyID, err)
  40. ctx.JSON(http.StatusInternalServerError, private.Response{
  41. Err: err.Error(),
  42. })
  43. return
  44. }
  45. results.Key = key
  46. if key.Type == asymkey_model.KeyTypeUser || key.Type == asymkey_model.KeyTypePrincipal {
  47. user, err := user_model.GetUserByID(ctx, key.OwnerID)
  48. if err != nil {
  49. if user_model.IsErrUserNotExist(err) {
  50. ctx.JSON(http.StatusUnauthorized, private.Response{
  51. UserMsg: fmt.Sprintf("Cannot find owner with id: %d for key: %d", key.OwnerID, keyID),
  52. })
  53. return
  54. }
  55. log.Error("Unable to get owner with id: %d for public key: %d Error: %v", key.OwnerID, keyID, err)
  56. ctx.JSON(http.StatusInternalServerError, private.Response{
  57. Err: err.Error(),
  58. })
  59. return
  60. }
  61. if !user.IsActive || user.ProhibitLogin {
  62. ctx.JSON(http.StatusForbidden, private.Response{
  63. UserMsg: "Your account is disabled.",
  64. })
  65. return
  66. }
  67. results.Owner = user
  68. }
  69. ctx.JSON(http.StatusOK, &results)
  70. }
  71. // ServCommand returns information about the provided keyid
  72. func ServCommand(ctx *context.PrivateContext) {
  73. keyID := ctx.ParamsInt64(":keyid")
  74. ownerName := ctx.Params(":owner")
  75. repoName := ctx.Params(":repo")
  76. mode := perm.AccessMode(ctx.FormInt("mode"))
  77. // Set the basic parts of the results to return
  78. results := private.ServCommandResults{
  79. RepoName: repoName,
  80. OwnerName: ownerName,
  81. KeyID: keyID,
  82. }
  83. // Now because we're not translating things properly let's just default some English strings here
  84. modeString := "read"
  85. if mode > perm.AccessModeRead {
  86. modeString = "write to"
  87. }
  88. // The default unit we're trying to look at is code
  89. unitType := unit.TypeCode
  90. // Unless we're a wiki...
  91. if strings.HasSuffix(repoName, ".wiki") {
  92. // in which case we need to look at the wiki
  93. unitType = unit.TypeWiki
  94. // And we'd better munge the reponame and tell downstream we're looking at a wiki
  95. results.IsWiki = true
  96. results.RepoName = repoName[:len(repoName)-5]
  97. }
  98. owner, err := user_model.GetUserByName(ctx, results.OwnerName)
  99. if err != nil {
  100. if user_model.IsErrUserNotExist(err) {
  101. // User is fetching/cloning a non-existent repository
  102. log.Warn("Failed authentication attempt (cannot find repository: %s/%s) from %s", results.OwnerName, results.RepoName, ctx.RemoteAddr())
  103. ctx.JSON(http.StatusNotFound, private.Response{
  104. UserMsg: fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
  105. })
  106. return
  107. }
  108. log.Error("Unable to get repository owner: %s/%s Error: %v", results.OwnerName, results.RepoName, err)
  109. ctx.JSON(http.StatusForbidden, private.Response{
  110. UserMsg: fmt.Sprintf("Unable to get repository owner: %s/%s %v", results.OwnerName, results.RepoName, err),
  111. })
  112. return
  113. }
  114. if !owner.IsOrganization() && !owner.IsActive {
  115. ctx.JSON(http.StatusForbidden, private.Response{
  116. UserMsg: "Repository cannot be accessed, you could retry it later",
  117. })
  118. return
  119. }
  120. // Now get the Repository and set the results section
  121. repoExist := true
  122. repo, err := repo_model.GetRepositoryByName(ctx, owner.ID, results.RepoName)
  123. if err != nil {
  124. if repo_model.IsErrRepoNotExist(err) {
  125. repoExist = false
  126. for _, verb := range ctx.FormStrings("verb") {
  127. if verb == "git-upload-pack" {
  128. // User is fetching/cloning a non-existent repository
  129. log.Warn("Failed authentication attempt (cannot find repository: %s/%s) from %s", results.OwnerName, results.RepoName, ctx.RemoteAddr())
  130. ctx.JSON(http.StatusNotFound, private.Response{
  131. UserMsg: fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
  132. })
  133. return
  134. }
  135. }
  136. } else {
  137. log.Error("Unable to get repository: %s/%s Error: %v", results.OwnerName, results.RepoName, err)
  138. ctx.JSON(http.StatusInternalServerError, private.Response{
  139. Err: fmt.Sprintf("Unable to get repository: %s/%s %v", results.OwnerName, results.RepoName, err),
  140. })
  141. return
  142. }
  143. }
  144. if repoExist {
  145. repo.Owner = owner
  146. repo.OwnerName = ownerName
  147. results.RepoID = repo.ID
  148. if repo.IsBeingCreated() {
  149. ctx.JSON(http.StatusInternalServerError, private.Response{
  150. Err: "Repository is being created, you could retry after it finished",
  151. })
  152. return
  153. }
  154. if repo.IsBroken() {
  155. ctx.JSON(http.StatusInternalServerError, private.Response{
  156. Err: "Repository is in a broken state",
  157. })
  158. return
  159. }
  160. // We can shortcut at this point if the repo is a mirror
  161. if mode > perm.AccessModeRead && repo.IsMirror {
  162. ctx.JSON(http.StatusForbidden, private.Response{
  163. UserMsg: fmt.Sprintf("Mirror Repository %s/%s is read-only", results.OwnerName, results.RepoName),
  164. })
  165. return
  166. }
  167. }
  168. // Get the Public Key represented by the keyID
  169. key, err := asymkey_model.GetPublicKeyByID(ctx, keyID)
  170. if err != nil {
  171. if asymkey_model.IsErrKeyNotExist(err) {
  172. ctx.JSON(http.StatusNotFound, private.Response{
  173. UserMsg: fmt.Sprintf("Cannot find key: %d", keyID),
  174. })
  175. return
  176. }
  177. log.Error("Unable to get public key: %d Error: %v", keyID, err)
  178. ctx.JSON(http.StatusInternalServerError, private.Response{
  179. Err: fmt.Sprintf("Unable to get key: %d Error: %v", keyID, err),
  180. })
  181. return
  182. }
  183. results.KeyName = key.Name
  184. results.KeyID = key.ID
  185. results.UserID = key.OwnerID
  186. // If repo doesn't exist, deploy key doesn't make sense
  187. if !repoExist && key.Type == asymkey_model.KeyTypeDeploy {
  188. ctx.JSON(http.StatusNotFound, private.Response{
  189. UserMsg: fmt.Sprintf("Cannot find repository %s/%s", results.OwnerName, results.RepoName),
  190. })
  191. return
  192. }
  193. // Deploy Keys have ownerID set to 0 therefore we can't use the owner
  194. // So now we need to check if the key is a deploy key
  195. // We'll keep hold of the deploy key here for permissions checking
  196. var deployKey *asymkey_model.DeployKey
  197. var user *user_model.User
  198. if key.Type == asymkey_model.KeyTypeDeploy {
  199. var err error
  200. deployKey, err = asymkey_model.GetDeployKeyByRepo(ctx, key.ID, repo.ID)
  201. if err != nil {
  202. if asymkey_model.IsErrDeployKeyNotExist(err) {
  203. ctx.JSON(http.StatusNotFound, private.Response{
  204. UserMsg: fmt.Sprintf("Public (Deploy) Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
  205. })
  206. return
  207. }
  208. log.Error("Unable to get deploy for public (deploy) key: %d in %-v Error: %v", key.ID, repo, err)
  209. ctx.JSON(http.StatusInternalServerError, private.Response{
  210. Err: fmt.Sprintf("Unable to get Deploy Key for Public Key: %d:%s in %s/%s.", key.ID, key.Name, results.OwnerName, results.RepoName),
  211. })
  212. return
  213. }
  214. results.DeployKeyID = deployKey.ID
  215. results.KeyName = deployKey.Name
  216. // FIXME: Deploy keys aren't really the owner of the repo pushing changes
  217. // however we don't have good way of representing deploy keys in hook.go
  218. // so for now use the owner of the repository
  219. results.UserName = results.OwnerName
  220. results.UserID = repo.OwnerID
  221. if !repo.Owner.KeepEmailPrivate {
  222. results.UserEmail = repo.Owner.Email
  223. }
  224. } else {
  225. // Get the user represented by the Key
  226. var err error
  227. user, err = user_model.GetUserByID(ctx, key.OwnerID)
  228. if err != nil {
  229. if user_model.IsErrUserNotExist(err) {
  230. ctx.JSON(http.StatusUnauthorized, private.Response{
  231. UserMsg: fmt.Sprintf("Public Key: %d:%s owner %d does not exist.", key.ID, key.Name, key.OwnerID),
  232. })
  233. return
  234. }
  235. log.Error("Unable to get owner: %d for public key: %d:%s Error: %v", key.OwnerID, key.ID, key.Name, err)
  236. ctx.JSON(http.StatusInternalServerError, private.Response{
  237. Err: fmt.Sprintf("Unable to get Owner: %d for Deploy Key: %d:%s in %s/%s.", key.OwnerID, key.ID, key.Name, ownerName, repoName),
  238. })
  239. return
  240. }
  241. if !user.IsActive || user.ProhibitLogin {
  242. ctx.JSON(http.StatusForbidden, private.Response{
  243. UserMsg: "Your account is disabled.",
  244. })
  245. return
  246. }
  247. results.UserName = user.Name
  248. if !user.KeepEmailPrivate {
  249. results.UserEmail = user.Email
  250. }
  251. }
  252. // Don't allow pushing if the repo is archived
  253. if repoExist && mode > perm.AccessModeRead && repo.IsArchived {
  254. ctx.JSON(http.StatusUnauthorized, private.Response{
  255. UserMsg: fmt.Sprintf("Repo: %s/%s is archived.", results.OwnerName, results.RepoName),
  256. })
  257. return
  258. }
  259. // Permissions checking:
  260. if repoExist &&
  261. (mode > perm.AccessModeRead ||
  262. repo.IsPrivate ||
  263. owner.Visibility.IsPrivate() ||
  264. (user != nil && user.IsRestricted) || // user will be nil if the key is a deploykey
  265. setting.Service.RequireSignInView) {
  266. if key.Type == asymkey_model.KeyTypeDeploy {
  267. if deployKey.Mode < mode {
  268. ctx.JSON(http.StatusUnauthorized, private.Response{
  269. UserMsg: fmt.Sprintf("Deploy Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
  270. })
  271. return
  272. }
  273. } else {
  274. // Because of the special ref "refs/for" we will need to delay write permission check
  275. if git.DefaultFeatures().SupportProcReceive && unitType == unit.TypeCode {
  276. mode = perm.AccessModeRead
  277. }
  278. perm, err := access_model.GetUserRepoPermission(ctx, repo, user)
  279. if err != nil {
  280. log.Error("Unable to get permissions for %-v with key %d in %-v Error: %v", user, key.ID, repo, err)
  281. ctx.JSON(http.StatusInternalServerError, private.Response{
  282. Err: fmt.Sprintf("Unable to get permissions for user %d:%s with key %d in %s/%s Error: %v", user.ID, user.Name, key.ID, results.OwnerName, results.RepoName, err),
  283. })
  284. return
  285. }
  286. userMode := perm.UnitAccessMode(unitType)
  287. if userMode < mode {
  288. log.Warn("Failed authentication attempt for %s with key %s (not authorized to %s %s/%s) from %s", user.Name, key.Name, modeString, ownerName, repoName, ctx.RemoteAddr())
  289. ctx.JSON(http.StatusUnauthorized, private.Response{
  290. UserMsg: fmt.Sprintf("User: %d:%s with Key: %d:%s is not authorized to %s %s/%s.", user.ID, user.Name, key.ID, key.Name, modeString, ownerName, repoName),
  291. })
  292. return
  293. }
  294. }
  295. }
  296. // We already know we aren't using a deploy key
  297. if !repoExist {
  298. owner, err := user_model.GetUserByName(ctx, ownerName)
  299. if err != nil {
  300. ctx.JSON(http.StatusInternalServerError, private.Response{
  301. Err: fmt.Sprintf("Unable to get owner: %s %v", results.OwnerName, err),
  302. })
  303. return
  304. }
  305. if owner.IsOrganization() && !setting.Repository.EnablePushCreateOrg {
  306. ctx.JSON(http.StatusForbidden, private.Response{
  307. UserMsg: "Push to create is not enabled for organizations.",
  308. })
  309. return
  310. }
  311. if !owner.IsOrganization() && !setting.Repository.EnablePushCreateUser {
  312. ctx.JSON(http.StatusForbidden, private.Response{
  313. UserMsg: "Push to create is not enabled for users.",
  314. })
  315. return
  316. }
  317. repo, err = repo_service.PushCreateRepo(ctx, user, owner, results.RepoName)
  318. if err != nil {
  319. log.Error("pushCreateRepo: %v", err)
  320. ctx.JSON(http.StatusNotFound, private.Response{
  321. UserMsg: fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
  322. })
  323. return
  324. }
  325. results.RepoID = repo.ID
  326. }
  327. if results.IsWiki {
  328. // Ensure the wiki is enabled before we allow access to it
  329. if _, err := repo.GetUnit(ctx, unit.TypeWiki); err != nil {
  330. if repo_model.IsErrUnitTypeNotExist(err) {
  331. ctx.JSON(http.StatusForbidden, private.Response{
  332. UserMsg: "repository wiki is disabled",
  333. })
  334. return
  335. }
  336. log.Error("Failed to get the wiki unit in %-v Error: %v", repo, err)
  337. ctx.JSON(http.StatusInternalServerError, private.Response{
  338. Err: fmt.Sprintf("Failed to get the wiki unit in %s/%s Error: %v", ownerName, repoName, err),
  339. })
  340. return
  341. }
  342. // Finally if we're trying to touch the wiki we should init it
  343. if err = wiki_service.InitWiki(ctx, repo); err != nil {
  344. log.Error("Failed to initialize the wiki in %-v Error: %v", repo, err)
  345. ctx.JSON(http.StatusInternalServerError, private.Response{
  346. Err: fmt.Sprintf("Failed to initialize the wiki in %s/%s Error: %v", ownerName, repoName, err),
  347. })
  348. return
  349. }
  350. }
  351. log.Debug("Serv Results:\nIsWiki: %t\nDeployKeyID: %d\nKeyID: %d\tKeyName: %s\nUserName: %s\nUserID: %d\nOwnerName: %s\nRepoName: %s\nRepoID: %d",
  352. results.IsWiki,
  353. results.DeployKeyID,
  354. results.KeyID,
  355. results.KeyName,
  356. results.UserName,
  357. results.UserID,
  358. results.OwnerName,
  359. results.RepoName,
  360. results.RepoID)
  361. ctx.JSON(http.StatusOK, results)
  362. // We will update the keys in a different call.
  363. }